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 strvec_push(&cp.args, "describe");
298 strvec_pushv(&cp.args, *d);
299 strvec_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 FOREACH_CB_INIT { 0 }
456 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
459 struct foreach_cb *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 strvec_pushf(&cp.env_array, "name=%s", sub->name);
499 strvec_pushf(&cp.env_array, "sm_path=%s", path);
500 strvec_pushf(&cp.env_array, "displaypath=%s", displaypath);
501 strvec_pushf(&cp.env_array, "sha1=%s",
503 strvec_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 strvec and not via env_array.
514 sq_quote_buf(&sb, path);
515 strvec_pushf(&cp.args, "path=%s; %s",
516 sb.buf, info->argv[0]);
520 strvec_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 strvec_pushl(&cpr.args, "--super-prefix", NULL);
538 strvec_pushf(&cpr.args, "%s/", displaypath);
539 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
543 strvec_push(&cpr.args, "--quiet");
545 strvec_push(&cpr.args, "--");
546 strvec_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 foreach_cb info = FOREACH_CB_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);
615 #define INIT_CB_INIT { NULL, 0 }
617 static void init_submodule(const char *path, const char *prefix,
620 const struct submodule *sub;
621 struct strbuf sb = STRBUF_INIT;
622 char *upd = NULL, *url = NULL, *displaypath;
624 displaypath = get_submodule_displaypath(path, prefix);
626 sub = submodule_from_path(the_repository, &null_oid, path);
629 die(_("No url found for submodule path '%s' in .gitmodules"),
633 * NEEDSWORK: In a multi-working-tree world, this needs to be
634 * set in the per-worktree config.
636 * Set active flag for the submodule being initialized
638 if (!is_submodule_active(the_repository, path)) {
639 strbuf_addf(&sb, "submodule.%s.active", sub->name);
640 git_config_set_gently(sb.buf, "true");
645 * Copy url setting when it is not set yet.
646 * To look up the url in .git/config, we must not fall back to
647 * .gitmodules, so look it up directly.
649 strbuf_addf(&sb, "submodule.%s.url", sub->name);
650 if (git_config_get_string(sb.buf, &url)) {
652 die(_("No url found for submodule path '%s' in .gitmodules"),
655 url = xstrdup(sub->url);
657 /* Possibly a url relative to parent */
658 if (starts_with_dot_dot_slash(url) ||
659 starts_with_dot_slash(url)) {
661 url = compute_submodule_clone_url(oldurl);
665 if (git_config_set_gently(sb.buf, url))
666 die(_("Failed to register url for submodule path '%s'"),
668 if (!(flags & OPT_QUIET))
670 _("Submodule '%s' (%s) registered for path '%s'\n"),
671 sub->name, url, displaypath);
675 /* Copy "update" setting when it is not set yet */
676 strbuf_addf(&sb, "submodule.%s.update", sub->name);
677 if (git_config_get_string(sb.buf, &upd) &&
678 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
679 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
680 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
682 upd = xstrdup("none");
684 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
686 if (git_config_set_gently(sb.buf, upd))
687 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
695 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
697 struct init_cb *info = cb_data;
698 init_submodule(list_item->name, info->prefix, info->flags);
701 static int module_init(int argc, const char **argv, const char *prefix)
703 struct init_cb info = INIT_CB_INIT;
704 struct pathspec pathspec;
705 struct module_list list = MODULE_LIST_INIT;
708 struct option module_init_options[] = {
709 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
713 const char *const git_submodule_helper_usage[] = {
714 N_("git submodule--helper init [<options>] [<path>]"),
718 argc = parse_options(argc, argv, prefix, module_init_options,
719 git_submodule_helper_usage, 0);
721 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
725 * If there are no path args and submodule.active is set then,
726 * by default, only initialize 'active' modules.
728 if (!argc && git_config_get_value_multi("submodule.active"))
729 module_list_active(&list);
731 info.prefix = prefix;
733 info.flags |= OPT_QUIET;
735 for_each_listed_submodule(&list, init_submodule_cb, &info);
744 #define STATUS_CB_INIT { NULL, 0 }
746 static void print_status(unsigned int flags, char state, const char *path,
747 const struct object_id *oid, const char *displaypath)
749 if (flags & OPT_QUIET)
752 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
754 if (state == ' ' || state == '+') {
755 const char *name = compute_rev_name(path, oid_to_hex(oid));
758 printf(" (%s)", name);
764 static int handle_submodule_head_ref(const char *refname,
765 const struct object_id *oid, int flags,
768 struct object_id *output = cb_data;
775 static void status_submodule(const char *path, const struct object_id *ce_oid,
776 unsigned int ce_flags, const char *prefix,
780 struct strvec diff_files_args = STRVEC_INIT;
782 int diff_files_result;
783 struct strbuf buf = STRBUF_INIT;
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 strbuf_addf(&buf, "%s/.git", path);
798 git_dir = read_gitfile(buf.buf);
802 if (!is_submodule_active(the_repository, path) ||
803 !is_git_directory(git_dir)) {
804 print_status(flags, '-', path, ce_oid, displaypath);
805 strbuf_release(&buf);
808 strbuf_release(&buf);
810 strvec_pushl(&diff_files_args, "diff-files",
811 "--ignore-submodules=dirty", "--quiet", "--",
814 git_config(git_diff_basic_config, NULL);
816 repo_init_revisions(the_repository, &rev, NULL);
818 diff_files_args.nr = setup_revisions(diff_files_args.nr,
821 diff_files_result = run_diff_files(&rev, 0);
823 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
824 print_status(flags, ' ', path, ce_oid,
826 } else if (!(flags & OPT_CACHED)) {
827 struct object_id oid;
828 struct ref_store *refs = get_submodule_ref_store(path);
831 print_status(flags, '-', path, ce_oid, displaypath);
834 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
835 die(_("could not resolve HEAD ref inside the "
836 "submodule '%s'"), path);
838 print_status(flags, '+', path, &oid, displaypath);
840 print_status(flags, '+', path, ce_oid, displaypath);
843 if (flags & OPT_RECURSIVE) {
844 struct child_process cpr = CHILD_PROCESS_INIT;
848 prepare_submodule_repo_env(&cpr.env_array);
850 strvec_push(&cpr.args, "--super-prefix");
851 strvec_pushf(&cpr.args, "%s/", displaypath);
852 strvec_pushl(&cpr.args, "submodule--helper", "status",
853 "--recursive", NULL);
855 if (flags & OPT_CACHED)
856 strvec_push(&cpr.args, "--cached");
858 if (flags & OPT_QUIET)
859 strvec_push(&cpr.args, "--quiet");
861 if (run_command(&cpr))
862 die(_("failed to recurse into submodule '%s'"), path);
866 strvec_clear(&diff_files_args);
870 static void status_submodule_cb(const struct cache_entry *list_item,
873 struct status_cb *info = cb_data;
874 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
875 info->prefix, info->flags);
878 static int module_status(int argc, const char **argv, const char *prefix)
880 struct status_cb info = STATUS_CB_INIT;
881 struct pathspec pathspec;
882 struct module_list list = MODULE_LIST_INIT;
885 struct option module_status_options[] = {
886 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
887 OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
888 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
892 const char *const git_submodule_helper_usage[] = {
893 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
897 argc = parse_options(argc, argv, prefix, module_status_options,
898 git_submodule_helper_usage, 0);
900 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
903 info.prefix = prefix;
905 info.flags |= OPT_QUIET;
907 for_each_listed_submodule(&list, status_submodule_cb, &info);
912 static int module_name(int argc, const char **argv, const char *prefix)
914 const struct submodule *sub;
917 usage(_("git submodule--helper name <path>"));
919 sub = submodule_from_path(the_repository, &null_oid, argv[1]);
922 die(_("no submodule mapping found in .gitmodules for path '%s'"),
925 printf("%s\n", sub->name);
934 #define SYNC_CB_INIT { NULL, 0 }
936 static void sync_submodule(const char *path, const char *prefix,
939 const struct submodule *sub;
940 char *remote_key = NULL;
941 char *sub_origin_url, *super_config_url, *displaypath;
942 struct strbuf sb = STRBUF_INIT;
943 struct child_process cp = CHILD_PROCESS_INIT;
944 char *sub_config_path = NULL;
946 if (!is_submodule_active(the_repository, path))
949 sub = submodule_from_path(the_repository, &null_oid, path);
951 if (sub && sub->url) {
952 if (starts_with_dot_dot_slash(sub->url) ||
953 starts_with_dot_slash(sub->url)) {
954 char *remote_url, *up_path;
955 char *remote = get_default_remote();
956 strbuf_addf(&sb, "remote.%s.url", remote);
958 if (git_config_get_string(sb.buf, &remote_url))
959 remote_url = xgetcwd();
961 up_path = get_up_path(path);
962 sub_origin_url = relative_url(remote_url, sub->url, up_path);
963 super_config_url = relative_url(remote_url, sub->url, NULL);
969 sub_origin_url = xstrdup(sub->url);
970 super_config_url = xstrdup(sub->url);
973 sub_origin_url = xstrdup("");
974 super_config_url = xstrdup("");
977 displaypath = get_submodule_displaypath(path, prefix);
979 if (!(flags & OPT_QUIET))
980 printf(_("Synchronizing submodule url for '%s'\n"),
984 strbuf_addf(&sb, "submodule.%s.url", sub->name);
985 if (git_config_set_gently(sb.buf, super_config_url))
986 die(_("failed to register url for submodule path '%s'"),
989 if (!is_submodule_populated_gently(path, NULL))
992 prepare_submodule_repo_env(&cp.env_array);
995 strvec_pushl(&cp.args, "submodule--helper",
996 "print-default-remote", NULL);
999 if (capture_command(&cp, &sb, 0))
1000 die(_("failed to get the default remote for submodule '%s'"),
1003 strbuf_strip_suffix(&sb, "\n");
1004 remote_key = xstrfmt("remote.%s.url", sb.buf);
1007 submodule_to_gitdir(&sb, path);
1008 strbuf_addstr(&sb, "/config");
1010 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1011 die(_("failed to update remote for submodule '%s'"),
1014 if (flags & OPT_RECURSIVE) {
1015 struct child_process cpr = CHILD_PROCESS_INIT;
1019 prepare_submodule_repo_env(&cpr.env_array);
1021 strvec_push(&cpr.args, "--super-prefix");
1022 strvec_pushf(&cpr.args, "%s/", displaypath);
1023 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1024 "--recursive", NULL);
1026 if (flags & OPT_QUIET)
1027 strvec_push(&cpr.args, "--quiet");
1029 if (run_command(&cpr))
1030 die(_("failed to recurse into submodule '%s'"),
1035 free(super_config_url);
1036 free(sub_origin_url);
1037 strbuf_release(&sb);
1040 free(sub_config_path);
1043 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1045 struct sync_cb *info = cb_data;
1046 sync_submodule(list_item->name, info->prefix, info->flags);
1049 static int module_sync(int argc, const char **argv, const char *prefix)
1051 struct sync_cb info = SYNC_CB_INIT;
1052 struct pathspec pathspec;
1053 struct module_list list = MODULE_LIST_INIT;
1057 struct option module_sync_options[] = {
1058 OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
1059 OPT_BOOL(0, "recursive", &recursive,
1060 N_("Recurse into nested submodules")),
1064 const char *const git_submodule_helper_usage[] = {
1065 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1069 argc = parse_options(argc, argv, prefix, module_sync_options,
1070 git_submodule_helper_usage, 0);
1072 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1075 info.prefix = prefix;
1077 info.flags |= OPT_QUIET;
1079 info.flags |= OPT_RECURSIVE;
1081 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1090 #define DEINIT_CB_INIT { NULL, 0 }
1092 static void deinit_submodule(const char *path, const char *prefix,
1095 const struct submodule *sub;
1096 char *displaypath = NULL;
1097 struct child_process cp_config = CHILD_PROCESS_INIT;
1098 struct strbuf sb_config = STRBUF_INIT;
1099 char *sub_git_dir = xstrfmt("%s/.git", path);
1101 sub = submodule_from_path(the_repository, &null_oid, path);
1103 if (!sub || !sub->name)
1106 displaypath = get_submodule_displaypath(path, prefix);
1108 /* remove the submodule work tree (unless the user already did it) */
1109 if (is_directory(path)) {
1110 struct strbuf sb_rm = STRBUF_INIT;
1114 * protect submodules containing a .git directory
1115 * NEEDSWORK: instead of dying, automatically call
1116 * absorbgitdirs and (possibly) warn.
1118 if (is_directory(sub_git_dir))
1119 die(_("Submodule work tree '%s' contains a .git "
1120 "directory (use 'rm -rf' if you really want "
1121 "to remove it including all of its history)"),
1124 if (!(flags & OPT_FORCE)) {
1125 struct child_process cp_rm = CHILD_PROCESS_INIT;
1127 strvec_pushl(&cp_rm.args, "rm", "-qn",
1130 if (run_command(&cp_rm))
1131 die(_("Submodule work tree '%s' contains local "
1132 "modifications; use '-f' to discard them"),
1136 strbuf_addstr(&sb_rm, path);
1138 if (!remove_dir_recursively(&sb_rm, 0))
1139 format = _("Cleared directory '%s'\n");
1141 format = _("Could not remove submodule work tree '%s'\n");
1143 if (!(flags & OPT_QUIET))
1144 printf(format, displaypath);
1146 submodule_unset_core_worktree(sub);
1148 strbuf_release(&sb_rm);
1151 if (mkdir(path, 0777))
1152 printf(_("could not create empty submodule directory %s"),
1155 cp_config.git_cmd = 1;
1156 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1157 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1159 /* remove the .git/config entries (unless the user already did it) */
1160 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1161 char *sub_key = xstrfmt("submodule.%s", sub->name);
1163 * remove the whole section so we have a clean state when
1164 * the user later decides to init this submodule again
1166 git_config_rename_section_in_file(NULL, sub_key, NULL);
1167 if (!(flags & OPT_QUIET))
1168 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1169 sub->name, sub->url, displaypath);
1176 strbuf_release(&sb_config);
1179 static void deinit_submodule_cb(const struct cache_entry *list_item,
1182 struct deinit_cb *info = cb_data;
1183 deinit_submodule(list_item->name, info->prefix, info->flags);
1186 static int module_deinit(int argc, const char **argv, const char *prefix)
1188 struct deinit_cb info = DEINIT_CB_INIT;
1189 struct pathspec pathspec;
1190 struct module_list list = MODULE_LIST_INIT;
1195 struct option module_deinit_options[] = {
1196 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
1197 OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
1198 OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
1202 const char *const git_submodule_helper_usage[] = {
1203 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1207 argc = parse_options(argc, argv, prefix, module_deinit_options,
1208 git_submodule_helper_usage, 0);
1211 error("pathspec and --all are incompatible");
1212 usage_with_options(git_submodule_helper_usage,
1213 module_deinit_options);
1217 die(_("Use '--all' if you really want to deinitialize all submodules"));
1219 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1222 info.prefix = prefix;
1224 info.flags |= OPT_QUIET;
1226 info.flags |= OPT_FORCE;
1228 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1233 static int clone_submodule(const char *path, const char *gitdir, const char *url,
1234 const char *depth, struct string_list *reference, int dissociate,
1235 int quiet, int progress, int single_branch)
1237 struct child_process cp = CHILD_PROCESS_INIT;
1239 strvec_push(&cp.args, "clone");
1240 strvec_push(&cp.args, "--no-checkout");
1242 strvec_push(&cp.args, "--quiet");
1244 strvec_push(&cp.args, "--progress");
1245 if (depth && *depth)
1246 strvec_pushl(&cp.args, "--depth", depth, NULL);
1247 if (reference->nr) {
1248 struct string_list_item *item;
1249 for_each_string_list_item(item, reference)
1250 strvec_pushl(&cp.args, "--reference",
1251 item->string, NULL);
1254 strvec_push(&cp.args, "--dissociate");
1255 if (gitdir && *gitdir)
1256 strvec_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1257 if (single_branch >= 0)
1258 strvec_push(&cp.args, single_branch ?
1260 "--no-single-branch");
1262 strvec_push(&cp.args, "--");
1263 strvec_push(&cp.args, url);
1264 strvec_push(&cp.args, path);
1267 prepare_submodule_repo_env(&cp.env_array);
1270 return run_command(&cp);
1273 struct submodule_alternate_setup {
1274 const char *submodule_name;
1275 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1276 SUBMODULE_ALTERNATE_ERROR_DIE,
1277 SUBMODULE_ALTERNATE_ERROR_INFO,
1278 SUBMODULE_ALTERNATE_ERROR_IGNORE
1280 struct string_list *reference;
1282 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1283 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1285 static const char alternate_error_advice[] = N_(
1286 "An alternate computed from a superproject's alternate is invalid.\n"
1287 "To allow Git to clone without an alternate in such a case, set\n"
1288 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1289 "'--reference-if-able' instead of '--reference'."
1292 static int add_possible_reference_from_superproject(
1293 struct object_directory *odb, void *sas_cb)
1295 struct submodule_alternate_setup *sas = sas_cb;
1299 * If the alternate object store is another repository, try the
1300 * standard layout with .git/(modules/<name>)+/objects
1302 if (strip_suffix(odb->path, "/objects", &len)) {
1304 struct strbuf sb = STRBUF_INIT;
1305 struct strbuf err = STRBUF_INIT;
1306 strbuf_add(&sb, odb->path, len);
1309 * We need to end the new path with '/' to mark it as a dir,
1310 * otherwise a submodule name containing '/' will be broken
1311 * as the last part of a missing submodule reference would
1312 * be taken as a file name.
1314 strbuf_addf(&sb, "/modules/%s/", sas->submodule_name);
1316 sm_alternate = compute_alternate_path(sb.buf, &err);
1318 string_list_append(sas->reference, xstrdup(sb.buf));
1321 switch (sas->error_mode) {
1322 case SUBMODULE_ALTERNATE_ERROR_DIE:
1323 if (advice_submodule_alternate_error_strategy_die)
1324 advise(_(alternate_error_advice));
1325 die(_("submodule '%s' cannot add alternate: %s"),
1326 sas->submodule_name, err.buf);
1327 case SUBMODULE_ALTERNATE_ERROR_INFO:
1328 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1329 sas->submodule_name, err.buf);
1330 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1334 strbuf_release(&sb);
1340 static void prepare_possible_alternates(const char *sm_name,
1341 struct string_list *reference)
1343 char *sm_alternate = NULL, *error_strategy = NULL;
1344 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1346 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1350 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1352 if (!error_strategy)
1353 error_strategy = xstrdup("die");
1355 sas.submodule_name = sm_name;
1356 sas.reference = reference;
1357 if (!strcmp(error_strategy, "die"))
1358 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1359 else if (!strcmp(error_strategy, "info"))
1360 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1361 else if (!strcmp(error_strategy, "ignore"))
1362 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1364 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1366 if (!strcmp(sm_alternate, "superproject"))
1367 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1368 else if (!strcmp(sm_alternate, "no"))
1371 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1374 free(error_strategy);
1377 static int module_clone(int argc, const char **argv, const char *prefix)
1379 const char *name = NULL, *url = NULL, *depth = NULL;
1382 char *p, *path = NULL, *sm_gitdir;
1383 struct strbuf sb = STRBUF_INIT;
1384 struct string_list reference = STRING_LIST_INIT_NODUP;
1385 int dissociate = 0, require_init = 0;
1386 char *sm_alternate = NULL, *error_strategy = NULL;
1387 int single_branch = -1;
1389 struct option module_clone_options[] = {
1390 OPT_STRING(0, "prefix", &prefix,
1392 N_("alternative anchor for relative paths")),
1393 OPT_STRING(0, "path", &path,
1395 N_("where the new submodule will be cloned to")),
1396 OPT_STRING(0, "name", &name,
1398 N_("name of the new submodule")),
1399 OPT_STRING(0, "url", &url,
1401 N_("url where to clone the submodule from")),
1402 OPT_STRING_LIST(0, "reference", &reference,
1404 N_("reference repository")),
1405 OPT_BOOL(0, "dissociate", &dissociate,
1406 N_("use --reference only while cloning")),
1407 OPT_STRING(0, "depth", &depth,
1409 N_("depth for shallow clones")),
1410 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
1411 OPT_BOOL(0, "progress", &progress,
1412 N_("force cloning progress")),
1413 OPT_BOOL(0, "require-init", &require_init,
1414 N_("disallow cloning into non-empty directory")),
1415 OPT_BOOL(0, "single-branch", &single_branch,
1416 N_("clone only one branch, HEAD or --branch")),
1420 const char *const git_submodule_helper_usage[] = {
1421 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1422 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1423 "[--single-branch] "
1424 "--url <url> --path <path>"),
1428 argc = parse_options(argc, argv, prefix, module_clone_options,
1429 git_submodule_helper_usage, 0);
1431 if (argc || !url || !path || !*path)
1432 usage_with_options(git_submodule_helper_usage,
1433 module_clone_options);
1435 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
1436 sm_gitdir = absolute_pathdup(sb.buf);
1439 if (!is_absolute_path(path)) {
1440 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
1441 path = strbuf_detach(&sb, NULL);
1443 path = xstrdup(path);
1445 if (validate_submodule_git_dir(sm_gitdir, name) < 0)
1446 die(_("refusing to create/use '%s' in another submodule's "
1447 "git dir"), sm_gitdir);
1449 if (!file_exists(sm_gitdir)) {
1450 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1451 die(_("could not create directory '%s'"), sm_gitdir);
1453 prepare_possible_alternates(name, &reference);
1455 if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
1456 quiet, progress, single_branch))
1457 die(_("clone of '%s' into submodule path '%s' failed"),
1460 if (require_init && !access(path, X_OK) && !is_empty_dir(path))
1461 die(_("directory not empty: '%s'"), path);
1462 if (safe_create_leading_directories_const(path) < 0)
1463 die(_("could not create directory '%s'"), path);
1464 strbuf_addf(&sb, "%s/index", sm_gitdir);
1465 unlink_or_warn(sb.buf);
1469 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
1471 p = git_pathdup_submodule(path, "config");
1473 die(_("could not get submodule directory for '%s'"), path);
1475 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1476 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1478 git_config_set_in_file(p, "submodule.alternateLocation",
1480 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1482 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1486 free(error_strategy);
1488 strbuf_release(&sb);
1495 static void determine_submodule_update_strategy(struct repository *r,
1499 struct submodule_update_strategy *out)
1501 const struct submodule *sub = submodule_from_path(r, &null_oid, path);
1505 key = xstrfmt("submodule.%s.update", sub->name);
1508 if (parse_submodule_update_strategy(update, out) < 0)
1509 die(_("Invalid update mode '%s' for submodule path '%s'"),
1511 } else if (!repo_config_get_string_const(r, key, &val)) {
1512 if (parse_submodule_update_strategy(val, out) < 0)
1513 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1515 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1516 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1517 BUG("how did we read update = !command from .gitmodules?");
1518 out->type = sub->update_strategy.type;
1519 out->command = sub->update_strategy.command;
1521 out->type = SM_UPDATE_CHECKOUT;
1524 (out->type == SM_UPDATE_MERGE ||
1525 out->type == SM_UPDATE_REBASE ||
1526 out->type == SM_UPDATE_NONE))
1527 out->type = SM_UPDATE_CHECKOUT;
1532 static int module_update_module_mode(int argc, const char **argv, const char *prefix)
1534 const char *path, *update = NULL;
1536 struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
1538 if (argc < 3 || argc > 4)
1539 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1541 just_cloned = git_config_int("just_cloned", argv[1]);
1547 determine_submodule_update_strategy(the_repository,
1548 just_cloned, path, update,
1550 fputs(submodule_strategy_to_string(&update_strategy), stdout);
1555 struct update_clone_data {
1556 const struct submodule *sub;
1557 struct object_id oid;
1558 unsigned just_cloned;
1561 struct submodule_update_clone {
1562 /* index into 'list', the list of submodules to look into for cloning */
1564 struct module_list list;
1565 unsigned warn_if_uninitialized : 1;
1567 /* update parameter passed via commandline */
1568 struct submodule_update_strategy update;
1570 /* configuration parameters which are passed on to the children */
1573 int recommend_shallow;
1574 struct string_list references;
1576 unsigned require_init;
1578 const char *recursive_prefix;
1582 /* to be consumed by git-submodule.sh */
1583 struct update_clone_data *update_clone;
1584 int update_clone_nr; int update_clone_alloc;
1586 /* If we want to stop as fast as possible and return an error */
1587 unsigned quickstop : 1;
1589 /* failed clones to be retried again */
1590 const struct cache_entry **failed_clones;
1591 int failed_clones_nr, failed_clones_alloc;
1595 #define SUBMODULE_UPDATE_CLONE_INIT { \
1596 .list = MODULE_LIST_INIT, \
1597 .update = SUBMODULE_UPDATE_STRATEGY_INIT, \
1598 .recommend_shallow = -1, \
1599 .references = STRING_LIST_INIT_DUP, \
1600 .single_branch = -1, \
1605 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1606 struct strbuf *out, const char *displaypath)
1609 * Only mention uninitialized submodules when their
1610 * paths have been specified.
1612 if (suc->warn_if_uninitialized) {
1614 _("Submodule path '%s' not initialized"),
1616 strbuf_addch(out, '\n');
1618 _("Maybe you want to use 'update --init'?"));
1619 strbuf_addch(out, '\n');
1624 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1625 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1627 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1628 struct child_process *child,
1629 struct submodule_update_clone *suc,
1632 const struct submodule *sub = NULL;
1633 const char *url = NULL;
1634 const char *update_string;
1635 enum submodule_update_type update_type;
1637 struct strbuf displaypath_sb = STRBUF_INIT;
1638 struct strbuf sb = STRBUF_INIT;
1639 const char *displaypath = NULL;
1640 int needs_cloning = 0;
1641 int need_free_url = 0;
1644 if (suc->recursive_prefix)
1645 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1647 strbuf_addstr(&sb, ce->name);
1648 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1649 strbuf_addch(out, '\n');
1653 sub = submodule_from_path(the_repository, &null_oid, ce->name);
1655 if (suc->recursive_prefix)
1656 displaypath = relative_path(suc->recursive_prefix,
1657 ce->name, &displaypath_sb);
1659 displaypath = ce->name;
1662 next_submodule_warn_missing(suc, out, displaypath);
1666 key = xstrfmt("submodule.%s.update", sub->name);
1667 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1668 update_type = parse_submodule_update_type(update_string);
1670 update_type = sub->update_strategy.type;
1674 if (suc->update.type == SM_UPDATE_NONE
1675 || (suc->update.type == SM_UPDATE_UNSPECIFIED
1676 && update_type == SM_UPDATE_NONE)) {
1677 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1678 strbuf_addch(out, '\n');
1682 /* Check if the submodule has been initialized. */
1683 if (!is_submodule_active(the_repository, ce->name)) {
1684 next_submodule_warn_missing(suc, out, displaypath);
1689 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1690 if (repo_config_get_string_const(the_repository, sb.buf, &url)) {
1691 if (starts_with_dot_slash(sub->url) ||
1692 starts_with_dot_dot_slash(sub->url)) {
1693 url = compute_submodule_clone_url(sub->url);
1700 strbuf_addf(&sb, "%s/.git", ce->name);
1701 needs_cloning = !file_exists(sb.buf);
1703 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
1704 suc->update_clone_alloc);
1705 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
1706 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
1707 suc->update_clone[suc->update_clone_nr].sub = sub;
1708 suc->update_clone_nr++;
1714 child->no_stdin = 1;
1715 child->stdout_to_stderr = 1;
1717 strvec_push(&child->args, "submodule--helper");
1718 strvec_push(&child->args, "clone");
1720 strvec_push(&child->args, "--progress");
1722 strvec_push(&child->args, "--quiet");
1724 strvec_pushl(&child->args, "--prefix", suc->prefix, NULL);
1725 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1726 strvec_push(&child->args, "--depth=1");
1727 if (suc->require_init)
1728 strvec_push(&child->args, "--require-init");
1729 strvec_pushl(&child->args, "--path", sub->path, NULL);
1730 strvec_pushl(&child->args, "--name", sub->name, NULL);
1731 strvec_pushl(&child->args, "--url", url, NULL);
1732 if (suc->references.nr) {
1733 struct string_list_item *item;
1734 for_each_string_list_item(item, &suc->references)
1735 strvec_pushl(&child->args, "--reference", item->string, NULL);
1737 if (suc->dissociate)
1738 strvec_push(&child->args, "--dissociate");
1740 strvec_push(&child->args, suc->depth);
1741 if (suc->single_branch >= 0)
1742 strvec_push(&child->args, suc->single_branch ?
1744 "--no-single-branch");
1747 strbuf_reset(&displaypath_sb);
1752 return needs_cloning;
1755 static int update_clone_get_next_task(struct child_process *child,
1760 struct submodule_update_clone *suc = suc_cb;
1761 const struct cache_entry *ce;
1764 for (; suc->current < suc->list.nr; suc->current++) {
1765 ce = suc->list.entries[suc->current];
1766 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1767 int *p = xmalloc(sizeof(*p));
1776 * The loop above tried cloning each submodule once, now try the
1777 * stragglers again, which we can imagine as an extension of the
1780 index = suc->current - suc->list.nr;
1781 if (index < suc->failed_clones_nr) {
1783 ce = suc->failed_clones[index];
1784 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1786 strbuf_addstr(err, "BUG: submodule considered for "
1787 "cloning, doesn't need cloning "
1791 p = xmalloc(sizeof(*p));
1801 static int update_clone_start_failure(struct strbuf *err,
1805 struct submodule_update_clone *suc = suc_cb;
1810 static int update_clone_task_finished(int result,
1815 const struct cache_entry *ce;
1816 struct submodule_update_clone *suc = suc_cb;
1818 int *idxP = idx_task_cb;
1825 if (idx < suc->list.nr) {
1826 ce = suc->list.entries[idx];
1827 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1829 strbuf_addch(err, '\n');
1830 ALLOC_GROW(suc->failed_clones,
1831 suc->failed_clones_nr + 1,
1832 suc->failed_clones_alloc);
1833 suc->failed_clones[suc->failed_clones_nr++] = ce;
1836 idx -= suc->list.nr;
1837 ce = suc->failed_clones[idx];
1838 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1840 strbuf_addch(err, '\n');
1848 static int git_update_clone_config(const char *var, const char *value,
1852 if (!strcmp(var, "submodule.fetchjobs"))
1853 *max_jobs = parse_submodule_fetchjobs(var, value);
1857 static void update_submodule(struct update_clone_data *ucd)
1859 fprintf(stdout, "dummy %s %d\t%s\n",
1860 oid_to_hex(&ucd->oid),
1865 static int update_submodules(struct submodule_update_clone *suc)
1869 run_processes_parallel_tr2(suc->max_jobs, update_clone_get_next_task,
1870 update_clone_start_failure,
1871 update_clone_task_finished, suc, "submodule",
1875 * We saved the output and put it out all at once now.
1877 * - the listener does not have to interleave their (checkout)
1878 * work with our fetching. The writes involved in a
1879 * checkout involve more straightforward sequential I/O.
1880 * - the listener can avoid doing any work if fetching failed.
1885 for (i = 0; i < suc->update_clone_nr; i++)
1886 update_submodule(&suc->update_clone[i]);
1891 static int update_clone(int argc, const char **argv, const char *prefix)
1893 const char *update = NULL;
1894 struct pathspec pathspec;
1895 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1897 struct option module_update_clone_options[] = {
1898 OPT_STRING(0, "prefix", &prefix,
1900 N_("path into the working tree")),
1901 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1903 N_("path into the working tree, across nested "
1904 "submodule boundaries")),
1905 OPT_STRING(0, "update", &update,
1907 N_("rebase, merge, checkout or none")),
1908 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1909 N_("reference repository")),
1910 OPT_BOOL(0, "dissociate", &suc.dissociate,
1911 N_("use --reference only while cloning")),
1912 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1913 N_("Create a shallow clone truncated to the "
1914 "specified number of revisions")),
1915 OPT_INTEGER('j', "jobs", &suc.max_jobs,
1916 N_("parallel jobs")),
1917 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1918 N_("whether the initial clone should follow the shallow recommendation")),
1919 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1920 OPT_BOOL(0, "progress", &suc.progress,
1921 N_("force cloning progress")),
1922 OPT_BOOL(0, "require-init", &suc.require_init,
1923 N_("disallow cloning into non-empty directory")),
1924 OPT_BOOL(0, "single-branch", &suc.single_branch,
1925 N_("clone only one branch, HEAD or --branch")),
1929 const char *const git_submodule_helper_usage[] = {
1930 N_("git submodule--helper update-clone [--prefix=<path>] [<path>...]"),
1933 suc.prefix = prefix;
1935 update_clone_config_from_gitmodules(&suc.max_jobs);
1936 git_config(git_update_clone_config, &suc.max_jobs);
1938 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1939 git_submodule_helper_usage, 0);
1942 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1943 die(_("bad value for update parameter"));
1945 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1949 suc.warn_if_uninitialized = 1;
1951 return update_submodules(&suc);
1954 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1956 struct strbuf sb = STRBUF_INIT;
1958 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1960 printf("%s", relative_path(argv[1], argv[2], &sb));
1961 strbuf_release(&sb);
1965 static const char *remote_submodule_branch(const char *path)
1967 const struct submodule *sub;
1968 const char *branch = NULL;
1971 sub = submodule_from_path(the_repository, &null_oid, path);
1975 key = xstrfmt("submodule.%s.branch", sub->name);
1976 if (repo_config_get_string_const(the_repository, key, &branch))
1977 branch = sub->branch;
1983 if (!strcmp(branch, ".")) {
1984 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1987 die(_("No such ref: %s"), "HEAD");
1990 if (!strcmp(refname, "HEAD"))
1991 die(_("Submodule (%s) branch configured to inherit "
1992 "branch from superproject, but the superproject "
1993 "is not on any branch"), sub->name);
1995 if (!skip_prefix(refname, "refs/heads/", &refname))
1996 die(_("Expecting a full ref name, got %s"), refname);
2003 static int resolve_remote_submodule_branch(int argc, const char **argv,
2007 struct strbuf sb = STRBUF_INIT;
2009 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
2011 ret = remote_submodule_branch(argv[1]);
2013 die("submodule %s doesn't exist", argv[1]);
2016 strbuf_release(&sb);
2020 static int push_check(int argc, const char **argv, const char *prefix)
2022 struct remote *remote;
2023 const char *superproject_head;
2025 int detached_head = 0;
2026 struct object_id head_oid;
2029 die("submodule--helper push-check requires at least 2 arguments");
2032 * superproject's resolved head ref.
2033 * if HEAD then the superproject is in a detached head state, otherwise
2034 * it will be the resolved head ref.
2036 superproject_head = argv[1];
2039 /* Get the submodule's head ref and determine if it is detached */
2040 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2042 die(_("Failed to resolve HEAD as a valid ref."));
2043 if (!strcmp(head, "HEAD"))
2047 * The remote must be configured.
2048 * This is to avoid pushing to the exact same URL as the parent.
2050 remote = pushremote_get(argv[1]);
2051 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2052 die("remote '%s' not configured", argv[1]);
2054 /* Check the refspec */
2057 struct ref *local_refs = get_local_heads();
2058 struct refspec refspec = REFSPEC_INIT_PUSH;
2060 refspec_appendn(&refspec, argv + 2, argc - 2);
2062 for (i = 0; i < refspec.nr; i++) {
2063 const struct refspec_item *rs = &refspec.items[i];
2065 if (rs->pattern || rs->matching)
2068 /* LHS must match a single ref */
2069 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2074 * If LHS matches 'HEAD' then we need to ensure
2075 * that it matches the same named branch
2076 * checked out in the superproject.
2078 if (!strcmp(rs->src, "HEAD")) {
2079 if (!detached_head &&
2080 !strcmp(head, superproject_head))
2082 die("HEAD does not match the named branch in the superproject");
2086 die("src refspec '%s' must name a ref",
2090 refspec_clear(&refspec);
2097 static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
2099 const struct submodule *sub;
2102 struct repository subrepo;
2105 BUG("submodule--helper ensure-core-worktree <path>");
2109 sub = submodule_from_path(the_repository, &null_oid, path);
2111 BUG("We could get the submodule handle before?");
2113 if (repo_submodule_init(&subrepo, the_repository, sub))
2114 die(_("could not get a repository handle for submodule '%s'"), path);
2116 if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
2117 char *cfg_file, *abs_path;
2118 const char *rel_path;
2119 struct strbuf sb = STRBUF_INIT;
2121 cfg_file = repo_git_path(&subrepo, "config");
2123 abs_path = absolute_pathdup(path);
2124 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2126 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2130 strbuf_release(&sb);
2136 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2139 struct pathspec pathspec;
2140 struct module_list list = MODULE_LIST_INIT;
2141 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2143 struct option embed_gitdir_options[] = {
2144 OPT_STRING(0, "prefix", &prefix,
2146 N_("path into the working tree")),
2147 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2148 ABSORB_GITDIR_RECURSE_SUBMODULES),
2152 const char *const git_submodule_helper_usage[] = {
2153 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2157 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2158 git_submodule_helper_usage, 0);
2160 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2163 for (i = 0; i < list.nr; i++)
2164 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2169 static int is_active(int argc, const char **argv, const char *prefix)
2172 die("submodule--helper is-active takes exactly 1 argument");
2174 return !is_submodule_active(the_repository, argv[1]);
2178 * Exit non-zero if any of the submodule names given on the command line is
2179 * invalid. If no names are given, filter stdin to print only valid names
2180 * (which is primarily intended for testing).
2182 static int check_name(int argc, const char **argv, const char *prefix)
2186 if (check_submodule_name(*argv) < 0)
2190 struct strbuf buf = STRBUF_INIT;
2191 while (strbuf_getline(&buf, stdin) != EOF) {
2192 if (!check_submodule_name(buf.buf))
2193 printf("%s\n", buf.buf);
2195 strbuf_release(&buf);
2200 static int module_config(int argc, const char **argv, const char *prefix)
2203 CHECK_WRITEABLE = 1,
2207 struct option module_config_options[] = {
2208 OPT_CMDMODE(0, "check-writeable", &command,
2209 N_("check if it is safe to write to the .gitmodules file"),
2211 OPT_CMDMODE(0, "unset", &command,
2212 N_("unset the config in the .gitmodules file"),
2216 const char *const git_submodule_helper_usage[] = {
2217 N_("git submodule--helper config <name> [<value>]"),
2218 N_("git submodule--helper config --unset <name>"),
2219 N_("git submodule--helper config --check-writeable"),
2223 argc = parse_options(argc, argv, prefix, module_config_options,
2224 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2226 if (argc == 1 && command == CHECK_WRITEABLE)
2227 return is_writing_gitmodules_ok() ? 0 : -1;
2229 /* Equivalent to ACTION_GET in builtin/config.c */
2230 if (argc == 2 && command != DO_UNSET)
2231 return print_config_from_gitmodules(the_repository, argv[1]);
2233 /* Equivalent to ACTION_SET in builtin/config.c */
2234 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2235 const char *value = (argc == 3) ? argv[2] : NULL;
2237 if (!is_writing_gitmodules_ok())
2238 die(_("please make sure that the .gitmodules file is in the working tree"));
2240 return config_set_in_gitmodules_file_gently(argv[1], value);
2243 usage_with_options(git_submodule_helper_usage, module_config_options);
2246 static int module_set_url(int argc, const char **argv, const char *prefix)
2253 struct option options[] = {
2254 OPT__QUIET(&quiet, N_("Suppress output for setting url of a submodule")),
2257 const char *const usage[] = {
2258 N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
2262 argc = parse_options(argc, argv, prefix, options, usage, 0);
2264 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2265 usage_with_options(usage, options);
2267 config_name = xstrfmt("submodule.%s.url", path);
2269 config_set_in_gitmodules_file_gently(config_name, newurl);
2270 sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
2277 static int module_set_branch(int argc, const char **argv, const char *prefix)
2279 int opt_default = 0, ret;
2280 const char *opt_branch = NULL;
2285 * We accept the `quiet` option for uniformity across subcommands,
2286 * though there is nothing to make less verbose in this subcommand.
2288 struct option options[] = {
2289 OPT_NOOP_NOARG('q', "quiet"),
2290 OPT_BOOL('d', "default", &opt_default,
2291 N_("set the default tracking branch to master")),
2292 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2293 N_("set the default tracking branch")),
2296 const char *const usage[] = {
2297 N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
2298 N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2302 argc = parse_options(argc, argv, prefix, options, usage, 0);
2304 if (!opt_branch && !opt_default)
2305 die(_("--branch or --default required"));
2307 if (opt_branch && opt_default)
2308 die(_("--branch and --default are mutually exclusive"));
2310 if (argc != 1 || !(path = argv[0]))
2311 usage_with_options(usage, options);
2313 config_name = xstrfmt("submodule.%s.branch", path);
2314 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2320 #define SUPPORT_SUPER_PREFIX (1<<0)
2324 int (*fn)(int, const char **, const char *);
2328 static struct cmd_struct commands[] = {
2329 {"list", module_list, 0},
2330 {"name", module_name, 0},
2331 {"clone", module_clone, 0},
2332 {"update-module-mode", module_update_module_mode, 0},
2333 {"update-clone", update_clone, 0},
2334 {"ensure-core-worktree", ensure_core_worktree, 0},
2335 {"relative-path", resolve_relative_path, 0},
2336 {"resolve-relative-url", resolve_relative_url, 0},
2337 {"resolve-relative-url-test", resolve_relative_url_test, 0},
2338 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
2339 {"init", module_init, SUPPORT_SUPER_PREFIX},
2340 {"status", module_status, SUPPORT_SUPER_PREFIX},
2341 {"print-default-remote", print_default_remote, 0},
2342 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
2343 {"deinit", module_deinit, 0},
2344 {"remote-branch", resolve_remote_submodule_branch, 0},
2345 {"push-check", push_check, 0},
2346 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
2347 {"is-active", is_active, 0},
2348 {"check-name", check_name, 0},
2349 {"config", module_config, 0},
2350 {"set-url", module_set_url, 0},
2351 {"set-branch", module_set_branch, 0},
2354 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2357 if (argc < 2 || !strcmp(argv[1], "-h"))
2358 usage("git submodule--helper <command>");
2360 for (i = 0; i < ARRAY_SIZE(commands); i++) {
2361 if (!strcmp(argv[1], commands[i].cmd)) {
2362 if (get_super_prefix() &&
2363 !(commands[i].option & SUPPORT_SUPER_PREFIX))
2364 die(_("%s doesn't support --super-prefix"),
2366 return commands[i].fn(argc - 1, argv + 1, prefix);
2370 die(_("'%s' is not a valid submodule--helper "
2371 "subcommand"), argv[1]);