grep: add -r/--[no-]recursive

Recognize -r and --recursive as synonyms for --max-depth=-1 for compatibility with GNU grep; it's still the default for git grep. This also adds --no-recursive as synonym for --max-depth=0 for free, which is welcome for completeness and consistency. Fix the description for --max-depth, while we're at it -- negative values other than -1 actually disable recursion, i.e. they are equivalent to --max-depth=0. Requested-by: Christoph Berg <myon@debian.org> Suggested-by: Junio C Hamano <gitster@pobox.com> Initial-patch-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 1, 2018 at 21:15 UTC 0a09e5edc281941b77dbf7aa3436781250d16088
3 files changed +23 -2
Documentation/git-grep.txt
+9 -2
@@ -18,7 +18,7 @@ SYNOPSIS
18 [(-O | --open-files-in-pager) [<pager>]]
19 [-z | --null]
20 [ -o | --only-matching ] [-c | --count] [--all-match] [-q | --quiet]
21 - [--max-depth <depth>]
21 + [--max-depth <depth>] [--[no-]recursive]
22 [--color[=<when>] | --no-color]
23 [--break] [--heading] [-p | --show-function]
24 [-A <post-context>] [-B <pre-context>] [-C <context>]
@@ -119,11 +119,18 @@ OPTIONS
119
120 --max-depth <depth>::
121 For each <pathspec> given on command line, descend at most <depth>
122 - levels of directories. A negative value means no limit.
122 + levels of directories. A value of -1 means no limit.
123 This option is ignored if <pathspec> contains active wildcards.
124 In other words if "a*" matches a directory named "a*",
125 "*" is matched literally so --max-depth is still effective.
126
127 +-r::
128 +--recursive::
129 + Same as `--max-depth=-1`; this is the default.
130 +
131 +--no-recursive::
132 + Same as `--max-depth=0`.
133 +
134 -w::
135 --word-regexp::
136 Match the pattern only at word boundary (either begin at the
builtin/grep.c
+2
@@ -811,6 +811,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
811 GREP_BINARY_NOMATCH),
812 OPT_BOOL(0, "textconv", &opt.allow_textconv,
813 N_("process binary files with textconv filters")),
814 + OPT_SET_INT('r', "recursive", &opt.max_depth,
815 + N_("search in subdirectories (default)"), -1),
816 { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, N_("depth"),
817 N_("descend at most <depth> levels"), PARSE_OPT_NONEG,
818 NULL, 1 },
t/t7810-grep.sh
+12
@@ -309,6 +309,8 @@ do
309 echo ${HC}v:1:vvv
310 } >expected &&
311 git grep --max-depth -1 -n -e vvv $H >actual &&
312 + test_cmp expected actual &&
313 + git grep --recursive -n -e vvv $H >actual &&
314 test_cmp expected actual
315 '
316
@@ -317,6 +319,8 @@ do
319 echo ${HC}v:1:vvv
320 } >expected &&
321 git grep --max-depth 0 -n -e vvv $H >actual &&
322 + test_cmp expected actual &&
323 + git grep --no-recursive -n -e vvv $H >actual &&
324 test_cmp expected actual
325 '
326
@@ -327,6 +331,8 @@ do
331 echo ${HC}v:1:vvv
332 } >expected &&
333 git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
334 + test_cmp expected actual &&
335 + git grep --no-recursive -n -e vvv $H -- "*" >actual &&
336 test_cmp expected actual
337 '
338
@@ -344,6 +350,8 @@ do
350 echo ${HC}t/v:1:vvv
351 } >expected &&
352 git grep --max-depth 0 -n -e vvv $H -- t >actual &&
353 + test_cmp expected actual &&
354 + git grep --no-recursive -n -e vvv $H -- t >actual &&
355 test_cmp expected actual
356 '
357
@@ -353,6 +361,8 @@ do
361 echo ${HC}v:1:vvv
362 } >expected &&
363 git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
364 + test_cmp expected actual &&
365 + git grep --no-recursive -n -e vvv $H -- . t >actual &&
366 test_cmp expected actual
367 '
368
@@ -362,6 +372,8 @@ do
372 echo ${HC}v:1:vvv
373 } >expected &&
374 git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
375 + test_cmp expected actual &&
376 + git grep --no-recursive -n -e vvv $H -- t . >actual &&
377 test_cmp expected actual
378 '
379 test_expect_success "grep $L with grep.extendedRegexp=false" '