Skip to content

Commit 55cccf4

Browse files
peffgitster
authored andcommitted
color_parse_mem: allow empty color spec
Prior to c2f41bf (color.c: fix color_parse_mem() with value_len == 0, 2017-01-19), the empty string was interpreted as a color "reset". This was an accidental outcome, and that commit turned it into an error. However, scripts may pass the empty string as a default value to "git config --get-color" to disable color when the value is not defined. The git-add--interactive script does this. As a result, the script is unusable since c2f41bf unless you have color.diff.plain defined (if it is defined, then we don't parse the empty default at all). Our test scripts didn't notice the recent breakage because they run without a terminal, and thus without color. They never hit this code path at all. And nobody noticed the original buggy "reset" behavior, because it was effectively a noop. Let's fix the code to have an empty color name produce an empty sequence of color codes. The tests need a few fixups: - we'll add a new test in t4026 to cover this case. But note that we need to tweak the color() helper. While we're there, let's factor out the literal ANSI ESC character. Otherwise it makes the diff quite hard to read. - we'll add a basic sanity-check in t4026 that "git add -p" works at all when color is enabled. That would have caught this bug, as well as any others that are specific to the color code paths. - 73c727d (log --graph: customize the graph lines with config log.graphColors, 2017-01-19) added a test to t4202 that checks some "invalid" graph color config. Since ",, blue" before yielded only "blue" as valid, and now yields "empty, empty, blue", we don't match the expected output. One way to fix this would be to change the expectation to the empty color strings. But that makes the test much less interesting, since we show only two graph lines, both of which would be colorless. Since the empty-string case is now covered by t4026, let's remove them entirely here. They're just in the way of the primary thing the test is supposed to be checking. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 73c727d commit 55cccf4

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

color.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,10 @@ int color_parse_mem(const char *value, int value_len, char *dst)
212212
len--;
213213
}
214214

215-
if (!len)
216-
return -1;
215+
if (!len) {
216+
dst[0] = '\0';
217+
return 0;
218+
}
217219

218220
if (!strncasecmp(ptr, "reset", len)) {
219221
xsnprintf(dst, end - dst, GIT_COLOR_RESET);

t/t3701-add-interactive.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,18 @@ test_expect_success 'patch mode ignores unmerged entries' '
380380
test_cmp expected diff
381381
'
382382

383+
test_expect_success 'diffs can be colorized' '
384+
git reset --hard &&
385+
386+
# force color even though the test script has no terminal
387+
test_config color.ui always &&
388+
389+
echo content >test &&
390+
printf y | git add -p >output 2>&1 &&
391+
392+
# We do not want to depend on the exact coloring scheme
393+
# git uses for diffs, so just check that we saw some kind of color.
394+
grep "$(printf "\\033")" output
395+
'
396+
383397
test_done

t/t4026-color.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
test_description='Test diff/status color escape codes'
77
. ./test-lib.sh
88

9+
ESC=$(printf '\033')
910
color()
1011
{
1112
actual=$(git config --get-color no.such.slot "$1") &&
12-
test "$actual" = "$2"
13+
test "$actual" = "${2:+$ESC}$2"
1314
}
1415

1516
invalid_color()
@@ -21,6 +22,10 @@ test_expect_success 'reset' '
2122
color "reset" "[m"
2223
'
2324

25+
test_expect_success 'empty color is empty' '
26+
color "" ""
27+
'
28+
2429
test_expect_success 'attribute before color name' '
2530
color "bold red" "[1;31m"
2631
'

t/t4202-log.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ cat > expect.colors <<\EOF
329329
EOF
330330

331331
test_expect_success 'log --graph with merge with log.graphColors' '
332-
test_config log.graphColors ",, blue,invalid-color, cyan, red , " &&
332+
test_config log.graphColors " blue,invalid-color, cyan, red , " &&
333333
git log --color=always --graph --date-order --pretty=tformat:%s |
334334
test_decode_color | sed "s/ *\$//" >actual &&
335335
test_cmp expect.colors actual

0 commit comments

Comments
 (0)