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 <junkio@cox.net>
Junio C Hamano committed
May 17, 2006 at 02:48 UTC
518920b764ee9150781e68217181b24d0712748e
2 files changed
+30
-3
Makefile
+11
@@ -46,6 +46,8 @@ all:
46
#
47
# Define NO_MMAP if you want to avoid mmap.
48
#
49
+# Define NO_H_OPTION_IN_GREP if your grep does not understand -H.
50
+#
51
# Define WITH_OWN_SUBPROCESS_PY if you want to use with python 2.3.
52
#
53
# Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
@@ -444,6 +446,12 @@ ifdef NO_ACCURATE_DIFF
446
ALL_CFLAGS += -DNO_ACCURATE_DIFF
447
endif
448
449
+ifdef NO_H_OPTION_IN_GREP
450
+ NO_H_OPTION_IN_GREP=1
451
+else
452
+ NO_H_OPTION_IN_GREP=0
453
+endif
454
+
455
# Shell quote (do not use $(call) to accomodate ancient setups);
456
457
SHA1_HEADER_SQ = $(subst ','\'',$(SHA1_HEADER))
@@ -526,6 +534,9 @@ git$X git.spec \
534
%.o: %.S
535
$(CC) -o $*.o -c $(ALL_CFLAGS) $<
536
537
+builtin-grep.o: builtin-grep.c
538
+ $(CC) -o $*.o -c $(ALL_CFLAGS) -DNO_H_OPTION_IN_GREP=$(NO_H_OPTION_IN_GREP) $<
539
+
540
exec_cmd.o: exec_cmd.c
541
$(CC) -o $*.o -c $(ALL_CFLAGS) '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' $<
542
builtin-grep.c
+19
-3
@@ -453,7 +453,6 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
453
454
len = nr = 0;
455
push_arg("grep");
456
- push_arg("-H");
456
if (opt->fixed)
457
push_arg("-F");
458
if (opt->linenum)
@@ -503,7 +502,13 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
502
push_arg("-e");
503
push_arg(p->pattern);
504
}
506
- push_arg("--");
505
+
506
+ if (NO_H_OPTION_IN_GREP)
507
+ push_arg("/dev/null");
508
+ else {
509
+ push_arg("-H");
510
+ push_arg("--");
511
+ }
512
513
hit = 0;
514
argc = nr;
@@ -535,8 +540,19 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
540
* Use the external "grep" command for the case where
541
* we grep through the checked-out files. It tends to
542
* be a lot more optimized
543
+ *
544
+ * Some grep implementations do not understand -H nor --
545
+ * but /dev/null can be used as a substitution in most
546
+ * cases.
547
+ *
548
+ * However -L and -c would slightly misbehave (-L would
549
+ * list /dev/null as a hit, and -c would report 0 hits
550
+ * from /dev/null); so do not use the external one on
551
+ * such platforms.
552
*/
539
- if (!cached) {
553
+ if (!cached &&
554
+ (!NO_H_OPTION_IN_GREP ||
555
+ (!opt->count && !opt->unmatch_name_only))) {
556
hit = external_grep(opt, paths, cached);
557
if (hit >= 0)
558
return hit;