Skip to content

Commit 50dd0f2

Browse files
Albert Yalegitster
authored andcommitted
grep: fix -l/-L interaction with decoration lines
In threaded mode, git-grep emits file breaks (enabled with context, -W and --break) into the accumulation buffers even if they are not required. The output collection thread then uses skip_first_line to skip the first such line in the output, which would otherwise be at the very top. This is wrong when the user also specified -l/-L/-c, in which case every line is relevant. While arguably giving these options together doesn't make any sense, git-grep has always quietly accepted it. So do not skip anything in these cases. Signed-off-by: Albert Yale <[email protected]> Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ced7469 commit 50dd0f2

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

builtin/grep.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
10341034

10351035
#ifndef NO_PTHREADS
10361036
if (use_threads) {
1037-
if (opt.pre_context || opt.post_context || opt.file_break ||
1038-
opt.funcbody)
1037+
if (!(opt.name_only || opt.unmatch_name_only || opt.count)
1038+
&& (opt.pre_context || opt.post_context ||
1039+
opt.file_break || opt.funcbody))
10391040
skip_first_line = 1;
10401041
start_threads(&opt);
10411042
}

t/t7810-grep.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,28 @@ do
245245
'
246246
done
247247

248+
cat >expected <<EOF
249+
file
250+
EOF
251+
test_expect_success 'grep -l -C' '
252+
git grep -l -C1 foo >actual &&
253+
test_cmp expected actual
254+
'
255+
256+
cat >expected <<EOF
257+
file:5
258+
EOF
259+
test_expect_success 'grep -l -C' '
260+
git grep -c -C1 foo >actual &&
261+
test_cmp expected actual
262+
'
263+
264+
test_expect_success 'grep -L -C' '
265+
git ls-files >expected &&
266+
git grep -L -C1 nonexistent_string >actual &&
267+
test_cmp expected actual
268+
'
269+
248270
cat >expected <<EOF
249271
file:foo mmap bar_mmap
250272
EOF

0 commit comments

Comments
 (0)