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

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

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Sep 29, 2018, 14:55 UTC
Message-ID
<20180929145527.23444-1-avarab@gmail.com>
In-Reply-To
<20180929140132.GA31238@msg.df7cb.de>

This --recursive (-r) option does nothing, and is purely here to appease people who have "grep -r ..." burned into their muscle memory.

Requested-by: Christoph Berg <myon@debian.org>
Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
On Sat, Sep 29, 2018 at 4:10 PM Christoph Berg <myon@debian.org> wrote:
Show 9 quoted lines
>
> I often use "grep -r $pattern" to recursively grep a source tree. If
> that takes too long, I hit ^C and tag "git" in front of the command
> line and re-run it. git then complains "error: unknown switch `r'"
> because "git grep" is naturally recursive.
>
> Could we have "git grep -r" accept the argument for compatibility?
> Other important grep switches like "-i" are compatible, adding -r
> would improve usability.

I don't have an opinion on this either way, it doesn't scratch my itch, but hey, why not. Here's a patch to implement it.

 Documentation/git-grep.txt | 6 ++++++
 builtin/grep.c             | 3 +++
 t/t7810-grep.sh            | 8 ++++++++
 3 files changed, 17 insertions(+)
diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index a3049af1a3..a1aea8be4e 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -290,6 +290,12 @@ providing this option will cause it to die.
 	Do not output matched lines; instead, exit with status 0 when
 	there is a match and with non-zero status when there isn't.
 
+-r::
+--recursive::
+	This option does nothing. git-grep is always recursive. This
+	noop option is provided for compatibility with the muscle
+	memory of people used to grep(1).
+
 <tree>...::
 	Instead of searching tracked files in the working tree, search
 	blobs in the given trees.
diff --git a/builtin/grep.c b/builtin/grep.c
index 601f801158..02d4384225 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -785,6 +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 */
 
 	struct option options[] = {
 		OPT_BOOL(0, "cached", &cached,
@@ -802,6 +803,8 @@ 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,
+			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")),
 		OPT_SET_INT('a', "text", &opt.binary,
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index be5c1bd553..c48d1fa34b 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -469,6 +469,14 @@ do
 		git grep --count -h -e b $H -- ab >actual &&
 		test_cmp expected actual
 	'
+
+	for flag in '' ' -r' ' --recursive'
+	do
+		test_expect_success "grep $flag . (testing that --recursive is a noop)" '
+			git grep$flag . >actual &&
+			test_line_count = 43 actual
+	'
+	done
 done
 
 cat >expected <<EOF
-- 
2.19.0.605.g01d371f741
Previous: Christoph BergNext: Duy Nguyen
Message 2 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.