2 #include "repository.h"
5 #include "parse-options.h"
10 #include "submodule-config.h"
11 #include "string-list.h"
12 #include "run-command.h"
18 static char *get_default_remote(void)
20 char *dest = NULL, *ret;
21 unsigned char sha1[20];
22 struct strbuf sb = STRBUF_INIT;
23 const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
26 die(_("No such ref: %s"), "HEAD");
29 if (!strcmp(refname, "HEAD"))
30 return xstrdup("origin");
32 if (!skip_prefix(refname, "refs/heads/", &refname))
33 die(_("Expecting a full ref name, got %s"), refname);
35 strbuf_addf(&sb, "branch.%s.remote", refname);
36 if (git_config_get_string(sb.buf, &dest))
37 ret = xstrdup("origin");
45 static int starts_with_dot_slash(const char *str)
47 return str[0] == '.' && is_dir_sep(str[1]);
50 static int starts_with_dot_dot_slash(const char *str)
52 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
56 * Returns 1 if it was the last chop before ':'.
58 static int chop_last_dir(char **remoteurl, int is_relative)
60 char *rfind = find_last_dir_sep(*remoteurl);
66 rfind = strrchr(*remoteurl, ':');
72 if (is_relative || !strcmp(".", *remoteurl))
73 die(_("cannot strip one component off url '%s'"),
77 *remoteurl = xstrdup(".");
82 * The `url` argument is the URL that navigates to the submodule origin
83 * repo. When relative, this URL is relative to the superproject origin
84 * URL repo. The `up_path` argument, if specified, is the relative
85 * path that navigates from the submodule working tree to the superproject
86 * working tree. Returns the origin URL of the submodule.
88 * Return either an absolute URL or filesystem path (if the superproject
89 * origin URL is an absolute URL or filesystem path, respectively) or a
90 * relative file system path (if the superproject origin URL is a relative
93 * When the output is a relative file system path, the path is either
94 * relative to the submodule working tree, if up_path is specified, or to
95 * the superproject working tree otherwise.
97 * NEEDSWORK: This works incorrectly on the domain and protocol part.
98 * remote_url url outcome expectation
99 * http://a.com/b ../c http://a.com/c as is
100 * http://a.com/b/ ../c http://a.com/c same as previous line, but
101 * ignore trailing slash in url
102 * http://a.com/b ../../c http://c error out
103 * http://a.com/b ../../../c http:/c error out
104 * http://a.com/b ../../../../c http:c error out
105 * http://a.com/b ../../../../../c .:c error out
106 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
107 * when a local part has a colon in its path component, too.
109 static char *relative_url(const char *remote_url,
116 char *remoteurl = xstrdup(remote_url);
117 struct strbuf sb = STRBUF_INIT;
118 size_t len = strlen(remoteurl);
120 if (is_dir_sep(remoteurl[len-1]))
121 remoteurl[len-1] = '\0';
123 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
128 * Prepend a './' to ensure all relative
129 * remoteurls start with './' or '../'
131 if (!starts_with_dot_slash(remoteurl) &&
132 !starts_with_dot_dot_slash(remoteurl)) {
134 strbuf_addf(&sb, "./%s", remoteurl);
136 remoteurl = strbuf_detach(&sb, NULL);
140 * When the url starts with '../', remove that and the
141 * last directory in remoteurl.
144 if (starts_with_dot_dot_slash(url)) {
146 colonsep |= chop_last_dir(&remoteurl, is_relative);
147 } else if (starts_with_dot_slash(url))
153 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
154 if (ends_with(url, "/"))
155 strbuf_setlen(&sb, sb.len - 1);
158 if (starts_with_dot_slash(sb.buf))
159 out = xstrdup(sb.buf + 2);
161 out = xstrdup(sb.buf);
164 if (!up_path || !is_relative)
167 strbuf_addf(&sb, "%s%s", up_path, out);
169 return strbuf_detach(&sb, NULL);
172 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
174 char *remoteurl = NULL;
175 char *remote = get_default_remote();
176 const char *up_path = NULL;
179 struct strbuf sb = STRBUF_INIT;
181 if (argc != 2 && argc != 3)
182 die("resolve-relative-url only accepts one or two arguments");
185 strbuf_addf(&sb, "remote.%s.url", remote);
188 if (git_config_get_string(sb.buf, &remoteurl))
189 /* the repository is its own authoritative upstream */
190 remoteurl = xgetcwd();
195 res = relative_url(remoteurl, url, up_path);
202 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
204 char *remoteurl, *res;
205 const char *up_path, *url;
208 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
211 remoteurl = xstrdup(argv[2]);
214 if (!strcmp(up_path, "(null)"))
217 res = relative_url(remoteurl, url, up_path);
225 const struct cache_entry **entries;
228 #define MODULE_LIST_INIT { NULL, 0, 0 }
230 static int module_list_compute(int argc, const char **argv,
232 struct pathspec *pathspec,
233 struct module_list *list)
236 char *ps_matched = NULL;
237 parse_pathspec(pathspec, 0,
238 PATHSPEC_PREFER_FULL,
242 ps_matched = xcalloc(pathspec->nr, 1);
244 if (read_cache() < 0)
245 die(_("index file corrupt"));
247 for (i = 0; i < active_nr; i++) {
248 const struct cache_entry *ce = active_cache[i];
250 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
252 !S_ISGITLINK(ce->ce_mode))
255 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
256 list->entries[list->nr++] = ce;
257 while (i + 1 < active_nr &&
258 !strcmp(ce->name, active_cache[i + 1]->name))
260 * Skip entries with the same name in different stages
261 * to make sure an entry is returned only once.
266 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
274 static void module_list_active(struct module_list *list)
277 struct module_list active_modules = MODULE_LIST_INIT;
281 for (i = 0; i < list->nr; i++) {
282 const struct cache_entry *ce = list->entries[i];
284 if (!is_submodule_active(the_repository, ce->name))
287 ALLOC_GROW(active_modules.entries,
288 active_modules.nr + 1,
289 active_modules.alloc);
290 active_modules.entries[active_modules.nr++] = ce;
294 *list = active_modules;
297 static int module_list(int argc, const char **argv, const char *prefix)
300 struct pathspec pathspec;
301 struct module_list list = MODULE_LIST_INIT;
303 struct option module_list_options[] = {
304 OPT_STRING(0, "prefix", &prefix,
306 N_("alternative anchor for relative paths")),
310 const char *const git_submodule_helper_usage[] = {
311 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
315 argc = parse_options(argc, argv, prefix, module_list_options,
316 git_submodule_helper_usage, 0);
318 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
321 for (i = 0; i < list.nr; i++) {
322 const struct cache_entry *ce = list.entries[i];
325 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
327 printf("%06o %s %d\t", ce->ce_mode,
328 oid_to_hex(&ce->oid), ce_stage(ce));
330 fprintf(stdout, "%s\n", ce->name);
335 static void init_submodule(const char *path, const char *prefix, int quiet)
337 const struct submodule *sub;
338 struct strbuf sb = STRBUF_INIT;
339 char *upd = NULL, *url = NULL, *displaypath;
341 /* Only loads from .gitmodules, no overlay with .git/config */
344 if (prefix && get_super_prefix())
345 die("BUG: cannot have prefix and superprefix");
347 displaypath = xstrdup(relative_path(path, prefix, &sb));
348 else if (get_super_prefix()) {
349 strbuf_addf(&sb, "%s%s", get_super_prefix(), path);
350 displaypath = strbuf_detach(&sb, NULL);
352 displaypath = xstrdup(path);
354 sub = submodule_from_path(null_sha1, path);
357 die(_("No url found for submodule path '%s' in .gitmodules"),
361 * NEEDSWORK: In a multi-working-tree world, this needs to be
362 * set in the per-worktree config.
364 * Set active flag for the submodule being initialized
366 if (!is_submodule_active(the_repository, path)) {
368 strbuf_addf(&sb, "submodule.%s.active", sub->name);
369 git_config_set_gently(sb.buf, "true");
373 * Copy url setting when it is not set yet.
374 * To look up the url in .git/config, we must not fall back to
375 * .gitmodules, so look it up directly.
378 strbuf_addf(&sb, "submodule.%s.url", sub->name);
379 if (git_config_get_string(sb.buf, &url)) {
381 die(_("No url found for submodule path '%s' in .gitmodules"),
384 url = xstrdup(sub->url);
386 /* Possibly a url relative to parent */
387 if (starts_with_dot_dot_slash(url) ||
388 starts_with_dot_slash(url)) {
389 char *remoteurl, *relurl;
390 char *remote = get_default_remote();
391 struct strbuf remotesb = STRBUF_INIT;
392 strbuf_addf(&remotesb, "remote.%s.url", remote);
395 if (git_config_get_string(remotesb.buf, &remoteurl)) {
396 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
397 remoteurl = xgetcwd();
399 relurl = relative_url(remoteurl, url, NULL);
400 strbuf_release(&remotesb);
406 if (git_config_set_gently(sb.buf, url))
407 die(_("Failed to register url for submodule path '%s'"),
411 _("Submodule '%s' (%s) registered for path '%s'\n"),
412 sub->name, url, displaypath);
415 /* Copy "update" setting when it is not set yet */
417 strbuf_addf(&sb, "submodule.%s.update", sub->name);
418 if (git_config_get_string(sb.buf, &upd) &&
419 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
420 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
421 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
423 upd = xstrdup("none");
425 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
427 if (git_config_set_gently(sb.buf, upd))
428 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
436 static int module_init(int argc, const char **argv, const char *prefix)
438 struct pathspec pathspec;
439 struct module_list list = MODULE_LIST_INIT;
443 struct option module_init_options[] = {
444 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
448 const char *const git_submodule_helper_usage[] = {
449 N_("git submodule--helper init [<path>]"),
453 argc = parse_options(argc, argv, prefix, module_init_options,
454 git_submodule_helper_usage, 0);
456 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
460 * If there are no path args and submodule.active is set then,
461 * by default, only initialize 'active' modules.
463 if (!argc && git_config_get_value_multi("submodule.active"))
464 module_list_active(&list);
466 for (i = 0; i < list.nr; i++)
467 init_submodule(list.entries[i]->name, prefix, quiet);
472 static int module_name(int argc, const char **argv, const char *prefix)
474 const struct submodule *sub;
477 usage(_("git submodule--helper name <path>"));
480 sub = submodule_from_path(null_sha1, argv[1]);
483 die(_("no submodule mapping found in .gitmodules for path '%s'"),
486 printf("%s\n", sub->name);
491 static int clone_submodule(const char *path, const char *gitdir, const char *url,
492 const char *depth, struct string_list *reference,
493 int quiet, int progress)
495 struct child_process cp = CHILD_PROCESS_INIT;
497 argv_array_push(&cp.args, "clone");
498 argv_array_push(&cp.args, "--no-checkout");
500 argv_array_push(&cp.args, "--quiet");
502 argv_array_push(&cp.args, "--progress");
504 argv_array_pushl(&cp.args, "--depth", depth, NULL);
506 struct string_list_item *item;
507 for_each_string_list_item(item, reference)
508 argv_array_pushl(&cp.args, "--reference",
511 if (gitdir && *gitdir)
512 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
514 argv_array_push(&cp.args, "--");
515 argv_array_push(&cp.args, url);
516 argv_array_push(&cp.args, path);
519 prepare_submodule_repo_env(&cp.env_array);
522 return run_command(&cp);
525 struct submodule_alternate_setup {
526 const char *submodule_name;
527 enum SUBMODULE_ALTERNATE_ERROR_MODE {
528 SUBMODULE_ALTERNATE_ERROR_DIE,
529 SUBMODULE_ALTERNATE_ERROR_INFO,
530 SUBMODULE_ALTERNATE_ERROR_IGNORE
532 struct string_list *reference;
534 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
535 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
537 static int add_possible_reference_from_superproject(
538 struct alternate_object_database *alt, void *sas_cb)
540 struct submodule_alternate_setup *sas = sas_cb;
543 * If the alternate object store is another repository, try the
544 * standard layout with .git/(modules/<name>)+/objects
546 if (ends_with(alt->path, "/objects")) {
548 struct strbuf sb = STRBUF_INIT;
549 struct strbuf err = STRBUF_INIT;
550 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
553 * We need to end the new path with '/' to mark it as a dir,
554 * otherwise a submodule name containing '/' will be broken
555 * as the last part of a missing submodule reference would
556 * be taken as a file name.
558 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
560 sm_alternate = compute_alternate_path(sb.buf, &err);
562 string_list_append(sas->reference, xstrdup(sb.buf));
565 switch (sas->error_mode) {
566 case SUBMODULE_ALTERNATE_ERROR_DIE:
567 die(_("submodule '%s' cannot add alternate: %s"),
568 sas->submodule_name, err.buf);
569 case SUBMODULE_ALTERNATE_ERROR_INFO:
570 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
571 sas->submodule_name, err.buf);
572 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
582 static void prepare_possible_alternates(const char *sm_name,
583 struct string_list *reference)
585 char *sm_alternate = NULL, *error_strategy = NULL;
586 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
588 git_config_get_string("submodule.alternateLocation", &sm_alternate);
592 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
595 error_strategy = xstrdup("die");
597 sas.submodule_name = sm_name;
598 sas.reference = reference;
599 if (!strcmp(error_strategy, "die"))
600 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
601 else if (!strcmp(error_strategy, "info"))
602 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
603 else if (!strcmp(error_strategy, "ignore"))
604 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
606 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
608 if (!strcmp(sm_alternate, "superproject"))
609 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
610 else if (!strcmp(sm_alternate, "no"))
613 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
616 free(error_strategy);
619 static int module_clone(int argc, const char **argv, const char *prefix)
621 const char *name = NULL, *url = NULL, *depth = NULL;
624 char *p, *path = NULL, *sm_gitdir;
625 struct strbuf sb = STRBUF_INIT;
626 struct string_list reference = STRING_LIST_INIT_NODUP;
627 int require_init = 0;
628 char *sm_alternate = NULL, *error_strategy = NULL;
630 struct option module_clone_options[] = {
631 OPT_STRING(0, "prefix", &prefix,
633 N_("alternative anchor for relative paths")),
634 OPT_STRING(0, "path", &path,
636 N_("where the new submodule will be cloned to")),
637 OPT_STRING(0, "name", &name,
639 N_("name of the new submodule")),
640 OPT_STRING(0, "url", &url,
642 N_("url where to clone the submodule from")),
643 OPT_STRING_LIST(0, "reference", &reference,
645 N_("reference repository")),
646 OPT_STRING(0, "depth", &depth,
648 N_("depth for shallow clones")),
649 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
650 OPT_BOOL(0, "progress", &progress,
651 N_("force cloning progress")),
652 OPT_BOOL(0, "require-init", &require_init,
653 N_("disallow cloning into non-empty directory")),
657 const char *const git_submodule_helper_usage[] = {
658 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
659 "[--reference <repository>] [--name <name>] [--depth <depth>] "
660 "--url <url> --path <path>"),
664 argc = parse_options(argc, argv, prefix, module_clone_options,
665 git_submodule_helper_usage, 0);
667 if (argc || !url || !path || !*path)
668 usage_with_options(git_submodule_helper_usage,
669 module_clone_options);
671 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
672 sm_gitdir = absolute_pathdup(sb.buf);
675 if (!is_absolute_path(path)) {
676 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
677 path = strbuf_detach(&sb, NULL);
679 path = xstrdup(path);
681 if (validate_submodule_git_dir(sm_gitdir, name) < 0)
682 die(_("refusing to create/use '%s' in another submodule's "
683 "git dir"), sm_gitdir);
685 if (!file_exists(sm_gitdir)) {
686 if (safe_create_leading_directories_const(sm_gitdir) < 0)
687 die(_("could not create directory '%s'"), sm_gitdir);
689 prepare_possible_alternates(name, &reference);
691 if (clone_submodule(path, sm_gitdir, url, depth, &reference,
693 die(_("clone of '%s' into submodule path '%s' failed"),
696 if (require_init && !access(path, X_OK) && !is_empty_dir(path))
697 die(_("directory not empty: '%s'"), path);
698 if (safe_create_leading_directories_const(path) < 0)
699 die(_("could not create directory '%s'"), path);
700 strbuf_addf(&sb, "%s/index", sm_gitdir);
701 unlink_or_warn(sb.buf);
705 /* Connect module worktree and git dir */
706 connect_work_tree_and_git_dir(path, sm_gitdir);
708 p = git_pathdup_submodule(path, "config");
710 die(_("could not get submodule directory for '%s'"), path);
712 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
713 git_config_get_string("submodule.alternateLocation", &sm_alternate);
715 git_config_set_in_file(p, "submodule.alternateLocation",
717 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
719 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
723 free(error_strategy);
732 struct submodule_update_clone {
733 /* index into 'list', the list of submodules to look into for cloning */
735 struct module_list list;
736 unsigned warn_if_uninitialized : 1;
738 /* update parameter passed via commandline */
739 struct submodule_update_strategy update;
741 /* configuration parameters which are passed on to the children */
744 int recommend_shallow;
745 struct string_list references;
746 unsigned require_init;
748 const char *recursive_prefix;
751 /* Machine-readable status lines to be consumed by git-submodule.sh */
752 struct string_list projectlines;
754 /* If we want to stop as fast as possible and return an error */
755 unsigned quickstop : 1;
757 /* failed clones to be retried again */
758 const struct cache_entry **failed_clones;
759 int failed_clones_nr, failed_clones_alloc;
761 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
762 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
764 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
767 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
768 struct strbuf *out, const char *displaypath)
771 * Only mention uninitialized submodules when their
772 * paths have been specified.
774 if (suc->warn_if_uninitialized) {
776 _("Submodule path '%s' not initialized"),
778 strbuf_addch(out, '\n');
780 _("Maybe you want to use 'update --init'?"));
781 strbuf_addch(out, '\n');
786 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
787 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
789 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
790 struct child_process *child,
791 struct submodule_update_clone *suc,
794 const struct submodule *sub = NULL;
795 struct strbuf displaypath_sb = STRBUF_INIT;
796 struct strbuf sb = STRBUF_INIT;
797 const char *displaypath = NULL;
798 int needs_cloning = 0;
801 if (suc->recursive_prefix)
802 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
804 strbuf_addstr(&sb, ce->name);
805 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
806 strbuf_addch(out, '\n');
810 sub = submodule_from_path(null_sha1, ce->name);
812 if (suc->recursive_prefix)
813 displaypath = relative_path(suc->recursive_prefix,
814 ce->name, &displaypath_sb);
816 displaypath = ce->name;
819 next_submodule_warn_missing(suc, out, displaypath);
823 if (suc->update.type == SM_UPDATE_NONE
824 || (suc->update.type == SM_UPDATE_UNSPECIFIED
825 && sub->update_strategy.type == SM_UPDATE_NONE)) {
826 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
827 strbuf_addch(out, '\n');
831 /* Check if the submodule has been initialized. */
832 if (!is_submodule_active(the_repository, ce->name)) {
833 next_submodule_warn_missing(suc, out, displaypath);
838 strbuf_addf(&sb, "%s/.git", ce->name);
839 needs_cloning = !file_exists(sb.buf);
842 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
843 oid_to_hex(&ce->oid), ce_stage(ce),
844 needs_cloning, ce->name);
845 string_list_append(&suc->projectlines, sb.buf);
852 child->stdout_to_stderr = 1;
854 argv_array_push(&child->args, "submodule--helper");
855 argv_array_push(&child->args, "clone");
857 argv_array_push(&child->args, "--progress");
859 argv_array_push(&child->args, "--quiet");
861 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
862 if (suc->recommend_shallow && sub->recommend_shallow == 1)
863 argv_array_push(&child->args, "--depth=1");
864 if (suc->require_init)
865 argv_array_push(&child->args, "--require-init");
866 argv_array_pushl(&child->args, "--path", sub->path, NULL);
867 argv_array_pushl(&child->args, "--name", sub->name, NULL);
868 argv_array_pushl(&child->args, "--url", sub->url, NULL);
869 if (suc->references.nr) {
870 struct string_list_item *item;
871 for_each_string_list_item(item, &suc->references)
872 argv_array_pushl(&child->args, "--reference", item->string, NULL);
875 argv_array_push(&child->args, suc->depth);
878 strbuf_reset(&displaypath_sb);
881 return needs_cloning;
884 static int update_clone_get_next_task(struct child_process *child,
889 struct submodule_update_clone *suc = suc_cb;
890 const struct cache_entry *ce;
893 for (; suc->current < suc->list.nr; suc->current++) {
894 ce = suc->list.entries[suc->current];
895 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
896 int *p = xmalloc(sizeof(*p));
905 * The loop above tried cloning each submodule once, now try the
906 * stragglers again, which we can imagine as an extension of the
909 index = suc->current - suc->list.nr;
910 if (index < suc->failed_clones_nr) {
912 ce = suc->failed_clones[index];
913 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
915 strbuf_addstr(err, "BUG: submodule considered for "
916 "cloning, doesn't need cloning "
920 p = xmalloc(sizeof(*p));
930 static int update_clone_start_failure(struct strbuf *err,
934 struct submodule_update_clone *suc = suc_cb;
939 static int update_clone_task_finished(int result,
944 const struct cache_entry *ce;
945 struct submodule_update_clone *suc = suc_cb;
947 int *idxP = idx_task_cb;
954 if (idx < suc->list.nr) {
955 ce = suc->list.entries[idx];
956 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
958 strbuf_addch(err, '\n');
959 ALLOC_GROW(suc->failed_clones,
960 suc->failed_clones_nr + 1,
961 suc->failed_clones_alloc);
962 suc->failed_clones[suc->failed_clones_nr++] = ce;
966 ce = suc->failed_clones[idx];
967 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
969 strbuf_addch(err, '\n');
977 static int update_clone(int argc, const char **argv, const char *prefix)
979 const char *update = NULL;
981 struct string_list_item *item;
982 struct pathspec pathspec;
983 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
985 struct option module_update_clone_options[] = {
986 OPT_STRING(0, "prefix", &prefix,
988 N_("path into the working tree")),
989 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
991 N_("path into the working tree, across nested "
992 "submodule boundaries")),
993 OPT_STRING(0, "update", &update,
995 N_("rebase, merge, checkout or none")),
996 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
997 N_("reference repository")),
998 OPT_STRING(0, "depth", &suc.depth, "<depth>",
999 N_("Create a shallow clone truncated to the "
1000 "specified number of revisions")),
1001 OPT_INTEGER('j', "jobs", &max_jobs,
1002 N_("parallel jobs")),
1003 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1004 N_("whether the initial clone should follow the shallow recommendation")),
1005 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1006 OPT_BOOL(0, "progress", &suc.progress,
1007 N_("force cloning progress")),
1008 OPT_BOOL(0, "require-init", &suc.require_init,
1009 N_("disallow cloning into non-empty directory")),
1013 const char *const git_submodule_helper_usage[] = {
1014 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1017 suc.prefix = prefix;
1019 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1020 git_submodule_helper_usage, 0);
1023 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1024 die(_("bad value for update parameter"));
1026 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1030 suc.warn_if_uninitialized = 1;
1032 /* Overlay the parsed .gitmodules file with .git/config */
1033 gitmodules_config();
1034 git_config(submodule_config, NULL);
1037 max_jobs = parallel_submodules();
1039 run_processes_parallel(max_jobs,
1040 update_clone_get_next_task,
1041 update_clone_start_failure,
1042 update_clone_task_finished,
1046 * We saved the output and put it out all at once now.
1048 * - the listener does not have to interleave their (checkout)
1049 * work with our fetching. The writes involved in a
1050 * checkout involve more straightforward sequential I/O.
1051 * - the listener can avoid doing any work if fetching failed.
1056 for_each_string_list_item(item, &suc.projectlines)
1057 fprintf(stdout, "%s", item->string);
1062 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1064 struct strbuf sb = STRBUF_INIT;
1066 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1068 printf("%s", relative_path(argv[1], argv[2], &sb));
1069 strbuf_release(&sb);
1073 static const char *remote_submodule_branch(const char *path)
1075 const struct submodule *sub;
1076 gitmodules_config();
1077 git_config(submodule_config, NULL);
1079 sub = submodule_from_path(null_sha1, path);
1086 if (!strcmp(sub->branch, ".")) {
1087 unsigned char sha1[20];
1088 const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
1091 die(_("No such ref: %s"), "HEAD");
1094 if (!strcmp(refname, "HEAD"))
1095 die(_("Submodule (%s) branch configured to inherit "
1096 "branch from superproject, but the superproject "
1097 "is not on any branch"), sub->name);
1099 if (!skip_prefix(refname, "refs/heads/", &refname))
1100 die(_("Expecting a full ref name, got %s"), refname);
1107 static int resolve_remote_submodule_branch(int argc, const char **argv,
1111 struct strbuf sb = STRBUF_INIT;
1113 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1115 ret = remote_submodule_branch(argv[1]);
1117 die("submodule %s doesn't exist", argv[1]);
1120 strbuf_release(&sb);
1124 static int push_check(int argc, const char **argv, const char *prefix)
1126 struct remote *remote;
1127 const char *superproject_head;
1129 int detached_head = 0;
1130 struct object_id head_oid;
1133 die("submodule--helper push-check requires at least 2 arguments");
1136 * superproject's resolved head ref.
1137 * if HEAD then the superproject is in a detached head state, otherwise
1138 * it will be the resolved head ref.
1140 superproject_head = argv[1];
1143 /* Get the submodule's head ref and determine if it is detached */
1144 head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
1146 die(_("Failed to resolve HEAD as a valid ref."));
1147 if (!strcmp(head, "HEAD"))
1151 * The remote must be configured.
1152 * This is to avoid pushing to the exact same URL as the parent.
1154 remote = pushremote_get(argv[1]);
1155 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1156 die("remote '%s' not configured", argv[1]);
1158 /* Check the refspec */
1160 int i, refspec_nr = argc - 2;
1161 struct ref *local_refs = get_local_heads();
1162 struct refspec *refspec = parse_push_refspec(refspec_nr,
1165 for (i = 0; i < refspec_nr; i++) {
1166 struct refspec *rs = refspec + i;
1168 if (rs->pattern || rs->matching)
1171 /* LHS must match a single ref */
1172 switch (count_refspec_match(rs->src, local_refs, NULL)) {
1177 * If LHS matches 'HEAD' then we need to ensure
1178 * that it matches the same named branch
1179 * checked out in the superproject.
1181 if (!strcmp(rs->src, "HEAD")) {
1182 if (!detached_head &&
1183 !strcmp(head, superproject_head))
1185 die("HEAD does not match the named branch in the superproject");
1188 die("src refspec '%s' must name a ref",
1192 free_refspec(refspec_nr, refspec);
1199 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1202 struct pathspec pathspec;
1203 struct module_list list = MODULE_LIST_INIT;
1204 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1206 struct option embed_gitdir_options[] = {
1207 OPT_STRING(0, "prefix", &prefix,
1209 N_("path into the working tree")),
1210 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1211 ABSORB_GITDIR_RECURSE_SUBMODULES),
1215 const char *const git_submodule_helper_usage[] = {
1216 N_("git submodule--helper embed-git-dir [<path>...]"),
1220 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1221 git_submodule_helper_usage, 0);
1223 gitmodules_config();
1224 git_config(submodule_config, NULL);
1226 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1229 for (i = 0; i < list.nr; i++)
1230 absorb_git_dir_into_superproject(prefix,
1231 list.entries[i]->name, flags);
1236 static int is_active(int argc, const char **argv, const char *prefix)
1239 die("submodule--helper is-active takes exactly 1 argument");
1241 gitmodules_config();
1243 return !is_submodule_active(the_repository, argv[1]);
1247 * Exit non-zero if any of the submodule names given on the command line is
1248 * invalid. If no names are given, filter stdin to print only valid names
1249 * (which is primarily intended for testing).
1251 static int check_name(int argc, const char **argv, const char *prefix)
1255 if (check_submodule_name(*argv) < 0)
1259 struct strbuf buf = STRBUF_INIT;
1260 while (strbuf_getline(&buf, stdin) != EOF) {
1261 if (!check_submodule_name(buf.buf))
1262 printf("%s\n", buf.buf);
1264 strbuf_release(&buf);
1269 #define SUPPORT_SUPER_PREFIX (1<<0)
1273 int (*fn)(int, const char **, const char *);
1277 static struct cmd_struct commands[] = {
1278 {"list", module_list, 0},
1279 {"name", module_name, 0},
1280 {"clone", module_clone, 0},
1281 {"update-clone", update_clone, 0},
1282 {"relative-path", resolve_relative_path, 0},
1283 {"resolve-relative-url", resolve_relative_url, 0},
1284 {"resolve-relative-url-test", resolve_relative_url_test, 0},
1285 {"init", module_init, SUPPORT_SUPER_PREFIX},
1286 {"remote-branch", resolve_remote_submodule_branch, 0},
1287 {"push-check", push_check, 0},
1288 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1289 {"is-active", is_active, 0},
1290 {"check-name", check_name, 0},
1293 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
1296 if (argc < 2 || !strcmp(argv[1], "-h"))
1297 usage("git submodule--helper <command>");
1299 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1300 if (!strcmp(argv[1], commands[i].cmd)) {
1301 if (get_super_prefix() &&
1302 !(commands[i].option & SUPPORT_SUPER_PREFIX))
1303 die(_("%s doesn't support --super-prefix"),
1305 return commands[i].fn(argc - 1, argv + 1, prefix);
1309 die(_("'%s' is not a valid submodule--helper "
1310 "subcommand"), argv[1]);