Add "-h/-H" parsing to "git grep"
It turns out that I actually wanted to avoid the filenames (because I didn't care - I just wanted to see the context in which something was used) when doing a grep. But since "git grep" didn't take the "-h" parameter, I ended up having to do "grep -5 -h *.c" instead. So here's a trivial patch that adds "-h" (and thus has to enable -H too) to "git grep" parsing. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Sep 14, 2006 at 10:45 UTC
7977f0ea53fad7103c83e5bd61777da27451f1d6
1 file changed
+11
-4
builtin-grep.c
+11
-4
@@ -138,6 +138,7 @@ struct grep_opt {
138
unsigned binary:2;
139
unsigned extended:1;
140
unsigned relative:1;
141
+ unsigned pathname:1;
142
int regflags;
143
unsigned pre_context;
144
unsigned post_context;
@@ -316,7 +317,8 @@ static int word_char(char ch)
317
static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
318
const char *name, unsigned lno, char sign)
319
{
319
- printf("%s%c", name, sign);
320
+ if (opt->pathname)
321
+ printf("%s%c", name, sign);
322
if (opt->linenum)
323
printf("%d%c", lno, sign);
324
printf("%.*s\n", (int)(eol-bol), bol);
@@ -691,6 +693,8 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
693
push_arg("-F");
694
if (opt->linenum)
695
push_arg("-n");
696
+ if (!opt->pathname)
697
+ push_arg("-h");
698
if (opt->regflags & REG_EXTENDED)
699
push_arg("-E");
700
if (opt->regflags & REG_ICASE)
@@ -911,6 +915,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
915
memset(&opt, 0, sizeof(opt));
916
opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
917
opt.relative = 1;
918
+ opt.pathname = 1;
919
opt.pattern_tail = &opt.pattern_list;
920
opt.regflags = REG_NEWLINE;
921
@@ -970,10 +975,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
975
opt.linenum = 1;
976
continue;
977
}
978
+ if (!strcmp("-h", arg)) {
979
+ opt.pathname = 0;
980
+ continue;
981
+ }
982
if (!strcmp("-H", arg)) {
974
- /* We always show the pathname, so this
975
- * is a noop.
976
- */
983
+ opt.pathname = 1;
984
continue;
985
}
986
if (!strcmp("-l", arg) ||