]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/grep.c
Revert "color: check color.ui in git_default_config()"
[thirdparty/git.git] / builtin / grep.c
index 254c1c7849152884d9abd0b603f1c75f87e085b5..7e79eb1a754a5f604829e3e9e6d98d83712be9b6 100644 (file)
@@ -4,6 +4,8 @@
  * Copyright (c) 2006 Junio C Hamano
  */
 #include "cache.h"
+#include "repository.h"
+#include "config.h"
 #include "blob.h"
 #include "tree.h"
 #include "commit.h"
@@ -73,14 +75,14 @@ static pthread_mutex_t grep_mutex;
 
 static inline void grep_lock(void)
 {
-       if (num_threads)
-               pthread_mutex_lock(&grep_mutex);
+       assert(num_threads);
+       pthread_mutex_lock(&grep_mutex);
 }
 
 static inline void grep_unlock(void)
 {
-       if (num_threads)
-               pthread_mutex_unlock(&grep_mutex);
+       assert(num_threads);
+       pthread_mutex_unlock(&grep_mutex);
 }
 
 /* Signalled when a new work_item is added to todo. */
@@ -224,7 +226,8 @@ static void start_threads(struct grep_opt *opt)
                int err;
                struct grep_opt *o = grep_opt_dup(opt);
                o->output = strbuf_out;
-               o->debug = 0;
+               if (i)
+                       o->debug = 0;
                compile_grep_patterns(o);
                err = pthread_create(&threads[i], NULL, run, o);
 
@@ -289,8 +292,22 @@ 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) {
+                       /*
+                        * TRANSLATORS: %s is the configuration
+                        * variable for tweaking threads, currently
+                        * grep.threads
+                        */
+                       warning(_("no threads support, ignoring %s"), var);
+                       num_threads = 0;
+               }
+#endif
        }
 
+       if (!strcmp(var, "submodule.recurse"))
+               recurse_submodules = git_config_bool(var, value);
+
        return st;
 }
 
@@ -327,7 +344,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
 
 #ifndef NO_PTHREADS
        if (num_threads) {
-               add_work(opt, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
+               add_work(opt, GREP_SOURCE_OID, pathbuf.buf, path, oid);
                strbuf_release(&pathbuf);
                return 0;
        } else
@@ -336,7 +353,7 @@ static int grep_oid(struct grep_opt *opt, const struct object_id *oid,
                struct grep_source gs;
                int hit;
 
-               grep_source_init(&gs, GREP_SOURCE_SHA1, pathbuf.buf, path, oid);
+               grep_source_init(&gs, GREP_SOURCE_OID, pathbuf.buf, path, oid);
                strbuf_release(&pathbuf);
                hit = grep_source(opt, &gs);
 
@@ -495,6 +512,8 @@ static void compile_submodule_options(const struct grep_opt *opt,
                break;
        case GREP_PATTERN_TYPE_UNSPECIFIED:
                break;
+       default:
+               die("BUG: Added a new grep pattern type without updating switch statement");
        }
 
        for (pattern = opt->pattern_list; pattern != NULL;
@@ -524,7 +543,7 @@ static void compile_submodule_options(const struct grep_opt *opt,
         * submodule process has its own thread pool.
         */
        argv_array_pushf(&submodule_options, "--threads=%d",
-                        (num_threads + 1) / 2);
+                        DIV_ROUND_UP(num_threads, 2));
 
        /* Add Pathspecs */
        argv_array_push(&submodule_options, "--");
@@ -570,7 +589,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
         * with the object's name: 'tree-name:filename'.  In order to
         * provide uniformity of output we want to pass the name of the
         * parent project's object name to the submodule so the submodule can
-        * prefix its output with the parent's name and not its own SHA1.
+        * prefix its output with the parent's name and not its own OID.
         */
        if (gs->identifier && end_of_base)
                argv_array_pushf(&cp.args, "--parent-basename=%.*s",
@@ -583,12 +602,12 @@ static int grep_submodule_launch(struct grep_opt *opt,
                 * If there is a tree identifier for the submodule, add the
                 * rev after adding the submodule options but before the
                 * pathspecs.  To do this we listen for the '--' and insert the
-                * sha1 before pushing the '--' onto the child process argv
+                * oid before pushing the '--' onto the child process argv
                 * array.
                 */
                if (gs->identifier &&
                    !strcmp("--", submodule_options.argv[i])) {
-                       argv_array_push(&cp.args, sha1_to_hex(gs->identifier));
+                       argv_array_push(&cp.args, oid_to_hex(gs->identifier));
                }
 
                argv_array_push(&cp.args, submodule_options.argv[i]);
@@ -618,21 +637,21 @@ static int grep_submodule_launch(struct grep_opt *opt,
 
 /*
  * Prep grep structures for a submodule grep
- * sha1: the sha1 of the submodule or NULL if using the working tree
+ * oid: the oid of the submodule or NULL if using the working tree
  * filename: name of the submodule including tree name of parent
  * path: location of the submodule
  */
-static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
+static int grep_submodule(struct grep_opt *opt, const struct object_id *oid,
                          const char *filename, const char *path)
 {
-       if (!is_submodule_initialized(path))
+       if (!is_submodule_active(the_repository, path))
                return 0;
        if (!is_submodule_populated_gently(path, NULL)) {
                /*
-                * If searching history, check for the presense of the
+                * If searching history, check for the presence of the
                 * submodule's gitdir before skipping the submodule.
                 */
-               if (sha1) {
+               if (oid) {
                        const struct submodule *sub =
                                        submodule_from_path(null_sha1, path);
                        if (sub)
@@ -647,7 +666,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
 
 #ifndef NO_PTHREADS
        if (num_threads) {
-               add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, sha1);
+               add_work(opt, GREP_SOURCE_SUBMODULE, filename, path, oid);
                return 0;
        } else
 #endif
@@ -656,7 +675,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
                int hit;
 
                grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
-                                filename, path, sha1);
+                                filename, path, oid);
                hit = grep_submodule_launch(opt, &gs);
 
                grep_source_clear(&gs);
@@ -775,7 +794,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
                                         check_attr);
                        free(data);
                } else if (recurse_submodules && S_ISGITLINK(entry.mode)) {
-                       hit |= grep_submodule(opt, entry.oid->hash, base->buf,
+                       hit |= grep_submodule(opt, entry.oid, base->buf,
                                              base->buf + tn_len);
                }
 
@@ -866,7 +885,7 @@ static int grep_directory(struct grep_opt *opt, const struct pathspec *pathspec,
        if (exc_std)
                setup_standard_excludes(&dir);
 
-       fill_directory(&dir, pathspec);
+       fill_directory(&dir, &the_index, pathspec);
        for (i = 0; i < dir.nr; i++) {
                if (!dir_path_match(dir.entries[i], pathspec, 0, NULL))
                        continue;
@@ -1151,10 +1170,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 
        if (!opt.pattern_list)
                die(_("no pattern given."));
-       if (!opt.fixed && opt.ignore_case)
-               opt.regflags |= REG_ICASE;
-
-       compile_grep_patterns(&opt);
 
        /*
         * We have to find "--" in a separate pass, because its presence
@@ -1197,7 +1212,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                        break;
                }
 
-               object = parse_object_or_die(oid.hash, arg);
+               object = parse_object_or_die(&oid, arg);
                if (!seen_dashdash)
                        verify_non_filename(prefix, arg);
                add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
@@ -1228,10 +1243,23 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                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)
+               warning(_("no threads support, ignoring --threads"));
        num_threads = 0;
 #endif
 
+       if (!num_threads)
+               /*
+                * The compiled patterns on the main path are only
+                * used when not using threading. Otherwise
+                * start_threads() below 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)