summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Documentation/git-grep.txt4
-rw-r--r--builtin/grep.c2
-rw-r--r--grep.c6
-rw-r--r--grep.h1
-rwxr-xr-xt/t7810-grep.sh37
5 files changed, 49 insertions, 1 deletions
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index dea7cad0c2..07b3c6a086 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -151,6 +151,10 @@ OPTIONS
--break::
Print an empty line between matches from different files.
+--heading::
+ Show the filename above the matches in that file instead of
+ at the start of each shown line.
+
-[ABC] <context>::
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 42bb87f544..cccf8da6d2 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -824,6 +824,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
OPT__COLOR(&opt.color, "highlight matches"),
OPT_BOOLEAN(0, "break", &opt.file_break,
"print empty line between matches from different files"),
+ OPT_BOOLEAN(0, "heading", &opt.heading,
+ "show filename only once above matches from same file"),
OPT_GROUP(""),
OPT_CALLBACK('C', NULL, &opt, "n",
"show <n> context lines before and after matches",
diff --git a/grep.c b/grep.c
index b0b860a984..04e9ba4ec4 100644
--- a/grep.c
+++ b/grep.c
@@ -735,9 +735,13 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
opt->output(opt, "\n", 1);
}
}
+ if (opt->heading && opt->last_shown == 0) {
+ output_color(opt, name, strlen(name), opt->color_filename);
+ opt->output(opt, "\n", 1);
+ }
opt->last_shown = lno;
- if (opt->pathname) {
+ if (!opt->heading && opt->pathname) {
output_color(opt, name, strlen(name), opt->color_filename);
output_sep(opt, sign);
}
diff --git a/grep.h b/grep.h
index 638bee848d..c5682973ea 100644
--- a/grep.h
+++ b/grep.h
@@ -111,6 +111,7 @@ struct grep_opt {
unsigned last_shown;
int show_hunk_mark;
int file_break;
+ int heading;
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 f55793e3cb..1227fa69b4 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -774,4 +774,41 @@ test_expect_success 'grep --break with context' '
test_cmp expected actual
'
+cat >expected <<EOF
+hello.c
+int main(int argc, const char **argv)
+ /* char ?? */
+hello_world
+Hello_world
+EOF
+
+test_expect_success 'grep --heading' '
+ git grep --heading -e char -e lo_w hello.c hello_world >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+<BOLD;GREEN>hello.c<RESET>
+2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
+6: /* <BLACK;BYELLOW>char<RESET> ?? */
+
+<BOLD;GREEN>hello_world<RESET>
+3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
+EOF
+
+test_expect_success 'mimic ack-grep --group' '
+ test_config color.grep.context normal &&
+ test_config color.grep.filename "bold green" &&
+ test_config color.grep.function normal &&
+ test_config color.grep.linenumber normal &&
+ test_config color.grep.match "black yellow" &&
+ test_config color.grep.selected normal &&
+ test_config color.grep.separator normal &&
+
+ git grep --break --heading -n --color \
+ -e char -e lo_w hello.c hello_world |
+ test_decode_color >actual &&
+ test_cmp expected actual
+'
+
test_done