git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] branch: make -v useful

From
Felipe Contreras <felipe.contreras@gmail.com>
Date
Jun 5, 2021, 01:13 UTC
Message-ID
<20210605011339.2202-1-felipe.contreras@gmail.com>

Currently `git branch -v` shows something like "[ahead 10]", but ahead of what?

We git experts know ahead of what, but not what that what is set to. Just like "[@{upstream}: ahead 10]" would not be particularly useful to anyone that doesn't know, or remembers, what @{upstream} is set to.

On the other hand "[master: ahead 10]" is perfectly clear to anyone.

This confusion only gets worse when you see "[ahead 10, behind 100]". Is it master? Is it next? Is it john/experimental-feature-i-based-my-branch-on?

Inevitably most users will need to know what @{upstream} is.
So let's make `git branch -v` output what is most useful:
  [master]
Before:
  * fc/branch/sane-colors b2489a3735 [ahead 1] branch: make -v useful
After:
  * fc/branch/sane-colors b2489a3735 [master] branch: make -v useful

An additional benefit is that `git branch -v` is slightly faster: 30ms vs. 60ms on my system.

`git branch -vv` is unaffected.
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
This is a reboot of my old patch series [1].
Every time I use git without this feature I miss it.
[1] https://lore.kernel.org/git/1398027514-19399-1-git-send-email-felipe.contreras@gmail.com/
 builtin/branch.c           |  9 ++++-----
 t/t3201-branch-contains.sh |  2 +-
 t/t6040-tracking-info.sh   | 12 ++++++------
 3 files changed, 11 insertions(+), 12 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index b23b1d1752..7c0d3f7e4e 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -375,16 +375,15 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
 		strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET));
 		strbuf_addf(&local, " %s ", obname.buf);
 
+		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));
 		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)");
+			strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s] %%(end)%%(contents:subject)",
+				    branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
 
 		strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
 			    "%%(if)%%(symref)%%(then) -> %%(symref:short)"
diff --git a/t/t3201-branch-contains.sh b/t/t3201-branch-contains.sh
index 349a810cee..53e2d65e67 100755
--- a/t/t3201-branch-contains.sh
+++ b/t/t3201-branch-contains.sh
@@ -261,7 +261,7 @@ test_expect_success 'branch --merged with --verbose' '
 	git branch --verbose --merged topic >actual &&
 	cat >expect <<-EOF &&
 	  main  $(git rev-parse --short main) second on main
-	* topic $(git rev-parse --short topic ) [ahead 1] foo
+	* topic $(git rev-parse --short topic ) [main] foo
 	  zzz   $(git rev-parse --short zzz   ) second on main
 	EOF
 	test_cmp expect actual
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index a313849406..30f80ad61b 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -43,12 +43,12 @@ test_expect_success setup '
 
 t6040_script='s/^..\(b.\) *[0-9a-f]* \(.*\)$/\1 \2/p'
 cat >expect <<\EOF
-b1 [ahead 1, behind 1] d
-b2 [ahead 1, behind 1] d
-b3 [behind 1] b
-b4 [ahead 2] f
-b5 [gone] g
-b6 c
+b1 [origin/main] d
+b2 [origin/main] d
+b3 [origin/main] b
+b4 [origin/main] f
+b5 [brokenbase] g
+b6 [origin/main] c
 EOF
 
 test_expect_success 'branch -v' '
-- 
2.32.0.rc2
Next: Ævar Arnfjörð Bjarmason
Message 1 of 18 in “branch: make -v useful”
  1. branch: make -v usefulFelipe Contreras, Jun 5, 2021
  2. Ævar Arnfjörð BjarmasonJun 5, 2021
  3. Jeff KingJun 5, 2021
  4. Felipe ContrerasJun 7, 2021
  5. Jeff KingJun 8, 2021
  6. Felipe ContrerasJun 8, 2021
  7. Jeff KingJun 8, 2021
  8. Felipe ContrerasJun 8, 2021
  9. Kerry, RichardJun 8, 2021
  10. Felipe ContrerasJun 8, 2021
  11. Kerry, RichardJun 8, 2021
  12. Felipe ContrerasJun 10, 2021
  13. Kerry, RichardJun 25, 2021
  14. Felipe ContrerasJun 25, 2021
  15. Felipe ContrerasJun 7, 2021
  16. Ævar Arnfjörð BjarmasonJun 7, 2021
  17. Felipe ContrerasJun 7, 2021
  18. Felipe ContrerasJun 7, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.