Skip to content

Commit 046802d

Browse files
René Scharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5dd06d3 commit 046802d

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

builtin-grep.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ static int flush_grep(struct grep_opt *opt,
278278
argc -= 2;
279279
}
280280

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+
281292
status = exec_grep(argc, argv);
282293

283294
if (kept_0) {

grep.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,12 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
491491
int rest = eol - bol;
492492

493493
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)
495500
fputs("--\n", stdout);
496501
}
497502
opt->last_shown = lno;

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ struct grep_opt {
8585
unsigned pre_context;
8686
unsigned post_context;
8787
unsigned last_shown;
88+
int show_hunk_mark;
8889
};
8990

9091
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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ test_expect_success 'grep -e A --and --not -e B' '
155155
test_cmp expected actual
156156
'
157157

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+
158180
test_expect_success 'log grep setup' '
159181
echo a >>file &&
160182
test_tick &&

0 commit comments

Comments
 (0)