Skip to content

Commit dcb347f

Browse files
ak2gitster
authored andcommitted
decorate: color each token separately
Wrap "tag:" prefixes and the arrows in "HEAD -> branch" decorations in their own color sequences. Otherwise, if --graph is used, tag names or arrows can end up uncolored when %w width formatting breaks a line just before them. This is because --graph resets the color after doing its drawing at the start of a line. Amend test t4207-log-decoration-colors.sh accordingly. Signed-off-by: Andy Koppe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b87a9a2 commit dcb347f

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

log-tree.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,26 +342,34 @@ void format_decorations(struct strbuf *sb,
342342
* appeared, skipping the entry for current.
343343
*/
344344
if (decoration != current_and_HEAD) {
345+
const char *color =
346+
decorate_get_color(use_color, decoration->type);
347+
345348
if (*prefix) {
346349
strbuf_addstr(sb, color_commit);
347350
strbuf_addstr(sb, prefix);
348351
strbuf_addstr(sb, color_reset);
349352
}
350353

351-
strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
352-
if (decoration->type == DECORATION_REF_TAG)
354+
if (decoration->type == DECORATION_REF_TAG) {
355+
strbuf_addstr(sb, color);
353356
strbuf_addstr(sb, "tag: ");
357+
strbuf_addstr(sb, color_reset);
358+
}
354359

360+
strbuf_addstr(sb, color);
355361
show_name(sb, decoration);
362+
strbuf_addstr(sb, color_reset);
356363

357364
if (current_and_HEAD &&
358365
decoration->type == DECORATION_REF_HEAD) {
366+
strbuf_addstr(sb, color);
359367
strbuf_addstr(sb, " -> ");
360368
strbuf_addstr(sb, color_reset);
361369
strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
362370
show_name(sb, current_and_HEAD);
371+
strbuf_addstr(sb, color_reset);
363372
}
364-
strbuf_addstr(sb, color_reset);
365373

366374
prefix = separator;
367375
}

t/t4207-log-decoration-colors.sh

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@ cmp_filtered_decorations () {
5353
# to this test since it does not contain any decoration, hence --first-parent
5454
test_expect_success 'commit decorations colored correctly' '
5555
cat >expect <<-EOF &&
56-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
57-
${c_reset}${c_branch}main${c_reset}${c_commit}, \
58-
${c_reset}${c_tag}tag: v1.0${c_reset}${c_commit}, \
59-
${c_reset}${c_tag}tag: B${c_reset}${c_commit})${c_reset} B
60-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A1${c_reset}${c_commit}, \
56+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD${c_reset}\
57+
${c_HEAD} -> ${c_reset}${c_branch}main${c_reset}${c_commit}, \
58+
${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
59+
${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
60+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
61+
${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
6162
${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
62-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_stash}refs/stash${c_reset}${c_commit})${c_reset} \
63-
On main: Changes to A.t
64-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
63+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
64+
${c_stash}refs/stash${c_reset}${c_commit})${c_reset} On main: Changes to A.t
65+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
66+
${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
6567
EOF
6668
6769
git log --first-parent --no-abbrev --decorate --oneline --color=always --all >actual &&
@@ -76,12 +78,14 @@ test_expect_success 'test coloring with replace-objects' '
7678
git replace HEAD~1 HEAD~2 &&
7779
7880
cat >expect <<-EOF &&
79-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
80-
${c_reset}${c_branch}main${c_reset}${c_commit}, \
81-
${c_reset}${c_tag}tag: D${c_reset}${c_commit})${c_reset} D
82-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: C${c_reset}${c_commit}, \
81+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD${c_reset}\
82+
${c_HEAD} -> ${c_reset}${c_branch}main${c_reset}${c_commit}, \
83+
${c_reset}${c_tag}tag: ${c_reset}${c_tag}D${c_reset}${c_commit})${c_reset} D
84+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
85+
${c_tag}tag: ${c_reset}${c_tag}C${c_reset}${c_commit}, \
8386
${c_reset}${c_grafted}replaced${c_reset}${c_commit})${c_reset} B
84-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
87+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
88+
${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
8589
EOF
8690
8791
git log --first-parent --no-abbrev --decorate --oneline --color=always HEAD >actual &&
@@ -100,13 +104,15 @@ test_expect_success 'test coloring with grafted commit' '
100104
git replace --graft HEAD HEAD~2 &&
101105
102106
cat >expect <<-EOF &&
103-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
104-
${c_reset}${c_branch}main${c_reset}${c_commit}, \
105-
${c_reset}${c_tag}tag: D${c_reset}${c_commit}, \
107+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD${c_reset}\
108+
${c_HEAD} -> ${c_reset}${c_branch}main${c_reset}${c_commit}, \
109+
${c_reset}${c_tag}tag: ${c_reset}${c_tag}D${c_reset}${c_commit}, \
106110
${c_reset}${c_grafted}replaced${c_reset}${c_commit})${c_reset} D
107-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: v1.0${c_reset}${c_commit}, \
108-
${c_reset}${c_tag}tag: B${c_reset}${c_commit})${c_reset} B
109-
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
111+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
112+
${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
113+
${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
114+
${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
115+
${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
110116
EOF
111117
112118
git log --first-parent --no-abbrev --decorate --oneline --color=always HEAD >actual &&

0 commit comments

Comments
 (0)