grep: add -r/--[no-]recursive
authorRené Scharfe <l.s.r@web.de>
Mon, 1 Oct 2018 19:15:57 +0000 (21:15 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 4 Oct 2018 04:25:57 +0000 (21:25 -0700)
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>
Documentation/git-grep.txt
builtin/grep.c
t/t7810-grep.sh
index a3049af1a36c09325a88ad2cc822ae2891c8974b..84fe236a8ee2fd47814c4c7ee854c47279921169 100644 (file)
@@ -18,7 +18,7 @@ SYNOPSIS
           [(-O | --open-files-in-pager) [<pager>]]
           [-z | --null]
           [ -o | --only-matching ] [-c | --count] [--all-match] [-q | --quiet]
-          [--max-depth <depth>]
+          [--max-depth <depth>] [--[no-]recursive]
           [--color[=<when>] | --no-color]
           [--break] [--heading] [-p | --show-function]
           [-A <post-context>] [-B <pre-context>] [-C <context>]
@@ -119,11 +119,18 @@ OPTIONS
 
 --max-depth <depth>::
        For each <pathspec> given on command line, descend at most <depth>
-       levels of directories. A negative value means no limit.
+       levels of directories. A value of -1 means no limit.
        This option is ignored if <pathspec> contains active wildcards.
        In other words if "a*" matches a directory named "a*",
        "*" is matched literally so --max-depth is still effective.
 
+-r::
+--recursive::
+       Same as `--max-depth=-1`; this is the default.
+
+--no-recursive::
+       Same as `--max-depth=0`.
+
 -w::
 --word-regexp::
        Match the pattern only at word boundary (either begin at the
index 601f801158f097b302dcf6615016bb4cefbc0225..f6e127f0bca3b9569aa0bc29bbc70b3a91a78651 100644 (file)
@@ -811,6 +811,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        GREP_BINARY_NOMATCH),
                OPT_BOOL(0, "textconv", &opt.allow_textconv,
                         N_("process binary files with textconv filters")),
+               OPT_SET_INT('r', "recursive", &opt.max_depth,
+                           N_("search in subdirectories (default)"), -1),
                { OPTION_INTEGER, 0, "max-depth", &opt.max_depth, N_("depth"),
                        N_("descend at most <depth> levels"), PARSE_OPT_NONEG,
                        NULL, 1 },
index be5c1bd553ede55d54e1d918625c99000ba32d80..43aa4161cf030b4c58ab2785e0bffc6635127a5f 100755 (executable)
@@ -309,6 +309,8 @@ do
                        echo ${HC}v:1:vvv
                } >expected &&
                git grep --max-depth -1 -n -e vvv $H >actual &&
+               test_cmp expected actual &&
+               git grep --recursive -n -e vvv $H >actual &&
                test_cmp expected actual
        '
 
@@ -317,6 +319,8 @@ do
                        echo ${HC}v:1:vvv
                } >expected &&
                git grep --max-depth 0 -n -e vvv $H >actual &&
+               test_cmp expected actual &&
+               git grep --no-recursive -n -e vvv $H >actual &&
                test_cmp expected actual
        '
 
@@ -327,6 +331,8 @@ do
                        echo ${HC}v:1:vvv
                } >expected &&
                git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
+               test_cmp expected actual &&
+               git grep --no-recursive -n -e vvv $H -- "*" >actual &&
                test_cmp expected actual
        '
 
@@ -344,6 +350,8 @@ do
                        echo ${HC}t/v:1:vvv
                } >expected &&
                git grep --max-depth 0 -n -e vvv $H -- t >actual &&
+               test_cmp expected actual &&
+               git grep --no-recursive -n -e vvv $H -- t >actual &&
                test_cmp expected actual
        '
 
@@ -353,6 +361,8 @@ do
                        echo ${HC}v:1:vvv
                } >expected &&
                git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
+               test_cmp expected actual &&
+               git grep --no-recursive -n -e vvv $H -- . t >actual &&
                test_cmp expected actual
        '
 
@@ -362,6 +372,8 @@ do
                        echo ${HC}v:1:vvv
                } >expected &&
                git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
+               test_cmp expected actual &&
+               git grep --no-recursive -n -e vvv $H -- t . >actual &&
                test_cmp expected actual
        '
        test_expect_success "grep $L with grep.extendedRegexp=false" '