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>
Albert Yale committed
Jan 23, 2012 at 18:52 UTC
50dd0f2fd910d9973760db052897ee8e73ed2f1f
2 files changed
+25
-2
builtin/grep.c
+3
-2
@@ -1034,8 +1034,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
1034
1035
#ifndef NO_PTHREADS
1036
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))
1040
skip_first_line = 1;
1041
start_threads(&opt);
1042
}
t/t7810-grep.sh
+22
@@ -245,6 +245,28 @@ do
245
'
246
done
247
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
+
270
cat >expected <<EOF
271
file:foo mmap bar_mmap
272
EOF