Skip to content

Commit 95c38fb

Browse files
peffgitster
authored andcommitted
branch: fix shortening of non-remote symrefs
Commit aedcb7d (branch.c: use 'ref-filter' APIs, 2015-09-23) adjusted the symref-printing code to look like this: if (item->symref) { skip_prefix(item->symref, "refs/remotes/", &desc); strbuf_addf(&out, " -> %s", desc); } This has three bugs in it: 1. It always skips past "refs/remotes/", instead of skipping past the prefix associated with the branch we are showing (so commonly we see "refs/remotes/" for the refs/remotes/origin/HEAD symref, but the previous code would skip "refs/heads/" when showing a symref it found in refs/heads/. 2. If skip_prefix() does not match, it leaves "desc" untouched, and we show whatever happened to be in it (which is the refname from a call to skip_prefix() earlier in the function). 3. If we do match with skip_prefix(), we stomp on the "desc" variable, which is later passed to add_verbose_info(). We probably want to retain the original refname there (though it likely doesn't matter in practice, since after all, one points to the other). The fix to match the original code is fairly easy: record the prefix to strip based on item->kind, and use it here. However, since we already have a local variable named "prefix", let's give the two prefixes verbose names so we don't confuse them. Signed-off-by: Jeff King <[email protected]> Acked-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a0feb1b commit 95c38fb

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
@@ -393,22 +393,25 @@ static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth,
393393
int current = 0;
394394
int color;
395395
struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
396-
const char *prefix = "";
396+
const char *prefix_to_show = "";
397+
const char *prefix_to_skip = NULL;
397398
const char *desc = item->refname;
398399
char *to_free = NULL;
399400

400401
switch (item->kind) {
401402
case FILTER_REFS_BRANCHES:
402-
skip_prefix(desc, "refs/heads/", &desc);
403+
prefix_to_skip = "refs/heads/";
404+
skip_prefix(desc, prefix_to_skip, &desc);
403405
if (!filter->detached && !strcmp(desc, head))
404406
current = 1;
405407
else
406408
color = BRANCH_COLOR_LOCAL;
407409
break;
408410
case FILTER_REFS_REMOTES:
409-
skip_prefix(desc, "refs/remotes/", &desc);
411+
prefix_to_skip = "refs/remotes/";
412+
skip_prefix(desc, prefix_to_skip, &desc);
410413
color = BRANCH_COLOR_REMOTE;
411-
prefix = remote_prefix;
414+
prefix_to_show = remote_prefix;
412415
break;
413416
case FILTER_REFS_DETACHED_HEAD:
414417
desc = to_free = get_head_description();
@@ -425,7 +428,7 @@ static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth,
425428
color = BRANCH_COLOR_CURRENT;
426429
}
427430

428-
strbuf_addf(&name, "%s%s", prefix, desc);
431+
strbuf_addf(&name, "%s%s", prefix_to_show, desc);
429432
if (filter->verbose) {
430433
int utf8_compensation = strlen(name.buf) - utf8_strwidth(name.buf);
431434
strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
@@ -436,8 +439,10 @@ static void format_and_print_ref_item(struct ref_array_item *item, int maxwidth,
436439
name.buf, branch_get_color(BRANCH_COLOR_RESET));
437440

438441
if (item->symref) {
439-
skip_prefix(item->symref, "refs/remotes/", &desc);
440-
strbuf_addf(&out, " -> %s", desc);
442+
const char *symref = item->symref;
443+
if (prefix_to_skip)
444+
skip_prefix(symref, prefix_to_skip, &symref);
445+
strbuf_addf(&out, " -> %s", symref);
441446
}
442447
else if (filter->verbose)
443448
/* " 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)