2 #include "repository.h"
5 #include "parse-options.h"
10 #include "submodule-config.h"
11 #include "string-list.h"
12 #include "run-command.h"
21 #define OPT_QUIET (1 << 0)
22 #define OPT_CACHED (1 << 1)
23 #define OPT_RECURSIVE (1 << 2)
25 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
28 static char *get_default_remote(void)
30 char *dest = NULL, *ret;
31 struct strbuf sb = STRBUF_INIT;
32 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
35 die(_("No such ref: %s"), "HEAD");
38 if (!strcmp(refname, "HEAD"))
39 return xstrdup("origin");
41 if (!skip_prefix(refname, "refs/heads/", &refname))
42 die(_("Expecting a full ref name, got %s"), refname);
44 strbuf_addf(&sb, "branch.%s.remote", refname);
45 if (git_config_get_string(sb.buf, &dest))
46 ret = xstrdup("origin");
54 static int starts_with_dot_slash(const char *str)
56 return str[0] == '.' && is_dir_sep(str[1]);
59 static int starts_with_dot_dot_slash(const char *str)
61 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
65 * Returns 1 if it was the last chop before ':'.
67 static int chop_last_dir(char **remoteurl, int is_relative)
69 char *rfind = find_last_dir_sep(*remoteurl);
75 rfind = strrchr(*remoteurl, ':');
81 if (is_relative || !strcmp(".", *remoteurl))
82 die(_("cannot strip one component off url '%s'"),
86 *remoteurl = xstrdup(".");
91 * The `url` argument is the URL that navigates to the submodule origin
92 * repo. When relative, this URL is relative to the superproject origin
93 * URL repo. The `up_path` argument, if specified, is the relative
94 * path that navigates from the submodule working tree to the superproject
95 * working tree. Returns the origin URL of the submodule.
97 * Return either an absolute URL or filesystem path (if the superproject
98 * origin URL is an absolute URL or filesystem path, respectively) or a
99 * relative file system path (if the superproject origin URL is a relative
102 * When the output is a relative file system path, the path is either
103 * relative to the submodule working tree, if up_path is specified, or to
104 * the superproject working tree otherwise.
106 * NEEDSWORK: This works incorrectly on the domain and protocol part.
107 * remote_url url outcome expectation
108 * http://a.com/b ../c http://a.com/c as is
109 * http://a.com/b/ ../c http://a.com/c same as previous line, but
110 * ignore trailing slash in url
111 * http://a.com/b ../../c http://c error out
112 * http://a.com/b ../../../c http:/c error out
113 * http://a.com/b ../../../../c http:c error out
114 * http://a.com/b ../../../../../c .:c error out
115 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
116 * when a local part has a colon in its path component, too.
118 static char *relative_url(const char *remote_url,
125 char *remoteurl = xstrdup(remote_url);
126 struct strbuf sb = STRBUF_INIT;
127 size_t len = strlen(remoteurl);
129 if (is_dir_sep(remoteurl[len-1]))
130 remoteurl[len-1] = '\0';
132 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
137 * Prepend a './' to ensure all relative
138 * remoteurls start with './' or '../'
140 if (!starts_with_dot_slash(remoteurl) &&
141 !starts_with_dot_dot_slash(remoteurl)) {
143 strbuf_addf(&sb, "./%s", remoteurl);
145 remoteurl = strbuf_detach(&sb, NULL);
149 * When the url starts with '../', remove that and the
150 * last directory in remoteurl.
153 if (starts_with_dot_dot_slash(url)) {
155 colonsep |= chop_last_dir(&remoteurl, is_relative);
156 } else if (starts_with_dot_slash(url))
162 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
163 if (ends_with(url, "/"))
164 strbuf_setlen(&sb, sb.len - 1);
167 if (starts_with_dot_slash(sb.buf))
168 out = xstrdup(sb.buf + 2);
170 out = xstrdup(sb.buf);
173 if (!up_path || !is_relative)
176 strbuf_addf(&sb, "%s%s", up_path, out);
178 return strbuf_detach(&sb, NULL);
181 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
183 char *remoteurl = NULL;
184 char *remote = get_default_remote();
185 const char *up_path = NULL;
188 struct strbuf sb = STRBUF_INIT;
190 if (argc != 2 && argc != 3)
191 die("resolve-relative-url only accepts one or two arguments");
194 strbuf_addf(&sb, "remote.%s.url", remote);
197 if (git_config_get_string(sb.buf, &remoteurl))
198 /* the repository is its own authoritative upstream */
199 remoteurl = xgetcwd();
204 res = relative_url(remoteurl, url, up_path);
211 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
213 char *remoteurl, *res;
214 const char *up_path, *url;
217 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
220 remoteurl = xstrdup(argv[2]);
223 if (!strcmp(up_path, "(null)"))
226 res = relative_url(remoteurl, url, up_path);
233 /* the result should be freed by the caller. */
234 static char *get_submodule_displaypath(const char *path, const char *prefix)
236 const char *super_prefix = get_super_prefix();
238 if (prefix && super_prefix) {
239 BUG("cannot have prefix '%s' and superprefix '%s'",
240 prefix, super_prefix);
242 struct strbuf sb = STRBUF_INIT;
243 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
246 } else if (super_prefix) {
247 return xstrfmt("%s%s", super_prefix, path);
249 return xstrdup(path);
253 static char *compute_rev_name(const char *sub_path, const char* object_id)
255 struct strbuf sb = STRBUF_INIT;
258 static const char *describe_bare[] = { NULL };
260 static const char *describe_tags[] = { "--tags", NULL };
262 static const char *describe_contains[] = { "--contains", NULL };
264 static const char *describe_all_always[] = { "--all", "--always", NULL };
266 static const char **describe_argv[] = { describe_bare, describe_tags,
268 describe_all_always, NULL };
270 for (d = describe_argv; *d; d++) {
271 struct child_process cp = CHILD_PROCESS_INIT;
272 prepare_submodule_repo_env(&cp.env_array);
277 argv_array_push(&cp.args, "describe");
278 argv_array_pushv(&cp.args, *d);
279 argv_array_push(&cp.args, object_id);
281 if (!capture_command(&cp, &sb, 0)) {
282 strbuf_strip_suffix(&sb, "\n");
283 return strbuf_detach(&sb, NULL);
292 const struct cache_entry **entries;
295 #define MODULE_LIST_INIT { NULL, 0, 0 }
297 static int module_list_compute(int argc, const char **argv,
299 struct pathspec *pathspec,
300 struct module_list *list)
303 char *ps_matched = NULL;
304 parse_pathspec(pathspec, 0,
305 PATHSPEC_PREFER_FULL,
309 ps_matched = xcalloc(pathspec->nr, 1);
311 if (read_cache() < 0)
312 die(_("index file corrupt"));
314 for (i = 0; i < active_nr; i++) {
315 const struct cache_entry *ce = active_cache[i];
317 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
319 !S_ISGITLINK(ce->ce_mode))
322 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
323 list->entries[list->nr++] = ce;
324 while (i + 1 < active_nr &&
325 !strcmp(ce->name, active_cache[i + 1]->name))
327 * Skip entries with the same name in different stages
328 * to make sure an entry is returned only once.
333 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
341 static void module_list_active(struct module_list *list)
344 struct module_list active_modules = MODULE_LIST_INIT;
346 for (i = 0; i < list->nr; i++) {
347 const struct cache_entry *ce = list->entries[i];
349 if (!is_submodule_active(the_repository, ce->name))
352 ALLOC_GROW(active_modules.entries,
353 active_modules.nr + 1,
354 active_modules.alloc);
355 active_modules.entries[active_modules.nr++] = ce;
359 *list = active_modules;
362 static int module_list(int argc, const char **argv, const char *prefix)
365 struct pathspec pathspec;
366 struct module_list list = MODULE_LIST_INIT;
368 struct option module_list_options[] = {
369 OPT_STRING(0, "prefix", &prefix,
371 N_("alternative anchor for relative paths")),
375 const char *const git_submodule_helper_usage[] = {
376 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
380 argc = parse_options(argc, argv, prefix, module_list_options,
381 git_submodule_helper_usage, 0);
383 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
386 for (i = 0; i < list.nr; i++) {
387 const struct cache_entry *ce = list.entries[i];
390 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
392 printf("%06o %s %d\t", ce->ce_mode,
393 oid_to_hex(&ce->oid), ce_stage(ce));
395 fprintf(stdout, "%s\n", ce->name);
400 static void for_each_listed_submodule(const struct module_list *list,
401 each_submodule_fn fn, void *cb_data)
404 for (i = 0; i < list->nr; i++)
405 fn(list->entries[i], cb_data);
413 #define INIT_CB_INIT { NULL, 0 }
415 static void init_submodule(const char *path, const char *prefix,
418 const struct submodule *sub;
419 struct strbuf sb = STRBUF_INIT;
420 char *upd = NULL, *url = NULL, *displaypath;
422 displaypath = get_submodule_displaypath(path, prefix);
424 sub = submodule_from_path(&null_oid, path);
427 die(_("No url found for submodule path '%s' in .gitmodules"),
431 * NEEDSWORK: In a multi-working-tree world, this needs to be
432 * set in the per-worktree config.
434 * Set active flag for the submodule being initialized
436 if (!is_submodule_active(the_repository, path)) {
437 strbuf_addf(&sb, "submodule.%s.active", sub->name);
438 git_config_set_gently(sb.buf, "true");
443 * Copy url setting when it is not set yet.
444 * To look up the url in .git/config, we must not fall back to
445 * .gitmodules, so look it up directly.
447 strbuf_addf(&sb, "submodule.%s.url", sub->name);
448 if (git_config_get_string(sb.buf, &url)) {
450 die(_("No url found for submodule path '%s' in .gitmodules"),
453 url = xstrdup(sub->url);
455 /* Possibly a url relative to parent */
456 if (starts_with_dot_dot_slash(url) ||
457 starts_with_dot_slash(url)) {
458 char *remoteurl, *relurl;
459 char *remote = get_default_remote();
460 struct strbuf remotesb = STRBUF_INIT;
461 strbuf_addf(&remotesb, "remote.%s.url", remote);
464 if (git_config_get_string(remotesb.buf, &remoteurl)) {
465 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
466 remoteurl = xgetcwd();
468 relurl = relative_url(remoteurl, url, NULL);
469 strbuf_release(&remotesb);
475 if (git_config_set_gently(sb.buf, url))
476 die(_("Failed to register url for submodule path '%s'"),
478 if (!(flags & OPT_QUIET))
480 _("Submodule '%s' (%s) registered for path '%s'\n"),
481 sub->name, url, displaypath);
485 /* Copy "update" setting when it is not set yet */
486 strbuf_addf(&sb, "submodule.%s.update", sub->name);
487 if (git_config_get_string(sb.buf, &upd) &&
488 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
489 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
490 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
492 upd = xstrdup("none");
494 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
496 if (git_config_set_gently(sb.buf, upd))
497 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
505 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
507 struct init_cb *info = cb_data;
508 init_submodule(list_item->name, info->prefix, info->flags);
511 static int module_init(int argc, const char **argv, const char *prefix)
513 struct init_cb info = INIT_CB_INIT;
514 struct pathspec pathspec;
515 struct module_list list = MODULE_LIST_INIT;
518 struct option module_init_options[] = {
519 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
523 const char *const git_submodule_helper_usage[] = {
524 N_("git submodule--helper init [<path>]"),
528 argc = parse_options(argc, argv, prefix, module_init_options,
529 git_submodule_helper_usage, 0);
531 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
535 * If there are no path args and submodule.active is set then,
536 * by default, only initialize 'active' modules.
538 if (!argc && git_config_get_value_multi("submodule.active"))
539 module_list_active(&list);
541 info.prefix = prefix;
543 info.flags |= OPT_QUIET;
545 for_each_listed_submodule(&list, init_submodule_cb, &info);
555 #define STATUS_CB_INIT { NULL, 0 }
557 static void print_status(unsigned int flags, char state, const char *path,
558 const struct object_id *oid, const char *displaypath)
560 if (flags & OPT_QUIET)
563 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
565 if (state == ' ' || state == '+')
566 printf(" (%s)", compute_rev_name(path, oid_to_hex(oid)));
571 static int handle_submodule_head_ref(const char *refname,
572 const struct object_id *oid, int flags,
575 struct object_id *output = cb_data;
582 static void status_submodule(const char *path, const struct object_id *ce_oid,
583 unsigned int ce_flags, const char *prefix,
587 struct argv_array diff_files_args = ARGV_ARRAY_INIT;
589 int diff_files_result;
591 if (!submodule_from_path(&null_oid, path))
592 die(_("no submodule mapping found in .gitmodules for path '%s'"),
595 displaypath = get_submodule_displaypath(path, prefix);
597 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
598 print_status(flags, 'U', path, &null_oid, displaypath);
602 if (!is_submodule_active(the_repository, path)) {
603 print_status(flags, '-', path, ce_oid, displaypath);
607 argv_array_pushl(&diff_files_args, "diff-files",
608 "--ignore-submodules=dirty", "--quiet", "--",
611 git_config(git_diff_basic_config, NULL);
612 init_revisions(&rev, prefix);
614 diff_files_args.argc = setup_revisions(diff_files_args.argc,
615 diff_files_args.argv,
617 diff_files_result = run_diff_files(&rev, 0);
619 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
620 print_status(flags, ' ', path, ce_oid,
622 } else if (!(flags & OPT_CACHED)) {
623 struct object_id oid;
625 if (refs_head_ref(get_submodule_ref_store(path),
626 handle_submodule_head_ref, &oid))
627 die(_("could not resolve HEAD ref inside the "
628 "submodule '%s'"), path);
630 print_status(flags, '+', path, &oid, displaypath);
632 print_status(flags, '+', path, ce_oid, displaypath);
635 if (flags & OPT_RECURSIVE) {
636 struct child_process cpr = CHILD_PROCESS_INIT;
640 prepare_submodule_repo_env(&cpr.env_array);
642 argv_array_push(&cpr.args, "--super-prefix");
643 argv_array_pushf(&cpr.args, "%s/", displaypath);
644 argv_array_pushl(&cpr.args, "submodule--helper", "status",
645 "--recursive", NULL);
647 if (flags & OPT_CACHED)
648 argv_array_push(&cpr.args, "--cached");
650 if (flags & OPT_QUIET)
651 argv_array_push(&cpr.args, "--quiet");
653 if (run_command(&cpr))
654 die(_("failed to recurse into submodule '%s'"), path);
658 argv_array_clear(&diff_files_args);
662 static void status_submodule_cb(const struct cache_entry *list_item,
665 struct status_cb *info = cb_data;
666 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
667 info->prefix, info->flags);
670 static int module_status(int argc, const char **argv, const char *prefix)
672 struct status_cb info = STATUS_CB_INIT;
673 struct pathspec pathspec;
674 struct module_list list = MODULE_LIST_INIT;
677 struct option module_status_options[] = {
678 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
679 OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
680 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
684 const char *const git_submodule_helper_usage[] = {
685 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
689 argc = parse_options(argc, argv, prefix, module_status_options,
690 git_submodule_helper_usage, 0);
692 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
695 info.prefix = prefix;
697 info.flags |= OPT_QUIET;
699 for_each_listed_submodule(&list, status_submodule_cb, &info);
704 static int module_name(int argc, const char **argv, const char *prefix)
706 const struct submodule *sub;
709 usage(_("git submodule--helper name <path>"));
711 sub = submodule_from_path(&null_oid, argv[1]);
714 die(_("no submodule mapping found in .gitmodules for path '%s'"),
717 printf("%s\n", sub->name);
722 static int clone_submodule(const char *path, const char *gitdir, const char *url,
723 const char *depth, struct string_list *reference,
724 int quiet, int progress)
726 struct child_process cp = CHILD_PROCESS_INIT;
728 argv_array_push(&cp.args, "clone");
729 argv_array_push(&cp.args, "--no-checkout");
731 argv_array_push(&cp.args, "--quiet");
733 argv_array_push(&cp.args, "--progress");
735 argv_array_pushl(&cp.args, "--depth", depth, NULL);
737 struct string_list_item *item;
738 for_each_string_list_item(item, reference)
739 argv_array_pushl(&cp.args, "--reference",
742 if (gitdir && *gitdir)
743 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
745 argv_array_push(&cp.args, "--");
746 argv_array_push(&cp.args, url);
747 argv_array_push(&cp.args, path);
750 prepare_submodule_repo_env(&cp.env_array);
753 return run_command(&cp);
756 struct submodule_alternate_setup {
757 const char *submodule_name;
758 enum SUBMODULE_ALTERNATE_ERROR_MODE {
759 SUBMODULE_ALTERNATE_ERROR_DIE,
760 SUBMODULE_ALTERNATE_ERROR_INFO,
761 SUBMODULE_ALTERNATE_ERROR_IGNORE
763 struct string_list *reference;
765 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
766 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
768 static int add_possible_reference_from_superproject(
769 struct alternate_object_database *alt, void *sas_cb)
771 struct submodule_alternate_setup *sas = sas_cb;
774 * If the alternate object store is another repository, try the
775 * standard layout with .git/(modules/<name>)+/objects
777 if (ends_with(alt->path, "/objects")) {
779 struct strbuf sb = STRBUF_INIT;
780 struct strbuf err = STRBUF_INIT;
781 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
784 * We need to end the new path with '/' to mark it as a dir,
785 * otherwise a submodule name containing '/' will be broken
786 * as the last part of a missing submodule reference would
787 * be taken as a file name.
789 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
791 sm_alternate = compute_alternate_path(sb.buf, &err);
793 string_list_append(sas->reference, xstrdup(sb.buf));
796 switch (sas->error_mode) {
797 case SUBMODULE_ALTERNATE_ERROR_DIE:
798 die(_("submodule '%s' cannot add alternate: %s"),
799 sas->submodule_name, err.buf);
800 case SUBMODULE_ALTERNATE_ERROR_INFO:
801 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
802 sas->submodule_name, err.buf);
803 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
813 static void prepare_possible_alternates(const char *sm_name,
814 struct string_list *reference)
816 char *sm_alternate = NULL, *error_strategy = NULL;
817 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
819 git_config_get_string("submodule.alternateLocation", &sm_alternate);
823 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
826 error_strategy = xstrdup("die");
828 sas.submodule_name = sm_name;
829 sas.reference = reference;
830 if (!strcmp(error_strategy, "die"))
831 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
832 else if (!strcmp(error_strategy, "info"))
833 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
834 else if (!strcmp(error_strategy, "ignore"))
835 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
837 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
839 if (!strcmp(sm_alternate, "superproject"))
840 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
841 else if (!strcmp(sm_alternate, "no"))
844 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
847 free(error_strategy);
850 static int module_clone(int argc, const char **argv, const char *prefix)
852 const char *name = NULL, *url = NULL, *depth = NULL;
855 char *p, *path = NULL, *sm_gitdir;
856 struct strbuf sb = STRBUF_INIT;
857 struct string_list reference = STRING_LIST_INIT_NODUP;
858 int require_init = 0;
859 char *sm_alternate = NULL, *error_strategy = NULL;
861 struct option module_clone_options[] = {
862 OPT_STRING(0, "prefix", &prefix,
864 N_("alternative anchor for relative paths")),
865 OPT_STRING(0, "path", &path,
867 N_("where the new submodule will be cloned to")),
868 OPT_STRING(0, "name", &name,
870 N_("name of the new submodule")),
871 OPT_STRING(0, "url", &url,
873 N_("url where to clone the submodule from")),
874 OPT_STRING_LIST(0, "reference", &reference,
876 N_("reference repository")),
877 OPT_STRING(0, "depth", &depth,
879 N_("depth for shallow clones")),
880 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
881 OPT_BOOL(0, "progress", &progress,
882 N_("force cloning progress")),
883 OPT_BOOL(0, "require-init", &require_init,
884 N_("disallow cloning into non-empty directory")),
888 const char *const git_submodule_helper_usage[] = {
889 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
890 "[--reference <repository>] [--name <name>] [--depth <depth>] "
891 "--url <url> --path <path>"),
895 argc = parse_options(argc, argv, prefix, module_clone_options,
896 git_submodule_helper_usage, 0);
898 if (argc || !url || !path || !*path)
899 usage_with_options(git_submodule_helper_usage,
900 module_clone_options);
902 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
903 sm_gitdir = absolute_pathdup(sb.buf);
906 if (!is_absolute_path(path)) {
907 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
908 path = strbuf_detach(&sb, NULL);
910 path = xstrdup(path);
912 if (validate_submodule_git_dir(sm_gitdir, name) < 0)
913 die(_("refusing to create/use '%s' in another submodule's "
914 "git dir"), sm_gitdir);
916 if (!file_exists(sm_gitdir)) {
917 if (safe_create_leading_directories_const(sm_gitdir) < 0)
918 die(_("could not create directory '%s'"), sm_gitdir);
920 prepare_possible_alternates(name, &reference);
922 if (clone_submodule(path, sm_gitdir, url, depth, &reference,
924 die(_("clone of '%s' into submodule path '%s' failed"),
927 if (require_init && !access(path, X_OK) && !is_empty_dir(path))
928 die(_("directory not empty: '%s'"), path);
929 if (safe_create_leading_directories_const(path) < 0)
930 die(_("could not create directory '%s'"), path);
931 strbuf_addf(&sb, "%s/index", sm_gitdir);
932 unlink_or_warn(sb.buf);
936 /* Connect module worktree and git dir */
937 connect_work_tree_and_git_dir(path, sm_gitdir);
939 p = git_pathdup_submodule(path, "config");
941 die(_("could not get submodule directory for '%s'"), path);
943 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
944 git_config_get_string("submodule.alternateLocation", &sm_alternate);
946 git_config_set_in_file(p, "submodule.alternateLocation",
948 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
950 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
954 free(error_strategy);
963 struct submodule_update_clone {
964 /* index into 'list', the list of submodules to look into for cloning */
966 struct module_list list;
967 unsigned warn_if_uninitialized : 1;
969 /* update parameter passed via commandline */
970 struct submodule_update_strategy update;
972 /* configuration parameters which are passed on to the children */
975 int recommend_shallow;
976 struct string_list references;
977 unsigned require_init;
979 const char *recursive_prefix;
982 /* Machine-readable status lines to be consumed by git-submodule.sh */
983 struct string_list projectlines;
985 /* If we want to stop as fast as possible and return an error */
986 unsigned quickstop : 1;
988 /* failed clones to be retried again */
989 const struct cache_entry **failed_clones;
990 int failed_clones_nr, failed_clones_alloc;
992 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
993 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
995 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
998 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
999 struct strbuf *out, const char *displaypath)
1002 * Only mention uninitialized submodules when their
1003 * paths have been specified.
1005 if (suc->warn_if_uninitialized) {
1007 _("Submodule path '%s' not initialized"),
1009 strbuf_addch(out, '\n');
1011 _("Maybe you want to use 'update --init'?"));
1012 strbuf_addch(out, '\n');
1017 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1018 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1020 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1021 struct child_process *child,
1022 struct submodule_update_clone *suc,
1025 const struct submodule *sub = NULL;
1026 const char *url = NULL;
1027 const char *update_string;
1028 enum submodule_update_type update_type;
1030 struct strbuf displaypath_sb = STRBUF_INIT;
1031 struct strbuf sb = STRBUF_INIT;
1032 const char *displaypath = NULL;
1033 int needs_cloning = 0;
1036 if (suc->recursive_prefix)
1037 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1039 strbuf_addstr(&sb, ce->name);
1040 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1041 strbuf_addch(out, '\n');
1045 sub = submodule_from_path(&null_oid, ce->name);
1047 if (suc->recursive_prefix)
1048 displaypath = relative_path(suc->recursive_prefix,
1049 ce->name, &displaypath_sb);
1051 displaypath = ce->name;
1054 next_submodule_warn_missing(suc, out, displaypath);
1058 key = xstrfmt("submodule.%s.update", sub->name);
1059 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1060 update_type = parse_submodule_update_type(update_string);
1062 update_type = sub->update_strategy.type;
1066 if (suc->update.type == SM_UPDATE_NONE
1067 || (suc->update.type == SM_UPDATE_UNSPECIFIED
1068 && update_type == SM_UPDATE_NONE)) {
1069 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1070 strbuf_addch(out, '\n');
1074 /* Check if the submodule has been initialized. */
1075 if (!is_submodule_active(the_repository, ce->name)) {
1076 next_submodule_warn_missing(suc, out, displaypath);
1081 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1082 if (repo_config_get_string_const(the_repository, sb.buf, &url))
1086 strbuf_addf(&sb, "%s/.git", ce->name);
1087 needs_cloning = !file_exists(sb.buf);
1090 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
1091 oid_to_hex(&ce->oid), ce_stage(ce),
1092 needs_cloning, ce->name);
1093 string_list_append(&suc->projectlines, sb.buf);
1099 child->no_stdin = 1;
1100 child->stdout_to_stderr = 1;
1102 argv_array_push(&child->args, "submodule--helper");
1103 argv_array_push(&child->args, "clone");
1105 argv_array_push(&child->args, "--progress");
1107 argv_array_push(&child->args, "--quiet");
1109 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1110 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1111 argv_array_push(&child->args, "--depth=1");
1112 if (suc->require_init)
1113 argv_array_push(&child->args, "--require-init");
1114 argv_array_pushl(&child->args, "--path", sub->path, NULL);
1115 argv_array_pushl(&child->args, "--name", sub->name, NULL);
1116 argv_array_pushl(&child->args, "--url", url, NULL);
1117 if (suc->references.nr) {
1118 struct string_list_item *item;
1119 for_each_string_list_item(item, &suc->references)
1120 argv_array_pushl(&child->args, "--reference", item->string, NULL);
1123 argv_array_push(&child->args, suc->depth);
1126 strbuf_reset(&displaypath_sb);
1129 return needs_cloning;
1132 static int update_clone_get_next_task(struct child_process *child,
1137 struct submodule_update_clone *suc = suc_cb;
1138 const struct cache_entry *ce;
1141 for (; suc->current < suc->list.nr; suc->current++) {
1142 ce = suc->list.entries[suc->current];
1143 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1144 int *p = xmalloc(sizeof(*p));
1153 * The loop above tried cloning each submodule once, now try the
1154 * stragglers again, which we can imagine as an extension of the
1157 index = suc->current - suc->list.nr;
1158 if (index < suc->failed_clones_nr) {
1160 ce = suc->failed_clones[index];
1161 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1163 strbuf_addstr(err, "BUG: submodule considered for "
1164 "cloning, doesn't need cloning "
1168 p = xmalloc(sizeof(*p));
1178 static int update_clone_start_failure(struct strbuf *err,
1182 struct submodule_update_clone *suc = suc_cb;
1187 static int update_clone_task_finished(int result,
1192 const struct cache_entry *ce;
1193 struct submodule_update_clone *suc = suc_cb;
1195 int *idxP = idx_task_cb;
1202 if (idx < suc->list.nr) {
1203 ce = suc->list.entries[idx];
1204 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1206 strbuf_addch(err, '\n');
1207 ALLOC_GROW(suc->failed_clones,
1208 suc->failed_clones_nr + 1,
1209 suc->failed_clones_alloc);
1210 suc->failed_clones[suc->failed_clones_nr++] = ce;
1213 idx -= suc->list.nr;
1214 ce = suc->failed_clones[idx];
1215 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1217 strbuf_addch(err, '\n');
1225 static int gitmodules_update_clone_config(const char *var, const char *value,
1229 if (!strcmp(var, "submodule.fetchjobs"))
1230 *max_jobs = parse_submodule_fetchjobs(var, value);
1234 static int update_clone(int argc, const char **argv, const char *prefix)
1236 const char *update = NULL;
1238 struct string_list_item *item;
1239 struct pathspec pathspec;
1240 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1242 struct option module_update_clone_options[] = {
1243 OPT_STRING(0, "prefix", &prefix,
1245 N_("path into the working tree")),
1246 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1248 N_("path into the working tree, across nested "
1249 "submodule boundaries")),
1250 OPT_STRING(0, "update", &update,
1252 N_("rebase, merge, checkout or none")),
1253 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1254 N_("reference repository")),
1255 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1256 N_("Create a shallow clone truncated to the "
1257 "specified number of revisions")),
1258 OPT_INTEGER('j', "jobs", &max_jobs,
1259 N_("parallel jobs")),
1260 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1261 N_("whether the initial clone should follow the shallow recommendation")),
1262 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1263 OPT_BOOL(0, "progress", &suc.progress,
1264 N_("force cloning progress")),
1265 OPT_BOOL(0, "require-init", &suc.require_init,
1266 N_("disallow cloning into non-empty directory")),
1270 const char *const git_submodule_helper_usage[] = {
1271 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1274 suc.prefix = prefix;
1276 config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
1277 git_config(gitmodules_update_clone_config, &max_jobs);
1279 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1280 git_submodule_helper_usage, 0);
1283 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1284 die(_("bad value for update parameter"));
1286 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1290 suc.warn_if_uninitialized = 1;
1292 run_processes_parallel(max_jobs,
1293 update_clone_get_next_task,
1294 update_clone_start_failure,
1295 update_clone_task_finished,
1299 * We saved the output and put it out all at once now.
1301 * - the listener does not have to interleave their (checkout)
1302 * work with our fetching. The writes involved in a
1303 * checkout involve more straightforward sequential I/O.
1304 * - the listener can avoid doing any work if fetching failed.
1309 for_each_string_list_item(item, &suc.projectlines)
1310 fprintf(stdout, "%s", item->string);
1315 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1317 struct strbuf sb = STRBUF_INIT;
1319 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1321 printf("%s", relative_path(argv[1], argv[2], &sb));
1322 strbuf_release(&sb);
1326 static const char *remote_submodule_branch(const char *path)
1328 const struct submodule *sub;
1329 const char *branch = NULL;
1332 sub = submodule_from_path(&null_oid, path);
1336 key = xstrfmt("submodule.%s.branch", sub->name);
1337 if (repo_config_get_string_const(the_repository, key, &branch))
1338 branch = sub->branch;
1344 if (!strcmp(branch, ".")) {
1345 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1348 die(_("No such ref: %s"), "HEAD");
1351 if (!strcmp(refname, "HEAD"))
1352 die(_("Submodule (%s) branch configured to inherit "
1353 "branch from superproject, but the superproject "
1354 "is not on any branch"), sub->name);
1356 if (!skip_prefix(refname, "refs/heads/", &refname))
1357 die(_("Expecting a full ref name, got %s"), refname);
1364 static int resolve_remote_submodule_branch(int argc, const char **argv,
1368 struct strbuf sb = STRBUF_INIT;
1370 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1372 ret = remote_submodule_branch(argv[1]);
1374 die("submodule %s doesn't exist", argv[1]);
1377 strbuf_release(&sb);
1381 static int push_check(int argc, const char **argv, const char *prefix)
1383 struct remote *remote;
1384 const char *superproject_head;
1386 int detached_head = 0;
1387 struct object_id head_oid;
1390 die("submodule--helper push-check requires at least 2 arguments");
1393 * superproject's resolved head ref.
1394 * if HEAD then the superproject is in a detached head state, otherwise
1395 * it will be the resolved head ref.
1397 superproject_head = argv[1];
1400 /* Get the submodule's head ref and determine if it is detached */
1401 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1403 die(_("Failed to resolve HEAD as a valid ref."));
1404 if (!strcmp(head, "HEAD"))
1408 * The remote must be configured.
1409 * This is to avoid pushing to the exact same URL as the parent.
1411 remote = pushremote_get(argv[1]);
1412 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1413 die("remote '%s' not configured", argv[1]);
1415 /* Check the refspec */
1417 int i, refspec_nr = argc - 2;
1418 struct ref *local_refs = get_local_heads();
1419 struct refspec *refspec = parse_push_refspec(refspec_nr,
1422 for (i = 0; i < refspec_nr; i++) {
1423 struct refspec *rs = refspec + i;
1425 if (rs->pattern || rs->matching)
1428 /* LHS must match a single ref */
1429 switch (count_refspec_match(rs->src, local_refs, NULL)) {
1434 * If LHS matches 'HEAD' then we need to ensure
1435 * that it matches the same named branch
1436 * checked out in the superproject.
1438 if (!strcmp(rs->src, "HEAD")) {
1439 if (!detached_head &&
1440 !strcmp(head, superproject_head))
1442 die("HEAD does not match the named branch in the superproject");
1446 die("src refspec '%s' must name a ref",
1450 free_refspec(refspec_nr, refspec);
1457 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1460 struct pathspec pathspec;
1461 struct module_list list = MODULE_LIST_INIT;
1462 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1464 struct option embed_gitdir_options[] = {
1465 OPT_STRING(0, "prefix", &prefix,
1467 N_("path into the working tree")),
1468 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1469 ABSORB_GITDIR_RECURSE_SUBMODULES),
1473 const char *const git_submodule_helper_usage[] = {
1474 N_("git submodule--helper embed-git-dir [<path>...]"),
1478 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1479 git_submodule_helper_usage, 0);
1481 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1484 for (i = 0; i < list.nr; i++)
1485 absorb_git_dir_into_superproject(prefix,
1486 list.entries[i]->name, flags);
1491 static int is_active(int argc, const char **argv, const char *prefix)
1494 die("submodule--helper is-active takes exactly 1 argument");
1496 return !is_submodule_active(the_repository, argv[1]);
1500 * Exit non-zero if any of the submodule names given on the command line is
1501 * invalid. If no names are given, filter stdin to print only valid names
1502 * (which is primarily intended for testing).
1504 static int check_name(int argc, const char **argv, const char *prefix)
1508 if (check_submodule_name(*argv) < 0)
1512 struct strbuf buf = STRBUF_INIT;
1513 while (strbuf_getline(&buf, stdin) != EOF) {
1514 if (!check_submodule_name(buf.buf))
1515 printf("%s\n", buf.buf);
1517 strbuf_release(&buf);
1522 #define SUPPORT_SUPER_PREFIX (1<<0)
1526 int (*fn)(int, const char **, const char *);
1530 static struct cmd_struct commands[] = {
1531 {"list", module_list, 0},
1532 {"name", module_name, 0},
1533 {"clone", module_clone, 0},
1534 {"update-clone", update_clone, 0},
1535 {"relative-path", resolve_relative_path, 0},
1536 {"resolve-relative-url", resolve_relative_url, 0},
1537 {"resolve-relative-url-test", resolve_relative_url_test, 0},
1538 {"init", module_init, SUPPORT_SUPER_PREFIX},
1539 {"status", module_status, SUPPORT_SUPER_PREFIX},
1540 {"remote-branch", resolve_remote_submodule_branch, 0},
1541 {"push-check", push_check, 0},
1542 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1543 {"is-active", is_active, 0},
1544 {"check-name", check_name, 0},
1547 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
1550 if (argc < 2 || !strcmp(argv[1], "-h"))
1551 usage("git submodule--helper <command>");
1553 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1554 if (!strcmp(argv[1], commands[i].cmd)) {
1555 if (get_super_prefix() &&
1556 !(commands[i].option & SUPPORT_SUPER_PREFIX))
1557 die(_("%s doesn't support --super-prefix"),
1559 return commands[i].fn(argc - 1, argv + 1, prefix);
1563 die(_("'%s' is not a valid submodule--helper "
1564 "subcommand"), argv[1]);