4 #include "parse-options.h"
10 #include "submodule-config.h"
11 #include "string-list.h"
12 #include "run-command.h"
17 static char *get_default_remote(void)
19 char *dest = NULL, *ret;
20 unsigned char sha1[20];
21 struct strbuf sb = STRBUF_INIT;
22 const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, 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 |
238 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
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_initialized(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 utf8_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_initialized(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, url);
515 argv_array_push(&cp.args, path);
518 prepare_submodule_repo_env(&cp.env_array);
521 return run_command(&cp);
524 struct submodule_alternate_setup {
525 const char *submodule_name;
526 enum SUBMODULE_ALTERNATE_ERROR_MODE {
527 SUBMODULE_ALTERNATE_ERROR_DIE,
528 SUBMODULE_ALTERNATE_ERROR_INFO,
529 SUBMODULE_ALTERNATE_ERROR_IGNORE
531 struct string_list *reference;
533 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
534 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
536 static int add_possible_reference_from_superproject(
537 struct alternate_object_database *alt, void *sas_cb)
539 struct submodule_alternate_setup *sas = sas_cb;
542 * If the alternate object store is another repository, try the
543 * standard layout with .git/(modules/<name>)+/objects
545 if (ends_with(alt->path, "/objects")) {
547 struct strbuf sb = STRBUF_INIT;
548 struct strbuf err = STRBUF_INIT;
549 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
552 * We need to end the new path with '/' to mark it as a dir,
553 * otherwise a submodule name containing '/' will be broken
554 * as the last part of a missing submodule reference would
555 * be taken as a file name.
557 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
559 sm_alternate = compute_alternate_path(sb.buf, &err);
561 string_list_append(sas->reference, xstrdup(sb.buf));
564 switch (sas->error_mode) {
565 case SUBMODULE_ALTERNATE_ERROR_DIE:
566 die(_("submodule '%s' cannot add alternate: %s"),
567 sas->submodule_name, err.buf);
568 case SUBMODULE_ALTERNATE_ERROR_INFO:
569 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
570 sas->submodule_name, err.buf);
571 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
581 static void prepare_possible_alternates(const char *sm_name,
582 struct string_list *reference)
584 char *sm_alternate = NULL, *error_strategy = NULL;
585 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
587 git_config_get_string("submodule.alternateLocation", &sm_alternate);
591 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
594 error_strategy = xstrdup("die");
596 sas.submodule_name = sm_name;
597 sas.reference = reference;
598 if (!strcmp(error_strategy, "die"))
599 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
600 else if (!strcmp(error_strategy, "info"))
601 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
602 else if (!strcmp(error_strategy, "ignore"))
603 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
605 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
607 if (!strcmp(sm_alternate, "superproject"))
608 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
609 else if (!strcmp(sm_alternate, "no"))
612 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
615 free(error_strategy);
618 static int module_clone(int argc, const char **argv, const char *prefix)
620 const char *name = NULL, *url = NULL, *depth = NULL;
623 char *p, *path = NULL, *sm_gitdir;
624 struct strbuf sb = STRBUF_INIT;
625 struct string_list reference = STRING_LIST_INIT_NODUP;
626 char *sm_alternate = NULL, *error_strategy = NULL;
628 struct option module_clone_options[] = {
629 OPT_STRING(0, "prefix", &prefix,
631 N_("alternative anchor for relative paths")),
632 OPT_STRING(0, "path", &path,
634 N_("where the new submodule will be cloned to")),
635 OPT_STRING(0, "name", &name,
637 N_("name of the new submodule")),
638 OPT_STRING(0, "url", &url,
640 N_("url where to clone the submodule from")),
641 OPT_STRING_LIST(0, "reference", &reference,
643 N_("reference repository")),
644 OPT_STRING(0, "depth", &depth,
646 N_("depth for shallow clones")),
647 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
648 OPT_BOOL(0, "progress", &progress,
649 N_("force cloning progress")),
653 const char *const git_submodule_helper_usage[] = {
654 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
655 "[--reference <repository>] [--name <name>] [--depth <depth>] "
656 "--url <url> --path <path>"),
660 argc = parse_options(argc, argv, prefix, module_clone_options,
661 git_submodule_helper_usage, 0);
663 if (argc || !url || !path || !*path)
664 usage_with_options(git_submodule_helper_usage,
665 module_clone_options);
667 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
668 sm_gitdir = absolute_pathdup(sb.buf);
671 if (!is_absolute_path(path)) {
672 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
673 path = strbuf_detach(&sb, NULL);
675 path = xstrdup(path);
677 if (!file_exists(sm_gitdir)) {
678 if (safe_create_leading_directories_const(sm_gitdir) < 0)
679 die(_("could not create directory '%s'"), sm_gitdir);
681 prepare_possible_alternates(name, &reference);
683 if (clone_submodule(path, sm_gitdir, url, depth, &reference,
685 die(_("clone of '%s' into submodule path '%s' failed"),
688 if (safe_create_leading_directories_const(path) < 0)
689 die(_("could not create directory '%s'"), path);
690 strbuf_addf(&sb, "%s/index", sm_gitdir);
691 unlink_or_warn(sb.buf);
695 /* Connect module worktree and git dir */
696 connect_work_tree_and_git_dir(path, sm_gitdir);
698 p = git_pathdup_submodule(path, "config");
700 die(_("could not get submodule directory for '%s'"), path);
702 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
703 git_config_get_string("submodule.alternateLocation", &sm_alternate);
705 git_config_set_in_file(p, "submodule.alternateLocation",
707 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
709 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
713 free(error_strategy);
722 struct submodule_update_clone {
723 /* index into 'list', the list of submodules to look into for cloning */
725 struct module_list list;
726 unsigned warn_if_uninitialized : 1;
728 /* update parameter passed via commandline */
729 struct submodule_update_strategy update;
731 /* configuration parameters which are passed on to the children */
734 int recommend_shallow;
735 struct string_list references;
737 const char *recursive_prefix;
740 /* Machine-readable status lines to be consumed by git-submodule.sh */
741 struct string_list projectlines;
743 /* If we want to stop as fast as possible and return an error */
744 unsigned quickstop : 1;
746 /* failed clones to be retried again */
747 const struct cache_entry **failed_clones;
748 int failed_clones_nr, failed_clones_alloc;
750 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
751 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \
753 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
756 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
757 struct strbuf *out, const char *displaypath)
760 * Only mention uninitialized submodules when their
761 * paths have been specified.
763 if (suc->warn_if_uninitialized) {
765 _("Submodule path '%s' not initialized"),
767 strbuf_addch(out, '\n');
769 _("Maybe you want to use 'update --init'?"));
770 strbuf_addch(out, '\n');
775 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
776 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
778 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
779 struct child_process *child,
780 struct submodule_update_clone *suc,
783 const struct submodule *sub = NULL;
784 struct strbuf displaypath_sb = STRBUF_INIT;
785 struct strbuf sb = STRBUF_INIT;
786 const char *displaypath = NULL;
787 int needs_cloning = 0;
790 if (suc->recursive_prefix)
791 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
793 strbuf_addstr(&sb, ce->name);
794 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
795 strbuf_addch(out, '\n');
799 sub = submodule_from_path(null_sha1, ce->name);
801 if (suc->recursive_prefix)
802 displaypath = relative_path(suc->recursive_prefix,
803 ce->name, &displaypath_sb);
805 displaypath = ce->name;
808 next_submodule_warn_missing(suc, out, displaypath);
812 if (suc->update.type == SM_UPDATE_NONE
813 || (suc->update.type == SM_UPDATE_UNSPECIFIED
814 && sub->update_strategy.type == SM_UPDATE_NONE)) {
815 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
816 strbuf_addch(out, '\n');
820 /* Check if the submodule has been initialized. */
821 if (!is_submodule_initialized(ce->name)) {
822 next_submodule_warn_missing(suc, out, displaypath);
827 strbuf_addf(&sb, "%s/.git", ce->name);
828 needs_cloning = !file_exists(sb.buf);
831 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
832 oid_to_hex(&ce->oid), ce_stage(ce),
833 needs_cloning, ce->name);
834 string_list_append(&suc->projectlines, sb.buf);
841 child->stdout_to_stderr = 1;
843 argv_array_push(&child->args, "submodule--helper");
844 argv_array_push(&child->args, "clone");
846 argv_array_push(&child->args, "--progress");
848 argv_array_push(&child->args, "--quiet");
850 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
851 if (suc->recommend_shallow && sub->recommend_shallow == 1)
852 argv_array_push(&child->args, "--depth=1");
853 argv_array_pushl(&child->args, "--path", sub->path, NULL);
854 argv_array_pushl(&child->args, "--name", sub->name, NULL);
855 argv_array_pushl(&child->args, "--url", sub->url, NULL);
856 if (suc->references.nr) {
857 struct string_list_item *item;
858 for_each_string_list_item(item, &suc->references)
859 argv_array_pushl(&child->args, "--reference", item->string, NULL);
862 argv_array_push(&child->args, suc->depth);
865 strbuf_reset(&displaypath_sb);
868 return needs_cloning;
871 static int update_clone_get_next_task(struct child_process *child,
876 struct submodule_update_clone *suc = suc_cb;
877 const struct cache_entry *ce;
880 for (; suc->current < suc->list.nr; suc->current++) {
881 ce = suc->list.entries[suc->current];
882 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
883 int *p = xmalloc(sizeof(*p));
892 * The loop above tried cloning each submodule once, now try the
893 * stragglers again, which we can imagine as an extension of the
896 index = suc->current - suc->list.nr;
897 if (index < suc->failed_clones_nr) {
899 ce = suc->failed_clones[index];
900 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
902 strbuf_addstr(err, "BUG: submodule considered for "
903 "cloning, doesn't need cloning "
907 p = xmalloc(sizeof(*p));
917 static int update_clone_start_failure(struct strbuf *err,
921 struct submodule_update_clone *suc = suc_cb;
926 static int update_clone_task_finished(int result,
931 const struct cache_entry *ce;
932 struct submodule_update_clone *suc = suc_cb;
934 int *idxP = *(int**)idx_task_cb;
941 if (idx < suc->list.nr) {
942 ce = suc->list.entries[idx];
943 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
945 strbuf_addch(err, '\n');
946 ALLOC_GROW(suc->failed_clones,
947 suc->failed_clones_nr + 1,
948 suc->failed_clones_alloc);
949 suc->failed_clones[suc->failed_clones_nr++] = ce;
953 ce = suc->failed_clones[idx];
954 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
956 strbuf_addch(err, '\n');
964 static int update_clone(int argc, const char **argv, const char *prefix)
966 const char *update = NULL;
968 struct string_list_item *item;
969 struct pathspec pathspec;
970 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
972 struct option module_update_clone_options[] = {
973 OPT_STRING(0, "prefix", &prefix,
975 N_("path into the working tree")),
976 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
978 N_("path into the working tree, across nested "
979 "submodule boundaries")),
980 OPT_STRING(0, "update", &update,
982 N_("rebase, merge, checkout or none")),
983 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
984 N_("reference repository")),
985 OPT_STRING(0, "depth", &suc.depth, "<depth>",
986 N_("Create a shallow clone truncated to the "
987 "specified number of revisions")),
988 OPT_INTEGER('j', "jobs", &max_jobs,
989 N_("parallel jobs")),
990 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
991 N_("whether the initial clone should follow the shallow recommendation")),
992 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
993 OPT_BOOL(0, "progress", &suc.progress,
994 N_("force cloning progress")),
998 const char *const git_submodule_helper_usage[] = {
999 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1002 suc.prefix = prefix;
1004 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1005 git_submodule_helper_usage, 0);
1008 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1009 die(_("bad value for update parameter"));
1011 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1015 suc.warn_if_uninitialized = 1;
1017 /* Overlay the parsed .gitmodules file with .git/config */
1018 gitmodules_config();
1019 git_config(submodule_config, NULL);
1022 max_jobs = parallel_submodules();
1024 run_processes_parallel(max_jobs,
1025 update_clone_get_next_task,
1026 update_clone_start_failure,
1027 update_clone_task_finished,
1031 * We saved the output and put it out all at once now.
1033 * - the listener does not have to interleave their (checkout)
1034 * work with our fetching. The writes involved in a
1035 * checkout involve more straightforward sequential I/O.
1036 * - the listener can avoid doing any work if fetching failed.
1041 for_each_string_list_item(item, &suc.projectlines)
1042 utf8_fprintf(stdout, "%s", item->string);
1047 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1049 struct strbuf sb = STRBUF_INIT;
1051 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1053 printf("%s", relative_path(argv[1], argv[2], &sb));
1054 strbuf_release(&sb);
1058 static const char *remote_submodule_branch(const char *path)
1060 const struct submodule *sub;
1061 gitmodules_config();
1062 git_config(submodule_config, NULL);
1064 sub = submodule_from_path(null_sha1, path);
1071 if (!strcmp(sub->branch, ".")) {
1072 unsigned char sha1[20];
1073 const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL);
1076 die(_("No such ref: %s"), "HEAD");
1079 if (!strcmp(refname, "HEAD"))
1080 die(_("Submodule (%s) branch configured to inherit "
1081 "branch from superproject, but the superproject "
1082 "is not on any branch"), sub->name);
1084 if (!skip_prefix(refname, "refs/heads/", &refname))
1085 die(_("Expecting a full ref name, got %s"), refname);
1092 static int resolve_remote_submodule_branch(int argc, const char **argv,
1096 struct strbuf sb = STRBUF_INIT;
1098 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1100 ret = remote_submodule_branch(argv[1]);
1102 die("submodule %s doesn't exist", argv[1]);
1105 strbuf_release(&sb);
1109 static int push_check(int argc, const char **argv, const char *prefix)
1111 struct remote *remote;
1114 die("submodule--helper push-check requires at least 1 argument");
1117 * The remote must be configured.
1118 * This is to avoid pushing to the exact same URL as the parent.
1120 remote = pushremote_get(argv[1]);
1121 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1122 die("remote '%s' not configured", argv[1]);
1124 /* Check the refspec */
1126 int i, refspec_nr = argc - 2;
1127 struct ref *local_refs = get_local_heads();
1128 struct refspec *refspec = parse_push_refspec(refspec_nr,
1131 for (i = 0; i < refspec_nr; i++) {
1132 struct refspec *rs = refspec + i;
1134 if (rs->pattern || rs->matching)
1138 * LHS must match a single ref
1139 * NEEDSWORK: add logic to special case 'HEAD' once
1140 * working with submodules in a detached head state
1141 * ceases to be the norm.
1143 if (count_refspec_match(rs->src, local_refs, NULL) != 1)
1144 die("src refspec '%s' must name a ref",
1147 free_refspec(refspec_nr, refspec);
1153 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1156 struct pathspec pathspec;
1157 struct module_list list = MODULE_LIST_INIT;
1158 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1160 struct option embed_gitdir_options[] = {
1161 OPT_STRING(0, "prefix", &prefix,
1163 N_("path into the working tree")),
1164 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1165 ABSORB_GITDIR_RECURSE_SUBMODULES),
1169 const char *const git_submodule_helper_usage[] = {
1170 N_("git submodule--helper embed-git-dir [<path>...]"),
1174 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1175 git_submodule_helper_usage, 0);
1177 gitmodules_config();
1178 git_config(submodule_config, NULL);
1180 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1183 for (i = 0; i < list.nr; i++)
1184 absorb_git_dir_into_superproject(prefix,
1185 list.entries[i]->name, flags);
1190 static int is_active(int argc, const char **argv, const char *prefix)
1193 die("submodule--helper is-active takes exactly 1 argument");
1195 gitmodules_config();
1197 return !is_submodule_initialized(argv[1]);
1200 #define SUPPORT_SUPER_PREFIX (1<<0)
1204 int (*fn)(int, const char **, const char *);
1208 static struct cmd_struct commands[] = {
1209 {"list", module_list, 0},
1210 {"name", module_name, 0},
1211 {"clone", module_clone, 0},
1212 {"update-clone", update_clone, 0},
1213 {"relative-path", resolve_relative_path, 0},
1214 {"resolve-relative-url", resolve_relative_url, 0},
1215 {"resolve-relative-url-test", resolve_relative_url_test, 0},
1216 {"init", module_init, SUPPORT_SUPER_PREFIX},
1217 {"remote-branch", resolve_remote_submodule_branch, 0},
1218 {"push-check", push_check, 0},
1219 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1220 {"is-active", is_active, 0},
1223 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
1227 die(_("submodule--helper subcommand must be "
1228 "called with a subcommand"));
1230 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1231 if (!strcmp(argv[1], commands[i].cmd)) {
1232 if (get_super_prefix() &&
1233 !(commands[i].option & SUPPORT_SUPER_PREFIX))
1234 die(_("%s doesn't support --super-prefix"),
1236 return commands[i].fn(argc - 1, argv + 1, prefix);
1240 die(_("'%s' is not a valid submodule--helper "
1241 "subcommand"), argv[1]);