summaryrefslogtreecommitdiff
path: root/builtin/grep.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin/grep.c')
-rw-r--r--builtin/grep.c72
1 files changed, 51 insertions, 21 deletions
diff --git a/builtin/grep.c b/builtin/grep.c
index 3ffb5b4..3e4b960 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -73,14 +73,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 +224,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 +290,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 +342,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 +351,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 +510,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;
@@ -570,7 +587,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 +600,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,11 +635,11 @@ 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))
@@ -632,7 +649,7 @@ static int grep_submodule(struct grep_opt *opt, const unsigned char *sha1,
* If searching history, check for the presense 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 +664,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 +673,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 +792,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 +883,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;
@@ -1154,8 +1171,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
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
* influences how we will parse arguments that come before it.
@@ -1190,16 +1205,18 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
break;
}
- if (get_sha1_with_context(arg, 0, oid.hash, &oc)) {
+ if (get_sha1_with_context(arg, GET_SHA1_RECORD_PATH,
+ oid.hash, &oc)) {
if (seen_dashdash)
die(_("unable to resolve revision: %s"), arg);
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);
+ free(oc.path);
}
/*
@@ -1226,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)