status: show the branch name if possible in in-progress info
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 3 Feb 2013 05:53:27 +0000 (12:53 +0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 5 Feb 2013 16:21:02 +0000 (08:21 -0800)
The typical use-case is starting a rebase, do something else, come
back the day after, run "git status" or make a new commit and wonder
what in the world's going on. Which branch is being rebased is
probably the most useful tidbit to help, but the target may help
too.

Ideally, I would have loved to see "rebasing master on
origin/master", but the target ref name is not stored during rebase,
so this patch writes "rebasing master on a78c8c98b" as a
half-measure to remind future users of that potential improvement.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t7512-status-help.sh
wt-status.c
wt-status.h
index b3f6eb9c684c406dd896a144caa6afc160b03226..51ab89411f06aa200c27815b8642cb0c78caeba4 100755 (executable)
@@ -73,10 +73,11 @@ test_expect_success 'prepare for rebase conflicts' '
 
 test_expect_success 'status when rebase in progress before resolving conflicts' '
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD^^) &&
        test_must_fail git rebase HEAD^ --onto HEAD^^ &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently rebasing.
+       # 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)
        #   (use "git rebase --abort" to check out the original branch)
@@ -97,12 +98,13 @@ test_expect_success 'status when rebase in progress before resolving conflicts'
 test_expect_success 'status when rebase in progress before rebase --continue' '
        git reset --hard rebase_conflicts &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD^^) &&
        test_must_fail git rebase HEAD^ --onto HEAD^^ &&
        echo three >main.txt &&
        git add main.txt &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently rebasing.
+       # You are currently rebasing branch '\''rebase_conflicts'\'' on '\''$ONTO'\''.
        #   (all conflicts fixed: run "git rebase --continue")
        #
        # Changes to be committed:
@@ -130,10 +132,11 @@ test_expect_success 'prepare for rebase_i_conflicts' '
 
 test_expect_success 'status during rebase -i when conflicts unresolved' '
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short rebase_i_conflicts) &&
        test_must_fail git rebase -i rebase_i_conflicts &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently rebasing.
+       # 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)
        #   (use "git rebase --abort" to check out the original branch)
@@ -154,11 +157,12 @@ test_expect_success 'status during rebase -i when conflicts unresolved' '
 test_expect_success 'status during rebase -i after resolving conflicts' '
        git reset --hard rebase_i_conflicts_second &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short rebase_i_conflicts) &&
        test_must_fail git rebase -i rebase_i_conflicts &&
        git add main.txt &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently rebasing.
+       # You are currently rebasing branch '\''rebase_i_conflicts_second'\'' on '\''$ONTO'\''.
        #   (all conflicts fixed: run "git rebase --continue")
        #
        # Changes to be committed:
@@ -182,10 +186,11 @@ test_expect_success 'status when rebasing -i in edit mode' '
        FAKE_LINES="1 edit 2" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~2) &&
        git rebase -i HEAD~2 &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently editing a commit during a rebase.
+       # 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)
        #
@@ -206,11 +211,12 @@ test_expect_success 'status when splitting a commit' '
        FAKE_LINES="1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git reset HEAD^ &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently splitting a commit during a rebase.
+       # You are currently splitting a commit while rebasing branch '\''split_commit'\'' on '\''$ONTO'\''.
        #   (Once your working directory is clean, run "git rebase --continue")
        #
        # Changes not staged for commit:
@@ -236,11 +242,12 @@ test_expect_success 'status after editing the last commit with --amend during a
        FAKE_LINES="1 2 edit 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git commit --amend -m "foo" &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently editing a commit during a rebase.
+       # 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)
        #
@@ -265,11 +272,12 @@ test_expect_success 'status: (continue first edit) second edit' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git rebase --continue &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently editing a commit during a rebase.
+       # 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)
        #
@@ -285,12 +293,13 @@ test_expect_success 'status: (continue first edit) second edit and split' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git rebase --continue &&
        git reset HEAD^ &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently splitting a commit during a rebase.
+       # You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
        #   (Once your working directory is clean, run "git rebase --continue")
        #
        # Changes not staged for commit:
@@ -311,12 +320,13 @@ test_expect_success 'status: (continue first edit) second edit and amend' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git rebase --continue &&
        git commit --amend -m "foo" &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently editing a commit during a rebase.
+       # 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)
        #
@@ -332,12 +342,13 @@ test_expect_success 'status: (amend first edit) second edit' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git commit --amend -m "a" &&
        git rebase --continue &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently editing a commit during a rebase.
+       # 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)
        #
@@ -353,13 +364,14 @@ test_expect_success 'status: (amend first edit) second edit and split' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git commit --amend -m "b" &&
        git rebase --continue &&
        git reset HEAD^ &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently splitting a commit during a rebase.
+       # You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
        #   (Once your working directory is clean, run "git rebase --continue")
        #
        # Changes not staged for commit:
@@ -380,13 +392,14 @@ test_expect_success 'status: (amend first edit) second edit and amend' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git commit --amend -m "c" &&
        git rebase --continue &&
        git commit --amend -m "d" &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently editing a commit during a rebase.
+       # 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)
        #
@@ -402,14 +415,15 @@ test_expect_success 'status: (split first edit) second edit' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git reset HEAD^ &&
        git add main.txt &&
        git commit -m "e" &&
        git rebase --continue &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently editing a commit during a rebase.
+       # 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)
        #
@@ -425,15 +439,16 @@ test_expect_success 'status: (split first edit) second edit and split' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git reset HEAD^ &&
        git add main.txt &&
        git commit --amend -m "f" &&
        git rebase --continue &&
        git reset HEAD^ &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently splitting a commit during a rebase.
+       # You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
        #   (Once your working directory is clean, run "git rebase --continue")
        #
        # Changes not staged for commit:
@@ -454,15 +469,16 @@ test_expect_success 'status: (split first edit) second edit and amend' '
        FAKE_LINES="edit 1 edit 2 3" &&
        export FAKE_LINES &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD~3) &&
        git rebase -i HEAD~3 &&
        git reset HEAD^ &&
        git add main.txt &&
        git commit --amend -m "g" &&
        git rebase --continue &&
        git commit --amend -m "h" &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently editing a commit during a rebase.
+       # 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)
        #
@@ -558,7 +574,7 @@ test_expect_success 'status when bisecting' '
        git bisect good one_bisect &&
        cat >expected <<-\EOF &&
        # Not currently on any branch.
-       # You are currently bisecting.
+       # You are currently bisecting branch '\''bisect'\''.
        #   (use "git bisect reset" to get back to the original branch)
        #
        nothing to commit (use -u to show untracked files)
@@ -577,10 +593,11 @@ test_expect_success 'status when rebase conflicts with statushints disabled' '
        test_commit two_statushints main.txt two &&
        test_commit three_statushints main.txt three &&
        test_when_finished "git rebase --abort" &&
+       ONTO=$(git rev-parse --short HEAD^^) &&
        test_must_fail git rebase HEAD^ --onto HEAD^^ &&
-       cat >expected <<-\EOF &&
+       cat >expected <<-EOF &&
        # Not currently on any branch.
-       # You are currently rebasing.
+       # You are currently rebasing branch '\''statushints_disabled'\'' on '\''$ONTO'\''.
        #
        # Unmerged paths:
        #       both modified:      main.txt
index d7cfe8f31cd3d2b4b8bdc18c5da2449255b0531b..983e2f4b5e705985d4084ffea504fdd7d483e64b 100644 (file)
@@ -872,7 +872,14 @@ static void show_rebase_in_progress(struct wt_status *s,
        struct stat st;
 
        if (has_unmerged(s)) {
-               status_printf_ln(s, color, _("You are currently rebasing."));
+               if (state->branch)
+                       status_printf_ln(s, color,
+                                        _("You are currently rebasing branch '%s' on '%s'."),
+                                        state->branch,
+                                        state->onto);
+               else
+                       status_printf_ln(s, color,
+                                        _("You are currently rebasing."));
                if (advice_status_hints) {
                        status_printf_ln(s, color,
                                _("  (fix conflicts and then run \"git rebase --continue\")"));
@@ -882,17 +889,38 @@ static void show_rebase_in_progress(struct wt_status *s,
                                _("  (use \"git rebase --abort\" to check out the original branch)"));
                }
        } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
-               status_printf_ln(s, color, _("You are currently rebasing."));
+               if (state->branch)
+                       status_printf_ln(s, color,
+                                        _("You are currently rebasing branch '%s' on '%s'."),
+                                        state->branch,
+                                        state->onto);
+               else
+                       status_printf_ln(s, color,
+                                        _("You are currently rebasing."));
                if (advice_status_hints)
                        status_printf_ln(s, color,
                                _("  (all conflicts fixed: run \"git rebase --continue\")"));
        } else if (split_commit_in_progress(s)) {
-               status_printf_ln(s, color, _("You are currently splitting a commit during a rebase."));
+               if (state->branch)
+                       status_printf_ln(s, color,
+                                        _("You are currently splitting a commit while rebasing branch '%s' on '%s'."),
+                                        state->branch,
+                                        state->onto);
+               else
+                       status_printf_ln(s, color,
+                                        _("You are currently splitting a commit during a rebase."));
                if (advice_status_hints)
                        status_printf_ln(s, color,
                                _("  (Once your working directory is clean, run \"git rebase --continue\")"));
        } else {
-               status_printf_ln(s, color, _("You are currently editing a commit during a rebase."));
+               if (state->branch)
+                       status_printf_ln(s, color,
+                                        _("You are currently editing a commit while rebasing branch '%s' on '%s'."),
+                                        state->branch,
+                                        state->onto);
+               else
+                       status_printf_ln(s, color,
+                                        _("You are currently editing a commit during a rebase."));
                if (advice_status_hints && !s->amend) {
                        status_printf_ln(s, color,
                                _("  (use \"git commit --amend\" to amend the current commit)"));
@@ -923,16 +951,57 @@ static void show_bisect_in_progress(struct wt_status *s,
                                struct wt_status_state *state,
                                const char *color)
 {
-       status_printf_ln(s, color, _("You are currently bisecting."));
+       if (state->branch)
+               status_printf_ln(s, color,
+                                _("You are currently bisecting branch '%s'."),
+                                state->branch);
+       else
+               status_printf_ln(s, color,
+                                _("You are currently bisecting."));
        if (advice_status_hints)
                status_printf_ln(s, color,
                        _("  (use \"git bisect reset\" to get back to the original branch)"));
        wt_status_print_trailer(s);
 }
 
+/*
+ * Extract branch information from rebase/bisect
+ */
+static void read_and_strip_branch(struct strbuf *sb,
+                                 const char **branch,
+                                 const char *path)
+{
+       unsigned char sha1[20];
+
+       strbuf_reset(sb);
+       if (strbuf_read_file(sb, git_path("%s", path), 0) <= 0)
+               return;
+
+       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)) {
+               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 */
+               ;
+       else                    /* bisect */
+               *branch = sb->buf;
+}
+
 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;
 
@@ -947,17 +1016,28 @@ 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");
                }
        } 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");
        } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
                state.cherry_pick_in_progress = 1;
        }
-       if (!stat(git_path("BISECT_LOG"), &st))
+       if (!stat(git_path("BISECT_LOG"), &st)) {
                state.bisect_in_progress = 1;
+               read_and_strip_branch(&branch, &state.branch,
+                                     "BISECT_START");
+       }
 
        if (state.merge_in_progress)
                show_merge_in_progress(s, &state, state_color);
@@ -969,6 +1049,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);
 }
 
 void wt_status_print(struct wt_status *s)
index 236b41fd345ca3dd95ce450ac3fb3fca2b725e47..81e1dcf84dbfbea76657d3b4c65e3e63890396e9 100644 (file)
@@ -79,6 +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;
 };
 
 void wt_status_prepare(struct wt_status *s);