From: Raphael Zimmerer <killekulla@rdrz.de>
To: spearce@spearce.org
Cc: git@vger.kernel.org, Johannes.Schindelin@gmx.de, madcoder@debian.org
Subject: [PATCH v2] git grep: Add "-z/--null" option as in GNU's grep.
Date: Wed, 1 Oct 2008 18:11:15 +0200 [thread overview]
Message-ID: <1222877475-2699-1-git-send-email-killekulla@rdrz.de> (raw)
In-Reply-To: <1222816390-9141-1-git-send-email-killekulla@rdrz.de>
Here's a trivial patch that adds "-z" and "--null" options to "git
grep". It was discussed on the mailing-list that git's "-z"
convention should be used instead of GNU grep's "-Z".
So things like 'git grep -l -z "$FOO" | xargs -0 sed -i "s/$FOO/$BOO/"'
do work now.
Signed-off-by: Raphael Zimmerer <killekulla@rdrz.de>
---
Changes from first patch:
* "-Z" -> "-z"
* use '\0' instead of 0
Regards
Raphael
Documentation/git-grep.txt | 6 ++++++
builtin-grep.c | 8 ++++++++
grep.c | 14 +++++++++++---
grep.h | 1 +
4 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index fa4d133..553da6c 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -15,6 +15,7 @@ SYNOPSIS
[-E | --extended-regexp] [-G | --basic-regexp]
[-F | --fixed-strings] [-n]
[-l | --files-with-matches] [-L | --files-without-match]
+ [-z | --null]
[-c | --count] [--all-match]
[-A <post-context>] [-B <pre-context>] [-C <context>]
[-f <file>] [-e] <pattern>
@@ -94,6 +95,11 @@ OPTIONS
For better compatibility with 'git-diff', --name-only is a
synonym for --files-with-matches.
+-z::
+--null::
+ Output \0 instead of the character that normally follows a
+ file name.
+
-c::
--count::
Instead of showing every matched line, show the number of
diff --git a/builtin-grep.c b/builtin-grep.c
index 3a51662..2241324 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -295,6 +295,9 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
push_arg("-l");
if (opt->unmatch_name_only)
push_arg("-L");
+ if (opt->null_following_name)
+ // in GNU grep git's "-z" translates to "-Z"
+ push_arg("-Z");
if (opt->count)
push_arg("-c");
if (opt->post_context || opt->pre_context) {
@@ -599,6 +602,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
opt.unmatch_name_only = 1;
continue;
}
+ if (!strcmp("-z", arg) ||
+ !strcmp("--null", arg)) {
+ opt.null_following_name = 1;
+ continue;
+ }
if (!strcmp("-c", arg) ||
!strcmp("--count", arg)) {
opt.count = 1;
diff --git a/grep.c b/grep.c
index 7063511..e2c190a 100644
--- a/grep.c
+++ b/grep.c
@@ -239,6 +239,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)
{
+ if (opt->null_following_name)
+ sign = '\0';
if (opt->pathname)
printf("%s%c", name, sign);
if (opt->linenum)
@@ -246,6 +248,11 @@ static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
printf("%.*s\n", (int)(eol-bol), bol);
}
+static void show_name(struct grep_opt *opt, const char *name)
+{
+ printf("%s%c", name, opt->null_following_name ? '\0' : '\n');
+}
+
static int fixmatch(const char *pattern, char *line, regmatch_t *match)
{
char *hit = strstr(line, pattern);
@@ -489,7 +496,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
return 1;
}
if (opt->name_only) {
- printf("%s\n", name);
+ show_name(opt, name);
return 1;
}
/* Hit at this line. If we haven't shown the
@@ -555,7 +562,7 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
return 0;
if (opt->unmatch_name_only) {
/* We did not see any hit, so we want to show this */
- printf("%s\n", name);
+ show_name(opt, name);
return 1;
}
@@ -565,7 +572,8 @@ static int grep_buffer_1(struct grep_opt *opt, const char *name,
* make it another option? For now suppress them.
*/
if (opt->count && count)
- printf("%s:%u\n", name, count);
+ printf("%s%c%u\n", name,
+ opt->null_following_name ? '\0' : ':', count);
return !!last_hit;
}
diff --git a/grep.h b/grep.h
index 59b3f87..45a222d 100644
--- a/grep.h
+++ b/grep.h
@@ -74,6 +74,7 @@ struct grep_opt {
unsigned extended:1;
unsigned relative:1;
unsigned pathname:1;
+ unsigned null_following_name:1;
int regflags;
unsigned pre_context;
unsigned post_context;
--
1.5.6.5
next prev parent reply other threads:[~2008-10-01 16:12 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-30 23:13 [PATCH] git grep: Add "-Z/--null" option as in GNU's grep Raphael Zimmerer
2008-09-30 23:16 ` Shawn O. Pearce
2008-09-30 23:41 ` Raphael Zimmerer
2008-10-01 13:15 ` Johannes Schindelin
2008-10-01 13:23 ` Raphael Zimmerer
2008-10-01 14:52 ` Shawn O. Pearce
2008-10-01 15:11 ` Raphael Zimmerer
2008-10-01 6:12 ` Pierre Habouzit
2008-10-01 15:03 ` Shawn O. Pearce
2008-10-01 16:11 ` Raphael Zimmerer [this message]
2008-10-01 16:17 ` [PATCH v2] git grep: Add "-z/--null" " Shawn O. Pearce
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=1222877475-2699-1-git-send-email-killekulla@rdrz.de \
--to=killekulla@rdrz.de \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=madcoder@debian.org \
--cc=spearce@spearce.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).