1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
3 #include "repository.h"
6 #include "parse-options.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
21 #include "object-store.h"
25 #define OPT_QUIET (1 << 0)
26 #define OPT_CACHED (1 << 1)
27 #define OPT_RECURSIVE (1 << 2)
28 #define OPT_FORCE (1 << 3)
30 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
33 static char *get_default_remote(void)
35 char *dest = NULL, *ret;
36 struct strbuf sb = STRBUF_INIT;
37 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
40 die(_("No such ref: %s"), "HEAD");
43 if (!strcmp(refname, "HEAD"))
44 return xstrdup("origin");
46 if (!skip_prefix(refname, "refs/heads/", &refname))
47 die(_("Expecting a full ref name, got %s"), refname);
49 strbuf_addf(&sb, "branch.%s.remote", refname);
50 if (git_config_get_string(sb.buf, &dest))
51 ret = xstrdup("origin");
59 static int print_default_remote(int argc, const char **argv, const char *prefix)
64 die(_("submodule--helper print-default-remote takes no arguments"));
66 remote = get_default_remote();
68 printf("%s\n", remote);
74 static int starts_with_dot_slash(const char *str)
76 return str[0] == '.' && is_dir_sep(str[1]);
79 static int starts_with_dot_dot_slash(const char *str)
81 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
85 * Returns 1 if it was the last chop before ':'.
87 static int chop_last_dir(char **remoteurl, int is_relative)
89 char *rfind = find_last_dir_sep(*remoteurl);
95 rfind = strrchr(*remoteurl, ':');
101 if (is_relative || !strcmp(".", *remoteurl))
102 die(_("cannot strip one component off url '%s'"),
106 *remoteurl = xstrdup(".");
111 * The `url` argument is the URL that navigates to the submodule origin
112 * repo. When relative, this URL is relative to the superproject origin
113 * URL repo. The `up_path` argument, if specified, is the relative
114 * path that navigates from the submodule working tree to the superproject
115 * working tree. Returns the origin URL of the submodule.
117 * Return either an absolute URL or filesystem path (if the superproject
118 * origin URL is an absolute URL or filesystem path, respectively) or a
119 * relative file system path (if the superproject origin URL is a relative
122 * When the output is a relative file system path, the path is either
123 * relative to the submodule working tree, if up_path is specified, or to
124 * the superproject working tree otherwise.
126 * NEEDSWORK: This works incorrectly on the domain and protocol part.
127 * remote_url url outcome expectation
128 * http://a.com/b ../c http://a.com/c as is
129 * http://a.com/b/ ../c http://a.com/c same as previous line, but
130 * ignore trailing slash in url
131 * http://a.com/b ../../c http://c error out
132 * http://a.com/b ../../../c http:/c error out
133 * http://a.com/b ../../../../c http:c error out
134 * http://a.com/b ../../../../../c .:c error out
135 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
136 * when a local part has a colon in its path component, too.
138 static char *relative_url(const char *remote_url,
145 char *remoteurl = xstrdup(remote_url);
146 struct strbuf sb = STRBUF_INIT;
147 size_t len = strlen(remoteurl);
149 if (is_dir_sep(remoteurl[len-1]))
150 remoteurl[len-1] = '\0';
152 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
157 * Prepend a './' to ensure all relative
158 * remoteurls start with './' or '../'
160 if (!starts_with_dot_slash(remoteurl) &&
161 !starts_with_dot_dot_slash(remoteurl)) {
163 strbuf_addf(&sb, "./%s", remoteurl);
165 remoteurl = strbuf_detach(&sb, NULL);
169 * When the url starts with '../', remove that and the
170 * last directory in remoteurl.
173 if (starts_with_dot_dot_slash(url)) {
175 colonsep |= chop_last_dir(&remoteurl, is_relative);
176 } else if (starts_with_dot_slash(url))
182 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
183 if (ends_with(url, "/"))
184 strbuf_setlen(&sb, sb.len - 1);
187 if (starts_with_dot_slash(sb.buf))
188 out = xstrdup(sb.buf + 2);
190 out = xstrdup(sb.buf);
193 if (!up_path || !is_relative)
196 strbuf_addf(&sb, "%s%s", up_path, out);
198 return strbuf_detach(&sb, NULL);
201 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
203 char *remoteurl = NULL;
204 char *remote = get_default_remote();
205 const char *up_path = NULL;
208 struct strbuf sb = STRBUF_INIT;
210 if (argc != 2 && argc != 3)
211 die("resolve-relative-url only accepts one or two arguments");
214 strbuf_addf(&sb, "remote.%s.url", remote);
217 if (git_config_get_string(sb.buf, &remoteurl))
218 /* the repository is its own authoritative upstream */
219 remoteurl = xgetcwd();
224 res = relative_url(remoteurl, url, up_path);
231 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
233 char *remoteurl, *res;
234 const char *up_path, *url;
237 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
240 remoteurl = xstrdup(argv[2]);
243 if (!strcmp(up_path, "(null)"))
246 res = relative_url(remoteurl, url, up_path);
253 /* the result should be freed by the caller. */
254 static char *get_submodule_displaypath(const char *path, const char *prefix)
256 const char *super_prefix = get_super_prefix();
258 if (prefix && super_prefix) {
259 BUG("cannot have prefix '%s' and superprefix '%s'",
260 prefix, super_prefix);
262 struct strbuf sb = STRBUF_INIT;
263 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
266 } else if (super_prefix) {
267 return xstrfmt("%s%s", super_prefix, path);
269 return xstrdup(path);
273 static char *compute_rev_name(const char *sub_path, const char* object_id)
275 struct strbuf sb = STRBUF_INIT;
278 static const char *describe_bare[] = { NULL };
280 static const char *describe_tags[] = { "--tags", NULL };
282 static const char *describe_contains[] = { "--contains", NULL };
284 static const char *describe_all_always[] = { "--all", "--always", NULL };
286 static const char **describe_argv[] = { describe_bare, describe_tags,
288 describe_all_always, NULL };
290 for (d = describe_argv; *d; d++) {
291 struct child_process cp = CHILD_PROCESS_INIT;
292 prepare_submodule_repo_env(&cp.env_array);
297 argv_array_push(&cp.args, "describe");
298 argv_array_pushv(&cp.args, *d);
299 argv_array_push(&cp.args, object_id);
301 if (!capture_command(&cp, &sb, 0)) {
302 strbuf_strip_suffix(&sb, "\n");
303 return strbuf_detach(&sb, NULL);
312 const struct cache_entry **entries;
315 #define MODULE_LIST_INIT { NULL, 0, 0 }
317 static int module_list_compute(int argc, const char **argv,
319 struct pathspec *pathspec,
320 struct module_list *list)
323 char *ps_matched = NULL;
324 parse_pathspec(pathspec, 0,
325 PATHSPEC_PREFER_FULL,
329 ps_matched = xcalloc(pathspec->nr, 1);
331 if (read_cache() < 0)
332 die(_("index file corrupt"));
334 for (i = 0; i < active_nr; i++) {
335 const struct cache_entry *ce = active_cache[i];
337 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
339 !S_ISGITLINK(ce->ce_mode))
342 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
343 list->entries[list->nr++] = ce;
344 while (i + 1 < active_nr &&
345 !strcmp(ce->name, active_cache[i + 1]->name))
347 * Skip entries with the same name in different stages
348 * to make sure an entry is returned only once.
353 if (ps_matched && report_path_error(ps_matched, pathspec))
361 static void module_list_active(struct module_list *list)
364 struct module_list active_modules = MODULE_LIST_INIT;
366 for (i = 0; i < list->nr; i++) {
367 const struct cache_entry *ce = list->entries[i];
369 if (!is_submodule_active(the_repository, ce->name))
372 ALLOC_GROW(active_modules.entries,
373 active_modules.nr + 1,
374 active_modules.alloc);
375 active_modules.entries[active_modules.nr++] = ce;
379 *list = active_modules;
382 static char *get_up_path(const char *path)
385 struct strbuf sb = STRBUF_INIT;
387 for (i = count_slashes(path); i; i--)
388 strbuf_addstr(&sb, "../");
391 * Check if 'path' ends with slash or not
392 * for having the same output for dir/sub_dir
395 if (!is_dir_sep(path[strlen(path) - 1]))
396 strbuf_addstr(&sb, "../");
398 return strbuf_detach(&sb, NULL);
401 static int module_list(int argc, const char **argv, const char *prefix)
404 struct pathspec pathspec;
405 struct module_list list = MODULE_LIST_INIT;
407 struct option module_list_options[] = {
408 OPT_STRING(0, "prefix", &prefix,
410 N_("alternative anchor for relative paths")),
414 const char *const git_submodule_helper_usage[] = {
415 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
419 argc = parse_options(argc, argv, prefix, module_list_options,
420 git_submodule_helper_usage, 0);
422 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
425 for (i = 0; i < list.nr; i++) {
426 const struct cache_entry *ce = list.entries[i];
429 printf("%06o %s U\t", ce->ce_mode, oid_to_hex(&null_oid));
431 printf("%06o %s %d\t", ce->ce_mode,
432 oid_to_hex(&ce->oid), ce_stage(ce));
434 fprintf(stdout, "%s\n", ce->name);
439 static void for_each_listed_submodule(const struct module_list *list,
440 each_submodule_fn fn, void *cb_data)
443 for (i = 0; i < list->nr; i++)
444 fn(list->entries[i], cb_data);
454 #define CB_FOREACH_INIT { 0 }
456 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
459 struct cb_foreach *info = cb_data;
460 const char *path = list_item->name;
461 const struct object_id *ce_oid = &list_item->oid;
463 const struct submodule *sub;
464 struct child_process cp = CHILD_PROCESS_INIT;
467 displaypath = get_submodule_displaypath(path, info->prefix);
469 sub = submodule_from_path(the_repository, &null_oid, path);
472 die(_("No url found for submodule path '%s' in .gitmodules"),
475 if (!is_submodule_populated_gently(path, NULL))
478 prepare_submodule_repo_env(&cp.env_array);
481 * For the purpose of executing <command> in the submodule,
482 * separate shell is used for the purpose of running the
489 * NEEDSWORK: the command currently has access to the variables $name,
490 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
491 * contains a single argument. This is done for maintaining a faithful
492 * translation from shell script.
494 if (info->argc == 1) {
495 char *toplevel = xgetcwd();
496 struct strbuf sb = STRBUF_INIT;
498 argv_array_pushf(&cp.env_array, "name=%s", sub->name);
499 argv_array_pushf(&cp.env_array, "sm_path=%s", path);
500 argv_array_pushf(&cp.env_array, "displaypath=%s", displaypath);
501 argv_array_pushf(&cp.env_array, "sha1=%s",
503 argv_array_pushf(&cp.env_array, "toplevel=%s", toplevel);
506 * Since the path variable was accessible from the script
507 * before porting, it is also made available after porting.
508 * The environment variable "PATH" has a very special purpose
509 * on windows. And since environment variables are
510 * case-insensitive in windows, it interferes with the
511 * existing PATH variable. Hence, to avoid that, we expose
512 * path via the args argv_array and not via env_array.
514 sq_quote_buf(&sb, path);
515 argv_array_pushf(&cp.args, "path=%s; %s",
516 sb.buf, info->argv[0]);
520 argv_array_pushv(&cp.args, info->argv);
524 printf(_("Entering '%s'\n"), displaypath);
526 if (info->argv[0] && run_command(&cp))
527 die(_("run_command returned non-zero status for %s\n."),
530 if (info->recursive) {
531 struct child_process cpr = CHILD_PROCESS_INIT;
535 prepare_submodule_repo_env(&cpr.env_array);
537 argv_array_pushl(&cpr.args, "--super-prefix", NULL);
538 argv_array_pushf(&cpr.args, "%s/", displaypath);
539 argv_array_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
543 argv_array_push(&cpr.args, "--quiet");
545 argv_array_push(&cpr.args, "--");
546 argv_array_pushv(&cpr.args, info->argv);
548 if (run_command(&cpr))
549 die(_("run_command returned non-zero status while "
550 "recursing in the nested submodules of %s\n."),
558 static int module_foreach(int argc, const char **argv, const char *prefix)
560 struct cb_foreach info = CB_FOREACH_INIT;
561 struct pathspec pathspec;
562 struct module_list list = MODULE_LIST_INIT;
564 struct option module_foreach_options[] = {
565 OPT__QUIET(&info.quiet, N_("Suppress output of entering each submodule command")),
566 OPT_BOOL(0, "recursive", &info.recursive,
567 N_("Recurse into nested submodules")),
571 const char *const git_submodule_helper_usage[] = {
572 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
576 argc = parse_options(argc, argv, prefix, module_foreach_options,
577 git_submodule_helper_usage, 0);
579 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
584 info.prefix = prefix;
586 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
591 static char *compute_submodule_clone_url(const char *rel_url)
593 char *remoteurl, *relurl;
594 char *remote = get_default_remote();
595 struct strbuf remotesb = STRBUF_INIT;
597 strbuf_addf(&remotesb, "remote.%s.url", remote);
598 if (git_config_get_string(remotesb.buf, &remoteurl)) {
599 warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
600 remoteurl = xgetcwd();
602 relurl = relative_url(remoteurl, rel_url, NULL);
606 strbuf_release(&remotesb);
616 #define INIT_CB_INIT { NULL, 0 }
618 static void init_submodule(const char *path, const char *prefix,
621 const struct submodule *sub;
622 struct strbuf sb = STRBUF_INIT;
623 char *upd = NULL, *url = NULL, *displaypath;
625 displaypath = get_submodule_displaypath(path, prefix);
627 sub = submodule_from_path(the_repository, &null_oid, path);
630 die(_("No url found for submodule path '%s' in .gitmodules"),
634 * NEEDSWORK: In a multi-working-tree world, this needs to be
635 * set in the per-worktree config.
637 * Set active flag for the submodule being initialized
639 if (!is_submodule_active(the_repository, path)) {
640 strbuf_addf(&sb, "submodule.%s.active", sub->name);
641 git_config_set_gently(sb.buf, "true");
646 * Copy url setting when it is not set yet.
647 * To look up the url in .git/config, we must not fall back to
648 * .gitmodules, so look it up directly.
650 strbuf_addf(&sb, "submodule.%s.url", sub->name);
651 if (git_config_get_string(sb.buf, &url)) {
653 die(_("No url found for submodule path '%s' in .gitmodules"),
656 url = xstrdup(sub->url);
658 /* Possibly a url relative to parent */
659 if (starts_with_dot_dot_slash(url) ||
660 starts_with_dot_slash(url)) {
662 url = compute_submodule_clone_url(oldurl);
666 if (git_config_set_gently(sb.buf, url))
667 die(_("Failed to register url for submodule path '%s'"),
669 if (!(flags & OPT_QUIET))
671 _("Submodule '%s' (%s) registered for path '%s'\n"),
672 sub->name, url, displaypath);
676 /* Copy "update" setting when it is not set yet */
677 strbuf_addf(&sb, "submodule.%s.update", sub->name);
678 if (git_config_get_string(sb.buf, &upd) &&
679 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
680 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
681 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
683 upd = xstrdup("none");
685 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
687 if (git_config_set_gently(sb.buf, upd))
688 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
696 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
698 struct init_cb *info = cb_data;
699 init_submodule(list_item->name, info->prefix, info->flags);
702 static int module_init(int argc, const char **argv, const char *prefix)
704 struct init_cb info = INIT_CB_INIT;
705 struct pathspec pathspec;
706 struct module_list list = MODULE_LIST_INIT;
709 struct option module_init_options[] = {
710 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
714 const char *const git_submodule_helper_usage[] = {
715 N_("git submodule--helper init [<options>] [<path>]"),
719 argc = parse_options(argc, argv, prefix, module_init_options,
720 git_submodule_helper_usage, 0);
722 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
726 * If there are no path args and submodule.active is set then,
727 * by default, only initialize 'active' modules.
729 if (!argc && git_config_get_value_multi("submodule.active"))
730 module_list_active(&list);
732 info.prefix = prefix;
734 info.flags |= OPT_QUIET;
736 for_each_listed_submodule(&list, init_submodule_cb, &info);
746 #define STATUS_CB_INIT { NULL, 0 }
748 static void print_status(unsigned int flags, char state, const char *path,
749 const struct object_id *oid, const char *displaypath)
751 if (flags & OPT_QUIET)
754 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
756 if (state == ' ' || state == '+') {
757 const char *name = compute_rev_name(path, oid_to_hex(oid));
760 printf(" (%s)", name);
766 static int handle_submodule_head_ref(const char *refname,
767 const struct object_id *oid, int flags,
770 struct object_id *output = cb_data;
777 static void status_submodule(const char *path, const struct object_id *ce_oid,
778 unsigned int ce_flags, const char *prefix,
782 struct argv_array diff_files_args = ARGV_ARRAY_INIT;
784 int diff_files_result;
786 if (!submodule_from_path(the_repository, &null_oid, path))
787 die(_("no submodule mapping found in .gitmodules for path '%s'"),
790 displaypath = get_submodule_displaypath(path, prefix);
792 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
793 print_status(flags, 'U', path, &null_oid, displaypath);
797 if (!is_submodule_active(the_repository, path)) {
798 print_status(flags, '-', path, ce_oid, displaypath);
802 argv_array_pushl(&diff_files_args, "diff-files",
803 "--ignore-submodules=dirty", "--quiet", "--",
806 git_config(git_diff_basic_config, NULL);
808 repo_init_revisions(the_repository, &rev, NULL);
810 diff_files_args.argc = setup_revisions(diff_files_args.argc,
811 diff_files_args.argv,
813 diff_files_result = run_diff_files(&rev, 0);
815 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
816 print_status(flags, ' ', path, ce_oid,
818 } else if (!(flags & OPT_CACHED)) {
819 struct object_id oid;
820 struct ref_store *refs = get_submodule_ref_store(path);
823 print_status(flags, '-', path, ce_oid, displaypath);
826 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
827 die(_("could not resolve HEAD ref inside the "
828 "submodule '%s'"), path);
830 print_status(flags, '+', path, &oid, displaypath);
832 print_status(flags, '+', path, ce_oid, displaypath);
835 if (flags & OPT_RECURSIVE) {
836 struct child_process cpr = CHILD_PROCESS_INIT;
840 prepare_submodule_repo_env(&cpr.env_array);
842 argv_array_push(&cpr.args, "--super-prefix");
843 argv_array_pushf(&cpr.args, "%s/", displaypath);
844 argv_array_pushl(&cpr.args, "submodule--helper", "status",
845 "--recursive", NULL);
847 if (flags & OPT_CACHED)
848 argv_array_push(&cpr.args, "--cached");
850 if (flags & OPT_QUIET)
851 argv_array_push(&cpr.args, "--quiet");
853 if (run_command(&cpr))
854 die(_("failed to recurse into submodule '%s'"), path);
858 argv_array_clear(&diff_files_args);
862 static void status_submodule_cb(const struct cache_entry *list_item,
865 struct status_cb *info = cb_data;
866 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
867 info->prefix, info->flags);
870 static int module_status(int argc, const char **argv, const char *prefix)
872 struct status_cb info = STATUS_CB_INIT;
873 struct pathspec pathspec;
874 struct module_list list = MODULE_LIST_INIT;
877 struct option module_status_options[] = {
878 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
879 OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
880 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
884 const char *const git_submodule_helper_usage[] = {
885 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
889 argc = parse_options(argc, argv, prefix, module_status_options,
890 git_submodule_helper_usage, 0);
892 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
895 info.prefix = prefix;
897 info.flags |= OPT_QUIET;
899 for_each_listed_submodule(&list, status_submodule_cb, &info);
904 static int module_name(int argc, const char **argv, const char *prefix)
906 const struct submodule *sub;
909 usage(_("git submodule--helper name <path>"));
911 sub = submodule_from_path(the_repository, &null_oid, argv[1]);
914 die(_("no submodule mapping found in .gitmodules for path '%s'"),
917 printf("%s\n", sub->name);
927 #define SYNC_CB_INIT { NULL, 0 }
929 static void sync_submodule(const char *path, const char *prefix,
932 const struct submodule *sub;
933 char *remote_key = NULL;
934 char *sub_origin_url, *super_config_url, *displaypath;
935 struct strbuf sb = STRBUF_INIT;
936 struct child_process cp = CHILD_PROCESS_INIT;
937 char *sub_config_path = NULL;
939 if (!is_submodule_active(the_repository, path))
942 sub = submodule_from_path(the_repository, &null_oid, path);
944 if (sub && sub->url) {
945 if (starts_with_dot_dot_slash(sub->url) ||
946 starts_with_dot_slash(sub->url)) {
947 char *remote_url, *up_path;
948 char *remote = get_default_remote();
949 strbuf_addf(&sb, "remote.%s.url", remote);
951 if (git_config_get_string(sb.buf, &remote_url))
952 remote_url = xgetcwd();
954 up_path = get_up_path(path);
955 sub_origin_url = relative_url(remote_url, sub->url, up_path);
956 super_config_url = relative_url(remote_url, sub->url, NULL);
962 sub_origin_url = xstrdup(sub->url);
963 super_config_url = xstrdup(sub->url);
966 sub_origin_url = xstrdup("");
967 super_config_url = xstrdup("");
970 displaypath = get_submodule_displaypath(path, prefix);
972 if (!(flags & OPT_QUIET))
973 printf(_("Synchronizing submodule url for '%s'\n"),
977 strbuf_addf(&sb, "submodule.%s.url", sub->name);
978 if (git_config_set_gently(sb.buf, super_config_url))
979 die(_("failed to register url for submodule path '%s'"),
982 if (!is_submodule_populated_gently(path, NULL))
985 prepare_submodule_repo_env(&cp.env_array);
988 argv_array_pushl(&cp.args, "submodule--helper",
989 "print-default-remote", NULL);
992 if (capture_command(&cp, &sb, 0))
993 die(_("failed to get the default remote for submodule '%s'"),
996 strbuf_strip_suffix(&sb, "\n");
997 remote_key = xstrfmt("remote.%s.url", sb.buf);
1000 submodule_to_gitdir(&sb, path);
1001 strbuf_addstr(&sb, "/config");
1003 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1004 die(_("failed to update remote for submodule '%s'"),
1007 if (flags & OPT_RECURSIVE) {
1008 struct child_process cpr = CHILD_PROCESS_INIT;
1012 prepare_submodule_repo_env(&cpr.env_array);
1014 argv_array_push(&cpr.args, "--super-prefix");
1015 argv_array_pushf(&cpr.args, "%s/", displaypath);
1016 argv_array_pushl(&cpr.args, "submodule--helper", "sync",
1017 "--recursive", NULL);
1019 if (flags & OPT_QUIET)
1020 argv_array_push(&cpr.args, "--quiet");
1022 if (run_command(&cpr))
1023 die(_("failed to recurse into submodule '%s'"),
1028 free(super_config_url);
1029 free(sub_origin_url);
1030 strbuf_release(&sb);
1033 free(sub_config_path);
1036 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1038 struct sync_cb *info = cb_data;
1039 sync_submodule(list_item->name, info->prefix, info->flags);
1042 static int module_sync(int argc, const char **argv, const char *prefix)
1044 struct sync_cb info = SYNC_CB_INIT;
1045 struct pathspec pathspec;
1046 struct module_list list = MODULE_LIST_INIT;
1050 struct option module_sync_options[] = {
1051 OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
1052 OPT_BOOL(0, "recursive", &recursive,
1053 N_("Recurse into nested submodules")),
1057 const char *const git_submodule_helper_usage[] = {
1058 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1062 argc = parse_options(argc, argv, prefix, module_sync_options,
1063 git_submodule_helper_usage, 0);
1065 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1068 info.prefix = prefix;
1070 info.flags |= OPT_QUIET;
1072 info.flags |= OPT_RECURSIVE;
1074 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1083 #define DEINIT_CB_INIT { NULL, 0 }
1085 static void deinit_submodule(const char *path, const char *prefix,
1088 const struct submodule *sub;
1089 char *displaypath = NULL;
1090 struct child_process cp_config = CHILD_PROCESS_INIT;
1091 struct strbuf sb_config = STRBUF_INIT;
1092 char *sub_git_dir = xstrfmt("%s/.git", path);
1094 sub = submodule_from_path(the_repository, &null_oid, path);
1096 if (!sub || !sub->name)
1099 displaypath = get_submodule_displaypath(path, prefix);
1101 /* remove the submodule work tree (unless the user already did it) */
1102 if (is_directory(path)) {
1103 struct strbuf sb_rm = STRBUF_INIT;
1107 * protect submodules containing a .git directory
1108 * NEEDSWORK: instead of dying, automatically call
1109 * absorbgitdirs and (possibly) warn.
1111 if (is_directory(sub_git_dir))
1112 die(_("Submodule work tree '%s' contains a .git "
1113 "directory (use 'rm -rf' if you really want "
1114 "to remove it including all of its history)"),
1117 if (!(flags & OPT_FORCE)) {
1118 struct child_process cp_rm = CHILD_PROCESS_INIT;
1120 argv_array_pushl(&cp_rm.args, "rm", "-qn",
1123 if (run_command(&cp_rm))
1124 die(_("Submodule work tree '%s' contains local "
1125 "modifications; use '-f' to discard them"),
1129 strbuf_addstr(&sb_rm, path);
1131 if (!remove_dir_recursively(&sb_rm, 0))
1132 format = _("Cleared directory '%s'\n");
1134 format = _("Could not remove submodule work tree '%s'\n");
1136 if (!(flags & OPT_QUIET))
1137 printf(format, displaypath);
1139 submodule_unset_core_worktree(sub);
1141 strbuf_release(&sb_rm);
1144 if (mkdir(path, 0777))
1145 printf(_("could not create empty submodule directory %s"),
1148 cp_config.git_cmd = 1;
1149 argv_array_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1150 argv_array_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1152 /* remove the .git/config entries (unless the user already did it) */
1153 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1154 char *sub_key = xstrfmt("submodule.%s", sub->name);
1156 * remove the whole section so we have a clean state when
1157 * the user later decides to init this submodule again
1159 git_config_rename_section_in_file(NULL, sub_key, NULL);
1160 if (!(flags & OPT_QUIET))
1161 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1162 sub->name, sub->url, displaypath);
1169 strbuf_release(&sb_config);
1172 static void deinit_submodule_cb(const struct cache_entry *list_item,
1175 struct deinit_cb *info = cb_data;
1176 deinit_submodule(list_item->name, info->prefix, info->flags);
1179 static int module_deinit(int argc, const char **argv, const char *prefix)
1181 struct deinit_cb info = DEINIT_CB_INIT;
1182 struct pathspec pathspec;
1183 struct module_list list = MODULE_LIST_INIT;
1188 struct option module_deinit_options[] = {
1189 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
1190 OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
1191 OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
1195 const char *const git_submodule_helper_usage[] = {
1196 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1200 argc = parse_options(argc, argv, prefix, module_deinit_options,
1201 git_submodule_helper_usage, 0);
1204 error("pathspec and --all are incompatible");
1205 usage_with_options(git_submodule_helper_usage,
1206 module_deinit_options);
1210 die(_("Use '--all' if you really want to deinitialize all submodules"));
1212 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1215 info.prefix = prefix;
1217 info.flags |= OPT_QUIET;
1219 info.flags |= OPT_FORCE;
1221 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1226 static int clone_submodule(const char *path, const char *gitdir, const char *url,
1227 const char *depth, struct string_list *reference, int dissociate,
1228 int quiet, int progress)
1230 struct child_process cp = CHILD_PROCESS_INIT;
1232 argv_array_push(&cp.args, "clone");
1233 argv_array_push(&cp.args, "--no-checkout");
1235 argv_array_push(&cp.args, "--quiet");
1237 argv_array_push(&cp.args, "--progress");
1238 if (depth && *depth)
1239 argv_array_pushl(&cp.args, "--depth", depth, NULL);
1240 if (reference->nr) {
1241 struct string_list_item *item;
1242 for_each_string_list_item(item, reference)
1243 argv_array_pushl(&cp.args, "--reference",
1244 item->string, NULL);
1247 argv_array_push(&cp.args, "--dissociate");
1248 if (gitdir && *gitdir)
1249 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1251 argv_array_push(&cp.args, "--");
1252 argv_array_push(&cp.args, url);
1253 argv_array_push(&cp.args, path);
1256 prepare_submodule_repo_env(&cp.env_array);
1259 return run_command(&cp);
1262 struct submodule_alternate_setup {
1263 const char *submodule_name;
1264 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1265 SUBMODULE_ALTERNATE_ERROR_DIE,
1266 SUBMODULE_ALTERNATE_ERROR_INFO,
1267 SUBMODULE_ALTERNATE_ERROR_IGNORE
1269 struct string_list *reference;
1271 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1272 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1274 static const char alternate_error_advice[] = N_(
1275 "An alternate computed from a superproject's alternate is invalid.\n"
1276 "To allow Git to clone without an alternate in such a case, set\n"
1277 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1278 "'--reference-if-able' instead of '--reference'."
1281 static int add_possible_reference_from_superproject(
1282 struct object_directory *odb, void *sas_cb)
1284 struct submodule_alternate_setup *sas = sas_cb;
1288 * If the alternate object store is another repository, try the
1289 * standard layout with .git/(modules/<name>)+/objects
1291 if (strip_suffix(odb->path, "/objects", &len)) {
1293 struct strbuf sb = STRBUF_INIT;
1294 struct strbuf err = STRBUF_INIT;
1295 strbuf_add(&sb, odb->path, len);
1298 * We need to end the new path with '/' to mark it as a dir,
1299 * otherwise a submodule name containing '/' will be broken
1300 * as the last part of a missing submodule reference would
1301 * be taken as a file name.
1303 strbuf_addf(&sb, "/modules/%s/", sas->submodule_name);
1305 sm_alternate = compute_alternate_path(sb.buf, &err);
1307 string_list_append(sas->reference, xstrdup(sb.buf));
1310 switch (sas->error_mode) {
1311 case SUBMODULE_ALTERNATE_ERROR_DIE:
1312 if (advice_submodule_alternate_error_strategy_die)
1313 advise(_(alternate_error_advice));
1314 die(_("submodule '%s' cannot add alternate: %s"),
1315 sas->submodule_name, err.buf);
1316 case SUBMODULE_ALTERNATE_ERROR_INFO:
1317 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1318 sas->submodule_name, err.buf);
1319 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1323 strbuf_release(&sb);
1329 static void prepare_possible_alternates(const char *sm_name,
1330 struct string_list *reference)
1332 char *sm_alternate = NULL, *error_strategy = NULL;
1333 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1335 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1339 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1341 if (!error_strategy)
1342 error_strategy = xstrdup("die");
1344 sas.submodule_name = sm_name;
1345 sas.reference = reference;
1346 if (!strcmp(error_strategy, "die"))
1347 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1348 else if (!strcmp(error_strategy, "info"))
1349 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1350 else if (!strcmp(error_strategy, "ignore"))
1351 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1353 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1355 if (!strcmp(sm_alternate, "superproject"))
1356 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1357 else if (!strcmp(sm_alternate, "no"))
1360 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1363 free(error_strategy);
1366 static int module_clone(int argc, const char **argv, const char *prefix)
1368 const char *name = NULL, *url = NULL, *depth = NULL;
1371 char *p, *path = NULL, *sm_gitdir;
1372 struct strbuf sb = STRBUF_INIT;
1373 struct string_list reference = STRING_LIST_INIT_NODUP;
1374 int dissociate = 0, require_init = 0;
1375 char *sm_alternate = NULL, *error_strategy = NULL;
1377 struct option module_clone_options[] = {
1378 OPT_STRING(0, "prefix", &prefix,
1380 N_("alternative anchor for relative paths")),
1381 OPT_STRING(0, "path", &path,
1383 N_("where the new submodule will be cloned to")),
1384 OPT_STRING(0, "name", &name,
1386 N_("name of the new submodule")),
1387 OPT_STRING(0, "url", &url,
1389 N_("url where to clone the submodule from")),
1390 OPT_STRING_LIST(0, "reference", &reference,
1392 N_("reference repository")),
1393 OPT_BOOL(0, "dissociate", &dissociate,
1394 N_("use --reference only while cloning")),
1395 OPT_STRING(0, "depth", &depth,
1397 N_("depth for shallow clones")),
1398 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
1399 OPT_BOOL(0, "progress", &progress,
1400 N_("force cloning progress")),
1401 OPT_BOOL(0, "require-init", &require_init,
1402 N_("disallow cloning into non-empty directory")),
1406 const char *const git_submodule_helper_usage[] = {
1407 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1408 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1409 "--url <url> --path <path>"),
1413 argc = parse_options(argc, argv, prefix, module_clone_options,
1414 git_submodule_helper_usage, 0);
1416 if (argc || !url || !path || !*path)
1417 usage_with_options(git_submodule_helper_usage,
1418 module_clone_options);
1420 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
1421 sm_gitdir = absolute_pathdup(sb.buf);
1424 if (!is_absolute_path(path)) {
1425 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
1426 path = strbuf_detach(&sb, NULL);
1428 path = xstrdup(path);
1430 if (validate_submodule_git_dir(sm_gitdir, name) < 0)
1431 die(_("refusing to create/use '%s' in another submodule's "
1432 "git dir"), sm_gitdir);
1434 if (!file_exists(sm_gitdir)) {
1435 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1436 die(_("could not create directory '%s'"), sm_gitdir);
1438 prepare_possible_alternates(name, &reference);
1440 if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
1442 die(_("clone of '%s' into submodule path '%s' failed"),
1445 if (require_init && !access(path, X_OK) && !is_empty_dir(path))
1446 die(_("directory not empty: '%s'"), path);
1447 if (safe_create_leading_directories_const(path) < 0)
1448 die(_("could not create directory '%s'"), path);
1449 strbuf_addf(&sb, "%s/index", sm_gitdir);
1450 unlink_or_warn(sb.buf);
1454 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
1456 p = git_pathdup_submodule(path, "config");
1458 die(_("could not get submodule directory for '%s'"), path);
1460 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1461 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1463 git_config_set_in_file(p, "submodule.alternateLocation",
1465 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1467 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1471 free(error_strategy);
1473 strbuf_release(&sb);
1480 static void determine_submodule_update_strategy(struct repository *r,
1484 struct submodule_update_strategy *out)
1486 const struct submodule *sub = submodule_from_path(r, &null_oid, path);
1490 key = xstrfmt("submodule.%s.update", sub->name);
1493 if (parse_submodule_update_strategy(update, out) < 0)
1494 die(_("Invalid update mode '%s' for submodule path '%s'"),
1496 } else if (!repo_config_get_string_const(r, key, &val)) {
1497 if (parse_submodule_update_strategy(val, out) < 0)
1498 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1500 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1501 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1502 BUG("how did we read update = !command from .gitmodules?");
1503 out->type = sub->update_strategy.type;
1504 out->command = sub->update_strategy.command;
1506 out->type = SM_UPDATE_CHECKOUT;
1509 (out->type == SM_UPDATE_MERGE ||
1510 out->type == SM_UPDATE_REBASE ||
1511 out->type == SM_UPDATE_NONE))
1512 out->type = SM_UPDATE_CHECKOUT;
1517 static int module_update_module_mode(int argc, const char **argv, const char *prefix)
1519 const char *path, *update = NULL;
1521 struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
1523 if (argc < 3 || argc > 4)
1524 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1526 just_cloned = git_config_int("just_cloned", argv[1]);
1532 determine_submodule_update_strategy(the_repository,
1533 just_cloned, path, update,
1535 fputs(submodule_strategy_to_string(&update_strategy), stdout);
1540 struct update_clone_data {
1541 const struct submodule *sub;
1542 struct object_id oid;
1543 unsigned just_cloned;
1546 struct submodule_update_clone {
1547 /* index into 'list', the list of submodules to look into for cloning */
1549 struct module_list list;
1550 unsigned warn_if_uninitialized : 1;
1552 /* update parameter passed via commandline */
1553 struct submodule_update_strategy update;
1555 /* configuration parameters which are passed on to the children */
1558 int recommend_shallow;
1559 struct string_list references;
1561 unsigned require_init;
1563 const char *recursive_prefix;
1566 /* to be consumed by git-submodule.sh */
1567 struct update_clone_data *update_clone;
1568 int update_clone_nr; int update_clone_alloc;
1570 /* If we want to stop as fast as possible and return an error */
1571 unsigned quickstop : 1;
1573 /* failed clones to be retried again */
1574 const struct cache_entry **failed_clones;
1575 int failed_clones_nr, failed_clones_alloc;
1579 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1580 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, 0, \
1582 NULL, 0, 0, 0, NULL, 0, 0, 1}
1585 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1586 struct strbuf *out, const char *displaypath)
1589 * Only mention uninitialized submodules when their
1590 * paths have been specified.
1592 if (suc->warn_if_uninitialized) {
1594 _("Submodule path '%s' not initialized"),
1596 strbuf_addch(out, '\n');
1598 _("Maybe you want to use 'update --init'?"));
1599 strbuf_addch(out, '\n');
1604 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1605 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1607 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1608 struct child_process *child,
1609 struct submodule_update_clone *suc,
1612 const struct submodule *sub = NULL;
1613 const char *url = NULL;
1614 const char *update_string;
1615 enum submodule_update_type update_type;
1617 struct strbuf displaypath_sb = STRBUF_INIT;
1618 struct strbuf sb = STRBUF_INIT;
1619 const char *displaypath = NULL;
1620 int needs_cloning = 0;
1621 int need_free_url = 0;
1624 if (suc->recursive_prefix)
1625 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1627 strbuf_addstr(&sb, ce->name);
1628 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1629 strbuf_addch(out, '\n');
1633 sub = submodule_from_path(the_repository, &null_oid, ce->name);
1635 if (suc->recursive_prefix)
1636 displaypath = relative_path(suc->recursive_prefix,
1637 ce->name, &displaypath_sb);
1639 displaypath = ce->name;
1642 next_submodule_warn_missing(suc, out, displaypath);
1646 key = xstrfmt("submodule.%s.update", sub->name);
1647 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1648 update_type = parse_submodule_update_type(update_string);
1650 update_type = sub->update_strategy.type;
1654 if (suc->update.type == SM_UPDATE_NONE
1655 || (suc->update.type == SM_UPDATE_UNSPECIFIED
1656 && update_type == SM_UPDATE_NONE)) {
1657 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1658 strbuf_addch(out, '\n');
1662 /* Check if the submodule has been initialized. */
1663 if (!is_submodule_active(the_repository, ce->name)) {
1664 next_submodule_warn_missing(suc, out, displaypath);
1669 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1670 if (repo_config_get_string_const(the_repository, sb.buf, &url)) {
1671 if (starts_with_dot_slash(sub->url) ||
1672 starts_with_dot_dot_slash(sub->url)) {
1673 url = compute_submodule_clone_url(sub->url);
1680 strbuf_addf(&sb, "%s/.git", ce->name);
1681 needs_cloning = !file_exists(sb.buf);
1683 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
1684 suc->update_clone_alloc);
1685 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
1686 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
1687 suc->update_clone[suc->update_clone_nr].sub = sub;
1688 suc->update_clone_nr++;
1694 child->no_stdin = 1;
1695 child->stdout_to_stderr = 1;
1697 argv_array_push(&child->args, "submodule--helper");
1698 argv_array_push(&child->args, "clone");
1700 argv_array_push(&child->args, "--progress");
1702 argv_array_push(&child->args, "--quiet");
1704 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1705 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1706 argv_array_push(&child->args, "--depth=1");
1707 if (suc->require_init)
1708 argv_array_push(&child->args, "--require-init");
1709 argv_array_pushl(&child->args, "--path", sub->path, NULL);
1710 argv_array_pushl(&child->args, "--name", sub->name, NULL);
1711 argv_array_pushl(&child->args, "--url", url, NULL);
1712 if (suc->references.nr) {
1713 struct string_list_item *item;
1714 for_each_string_list_item(item, &suc->references)
1715 argv_array_pushl(&child->args, "--reference", item->string, NULL);
1717 if (suc->dissociate)
1718 argv_array_push(&child->args, "--dissociate");
1720 argv_array_push(&child->args, suc->depth);
1723 strbuf_reset(&displaypath_sb);
1728 return needs_cloning;
1731 static int update_clone_get_next_task(struct child_process *child,
1736 struct submodule_update_clone *suc = suc_cb;
1737 const struct cache_entry *ce;
1740 for (; suc->current < suc->list.nr; suc->current++) {
1741 ce = suc->list.entries[suc->current];
1742 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1743 int *p = xmalloc(sizeof(*p));
1752 * The loop above tried cloning each submodule once, now try the
1753 * stragglers again, which we can imagine as an extension of the
1756 index = suc->current - suc->list.nr;
1757 if (index < suc->failed_clones_nr) {
1759 ce = suc->failed_clones[index];
1760 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1762 strbuf_addstr(err, "BUG: submodule considered for "
1763 "cloning, doesn't need cloning "
1767 p = xmalloc(sizeof(*p));
1777 static int update_clone_start_failure(struct strbuf *err,
1781 struct submodule_update_clone *suc = suc_cb;
1786 static int update_clone_task_finished(int result,
1791 const struct cache_entry *ce;
1792 struct submodule_update_clone *suc = suc_cb;
1794 int *idxP = idx_task_cb;
1801 if (idx < suc->list.nr) {
1802 ce = suc->list.entries[idx];
1803 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1805 strbuf_addch(err, '\n');
1806 ALLOC_GROW(suc->failed_clones,
1807 suc->failed_clones_nr + 1,
1808 suc->failed_clones_alloc);
1809 suc->failed_clones[suc->failed_clones_nr++] = ce;
1812 idx -= suc->list.nr;
1813 ce = suc->failed_clones[idx];
1814 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1816 strbuf_addch(err, '\n');
1824 static int git_update_clone_config(const char *var, const char *value,
1828 if (!strcmp(var, "submodule.fetchjobs"))
1829 *max_jobs = parse_submodule_fetchjobs(var, value);
1833 static void update_submodule(struct update_clone_data *ucd)
1835 fprintf(stdout, "dummy %s %d\t%s\n",
1836 oid_to_hex(&ucd->oid),
1841 static int update_submodules(struct submodule_update_clone *suc)
1845 run_processes_parallel_tr2(suc->max_jobs, update_clone_get_next_task,
1846 update_clone_start_failure,
1847 update_clone_task_finished, suc, "submodule",
1851 * We saved the output and put it out all at once now.
1853 * - the listener does not have to interleave their (checkout)
1854 * work with our fetching. The writes involved in a
1855 * checkout involve more straightforward sequential I/O.
1856 * - the listener can avoid doing any work if fetching failed.
1861 for (i = 0; i < suc->update_clone_nr; i++)
1862 update_submodule(&suc->update_clone[i]);
1867 static int update_clone(int argc, const char **argv, const char *prefix)
1869 const char *update = NULL;
1870 struct pathspec pathspec;
1871 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1873 struct option module_update_clone_options[] = {
1874 OPT_STRING(0, "prefix", &prefix,
1876 N_("path into the working tree")),
1877 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1879 N_("path into the working tree, across nested "
1880 "submodule boundaries")),
1881 OPT_STRING(0, "update", &update,
1883 N_("rebase, merge, checkout or none")),
1884 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1885 N_("reference repository")),
1886 OPT_BOOL(0, "dissociate", &suc.dissociate,
1887 N_("use --reference only while cloning")),
1888 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1889 N_("Create a shallow clone truncated to the "
1890 "specified number of revisions")),
1891 OPT_INTEGER('j', "jobs", &suc.max_jobs,
1892 N_("parallel jobs")),
1893 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1894 N_("whether the initial clone should follow the shallow recommendation")),
1895 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1896 OPT_BOOL(0, "progress", &suc.progress,
1897 N_("force cloning progress")),
1898 OPT_BOOL(0, "require-init", &suc.require_init,
1899 N_("disallow cloning into non-empty directory")),
1903 const char *const git_submodule_helper_usage[] = {
1904 N_("git submodule--helper update-clone [--prefix=<path>] [<path>...]"),
1907 suc.prefix = prefix;
1909 update_clone_config_from_gitmodules(&suc.max_jobs);
1910 git_config(git_update_clone_config, &suc.max_jobs);
1912 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1913 git_submodule_helper_usage, 0);
1916 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1917 die(_("bad value for update parameter"));
1919 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1923 suc.warn_if_uninitialized = 1;
1925 return update_submodules(&suc);
1928 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1930 struct strbuf sb = STRBUF_INIT;
1932 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1934 printf("%s", relative_path(argv[1], argv[2], &sb));
1935 strbuf_release(&sb);
1939 static const char *remote_submodule_branch(const char *path)
1941 const struct submodule *sub;
1942 const char *branch = NULL;
1945 sub = submodule_from_path(the_repository, &null_oid, path);
1949 key = xstrfmt("submodule.%s.branch", sub->name);
1950 if (repo_config_get_string_const(the_repository, key, &branch))
1951 branch = sub->branch;
1957 if (!strcmp(branch, ".")) {
1958 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1961 die(_("No such ref: %s"), "HEAD");
1964 if (!strcmp(refname, "HEAD"))
1965 die(_("Submodule (%s) branch configured to inherit "
1966 "branch from superproject, but the superproject "
1967 "is not on any branch"), sub->name);
1969 if (!skip_prefix(refname, "refs/heads/", &refname))
1970 die(_("Expecting a full ref name, got %s"), refname);
1977 static int resolve_remote_submodule_branch(int argc, const char **argv,
1981 struct strbuf sb = STRBUF_INIT;
1983 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1985 ret = remote_submodule_branch(argv[1]);
1987 die("submodule %s doesn't exist", argv[1]);
1990 strbuf_release(&sb);
1994 static int push_check(int argc, const char **argv, const char *prefix)
1996 struct remote *remote;
1997 const char *superproject_head;
1999 int detached_head = 0;
2000 struct object_id head_oid;
2003 die("submodule--helper push-check requires at least 2 arguments");
2006 * superproject's resolved head ref.
2007 * if HEAD then the superproject is in a detached head state, otherwise
2008 * it will be the resolved head ref.
2010 superproject_head = argv[1];
2013 /* Get the submodule's head ref and determine if it is detached */
2014 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2016 die(_("Failed to resolve HEAD as a valid ref."));
2017 if (!strcmp(head, "HEAD"))
2021 * The remote must be configured.
2022 * This is to avoid pushing to the exact same URL as the parent.
2024 remote = pushremote_get(argv[1]);
2025 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2026 die("remote '%s' not configured", argv[1]);
2028 /* Check the refspec */
2031 struct ref *local_refs = get_local_heads();
2032 struct refspec refspec = REFSPEC_INIT_PUSH;
2034 refspec_appendn(&refspec, argv + 2, argc - 2);
2036 for (i = 0; i < refspec.nr; i++) {
2037 const struct refspec_item *rs = &refspec.items[i];
2039 if (rs->pattern || rs->matching)
2042 /* LHS must match a single ref */
2043 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2048 * If LHS matches 'HEAD' then we need to ensure
2049 * that it matches the same named branch
2050 * checked out in the superproject.
2052 if (!strcmp(rs->src, "HEAD")) {
2053 if (!detached_head &&
2054 !strcmp(head, superproject_head))
2056 die("HEAD does not match the named branch in the superproject");
2060 die("src refspec '%s' must name a ref",
2064 refspec_clear(&refspec);
2071 static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
2073 const struct submodule *sub;
2076 struct repository subrepo;
2079 BUG("submodule--helper ensure-core-worktree <path>");
2083 sub = submodule_from_path(the_repository, &null_oid, path);
2085 BUG("We could get the submodule handle before?");
2087 if (repo_submodule_init(&subrepo, the_repository, sub))
2088 die(_("could not get a repository handle for submodule '%s'"), path);
2090 if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
2091 char *cfg_file, *abs_path;
2092 const char *rel_path;
2093 struct strbuf sb = STRBUF_INIT;
2095 cfg_file = repo_git_path(&subrepo, "config");
2097 abs_path = absolute_pathdup(path);
2098 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2100 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2104 strbuf_release(&sb);
2110 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2113 struct pathspec pathspec;
2114 struct module_list list = MODULE_LIST_INIT;
2115 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2117 struct option embed_gitdir_options[] = {
2118 OPT_STRING(0, "prefix", &prefix,
2120 N_("path into the working tree")),
2121 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2122 ABSORB_GITDIR_RECURSE_SUBMODULES),
2126 const char *const git_submodule_helper_usage[] = {
2127 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2131 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2132 git_submodule_helper_usage, 0);
2134 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2137 for (i = 0; i < list.nr; i++)
2138 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2143 static int is_active(int argc, const char **argv, const char *prefix)
2146 die("submodule--helper is-active takes exactly 1 argument");
2148 return !is_submodule_active(the_repository, argv[1]);
2152 * Exit non-zero if any of the submodule names given on the command line is
2153 * invalid. If no names are given, filter stdin to print only valid names
2154 * (which is primarily intended for testing).
2156 static int check_name(int argc, const char **argv, const char *prefix)
2160 if (check_submodule_name(*argv) < 0)
2164 struct strbuf buf = STRBUF_INIT;
2165 while (strbuf_getline(&buf, stdin) != EOF) {
2166 if (!check_submodule_name(buf.buf))
2167 printf("%s\n", buf.buf);
2169 strbuf_release(&buf);
2174 static int module_config(int argc, const char **argv, const char *prefix)
2177 CHECK_WRITEABLE = 1,
2181 struct option module_config_options[] = {
2182 OPT_CMDMODE(0, "check-writeable", &command,
2183 N_("check if it is safe to write to the .gitmodules file"),
2185 OPT_CMDMODE(0, "unset", &command,
2186 N_("unset the config in the .gitmodules file"),
2190 const char *const git_submodule_helper_usage[] = {
2191 N_("git submodule--helper config <name> [<value>]"),
2192 N_("git submodule--helper config --unset <name>"),
2193 N_("git submodule--helper config --check-writeable"),
2197 argc = parse_options(argc, argv, prefix, module_config_options,
2198 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2200 if (argc == 1 && command == CHECK_WRITEABLE)
2201 return is_writing_gitmodules_ok() ? 0 : -1;
2203 /* Equivalent to ACTION_GET in builtin/config.c */
2204 if (argc == 2 && command != DO_UNSET)
2205 return print_config_from_gitmodules(the_repository, argv[1]);
2207 /* Equivalent to ACTION_SET in builtin/config.c */
2208 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2209 const char *value = (argc == 3) ? argv[2] : NULL;
2211 if (!is_writing_gitmodules_ok())
2212 die(_("please make sure that the .gitmodules file is in the working tree"));
2214 return config_set_in_gitmodules_file_gently(argv[1], value);
2217 usage_with_options(git_submodule_helper_usage, module_config_options);
2220 #define SUPPORT_SUPER_PREFIX (1<<0)
2224 int (*fn)(int, const char **, const char *);
2228 static struct cmd_struct commands[] = {
2229 {"list", module_list, 0},
2230 {"name", module_name, 0},
2231 {"clone", module_clone, 0},
2232 {"update-module-mode", module_update_module_mode, 0},
2233 {"update-clone", update_clone, 0},
2234 {"ensure-core-worktree", ensure_core_worktree, 0},
2235 {"relative-path", resolve_relative_path, 0},
2236 {"resolve-relative-url", resolve_relative_url, 0},
2237 {"resolve-relative-url-test", resolve_relative_url_test, 0},
2238 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
2239 {"init", module_init, SUPPORT_SUPER_PREFIX},
2240 {"status", module_status, SUPPORT_SUPER_PREFIX},
2241 {"print-default-remote", print_default_remote, 0},
2242 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
2243 {"deinit", module_deinit, 0},
2244 {"remote-branch", resolve_remote_submodule_branch, 0},
2245 {"push-check", push_check, 0},
2246 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
2247 {"is-active", is_active, 0},
2248 {"check-name", check_name, 0},
2249 {"config", module_config, 0},
2252 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2255 if (argc < 2 || !strcmp(argv[1], "-h"))
2256 usage("git submodule--helper <command>");
2258 for (i = 0; i < ARRAY_SIZE(commands); i++) {
2259 if (!strcmp(argv[1], commands[i].cmd)) {
2260 if (get_super_prefix() &&
2261 !(commands[i].option & SUPPORT_SUPER_PREFIX))
2262 die(_("%s doesn't support --super-prefix"),
2264 return commands[i].fn(argc - 1, argv + 1, prefix);
2268 die(_("'%s' is not a valid submodule--helper "
2269 "subcommand"), argv[1]);