Skip to content

Commit ac5bbc0

Browse files
committed
branch: honor --abbrev/--no-abbrev in --list mode
When the "branch --list" command was converted to use the --format facility from the ref-filter API, we forgot to honor the --abbrev setting in the default output format and instead used a hardcoded "7". Signed-off-by: Junio C Hamano <[email protected]>
1 parent 44a6b6c commit ac5bbc0

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

builtin/branch.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,18 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
335335
branch_get_color(BRANCH_COLOR_CURRENT));
336336

337337
if (filter->verbose) {
338+
struct strbuf obname = STRBUF_INIT;
339+
340+
if (filter->abbrev < 0)
341+
strbuf_addf(&obname, "%%(objectname:short)");
342+
else if (!filter->abbrev)
343+
strbuf_addf(&obname, "%%(objectname)");
344+
else
345+
strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev);
346+
338347
strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
339348
strbuf_addf(&local, "%s", branch_get_color(BRANCH_COLOR_RESET));
340-
strbuf_addf(&local, " %%(objectname:short=7) ");
349+
strbuf_addf(&local, " %s ", obname.buf);
341350

342351
if (filter->verbose > 1)
343352
strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
@@ -346,10 +355,12 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
346355
else
347356
strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
348357

349-
strbuf_addf(&remote, "%s%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s%%(if)%%(symref)%%(then) -> %%(symref:short)"
350-
"%%(else) %%(objectname:short=7) %%(contents:subject)%%(end)",
358+
strbuf_addf(&remote, "%s%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
359+
"%%(if)%%(symref)%%(then) -> %%(symref:short)"
360+
"%%(else) %s %%(contents:subject)%%(end)",
351361
branch_get_color(BRANCH_COLOR_REMOTE), maxwidth, quote_literal_for_format(remote_prefix),
352-
branch_get_color(BRANCH_COLOR_RESET));
362+
branch_get_color(BRANCH_COLOR_RESET), obname.buf);
363+
strbuf_release(&obname);
353364
} else {
354365
strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
355366
branch_get_color(BRANCH_COLOR_RESET));

t/t3200-branch.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,31 @@ test_expect_success 'git branch --list -d t should fail' '
207207
test_path_is_missing .git/refs/heads/t
208208
'
209209

210+
test_expect_success 'git branch --list -v with --abbrev' '
211+
test_when_finished "git branch -D t" &&
212+
git branch t &&
213+
git branch -v --list t >actual.default &&
214+
git branch -v --list --abbrev t >actual.abbrev &&
215+
test_cmp actual.default actual.abbrev &&
216+
217+
git branch -v --list --no-abbrev t >actual.noabbrev &&
218+
git branch -v --list --abbrev=0 t >actual.0abbrev &&
219+
test_cmp actual.noabbrev actual.0abbrev &&
220+
221+
git branch -v --list --abbrev=36 t >actual.36abbrev &&
222+
# how many hexdigits are used?
223+
read name objdefault rest <actual.abbrev &&
224+
read name obj36 rest <actual.36abbrev &&
225+
objfull=$(git rev-parse --verify t) &&
226+
227+
# are we really getting abbreviations?
228+
test "$obj36" != "$objdefault" &&
229+
expr "$obj36" : "$objdefault" >/dev/null &&
230+
test "$objfull" != "$obj36" &&
231+
expr "$objfull" : "$obj36" >/dev/null
232+
233+
'
234+
210235
test_expect_success 'git branch --column' '
211236
COLUMNS=81 git branch --column=column >actual &&
212237
cat >expected <<\EOF &&

0 commit comments

Comments
 (0)