git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Duy Nguyen <pclouds@gmail.com>
Date
Sep 29, 2018, 15:47 UTC
Message-ID
<CACsJy8C7hxpxaXvvgv5CRh4gCKLF2ZvOn6FZ5-yMyVLcFi26rg@mail.gmail.com>
In-Reply-To
<87d0swiavd.fsf@evledraar.gmail.com>

On Sat, Sep 29, 2018 at 5:25 PM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:

Show 19 quoted lines
>
>
> On Sat, Sep 29 2018, Duy Nguyen wrote:
>
> > On Sat, Sep 29, 2018 at 4:58 PM Ævar Arnfjörð Bjarmason
> > <avarab@gmail.com> 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).

Show 44 quoted lines
> 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
Previous: Ævar Arnfjörð BjarmasonNext: Junio C Hamano
Message 5 of 15 in “wishlist: git grep -r”
  1. Christoph BergSep 29, 2018
  2. grep: provide a noop --recursive optionÆvar Arnfjörð Bjarmason, Sep 29, 2018
  3. Duy NguyenSep 29, 2018
  4. Ævar Arnfjörð BjarmasonSep 29, 2018
  5. Duy NguyenSep 29, 2018
  6. Junio C HamanoSep 29, 2018
  7. Christoph BergSep 29, 2018
  8. René ScharfeOct 1, 2018
  9. Junio C HamanoOct 5, 2018
  10. Ævar Arnfjörð BjarmasonOct 5, 2018
  11. Stefan BellerOct 1, 2018
  12. Christoph BergOct 5, 2018
  13. Junio C HamanoOct 5, 2018
  14. Mischa POSLAWSKYOct 5, 2018
  15. Stefan BellerOct 5, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.