From 518920b764ee9150781e68217181b24d0712748e Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 17 May 2006 02:48:13 -0700 Subject: builtin-grep: workaround for non GNU grep. Some implementations do not know what to do with -H; define NO_H_OPTION_IN_GREP when you build git if your grep lacks -H. Most of the time, it can be worked around by prepending /dev/null to the argument list, but that causes -L and -c to slightly misbehave (they both expose /dev/null is given), so when these options are given, do not run external grep that does not understand -H. Signed-off-by: Junio C Hamano --- builtin-grep.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'builtin-grep.c') diff --git a/builtin-grep.c b/builtin-grep.c index 66111de514..36512d8a17 100644 --- a/builtin-grep.c +++ b/builtin-grep.c @@ -453,7 +453,6 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached) len = nr = 0; push_arg("grep"); - push_arg("-H"); if (opt->fixed) push_arg("-F"); if (opt->linenum) @@ -503,7 +502,13 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached) push_arg("-e"); push_arg(p->pattern); } - push_arg("--"); + + if (NO_H_OPTION_IN_GREP) + push_arg("/dev/null"); + else { + push_arg("-H"); + push_arg("--"); + } hit = 0; argc = nr; @@ -535,8 +540,19 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached) * Use the external "grep" command for the case where * we grep through the checked-out files. It tends to * be a lot more optimized + * + * Some grep implementations do not understand -H nor -- + * but /dev/null can be used as a substitution in most + * cases. + * + * However -L and -c would slightly misbehave (-L would + * list /dev/null as a hit, and -c would report 0 hits + * from /dev/null); so do not use the external one on + * such platforms. */ - if (!cached) { + if (!cached && + (!NO_H_OPTION_IN_GREP || + (!opt->count && !opt->unmatch_name_only))) { hit = external_grep(opt, paths, cached); if (hit >= 0) return hit; -- cgit v1.2.3