Skip to content

Commit 75375ea

Browse files
committed
Merge branch 'jk/branch-shortening-funny-symrefs' into maint
A change back in version 2.7 to "git branch" broke display of a symbolic ref in a non-standard place in the refs/ hierarchy (we expect symbolic refs to appear in refs/remotes/*/HEAD to point at the primary branch the remote has, and as .git/HEAD to point at the branch we locally checked out). * jk/branch-shortening-funny-symrefs: branch: fix shortening of non-remote symrefs
2 parents a3fa565 + 95c38fb commit 75375ea

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

builtin/branch.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -399,22 +399,25 @@ static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth,
399399
int current = 0;
400400
int color;
401401
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
402-
const char *prefix = "";
402+
const char *prefix_to_show = "";
403+
const char *prefix_to_skip = NULL;
403404
const char *desc = item->refname;
404405
char *to_free = NULL;
405406

406407
switch (item->kind) {
407408
case FILTER_REFS_BRANCHES:
408-
skip_prefix(desc, "refs/heads/", &desc);
409+
prefix_to_skip = "refs/heads/";
410+
skip_prefix(desc, prefix_to_skip, &desc);
409411
if (!filter->detached && !strcmp(desc, head))
410412
current = 1;
411413
else
412414
color = BRANCH_COLOR_LOCAL;
413415
break;
414416
case FILTER_REFS_REMOTES:
415-
skip_prefix(desc, "refs/remotes/", &desc);
417+
prefix_to_skip = "refs/remotes/";
418+
skip_prefix(desc, prefix_to_skip, &desc);
416419
color = BRANCH_COLOR_REMOTE;
417-
prefix = remote_prefix;
420+
prefix_to_show = remote_prefix;
418421
break;
419422
case FILTER_REFS_DETACHED_HEAD:
420423
desc = to_free = get_head_description();
@@ -431,7 +434,7 @@ static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth,
431434
color = BRANCH_COLOR_CURRENT;
432435
}
433436

434-
strbuf_addf(&name, "%s%s", prefix, desc);
437+
strbuf_addf(&name, "%s%s", prefix_to_show, desc);
435438
if (filter->verbose) {
436439
int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf);
437440
strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
@@ -442,8 +445,10 @@ static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth,
442445
name.buf, branch_get_color(BRANCH_COLOR_RESET));
443446

444447
if (item->symref) {
445-
skip_prefix(item->symref, "refs/remotes/", &desc);
446-
strbuf_addf(&out, " -> %s", desc);
448+
const char *symref = item->symref;
449+
if (prefix_to_skip)
450+
skip_prefix(symref, prefix_to_skip, &symref);
451+
strbuf_addf(&out, " -> %s", symref);
447452
}
448453
else if (filter->verbose)
449454
/* " f7c0c00 [ahead 58, behind 197] vcs-svn: drop obj_pool.h" */

t/t3203-branch-output.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,16 @@ test_expect_success 'ambiguous branch/tag not marked' '
184184
test_cmp expect actual
185185
'
186186

187+
test_expect_success 'local-branch symrefs shortened properly' '
188+
git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
189+
git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
190+
cat >expect <<-\EOF &&
191+
ref-to-branch -> branch-one
192+
ref-to-remote -> refs/remotes/origin/branch-one
193+
EOF
194+
git branch >actual.raw &&
195+
grep ref-to <actual.raw >actual &&
196+
test_cmp expect actual
197+
'
198+
187199
test_done

0 commit comments

Comments
 (0)