git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Matheus Tavares Bernardino <matheus.bernardino@usp.br>
To: git <git@vger.kernel.org>
Cc: "Greg Hurrell" <greg@hurrell.net>,
	"Junio C Hamano" <gitster@pobox.com>,
	"Andreas Heiduk" <asheiduk@gmail.com>,
	"René Scharfe" <l.s.r@web.de>
Subject: Re: git-grep's "-z" option misbehaves in subdirectory
Date: Thu, 16 Apr 2020 15:59:12 -0300	[thread overview]
Message-ID: <CAHd-oW4NK3E2umq9OXXW9TUyLKQwWN4R-b1KKK117tWPc=K7aw@mail.gmail.com> (raw)
In-Reply-To: <CAOyLvt9=wRfpvGGJqLMi7=wLWu881pOur8c9qNEg+Xqhf8W2ww@mail.gmail.com>

[Oops, I messed up the headers in my reply, so I'm not sure if the
message was properly delivered. I'm replying again, just in case.]

On Mon, Apr 13, 2020 at 6:57 PM Greg Hurrell <greg@hurrell.net> wrote:
>
> It seems that `git-grep -lz` behaves differently depending on whether
> it is inside a subdirectory:
[...]
> $ git grep -lz content
> an "example".txt^@nested/other "example".txt^@
>
> Note that, as expected, the files are NUL-terminated and not wrapped
> in quotes. ("^@" represents NUL byte.)
>
> $ cd nested
> $ git grep -lz content
> "other \"example\".txt"^@
>
> As soon as we move into a subdirectory, files are wrapped in quotes
> and contain escapes, despite the "-z" switch.

Note that, differently from other Git commands, "-z" doesn't affect
quoting/escaping in git-grep. For example, while git-ls-files' man
page says:

-z
      \0 line termination on output **and do not quote filenames**.

The git-grep one only says:

-z, --null
      Output \0 instead of the character that normally follows a file name.

Indeed, this inconsistency might be a little confusing. The reason for
it may be because "-z" was added in git-grep [1] to mimic the GNU grep
"-Z" flag, which outputs '\0' after file names. In GNU grep, pathnames
are never quoted (regardless of whether or not "-Z" is used). Also
note that, probably due to the same reason, git-grep doesn't obey the
core.quotePath setting (which would make it quote unusual pathnames by
default).

Nevertheless, quoting relative paths but not absolute ones is a bug.
From what I see, we have two options to fix it:

1.  Make git-grep more consistent with other Git commands by always
quoting/escaping unusual pathnames (relative or not), unless "-z" is
given or core.quotePath is set to "false".

2.  Keep git-grep compatible with GNU grep, ripgrep and other grepping
tools, removing the quotes/escaping from relative paths as well. In
this case, I think it is also a good idea to add a note about this
behavior in core.quotePath and in git-grep's description of "-z" (as
it may seem confusing to those who are used to the behavior of "-z" in
other Git commands).

In my previous [and possibly corrupted] reply, I included a patch[2]
in the direction of the second option. But thinking about it again,
I'm not really sure which one is the best way forward here. I'd love
to hear what others have to say about it, before sending a more
refined patch.

[1]: See 83caecc, (git grep: Add "-z/--null" option as in GNU's grep.,
2008-10-01)
[2]: https://public-inbox.org/git/20200414074204.677139-1-matheus.bernardino@usp.br/
(for some reason, the mail didn't reach lore.kernel.org. Probably my
fault, messing with the headers :/)

  parent reply	other threads:[~2020-04-16 18:59 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-13 21:55 git-grep's "-z" option misbehaves in subdirectory Greg Hurrell
2020-04-13 23:33 ` Junio C Hamano
2020-04-14  7:42 ` Matheus Tavares
2020-04-16 18:59 ` Matheus Tavares Bernardino [this message]
2020-04-16 20:07   ` git-grep's "-z" option misbehaves in subdirectory Junio C Hamano
2020-04-17  6:04     ` [PATCH] grep: follow conventions for printing paths w/ unusual chars Matheus Tavares
2020-04-17  6:45       ` Junio C Hamano
2020-04-17 21:19         ` Matheus Tavares Bernardino
2020-04-17 21:35           ` Junio C Hamano
2020-04-18 13:13       ` Johannes Schindelin
2020-04-18 14:56         ` Johannes Schindelin
2020-04-19  6:27           ` Matheus Tavares Bernardino
2020-04-19  6:33       ` [PATCH v2] " Matheus Tavares

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to='CAHd-oW4NK3E2umq9OXXW9TUyLKQwWN4R-b1KKK117tWPc=K7aw@mail.gmail.com' \
    --to=matheus.bernardino@usp.br \
    --cc=asheiduk@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=greg@hurrell.net \
    --cc=l.s.r@web.de \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).