From: René Scharfe Date: Sun, 5 Jun 2011 15:24:25 +0000 (+0200) Subject: grep: add --break X-Git-Tag: v1.7.7-rc0~119^2~1 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=a8f0e7649eba3ce78e1f09fc4dcbb2c3fcc3d866;p=git.git grep: add --break With --break, an empty line is printed between matches from different files, increasing readability. This option is taken from ack (http://betterthangrep.com/). Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt index e150c77cf..dea7cad0c 100644 --- a/Documentation/git-grep.txt +++ b/Documentation/git-grep.txt @@ -148,6 +148,9 @@ OPTIONS gives the default to color output. Same as `--color=never`. +--break:: + Print an empty line between matches from different files. + -[ABC] :: Show `context` trailing (`A` -- after), or leading (`B` -- before), or both (`C` -- context) lines, and place a diff --git a/builtin/grep.c b/builtin/grep.c index 0d5a90b94..42bb87f54 100644 --- a/builtin/grep.c +++ b/builtin/grep.c @@ -822,6 +822,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix) OPT_BOOLEAN('c', "count", &opt.count, "show the number of matches instead of matching lines"), OPT__COLOR(&opt.color, "highlight matches"), + OPT_BOOLEAN(0, "break", &opt.file_break, + "print empty line between matches from different files"), OPT_GROUP(""), OPT_CALLBACK('C', NULL, &opt, "n", "show context lines before and after matches", @@ -976,7 +978,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix) use_threads = 0; if (use_threads) { - if (opt.pre_context || opt.post_context) + if (opt.pre_context || opt.post_context || opt.file_break) skip_first_line = 1; start_threads(&opt); } diff --git a/grep.c b/grep.c index 3f15085d0..b0b860a98 100644 --- a/grep.c +++ b/grep.c @@ -721,7 +721,10 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol, int rest = eol - bol; char *line_color = NULL; - if (opt->pre_context || opt->post_context) { + if (opt->file_break && opt->last_shown == 0) { + if (opt->show_hunk_mark) + opt->output(opt, "\n", 1); + } else if (opt->pre_context || opt->post_context) { if (opt->last_shown == 0) { if (opt->show_hunk_mark) { output_color(opt, "--", 2, opt->color_sep); @@ -941,7 +944,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name, if (!opt->output) opt->output = std_output; - if (opt->pre_context || opt->post_context) { + if (opt->pre_context || opt->post_context || opt->file_break) { /* Show hunk marks, except for the first file. */ if (opt->last_shown) opt->show_hunk_mark = 1; diff --git a/grep.h b/grep.h index cd055cdfa..638bee848 100644 --- a/grep.h +++ b/grep.h @@ -110,6 +110,7 @@ struct grep_opt { unsigned post_context; unsigned last_shown; int show_hunk_mark; + int file_break; void *priv; void (*output)(struct grep_opt *opt, const void *data, size_t size); diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh index 539a8fe6e..f55793e3c 100755 --- a/t/t7810-grep.sh +++ b/t/t7810-grep.sh @@ -746,4 +746,32 @@ test_expect_success 'grep --color, separator' ' test_cmp expected actual ' +cat >expected <actual && + test_cmp expected actual +' + +cat >expected <actual && + test_cmp expected actual +' + test_done