From c5f9d0106cea2aa558969706a9b5303910b4c6cb Mon Sep 17 00:00:00 2001 From: Felipe Contreras Date: Wed, 28 Aug 2013 18:52:51 -0500 Subject: [PATCH] branch: reorganize verbose options `git branch -v` before: fc/branch/nice-verbose 4761939 [ahead 1] branch: reorganize verbose options `git branch -v` after: fc/branch/nice-verbose 4761939 [master] 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 1.28s user 0.02s system 99% cpu 1.299 total After: git branch -v 0.01s user 0.01s system 87% cpu 0.019 total Signed-off-by: Felipe Contreras --- builtin/branch.c | 70 +++++++++++++++++--------------------- t/t3201-branch-contains.sh | 2 +- t/t6040-tracking-info.sh | 12 +++---- 3 files changed, 38 insertions(+), 46 deletions(-) diff --git a/builtin/branch.c b/builtin/branch.c index 7b45b6bd6b..97280ca756 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -272,23 +272,31 @@ static int delete_branches(int argc, const char **argv, int force, int kinds, } 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); - const char *upstream; + const char *upstream = NULL; struct strbuf fancy = STRBUF_INIT; int upstream_is_gone = 0; - int added_decoration = 1; - if (stat_tracking_info(branch, &ours, &theirs, &upstream) < 0) { - if (!upstream) - return; - upstream_is_gone = 1; + if (!branch) + return; + + if (show_tracking) { + if (stat_tracking_info(branch, &ours, &theirs, &upstream) < 0) { + if (!upstream) + return; + upstream_is_gone = 1; + } + } else { + if (branch->merge && branch->merge[0]) + upstream = branch->merge[0]->dst; + ours = theirs = 0; } - if (show_upstream_ref) { + if (upstream) { ref = shorten_unambiguous_ref(upstream, 0); if (want_color(branch_use_color)) strbuf_addf(&fancy, "%s%s%s", @@ -297,39 +305,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) + 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); - 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); - - } 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/t3201-branch-contains.sh b/t/t3201-branch-contains.sh index 912a6635a8..eeb238d05c 100755 --- a/t/t3201-branch-contains.sh +++ b/t/t3201-branch-contains.sh @@ -153,7 +153,7 @@ test_expect_success 'branch --merged with --verbose' ' git branch --verbose --merged topic >actual && cat >expect <<-\EOF && master c77a0a9 second on master - * topic 2c939f4 [ahead 1] foo + * topic 2c939f4 [master] foo zzz c77a0a9 second on master EOF test_cmp expect actual diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh index 3d5c238c81..962ac3b0ed 100755 --- a/t/t6040-tracking-info.sh +++ b/t/t6040-tracking-info.sh @@ -40,12 +40,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' ' -- 2.32.0.93.g670b81a890