[PATCH] branch: make -v useful

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---

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@xxxxxxxxx/

 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




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux