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

Disallow empty pattern in "git grep"

From
Linus Torvalds <torvalds@osdl.org>
Date
Nov 16, 2005, 17:38 UTC
Message-ID
<Pine.LNX.4.64.0511160934480.13959@g5.osdl.org>

For some reason I've done a "git grep" twice with no pattern, which is really irritating, since it just grep everything. If I actually wanted that, I could do "git grep ^" or something.

So add a "usage" message if the pattern is empty.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---

Yeah, maybe we should allow an empty pattern, and just check the number of arguments instead. However, the argument parsing isn't set up that way, so this was the simple and ugly approach.

So this will warn even for
	git grep ""
which might be otherwise be considered legal (but stupid).
diff --git a/git-grep.sh b/git-grep.sh
index e7a35eb..44c1613 100755
--- a/git-grep.sh
+++ b/git-grep.sh
@@ -39,5 +39,9 @@ while : ; do
 	esac
 	shift
 done
+[ "$pattern" ] || {
+	echo >&2 "usage: 'git grep <pattern> [pathspec*]'"
+	exit 1
+}
 git-ls-files -z "${git_flags[@]}" "$@" |
 	xargs -0 grep "${flags[@]}" -e "$pattern"
Next: Junio C Hamano
Message 1 of 2 in “Disallow empty pattern in "git grep"”
  1. Linus TorvaldsNov 16, 2005
  2. Junio C HamanoNov 16, 2005

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.