{
struct strbuf pathbuf = STRBUF_INIT;
- if (opt->relative && opt->prefix_length) {
- quote_path_relative(filename + tree_name_len, opt->prefix, &pathbuf);
- strbuf_insert(&pathbuf, 0, filename, tree_name_len);
- } else if (super_prefix) {
+ if (super_prefix) {
strbuf_add(&pathbuf, filename, tree_name_len);
strbuf_addstr(&pathbuf, super_prefix);
strbuf_addstr(&pathbuf, filename + tree_name_len);
strbuf_addstr(&pathbuf, filename);
}
+ if (opt->relative && opt->prefix_length) {
+ char *name = strbuf_detach(&pathbuf, NULL);
+ quote_path_relative(name + tree_name_len, opt->prefix, &pathbuf);
+ strbuf_insert(&pathbuf, 0, name, tree_name_len);
+ free(name);
+ }
+
#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
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);
{
struct strbuf buf = STRBUF_INIT;
+ if (super_prefix)
+ strbuf_addstr(&buf, super_prefix);
+ strbuf_addstr(&buf, filename);
+
if (opt->relative && opt->prefix_length) {
- quote_path_relative(filename, opt->prefix, &buf);
- } else {
- if (super_prefix)
- strbuf_addstr(&buf, super_prefix);
- strbuf_addstr(&buf, filename);
+ char *name = strbuf_detach(&buf, NULL);
+ quote_path_relative(name, opt->prefix, &buf);
+ free(name);
}
#ifndef NO_PTHREADS
}
static void compile_submodule_options(const struct grep_opt *opt,
- const struct pathspec *pathspec,
+ const char **argv,
int cached, int untracked,
int opt_exclude, int use_index,
int pattern_type_arg)
{
struct grep_pat *pattern;
- int i;
if (recurse_submodules)
argv_array_push(&submodule_options, "--recurse-submodules");
/* Add Pathspecs */
argv_array_push(&submodule_options, "--");
- for (i = 0; i < pathspec->nr; i++)
- argv_array_push(&submodule_options,
- pathspec->items[i].original);
+ for (; *argv; argv++)
+ argv_array_push(&submodule_options, *argv);
}
/*
int status, i;
const char *end_of_base;
const char *name;
- struct work_item *w = opt->output_priv;
+ struct strbuf child_output = STRBUF_INIT;
end_of_base = strchr(gs->name, ':');
if (gs->identifier && end_of_base)
prepare_submodule_repo_env(&cp.env_array);
argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
+ if (opt->relative && opt->prefix_length)
+ argv_array_pushf(&cp.env_array, "%s=%s",
+ GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
+ opt->prefix);
+
/* Add super prefix */
argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
super_prefix ? super_prefix : "",
* 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",
* 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]);
* child process. A '0' indicates a hit, a '1' indicates no hit and
* anything else is an error.
*/
- status = capture_command(&cp, &w->out, 0);
+ status = capture_command(&cp, &child_output, 0);
if (status && (status != 1)) {
/* flush the buffer */
- write_or_die(1, w->out.buf, w->out.len);
+ write_or_die(1, child_output.buf, child_output.len);
die("process for submodule '%s' failed with exit code: %d",
gs->name, status);
}
+ opt->output(opt, child_output.buf, child_output.len);
+ strbuf_release(&child_output);
/* invert the return code to make a hit equal to 1 */
return !status;
}
/*
* 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))
return 0;
- if (!is_submodule_populated(path)) {
+ if (!is_submodule_populated_gently(path, NULL)) {
/*
* 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)
#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
{
- struct work_item w;
+ struct grep_source gs;
int hit;
- grep_source_init(&w.source, GREP_SOURCE_SUBMODULE,
- filename, path, sha1);
- strbuf_init(&w.out, 0);
- opt->output_priv = &w;
- hit = grep_submodule_launch(opt, &w.source);
-
- write_or_die(1, w.out.buf, w.out.len);
+ grep_source_init(&gs, GREP_SOURCE_SUBMODULE,
+ filename, path, oid);
+ hit = grep_submodule_launch(opt, &gs);
- grep_source_clear(&w.source);
- strbuf_release(&w.out);
+ grep_source_clear(&gs);
return hit;
}
}
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);
}
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;
OPT_SET_INT(0, "exclude-standard", &opt_exclude,
N_("ignore files specified via '.gitignore'"), 1),
OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
- N_("recursivley search in each submodule")),
+ N_("recursively search in each submodule")),
OPT_STRING(0, "parent-basename", &parent_basename,
N_("basename"),
N_("prepend parent project's basename to output")),
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);
if (recurse_submodules) {
gitmodules_config();
- compile_submodule_options(&opt, &pathspec, cached, untracked,
+ compile_submodule_options(&opt, argv + i, cached, untracked,
opt_exclude, use_index,
pattern_type_arg);
}
hit |= wait_all();
if (hit && show_in_pager)
run_pager(&opt, prefix);
+ clear_pathspec(&pathspec);
free_grep_patterns(&opt);
return !hit;
}