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 struct strbuf sb = STRBUF_INIT;
22 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
25 die(_("No such ref: %s"), "HEAD");
28 if (!strcmp(refname, "HEAD"))
29 return xstrdup("origin");
31 if (!skip_prefix(refname, "refs/heads/", &refname))
32 die(_("Expecting a full ref name, got %s"), refname);
34 strbuf_addf(&sb, "branch.%s.remote", refname);
35 if (git_config_get_string(sb.buf, &dest))
36 ret = xstrdup("origin");
44 static int starts_with_dot_slash(const char *str)
46 return str[0] == '.' && is_dir_sep(str[1]);
49 static int starts_with_dot_dot_slash(const char *str)
51 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
55 * Returns 1 if it was the last chop before ':'.
57 static int chop_last_dir(char **remoteurl, int is_relative)
59 char *rfind = find_last_dir_sep(*remoteurl);
65 rfind = strrchr(*remoteurl, ':');
71 if (is_relative || !strcmp(".", *remoteurl))
72 die(_("cannot strip one component off url '%s'"),
76 *remoteurl = xstrdup(".");
81 * The `url` argument is the URL that navigates to the submodule origin
82 * repo. When relative, this URL is relative to the superproject origin
83 * URL repo. The `up_path` argument, if specified, is the relative
84 * path that navigates from the submodule working tree to the superproject
85 * working tree. Returns the origin URL of the submodule.
87 * Return either an absolute URL or filesystem path (if the superproject
88 * origin URL is an absolute URL or filesystem path, respectively) or a
89 * relative file system path (if the superproject origin URL is a relative
92 * When the output is a relative file system path, the path is either
93 * relative to the submodule working tree, if up_path is specified, or to
94 * the superproject working tree otherwise.
96 * NEEDSWORK: This works incorrectly on the domain and protocol part.
97 * remote_url url outcome expectation
98 * http://a.com/b ../c http://a.com/c as is
99 * http://a.com/b/ ../c http://a.com/c same as previous line, but
100 * ignore trailing slash in url
101 * http://a.com/b ../../c http://c error out
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 .:c error out
105 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
106 * when a local part has a colon in its path component, too.
108 static char *relative_url(const char *remote_url,
115 char *remoteurl = xstrdup(remote_url);
116 struct strbuf sb = STRBUF_INIT;
117 size_t len = strlen(remoteurl);
119 if (is_dir_sep(remoteurl[len-1]))
120 remoteurl[len-1] = '\0';
122 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
127 * Prepend a './' to ensure all relative
128 * remoteurls start with './' or '../'
130 if (!starts_with_dot_slash(remoteurl) &&
131 !starts_with_dot_dot_slash(remoteurl)) {
133 strbuf_addf(&sb, "./%s", remoteurl);
135 remoteurl = strbuf_detach(&sb, NULL);
139 * When the url starts with '../', remove that and the
140 * last directory in remoteurl.
143 if (starts_with_dot_dot_slash(url)) {
145 colonsep |= chop_last_dir(&remoteurl, is_relative);
146 } else if (starts_with_dot_slash(url))
152 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
153 if (ends_with(url, "/"))
154 strbuf_setlen(&sb, sb.len - 1);
157 if (starts_with_dot_slash(sb.buf))
158 out = xstrdup(sb.buf + 2);
160 out = xstrdup(sb.buf);
163 if (!up_path || !is_relative)
166 strbuf_addf(&sb, "%s%s", up_path, out);
168 return strbuf_detach(&sb, NULL);
171 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
173 char *remoteurl = NULL;
174 char *remote = get_default_remote();
175 const char *up_path = NULL;
178 struct strbuf sb = STRBUF_INIT;
180 if (argc != 2 && argc != 3)
181 die("resolve-relative-url only accepts one or two arguments");
184 strbuf_addf(&sb, "remote.%s.url", remote);
187 if (git_config_get_string(sb.buf, &remoteurl))
188 /* the repository is its own authoritative upstream */
189 remoteurl = xgetcwd();
194 res = relative_url(remoteurl, url, up_path);
201 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
203 char *remoteurl, *res;
204 const char *up_path, *url;
207 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
210 remoteurl = xstrdup(argv[2]);
213 if (!strcmp(up_path, "(null)"))
216 res = relative_url(remoteurl, url, up_path);
224 const struct cache_entry **entries;
227 #define MODULE_LIST_INIT { NULL, 0, 0 }
229 static int module_list_compute(int argc, const char **argv,
231 struct pathspec *pathspec,
232 struct module_list *list)
235 char *ps_matched = NULL;
236 parse_pathspec(pathspec, 0,
237 PATHSPEC_PREFER_FULL,
241 ps_matched = xcalloc(pathspec->nr, 1);
243 if (read_cache() < 0)
244 die(_("index file corrupt"));
246 for (i = 0; i < active_nr; i++) {
247 const struct cache_entry *ce = active_cache[i];
249 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
251 !S_ISGITLINK(ce->ce_mode))
254 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
255 list->entries[list->nr++] = ce;
256 while (i + 1 < active_nr &&
257 !strcmp(ce->name, active_cache[i + 1]->name))
259 * Skip entries with the same name in different stages
260 * to make sure an entry is returned only once.
265 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
273 static void module_list_active(struct module_list *list)
276 struct module_list active_modules = MODULE_LIST_INIT;
278 for (i = 0; i < list->nr; i++) {
279 const struct cache_entry *ce = list->entries[i];
281 if (!is_submodule_active(the_repository, ce->name))
284 ALLOC_GROW(active_modules.entries,
285 active_modules.nr + 1,
286 active_modules.alloc);
287 active_modules.entries[active_modules.nr++] = ce;
291 *list = active_modules;
294 static int module_list(int argc, const char **argv, const char *prefix)
297 struct pathspec pathspec;
298 struct module_list list = MODULE_LIST_INIT;
300 struct option module_list_options[] = {
301 OPT_STRING(0, "prefix", &prefix,
303 N_("alternative anchor for relative paths")),
307 const char *const git_submodule_helper_usage[] = {
308 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
312 argc = parse_options(argc, argv, prefix, module_list_options,
313 git_submodule_helper_usage, 0);
315 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
318 for (i = 0; i < list.nr; i++) {
319 const struct cache_entry *ce = list.entries[i];
322 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
324 printf("%06o %s %d\t", ce->ce_mode,
325 oid_to_hex(&ce->oid), ce_stage(ce));
327 fprintf(stdout, "%s\n", ce->name);
332 static void init_submodule(const char *path, const char *prefix, int quiet)
334 const struct submodule *sub;
335 struct strbuf sb = STRBUF_INIT;
336 char *upd = NULL, *url = NULL, *displaypath;
338 if (prefix && get_super_prefix())
339 die("BUG: cannot have prefix and superprefix");
341 displaypath = xstrdup(relative_path(path, prefix, &sb));
342 else if (get_super_prefix()) {
343 strbuf_addf(&sb, "%s%s", get_super_prefix(), path);
344 displaypath = strbuf_detach(&sb, NULL);
346 displaypath = xstrdup(path);
348 sub = submodule_from_path(&null_oid, path);
351 die(_("No url found for submodule path '%s' in .gitmodules"),
355 * NEEDSWORK: In a multi-working-tree world, this needs to be
356 * set in the per-worktree config.
358 * Set active flag for the submodule being initialized
360 if (!is_submodule_active(the_repository, path)) {
362 strbuf_addf(&sb, "submodule.%s.active", sub->name);
363 git_config_set_gently(sb.buf, "true");
367 * Copy url setting when it is not set yet.
368 * To look up the url in .git/config, we must not fall back to
369 * .gitmodules, so look it up directly.
372 strbuf_addf(&sb, "submodule.%s.url", sub->name);
373 if (git_config_get_string(sb.buf, &url)) {
375 die(_("No url found for submodule path '%s' in .gitmodules"),
378 url = xstrdup(sub->url);
380 /* Possibly a url relative to parent */
381 if (starts_with_dot_dot_slash(url) ||
382 starts_with_dot_slash(url)) {
383 char *remoteurl, *relurl;
384 char *remote = get_default_remote();
385 struct strbuf remotesb = STRBUF_INIT;
386 strbuf_addf(&remotesb, "remote.%s.url", remote);
389 if (git_config_get_string(remotesb.buf, &remoteurl)) {
390 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
391 remoteurl = xgetcwd();
393 relurl = relative_url(remoteurl, url, NULL);
394 strbuf_release(&remotesb);
400 if (git_config_set_gently(sb.buf, url))
401 die(_("Failed to register url for submodule path '%s'"),
405 _("Submodule '%s' (%s) registered for path '%s'\n"),
406 sub->name, url, displaypath);
409 /* Copy "update" setting when it is not set yet */
411 strbuf_addf(&sb, "submodule.%s.update", sub->name);
412 if (git_config_get_string(sb.buf, &upd) &&
413 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
414 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
415 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
417 upd = xstrdup("none");
419 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
421 if (git_config_set_gently(sb.buf, upd))
422 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
430 static int module_init(int argc, const char **argv, const char *prefix)
432 struct pathspec pathspec;
433 struct module_list list = MODULE_LIST_INIT;
437 struct option module_init_options[] = {
438 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
442 const char *const git_submodule_helper_usage[] = {
443 N_("git submodule--helper init [<path>]"),
447 argc = parse_options(argc, argv, prefix, module_init_options,
448 git_submodule_helper_usage, 0);
450 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
454 * If there are no path args and submodule.active is set then,
455 * by default, only initialize 'active' modules.
457 if (!argc && git_config_get_value_multi("submodule.active"))
458 module_list_active(&list);
460 for (i = 0; i < list.nr; i++)
461 init_submodule(list.entries[i]->name, prefix, quiet);
466 static int module_name(int argc, const char **argv, const char *prefix)
468 const struct submodule *sub;
471 usage(_("git submodule--helper name <path>"));
473 sub = submodule_from_path(&null_oid, argv[1]);
476 die(_("no submodule mapping found in .gitmodules for path '%s'"),
479 printf("%s\n", sub->name);
484 static int clone_submodule(const char *path, const char *gitdir, const char *url,
485 const char *depth, struct string_list *reference,
486 int quiet, int progress)
488 struct child_process cp = CHILD_PROCESS_INIT;
490 argv_array_push(&cp.args, "clone");
491 argv_array_push(&cp.args, "--no-checkout");
493 argv_array_push(&cp.args, "--quiet");
495 argv_array_push(&cp.args, "--progress");
497 argv_array_pushl(&cp.args, "--depth", depth, NULL);
499 struct string_list_item *item;
500 for_each_string_list_item(item, reference)
501 argv_array_pushl(&cp.args, "--reference",
504 if (gitdir && *gitdir)
505 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
507 argv_array_push(&cp.args, "--");
508 argv_array_push(&cp.args, url);
509 argv_array_push(&cp.args, path);
512 prepare_submodule_repo_env(&cp.env_array);
515 return run_command(&cp);
518 struct submodule_alternate_setup {
519 const char *submodule_name;
520 enum SUBMODULE_ALTERNATE_ERROR_MODE {
521 SUBMODULE_ALTERNATE_ERROR_DIE,
522 SUBMODULE_ALTERNATE_ERROR_INFO,
523 SUBMODULE_ALTERNATE_ERROR_IGNORE
525 struct string_list *reference;
527 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
528 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
530 static int add_possible_reference_from_superproject(
531 struct alternate_object_database *alt, void *sas_cb)
533 struct submodule_alternate_setup *sas = sas_cb;
536 * If the alternate object store is another repository, try the
537 * standard layout with .git/(modules/<name>)+/objects
539 if (ends_with(alt->path, "/objects")) {
541 struct strbuf sb = STRBUF_INIT;
542 struct strbuf err = STRBUF_INIT;
543 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
546 * We need to end the new path with '/' to mark it as a dir,
547 * otherwise a submodule name containing '/' will be broken
548 * as the last part of a missing submodule reference would
549 * be taken as a file name.
551 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
553 sm_alternate = compute_alternate_path(sb.buf, &err);
555 string_list_append(sas->reference, xstrdup(sb.buf));
558 switch (sas->error_mode) {
559 case SUBMODULE_ALTERNATE_ERROR_DIE:
560 die(_("submodule '%s' cannot add alternate: %s"),
561 sas->submodule_name, err.buf);
562 case SUBMODULE_ALTERNATE_ERROR_INFO:
563 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
564 sas->submodule_name, err.buf);
565 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
575 static void prepare_possible_alternates(const char *sm_name,
576 struct string_list *reference)
578 char *sm_alternate = NULL, *error_strategy = NULL;
579 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
581 git_config_get_string("submodule.alternateLocation", &sm_alternate);
585 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
588 error_strategy = xstrdup("die");
590 sas.submodule_name = sm_name;
591 sas.reference = reference;
592 if (!strcmp(error_strategy, "die"))
593 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
594 else if (!strcmp(error_strategy, "info"))
595 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
596 else if (!strcmp(error_strategy, "ignore"))
597 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
599 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
601 if (!strcmp(sm_alternate, "superproject"))
602 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
603 else if (!strcmp(sm_alternate, "no"))
606 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
609 free(error_strategy);
612 static int module_clone(int argc, const char **argv, const char *prefix)
614 const char *name = NULL, *url = NULL, *depth = NULL;
617 char *p, *path = NULL, *sm_gitdir;
618 struct strbuf sb = STRBUF_INIT;
619 struct string_list reference = STRING_LIST_INIT_NODUP;
620 int require_init = 0;
621 char *sm_alternate = NULL, *error_strategy = NULL;
623 struct option module_clone_options[] = {
624 OPT_STRING(0, "prefix", &prefix,
626 N_("alternative anchor for relative paths")),
627 OPT_STRING(0, "path", &path,
629 N_("where the new submodule will be cloned to")),
630 OPT_STRING(0, "name", &name,
632 N_("name of the new submodule")),
633 OPT_STRING(0, "url", &url,
635 N_("url where to clone the submodule from")),
636 OPT_STRING_LIST(0, "reference", &reference,
638 N_("reference repository")),
639 OPT_STRING(0, "depth", &depth,
641 N_("depth for shallow clones")),
642 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
643 OPT_BOOL(0, "progress", &progress,
644 N_("force cloning progress")),
645 OPT_BOOL(0, "require-init", &require_init,
646 N_("disallow cloning into non-empty directory")),
650 const char *const git_submodule_helper_usage[] = {
651 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
652 "[--reference <repository>] [--name <name>] [--depth <depth>] "
653 "--url <url> --path <path>"),
657 argc = parse_options(argc, argv, prefix, module_clone_options,
658 git_submodule_helper_usage, 0);
660 if (argc || !url || !path || !*path)
661 usage_with_options(git_submodule_helper_usage,
662 module_clone_options);
664 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
665 sm_gitdir = absolute_pathdup(sb.buf);
668 if (!is_absolute_path(path)) {
669 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
670 path = strbuf_detach(&sb, NULL);
672 path = xstrdup(path);
674 if (validate_submodule_git_dir(sm_gitdir, name) < 0)
675 die(_("refusing to create/use '%s' in another submodule's "
676 "git dir"), sm_gitdir);
678 if (!file_exists(sm_gitdir)) {
679 if (safe_create_leading_directories_const(sm_gitdir) < 0)
680 die(_("could not create directory '%s'"), sm_gitdir);
682 prepare_possible_alternates(name, &reference);
684 if (clone_submodule(path, sm_gitdir, url, depth, &reference,
686 die(_("clone of '%s' into submodule path '%s' failed"),
689 if (require_init && !access(path, X_OK) && !is_empty_dir(path))
690 die(_("directory not empty: '%s'"), path);
691 if (safe_create_leading_directories_const(path) < 0)
692 die(_("could not create directory '%s'"), path);
693 strbuf_addf(&sb, "%s/index", sm_gitdir);
694 unlink_or_warn(sb.buf);
698 /* Connect module worktree and git dir */
699 connect_work_tree_and_git_dir(path, sm_gitdir);
701 p = git_pathdup_submodule(path, "config");
703 die(_("could not get submodule directory for '%s'"), path);
705 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
706 git_config_get_string("submodule.alternateLocation", &sm_alternate);
708 git_config_set_in_file(p, "submodule.alternateLocation",
710 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
712 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
716 free(error_strategy);
725 struct submodule_update_clone {
726 /* index into 'list', the list of submodules to look into for cloning */
728 struct module_list list;
729 unsigned warn_if_uninitialized : 1;
731 /* update parameter passed via commandline */
732 struct submodule_update_strategy update;
734 /* configuration parameters which are passed on to the children */
737 int recommend_shallow;
738 struct string_list references;
739 unsigned require_init;
741 const char *recursive_prefix;
744 /* Machine-readable status lines to be consumed by git-submodule.sh */
745 struct string_list projectlines;
747 /* If we want to stop as fast as possible and return an error */
748 unsigned quickstop : 1;
750 /* failed clones to be retried again */
751 const struct cache_entry **failed_clones;
752 int failed_clones_nr, failed_clones_alloc;
754 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
755 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
757 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
760 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
761 struct strbuf *out, const char *displaypath)
764 * Only mention uninitialized submodules when their
765 * paths have been specified.
767 if (suc->warn_if_uninitialized) {
769 _("Submodule path '%s' not initialized"),
771 strbuf_addch(out, '\n');
773 _("Maybe you want to use 'update --init'?"));
774 strbuf_addch(out, '\n');
779 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
780 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
782 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
783 struct child_process *child,
784 struct submodule_update_clone *suc,
787 const struct submodule *sub = NULL;
788 const char *url = NULL;
789 const char *update_string;
790 enum submodule_update_type update_type;
792 struct strbuf displaypath_sb = STRBUF_INIT;
793 struct strbuf sb = STRBUF_INIT;
794 const char *displaypath = NULL;
795 int needs_cloning = 0;
798 if (suc->recursive_prefix)
799 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
801 strbuf_addstr(&sb, ce->name);
802 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
803 strbuf_addch(out, '\n');
807 sub = submodule_from_path(&null_oid, ce->name);
809 if (suc->recursive_prefix)
810 displaypath = relative_path(suc->recursive_prefix,
811 ce->name, &displaypath_sb);
813 displaypath = ce->name;
816 next_submodule_warn_missing(suc, out, displaypath);
820 key = xstrfmt("submodule.%s.update", sub->name);
821 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
822 update_type = parse_submodule_update_type(update_string);
824 update_type = sub->update_strategy.type;
828 if (suc->update.type == SM_UPDATE_NONE
829 || (suc->update.type == SM_UPDATE_UNSPECIFIED
830 && update_type == SM_UPDATE_NONE)) {
831 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
832 strbuf_addch(out, '\n');
836 /* Check if the submodule has been initialized. */
837 if (!is_submodule_active(the_repository, ce->name)) {
838 next_submodule_warn_missing(suc, out, displaypath);
843 strbuf_addf(&sb, "submodule.%s.url", sub->name);
844 if (repo_config_get_string_const(the_repository, sb.buf, &url))
848 strbuf_addf(&sb, "%s/.git", ce->name);
849 needs_cloning = !file_exists(sb.buf);
852 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
853 oid_to_hex(&ce->oid), ce_stage(ce),
854 needs_cloning, ce->name);
855 string_list_append(&suc->projectlines, sb.buf);
862 child->stdout_to_stderr = 1;
864 argv_array_push(&child->args, "submodule--helper");
865 argv_array_push(&child->args, "clone");
867 argv_array_push(&child->args, "--progress");
869 argv_array_push(&child->args, "--quiet");
871 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
872 if (suc->recommend_shallow && sub->recommend_shallow == 1)
873 argv_array_push(&child->args, "--depth=1");
874 if (suc->require_init)
875 argv_array_push(&child->args, "--require-init");
876 argv_array_pushl(&child->args, "--path", sub->path, NULL);
877 argv_array_pushl(&child->args, "--name", sub->name, NULL);
878 argv_array_pushl(&child->args, "--url", url, NULL);
879 if (suc->references.nr) {
880 struct string_list_item *item;
881 for_each_string_list_item(item, &suc->references)
882 argv_array_pushl(&child->args, "--reference", item->string, NULL);
885 argv_array_push(&child->args, suc->depth);
888 strbuf_reset(&displaypath_sb);
891 return needs_cloning;
894 static int update_clone_get_next_task(struct child_process *child,
899 struct submodule_update_clone *suc = suc_cb;
900 const struct cache_entry *ce;
903 for (; suc->current < suc->list.nr; suc->current++) {
904 ce = suc->list.entries[suc->current];
905 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
906 int *p = xmalloc(sizeof(*p));
915 * The loop above tried cloning each submodule once, now try the
916 * stragglers again, which we can imagine as an extension of the
919 index = suc->current - suc->list.nr;
920 if (index < suc->failed_clones_nr) {
922 ce = suc->failed_clones[index];
923 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
925 strbuf_addstr(err, "BUG: submodule considered for "
926 "cloning, doesn't need cloning "
930 p = xmalloc(sizeof(*p));
940 static int update_clone_start_failure(struct strbuf *err,
944 struct submodule_update_clone *suc = suc_cb;
949 static int update_clone_task_finished(int result,
954 const struct cache_entry *ce;
955 struct submodule_update_clone *suc = suc_cb;
957 int *idxP = idx_task_cb;
964 if (idx < suc->list.nr) {
965 ce = suc->list.entries[idx];
966 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
968 strbuf_addch(err, '\n');
969 ALLOC_GROW(suc->failed_clones,
970 suc->failed_clones_nr + 1,
971 suc->failed_clones_alloc);
972 suc->failed_clones[suc->failed_clones_nr++] = ce;
976 ce = suc->failed_clones[idx];
977 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
979 strbuf_addch(err, '\n');
987 static int gitmodules_update_clone_config(const char *var, const char *value,
991 if (!strcmp(var, "submodule.fetchjobs"))
992 *max_jobs = parse_submodule_fetchjobs(var, value);
996 static int update_clone(int argc, const char **argv, const char *prefix)
998 const char *update = NULL;
1000 struct string_list_item *item;
1001 struct pathspec pathspec;
1002 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1004 struct option module_update_clone_options[] = {
1005 OPT_STRING(0, "prefix", &prefix,
1007 N_("path into the working tree")),
1008 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1010 N_("path into the working tree, across nested "
1011 "submodule boundaries")),
1012 OPT_STRING(0, "update", &update,
1014 N_("rebase, merge, checkout or none")),
1015 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1016 N_("reference repository")),
1017 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1018 N_("Create a shallow clone truncated to the "
1019 "specified number of revisions")),
1020 OPT_INTEGER('j', "jobs", &max_jobs,
1021 N_("parallel jobs")),
1022 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1023 N_("whether the initial clone should follow the shallow recommendation")),
1024 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1025 OPT_BOOL(0, "progress", &suc.progress,
1026 N_("force cloning progress")),
1027 OPT_BOOL(0, "require-init", &suc.require_init,
1028 N_("disallow cloning into non-empty directory")),
1032 const char *const git_submodule_helper_usage[] = {
1033 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1036 suc.prefix = prefix;
1038 config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
1039 git_config(gitmodules_update_clone_config, &max_jobs);
1041 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1042 git_submodule_helper_usage, 0);
1045 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1046 die(_("bad value for update parameter"));
1048 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1052 suc.warn_if_uninitialized = 1;
1054 run_processes_parallel(max_jobs,
1055 update_clone_get_next_task,
1056 update_clone_start_failure,
1057 update_clone_task_finished,
1061 * We saved the output and put it out all at once now.
1063 * - the listener does not have to interleave their (checkout)
1064 * work with our fetching. The writes involved in a
1065 * checkout involve more straightforward sequential I/O.
1066 * - the listener can avoid doing any work if fetching failed.
1071 for_each_string_list_item(item, &suc.projectlines)
1072 fprintf(stdout, "%s", item->string);
1077 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1079 struct strbuf sb = STRBUF_INIT;
1081 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1083 printf("%s", relative_path(argv[1], argv[2], &sb));
1084 strbuf_release(&sb);
1088 static const char *remote_submodule_branch(const char *path)
1090 const struct submodule *sub;
1091 const char *branch = NULL;
1094 sub = submodule_from_path(&null_oid, path);
1098 key = xstrfmt("submodule.%s.branch", sub->name);
1099 if (repo_config_get_string_const(the_repository, key, &branch))
1100 branch = sub->branch;
1106 if (!strcmp(branch, ".")) {
1107 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1110 die(_("No such ref: %s"), "HEAD");
1113 if (!strcmp(refname, "HEAD"))
1114 die(_("Submodule (%s) branch configured to inherit "
1115 "branch from superproject, but the superproject "
1116 "is not on any branch"), sub->name);
1118 if (!skip_prefix(refname, "refs/heads/", &refname))
1119 die(_("Expecting a full ref name, got %s"), refname);
1126 static int resolve_remote_submodule_branch(int argc, const char **argv,
1130 struct strbuf sb = STRBUF_INIT;
1132 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1134 ret = remote_submodule_branch(argv[1]);
1136 die("submodule %s doesn't exist", argv[1]);
1139 strbuf_release(&sb);
1143 static int push_check(int argc, const char **argv, const char *prefix)
1145 struct remote *remote;
1146 const char *superproject_head;
1148 int detached_head = 0;
1149 struct object_id head_oid;
1152 die("submodule--helper push-check requires at least 2 arguments");
1155 * superproject's resolved head ref.
1156 * if HEAD then the superproject is in a detached head state, otherwise
1157 * it will be the resolved head ref.
1159 superproject_head = argv[1];
1162 /* Get the submodule's head ref and determine if it is detached */
1163 head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
1165 die(_("Failed to resolve HEAD as a valid ref."));
1166 if (!strcmp(head, "HEAD"))
1170 * The remote must be configured.
1171 * This is to avoid pushing to the exact same URL as the parent.
1173 remote = pushremote_get(argv[1]);
1174 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1175 die("remote '%s' not configured", argv[1]);
1177 /* Check the refspec */
1179 int i, refspec_nr = argc - 2;
1180 struct ref *local_refs = get_local_heads();
1181 struct refspec *refspec = parse_push_refspec(refspec_nr,
1184 for (i = 0; i < refspec_nr; i++) {
1185 struct refspec *rs = refspec + i;
1187 if (rs->pattern || rs->matching)
1190 /* LHS must match a single ref */
1191 switch (count_refspec_match(rs->src, local_refs, NULL)) {
1196 * If LHS matches 'HEAD' then we need to ensure
1197 * that it matches the same named branch
1198 * checked out in the superproject.
1200 if (!strcmp(rs->src, "HEAD")) {
1201 if (!detached_head &&
1202 !strcmp(head, superproject_head))
1204 die("HEAD does not match the named branch in the superproject");
1208 die("src refspec '%s' must name a ref",
1212 free_refspec(refspec_nr, refspec);
1219 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1222 struct pathspec pathspec;
1223 struct module_list list = MODULE_LIST_INIT;
1224 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1226 struct option embed_gitdir_options[] = {
1227 OPT_STRING(0, "prefix", &prefix,
1229 N_("path into the working tree")),
1230 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1231 ABSORB_GITDIR_RECURSE_SUBMODULES),
1235 const char *const git_submodule_helper_usage[] = {
1236 N_("git submodule--helper embed-git-dir [<path>...]"),
1240 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1241 git_submodule_helper_usage, 0);
1243 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1246 for (i = 0; i < list.nr; i++)
1247 absorb_git_dir_into_superproject(prefix,
1248 list.entries[i]->name, flags);
1253 static int is_active(int argc, const char **argv, const char *prefix)
1256 die("submodule--helper is-active takes exactly 1 argument");
1258 return !is_submodule_active(the_repository, argv[1]);
1262 * Exit non-zero if any of the submodule names given on the command line is
1263 * invalid. If no names are given, filter stdin to print only valid names
1264 * (which is primarily intended for testing).
1266 static int check_name(int argc, const char **argv, const char *prefix)
1270 if (check_submodule_name(*argv) < 0)
1274 struct strbuf buf = STRBUF_INIT;
1275 while (strbuf_getline(&buf, stdin) != EOF) {
1276 if (!check_submodule_name(buf.buf))
1277 printf("%s\n", buf.buf);
1279 strbuf_release(&buf);
1284 #define SUPPORT_SUPER_PREFIX (1<<0)
1288 int (*fn)(int, const char **, const char *);
1292 static struct cmd_struct commands[] = {
1293 {"list", module_list, 0},
1294 {"name", module_name, 0},
1295 {"clone", module_clone, 0},
1296 {"update-clone", update_clone, 0},
1297 {"relative-path", resolve_relative_path, 0},
1298 {"resolve-relative-url", resolve_relative_url, 0},
1299 {"resolve-relative-url-test", resolve_relative_url_test, 0},
1300 {"init", module_init, SUPPORT_SUPER_PREFIX},
1301 {"remote-branch", resolve_remote_submodule_branch, 0},
1302 {"push-check", push_check, 0},
1303 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1304 {"is-active", is_active, 0},
1305 {"check-name", check_name, 0},
1308 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
1311 if (argc < 2 || !strcmp(argv[1], "-h"))
1312 usage("git submodule--helper <command>");
1314 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1315 if (!strcmp(argv[1], commands[i].cmd)) {
1316 if (get_super_prefix() &&
1317 !(commands[i].option & SUPPORT_SUPER_PREFIX))
1318 die(_("%s doesn't support --super-prefix"),
1320 return commands[i].fn(argc - 1, argv + 1, prefix);
1324 die(_("'%s' is not a valid submodule--helper "
1325 "subcommand"), argv[1]);