Skip to content

Commit 0a09e5e

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Suggested-by: Junio C Hamano <[email protected]> Initial-patch-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fe8321e commit 0a09e5e

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Documentation/git-grep.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ SYNOPSIS
1818
[(-O | --open-files-in-pager) [<pager>]]
1919
[-z | --null]
2020
[ -o | --only-matching ] [-c | --count] [--all-match] [-q | --quiet]
21-
[--max-depth <depth>]
21+
[--max-depth <depth>] [--[no-]recursive]
2222
[--color[=<when>] | --no-color]
2323
[--break] [--heading] [-p | --show-function]
2424
[-A <post-context>] [-B <pre-context>] [-C <context>]
@@ -119,11 +119,18 @@ OPTIONS
119119

120120
--max-depth <depth>::
121121
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.
123123
This option is ignored if <pathspec> contains active wildcards.
124124
In other words if "a*" matches a directory named "a*",
125125
"*" is matched literally so --max-depth is still effective.
126126

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+
127134
-w::
128135
--word-regexp::
129136
Match the pattern only at word boundary (either begin at the

builtin/grep.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
811811
GREP_BINARY_NOMATCH),
812812
OPT_BOOL(0, "textconv", &opt.allow_textconv,
813813
N_("process binary files with textconv filters")),
814+
OPT_SET_INT('r', "recursive", &opt.max_depth,
815+
N_("search in subdirectories (default)"), -1),
814816
{ OPTION_INTEGER, 0, "max-depth", &opt.max_depth, N_("depth"),
815817
N_("descend at most <depth> levels"), PARSE_OPT_NONEG,
816818
NULL, 1 },

t/t7810-grep.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ do
309309
echo ${HC}v:1:vvv
310310
} >expected &&
311311
git grep --max-depth -1 -n -e vvv $H >actual &&
312+
test_cmp expected actual &&
313+
git grep --recursive -n -e vvv $H >actual &&
312314
test_cmp expected actual
313315
'
314316

@@ -317,6 +319,8 @@ do
317319
echo ${HC}v:1:vvv
318320
} >expected &&
319321
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 &&
320324
test_cmp expected actual
321325
'
322326

@@ -327,6 +331,8 @@ do
327331
echo ${HC}v:1:vvv
328332
} >expected &&
329333
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 &&
330336
test_cmp expected actual
331337
'
332338

@@ -344,6 +350,8 @@ do
344350
echo ${HC}t/v:1:vvv
345351
} >expected &&
346352
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 &&
347355
test_cmp expected actual
348356
'
349357

@@ -353,6 +361,8 @@ do
353361
echo ${HC}v:1:vvv
354362
} >expected &&
355363
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 &&
356366
test_cmp expected actual
357367
'
358368

@@ -362,6 +372,8 @@ do
362372
echo ${HC}v:1:vvv
363373
} >expected &&
364374
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 &&
365377
test_cmp expected actual
366378
'
367379
test_expect_success "grep $L with grep.extendedRegexp=false" '

0 commit comments

Comments
 (0)