branch: show (rebasing) or (bisecting) instead of (no branch) when possible
PATCH 1/1 branch: show (rebasing) or (bisecting) instead of (no branch) when possible
---
In the spirit of status' in-progress info. I think showing this is more useful than "(no branch)". I tend to do "git br" more often than "git st" and this catches my eyes.
builtin/branch.c | 10 +++++++++- t/t6030-bisect-porcelain.sh | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-)
builtin/branch.c +9 -1
@@ -557,7 +557,15 @@ static void show_detached(struct ref_list *ref_list)if (head_commit &&is_descendant_of (head_commit, ref_list->with_commit)) {struct ref_item item; - item.name =xstrdup (_ ("(no branch)" )); +struct stat st; +if ((!stat (git_path ("rebase-apply" ), &st) && +stat (git_path ("rebase-apply/applying" ), &st)) || + !stat (git_path ("rebase-merge" ), &st)) + item.name =xstrdup (_ ("(rebasing)" )); +else if (!stat (git_path ("BISECT_LOG" ), &st)) + item.name =xstrdup (_ ("(bisecting)" )); +else + item.name =xstrdup (_ ("(no branch)" )); item.width =utf8_strwidth (item.name); item.kind =REF_LOCAL_BRANCH ; item.dest =NULL ;
t/t6030-bisect-porcelain.sh +1 -1
@@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if cp .git/BISECT_START saved && test_must_fail git bisect start $HASH4 foo -- && git branch > branch.output && - test_i18ngrep "* (no branch)" branch.output > /dev/null && + test_i18ngrep "* (bisecting)" branch.output > /dev/null && test_cmp saved .git/BISECT_START ' test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '
Signature · 1 line
1.8.1.1.459.g5970e58
Quoted 5 lines · Nguyễn Thái Ngọc Duy
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>---
In the spirit of status' in-progress info. I think showing this is more useful than "(no branch)". I tend to do "git br" more often than "git st" and this catches my eyes.
Very nice idea. This would also have been a nice way to avoid confusion when my officemate used bisect for the first time.
Any particular reason the above explanation is after the triple-dash instead of before it?
[...]
builtin/branch.c static void show_detached(struct ref_list *ref_list) · line 563
Quoted 11 lines
builtin/branch.c +4
-1@@ -557,7 +557,15 @@ static void show_detached(struct ref_list *ref_list)if (head_commit &&is_descendant_of (head_commit, ref_list->with_commit)) {struct ref_item item; - item.name =xstrdup (_ ("(no branch)" )); +struct stat st; +if ((!stat (git_path ("rebase-apply" ), &st) && +stat (git_path ("rebase-apply/applying" ), &st)) || + !stat (git_path ("rebase-merge" ), &st))
Here's a straight translation of contrib/completion/prompt.sh for comparison, skipping the cases that don't involve automatically detaching HEAD:
if (!stat(git_path("rebase-merge"), &st) && S_ISDIR(st.st_mode))
item.name = xstrdup(_("(rebasing)"));
else if (!access(git_path("rebase-apply/rebasing"), F_OK))
item.name = xstrdup(_("(rebasing)"));
else if (!access(git_path("BISECT_LOG"), F_OK))
item.name = xstrdup(_("(bisecting)"));
else
item.name = xstrdup(_("(no branch)"));That would mean:
* using access() instead of stat() to avoid unnecessary work
* relying on rebase--am to write .git/rebase-apply/rebasing when appropriate instead of guessing
Not important, though. :)
Jonathan
PATCH v2 1/1 branch: show rebase/bisect info when possible instead of "(no branch)"
In reply to Nguyễn Thái Ngọc DuyThis prints more helpful info when HEAD is detached: is it detached because of bisect or rebase? What is the original branch name in those cases?
---
- Incorporate Jonathan's version of checking
- Show original branch name, e.g. "(rebasing foo)", when possible
builtin/branch.c | 40 +++++++++++++++++++++++++++++++++++++++- t/t6030-bisect-porcelain.sh | 2 +- 2 files changed, 40 insertions(+), 2 deletions(-)
builtin/branch.c +39 -1
@@ -550,6 +550,44 @@ static int calc_maxwidth(struct ref_list *refs)return w; } +static char *get_head_description () +{ +struct stat st; +struct strbuf sb =STRBUF_INIT ; +struct strbuf result =STRBUF_INIT ; +int bisect =0 ; +int ret; +if (!stat (git_path ("rebase-merge" ), &st) &&S_ISDIR (st.st_mode)) + ret =strbuf_read_file (&sb,git_path ("rebase-merge/head-name" ),0 ); +else if (!access (git_path ("rebase-apply/rebasing" ),F_OK )) + ret =strbuf_read_file (&sb,git_path ("rebase-apply/head-name" ),0 ); +else if (!access (git_path ("BISECT_LOG" ),F_OK )) { + ret =strbuf_read_file (&sb,git_path ("BISECT_START" ),0 ); + bisect =1 ; + }else +return xstrdup (_ ("(no branch)" )); + +if (ret <=0 ) +return xstrdup (bisect ?_ ("(bisecting)" ) :_ ("_(rebasing)" )); + +while (sb.len && sb.buf[sb.len -1 ] =='\n' ) +strbuf_setlen (&sb, sb.len -1 ); + +if (bisect) { +unsigned char sha1[20 ]; +if (!get_sha1_hex (sb.buf, sha1)) +strbuf_addstr (&result,_ ("(bisecting)" )); +else +strbuf_addf (&result,_ ("(bisecting %s)" ), sb.buf); + }else { +if (!prefixcmp (sb.buf,"refs/heads/" )) +strbuf_addf (&result,_ ("(rebasing %s)" ), sb.buf +11 ); +else +strbuf_addstr (&result,_ ("(rebasing)" )); + } +strbuf_release (&sb); +return strbuf_detach (&result,NULL ); +}static void show_detached (struct ref_list *ref_list) {
@@ -557,7 +595,7 @@ static void show_detached(struct ref_list *ref_list)if (head_commit &&is_descendant_of (head_commit, ref_list->with_commit)) {struct ref_item item; - item.name =xstrdup (_ ("(no branch)" )); + item.name =get_head_description (); item.width =utf8_strwidth (item.name); item.kind =REF_LOCAL_BRANCH ; item.dest =NULL ;
t/t6030-bisect-porcelain.sh +1 -1
@@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if cp .git/BISECT_START saved && test_must_fail git bisect start $HASH4 foo -- && git branch > branch.output && - test_i18ngrep "* (no branch)" branch.output > /dev/null && + test_i18ngrep "* (bisecting other)" branch.output > /dev/null && test_cmp saved .git/BISECT_START ' test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '
Signature · 1 line
1.8.1.1.459.g5970e58
Re: [PATCH v2] branch: show rebase/bisect info when possible instead of "(no branch)"
t/t6030-bisect-porcelain.sh test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if · line 167
Quoted 8 lines · Nguyễn Thái Ngọc Duy
t/t6030-bisect-porcelain.sh +1
-1@@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if cp .git/BISECT_START saved && test_must_fail git bisect start $HASH4 foo -- && git branch > branch.output && - test_i18ngrep "* (no branch)" branch.output > /dev/null && + test_i18ngrep "* (bisecting other)" branch.output > /dev/null &&
I'd have spelled it (no branch, bisecting other) to make it clear that we're on detached HEAD, and avoid confusing old-timers. But maybe your version is enough, I'm not sure.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Re: [PATCH v2] branch: show rebase/bisect info when possible instead of "(no branch)"
Quoted 14 lines · Matthieu Moy
t/t6030-bisect-porcelain.sh test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if · line 167
Quoted 8 lines · Nguyễn Thái Ngọc Duy
t/t6030-bisect-porcelain.sh +1
-1@@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if cp .git/BISECT_START saved && test_must_fail git bisect start $HASH4 foo -- && git branch > branch.output && - test_i18ngrep "* (no branch)" branch.output > /dev/null && + test_i18ngrep "* (bisecting other)" branch.output > /dev/null &&I'd have spelled it (no branch, bisecting other) to make it clear that we're on detached HEAD, and avoid confusing old-timers. But maybe your version is enough, I'm not sure.
Yeah, I do not think "bisecting other" alone makes much sense.
What does "other" refer to when you start your bisection at a detached head? I personally think "other" has _any_ value in that message, because "(no branch, bisecting)" gives the same amount of information, especially because "other" does not say which branch it refers to at all.
Re: [PATCH v2] branch: show rebase/bisect info when possible instead of "(no branch)"
Quoted 4 lines · Junio C Hamano
Yeah, I do not think "bisecting other" alone makes much sense.
What does "other" refer to when you start your bisection at a detached head? I personally think "other" has _any_ value in that
s/_any_/_no_/; obviously ;-)
Quoted 3 lines
message, because "(no branch, bisecting)" gives the same amount of information, especially because "other" does not say which branch it refers to at all.
Re: [PATCH v2] branch: show rebase/bisect info when possible instead of "(no branch)"
In reply to Matthieu MoyQuoted 14 lines · Matthieu Moy
t/t6030-bisect-porcelain.sh test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if · line 167
Quoted 8 lines · Nguyễn Thái Ngọc Duy
t/t6030-bisect-porcelain.sh +1
-1@@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if cp .git/BISECT_START saved && test_must_fail git bisect start $HASH4 foo -- && git branch > branch.output && - test_i18ngrep "* (no branch)" branch.output > /dev/null && + test_i18ngrep "* (bisecting other)" branch.output > /dev/null &&I'd have spelled it (no branch, bisecting other) to make it clear that we're on detached HEAD, and avoid confusing old-timers. But maybe your version is enough, I'm not sure.
If we want to make it clear, I think the standard "* (no branch)" should become
* HEAD (detached)
or non-detached case:
* HEAD -> foo
Then we could present rebase/bisect information as
* HEAD (detached, bisecting)
* HEAD (detached, rebasing)
* foo (rebasing)
I don't want to make this line too long because it would break (well, waste space in) column layout. So if we do this, no branch name added for rebase/bisect.
Signature · 1 line
Duy
Re: [PATCH v2] branch: show rebase/bisect info when possible instead of "(no branch)"
Quoted 1 line · Duy Nguyen
* foo (rebasing)
Well, this one does not make sense (or causes more confusion).
Signature · 1 line
Duy
Re: [PATCH v2] branch: show rebase/bisect info when possible instead of "(no branch)"
In reply to Junio C HamanoQuoted 12 lines · Junio C Hamano
Quoted 10 lines · Matthieu Moy
Quoted 8 lines · Nguyễn Thái Ngọc Duy
t/t6030-bisect-porcelain.sh +1
-1@@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if cp .git/BISECT_START saved && test_must_fail git bisect start $HASH4 foo -- && git branch > branch.output && - test_i18ngrep "* (no branch)" branch.output > /dev/null && + test_i18ngrep "* (bisecting other)" branch.output > /dev/null &&
[...]
Quoted 2 lines
What does "other" refer to when you start your bisection at a detached head?
If I read correctly, the branch name is shown only when the bisection was not started on a detached HEAD:
+ if (!get_sha1_hex(sb.buf, sha1))
+ strbuf_addstr(&result, _("(bisecting)"));
+ else
+ strbuf_addf(&result, _("(bisecting %s)"), sb.buf);
Quoted 4 lines
I personally think "other" has _no_ value in that message, because "(no branch, bisecting)" gives the same amount of information, especially because "other" does not say which branch it refers to at all.
Just to be sure: "other" here is not a hardcoded message "bisecting other", but an instance of "bisecting <branch-name>" with the branch being called "other".
I think this is very valuable information, and most likely the information you'd be looking for when running "git branch". You're technically on detached HEAD, but morally on some branch, the HEAD being detached only because a bisect is in progress.
Signature · 2 lines
Matthieu Moy http://www-verimag.imag.fr/~moy/
Re: [PATCH v2] branch: show rebase/bisect info when possible instead of "(no branch)"
Quoted 3 lines · Matthieu Moy
Just to be sure: "other" here is not a hardcoded message "bisecting other", but an instance of "bisecting <branch-name>" with the branch being called "other".
OK, then I do not have any objection. Thanks.
PATCH v3 1/1 branch: show rebase/bisect info when possible instead of "(no branch)"
In reply to Nguyễn Thái Ngọc DuyThis prints more helpful info when HEAD is detached: is it detached because of bisect or rebase? What is the original branch name in those cases?
---
Keep "no branch" in all cases. Just append "rebasing/bisecting [%s]" when applicable.
builtin/branch.c | 44 +++++++++++++++++++++++++++++++++++++++++++- t/t6030-bisect-porcelain.sh | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-)
builtin/branch.c +43 -1
@@ -550,6 +550,48 @@ static int calc_maxwidth(struct ref_list *refs)return w; } +static char *get_head_description () +{ +struct stat st; +struct strbuf sb =STRBUF_INIT ; +struct strbuf result =STRBUF_INIT ; +int bisect =0 ; +int ret; +if (!stat (git_path ("rebase-merge" ), &st) &&S_ISDIR (st.st_mode)) + ret =strbuf_read_file (&sb,git_path ("rebase-merge/head-name" ),0 ); +else if (!access (git_path ("rebase-apply/rebasing" ),F_OK )) + ret =strbuf_read_file (&sb,git_path ("rebase-apply/head-name" ),0 ); +else if (!access (git_path ("BISECT_LOG" ),F_OK )) { + ret =strbuf_read_file (&sb,git_path ("BISECT_START" ),0 ); + bisect =1 ; + }else +return xstrdup (_ ("(no branch)" )); + +if (ret <=0 ) { +if (bisect) +return xstrdup (_ ("(no branch, bisecting)" )); +else +return xstrdup (_ ("_(no branch, rebasing)" )); + } + +while (sb.len && sb.buf[sb.len -1 ] =='\n' ) +strbuf_setlen (&sb, sb.len -1 ); + +if (bisect) { +unsigned char sha1[20 ]; +if (!get_sha1_hex (sb.buf, sha1)) +strbuf_addstr (&result,_ ("(no branch, bisecting)" )); +else +strbuf_addf (&result,_ ("(no branch, bisecting %s)" ), sb.buf); + }else { +if (!prefixcmp (sb.buf,"refs/heads/" )) +strbuf_addf (&result,_ ("(no branch, rebasing %s)" ), sb.buf +11 ); +else +strbuf_addstr (&result,_ ("(no branch, rebasing)" )); + } +strbuf_release (&sb); +return strbuf_detach (&result,NULL ); +}static void show_detached (struct ref_list *ref_list) {
@@ -557,7 +599,7 @@ static void show_detached(struct ref_list *ref_list)if (head_commit &&is_descendant_of (head_commit, ref_list->with_commit)) {struct ref_item item; - item.name =xstrdup (_ ("(no branch)" )); + item.name =get_head_description (); item.width =utf8_strwidth (item.name); item.kind =REF_LOCAL_BRANCH ; item.dest =NULL ;
t/t6030-bisect-porcelain.sh +1 -1
@@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if cp .git/BISECT_START saved && test_must_fail git bisect start $HASH4 foo -- && git branch > branch.output && - test_i18ngrep "* (no branch)" branch.output > /dev/null && + test_i18ngrep "* (no branch, bisecting other)" branch.output > /dev/null && test_cmp saved .git/BISECT_START ' test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '
Signature · 1 line
1.8.1.2.536.gf441e6d
Re: [PATCH v3] branch: show rebase/bisect info when possible instead of "(no branch)"
Quoted 29 lines · Nguyễn Thái Ngọc Duy
This prints more helpful info when HEAD is detached: is it detached because of bisect or rebase? What is the original branch name in those cases?
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>---
Keep "no branch" in all cases. Just append "rebasing/bisecting [%s]" when applicable.builtin/branch.c | 44 +++++++++++++++++++++++++++++++++++++++++++- t/t6030-bisect-porcelain.sh | 2 +- 2 files changed, 44 insertions(+), 2 deletions(-)builtin/branch.c +8
-0@@ -550,6 +550,48 @@ static int calc_maxwidth(struct ref_list *refs)return w; } +static char *get_head_description () +{ +struct stat st; +struct strbuf sb =STRBUF_INIT ; +struct strbuf result =STRBUF_INIT ; +int bisect =0 ; +int ret; +if (!stat (git_path ("rebase-merge" ), &st) &&S_ISDIR (st.st_mode))
builtin/branch.c static int calc_maxwidth(struct ref_list *refs) · line 570
Quoted 10 lines
builtin/branch.c +10
-0+ ret =strbuf_read_file (&sb,git_path ("rebase-merge/head-name" ),0 ); + else if (!access(git_path("rebase-apply/rebasing"), F_OK)) + ret =strbuf_read_file (&sb,git_path ("rebase-apply/head-name" ),0 ); + else if (!access(git_path("BISECT_LOG"), F_OK)) { + ret =strbuf_read_file (&sb,git_path ("BISECT_START" ),0 ); + bisect =1 ; + } else + return xstrdup(_("(no branch)")); + +if (ret <=0 ) {
Doesn't the general "negative signals an error" apply here?
The end result may be the same, as the later part of this function that uses sb with len==0 ends up showing the same "bisecting" (or "rebasing") without any other information, but the logic to reach that outcome looks wrong.
Quoted 14 lines
builtin/branch.c +14
-0+if (bisect) +return xstrdup (_ ("(no branch, bisecting)" )); +else +return xstrdup (_ ("_(no branch, rebasing)" )); + } + +while (sb.len && sb.buf[sb.len -1 ] =='\n' ) +strbuf_setlen (&sb, sb.len -1 ); + +if (bisect) { +unsigned char sha1[20 ]; +if (!get_sha1_hex (sb.buf, sha1)) +strbuf_addstr (&result,_ ("(no branch, bisecting)" )); +else
builtin/branch.c static int calc_maxwidth(struct ref_list *refs) · line 594
Quoted 10 lines
builtin/branch.c +10
-0+strbuf_addf (&result,_ ("(no branch, bisecting %s)" ), sb.buf); + }else { +if (!prefixcmp (sb.buf,"refs/heads/" )) +strbuf_addf (&result,_ ("(no branch, rebasing %s)" ), sb.buf +11 ); +else +strbuf_addstr (&result,_ ("(no branch, rebasing)" )); + } +strbuf_release (&sb); +return strbuf_detach (&result,NULL ); +}
We may want to refactor wt_status_print_state() and its callee a bit so that it and this part can share the logic without duplication and risk implementing subtly different decision. wt_status used to have clean separation between collection phase and presentation phase, but the wall between the phases seems deteriorated over time as more "in progress" crufts have been piled on top.
Such a refactoring may look larger than necessary, but on the other hand, I do not see this feature very useful if it can over time drift away from what we will see in "git status", so...
Quoted 25 lines
builtin/branch.c +1
-1static void show_detached (struct ref_list *ref_list) {@@ -557,7 +599,7 @@ static void show_detached(struct ref_list *ref_list)if (head_commit &&is_descendant_of (head_commit, ref_list->with_commit)) {struct ref_item item; - item.name =xstrdup (_ ("(no branch)" )); + item.name =get_head_description (); item.width =utf8_strwidth (item.name); item.kind =REF_LOCAL_BRANCH ; item.dest =NULL ;t/t6030-bisect-porcelain.sh +1
-1@@ -164,7 +164,7 @@ test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if cp .git/BISECT_START saved && test_must_fail git bisect start $HASH4 foo -- && git branch > branch.output && - test_i18ngrep "* (no branch)" branch.output > /dev/null && + test_i18ngrep "* (no branch, bisecting other)" branch.output > /dev/null && test_cmp saved .git/BISECT_START ' test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '
Re: [PATCH v3] branch: show rebase/bisect info when possible instead of "(no branch)"
In reply to Nguyễn Thái Ngọc Duybuiltin/branch.c static int calc_maxwidth(struct ref_list *refs) · line 561
Quoted 9 lines · Nguyễn Thái Ngọc Duy
builtin/branch.c +9
-0+static char *get_head_description () +{ +struct stat st; +struct strbuf sb =STRBUF_INIT ; +struct strbuf result =STRBUF_INIT ; +int bisect =0 ; +int ret; +if (!stat (git_path ("rebase-merge" ), &st) &&S_ISDIR (st.st_mode)) + ret =strbuf_read_file (&sb,git_path ("rebase-merge/head-name" ),0 );
Hrmph. Why isn't this checking if the file exists and then read it, i.e.
if (access(git_path("rebase-merge/head-name"), F_OK))
ret = strbuf_read_file(&sb, git_path("rebase-merge/head-name"), 0);It is not like you are creating this file and making sure leading directories exist, so the sequence looks a bit strange.
builtin/branch.c static int calc_maxwidth(struct ref_list *refs) · line 566
Quoted 5 lines
builtin/branch.c +5
-0+else if (!access (git_path ("rebase-apply/rebasing" ),F_OK )) + ret =strbuf_read_file (&sb,git_path ("rebase-apply/head-name" ),0 ); +else if (!access (git_path ("BISECT_LOG" ),F_OK )) { + ret =strbuf_read_file (&sb,git_path ("BISECT_START" ),0 ); + bisect =1 ;
And if the answer to the above question is "because if rebase-merge/ exists, with or without head-name, we know we are not bisecting", then that may suggest that the structure of if/elseif cascade is misdesigned. Shouldn't the "bisect" boolean be an enum "what are we doing" that is initialized to "I do not know" and each of these if/elseif cascade set the state to it when they know what we are doing, in order for this function to be longer-term maintainable?
Re: [PATCH v3] branch: show rebase/bisect info when possible instead of "(no branch)"
In reply to Junio C HamanoQuoted 10 lines · Junio C Hamano
We may want to refactor
wt_status_print_state()and its callee a bit so that it and this part can share the logic without duplication and risk implementing subtly different decision.wt_statusused to have clean separation between collection phase and presentation phase, but the wall between the phases seems deteriorated over time as more "in progress" crufts have been piled on top.Such a refactoring may look larger than necessary, but on the other hand, I do not see this feature very useful if it can over time drift away from what we will see in "git status", so...
OK. I'll wait until nd/status-show-in-progress is merged to master, then rework this patch on top.
Signature · 1 line
Duy