1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
3 #include "repository.h"
6 #include "parse-options.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
21 #include "object-store.h"
23 #define OPT_QUIET (1 << 0)
24 #define OPT_CACHED (1 << 1)
25 #define OPT_RECURSIVE (1 << 2)
26 #define OPT_FORCE (1 << 3)
28 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
31 static char *get_default_remote(void)
33 char *dest = NULL, *ret;
34 struct strbuf sb = STRBUF_INIT;
35 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
38 die(_("No such ref: %s"), "HEAD");
41 if (!strcmp(refname, "HEAD"))
42 return xstrdup("origin");
44 if (!skip_prefix(refname, "refs/heads/", &refname))
45 die(_("Expecting a full ref name, got %s"), refname);
47 strbuf_addf(&sb, "branch.%s.remote", refname);
48 if (git_config_get_string(sb.buf, &dest))
49 ret = xstrdup("origin");
57 static int print_default_remote(int argc, const char **argv, const char *prefix)
62 die(_("submodule--helper print-default-remote takes no arguments"));
64 remote = get_default_remote();
66 printf("%s\n", remote);
72 static int starts_with_dot_slash(const char *str)
74 return str[0] == '.' && is_dir_sep(str[1]);
77 static int starts_with_dot_dot_slash(const char *str)
79 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
83 * Returns 1 if it was the last chop before ':'.
85 static int chop_last_dir(char **remoteurl, int is_relative)
87 char *rfind = find_last_dir_sep(*remoteurl);
93 rfind = strrchr(*remoteurl, ':');
99 if (is_relative || !strcmp(".", *remoteurl))
100 die(_("cannot strip one component off url '%s'"),
104 *remoteurl = xstrdup(".");
109 * The `url` argument is the URL that navigates to the submodule origin
110 * repo. When relative, this URL is relative to the superproject origin
111 * URL repo. The `up_path` argument, if specified, is the relative
112 * path that navigates from the submodule working tree to the superproject
113 * working tree. Returns the origin URL of the submodule.
115 * Return either an absolute URL or filesystem path (if the superproject
116 * origin URL is an absolute URL or filesystem path, respectively) or a
117 * relative file system path (if the superproject origin URL is a relative
120 * When the output is a relative file system path, the path is either
121 * relative to the submodule working tree, if up_path is specified, or to
122 * the superproject working tree otherwise.
124 * NEEDSWORK: This works incorrectly on the domain and protocol part.
125 * remote_url url outcome expectation
126 * http://a.com/b ../c http://a.com/c as is
127 * http://a.com/b/ ../c http://a.com/c same as previous line, but
128 * ignore trailing slash in url
129 * http://a.com/b ../../c http://c error out
130 * http://a.com/b ../../../c http:/c error out
131 * http://a.com/b ../../../../c http:c error out
132 * http://a.com/b ../../../../../c .:c error out
133 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
134 * when a local part has a colon in its path component, too.
136 static char *relative_url(const char *remote_url,
143 char *remoteurl = xstrdup(remote_url);
144 struct strbuf sb = STRBUF_INIT;
145 size_t len = strlen(remoteurl);
147 if (is_dir_sep(remoteurl[len-1]))
148 remoteurl[len-1] = '\0';
150 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
155 * Prepend a './' to ensure all relative
156 * remoteurls start with './' or '../'
158 if (!starts_with_dot_slash(remoteurl) &&
159 !starts_with_dot_dot_slash(remoteurl)) {
161 strbuf_addf(&sb, "./%s", remoteurl);
163 remoteurl = strbuf_detach(&sb, NULL);
167 * When the url starts with '../', remove that and the
168 * last directory in remoteurl.
171 if (starts_with_dot_dot_slash(url)) {
173 colonsep |= chop_last_dir(&remoteurl, is_relative);
174 } else if (starts_with_dot_slash(url))
180 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
181 if (ends_with(url, "/"))
182 strbuf_setlen(&sb, sb.len - 1);
185 if (starts_with_dot_slash(sb.buf))
186 out = xstrdup(sb.buf + 2);
188 out = xstrdup(sb.buf);
191 if (!up_path || !is_relative)
194 strbuf_addf(&sb, "%s%s", up_path, out);
196 return strbuf_detach(&sb, NULL);
199 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
201 char *remoteurl = NULL;
202 char *remote = get_default_remote();
203 const char *up_path = NULL;
206 struct strbuf sb = STRBUF_INIT;
208 if (argc != 2 && argc != 3)
209 die("resolve-relative-url only accepts one or two arguments");
212 strbuf_addf(&sb, "remote.%s.url", remote);
215 if (git_config_get_string(sb.buf, &remoteurl))
216 /* the repository is its own authoritative upstream */
217 remoteurl = xgetcwd();
222 res = relative_url(remoteurl, url, up_path);
229 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
231 char *remoteurl, *res;
232 const char *up_path, *url;
235 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
238 remoteurl = xstrdup(argv[2]);
241 if (!strcmp(up_path, "(null)"))
244 res = relative_url(remoteurl, url, up_path);
251 /* the result should be freed by the caller. */
252 static char *get_submodule_displaypath(const char *path, const char *prefix)
254 const char *super_prefix = get_super_prefix();
256 if (prefix && super_prefix) {
257 BUG("cannot have prefix '%s' and superprefix '%s'",
258 prefix, super_prefix);
260 struct strbuf sb = STRBUF_INIT;
261 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
264 } else if (super_prefix) {
265 return xstrfmt("%s%s", super_prefix, path);
267 return xstrdup(path);
271 static char *compute_rev_name(const char *sub_path, const char* object_id)
273 struct strbuf sb = STRBUF_INIT;
276 static const char *describe_bare[] = { NULL };
278 static const char *describe_tags[] = { "--tags", NULL };
280 static const char *describe_contains[] = { "--contains", NULL };
282 static const char *describe_all_always[] = { "--all", "--always", NULL };
284 static const char **describe_argv[] = { describe_bare, describe_tags,
286 describe_all_always, NULL };
288 for (d = describe_argv; *d; d++) {
289 struct child_process cp = CHILD_PROCESS_INIT;
290 prepare_submodule_repo_env(&cp.env_array);
295 argv_array_push(&cp.args, "describe");
296 argv_array_pushv(&cp.args, *d);
297 argv_array_push(&cp.args, object_id);
299 if (!capture_command(&cp, &sb, 0)) {
300 strbuf_strip_suffix(&sb, "\n");
301 return strbuf_detach(&sb, NULL);
310 const struct cache_entry **entries;
313 #define MODULE_LIST_INIT { NULL, 0, 0 }
315 static int module_list_compute(int argc, const char **argv,
317 struct pathspec *pathspec,
318 struct module_list *list)
321 char *ps_matched = NULL;
322 parse_pathspec(pathspec, 0,
323 PATHSPEC_PREFER_FULL,
327 ps_matched = xcalloc(pathspec->nr, 1);
329 if (read_cache() < 0)
330 die(_("index file corrupt"));
332 for (i = 0; i < active_nr; i++) {
333 const struct cache_entry *ce = active_cache[i];
335 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
337 !S_ISGITLINK(ce->ce_mode))
340 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
341 list->entries[list->nr++] = ce;
342 while (i + 1 < active_nr &&
343 !strcmp(ce->name, active_cache[i + 1]->name))
345 * Skip entries with the same name in different stages
346 * to make sure an entry is returned only once.
351 if (ps_matched && report_path_error(ps_matched, pathspec))
359 static void module_list_active(struct module_list *list)
362 struct module_list active_modules = MODULE_LIST_INIT;
364 for (i = 0; i < list->nr; i++) {
365 const struct cache_entry *ce = list->entries[i];
367 if (!is_submodule_active(the_repository, ce->name))
370 ALLOC_GROW(active_modules.entries,
371 active_modules.nr + 1,
372 active_modules.alloc);
373 active_modules.entries[active_modules.nr++] = ce;
377 *list = active_modules;
380 static char *get_up_path(const char *path)
383 struct strbuf sb = STRBUF_INIT;
385 for (i = count_slashes(path); i; i--)
386 strbuf_addstr(&sb, "../");
389 * Check if 'path' ends with slash or not
390 * for having the same output for dir/sub_dir
393 if (!is_dir_sep(path[strlen(path) - 1]))
394 strbuf_addstr(&sb, "../");
396 return strbuf_detach(&sb, NULL);
399 static int module_list(int argc, const char **argv, const char *prefix)
402 struct pathspec pathspec;
403 struct module_list list = MODULE_LIST_INIT;
405 struct option module_list_options[] = {
406 OPT_STRING(0, "prefix", &prefix,
408 N_("alternative anchor for relative paths")),
412 const char *const git_submodule_helper_usage[] = {
413 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
417 argc = parse_options(argc, argv, prefix, module_list_options,
418 git_submodule_helper_usage, 0);
420 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
423 for (i = 0; i < list.nr; i++) {
424 const struct cache_entry *ce = list.entries[i];
427 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
429 printf("%06o %s %d\t", ce->ce_mode,
430 oid_to_hex(&ce->oid), ce_stage(ce));
432 fprintf(stdout, "%s\n", ce->name);
437 static void for_each_listed_submodule(const struct module_list *list,
438 each_submodule_fn fn, void *cb_data)
441 for (i = 0; i < list->nr; i++)
442 fn(list->entries[i], cb_data);
452 #define CB_FOREACH_INIT { 0 }
454 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
457 struct cb_foreach *info = cb_data;
458 const char *path = list_item->name;
459 const struct object_id *ce_oid = &list_item->oid;
461 const struct submodule *sub;
462 struct child_process cp = CHILD_PROCESS_INIT;
465 displaypath = get_submodule_displaypath(path, info->prefix);
467 sub = submodule_from_path(the_repository, &null_oid, path);
470 die(_("No url found for submodule path '%s' in .gitmodules"),
473 if (!is_submodule_populated_gently(path, NULL))
476 prepare_submodule_repo_env(&cp.env_array);
479 * For the purpose of executing <command> in the submodule,
480 * separate shell is used for the purpose of running the
487 * NEEDSWORK: the command currently has access to the variables $name,
488 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
489 * contains a single argument. This is done for maintaining a faithful
490 * translation from shell script.
492 if (info->argc == 1) {
493 char *toplevel = xgetcwd();
494 struct strbuf sb = STRBUF_INIT;
496 argv_array_pushf(&cp.env_array, "name=%s", sub->name);
497 argv_array_pushf(&cp.env_array, "sm_path=%s", path);
498 argv_array_pushf(&cp.env_array, "displaypath=%s", displaypath);
499 argv_array_pushf(&cp.env_array, "sha1=%s",
501 argv_array_pushf(&cp.env_array, "toplevel=%s", toplevel);
504 * Since the path variable was accessible from the script
505 * before porting, it is also made available after porting.
506 * The environment variable "PATH" has a very special purpose
507 * on windows. And since environment variables are
508 * case-insensitive in windows, it interferes with the
509 * existing PATH variable. Hence, to avoid that, we expose
510 * path via the args argv_array and not via env_array.
512 sq_quote_buf(&sb, path);
513 argv_array_pushf(&cp.args, "path=%s; %s",
514 sb.buf, info->argv[0]);
518 argv_array_pushv(&cp.args, info->argv);
522 printf(_("Entering '%s'\n"), displaypath);
524 if (info->argv[0] && run_command(&cp))
525 die(_("run_command returned non-zero status for %s\n."),
528 if (info->recursive) {
529 struct child_process cpr = CHILD_PROCESS_INIT;
533 prepare_submodule_repo_env(&cpr.env_array);
535 argv_array_pushl(&cpr.args, "--super-prefix", NULL);
536 argv_array_pushf(&cpr.args, "%s/", displaypath);
537 argv_array_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
541 argv_array_push(&cpr.args, "--quiet");
543 argv_array_push(&cpr.args, "--");
544 argv_array_pushv(&cpr.args, info->argv);
546 if (run_command(&cpr))
547 die(_("run_command returned non-zero status while "
548 "recursing in the nested submodules of %s\n."),
556 static int module_foreach(int argc, const char **argv, const char *prefix)
558 struct cb_foreach info = CB_FOREACH_INIT;
559 struct pathspec pathspec;
560 struct module_list list = MODULE_LIST_INIT;
562 struct option module_foreach_options[] = {
563 OPT__QUIET(&info.quiet, N_("Suppress output of entering each submodule command")),
564 OPT_BOOL(0, "recursive", &info.recursive,
565 N_("Recurse into nested submodules")),
569 const char *const git_submodule_helper_usage[] = {
570 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
574 argc = parse_options(argc, argv, prefix, module_foreach_options,
575 git_submodule_helper_usage, 0);
577 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
582 info.prefix = prefix;
584 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
589 static char *compute_submodule_clone_url(const char *rel_url)
591 char *remoteurl, *relurl;
592 char *remote = get_default_remote();
593 struct strbuf remotesb = STRBUF_INIT;
595 strbuf_addf(&remotesb, "remote.%s.url", remote);
596 if (git_config_get_string(remotesb.buf, &remoteurl)) {
597 warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
598 remoteurl = xgetcwd();
600 relurl = relative_url(remoteurl, rel_url, NULL);
604 strbuf_release(&remotesb);
614 #define INIT_CB_INIT { NULL, 0 }
616 static void init_submodule(const char *path, const char *prefix,
619 const struct submodule *sub;
620 struct strbuf sb = STRBUF_INIT;
621 char *upd = NULL, *url = NULL, *displaypath;
623 displaypath = get_submodule_displaypath(path, prefix);
625 sub = submodule_from_path(the_repository, &null_oid, path);
628 die(_("No url found for submodule path '%s' in .gitmodules"),
632 * NEEDSWORK: In a multi-working-tree world, this needs to be
633 * set in the per-worktree config.
635 * Set active flag for the submodule being initialized
637 if (!is_submodule_active(the_repository, path)) {
638 strbuf_addf(&sb, "submodule.%s.active", sub->name);
639 git_config_set_gently(sb.buf, "true");
644 * Copy url setting when it is not set yet.
645 * To look up the url in .git/config, we must not fall back to
646 * .gitmodules, so look it up directly.
648 strbuf_addf(&sb, "submodule.%s.url", sub->name);
649 if (git_config_get_string(sb.buf, &url)) {
651 die(_("No url found for submodule path '%s' in .gitmodules"),
654 url = xstrdup(sub->url);
656 /* Possibly a url relative to parent */
657 if (starts_with_dot_dot_slash(url) ||
658 starts_with_dot_slash(url)) {
660 url = compute_submodule_clone_url(oldurl);
664 if (git_config_set_gently(sb.buf, url))
665 die(_("Failed to register url for submodule path '%s'"),
667 if (!(flags & OPT_QUIET))
669 _("Submodule '%s' (%s) registered for path '%s'\n"),
670 sub->name, url, displaypath);
674 /* Copy "update" setting when it is not set yet */
675 strbuf_addf(&sb, "submodule.%s.update", sub->name);
676 if (git_config_get_string(sb.buf, &upd) &&
677 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
678 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
679 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
681 upd = xstrdup("none");
683 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
685 if (git_config_set_gently(sb.buf, upd))
686 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
694 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
696 struct init_cb *info = cb_data;
697 init_submodule(list_item->name, info->prefix, info->flags);
700 static int module_init(int argc, const char **argv, const char *prefix)
702 struct init_cb info = INIT_CB_INIT;
703 struct pathspec pathspec;
704 struct module_list list = MODULE_LIST_INIT;
707 struct option module_init_options[] = {
708 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
712 const char *const git_submodule_helper_usage[] = {
713 N_("git submodule--helper init [<options>] [<path>]"),
717 argc = parse_options(argc, argv, prefix, module_init_options,
718 git_submodule_helper_usage, 0);
720 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
724 * If there are no path args and submodule.active is set then,
725 * by default, only initialize 'active' modules.
727 if (!argc && git_config_get_value_multi("submodule.active"))
728 module_list_active(&list);
730 info.prefix = prefix;
732 info.flags |= OPT_QUIET;
734 for_each_listed_submodule(&list, init_submodule_cb, &info);
744 #define STATUS_CB_INIT { NULL, 0 }
746 static void print_status(unsigned int flags, char state, const char *path,
747 const struct object_id *oid, const char *displaypath)
749 if (flags & OPT_QUIET)
752 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
754 if (state == ' ' || state == '+') {
755 const char *name = compute_rev_name(path, oid_to_hex(oid));
758 printf(" (%s)", name);
764 static int handle_submodule_head_ref(const char *refname,
765 const struct object_id *oid, int flags,
768 struct object_id *output = cb_data;
775 static void status_submodule(const char *path, const struct object_id *ce_oid,
776 unsigned int ce_flags, const char *prefix,
780 struct argv_array diff_files_args = ARGV_ARRAY_INIT;
782 int diff_files_result;
784 if (!submodule_from_path(the_repository, &null_oid, path))
785 die(_("no submodule mapping found in .gitmodules for path '%s'"),
788 displaypath = get_submodule_displaypath(path, prefix);
790 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
791 print_status(flags, 'U', path, &null_oid, displaypath);
795 if (!is_submodule_active(the_repository, path)) {
796 print_status(flags, '-', path, ce_oid, displaypath);
800 argv_array_pushl(&diff_files_args, "diff-files",
801 "--ignore-submodules=dirty", "--quiet", "--",
804 git_config(git_diff_basic_config, NULL);
805 repo_init_revisions(the_repository, &rev, prefix);
807 diff_files_args.argc = setup_revisions(diff_files_args.argc,
808 diff_files_args.argv,
810 diff_files_result = run_diff_files(&rev, 0);
812 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
813 print_status(flags, ' ', path, ce_oid,
815 } else if (!(flags & OPT_CACHED)) {
816 struct object_id oid;
817 struct ref_store *refs = get_submodule_ref_store(path);
820 print_status(flags, '-', path, ce_oid, displaypath);
823 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
824 die(_("could not resolve HEAD ref inside the "
825 "submodule '%s'"), path);
827 print_status(flags, '+', path, &oid, displaypath);
829 print_status(flags, '+', path, ce_oid, displaypath);
832 if (flags & OPT_RECURSIVE) {
833 struct child_process cpr = CHILD_PROCESS_INIT;
837 prepare_submodule_repo_env(&cpr.env_array);
839 argv_array_push(&cpr.args, "--super-prefix");
840 argv_array_pushf(&cpr.args, "%s/", displaypath);
841 argv_array_pushl(&cpr.args, "submodule--helper", "status",
842 "--recursive", NULL);
844 if (flags & OPT_CACHED)
845 argv_array_push(&cpr.args, "--cached");
847 if (flags & OPT_QUIET)
848 argv_array_push(&cpr.args, "--quiet");
850 if (run_command(&cpr))
851 die(_("failed to recurse into submodule '%s'"), path);
855 argv_array_clear(&diff_files_args);
859 static void status_submodule_cb(const struct cache_entry *list_item,
862 struct status_cb *info = cb_data;
863 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
864 info->prefix, info->flags);
867 static int module_status(int argc, const char **argv, const char *prefix)
869 struct status_cb info = STATUS_CB_INIT;
870 struct pathspec pathspec;
871 struct module_list list = MODULE_LIST_INIT;
874 struct option module_status_options[] = {
875 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
876 OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
877 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
881 const char *const git_submodule_helper_usage[] = {
882 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
886 argc = parse_options(argc, argv, prefix, module_status_options,
887 git_submodule_helper_usage, 0);
889 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
892 info.prefix = prefix;
894 info.flags |= OPT_QUIET;
896 for_each_listed_submodule(&list, status_submodule_cb, &info);
901 static int module_name(int argc, const char **argv, const char *prefix)
903 const struct submodule *sub;
906 usage(_("git submodule--helper name <path>"));
908 sub = submodule_from_path(the_repository, &null_oid, argv[1]);
911 die(_("no submodule mapping found in .gitmodules for path '%s'"),
914 printf("%s\n", sub->name);
924 #define SYNC_CB_INIT { NULL, 0 }
926 static void sync_submodule(const char *path, const char *prefix,
929 const struct submodule *sub;
930 char *remote_key = NULL;
931 char *sub_origin_url, *super_config_url, *displaypath;
932 struct strbuf sb = STRBUF_INIT;
933 struct child_process cp = CHILD_PROCESS_INIT;
934 char *sub_config_path = NULL;
936 if (!is_submodule_active(the_repository, path))
939 sub = submodule_from_path(the_repository, &null_oid, path);
941 if (sub && sub->url) {
942 if (starts_with_dot_dot_slash(sub->url) ||
943 starts_with_dot_slash(sub->url)) {
944 char *remote_url, *up_path;
945 char *remote = get_default_remote();
946 strbuf_addf(&sb, "remote.%s.url", remote);
948 if (git_config_get_string(sb.buf, &remote_url))
949 remote_url = xgetcwd();
951 up_path = get_up_path(path);
952 sub_origin_url = relative_url(remote_url, sub->url, up_path);
953 super_config_url = relative_url(remote_url, sub->url, NULL);
959 sub_origin_url = xstrdup(sub->url);
960 super_config_url = xstrdup(sub->url);
963 sub_origin_url = xstrdup("");
964 super_config_url = xstrdup("");
967 displaypath = get_submodule_displaypath(path, prefix);
969 if (!(flags & OPT_QUIET))
970 printf(_("Synchronizing submodule url for '%s'\n"),
974 strbuf_addf(&sb, "submodule.%s.url", sub->name);
975 if (git_config_set_gently(sb.buf, super_config_url))
976 die(_("failed to register url for submodule path '%s'"),
979 if (!is_submodule_populated_gently(path, NULL))
982 prepare_submodule_repo_env(&cp.env_array);
985 argv_array_pushl(&cp.args, "submodule--helper",
986 "print-default-remote", NULL);
989 if (capture_command(&cp, &sb, 0))
990 die(_("failed to get the default remote for submodule '%s'"),
993 strbuf_strip_suffix(&sb, "\n");
994 remote_key = xstrfmt("remote.%s.url", sb.buf);
997 submodule_to_gitdir(&sb, path);
998 strbuf_addstr(&sb, "/config");
1000 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1001 die(_("failed to update remote for submodule '%s'"),
1004 if (flags & OPT_RECURSIVE) {
1005 struct child_process cpr = CHILD_PROCESS_INIT;
1009 prepare_submodule_repo_env(&cpr.env_array);
1011 argv_array_push(&cpr.args, "--super-prefix");
1012 argv_array_pushf(&cpr.args, "%s/", displaypath);
1013 argv_array_pushl(&cpr.args, "submodule--helper", "sync",
1014 "--recursive", NULL);
1016 if (flags & OPT_QUIET)
1017 argv_array_push(&cpr.args, "--quiet");
1019 if (run_command(&cpr))
1020 die(_("failed to recurse into submodule '%s'"),
1025 free(super_config_url);
1026 free(sub_origin_url);
1027 strbuf_release(&sb);
1030 free(sub_config_path);
1033 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1035 struct sync_cb *info = cb_data;
1036 sync_submodule(list_item->name, info->prefix, info->flags);
1039 static int module_sync(int argc, const char **argv, const char *prefix)
1041 struct sync_cb info = SYNC_CB_INIT;
1042 struct pathspec pathspec;
1043 struct module_list list = MODULE_LIST_INIT;
1047 struct option module_sync_options[] = {
1048 OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
1049 OPT_BOOL(0, "recursive", &recursive,
1050 N_("Recurse into nested submodules")),
1054 const char *const git_submodule_helper_usage[] = {
1055 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1059 argc = parse_options(argc, argv, prefix, module_sync_options,
1060 git_submodule_helper_usage, 0);
1062 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1065 info.prefix = prefix;
1067 info.flags |= OPT_QUIET;
1069 info.flags |= OPT_RECURSIVE;
1071 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1080 #define DEINIT_CB_INIT { NULL, 0 }
1082 static void deinit_submodule(const char *path, const char *prefix,
1085 const struct submodule *sub;
1086 char *displaypath = NULL;
1087 struct child_process cp_config = CHILD_PROCESS_INIT;
1088 struct strbuf sb_config = STRBUF_INIT;
1089 char *sub_git_dir = xstrfmt("%s/.git", path);
1091 sub = submodule_from_path(the_repository, &null_oid, path);
1093 if (!sub || !sub->name)
1096 displaypath = get_submodule_displaypath(path, prefix);
1098 /* remove the submodule work tree (unless the user already did it) */
1099 if (is_directory(path)) {
1100 struct strbuf sb_rm = STRBUF_INIT;
1104 * protect submodules containing a .git directory
1105 * NEEDSWORK: instead of dying, automatically call
1106 * absorbgitdirs and (possibly) warn.
1108 if (is_directory(sub_git_dir))
1109 die(_("Submodule work tree '%s' contains a .git "
1110 "directory (use 'rm -rf' if you really want "
1111 "to remove it including all of its history)"),
1114 if (!(flags & OPT_FORCE)) {
1115 struct child_process cp_rm = CHILD_PROCESS_INIT;
1117 argv_array_pushl(&cp_rm.args, "rm", "-qn",
1120 if (run_command(&cp_rm))
1121 die(_("Submodule work tree '%s' contains local "
1122 "modifications; use '-f' to discard them"),
1126 strbuf_addstr(&sb_rm, path);
1128 if (!remove_dir_recursively(&sb_rm, 0))
1129 format = _("Cleared directory '%s'\n");
1131 format = _("Could not remove submodule work tree '%s'\n");
1133 if (!(flags & OPT_QUIET))
1134 printf(format, displaypath);
1136 submodule_unset_core_worktree(sub);
1138 strbuf_release(&sb_rm);
1141 if (mkdir(path, 0777))
1142 printf(_("could not create empty submodule directory %s"),
1145 cp_config.git_cmd = 1;
1146 argv_array_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1147 argv_array_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1149 /* remove the .git/config entries (unless the user already did it) */
1150 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1151 char *sub_key = xstrfmt("submodule.%s", sub->name);
1153 * remove the whole section so we have a clean state when
1154 * the user later decides to init this submodule again
1156 git_config_rename_section_in_file(NULL, sub_key, NULL);
1157 if (!(flags & OPT_QUIET))
1158 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1159 sub->name, sub->url, displaypath);
1166 strbuf_release(&sb_config);
1169 static void deinit_submodule_cb(const struct cache_entry *list_item,
1172 struct deinit_cb *info = cb_data;
1173 deinit_submodule(list_item->name, info->prefix, info->flags);
1176 static int module_deinit(int argc, const char **argv, const char *prefix)
1178 struct deinit_cb info = DEINIT_CB_INIT;
1179 struct pathspec pathspec;
1180 struct module_list list = MODULE_LIST_INIT;
1185 struct option module_deinit_options[] = {
1186 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
1187 OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
1188 OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
1192 const char *const git_submodule_helper_usage[] = {
1193 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1197 argc = parse_options(argc, argv, prefix, module_deinit_options,
1198 git_submodule_helper_usage, 0);
1201 error("pathspec and --all are incompatible");
1202 usage_with_options(git_submodule_helper_usage,
1203 module_deinit_options);
1207 die(_("Use '--all' if you really want to deinitialize all submodules"));
1209 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1212 info.prefix = prefix;
1214 info.flags |= OPT_QUIET;
1216 info.flags |= OPT_FORCE;
1218 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1223 static int clone_submodule(const char *path, const char *gitdir, const char *url,
1224 const char *depth, struct string_list *reference, int dissociate,
1225 int quiet, int progress)
1227 struct child_process cp = CHILD_PROCESS_INIT;
1229 argv_array_push(&cp.args, "clone");
1230 argv_array_push(&cp.args, "--no-checkout");
1232 argv_array_push(&cp.args, "--quiet");
1234 argv_array_push(&cp.args, "--progress");
1235 if (depth && *depth)
1236 argv_array_pushl(&cp.args, "--depth", depth, NULL);
1237 if (reference->nr) {
1238 struct string_list_item *item;
1239 for_each_string_list_item(item, reference)
1240 argv_array_pushl(&cp.args, "--reference",
1241 item->string, NULL);
1244 argv_array_push(&cp.args, "--dissociate");
1245 if (gitdir && *gitdir)
1246 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1248 argv_array_push(&cp.args, "--");
1249 argv_array_push(&cp.args, url);
1250 argv_array_push(&cp.args, path);
1253 prepare_submodule_repo_env(&cp.env_array);
1256 return run_command(&cp);
1259 struct submodule_alternate_setup {
1260 const char *submodule_name;
1261 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1262 SUBMODULE_ALTERNATE_ERROR_DIE,
1263 SUBMODULE_ALTERNATE_ERROR_INFO,
1264 SUBMODULE_ALTERNATE_ERROR_IGNORE
1266 struct string_list *reference;
1268 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1269 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1271 static int add_possible_reference_from_superproject(
1272 struct object_directory *odb, void *sas_cb)
1274 struct submodule_alternate_setup *sas = sas_cb;
1278 * If the alternate object store is another repository, try the
1279 * standard layout with .git/(modules/<name>)+/objects
1281 if (strip_suffix(odb->path, "/objects", &len)) {
1283 struct strbuf sb = STRBUF_INIT;
1284 struct strbuf err = STRBUF_INIT;
1285 strbuf_add(&sb, odb->path, len);
1288 * We need to end the new path with '/' to mark it as a dir,
1289 * otherwise a submodule name containing '/' will be broken
1290 * as the last part of a missing submodule reference would
1291 * be taken as a file name.
1293 strbuf_addf(&sb, "/modules/%s/", sas->submodule_name);
1295 sm_alternate = compute_alternate_path(sb.buf, &err);
1297 string_list_append(sas->reference, xstrdup(sb.buf));
1300 switch (sas->error_mode) {
1301 case SUBMODULE_ALTERNATE_ERROR_DIE:
1302 die(_("submodule '%s' cannot add alternate: %s"),
1303 sas->submodule_name, err.buf);
1304 case SUBMODULE_ALTERNATE_ERROR_INFO:
1305 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1306 sas->submodule_name, err.buf);
1307 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1311 strbuf_release(&sb);
1317 static void prepare_possible_alternates(const char *sm_name,
1318 struct string_list *reference)
1320 char *sm_alternate = NULL, *error_strategy = NULL;
1321 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1323 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1327 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1329 if (!error_strategy)
1330 error_strategy = xstrdup("die");
1332 sas.submodule_name = sm_name;
1333 sas.reference = reference;
1334 if (!strcmp(error_strategy, "die"))
1335 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1336 else if (!strcmp(error_strategy, "info"))
1337 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1338 else if (!strcmp(error_strategy, "ignore"))
1339 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1341 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1343 if (!strcmp(sm_alternate, "superproject"))
1344 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1345 else if (!strcmp(sm_alternate, "no"))
1348 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1351 free(error_strategy);
1354 static int module_clone(int argc, const char **argv, const char *prefix)
1356 const char *name = NULL, *url = NULL, *depth = NULL;
1359 char *p, *path = NULL, *sm_gitdir;
1360 struct strbuf sb = STRBUF_INIT;
1361 struct string_list reference = STRING_LIST_INIT_NODUP;
1363 char *sm_alternate = NULL, *error_strategy = NULL;
1365 struct option module_clone_options[] = {
1366 OPT_STRING(0, "prefix", &prefix,
1368 N_("alternative anchor for relative paths")),
1369 OPT_STRING(0, "path", &path,
1371 N_("where the new submodule will be cloned to")),
1372 OPT_STRING(0, "name", &name,
1374 N_("name of the new submodule")),
1375 OPT_STRING(0, "url", &url,
1377 N_("url where to clone the submodule from")),
1378 OPT_STRING_LIST(0, "reference", &reference,
1380 N_("reference repository")),
1381 OPT_BOOL(0, "dissociate", &dissociate,
1382 N_("use --reference only while cloning")),
1383 OPT_STRING(0, "depth", &depth,
1385 N_("depth for shallow clones")),
1386 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
1387 OPT_BOOL(0, "progress", &progress,
1388 N_("force cloning progress")),
1392 const char *const git_submodule_helper_usage[] = {
1393 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1394 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1395 "--url <url> --path <path>"),
1399 argc = parse_options(argc, argv, prefix, module_clone_options,
1400 git_submodule_helper_usage, 0);
1402 if (argc || !url || !path || !*path)
1403 usage_with_options(git_submodule_helper_usage,
1404 module_clone_options);
1406 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
1407 sm_gitdir = absolute_pathdup(sb.buf);
1410 if (!is_absolute_path(path)) {
1411 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
1412 path = strbuf_detach(&sb, NULL);
1414 path = xstrdup(path);
1416 if (!file_exists(sm_gitdir)) {
1417 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1418 die(_("could not create directory '%s'"), sm_gitdir);
1420 prepare_possible_alternates(name, &reference);
1422 if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
1424 die(_("clone of '%s' into submodule path '%s' failed"),
1427 if (safe_create_leading_directories_const(path) < 0)
1428 die(_("could not create directory '%s'"), path);
1429 strbuf_addf(&sb, "%s/index", sm_gitdir);
1430 unlink_or_warn(sb.buf);
1434 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
1436 p = git_pathdup_submodule(path, "config");
1438 die(_("could not get submodule directory for '%s'"), path);
1440 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1441 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1443 git_config_set_in_file(p, "submodule.alternateLocation",
1445 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1447 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1451 free(error_strategy);
1453 strbuf_release(&sb);
1460 static void determine_submodule_update_strategy(struct repository *r,
1464 struct submodule_update_strategy *out)
1466 const struct submodule *sub = submodule_from_path(r, &null_oid, path);
1470 key = xstrfmt("submodule.%s.update", sub->name);
1473 if (parse_submodule_update_strategy(update, out) < 0)
1474 die(_("Invalid update mode '%s' for submodule path '%s'"),
1476 } else if (!repo_config_get_string_const(r, key, &val)) {
1477 if (parse_submodule_update_strategy(val, out) < 0)
1478 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1480 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1481 out->type = sub->update_strategy.type;
1482 out->command = sub->update_strategy.command;
1484 out->type = SM_UPDATE_CHECKOUT;
1487 (out->type == SM_UPDATE_MERGE ||
1488 out->type == SM_UPDATE_REBASE ||
1489 out->type == SM_UPDATE_NONE))
1490 out->type = SM_UPDATE_CHECKOUT;
1495 static int module_update_module_mode(int argc, const char **argv, const char *prefix)
1497 const char *path, *update = NULL;
1499 struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
1501 if (argc < 3 || argc > 4)
1502 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1504 just_cloned = git_config_int("just_cloned", argv[1]);
1510 determine_submodule_update_strategy(the_repository,
1511 just_cloned, path, update,
1513 fputs(submodule_strategy_to_string(&update_strategy), stdout);
1518 struct update_clone_data {
1519 const struct submodule *sub;
1520 struct object_id oid;
1521 unsigned just_cloned;
1524 struct submodule_update_clone {
1525 /* index into 'list', the list of submodules to look into for cloning */
1527 struct module_list list;
1528 unsigned warn_if_uninitialized : 1;
1530 /* update parameter passed via commandline */
1531 struct submodule_update_strategy update;
1533 /* configuration parameters which are passed on to the children */
1536 int recommend_shallow;
1537 struct string_list references;
1540 const char *recursive_prefix;
1543 /* to be consumed by git-submodule.sh */
1544 struct update_clone_data *update_clone;
1545 int update_clone_nr; int update_clone_alloc;
1547 /* If we want to stop as fast as possible and return an error */
1548 unsigned quickstop : 1;
1550 /* failed clones to be retried again */
1551 const struct cache_entry **failed_clones;
1552 int failed_clones_nr, failed_clones_alloc;
1556 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1557 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
1559 NULL, 0, 0, 0, NULL, 0, 0, 1}
1562 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1563 struct strbuf *out, const char *displaypath)
1566 * Only mention uninitialized submodules when their
1567 * paths have been specified.
1569 if (suc->warn_if_uninitialized) {
1571 _("Submodule path '%s' not initialized"),
1573 strbuf_addch(out, '\n');
1575 _("Maybe you want to use 'update --init'?"));
1576 strbuf_addch(out, '\n');
1581 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1582 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1584 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1585 struct child_process *child,
1586 struct submodule_update_clone *suc,
1589 const struct submodule *sub = NULL;
1590 const char *url = NULL;
1591 const char *update_string;
1592 enum submodule_update_type update_type;
1594 struct strbuf displaypath_sb = STRBUF_INIT;
1595 struct strbuf sb = STRBUF_INIT;
1596 const char *displaypath = NULL;
1597 int needs_cloning = 0;
1598 int need_free_url = 0;
1601 if (suc->recursive_prefix)
1602 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1604 strbuf_addstr(&sb, ce->name);
1605 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1606 strbuf_addch(out, '\n');
1610 sub = submodule_from_path(the_repository, &null_oid, ce->name);
1612 if (suc->recursive_prefix)
1613 displaypath = relative_path(suc->recursive_prefix,
1614 ce->name, &displaypath_sb);
1616 displaypath = ce->name;
1619 next_submodule_warn_missing(suc, out, displaypath);
1623 key = xstrfmt("submodule.%s.update", sub->name);
1624 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1625 update_type = parse_submodule_update_type(update_string);
1627 update_type = sub->update_strategy.type;
1631 if (suc->update.type == SM_UPDATE_NONE
1632 || (suc->update.type == SM_UPDATE_UNSPECIFIED
1633 && update_type == SM_UPDATE_NONE)) {
1634 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1635 strbuf_addch(out, '\n');
1639 /* Check if the submodule has been initialized. */
1640 if (!is_submodule_active(the_repository, ce->name)) {
1641 next_submodule_warn_missing(suc, out, displaypath);
1646 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1647 if (repo_config_get_string_const(the_repository, sb.buf, &url)) {
1648 if (starts_with_dot_slash(sub->url) ||
1649 starts_with_dot_dot_slash(sub->url)) {
1650 url = compute_submodule_clone_url(sub->url);
1657 strbuf_addf(&sb, "%s/.git", ce->name);
1658 needs_cloning = !file_exists(sb.buf);
1660 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
1661 suc->update_clone_alloc);
1662 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
1663 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
1664 suc->update_clone[suc->update_clone_nr].sub = sub;
1665 suc->update_clone_nr++;
1671 child->no_stdin = 1;
1672 child->stdout_to_stderr = 1;
1674 argv_array_push(&child->args, "submodule--helper");
1675 argv_array_push(&child->args, "clone");
1677 argv_array_push(&child->args, "--progress");
1679 argv_array_push(&child->args, "--quiet");
1681 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1682 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1683 argv_array_push(&child->args, "--depth=1");
1684 argv_array_pushl(&child->args, "--path", sub->path, NULL);
1685 argv_array_pushl(&child->args, "--name", sub->name, NULL);
1686 argv_array_pushl(&child->args, "--url", url, NULL);
1687 if (suc->references.nr) {
1688 struct string_list_item *item;
1689 for_each_string_list_item(item, &suc->references)
1690 argv_array_pushl(&child->args, "--reference", item->string, NULL);
1692 if (suc->dissociate)
1693 argv_array_push(&child->args, "--dissociate");
1695 argv_array_push(&child->args, suc->depth);
1698 strbuf_reset(&displaypath_sb);
1703 return needs_cloning;
1706 static int update_clone_get_next_task(struct child_process *child,
1711 struct submodule_update_clone *suc = suc_cb;
1712 const struct cache_entry *ce;
1715 for (; suc->current < suc->list.nr; suc->current++) {
1716 ce = suc->list.entries[suc->current];
1717 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1718 int *p = xmalloc(sizeof(*p));
1727 * The loop above tried cloning each submodule once, now try the
1728 * stragglers again, which we can imagine as an extension of the
1731 index = suc->current - suc->list.nr;
1732 if (index < suc->failed_clones_nr) {
1734 ce = suc->failed_clones[index];
1735 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1737 strbuf_addstr(err, "BUG: submodule considered for "
1738 "cloning, doesn't need cloning "
1742 p = xmalloc(sizeof(*p));
1752 static int update_clone_start_failure(struct strbuf *err,
1756 struct submodule_update_clone *suc = suc_cb;
1761 static int update_clone_task_finished(int result,
1766 const struct cache_entry *ce;
1767 struct submodule_update_clone *suc = suc_cb;
1769 int *idxP = idx_task_cb;
1776 if (idx < suc->list.nr) {
1777 ce = suc->list.entries[idx];
1778 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1780 strbuf_addch(err, '\n');
1781 ALLOC_GROW(suc->failed_clones,
1782 suc->failed_clones_nr + 1,
1783 suc->failed_clones_alloc);
1784 suc->failed_clones[suc->failed_clones_nr++] = ce;
1787 idx -= suc->list.nr;
1788 ce = suc->failed_clones[idx];
1789 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1791 strbuf_addch(err, '\n');
1799 static int git_update_clone_config(const char *var, const char *value,
1803 if (!strcmp(var, "submodule.fetchjobs"))
1804 *max_jobs = parse_submodule_fetchjobs(var, value);
1808 static void update_submodule(struct update_clone_data *ucd)
1810 fprintf(stdout, "dummy %s %d\t%s\n",
1811 oid_to_hex(&ucd->oid),
1816 static int update_submodules(struct submodule_update_clone *suc)
1820 run_processes_parallel_tr2(suc->max_jobs, update_clone_get_next_task,
1821 update_clone_start_failure,
1822 update_clone_task_finished, suc, "submodule",
1826 * We saved the output and put it out all at once now.
1828 * - the listener does not have to interleave their (checkout)
1829 * work with our fetching. The writes involved in a
1830 * checkout involve more straightforward sequential I/O.
1831 * - the listener can avoid doing any work if fetching failed.
1836 for (i = 0; i < suc->update_clone_nr; i++)
1837 update_submodule(&suc->update_clone[i]);
1842 static int update_clone(int argc, const char **argv, const char *prefix)
1844 const char *update = NULL;
1845 struct pathspec pathspec;
1846 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1848 struct option module_update_clone_options[] = {
1849 OPT_STRING(0, "prefix", &prefix,
1851 N_("path into the working tree")),
1852 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1854 N_("path into the working tree, across nested "
1855 "submodule boundaries")),
1856 OPT_STRING(0, "update", &update,
1858 N_("rebase, merge, checkout or none")),
1859 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1860 N_("reference repository")),
1861 OPT_BOOL(0, "dissociate", &suc.dissociate,
1862 N_("use --reference only while cloning")),
1863 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1864 N_("Create a shallow clone truncated to the "
1865 "specified number of revisions")),
1866 OPT_INTEGER('j', "jobs", &suc.max_jobs,
1867 N_("parallel jobs")),
1868 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1869 N_("whether the initial clone should follow the shallow recommendation")),
1870 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1871 OPT_BOOL(0, "progress", &suc.progress,
1872 N_("force cloning progress")),
1876 const char *const git_submodule_helper_usage[] = {
1877 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1880 suc.prefix = prefix;
1882 update_clone_config_from_gitmodules(&suc.max_jobs);
1883 git_config(git_update_clone_config, &suc.max_jobs);
1885 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1886 git_submodule_helper_usage, 0);
1889 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1890 die(_("bad value for update parameter"));
1892 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1896 suc.warn_if_uninitialized = 1;
1898 return update_submodules(&suc);
1901 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1903 struct strbuf sb = STRBUF_INIT;
1905 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1907 printf("%s", relative_path(argv[1], argv[2], &sb));
1908 strbuf_release(&sb);
1912 static const char *remote_submodule_branch(const char *path)
1914 const struct submodule *sub;
1915 const char *branch = NULL;
1918 sub = submodule_from_path(the_repository, &null_oid, path);
1922 key = xstrfmt("submodule.%s.branch", sub->name);
1923 if (repo_config_get_string_const(the_repository, key, &branch))
1924 branch = sub->branch;
1930 if (!strcmp(branch, ".")) {
1931 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1934 die(_("No such ref: %s"), "HEAD");
1937 if (!strcmp(refname, "HEAD"))
1938 die(_("Submodule (%s) branch configured to inherit "
1939 "branch from superproject, but the superproject "
1940 "is not on any branch"), sub->name);
1942 if (!skip_prefix(refname, "refs/heads/", &refname))
1943 die(_("Expecting a full ref name, got %s"), refname);
1950 static int resolve_remote_submodule_branch(int argc, const char **argv,
1954 struct strbuf sb = STRBUF_INIT;
1956 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1958 ret = remote_submodule_branch(argv[1]);
1960 die("submodule %s doesn't exist", argv[1]);
1963 strbuf_release(&sb);
1967 static int push_check(int argc, const char **argv, const char *prefix)
1969 struct remote *remote;
1970 const char *superproject_head;
1972 int detached_head = 0;
1973 struct object_id head_oid;
1976 die("submodule--helper push-check requires at least 2 arguments");
1979 * superproject's resolved head ref.
1980 * if HEAD then the superproject is in a detached head state, otherwise
1981 * it will be the resolved head ref.
1983 superproject_head = argv[1];
1986 /* Get the submodule's head ref and determine if it is detached */
1987 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1989 die(_("Failed to resolve HEAD as a valid ref."));
1990 if (!strcmp(head, "HEAD"))
1994 * The remote must be configured.
1995 * This is to avoid pushing to the exact same URL as the parent.
1997 remote = pushremote_get(argv[1]);
1998 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1999 die("remote '%s' not configured", argv[1]);
2001 /* Check the refspec */
2004 struct ref *local_refs = get_local_heads();
2005 struct refspec refspec = REFSPEC_INIT_PUSH;
2007 refspec_appendn(&refspec, argv + 2, argc - 2);
2009 for (i = 0; i < refspec.nr; i++) {
2010 const struct refspec_item *rs = &refspec.items[i];
2012 if (rs->pattern || rs->matching)
2015 /* LHS must match a single ref */
2016 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2021 * If LHS matches 'HEAD' then we need to ensure
2022 * that it matches the same named branch
2023 * checked out in the superproject.
2025 if (!strcmp(rs->src, "HEAD")) {
2026 if (!detached_head &&
2027 !strcmp(head, superproject_head))
2029 die("HEAD does not match the named branch in the superproject");
2033 die("src refspec '%s' must name a ref",
2037 refspec_clear(&refspec);
2044 static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
2046 const struct submodule *sub;
2049 struct repository subrepo;
2052 BUG("submodule--helper ensure-core-worktree <path>");
2056 sub = submodule_from_path(the_repository, &null_oid, path);
2058 BUG("We could get the submodule handle before?");
2060 if (repo_submodule_init(&subrepo, the_repository, sub))
2061 die(_("could not get a repository handle for submodule '%s'"), path);
2063 if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
2064 char *cfg_file, *abs_path;
2065 const char *rel_path;
2066 struct strbuf sb = STRBUF_INIT;
2068 cfg_file = repo_git_path(&subrepo, "config");
2070 abs_path = absolute_pathdup(path);
2071 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2073 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2077 strbuf_release(&sb);
2083 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2086 struct pathspec pathspec;
2087 struct module_list list = MODULE_LIST_INIT;
2088 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2090 struct option embed_gitdir_options[] = {
2091 OPT_STRING(0, "prefix", &prefix,
2093 N_("path into the working tree")),
2094 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2095 ABSORB_GITDIR_RECURSE_SUBMODULES),
2099 const char *const git_submodule_helper_usage[] = {
2100 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2104 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2105 git_submodule_helper_usage, 0);
2107 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2110 for (i = 0; i < list.nr; i++)
2111 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2116 static int is_active(int argc, const char **argv, const char *prefix)
2119 die("submodule--helper is-active takes exactly 1 argument");
2121 return !is_submodule_active(the_repository, argv[1]);
2125 * Exit non-zero if any of the submodule names given on the command line is
2126 * invalid. If no names are given, filter stdin to print only valid names
2127 * (which is primarily intended for testing).
2129 static int check_name(int argc, const char **argv, const char *prefix)
2133 if (check_submodule_name(*argv) < 0)
2137 struct strbuf buf = STRBUF_INIT;
2138 while (strbuf_getline(&buf, stdin) != EOF) {
2139 if (!check_submodule_name(buf.buf))
2140 printf("%s\n", buf.buf);
2142 strbuf_release(&buf);
2147 static int module_config(int argc, const char **argv, const char *prefix)
2150 CHECK_WRITEABLE = 1,
2154 struct option module_config_options[] = {
2155 OPT_CMDMODE(0, "check-writeable", &command,
2156 N_("check if it is safe to write to the .gitmodules file"),
2158 OPT_CMDMODE(0, "unset", &command,
2159 N_("unset the config in the .gitmodules file"),
2163 const char *const git_submodule_helper_usage[] = {
2164 N_("git submodule--helper config <name> [<value>]"),
2165 N_("git submodule--helper config --unset <name>"),
2166 N_("git submodule--helper config --check-writeable"),
2170 argc = parse_options(argc, argv, prefix, module_config_options,
2171 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2173 if (argc == 1 && command == CHECK_WRITEABLE)
2174 return is_writing_gitmodules_ok() ? 0 : -1;
2176 /* Equivalent to ACTION_GET in builtin/config.c */
2177 if (argc == 2 && command != DO_UNSET)
2178 return print_config_from_gitmodules(the_repository, argv[1]);
2180 /* Equivalent to ACTION_SET in builtin/config.c */
2181 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2182 const char *value = (argc == 3) ? argv[2] : NULL;
2184 if (!is_writing_gitmodules_ok())
2185 die(_("please make sure that the .gitmodules file is in the working tree"));
2187 return config_set_in_gitmodules_file_gently(argv[1], value);
2190 usage_with_options(git_submodule_helper_usage, module_config_options);
2193 #define SUPPORT_SUPER_PREFIX (1<<0)
2197 int (*fn)(int, const char **, const char *);
2201 static struct cmd_struct commands[] = {
2202 {"list", module_list, 0},
2203 {"name", module_name, 0},
2204 {"clone", module_clone, 0},
2205 {"update-module-mode", module_update_module_mode, 0},
2206 {"update-clone", update_clone, 0},
2207 {"ensure-core-worktree", ensure_core_worktree, 0},
2208 {"relative-path", resolve_relative_path, 0},
2209 {"resolve-relative-url", resolve_relative_url, 0},
2210 {"resolve-relative-url-test", resolve_relative_url_test, 0},
2211 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
2212 {"init", module_init, SUPPORT_SUPER_PREFIX},
2213 {"status", module_status, SUPPORT_SUPER_PREFIX},
2214 {"print-default-remote", print_default_remote, 0},
2215 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
2216 {"deinit", module_deinit, 0},
2217 {"remote-branch", resolve_remote_submodule_branch, 0},
2218 {"push-check", push_check, 0},
2219 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
2220 {"is-active", is_active, 0},
2221 {"check-name", check_name, 0},
2222 {"config", module_config, 0},
2225 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2228 if (argc < 2 || !strcmp(argv[1], "-h"))
2229 usage("git submodule--helper <command>");
2231 for (i = 0; i < ARRAY_SIZE(commands); i++) {
2232 if (!strcmp(argv[1], commands[i].cmd)) {
2233 if (get_super_prefix() &&
2234 !(commands[i].option & SUPPORT_SUPER_PREFIX))
2235 die(_("%s doesn't support --super-prefix"),
2237 return commands[i].fn(argc - 1, argv + 1, prefix);
2241 die(_("'%s' is not a valid submodule--helper "
2242 "subcommand"), argv[1]);