Skip to content

Commit 9822b8f

Browse files
committed
Merge branch 'rs/grep-no-recursive'
Unlike "grep", "git grep" by default recurses to the whole tree. The command learned "git grep --recursive" option, so that "git grep --no-recursive" can serve as a synonym to setting the max-depth to 0. * rs/grep-no-recursive: grep: add -r/--[no-]recursive
2 parents 54e564e + 0a09e5e commit 9822b8f

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
@@ -812,6 +812,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
812812
GREP_BINARY_NOMATCH),
813813
OPT_BOOL(0, "textconv", &opt.allow_textconv,
814814
N_("process binary files with textconv filters")),
815+
OPT_SET_INT('r', "recursive", &opt.max_depth,
816+
N_("search in subdirectories (default)"), -1),
815817
{ OPTION_INTEGER, 0, "max-depth", &opt.max_depth, N_("depth"),
816818
N_("descend at most <depth> levels"), PARSE_OPT_NONEG,
817819
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)