summaryrefslogtreecommitdiff
path: root/builtin-grep.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@osdl.org>2006-09-14 10:45:12 -0700
committerJunio C Hamano <junkio@cox.net>2006-09-14 11:46:11 -0700
commit7977f0ea53fad7103c83e5bd61777da27451f1d6 (patch)
tree5f67ed25cb9a4d1cf6d23e381aac47cf588bfa8d /builtin-grep.c
parent38529e28a4f465ad5d5f2fa249ca17da680bac5f (diff)
downloadgit-7977f0ea53fad7103c83e5bd61777da27451f1d6.tar.gz
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>
Diffstat (limited to 'builtin-grep.c')
-rw-r--r--builtin-grep.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/builtin-grep.c b/builtin-grep.c
index 6430f6d79e..ed87a5550c 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -138,6 +138,7 @@ struct grep_opt {
unsigned binary:2;
unsigned extended:1;
unsigned relative:1;
+ unsigned pathname:1;
int regflags;
unsigned pre_context;
unsigned post_context;
@@ -316,7 +317,8 @@ static int word_char(char ch)
static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
const char *name, unsigned lno, char sign)
{
- printf("%s%c", name, sign);
+ if (opt->pathname)
+ printf("%s%c", name, sign);
if (opt->linenum)
printf("%d%c", lno, sign);
printf("%.*s\n", (int)(eol-bol), bol);
@@ -691,6 +693,8 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
push_arg("-F");
if (opt->linenum)
push_arg("-n");
+ if (!opt->pathname)
+ push_arg("-h");
if (opt->regflags & REG_EXTENDED)
push_arg("-E");
if (opt->regflags & REG_ICASE)
@@ -911,6 +915,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
memset(&opt, 0, sizeof(opt));
opt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
opt.relative = 1;
+ opt.pathname = 1;
opt.pattern_tail = &opt.pattern_list;
opt.regflags = REG_NEWLINE;
@@ -970,10 +975,12 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
opt.linenum = 1;
continue;
}
+ if (!strcmp("-h", arg)) {
+ opt.pathname = 0;
+ continue;
+ }
if (!strcmp("-H", arg)) {
- /* We always show the pathname, so this
- * is a noop.
- */
+ opt.pathname = 1;
continue;
}
if (!strcmp("-l", arg) ||