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 <surfingalbert@gmail.com>
Signed-off-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Albert Yale 2012-01-23 18:52:44 +01:00 committed by Junio C Hamano
parent ced7469f07
commit 50dd0f2fd9
2 changed files with 25 additions and 2 deletions

View File

@ -1034,8 +1034,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)


#ifndef NO_PTHREADS #ifndef NO_PTHREADS
if (use_threads) { if (use_threads) {
if (opt.pre_context || opt.post_context || opt.file_break || if (!(opt.name_only || opt.unmatch_name_only || opt.count)
opt.funcbody) && (opt.pre_context || opt.post_context ||
opt.file_break || opt.funcbody))
skip_first_line = 1; skip_first_line = 1;
start_threads(&opt); start_threads(&opt);
} }

View File

@ -245,6 +245,28 @@ do
' '
done done


cat >expected <<EOF
file
EOF
test_expect_success 'grep -l -C' '
git grep -l -C1 foo >actual &&
test_cmp expected actual
'

cat >expected <<EOF
file:5
EOF
test_expect_success 'grep -l -C' '
git grep -c -C1 foo >actual &&
test_cmp expected actual
'

test_expect_success 'grep -L -C' '
git ls-files >expected &&
git grep -L -C1 nonexistent_string >actual &&
test_cmp expected actual
'

cat >expected <<EOF cat >expected <<EOF
file:foo mmap bar_mmap file:foo mmap bar_mmap
EOF EOF