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 <gitster@pobox.com>

Junio C Hamano committed Mar 8, 2017 at 14:13 UTC ac5bbc02b8c3781d517f1414655270b3fa38b028
2 files changed +40 -4
builtin/branch.c
+15 -4
@@ -335,9 +335,18 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
335 branch_get_color(BRANCH_COLOR_CURRENT));
336
337 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 +
347 strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
348 strbuf_addf(&local, "%s", branch_get_color(BRANCH_COLOR_RESET));
340 - strbuf_addf(&local, " %%(objectname:short=7) ");
349 + strbuf_addf(&local, " %s ", obname.buf);
350
351 if (filter->verbose > 1)
352 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
355 else
356 strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
357
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)",
361 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);
364 } else {
365 strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
366 branch_get_color(BRANCH_COLOR_RESET));
t/t3200-branch.sh
+25
@@ -207,6 +207,31 @@ test_expect_success 'git branch --list -d t should fail' '
207 test_path_is_missing .git/refs/heads/t
208 '
209
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 +
235 test_expect_success 'git branch --column' '
236 COLUMNS=81 git branch --column=column >actual &&
237 cat >expected <<\EOF &&