Web lists-archives.com

Re: [PATCH] grep: provide a noop --recursive option




On Sat, Sep 29, 2018 at 5:25 PM Ævar Arnfjörð Bjarmason
<avarab@xxxxxxxxx> wrote:
>
>
> On Sat, Sep 29 2018, Duy Nguyen wrote:
>
> > On Sat, Sep 29, 2018 at 4:58 PM Ævar Arnfjörð Bjarmason
> > <avarab@xxxxxxxxx> wrote:
> >>
> >> This --recursive (-r) option does nothing, and is purely here to
> >> appease people who have "grep -r ..." burned into their muscle memory.
> >
> > GNU grep -r recurses infinitely but Git grep also has --max-depth. How
> > do these interact? My knee-jerk reaction is -r equals --max-depth=-1
> > (i.e. overriding previous --mex-depth options on command line, or from
> > alias)
>
> I didn't know about --max-depth, we could squash the following in to
> deal with that, but that still leaves --max-depth=123 --no-recursive as
> using depth 123, and we'd need different options parsing than OPT_BOOL
> to deal with that.

I think if you initialize recursive variable below to -1, then you can
detect both --recursive (1) and --no-recursive (0).

> diff --git a/builtin/grep.c b/builtin/grep.c
> index 02d4384225..2e048d9b49 100644
> --- a/builtin/grep.c
> +++ b/builtin/grep.c
> @@ -785,7 +785,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>         int use_index = 1;
>         int pattern_type_arg = GREP_PATTERN_TYPE_UNSPECIFIED;
>         int allow_revs;
> -       int unused_recursive; /* this is never used */
> +       int recursive = 0;
>
>         struct option options[] = {
>                 OPT_BOOL(0, "cached", &cached,
> @@ -803,7 +803,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>                         N_("show non-matching lines")),
>                 OPT_BOOL('i', "ignore-case", &opt.ignore_case,
>                         N_("case insensitive matching")),
> -               OPT_BOOL('r', "recursive", &unused_recursive,
> +               OPT_BOOL('r', "recursive", &recursive,
>                         N_("does nothing, git-grep is always recursive, for grep(1) muscle memory compatibility")),
>                 OPT_BOOL('w', "word-regexp", &opt.word_regexp,
>                         N_("match patterns only at word boundaries")),
> @@ -926,6 +926,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>                              PARSE_OPT_STOP_AT_NON_OPTION);
>         grep_commit_pattern_type(pattern_type_arg, &opt);
>
> +       if (opt.max_depth != -1 && recursive)
> +               die(_("The --max-depth and --recursive options are incompatible"));
>         if (use_index && !startup_info->have_repository) {
>                 int fallback = 0;
>                 git_config_get_bool("grep.fallbacktonoindex", &fallback);
> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
> index c48d1fa34b..ad25cd50f5 100755
> --- a/t/t7810-grep.sh
> +++ b/t/t7810-grep.sh
> @@ -1697,4 +1697,8 @@ test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
>         test_cmp expected actual
>  '
>
> +test_expect_success 'grep --recursive is incompatible with --max-depth' '
> +       test_must_fail git grep --recursive --max-depth=1
> +'
> +
>  test_done



-- 
Duy