All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <junkio@cox.net>
To: Linus Torvalds <torvalds@osdl.org>
Cc: git@vger.kernel.org
Subject: [PATCH] builtin-grep: workaround for non GNU grep.
Date: Wed, 17 May 2006 02:54:27 -0700	[thread overview]
Message-ID: <7vves5geng.fsf_-_@assigned-by-dhcp.cox.net> (raw)
In-Reply-To: <7vejythvkr.fsf@assigned-by-dhcp.cox.net> (Junio C. Hamano's message of "Wed, 17 May 2006 02:03:32 -0700")

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 <junkio@cox.net> writes:

 > But I think this approach breaks -L; I do not think Solaris
 > supports -L, so it does not matter there, but on platforms that
 > knows how to do -L it does.

 So this is an updated version.  I am not proud of the handling
 of the new Makefile variable, although I like the C code that
 does not need #ifdef thanks to it.

 Makefile       |   11 +++++++++++
 builtin-grep.c |   22 +++++++++++++++++++---
 2 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 9ba608c..c67108d 100644
--- a/Makefile
+++ b/Makefile
@@ -46,6 +46,8 @@ # Patrick Mauritz).
 #
 # Define NO_MMAP if you want to avoid mmap.
 #
+# Define NO_H_OPTION_IN_GREP if your grep does not understand -H.
+#
 # Define WITH_OWN_SUBPROCESS_PY if you want to use with python 2.3.
 #
 # Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
@@ -444,6 +446,12 @@ ifdef NO_ACCURATE_DIFF
 	ALL_CFLAGS += -DNO_ACCURATE_DIFF
 endif
 
+ifdef NO_H_OPTION_IN_GREP
+	NO_H_OPTION_IN_GREP=1
+else
+	NO_H_OPTION_IN_GREP=0
+endif
+
 # Shell quote (do not use $(call) to accomodate ancient setups);
 
 SHA1_HEADER_SQ = $(subst ','\'',$(SHA1_HEADER))
@@ -526,6 +534,9 @@ git$X git.spec \
 %.o: %.S
 	$(CC) -o $*.o -c $(ALL_CFLAGS) $<
 
+builtin-grep.o: builtin-grep.c
+	$(CC) -o $*.o -c $(ALL_CFLAGS) -DNO_H_OPTION_IN_GREP=$(NO_H_OPTION_IN_GREP) $<
+
 exec_cmd.o: exec_cmd.c
 	$(CC) -o $*.o -c $(ALL_CFLAGS) '-DGIT_EXEC_PATH="$(gitexecdir_SQ)"' $<
 
diff --git a/builtin-grep.c b/builtin-grep.c
index 66111de..36512d8 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -453,7 +453,6 @@ static int external_grep(struct grep_opt
 
 	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
 		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 @@ #ifdef __unix__
 	 * 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;
-- 
1.3.3.g8a24

  reply	other threads:[~2006-05-17  9:54 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-16 23:52 Git 1.3.2 on Solaris Stefan Pfetzing
2006-05-17  1:25 ` Jason Riedy
2006-05-17  2:20 ` Linus Torvalds
2006-05-17  3:26   ` Jason Riedy
2006-05-17  3:49     ` Linus Torvalds
2006-05-17  8:05       ` Stefan Pfetzing
2006-05-17 14:33         ` Linus Torvalds
2006-05-17 15:08           ` Stefan Pfetzing
2006-05-17 16:24             ` Linus Torvalds
2006-05-17 16:35               ` Jason Riedy
2006-05-23  3:20                 ` Stefan Pfetzing
2006-05-23  4:51                   ` Jason Riedy
2006-05-23 12:04                     ` Stefan Pfetzing
2006-05-23 14:53                     ` Linus Torvalds
2006-05-23 15:20                       ` Edgar Toernig
2006-05-23 15:31                         ` Linus Torvalds
2006-05-23 18:43                           ` Edgar Toernig
2006-05-23 18:03                       ` Jason Riedy
2006-05-23 18:24                         ` Linus Torvalds
2006-05-23 18:48                           ` Linus Torvalds
2006-05-26  3:30                   ` Stefan Pfetzing
2006-05-17  5:15     ` Ryan Anderson
2006-05-17  8:22       ` Junio C Hamano
2006-05-17  9:03   ` Junio C Hamano
2006-05-17  9:54     ` Junio C Hamano [this message]
2006-05-17 14:24       ` [PATCH] builtin-grep: workaround for non GNU grep Linus Torvalds
2006-05-17 17:41         ` Junio C Hamano
2006-05-17 15:39       ` Bertrand Jacquin
2006-05-17 17:42         ` Junio C Hamano
2006-05-17 18:12           ` Linus Torvalds
2006-05-17 18:59             ` Junio C Hamano
2006-05-17 19:42               ` Linus Torvalds
2006-05-17  8:28 ` Git 1.3.2 on Solaris Junio C Hamano
2006-05-17  9:06   ` Stefan Pfetzing
2006-05-17  9:22     ` Junio C Hamano
2006-05-17 10:41       ` Stefan Pfetzing

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vves5geng.fsf_-_@assigned-by-dhcp.cox.net \
    --to=junkio@cox.net \
    --cc=git@vger.kernel.org \
    --cc=torvalds@osdl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.