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 #include "object-store.h"
22 #define OPT_QUIET (1 << 0)
23 #define OPT_CACHED (1 << 1)
24 #define OPT_RECURSIVE (1 << 2)
25 #define OPT_FORCE (1 << 3)
27 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
30 static char *get_default_remote(void)
32 char *dest = NULL, *ret;
33 struct strbuf sb = STRBUF_INIT;
34 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
37 die(_("No such ref: %s"), "HEAD");
40 if (!strcmp(refname, "HEAD"))
41 return xstrdup("origin");
43 if (!skip_prefix(refname, "refs/heads/", &refname))
44 die(_("Expecting a full ref name, got %s"), refname);
46 strbuf_addf(&sb, "branch.%s.remote", refname);
47 if (git_config_get_string(sb.buf, &dest))
48 ret = xstrdup("origin");
56 static int print_default_remote(int argc, const char **argv, const char *prefix)
61 die(_("submodule--helper print-default-remote takes no arguments"));
63 remote = get_default_remote();
65 printf("%s\n", remote);
71 static int starts_with_dot_slash(const char *str)
73 return str[0] == '.' && is_dir_sep(str[1]);
76 static int starts_with_dot_dot_slash(const char *str)
78 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
82 * Returns 1 if it was the last chop before ':'.
84 static int chop_last_dir(char **remoteurl, int is_relative)
86 char *rfind = find_last_dir_sep(*remoteurl);
92 rfind = strrchr(*remoteurl, ':');
98 if (is_relative || !strcmp(".", *remoteurl))
99 die(_("cannot strip one component off url '%s'"),
103 *remoteurl = xstrdup(".");
108 * The `url` argument is the URL that navigates to the submodule origin
109 * repo. When relative, this URL is relative to the superproject origin
110 * URL repo. The `up_path` argument, if specified, is the relative
111 * path that navigates from the submodule working tree to the superproject
112 * working tree. Returns the origin URL of the submodule.
114 * Return either an absolute URL or filesystem path (if the superproject
115 * origin URL is an absolute URL or filesystem path, respectively) or a
116 * relative file system path (if the superproject origin URL is a relative
119 * When the output is a relative file system path, the path is either
120 * relative to the submodule working tree, if up_path is specified, or to
121 * the superproject working tree otherwise.
123 * NEEDSWORK: This works incorrectly on the domain and protocol part.
124 * remote_url url outcome expectation
125 * http://a.com/b ../c http://a.com/c as is
126 * http://a.com/b/ ../c http://a.com/c same as previous line, but
127 * ignore trailing slash in url
128 * http://a.com/b ../../c http://c error out
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 .:c error out
132 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
133 * when a local part has a colon in its path component, too.
135 static char *relative_url(const char *remote_url,
142 char *remoteurl = xstrdup(remote_url);
143 struct strbuf sb = STRBUF_INIT;
144 size_t len = strlen(remoteurl);
146 if (is_dir_sep(remoteurl[len-1]))
147 remoteurl[len-1] = '\0';
149 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
154 * Prepend a './' to ensure all relative
155 * remoteurls start with './' or '../'
157 if (!starts_with_dot_slash(remoteurl) &&
158 !starts_with_dot_dot_slash(remoteurl)) {
160 strbuf_addf(&sb, "./%s", remoteurl);
162 remoteurl = strbuf_detach(&sb, NULL);
166 * When the url starts with '../', remove that and the
167 * last directory in remoteurl.
170 if (starts_with_dot_dot_slash(url)) {
172 colonsep |= chop_last_dir(&remoteurl, is_relative);
173 } else if (starts_with_dot_slash(url))
179 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
180 if (ends_with(url, "/"))
181 strbuf_setlen(&sb, sb.len - 1);
184 if (starts_with_dot_slash(sb.buf))
185 out = xstrdup(sb.buf + 2);
187 out = xstrdup(sb.buf);
190 if (!up_path || !is_relative)
193 strbuf_addf(&sb, "%s%s", up_path, out);
195 return strbuf_detach(&sb, NULL);
198 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
200 char *remoteurl = NULL;
201 char *remote = get_default_remote();
202 const char *up_path = NULL;
205 struct strbuf sb = STRBUF_INIT;
207 if (argc != 2 && argc != 3)
208 die("resolve-relative-url only accepts one or two arguments");
211 strbuf_addf(&sb, "remote.%s.url", remote);
214 if (git_config_get_string(sb.buf, &remoteurl))
215 /* the repository is its own authoritative upstream */
216 remoteurl = xgetcwd();
221 res = relative_url(remoteurl, url, up_path);
228 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
230 char *remoteurl, *res;
231 const char *up_path, *url;
234 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
237 remoteurl = xstrdup(argv[2]);
240 if (!strcmp(up_path, "(null)"))
243 res = relative_url(remoteurl, url, up_path);
250 /* the result should be freed by the caller. */
251 static char *get_submodule_displaypath(const char *path, const char *prefix)
253 const char *super_prefix = get_super_prefix();
255 if (prefix && super_prefix) {
256 BUG("cannot have prefix '%s' and superprefix '%s'",
257 prefix, super_prefix);
259 struct strbuf sb = STRBUF_INIT;
260 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
263 } else if (super_prefix) {
264 return xstrfmt("%s%s", super_prefix, path);
266 return xstrdup(path);
270 static char *compute_rev_name(const char *sub_path, const char* object_id)
272 struct strbuf sb = STRBUF_INIT;
275 static const char *describe_bare[] = { NULL };
277 static const char *describe_tags[] = { "--tags", NULL };
279 static const char *describe_contains[] = { "--contains", NULL };
281 static const char *describe_all_always[] = { "--all", "--always", NULL };
283 static const char **describe_argv[] = { describe_bare, describe_tags,
285 describe_all_always, NULL };
287 for (d = describe_argv; *d; d++) {
288 struct child_process cp = CHILD_PROCESS_INIT;
289 prepare_submodule_repo_env(&cp.env_array);
294 argv_array_push(&cp.args, "describe");
295 argv_array_pushv(&cp.args, *d);
296 argv_array_push(&cp.args, object_id);
298 if (!capture_command(&cp, &sb, 0)) {
299 strbuf_strip_suffix(&sb, "\n");
300 return strbuf_detach(&sb, NULL);
309 const struct cache_entry **entries;
312 #define MODULE_LIST_INIT { NULL, 0, 0 }
314 static int module_list_compute(int argc, const char **argv,
316 struct pathspec *pathspec,
317 struct module_list *list)
320 char *ps_matched = NULL;
321 parse_pathspec(pathspec, 0,
322 PATHSPEC_PREFER_FULL,
326 ps_matched = xcalloc(pathspec->nr, 1);
328 if (read_cache() < 0)
329 die(_("index file corrupt"));
331 for (i = 0; i < active_nr; i++) {
332 const struct cache_entry *ce = active_cache[i];
334 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
336 !S_ISGITLINK(ce->ce_mode))
339 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
340 list->entries[list->nr++] = ce;
341 while (i + 1 < active_nr &&
342 !strcmp(ce->name, active_cache[i + 1]->name))
344 * Skip entries with the same name in different stages
345 * to make sure an entry is returned only once.
350 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
358 static void module_list_active(struct module_list *list)
361 struct module_list active_modules = MODULE_LIST_INIT;
363 for (i = 0; i < list->nr; i++) {
364 const struct cache_entry *ce = list->entries[i];
366 if (!is_submodule_active(the_repository, ce->name))
369 ALLOC_GROW(active_modules.entries,
370 active_modules.nr + 1,
371 active_modules.alloc);
372 active_modules.entries[active_modules.nr++] = ce;
376 *list = active_modules;
379 static char *get_up_path(const char *path)
382 struct strbuf sb = STRBUF_INIT;
384 for (i = count_slashes(path); i; i--)
385 strbuf_addstr(&sb, "../");
388 * Check if 'path' ends with slash or not
389 * for having the same output for dir/sub_dir
392 if (!is_dir_sep(path[strlen(path) - 1]))
393 strbuf_addstr(&sb, "../");
395 return strbuf_detach(&sb, NULL);
398 static int module_list(int argc, const char **argv, const char *prefix)
401 struct pathspec pathspec;
402 struct module_list list = MODULE_LIST_INIT;
404 struct option module_list_options[] = {
405 OPT_STRING(0, "prefix", &prefix,
407 N_("alternative anchor for relative paths")),
411 const char *const git_submodule_helper_usage[] = {
412 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
416 argc = parse_options(argc, argv, prefix, module_list_options,
417 git_submodule_helper_usage, 0);
419 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
422 for (i = 0; i < list.nr; i++) {
423 const struct cache_entry *ce = list.entries[i];
426 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
428 printf("%06o %s %d\t", ce->ce_mode,
429 oid_to_hex(&ce->oid), ce_stage(ce));
431 fprintf(stdout, "%s\n", ce->name);
436 static void for_each_listed_submodule(const struct module_list *list,
437 each_submodule_fn fn, void *cb_data)
440 for (i = 0; i < list->nr; i++)
441 fn(list->entries[i], cb_data);
451 #define CB_FOREACH_INIT { 0 }
453 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
456 struct cb_foreach *info = cb_data;
457 const char *path = list_item->name;
458 const struct object_id *ce_oid = &list_item->oid;
460 const struct submodule *sub;
461 struct child_process cp = CHILD_PROCESS_INIT;
464 displaypath = get_submodule_displaypath(path, info->prefix);
466 sub = submodule_from_path(the_repository, &null_oid, path);
469 die(_("No url found for submodule path '%s' in .gitmodules"),
472 if (!is_submodule_populated_gently(path, NULL))
475 prepare_submodule_repo_env(&cp.env_array);
478 * For the purpose of executing <command> in the submodule,
479 * separate shell is used for the purpose of running the
486 * NEEDSWORK: the command currently has access to the variables $name,
487 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
488 * contains a single argument. This is done for maintaining a faithful
489 * translation from shell script.
491 if (info->argc == 1) {
492 char *toplevel = xgetcwd();
493 struct strbuf sb = STRBUF_INIT;
495 argv_array_pushf(&cp.env_array, "name=%s", sub->name);
496 argv_array_pushf(&cp.env_array, "sm_path=%s", path);
497 argv_array_pushf(&cp.env_array, "displaypath=%s", displaypath);
498 argv_array_pushf(&cp.env_array, "sha1=%s",
500 argv_array_pushf(&cp.env_array, "toplevel=%s", toplevel);
503 * Since the path variable was accessible from the script
504 * before porting, it is also made available after porting.
505 * The environment variable "PATH" has a very special purpose
506 * on windows. And since environment variables are
507 * case-insensitive in windows, it interferes with the
508 * existing PATH variable. Hence, to avoid that, we expose
509 * path via the args argv_array and not via env_array.
511 sq_quote_buf(&sb, path);
512 argv_array_pushf(&cp.args, "path=%s; %s",
513 sb.buf, info->argv[0]);
517 argv_array_pushv(&cp.args, info->argv);
521 printf(_("Entering '%s'\n"), displaypath);
523 if (info->argv[0] && run_command(&cp))
524 die(_("run_command returned non-zero status for %s\n."),
527 if (info->recursive) {
528 struct child_process cpr = CHILD_PROCESS_INIT;
532 prepare_submodule_repo_env(&cpr.env_array);
534 argv_array_pushl(&cpr.args, "--super-prefix", NULL);
535 argv_array_pushf(&cpr.args, "%s/", displaypath);
536 argv_array_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
540 argv_array_push(&cpr.args, "--quiet");
542 argv_array_pushv(&cpr.args, info->argv);
544 if (run_command(&cpr))
545 die(_("run_command returned non-zero status while"
546 "recursing in the nested submodules of %s\n."),
554 static int module_foreach(int argc, const char **argv, const char *prefix)
556 struct cb_foreach info = CB_FOREACH_INIT;
557 struct pathspec pathspec;
558 struct module_list list = MODULE_LIST_INIT;
560 struct option module_foreach_options[] = {
561 OPT__QUIET(&info.quiet, N_("Suppress output of entering each submodule command")),
562 OPT_BOOL(0, "recursive", &info.recursive,
563 N_("Recurse into nested submodules")),
567 const char *const git_submodule_helper_usage[] = {
568 N_("git submodule--helper foreach [--quiet] [--recursive] <command>"),
572 argc = parse_options(argc, argv, prefix, module_foreach_options,
573 git_submodule_helper_usage, PARSE_OPT_KEEP_UNKNOWN);
575 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
580 info.prefix = prefix;
582 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
592 #define INIT_CB_INIT { NULL, 0 }
594 static void init_submodule(const char *path, const char *prefix,
597 const struct submodule *sub;
598 struct strbuf sb = STRBUF_INIT;
599 char *upd = NULL, *url = NULL, *displaypath;
601 displaypath = get_submodule_displaypath(path, prefix);
603 sub = submodule_from_path(the_repository, &null_oid, path);
606 die(_("No url found for submodule path '%s' in .gitmodules"),
610 * NEEDSWORK: In a multi-working-tree world, this needs to be
611 * set in the per-worktree config.
613 * Set active flag for the submodule being initialized
615 if (!is_submodule_active(the_repository, path)) {
616 strbuf_addf(&sb, "submodule.%s.active", sub->name);
617 git_config_set_gently(sb.buf, "true");
622 * Copy url setting when it is not set yet.
623 * To look up the url in .git/config, we must not fall back to
624 * .gitmodules, so look it up directly.
626 strbuf_addf(&sb, "submodule.%s.url", sub->name);
627 if (git_config_get_string(sb.buf, &url)) {
629 die(_("No url found for submodule path '%s' in .gitmodules"),
632 url = xstrdup(sub->url);
634 /* Possibly a url relative to parent */
635 if (starts_with_dot_dot_slash(url) ||
636 starts_with_dot_slash(url)) {
637 char *remoteurl, *relurl;
638 char *remote = get_default_remote();
639 struct strbuf remotesb = STRBUF_INIT;
640 strbuf_addf(&remotesb, "remote.%s.url", remote);
643 if (git_config_get_string(remotesb.buf, &remoteurl)) {
644 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
645 remoteurl = xgetcwd();
647 relurl = relative_url(remoteurl, url, NULL);
648 strbuf_release(&remotesb);
654 if (git_config_set_gently(sb.buf, url))
655 die(_("Failed to register url for submodule path '%s'"),
657 if (!(flags & OPT_QUIET))
659 _("Submodule '%s' (%s) registered for path '%s'\n"),
660 sub->name, url, displaypath);
664 /* Copy "update" setting when it is not set yet */
665 strbuf_addf(&sb, "submodule.%s.update", sub->name);
666 if (git_config_get_string(sb.buf, &upd) &&
667 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
668 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
669 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
671 upd = xstrdup("none");
673 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
675 if (git_config_set_gently(sb.buf, upd))
676 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
684 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
686 struct init_cb *info = cb_data;
687 init_submodule(list_item->name, info->prefix, info->flags);
690 static int module_init(int argc, const char **argv, const char *prefix)
692 struct init_cb info = INIT_CB_INIT;
693 struct pathspec pathspec;
694 struct module_list list = MODULE_LIST_INIT;
697 struct option module_init_options[] = {
698 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
702 const char *const git_submodule_helper_usage[] = {
703 N_("git submodule--helper init [<path>]"),
707 argc = parse_options(argc, argv, prefix, module_init_options,
708 git_submodule_helper_usage, 0);
710 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
714 * If there are no path args and submodule.active is set then,
715 * by default, only initialize 'active' modules.
717 if (!argc && git_config_get_value_multi("submodule.active"))
718 module_list_active(&list);
720 info.prefix = prefix;
722 info.flags |= OPT_QUIET;
724 for_each_listed_submodule(&list, init_submodule_cb, &info);
734 #define STATUS_CB_INIT { NULL, 0 }
736 static void print_status(unsigned int flags, char state, const char *path,
737 const struct object_id *oid, const char *displaypath)
739 if (flags & OPT_QUIET)
742 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
744 if (state == ' ' || state == '+') {
745 const char *name = compute_rev_name(path, oid_to_hex(oid));
748 printf(" (%s)", name);
754 static int handle_submodule_head_ref(const char *refname,
755 const struct object_id *oid, int flags,
758 struct object_id *output = cb_data;
765 static void status_submodule(const char *path, const struct object_id *ce_oid,
766 unsigned int ce_flags, const char *prefix,
770 struct argv_array diff_files_args = ARGV_ARRAY_INIT;
772 int diff_files_result;
774 if (!submodule_from_path(the_repository, &null_oid, path))
775 die(_("no submodule mapping found in .gitmodules for path '%s'"),
778 displaypath = get_submodule_displaypath(path, prefix);
780 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
781 print_status(flags, 'U', path, &null_oid, displaypath);
785 if (!is_submodule_active(the_repository, path)) {
786 print_status(flags, '-', path, ce_oid, displaypath);
790 argv_array_pushl(&diff_files_args, "diff-files",
791 "--ignore-submodules=dirty", "--quiet", "--",
794 git_config(git_diff_basic_config, NULL);
795 init_revisions(&rev, prefix);
797 diff_files_args.argc = setup_revisions(diff_files_args.argc,
798 diff_files_args.argv,
800 diff_files_result = run_diff_files(&rev, 0);
802 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
803 print_status(flags, ' ', path, ce_oid,
805 } else if (!(flags & OPT_CACHED)) {
806 struct object_id oid;
807 struct ref_store *refs = get_submodule_ref_store(path);
810 print_status(flags, '-', path, ce_oid, displaypath);
813 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
814 die(_("could not resolve HEAD ref inside the "
815 "submodule '%s'"), path);
817 print_status(flags, '+', path, &oid, displaypath);
819 print_status(flags, '+', path, ce_oid, displaypath);
822 if (flags & OPT_RECURSIVE) {
823 struct child_process cpr = CHILD_PROCESS_INIT;
827 prepare_submodule_repo_env(&cpr.env_array);
829 argv_array_push(&cpr.args, "--super-prefix");
830 argv_array_pushf(&cpr.args, "%s/", displaypath);
831 argv_array_pushl(&cpr.args, "submodule--helper", "status",
832 "--recursive", NULL);
834 if (flags & OPT_CACHED)
835 argv_array_push(&cpr.args, "--cached");
837 if (flags & OPT_QUIET)
838 argv_array_push(&cpr.args, "--quiet");
840 if (run_command(&cpr))
841 die(_("failed to recurse into submodule '%s'"), path);
845 argv_array_clear(&diff_files_args);
849 static void status_submodule_cb(const struct cache_entry *list_item,
852 struct status_cb *info = cb_data;
853 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
854 info->prefix, info->flags);
857 static int module_status(int argc, const char **argv, const char *prefix)
859 struct status_cb info = STATUS_CB_INIT;
860 struct pathspec pathspec;
861 struct module_list list = MODULE_LIST_INIT;
864 struct option module_status_options[] = {
865 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
866 OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
867 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
871 const char *const git_submodule_helper_usage[] = {
872 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
876 argc = parse_options(argc, argv, prefix, module_status_options,
877 git_submodule_helper_usage, 0);
879 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
882 info.prefix = prefix;
884 info.flags |= OPT_QUIET;
886 for_each_listed_submodule(&list, status_submodule_cb, &info);
891 static int module_name(int argc, const char **argv, const char *prefix)
893 const struct submodule *sub;
896 usage(_("git submodule--helper name <path>"));
898 sub = submodule_from_path(the_repository, &null_oid, argv[1]);
901 die(_("no submodule mapping found in .gitmodules for path '%s'"),
904 printf("%s\n", sub->name);
914 #define SYNC_CB_INIT { NULL, 0 }
916 static void sync_submodule(const char *path, const char *prefix,
919 const struct submodule *sub;
920 char *remote_key = NULL;
921 char *sub_origin_url, *super_config_url, *displaypath;
922 struct strbuf sb = STRBUF_INIT;
923 struct child_process cp = CHILD_PROCESS_INIT;
924 char *sub_config_path = NULL;
926 if (!is_submodule_active(the_repository, path))
929 sub = submodule_from_path(the_repository, &null_oid, path);
931 if (sub && sub->url) {
932 if (starts_with_dot_dot_slash(sub->url) ||
933 starts_with_dot_slash(sub->url)) {
934 char *remote_url, *up_path;
935 char *remote = get_default_remote();
936 strbuf_addf(&sb, "remote.%s.url", remote);
938 if (git_config_get_string(sb.buf, &remote_url))
939 remote_url = xgetcwd();
941 up_path = get_up_path(path);
942 sub_origin_url = relative_url(remote_url, sub->url, up_path);
943 super_config_url = relative_url(remote_url, sub->url, NULL);
949 sub_origin_url = xstrdup(sub->url);
950 super_config_url = xstrdup(sub->url);
953 sub_origin_url = xstrdup("");
954 super_config_url = xstrdup("");
957 displaypath = get_submodule_displaypath(path, prefix);
959 if (!(flags & OPT_QUIET))
960 printf(_("Synchronizing submodule url for '%s'\n"),
964 strbuf_addf(&sb, "submodule.%s.url", sub->name);
965 if (git_config_set_gently(sb.buf, super_config_url))
966 die(_("failed to register url for submodule path '%s'"),
969 if (!is_submodule_populated_gently(path, NULL))
972 prepare_submodule_repo_env(&cp.env_array);
975 argv_array_pushl(&cp.args, "submodule--helper",
976 "print-default-remote", NULL);
979 if (capture_command(&cp, &sb, 0))
980 die(_("failed to get the default remote for submodule '%s'"),
983 strbuf_strip_suffix(&sb, "\n");
984 remote_key = xstrfmt("remote.%s.url", sb.buf);
987 submodule_to_gitdir(&sb, path);
988 strbuf_addstr(&sb, "/config");
990 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
991 die(_("failed to update remote for submodule '%s'"),
994 if (flags & OPT_RECURSIVE) {
995 struct child_process cpr = CHILD_PROCESS_INIT;
999 prepare_submodule_repo_env(&cpr.env_array);
1001 argv_array_push(&cpr.args, "--super-prefix");
1002 argv_array_pushf(&cpr.args, "%s/", displaypath);
1003 argv_array_pushl(&cpr.args, "submodule--helper", "sync",
1004 "--recursive", NULL);
1006 if (flags & OPT_QUIET)
1007 argv_array_push(&cpr.args, "--quiet");
1009 if (run_command(&cpr))
1010 die(_("failed to recurse into submodule '%s'"),
1015 free(super_config_url);
1016 free(sub_origin_url);
1017 strbuf_release(&sb);
1020 free(sub_config_path);
1023 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1025 struct sync_cb *info = cb_data;
1026 sync_submodule(list_item->name, info->prefix, info->flags);
1030 static int module_sync(int argc, const char **argv, const char *prefix)
1032 struct sync_cb info = SYNC_CB_INIT;
1033 struct pathspec pathspec;
1034 struct module_list list = MODULE_LIST_INIT;
1038 struct option module_sync_options[] = {
1039 OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
1040 OPT_BOOL(0, "recursive", &recursive,
1041 N_("Recurse into nested submodules")),
1045 const char *const git_submodule_helper_usage[] = {
1046 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1050 argc = parse_options(argc, argv, prefix, module_sync_options,
1051 git_submodule_helper_usage, 0);
1053 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1056 info.prefix = prefix;
1058 info.flags |= OPT_QUIET;
1060 info.flags |= OPT_RECURSIVE;
1062 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1071 #define DEINIT_CB_INIT { NULL, 0 }
1073 static void deinit_submodule(const char *path, const char *prefix,
1076 const struct submodule *sub;
1077 char *displaypath = NULL;
1078 struct child_process cp_config = CHILD_PROCESS_INIT;
1079 struct strbuf sb_config = STRBUF_INIT;
1080 char *sub_git_dir = xstrfmt("%s/.git", path);
1082 sub = submodule_from_path(the_repository, &null_oid, path);
1084 if (!sub || !sub->name)
1087 displaypath = get_submodule_displaypath(path, prefix);
1089 /* remove the submodule work tree (unless the user already did it) */
1090 if (is_directory(path)) {
1091 struct strbuf sb_rm = STRBUF_INIT;
1095 * protect submodules containing a .git directory
1096 * NEEDSWORK: instead of dying, automatically call
1097 * absorbgitdirs and (possibly) warn.
1099 if (is_directory(sub_git_dir))
1100 die(_("Submodule work tree '%s' contains a .git "
1101 "directory (use 'rm -rf' if you really want "
1102 "to remove it including all of its history)"),
1105 if (!(flags & OPT_FORCE)) {
1106 struct child_process cp_rm = CHILD_PROCESS_INIT;
1108 argv_array_pushl(&cp_rm.args, "rm", "-qn",
1111 if (run_command(&cp_rm))
1112 die(_("Submodule work tree '%s' contains local "
1113 "modifications; use '-f' to discard them"),
1117 strbuf_addstr(&sb_rm, path);
1119 if (!remove_dir_recursively(&sb_rm, 0))
1120 format = _("Cleared directory '%s'\n");
1122 format = _("Could not remove submodule work tree '%s'\n");
1124 if (!(flags & OPT_QUIET))
1125 printf(format, displaypath);
1127 submodule_unset_core_worktree(sub);
1129 strbuf_release(&sb_rm);
1132 if (mkdir(path, 0777))
1133 printf(_("could not create empty submodule directory %s"),
1136 cp_config.git_cmd = 1;
1137 argv_array_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1138 argv_array_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1140 /* remove the .git/config entries (unless the user already did it) */
1141 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1142 char *sub_key = xstrfmt("submodule.%s", sub->name);
1144 * remove the whole section so we have a clean state when
1145 * the user later decides to init this submodule again
1147 git_config_rename_section_in_file(NULL, sub_key, NULL);
1148 if (!(flags & OPT_QUIET))
1149 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1150 sub->name, sub->url, displaypath);
1157 strbuf_release(&sb_config);
1160 static void deinit_submodule_cb(const struct cache_entry *list_item,
1163 struct deinit_cb *info = cb_data;
1164 deinit_submodule(list_item->name, info->prefix, info->flags);
1167 static int module_deinit(int argc, const char **argv, const char *prefix)
1169 struct deinit_cb info = DEINIT_CB_INIT;
1170 struct pathspec pathspec;
1171 struct module_list list = MODULE_LIST_INIT;
1176 struct option module_deinit_options[] = {
1177 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
1178 OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
1179 OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
1183 const char *const git_submodule_helper_usage[] = {
1184 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1188 argc = parse_options(argc, argv, prefix, module_deinit_options,
1189 git_submodule_helper_usage, 0);
1192 error("pathspec and --all are incompatible");
1193 usage_with_options(git_submodule_helper_usage,
1194 module_deinit_options);
1198 die(_("Use '--all' if you really want to deinitialize all submodules"));
1200 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1203 info.prefix = prefix;
1205 info.flags |= OPT_QUIET;
1207 info.flags |= OPT_FORCE;
1209 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1214 static int clone_submodule(const char *path, const char *gitdir, const char *url,
1215 const char *depth, struct string_list *reference, int dissociate,
1216 int quiet, int progress)
1218 struct child_process cp = CHILD_PROCESS_INIT;
1220 argv_array_push(&cp.args, "clone");
1221 argv_array_push(&cp.args, "--no-checkout");
1223 argv_array_push(&cp.args, "--quiet");
1225 argv_array_push(&cp.args, "--progress");
1226 if (depth && *depth)
1227 argv_array_pushl(&cp.args, "--depth", depth, NULL);
1228 if (reference->nr) {
1229 struct string_list_item *item;
1230 for_each_string_list_item(item, reference)
1231 argv_array_pushl(&cp.args, "--reference",
1232 item->string, NULL);
1235 argv_array_push(&cp.args, "--dissociate");
1236 if (gitdir && *gitdir)
1237 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1239 argv_array_push(&cp.args, url);
1240 argv_array_push(&cp.args, path);
1243 prepare_submodule_repo_env(&cp.env_array);
1246 return run_command(&cp);
1249 struct submodule_alternate_setup {
1250 const char *submodule_name;
1251 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1252 SUBMODULE_ALTERNATE_ERROR_DIE,
1253 SUBMODULE_ALTERNATE_ERROR_INFO,
1254 SUBMODULE_ALTERNATE_ERROR_IGNORE
1256 struct string_list *reference;
1258 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1259 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1261 static int add_possible_reference_from_superproject(
1262 struct alternate_object_database *alt, void *sas_cb)
1264 struct submodule_alternate_setup *sas = sas_cb;
1267 * If the alternate object store is another repository, try the
1268 * standard layout with .git/(modules/<name>)+/objects
1270 if (ends_with(alt->path, "/objects")) {
1272 struct strbuf sb = STRBUF_INIT;
1273 struct strbuf err = STRBUF_INIT;
1274 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
1277 * We need to end the new path with '/' to mark it as a dir,
1278 * otherwise a submodule name containing '/' will be broken
1279 * as the last part of a missing submodule reference would
1280 * be taken as a file name.
1282 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
1284 sm_alternate = compute_alternate_path(sb.buf, &err);
1286 string_list_append(sas->reference, xstrdup(sb.buf));
1289 switch (sas->error_mode) {
1290 case SUBMODULE_ALTERNATE_ERROR_DIE:
1291 die(_("submodule '%s' cannot add alternate: %s"),
1292 sas->submodule_name, err.buf);
1293 case SUBMODULE_ALTERNATE_ERROR_INFO:
1294 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
1295 sas->submodule_name, err.buf);
1296 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1300 strbuf_release(&sb);
1306 static void prepare_possible_alternates(const char *sm_name,
1307 struct string_list *reference)
1309 char *sm_alternate = NULL, *error_strategy = NULL;
1310 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1312 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1316 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1318 if (!error_strategy)
1319 error_strategy = xstrdup("die");
1321 sas.submodule_name = sm_name;
1322 sas.reference = reference;
1323 if (!strcmp(error_strategy, "die"))
1324 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1325 else if (!strcmp(error_strategy, "info"))
1326 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1327 else if (!strcmp(error_strategy, "ignore"))
1328 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1330 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1332 if (!strcmp(sm_alternate, "superproject"))
1333 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1334 else if (!strcmp(sm_alternate, "no"))
1337 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1340 free(error_strategy);
1343 static int module_clone(int argc, const char **argv, const char *prefix)
1345 const char *name = NULL, *url = NULL, *depth = NULL;
1348 char *p, *path = NULL, *sm_gitdir;
1349 struct strbuf sb = STRBUF_INIT;
1350 struct string_list reference = STRING_LIST_INIT_NODUP;
1352 char *sm_alternate = NULL, *error_strategy = NULL;
1354 struct option module_clone_options[] = {
1355 OPT_STRING(0, "prefix", &prefix,
1357 N_("alternative anchor for relative paths")),
1358 OPT_STRING(0, "path", &path,
1360 N_("where the new submodule will be cloned to")),
1361 OPT_STRING(0, "name", &name,
1363 N_("name of the new submodule")),
1364 OPT_STRING(0, "url", &url,
1366 N_("url where to clone the submodule from")),
1367 OPT_STRING_LIST(0, "reference", &reference,
1369 N_("reference repository")),
1370 OPT_BOOL(0, "dissociate", &dissociate,
1371 N_("use --reference only while cloning")),
1372 OPT_STRING(0, "depth", &depth,
1374 N_("depth for shallow clones")),
1375 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
1376 OPT_BOOL(0, "progress", &progress,
1377 N_("force cloning progress")),
1381 const char *const git_submodule_helper_usage[] = {
1382 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1383 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1384 "--url <url> --path <path>"),
1388 argc = parse_options(argc, argv, prefix, module_clone_options,
1389 git_submodule_helper_usage, 0);
1391 if (argc || !url || !path || !*path)
1392 usage_with_options(git_submodule_helper_usage,
1393 module_clone_options);
1395 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
1396 sm_gitdir = absolute_pathdup(sb.buf);
1399 if (!is_absolute_path(path)) {
1400 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
1401 path = strbuf_detach(&sb, NULL);
1403 path = xstrdup(path);
1405 if (!file_exists(sm_gitdir)) {
1406 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1407 die(_("could not create directory '%s'"), sm_gitdir);
1409 prepare_possible_alternates(name, &reference);
1411 if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
1413 die(_("clone of '%s' into submodule path '%s' failed"),
1416 if (safe_create_leading_directories_const(path) < 0)
1417 die(_("could not create directory '%s'"), path);
1418 strbuf_addf(&sb, "%s/index", sm_gitdir);
1419 unlink_or_warn(sb.buf);
1423 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
1425 p = git_pathdup_submodule(path, "config");
1427 die(_("could not get submodule directory for '%s'"), path);
1429 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1430 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1432 git_config_set_in_file(p, "submodule.alternateLocation",
1434 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1436 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1440 free(error_strategy);
1442 strbuf_release(&sb);
1449 struct submodule_update_clone {
1450 /* index into 'list', the list of submodules to look into for cloning */
1452 struct module_list list;
1453 unsigned warn_if_uninitialized : 1;
1455 /* update parameter passed via commandline */
1456 struct submodule_update_strategy update;
1458 /* configuration parameters which are passed on to the children */
1461 int recommend_shallow;
1462 struct string_list references;
1465 const char *recursive_prefix;
1468 /* Machine-readable status lines to be consumed by git-submodule.sh */
1469 struct string_list projectlines;
1471 /* If we want to stop as fast as possible and return an error */
1472 unsigned quickstop : 1;
1474 /* failed clones to be retried again */
1475 const struct cache_entry **failed_clones;
1476 int failed_clones_nr, failed_clones_alloc;
1478 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1479 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
1481 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
1484 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1485 struct strbuf *out, const char *displaypath)
1488 * Only mention uninitialized submodules when their
1489 * paths have been specified.
1491 if (suc->warn_if_uninitialized) {
1493 _("Submodule path '%s' not initialized"),
1495 strbuf_addch(out, '\n');
1497 _("Maybe you want to use 'update --init'?"));
1498 strbuf_addch(out, '\n');
1503 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1504 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1506 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1507 struct child_process *child,
1508 struct submodule_update_clone *suc,
1511 const struct submodule *sub = NULL;
1512 const char *url = NULL;
1513 const char *update_string;
1514 enum submodule_update_type update_type;
1516 struct strbuf displaypath_sb = STRBUF_INIT;
1517 struct strbuf sb = STRBUF_INIT;
1518 const char *displaypath = NULL;
1519 int needs_cloning = 0;
1522 if (suc->recursive_prefix)
1523 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1525 strbuf_addstr(&sb, ce->name);
1526 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1527 strbuf_addch(out, '\n');
1531 sub = submodule_from_path(the_repository, &null_oid, ce->name);
1533 if (suc->recursive_prefix)
1534 displaypath = relative_path(suc->recursive_prefix,
1535 ce->name, &displaypath_sb);
1537 displaypath = ce->name;
1540 next_submodule_warn_missing(suc, out, displaypath);
1544 key = xstrfmt("submodule.%s.update", sub->name);
1545 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1546 update_type = parse_submodule_update_type(update_string);
1548 update_type = sub->update_strategy.type;
1552 if (suc->update.type == SM_UPDATE_NONE
1553 || (suc->update.type == SM_UPDATE_UNSPECIFIED
1554 && update_type == SM_UPDATE_NONE)) {
1555 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1556 strbuf_addch(out, '\n');
1560 /* Check if the submodule has been initialized. */
1561 if (!is_submodule_active(the_repository, ce->name)) {
1562 next_submodule_warn_missing(suc, out, displaypath);
1567 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1568 if (repo_config_get_string_const(the_repository, sb.buf, &url))
1572 strbuf_addf(&sb, "%s/.git", ce->name);
1573 needs_cloning = !file_exists(sb.buf);
1576 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
1577 oid_to_hex(&ce->oid), ce_stage(ce),
1578 needs_cloning, ce->name);
1579 string_list_append(&suc->projectlines, sb.buf);
1585 child->no_stdin = 1;
1586 child->stdout_to_stderr = 1;
1588 argv_array_push(&child->args, "submodule--helper");
1589 argv_array_push(&child->args, "clone");
1591 argv_array_push(&child->args, "--progress");
1593 argv_array_push(&child->args, "--quiet");
1595 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1596 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1597 argv_array_push(&child->args, "--depth=1");
1598 argv_array_pushl(&child->args, "--path", sub->path, NULL);
1599 argv_array_pushl(&child->args, "--name", sub->name, NULL);
1600 argv_array_pushl(&child->args, "--url", url, NULL);
1601 if (suc->references.nr) {
1602 struct string_list_item *item;
1603 for_each_string_list_item(item, &suc->references)
1604 argv_array_pushl(&child->args, "--reference", item->string, NULL);
1606 if (suc->dissociate)
1607 argv_array_push(&child->args, "--dissociate");
1609 argv_array_push(&child->args, suc->depth);
1612 strbuf_reset(&displaypath_sb);
1615 return needs_cloning;
1618 static int update_clone_get_next_task(struct child_process *child,
1623 struct submodule_update_clone *suc = suc_cb;
1624 const struct cache_entry *ce;
1627 for (; suc->current < suc->list.nr; suc->current++) {
1628 ce = suc->list.entries[suc->current];
1629 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1630 int *p = xmalloc(sizeof(*p));
1639 * The loop above tried cloning each submodule once, now try the
1640 * stragglers again, which we can imagine as an extension of the
1643 index = suc->current - suc->list.nr;
1644 if (index < suc->failed_clones_nr) {
1646 ce = suc->failed_clones[index];
1647 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1649 strbuf_addstr(err, "BUG: submodule considered for "
1650 "cloning, doesn't need cloning "
1654 p = xmalloc(sizeof(*p));
1664 static int update_clone_start_failure(struct strbuf *err,
1668 struct submodule_update_clone *suc = suc_cb;
1673 static int update_clone_task_finished(int result,
1678 const struct cache_entry *ce;
1679 struct submodule_update_clone *suc = suc_cb;
1681 int *idxP = idx_task_cb;
1688 if (idx < suc->list.nr) {
1689 ce = suc->list.entries[idx];
1690 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1692 strbuf_addch(err, '\n');
1693 ALLOC_GROW(suc->failed_clones,
1694 suc->failed_clones_nr + 1,
1695 suc->failed_clones_alloc);
1696 suc->failed_clones[suc->failed_clones_nr++] = ce;
1699 idx -= suc->list.nr;
1700 ce = suc->failed_clones[idx];
1701 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1703 strbuf_addch(err, '\n');
1711 static int git_update_clone_config(const char *var, const char *value,
1715 if (!strcmp(var, "submodule.fetchjobs"))
1716 *max_jobs = parse_submodule_fetchjobs(var, value);
1720 static int update_clone(int argc, const char **argv, const char *prefix)
1722 const char *update = NULL;
1724 struct string_list_item *item;
1725 struct pathspec pathspec;
1726 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1728 struct option module_update_clone_options[] = {
1729 OPT_STRING(0, "prefix", &prefix,
1731 N_("path into the working tree")),
1732 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1734 N_("path into the working tree, across nested "
1735 "submodule boundaries")),
1736 OPT_STRING(0, "update", &update,
1738 N_("rebase, merge, checkout or none")),
1739 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1740 N_("reference repository")),
1741 OPT_BOOL(0, "dissociate", &suc.dissociate,
1742 N_("use --reference only while cloning")),
1743 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1744 N_("Create a shallow clone truncated to the "
1745 "specified number of revisions")),
1746 OPT_INTEGER('j', "jobs", &max_jobs,
1747 N_("parallel jobs")),
1748 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1749 N_("whether the initial clone should follow the shallow recommendation")),
1750 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1751 OPT_BOOL(0, "progress", &suc.progress,
1752 N_("force cloning progress")),
1756 const char *const git_submodule_helper_usage[] = {
1757 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1760 suc.prefix = prefix;
1762 update_clone_config_from_gitmodules(&max_jobs);
1763 git_config(git_update_clone_config, &max_jobs);
1765 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1766 git_submodule_helper_usage, 0);
1769 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1770 die(_("bad value for update parameter"));
1772 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1776 suc.warn_if_uninitialized = 1;
1778 run_processes_parallel(max_jobs,
1779 update_clone_get_next_task,
1780 update_clone_start_failure,
1781 update_clone_task_finished,
1785 * We saved the output and put it out all at once now.
1787 * - the listener does not have to interleave their (checkout)
1788 * work with our fetching. The writes involved in a
1789 * checkout involve more straightforward sequential I/O.
1790 * - the listener can avoid doing any work if fetching failed.
1795 for_each_string_list_item(item, &suc.projectlines)
1796 fprintf(stdout, "%s", item->string);
1801 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1803 struct strbuf sb = STRBUF_INIT;
1805 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1807 printf("%s", relative_path(argv[1], argv[2], &sb));
1808 strbuf_release(&sb);
1812 static const char *remote_submodule_branch(const char *path)
1814 const struct submodule *sub;
1815 const char *branch = NULL;
1818 sub = submodule_from_path(the_repository, &null_oid, path);
1822 key = xstrfmt("submodule.%s.branch", sub->name);
1823 if (repo_config_get_string_const(the_repository, key, &branch))
1824 branch = sub->branch;
1830 if (!strcmp(branch, ".")) {
1831 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1834 die(_("No such ref: %s"), "HEAD");
1837 if (!strcmp(refname, "HEAD"))
1838 die(_("Submodule (%s) branch configured to inherit "
1839 "branch from superproject, but the superproject "
1840 "is not on any branch"), sub->name);
1842 if (!skip_prefix(refname, "refs/heads/", &refname))
1843 die(_("Expecting a full ref name, got %s"), refname);
1850 static int resolve_remote_submodule_branch(int argc, const char **argv,
1854 struct strbuf sb = STRBUF_INIT;
1856 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1858 ret = remote_submodule_branch(argv[1]);
1860 die("submodule %s doesn't exist", argv[1]);
1863 strbuf_release(&sb);
1867 static int push_check(int argc, const char **argv, const char *prefix)
1869 struct remote *remote;
1870 const char *superproject_head;
1872 int detached_head = 0;
1873 struct object_id head_oid;
1876 die("submodule--helper push-check requires at least 2 arguments");
1879 * superproject's resolved head ref.
1880 * if HEAD then the superproject is in a detached head state, otherwise
1881 * it will be the resolved head ref.
1883 superproject_head = argv[1];
1886 /* Get the submodule's head ref and determine if it is detached */
1887 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1889 die(_("Failed to resolve HEAD as a valid ref."));
1890 if (!strcmp(head, "HEAD"))
1894 * The remote must be configured.
1895 * This is to avoid pushing to the exact same URL as the parent.
1897 remote = pushremote_get(argv[1]);
1898 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1899 die("remote '%s' not configured", argv[1]);
1901 /* Check the refspec */
1904 struct ref *local_refs = get_local_heads();
1905 struct refspec refspec = REFSPEC_INIT_PUSH;
1907 refspec_appendn(&refspec, argv + 2, argc - 2);
1909 for (i = 0; i < refspec.nr; i++) {
1910 const struct refspec_item *rs = &refspec.items[i];
1912 if (rs->pattern || rs->matching)
1915 /* LHS must match a single ref */
1916 switch (count_refspec_match(rs->src, local_refs, NULL)) {
1921 * If LHS matches 'HEAD' then we need to ensure
1922 * that it matches the same named branch
1923 * checked out in the superproject.
1925 if (!strcmp(rs->src, "HEAD")) {
1926 if (!detached_head &&
1927 !strcmp(head, superproject_head))
1929 die("HEAD does not match the named branch in the superproject");
1933 die("src refspec '%s' must name a ref",
1937 refspec_clear(&refspec);
1944 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1947 struct pathspec pathspec;
1948 struct module_list list = MODULE_LIST_INIT;
1949 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1951 struct option embed_gitdir_options[] = {
1952 OPT_STRING(0, "prefix", &prefix,
1954 N_("path into the working tree")),
1955 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1956 ABSORB_GITDIR_RECURSE_SUBMODULES),
1960 const char *const git_submodule_helper_usage[] = {
1961 N_("git submodule--helper embed-git-dir [<path>...]"),
1965 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1966 git_submodule_helper_usage, 0);
1968 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1971 for (i = 0; i < list.nr; i++)
1972 absorb_git_dir_into_superproject(prefix,
1973 list.entries[i]->name, flags);
1978 static int is_active(int argc, const char **argv, const char *prefix)
1981 die("submodule--helper is-active takes exactly 1 argument");
1983 return !is_submodule_active(the_repository, argv[1]);
1987 * Exit non-zero if any of the submodule names given on the command line is
1988 * invalid. If no names are given, filter stdin to print only valid names
1989 * (which is primarily intended for testing).
1991 static int check_name(int argc, const char **argv, const char *prefix)
1995 if (check_submodule_name(*argv) < 0)
1999 struct strbuf buf = STRBUF_INIT;
2000 while (strbuf_getline(&buf, stdin) != EOF) {
2001 if (!check_submodule_name(buf.buf))
2002 printf("%s\n", buf.buf);
2004 strbuf_release(&buf);
2009 static int connect_gitdir_workingtree(int argc, const char **argv, const char *prefix)
2011 struct strbuf sb = STRBUF_INIT;
2012 const char *name, *path;
2016 BUG("submodule--helper connect-gitdir-workingtree <name> <path>");
2021 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
2022 sm_gitdir = absolute_pathdup(sb.buf);
2024 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
2026 strbuf_release(&sb);
2032 #define SUPPORT_SUPER_PREFIX (1<<0)
2036 int (*fn)(int, const char **, const char *);
2040 static struct cmd_struct commands[] = {
2041 {"list", module_list, 0},
2042 {"name", module_name, 0},
2043 {"clone", module_clone, 0},
2044 {"update-clone", update_clone, 0},
2045 {"connect-gitdir-workingtree", connect_gitdir_workingtree, 0},
2046 {"relative-path", resolve_relative_path, 0},
2047 {"resolve-relative-url", resolve_relative_url, 0},
2048 {"resolve-relative-url-test", resolve_relative_url_test, 0},
2049 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
2050 {"init", module_init, SUPPORT_SUPER_PREFIX},
2051 {"status", module_status, SUPPORT_SUPER_PREFIX},
2052 {"print-default-remote", print_default_remote, 0},
2053 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
2054 {"deinit", module_deinit, 0},
2055 {"remote-branch", resolve_remote_submodule_branch, 0},
2056 {"push-check", push_check, 0},
2057 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
2058 {"is-active", is_active, 0},
2059 {"check-name", check_name, 0},
2062 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2065 if (argc < 2 || !strcmp(argv[1], "-h"))
2066 usage("git submodule--helper <command>");
2068 for (i = 0; i < ARRAY_SIZE(commands); i++) {
2069 if (!strcmp(argv[1], commands[i].cmd)) {
2070 if (get_super_prefix() &&
2071 !(commands[i].option & SUPPORT_SUPER_PREFIX))
2072 die(_("%s doesn't support --super-prefix"),
2074 return commands[i].fn(argc - 1, argv + 1, prefix);
2078 die(_("'%s' is not a valid submodule--helper "
2079 "subcommand"), argv[1]);