Raphael Zimmerer | 1 Oct 2008 01:13
Picon

[PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

Here's a trivial patch that adds "-Z" and "--null" options to "git
grep" equal to GNU's grep.
So things like 'git grep -l -Z "$FOO" | xargs -0 sed -i "s/$FOO/$BOO/"'
are more comfortable.

Signed-off-by: Raphael Zimmerer <killekulla <at> rdrz.de>
---
 Documentation/git-grep.txt |    6 ++++++
 builtin-grep.c             |    7 +++++++
 grep.c                     |   14 +++++++++++---
 grep.h                     |    1 +
 4 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index fa4d133..9317377 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
 <at>  <at>  -15,6 +15,7  <at>  <at>  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>
 <at>  <at>  -94,6 +95,11  <at>  <at>  OPTIONS
 	For better compatibility with 'git-diff', --name-only is a
 	synonym for --files-with-matches.

+-Z::
(Continue reading)

Raphael Zimmerer | 1 Oct 2008 18:11
Picon

[PATCH v2] git grep: Add "-z/--null" option as in GNU's grep.

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 <at> 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
 <at>  <at>  -15,6 +15,7  <at>  <at>  SYNOPSIS
 	   [-E | --extended-regexp] [-G | --basic-regexp]
 	   [-F | --fixed-strings] [-n]
 	   [-l | --files-with-matches] [-L | --files-without-match]
+	   [-z | --null]
(Continue reading)

Shawn O. Pearce | 1 Oct 2008 18:17
Gravatar

Re: [PATCH v2] git grep: Add "-z/--null" option as in GNU's grep.

Raphael Zimmerer <killekulla <at> rdrz.de> wrote:
> diff --git a/builtin-grep.c b/builtin-grep.c
> index 3a51662..2241324 100644
> --- a/builtin-grep.c
> +++ b/builtin-grep.c
>  <at>  <at>  -295,6 +295,9  <at>  <at>  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");

We use /* */ style comments in Git.  I've amended the patch with
the simple // -> /* */ translation.

The rest of this change looks good to me.  Its queued for next.

--

-- 
Shawn.
Pierre Habouzit | 1 Oct 2008 08:12
X-Face
Picon
Favicon
Gravatar

Re: [PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

On Tue, Sep 30, 2008 at 11:13:10PM +0000, Raphael Zimmerer wrote:
> +	printf("%s%c", name, opt->null_following_name ? 0 : '\n');

I know I'm nitpicking and I don't know what the git custom on this
really is, but I tend to prefer '\0' when in the context of a char.
There is no confusion here of course, but I believe it to be a sane
habit. (In the same vein that it's ugly to use 0 for NULL ;p).

--

-- 
·O·  Pierre Habouzit
··O                                                madcoder <at> debian.org
OOO                                                http://www.madism.org
Shawn O. Pearce | 1 Oct 2008 17:03
Gravatar

Re: [PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

Pierre Habouzit <madcoder <at> debian.org> wrote:
> On Tue, Sep 30, 2008 at 11:13:10PM +0000, Raphael Zimmerer wrote:
> > +	printf("%s%c", name, opt->null_following_name ? 0 : '\n');
> 
> I know I'm nitpicking and I don't know what the git custom on this
> really is, but I tend to prefer '\0' when in the context of a char.
> There is no confusion here of course, but I believe it to be a sane
> habit. (In the same vein that it's ugly to use 0 for NULL ;p).

Its a valid nitpick.  NUL is '\0' when dealing with chars in Git,
NULL is NULL, and 0 is the integer 0.  That line should read '\0'
to conform with the other code already in git.git.

--

-- 
Shawn.
Shawn O. Pearce | 1 Oct 2008 01:16
Gravatar

Re: [PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

Raphael Zimmerer <killekulla <at> rdrz.de> wrote:
> Here's a trivial patch that adds "-Z" and "--null" options to "git
> grep" equal to GNU's grep.
> So things like 'git grep -l -Z "$FOO" | xargs -0 sed -i "s/$FOO/$BOO/"'
> are more comfortable.

Elsewhere in Git we call this "-z", like "git ls-tree -z", "git
log -z".  Should we match grep or git convention here?

>  Documentation/git-grep.txt |    6 ++++++
>  builtin-grep.c             |    7 +++++++
>  grep.c                     |   14 +++++++++++---
>  grep.h                     |    1 +

--

-- 
Shawn.
Raphael Zimmerer | 1 Oct 2008 01:41
Picon

Re: [PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

On Tue, Sep 30, 2008 at 04:16:19PM -0700, Shawn O. Pearce wrote:
> Elsewhere in Git we call this "-z", like "git ls-tree -z", "git
> log -z".  Should we match grep or git convention here?

I'd tend to grep's convention, as most options of git-grep mimic those
of grep. grep uses "-z" for \0 on _input_, so that would be very
confusing for grep users...

- Raphael
Johannes Schindelin | 1 Oct 2008 15:15
Picon
Picon

Re: [PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

Hi,

On Wed, 1 Oct 2008, Raphael Zimmerer wrote:

> On Tue, Sep 30, 2008 at 04:16:19PM -0700, Shawn O. Pearce wrote:
> > Elsewhere in Git we call this "-z", like "git ls-tree -z", "git log 
> > -z".  Should we match grep or git convention here?
> 
> I'd tend to grep's convention, as most options of git-grep mimic those 
> of grep. grep uses "-z" for \0 on _input_, so that would be very 
> confusing for grep users...

I tend to disagree.  Git is _already_ perceived as too heterogenous, and 
we should not add to that pile.

Ciao,
Dscho

Shawn O. Pearce | 1 Oct 2008 16:52
Gravatar

Re: [PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

Johannes Schindelin <Johannes.Schindelin <at> gmx.de> wrote:
> On Wed, 1 Oct 2008, Raphael Zimmerer wrote:
> 
> > On Tue, Sep 30, 2008 at 04:16:19PM -0700, Shawn O. Pearce wrote:
> > > Elsewhere in Git we call this "-z", like "git ls-tree -z", "git log 
> > > -z".  Should we match grep or git convention here?
> > 
> > I'd tend to grep's convention, as most options of git-grep mimic those 
> > of grep. grep uses "-z" for \0 on _input_, so that would be very 
> > confusing for grep users...
> 
> I tend to disagree.  Git is _already_ perceived as too heterogenous, and 
> we should not add to that pile.

I already have my brain wired that "\0 terminators in Git are -z".
Thus I'd assume "git grep -z  .. | xargs -0" would work.  Today it
doesn't without this patch, but if the patch was added I'd assume
it would work.

Perhaps I'm too close to git as a contributor and experienced user
to realize any brain damage.

I'd rather stick to "-z" in Git.  At least its consistent.

Its not like tools outside of Git are all that consistent.  GNU
grep uses --null/-Z.  xargs and perl use -0.  find uses -print0.
The human at the keyboard already has to navigate this rats nest
between different tools, but within a tool (git) we should be as
consistent as we can.

(Continue reading)

Raphael Zimmerer | 1 Oct 2008 17:11
Picon

Re: [PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

On Wed, Oct 01, 2008 at 07:52:07AM -0700, Shawn O. Pearce wrote:
> Its not like tools outside of Git are all that consistent.  GNU
> grep uses --null/-Z.  xargs and perl use -0.  find uses -print0.
> The human at the keyboard already has to navigate this rats nest
> between different tools, but within a tool (git) we should be as
> consistent as we can.

... and git-config uses --null/-z.

I will send an updated patch with --null/-z.

- Raphael
Raphael Zimmerer | 1 Oct 2008 15:23
Picon

Re: [PATCH] git grep: Add "-Z/--null" option as in GNU's grep.

On Wed, Oct 01, 2008 at 03:15:34PM +0200, Johannes Schindelin wrote:
> On Wed, 1 Oct 2008, Raphael Zimmerer wrote:
> > On Tue, Sep 30, 2008 at 04:16:19PM -0700, Shawn O. Pearce wrote:
> > > Elsewhere in Git we call this "-z", like "git ls-tree -z", "git log 
> > > -z".  Should we match grep or git convention here?
> > 
> > I'd tend to grep's convention, as most options of git-grep mimic those 
> > of grep. grep uses "-z" for \0 on _input_, so that would be very 
> > confusing for grep users...
> 
> I tend to disagree.  Git is _already_ perceived as too heterogenous, and 
> we should not add to that pile.

How about discarding "-Z" from my patch, and only leave in "--null"?
That removes ambiguity _and_ is compatible to GNU grep.

- Raphael

Gmane