branch: fix --verbose output column alignment

Commit f2e0873 (branch: report invalid tracking branch as gone) removed an early return from fill_tracking_info() in the path taken when 'git branch -v' lists a branch in sync with its upstream. This resulted in an unconditionally added space in front of the subject line: $ git branch -v * master f5eb3da commit pushed to upstream topic f935eb6 unpublished topic Instead, only add the trailing space if a decoration have been added. To catch this kind of whitespace breakage in the tests, be a bit less smart when filtering the output through sed. Signed-off-by: Torstein Hegge <hegge@resisty.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Torstein Hegge committed Nov 14, 2013 at 19:18 UTC 6b364d48f2e72d6c37115e6aa4fe769a523dd1d4
2 files changed +20 -12
builtin/branch.c
+7 -1
@@ -424,6 +424,7 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
424 struct branch *branch = branch_get(branch_name);
425 struct strbuf fancy = STRBUF_INIT;
426 int upstream_is_gone = 0;
427 + int added_decoration = 1;
428
429 switch (stat_tracking_info(branch, &ours, &theirs)) {
430 case 0:
@@ -451,9 +452,13 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
452 if (upstream_is_gone) {
453 if (show_upstream_ref)
454 strbuf_addf(stat, _("[%s: gone]"), fancy.buf);
455 + else
456 + added_decoration = 0;
457 } else if (!ours && !theirs) {
458 if (show_upstream_ref)
459 strbuf_addf(stat, _("[%s]"), fancy.buf);
460 + else
461 + added_decoration = 0;
462 } else if (!ours) {
463 if (show_upstream_ref)
464 strbuf_addf(stat, _("[%s: behind %d]"), fancy.buf, theirs);
@@ -474,7 +479,8 @@ static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
479 ours, theirs);
480 }
481 strbuf_release(&fancy);
477 - strbuf_addch(stat, ' ');
482 + if (added_decoration)
483 + strbuf_addch(stat, ' ');
484 free(ref);
485 }
486
t/t6040-tracking-info.sh
+13 -11
@@ -39,12 +39,14 @@ test_expect_success setup '
39 advance h
40 '
41
42 -script='s/^..\(b.\)[ 0-9a-f]*\[\([^]]*\)\].*/\1 \2/p'
42 +script='s/^..\(b.\) *[0-9a-f]* \(.*\)$/\1 \2/p'
43 cat >expect <<\EOF
44 -b1 ahead 1, behind 1
45 -b2 ahead 1, behind 1
46 -b3 behind 1
47 -b4 ahead 2
44 +b1 [ahead 1, behind 1] d
45 +b2 [ahead 1, behind 1] d
46 +b3 [behind 1] b
47 +b4 [ahead 2] f
48 +b5 g
49 +b6 c
50 EOF
51
52 test_expect_success 'branch -v' '
@@ -57,12 +59,12 @@ test_expect_success 'branch -v' '
59 '
60
61 cat >expect <<\EOF
60 -b1 origin/master: ahead 1, behind 1
61 -b2 origin/master: ahead 1, behind 1
62 -b3 origin/master: behind 1
63 -b4 origin/master: ahead 2
64 -b5 brokenbase: gone
65 -b6 origin/master
62 +b1 [origin/master: ahead 1, behind 1] d
63 +b2 [origin/master: ahead 1, behind 1] d
64 +b3 [origin/master: behind 1] b
65 +b4 [origin/master: ahead 2] f
66 +b5 [brokenbase: gone] g
67 +b6 [origin/master] c
68 EOF
69
70 test_expect_success 'branch -vv' '