From a5b3663898d6e45a5d109ce89c516adc2fd4ded2 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sun, 9 Jul 2017 05:58:10 -0400 Subject: branch: only perform HEAD check for local branches When assembling the ref-filter format to show "git branch" output, we put the "%(if)%(HEAD)" conditional at the start of the overall format. But there's no point in checking whether a remote branch matches HEAD, as it never will. The check should go inside the local conditional; we assemble that format inside the "local" strbuf. By itself, this is just a minor optimization. But in a future patch, we'll need this refactoring to fix local-branch coloring. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/builtin/branch.c b/builtin/branch.c index 64d3e7d..5f8b5ad 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -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(&fmt, "%%(if)%%(HEAD)%%(then)* %s%%(else) %%(end)", + strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else) %%(end)", branch_get_color(BRANCH_COLOR_CURRENT)); + strbuf_addf(&remote, " "); if (filter->verbose) { struct strbuf obname = STRBUF_INIT; -- cgit v0.10.2-6-g49f6 From 34d820ee336598d93f1fa619f517811ee0ef503d Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sun, 9 Jul 2017 05:59:33 -0400 Subject: branch: use BRANCH_COLOR_LOCAL in ref-filter format 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 Signed-off-by: Junio C Hamano diff --git a/builtin/branch.c b/builtin/branch.c index 5f8b5ad..b660ec5 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -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 index 0000000..9343550 --- /dev/null +++ b/t/t3205-branch-color.sh @@ -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 && + * master + other + remotes/origin/master + EOF + git branch -a >actual.raw && + test_decode_color actual && + test_cmp expect actual +' + +test_expect_success 'verbose output shows colors' ' + oid=$(git rev-parse --short HEAD) && + cat >expect <<-EOF && + * master $oid foo + other $oid foo + remotes/origin/master $oid foo + EOF + git branch -v -a >actual.raw && + test_decode_color actual && + test_cmp expect actual +' + +test_done -- cgit v0.10.2-6-g49f6 From 7ca260abfeab45a651f0cf58864a3a02da76cd1f Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sun, 9 Jul 2017 06:00:45 -0400 Subject: branch: set remote color in ref-filter branch immediately We set the current and local branch colors at the top of the build_format() function. Let's do the same for the remote color. This saves a little bit of repetition, but more importantly it puts all of the color-setting in the same place. That makes it easier to see that we are coloring all possibilities. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/builtin/branch.c b/builtin/branch.c index b660ec5..fd186fe 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -334,7 +334,8 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r 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, " "); + strbuf_addf(&remote, " %s", + branch_get_color(BRANCH_COLOR_REMOTE)); if (filter->verbose) { struct strbuf obname = STRBUF_INIT; @@ -357,17 +358,17 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r else strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)"); - strbuf_addf(&remote, "%s%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s" + strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s" "%%(if)%%(symref)%%(then) -> %%(symref:short)" "%%(else) %s %%(contents:subject)%%(end)", - branch_get_color(BRANCH_COLOR_REMOTE), maxwidth, quote_literal_for_format(remote_prefix), + maxwidth, quote_literal_for_format(remote_prefix), branch_get_color(BRANCH_COLOR_RESET), obname.buf); strbuf_release(&obname); } else { strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", branch_get_color(BRANCH_COLOR_RESET)); - strbuf_addf(&remote, "%s%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", - branch_get_color(BRANCH_COLOR_REMOTE), quote_literal_for_format(remote_prefix), + strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", + quote_literal_for_format(remote_prefix), branch_get_color(BRANCH_COLOR_RESET)); } -- cgit v0.10.2-6-g49f6 From 5b5c9c3e19eb6afcb987093c705bb5e2e80a63ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20R=2E=20Sede=C3=B1o?= Date: Mon, 10 Jul 2017 15:03:03 -0400 Subject: ref-filter.c: drop return from void function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sun's C compiler errors out on this pattern: void foo() { ... } void bar() { return foo(); } Signed-off-by: Alejandro R. SedeƱo Signed-off-by: Junio C Hamano diff --git a/ref-filter.c b/ref-filter.c index f4c6823..238888e 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -220,7 +220,7 @@ static void objectname_atom_parser(struct used_atom *atom, const char *arg) static void refname_atom_parser(struct used_atom *atom, const char *arg) { - return refname_atom_parser_internal(&atom->u.refname, arg, atom->name); + refname_atom_parser_internal(&atom->u.refname, arg, atom->name); } static align_type parse_align_position(const char *s) -- cgit v0.10.2-6-g49f6