Re: [PATCH v3] grep: support the --pathspec-from-file option
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Dec 13, 2019, 18:26 UTC
- Message-ID
- <xmqqpngskrpd.fsf@gitster-ct.c.googlers.com>
- In-Reply-To
- <20191213041254.13202-1-emilyshaffer@google.com>
Emily Shaffer <emilyshaffer@google.com> writes:
Show 19 quoted lines
> 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.
Show 28 quoted lines
> -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?
Show 14 quoted lines
> +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?
Show 9 quoted lines
> + > +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".
Show 18 quoted lines
> +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 && > --