git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH try3] branch: reorganize verbose options
@ 2014-04-20 20:58 Felipe Contreras
  0 siblings, 0 replies; only message in thread
From: Felipe Contreras @ 2014-04-20 20:58 UTC (permalink / raw)
  To: git
  Cc: Nguyễn Thái Ngọc Duy, Jeff King,
	Michael J Gruber, Felipe Contreras

`git branch -v` before:

  fc/branch/nice-verbose 4761939 [master] branch: reorganize verbose options

`git branch -v` after:

  fc/branch/nice-verbose 4761939 [ahead 1] branch: reorganize verbose options

Showing the upstream tracking branch is more important than how many commits
are ahead/behind, after all you need to know ahead/behind compared to _what_.
So now `git branch -v` shows the upstream, but not the tracking info, and
`git branch -vv` shows all information (unchanged).

An additional benefit is that `git branch -v` is now much faster:

Before:

 git branch -v  0.01s user 0.01s system 90% cpu 0.014 total

After:

 git branch -v  1.67s user 0.03s system 99% cpu 1.698 total

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 builtin/branch.c         | 76 +++++++++++++++++++++---------------------------
 t/t6040-tracking-info.sh | 12 ++++----
 2 files changed, 39 insertions(+), 49 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index b4d7716..8013540 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -417,29 +417,35 @@ static int ref_cmp(const void *r1, const void *r2)
 }
 
 static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
-		int show_upstream_ref)
+		int show_tracking)
 {
 	int ours, theirs;
 	char *ref = NULL;
 	struct branch *branch = branch_get(branch_name);
 	struct strbuf fancy = STRBUF_INIT;
 	int upstream_is_gone = 0;
-	int added_decoration = 1;
 
-	switch (stat_tracking_info(branch, &ours, &theirs)) {
-	case 0:
-		/* no base */
+	if (!branch)
 		return;
-	case -1:
-		/* with "gone" base */
-		upstream_is_gone = 1;
-		break;
-	default:
-		/* with base */
-		break;
+
+	if (show_tracking) {
+		switch (stat_tracking_info(branch, &ours, &theirs)) {
+		case 0:
+			/* no base */
+			return;
+		case -1:
+			/* with "gone" base */
+			upstream_is_gone = 1;
+			break;
+		default:
+			/* with base */
+			break;
+		}
+	} else {
+		ours = theirs = 0;
 	}
 
-	if (show_upstream_ref) {
+	if (branch->merge && branch->merge[0] && branch->merge[0]->dst) {
 		ref = shorten_unambiguous_ref(branch->merge[0]->dst, 0);
 		if (want_color(branch_use_color))
 			strbuf_addf(&fancy, "%s%s%s",
@@ -448,39 +454,23 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
 		else
 			strbuf_addstr(&fancy, ref);
 	}
+	if (!fancy.len)
+		return;
 
-	if (upstream_is_gone) {
-		if (show_upstream_ref)
-			strbuf_addf(stat, _("[%s: gone]"), fancy.buf);
-		else
-			added_decoration = 0;
-	} else if (!ours && !theirs) {
-		if (show_upstream_ref)
-			strbuf_addf(stat, _("[%s]"), fancy.buf);
-		else
-			added_decoration = 0;
-	} else if (!ours) {
-		if (show_upstream_ref)
-			strbuf_addf(stat, _("[%s: behind %d]"), fancy.buf, theirs);
-		else
-			strbuf_addf(stat, _("[behind %d]"), theirs);
+	if (upstream_is_gone)
+		strbuf_addf(stat, _("[%s: gone]"), fancy.buf);
+	else if (!ours && !theirs)
+		strbuf_addf(stat, _("[%s]"), fancy.buf);
+	else if (!ours)
+		strbuf_addf(stat, _("[%s: behind %d]"), fancy.buf, theirs);
+	else if (!theirs)
+		strbuf_addf(stat, _("[%s: ahead %d]"), fancy.buf, ours);
+	else
+		strbuf_addf(stat, _("[%s: ahead %d, behind %d]"),
+			    fancy.buf, ours, theirs);
 
-	} else if (!theirs) {
-		if (show_upstream_ref)
-			strbuf_addf(stat, _("[%s: ahead %d]"), fancy.buf, ours);
-		else
-			strbuf_addf(stat, _("[ahead %d]"), ours);
-	} else {
-		if (show_upstream_ref)
-			strbuf_addf(stat, _("[%s: ahead %d, behind %d]"),
-				    fancy.buf, ours, theirs);
-		else
-			strbuf_addf(stat, _("[ahead %d, behind %d]"),
-				    ours, theirs);
-	}
 	strbuf_release(&fancy);
-	if (added_decoration)
-		strbuf_addch(stat, ' ');
+	strbuf_addch(stat, ' ');
 	free(ref);
 }
 
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
index 7ac8fd0..af26db5 100755
--- a/t/t6040-tracking-info.sh
+++ b/t/t6040-tracking-info.sh
@@ -41,12 +41,12 @@ test_expect_success setup '
 
 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 g
-b6 c
+b1 [origin/master] d
+b2 [origin/master] d
+b3 [origin/master] b
+b4 [origin/master] f
+b5 [brokenbase] g
+b6 [origin/master] c
 EOF
 
 test_expect_success 'branch -v' '
-- 
1.9.1+fc3.9.gc73078e

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2014-04-20 21:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-20 20:58 [PATCH try3] branch: reorganize verbose options Felipe Contreras

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).