Skip to content

Commit 34d820e

Browse files
peffgitster
authored andcommitted
branch: use BRANCH_COLOR_LOCAL in ref-filter format
Since 949af06 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5b3663 commit 34d820e

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

builtin/branch.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
331331
struct strbuf local = STRBUF_INIT;
332332
struct strbuf remote = STRBUF_INIT;
333333

334-
strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else) %%(end)",
335-
branch_get_color(BRANCH_COLOR_CURRENT));
334+
strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else) %s%%(end)",
335+
branch_get_color(BRANCH_COLOR_CURRENT),
336+
branch_get_color(BRANCH_COLOR_LOCAL));
336337
strbuf_addf(&remote, " ");
337338

338339
if (filter->verbose) {

t/t3205-branch-color.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/sh
2+
3+
test_description='basic branch output coloring'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'set up some sample branches' '
7+
test_commit foo &&
8+
git update-ref refs/remotes/origin/master HEAD &&
9+
git update-ref refs/heads/other HEAD
10+
'
11+
12+
# choose non-default colors to make sure config
13+
# is taking effect
14+
test_expect_success 'set up some color config' '
15+
git config color.branch always &&
16+
git config color.branch.local blue &&
17+
git config color.branch.remote yellow &&
18+
git config color.branch.current cyan
19+
'
20+
21+
test_expect_success 'regular output shows colors' '
22+
cat >expect <<-\EOF &&
23+
* <CYAN>master<RESET>
24+
<BLUE>other<RESET>
25+
<YELLOW>remotes/origin/master<RESET>
26+
EOF
27+
git branch -a >actual.raw &&
28+
test_decode_color <actual.raw >actual &&
29+
test_cmp expect actual
30+
'
31+
32+
test_expect_success 'verbose output shows colors' '
33+
oid=$(git rev-parse --short HEAD) &&
34+
cat >expect <<-EOF &&
35+
* <CYAN>master <RESET> $oid foo
36+
<BLUE>other <RESET> $oid foo
37+
<YELLOW>remotes/origin/master<RESET> $oid foo
38+
EOF
39+
git branch -v -a >actual.raw &&
40+
test_decode_color <actual.raw >actual &&
41+
test_cmp expect actual
42+
'
43+
44+
test_done

0 commit comments

Comments
 (0)