3 #include "parse-options.h"
9 #include "submodule-config.h"
10 #include "string-list.h"
11 #include "run-command.h"
14 const struct cache_entry **entries;
17 #define MODULE_LIST_INIT { NULL, 0, 0 }
19 static int module_list_compute(int argc, const char **argv,
21 struct pathspec *pathspec,
22 struct module_list *list)
25 char *ps_matched = NULL;
26 parse_pathspec(pathspec, 0,
27 PATHSPEC_PREFER_FULL |
28 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
32 ps_matched = xcalloc(pathspec->nr, 1);
35 die(_("index file corrupt"));
37 for (i = 0; i < active_nr; i++) {
38 const struct cache_entry *ce = active_cache[i];
40 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
42 !S_ISGITLINK(ce->ce_mode))
45 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
46 list->entries[list->nr++] = ce;
47 while (i + 1 < active_nr &&
48 !strcmp(ce->name, active_cache[i + 1]->name))
50 * Skip entries with the same name in different stages
51 * to make sure an entry is returned only once.
56 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
64 static int module_list(int argc, const char **argv, const char *prefix)
67 struct pathspec pathspec;
68 struct module_list list = MODULE_LIST_INIT;
70 struct option module_list_options[] = {
71 OPT_STRING(0, "prefix", &prefix,
73 N_("alternative anchor for relative paths")),
77 const char *const git_submodule_helper_usage[] = {
78 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
82 argc = parse_options(argc, argv, prefix, module_list_options,
83 git_submodule_helper_usage, 0);
85 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) {
86 printf("#unmatched\n");
90 for (i = 0; i < list.nr; i++) {
91 const struct cache_entry *ce = list.entries[i];
94 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
96 printf("%06o %s %d\t", ce->ce_mode, sha1_to_hex(ce->sha1), ce_stage(ce));
98 utf8_fprintf(stdout, "%s\n", ce->name);
103 static int module_name(int argc, const char **argv, const char *prefix)
105 const struct submodule *sub;
108 usage(_("git submodule--helper name <path>"));
111 sub = submodule_from_path(null_sha1, argv[1]);
114 die(_("no submodule mapping found in .gitmodules for path '%s'"),
117 printf("%s\n", sub->name);
121 static int clone_submodule(const char *path, const char *gitdir, const char *url,
122 const char *depth, const char *reference, int quiet)
124 struct child_process cp;
125 child_process_init(&cp);
127 argv_array_push(&cp.args, "clone");
128 argv_array_push(&cp.args, "--no-checkout");
130 argv_array_push(&cp.args, "--quiet");
132 argv_array_pushl(&cp.args, "--depth", depth, NULL);
133 if (reference && *reference)
134 argv_array_pushl(&cp.args, "--reference", reference, NULL);
135 if (gitdir && *gitdir)
136 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
138 argv_array_push(&cp.args, url);
139 argv_array_push(&cp.args, path);
142 cp.env = local_repo_env;
145 return run_command(&cp);
148 static int module_clone(int argc, const char **argv, const char *prefix)
150 const char *path = NULL, *name = NULL, *url = NULL;
151 const char *reference = NULL, *depth = NULL;
153 FILE *submodule_dot_git;
154 char *sm_gitdir, *cwd, *p;
155 struct strbuf rel_path = STRBUF_INIT;
156 struct strbuf sb = STRBUF_INIT;
158 struct option module_clone_options[] = {
159 OPT_STRING(0, "prefix", &prefix,
161 N_("alternative anchor for relative paths")),
162 OPT_STRING(0, "path", &path,
164 N_("where the new submodule will be cloned to")),
165 OPT_STRING(0, "name", &name,
167 N_("name of the new submodule")),
168 OPT_STRING(0, "url", &url,
170 N_("url where to clone the submodule from")),
171 OPT_STRING(0, "reference", &reference,
173 N_("reference repository")),
174 OPT_STRING(0, "depth", &depth,
176 N_("depth for shallow clones")),
177 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
181 const char *const git_submodule_helper_usage[] = {
182 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
183 "[--reference <repository>] [--name <name>] [--url <url>]"
184 "[--depth <depth>] [--] [<path>...]"),
188 argc = parse_options(argc, argv, prefix, module_clone_options,
189 git_submodule_helper_usage, 0);
191 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
192 sm_gitdir = strbuf_detach(&sb, NULL);
194 if (!file_exists(sm_gitdir)) {
195 if (safe_create_leading_directories_const(sm_gitdir) < 0)
196 die(_("could not create directory '%s'"), sm_gitdir);
197 if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
198 die(_("clone of '%s' into submodule path '%s' failed"),
201 if (safe_create_leading_directories_const(path) < 0)
202 die(_("could not create directory '%s'"), path);
203 strbuf_addf(&sb, "%s/index", sm_gitdir);
204 unlink_or_warn(sb.buf);
208 /* Write a .git file in the submodule to redirect to the superproject. */
209 if (safe_create_leading_directories_const(path) < 0)
210 die(_("could not create directory '%s'"), path);
213 strbuf_addf(&sb, "%s/.git", path);
215 strbuf_addstr(&sb, ".git");
217 if (safe_create_leading_directories_const(sb.buf) < 0)
218 die(_("could not create leading directories of '%s'"), sb.buf);
219 submodule_dot_git = fopen(sb.buf, "w");
220 if (!submodule_dot_git)
221 die_errno(_("cannot open file '%s'"), sb.buf);
223 fprintf(submodule_dot_git, "gitdir: %s\n",
224 relative_path(sm_gitdir, path, &rel_path));
225 if (fclose(submodule_dot_git))
226 die(_("could not close file %s"), sb.buf);
228 strbuf_reset(&rel_path);
231 /* Redirect the worktree of the submodule in the superproject's config */
232 if (!is_absolute_path(sm_gitdir)) {
233 strbuf_addf(&sb, "%s/%s", cwd, sm_gitdir);
235 sm_gitdir = strbuf_detach(&sb, NULL);
238 strbuf_addf(&sb, "%s/%s", cwd, path);
239 p = git_pathdup_submodule(path, "config");
241 die(_("could not get submodule directory for '%s'"), path);
242 git_config_set_in_file(p, "core.worktree",
243 relative_path(sb.buf, sm_gitdir, &rel_path));
245 strbuf_release(&rel_path);
252 struct submodule_update_clone {
253 /* index into 'list', the list of submodules to look into for cloning */
255 struct module_list list;
256 unsigned warn_if_uninitialized : 1;
258 /* update parameter passed via commandline */
259 struct submodule_update_strategy update;
261 /* configuration parameters which are passed on to the children */
263 const char *reference;
265 const char *recursive_prefix;
268 /* Machine-readable status lines to be consumed by git-submodule.sh */
269 struct string_list projectlines;
271 /* If we want to stop as fast as possible and return an error */
272 unsigned quickstop : 1;
274 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
275 SUBMODULE_UPDATE_STRATEGY_INIT, 0, NULL, NULL, NULL, NULL, \
276 STRING_LIST_INIT_DUP, 0}
279 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
280 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
282 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
283 struct child_process *child,
284 struct submodule_update_clone *suc,
287 const struct submodule *sub = NULL;
288 struct strbuf displaypath_sb = STRBUF_INIT;
289 struct strbuf sb = STRBUF_INIT;
290 const char *displaypath = NULL;
292 int needs_cloning = 0;
295 if (suc->recursive_prefix)
296 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
298 strbuf_addf(&sb, "%s", ce->name);
299 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
300 strbuf_addch(out, '\n');
304 sub = submodule_from_path(null_sha1, ce->name);
306 if (suc->recursive_prefix)
307 displaypath = relative_path(suc->recursive_prefix,
308 ce->name, &displaypath_sb);
310 displaypath = ce->name;
312 if (suc->update.type == SM_UPDATE_NONE
313 || (suc->update.type == SM_UPDATE_UNSPECIFIED
314 && sub->update_strategy.type == SM_UPDATE_NONE)) {
315 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
316 strbuf_addch(out, '\n');
321 * Looking up the url in .git/config.
322 * We must not fall back to .gitmodules as we only want
323 * to process configured submodules.
326 strbuf_addf(&sb, "submodule.%s.url", sub->name);
327 git_config_get_string(sb.buf, &url);
330 * Only mention uninitialized submodules when their
331 * path have been specified
333 if (suc->warn_if_uninitialized) {
335 _("Submodule path '%s' not initialized"),
337 strbuf_addch(out, '\n');
339 _("Maybe you want to use 'update --init'?"));
340 strbuf_addch(out, '\n');
346 strbuf_addf(&sb, "%s/.git", ce->name);
347 needs_cloning = !file_exists(sb.buf);
350 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
351 sha1_to_hex(ce->sha1), ce_stage(ce),
352 needs_cloning, ce->name);
353 string_list_append(&suc->projectlines, sb.buf);
360 child->stdout_to_stderr = 1;
362 argv_array_push(&child->args, "submodule--helper");
363 argv_array_push(&child->args, "clone");
365 argv_array_push(&child->args, "--quiet");
367 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
368 argv_array_pushl(&child->args, "--path", sub->path, NULL);
369 argv_array_pushl(&child->args, "--name", sub->name, NULL);
370 argv_array_pushl(&child->args, "--url", url, NULL);
372 argv_array_push(&child->args, suc->reference);
374 argv_array_push(&child->args, suc->depth);
378 strbuf_reset(&displaypath_sb);
381 return needs_cloning;
384 static int update_clone_get_next_task(struct child_process *child,
389 struct submodule_update_clone *suc = suc_cb;
391 for (; suc->current < suc->list.nr; suc->current++) {
392 const struct cache_entry *ce = suc->list.entries[suc->current];
393 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
401 static int update_clone_start_failure(struct strbuf *err,
405 struct submodule_update_clone *suc = suc_cb;
410 static int update_clone_task_finished(int result,
415 struct submodule_update_clone *suc = suc_cb;
424 static int update_clone(int argc, const char **argv, const char *prefix)
426 const char *update = NULL;
428 struct string_list_item *item;
429 struct pathspec pathspec;
430 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
432 struct option module_update_clone_options[] = {
433 OPT_STRING(0, "prefix", &prefix,
435 N_("path into the working tree")),
436 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
438 N_("path into the working tree, across nested "
439 "submodule boundaries")),
440 OPT_STRING(0, "update", &update,
442 N_("rebase, merge, checkout or none")),
443 OPT_STRING(0, "reference", &suc.reference, N_("repo"),
444 N_("reference repository")),
445 OPT_STRING(0, "depth", &suc.depth, "<depth>",
446 N_("Create a shallow clone truncated to the "
447 "specified number of revisions")),
448 OPT_INTEGER('j', "jobs", &max_jobs,
449 N_("parallel jobs")),
450 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
454 const char *const git_submodule_helper_usage[] = {
455 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
460 argc = parse_options(argc, argv, prefix, module_update_clone_options,
461 git_submodule_helper_usage, 0);
464 if (parse_submodule_update_strategy(update, &suc.update) < 0)
465 die(_("bad value for update parameter"));
467 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
471 suc.warn_if_uninitialized = 1;
473 /* Overlay the parsed .gitmodules file with .git/config */
475 git_config(submodule_config, NULL);
478 max_jobs = parallel_submodules();
480 run_processes_parallel(max_jobs,
481 update_clone_get_next_task,
482 update_clone_start_failure,
483 update_clone_task_finished,
487 * We saved the output and put it out all at once now.
489 * - the listener does not have to interleave their (checkout)
490 * work with our fetching. The writes involved in a
491 * checkout involve more straightforward sequential I/O.
492 * - the listener can avoid doing any work if fetching failed.
497 for_each_string_list_item(item, &suc.projectlines)
498 utf8_fprintf(stdout, "%s", item->string);
505 int (*fn)(int, const char **, const char *);
508 static struct cmd_struct commands[] = {
509 {"list", module_list},
510 {"name", module_name},
511 {"clone", module_clone},
512 {"update-clone", update_clone}
515 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
519 die(_("submodule--helper subcommand must be "
520 "called with a subcommand"));
522 for (i = 0; i < ARRAY_SIZE(commands); i++)
523 if (!strcmp(argv[1], commands[i].cmd))
524 return commands[i].fn(argc - 1, argv + 1, prefix);
526 die(_("'%s' is not a valid submodule--helper "
527 "subcommand"), argv[1]);