Skip to content

Commit a8f0e76

Browse files
René Scharfegitster
authored andcommitted
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>
1 parent 08303c3 commit a8f0e76

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

Documentation/git-grep.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ OPTIONS
148148
gives the default to color output.
149149
Same as `--color=never`.
150150

151+
--break::
152+
Print an empty line between matches from different files.
153+
151154
-[ABC] <context>::
152155
Show `context` trailing (`A` -- after), or leading (`B`
153156
-- before), or both (`C` -- context) lines, and place a

builtin/grep.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
822822
OPT_BOOLEAN('c', "count", &opt.count,
823823
"show the number of matches instead of matching lines"),
824824
OPT__COLOR(&opt.color, "highlight matches"),
825+
OPT_BOOLEAN(0, "break", &opt.file_break,
826+
"print empty line between matches from different files"),
825827
OPT_GROUP(""),
826828
OPT_CALLBACK('C', NULL, &opt, "n",
827829
"show <n> context lines before and after matches",
@@ -976,7 +978,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
976978
use_threads = 0;
977979

978980
if (use_threads) {
979-
if (opt.pre_context || opt.post_context)
981+
if (opt.pre_context || opt.post_context || opt.file_break)
980982
skip_first_line = 1;
981983
start_threads(&opt);
982984
}

grep.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,10 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
721721
int rest = eol - bol;
722722
char *line_color = NULL;
723723

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) {
725728
if (opt->last_shown == 0) {
726729
if (opt->show_hunk_mark) {
727730
output_color(opt, "--", 2, opt->color_sep);
@@ -941,7 +944,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
941944
if (!opt->output)
942945
opt->output = std_output;
943946

944-
if (opt->pre_context || opt->post_context) {
947+
if (opt->pre_context || opt->post_context || opt->file_break) {
945948
/* Show hunk marks, except for the first file. */
946949
if (opt->last_shown)
947950
opt->show_hunk_mark = 1;

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct grep_opt {
110110
unsigned post_context;
111111
unsigned last_shown;
112112
int show_hunk_mark;
113+
int file_break;
113114
void *priv;
114115

115116
void (*output)(struct grep_opt *opt, const void *data, size_t size);

t/t7810-grep.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,4 +746,32 @@ test_expect_success 'grep --color, separator' '
746746
test_cmp expected actual
747747
'
748748

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+
749777
test_done

0 commit comments

Comments
 (0)