Subject: [PATCH v8 3/3] branch: add worktree info on verbose output

From: [email-masked]
Date: Tue, 19 Feb 2019 17:31:23 +0900

From: Nickolai Belakovski <[email-masked]>

To display worktree path for refs checked out in a linked worktree

Signed-off-by: Nickolai Belakovski <[email-masked]>
---
 Documentation/git-branch.txt |  6 ++++--
 builtin/branch.c             |  4 ++++
 t/t3203-branch-output.sh     | 21 ++++++++++++++++++++-
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index f2e5a07d64..326a45f648 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -168,8 +168,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
-	the name of the upstream branch, as well (see also `git remote
-	show <remote>`).
+	the path of the linked worktree, if applicable (not applicable
+	for current worktree since user's path will already be in current
+	worktree) and the name of the upstream branch, as well (see also
+	`git remote show <remote>`).
 
 -q::
 --quiet::
diff --git a/builtin/branch.c b/builtin/branch.c
index c2a86362bb..0b8ba9e4c5 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -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, "%%(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));
+		}
 		else
 			strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
 
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index 94ab05ad59..012ddde7f2 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -241,7 +241,6 @@ test_expect_success 'git branch --format option' '
 '
 
 test_expect_success '"add" a worktree' '
-	mkdir worktree_dir &&
 	git worktree add -b master_worktree worktree_dir master
 '
 
@@ -285,4 +284,24 @@ test_expect_success '--color overrides auto-color' '
 	test_cmp expect.color actual
 '
 
+# This test case has some special code to strip the first 30 characters or so
+# of the output so that we do not have to put commit hashes into the expect
+test_expect_success 'verbose output lists worktree path' '
+	cat >expect <<-EOF &&
+	one
+	one
+	two
+	one
+	two
+	($(pwd)/worktree_dir) two
+	two
+	two
+	EOF
+	git branch -vv >tmp &&
+	SUBSTRLENGTH=$(head -1 tmp | awk "{print index(\$0, \"one\")}") &&
+	awk -v substrlength="$SUBSTRLENGTH" "{print substr(\$0,substrlength,length(\$0))}" <tmp >actual &&
+	test_cmp expect actual
+'
+
+
 test_done
-- 
2.14.2