Skip to content

Commit 1d84f72

Browse files
René Scharfegitster
authored andcommitted
grep: add --heading
With --heading, the filename is printed once before matches from that file instead of at the start of each line, giving more screen space to the actual search results. This option is taken from ack (http://betterthangrep.com/). And now git grep can dress up like it: $ git config alias.ack "grep --break --heading --line-number" $ git ack -e --heading Documentation/git-grep.txt 154:--heading:: t/t7810-grep.sh 785:test_expect_success 'grep --heading' ' 786: git grep --heading -e char -e lo_w hello.c hello_world >actual && 808: git grep --break --heading -n --color \ Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a8f0e76 commit 1d84f72

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

Documentation/git-grep.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ OPTIONS
151151
--break::
152152
Print an empty line between matches from different files.
153153

154+
--heading::
155+
Show the filename above the matches in that file instead of
156+
at the start of each shown line.
157+
154158
-[ABC] <context>::
155159
Show `context` trailing (`A` -- after), or leading (`B`
156160
-- before), or both (`C` -- context) lines, and place a

builtin/grep.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
824824
OPT__COLOR(&opt.color, "highlight matches"),
825825
OPT_BOOLEAN(0, "break", &opt.file_break,
826826
"print empty line between matches from different files"),
827+
OPT_BOOLEAN(0, "heading", &opt.heading,
828+
"show filename only once above matches from same file"),
827829
OPT_GROUP(""),
828830
OPT_CALLBACK('C', NULL, &opt, "n",
829831
"show <n> context lines before and after matches",

grep.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,13 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
735735
opt->output(opt, "\n", 1);
736736
}
737737
}
738+
if (opt->heading && opt->last_shown == 0) {
739+
output_color(opt, name, strlen(name), opt->color_filename);
740+
opt->output(opt, "\n", 1);
741+
}
738742
opt->last_shown = lno;
739743

740-
if (opt->pathname) {
744+
if (!opt->heading && opt->pathname) {
741745
output_color(opt, name, strlen(name), opt->color_filename);
742746
output_sep(opt, sign);
743747
}

grep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ struct grep_opt {
111111
unsigned last_shown;
112112
int show_hunk_mark;
113113
int file_break;
114+
int heading;
114115
void *priv;
115116

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

t/t7810-grep.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,4 +774,41 @@ test_expect_success 'grep --break with context' '
774774
test_cmp expected actual
775775
'
776776

777+
cat >expected <<EOF
778+
hello.c
779+
int main(int argc, const char **argv)
780+
/* char ?? */
781+
hello_world
782+
Hello_world
783+
EOF
784+
785+
test_expect_success 'grep --heading' '
786+
git grep --heading -e char -e lo_w hello.c hello_world >actual &&
787+
test_cmp expected actual
788+
'
789+
790+
cat >expected <<EOF
791+
<BOLD;GREEN>hello.c<RESET>
792+
2:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
793+
6: /* <BLACK;BYELLOW>char<RESET> ?? */
794+
795+
<BOLD;GREEN>hello_world<RESET>
796+
3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
797+
EOF
798+
799+
test_expect_success 'mimic ack-grep --group' '
800+
test_config color.grep.context normal &&
801+
test_config color.grep.filename "bold green" &&
802+
test_config color.grep.function normal &&
803+
test_config color.grep.linenumber normal &&
804+
test_config color.grep.match "black yellow" &&
805+
test_config color.grep.selected normal &&
806+
test_config color.grep.separator normal &&
807+
808+
git grep --break --heading -n --color \
809+
-e char -e lo_w hello.c hello_world |
810+
test_decode_color >actual &&
811+
test_cmp expected actual
812+
'
813+
777814
test_done

0 commit comments

Comments
 (0)