From 8b87cfd000c7c98f58279cff698f6e8c7892c059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sat, 16 Mar 2013 09:12:36 +0700 Subject: wt-status: move strbuf into read_and_strip_branch() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The strbufs are placed outside read_and_strip_branch as a premature optimization: when it reads "refs/heads/foo" to strbuf and wants to return just "foo", it could do so without memory movement. In return the caller must not use the returned pointer after releasing strbufs, which own the buffers that contain the returned strings. It's a clumsy design. By moving strbufs into read_and_strip_branch(), the returned pointer always points to a malloc'd buffer or NULL. The pointer can be passed around and freed after use. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano diff --git a/wt-status.c b/wt-status.c index 983e2f4..aa53436 100644 --- a/wt-status.c +++ b/wt-status.c @@ -967,41 +967,41 @@ static void show_bisect_in_progress(struct wt_status *s, /* * Extract branch information from rebase/bisect */ -static void read_and_strip_branch(struct strbuf *sb, - const char **branch, - const char *path) +static char *read_and_strip_branch(const char *path) { + struct strbuf sb = STRBUF_INIT; unsigned char sha1[20]; - strbuf_reset(sb); - if (strbuf_read_file(sb, git_path("%s", path), 0) <= 0) - return; + if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0) + goto got_nothing; - while (sb->len && sb->buf[sb->len - 1] == '\n') - strbuf_setlen(sb, sb->len - 1); - if (!sb->len) - return; - if (!prefixcmp(sb->buf, "refs/heads/")) - *branch = sb->buf + strlen("refs/heads/"); - else if (!prefixcmp(sb->buf, "refs/")) - *branch = sb->buf; - else if (!get_sha1_hex(sb->buf, sha1)) { + while (&sb.len && sb.buf[sb.len - 1] == '\n') + strbuf_setlen(&sb, sb.len - 1); + if (!sb.len) + goto got_nothing; + if (!prefixcmp(sb.buf, "refs/heads/")) + strbuf_remove(&sb,0, strlen("refs/heads/")); + else if (!prefixcmp(sb.buf, "refs/")) + ; + else if (!get_sha1_hex(sb.buf, sha1)) { const char *abbrev; abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV); - strbuf_reset(sb); - strbuf_addstr(sb, abbrev); - *branch = sb->buf; - } else if (!strcmp(sb->buf, "detached HEAD")) /* rebase */ - ; + strbuf_reset(&sb); + strbuf_addstr(&sb, abbrev); + } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */ + goto got_nothing; else /* bisect */ - *branch = sb->buf; + ; + return strbuf_detach(&sb, NULL); + +got_nothing: + strbuf_release(&sb); + return NULL; } static void wt_status_print_state(struct wt_status *s) { const char *state_color = color(WT_STATUS_HEADER, s); - struct strbuf branch = STRBUF_INIT; - struct strbuf onto = STRBUF_INIT; struct wt_status_state state; struct stat st; @@ -1016,27 +1016,22 @@ static void wt_status_print_state(struct wt_status *s) state.am_empty_patch = 1; } else { state.rebase_in_progress = 1; - read_and_strip_branch(&branch, &state.branch, - "rebase-apply/head-name"); - read_and_strip_branch(&onto, &state.onto, - "rebase-apply/onto"); + state.branch = read_and_strip_branch("rebase-apply/head-name"); + state.onto = read_and_strip_branch("rebase-apply/onto"); } } else if (!stat(git_path("rebase-merge"), &st)) { if (!stat(git_path("rebase-merge/interactive"), &st)) state.rebase_interactive_in_progress = 1; else state.rebase_in_progress = 1; - read_and_strip_branch(&branch, &state.branch, - "rebase-merge/head-name"); - read_and_strip_branch(&onto, &state.onto, - "rebase-merge/onto"); + state.branch = read_and_strip_branch("rebase-merge/head-name"); + state.onto = read_and_strip_branch("rebase-merge/onto"); } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) { state.cherry_pick_in_progress = 1; } if (!stat(git_path("BISECT_LOG"), &st)) { state.bisect_in_progress = 1; - read_and_strip_branch(&branch, &state.branch, - "BISECT_START"); + state.branch = read_and_strip_branch("BISECT_START"); } if (state.merge_in_progress) @@ -1049,8 +1044,8 @@ static void wt_status_print_state(struct wt_status *s) show_cherry_pick_in_progress(s, &state, state_color); if (state.bisect_in_progress) show_bisect_in_progress(s, &state, state_color); - strbuf_release(&branch); - strbuf_release(&onto); + free(state.branch); + free(state.onto); } void wt_status_print(struct wt_status *s) diff --git a/wt-status.h b/wt-status.h index 81e1dcf..b8c3512 100644 --- a/wt-status.h +++ b/wt-status.h @@ -79,8 +79,8 @@ struct wt_status_state { int rebase_interactive_in_progress; int cherry_pick_in_progress; int bisect_in_progress; - const char *branch; - const char *onto; + char *branch; + char *onto; }; void wt_status_prepare(struct wt_status *s); -- cgit v0.10.2-6-g49f6 From b9691db4f90164b3de2c2ea7e82ea545f2056f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 13 Mar 2013 18:42:50 +0700 Subject: wt-status: split wt_status_state parsing function out MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano diff --git a/wt-status.c b/wt-status.c index aa53436..96b3701 100644 --- a/wt-status.c +++ b/wt-status.c @@ -999,40 +999,45 @@ got_nothing: return NULL; } -static void wt_status_print_state(struct wt_status *s) +void wt_status_get_state(struct wt_status_state *state) { - const char *state_color = color(WT_STATUS_HEADER, s); - struct wt_status_state state; struct stat st; - memset(&state, 0, sizeof(state)); - if (!stat(git_path("MERGE_HEAD"), &st)) { - state.merge_in_progress = 1; + state->merge_in_progress = 1; } else if (!stat(git_path("rebase-apply"), &st)) { if (!stat(git_path("rebase-apply/applying"), &st)) { - state.am_in_progress = 1; + state->am_in_progress = 1; if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size) - state.am_empty_patch = 1; + state->am_empty_patch = 1; } else { - state.rebase_in_progress = 1; - state.branch = read_and_strip_branch("rebase-apply/head-name"); - state.onto = read_and_strip_branch("rebase-apply/onto"); + state->rebase_in_progress = 1; + state->branch = read_and_strip_branch("rebase-apply/head-name"); + state->onto = read_and_strip_branch("rebase-apply/onto"); } } else if (!stat(git_path("rebase-merge"), &st)) { if (!stat(git_path("rebase-merge/interactive"), &st)) - state.rebase_interactive_in_progress = 1; + state->rebase_interactive_in_progress = 1; else - state.rebase_in_progress = 1; - state.branch = read_and_strip_branch("rebase-merge/head-name"); - state.onto = read_and_strip_branch("rebase-merge/onto"); + state->rebase_in_progress = 1; + state->branch = read_and_strip_branch("rebase-merge/head-name"); + state->onto = read_and_strip_branch("rebase-merge/onto"); } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) { - state.cherry_pick_in_progress = 1; + state->cherry_pick_in_progress = 1; } if (!stat(git_path("BISECT_LOG"), &st)) { - state.bisect_in_progress = 1; - state.branch = read_and_strip_branch("BISECT_START"); + state->bisect_in_progress = 1; + state->branch = read_and_strip_branch("BISECT_START"); } +} + +static void wt_status_print_state(struct wt_status *s) +{ + const char *state_color = color(WT_STATUS_HEADER, s); + struct wt_status_state state; + + memset(&state, 0, sizeof(state)); + wt_status_get_state(&state); if (state.merge_in_progress) show_merge_in_progress(s, &state, state_color); diff --git a/wt-status.h b/wt-status.h index b8c3512..5ddcbf6 100644 --- a/wt-status.h +++ b/wt-status.h @@ -86,6 +86,7 @@ struct wt_status_state { void wt_status_prepare(struct wt_status *s); void wt_status_print(struct wt_status *s); void wt_status_collect(struct wt_status *s); +void wt_status_get_state(struct wt_status_state *state); void wt_shortstatus_print(struct wt_status *s); void wt_porcelain_print(struct wt_status *s); -- cgit v0.10.2-6-g49f6 From 3b691cccb02e660002a7ff414ad21ddac932dc6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 13 Mar 2013 18:42:51 +0700 Subject: wt-status: move wt_status_get_state() out to wt_status_print() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano diff --git a/wt-status.c b/wt-status.c index 96b3701..40dced3 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1031,32 +1031,30 @@ void wt_status_get_state(struct wt_status_state *state) } } -static void wt_status_print_state(struct wt_status *s) +static void wt_status_print_state(struct wt_status *s, + struct wt_status_state *state) { const char *state_color = color(WT_STATUS_HEADER, s); - struct wt_status_state state; - - memset(&state, 0, sizeof(state)); - wt_status_get_state(&state); - - if (state.merge_in_progress) - show_merge_in_progress(s, &state, state_color); - else if (state.am_in_progress) - show_am_in_progress(s, &state, state_color); - else if (state.rebase_in_progress || state.rebase_interactive_in_progress) - show_rebase_in_progress(s, &state, state_color); - else if (state.cherry_pick_in_progress) - show_cherry_pick_in_progress(s, &state, state_color); - if (state.bisect_in_progress) - show_bisect_in_progress(s, &state, state_color); - free(state.branch); - free(state.onto); + if (state->merge_in_progress) + show_merge_in_progress(s, state, state_color); + else if (state->am_in_progress) + show_am_in_progress(s, state, state_color); + else if (state->rebase_in_progress || state->rebase_interactive_in_progress) + show_rebase_in_progress(s, state, state_color); + else if (state->cherry_pick_in_progress) + show_cherry_pick_in_progress(s, state, state_color); + if (state->bisect_in_progress) + show_bisect_in_progress(s, state, state_color); } void wt_status_print(struct wt_status *s) { const char *branch_color = color(WT_STATUS_ONBRANCH, s); const char *branch_status_color = color(WT_STATUS_HEADER, s); + struct wt_status_state state; + + memset(&state, 0, sizeof(state)); + wt_status_get_state(&state); if (s->branch) { const char *on_what = _("On branch "); @@ -1075,7 +1073,10 @@ void wt_status_print(struct wt_status *s) wt_status_print_tracking(s); } - wt_status_print_state(s); + wt_status_print_state(s, &state); + free(state.branch); + free(state.onto); + if (s->is_initial) { status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); status_printf_ln(s, color(WT_STATUS_HEADER, s), _("Initial commit")); -- cgit v0.10.2-6-g49f6 From b397ea4863a143e97cb9da76c6c18cc555537d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 13 Mar 2013 18:42:52 +0700 Subject: status: show more info than "currently not on any branch" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a remote ref or a tag is checked out, HEAD is automatically detached. There is no user-friendly way to find out what ref is checked out in this case. This patch digs in reflog for this information and shows "HEAD detached from origin/master" or "HEAD detached at v1.8.0" instead of "currently not on any branch". When it cannot figure out the original ref, it shows an abbreviated SHA-1. "Currently not on any branch" would never display (unless reflog is pruned to near empty that the last checkout entry is lost). Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh index 4975ec0..50ac020 100755 --- a/t/t7406-submodule-update.sh +++ b/t/t7406-submodule-update.sh @@ -664,8 +664,10 @@ test_expect_success 'submodule add properly re-creates deeper level submodules' test_expect_success 'submodule update properly revives a moved submodule' ' (cd super && + H=$(git rev-parse --short HEAD) && git commit -am "pre move" && - git status >expect&& + H2=$(git rev-parse --short HEAD) && + git status | sed "s/$H/XXX/" >expect && H=$(cd submodule2; git rev-parse HEAD) && git rm --cached submodule2 && rm -rf submodule2 && @@ -674,7 +676,7 @@ test_expect_success 'submodule update properly revives a moved submodule' ' git config -f .gitmodules submodule.submodule2.path "moved/sub module" git commit -am "post move" && git submodule update && - git status >actual && + git status | sed "s/$H2/XXX/" >actual && test_cmp expect actual ) ' diff --git a/t/t7512-status-help.sh b/t/t7512-status-help.sh index 51ab894..5adba4f 100755 --- a/t/t7512-status-help.sh +++ b/t/t7512-status-help.sh @@ -76,7 +76,7 @@ test_expect_success 'status when rebase in progress before resolving conflicts' ONTO=$(git rev-parse --short HEAD^^) && test_must_fail git rebase HEAD^ --onto HEAD^^ && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached at $ONTO # You are currently rebasing branch '\''rebase_conflicts'\'' on '\''$ONTO'\''. # (fix conflicts and then run "git rebase --continue") # (use "git rebase --skip" to skip this patch) @@ -103,7 +103,7 @@ test_expect_success 'status when rebase in progress before rebase --continue' ' echo three >main.txt && git add main.txt && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached at $ONTO # You are currently rebasing branch '\''rebase_conflicts'\'' on '\''$ONTO'\''. # (all conflicts fixed: run "git rebase --continue") # @@ -135,7 +135,7 @@ test_expect_success 'status during rebase -i when conflicts unresolved' ' ONTO=$(git rev-parse --short rebase_i_conflicts) && test_must_fail git rebase -i rebase_i_conflicts && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached at $ONTO # You are currently rebasing branch '\''rebase_i_conflicts_second'\'' on '\''$ONTO'\''. # (fix conflicts and then run "git rebase --continue") # (use "git rebase --skip" to skip this patch) @@ -161,7 +161,7 @@ test_expect_success 'status during rebase -i after resolving conflicts' ' test_must_fail git rebase -i rebase_i_conflicts && git add main.txt && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached at $ONTO # You are currently rebasing branch '\''rebase_i_conflicts_second'\'' on '\''$ONTO'\''. # (all conflicts fixed: run "git rebase --continue") # @@ -187,9 +187,10 @@ test_expect_success 'status when rebasing -i in edit mode' ' export FAKE_LINES && test_when_finished "git rebase --abort" && ONTO=$(git rev-parse --short HEAD~2) && + TGT=$(git rev-parse --short two_rebase_i) && git rebase -i HEAD~2 && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $TGT # You are currently editing a commit while rebasing branch '\''rebase_i_edit'\'' on '\''$ONTO'\''. # (use "git commit --amend" to amend the current commit) # (use "git rebase --continue" once you are satisfied with your changes) @@ -214,8 +215,9 @@ test_expect_success 'status when splitting a commit' ' ONTO=$(git rev-parse --short HEAD~3) && git rebase -i HEAD~3 && git reset HEAD^ && + TGT=$(git rev-parse --short HEAD) && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached at $TGT # You are currently splitting a commit while rebasing branch '\''split_commit'\'' on '\''$ONTO'\''. # (Once your working directory is clean, run "git rebase --continue") # @@ -243,10 +245,11 @@ test_expect_success 'status after editing the last commit with --amend during a export FAKE_LINES && test_when_finished "git rebase --abort" && ONTO=$(git rev-parse --short HEAD~3) && + TGT=$(git rev-parse --short three_amend) && git rebase -i HEAD~3 && git commit --amend -m "foo" && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $TGT # You are currently editing a commit while rebasing branch '\''amend_last'\'' on '\''$ONTO'\''. # (use "git commit --amend" to amend the current commit) # (use "git rebase --continue" once you are satisfied with your changes) @@ -276,7 +279,7 @@ test_expect_success 'status: (continue first edit) second edit' ' git rebase -i HEAD~3 && git rebase --continue && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (use "git commit --amend" to amend the current commit) # (use "git rebase --continue" once you are satisfied with your changes) @@ -298,7 +301,7 @@ test_expect_success 'status: (continue first edit) second edit and split' ' git rebase --continue && git reset HEAD^ && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (Once your working directory is clean, run "git rebase --continue") # @@ -325,7 +328,7 @@ test_expect_success 'status: (continue first edit) second edit and amend' ' git rebase --continue && git commit --amend -m "foo" && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (use "git commit --amend" to amend the current commit) # (use "git rebase --continue" once you are satisfied with your changes) @@ -347,7 +350,7 @@ test_expect_success 'status: (amend first edit) second edit' ' git commit --amend -m "a" && git rebase --continue && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (use "git commit --amend" to amend the current commit) # (use "git rebase --continue" once you are satisfied with your changes) @@ -370,7 +373,7 @@ test_expect_success 'status: (amend first edit) second edit and split' ' git rebase --continue && git reset HEAD^ && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (Once your working directory is clean, run "git rebase --continue") # @@ -398,7 +401,7 @@ test_expect_success 'status: (amend first edit) second edit and amend' ' git rebase --continue && git commit --amend -m "d" && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (use "git commit --amend" to amend the current commit) # (use "git rebase --continue" once you are satisfied with your changes) @@ -422,7 +425,7 @@ test_expect_success 'status: (split first edit) second edit' ' git commit -m "e" && git rebase --continue && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (use "git commit --amend" to amend the current commit) # (use "git rebase --continue" once you are satisfied with your changes) @@ -447,7 +450,7 @@ test_expect_success 'status: (split first edit) second edit and split' ' git rebase --continue && git reset HEAD^ && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (Once your working directory is clean, run "git rebase --continue") # @@ -477,7 +480,7 @@ test_expect_success 'status: (split first edit) second edit and amend' ' git rebase --continue && git commit --amend -m "h" && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached from $ONTO # You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''. # (use "git commit --amend" to amend the current commit) # (use "git rebase --continue" once you are satisfied with your changes) @@ -572,8 +575,9 @@ test_expect_success 'status when bisecting' ' git bisect start && git bisect bad && git bisect good one_bisect && - cat >expected <<-\EOF && - # Not currently on any branch. + TGT=$(git rev-parse --short two_bisect) && + cat >expected <<-EOF && + # HEAD detached at $TGT # You are currently bisecting branch '\''bisect'\''. # (use "git bisect reset" to get back to the original branch) # @@ -596,7 +600,7 @@ test_expect_success 'status when rebase conflicts with statushints disabled' ' ONTO=$(git rev-parse --short HEAD^^) && test_must_fail git rebase HEAD^ --onto HEAD^^ && cat >expected <<-EOF && - # Not currently on any branch. + # HEAD detached at $ONTO # You are currently rebasing branch '\''statushints_disabled'\'' on '\''$ONTO'\''. # # Unmerged paths: @@ -662,5 +666,15 @@ test_expect_success 'status when cherry-picking after resolving conflicts' ' test_i18ncmp expected actual ' +test_expect_success 'status showing detached from a tag' ' + test_commit atag tagging && + git checkout atag && + cat >expected <<-\EOF + # HEAD detached at atag + nothing to commit (use -u to show untracked files) + EOF + git status --untracked-files=no >actual && + test_i18ncmp expected actual +' test_done diff --git a/wt-status.c b/wt-status.c index 40dced3..32a51e1 100644 --- a/wt-status.c +++ b/wt-status.c @@ -999,7 +999,73 @@ got_nothing: return NULL; } -void wt_status_get_state(struct wt_status_state *state) +struct grab_1st_switch_cbdata { + int found; + struct strbuf buf; + unsigned char nsha1[20]; +}; + +static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1, + const char *email, unsigned long timestamp, int tz, + const char *message, void *cb_data) +{ + struct grab_1st_switch_cbdata *cb = cb_data; + const char *target = NULL, *end; + + if (prefixcmp(message, "checkout: moving from ")) + return 0; + message += strlen("checkout: moving from "); + target = strstr(message, " to "); + if (!target) + return 0; + target += strlen(" to "); + strbuf_reset(&cb->buf); + hashcpy(cb->nsha1, nsha1); + for (end = target; *end && *end != '\n'; end++) + ; + strbuf_add(&cb->buf, target, end - target); + cb->found = 1; + return 1; +} + +static void wt_status_get_detached_from(struct wt_status_state *state) +{ + struct grab_1st_switch_cbdata cb; + struct commit *commit; + unsigned char sha1[20]; + char *ref = NULL; + + strbuf_init(&cb.buf, 0); + if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) { + strbuf_release(&cb.buf); + return; + } + + if (dwim_ref(cb.buf.buf, cb.buf.len, sha1, &ref) == 1 && + /* sha1 is a commit? match without further lookup */ + (!hashcmp(cb.nsha1, sha1) || + /* perhaps sha1 is a tag, try to dereference to a commit */ + ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL && + !hashcmp(cb.nsha1, commit->object.sha1)))) { + int ofs; + if (!prefixcmp(ref, "refs/tags/")) + ofs = strlen("refs/tags/"); + else if (!prefixcmp(ref, "refs/remotes/")) + ofs = strlen("refs/remotes/"); + else + ofs = 0; + state->detached_from = xstrdup(ref + ofs); + } else + state->detached_from = + xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV)); + hashcpy(state->detached_sha1, cb.nsha1); + + free(ref); + strbuf_release(&cb.buf); +} + +void wt_status_get_state(struct wt_status_state *state, + int get_detached_from) { struct stat st; @@ -1029,6 +1095,9 @@ void wt_status_get_state(struct wt_status_state *state) state->bisect_in_progress = 1; state->branch = read_and_strip_branch("BISECT_START"); } + + if (get_detached_from) + wt_status_get_detached_from(state); } static void wt_status_print_state(struct wt_status *s, @@ -1054,7 +1123,8 @@ void wt_status_print(struct wt_status *s) struct wt_status_state state; memset(&state, 0, sizeof(state)); - wt_status_get_state(&state); + wt_status_get_state(&state, + s->branch && !strcmp(s->branch, "HEAD")); if (s->branch) { const char *on_what = _("On branch "); @@ -1062,9 +1132,19 @@ void wt_status_print(struct wt_status *s) if (!prefixcmp(branch_name, "refs/heads/")) branch_name += 11; else if (!strcmp(branch_name, "HEAD")) { - branch_name = ""; branch_status_color = color(WT_STATUS_NOBRANCH, s); - on_what = _("Not currently on any branch."); + if (state.detached_from) { + unsigned char sha1[20]; + branch_name = state.detached_from; + if (!get_sha1("HEAD", sha1) && + !hashcmp(sha1, state.detached_sha1)) + on_what = _("HEAD detached at "); + else + on_what = _("HEAD detached from "); + } else { + branch_name = ""; + on_what = _("Not currently on any branch."); + } } status_printf(s, color(WT_STATUS_HEADER, s), ""); status_printf_more(s, branch_status_color, "%s", on_what); @@ -1076,6 +1156,7 @@ void wt_status_print(struct wt_status *s) wt_status_print_state(s, &state); free(state.branch); free(state.onto); + free(state.detached_from); if (s->is_initial) { status_printf_ln(s, color(WT_STATUS_HEADER, s), ""); diff --git a/wt-status.h b/wt-status.h index 5ddcbf6..5cb7df9 100644 --- a/wt-status.h +++ b/wt-status.h @@ -81,12 +81,14 @@ struct wt_status_state { int bisect_in_progress; char *branch; char *onto; + char *detached_from; + unsigned char detached_sha1[20]; }; void wt_status_prepare(struct wt_status *s); void wt_status_print(struct wt_status *s); void wt_status_collect(struct wt_status *s); -void wt_status_get_state(struct wt_status_state *state); +void wt_status_get_state(struct wt_status_state *state, int get_detached_from); void wt_shortstatus_print(struct wt_status *s); void wt_porcelain_print(struct wt_status *s); -- cgit v0.10.2-6-g49f6 From c8183cd2851965665655738fc399c44f546d5b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Wed, 13 Mar 2013 18:42:53 +0700 Subject: branch: show more information when HEAD is detached MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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? Is it detached because the user checks out a remote ref or a tag (and which one)? Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano diff --git a/builtin/branch.c b/builtin/branch.c index ea6498b..99105f8 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -18,6 +18,7 @@ #include "string-list.h" #include "column.h" #include "utf8.h" +#include "wt-status.h" static const char * const builtin_branch_usage[] = { N_("git branch [options] [-r | -a] [--merged | --no-merged]"), @@ -550,6 +551,29 @@ static int calc_maxwidth(struct ref_list *refs) return w; } +static char *get_head_description(void) +{ + struct strbuf desc = STRBUF_INIT; + struct wt_status_state state; + memset(&state, 0, sizeof(state)); + wt_status_get_state(&state, 1); + if (state.rebase_in_progress || + state.rebase_interactive_in_progress) + strbuf_addf(&desc, _("(no branch, rebasing %s)"), + state.branch); + else if (state.bisect_in_progress) + strbuf_addf(&desc, _("(no branch, bisecting %s)"), + state.branch); + else if (state.detached_from) + strbuf_addf(&desc, _("(detached from %s)"), + state.detached_from); + else + strbuf_addstr(&desc, _("(no branch)")); + free(state.branch); + free(state.onto); + free(state.detached_from); + return strbuf_detach(&desc, NULL); +} static void show_detached(struct ref_list *ref_list) { @@ -557,7 +581,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; diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh index 76fe7e0..ba4f98e 100755 --- a/t/t3203-branch-output.sh +++ b/t/t3203-branch-output.sh @@ -94,13 +94,13 @@ test_expect_success 'git branch -v pattern does not show branch summaries' ' test_must_fail git branch -v branch* ' -cat >expect <<'EOF' -* (no branch) +test_expect_success 'git branch shows detached HEAD properly' ' + cat >expect <actual && test_i18ncmp expect actual diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index 3e0e15f..9b6f0d0 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -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' ' -- cgit v0.10.2-6-g49f6 From 6deab24d8832e3cb2f554b827af521b094a2b32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sat, 23 Mar 2013 10:52:44 +0700 Subject: status, branch: fix the misleading "bisecting" message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current message is "bisecting %s" (or "bisecting branch %s"). "%s" is the current branch when we started bisecting. Clarify that to avoid confusion with good and bad refs passed to "bisect" command. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano diff --git a/builtin/branch.c b/builtin/branch.c index 99105f8..8f00203 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -562,7 +562,7 @@ static char *get_head_description(void) strbuf_addf(&desc, _("(no branch, rebasing %s)"), state.branch); else if (state.bisect_in_progress) - strbuf_addf(&desc, _("(no branch, bisecting %s)"), + strbuf_addf(&desc, _("(no branch, bisect started on %s)"), state.branch); else if (state.detached_from) strbuf_addf(&desc, _("(detached from %s)"), diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh index 9b6f0d0..2fce99a 100755 --- a/t/t6030-bisect-porcelain.sh +++ b/t/t6030-bisect-porcelain.sh @@ -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, bisecting other)" branch.output > /dev/null && + test_i18ngrep "* (no branch, bisect started on other)" branch.output > /dev/null && test_cmp saved .git/BISECT_START ' test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' ' diff --git a/t/t7512-status-help.sh b/t/t7512-status-help.sh index 5adba4f..c35d01d 100755 --- a/t/t7512-status-help.sh +++ b/t/t7512-status-help.sh @@ -578,7 +578,7 @@ test_expect_success 'status when bisecting' ' TGT=$(git rev-parse --short two_bisect) && cat >expected <<-EOF && # HEAD detached at $TGT - # You are currently bisecting branch '\''bisect'\''. + # You are currently bisecting, started from branch '\''bisect'\''. # (use "git bisect reset" to get back to the original branch) # nothing to commit (use -u to show untracked files) diff --git a/wt-status.c b/wt-status.c index 32a51e1..cf3d81a 100644 --- a/wt-status.c +++ b/wt-status.c @@ -953,7 +953,7 @@ static void show_bisect_in_progress(struct wt_status *s, { if (state->branch) status_printf_ln(s, color, - _("You are currently bisecting branch '%s'."), + _("You are currently bisecting, started from branch '%s'."), state->branch); else status_printf_ln(s, color, -- cgit v0.10.2-6-g49f6