t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / builtin / submodule--helper.c
blobdc89488a7da335e7ff7f0259da6d8d131eaa009b
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 = get_default_remote();
2338 strvec_pushl(&cp.args, remote, hex, NULL);
2339 free(remote);
2342 return run_command(&cp);
2345 static int run_update_command(const struct update_data *ud, int subforce)
2347 struct child_process cp = CHILD_PROCESS_INIT;
2348 char *oid = oid_to_hex(&ud->oid);
2349 int ret;
2351 switch (ud->update_strategy.type) {
2352 case SM_UPDATE_CHECKOUT:
2353 cp.git_cmd = 1;
2354 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2355 if (subforce)
2356 strvec_push(&cp.args, "-f");
2357 break;
2358 case SM_UPDATE_REBASE:
2359 cp.git_cmd = 1;
2360 strvec_push(&cp.args, "rebase");
2361 if (ud->quiet)
2362 strvec_push(&cp.args, "--quiet");
2363 break;
2364 case SM_UPDATE_MERGE:
2365 cp.git_cmd = 1;
2366 strvec_push(&cp.args, "merge");
2367 if (ud->quiet)
2368 strvec_push(&cp.args, "--quiet");
2369 break;
2370 case SM_UPDATE_COMMAND:
2371 cp.use_shell = 1;
2372 strvec_push(&cp.args, ud->update_strategy.command);
2373 break;
2374 default:
2375 BUG("unexpected update strategy type: %d",
2376 ud->update_strategy.type);
2378 strvec_push(&cp.args, oid);
2380 cp.dir = ud->sm_path;
2381 prepare_submodule_repo_env(&cp.env);
2382 if ((ret = run_command(&cp))) {
2383 switch (ud->update_strategy.type) {
2384 case SM_UPDATE_CHECKOUT:
2385 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2386 oid, ud->displaypath);
2387 /* No "ret" assignment, use "git checkout"'s */
2388 break;
2389 case SM_UPDATE_REBASE:
2390 ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2391 oid, ud->displaypath);
2392 break;
2393 case SM_UPDATE_MERGE:
2394 ret = die_message(_("Unable to merge '%s' in submodule path '%s'"),
2395 oid, ud->displaypath);
2396 break;
2397 case SM_UPDATE_COMMAND:
2398 ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2399 ud->update_strategy.command, oid, ud->displaypath);
2400 break;
2401 default:
2402 BUG("unexpected update strategy type: %d",
2403 ud->update_strategy.type);
2406 return ret;
2409 if (ud->quiet)
2410 return 0;
2412 switch (ud->update_strategy.type) {
2413 case SM_UPDATE_CHECKOUT:
2414 printf(_("Submodule path '%s': checked out '%s'\n"),
2415 ud->displaypath, oid);
2416 break;
2417 case SM_UPDATE_REBASE:
2418 printf(_("Submodule path '%s': rebased into '%s'\n"),
2419 ud->displaypath, oid);
2420 break;
2421 case SM_UPDATE_MERGE:
2422 printf(_("Submodule path '%s': merged in '%s'\n"),
2423 ud->displaypath, oid);
2424 break;
2425 case SM_UPDATE_COMMAND:
2426 printf(_("Submodule path '%s': '%s %s'\n"),
2427 ud->displaypath, ud->update_strategy.command, oid);
2428 break;
2429 default:
2430 BUG("unexpected update strategy type: %d",
2431 ud->update_strategy.type);
2434 return 0;
2437 static int run_update_procedure(const struct update_data *ud)
2439 int subforce = is_null_oid(&ud->suboid) || ud->force;
2441 if (!ud->nofetch) {
2443 * Run fetch only if `oid` isn't present or it
2444 * is not reachable from a ref.
2446 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2447 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2448 !ud->quiet)
2449 fprintf_ln(stderr,
2450 _("Unable to fetch in submodule path '%s'; "
2451 "trying to directly fetch %s:"),
2452 ud->displaypath, oid_to_hex(&ud->oid));
2454 * Now we tried the usual fetch, but `oid` may
2455 * not be reachable from any of the refs.
2457 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2458 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2459 return die_message(_("Fetched in submodule path '%s', but it did not "
2460 "contain %s. Direct fetching of that commit failed."),
2461 ud->displaypath, oid_to_hex(&ud->oid));
2464 return run_update_command(ud, subforce);
2467 static int remote_submodule_branch(const char *path, const char **branch)
2469 const struct submodule *sub;
2470 char *key;
2471 *branch = NULL;
2473 sub = submodule_from_path(the_repository, null_oid(), path);
2474 if (!sub)
2475 return die_message(_("could not initialize submodule at path '%s'"),
2476 path);
2478 key = xstrfmt("submodule.%s.branch", sub->name);
2479 if (repo_config_get_string_tmp(the_repository, key, branch))
2480 *branch = sub->branch;
2481 free(key);
2483 if (!*branch) {
2484 *branch = "HEAD";
2485 return 0;
2488 if (!strcmp(*branch, ".")) {
2489 const char *refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2490 "HEAD", 0, NULL,
2491 NULL);
2493 if (!refname)
2494 return die_message(_("No such ref: %s"), "HEAD");
2496 /* detached HEAD */
2497 if (!strcmp(refname, "HEAD"))
2498 return die_message(_("Submodule (%s) branch configured to inherit "
2499 "branch from superproject, but the superproject "
2500 "is not on any branch"), sub->name);
2502 if (!skip_prefix(refname, "refs/heads/", &refname))
2503 return die_message(_("Expecting a full ref name, got %s"),
2504 refname);
2506 *branch = refname;
2507 return 0;
2510 /* Our "branch" is coming from repo_config_get_string_tmp() */
2511 return 0;
2514 static int ensure_core_worktree(const char *path)
2516 const char *cw;
2517 struct repository subrepo;
2519 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2520 return die_message(_("could not get a repository handle for submodule '%s'"),
2521 path);
2523 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2524 char *cfg_file, *abs_path;
2525 const char *rel_path;
2526 struct strbuf sb = STRBUF_INIT;
2528 cfg_file = repo_git_path(&subrepo, "config");
2530 abs_path = absolute_pathdup(path);
2531 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2533 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2535 free(cfg_file);
2536 free(abs_path);
2537 strbuf_release(&sb);
2540 repo_clear(&subrepo);
2541 return 0;
2544 static const char *submodule_update_type_to_label(enum submodule_update_type type)
2546 switch (type) {
2547 case SM_UPDATE_CHECKOUT:
2548 return "checkout";
2549 case SM_UPDATE_MERGE:
2550 return "merge";
2551 case SM_UPDATE_REBASE:
2552 return "rebase";
2553 case SM_UPDATE_UNSPECIFIED:
2554 case SM_UPDATE_NONE:
2555 case SM_UPDATE_COMMAND:
2556 break;
2558 BUG("unreachable with type %d", type);
2561 static void update_data_to_args(const struct update_data *update_data,
2562 struct strvec *args)
2564 enum submodule_update_type update_type = update_data->update_default;
2566 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2567 if (update_data->displaypath)
2568 strvec_pushf(args, "--super-prefix=%s/",
2569 update_data->displaypath);
2570 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2571 if (update_data->quiet)
2572 strvec_push(args, "--quiet");
2573 if (update_data->force)
2574 strvec_push(args, "--force");
2575 if (update_data->init)
2576 strvec_push(args, "--init");
2577 if (update_data->remote)
2578 strvec_push(args, "--remote");
2579 if (update_data->nofetch)
2580 strvec_push(args, "--no-fetch");
2581 if (update_data->dissociate)
2582 strvec_push(args, "--dissociate");
2583 if (update_data->progress)
2584 strvec_push(args, "--progress");
2585 if (update_data->require_init)
2586 strvec_push(args, "--require-init");
2587 if (update_data->depth)
2588 strvec_pushf(args, "--depth=%d", update_data->depth);
2589 if (update_type != SM_UPDATE_UNSPECIFIED)
2590 strvec_pushf(args, "--%s",
2591 submodule_update_type_to_label(update_type));
2593 if (update_data->references.nr) {
2594 struct string_list_item *item;
2596 for_each_string_list_item(item, &update_data->references)
2597 strvec_pushl(args, "--reference", item->string, NULL);
2599 if (update_data->ref_storage_format != REF_STORAGE_FORMAT_UNKNOWN)
2600 strvec_pushf(args, "--ref-format=%s",
2601 ref_storage_format_to_name(update_data->ref_storage_format));
2602 if (update_data->filter_options && update_data->filter_options->choice)
2603 strvec_pushf(args, "--filter=%s",
2604 expand_list_objects_filter_spec(
2605 update_data->filter_options));
2606 if (update_data->recommend_shallow == 0)
2607 strvec_push(args, "--no-recommend-shallow");
2608 else if (update_data->recommend_shallow == 1)
2609 strvec_push(args, "--recommend-shallow");
2610 if (update_data->single_branch >= 0)
2611 strvec_push(args, update_data->single_branch ?
2612 "--single-branch" :
2613 "--no-single-branch");
2616 static int update_submodule(struct update_data *update_data)
2618 int ret;
2620 if (validate_submodule_path(update_data->sm_path) < 0)
2621 return -1;
2623 ret = determine_submodule_update_strategy(the_repository,
2624 update_data->just_cloned,
2625 update_data->sm_path,
2626 update_data->update_default,
2627 &update_data->update_strategy);
2628 if (ret)
2629 return ret;
2631 if (update_data->just_cloned)
2632 oidcpy(&update_data->suboid, null_oid());
2633 else if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
2634 "HEAD", &update_data->suboid))
2635 return die_message(_("Unable to find current revision in submodule path '%s'"),
2636 update_data->displaypath);
2638 if (update_data->remote) {
2639 char *remote_name;
2640 const char *branch;
2641 char *remote_ref;
2642 int code;
2644 code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2645 if (code)
2646 return code;
2647 code = remote_submodule_branch(update_data->sm_path, &branch);
2648 if (code)
2649 return code;
2650 remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2652 free(remote_name);
2654 if (!update_data->nofetch) {
2655 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2656 0, NULL)) {
2657 free(remote_ref);
2658 return die_message(_("Unable to fetch in submodule path '%s'"),
2659 update_data->sm_path);
2663 if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
2664 remote_ref, &update_data->oid)) {
2665 ret = die_message(_("Unable to find %s revision in submodule path '%s'"),
2666 remote_ref, update_data->sm_path);
2667 free(remote_ref);
2668 return ret;
2671 free(remote_ref);
2674 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2675 ret = run_update_procedure(update_data);
2676 if (ret)
2677 return ret;
2680 if (update_data->recursive) {
2681 struct child_process cp = CHILD_PROCESS_INIT;
2682 struct update_data next = *update_data;
2684 next.prefix = NULL;
2685 oidcpy(&next.oid, null_oid());
2686 oidcpy(&next.suboid, null_oid());
2688 cp.dir = update_data->sm_path;
2689 cp.git_cmd = 1;
2690 prepare_submodule_repo_env(&cp.env);
2691 update_data_to_args(&next, &cp.args);
2693 ret = run_command(&cp);
2694 if (ret)
2695 die_message(_("Failed to recurse into submodule path '%s'"),
2696 update_data->displaypath);
2697 return ret;
2700 return 0;
2703 static int update_submodules(struct update_data *update_data)
2705 int i, ret = 0;
2706 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2707 const struct run_process_parallel_opts opts = {
2708 .tr2_category = "submodule",
2709 .tr2_label = "parallel/update",
2711 .processes = update_data->max_jobs,
2713 .get_next_task = update_clone_get_next_task,
2714 .start_failure = update_clone_start_failure,
2715 .task_finished = update_clone_task_finished,
2716 .data = &suc,
2719 suc.update_data = update_data;
2720 run_processes_parallel(&opts);
2723 * We saved the output and put it out all at once now.
2724 * That means:
2725 * - the listener does not have to interleave their (checkout)
2726 * work with our fetching. The writes involved in a
2727 * checkout involve more straightforward sequential I/O.
2728 * - the listener can avoid doing any work if fetching failed.
2730 if (suc.quickstop) {
2731 ret = 1;
2732 goto cleanup;
2735 for (i = 0; i < suc.update_clone_nr; i++) {
2736 struct update_clone_data ucd = suc.update_clone[i];
2737 int code = 128;
2739 oidcpy(&update_data->oid, &ucd.oid);
2740 update_data->just_cloned = ucd.just_cloned;
2741 update_data->sm_path = ucd.sub->path;
2744 * Verify that the submodule path does not contain any
2745 * symlinks; if it does, it might have been tampered with.
2746 * TODO: allow exempting it via
2747 * `safe.submodule.path` or something
2749 if (validate_submodule_path(update_data->sm_path) < 0)
2750 goto fail;
2752 code = ensure_core_worktree(update_data->sm_path);
2753 if (code)
2754 goto fail;
2756 update_data->displaypath = get_submodule_displaypath(
2757 update_data->sm_path, update_data->prefix,
2758 update_data->super_prefix);
2759 code = update_submodule(update_data);
2760 FREE_AND_NULL(update_data->displaypath);
2761 fail:
2762 if (!code)
2763 continue;
2764 ret = code;
2765 if (ret == 128)
2766 goto cleanup;
2769 cleanup:
2770 submodule_update_clone_release(&suc);
2771 string_list_clear(&update_data->references, 0);
2772 return ret;
2775 static int module_update(int argc, const char **argv, const char *prefix)
2777 struct pathspec pathspec = { 0 };
2778 struct pathspec pathspec2 = { 0 };
2779 struct update_data opt = UPDATE_DATA_INIT;
2780 struct list_objects_filter_options filter_options =
2781 LIST_OBJECTS_FILTER_INIT;
2782 const char *ref_storage_format = NULL;
2783 int ret;
2784 struct option module_update_options[] = {
2785 OPT__SUPER_PREFIX(&opt.super_prefix),
2786 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2787 OPT_BOOL(0, "init", &opt.init,
2788 N_("initialize uninitialized submodules before update")),
2789 OPT_BOOL(0, "remote", &opt.remote,
2790 N_("use SHA-1 of submodule's remote tracking branch")),
2791 OPT_BOOL(0, "recursive", &opt.recursive,
2792 N_("traverse submodules recursively")),
2793 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2794 N_("don't fetch new objects from the remote site")),
2795 OPT_SET_INT(0, "checkout", &opt.update_default,
2796 N_("use the 'checkout' update strategy (default)"),
2797 SM_UPDATE_CHECKOUT),
2798 OPT_SET_INT('m', "merge", &opt.update_default,
2799 N_("use the 'merge' update strategy"),
2800 SM_UPDATE_MERGE),
2801 OPT_SET_INT('r', "rebase", &opt.update_default,
2802 N_("use the 'rebase' update strategy"),
2803 SM_UPDATE_REBASE),
2804 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2805 N_("reference repository")),
2806 OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
2807 N_("specify the reference format to use")),
2808 OPT_BOOL(0, "dissociate", &opt.dissociate,
2809 N_("use --reference only while cloning")),
2810 OPT_INTEGER(0, "depth", &opt.depth,
2811 N_("create a shallow clone truncated to the "
2812 "specified number of revisions")),
2813 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2814 N_("parallel jobs")),
2815 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2816 N_("whether the initial clone should follow the shallow recommendation")),
2817 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2818 OPT_BOOL(0, "progress", &opt.progress,
2819 N_("force cloning progress")),
2820 OPT_BOOL(0, "require-init", &opt.require_init,
2821 N_("disallow cloning into non-empty directory, implies --init")),
2822 OPT_BOOL(0, "single-branch", &opt.single_branch,
2823 N_("clone only one branch, HEAD or --branch")),
2824 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2825 OPT_END()
2827 const char *const git_submodule_helper_usage[] = {
2828 N_("git submodule [--quiet] update"
2829 " [--init [--filter=<filter-spec>]] [--remote]"
2830 " [-N|--no-fetch] [-f|--force]"
2831 " [--checkout|--merge|--rebase]"
2832 " [--[no-]recommend-shallow] [--reference <repository>]"
2833 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2834 NULL
2837 update_clone_config_from_gitmodules(&opt.max_jobs);
2838 git_config(git_update_clone_config, &opt.max_jobs);
2840 argc = parse_options(argc, argv, prefix, module_update_options,
2841 git_submodule_helper_usage, 0);
2843 if (opt.require_init)
2844 opt.init = 1;
2846 if (filter_options.choice && !opt.init) {
2847 usage_with_options(git_submodule_helper_usage,
2848 module_update_options);
2851 if (ref_storage_format) {
2852 opt.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
2853 if (opt.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
2854 die(_("unknown ref storage format '%s'"), ref_storage_format);
2857 opt.filter_options = &filter_options;
2858 opt.prefix = prefix;
2860 if (opt.update_default)
2861 opt.update_strategy.type = opt.update_default;
2863 if (module_list_compute(argv, prefix, &pathspec, &opt.list) < 0) {
2864 ret = 1;
2865 goto cleanup;
2868 if (pathspec.nr)
2869 opt.warn_if_uninitialized = 1;
2871 if (opt.init) {
2872 struct module_list list = MODULE_LIST_INIT;
2873 struct init_cb info = INIT_CB_INIT;
2875 if (module_list_compute(argv, opt.prefix,
2876 &pathspec2, &list) < 0) {
2877 module_list_release(&list);
2878 ret = 1;
2879 goto cleanup;
2883 * If there are no path args and submodule.active is set then,
2884 * by default, only initialize 'active' modules.
2886 if (!argc && !git_config_get("submodule.active"))
2887 module_list_active(&list);
2889 info.prefix = opt.prefix;
2890 info.super_prefix = opt.super_prefix;
2891 if (opt.quiet)
2892 info.flags |= OPT_QUIET;
2894 for_each_listed_submodule(&list, init_submodule_cb, &info);
2895 module_list_release(&list);
2898 ret = update_submodules(&opt);
2899 cleanup:
2900 update_data_release(&opt);
2901 list_objects_filter_release(&filter_options);
2902 clear_pathspec(&pathspec);
2903 clear_pathspec(&pathspec2);
2904 return ret;
2907 static int push_check(int argc, const char **argv, const char *prefix UNUSED)
2909 struct remote *remote;
2910 const char *superproject_head;
2911 char *head;
2912 int detached_head = 0;
2913 struct object_id head_oid;
2915 if (argc < 3)
2916 die("submodule--helper push-check requires at least 2 arguments");
2919 * superproject's resolved head ref.
2920 * if HEAD then the superproject is in a detached head state, otherwise
2921 * it will be the resolved head ref.
2923 superproject_head = argv[1];
2924 argv++;
2925 argc--;
2926 /* Get the submodule's head ref and determine if it is detached */
2927 head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
2928 0, &head_oid, NULL);
2929 if (!head)
2930 die(_("Failed to resolve HEAD as a valid ref."));
2931 if (!strcmp(head, "HEAD"))
2932 detached_head = 1;
2935 * The remote must be configured.
2936 * This is to avoid pushing to the exact same URL as the parent.
2938 remote = pushremote_get(argv[1]);
2939 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2940 die("remote '%s' not configured", argv[1]);
2942 /* Check the refspec */
2943 if (argc > 2) {
2944 int i;
2945 struct ref *local_refs = get_local_heads();
2946 struct refspec refspec = REFSPEC_INIT_PUSH;
2948 refspec_appendn(&refspec, argv + 2, argc - 2);
2950 for (i = 0; i < refspec.nr; i++) {
2951 const struct refspec_item *rs = &refspec.items[i];
2953 if (rs->pattern || rs->matching)
2954 continue;
2956 /* LHS must match a single ref */
2957 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2958 case 1:
2959 break;
2960 case 0:
2962 * If LHS matches 'HEAD' then we need to ensure
2963 * that it matches the same named branch
2964 * checked out in the superproject.
2966 if (!strcmp(rs->src, "HEAD")) {
2967 if (!detached_head &&
2968 !strcmp(head, superproject_head))
2969 break;
2970 die("HEAD does not match the named branch in the superproject");
2972 /* fallthrough */
2973 default:
2974 die("src refspec '%s' must name a ref",
2975 rs->src);
2979 refspec_clear(&refspec);
2980 free_refs(local_refs);
2982 free(head);
2984 return 0;
2987 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2989 int i;
2990 struct pathspec pathspec = { 0 };
2991 struct module_list list = MODULE_LIST_INIT;
2992 const char *super_prefix = NULL;
2993 struct option embed_gitdir_options[] = {
2994 OPT__SUPER_PREFIX(&super_prefix),
2995 OPT_END()
2997 const char *const git_submodule_helper_usage[] = {
2998 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2999 NULL
3001 int ret = 1;
3003 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
3004 git_submodule_helper_usage, 0);
3006 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
3007 goto cleanup;
3009 for (i = 0; i < list.nr; i++)
3010 absorb_git_dir_into_superproject(list.entries[i]->name,
3011 super_prefix);
3013 ret = 0;
3014 cleanup:
3015 clear_pathspec(&pathspec);
3016 module_list_release(&list);
3017 return ret;
3020 static int module_set_url(int argc, const char **argv, const char *prefix)
3022 int quiet = 0, ret;
3023 const char *newurl;
3024 const char *path;
3025 char *config_name;
3026 struct option options[] = {
3027 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
3028 OPT_END()
3030 const char *const usage[] = {
3031 N_("git submodule set-url [--quiet] <path> <newurl>"),
3032 NULL
3034 const struct submodule *sub;
3036 argc = parse_options(argc, argv, prefix, options, usage, 0);
3038 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
3039 usage_with_options(usage, options);
3041 sub = submodule_from_path(the_repository, null_oid(), path);
3043 if (!sub)
3044 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3045 path);
3047 config_name = xstrfmt("submodule.%s.url", sub->name);
3048 ret = config_set_in_gitmodules_file_gently(config_name, newurl);
3050 if (!ret) {
3051 repo_read_gitmodules(the_repository, 0);
3052 sync_submodule(sub->path, prefix, NULL, quiet ? OPT_QUIET : 0);
3055 free(config_name);
3056 return !!ret;
3059 static int module_set_branch(int argc, const char **argv, const char *prefix)
3061 int opt_default = 0, ret;
3062 const char *opt_branch = NULL;
3063 const char *path;
3064 char *config_name;
3065 struct option options[] = {
3067 * We accept the `quiet` option for uniformity across subcommands,
3068 * though there is nothing to make less verbose in this subcommand.
3070 OPT_NOOP_NOARG('q', "quiet"),
3072 OPT_BOOL('d', "default", &opt_default,
3073 N_("set the default tracking branch to master")),
3074 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
3075 N_("set the default tracking branch")),
3076 OPT_END()
3078 const char *const usage[] = {
3079 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3080 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3081 NULL
3083 const struct submodule *sub;
3085 argc = parse_options(argc, argv, prefix, options, usage, 0);
3087 if (!opt_branch && !opt_default)
3088 die(_("--branch or --default required"));
3090 if (opt_branch && opt_default)
3091 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3093 if (argc != 1 || !(path = argv[0]))
3094 usage_with_options(usage, options);
3096 sub = submodule_from_path(the_repository, null_oid(), path);
3098 if (!sub)
3099 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3100 path);
3102 config_name = xstrfmt("submodule.%s.branch", sub->name);
3103 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
3105 free(config_name);
3106 return !!ret;
3109 static int module_create_branch(int argc, const char **argv, const char *prefix)
3111 enum branch_track track;
3112 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
3113 struct option options[] = {
3114 OPT__QUIET(&quiet, N_("print only error messages")),
3115 OPT__FORCE(&force, N_("force creation"), 0),
3116 OPT_BOOL(0, "create-reflog", &reflog,
3117 N_("create the branch's reflog")),
3118 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
3119 N_("set branch tracking configuration"),
3120 PARSE_OPT_OPTARG,
3121 parse_opt_tracking_mode),
3122 OPT__DRY_RUN(&dry_run,
3123 N_("show whether the branch would be created")),
3124 OPT_END()
3126 const char *const usage[] = {
3127 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3128 NULL
3131 git_config(git_default_config, NULL);
3132 track = git_branch_track;
3133 argc = parse_options(argc, argv, prefix, options, usage, 0);
3135 if (argc != 3)
3136 usage_with_options(usage, options);
3138 if (!quiet && !dry_run)
3139 printf_ln(_("creating branch '%s'"), argv[0]);
3141 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
3142 force, reflog, quiet, track, dry_run);
3143 return 0;
3146 struct add_data {
3147 const char *prefix;
3148 const char *branch;
3149 const char *reference_path;
3150 char *sm_path;
3151 const char *sm_name;
3152 const char *repo;
3153 const char *realrepo;
3154 enum ref_storage_format ref_storage_format;
3155 int depth;
3156 unsigned int force: 1;
3157 unsigned int quiet: 1;
3158 unsigned int progress: 1;
3159 unsigned int dissociate: 1;
3161 #define ADD_DATA_INIT { \
3162 .depth = -1, \
3163 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
3166 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3168 struct child_process cp_remote = CHILD_PROCESS_INIT;
3169 struct strbuf sb_remote_out = STRBUF_INIT;
3171 cp_remote.git_cmd = 1;
3172 strvec_pushf(&cp_remote.env,
3173 "GIT_DIR=%s", git_dir_path);
3174 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3175 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3176 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3177 char *next_line;
3178 char *line = sb_remote_out.buf;
3180 while ((next_line = strchr(line, '\n')) != NULL) {
3181 size_t len = next_line - line;
3183 if (strip_suffix_mem(line, &len, " (fetch)"))
3184 strbuf_addf(msg, " %.*s\n", (int)len, line);
3185 line = next_line + 1;
3189 strbuf_release(&sb_remote_out);
3192 static int add_submodule(const struct add_data *add_data)
3194 char *submod_gitdir_path;
3195 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3196 struct string_list reference = STRING_LIST_INIT_NODUP;
3197 int ret = -1;
3199 /* perhaps the path already exists and is already a git repo, else clone it */
3200 if (is_directory(add_data->sm_path)) {
3201 struct strbuf sm_path = STRBUF_INIT;
3202 strbuf_addstr(&sm_path, add_data->sm_path);
3203 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3204 if (is_nonbare_repository_dir(&sm_path))
3205 printf(_("Adding existing repo at '%s' to the index\n"),
3206 add_data->sm_path);
3207 else
3208 die(_("'%s' already exists and is not a valid git repo"),
3209 add_data->sm_path);
3210 strbuf_release(&sm_path);
3211 free(submod_gitdir_path);
3212 } else {
3213 struct child_process cp = CHILD_PROCESS_INIT;
3215 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3217 if (is_directory(submod_gitdir_path)) {
3218 if (!add_data->force) {
3219 struct strbuf msg = STRBUF_INIT;
3220 char *die_msg;
3222 strbuf_addf(&msg, _("A git directory for '%s' is found "
3223 "locally with remote(s):\n"),
3224 add_data->sm_name);
3226 append_fetch_remotes(&msg, submod_gitdir_path);
3227 free(submod_gitdir_path);
3229 strbuf_addf(&msg, _("If you want to reuse this local git "
3230 "directory instead of cloning again from\n"
3231 " %s\n"
3232 "use the '--force' option. If the local git "
3233 "directory is not the correct repo\n"
3234 "or you are unsure what this means choose "
3235 "another name with the '--name' option."),
3236 add_data->realrepo);
3238 die_msg = strbuf_detach(&msg, NULL);
3239 die("%s", die_msg);
3240 } else {
3241 printf(_("Reactivating local git directory for "
3242 "submodule '%s'\n"), add_data->sm_name);
3245 free(submod_gitdir_path);
3247 clone_data.prefix = add_data->prefix;
3248 clone_data.path = add_data->sm_path;
3249 clone_data.name = add_data->sm_name;
3250 clone_data.url = add_data->realrepo;
3251 clone_data.quiet = add_data->quiet;
3252 clone_data.progress = add_data->progress;
3253 if (add_data->reference_path) {
3254 char *p = xstrdup(add_data->reference_path);
3256 string_list_append(&reference, p)->util = p;
3258 clone_data.ref_storage_format = add_data->ref_storage_format;
3259 clone_data.dissociate = add_data->dissociate;
3260 if (add_data->depth >= 0)
3261 clone_data.depth = add_data->depth;
3263 if (clone_submodule(&clone_data, &reference))
3264 goto cleanup;
3266 prepare_submodule_repo_env(&cp.env);
3267 cp.git_cmd = 1;
3268 cp.dir = add_data->sm_path;
3270 * NOTE: we only get here if add_data->force is true, so
3271 * passing --force to checkout is reasonable.
3273 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3275 if (add_data->branch) {
3276 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3277 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3280 if (run_command(&cp))
3281 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3283 ret = 0;
3285 cleanup:
3286 string_list_clear(&reference, 1);
3287 return ret;
3290 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3292 char *key;
3293 int ret;
3295 if (!is_writing_gitmodules_ok())
3296 die(_("please make sure that the .gitmodules file is in the working tree"));
3298 key = xstrfmt("submodule.%s.%s", name, var);
3299 ret = config_set_in_gitmodules_file_gently(key, value);
3300 free(key);
3302 return ret;
3305 static void configure_added_submodule(struct add_data *add_data)
3307 char *key;
3308 struct child_process add_submod = CHILD_PROCESS_INIT;
3309 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3311 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3312 git_config_set_gently(key, add_data->realrepo);
3313 free(key);
3315 add_submod.git_cmd = 1;
3316 strvec_pushl(&add_submod.args, "add",
3317 "--no-warn-embedded-repo", NULL);
3318 if (add_data->force)
3319 strvec_push(&add_submod.args, "--force");
3320 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3322 if (run_command(&add_submod))
3323 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3325 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3326 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3327 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3329 if (add_data->branch) {
3330 if (config_submodule_in_gitmodules(add_data->sm_name,
3331 "branch", add_data->branch))
3332 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3335 add_gitmodules.git_cmd = 1;
3336 strvec_pushl(&add_gitmodules.args,
3337 "add", "--force", "--", ".gitmodules", NULL);
3339 if (run_command(&add_gitmodules))
3340 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3343 * NEEDSWORK: In a multi-working-tree world this needs to be
3344 * set in the per-worktree config.
3347 * NEEDSWORK: In the longer run, we need to get rid of this
3348 * pattern of querying "submodule.active" before calling
3349 * is_submodule_active(), since that function needs to find
3350 * out the value of "submodule.active" again anyway.
3352 if (!git_config_get("submodule.active")) {
3354 * If the submodule being added isn't already covered by the
3355 * current configured pathspec, set the submodule's active flag
3357 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3358 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3359 git_config_set_gently(key, "true");
3360 free(key);
3362 } else {
3363 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3364 git_config_set_gently(key, "true");
3365 free(key);
3369 static void die_on_index_match(const char *path, int force)
3371 struct pathspec ps;
3372 const char *args[] = { path, NULL };
3373 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3375 if (repo_read_index_preload(the_repository, NULL, 0) < 0)
3376 die(_("index file corrupt"));
3378 if (ps.nr) {
3379 int i;
3380 char *ps_matched = xcalloc(ps.nr, 1);
3382 /* TODO: audit for interaction with sparse-index. */
3383 ensure_full_index(the_repository->index);
3386 * Since there is only one pathspec, we just need to
3387 * check ps_matched[0] to know if a cache entry matched.
3389 for (i = 0; i < the_repository->index->cache_nr; i++) {
3390 ce_path_match(the_repository->index, the_repository->index->cache[i], &ps,
3391 ps_matched);
3393 if (ps_matched[0]) {
3394 if (!force)
3395 die(_("'%s' already exists in the index"),
3396 path);
3397 if (!S_ISGITLINK(the_repository->index->cache[i]->ce_mode))
3398 die(_("'%s' already exists in the index "
3399 "and is not a submodule"), path);
3400 break;
3403 free(ps_matched);
3405 clear_pathspec(&ps);
3408 static void die_on_repo_without_commits(const char *path)
3410 struct strbuf sb = STRBUF_INIT;
3411 strbuf_addstr(&sb, path);
3412 if (is_nonbare_repository_dir(&sb)) {
3413 struct object_id oid;
3414 if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
3415 die(_("'%s' does not have a commit checked out"), path);
3417 strbuf_release(&sb);
3420 static int module_add(int argc, const char **argv, const char *prefix)
3422 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3423 struct add_data add_data = ADD_DATA_INIT;
3424 const char *ref_storage_format = NULL;
3425 char *to_free = NULL;
3426 struct option options[] = {
3427 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3428 N_("branch of repository to add as submodule")),
3429 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3430 PARSE_OPT_NOCOMPLETE),
3431 OPT__QUIET(&quiet, N_("print only error messages")),
3432 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3433 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3434 N_("reference repository")),
3435 OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
3436 N_("specify the reference format to use")),
3437 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3438 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3439 N_("sets the submodule's name to the given string "
3440 "instead of defaulting to its path")),
3441 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3442 OPT_END()
3444 const char *const usage[] = {
3445 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3446 NULL
3448 struct strbuf sb = STRBUF_INIT;
3449 int ret = 1;
3451 argc = parse_options(argc, argv, prefix, options, usage, 0);
3453 if (!is_writing_gitmodules_ok())
3454 die(_("please make sure that the .gitmodules file is in the working tree"));
3456 if (prefix && *prefix &&
3457 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3458 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3460 if (argc == 0 || argc > 2)
3461 usage_with_options(usage, options);
3463 if (ref_storage_format) {
3464 add_data.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
3465 if (add_data.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
3466 die(_("unknown ref storage format '%s'"), ref_storage_format);
3469 add_data.repo = argv[0];
3470 if (argc == 1)
3471 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3472 else
3473 add_data.sm_path = xstrdup(argv[1]);
3475 if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
3476 char *sm_path = add_data.sm_path;
3478 add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
3479 free(sm_path);
3482 if (starts_with_dot_dot_slash(add_data.repo) ||
3483 starts_with_dot_slash(add_data.repo)) {
3484 if (prefix)
3485 die(_("Relative path can only be used from the toplevel "
3486 "of the working tree"));
3488 /* dereference source url relative to parent's url */
3489 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3490 add_data.realrepo = to_free;
3491 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3492 add_data.realrepo = add_data.repo;
3493 } else {
3494 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3495 add_data.repo);
3499 * normalize path:
3500 * multiple //; leading ./; /./; /../;
3502 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3503 strip_dir_trailing_slashes(add_data.sm_path);
3505 if (validate_submodule_path(add_data.sm_path) < 0)
3506 exit(128);
3508 die_on_index_match(add_data.sm_path, force);
3509 die_on_repo_without_commits(add_data.sm_path);
3511 if (!force) {
3512 struct child_process cp = CHILD_PROCESS_INIT;
3514 cp.git_cmd = 1;
3515 cp.no_stdout = 1;
3516 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3517 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3518 if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3519 strbuf_complete_line(&sb);
3520 fputs(sb.buf, stderr);
3521 goto cleanup;
3525 if(!add_data.sm_name)
3526 add_data.sm_name = add_data.sm_path;
3528 if (check_submodule_name(add_data.sm_name))
3529 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3531 add_data.prefix = prefix;
3532 add_data.force = !!force;
3533 add_data.quiet = !!quiet;
3534 add_data.progress = !!progress;
3535 add_data.dissociate = !!dissociate;
3537 if (add_submodule(&add_data))
3538 goto cleanup;
3539 configure_added_submodule(&add_data);
3541 ret = 0;
3542 cleanup:
3543 free(add_data.sm_path);
3544 free(to_free);
3545 strbuf_release(&sb);
3547 return ret;
3550 int cmd_submodule__helper(int argc,
3551 const char **argv,
3552 const char *prefix,
3553 struct repository *repo UNUSED)
3555 parse_opt_subcommand_fn *fn = NULL;
3556 const char *const usage[] = {
3557 N_("git submodule--helper <command>"),
3558 NULL
3560 struct option options[] = {
3561 OPT_SUBCOMMAND("clone", &fn, module_clone),
3562 OPT_SUBCOMMAND("add", &fn, module_add),
3563 OPT_SUBCOMMAND("update", &fn, module_update),
3564 OPT_SUBCOMMAND("foreach", &fn, module_foreach),
3565 OPT_SUBCOMMAND("init", &fn, module_init),
3566 OPT_SUBCOMMAND("status", &fn, module_status),
3567 OPT_SUBCOMMAND("sync", &fn, module_sync),
3568 OPT_SUBCOMMAND("deinit", &fn, module_deinit),
3569 OPT_SUBCOMMAND("summary", &fn, module_summary),
3570 OPT_SUBCOMMAND("push-check", &fn, push_check),
3571 OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
3572 OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3573 OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3574 OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3575 OPT_END()
3577 argc = parse_options(argc, argv, prefix, options, usage, 0);
3579 return fn(argc, argv, prefix);