git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Emily Shaffer <emilyshaffer@google.com>
Cc: git@vger.kernel.org,
	"Alexandr Miloslavskiy" <alexandr.miloslavskiy@syntevo.com>,
	"Denton Liu" <liu.denton@gmail.com>,
	"SZEDER Gábor" <szeder.dev@gmail.com>,
	"Johannes Schindelin" <Johannes.Schindelin@gmx.de>
Subject: Re: [PATCH v3] grep: support the --pathspec-from-file option
Date: Fri, 13 Dec 2019 10:26:06 -0800	[thread overview]
Message-ID: <xmqqpngskrpd.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20191213041254.13202-1-emilyshaffer@google.com> (Emily Shaffer's message of "Thu, 12 Dec 2019 20:12:54 -0800")

Emily Shaffer <emilyshaffer@google.com> writes:

> Teach 'git grep' to use OPT_PATHSPEC_FROM_FILE and update the
> documentation accordingly.
>
> This changes enables 'git grep' to receive the pathspec from a file by
> specifying the path, or from stdin by specifying '-' as the path. This
> matches the previous functionality of '-f', so the documentation of '-f'
> has been expanded to describe this functionality. To let '-f' match the
> new '--pathspec-from-file' option, also teach a '--patterns-from-file'
> long name to '-f'.
>
> Since there are now two arguments which can attempt to read from stdin,
> add a safeguard to check whether the user specified '-' for both of
> them. It is still possible for a user to pass '/dev/stdin' to one or
> both arguments at present; we do not explicitly check.
>
> Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
> ---
> I built this on 'next' locally, but it does require 'am/pathspec-from-file'
> (which was merged to 'next').

Thanks.  I'd probably just queue on top of 'master' as the other
topic has graduated as part of the fifth batch.

> -static int file_callback(const struct option *opt, const char *arg, int unset)
> +static int read_pattern_file(struct grep_opt *grep_opt, const char *path)
>  {
> -	struct grep_opt *grep_opt = opt->value;
>  	int from_stdin;
>  	FILE *patterns;
>  	int lno = 0;
>  	struct strbuf sb = STRBUF_INIT;
>  
> -	BUG_ON_OPT_NEG(unset);
> -
> -	from_stdin = !strcmp(arg, "-");
> -	patterns = from_stdin ? stdin : fopen(arg, "r");
> +	from_stdin = !strcmp(path, "-");
> +	patterns = from_stdin ? stdin : fopen(path, "r");
>  	if (!patterns)
> -		die_errno(_("cannot open '%s'"), arg);
> +		die_errno(_("cannot open '%s'"), path);
>  	while (strbuf_getline(&sb, patterns) == 0) {
>  		/* ignore empty line like grep does */
>  		if (sb.len == 0)
>  			continue;
>  
> -		append_grep_pat(grep_opt, sb.buf, sb.len, arg, ++lno,
> +		append_grep_pat(grep_opt, sb.buf, sb.len, path, ++lno,
>  				GREP_PATTERN);
>  	}
>  	if (!from_stdin)

Hmph.  This has nothing to do with "--pathspec-from-file" that was
advertised on the title of the patch.  It used to be that

	git grep -f one -f two

can be used to read patterns from two sources, but that is no
longer possible, is it?  Am I missing a larger benefit to accept
this regression?

> +test_expect_success 'setup pathspecs-file tests' '
> +cat >excluded-file <<EOF &&
> +bar
> +EOF
> +cat >pathspec-file <<EOF &&
> +foo
> +bar
> +baz
> +EOF
> +cat >unrelated-file <<EOF &&
> +xyz
> +EOF
> +git add excluded-file pathspec-file unrelated-file
> +'

As Alexandr mentioned, the above is harder to read than necessary.


	test_expect_success 'setup pathspec-file tests' '
		test_write_lines >excluded-file bar &&
		test_write_lines >pathspec-file foo bar baz &&
		test_write_lines >unrelated-file xyz &&
		git add ...
	'

perhaps?

> +
> +cat >pathspecs <<EOF
> +pathspec-file
> +unrelated-file
> +EOF
> +
> +cat >expected <<EOF
> +pathspec-file:bar
> +EOF

Also, shouldn't the above also be part of the (sub)setup for these
tests?  IOW, after that addition of three files with "git add", 

		test_write_lines >pathspec pathspec-file unrelated-file &&
		test_write_lines >expect pathspec-file:bar

in the "setup pathspec-file tests".

> +test_expect_success 'grep --pathspec-from-file with file' '
> +	git grep --pathspec-from-file pathspecs "bar" >actual &&
> +	test_cmp expected actual
> +'
> +
> +test_expect_success 'grep --pathspec-file with stdin' '
> +	git grep --pathspec-from-file - "bar" <pathspecs >actual &&
> +	test_cmp expected actual
> +'
> +
> +test_expect_success 'grep with two stdin inputs fails' '
> +	test_must_fail git grep --pathspec-from-file - --patterns-from-file - <pathspecs 2>err &&
> +	test_i18ngrep "cannot specify both patterns and pathspec via stdin" err
> +'
> +
>  test_expect_success 'setup double-dash tests' '
>  cat >double-dash <<EOF &&
>  --

  parent reply	other threads:[~2019-12-13 18:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-22  1:16 [PATCH] grep: provide pathspecs/patterns via file or stdin Emily Shaffer
2019-11-22  2:14 ` Denton Liu
2019-11-22  2:34   ` Junio C Hamano
2019-11-22  3:56     ` Junio C Hamano
2019-11-22 18:52     ` Denton Liu
2019-11-22 22:02     ` Emily Shaffer
2019-11-22 22:06       ` Emily Shaffer
2019-11-23  0:28       ` Junio C Hamano
2019-11-22  2:24 ` Junio C Hamano
2019-12-04 20:39 ` [PATCH v2] grep: support the --pathspec-from-file option Emily Shaffer
2019-12-04 21:05   ` Denton Liu
2019-12-04 21:24     ` Junio C Hamano
2019-12-04 22:24   ` Junio C Hamano
2019-12-13  3:07     ` Emily Shaffer
2019-12-05 11:58   ` Alexandr Miloslavskiy
2019-12-13  4:00     ` Emily Shaffer
2019-12-06 11:22   ` Johannes Schindelin
2019-12-06 11:34   ` SZEDER Gábor
2019-12-13  4:12   ` [PATCH v3] " Emily Shaffer
2019-12-13 13:04     ` Alexandr Miloslavskiy
2019-12-13 18:26     ` Junio C Hamano [this message]
2019-12-13 20:13       ` Alexandr Miloslavskiy
2019-12-17  0:33         ` Emily Shaffer

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=xmqqpngskrpd.fsf@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=alexandr.miloslavskiy@syntevo.com \
    --cc=emilyshaffer@google.com \
    --cc=git@vger.kernel.org \
    --cc=liu.denton@gmail.com \
    --cc=szeder.dev@gmail.com \
    /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).