branch: add worktree info on verbose output
authorNickolai Belakovski <nbelakovski@gmail.com>
Mon, 29 Apr 2019 05:19:44 +0000 (22:19 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 May 2019 04:45:55 +0000 (13:45 +0900)
To display worktree path for refs checked out in a linked worktree

Signed-off-by: Nickolai Belakovski <nbelakovski@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-branch.txt
builtin/branch.c
t/t3203-branch-output.sh
index 7d18dffc4b89d3b1e9be934ffdf1dc130bf8ceb6..d11d00583a6c398c06c7acce458b5cae30ad62f7 100644 (file)
@@ -172,8 +172,10 @@ This option is only applicable in non-verbose mode.
        When in list mode,
        show sha1 and commit subject line for each head, along with
        relationship to upstream branch (if any). If given twice, print
        When in list mode,
        show sha1 and commit subject line for each head, along with
        relationship to upstream branch (if any). If given twice, print
-       the name of the upstream branch, as well (see also `git remote
-       show <remote>`).
+       the path of the linked worktree (if any) and the name of the upstream
+       branch, as well (see also `git remote show <remote>`).  Note that the
+       current worktree's HEAD will not have its path printed (it will always
+       be your current directory).
 
 -q::
 --quiet::
 
 -q::
 --quiet::
index 350b0390632ab4ae6bac12c52626c624d7979757..3d1872babc826a611f4eeb71c36fb0dcc67bec85 100644 (file)
@@ -367,9 +367,13 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
                strbuf_addf(&local, " %s ", obname.buf);
 
                if (filter->verbose > 1)
                strbuf_addf(&local, " %s ", obname.buf);
 
                if (filter->verbose > 1)
+               {
+                       strbuf_addf(&local, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)",
+                                   branch_get_color(BRANCH_COLOR_WORKTREE), branch_get_color(BRANCH_COLOR_RESET));
                        strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
                                    "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
                                    branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
                        strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
                                    "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
                                    branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
+               }
                else
                        strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
 
                else
                        strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
 
index a3436bf25a63a37c4c46433e41ad868230b5b6ba..4bef8c75697d1725c6068091f045e71753951fda 100755 (executable)
@@ -328,4 +328,23 @@ test_expect_success '--color overrides auto-color' '
        test_cmp expect.color actual
 '
 
        test_cmp expect.color actual
 '
 
+test_expect_success 'verbose output lists worktree path' '
+       one=$(git rev-parse --short HEAD) &&
+       two=$(git rev-parse --short master) &&
+       cat >expect <<-EOF &&
+       * (HEAD detached from fromtag) $one one
+         ambiguous                    $one one
+         branch-one                   $two two
+       + branch-two                   $one ($(pwd)/worktree_dir) one
+         master                       $two two
+         ref-to-branch                $two two
+         ref-to-remote                $two two
+       EOF
+       git worktree add worktree_dir branch-two &&
+       git branch -vv >actual &&
+       rm -r worktree_dir &&
+       git worktree prune &&
+       test_i18ncmp expect actual
+'
+
 test_done
 test_done