Imported Upstream version 2.25.1
[platform/upstream/git.git] / builtin / grep.c
index 3ca4ac8..ae2d5bb 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (c) 2006 Junio C Hamano
  */
+#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "cache.h"
 #include "repository.h"
 #include "config.h"
@@ -22,6 +23,7 @@
 #include "pathspec.h"
 #include "submodule.h"
 #include "submodule-config.h"
+#include "object-store.h"
 
 static char const * const grep_usage[] = {
        N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"),
@@ -33,7 +35,6 @@ static int recurse_submodules;
 #define GREP_NUM_THREADS_DEFAULT 8
 static int num_threads;
 
-#ifndef NO_PTHREADS
 static pthread_t *threads;
 
 /* We use one producer thread and THREADS consumer
@@ -69,13 +70,11 @@ static pthread_mutex_t grep_mutex;
 
 static inline void grep_lock(void)
 {
-       assert(num_threads);
        pthread_mutex_lock(&grep_mutex);
 }
 
 static inline void grep_unlock(void)
 {
-       assert(num_threads);
        pthread_mutex_unlock(&grep_mutex);
 }
 
@@ -92,8 +91,7 @@ static pthread_cond_t cond_result;
 
 static int skip_first_line;
 
-static void add_work(struct grep_opt *opt, enum grep_source_type type,
-                    const char *name, const char *path, const void *id)
+static void add_work(struct grep_opt *opt, const struct grep_source *gs)
 {
        grep_lock();
 
@@ -101,9 +99,10 @@ static void add_work(struct grep_opt *opt, enum grep_source_type type,
                pthread_cond_wait(&cond_write, &grep_mutex);
        }
 
-       grep_source_init(&todo[todo_end].source, type, name, path, id);
+       todo[todo_end].source = *gs;
        if (opt->binary != GREP_BINARY_TEXT)
-               grep_source_load_driver(&todo[todo_end].source);
+               grep_source_load_driver(&todo[todo_end].source,
+                                       opt->repo->index);
        todo[todo_end].done = 0;
        strbuf_reset(&todo[todo_end].out);
        todo_end = (todo_end + 1) % ARRAY_SIZE(todo);
@@ -233,6 +232,9 @@ static int wait_all(void)
        int hit = 0;
        int i;
 
+       if (!HAVE_THREADS)
+               BUG("Never call this function unless you have started threads");
+
        grep_lock();
        all_work_added = 1;
 
@@ -264,13 +266,6 @@ static int wait_all(void)
 
        return hit;
 }
-#else /* !NO_PTHREADS */
-
-static int wait_all(void)
-{
-       return 0;
-}
-#endif
 
 static int grep_cmd_config(const char *var, const char *value, void *cb)
 {
@@ -283,17 +278,15 @@ static int grep_cmd_config(const char *var, const char *value, void *cb)
                if (num_threads < 0)
                        die(_("invalid number of threads specified (%d) for %s"),
                            num_threads, var);
-#ifdef NO_PTHREADS
-               else if (num_threads && num_threads != 1) {
+               else if (!HAVE_THREADS && num_threads > 1) {
                        /*
                         * TRANSLATORS: %s is the configuration
                         * variable for tweaking threads, currently
                         * grep.threads
                         */
                        warning(_("no threads support, ignoring %s"), var);
-                       num_threads = 0;
+                       num_threads = 1;
                }
-#endif
        }
 
        if (!strcmp(var, "submodule.recurse"))
@@ -307,7 +300,7 @@ static void *lock_and_read_oid_file(const struct object_id *oid, enum object_typ
        void *data;
 
        grep_read_lock();
-       data = read_sha1_file(oid->hash, type, size);
+       data = read_object_file(oid, type, size);
        grep_read_unlock();
        return data;
 }
@@ -317,6 +310,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
                     const char *path)
 {
        struct strbuf pathbuf = STRBUF_INIT;
+       struct grep_source gs;
 
        if (opt->relative && opt->prefix_length) {
                quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
@@ -325,19 +319,19 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
                strbuf_addstr(&pathbuf, filename);
        }
 
-#ifndef NO_PTHREADS
-       if (num_threads) {
-               add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
-               strbuf_release(&pathbuf);
+       grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
+       strbuf_release(&pathbuf);
+
+       if (num_threads > 1) {
+               /*
+                * add_work() copies gs and thus assumes ownership of
+                * its fields, so do not call grep_source_clear()
+                */
+               add_work(opt, &gs);
                return 0;
-       } else
-#endif
-       {
-               struct grep_source gs;
+       } else {
                int hit;
 
-               grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
-               strbuf_release(&pathbuf);
                hit = grep_source(opt, &gs);
 
                grep_source_clear(&gs);
@@ -348,25 +342,26 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
 static int grep_file(struct grep_opt *opt, const char *filename)
 {
        struct strbuf buf = STRBUF_INIT;
+       struct grep_source gs;
 
        if (opt->relative && opt->prefix_length)
                quote_path_relative(filename, opt->prefix, &buf);
        else
                strbuf_addstr(&buf, filename);
 
-#ifndef NO_PTHREADS
-       if (num_threads) {
-               add_work(opt, GREP_SOURCE_FILE, buf.buf, filename, filename);
-               strbuf_release(&buf);
+       grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
+       strbuf_release(&buf);
+
+       if (num_threads > 1) {
+               /*
+                * add_work() copies gs and thus assumes ownership of
+                * its fields, so do not call grep_source_clear()
+                */
+               add_work(opt, &gs);
                return 0;
-       } else
-#endif
-       {
-               struct grep_source gs;
+       } else {
                int hit;
 
-               grep_source_init(&gs, GREP_SOURCE_FILE, buf.buf, filename, filename);
-               strbuf_release(&buf);
                hit = grep_source(opt, &gs);
 
                grep_source_clear(&gs);
@@ -399,27 +394,43 @@ static void run_pager(struct grep_opt *opt, const char *prefix)
                exit(status);
 }
 
-static int grep_cache(struct grep_opt *opt, struct repository *repo,
+static int grep_cache(struct grep_opt *opt,
                      const struct pathspec *pathspec, int cached);
 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
                     struct tree_desc *tree, struct strbuf *base, int tn_len,
-                    int check_attr, struct repository *repo);
+                    int check_attr);
 
-static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
+static int grep_submodule(struct grep_opt *opt,
                          const struct pathspec *pathspec,
                          const struct object_id *oid,
-                         const char *filename, const char *path)
+                         const char *filename, const char *path, int cached)
 {
-       struct repository submodule;
+       struct repository subrepo;
+       struct repository *superproject = opt->repo;
+       const struct submodule *sub = submodule_from_path(superproject,
+                                                         &null_oid, path);
+       struct grep_opt subopt;
        int hit;
 
-       if (!is_submodule_active(superproject, path))
+       /*
+        * NEEDSWORK: submodules functions need to be protected because they
+        * access the object store via config_from_gitmodules(): the latter
+        * uses get_oid() which, for now, relies on the global the_repository
+        * object.
+        */
+       grep_read_lock();
+
+       if (!is_submodule_active(superproject, path)) {
+               grep_read_unlock();
                return 0;
+       }
 
-       if (repo_submodule_init(&submodule, superproject, path))
+       if (repo_submodule_init(&subrepo, superproject, sub)) {
+               grep_read_unlock();
                return 0;
+       }
 
-       repo_read_gitmodules(&submodule);
+       repo_read_gitmodules(&subrepo);
 
        /*
         * NEEDSWORK: This adds the submodule's object directory to the list of
@@ -431,10 +442,12 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
         * store is no longer global and instead is a member of the repository
         * object.
         */
-       grep_read_lock();
-       add_to_alternates_memory(submodule.objectdir);
+       add_to_alternates_memory(subrepo.objects->odb->path);
        grep_read_unlock();
 
+       memcpy(&subopt, opt, sizeof(subopt));
+       subopt.repo = &subrepo;
+
        if (oid) {
                struct object *object;
                struct tree_desc tree;
@@ -445,7 +458,8 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
                object = parse_object_or_die(oid, oid_to_hex(oid));
 
                grep_read_lock();
-               data = read_object_with_reference(object->oid.hash, tree_type,
+               data = read_object_with_reference(&subrepo,
+                                                 &object->oid, tree_type,
                                                  &size, NULL);
                grep_read_unlock();
 
@@ -456,21 +470,22 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
                strbuf_addch(&base, '/');
 
                init_tree_desc(&tree, data, size);
-               hit = grep_tree(opt, pathspec, &tree, &base, base.len,
-                               object->type == OBJ_COMMIT, &submodule);
+               hit = grep_tree(&subopt, pathspec, &tree, &base, base.len,
+                               object->type == OBJ_COMMIT);
                strbuf_release(&base);
                free(data);
        } else {
-               hit = grep_cache(opt, &submodule, pathspec, 1);
+               hit = grep_cache(&subopt, pathspec, cached);
        }
 
-       repo_clear(&submodule);
+       repo_clear(&subrepo);
        return hit;
 }
 
-static int grep_cache(struct grep_opt *opt, struct repository *repo,
+static int grep_cache(struct grep_opt *opt,
                      const struct pathspec *pathspec, int cached)
 {
+       struct repository *repo = opt->repo;
        int hit = 0;
        int nr;
        struct strbuf name = STRBUF_INIT;
@@ -480,7 +495,8 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
                strbuf_addstr(&name, repo->submodule_prefix);
        }
 
-       repo_read_index(repo);
+       if (repo_read_index(repo) < 0)
+               die(_("index file corrupt"));
 
        for (nr = 0; nr < repo->index->cache_nr; nr++) {
                const struct cache_entry *ce = repo->index->cache[nr];
@@ -488,7 +504,7 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
                strbuf_addstr(&name, ce->name);
 
                if (S_ISREG(ce->ce_mode) &&
-                   match_pathspec(pathspec, name.buf, name.len, 0, NULL,
+                   match_pathspec(repo->index, pathspec, name.buf, name.len, 0, NULL,
                                   S_ISDIR(ce->ce_mode) ||
                                   S_ISGITLINK(ce->ce_mode))) {
                        /*
@@ -506,8 +522,9 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
                                hit |= grep_file(opt, name.buf);
                        }
                } else if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
-                          submodule_path_match(pathspec, name.buf, NULL)) {
-                       hit |= grep_submodule(opt, repo, pathspec, NULL, ce->name, ce->name);
+                          submodule_path_match(repo->index, pathspec, name.buf, NULL)) {
+                       hit |= grep_submodule(opt, pathspec, NULL, ce->name,
+                                             ce->name, cached);
                } else {
                        continue;
                }
@@ -529,8 +546,9 @@ static int grep_cache(struct grep_opt *opt, struct repository *repo,
 
 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
                     struct tree_desc *tree, struct strbuf *base, int tn_len,
-                    int check_attr, struct repository *repo)
+                    int check_attr)
 {
+       struct repository *repo = opt->repo;
        int hit = 0;
        enum interesting match = entry_not_interesting;
        struct name_entry entry;
@@ -547,7 +565,8 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 
                if (match != all_entries_interesting) {
                        strbuf_addstr(&name, base->buf + tn_len);
-                       match = tree_entry_interesting(&entry, &name,
+                       match = tree_entry_interesting(repo->index,
+                                                      &entry, &name,
                                                       0, pathspec);
                        strbuf_setlen(&name, name_base_len);
 
@@ -560,7 +579,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
                strbuf_add(base, entry.path, te_len);
 
                if (S_ISREG(entry.mode)) {
-                       hit |= grep_oid(opt, entry.oid, base->buf, tn_len,
+                       hit |= grep_oid(opt, &entry.oid, base->buf, tn_len,
                                         check_attr ? base->buf + tn_len : NULL);
                } else if (S_ISDIR(entry.mode)) {
                        enum object_type type;
@@ -568,19 +587,20 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
                        void *data;
                        unsigned long size;
 
-                       data = lock_and_read_oid_file(entry.oid, &type, &size);
+                       data = lock_and_read_oid_file(&entry.oid, &type, &size);
                        if (!data)
                                die(_("unable to read tree (%s)"),
-                                   oid_to_hex(entry.oid));
+                                   oid_to_hex(&entry.oid));
 
                        strbuf_addch(base, '/');
                        init_tree_desc(&sub, data, size);
                        hit |= grep_tree(opt, pathspec, &sub, base, tn_len,
-                                        check_attr, repo);
+                                        check_attr);
                        free(data);
                } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
-                       hit |= grep_submodule(opt, repo, pathspec, entry.oid,
-                                             base->buf, base->buf + tn_len);
+                       hit |= grep_submodule(opt, pathspec, &entry.oid,
+                                             base->buf, base->buf + tn_len,
+                                             1); /* ignored */
                }
 
                strbuf_setlen(base, old_baselen);
@@ -594,8 +614,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 }
 
 static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
-                      struct object *obj, const char *name, const char *path,
-                      struct repository *repo)
+                      struct object *obj, const char *name, const char *path)
 {
        if (obj->type == OBJ_BLOB)
                return grep_oid(opt, &obj->oid, name, 0, path);
@@ -607,7 +626,8 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
                int hit, len;
 
                grep_read_lock();
-               data = read_object_with_reference(obj->oid.hash, tree_type,
+               data = read_object_with_reference(opt->repo,
+                                                 &obj->oid, tree_type,
                                                  &size, NULL);
                grep_read_unlock();
 
@@ -622,16 +642,15 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,
                }
                init_tree_desc(&tree, data, size);
                hit = grep_tree(opt, pathspec, &tree, &base, base.len,
-                               obj->type == OBJ_COMMIT, repo);
+                               obj->type == OBJ_COMMIT);
                strbuf_release(&base);
                free(data);
                return hit;
        }
-       die(_("unable to grep from object of type %s"), typename(obj->type));
+       die(_("unable to grep from object of type %s"), type_name(obj->type));
 }
 
 static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
-                       struct repository *repo,
                        const struct object_array *list)
 {
        unsigned int i;
@@ -640,15 +659,16 @@ static int grep_objects(struct grep_opt *opt, const struct pathspec *pathspec,
 
        for (i = 0; i < nr; i++) {
                struct object *real_obj;
-               real_obj = deref_tag(list->objects[i].item, NULL, 0);
+               real_obj = deref_tag(opt->repo, list->objects[i].item,
+                                    NULL, 0);
 
                /* load the gitmodules file for this rev */
                if (recurse_submodules) {
-                       submodule_free();
+                       submodule_free(opt->repo);
                        gitmodules_config_oid(&real_obj->oid);
                }
-               if (grep_object(opt, pathspec, real_obj, list->objects[i].name, list->objects[i].path,
-                               repo)) {
+               if (grep_object(opt, pathspec, real_obj, list->objects[i].name,
+                               list->objects[i].path)) {
                        hit = 1;
                        if (opt->status_only)
                                break;
@@ -669,9 +689,9 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
        if (exc_std)
                setup_standard_excludes(&dir);
 
-       fill_directory(&dir, &the_index, pathspec);
+       fill_directory(&dir, opt->repo->index, pathspec);
        for (i = 0; i < dir.nr; i++) {
-               if (!dir_path_match(dir.entries[i], pathspec, 0, NULL))
+               if (!dir_path_match(opt->repo->index, dir.entries[i], pathspec, 0, NULL))
                        continue;
                hit |= grep_file(opt, dir.entries[i]->name);
                if (hit && opt->status_only)
@@ -703,11 +723,14 @@ static int context_callback(const struct option *opt, const char *arg,
 static int file_callback(const struct option *opt, const char *arg, int unset)
 {
        struct grep_opt *grep_opt = opt->value;
-       int from_stdin = !strcmp(arg, "-");
+       int from_stdin;
        FILE *patterns;
        int lno = 0;
        struct strbuf sb = STRBUF_INIT;
 
+       BUG_ON_OPT_NEG(unset);
+
+       from_stdin = !strcmp(arg, "-");
        patterns = from_stdin ? stdin : fopen(arg, "r");
        if (!patterns)
                die_errno(_("cannot open '%s'"), arg);
@@ -728,6 +751,8 @@ static int file_callback(const struct option *opt, const char *arg, int unset)
 static int not_callback(const struct option *opt, const char *arg, int unset)
 {
        struct grep_opt *grep_opt = opt->value;
+       BUG_ON_OPT_NEG(unset);
+       BUG_ON_OPT_ARG(arg);
        append_grep_pattern(grep_opt, "--not", "command line", 0, GREP_NOT);
        return 0;
 }
@@ -735,6 +760,8 @@ static int not_callback(const struct option *opt, const char *arg, int unset)
 static int and_callback(const struct option *opt, const char *arg, int unset)
 {
        struct grep_opt *grep_opt = opt->value;
+       BUG_ON_OPT_NEG(unset);
+       BUG_ON_OPT_ARG(arg);
        append_grep_pattern(grep_opt, "--and", "command line", 0, GREP_AND);
        return 0;
 }
@@ -742,6 +769,8 @@ static int and_callback(const struct option *opt, const char *arg, int unset)
 static int open_callback(const struct option *opt, const char *arg, int unset)
 {
        struct grep_opt *grep_opt = opt->value;
+       BUG_ON_OPT_NEG(unset);
+       BUG_ON_OPT_ARG(arg);
        append_grep_pattern(grep_opt, "(", "command line", 0, GREP_OPEN_PAREN);
        return 0;
 }
@@ -749,6 +778,8 @@ static int open_callback(const struct option *opt, const char *arg, int unset)
 static int close_callback(const struct option *opt, const char *arg, int unset)
 {
        struct grep_opt *grep_opt = opt->value;
+       BUG_ON_OPT_NEG(unset);
+       BUG_ON_OPT_ARG(arg);
        append_grep_pattern(grep_opt, ")", "command line", 0, GREP_CLOSE_PAREN);
        return 0;
 }
@@ -757,6 +788,7 @@ static int pattern_callback(const struct option *opt, const char *arg,
                            int unset)
 {
        struct grep_opt *grep_opt = opt->value;
+       BUG_ON_OPT_NEG(unset);
        append_grep_pattern(grep_opt, arg, "-e option", 0, GREP_PATTERN);
        return 0;
 }
@@ -803,6 +835,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 },
@@ -821,6 +855,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                            GREP_PATTERN_TYPE_PCRE),
                OPT_GROUP(""),
                OPT_BOOL('n', "line-number", &opt.linenum, N_("show line numbers")),
+               OPT_BOOL(0, "column", &opt.columnnum, N_("show column number of first match")),
                OPT_NEGBIT('h', NULL, &opt.pathname, N_("don't show filenames"), 1),
                OPT_BIT('H', NULL, &opt.pathname, N_("show filenames"), 1),
                OPT_NEGBIT(0, "full-name", &opt.relative,
@@ -832,8 +867,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                OPT_BOOL('L', "files-without-match",
                        &opt.unmatch_name_only,
                        N_("show only the names of files without match")),
-               OPT_BOOL('z', "null", &opt.null_following_name,
-                       N_("print NUL after filenames")),
+               OPT_BOOL_F('z', "null", &opt.null_following_name,
+                          N_("print NUL after filenames"),
+                          PARSE_OPT_NOCOMPLETE),
+               OPT_BOOL('o', "only-matching", &opt.only_matching,
+                       N_("show only matching parts of a line")),
                OPT_BOOL('c', "count", &opt.count,
                        N_("show the number of matches instead of matching lines")),
                OPT__COLOR(&opt.color, N_("highlight matches")),
@@ -878,21 +916,23 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                           N_("indicate hit with exit status without output")),
                OPT_BOOL(0, "all-match", &opt.all_match,
                        N_("show only matches from files that match all patterns")),
-               { OPTION_SET_INT, 0, "debug", &opt.debug, NULL,
-                 N_("show parse tree for grep expression"),
-                 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1 },
+               OPT_SET_INT_F(0, "debug", &opt.debug,
+                             N_("show parse tree for grep expression"),
+                             1, PARSE_OPT_HIDDEN),
                OPT_GROUP(""),
                { OPTION_STRING, 'O', "open-files-in-pager", &show_in_pager,
                        N_("pager"), N_("show matching files in the pager"),
-                       PARSE_OPT_OPTARG, NULL, (intptr_t)default_pager },
-               OPT_BOOL(0, "ext-grep", &external_grep_allowed__ignored,
-                        N_("allow calling of grep(1) (ignored by this build)")),
+                       PARSE_OPT_OPTARG | PARSE_OPT_NOCOMPLETE,
+                       NULL, (intptr_t)default_pager },
+               OPT_BOOL_F(0, "ext-grep", &external_grep_allowed__ignored,
+                          N_("allow calling of grep(1) (ignored by this build)"),
+                          PARSE_OPT_NOCOMPLETE),
                OPT_END()
        };
 
-       init_grep_defaults();
+       init_grep_defaults(the_repository);
        git_config(grep_cmd_config, NULL);
-       grep_init(&opt, prefix);
+       grep_init(&opt, the_repository, prefix);
 
        /*
         * If there is no -- then the paths must exist in the working
@@ -918,6 +958,9 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        /* die the same way as if we did it at the beginning */
                        setup_git_directory();
        }
+       /* Ignore --recurse-submodules if --no-index is given or implied */
+       if (!use_index)
+               recurse_submodules = 0;
 
        /*
         * skip a -- separator; we know it cannot be
@@ -949,7 +992,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
        }
 
        if (!opt.pattern_list)
-               die(_("no pattern given."));
+               die(_("no pattern given"));
+
+       /* --only-matching has no effect with --invert. */
+       if (opt.invert)
+               opt.only_matching = 0;
 
        /*
         * We have to find "--" in a separate pass, because its presence
@@ -985,7 +1032,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        break;
                }
 
-               if (get_oid_with_context(arg, GET_OID_RECORD_PATH,
+               if (get_oid_with_context(the_repository, arg,
+                                        GET_OID_RECORD_PATH,
                                         &oid, &oc)) {
                        if (seen_dashdash)
                                die(_("unable to resolve revision: %s"), arg);
@@ -1017,39 +1065,35 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
        pathspec.recursive = 1;
        pathspec.recurse_submodules = !!recurse_submodules;
 
-#ifndef NO_PTHREADS
-       if (list.nr || cached || show_in_pager)
-               num_threads = 0;
-       else if (num_threads == 0)
-               num_threads = GREP_NUM_THREADS_DEFAULT;
-       else if (num_threads < 0)
-               die(_("invalid number of threads specified (%d)"), num_threads);
-       if (num_threads == 1)
-               num_threads = 0;
-#else
-       if (num_threads)
+       if (list.nr || cached || show_in_pager) {
+               if (num_threads > 1)
+                       warning(_("invalid option combination, ignoring --threads"));
+               num_threads = 1;
+       } else if (!HAVE_THREADS && num_threads > 1) {
                warning(_("no threads support, ignoring --threads"));
-       num_threads = 0;
-#endif
+               num_threads = 1;
+       } else if (num_threads < 0)
+               die(_("invalid number of threads specified (%d)"), num_threads);
+       else if (num_threads == 0)
+               num_threads = HAVE_THREADS ? GREP_NUM_THREADS_DEFAULT : 1;
 
-       if (!num_threads)
+       if (num_threads > 1) {
+               if (!HAVE_THREADS)
+                       BUG("Somebody got num_threads calculation wrong!");
+               if (!(opt.name_only || opt.unmatch_name_only || opt.count)
+                   && (opt.pre_context || opt.post_context ||
+                       opt.file_break || opt.funcbody))
+                       skip_first_line = 1;
+               start_threads(&opt);
+       } else {
                /*
                 * The compiled patterns on the main path are only
                 * used when not using threading. Otherwise
-                * start_threads() below calls compile_grep_patterns()
+                * start_threads() above calls compile_grep_patterns()
                 * for each thread.
                 */
                compile_grep_patterns(&opt);
-
-#ifndef NO_PTHREADS
-       if (num_threads) {
-               if (!(opt.name_only || opt.unmatch_name_only || opt.count)
-                   && (opt.pre_context || opt.post_context ||
-                       opt.file_break || opt.funcbody))
-                       skip_first_line = 1;
-               start_threads(&opt);
        }
-#endif
 
        if (show_in_pager && (cached || list.nr))
                die(_("--open-files-in-pager only works on the worktree"));
@@ -1069,42 +1113,43 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        strbuf_addf(&buf, "+/%s%s",
                                        strcmp("less", pager) ? "" : "*",
                                        opt.pattern_list->pattern);
-                       string_list_append(&path_list, buf.buf);
-                       strbuf_detach(&buf, NULL);
+                       string_list_append(&path_list,
+                                          strbuf_detach(&buf, NULL));
                }
        }
 
-       if (recurse_submodules && (!use_index || untracked))
-               die(_("option not supported with --recurse-submodules."));
+       if (recurse_submodules && untracked)
+               die(_("--untracked not supported with --recurse-submodules"));
 
        if (!show_in_pager && !opt.status_only)
                setup_pager();
 
        if (!use_index && (untracked || cached))
-               die(_("--cached or --untracked cannot be used with --no-index."));
+               die(_("--cached or --untracked cannot be used with --no-index"));
 
        if (!use_index || untracked) {
                int use_exclude = (opt_exclude < 0) ? use_index : !!opt_exclude;
                hit = grep_directory(&opt, &pathspec, use_exclude, use_index);
        } else if (0 <= opt_exclude) {
-               die(_("--[no-]exclude-standard cannot be used for tracked contents."));
+               die(_("--[no-]exclude-standard cannot be used for tracked contents"));
        } else if (!list.nr) {
                if (!cached)
                        setup_work_tree();
 
-               hit = grep_cache(&opt, the_repository, &pathspec, cached);
+               hit = grep_cache(&opt, &pathspec, cached);
        } else {
                if (cached)
-                       die(_("both --cached and trees are given."));
+                       die(_("both --cached and trees are given"));
 
-               hit = grep_objects(&opt, &pathspec, the_repository, &list);
+               hit = grep_objects(&opt, &pathspec, &list);
        }
 
-       if (num_threads)
+       if (num_threads > 1)
                hit |= wait_all();
        if (hit && show_in_pager)
                run_pager(&opt, prefix);
        clear_pathspec(&pathspec);
        free_grep_patterns(&opt);
+       grep_destroy();
        return !hit;
 }