git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Matthias Lederhofer <matled@gmx.net>
To: git@vger.kernel.org
Subject: [PATCH] git-grep: --and to combine patterns with and instead of or
Date: Mon, 26 Jun 2006 02:02:31 +0200	[thread overview]
Message-ID: <E1FueYh-0004XE-Fg@moooo.ath.cx> (raw)
In-Reply-To: <Pine.LNX.4.63.0606260108510.29667@wbgn013.biozentrum.uni-wuerzburg.de>

Signed-off-by: Matthias Lederhofer <matled@gmx.net>
---
> ... and by the far the most common use is to pass more than one pattern. 
> Also, the usage is "[-e] <pattern> [-e <pattern>...]".

Here is a patch to allow combination of patterns with 'and' instead of
'or'. This makes it easier to search for combinations of words in a line
without using grep multiple times combined by pipes. So it is still
possible to use -A/-B/-C (something I miss in normal grep). --and
cannot be passed down, so we have to use the built-in version if it is
set.

 Documentation/git-grep.txt |    5 ++++-
 builtin-grep.c             |   17 +++++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index ebfe51b..df9d705 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -16,7 +16,7 @@ SYNOPSIS
 	   [-n] [-l | --files-with-matches] [-L | --files-without-match]
 	   [-c | --count]
 	   [-A <post-context>] [-B <pre-context>] [-C <context>]
-	   [-f <file>] [-e] <pattern> [-e <pattern> [..]]
+	   [-f <file>] [-e] <pattern> [-e <pattern> [..]] [--and]
 	   [<tree>...]
 	   [--] [<path>...]
 
@@ -77,6 +77,9 @@ OPTIONS
 	scripts passing user input to grep. You can specify multiple
 	patterns which will be combined by 'or'.
 
+--and::
+	Combine multiple patterns by 'and' instead of 'or'.
+
 `<tree>...`::
 	Search blobs in the trees for specified patterns.
 
diff --git a/builtin-grep.c b/builtin-grep.c
index d0677cc..a2a034a 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -96,6 +96,7 @@ struct grep_opt {
 	regex_t regexp;
 	unsigned linenum:1;
 	unsigned invert:1;
+	unsigned and:1;
 	unsigned name_only:1;
 	unsigned unmatch_name_only:1;
 	unsigned count:1;
@@ -268,7 +269,11 @@ static int grep_buffer(struct grep_opt *
 				    word_char(bol[pmatch[0].rm_eo]))
 					hit = 0;
 			}
-			if (hit)
+			if (opt->and && !hit) {
+				hit = 0;
+				break;
+			}
+			if (!opt->and && hit)
 				break;
 		}
 		/* "grep -v -e foo -e bla" should list lines
@@ -553,10 +558,10 @@ static int grep_cache(struct grep_opt *o
 #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
+	 * we grep through the checked-out files and do not use
+	 * non-standard options. It tends to be a lot more optimized.
 	 */
-	if (!cached) {
+	if (!cached && !opt->and) {
 		hit = external_grep(opt, paths, cached);
 		if (hit >= 0)
 			return hit;
@@ -690,6 +695,10 @@ int cmd_grep(int argc, const char **argv
 			opt.binary = GREP_BINARY_TEXT;
 			continue;
 		}
+		if (!strcmp("--and", arg)) {
+			opt.and = 1;
+			continue;
+		}
 		if (!strcmp("-i", arg) ||
 		    !strcmp("--ignore-case", arg)) {
 			opt.regflags |= REG_ICASE;
-- 
1.4.1.rc1.g72a4-dirty

  parent reply	other threads:[~2006-06-26  0:02 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-25 15:38 [PATCH] git-grep: allow patterns starting with - Matthias Lederhofer
2006-06-25 15:47 ` Timo Hirvonen
2006-06-25 16:07   ` [PATCH] correct documentation for git grep Matthias Lederhofer
2006-06-25 23:10     ` Johannes Schindelin
2006-06-25 23:39       ` Matthias Lederhofer
2006-06-26  0:06         ` Matthias Lederhofer
2006-06-26  6:59           ` Johannes Schindelin
2006-06-26  0:02       ` Matthias Lederhofer [this message]
2006-06-29 22:20         ` [PATCH] git-grep: --and to combine patterns with and instead of or Thomas Glanzmann
2006-06-29 22:44           ` Junio C Hamano
2006-06-30  2:25             ` Matthias Lederhofer
2006-06-30  4:13               ` Junio C Hamano
2006-06-30  7:46                 ` Matthias Lederhofer
2006-06-30  7:56                   ` Junio C Hamano
2006-06-30 10:08                     ` [PATCH] git-grep: boolean expression on pattern matching Junio C Hamano
2006-06-30 10:24                       ` Jakub Narebski
2006-06-30 10:29                         ` Junio C Hamano
2006-06-30 15:11                       ` Matthias Lederhofer
2006-06-30 10:57                     ` [PATCH] git-grep: --and to combine patterns with and instead of or Matthias Lederhofer
2006-06-30 15:57                       ` Junio C Hamano
2006-06-30 17:04                         ` Matthias Lederhofer
2006-06-30 17:18                           ` Junio C Hamano
2006-06-30 17:33                             ` Jakub Narebski
2006-06-30 17:49                               ` Matthias Lederhofer
2006-06-30 17:58                                 ` Junio C Hamano
2006-06-30 18:20                                   ` Matthias Lederhofer
2006-06-30 18:03                                 ` Jakub Narebski
2006-06-30 18:16                                   ` Junio C Hamano
2006-06-30 19:11                                     ` Jakub Narebski
2006-06-30 20:26                                       ` Junio C Hamano
2006-07-03  7:54                         ` Andreas Ericsson
2006-06-25 16:18   ` [PATCH] git-grep: allow patterns starting with - Matthias Lederhofer

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=E1FueYh-0004XE-Fg@moooo.ath.cx \
    --to=matled@gmx.net \
    --cc=git@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).