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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 5, 2011 at 17:24 UTC a8f0e7649eba3ce78e1f09fc4dcbb2c3fcc3d866
5 files changed +40 -3
Documentation/git-grep.txt
+3
@@ -148,6 +148,9 @@ OPTIONS
148 gives the default to color output.
149 Same as `--color=never`.
150
151 +--break::
152 + Print an empty line between matches from different files.
153 +
154 -[ABC] <context>::
155 Show `context` trailing (`A` -- after), or leading (`B`
156 -- before), or both (`C` -- context) lines, and place a
builtin/grep.c
+3 -1
@@ -822,6 +822,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
822 OPT_BOOLEAN('c', "count", &opt.count,
823 "show the number of matches instead of matching lines"),
824 OPT__COLOR(&opt.color, "highlight matches"),
825 + OPT_BOOLEAN(0, "break", &opt.file_break,
826 + "print empty line between matches from different files"),
827 OPT_GROUP(""),
828 OPT_CALLBACK('C', NULL, &opt, "n",
829 "show <n> context lines before and after matches",
@@ -976,7 +978,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
978 use_threads = 0;
979
980 if (use_threads) {
979 - if (opt.pre_context || opt.post_context)
981 + if (opt.pre_context || opt.post_context || opt.file_break)
982 skip_first_line = 1;
983 start_threads(&opt);
984 }
grep.c
+5 -2
@@ -721,7 +721,10 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
721 int rest = eol - bol;
722 char *line_color = NULL;
723
724 - if (opt->pre_context || opt->post_context) {
724 + if (opt->file_break && opt->last_shown == 0) {
725 + if (opt->show_hunk_mark)
726 + opt->output(opt, "\n", 1);
727 + } else if (opt->pre_context || opt->post_context) {
728 if (opt->last_shown == 0) {
729 if (opt->show_hunk_mark) {
730 output_color(opt, "--", 2, opt->color_sep);
@@ -941,7 +944,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
944 if (!opt->output)
945 opt->output = std_output;
946
944 - if (opt->pre_context || opt->post_context) {
947 + if (opt->pre_context || opt->post_context || opt->file_break) {
948 /* Show hunk marks, except for the first file. */
949 if (opt->last_shown)
950 opt->show_hunk_mark = 1;
grep.h
+1
@@ -110,6 +110,7 @@ struct grep_opt {
110 unsigned post_context;
111 unsigned last_shown;
112 int show_hunk_mark;
113 + int file_break;
114 void *priv;
115
116 void (*output)(struct grep_opt *opt, const void *data, size_t size);
t/t7810-grep.sh
+28
@@ -746,4 +746,32 @@ test_expect_success 'grep --color, separator' '
746 test_cmp expected actual
747 '
748
749 +cat >expected <<EOF
750 +hello.c:int main(int argc, const char **argv)
751 +hello.c: /* char ?? */
752 +
753 +hello_world:Hello_world
754 +EOF
755 +
756 +test_expect_success 'grep --break' '
757 + git grep --break -e char -e lo_w hello.c hello_world >actual &&
758 + test_cmp expected actual
759 +'
760 +
761 +cat >expected <<EOF
762 +hello.c:int main(int argc, const char **argv)
763 +hello.c-{
764 +--
765 +hello.c: /* char ?? */
766 +hello.c-}
767 +
768 +hello_world:Hello_world
769 +hello_world-HeLLo_world
770 +EOF
771 +
772 +test_expect_success 'grep --break with context' '
773 + git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
774 + test_cmp expected actual
775 +'
776 +
777 test_done