2 #include "repository.h"
5 #include "parse-options.h"
10 #include "submodule-config.h"
11 #include "string-list.h"
12 #include "run-command.h"
20 #define OPT_QUIET (1 << 0)
21 #define OPT_CACHED (1 << 1)
22 #define OPT_RECURSIVE (1 << 2)
24 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
27 static char *get_default_remote(void)
29 char *dest = NULL, *ret;
30 struct strbuf sb = STRBUF_INIT;
31 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
34 die(_("No such ref: %s"), "HEAD");
37 if (!strcmp(refname, "HEAD"))
38 return xstrdup("origin");
40 if (!skip_prefix(refname, "refs/heads/", &refname))
41 die(_("Expecting a full ref name, got %s"), refname);
43 strbuf_addf(&sb, "branch.%s.remote", refname);
44 if (git_config_get_string(sb.buf, &dest))
45 ret = xstrdup("origin");
53 static int starts_with_dot_slash(const char *str)
55 return str[0] == '.' && is_dir_sep(str[1]);
58 static int starts_with_dot_dot_slash(const char *str)
60 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
64 * Returns 1 if it was the last chop before ':'.
66 static int chop_last_dir(char **remoteurl, int is_relative)
68 char *rfind = find_last_dir_sep(*remoteurl);
74 rfind = strrchr(*remoteurl, ':');
80 if (is_relative || !strcmp(".", *remoteurl))
81 die(_("cannot strip one component off url '%s'"),
85 *remoteurl = xstrdup(".");
90 * The `url` argument is the URL that navigates to the submodule origin
91 * repo. When relative, this URL is relative to the superproject origin
92 * URL repo. The `up_path` argument, if specified, is the relative
93 * path that navigates from the submodule working tree to the superproject
94 * working tree. Returns the origin URL of the submodule.
96 * Return either an absolute URL or filesystem path (if the superproject
97 * origin URL is an absolute URL or filesystem path, respectively) or a
98 * relative file system path (if the superproject origin URL is a relative
101 * When the output is a relative file system path, the path is either
102 * relative to the submodule working tree, if up_path is specified, or to
103 * the superproject working tree otherwise.
105 * NEEDSWORK: This works incorrectly on the domain and protocol part.
106 * remote_url url outcome expectation
107 * http://a.com/b ../c http://a.com/c as is
108 * http://a.com/b/ ../c http://a.com/c same as previous line, but
109 * ignore trailing slash in url
110 * http://a.com/b ../../c http://c error out
111 * http://a.com/b ../../../c http:/c error out
112 * http://a.com/b ../../../../c http:c error out
113 * http://a.com/b ../../../../../c .:c error out
114 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
115 * when a local part has a colon in its path component, too.
117 static char *relative_url(const char *remote_url,
124 char *remoteurl = xstrdup(remote_url);
125 struct strbuf sb = STRBUF_INIT;
126 size_t len = strlen(remoteurl);
128 if (is_dir_sep(remoteurl[len-1]))
129 remoteurl[len-1] = '\0';
131 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
136 * Prepend a './' to ensure all relative
137 * remoteurls start with './' or '../'
139 if (!starts_with_dot_slash(remoteurl) &&
140 !starts_with_dot_dot_slash(remoteurl)) {
142 strbuf_addf(&sb, "./%s", remoteurl);
144 remoteurl = strbuf_detach(&sb, NULL);
148 * When the url starts with '../', remove that and the
149 * last directory in remoteurl.
152 if (starts_with_dot_dot_slash(url)) {
154 colonsep |= chop_last_dir(&remoteurl, is_relative);
155 } else if (starts_with_dot_slash(url))
161 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
162 if (ends_with(url, "/"))
163 strbuf_setlen(&sb, sb.len - 1);
166 if (starts_with_dot_slash(sb.buf))
167 out = xstrdup(sb.buf + 2);
169 out = xstrdup(sb.buf);
172 if (!up_path || !is_relative)
175 strbuf_addf(&sb, "%s%s", up_path, out);
177 return strbuf_detach(&sb, NULL);
180 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
182 char *remoteurl = NULL;
183 char *remote = get_default_remote();
184 const char *up_path = NULL;
187 struct strbuf sb = STRBUF_INIT;
189 if (argc != 2 && argc != 3)
190 die("resolve-relative-url only accepts one or two arguments");
193 strbuf_addf(&sb, "remote.%s.url", remote);
196 if (git_config_get_string(sb.buf, &remoteurl))
197 /* the repository is its own authoritative upstream */
198 remoteurl = xgetcwd();
203 res = relative_url(remoteurl, url, up_path);
210 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
212 char *remoteurl, *res;
213 const char *up_path, *url;
216 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
219 remoteurl = xstrdup(argv[2]);
222 if (!strcmp(up_path, "(null)"))
225 res = relative_url(remoteurl, url, up_path);
232 /* the result should be freed by the caller. */
233 static char *get_submodule_displaypath(const char *path, const char *prefix)
235 const char *super_prefix = get_super_prefix();
237 if (prefix && super_prefix) {
238 BUG("cannot have prefix '%s' and superprefix '%s'",
239 prefix, super_prefix);
241 struct strbuf sb = STRBUF_INIT;
242 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
245 } else if (super_prefix) {
246 return xstrfmt("%s%s", super_prefix, path);
248 return xstrdup(path);
252 static char *compute_rev_name(const char *sub_path, const char* object_id)
254 struct strbuf sb = STRBUF_INIT;
257 static const char *describe_bare[] = { NULL };
259 static const char *describe_tags[] = { "--tags", NULL };
261 static const char *describe_contains[] = { "--contains", NULL };
263 static const char *describe_all_always[] = { "--all", "--always", NULL };
265 static const char **describe_argv[] = { describe_bare, describe_tags,
267 describe_all_always, NULL };
269 for (d = describe_argv; *d; d++) {
270 struct child_process cp = CHILD_PROCESS_INIT;
271 prepare_submodule_repo_env(&cp.env_array);
276 argv_array_push(&cp.args, "describe");
277 argv_array_pushv(&cp.args, *d);
278 argv_array_push(&cp.args, object_id);
280 if (!capture_command(&cp, &sb, 0)) {
281 strbuf_strip_suffix(&sb, "\n");
282 return strbuf_detach(&sb, NULL);
291 const struct cache_entry **entries;
294 #define MODULE_LIST_INIT { NULL, 0, 0 }
296 static int module_list_compute(int argc, const char **argv,
298 struct pathspec *pathspec,
299 struct module_list *list)
302 char *ps_matched = NULL;
303 parse_pathspec(pathspec, 0,
304 PATHSPEC_PREFER_FULL,
308 ps_matched = xcalloc(pathspec->nr, 1);
310 if (read_cache() < 0)
311 die(_("index file corrupt"));
313 for (i = 0; i < active_nr; i++) {
314 const struct cache_entry *ce = active_cache[i];
316 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
318 !S_ISGITLINK(ce->ce_mode))
321 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
322 list->entries[list->nr++] = ce;
323 while (i + 1 < active_nr &&
324 !strcmp(ce->name, active_cache[i + 1]->name))
326 * Skip entries with the same name in different stages
327 * to make sure an entry is returned only once.
332 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
340 static void module_list_active(struct module_list *list)
343 struct module_list active_modules = MODULE_LIST_INIT;
345 for (i = 0; i < list->nr; i++) {
346 const struct cache_entry *ce = list->entries[i];
348 if (!is_submodule_active(the_repository, ce->name))
351 ALLOC_GROW(active_modules.entries,
352 active_modules.nr + 1,
353 active_modules.alloc);
354 active_modules.entries[active_modules.nr++] = ce;
358 *list = active_modules;
361 static int module_list(int argc, const char **argv, const char *prefix)
364 struct pathspec pathspec;
365 struct module_list list = MODULE_LIST_INIT;
367 struct option module_list_options[] = {
368 OPT_STRING(0, "prefix", &prefix,
370 N_("alternative anchor for relative paths")),
374 const char *const git_submodule_helper_usage[] = {
375 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
379 argc = parse_options(argc, argv, prefix, module_list_options,
380 git_submodule_helper_usage, 0);
382 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
385 for (i = 0; i < list.nr; i++) {
386 const struct cache_entry *ce = list.entries[i];
389 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
391 printf("%06o %s %d\t", ce->ce_mode,
392 oid_to_hex(&ce->oid), ce_stage(ce));
394 fprintf(stdout, "%s\n", ce->name);
399 static void for_each_listed_submodule(const struct module_list *list,
400 each_submodule_fn fn, void *cb_data)
403 for (i = 0; i < list->nr; i++)
404 fn(list->entries[i], cb_data);
412 #define INIT_CB_INIT { NULL, 0 }
414 static void init_submodule(const char *path, const char *prefix,
417 const struct submodule *sub;
418 struct strbuf sb = STRBUF_INIT;
419 char *upd = NULL, *url = NULL, *displaypath;
421 displaypath = get_submodule_displaypath(path, prefix);
423 sub = submodule_from_path(&null_oid, path);
426 die(_("No url found for submodule path '%s' in .gitmodules"),
430 * NEEDSWORK: In a multi-working-tree world, this needs to be
431 * set in the per-worktree config.
433 * Set active flag for the submodule being initialized
435 if (!is_submodule_active(the_repository, path)) {
436 strbuf_addf(&sb, "submodule.%s.active", sub->name);
437 git_config_set_gently(sb.buf, "true");
442 * Copy url setting when it is not set yet.
443 * To look up the url in .git/config, we must not fall back to
444 * .gitmodules, so look it up directly.
446 strbuf_addf(&sb, "submodule.%s.url", sub->name);
447 if (git_config_get_string(sb.buf, &url)) {
449 die(_("No url found for submodule path '%s' in .gitmodules"),
452 url = xstrdup(sub->url);
454 /* Possibly a url relative to parent */
455 if (starts_with_dot_dot_slash(url) ||
456 starts_with_dot_slash(url)) {
457 char *remoteurl, *relurl;
458 char *remote = get_default_remote();
459 struct strbuf remotesb = STRBUF_INIT;
460 strbuf_addf(&remotesb, "remote.%s.url", remote);
463 if (git_config_get_string(remotesb.buf, &remoteurl)) {
464 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
465 remoteurl = xgetcwd();
467 relurl = relative_url(remoteurl, url, NULL);
468 strbuf_release(&remotesb);
474 if (git_config_set_gently(sb.buf, url))
475 die(_("Failed to register url for submodule path '%s'"),
477 if (!(flags & OPT_QUIET))
479 _("Submodule '%s' (%s) registered for path '%s'\n"),
480 sub->name, url, displaypath);
484 /* Copy "update" setting when it is not set yet */
485 strbuf_addf(&sb, "submodule.%s.update", sub->name);
486 if (git_config_get_string(sb.buf, &upd) &&
487 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
488 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
489 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
491 upd = xstrdup("none");
493 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
495 if (git_config_set_gently(sb.buf, upd))
496 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
504 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
506 struct init_cb *info = cb_data;
507 init_submodule(list_item->name, info->prefix, info->flags);
510 static int module_init(int argc, const char **argv, const char *prefix)
512 struct init_cb info = INIT_CB_INIT;
513 struct pathspec pathspec;
514 struct module_list list = MODULE_LIST_INIT;
517 struct option module_init_options[] = {
518 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
522 const char *const git_submodule_helper_usage[] = {
523 N_("git submodule--helper init [<path>]"),
527 argc = parse_options(argc, argv, prefix, module_init_options,
528 git_submodule_helper_usage, 0);
530 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
534 * If there are no path args and submodule.active is set then,
535 * by default, only initialize 'active' modules.
537 if (!argc && git_config_get_value_multi("submodule.active"))
538 module_list_active(&list);
540 info.prefix = prefix;
542 info.flags |= OPT_QUIET;
544 for_each_listed_submodule(&list, init_submodule_cb, &info);
554 #define STATUS_CB_INIT { NULL, 0 }
556 static void print_status(unsigned int flags, char state, const char *path,
557 const struct object_id *oid, const char *displaypath)
559 if (flags & OPT_QUIET)
562 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
564 if (state == ' ' || state == '+')
565 printf(" (%s)", compute_rev_name(path, oid_to_hex(oid)));
570 static int handle_submodule_head_ref(const char *refname,
571 const struct object_id *oid, int flags,
574 struct object_id *output = cb_data;
581 static void status_submodule(const char *path, const struct object_id *ce_oid,
582 unsigned int ce_flags, const char *prefix,
586 struct argv_array diff_files_args = ARGV_ARRAY_INIT;
588 int diff_files_result;
590 if (!submodule_from_path(&null_oid, path))
591 die(_("no submodule mapping found in .gitmodules for path '%s'"),
594 displaypath = get_submodule_displaypath(path, prefix);
596 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
597 print_status(flags, 'U', path, &null_oid, displaypath);
601 if (!is_submodule_active(the_repository, path)) {
602 print_status(flags, '-', path, ce_oid, displaypath);
606 argv_array_pushl(&diff_files_args, "diff-files",
607 "--ignore-submodules=dirty", "--quiet", "--",
610 git_config(git_diff_basic_config, NULL);
611 init_revisions(&rev, prefix);
613 diff_files_args.argc = setup_revisions(diff_files_args.argc,
614 diff_files_args.argv,
616 diff_files_result = run_diff_files(&rev, 0);
618 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
619 print_status(flags, ' ', path, ce_oid,
621 } else if (!(flags & OPT_CACHED)) {
622 struct object_id oid;
624 if (refs_head_ref(get_submodule_ref_store(path),
625 handle_submodule_head_ref, &oid))
626 die(_("could not resolve HEAD ref inside the "
627 "submodule '%s'"), path);
629 print_status(flags, '+', path, &oid, displaypath);
631 print_status(flags, '+', path, ce_oid, displaypath);
634 if (flags & OPT_RECURSIVE) {
635 struct child_process cpr = CHILD_PROCESS_INIT;
639 prepare_submodule_repo_env(&cpr.env_array);
641 argv_array_push(&cpr.args, "--super-prefix");
642 argv_array_pushf(&cpr.args, "%s/", displaypath);
643 argv_array_pushl(&cpr.args, "submodule--helper", "status",
644 "--recursive", NULL);
646 if (flags & OPT_CACHED)
647 argv_array_push(&cpr.args, "--cached");
649 if (flags & OPT_QUIET)
650 argv_array_push(&cpr.args, "--quiet");
652 if (run_command(&cpr))
653 die(_("failed to recurse into submodule '%s'"), path);
657 argv_array_clear(&diff_files_args);
661 static void status_submodule_cb(const struct cache_entry *list_item,
664 struct status_cb *info = cb_data;
665 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
666 info->prefix, info->flags);
669 static int module_status(int argc, const char **argv, const char *prefix)
671 struct status_cb info = STATUS_CB_INIT;
672 struct pathspec pathspec;
673 struct module_list list = MODULE_LIST_INIT;
676 struct option module_status_options[] = {
677 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
678 OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
679 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
683 const char *const git_submodule_helper_usage[] = {
684 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
688 argc = parse_options(argc, argv, prefix, module_status_options,
689 git_submodule_helper_usage, 0);
691 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
694 info.prefix = prefix;
696 info.flags |= OPT_QUIET;
698 for_each_listed_submodule(&list, status_submodule_cb, &info);
703 static int module_name(int argc, const char **argv, const char *prefix)
705 const struct submodule *sub;
708 usage(_("git submodule--helper name <path>"));
710 sub = submodule_from_path(&null_oid, argv[1]);
713 die(_("no submodule mapping found in .gitmodules for path '%s'"),
716 printf("%s\n", sub->name);
721 static int clone_submodule(const char *path, const char *gitdir, const char *url,
722 const char *depth, struct string_list *reference,
723 int quiet, int progress)
725 struct child_process cp = CHILD_PROCESS_INIT;
727 argv_array_push(&cp.args, "clone");
728 argv_array_push(&cp.args, "--no-checkout");
730 argv_array_push(&cp.args, "--quiet");
732 argv_array_push(&cp.args, "--progress");
734 argv_array_pushl(&cp.args, "--depth", depth, NULL);
736 struct string_list_item *item;
737 for_each_string_list_item(item, reference)
738 argv_array_pushl(&cp.args, "--reference",
741 if (gitdir && *gitdir)
742 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
744 argv_array_push(&cp.args, url);
745 argv_array_push(&cp.args, path);
748 prepare_submodule_repo_env(&cp.env_array);
751 return run_command(&cp);
754 struct submodule_alternate_setup {
755 const char *submodule_name;
756 enum SUBMODULE_ALTERNATE_ERROR_MODE {
757 SUBMODULE_ALTERNATE_ERROR_DIE,
758 SUBMODULE_ALTERNATE_ERROR_INFO,
759 SUBMODULE_ALTERNATE_ERROR_IGNORE
761 struct string_list *reference;
763 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
764 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
766 static int add_possible_reference_from_superproject(
767 struct alternate_object_database *alt, void *sas_cb)
769 struct submodule_alternate_setup *sas = sas_cb;
772 * If the alternate object store is another repository, try the
773 * standard layout with .git/(modules/<name>)+/objects
775 if (ends_with(alt->path, "/objects")) {
777 struct strbuf sb = STRBUF_INIT;
778 struct strbuf err = STRBUF_INIT;
779 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
782 * We need to end the new path with '/' to mark it as a dir,
783 * otherwise a submodule name containing '/' will be broken
784 * as the last part of a missing submodule reference would
785 * be taken as a file name.
787 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
789 sm_alternate = compute_alternate_path(sb.buf, &err);
791 string_list_append(sas->reference, xstrdup(sb.buf));
794 switch (sas->error_mode) {
795 case SUBMODULE_ALTERNATE_ERROR_DIE:
796 die(_("submodule '%s' cannot add alternate: %s"),
797 sas->submodule_name, err.buf);
798 case SUBMODULE_ALTERNATE_ERROR_INFO:
799 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
800 sas->submodule_name, err.buf);
801 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
811 static void prepare_possible_alternates(const char *sm_name,
812 struct string_list *reference)
814 char *sm_alternate = NULL, *error_strategy = NULL;
815 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
817 git_config_get_string("submodule.alternateLocation", &sm_alternate);
821 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
824 error_strategy = xstrdup("die");
826 sas.submodule_name = sm_name;
827 sas.reference = reference;
828 if (!strcmp(error_strategy, "die"))
829 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
830 else if (!strcmp(error_strategy, "info"))
831 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
832 else if (!strcmp(error_strategy, "ignore"))
833 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
835 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
837 if (!strcmp(sm_alternate, "superproject"))
838 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
839 else if (!strcmp(sm_alternate, "no"))
842 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
845 free(error_strategy);
848 static int module_clone(int argc, const char **argv, const char *prefix)
850 const char *name = NULL, *url = NULL, *depth = NULL;
853 char *p, *path = NULL, *sm_gitdir;
854 struct strbuf sb = STRBUF_INIT;
855 struct string_list reference = STRING_LIST_INIT_NODUP;
856 char *sm_alternate = NULL, *error_strategy = NULL;
858 struct option module_clone_options[] = {
859 OPT_STRING(0, "prefix", &prefix,
861 N_("alternative anchor for relative paths")),
862 OPT_STRING(0, "path", &path,
864 N_("where the new submodule will be cloned to")),
865 OPT_STRING(0, "name", &name,
867 N_("name of the new submodule")),
868 OPT_STRING(0, "url", &url,
870 N_("url where to clone the submodule from")),
871 OPT_STRING_LIST(0, "reference", &reference,
873 N_("reference repository")),
874 OPT_STRING(0, "depth", &depth,
876 N_("depth for shallow clones")),
877 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
878 OPT_BOOL(0, "progress", &progress,
879 N_("force cloning progress")),
883 const char *const git_submodule_helper_usage[] = {
884 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
885 "[--reference <repository>] [--name <name>] [--depth <depth>] "
886 "--url <url> --path <path>"),
890 argc = parse_options(argc, argv, prefix, module_clone_options,
891 git_submodule_helper_usage, 0);
893 if (argc || !url || !path || !*path)
894 usage_with_options(git_submodule_helper_usage,
895 module_clone_options);
897 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
898 sm_gitdir = absolute_pathdup(sb.buf);
901 if (!is_absolute_path(path)) {
902 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
903 path = strbuf_detach(&sb, NULL);
905 path = xstrdup(path);
907 if (!file_exists(sm_gitdir)) {
908 if (safe_create_leading_directories_const(sm_gitdir) < 0)
909 die(_("could not create directory '%s'"), sm_gitdir);
911 prepare_possible_alternates(name, &reference);
913 if (clone_submodule(path, sm_gitdir, url, depth, &reference,
915 die(_("clone of '%s' into submodule path '%s' failed"),
918 if (safe_create_leading_directories_const(path) < 0)
919 die(_("could not create directory '%s'"), path);
920 strbuf_addf(&sb, "%s/index", sm_gitdir);
921 unlink_or_warn(sb.buf);
925 /* Connect module worktree and git dir */
926 connect_work_tree_and_git_dir(path, sm_gitdir);
928 p = git_pathdup_submodule(path, "config");
930 die(_("could not get submodule directory for '%s'"), path);
932 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
933 git_config_get_string("submodule.alternateLocation", &sm_alternate);
935 git_config_set_in_file(p, "submodule.alternateLocation",
937 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
939 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
943 free(error_strategy);
952 struct submodule_update_clone {
953 /* index into 'list', the list of submodules to look into for cloning */
955 struct module_list list;
956 unsigned warn_if_uninitialized : 1;
958 /* update parameter passed via commandline */
959 struct submodule_update_strategy update;
961 /* configuration parameters which are passed on to the children */
964 int recommend_shallow;
965 struct string_list references;
967 const char *recursive_prefix;
970 /* Machine-readable status lines to be consumed by git-submodule.sh */
971 struct string_list projectlines;
973 /* If we want to stop as fast as possible and return an error */
974 unsigned quickstop : 1;
976 /* failed clones to be retried again */
977 const struct cache_entry **failed_clones;
978 int failed_clones_nr, failed_clones_alloc;
980 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
981 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \
983 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
986 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
987 struct strbuf *out, const char *displaypath)
990 * Only mention uninitialized submodules when their
991 * paths have been specified.
993 if (suc->warn_if_uninitialized) {
995 _("Submodule path '%s' not initialized"),
997 strbuf_addch(out, '\n');
999 _("Maybe you want to use 'update --init'?"));
1000 strbuf_addch(out, '\n');
1005 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1006 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1008 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1009 struct child_process *child,
1010 struct submodule_update_clone *suc,
1013 const struct submodule *sub = NULL;
1014 const char *url = NULL;
1015 const char *update_string;
1016 enum submodule_update_type update_type;
1018 struct strbuf displaypath_sb = STRBUF_INIT;
1019 struct strbuf sb = STRBUF_INIT;
1020 const char *displaypath = NULL;
1021 int needs_cloning = 0;
1024 if (suc->recursive_prefix)
1025 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1027 strbuf_addstr(&sb, ce->name);
1028 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1029 strbuf_addch(out, '\n');
1033 sub = submodule_from_path(&null_oid, ce->name);
1035 if (suc->recursive_prefix)
1036 displaypath = relative_path(suc->recursive_prefix,
1037 ce->name, &displaypath_sb);
1039 displaypath = ce->name;
1042 next_submodule_warn_missing(suc, out, displaypath);
1046 key = xstrfmt("submodule.%s.update", sub->name);
1047 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1048 update_type = parse_submodule_update_type(update_string);
1050 update_type = sub->update_strategy.type;
1054 if (suc->update.type == SM_UPDATE_NONE
1055 || (suc->update.type == SM_UPDATE_UNSPECIFIED
1056 && update_type == SM_UPDATE_NONE)) {
1057 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1058 strbuf_addch(out, '\n');
1062 /* Check if the submodule has been initialized. */
1063 if (!is_submodule_active(the_repository, ce->name)) {
1064 next_submodule_warn_missing(suc, out, displaypath);
1069 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1070 if (repo_config_get_string_const(the_repository, sb.buf, &url))
1074 strbuf_addf(&sb, "%s/.git", ce->name);
1075 needs_cloning = !file_exists(sb.buf);
1078 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
1079 oid_to_hex(&ce->oid), ce_stage(ce),
1080 needs_cloning, ce->name);
1081 string_list_append(&suc->projectlines, sb.buf);
1087 child->no_stdin = 1;
1088 child->stdout_to_stderr = 1;
1090 argv_array_push(&child->args, "submodule--helper");
1091 argv_array_push(&child->args, "clone");
1093 argv_array_push(&child->args, "--progress");
1095 argv_array_push(&child->args, "--quiet");
1097 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1098 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1099 argv_array_push(&child->args, "--depth=1");
1100 argv_array_pushl(&child->args, "--path", sub->path, NULL);
1101 argv_array_pushl(&child->args, "--name", sub->name, NULL);
1102 argv_array_pushl(&child->args, "--url", url, NULL);
1103 if (suc->references.nr) {
1104 struct string_list_item *item;
1105 for_each_string_list_item(item, &suc->references)
1106 argv_array_pushl(&child->args, "--reference", item->string, NULL);
1109 argv_array_push(&child->args, suc->depth);
1112 strbuf_reset(&displaypath_sb);
1115 return needs_cloning;
1118 static int update_clone_get_next_task(struct child_process *child,
1123 struct submodule_update_clone *suc = suc_cb;
1124 const struct cache_entry *ce;
1127 for (; suc->current < suc->list.nr; suc->current++) {
1128 ce = suc->list.entries[suc->current];
1129 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1130 int *p = xmalloc(sizeof(*p));
1139 * The loop above tried cloning each submodule once, now try the
1140 * stragglers again, which we can imagine as an extension of the
1143 index = suc->current - suc->list.nr;
1144 if (index < suc->failed_clones_nr) {
1146 ce = suc->failed_clones[index];
1147 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1149 strbuf_addstr(err, "BUG: submodule considered for "
1150 "cloning, doesn't need cloning "
1154 p = xmalloc(sizeof(*p));
1164 static int update_clone_start_failure(struct strbuf *err,
1168 struct submodule_update_clone *suc = suc_cb;
1173 static int update_clone_task_finished(int result,
1178 const struct cache_entry *ce;
1179 struct submodule_update_clone *suc = suc_cb;
1181 int *idxP = idx_task_cb;
1188 if (idx < suc->list.nr) {
1189 ce = suc->list.entries[idx];
1190 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1192 strbuf_addch(err, '\n');
1193 ALLOC_GROW(suc->failed_clones,
1194 suc->failed_clones_nr + 1,
1195 suc->failed_clones_alloc);
1196 suc->failed_clones[suc->failed_clones_nr++] = ce;
1199 idx -= suc->list.nr;
1200 ce = suc->failed_clones[idx];
1201 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1203 strbuf_addch(err, '\n');
1211 static int gitmodules_update_clone_config(const char *var, const char *value,
1215 if (!strcmp(var, "submodule.fetchjobs"))
1216 *max_jobs = parse_submodule_fetchjobs(var, value);
1220 static int update_clone(int argc, const char **argv, const char *prefix)
1222 const char *update = NULL;
1224 struct string_list_item *item;
1225 struct pathspec pathspec;
1226 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1228 struct option module_update_clone_options[] = {
1229 OPT_STRING(0, "prefix", &prefix,
1231 N_("path into the working tree")),
1232 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1234 N_("path into the working tree, across nested "
1235 "submodule boundaries")),
1236 OPT_STRING(0, "update", &update,
1238 N_("rebase, merge, checkout or none")),
1239 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1240 N_("reference repository")),
1241 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1242 N_("Create a shallow clone truncated to the "
1243 "specified number of revisions")),
1244 OPT_INTEGER('j', "jobs", &max_jobs,
1245 N_("parallel jobs")),
1246 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1247 N_("whether the initial clone should follow the shallow recommendation")),
1248 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1249 OPT_BOOL(0, "progress", &suc.progress,
1250 N_("force cloning progress")),
1254 const char *const git_submodule_helper_usage[] = {
1255 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1258 suc.prefix = prefix;
1260 config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
1261 git_config(gitmodules_update_clone_config, &max_jobs);
1263 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1264 git_submodule_helper_usage, 0);
1267 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1268 die(_("bad value for update parameter"));
1270 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1274 suc.warn_if_uninitialized = 1;
1276 run_processes_parallel(max_jobs,
1277 update_clone_get_next_task,
1278 update_clone_start_failure,
1279 update_clone_task_finished,
1283 * We saved the output and put it out all at once now.
1285 * - the listener does not have to interleave their (checkout)
1286 * work with our fetching. The writes involved in a
1287 * checkout involve more straightforward sequential I/O.
1288 * - the listener can avoid doing any work if fetching failed.
1293 for_each_string_list_item(item, &suc.projectlines)
1294 fprintf(stdout, "%s", item->string);
1299 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1301 struct strbuf sb = STRBUF_INIT;
1303 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1305 printf("%s", relative_path(argv[1], argv[2], &sb));
1306 strbuf_release(&sb);
1310 static const char *remote_submodule_branch(const char *path)
1312 const struct submodule *sub;
1313 const char *branch = NULL;
1316 sub = submodule_from_path(&null_oid, path);
1320 key = xstrfmt("submodule.%s.branch", sub->name);
1321 if (repo_config_get_string_const(the_repository, key, &branch))
1322 branch = sub->branch;
1328 if (!strcmp(branch, ".")) {
1329 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1332 die(_("No such ref: %s"), "HEAD");
1335 if (!strcmp(refname, "HEAD"))
1336 die(_("Submodule (%s) branch configured to inherit "
1337 "branch from superproject, but the superproject "
1338 "is not on any branch"), sub->name);
1340 if (!skip_prefix(refname, "refs/heads/", &refname))
1341 die(_("Expecting a full ref name, got %s"), refname);
1348 static int resolve_remote_submodule_branch(int argc, const char **argv,
1352 struct strbuf sb = STRBUF_INIT;
1354 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1356 ret = remote_submodule_branch(argv[1]);
1358 die("submodule %s doesn't exist", argv[1]);
1361 strbuf_release(&sb);
1365 static int push_check(int argc, const char **argv, const char *prefix)
1367 struct remote *remote;
1368 const char *superproject_head;
1370 int detached_head = 0;
1371 struct object_id head_oid;
1374 die("submodule--helper push-check requires at least 2 arguments");
1377 * superproject's resolved head ref.
1378 * if HEAD then the superproject is in a detached head state, otherwise
1379 * it will be the resolved head ref.
1381 superproject_head = argv[1];
1384 /* Get the submodule's head ref and determine if it is detached */
1385 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1387 die(_("Failed to resolve HEAD as a valid ref."));
1388 if (!strcmp(head, "HEAD"))
1392 * The remote must be configured.
1393 * This is to avoid pushing to the exact same URL as the parent.
1395 remote = pushremote_get(argv[1]);
1396 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1397 die("remote '%s' not configured", argv[1]);
1399 /* Check the refspec */
1401 int i, refspec_nr = argc - 2;
1402 struct ref *local_refs = get_local_heads();
1403 struct refspec *refspec = parse_push_refspec(refspec_nr,
1406 for (i = 0; i < refspec_nr; i++) {
1407 struct refspec *rs = refspec + i;
1409 if (rs->pattern || rs->matching)
1412 /* LHS must match a single ref */
1413 switch (count_refspec_match(rs->src, local_refs, NULL)) {
1418 * If LHS matches 'HEAD' then we need to ensure
1419 * that it matches the same named branch
1420 * checked out in the superproject.
1422 if (!strcmp(rs->src, "HEAD")) {
1423 if (!detached_head &&
1424 !strcmp(head, superproject_head))
1426 die("HEAD does not match the named branch in the superproject");
1430 die("src refspec '%s' must name a ref",
1434 free_refspec(refspec_nr, refspec);
1441 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1444 struct pathspec pathspec;
1445 struct module_list list = MODULE_LIST_INIT;
1446 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1448 struct option embed_gitdir_options[] = {
1449 OPT_STRING(0, "prefix", &prefix,
1451 N_("path into the working tree")),
1452 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1453 ABSORB_GITDIR_RECURSE_SUBMODULES),
1457 const char *const git_submodule_helper_usage[] = {
1458 N_("git submodule--helper embed-git-dir [<path>...]"),
1462 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1463 git_submodule_helper_usage, 0);
1465 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1468 for (i = 0; i < list.nr; i++)
1469 absorb_git_dir_into_superproject(prefix,
1470 list.entries[i]->name, flags);
1475 static int is_active(int argc, const char **argv, const char *prefix)
1478 die("submodule--helper is-active takes exactly 1 argument");
1480 return !is_submodule_active(the_repository, argv[1]);
1483 #define SUPPORT_SUPER_PREFIX (1<<0)
1487 int (*fn)(int, const char **, const char *);
1491 static struct cmd_struct commands[] = {
1492 {"list", module_list, 0},
1493 {"name", module_name, 0},
1494 {"clone", module_clone, 0},
1495 {"update-clone", update_clone, 0},
1496 {"relative-path", resolve_relative_path, 0},
1497 {"resolve-relative-url", resolve_relative_url, 0},
1498 {"resolve-relative-url-test", resolve_relative_url_test, 0},
1499 {"init", module_init, SUPPORT_SUPER_PREFIX},
1500 {"status", module_status, SUPPORT_SUPER_PREFIX},
1501 {"remote-branch", resolve_remote_submodule_branch, 0},
1502 {"push-check", push_check, 0},
1503 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1504 {"is-active", is_active, 0},
1507 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
1510 if (argc < 2 || !strcmp(argv[1], "-h"))
1511 usage("git submodule--helper <command>");
1513 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1514 if (!strcmp(argv[1], commands[i].cmd)) {
1515 if (get_super_prefix() &&
1516 !(commands[i].option & SUPPORT_SUPER_PREFIX))
1517 die(_("%s doesn't support --super-prefix"),
1519 return commands[i].fn(argc - 1, argv + 1, prefix);
1523 die(_("'%s' is not a valid submodule--helper "
1524 "subcommand"), argv[1]);