path: root/builtin/grep.c
diff options
authorJonathan Tan <>2017-02-14 06:03:03 (GMT)
committerJunio C Hamano <>2017-02-14 19:26:37 (GMT)
commitdca3b5f5cef8138e695f87e6cb37fb2063a10ea4 (patch)
treee179d0e47bfad555b21ee7d73a80c75fd9afb467 /builtin/grep.c
parenta0fe2b0d2329d38a08c03427917b21be818bec1f (diff)
grep: do not unnecessarily query repo for "--"
When running a command of the form git grep --no-index pattern -- path in the absence of a Git repository, an error message will be printed: fatal: BUG: setup_git_env called without repository This is because "git grep" tries to interpret "--" as a rev. "git grep" has always tried to first interpret "--" as a rev for at least a few years, but this issue was upgraded from a pessimization to a bug in commit 59332d1 ("Resurrect "git grep --no-index"", 2010-02-06), which calls get_sha1 regardless of whether --no-index was specified. This bug appeared to be benign until commit b1ef400 ("setup_git_env: avoid blind fall-back to ".git"", 2016-10-20) when Git was taught to die in this situation. (This "git grep" bug appears to be one of the bugs that commit b1ef400 is meant to flush out.) Therefore, always interpret "--" as signaling the end of options, instead of trying to interpret it as a rev first. Signed-off-by: Jonathan Tan <> Signed-off-by: Jeff King <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/grep.c')
1 files changed, 5 insertions, 4 deletions
diff --git a/builtin/grep.c b/builtin/grep.c
index 5a282c4..081e1b5 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -1154,6 +1154,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
const char *arg = argv[i];
unsigned char sha1[20];
struct object_context oc;
+ if (!strcmp(arg, "--")) {
+ i++;
+ seen_dashdash = 1;
+ break;
+ }
/* Is it a rev? */
if (!get_sha1_with_context(arg, 0, sha1, &oc)) {
struct object *object = parse_object_or_die(sha1, arg);
@@ -1162,10 +1167,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
- if (!strcmp(arg, "--")) {
- i++;
- seen_dashdash = 1;
- }