Skip to content

Commit 14b9c2b

Browse files
peffgitster
authored andcommitted
log: handle --decorate-refs with userformat "%d"
In order to show ref decorations, we first have to load them. If you run: git log --decorate then git-log will recognize the option and load them up front via cmd_log_init(). Likewise if log.decorate is set. If you don't say --decorate explicitly, but do mention "%d" or "%D" in the output format, like so: git log --format=%d then this also works, because we lazy-load the ref decorations. This has been true since 3b3d443 (add '%d' pretty format specifier to show decoration, 2008-09-04), though the lazy-load was later moved into log-tree.c. But there's one problem: that lazy-load just uses the defaults; it doesn't take into account any --decorate-refs options (or its exclude variant, or their config). So this does not work: git log --decorate-refs=whatever --format=%d It will decorate using all refs, not just the specified ones. This has been true since --decorate-refs was added in 65516f5 (log: add option to choose which refs to decorate, 2017-11-21). Adding further confusion is that it _may_ work because of the auto-decoration feature. If that's in use (and it often is, as it's the default), then if the output is going to stdout, we do enable decorations early (and so load them up front, respecting the extra options). But otherwise we do not. So: git log --decorate-refs=whatever --format=%d >some-file would typically behave differently than it does when the output goes to the pager or terminal! The solution is simple: we should recognize in cmd_log_init() that we're going to show decorations, and make sure we load them there. We already check userformat_find_requirements(), so we can couple this with our existing code there. There are two new tests. The first shows off the actual fix. The second makes sure that our fix doesn't cause us to stomp on an existing --decorate option (see the new comment in the code, as well). Reported-by: Josh Rampersad <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent abe6bb3 commit 14b9c2b

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

builtin/log.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,22 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
245245
rev->abbrev_commit = 0;
246246
}
247247

248-
if (rev->commit_format == CMIT_FMT_USERFORMAT && !w.decorate)
249-
decoration_style = 0;
248+
if (rev->commit_format == CMIT_FMT_USERFORMAT) {
249+
if (!w.decorate) {
250+
/*
251+
* Disable decoration loading if the format will not
252+
* show them anyway.
253+
*/
254+
decoration_style = 0;
255+
} else if (!decoration_style) {
256+
/*
257+
* If we are going to show them, make sure we do load
258+
* them here, but taking care not to override a
259+
* specific style set by config or --decorate.
260+
*/
261+
decoration_style = DECORATE_SHORT_REFS;
262+
}
263+
}
250264

251265
if (decoration_style) {
252266
const struct string_list *config_exclude =

t/t4202-log.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,28 @@ test_expect_success 'decorate-refs-exclude and simplify-by-decoration' '
952952
test_cmp expect.decorate actual
953953
'
954954

955+
test_expect_success 'decorate-refs with implied decorate from format' '
956+
cat >expect <<-\EOF &&
957+
side-2 (tag: side-2)
958+
side-1
959+
EOF
960+
git log --no-walk --format="%s%d" \
961+
--decorate-refs="*side-2" side-1 side-2 \
962+
>actual &&
963+
test_cmp expect actual
964+
'
965+
966+
test_expect_success 'implied decorate does not override option' '
967+
cat >expect <<-\EOF &&
968+
side-2 (tag: refs/tags/side-2, refs/heads/side)
969+
side-1 (tag: refs/tags/side-1)
970+
EOF
971+
git log --no-walk --format="%s%d" \
972+
--decorate=full side-1 side-2 \
973+
>actual &&
974+
test_cmp expect actual
975+
'
976+
955977
test_expect_success 'log.decorate config parsing' '
956978
git log --oneline --decorate=full >expect.full &&
957979
git log --oneline --decorate=short >expect.short &&

0 commit comments

Comments
 (0)