t0410: make test description clearer
[git/gitster.git] / builtin / submodule--helper.c
blobb6b5f1ebde7c2e4780af4097a0c4d6d838948aee
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "abspath.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
8 #include "config.h"
9 #include "parse-options.h"
10 #include "quote.h"
11 #include "path.h"
12 #include "pathspec.h"
13 #include "preload-index.h"
14 #include "dir.h"
15 #include "read-cache.h"
16 #include "setup.h"
17 #include "sparse-index.h"
18 #include "submodule.h"
19 #include "submodule-config.h"
20 #include "string-list.h"
21 #include "run-command.h"
22 #include "remote.h"
23 #include "refs.h"
24 #include "refspec.h"
25 #include "revision.h"
26 #include "diffcore.h"
27 #include "diff.h"
28 #include "object-file.h"
29 #include "object-name.h"
30 #include "object-store-ll.h"
31 #include "advice.h"
32 #include "branch.h"
33 #include "list-objects-filter-options.h"
35 #define OPT_QUIET (1 << 0)
36 #define OPT_CACHED (1 << 1)
37 #define OPT_RECURSIVE (1 << 2)
38 #define OPT_FORCE (1 << 3)
40 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
41 void *cb_data);
43 static int repo_get_default_remote(struct repository *repo, char **default_remote)
45 char *dest = NULL;
46 struct strbuf sb = STRBUF_INIT;
47 struct ref_store *store = get_main_ref_store(repo);
48 const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
49 NULL);
51 if (!refname)
52 return die_message(_("No such ref: %s"), "HEAD");
54 /* detached HEAD */
55 if (!strcmp(refname, "HEAD")) {
56 *default_remote = xstrdup("origin");
57 return 0;
60 if (!skip_prefix(refname, "refs/heads/", &refname))
61 return die_message(_("Expecting a full ref name, got %s"),
62 refname);
64 strbuf_addf(&sb, "branch.%s.remote", refname);
65 if (repo_config_get_string(repo, sb.buf, &dest))
66 *default_remote = xstrdup("origin");
67 else
68 *default_remote = dest;
70 strbuf_release(&sb);
71 return 0;
74 static int get_default_remote_submodule(const char *module_path, char **default_remote)
76 struct repository subrepo;
77 int ret;
79 if (repo_submodule_init(&subrepo, the_repository, module_path,
80 null_oid()) < 0)
81 return die_message(_("could not get a repository handle for submodule '%s'"),
82 module_path);
83 ret = repo_get_default_remote(&subrepo, default_remote);
84 repo_clear(&subrepo);
86 return ret;
89 static char *get_default_remote(void)
91 char *default_remote;
92 int code = repo_get_default_remote(the_repository, &default_remote);
94 if (code)
95 exit(code);
97 return default_remote;
100 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
102 char *remoteurl, *resolved_url;
103 char *remote = get_default_remote();
104 struct strbuf remotesb = STRBUF_INIT;
106 strbuf_addf(&remotesb, "remote.%s.url", remote);
107 if (git_config_get_string(remotesb.buf, &remoteurl)) {
108 if (!quiet)
109 warning(_("could not look up configuration '%s'. "
110 "Assuming this repository is its own "
111 "authoritative upstream."),
112 remotesb.buf);
113 remoteurl = xgetcwd();
115 resolved_url = relative_url(remoteurl, rel_url, up_path);
117 free(remote);
118 free(remoteurl);
119 strbuf_release(&remotesb);
121 return resolved_url;
124 /* the result should be freed by the caller. */
125 static char *get_submodule_displaypath(const char *path, const char *prefix,
126 const char *super_prefix)
128 if (prefix && super_prefix) {
129 BUG("cannot have prefix '%s' and superprefix '%s'",
130 prefix, super_prefix);
131 } else if (prefix) {
132 struct strbuf sb = STRBUF_INIT;
133 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
134 strbuf_release(&sb);
135 return displaypath;
136 } else if (super_prefix) {
137 return xstrfmt("%s%s", super_prefix, path);
138 } else {
139 return xstrdup(path);
143 static char *compute_rev_name(const char *sub_path, const char* object_id)
145 struct strbuf sb = STRBUF_INIT;
146 const char ***d;
148 static const char *describe_bare[] = { NULL };
150 static const char *describe_tags[] = { "--tags", NULL };
152 static const char *describe_contains[] = { "--contains", NULL };
154 static const char *describe_all_always[] = { "--all", "--always", NULL };
156 static const char **describe_argv[] = { describe_bare, describe_tags,
157 describe_contains,
158 describe_all_always, NULL };
160 for (d = describe_argv; *d; d++) {
161 struct child_process cp = CHILD_PROCESS_INIT;
162 prepare_submodule_repo_env(&cp.env);
163 cp.dir = sub_path;
164 cp.git_cmd = 1;
165 cp.no_stderr = 1;
167 strvec_push(&cp.args, "describe");
168 strvec_pushv(&cp.args, *d);
169 strvec_push(&cp.args, object_id);
171 if (!capture_command(&cp, &sb, 0)) {
172 strbuf_strip_suffix(&sb, "\n");
173 return strbuf_detach(&sb, NULL);
177 strbuf_release(&sb);
178 return NULL;
181 struct module_list {
182 const struct cache_entry **entries;
183 int alloc, nr;
185 #define MODULE_LIST_INIT { 0 }
187 static void module_list_release(struct module_list *ml)
189 free(ml->entries);
192 static int module_list_compute(const char **argv,
193 const char *prefix,
194 struct pathspec *pathspec,
195 struct module_list *list)
197 int i, result = 0;
198 char *ps_matched = NULL;
200 parse_pathspec(pathspec, 0,
201 PATHSPEC_PREFER_FULL,
202 prefix, argv);
204 if (pathspec->nr)
205 ps_matched = xcalloc(pathspec->nr, 1);
207 if (repo_read_index(the_repository) < 0)
208 die(_("index file corrupt"));
210 for (i = 0; i < the_repository->index->cache_nr; i++) {
211 const struct cache_entry *ce = the_repository->index->cache[i];
213 if (!match_pathspec(the_repository->index, pathspec, ce->name, ce_namelen(ce),
214 0, ps_matched, 1) ||
215 !S_ISGITLINK(ce->ce_mode))
216 continue;
218 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
219 list->entries[list->nr++] = ce;
220 while (i + 1 < the_repository->index->cache_nr &&
221 !strcmp(ce->name, the_repository->index->cache[i + 1]->name))
223 * Skip entries with the same name in different stages
224 * to make sure an entry is returned only once.
226 i++;
229 if (ps_matched && report_path_error(ps_matched, pathspec))
230 result = -1;
232 free(ps_matched);
234 return result;
237 static void module_list_active(struct module_list *list)
239 int i;
240 struct module_list active_modules = MODULE_LIST_INIT;
242 for (i = 0; i < list->nr; i++) {
243 const struct cache_entry *ce = list->entries[i];
245 if (!is_submodule_active(the_repository, ce->name))
246 continue;
248 ALLOC_GROW(active_modules.entries,
249 active_modules.nr + 1,
250 active_modules.alloc);
251 active_modules.entries[active_modules.nr++] = ce;
254 module_list_release(list);
255 *list = active_modules;
258 static char *get_up_path(const char *path)
260 struct strbuf sb = STRBUF_INIT;
262 strbuf_addstrings(&sb, "../", count_slashes(path));
265 * Check if 'path' ends with slash or not
266 * for having the same output for dir/sub_dir
267 * and dir/sub_dir/
269 if (!is_dir_sep(path[strlen(path) - 1]))
270 strbuf_addstr(&sb, "../");
272 return strbuf_detach(&sb, NULL);
275 static void for_each_listed_submodule(const struct module_list *list,
276 each_submodule_fn fn, void *cb_data)
278 int i;
280 for (i = 0; i < list->nr; i++)
281 fn(list->entries[i], cb_data);
284 struct foreach_cb {
285 int argc;
286 const char **argv;
287 const char *prefix;
288 const char *super_prefix;
289 int quiet;
290 int recursive;
292 #define FOREACH_CB_INIT { 0 }
294 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
295 void *cb_data)
297 struct foreach_cb *info = cb_data;
298 const char *path = list_item->name;
299 const struct object_id *ce_oid = &list_item->oid;
300 const struct submodule *sub;
301 struct child_process cp = CHILD_PROCESS_INIT;
302 char *displaypath;
304 if (validate_submodule_path(path) < 0)
305 exit(128);
307 displaypath = get_submodule_displaypath(path, info->prefix,
308 info->super_prefix);
310 sub = submodule_from_path(the_repository, null_oid(), path);
312 if (!sub)
313 die(_("No url found for submodule path '%s' in .gitmodules"),
314 displaypath);
316 if (!is_submodule_populated_gently(path, NULL))
317 goto cleanup;
319 prepare_submodule_repo_env(&cp.env);
322 * For the purpose of executing <command> in the submodule,
323 * separate shell is used for the purpose of running the
324 * child process.
326 cp.use_shell = 1;
327 cp.dir = path;
330 * NEEDSWORK: the command currently has access to the variables $name,
331 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
332 * contains a single argument. This is done for maintaining a faithful
333 * translation from shell script.
335 if (info->argc == 1) {
336 char *toplevel = xgetcwd();
337 struct strbuf sb = STRBUF_INIT;
339 strvec_pushf(&cp.env, "name=%s", sub->name);
340 strvec_pushf(&cp.env, "sm_path=%s", path);
341 strvec_pushf(&cp.env, "displaypath=%s", displaypath);
342 strvec_pushf(&cp.env, "sha1=%s",
343 oid_to_hex(ce_oid));
344 strvec_pushf(&cp.env, "toplevel=%s", toplevel);
347 * Since the path variable was accessible from the script
348 * before porting, it is also made available after porting.
349 * The environment variable "PATH" has a very special purpose
350 * on windows. And since environment variables are
351 * case-insensitive in windows, it interferes with the
352 * existing PATH variable. Hence, to avoid that, we expose
353 * path via the args strvec and not via env.
355 sq_quote_buf(&sb, path);
356 strvec_pushf(&cp.args, "path=%s; %s",
357 sb.buf, info->argv[0]);
358 strbuf_release(&sb);
359 free(toplevel);
360 } else {
361 strvec_pushv(&cp.args, info->argv);
364 if (!info->quiet)
365 printf(_("Entering '%s'\n"), displaypath);
367 if (info->argv[0]) {
368 if (run_command(&cp))
369 die(_("run_command returned non-zero status for %s\n."),
370 displaypath);
371 } else {
372 child_process_clear(&cp);
375 if (info->recursive) {
376 struct child_process cpr = CHILD_PROCESS_INIT;
378 cpr.git_cmd = 1;
379 cpr.dir = path;
380 prepare_submodule_repo_env(&cpr.env);
382 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
383 NULL);
384 strvec_pushf(&cpr.args, "--super-prefix=%s/", displaypath);
386 if (info->quiet)
387 strvec_push(&cpr.args, "--quiet");
389 strvec_push(&cpr.args, "--");
390 strvec_pushv(&cpr.args, info->argv);
392 if (run_command(&cpr))
393 die(_("run_command returned non-zero status while "
394 "recursing in the nested submodules of %s\n."),
395 displaypath);
398 cleanup:
399 free(displaypath);
402 static int module_foreach(int argc, const char **argv, const char *prefix)
404 struct foreach_cb info = FOREACH_CB_INIT;
405 struct pathspec pathspec = { 0 };
406 struct module_list list = MODULE_LIST_INIT;
407 struct option module_foreach_options[] = {
408 OPT__SUPER_PREFIX(&info.super_prefix),
409 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
410 OPT_BOOL(0, "recursive", &info.recursive,
411 N_("recurse into nested submodules")),
412 OPT_END()
414 const char *const git_submodule_helper_usage[] = {
415 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
416 NULL
418 int ret = 1;
420 argc = parse_options(argc, argv, prefix, module_foreach_options,
421 git_submodule_helper_usage, 0);
423 if (module_list_compute(NULL, prefix, &pathspec, &list) < 0)
424 goto cleanup;
426 info.argc = argc;
427 info.argv = argv;
428 info.prefix = prefix;
430 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
432 ret = 0;
433 cleanup:
434 module_list_release(&list);
435 clear_pathspec(&pathspec);
436 return ret;
439 static int starts_with_dot_slash(const char *const path)
441 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
442 PATH_MATCH_XPLATFORM);
445 static int starts_with_dot_dot_slash(const char *const path)
447 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
448 PATH_MATCH_XPLATFORM);
451 struct init_cb {
452 const char *prefix;
453 const char *super_prefix;
454 unsigned int flags;
456 #define INIT_CB_INIT { 0 }
458 static void init_submodule(const char *path, const char *prefix,
459 const char *super_prefix,
460 unsigned int flags)
462 const struct submodule *sub;
463 struct strbuf sb = STRBUF_INIT;
464 const char *upd;
465 char *url = NULL, *displaypath;
467 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
469 sub = submodule_from_path(the_repository, null_oid(), path);
471 if (!sub)
472 die(_("No url found for submodule path '%s' in .gitmodules"),
473 displaypath);
476 * NEEDSWORK: In a multi-working-tree world, this needs to be
477 * set in the per-worktree config.
479 * Set active flag for the submodule being initialized
481 if (!is_submodule_active(the_repository, path)) {
482 strbuf_addf(&sb, "submodule.%s.active", sub->name);
483 git_config_set_gently(sb.buf, "true");
484 strbuf_reset(&sb);
488 * Copy url setting when it is not set yet.
489 * To look up the url in .git/config, we must not fall back to
490 * .gitmodules, so look it up directly.
492 strbuf_addf(&sb, "submodule.%s.url", sub->name);
493 if (git_config_get_string(sb.buf, &url)) {
494 if (!sub->url)
495 die(_("No url found for submodule path '%s' in .gitmodules"),
496 displaypath);
498 url = xstrdup(sub->url);
500 /* Possibly a url relative to parent */
501 if (starts_with_dot_dot_slash(url) ||
502 starts_with_dot_slash(url)) {
503 char *oldurl = url;
505 url = resolve_relative_url(oldurl, NULL, 0);
506 free(oldurl);
509 if (git_config_set_gently(sb.buf, url))
510 die(_("Failed to register url for submodule path '%s'"),
511 displaypath);
512 if (!(flags & OPT_QUIET))
513 fprintf(stderr,
514 _("Submodule '%s' (%s) registered for path '%s'\n"),
515 sub->name, url, displaypath);
517 strbuf_reset(&sb);
519 /* Copy "update" setting when it is not set yet */
520 strbuf_addf(&sb, "submodule.%s.update", sub->name);
521 if (git_config_get_string_tmp(sb.buf, &upd) &&
522 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
523 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
524 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
525 sub->name);
526 upd = "none";
527 } else {
528 upd = submodule_update_type_to_string(sub->update_strategy.type);
531 if (git_config_set_gently(sb.buf, upd))
532 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
534 strbuf_release(&sb);
535 free(displaypath);
536 free(url);
539 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
541 struct init_cb *info = cb_data;
543 init_submodule(list_item->name, info->prefix, info->super_prefix,
544 info->flags);
547 static int module_init(int argc, const char **argv, const char *prefix)
549 struct init_cb info = INIT_CB_INIT;
550 struct pathspec pathspec = { 0 };
551 struct module_list list = MODULE_LIST_INIT;
552 int quiet = 0;
553 struct option module_init_options[] = {
554 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
555 OPT_END()
557 const char *const git_submodule_helper_usage[] = {
558 N_("git submodule init [<options>] [<path>]"),
559 NULL
561 int ret = 1;
563 argc = parse_options(argc, argv, prefix, module_init_options,
564 git_submodule_helper_usage, 0);
566 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
567 goto cleanup;
570 * If there are no path args and submodule.active is set then,
571 * by default, only initialize 'active' modules.
573 if (!argc && !git_config_get("submodule.active"))
574 module_list_active(&list);
576 info.prefix = prefix;
577 if (quiet)
578 info.flags |= OPT_QUIET;
580 for_each_listed_submodule(&list, init_submodule_cb, &info);
582 ret = 0;
583 cleanup:
584 module_list_release(&list);
585 clear_pathspec(&pathspec);
586 return ret;
589 struct status_cb {
590 const char *prefix;
591 const char *super_prefix;
592 unsigned int flags;
594 #define STATUS_CB_INIT { 0 }
596 static void print_status(unsigned int flags, char state, const char *path,
597 const struct object_id *oid, const char *displaypath)
599 if (flags & OPT_QUIET)
600 return;
602 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
604 if (state == ' ' || state == '+') {
605 char *name = compute_rev_name(path, oid_to_hex(oid));
607 if (name)
608 printf(" (%s)", name);
609 free(name);
612 printf("\n");
615 static int handle_submodule_head_ref(const char *refname UNUSED,
616 const char *referent UNUSED,
617 const struct object_id *oid,
618 int flags UNUSED,
619 void *cb_data)
621 struct object_id *output = cb_data;
623 if (oid)
624 oidcpy(output, oid);
626 return 0;
629 static void status_submodule(const char *path, const struct object_id *ce_oid,
630 unsigned int ce_flags, const char *prefix,
631 const char *super_prefix, unsigned int flags)
633 char *displaypath;
634 struct strvec diff_files_args = STRVEC_INIT;
635 struct rev_info rev = REV_INFO_INIT;
636 struct strbuf buf = STRBUF_INIT;
637 const char *git_dir;
638 struct setup_revision_opt opt = {
639 .free_removed_argv_elements = 1,
642 if (validate_submodule_path(path) < 0)
643 exit(128);
645 if (!submodule_from_path(the_repository, null_oid(), path))
646 die(_("no submodule mapping found in .gitmodules for path '%s'"),
647 path);
649 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
651 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
652 print_status(flags, 'U', path, null_oid(), displaypath);
653 goto cleanup;
656 strbuf_addf(&buf, "%s/.git", path);
657 git_dir = read_gitfile(buf.buf);
658 if (!git_dir)
659 git_dir = buf.buf;
661 if (!is_submodule_active(the_repository, path) ||
662 !is_git_directory(git_dir)) {
663 print_status(flags, '-', path, ce_oid, displaypath);
664 strbuf_release(&buf);
665 goto cleanup;
667 strbuf_release(&buf);
669 strvec_pushl(&diff_files_args, "diff-files",
670 "--ignore-submodules=dirty", "--quiet", "--",
671 path, NULL);
673 git_config(git_diff_basic_config, NULL);
675 repo_init_revisions(the_repository, &rev, NULL);
676 rev.abbrev = 0;
677 setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt);
678 run_diff_files(&rev, 0);
680 if (!diff_result_code(&rev)) {
681 print_status(flags, ' ', path, ce_oid,
682 displaypath);
683 } else if (!(flags & OPT_CACHED)) {
684 struct object_id oid;
685 struct ref_store *refs = repo_get_submodule_ref_store(the_repository,
686 path);
688 if (!refs) {
689 print_status(flags, '-', path, ce_oid, displaypath);
690 goto cleanup;
692 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
693 die(_("could not resolve HEAD ref inside the "
694 "submodule '%s'"), path);
696 print_status(flags, '+', path, &oid, displaypath);
697 } else {
698 print_status(flags, '+', path, ce_oid, displaypath);
701 if (flags & OPT_RECURSIVE) {
702 struct child_process cpr = CHILD_PROCESS_INIT;
703 int res;
705 cpr.git_cmd = 1;
706 cpr.dir = path;
707 prepare_submodule_repo_env(&cpr.env);
709 strvec_pushl(&cpr.args, "submodule--helper", "status",
710 "--recursive", NULL);
711 strvec_pushf(&cpr.args, "--super-prefix=%s/", displaypath);
713 if (flags & OPT_CACHED)
714 strvec_push(&cpr.args, "--cached");
716 if (flags & OPT_QUIET)
717 strvec_push(&cpr.args, "--quiet");
719 res = run_command(&cpr);
720 if (res == SIGPIPE + 128)
721 raise(SIGPIPE);
722 else if (res)
723 die(_("failed to recurse into submodule '%s'"), path);
726 cleanup:
727 strvec_clear(&diff_files_args);
728 free(displaypath);
729 release_revisions(&rev);
732 static void status_submodule_cb(const struct cache_entry *list_item,
733 void *cb_data)
735 struct status_cb *info = cb_data;
737 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
738 info->prefix, info->super_prefix, info->flags);
741 static int module_status(int argc, const char **argv, const char *prefix)
743 struct status_cb info = STATUS_CB_INIT;
744 struct pathspec pathspec = { 0 };
745 struct module_list list = MODULE_LIST_INIT;
746 int quiet = 0;
747 struct option module_status_options[] = {
748 OPT__SUPER_PREFIX(&info.super_prefix),
749 OPT__QUIET(&quiet, N_("suppress submodule status output")),
750 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
751 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
752 OPT_END()
754 const char *const git_submodule_helper_usage[] = {
755 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
756 NULL
758 int ret = 1;
760 argc = parse_options(argc, argv, prefix, module_status_options,
761 git_submodule_helper_usage, 0);
763 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
764 goto cleanup;
766 info.prefix = prefix;
767 if (quiet)
768 info.flags |= OPT_QUIET;
770 for_each_listed_submodule(&list, status_submodule_cb, &info);
772 ret = 0;
773 cleanup:
774 module_list_release(&list);
775 clear_pathspec(&pathspec);
776 return ret;
779 struct module_cb {
780 unsigned int mod_src;
781 unsigned int mod_dst;
782 struct object_id oid_src;
783 struct object_id oid_dst;
784 char status;
785 char *sm_path;
787 #define MODULE_CB_INIT { 0 }
789 static void module_cb_release(struct module_cb *mcb)
791 free(mcb->sm_path);
794 struct module_cb_list {
795 struct module_cb **entries;
796 int alloc, nr;
798 #define MODULE_CB_LIST_INIT { 0 }
800 static void module_cb_list_release(struct module_cb_list *mcbl)
802 int i;
804 for (i = 0; i < mcbl->nr; i++) {
805 struct module_cb *mcb = mcbl->entries[i];
807 module_cb_release(mcb);
808 free(mcb);
810 free(mcbl->entries);
813 struct summary_cb {
814 int argc;
815 const char **argv;
816 const char *prefix;
817 const char *super_prefix;
818 unsigned int cached: 1;
819 unsigned int for_status: 1;
820 unsigned int files: 1;
821 int summary_limit;
823 #define SUMMARY_CB_INIT { 0 }
825 enum diff_cmd {
826 DIFF_INDEX,
827 DIFF_FILES
830 static char *verify_submodule_committish(const char *sm_path,
831 const char *committish)
833 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
834 struct strbuf result = STRBUF_INIT;
836 cp_rev_parse.git_cmd = 1;
837 cp_rev_parse.dir = sm_path;
838 prepare_submodule_repo_env(&cp_rev_parse.env);
839 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
840 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
841 strvec_push(&cp_rev_parse.args, "--");
843 if (capture_command(&cp_rev_parse, &result, 0))
844 return NULL;
846 strbuf_trim_trailing_newline(&result);
847 return strbuf_detach(&result, NULL);
850 static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
851 int total_commits, const char *displaypath,
852 const char *src_abbrev, const char *dst_abbrev,
853 struct module_cb *p)
855 if (p->status == 'T') {
856 if (S_ISGITLINK(p->mod_dst))
857 printf(_("* %s %s(blob)->%s(submodule)"),
858 displaypath, src_abbrev, dst_abbrev);
859 else
860 printf(_("* %s %s(submodule)->%s(blob)"),
861 displaypath, src_abbrev, dst_abbrev);
862 } else {
863 printf("* %s %s...%s",
864 displaypath, src_abbrev, dst_abbrev);
867 if (total_commits < 0)
868 printf(":\n");
869 else
870 printf(" (%d):\n", total_commits);
872 if (errmsg) {
873 printf(_("%s"), errmsg);
874 } else if (total_commits > 0) {
875 struct child_process cp_log = CHILD_PROCESS_INIT;
877 cp_log.git_cmd = 1;
878 cp_log.dir = p->sm_path;
879 prepare_submodule_repo_env(&cp_log.env);
880 strvec_pushl(&cp_log.args, "log", NULL);
882 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
883 if (info->summary_limit > 0)
884 strvec_pushf(&cp_log.args, "-%d",
885 info->summary_limit);
887 strvec_pushl(&cp_log.args, "--pretty= %m %s",
888 "--first-parent", NULL);
889 strvec_pushf(&cp_log.args, "%s...%s",
890 src_abbrev, dst_abbrev);
891 } else if (S_ISGITLINK(p->mod_dst)) {
892 strvec_pushl(&cp_log.args, "--pretty= > %s",
893 "-1", dst_abbrev, NULL);
894 } else {
895 strvec_pushl(&cp_log.args, "--pretty= < %s",
896 "-1", src_abbrev, NULL);
898 run_command(&cp_log);
900 printf("\n");
903 static void generate_submodule_summary(struct summary_cb *info,
904 struct module_cb *p)
906 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
907 int missing_src = 0, missing_dst = 0;
908 struct strbuf errmsg = STRBUF_INIT;
909 int total_commits = -1;
911 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
912 if (S_ISGITLINK(p->mod_dst)) {
913 struct ref_store *refs = repo_get_submodule_ref_store(the_repository,
914 p->sm_path);
916 if (refs)
917 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
918 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
919 struct stat st;
920 int fd = open(p->sm_path, O_RDONLY);
922 if (fd < 0 || fstat(fd, &st) < 0 ||
923 index_fd(the_repository->index, &p->oid_dst, fd, &st, OBJ_BLOB,
924 p->sm_path, 0))
925 error(_("couldn't hash object from '%s'"), p->sm_path);
926 } else {
927 /* for a submodule removal (mode:0000000), don't warn */
928 if (p->mod_dst)
929 warning(_("unexpected mode %o"), p->mod_dst);
933 if (S_ISGITLINK(p->mod_src)) {
934 if (p->status != 'D')
935 src_abbrev = verify_submodule_committish(p->sm_path,
936 oid_to_hex(&p->oid_src));
937 if (!src_abbrev) {
938 missing_src = 1;
940 * As `rev-parse` failed, we fallback to getting
941 * the abbreviated hash using oid_src. We do
942 * this as we might still need the abbreviated
943 * hash in cases like a submodule type change, etc.
945 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
947 } else {
949 * The source does not point to a submodule.
950 * So, we fallback to getting the abbreviation using
951 * oid_src as we might still need the abbreviated
952 * hash in cases like submodule add, etc.
954 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
957 if (S_ISGITLINK(p->mod_dst)) {
958 dst_abbrev = verify_submodule_committish(p->sm_path,
959 oid_to_hex(&p->oid_dst));
960 if (!dst_abbrev) {
961 missing_dst = 1;
963 * As `rev-parse` failed, we fallback to getting
964 * the abbreviated hash using oid_dst. We do
965 * this as we might still need the abbreviated
966 * hash in cases like a submodule type change, etc.
968 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
970 } else {
972 * The destination does not point to a submodule.
973 * So, we fallback to getting the abbreviation using
974 * oid_dst as we might still need the abbreviated
975 * hash in cases like a submodule removal, etc.
977 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
980 displaypath = get_submodule_displaypath(p->sm_path, info->prefix,
981 info->super_prefix);
983 if (!missing_src && !missing_dst) {
984 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
985 struct strbuf sb_rev_list = STRBUF_INIT;
987 strvec_pushl(&cp_rev_list.args, "rev-list",
988 "--first-parent", "--count", NULL);
989 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
990 strvec_pushf(&cp_rev_list.args, "%s...%s",
991 src_abbrev, dst_abbrev);
992 else
993 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
994 src_abbrev : dst_abbrev);
995 strvec_push(&cp_rev_list.args, "--");
997 cp_rev_list.git_cmd = 1;
998 cp_rev_list.dir = p->sm_path;
999 prepare_submodule_repo_env(&cp_rev_list.env);
1001 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
1002 total_commits = atoi(sb_rev_list.buf);
1004 strbuf_release(&sb_rev_list);
1005 } else {
1007 * Don't give error msg for modification whose dst is not
1008 * submodule, i.e., deleted or changed to blob
1010 if (S_ISGITLINK(p->mod_dst)) {
1011 if (missing_src && missing_dst) {
1012 strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n",
1013 displaypath, oid_to_hex(&p->oid_src),
1014 oid_to_hex(&p->oid_dst));
1015 } else {
1016 strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n",
1017 displaypath, missing_src ?
1018 oid_to_hex(&p->oid_src) :
1019 oid_to_hex(&p->oid_dst));
1024 print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
1025 total_commits, displaypath, src_abbrev,
1026 dst_abbrev, p);
1028 free(displaypath);
1029 free(src_abbrev);
1030 free(dst_abbrev);
1031 strbuf_release(&errmsg);
1034 static void prepare_submodule_summary(struct summary_cb *info,
1035 struct module_cb_list *list)
1037 int i;
1038 for (i = 0; i < list->nr; i++) {
1039 const struct submodule *sub;
1040 struct module_cb *p = list->entries[i];
1041 struct strbuf sm_gitdir = STRBUF_INIT;
1043 if (p->status == 'D' || p->status == 'T') {
1044 generate_submodule_summary(info, p);
1045 continue;
1048 if (info->for_status && p->status != 'A' &&
1049 (sub = submodule_from_path(the_repository,
1050 null_oid(), p->sm_path))) {
1051 char *config_key = NULL;
1052 const char *value;
1053 int ignore_all = 0;
1055 config_key = xstrfmt("submodule.%s.ignore",
1056 sub->name);
1057 if (!git_config_get_string_tmp(config_key, &value))
1058 ignore_all = !strcmp(value, "all");
1059 else if (sub->ignore)
1060 ignore_all = !strcmp(sub->ignore, "all");
1062 free(config_key);
1063 if (ignore_all)
1064 continue;
1067 /* Also show added or modified modules which are checked out */
1068 strbuf_addstr(&sm_gitdir, p->sm_path);
1069 if (is_nonbare_repository_dir(&sm_gitdir))
1070 generate_submodule_summary(info, p);
1071 strbuf_release(&sm_gitdir);
1075 static void submodule_summary_callback(struct diff_queue_struct *q,
1076 struct diff_options *options UNUSED,
1077 void *data)
1079 int i;
1080 struct module_cb_list *list = data;
1081 for (i = 0; i < q->nr; i++) {
1082 struct diff_filepair *p = q->queue[i];
1083 struct module_cb *temp;
1085 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1086 continue;
1087 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1088 temp->mod_src = p->one->mode;
1089 temp->mod_dst = p->two->mode;
1090 temp->oid_src = p->one->oid;
1091 temp->oid_dst = p->two->oid;
1092 temp->status = p->status;
1093 temp->sm_path = xstrdup(p->one->path);
1095 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1096 list->entries[list->nr++] = temp;
1100 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1102 switch (diff_cmd) {
1103 case DIFF_INDEX: return "diff-index";
1104 case DIFF_FILES: return "diff-files";
1105 default: BUG("bad diff_cmd value %d", diff_cmd);
1109 static int compute_summary_module_list(struct object_id *head_oid,
1110 struct summary_cb *info,
1111 enum diff_cmd diff_cmd)
1113 struct strvec diff_args = STRVEC_INIT;
1114 struct rev_info rev;
1115 struct setup_revision_opt opt = {
1116 .free_removed_argv_elements = 1,
1118 struct module_cb_list list = MODULE_CB_LIST_INIT;
1119 int ret = 0;
1121 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1122 if (info->cached)
1123 strvec_push(&diff_args, "--cached");
1124 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1125 if (head_oid)
1126 strvec_push(&diff_args, oid_to_hex(head_oid));
1127 strvec_push(&diff_args, "--");
1128 if (info->argc)
1129 strvec_pushv(&diff_args, info->argv);
1131 git_config(git_diff_basic_config, NULL);
1132 repo_init_revisions(the_repository, &rev, info->prefix);
1133 rev.abbrev = 0;
1134 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1135 setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
1136 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1137 rev.diffopt.format_callback = submodule_summary_callback;
1138 rev.diffopt.format_callback_data = &list;
1140 if (!info->cached) {
1141 if (diff_cmd == DIFF_INDEX)
1142 setup_work_tree();
1143 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
1144 perror("repo_read_index_preload");
1145 ret = -1;
1146 goto cleanup;
1148 } else if (repo_read_index(the_repository) < 0) {
1149 perror("repo_read_cache");
1150 ret = -1;
1151 goto cleanup;
1154 if (diff_cmd == DIFF_INDEX)
1155 run_diff_index(&rev, info->cached ? DIFF_INDEX_CACHED : 0);
1156 else
1157 run_diff_files(&rev, 0);
1158 prepare_submodule_summary(info, &list);
1159 cleanup:
1160 strvec_clear(&diff_args);
1161 release_revisions(&rev);
1162 module_cb_list_release(&list);
1163 return ret;
1166 static int module_summary(int argc, const char **argv, const char *prefix)
1168 struct summary_cb info = SUMMARY_CB_INIT;
1169 int cached = 0;
1170 int for_status = 0;
1171 int files = 0;
1172 int summary_limit = -1;
1173 enum diff_cmd diff_cmd = DIFF_INDEX;
1174 struct object_id head_oid;
1175 int ret;
1176 struct option module_summary_options[] = {
1177 OPT_BOOL(0, "cached", &cached,
1178 N_("use the commit stored in the index instead of the submodule HEAD")),
1179 OPT_BOOL(0, "files", &files,
1180 N_("compare the commit in the index with that in the submodule HEAD")),
1181 OPT_BOOL(0, "for-status", &for_status,
1182 N_("skip submodules with 'ignore_config' value set to 'all'")),
1183 OPT_INTEGER('n', "summary-limit", &summary_limit,
1184 N_("limit the summary size")),
1185 OPT_END()
1187 const char *const git_submodule_helper_usage[] = {
1188 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1189 NULL
1192 argc = parse_options(argc, argv, prefix, module_summary_options,
1193 git_submodule_helper_usage, 0);
1195 if (!summary_limit)
1196 return 0;
1198 if (!repo_get_oid(the_repository, argc ? argv[0] : "HEAD", &head_oid)) {
1199 if (argc) {
1200 argv++;
1201 argc--;
1203 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1204 /* before the first commit: compare with an empty tree */
1205 oidcpy(&head_oid, the_hash_algo->empty_tree);
1206 if (argc) {
1207 argv++;
1208 argc--;
1210 } else {
1211 if (repo_get_oid(the_repository, "HEAD", &head_oid))
1212 die(_("could not fetch a revision for HEAD"));
1215 if (files) {
1216 if (cached)
1217 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1218 diff_cmd = DIFF_FILES;
1221 info.argc = argc;
1222 info.argv = argv;
1223 info.prefix = prefix;
1224 info.cached = !!cached;
1225 info.files = !!files;
1226 info.for_status = !!for_status;
1227 info.summary_limit = summary_limit;
1229 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1230 &info, diff_cmd);
1231 return ret;
1234 struct sync_cb {
1235 const char *prefix;
1236 const char *super_prefix;
1237 unsigned int flags;
1239 #define SYNC_CB_INIT { 0 }
1241 static void sync_submodule(const char *path, const char *prefix,
1242 const char *super_prefix, unsigned int flags)
1244 const struct submodule *sub;
1245 char *remote_key = NULL;
1246 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1247 struct strbuf sb = STRBUF_INIT;
1248 char *sub_config_path = NULL;
1249 int code;
1251 if (!is_submodule_active(the_repository, path))
1252 return;
1254 if (validate_submodule_path(path) < 0)
1255 exit(128);
1257 sub = submodule_from_path(the_repository, null_oid(), path);
1259 if (sub && sub->url) {
1260 if (starts_with_dot_dot_slash(sub->url) ||
1261 starts_with_dot_slash(sub->url)) {
1262 char *up_path = get_up_path(path);
1264 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1265 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1266 free(up_path);
1267 } else {
1268 sub_origin_url = xstrdup(sub->url);
1269 super_config_url = xstrdup(sub->url);
1271 } else {
1272 sub_origin_url = xstrdup("");
1273 super_config_url = xstrdup("");
1276 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
1278 if (!(flags & OPT_QUIET))
1279 printf(_("Synchronizing submodule url for '%s'\n"),
1280 displaypath);
1282 strbuf_reset(&sb);
1283 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1284 if (git_config_set_gently(sb.buf, super_config_url))
1285 die(_("failed to register url for submodule path '%s'"),
1286 displaypath);
1288 if (!is_submodule_populated_gently(path, NULL))
1289 goto cleanup;
1291 strbuf_reset(&sb);
1292 code = get_default_remote_submodule(path, &default_remote);
1293 if (code)
1294 exit(code);
1296 remote_key = xstrfmt("remote.%s.url", default_remote);
1297 free(default_remote);
1299 submodule_to_gitdir(&sb, path);
1300 strbuf_addstr(&sb, "/config");
1302 if (git_config_set_in_file_gently(sb.buf, remote_key, NULL, sub_origin_url))
1303 die(_("failed to update remote for submodule '%s'"),
1304 path);
1306 if (flags & OPT_RECURSIVE) {
1307 struct child_process cpr = CHILD_PROCESS_INIT;
1309 cpr.git_cmd = 1;
1310 cpr.dir = path;
1311 prepare_submodule_repo_env(&cpr.env);
1313 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1314 "--recursive", NULL);
1315 strvec_pushf(&cpr.args, "--super-prefix=%s/", displaypath);
1317 if (flags & OPT_QUIET)
1318 strvec_push(&cpr.args, "--quiet");
1320 if (run_command(&cpr))
1321 die(_("failed to recurse into submodule '%s'"),
1322 path);
1325 cleanup:
1326 free(super_config_url);
1327 free(sub_origin_url);
1328 strbuf_release(&sb);
1329 free(remote_key);
1330 free(displaypath);
1331 free(sub_config_path);
1334 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1336 struct sync_cb *info = cb_data;
1338 sync_submodule(list_item->name, info->prefix, info->super_prefix,
1339 info->flags);
1342 static int module_sync(int argc, const char **argv, const char *prefix)
1344 struct sync_cb info = SYNC_CB_INIT;
1345 struct pathspec pathspec = { 0 };
1346 struct module_list list = MODULE_LIST_INIT;
1347 int quiet = 0;
1348 int recursive = 0;
1349 struct option module_sync_options[] = {
1350 OPT__SUPER_PREFIX(&info.super_prefix),
1351 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1352 OPT_BOOL(0, "recursive", &recursive,
1353 N_("recurse into nested submodules")),
1354 OPT_END()
1356 const char *const git_submodule_helper_usage[] = {
1357 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1358 NULL
1360 int ret = 1;
1362 argc = parse_options(argc, argv, prefix, module_sync_options,
1363 git_submodule_helper_usage, 0);
1365 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1366 goto cleanup;
1368 info.prefix = prefix;
1369 if (quiet)
1370 info.flags |= OPT_QUIET;
1371 if (recursive)
1372 info.flags |= OPT_RECURSIVE;
1374 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1376 ret = 0;
1377 cleanup:
1378 module_list_release(&list);
1379 clear_pathspec(&pathspec);
1380 return ret;
1383 struct deinit_cb {
1384 const char *prefix;
1385 unsigned int flags;
1387 #define DEINIT_CB_INIT { 0 }
1389 static void deinit_submodule(const char *path, const char *prefix,
1390 unsigned int flags)
1392 const struct submodule *sub;
1393 char *displaypath = NULL;
1394 struct child_process cp_config = CHILD_PROCESS_INIT;
1395 struct strbuf sb_config = STRBUF_INIT;
1396 char *sub_git_dir = xstrfmt("%s/.git", path);
1398 if (validate_submodule_path(path) < 0)
1399 exit(128);
1401 sub = submodule_from_path(the_repository, null_oid(), path);
1403 if (!sub || !sub->name)
1404 goto cleanup;
1406 displaypath = get_submodule_displaypath(path, prefix, NULL);
1408 /* remove the submodule work tree (unless the user already did it) */
1409 if (is_directory(path)) {
1410 struct strbuf sb_rm = STRBUF_INIT;
1411 const char *format;
1413 if (is_directory(sub_git_dir)) {
1414 if (!(flags & OPT_QUIET))
1415 warning(_("Submodule work tree '%s' contains a .git "
1416 "directory. This will be replaced with a "
1417 ".git file by using absorbgitdirs."),
1418 displaypath);
1420 absorb_git_dir_into_superproject(path, NULL);
1424 if (!(flags & OPT_FORCE)) {
1425 struct child_process cp_rm = CHILD_PROCESS_INIT;
1427 cp_rm.git_cmd = 1;
1428 strvec_pushl(&cp_rm.args, "rm", "-qn",
1429 path, NULL);
1431 if (run_command(&cp_rm))
1432 die(_("Submodule work tree '%s' contains local "
1433 "modifications; use '-f' to discard them"),
1434 displaypath);
1437 strbuf_addstr(&sb_rm, path);
1439 if (!remove_dir_recursively(&sb_rm, 0))
1440 format = _("Cleared directory '%s'\n");
1441 else
1442 format = _("Could not remove submodule work tree '%s'\n");
1444 if (!(flags & OPT_QUIET))
1445 printf(format, displaypath);
1447 submodule_unset_core_worktree(sub);
1449 strbuf_release(&sb_rm);
1452 if (mkdir(path, 0777))
1453 printf(_("could not create empty submodule directory %s"),
1454 displaypath);
1456 cp_config.git_cmd = 1;
1457 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1458 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1460 /* remove the .git/config entries (unless the user already did it) */
1461 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1462 char *sub_key = xstrfmt("submodule.%s", sub->name);
1465 * remove the whole section so we have a clean state when
1466 * the user later decides to init this submodule again
1468 repo_config_rename_section_in_file(the_repository, NULL, sub_key, NULL);
1469 if (!(flags & OPT_QUIET))
1470 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1471 sub->name, sub->url, displaypath);
1472 free(sub_key);
1475 cleanup:
1476 free(displaypath);
1477 free(sub_git_dir);
1478 strbuf_release(&sb_config);
1481 static void deinit_submodule_cb(const struct cache_entry *list_item,
1482 void *cb_data)
1484 struct deinit_cb *info = cb_data;
1485 deinit_submodule(list_item->name, info->prefix, info->flags);
1488 static int module_deinit(int argc, const char **argv, const char *prefix)
1490 struct deinit_cb info = DEINIT_CB_INIT;
1491 struct pathspec pathspec = { 0 };
1492 struct module_list list = MODULE_LIST_INIT;
1493 int quiet = 0;
1494 int force = 0;
1495 int all = 0;
1496 struct option module_deinit_options[] = {
1497 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1498 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1499 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1500 OPT_END()
1502 const char *const git_submodule_helper_usage[] = {
1503 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1504 NULL
1506 int ret = 1;
1508 argc = parse_options(argc, argv, prefix, module_deinit_options,
1509 git_submodule_helper_usage, 0);
1511 if (all && argc) {
1512 error("pathspec and --all are incompatible");
1513 usage_with_options(git_submodule_helper_usage,
1514 module_deinit_options);
1517 if (!argc && !all)
1518 die(_("Use '--all' if you really want to deinitialize all submodules"));
1520 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1521 goto cleanup;
1523 info.prefix = prefix;
1524 if (quiet)
1525 info.flags |= OPT_QUIET;
1526 if (force)
1527 info.flags |= OPT_FORCE;
1529 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1531 ret = 0;
1532 cleanup:
1533 module_list_release(&list);
1534 clear_pathspec(&pathspec);
1535 return ret;
1538 struct module_clone_data {
1539 const char *prefix;
1540 const char *path;
1541 const char *name;
1542 const char *url;
1543 int depth;
1544 struct list_objects_filter_options *filter_options;
1545 enum ref_storage_format ref_storage_format;
1546 unsigned int quiet: 1;
1547 unsigned int progress: 1;
1548 unsigned int dissociate: 1;
1549 unsigned int require_init: 1;
1550 int single_branch;
1552 #define MODULE_CLONE_DATA_INIT { \
1553 .single_branch = -1, \
1554 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
1557 struct submodule_alternate_setup {
1558 const char *submodule_name;
1559 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1560 SUBMODULE_ALTERNATE_ERROR_DIE,
1561 SUBMODULE_ALTERNATE_ERROR_INFO,
1562 SUBMODULE_ALTERNATE_ERROR_IGNORE
1563 } error_mode;
1564 struct string_list *reference;
1566 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1567 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1570 static const char alternate_error_advice[] = N_(
1571 "An alternate computed from a superproject's alternate is invalid.\n"
1572 "To allow Git to clone without an alternate in such a case, set\n"
1573 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1574 "'--reference-if-able' instead of '--reference'."
1577 static int add_possible_reference_from_superproject(
1578 struct object_directory *odb, void *sas_cb)
1580 struct submodule_alternate_setup *sas = sas_cb;
1581 size_t len;
1584 * If the alternate object store is another repository, try the
1585 * standard layout with .git/(modules/<name>)+/objects
1587 if (strip_suffix(odb->path, "/objects", &len)) {
1588 struct repository alternate;
1589 char *sm_alternate;
1590 struct strbuf sb = STRBUF_INIT;
1591 struct strbuf err = STRBUF_INIT;
1592 strbuf_add(&sb, odb->path, len);
1594 if (repo_init(&alternate, sb.buf, NULL) < 0)
1595 die(_("could not get a repository handle for gitdir '%s'"),
1596 sb.buf);
1599 * We need to end the new path with '/' to mark it as a dir,
1600 * otherwise a submodule name containing '/' will be broken
1601 * as the last part of a missing submodule reference would
1602 * be taken as a file name.
1604 strbuf_reset(&sb);
1605 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1606 strbuf_addch(&sb, '/');
1607 repo_clear(&alternate);
1609 sm_alternate = compute_alternate_path(sb.buf, &err);
1610 if (sm_alternate) {
1611 char *p = strbuf_detach(&sb, NULL);
1613 string_list_append(sas->reference, p)->util = p;
1614 free(sm_alternate);
1615 } else {
1616 switch (sas->error_mode) {
1617 case SUBMODULE_ALTERNATE_ERROR_DIE:
1618 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1619 advise(_(alternate_error_advice));
1620 die(_("submodule '%s' cannot add alternate: %s"),
1621 sas->submodule_name, err.buf);
1622 case SUBMODULE_ALTERNATE_ERROR_INFO:
1623 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1624 sas->submodule_name, err.buf);
1625 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1626 ; /* nothing */
1630 strbuf_release(&err);
1631 strbuf_release(&sb);
1634 return 0;
1637 static void prepare_possible_alternates(const char *sm_name,
1638 struct string_list *reference)
1640 char *sm_alternate = NULL, *error_strategy = NULL;
1641 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1643 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1644 if (!sm_alternate)
1645 return;
1647 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1649 if (!error_strategy)
1650 error_strategy = xstrdup("die");
1652 sas.submodule_name = sm_name;
1653 sas.reference = reference;
1654 if (!strcmp(error_strategy, "die"))
1655 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1656 else if (!strcmp(error_strategy, "info"))
1657 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1658 else if (!strcmp(error_strategy, "ignore"))
1659 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1660 else
1661 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1663 if (!strcmp(sm_alternate, "superproject"))
1664 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1665 else if (!strcmp(sm_alternate, "no"))
1666 ; /* do nothing */
1667 else
1668 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1670 free(sm_alternate);
1671 free(error_strategy);
1674 static char *clone_submodule_sm_gitdir(const char *name)
1676 struct strbuf sb = STRBUF_INIT;
1677 char *sm_gitdir;
1679 submodule_name_to_gitdir(&sb, the_repository, name);
1680 sm_gitdir = absolute_pathdup(sb.buf);
1681 strbuf_release(&sb);
1683 return sm_gitdir;
1686 static int dir_contains_only_dotgit(const char *path)
1688 DIR *dir = opendir(path);
1689 struct dirent *e;
1690 int ret = 1;
1692 if (!dir)
1693 return 0;
1695 e = readdir_skip_dot_and_dotdot(dir);
1696 if (!e)
1697 ret = 0;
1698 else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT, e->d_name) ||
1699 (e = readdir_skip_dot_and_dotdot(dir))) {
1700 error("unexpected item '%s' in '%s'", e->d_name, path);
1701 ret = 0;
1704 closedir(dir);
1705 return ret;
1708 static int clone_submodule(const struct module_clone_data *clone_data,
1709 struct string_list *reference)
1711 char *p;
1712 char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1713 char *sm_alternate = NULL, *error_strategy = NULL;
1714 struct stat st;
1715 struct child_process cp = CHILD_PROCESS_INIT;
1716 const char *clone_data_path = clone_data->path;
1717 char *to_free = NULL;
1719 if (validate_submodule_path(clone_data_path) < 0)
1720 exit(128);
1722 if (!is_absolute_path(clone_data->path))
1723 clone_data_path = to_free = xstrfmt("%s/%s", repo_get_work_tree(the_repository),
1724 clone_data->path);
1726 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1727 die(_("refusing to create/use '%s' in another submodule's "
1728 "git dir"), sm_gitdir);
1730 if (!file_exists(sm_gitdir)) {
1731 if (clone_data->require_init && !stat(clone_data_path, &st) &&
1732 !is_empty_dir(clone_data_path))
1733 die(_("directory not empty: '%s'"), clone_data_path);
1735 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1736 die(_("could not create directory '%s'"), sm_gitdir);
1738 prepare_possible_alternates(clone_data->name, reference);
1740 strvec_push(&cp.args, "clone");
1741 strvec_push(&cp.args, "--no-checkout");
1742 if (clone_data->quiet)
1743 strvec_push(&cp.args, "--quiet");
1744 if (clone_data->progress)
1745 strvec_push(&cp.args, "--progress");
1746 if (clone_data->depth > 0)
1747 strvec_pushf(&cp.args, "--depth=%d", clone_data->depth);
1748 if (reference->nr) {
1749 struct string_list_item *item;
1751 for_each_string_list_item(item, reference)
1752 strvec_pushl(&cp.args, "--reference",
1753 item->string, NULL);
1755 if (clone_data->ref_storage_format != REF_STORAGE_FORMAT_UNKNOWN)
1756 strvec_pushf(&cp.args, "--ref-format=%s",
1757 ref_storage_format_to_name(clone_data->ref_storage_format));
1758 if (clone_data->dissociate)
1759 strvec_push(&cp.args, "--dissociate");
1760 if (sm_gitdir && *sm_gitdir)
1761 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1762 if (clone_data->filter_options && clone_data->filter_options->choice)
1763 strvec_pushf(&cp.args, "--filter=%s",
1764 expand_list_objects_filter_spec(
1765 clone_data->filter_options));
1766 if (clone_data->single_branch >= 0)
1767 strvec_push(&cp.args, clone_data->single_branch ?
1768 "--single-branch" :
1769 "--no-single-branch");
1771 strvec_push(&cp.args, "--");
1772 strvec_push(&cp.args, clone_data->url);
1773 strvec_push(&cp.args, clone_data_path);
1775 cp.git_cmd = 1;
1776 prepare_submodule_repo_env(&cp.env);
1777 cp.no_stdin = 1;
1779 if(run_command(&cp))
1780 die(_("clone of '%s' into submodule path '%s' failed"),
1781 clone_data->url, clone_data_path);
1783 if (clone_data->require_init && !stat(clone_data_path, &st) &&
1784 !dir_contains_only_dotgit(clone_data_path)) {
1785 char *dot_git = xstrfmt("%s/.git", clone_data_path);
1786 unlink(dot_git);
1787 free(dot_git);
1788 die(_("directory not empty: '%s'"), clone_data_path);
1790 } else {
1791 char *path;
1793 if (clone_data->require_init && !stat(clone_data_path, &st) &&
1794 !is_empty_dir(clone_data_path))
1795 die(_("directory not empty: '%s'"), clone_data_path);
1796 if (safe_create_leading_directories_const(clone_data_path) < 0)
1797 die(_("could not create directory '%s'"), clone_data_path);
1798 path = xstrfmt("%s/index", sm_gitdir);
1799 unlink_or_warn(path);
1800 free(path);
1804 * We already performed this check at the beginning of this function,
1805 * before cloning the objects. This tries to detect racy behavior e.g.
1806 * in parallel clones, where another process could easily have made the
1807 * gitdir nested _after_ it was created.
1809 * To prevent further harm coming from this unintentionally-nested
1810 * gitdir, let's disable it by deleting the `HEAD` file.
1812 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) {
1813 char *head = xstrfmt("%s/HEAD", sm_gitdir);
1814 unlink(head);
1815 free(head);
1816 die(_("refusing to create/use '%s' in another submodule's "
1817 "git dir"), sm_gitdir);
1820 connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1822 p = git_pathdup_submodule(clone_data_path, "config");
1823 if (!p)
1824 die(_("could not get submodule directory for '%s'"), clone_data_path);
1826 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1827 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1828 if (sm_alternate)
1829 git_config_set_in_file(p, "submodule.alternateLocation",
1830 sm_alternate);
1831 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1832 if (error_strategy)
1833 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1834 error_strategy);
1836 free(sm_alternate);
1837 free(error_strategy);
1839 free(sm_gitdir);
1840 free(p);
1841 free(to_free);
1842 return 0;
1845 static int module_clone(int argc, const char **argv, const char *prefix)
1847 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1848 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1849 struct string_list reference = STRING_LIST_INIT_NODUP;
1850 struct list_objects_filter_options filter_options =
1851 LIST_OBJECTS_FILTER_INIT;
1852 const char *ref_storage_format = NULL;
1854 struct option module_clone_options[] = {
1855 OPT_STRING(0, "prefix", &clone_data.prefix,
1856 N_("path"),
1857 N_("alternative anchor for relative paths")),
1858 OPT_STRING(0, "path", &clone_data.path,
1859 N_("path"),
1860 N_("where the new submodule will be cloned to")),
1861 OPT_STRING(0, "name", &clone_data.name,
1862 N_("string"),
1863 N_("name of the new submodule")),
1864 OPT_STRING(0, "url", &clone_data.url,
1865 N_("string"),
1866 N_("url where to clone the submodule from")),
1867 OPT_STRING_LIST(0, "reference", &reference,
1868 N_("repo"),
1869 N_("reference repository")),
1870 OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
1871 N_("specify the reference format to use")),
1872 OPT_BOOL(0, "dissociate", &dissociate,
1873 N_("use --reference only while cloning")),
1874 OPT_INTEGER(0, "depth", &clone_data.depth,
1875 N_("depth for shallow clones")),
1876 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1877 OPT_BOOL(0, "progress", &progress,
1878 N_("force cloning progress")),
1879 OPT_BOOL(0, "require-init", &require_init,
1880 N_("disallow cloning into non-empty directory")),
1881 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1882 N_("clone only one branch, HEAD or --branch")),
1883 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1884 OPT_END()
1886 const char *const git_submodule_helper_usage[] = {
1887 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1888 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1889 "[--single-branch] [--filter <filter-spec>] "
1890 "--url <url> --path <path>"),
1891 NULL
1894 argc = parse_options(argc, argv, prefix, module_clone_options,
1895 git_submodule_helper_usage, 0);
1897 if (ref_storage_format) {
1898 clone_data.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
1899 if (clone_data.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
1900 die(_("unknown ref storage format '%s'"), ref_storage_format);
1902 clone_data.dissociate = !!dissociate;
1903 clone_data.quiet = !!quiet;
1904 clone_data.progress = !!progress;
1905 clone_data.require_init = !!require_init;
1906 clone_data.filter_options = &filter_options;
1908 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1909 usage_with_options(git_submodule_helper_usage,
1910 module_clone_options);
1912 clone_submodule(&clone_data, &reference);
1913 list_objects_filter_release(&filter_options);
1914 string_list_clear(&reference, 1);
1915 return 0;
1918 static int determine_submodule_update_strategy(struct repository *r,
1919 int just_cloned,
1920 const char *path,
1921 enum submodule_update_type update,
1922 struct submodule_update_strategy *out)
1924 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1925 char *key;
1926 const char *val;
1927 int ret;
1929 key = xstrfmt("submodule.%s.update", sub->name);
1931 if (update) {
1932 out->type = update;
1933 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1934 if (parse_submodule_update_strategy(val, out) < 0) {
1935 ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1936 val, path);
1937 goto cleanup;
1939 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1940 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1941 BUG("how did we read update = !command from .gitmodules?");
1942 out->type = sub->update_strategy.type;
1943 out->command = sub->update_strategy.command;
1944 } else
1945 out->type = SM_UPDATE_CHECKOUT;
1947 if (just_cloned &&
1948 (out->type == SM_UPDATE_MERGE ||
1949 out->type == SM_UPDATE_REBASE ||
1950 out->type == SM_UPDATE_NONE))
1951 out->type = SM_UPDATE_CHECKOUT;
1953 ret = 0;
1954 cleanup:
1955 free(key);
1956 return ret;
1959 struct update_clone_data {
1960 const struct submodule *sub;
1961 struct object_id oid;
1962 unsigned just_cloned;
1965 struct submodule_update_clone {
1966 /* index into 'update_data.list', the list of submodules to look into for cloning */
1967 int current;
1969 /* configuration parameters which are passed on to the children */
1970 const struct update_data *update_data;
1972 /* to be consumed by update_submodule() */
1973 struct update_clone_data *update_clone;
1974 int update_clone_nr; int update_clone_alloc;
1976 /* If we want to stop as fast as possible and return an error */
1977 unsigned quickstop : 1;
1979 /* failed clones to be retried again */
1980 const struct cache_entry **failed_clones;
1981 int failed_clones_nr, failed_clones_alloc;
1983 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1985 static void submodule_update_clone_release(struct submodule_update_clone *suc)
1987 free(suc->update_clone);
1988 free(suc->failed_clones);
1991 struct update_data {
1992 const char *prefix;
1993 const char *super_prefix;
1994 char *displaypath;
1995 enum submodule_update_type update_default;
1996 struct object_id suboid;
1997 struct string_list references;
1998 struct submodule_update_strategy update_strategy;
1999 struct list_objects_filter_options *filter_options;
2000 struct module_list list;
2001 enum ref_storage_format ref_storage_format;
2002 int depth;
2003 int max_jobs;
2004 int single_branch;
2005 int recommend_shallow;
2006 unsigned int require_init;
2007 unsigned int force;
2008 unsigned int quiet;
2009 unsigned int nofetch;
2010 unsigned int remote;
2011 unsigned int progress;
2012 unsigned int dissociate;
2013 unsigned int init;
2014 unsigned int warn_if_uninitialized;
2015 unsigned int recursive;
2017 /* copied over from update_clone_data */
2018 struct object_id oid;
2019 unsigned int just_cloned;
2020 const char *sm_path;
2022 #define UPDATE_DATA_INIT { \
2023 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2024 .list = MODULE_LIST_INIT, \
2025 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
2026 .recommend_shallow = -1, \
2027 .references = STRING_LIST_INIT_DUP, \
2028 .single_branch = -1, \
2029 .max_jobs = 1, \
2032 static void update_data_release(struct update_data *ud)
2034 free(ud->displaypath);
2035 submodule_update_strategy_release(&ud->update_strategy);
2036 module_list_release(&ud->list);
2039 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
2040 struct strbuf *out, const char *displaypath)
2043 * Only mention uninitialized submodules when their
2044 * paths have been specified.
2046 if (suc->update_data->warn_if_uninitialized) {
2047 strbuf_addf(out,
2048 _("Submodule path '%s' not initialized"),
2049 displaypath);
2050 strbuf_addch(out, '\n');
2051 strbuf_addstr(out,
2052 _("Maybe you want to use 'update --init'?"));
2053 strbuf_addch(out, '\n');
2058 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2059 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2061 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
2062 struct child_process *child,
2063 struct submodule_update_clone *suc,
2064 struct strbuf *out)
2066 const struct submodule *sub = NULL;
2067 const char *url = NULL;
2068 const char *update_string;
2069 enum submodule_update_type update_type;
2070 char *key;
2071 const struct update_data *ud = suc->update_data;
2072 char *displaypath = get_submodule_displaypath(ce->name, ud->prefix,
2073 ud->super_prefix);
2074 struct strbuf sb = STRBUF_INIT;
2075 int needs_cloning = 0;
2076 int need_free_url = 0;
2078 if (ce_stage(ce)) {
2079 strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
2080 strbuf_addch(out, '\n');
2081 goto cleanup;
2084 sub = submodule_from_path(the_repository, null_oid(), ce->name);
2086 if (!sub) {
2087 next_submodule_warn_missing(suc, out, displaypath);
2088 goto cleanup;
2091 key = xstrfmt("submodule.%s.update", sub->name);
2092 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
2093 update_type = parse_submodule_update_type(update_string);
2094 } else {
2095 update_type = sub->update_strategy.type;
2097 free(key);
2099 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
2100 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
2101 && update_type == SM_UPDATE_NONE)) {
2102 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
2103 strbuf_addch(out, '\n');
2104 goto cleanup;
2107 /* Check if the submodule has been initialized. */
2108 if (!is_submodule_active(the_repository, ce->name)) {
2109 next_submodule_warn_missing(suc, out, displaypath);
2110 goto cleanup;
2113 strbuf_reset(&sb);
2114 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2115 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2116 if (sub->url && (starts_with_dot_slash(sub->url) ||
2117 starts_with_dot_dot_slash(sub->url))) {
2118 url = resolve_relative_url(sub->url, NULL, 0);
2119 need_free_url = 1;
2120 } else
2121 url = sub->url;
2124 if (!url)
2125 die(_("cannot clone submodule '%s' without a URL"), sub->name);
2127 strbuf_reset(&sb);
2128 strbuf_addf(&sb, "%s/.git", ce->name);
2129 needs_cloning = !file_exists(sb.buf);
2131 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2132 suc->update_clone_alloc);
2133 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2134 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2135 suc->update_clone[suc->update_clone_nr].sub = sub;
2136 suc->update_clone_nr++;
2138 if (!needs_cloning)
2139 goto cleanup;
2141 child->git_cmd = 1;
2142 child->no_stdin = 1;
2143 child->stdout_to_stderr = 1;
2144 child->err = -1;
2145 strvec_push(&child->args, "submodule--helper");
2146 strvec_push(&child->args, "clone");
2147 if (suc->update_data->progress)
2148 strvec_push(&child->args, "--progress");
2149 if (suc->update_data->quiet)
2150 strvec_push(&child->args, "--quiet");
2151 if (suc->update_data->prefix)
2152 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2153 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2154 strvec_push(&child->args, "--depth=1");
2155 else if (suc->update_data->depth)
2156 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2157 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2158 strvec_pushf(&child->args, "--filter=%s",
2159 expand_list_objects_filter_spec(suc->update_data->filter_options));
2160 if (suc->update_data->require_init)
2161 strvec_push(&child->args, "--require-init");
2162 if (suc->update_data->ref_storage_format != REF_STORAGE_FORMAT_UNKNOWN)
2163 strvec_pushf(&child->args, "--ref-format=%s",
2164 ref_storage_format_to_name(suc->update_data->ref_storage_format));
2165 strvec_pushl(&child->args, "--path", sub->path, NULL);
2166 strvec_pushl(&child->args, "--name", sub->name, NULL);
2167 strvec_pushl(&child->args, "--url", url, NULL);
2168 if (suc->update_data->references.nr) {
2169 struct string_list_item *item;
2171 for_each_string_list_item(item, &suc->update_data->references)
2172 strvec_pushl(&child->args, "--reference", item->string, NULL);
2174 if (suc->update_data->dissociate)
2175 strvec_push(&child->args, "--dissociate");
2176 if (suc->update_data->single_branch >= 0)
2177 strvec_push(&child->args, suc->update_data->single_branch ?
2178 "--single-branch" :
2179 "--no-single-branch");
2181 cleanup:
2182 free(displaypath);
2183 strbuf_release(&sb);
2184 if (need_free_url)
2185 free((void*)url);
2187 return needs_cloning;
2190 static int update_clone_get_next_task(struct child_process *child,
2191 struct strbuf *err,
2192 void *suc_cb,
2193 void **idx_task_cb)
2195 struct submodule_update_clone *suc = suc_cb;
2196 const struct cache_entry *ce;
2197 int index;
2199 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2200 ce = suc->update_data->list.entries[suc->current];
2201 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2202 int *p = xmalloc(sizeof(*p));
2204 *p = suc->current;
2205 *idx_task_cb = p;
2206 suc->current++;
2207 return 1;
2212 * The loop above tried cloning each submodule once, now try the
2213 * stragglers again, which we can imagine as an extension of the
2214 * entry list.
2216 index = suc->current - suc->update_data->list.nr;
2217 if (index < suc->failed_clones_nr) {
2218 int *p;
2220 ce = suc->failed_clones[index];
2221 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2222 suc->current ++;
2223 strbuf_addstr(err, "BUG: submodule considered for "
2224 "cloning, doesn't need cloning "
2225 "any more?\n");
2226 return 0;
2228 p = xmalloc(sizeof(*p));
2229 *p = suc->current;
2230 *idx_task_cb = p;
2231 suc->current ++;
2232 return 1;
2235 return 0;
2238 static int update_clone_start_failure(struct strbuf *err UNUSED,
2239 void *suc_cb,
2240 void *idx_task_cb UNUSED)
2242 struct submodule_update_clone *suc = suc_cb;
2244 suc->quickstop = 1;
2245 return 1;
2248 static int update_clone_task_finished(int result,
2249 struct strbuf *err,
2250 void *suc_cb,
2251 void *idx_task_cb)
2253 const struct cache_entry *ce;
2254 struct submodule_update_clone *suc = suc_cb;
2255 int *idxP = idx_task_cb;
2256 int idx = *idxP;
2258 free(idxP);
2260 if (!result)
2261 return 0;
2263 if (idx < suc->update_data->list.nr) {
2264 ce = suc->update_data->list.entries[idx];
2265 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2266 ce->name);
2267 strbuf_addch(err, '\n');
2268 ALLOC_GROW(suc->failed_clones,
2269 suc->failed_clones_nr + 1,
2270 suc->failed_clones_alloc);
2271 suc->failed_clones[suc->failed_clones_nr++] = ce;
2272 return 0;
2273 } else {
2274 idx -= suc->update_data->list.nr;
2275 ce = suc->failed_clones[idx];
2276 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2277 ce->name);
2278 strbuf_addch(err, '\n');
2279 suc->quickstop = 1;
2280 return 1;
2283 return 0;
2286 static int git_update_clone_config(const char *var, const char *value,
2287 const struct config_context *ctx,
2288 void *cb)
2290 int *max_jobs = cb;
2292 if (!strcmp(var, "submodule.fetchjobs"))
2293 *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
2294 return 0;
2297 static int is_tip_reachable(const char *path, const struct object_id *oid)
2299 struct child_process cp = CHILD_PROCESS_INIT;
2300 struct strbuf rev = STRBUF_INIT;
2301 char *hex = oid_to_hex(oid);
2302 int reachable;
2304 cp.git_cmd = 1;
2305 cp.dir = path;
2306 cp.no_stderr = 1;
2307 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2309 prepare_submodule_repo_env(&cp.env);
2311 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2312 reachable = 0;
2313 else
2314 reachable = 1;
2316 strbuf_release(&rev);
2317 return reachable;
2320 static int fetch_in_submodule(const char *module_path, int depth, int quiet,
2321 const struct object_id *oid)
2323 struct child_process cp = CHILD_PROCESS_INIT;
2325 prepare_submodule_repo_env(&cp.env);
2326 cp.git_cmd = 1;
2327 cp.dir = module_path;
2329 strvec_push(&cp.args, "fetch");
2330 if (quiet)
2331 strvec_push(&cp.args, "--quiet");
2332 if (depth)
2333 strvec_pushf(&cp.args, "--depth=%d", depth);
2334 if (oid) {
2335 char *hex = oid_to_hex(oid);
2336 char *remote;
2337 int code;
2339 code = get_default_remote_submodule(module_path, &remote);
2340 if (code) {
2341 child_process_clear(&cp);
2342 return code;
2345 strvec_pushl(&cp.args, remote, hex, NULL);
2346 free(remote);
2349 return run_command(&cp);
2352 static int run_update_command(const struct update_data *ud, int subforce)
2354 struct child_process cp = CHILD_PROCESS_INIT;
2355 char *oid = oid_to_hex(&ud->oid);
2356 int ret;
2358 switch (ud->update_strategy.type) {
2359 case SM_UPDATE_CHECKOUT:
2360 cp.git_cmd = 1;
2361 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2362 if (subforce)
2363 strvec_push(&cp.args, "-f");
2364 break;
2365 case SM_UPDATE_REBASE:
2366 cp.git_cmd = 1;
2367 strvec_push(&cp.args, "rebase");
2368 if (ud->quiet)
2369 strvec_push(&cp.args, "--quiet");
2370 break;
2371 case SM_UPDATE_MERGE:
2372 cp.git_cmd = 1;
2373 strvec_push(&cp.args, "merge");
2374 if (ud->quiet)
2375 strvec_push(&cp.args, "--quiet");
2376 break;
2377 case SM_UPDATE_COMMAND:
2378 cp.use_shell = 1;
2379 strvec_push(&cp.args, ud->update_strategy.command);
2380 break;
2381 default:
2382 BUG("unexpected update strategy type: %d",
2383 ud->update_strategy.type);
2385 strvec_push(&cp.args, oid);
2387 cp.dir = ud->sm_path;
2388 prepare_submodule_repo_env(&cp.env);
2389 if ((ret = run_command(&cp))) {
2390 switch (ud->update_strategy.type) {
2391 case SM_UPDATE_CHECKOUT:
2392 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2393 oid, ud->displaypath);
2394 /* No "ret" assignment, use "git checkout"'s */
2395 break;
2396 case SM_UPDATE_REBASE:
2397 ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2398 oid, ud->displaypath);
2399 break;
2400 case SM_UPDATE_MERGE:
2401 ret = die_message(_("Unable to merge '%s' in submodule path '%s'"),
2402 oid, ud->displaypath);
2403 break;
2404 case SM_UPDATE_COMMAND:
2405 ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2406 ud->update_strategy.command, oid, ud->displaypath);
2407 break;
2408 default:
2409 BUG("unexpected update strategy type: %d",
2410 ud->update_strategy.type);
2413 return ret;
2416 if (ud->quiet)
2417 return 0;
2419 switch (ud->update_strategy.type) {
2420 case SM_UPDATE_CHECKOUT:
2421 printf(_("Submodule path '%s': checked out '%s'\n"),
2422 ud->displaypath, oid);
2423 break;
2424 case SM_UPDATE_REBASE:
2425 printf(_("Submodule path '%s': rebased into '%s'\n"),
2426 ud->displaypath, oid);
2427 break;
2428 case SM_UPDATE_MERGE:
2429 printf(_("Submodule path '%s': merged in '%s'\n"),
2430 ud->displaypath, oid);
2431 break;
2432 case SM_UPDATE_COMMAND:
2433 printf(_("Submodule path '%s': '%s %s'\n"),
2434 ud->displaypath, ud->update_strategy.command, oid);
2435 break;
2436 default:
2437 BUG("unexpected update strategy type: %d",
2438 ud->update_strategy.type);
2441 return 0;
2444 static int run_update_procedure(const struct update_data *ud)
2446 int subforce = is_null_oid(&ud->suboid) || ud->force;
2448 if (!ud->nofetch) {
2450 * Run fetch only if `oid` isn't present or it
2451 * is not reachable from a ref.
2453 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2454 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2455 !ud->quiet)
2456 fprintf_ln(stderr,
2457 _("Unable to fetch in submodule path '%s'; "
2458 "trying to directly fetch %s:"),
2459 ud->displaypath, oid_to_hex(&ud->oid));
2461 * Now we tried the usual fetch, but `oid` may
2462 * not be reachable from any of the refs.
2464 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2465 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2466 return die_message(_("Fetched in submodule path '%s', but it did not "
2467 "contain %s. Direct fetching of that commit failed."),
2468 ud->displaypath, oid_to_hex(&ud->oid));
2471 return run_update_command(ud, subforce);
2474 static int remote_submodule_branch(const char *path, const char **branch)
2476 const struct submodule *sub;
2477 char *key;
2478 *branch = NULL;
2480 sub = submodule_from_path(the_repository, null_oid(), path);
2481 if (!sub)
2482 return die_message(_("could not initialize submodule at path '%s'"),
2483 path);
2485 key = xstrfmt("submodule.%s.branch", sub->name);
2486 if (repo_config_get_string_tmp(the_repository, key, branch))
2487 *branch = sub->branch;
2488 free(key);
2490 if (!*branch) {
2491 *branch = "HEAD";
2492 return 0;
2495 if (!strcmp(*branch, ".")) {
2496 const char *refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2497 "HEAD", 0, NULL,
2498 NULL);
2500 if (!refname)
2501 return die_message(_("No such ref: %s"), "HEAD");
2503 /* detached HEAD */
2504 if (!strcmp(refname, "HEAD"))
2505 return die_message(_("Submodule (%s) branch configured to inherit "
2506 "branch from superproject, but the superproject "
2507 "is not on any branch"), sub->name);
2509 if (!skip_prefix(refname, "refs/heads/", &refname))
2510 return die_message(_("Expecting a full ref name, got %s"),
2511 refname);
2513 *branch = refname;
2514 return 0;
2517 /* Our "branch" is coming from repo_config_get_string_tmp() */
2518 return 0;
2521 static int ensure_core_worktree(const char *path)
2523 const char *cw;
2524 struct repository subrepo;
2526 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2527 return die_message(_("could not get a repository handle for submodule '%s'"),
2528 path);
2530 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2531 char *cfg_file, *abs_path;
2532 const char *rel_path;
2533 struct strbuf sb = STRBUF_INIT;
2535 cfg_file = repo_git_path(&subrepo, "config");
2537 abs_path = absolute_pathdup(path);
2538 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2540 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2542 free(cfg_file);
2543 free(abs_path);
2544 strbuf_release(&sb);
2547 repo_clear(&subrepo);
2548 return 0;
2551 static const char *submodule_update_type_to_label(enum submodule_update_type type)
2553 switch (type) {
2554 case SM_UPDATE_CHECKOUT:
2555 return "checkout";
2556 case SM_UPDATE_MERGE:
2557 return "merge";
2558 case SM_UPDATE_REBASE:
2559 return "rebase";
2560 case SM_UPDATE_UNSPECIFIED:
2561 case SM_UPDATE_NONE:
2562 case SM_UPDATE_COMMAND:
2563 break;
2565 BUG("unreachable with type %d", type);
2568 static void update_data_to_args(const struct update_data *update_data,
2569 struct strvec *args)
2571 enum submodule_update_type update_type = update_data->update_default;
2573 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2574 if (update_data->displaypath)
2575 strvec_pushf(args, "--super-prefix=%s/",
2576 update_data->displaypath);
2577 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2578 if (update_data->quiet)
2579 strvec_push(args, "--quiet");
2580 if (update_data->force)
2581 strvec_push(args, "--force");
2582 if (update_data->init)
2583 strvec_push(args, "--init");
2584 if (update_data->remote)
2585 strvec_push(args, "--remote");
2586 if (update_data->nofetch)
2587 strvec_push(args, "--no-fetch");
2588 if (update_data->dissociate)
2589 strvec_push(args, "--dissociate");
2590 if (update_data->progress)
2591 strvec_push(args, "--progress");
2592 if (update_data->require_init)
2593 strvec_push(args, "--require-init");
2594 if (update_data->depth)
2595 strvec_pushf(args, "--depth=%d", update_data->depth);
2596 if (update_type != SM_UPDATE_UNSPECIFIED)
2597 strvec_pushf(args, "--%s",
2598 submodule_update_type_to_label(update_type));
2600 if (update_data->references.nr) {
2601 struct string_list_item *item;
2603 for_each_string_list_item(item, &update_data->references)
2604 strvec_pushl(args, "--reference", item->string, NULL);
2606 if (update_data->ref_storage_format != REF_STORAGE_FORMAT_UNKNOWN)
2607 strvec_pushf(args, "--ref-format=%s",
2608 ref_storage_format_to_name(update_data->ref_storage_format));
2609 if (update_data->filter_options && update_data->filter_options->choice)
2610 strvec_pushf(args, "--filter=%s",
2611 expand_list_objects_filter_spec(
2612 update_data->filter_options));
2613 if (update_data->recommend_shallow == 0)
2614 strvec_push(args, "--no-recommend-shallow");
2615 else if (update_data->recommend_shallow == 1)
2616 strvec_push(args, "--recommend-shallow");
2617 if (update_data->single_branch >= 0)
2618 strvec_push(args, update_data->single_branch ?
2619 "--single-branch" :
2620 "--no-single-branch");
2623 static int update_submodule(struct update_data *update_data)
2625 int ret;
2627 if (validate_submodule_path(update_data->sm_path) < 0)
2628 return -1;
2630 ret = determine_submodule_update_strategy(the_repository,
2631 update_data->just_cloned,
2632 update_data->sm_path,
2633 update_data->update_default,
2634 &update_data->update_strategy);
2635 if (ret)
2636 return ret;
2638 if (update_data->just_cloned)
2639 oidcpy(&update_data->suboid, null_oid());
2640 else if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
2641 "HEAD", &update_data->suboid))
2642 return die_message(_("Unable to find current revision in submodule path '%s'"),
2643 update_data->displaypath);
2645 if (update_data->remote) {
2646 char *remote_name;
2647 const char *branch;
2648 char *remote_ref;
2649 int code;
2651 code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2652 if (code)
2653 return code;
2654 code = remote_submodule_branch(update_data->sm_path, &branch);
2655 if (code)
2656 return code;
2657 remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2659 free(remote_name);
2661 if (!update_data->nofetch) {
2662 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2663 0, NULL)) {
2664 free(remote_ref);
2665 return die_message(_("Unable to fetch in submodule path '%s'"),
2666 update_data->sm_path);
2670 if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
2671 remote_ref, &update_data->oid)) {
2672 ret = die_message(_("Unable to find %s revision in submodule path '%s'"),
2673 remote_ref, update_data->sm_path);
2674 free(remote_ref);
2675 return ret;
2678 free(remote_ref);
2681 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2682 ret = run_update_procedure(update_data);
2683 if (ret)
2684 return ret;
2687 if (update_data->recursive) {
2688 struct child_process cp = CHILD_PROCESS_INIT;
2689 struct update_data next = *update_data;
2691 next.prefix = NULL;
2692 oidcpy(&next.oid, null_oid());
2693 oidcpy(&next.suboid, null_oid());
2695 cp.dir = update_data->sm_path;
2696 cp.git_cmd = 1;
2697 prepare_submodule_repo_env(&cp.env);
2698 update_data_to_args(&next, &cp.args);
2700 ret = run_command(&cp);
2701 if (ret)
2702 die_message(_("Failed to recurse into submodule path '%s'"),
2703 update_data->displaypath);
2704 return ret;
2707 return 0;
2710 static int update_submodules(struct update_data *update_data)
2712 int i, ret = 0;
2713 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2714 const struct run_process_parallel_opts opts = {
2715 .tr2_category = "submodule",
2716 .tr2_label = "parallel/update",
2718 .processes = update_data->max_jobs,
2720 .get_next_task = update_clone_get_next_task,
2721 .start_failure = update_clone_start_failure,
2722 .task_finished = update_clone_task_finished,
2723 .data = &suc,
2726 suc.update_data = update_data;
2727 run_processes_parallel(&opts);
2730 * We saved the output and put it out all at once now.
2731 * That means:
2732 * - the listener does not have to interleave their (checkout)
2733 * work with our fetching. The writes involved in a
2734 * checkout involve more straightforward sequential I/O.
2735 * - the listener can avoid doing any work if fetching failed.
2737 if (suc.quickstop) {
2738 ret = 1;
2739 goto cleanup;
2742 for (i = 0; i < suc.update_clone_nr; i++) {
2743 struct update_clone_data ucd = suc.update_clone[i];
2744 int code = 128;
2746 oidcpy(&update_data->oid, &ucd.oid);
2747 update_data->just_cloned = ucd.just_cloned;
2748 update_data->sm_path = ucd.sub->path;
2751 * Verify that the submodule path does not contain any
2752 * symlinks; if it does, it might have been tampered with.
2753 * TODO: allow exempting it via
2754 * `safe.submodule.path` or something
2756 if (validate_submodule_path(update_data->sm_path) < 0)
2757 goto fail;
2759 code = ensure_core_worktree(update_data->sm_path);
2760 if (code)
2761 goto fail;
2763 update_data->displaypath = get_submodule_displaypath(
2764 update_data->sm_path, update_data->prefix,
2765 update_data->super_prefix);
2766 code = update_submodule(update_data);
2767 FREE_AND_NULL(update_data->displaypath);
2768 fail:
2769 if (!code)
2770 continue;
2771 ret = code;
2772 if (ret == 128)
2773 goto cleanup;
2776 cleanup:
2777 submodule_update_clone_release(&suc);
2778 string_list_clear(&update_data->references, 0);
2779 return ret;
2782 static int module_update(int argc, const char **argv, const char *prefix)
2784 struct pathspec pathspec = { 0 };
2785 struct pathspec pathspec2 = { 0 };
2786 struct update_data opt = UPDATE_DATA_INIT;
2787 struct list_objects_filter_options filter_options =
2788 LIST_OBJECTS_FILTER_INIT;
2789 const char *ref_storage_format = NULL;
2790 int ret;
2791 struct option module_update_options[] = {
2792 OPT__SUPER_PREFIX(&opt.super_prefix),
2793 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2794 OPT_BOOL(0, "init", &opt.init,
2795 N_("initialize uninitialized submodules before update")),
2796 OPT_BOOL(0, "remote", &opt.remote,
2797 N_("use SHA-1 of submodule's remote tracking branch")),
2798 OPT_BOOL(0, "recursive", &opt.recursive,
2799 N_("traverse submodules recursively")),
2800 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2801 N_("don't fetch new objects from the remote site")),
2802 OPT_SET_INT(0, "checkout", &opt.update_default,
2803 N_("use the 'checkout' update strategy (default)"),
2804 SM_UPDATE_CHECKOUT),
2805 OPT_SET_INT('m', "merge", &opt.update_default,
2806 N_("use the 'merge' update strategy"),
2807 SM_UPDATE_MERGE),
2808 OPT_SET_INT('r', "rebase", &opt.update_default,
2809 N_("use the 'rebase' update strategy"),
2810 SM_UPDATE_REBASE),
2811 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2812 N_("reference repository")),
2813 OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
2814 N_("specify the reference format to use")),
2815 OPT_BOOL(0, "dissociate", &opt.dissociate,
2816 N_("use --reference only while cloning")),
2817 OPT_INTEGER(0, "depth", &opt.depth,
2818 N_("create a shallow clone truncated to the "
2819 "specified number of revisions")),
2820 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2821 N_("parallel jobs")),
2822 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2823 N_("whether the initial clone should follow the shallow recommendation")),
2824 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2825 OPT_BOOL(0, "progress", &opt.progress,
2826 N_("force cloning progress")),
2827 OPT_BOOL(0, "require-init", &opt.require_init,
2828 N_("disallow cloning into non-empty directory, implies --init")),
2829 OPT_BOOL(0, "single-branch", &opt.single_branch,
2830 N_("clone only one branch, HEAD or --branch")),
2831 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2832 OPT_END()
2834 const char *const git_submodule_helper_usage[] = {
2835 N_("git submodule [--quiet] update"
2836 " [--init [--filter=<filter-spec>]] [--remote]"
2837 " [-N|--no-fetch] [-f|--force]"
2838 " [--checkout|--merge|--rebase]"
2839 " [--[no-]recommend-shallow] [--reference <repository>]"
2840 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2841 NULL
2844 update_clone_config_from_gitmodules(&opt.max_jobs);
2845 git_config(git_update_clone_config, &opt.max_jobs);
2847 argc = parse_options(argc, argv, prefix, module_update_options,
2848 git_submodule_helper_usage, 0);
2850 if (opt.require_init)
2851 opt.init = 1;
2853 if (filter_options.choice && !opt.init) {
2854 usage_with_options(git_submodule_helper_usage,
2855 module_update_options);
2858 if (ref_storage_format) {
2859 opt.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
2860 if (opt.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
2861 die(_("unknown ref storage format '%s'"), ref_storage_format);
2864 opt.filter_options = &filter_options;
2865 opt.prefix = prefix;
2867 if (opt.update_default)
2868 opt.update_strategy.type = opt.update_default;
2870 if (module_list_compute(argv, prefix, &pathspec, &opt.list) < 0) {
2871 ret = 1;
2872 goto cleanup;
2875 if (pathspec.nr)
2876 opt.warn_if_uninitialized = 1;
2878 if (opt.init) {
2879 struct module_list list = MODULE_LIST_INIT;
2880 struct init_cb info = INIT_CB_INIT;
2882 if (module_list_compute(argv, opt.prefix,
2883 &pathspec2, &list) < 0) {
2884 module_list_release(&list);
2885 ret = 1;
2886 goto cleanup;
2890 * If there are no path args and submodule.active is set then,
2891 * by default, only initialize 'active' modules.
2893 if (!argc && !git_config_get("submodule.active"))
2894 module_list_active(&list);
2896 info.prefix = opt.prefix;
2897 info.super_prefix = opt.super_prefix;
2898 if (opt.quiet)
2899 info.flags |= OPT_QUIET;
2901 for_each_listed_submodule(&list, init_submodule_cb, &info);
2902 module_list_release(&list);
2905 ret = update_submodules(&opt);
2906 cleanup:
2907 update_data_release(&opt);
2908 list_objects_filter_release(&filter_options);
2909 clear_pathspec(&pathspec);
2910 clear_pathspec(&pathspec2);
2911 return ret;
2914 static int push_check(int argc, const char **argv, const char *prefix UNUSED)
2916 struct remote *remote;
2917 const char *superproject_head;
2918 char *head;
2919 int detached_head = 0;
2920 struct object_id head_oid;
2922 if (argc < 3)
2923 die("submodule--helper push-check requires at least 2 arguments");
2926 * superproject's resolved head ref.
2927 * if HEAD then the superproject is in a detached head state, otherwise
2928 * it will be the resolved head ref.
2930 superproject_head = argv[1];
2931 argv++;
2932 argc--;
2933 /* Get the submodule's head ref and determine if it is detached */
2934 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
2935 0, &head_oid, NULL);
2936 if (!head)
2937 die(_("Failed to resolve HEAD as a valid ref."));
2938 if (!strcmp(head, "HEAD"))
2939 detached_head = 1;
2942 * The remote must be configured.
2943 * This is to avoid pushing to the exact same URL as the parent.
2945 remote = pushremote_get(argv[1]);
2946 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2947 die("remote '%s' not configured", argv[1]);
2949 /* Check the refspec */
2950 if (argc > 2) {
2951 int i;
2952 struct ref *local_refs = get_local_heads();
2953 struct refspec refspec = REFSPEC_INIT_PUSH;
2955 refspec_appendn(&refspec, argv + 2, argc - 2);
2957 for (i = 0; i < refspec.nr; i++) {
2958 const struct refspec_item *rs = &refspec.items[i];
2960 if (rs->pattern || rs->matching)
2961 continue;
2963 /* LHS must match a single ref */
2964 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2965 case 1:
2966 break;
2967 case 0:
2969 * If LHS matches 'HEAD' then we need to ensure
2970 * that it matches the same named branch
2971 * checked out in the superproject.
2973 if (!strcmp(rs->src, "HEAD")) {
2974 if (!detached_head &&
2975 !strcmp(head, superproject_head))
2976 break;
2977 die("HEAD does not match the named branch in the superproject");
2979 /* fallthrough */
2980 default:
2981 die("src refspec '%s' must name a ref",
2982 rs->src);
2986 refspec_clear(&refspec);
2987 free_refs(local_refs);
2989 free(head);
2991 return 0;
2994 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2996 int i;
2997 struct pathspec pathspec = { 0 };
2998 struct module_list list = MODULE_LIST_INIT;
2999 const char *super_prefix = NULL;
3000 struct option embed_gitdir_options[] = {
3001 OPT__SUPER_PREFIX(&super_prefix),
3002 OPT_END()
3004 const char *const git_submodule_helper_usage[] = {
3005 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
3006 NULL
3008 int ret = 1;
3010 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
3011 git_submodule_helper_usage, 0);
3013 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
3014 goto cleanup;
3016 for (i = 0; i < list.nr; i++)
3017 absorb_git_dir_into_superproject(list.entries[i]->name,
3018 super_prefix);
3020 ret = 0;
3021 cleanup:
3022 clear_pathspec(&pathspec);
3023 module_list_release(&list);
3024 return ret;
3027 static int module_set_url(int argc, const char **argv, const char *prefix)
3029 int quiet = 0, ret;
3030 const char *newurl;
3031 const char *path;
3032 char *config_name;
3033 struct option options[] = {
3034 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
3035 OPT_END()
3037 const char *const usage[] = {
3038 N_("git submodule set-url [--quiet] <path> <newurl>"),
3039 NULL
3041 const struct submodule *sub;
3043 argc = parse_options(argc, argv, prefix, options, usage, 0);
3045 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
3046 usage_with_options(usage, options);
3048 sub = submodule_from_path(the_repository, null_oid(), path);
3050 if (!sub)
3051 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3052 path);
3054 config_name = xstrfmt("submodule.%s.url", sub->name);
3055 ret = config_set_in_gitmodules_file_gently(config_name, newurl);
3057 if (!ret) {
3058 repo_read_gitmodules(the_repository, 0);
3059 sync_submodule(sub->path, prefix, NULL, quiet ? OPT_QUIET : 0);
3062 free(config_name);
3063 return !!ret;
3066 static int module_set_branch(int argc, const char **argv, const char *prefix)
3068 int opt_default = 0, ret;
3069 const char *opt_branch = NULL;
3070 const char *path;
3071 char *config_name;
3072 struct option options[] = {
3074 * We accept the `quiet` option for uniformity across subcommands,
3075 * though there is nothing to make less verbose in this subcommand.
3077 OPT_NOOP_NOARG('q', "quiet"),
3079 OPT_BOOL('d', "default", &opt_default,
3080 N_("set the default tracking branch to master")),
3081 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
3082 N_("set the default tracking branch")),
3083 OPT_END()
3085 const char *const usage[] = {
3086 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3087 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3088 NULL
3090 const struct submodule *sub;
3092 argc = parse_options(argc, argv, prefix, options, usage, 0);
3094 if (!opt_branch && !opt_default)
3095 die(_("--branch or --default required"));
3097 if (opt_branch && opt_default)
3098 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3100 if (argc != 1 || !(path = argv[0]))
3101 usage_with_options(usage, options);
3103 sub = submodule_from_path(the_repository, null_oid(), path);
3105 if (!sub)
3106 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3107 path);
3109 config_name = xstrfmt("submodule.%s.branch", sub->name);
3110 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
3112 free(config_name);
3113 return !!ret;
3116 static int module_create_branch(int argc, const char **argv, const char *prefix)
3118 enum branch_track track;
3119 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
3120 struct option options[] = {
3121 OPT__QUIET(&quiet, N_("print only error messages")),
3122 OPT__FORCE(&force, N_("force creation"), 0),
3123 OPT_BOOL(0, "create-reflog", &reflog,
3124 N_("create the branch's reflog")),
3125 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
3126 N_("set branch tracking configuration"),
3127 PARSE_OPT_OPTARG,
3128 parse_opt_tracking_mode),
3129 OPT__DRY_RUN(&dry_run,
3130 N_("show whether the branch would be created")),
3131 OPT_END()
3133 const char *const usage[] = {
3134 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3135 NULL
3138 git_config(git_default_config, NULL);
3139 track = git_branch_track;
3140 argc = parse_options(argc, argv, prefix, options, usage, 0);
3142 if (argc != 3)
3143 usage_with_options(usage, options);
3145 if (!quiet && !dry_run)
3146 printf_ln(_("creating branch '%s'"), argv[0]);
3148 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
3149 force, reflog, quiet, track, dry_run);
3150 return 0;
3153 struct add_data {
3154 const char *prefix;
3155 const char *branch;
3156 const char *reference_path;
3157 char *sm_path;
3158 const char *sm_name;
3159 const char *repo;
3160 const char *realrepo;
3161 enum ref_storage_format ref_storage_format;
3162 int depth;
3163 unsigned int force: 1;
3164 unsigned int quiet: 1;
3165 unsigned int progress: 1;
3166 unsigned int dissociate: 1;
3168 #define ADD_DATA_INIT { \
3169 .depth = -1, \
3170 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
3173 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3175 struct child_process cp_remote = CHILD_PROCESS_INIT;
3176 struct strbuf sb_remote_out = STRBUF_INIT;
3178 cp_remote.git_cmd = 1;
3179 strvec_pushf(&cp_remote.env,
3180 "GIT_DIR=%s", git_dir_path);
3181 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3182 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3183 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3184 char *next_line;
3185 char *line = sb_remote_out.buf;
3187 while ((next_line = strchr(line, '\n')) != NULL) {
3188 size_t len = next_line - line;
3190 if (strip_suffix_mem(line, &len, " (fetch)"))
3191 strbuf_addf(msg, " %.*s\n", (int)len, line);
3192 line = next_line + 1;
3196 strbuf_release(&sb_remote_out);
3199 static int add_submodule(const struct add_data *add_data)
3201 char *submod_gitdir_path;
3202 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3203 struct string_list reference = STRING_LIST_INIT_NODUP;
3204 int ret = -1;
3206 /* perhaps the path already exists and is already a git repo, else clone it */
3207 if (is_directory(add_data->sm_path)) {
3208 struct strbuf sm_path = STRBUF_INIT;
3209 strbuf_addstr(&sm_path, add_data->sm_path);
3210 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3211 if (is_nonbare_repository_dir(&sm_path))
3212 printf(_("Adding existing repo at '%s' to the index\n"),
3213 add_data->sm_path);
3214 else
3215 die(_("'%s' already exists and is not a valid git repo"),
3216 add_data->sm_path);
3217 strbuf_release(&sm_path);
3218 free(submod_gitdir_path);
3219 } else {
3220 struct child_process cp = CHILD_PROCESS_INIT;
3222 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3224 if (is_directory(submod_gitdir_path)) {
3225 if (!add_data->force) {
3226 struct strbuf msg = STRBUF_INIT;
3227 char *die_msg;
3229 strbuf_addf(&msg, _("A git directory for '%s' is found "
3230 "locally with remote(s):\n"),
3231 add_data->sm_name);
3233 append_fetch_remotes(&msg, submod_gitdir_path);
3234 free(submod_gitdir_path);
3236 strbuf_addf(&msg, _("If you want to reuse this local git "
3237 "directory instead of cloning again from\n"
3238 " %s\n"
3239 "use the '--force' option. If the local git "
3240 "directory is not the correct repo\n"
3241 "or you are unsure what this means choose "
3242 "another name with the '--name' option."),
3243 add_data->realrepo);
3245 die_msg = strbuf_detach(&msg, NULL);
3246 die("%s", die_msg);
3247 } else {
3248 printf(_("Reactivating local git directory for "
3249 "submodule '%s'\n"), add_data->sm_name);
3252 free(submod_gitdir_path);
3254 clone_data.prefix = add_data->prefix;
3255 clone_data.path = add_data->sm_path;
3256 clone_data.name = add_data->sm_name;
3257 clone_data.url = add_data->realrepo;
3258 clone_data.quiet = add_data->quiet;
3259 clone_data.progress = add_data->progress;
3260 if (add_data->reference_path) {
3261 char *p = xstrdup(add_data->reference_path);
3263 string_list_append(&reference, p)->util = p;
3265 clone_data.ref_storage_format = add_data->ref_storage_format;
3266 clone_data.dissociate = add_data->dissociate;
3267 if (add_data->depth >= 0)
3268 clone_data.depth = add_data->depth;
3270 if (clone_submodule(&clone_data, &reference))
3271 goto cleanup;
3273 prepare_submodule_repo_env(&cp.env);
3274 cp.git_cmd = 1;
3275 cp.dir = add_data->sm_path;
3277 * NOTE: we only get here if add_data->force is true, so
3278 * passing --force to checkout is reasonable.
3280 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3282 if (add_data->branch) {
3283 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3284 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3287 if (run_command(&cp))
3288 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3290 ret = 0;
3292 cleanup:
3293 string_list_clear(&reference, 1);
3294 return ret;
3297 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3299 char *key;
3300 int ret;
3302 if (!is_writing_gitmodules_ok())
3303 die(_("please make sure that the .gitmodules file is in the working tree"));
3305 key = xstrfmt("submodule.%s.%s", name, var);
3306 ret = config_set_in_gitmodules_file_gently(key, value);
3307 free(key);
3309 return ret;
3312 static void configure_added_submodule(struct add_data *add_data)
3314 char *key;
3315 struct child_process add_submod = CHILD_PROCESS_INIT;
3316 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3318 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3319 git_config_set_gently(key, add_data->realrepo);
3320 free(key);
3322 add_submod.git_cmd = 1;
3323 strvec_pushl(&add_submod.args, "add",
3324 "--no-warn-embedded-repo", NULL);
3325 if (add_data->force)
3326 strvec_push(&add_submod.args, "--force");
3327 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3329 if (run_command(&add_submod))
3330 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3332 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3333 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3334 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3336 if (add_data->branch) {
3337 if (config_submodule_in_gitmodules(add_data->sm_name,
3338 "branch", add_data->branch))
3339 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3342 add_gitmodules.git_cmd = 1;
3343 strvec_pushl(&add_gitmodules.args,
3344 "add", "--force", "--", ".gitmodules", NULL);
3346 if (run_command(&add_gitmodules))
3347 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3350 * NEEDSWORK: In a multi-working-tree world this needs to be
3351 * set in the per-worktree config.
3354 * NEEDSWORK: In the longer run, we need to get rid of this
3355 * pattern of querying "submodule.active" before calling
3356 * is_submodule_active(), since that function needs to find
3357 * out the value of "submodule.active" again anyway.
3359 if (!git_config_get("submodule.active")) {
3361 * If the submodule being added isn't already covered by the
3362 * current configured pathspec, set the submodule's active flag
3364 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3365 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3366 git_config_set_gently(key, "true");
3367 free(key);
3369 } else {
3370 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3371 git_config_set_gently(key, "true");
3372 free(key);
3376 static void die_on_index_match(const char *path, int force)
3378 struct pathspec ps;
3379 const char *args[] = { path, NULL };
3380 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3382 if (repo_read_index_preload(the_repository, NULL, 0) < 0)
3383 die(_("index file corrupt"));
3385 if (ps.nr) {
3386 int i;
3387 char *ps_matched = xcalloc(ps.nr, 1);
3389 /* TODO: audit for interaction with sparse-index. */
3390 ensure_full_index(the_repository->index);
3393 * Since there is only one pathspec, we just need to
3394 * check ps_matched[0] to know if a cache entry matched.
3396 for (i = 0; i < the_repository->index->cache_nr; i++) {
3397 ce_path_match(the_repository->index, the_repository->index->cache[i], &ps,
3398 ps_matched);
3400 if (ps_matched[0]) {
3401 if (!force)
3402 die(_("'%s' already exists in the index"),
3403 path);
3404 if (!S_ISGITLINK(the_repository->index->cache[i]->ce_mode))
3405 die(_("'%s' already exists in the index "
3406 "and is not a submodule"), path);
3407 break;
3410 free(ps_matched);
3412 clear_pathspec(&ps);
3415 static void die_on_repo_without_commits(const char *path)
3417 struct strbuf sb = STRBUF_INIT;
3418 strbuf_addstr(&sb, path);
3419 if (is_nonbare_repository_dir(&sb)) {
3420 struct object_id oid;
3421 if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
3422 die(_("'%s' does not have a commit checked out"), path);
3424 strbuf_release(&sb);
3427 static int module_add(int argc, const char **argv, const char *prefix)
3429 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3430 struct add_data add_data = ADD_DATA_INIT;
3431 const char *ref_storage_format = NULL;
3432 char *to_free = NULL;
3433 struct option options[] = {
3434 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3435 N_("branch of repository to add as submodule")),
3436 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3437 PARSE_OPT_NOCOMPLETE),
3438 OPT__QUIET(&quiet, N_("print only error messages")),
3439 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3440 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3441 N_("reference repository")),
3442 OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
3443 N_("specify the reference format to use")),
3444 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3445 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3446 N_("sets the submodule's name to the given string "
3447 "instead of defaulting to its path")),
3448 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3449 OPT_END()
3451 const char *const usage[] = {
3452 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3453 NULL
3455 struct strbuf sb = STRBUF_INIT;
3456 int ret = 1;
3458 argc = parse_options(argc, argv, prefix, options, usage, 0);
3460 if (!is_writing_gitmodules_ok())
3461 die(_("please make sure that the .gitmodules file is in the working tree"));
3463 if (prefix && *prefix &&
3464 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3465 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3467 if (argc == 0 || argc > 2)
3468 usage_with_options(usage, options);
3470 if (ref_storage_format) {
3471 add_data.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
3472 if (add_data.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
3473 die(_("unknown ref storage format '%s'"), ref_storage_format);
3476 add_data.repo = argv[0];
3477 if (argc == 1)
3478 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3479 else
3480 add_data.sm_path = xstrdup(argv[1]);
3482 if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
3483 char *sm_path = add_data.sm_path;
3485 add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
3486 free(sm_path);
3489 if (starts_with_dot_dot_slash(add_data.repo) ||
3490 starts_with_dot_slash(add_data.repo)) {
3491 if (prefix)
3492 die(_("Relative path can only be used from the toplevel "
3493 "of the working tree"));
3495 /* dereference source url relative to parent's url */
3496 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3497 add_data.realrepo = to_free;
3498 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3499 add_data.realrepo = add_data.repo;
3500 } else {
3501 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3502 add_data.repo);
3506 * normalize path:
3507 * multiple //; leading ./; /./; /../;
3509 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3510 strip_dir_trailing_slashes(add_data.sm_path);
3512 if (validate_submodule_path(add_data.sm_path) < 0)
3513 exit(128);
3515 die_on_index_match(add_data.sm_path, force);
3516 die_on_repo_without_commits(add_data.sm_path);
3518 if (!force) {
3519 struct child_process cp = CHILD_PROCESS_INIT;
3521 cp.git_cmd = 1;
3522 cp.no_stdout = 1;
3523 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3524 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3525 if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3526 strbuf_complete_line(&sb);
3527 fputs(sb.buf, stderr);
3528 goto cleanup;
3532 if(!add_data.sm_name)
3533 add_data.sm_name = add_data.sm_path;
3535 if (check_submodule_name(add_data.sm_name))
3536 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3538 add_data.prefix = prefix;
3539 add_data.force = !!force;
3540 add_data.quiet = !!quiet;
3541 add_data.progress = !!progress;
3542 add_data.dissociate = !!dissociate;
3544 if (add_submodule(&add_data))
3545 goto cleanup;
3546 configure_added_submodule(&add_data);
3548 ret = 0;
3549 cleanup:
3550 free(add_data.sm_path);
3551 free(to_free);
3552 strbuf_release(&sb);
3554 return ret;
3557 int cmd_submodule__helper(int argc,
3558 const char **argv,
3559 const char *prefix,
3560 struct repository *repo UNUSED)
3562 parse_opt_subcommand_fn *fn = NULL;
3563 const char *const usage[] = {
3564 N_("git submodule--helper <command>"),
3565 NULL
3567 struct option options[] = {
3568 OPT_SUBCOMMAND("clone", &fn, module_clone),
3569 OPT_SUBCOMMAND("add", &fn, module_add),
3570 OPT_SUBCOMMAND("update", &fn, module_update),
3571 OPT_SUBCOMMAND("foreach", &fn, module_foreach),
3572 OPT_SUBCOMMAND("init", &fn, module_init),
3573 OPT_SUBCOMMAND("status", &fn, module_status),
3574 OPT_SUBCOMMAND("sync", &fn, module_sync),
3575 OPT_SUBCOMMAND("deinit", &fn, module_deinit),
3576 OPT_SUBCOMMAND("summary", &fn, module_summary),
3577 OPT_SUBCOMMAND("push-check", &fn, push_check),
3578 OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
3579 OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3580 OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3581 OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3582 OPT_END()
3584 argc = parse_options(argc, argv, prefix, options, usage, 0);
3586 return fn(argc, argv, prefix);