Skip to content

Commit e7b082a

Browse files
nazrigitster
authored andcommitted
grep -O: Do not pass color sequences as filenames to pager
With a .gitconfig like this: [color] ui = auto [color "grep"] filename = magenta if stdout is a terminal, the grep machinery will output the color sequence \e[36m before each filename in its output. In the case of "git grep -O foo", output is argv for the pager. Disable color when calling the grep machinery in this case. Signed-off-by: Nazri Ramliy <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0c72cea commit e7b082a

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

builtin/grep.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
997997
if (show_in_pager == default_pager)
998998
show_in_pager = git_pager(1);
999999
if (show_in_pager) {
1000+
opt.color = 0;
10001001
opt.name_only = 1;
10011002
opt.null_following_name = 1;
10021003
opt.output_priv = &path_list;

t/t7811-grep-open.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ test_expect_success 'modified file' '
125125
test_cmp empty out
126126
'
127127

128+
test_config() {
129+
git config "$1" "$2" &&
130+
test_when_finished "git config --unset $1"
131+
}
132+
133+
test_expect_success 'copes with color settings' '
134+
rm -f actual &&
135+
echo grep.h >expect &&
136+
test_config color.grep always &&
137+
test_config color.grep.filename yellow &&
138+
test_config color.grep.separator green &&
139+
git grep -O'\''printf "%s\n" >actual'\'' GREP_AND &&
140+
test_cmp expect actual
141+
'
142+
128143
test_expect_success 'run from subdir' '
129144
rm -f actual &&
130145
echo grep.c >expect &&

0 commit comments

Comments
 (0)