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>
René Scharfe committed
Jun 5, 2011 at 17:24 UTC
1d84f72ef1dee1f851e11a6d92e8ab3f1979b2a2
5 files changed
+49
-1
Documentation/git-grep.txt
+4
@@ -151,6 +151,10 @@ OPTIONS
151
--break::
152
Print an empty line between matches from different files.
153
154
+--heading::
155
+ Show the filename above the matches in that file instead of
156
+ at the start of each shown line.
157
+
158
-[ABC] <context>::
159
Show `context` trailing (`A` -- after), or leading (`B`
160
-- before), or both (`C` -- context) lines, and place a
builtin/grep.c
+2
@@ -824,6 +824,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
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_BOOLEAN(0, "heading", &opt.heading,
828
+ "show filename only once above matches from same file"),
829
OPT_GROUP(""),
830
OPT_CALLBACK('C', NULL, &opt, "n",
831
"show <n> context lines before and after matches",
grep.c
+5
-1
@@ -735,9 +735,13 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
735
opt->output(opt, "\n", 1);
736
}
737
}
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
+ }
742
opt->last_shown = lno;
743
740
- if (opt->pathname) {
744
+ if (!opt->heading && opt->pathname) {
745
output_color(opt, name, strlen(name), opt->color_filename);
746
output_sep(opt, sign);
747
}
grep.h
+1
@@ -111,6 +111,7 @@ struct grep_opt {
111
unsigned last_shown;
112
int show_hunk_mark;
113
int file_break;
114
+ int heading;
115
void *priv;
116
117
void (*output)(struct grep_opt *opt, const void *data, size_t size);
t/t7810-grep.sh
+37
@@ -774,4 +774,41 @@ test_expect_success 'grep --break with context' '
774
test_cmp expected actual
775
'
776
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
+
814
test_done