git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Felipe Contreras <felipe.contreras@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
	"Jeff King" <peff@peff.net>,
	"Michael J Gruber" <git@drmicha.warpmail.net>,
	"Felipe Contreras" <felipe.contreras@gmail.com>
Subject: [PATCH try3] branch: reorganize verbose options
Date: Sun, 20 Apr 2014 15:58:34 -0500	[thread overview]
Message-ID: <1398027514-19399-1-git-send-email-felipe.contreras@gmail.com> (raw)

`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

                 reply	other threads:[~2014-04-20 21:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1398027514-19399-1-git-send-email-felipe.contreras@gmail.com \
    --to=felipe.contreras@gmail.com \
    --cc=git@drmicha.warpmail.net \
    --cc=git@vger.kernel.org \
    --cc=pclouds@gmail.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).