@matheusbsbs / matheus-bagliano / commits / 046802d015
Fork 0

grep: print context hunk marks between files

Print a hunk mark before matches from a new file are shown, in addition to the current behaviour of printing them if lines have been skipped. The result is easier to read, as (presumably unrelated) matches from different files are separated by a hunk mark. GNU grep does the same. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jul 2, 2009 at 00:03 UTC 046802d015b3be2e055ae68f29f76741023bc32d
4 files changed +40 -1
builtin-grep.c
+11
@@ -278,6 +278,17 @@ static int flush_grep(struct grep_opt *opt,
278 argc -= 2;
279 }
280
281 + if (opt->pre_context || opt->post_context) {
282 + /*
283 + * grep handles hunk marks between files, but we need to
284 + * do that ourselves between multiple calls.
285 + */
286 + if (opt->show_hunk_mark)
287 + write_or_die(1, "--\n", 3);
288 + else
289 + opt->show_hunk_mark = 1;
290 + }
291 +
292 status = exec_grep(argc, argv);
293
294 if (kept_0) {
grep.c
+6 -1
@@ -491,7 +491,12 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
491 int rest = eol - bol;
492
493 if (opt->pre_context || opt->post_context) {
494 - if (opt->last_shown && lno > opt->last_shown + 1)
494 + if (opt->last_shown == 0) {
495 + if (opt->show_hunk_mark)
496 + fputs("--\n", stdout);
497 + else
498 + opt->show_hunk_mark = 1;
499 + } else if (lno > opt->last_shown + 1)
500 fputs("--\n", stdout);
501 }
502 opt->last_shown = lno;
grep.h
+1
@@ -85,6 +85,7 @@ struct grep_opt {
85 unsigned pre_context;
86 unsigned post_context;
87 unsigned last_shown;
88 + int show_hunk_mark;
89 };
90
91 extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
t/t7002-grep.sh
+22
@@ -155,6 +155,28 @@ test_expect_success 'grep -e A --and --not -e B' '
155 test_cmp expected actual
156 '
157
158 +cat >expected <<EOF
159 +y:y yy
160 +--
161 +z:zzz
162 +EOF
163 +
164 +# Create 1024 file names that sort between "y" and "z" to make sure
165 +# the two files are handled by different calls to an external grep.
166 +# This depends on MAXARGS in builtin-grep.c being 1024 or less.
167 +c32="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v"
168 +test_expect_success 'grep -C1, hunk mark between files' '
169 + for a in $c32; do for b in $c32; do : >y-$a$b; done; done &&
170 + git add y-?? &&
171 + git grep -C1 "^[yz]" >actual &&
172 + test_cmp expected actual
173 +'
174 +
175 +test_expect_success 'grep -C1 --no-ext-grep, hunk mark between files' '
176 + git grep -C1 --no-ext-grep "^[yz]" >actual &&
177 + test_cmp expected actual
178 +'
179 +
180 test_expect_success 'log grep setup' '
181 echo a >>file &&
182 test_tick &&