branch: use BRANCH_COLOR_LOCAL in ref-filter format
authorJeff King <peff@peff.net>
Sun, 9 Jul 2017 09:59:33 +0000 (05:59 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sun, 9 Jul 2017 16:24:42 +0000 (09:24 -0700)
Since 949af0684 (branch: use ref-filter printing APIs,
2017-01-10), git-branch's output is generated by passing a
custom format to the ref-filter code. This format forgot to
pass BRANCH_COLOR_LOCAL, meaning that local branches
(besides the current one) were never colored at all.

We can add it in the %(if) block where we decide whether the
branch is "current" or merely "local". Note that this means
the current/local coloring is either/or. You can't set:

[color "branch"]
local = blue
current = bold

and expect the current branch to be "bold blue". This
matches the pre-949af0684 behavior.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/branch.c
t/t3205-branch-color.sh [new file with mode: 0755]
index 5f8b5ada8e209de07551412de7f93d7c04168976..b660ec558e5009e00b82da19315245617ff00431 100644 (file)
@@ -331,8 +331,9 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
        struct strbuf local = STRBUF_INIT;
        struct strbuf remote = STRBUF_INIT;
 
-       strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)  %%(end)",
-                   branch_get_color(BRANCH_COLOR_CURRENT));
+       strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)  %s%%(end)",
+                   branch_get_color(BRANCH_COLOR_CURRENT),
+                   branch_get_color(BRANCH_COLOR_LOCAL));
        strbuf_addf(&remote, "  ");
 
        if (filter->verbose) {
diff --git a/t/t3205-branch-color.sh b/t/t3205-branch-color.sh
new file mode 100755 (executable)
index 0000000..9343550
--- /dev/null
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+test_description='basic branch output coloring'
+. ./test-lib.sh
+
+test_expect_success 'set up some sample branches' '
+       test_commit foo &&
+       git update-ref refs/remotes/origin/master HEAD &&
+       git update-ref refs/heads/other HEAD
+'
+
+# choose non-default colors to make sure config
+# is taking effect
+test_expect_success 'set up some color config' '
+       git config color.branch always &&
+       git config color.branch.local blue &&
+       git config color.branch.remote yellow &&
+       git config color.branch.current cyan
+'
+
+test_expect_success 'regular output shows colors' '
+       cat >expect <<-\EOF &&
+       * <CYAN>master<RESET>
+         <BLUE>other<RESET>
+         <YELLOW>remotes/origin/master<RESET>
+       EOF
+       git branch -a >actual.raw &&
+       test_decode_color <actual.raw >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'verbose output shows colors' '
+       oid=$(git rev-parse --short HEAD) &&
+       cat >expect <<-EOF &&
+       * <CYAN>master               <RESET> $oid foo
+         <BLUE>other                <RESET> $oid foo
+         <YELLOW>remotes/origin/master<RESET> $oid foo
+       EOF
+       git branch -v -a >actual.raw &&
+       test_decode_color <actual.raw >actual &&
+       test_cmp expect actual
+'
+
+test_done