Merge branch 'po/bundle-doc-clonable'
[alt-git.git] / builtin / submodule--helper.c
bloba4f9f8dc72ba4865a4af430848f3fa5876b6fb7a
1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
2 #include "builtin.h"
3 #include "repository.h"
4 #include "cache.h"
5 #include "config.h"
6 #include "parse-options.h"
7 #include "quote.h"
8 #include "pathspec.h"
9 #include "dir.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
14 #include "remote.h"
15 #include "refs.h"
16 #include "refspec.h"
17 #include "connect.h"
18 #include "revision.h"
19 #include "diffcore.h"
20 #include "diff.h"
21 #include "object-store.h"
22 #include "dir.h"
24 #define OPT_QUIET (1 << 0)
25 #define OPT_CACHED (1 << 1)
26 #define OPT_RECURSIVE (1 << 2)
27 #define OPT_FORCE (1 << 3)
29 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
30 void *cb_data);
32 static char *get_default_remote(void)
34 char *dest = NULL, *ret;
35 struct strbuf sb = STRBUF_INIT;
36 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
38 if (!refname)
39 die(_("No such ref: %s"), "HEAD");
41 /* detached HEAD */
42 if (!strcmp(refname, "HEAD"))
43 return xstrdup("origin");
45 if (!skip_prefix(refname, "refs/heads/", &refname))
46 die(_("Expecting a full ref name, got %s"), refname);
48 strbuf_addf(&sb, "branch.%s.remote", refname);
49 if (git_config_get_string(sb.buf, &dest))
50 ret = xstrdup("origin");
51 else
52 ret = dest;
54 strbuf_release(&sb);
55 return ret;
58 static int print_default_remote(int argc, const char **argv, const char *prefix)
60 char *remote;
62 if (argc != 1)
63 die(_("submodule--helper print-default-remote takes no arguments"));
65 remote = get_default_remote();
66 if (remote)
67 printf("%s\n", remote);
69 free(remote);
70 return 0;
73 static int starts_with_dot_slash(const char *str)
75 return str[0] == '.' && is_dir_sep(str[1]);
78 static int starts_with_dot_dot_slash(const char *str)
80 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
84 * Returns 1 if it was the last chop before ':'.
86 static int chop_last_dir(char **remoteurl, int is_relative)
88 char *rfind = find_last_dir_sep(*remoteurl);
89 if (rfind) {
90 *rfind = '\0';
91 return 0;
94 rfind = strrchr(*remoteurl, ':');
95 if (rfind) {
96 *rfind = '\0';
97 return 1;
100 if (is_relative || !strcmp(".", *remoteurl))
101 die(_("cannot strip one component off url '%s'"),
102 *remoteurl);
104 free(*remoteurl);
105 *remoteurl = xstrdup(".");
106 return 0;
110 * The `url` argument is the URL that navigates to the submodule origin
111 * repo. When relative, this URL is relative to the superproject origin
112 * URL repo. The `up_path` argument, if specified, is the relative
113 * path that navigates from the submodule working tree to the superproject
114 * working tree. Returns the origin URL of the submodule.
116 * Return either an absolute URL or filesystem path (if the superproject
117 * origin URL is an absolute URL or filesystem path, respectively) or a
118 * relative file system path (if the superproject origin URL is a relative
119 * file system path).
121 * When the output is a relative file system path, the path is either
122 * relative to the submodule working tree, if up_path is specified, or to
123 * the superproject working tree otherwise.
125 * NEEDSWORK: This works incorrectly on the domain and protocol part.
126 * remote_url url outcome expectation
127 * http://a.com/b ../c http://a.com/c as is
128 * http://a.com/b/ ../c http://a.com/c same as previous line, but
129 * ignore trailing slash in url
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 http:c error out
133 * http://a.com/b ../../../../../c .:c error out
134 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
135 * when a local part has a colon in its path component, too.
137 static char *relative_url(const char *remote_url,
138 const char *url,
139 const char *up_path)
141 int is_relative = 0;
142 int colonsep = 0;
143 char *out;
144 char *remoteurl = xstrdup(remote_url);
145 struct strbuf sb = STRBUF_INIT;
146 size_t len = strlen(remoteurl);
148 if (is_dir_sep(remoteurl[len-1]))
149 remoteurl[len-1] = '\0';
151 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
152 is_relative = 0;
153 else {
154 is_relative = 1;
156 * Prepend a './' to ensure all relative
157 * remoteurls start with './' or '../'
159 if (!starts_with_dot_slash(remoteurl) &&
160 !starts_with_dot_dot_slash(remoteurl)) {
161 strbuf_reset(&sb);
162 strbuf_addf(&sb, "./%s", remoteurl);
163 free(remoteurl);
164 remoteurl = strbuf_detach(&sb, NULL);
168 * When the url starts with '../', remove that and the
169 * last directory in remoteurl.
171 while (url) {
172 if (starts_with_dot_dot_slash(url)) {
173 url += 3;
174 colonsep |= chop_last_dir(&remoteurl, is_relative);
175 } else if (starts_with_dot_slash(url))
176 url += 2;
177 else
178 break;
180 strbuf_reset(&sb);
181 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
182 if (ends_with(url, "/"))
183 strbuf_setlen(&sb, sb.len - 1);
184 free(remoteurl);
186 if (starts_with_dot_slash(sb.buf))
187 out = xstrdup(sb.buf + 2);
188 else
189 out = xstrdup(sb.buf);
190 strbuf_reset(&sb);
192 if (!up_path || !is_relative)
193 return out;
195 strbuf_addf(&sb, "%s%s", up_path, out);
196 free(out);
197 return strbuf_detach(&sb, NULL);
200 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
202 char *remoteurl = NULL;
203 char *remote = get_default_remote();
204 const char *up_path = NULL;
205 char *res;
206 const char *url;
207 struct strbuf sb = STRBUF_INIT;
209 if (argc != 2 && argc != 3)
210 die("resolve-relative-url only accepts one or two arguments");
212 url = argv[1];
213 strbuf_addf(&sb, "remote.%s.url", remote);
214 free(remote);
216 if (git_config_get_string(sb.buf, &remoteurl))
217 /* the repository is its own authoritative upstream */
218 remoteurl = xgetcwd();
220 if (argc == 3)
221 up_path = argv[2];
223 res = relative_url(remoteurl, url, up_path);
224 puts(res);
225 free(res);
226 free(remoteurl);
227 return 0;
230 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
232 char *remoteurl, *res;
233 const char *up_path, *url;
235 if (argc != 4)
236 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
238 up_path = argv[1];
239 remoteurl = xstrdup(argv[2]);
240 url = argv[3];
242 if (!strcmp(up_path, "(null)"))
243 up_path = NULL;
245 res = relative_url(remoteurl, url, up_path);
246 puts(res);
247 free(res);
248 free(remoteurl);
249 return 0;
252 /* the result should be freed by the caller. */
253 static char *get_submodule_displaypath(const char *path, const char *prefix)
255 const char *super_prefix = get_super_prefix();
257 if (prefix && super_prefix) {
258 BUG("cannot have prefix '%s' and superprefix '%s'",
259 prefix, super_prefix);
260 } else if (prefix) {
261 struct strbuf sb = STRBUF_INIT;
262 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
263 strbuf_release(&sb);
264 return displaypath;
265 } else if (super_prefix) {
266 return xstrfmt("%s%s", super_prefix, path);
267 } else {
268 return xstrdup(path);
272 static char *compute_rev_name(const char *sub_path, const char* object_id)
274 struct strbuf sb = STRBUF_INIT;
275 const char ***d;
277 static const char *describe_bare[] = { NULL };
279 static const char *describe_tags[] = { "--tags", NULL };
281 static const char *describe_contains[] = { "--contains", NULL };
283 static const char *describe_all_always[] = { "--all", "--always", NULL };
285 static const char **describe_argv[] = { describe_bare, describe_tags,
286 describe_contains,
287 describe_all_always, NULL };
289 for (d = describe_argv; *d; d++) {
290 struct child_process cp = CHILD_PROCESS_INIT;
291 prepare_submodule_repo_env(&cp.env_array);
292 cp.dir = sub_path;
293 cp.git_cmd = 1;
294 cp.no_stderr = 1;
296 argv_array_push(&cp.args, "describe");
297 argv_array_pushv(&cp.args, *d);
298 argv_array_push(&cp.args, object_id);
300 if (!capture_command(&cp, &sb, 0)) {
301 strbuf_strip_suffix(&sb, "\n");
302 return strbuf_detach(&sb, NULL);
306 strbuf_release(&sb);
307 return NULL;
310 struct module_list {
311 const struct cache_entry **entries;
312 int alloc, nr;
314 #define MODULE_LIST_INIT { NULL, 0, 0 }
316 static int module_list_compute(int argc, const char **argv,
317 const char *prefix,
318 struct pathspec *pathspec,
319 struct module_list *list)
321 int i, result = 0;
322 char *ps_matched = NULL;
323 parse_pathspec(pathspec, 0,
324 PATHSPEC_PREFER_FULL,
325 prefix, argv);
327 if (pathspec->nr)
328 ps_matched = xcalloc(pathspec->nr, 1);
330 if (read_cache() < 0)
331 die(_("index file corrupt"));
333 for (i = 0; i < active_nr; i++) {
334 const struct cache_entry *ce = active_cache[i];
336 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
337 0, ps_matched, 1) ||
338 !S_ISGITLINK(ce->ce_mode))
339 continue;
341 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
342 list->entries[list->nr++] = ce;
343 while (i + 1 < active_nr &&
344 !strcmp(ce->name, active_cache[i + 1]->name))
346 * Skip entries with the same name in different stages
347 * to make sure an entry is returned only once.
349 i++;
352 if (ps_matched && report_path_error(ps_matched, pathspec))
353 result = -1;
355 free(ps_matched);
357 return result;
360 static void module_list_active(struct module_list *list)
362 int i;
363 struct module_list active_modules = MODULE_LIST_INIT;
365 for (i = 0; i < list->nr; i++) {
366 const struct cache_entry *ce = list->entries[i];
368 if (!is_submodule_active(the_repository, ce->name))
369 continue;
371 ALLOC_GROW(active_modules.entries,
372 active_modules.nr + 1,
373 active_modules.alloc);
374 active_modules.entries[active_modules.nr++] = ce;
377 free(list->entries);
378 *list = active_modules;
381 static char *get_up_path(const char *path)
383 int i;
384 struct strbuf sb = STRBUF_INIT;
386 for (i = count_slashes(path); i; i--)
387 strbuf_addstr(&sb, "../");
390 * Check if 'path' ends with slash or not
391 * for having the same output for dir/sub_dir
392 * and dir/sub_dir/
394 if (!is_dir_sep(path[strlen(path) - 1]))
395 strbuf_addstr(&sb, "../");
397 return strbuf_detach(&sb, NULL);
400 static int module_list(int argc, const char **argv, const char *prefix)
402 int i;
403 struct pathspec pathspec;
404 struct module_list list = MODULE_LIST_INIT;
406 struct option module_list_options[] = {
407 OPT_STRING(0, "prefix", &prefix,
408 N_("path"),
409 N_("alternative anchor for relative paths")),
410 OPT_END()
413 const char *const git_submodule_helper_usage[] = {
414 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
415 NULL
418 argc = parse_options(argc, argv, prefix, module_list_options,
419 git_submodule_helper_usage, 0);
421 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
422 return 1;
424 for (i = 0; i < list.nr; i++) {
425 const struct cache_entry *ce = list.entries[i];
427 if (ce_stage(ce))
428 printf("%06o %s U\t", ce->ce_mode, oid_to_hex(&null_oid));
429 else
430 printf("%06o %s %d\t", ce->ce_mode,
431 oid_to_hex(&ce->oid), ce_stage(ce));
433 fprintf(stdout, "%s\n", ce->name);
435 return 0;
438 static void for_each_listed_submodule(const struct module_list *list,
439 each_submodule_fn fn, void *cb_data)
441 int i;
442 for (i = 0; i < list->nr; i++)
443 fn(list->entries[i], cb_data);
446 struct cb_foreach {
447 int argc;
448 const char **argv;
449 const char *prefix;
450 int quiet;
451 int recursive;
453 #define CB_FOREACH_INIT { 0 }
455 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
456 void *cb_data)
458 struct cb_foreach *info = cb_data;
459 const char *path = list_item->name;
460 const struct object_id *ce_oid = &list_item->oid;
462 const struct submodule *sub;
463 struct child_process cp = CHILD_PROCESS_INIT;
464 char *displaypath;
466 displaypath = get_submodule_displaypath(path, info->prefix);
468 sub = submodule_from_path(the_repository, &null_oid, path);
470 if (!sub)
471 die(_("No url found for submodule path '%s' in .gitmodules"),
472 displaypath);
474 if (!is_submodule_populated_gently(path, NULL))
475 goto cleanup;
477 prepare_submodule_repo_env(&cp.env_array);
480 * For the purpose of executing <command> in the submodule,
481 * separate shell is used for the purpose of running the
482 * child process.
484 cp.use_shell = 1;
485 cp.dir = path;
488 * NEEDSWORK: the command currently has access to the variables $name,
489 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
490 * contains a single argument. This is done for maintaining a faithful
491 * translation from shell script.
493 if (info->argc == 1) {
494 char *toplevel = xgetcwd();
495 struct strbuf sb = STRBUF_INIT;
497 argv_array_pushf(&cp.env_array, "name=%s", sub->name);
498 argv_array_pushf(&cp.env_array, "sm_path=%s", path);
499 argv_array_pushf(&cp.env_array, "displaypath=%s", displaypath);
500 argv_array_pushf(&cp.env_array, "sha1=%s",
501 oid_to_hex(ce_oid));
502 argv_array_pushf(&cp.env_array, "toplevel=%s", toplevel);
505 * Since the path variable was accessible from the script
506 * before porting, it is also made available after porting.
507 * The environment variable "PATH" has a very special purpose
508 * on windows. And since environment variables are
509 * case-insensitive in windows, it interferes with the
510 * existing PATH variable. Hence, to avoid that, we expose
511 * path via the args argv_array and not via env_array.
513 sq_quote_buf(&sb, path);
514 argv_array_pushf(&cp.args, "path=%s; %s",
515 sb.buf, info->argv[0]);
516 strbuf_release(&sb);
517 free(toplevel);
518 } else {
519 argv_array_pushv(&cp.args, info->argv);
522 if (!info->quiet)
523 printf(_("Entering '%s'\n"), displaypath);
525 if (info->argv[0] && run_command(&cp))
526 die(_("run_command returned non-zero status for %s\n."),
527 displaypath);
529 if (info->recursive) {
530 struct child_process cpr = CHILD_PROCESS_INIT;
532 cpr.git_cmd = 1;
533 cpr.dir = path;
534 prepare_submodule_repo_env(&cpr.env_array);
536 argv_array_pushl(&cpr.args, "--super-prefix", NULL);
537 argv_array_pushf(&cpr.args, "%s/", displaypath);
538 argv_array_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
539 NULL);
541 if (info->quiet)
542 argv_array_push(&cpr.args, "--quiet");
544 argv_array_push(&cpr.args, "--");
545 argv_array_pushv(&cpr.args, info->argv);
547 if (run_command(&cpr))
548 die(_("run_command returned non-zero status while "
549 "recursing in the nested submodules of %s\n."),
550 displaypath);
553 cleanup:
554 free(displaypath);
557 static int module_foreach(int argc, const char **argv, const char *prefix)
559 struct cb_foreach info = CB_FOREACH_INIT;
560 struct pathspec pathspec;
561 struct module_list list = MODULE_LIST_INIT;
563 struct option module_foreach_options[] = {
564 OPT__QUIET(&info.quiet, N_("Suppress output of entering each submodule command")),
565 OPT_BOOL(0, "recursive", &info.recursive,
566 N_("Recurse into nested submodules")),
567 OPT_END()
570 const char *const git_submodule_helper_usage[] = {
571 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
572 NULL
575 argc = parse_options(argc, argv, prefix, module_foreach_options,
576 git_submodule_helper_usage, 0);
578 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
579 return 1;
581 info.argc = argc;
582 info.argv = argv;
583 info.prefix = prefix;
585 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
587 return 0;
590 static char *compute_submodule_clone_url(const char *rel_url)
592 char *remoteurl, *relurl;
593 char *remote = get_default_remote();
594 struct strbuf remotesb = STRBUF_INIT;
596 strbuf_addf(&remotesb, "remote.%s.url", remote);
597 if (git_config_get_string(remotesb.buf, &remoteurl)) {
598 warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
599 remoteurl = xgetcwd();
601 relurl = relative_url(remoteurl, rel_url, NULL);
603 free(remote);
604 free(remoteurl);
605 strbuf_release(&remotesb);
607 return relurl;
610 struct init_cb {
611 const char *prefix;
612 unsigned int flags;
615 #define INIT_CB_INIT { NULL, 0 }
617 static void init_submodule(const char *path, const char *prefix,
618 unsigned int flags)
620 const struct submodule *sub;
621 struct strbuf sb = STRBUF_INIT;
622 char *upd = NULL, *url = NULL, *displaypath;
624 displaypath = get_submodule_displaypath(path, prefix);
626 sub = submodule_from_path(the_repository, &null_oid, path);
628 if (!sub)
629 die(_("No url found for submodule path '%s' in .gitmodules"),
630 displaypath);
633 * NEEDSWORK: In a multi-working-tree world, this needs to be
634 * set in the per-worktree config.
636 * Set active flag for the submodule being initialized
638 if (!is_submodule_active(the_repository, path)) {
639 strbuf_addf(&sb, "submodule.%s.active", sub->name);
640 git_config_set_gently(sb.buf, "true");
641 strbuf_reset(&sb);
645 * Copy url setting when it is not set yet.
646 * To look up the url in .git/config, we must not fall back to
647 * .gitmodules, so look it up directly.
649 strbuf_addf(&sb, "submodule.%s.url", sub->name);
650 if (git_config_get_string(sb.buf, &url)) {
651 if (!sub->url)
652 die(_("No url found for submodule path '%s' in .gitmodules"),
653 displaypath);
655 url = xstrdup(sub->url);
657 /* Possibly a url relative to parent */
658 if (starts_with_dot_dot_slash(url) ||
659 starts_with_dot_slash(url)) {
660 char *oldurl = url;
661 url = compute_submodule_clone_url(oldurl);
662 free(oldurl);
665 if (git_config_set_gently(sb.buf, url))
666 die(_("Failed to register url for submodule path '%s'"),
667 displaypath);
668 if (!(flags & OPT_QUIET))
669 fprintf(stderr,
670 _("Submodule '%s' (%s) registered for path '%s'\n"),
671 sub->name, url, displaypath);
673 strbuf_reset(&sb);
675 /* Copy "update" setting when it is not set yet */
676 strbuf_addf(&sb, "submodule.%s.update", sub->name);
677 if (git_config_get_string(sb.buf, &upd) &&
678 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
679 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
680 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
681 sub->name);
682 upd = xstrdup("none");
683 } else
684 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
686 if (git_config_set_gently(sb.buf, upd))
687 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
689 strbuf_release(&sb);
690 free(displaypath);
691 free(url);
692 free(upd);
695 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
697 struct init_cb *info = cb_data;
698 init_submodule(list_item->name, info->prefix, info->flags);
701 static int module_init(int argc, const char **argv, const char *prefix)
703 struct init_cb info = INIT_CB_INIT;
704 struct pathspec pathspec;
705 struct module_list list = MODULE_LIST_INIT;
706 int quiet = 0;
708 struct option module_init_options[] = {
709 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
710 OPT_END()
713 const char *const git_submodule_helper_usage[] = {
714 N_("git submodule--helper init [<options>] [<path>]"),
715 NULL
718 argc = parse_options(argc, argv, prefix, module_init_options,
719 git_submodule_helper_usage, 0);
721 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
722 return 1;
725 * If there are no path args and submodule.active is set then,
726 * by default, only initialize 'active' modules.
728 if (!argc && git_config_get_value_multi("submodule.active"))
729 module_list_active(&list);
731 info.prefix = prefix;
732 if (quiet)
733 info.flags |= OPT_QUIET;
735 for_each_listed_submodule(&list, init_submodule_cb, &info);
737 return 0;
740 struct status_cb {
741 const char *prefix;
742 unsigned int flags;
745 #define STATUS_CB_INIT { NULL, 0 }
747 static void print_status(unsigned int flags, char state, const char *path,
748 const struct object_id *oid, const char *displaypath)
750 if (flags & OPT_QUIET)
751 return;
753 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
755 if (state == ' ' || state == '+') {
756 const char *name = compute_rev_name(path, oid_to_hex(oid));
758 if (name)
759 printf(" (%s)", name);
762 printf("\n");
765 static int handle_submodule_head_ref(const char *refname,
766 const struct object_id *oid, int flags,
767 void *cb_data)
769 struct object_id *output = cb_data;
770 if (oid)
771 oidcpy(output, oid);
773 return 0;
776 static void status_submodule(const char *path, const struct object_id *ce_oid,
777 unsigned int ce_flags, const char *prefix,
778 unsigned int flags)
780 char *displaypath;
781 struct argv_array diff_files_args = ARGV_ARRAY_INIT;
782 struct rev_info rev;
783 int diff_files_result;
785 if (!submodule_from_path(the_repository, &null_oid, path))
786 die(_("no submodule mapping found in .gitmodules for path '%s'"),
787 path);
789 displaypath = get_submodule_displaypath(path, prefix);
791 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
792 print_status(flags, 'U', path, &null_oid, displaypath);
793 goto cleanup;
796 if (!is_submodule_active(the_repository, path)) {
797 print_status(flags, '-', path, ce_oid, displaypath);
798 goto cleanup;
801 argv_array_pushl(&diff_files_args, "diff-files",
802 "--ignore-submodules=dirty", "--quiet", "--",
803 path, NULL);
805 git_config(git_diff_basic_config, NULL);
807 repo_init_revisions(the_repository, &rev, NULL);
808 rev.abbrev = 0;
809 diff_files_args.argc = setup_revisions(diff_files_args.argc,
810 diff_files_args.argv,
811 &rev, NULL);
812 diff_files_result = run_diff_files(&rev, 0);
814 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
815 print_status(flags, ' ', path, ce_oid,
816 displaypath);
817 } else if (!(flags & OPT_CACHED)) {
818 struct object_id oid;
819 struct ref_store *refs = get_submodule_ref_store(path);
821 if (!refs) {
822 print_status(flags, '-', path, ce_oid, displaypath);
823 goto cleanup;
825 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
826 die(_("could not resolve HEAD ref inside the "
827 "submodule '%s'"), path);
829 print_status(flags, '+', path, &oid, displaypath);
830 } else {
831 print_status(flags, '+', path, ce_oid, displaypath);
834 if (flags & OPT_RECURSIVE) {
835 struct child_process cpr = CHILD_PROCESS_INIT;
837 cpr.git_cmd = 1;
838 cpr.dir = path;
839 prepare_submodule_repo_env(&cpr.env_array);
841 argv_array_push(&cpr.args, "--super-prefix");
842 argv_array_pushf(&cpr.args, "%s/", displaypath);
843 argv_array_pushl(&cpr.args, "submodule--helper", "status",
844 "--recursive", NULL);
846 if (flags & OPT_CACHED)
847 argv_array_push(&cpr.args, "--cached");
849 if (flags & OPT_QUIET)
850 argv_array_push(&cpr.args, "--quiet");
852 if (run_command(&cpr))
853 die(_("failed to recurse into submodule '%s'"), path);
856 cleanup:
857 argv_array_clear(&diff_files_args);
858 free(displaypath);
861 static void status_submodule_cb(const struct cache_entry *list_item,
862 void *cb_data)
864 struct status_cb *info = cb_data;
865 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
866 info->prefix, info->flags);
869 static int module_status(int argc, const char **argv, const char *prefix)
871 struct status_cb info = STATUS_CB_INIT;
872 struct pathspec pathspec;
873 struct module_list list = MODULE_LIST_INIT;
874 int quiet = 0;
876 struct option module_status_options[] = {
877 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
878 OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
879 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
880 OPT_END()
883 const char *const git_submodule_helper_usage[] = {
884 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
885 NULL
888 argc = parse_options(argc, argv, prefix, module_status_options,
889 git_submodule_helper_usage, 0);
891 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
892 return 1;
894 info.prefix = prefix;
895 if (quiet)
896 info.flags |= OPT_QUIET;
898 for_each_listed_submodule(&list, status_submodule_cb, &info);
900 return 0;
903 static int module_name(int argc, const char **argv, const char *prefix)
905 const struct submodule *sub;
907 if (argc != 2)
908 usage(_("git submodule--helper name <path>"));
910 sub = submodule_from_path(the_repository, &null_oid, argv[1]);
912 if (!sub)
913 die(_("no submodule mapping found in .gitmodules for path '%s'"),
914 argv[1]);
916 printf("%s\n", sub->name);
918 return 0;
921 struct sync_cb {
922 const char *prefix;
923 unsigned int flags;
926 #define SYNC_CB_INIT { NULL, 0 }
928 static void sync_submodule(const char *path, const char *prefix,
929 unsigned int flags)
931 const struct submodule *sub;
932 char *remote_key = NULL;
933 char *sub_origin_url, *super_config_url, *displaypath;
934 struct strbuf sb = STRBUF_INIT;
935 struct child_process cp = CHILD_PROCESS_INIT;
936 char *sub_config_path = NULL;
938 if (!is_submodule_active(the_repository, path))
939 return;
941 sub = submodule_from_path(the_repository, &null_oid, path);
943 if (sub && sub->url) {
944 if (starts_with_dot_dot_slash(sub->url) ||
945 starts_with_dot_slash(sub->url)) {
946 char *remote_url, *up_path;
947 char *remote = get_default_remote();
948 strbuf_addf(&sb, "remote.%s.url", remote);
950 if (git_config_get_string(sb.buf, &remote_url))
951 remote_url = xgetcwd();
953 up_path = get_up_path(path);
954 sub_origin_url = relative_url(remote_url, sub->url, up_path);
955 super_config_url = relative_url(remote_url, sub->url, NULL);
957 free(remote);
958 free(up_path);
959 free(remote_url);
960 } else {
961 sub_origin_url = xstrdup(sub->url);
962 super_config_url = xstrdup(sub->url);
964 } else {
965 sub_origin_url = xstrdup("");
966 super_config_url = xstrdup("");
969 displaypath = get_submodule_displaypath(path, prefix);
971 if (!(flags & OPT_QUIET))
972 printf(_("Synchronizing submodule url for '%s'\n"),
973 displaypath);
975 strbuf_reset(&sb);
976 strbuf_addf(&sb, "submodule.%s.url", sub->name);
977 if (git_config_set_gently(sb.buf, super_config_url))
978 die(_("failed to register url for submodule path '%s'"),
979 displaypath);
981 if (!is_submodule_populated_gently(path, NULL))
982 goto cleanup;
984 prepare_submodule_repo_env(&cp.env_array);
985 cp.git_cmd = 1;
986 cp.dir = path;
987 argv_array_pushl(&cp.args, "submodule--helper",
988 "print-default-remote", NULL);
990 strbuf_reset(&sb);
991 if (capture_command(&cp, &sb, 0))
992 die(_("failed to get the default remote for submodule '%s'"),
993 path);
995 strbuf_strip_suffix(&sb, "\n");
996 remote_key = xstrfmt("remote.%s.url", sb.buf);
998 strbuf_reset(&sb);
999 submodule_to_gitdir(&sb, path);
1000 strbuf_addstr(&sb, "/config");
1002 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1003 die(_("failed to update remote for submodule '%s'"),
1004 path);
1006 if (flags & OPT_RECURSIVE) {
1007 struct child_process cpr = CHILD_PROCESS_INIT;
1009 cpr.git_cmd = 1;
1010 cpr.dir = path;
1011 prepare_submodule_repo_env(&cpr.env_array);
1013 argv_array_push(&cpr.args, "--super-prefix");
1014 argv_array_pushf(&cpr.args, "%s/", displaypath);
1015 argv_array_pushl(&cpr.args, "submodule--helper", "sync",
1016 "--recursive", NULL);
1018 if (flags & OPT_QUIET)
1019 argv_array_push(&cpr.args, "--quiet");
1021 if (run_command(&cpr))
1022 die(_("failed to recurse into submodule '%s'"),
1023 path);
1026 cleanup:
1027 free(super_config_url);
1028 free(sub_origin_url);
1029 strbuf_release(&sb);
1030 free(remote_key);
1031 free(displaypath);
1032 free(sub_config_path);
1035 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1037 struct sync_cb *info = cb_data;
1038 sync_submodule(list_item->name, info->prefix, info->flags);
1041 static int module_sync(int argc, const char **argv, const char *prefix)
1043 struct sync_cb info = SYNC_CB_INIT;
1044 struct pathspec pathspec;
1045 struct module_list list = MODULE_LIST_INIT;
1046 int quiet = 0;
1047 int recursive = 0;
1049 struct option module_sync_options[] = {
1050 OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
1051 OPT_BOOL(0, "recursive", &recursive,
1052 N_("Recurse into nested submodules")),
1053 OPT_END()
1056 const char *const git_submodule_helper_usage[] = {
1057 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1058 NULL
1061 argc = parse_options(argc, argv, prefix, module_sync_options,
1062 git_submodule_helper_usage, 0);
1064 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1065 return 1;
1067 info.prefix = prefix;
1068 if (quiet)
1069 info.flags |= OPT_QUIET;
1070 if (recursive)
1071 info.flags |= OPT_RECURSIVE;
1073 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1075 return 0;
1078 struct deinit_cb {
1079 const char *prefix;
1080 unsigned int flags;
1082 #define DEINIT_CB_INIT { NULL, 0 }
1084 static void deinit_submodule(const char *path, const char *prefix,
1085 unsigned int flags)
1087 const struct submodule *sub;
1088 char *displaypath = NULL;
1089 struct child_process cp_config = CHILD_PROCESS_INIT;
1090 struct strbuf sb_config = STRBUF_INIT;
1091 char *sub_git_dir = xstrfmt("%s/.git", path);
1093 sub = submodule_from_path(the_repository, &null_oid, path);
1095 if (!sub || !sub->name)
1096 goto cleanup;
1098 displaypath = get_submodule_displaypath(path, prefix);
1100 /* remove the submodule work tree (unless the user already did it) */
1101 if (is_directory(path)) {
1102 struct strbuf sb_rm = STRBUF_INIT;
1103 const char *format;
1106 * protect submodules containing a .git directory
1107 * NEEDSWORK: instead of dying, automatically call
1108 * absorbgitdirs and (possibly) warn.
1110 if (is_directory(sub_git_dir))
1111 die(_("Submodule work tree '%s' contains a .git "
1112 "directory (use 'rm -rf' if you really want "
1113 "to remove it including all of its history)"),
1114 displaypath);
1116 if (!(flags & OPT_FORCE)) {
1117 struct child_process cp_rm = CHILD_PROCESS_INIT;
1118 cp_rm.git_cmd = 1;
1119 argv_array_pushl(&cp_rm.args, "rm", "-qn",
1120 path, NULL);
1122 if (run_command(&cp_rm))
1123 die(_("Submodule work tree '%s' contains local "
1124 "modifications; use '-f' to discard them"),
1125 displaypath);
1128 strbuf_addstr(&sb_rm, path);
1130 if (!remove_dir_recursively(&sb_rm, 0))
1131 format = _("Cleared directory '%s'\n");
1132 else
1133 format = _("Could not remove submodule work tree '%s'\n");
1135 if (!(flags & OPT_QUIET))
1136 printf(format, displaypath);
1138 submodule_unset_core_worktree(sub);
1140 strbuf_release(&sb_rm);
1143 if (mkdir(path, 0777))
1144 printf(_("could not create empty submodule directory %s"),
1145 displaypath);
1147 cp_config.git_cmd = 1;
1148 argv_array_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1149 argv_array_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1151 /* remove the .git/config entries (unless the user already did it) */
1152 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1153 char *sub_key = xstrfmt("submodule.%s", sub->name);
1155 * remove the whole section so we have a clean state when
1156 * the user later decides to init this submodule again
1158 git_config_rename_section_in_file(NULL, sub_key, NULL);
1159 if (!(flags & OPT_QUIET))
1160 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1161 sub->name, sub->url, displaypath);
1162 free(sub_key);
1165 cleanup:
1166 free(displaypath);
1167 free(sub_git_dir);
1168 strbuf_release(&sb_config);
1171 static void deinit_submodule_cb(const struct cache_entry *list_item,
1172 void *cb_data)
1174 struct deinit_cb *info = cb_data;
1175 deinit_submodule(list_item->name, info->prefix, info->flags);
1178 static int module_deinit(int argc, const char **argv, const char *prefix)
1180 struct deinit_cb info = DEINIT_CB_INIT;
1181 struct pathspec pathspec;
1182 struct module_list list = MODULE_LIST_INIT;
1183 int quiet = 0;
1184 int force = 0;
1185 int all = 0;
1187 struct option module_deinit_options[] = {
1188 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
1189 OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
1190 OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
1191 OPT_END()
1194 const char *const git_submodule_helper_usage[] = {
1195 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1196 NULL
1199 argc = parse_options(argc, argv, prefix, module_deinit_options,
1200 git_submodule_helper_usage, 0);
1202 if (all && argc) {
1203 error("pathspec and --all are incompatible");
1204 usage_with_options(git_submodule_helper_usage,
1205 module_deinit_options);
1208 if (!argc && !all)
1209 die(_("Use '--all' if you really want to deinitialize all submodules"));
1211 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1212 return 1;
1214 info.prefix = prefix;
1215 if (quiet)
1216 info.flags |= OPT_QUIET;
1217 if (force)
1218 info.flags |= OPT_FORCE;
1220 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1222 return 0;
1225 static int clone_submodule(const char *path, const char *gitdir, const char *url,
1226 const char *depth, struct string_list *reference, int dissociate,
1227 int quiet, int progress)
1229 struct child_process cp = CHILD_PROCESS_INIT;
1231 argv_array_push(&cp.args, "clone");
1232 argv_array_push(&cp.args, "--no-checkout");
1233 if (quiet)
1234 argv_array_push(&cp.args, "--quiet");
1235 if (progress)
1236 argv_array_push(&cp.args, "--progress");
1237 if (depth && *depth)
1238 argv_array_pushl(&cp.args, "--depth", depth, NULL);
1239 if (reference->nr) {
1240 struct string_list_item *item;
1241 for_each_string_list_item(item, reference)
1242 argv_array_pushl(&cp.args, "--reference",
1243 item->string, NULL);
1245 if (dissociate)
1246 argv_array_push(&cp.args, "--dissociate");
1247 if (gitdir && *gitdir)
1248 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1250 argv_array_push(&cp.args, "--");
1251 argv_array_push(&cp.args, url);
1252 argv_array_push(&cp.args, path);
1254 cp.git_cmd = 1;
1255 prepare_submodule_repo_env(&cp.env_array);
1256 cp.no_stdin = 1;
1258 return run_command(&cp);
1261 struct submodule_alternate_setup {
1262 const char *submodule_name;
1263 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1264 SUBMODULE_ALTERNATE_ERROR_DIE,
1265 SUBMODULE_ALTERNATE_ERROR_INFO,
1266 SUBMODULE_ALTERNATE_ERROR_IGNORE
1267 } error_mode;
1268 struct string_list *reference;
1270 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1271 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1273 static int add_possible_reference_from_superproject(
1274 struct object_directory *odb, void *sas_cb)
1276 struct submodule_alternate_setup *sas = sas_cb;
1277 size_t len;
1280 * If the alternate object store is another repository, try the
1281 * standard layout with .git/(modules/<name>)+/objects
1283 if (strip_suffix(odb->path, "/objects", &len)) {
1284 char *sm_alternate;
1285 struct strbuf sb = STRBUF_INIT;
1286 struct strbuf err = STRBUF_INIT;
1287 strbuf_add(&sb, odb->path, len);
1290 * We need to end the new path with '/' to mark it as a dir,
1291 * otherwise a submodule name containing '/' will be broken
1292 * as the last part of a missing submodule reference would
1293 * be taken as a file name.
1295 strbuf_addf(&sb, "/modules/%s/", sas->submodule_name);
1297 sm_alternate = compute_alternate_path(sb.buf, &err);
1298 if (sm_alternate) {
1299 string_list_append(sas->reference, xstrdup(sb.buf));
1300 free(sm_alternate);
1301 } else {
1302 switch (sas->error_mode) {
1303 case SUBMODULE_ALTERNATE_ERROR_DIE:
1304 die(_("submodule '%s' cannot add alternate: %s"),
1305 sas->submodule_name, err.buf);
1306 case SUBMODULE_ALTERNATE_ERROR_INFO:
1307 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1308 sas->submodule_name, err.buf);
1309 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1310 ; /* nothing */
1313 strbuf_release(&sb);
1316 return 0;
1319 static void prepare_possible_alternates(const char *sm_name,
1320 struct string_list *reference)
1322 char *sm_alternate = NULL, *error_strategy = NULL;
1323 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1325 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1326 if (!sm_alternate)
1327 return;
1329 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1331 if (!error_strategy)
1332 error_strategy = xstrdup("die");
1334 sas.submodule_name = sm_name;
1335 sas.reference = reference;
1336 if (!strcmp(error_strategy, "die"))
1337 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1338 else if (!strcmp(error_strategy, "info"))
1339 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1340 else if (!strcmp(error_strategy, "ignore"))
1341 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1342 else
1343 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1345 if (!strcmp(sm_alternate, "superproject"))
1346 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1347 else if (!strcmp(sm_alternate, "no"))
1348 ; /* do nothing */
1349 else
1350 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1352 free(sm_alternate);
1353 free(error_strategy);
1356 static int module_clone(int argc, const char **argv, const char *prefix)
1358 const char *name = NULL, *url = NULL, *depth = NULL;
1359 int quiet = 0;
1360 int progress = 0;
1361 char *p, *path = NULL, *sm_gitdir;
1362 struct strbuf sb = STRBUF_INIT;
1363 struct string_list reference = STRING_LIST_INIT_NODUP;
1364 int dissociate = 0, require_init = 0;
1365 char *sm_alternate = NULL, *error_strategy = NULL;
1367 struct option module_clone_options[] = {
1368 OPT_STRING(0, "prefix", &prefix,
1369 N_("path"),
1370 N_("alternative anchor for relative paths")),
1371 OPT_STRING(0, "path", &path,
1372 N_("path"),
1373 N_("where the new submodule will be cloned to")),
1374 OPT_STRING(0, "name", &name,
1375 N_("string"),
1376 N_("name of the new submodule")),
1377 OPT_STRING(0, "url", &url,
1378 N_("string"),
1379 N_("url where to clone the submodule from")),
1380 OPT_STRING_LIST(0, "reference", &reference,
1381 N_("repo"),
1382 N_("reference repository")),
1383 OPT_BOOL(0, "dissociate", &dissociate,
1384 N_("use --reference only while cloning")),
1385 OPT_STRING(0, "depth", &depth,
1386 N_("string"),
1387 N_("depth for shallow clones")),
1388 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
1389 OPT_BOOL(0, "progress", &progress,
1390 N_("force cloning progress")),
1391 OPT_BOOL(0, "require-init", &require_init,
1392 N_("disallow cloning into non-empty directory")),
1393 OPT_END()
1396 const char *const git_submodule_helper_usage[] = {
1397 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1398 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1399 "--url <url> --path <path>"),
1400 NULL
1403 argc = parse_options(argc, argv, prefix, module_clone_options,
1404 git_submodule_helper_usage, 0);
1406 if (argc || !url || !path || !*path)
1407 usage_with_options(git_submodule_helper_usage,
1408 module_clone_options);
1410 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
1411 sm_gitdir = absolute_pathdup(sb.buf);
1412 strbuf_reset(&sb);
1414 if (!is_absolute_path(path)) {
1415 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
1416 path = strbuf_detach(&sb, NULL);
1417 } else
1418 path = xstrdup(path);
1420 if (validate_submodule_git_dir(sm_gitdir, name) < 0)
1421 die(_("refusing to create/use '%s' in another submodule's "
1422 "git dir"), sm_gitdir);
1424 if (!file_exists(sm_gitdir)) {
1425 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1426 die(_("could not create directory '%s'"), sm_gitdir);
1428 prepare_possible_alternates(name, &reference);
1430 if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
1431 quiet, progress))
1432 die(_("clone of '%s' into submodule path '%s' failed"),
1433 url, path);
1434 } else {
1435 if (require_init && !access(path, X_OK) && !is_empty_dir(path))
1436 die(_("directory not empty: '%s'"), path);
1437 if (safe_create_leading_directories_const(path) < 0)
1438 die(_("could not create directory '%s'"), path);
1439 strbuf_addf(&sb, "%s/index", sm_gitdir);
1440 unlink_or_warn(sb.buf);
1441 strbuf_reset(&sb);
1444 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
1446 p = git_pathdup_submodule(path, "config");
1447 if (!p)
1448 die(_("could not get submodule directory for '%s'"), path);
1450 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1451 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1452 if (sm_alternate)
1453 git_config_set_in_file(p, "submodule.alternateLocation",
1454 sm_alternate);
1455 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1456 if (error_strategy)
1457 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1458 error_strategy);
1460 free(sm_alternate);
1461 free(error_strategy);
1463 strbuf_release(&sb);
1464 free(sm_gitdir);
1465 free(path);
1466 free(p);
1467 return 0;
1470 static void determine_submodule_update_strategy(struct repository *r,
1471 int just_cloned,
1472 const char *path,
1473 const char *update,
1474 struct submodule_update_strategy *out)
1476 const struct submodule *sub = submodule_from_path(r, &null_oid, path);
1477 char *key;
1478 const char *val;
1480 key = xstrfmt("submodule.%s.update", sub->name);
1482 if (update) {
1483 if (parse_submodule_update_strategy(update, out) < 0)
1484 die(_("Invalid update mode '%s' for submodule path '%s'"),
1485 update, path);
1486 } else if (!repo_config_get_string_const(r, key, &val)) {
1487 if (parse_submodule_update_strategy(val, out) < 0)
1488 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1489 val, path);
1490 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1491 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1492 BUG("how did we read update = !command from .gitmodules?");
1493 out->type = sub->update_strategy.type;
1494 out->command = sub->update_strategy.command;
1495 } else
1496 out->type = SM_UPDATE_CHECKOUT;
1498 if (just_cloned &&
1499 (out->type == SM_UPDATE_MERGE ||
1500 out->type == SM_UPDATE_REBASE ||
1501 out->type == SM_UPDATE_NONE))
1502 out->type = SM_UPDATE_CHECKOUT;
1504 free(key);
1507 static int module_update_module_mode(int argc, const char **argv, const char *prefix)
1509 const char *path, *update = NULL;
1510 int just_cloned;
1511 struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
1513 if (argc < 3 || argc > 4)
1514 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1516 just_cloned = git_config_int("just_cloned", argv[1]);
1517 path = argv[2];
1519 if (argc == 4)
1520 update = argv[3];
1522 determine_submodule_update_strategy(the_repository,
1523 just_cloned, path, update,
1524 &update_strategy);
1525 fputs(submodule_strategy_to_string(&update_strategy), stdout);
1527 return 0;
1530 struct update_clone_data {
1531 const struct submodule *sub;
1532 struct object_id oid;
1533 unsigned just_cloned;
1536 struct submodule_update_clone {
1537 /* index into 'list', the list of submodules to look into for cloning */
1538 int current;
1539 struct module_list list;
1540 unsigned warn_if_uninitialized : 1;
1542 /* update parameter passed via commandline */
1543 struct submodule_update_strategy update;
1545 /* configuration parameters which are passed on to the children */
1546 int progress;
1547 int quiet;
1548 int recommend_shallow;
1549 struct string_list references;
1550 int dissociate;
1551 unsigned require_init;
1552 const char *depth;
1553 const char *recursive_prefix;
1554 const char *prefix;
1556 /* to be consumed by git-submodule.sh */
1557 struct update_clone_data *update_clone;
1558 int update_clone_nr; int update_clone_alloc;
1560 /* If we want to stop as fast as possible and return an error */
1561 unsigned quickstop : 1;
1563 /* failed clones to be retried again */
1564 const struct cache_entry **failed_clones;
1565 int failed_clones_nr, failed_clones_alloc;
1567 int max_jobs;
1569 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1570 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, 0, \
1571 NULL, NULL, NULL, \
1572 NULL, 0, 0, 0, NULL, 0, 0, 1}
1575 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1576 struct strbuf *out, const char *displaypath)
1579 * Only mention uninitialized submodules when their
1580 * paths have been specified.
1582 if (suc->warn_if_uninitialized) {
1583 strbuf_addf(out,
1584 _("Submodule path '%s' not initialized"),
1585 displaypath);
1586 strbuf_addch(out, '\n');
1587 strbuf_addstr(out,
1588 _("Maybe you want to use 'update --init'?"));
1589 strbuf_addch(out, '\n');
1594 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1595 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1597 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1598 struct child_process *child,
1599 struct submodule_update_clone *suc,
1600 struct strbuf *out)
1602 const struct submodule *sub = NULL;
1603 const char *url = NULL;
1604 const char *update_string;
1605 enum submodule_update_type update_type;
1606 char *key;
1607 struct strbuf displaypath_sb = STRBUF_INIT;
1608 struct strbuf sb = STRBUF_INIT;
1609 const char *displaypath = NULL;
1610 int needs_cloning = 0;
1611 int need_free_url = 0;
1613 if (ce_stage(ce)) {
1614 if (suc->recursive_prefix)
1615 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1616 else
1617 strbuf_addstr(&sb, ce->name);
1618 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1619 strbuf_addch(out, '\n');
1620 goto cleanup;
1623 sub = submodule_from_path(the_repository, &null_oid, ce->name);
1625 if (suc->recursive_prefix)
1626 displaypath = relative_path(suc->recursive_prefix,
1627 ce->name, &displaypath_sb);
1628 else
1629 displaypath = ce->name;
1631 if (!sub) {
1632 next_submodule_warn_missing(suc, out, displaypath);
1633 goto cleanup;
1636 key = xstrfmt("submodule.%s.update", sub->name);
1637 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1638 update_type = parse_submodule_update_type(update_string);
1639 } else {
1640 update_type = sub->update_strategy.type;
1642 free(key);
1644 if (suc->update.type == SM_UPDATE_NONE
1645 || (suc->update.type == SM_UPDATE_UNSPECIFIED
1646 && update_type == SM_UPDATE_NONE)) {
1647 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1648 strbuf_addch(out, '\n');
1649 goto cleanup;
1652 /* Check if the submodule has been initialized. */
1653 if (!is_submodule_active(the_repository, ce->name)) {
1654 next_submodule_warn_missing(suc, out, displaypath);
1655 goto cleanup;
1658 strbuf_reset(&sb);
1659 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1660 if (repo_config_get_string_const(the_repository, sb.buf, &url)) {
1661 if (starts_with_dot_slash(sub->url) ||
1662 starts_with_dot_dot_slash(sub->url)) {
1663 url = compute_submodule_clone_url(sub->url);
1664 need_free_url = 1;
1665 } else
1666 url = sub->url;
1669 strbuf_reset(&sb);
1670 strbuf_addf(&sb, "%s/.git", ce->name);
1671 needs_cloning = !file_exists(sb.buf);
1673 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
1674 suc->update_clone_alloc);
1675 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
1676 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
1677 suc->update_clone[suc->update_clone_nr].sub = sub;
1678 suc->update_clone_nr++;
1680 if (!needs_cloning)
1681 goto cleanup;
1683 child->git_cmd = 1;
1684 child->no_stdin = 1;
1685 child->stdout_to_stderr = 1;
1686 child->err = -1;
1687 argv_array_push(&child->args, "submodule--helper");
1688 argv_array_push(&child->args, "clone");
1689 if (suc->progress)
1690 argv_array_push(&child->args, "--progress");
1691 if (suc->quiet)
1692 argv_array_push(&child->args, "--quiet");
1693 if (suc->prefix)
1694 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1695 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1696 argv_array_push(&child->args, "--depth=1");
1697 if (suc->require_init)
1698 argv_array_push(&child->args, "--require-init");
1699 argv_array_pushl(&child->args, "--path", sub->path, NULL);
1700 argv_array_pushl(&child->args, "--name", sub->name, NULL);
1701 argv_array_pushl(&child->args, "--url", url, NULL);
1702 if (suc->references.nr) {
1703 struct string_list_item *item;
1704 for_each_string_list_item(item, &suc->references)
1705 argv_array_pushl(&child->args, "--reference", item->string, NULL);
1707 if (suc->dissociate)
1708 argv_array_push(&child->args, "--dissociate");
1709 if (suc->depth)
1710 argv_array_push(&child->args, suc->depth);
1712 cleanup:
1713 strbuf_reset(&displaypath_sb);
1714 strbuf_reset(&sb);
1715 if (need_free_url)
1716 free((void*)url);
1718 return needs_cloning;
1721 static int update_clone_get_next_task(struct child_process *child,
1722 struct strbuf *err,
1723 void *suc_cb,
1724 void **idx_task_cb)
1726 struct submodule_update_clone *suc = suc_cb;
1727 const struct cache_entry *ce;
1728 int index;
1730 for (; suc->current < suc->list.nr; suc->current++) {
1731 ce = suc->list.entries[suc->current];
1732 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1733 int *p = xmalloc(sizeof(*p));
1734 *p = suc->current;
1735 *idx_task_cb = p;
1736 suc->current++;
1737 return 1;
1742 * The loop above tried cloning each submodule once, now try the
1743 * stragglers again, which we can imagine as an extension of the
1744 * entry list.
1746 index = suc->current - suc->list.nr;
1747 if (index < suc->failed_clones_nr) {
1748 int *p;
1749 ce = suc->failed_clones[index];
1750 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1751 suc->current ++;
1752 strbuf_addstr(err, "BUG: submodule considered for "
1753 "cloning, doesn't need cloning "
1754 "any more?\n");
1755 return 0;
1757 p = xmalloc(sizeof(*p));
1758 *p = suc->current;
1759 *idx_task_cb = p;
1760 suc->current ++;
1761 return 1;
1764 return 0;
1767 static int update_clone_start_failure(struct strbuf *err,
1768 void *suc_cb,
1769 void *idx_task_cb)
1771 struct submodule_update_clone *suc = suc_cb;
1772 suc->quickstop = 1;
1773 return 1;
1776 static int update_clone_task_finished(int result,
1777 struct strbuf *err,
1778 void *suc_cb,
1779 void *idx_task_cb)
1781 const struct cache_entry *ce;
1782 struct submodule_update_clone *suc = suc_cb;
1784 int *idxP = idx_task_cb;
1785 int idx = *idxP;
1786 free(idxP);
1788 if (!result)
1789 return 0;
1791 if (idx < suc->list.nr) {
1792 ce = suc->list.entries[idx];
1793 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1794 ce->name);
1795 strbuf_addch(err, '\n');
1796 ALLOC_GROW(suc->failed_clones,
1797 suc->failed_clones_nr + 1,
1798 suc->failed_clones_alloc);
1799 suc->failed_clones[suc->failed_clones_nr++] = ce;
1800 return 0;
1801 } else {
1802 idx -= suc->list.nr;
1803 ce = suc->failed_clones[idx];
1804 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1805 ce->name);
1806 strbuf_addch(err, '\n');
1807 suc->quickstop = 1;
1808 return 1;
1811 return 0;
1814 static int git_update_clone_config(const char *var, const char *value,
1815 void *cb)
1817 int *max_jobs = cb;
1818 if (!strcmp(var, "submodule.fetchjobs"))
1819 *max_jobs = parse_submodule_fetchjobs(var, value);
1820 return 0;
1823 static void update_submodule(struct update_clone_data *ucd)
1825 fprintf(stdout, "dummy %s %d\t%s\n",
1826 oid_to_hex(&ucd->oid),
1827 ucd->just_cloned,
1828 ucd->sub->path);
1831 static int update_submodules(struct submodule_update_clone *suc)
1833 int i;
1835 run_processes_parallel_tr2(suc->max_jobs, update_clone_get_next_task,
1836 update_clone_start_failure,
1837 update_clone_task_finished, suc, "submodule",
1838 "parallel/update");
1841 * We saved the output and put it out all at once now.
1842 * That means:
1843 * - the listener does not have to interleave their (checkout)
1844 * work with our fetching. The writes involved in a
1845 * checkout involve more straightforward sequential I/O.
1846 * - the listener can avoid doing any work if fetching failed.
1848 if (suc->quickstop)
1849 return 1;
1851 for (i = 0; i < suc->update_clone_nr; i++)
1852 update_submodule(&suc->update_clone[i]);
1854 return 0;
1857 static int update_clone(int argc, const char **argv, const char *prefix)
1859 const char *update = NULL;
1860 struct pathspec pathspec;
1861 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1863 struct option module_update_clone_options[] = {
1864 OPT_STRING(0, "prefix", &prefix,
1865 N_("path"),
1866 N_("path into the working tree")),
1867 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1868 N_("path"),
1869 N_("path into the working tree, across nested "
1870 "submodule boundaries")),
1871 OPT_STRING(0, "update", &update,
1872 N_("string"),
1873 N_("rebase, merge, checkout or none")),
1874 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1875 N_("reference repository")),
1876 OPT_BOOL(0, "dissociate", &suc.dissociate,
1877 N_("use --reference only while cloning")),
1878 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1879 N_("Create a shallow clone truncated to the "
1880 "specified number of revisions")),
1881 OPT_INTEGER('j', "jobs", &suc.max_jobs,
1882 N_("parallel jobs")),
1883 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1884 N_("whether the initial clone should follow the shallow recommendation")),
1885 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1886 OPT_BOOL(0, "progress", &suc.progress,
1887 N_("force cloning progress")),
1888 OPT_BOOL(0, "require-init", &suc.require_init,
1889 N_("disallow cloning into non-empty directory")),
1890 OPT_END()
1893 const char *const git_submodule_helper_usage[] = {
1894 N_("git submodule--helper update-clone [--prefix=<path>] [<path>...]"),
1895 NULL
1897 suc.prefix = prefix;
1899 update_clone_config_from_gitmodules(&suc.max_jobs);
1900 git_config(git_update_clone_config, &suc.max_jobs);
1902 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1903 git_submodule_helper_usage, 0);
1905 if (update)
1906 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1907 die(_("bad value for update parameter"));
1909 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1910 return 1;
1912 if (pathspec.nr)
1913 suc.warn_if_uninitialized = 1;
1915 return update_submodules(&suc);
1918 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1920 struct strbuf sb = STRBUF_INIT;
1921 if (argc != 3)
1922 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1924 printf("%s", relative_path(argv[1], argv[2], &sb));
1925 strbuf_release(&sb);
1926 return 0;
1929 static const char *remote_submodule_branch(const char *path)
1931 const struct submodule *sub;
1932 const char *branch = NULL;
1933 char *key;
1935 sub = submodule_from_path(the_repository, &null_oid, path);
1936 if (!sub)
1937 return NULL;
1939 key = xstrfmt("submodule.%s.branch", sub->name);
1940 if (repo_config_get_string_const(the_repository, key, &branch))
1941 branch = sub->branch;
1942 free(key);
1944 if (!branch)
1945 return "master";
1947 if (!strcmp(branch, ".")) {
1948 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1950 if (!refname)
1951 die(_("No such ref: %s"), "HEAD");
1953 /* detached HEAD */
1954 if (!strcmp(refname, "HEAD"))
1955 die(_("Submodule (%s) branch configured to inherit "
1956 "branch from superproject, but the superproject "
1957 "is not on any branch"), sub->name);
1959 if (!skip_prefix(refname, "refs/heads/", &refname))
1960 die(_("Expecting a full ref name, got %s"), refname);
1961 return refname;
1964 return branch;
1967 static int resolve_remote_submodule_branch(int argc, const char **argv,
1968 const char *prefix)
1970 const char *ret;
1971 struct strbuf sb = STRBUF_INIT;
1972 if (argc != 2)
1973 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1975 ret = remote_submodule_branch(argv[1]);
1976 if (!ret)
1977 die("submodule %s doesn't exist", argv[1]);
1979 printf("%s", ret);
1980 strbuf_release(&sb);
1981 return 0;
1984 static int push_check(int argc, const char **argv, const char *prefix)
1986 struct remote *remote;
1987 const char *superproject_head;
1988 char *head;
1989 int detached_head = 0;
1990 struct object_id head_oid;
1992 if (argc < 3)
1993 die("submodule--helper push-check requires at least 2 arguments");
1996 * superproject's resolved head ref.
1997 * if HEAD then the superproject is in a detached head state, otherwise
1998 * it will be the resolved head ref.
2000 superproject_head = argv[1];
2001 argv++;
2002 argc--;
2003 /* Get the submodule's head ref and determine if it is detached */
2004 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2005 if (!head)
2006 die(_("Failed to resolve HEAD as a valid ref."));
2007 if (!strcmp(head, "HEAD"))
2008 detached_head = 1;
2011 * The remote must be configured.
2012 * This is to avoid pushing to the exact same URL as the parent.
2014 remote = pushremote_get(argv[1]);
2015 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2016 die("remote '%s' not configured", argv[1]);
2018 /* Check the refspec */
2019 if (argc > 2) {
2020 int i;
2021 struct ref *local_refs = get_local_heads();
2022 struct refspec refspec = REFSPEC_INIT_PUSH;
2024 refspec_appendn(&refspec, argv + 2, argc - 2);
2026 for (i = 0; i < refspec.nr; i++) {
2027 const struct refspec_item *rs = &refspec.items[i];
2029 if (rs->pattern || rs->matching)
2030 continue;
2032 /* LHS must match a single ref */
2033 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2034 case 1:
2035 break;
2036 case 0:
2038 * If LHS matches 'HEAD' then we need to ensure
2039 * that it matches the same named branch
2040 * checked out in the superproject.
2042 if (!strcmp(rs->src, "HEAD")) {
2043 if (!detached_head &&
2044 !strcmp(head, superproject_head))
2045 break;
2046 die("HEAD does not match the named branch in the superproject");
2048 /* fallthrough */
2049 default:
2050 die("src refspec '%s' must name a ref",
2051 rs->src);
2054 refspec_clear(&refspec);
2056 free(head);
2058 return 0;
2061 static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
2063 const struct submodule *sub;
2064 const char *path;
2065 char *cw;
2066 struct repository subrepo;
2068 if (argc != 2)
2069 BUG("submodule--helper ensure-core-worktree <path>");
2071 path = argv[1];
2073 sub = submodule_from_path(the_repository, &null_oid, path);
2074 if (!sub)
2075 BUG("We could get the submodule handle before?");
2077 if (repo_submodule_init(&subrepo, the_repository, sub))
2078 die(_("could not get a repository handle for submodule '%s'"), path);
2080 if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
2081 char *cfg_file, *abs_path;
2082 const char *rel_path;
2083 struct strbuf sb = STRBUF_INIT;
2085 cfg_file = repo_git_path(&subrepo, "config");
2087 abs_path = absolute_pathdup(path);
2088 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2090 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2092 free(cfg_file);
2093 free(abs_path);
2094 strbuf_release(&sb);
2097 return 0;
2100 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2102 int i;
2103 struct pathspec pathspec;
2104 struct module_list list = MODULE_LIST_INIT;
2105 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2107 struct option embed_gitdir_options[] = {
2108 OPT_STRING(0, "prefix", &prefix,
2109 N_("path"),
2110 N_("path into the working tree")),
2111 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2112 ABSORB_GITDIR_RECURSE_SUBMODULES),
2113 OPT_END()
2116 const char *const git_submodule_helper_usage[] = {
2117 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2118 NULL
2121 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2122 git_submodule_helper_usage, 0);
2124 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2125 return 1;
2127 for (i = 0; i < list.nr; i++)
2128 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2130 return 0;
2133 static int is_active(int argc, const char **argv, const char *prefix)
2135 if (argc != 2)
2136 die("submodule--helper is-active takes exactly 1 argument");
2138 return !is_submodule_active(the_repository, argv[1]);
2142 * Exit non-zero if any of the submodule names given on the command line is
2143 * invalid. If no names are given, filter stdin to print only valid names
2144 * (which is primarily intended for testing).
2146 static int check_name(int argc, const char **argv, const char *prefix)
2148 if (argc > 1) {
2149 while (*++argv) {
2150 if (check_submodule_name(*argv) < 0)
2151 return 1;
2153 } else {
2154 struct strbuf buf = STRBUF_INIT;
2155 while (strbuf_getline(&buf, stdin) != EOF) {
2156 if (!check_submodule_name(buf.buf))
2157 printf("%s\n", buf.buf);
2159 strbuf_release(&buf);
2161 return 0;
2164 static int module_config(int argc, const char **argv, const char *prefix)
2166 enum {
2167 CHECK_WRITEABLE = 1,
2168 DO_UNSET = 2
2169 } command = 0;
2171 struct option module_config_options[] = {
2172 OPT_CMDMODE(0, "check-writeable", &command,
2173 N_("check if it is safe to write to the .gitmodules file"),
2174 CHECK_WRITEABLE),
2175 OPT_CMDMODE(0, "unset", &command,
2176 N_("unset the config in the .gitmodules file"),
2177 DO_UNSET),
2178 OPT_END()
2180 const char *const git_submodule_helper_usage[] = {
2181 N_("git submodule--helper config <name> [<value>]"),
2182 N_("git submodule--helper config --unset <name>"),
2183 N_("git submodule--helper config --check-writeable"),
2184 NULL
2187 argc = parse_options(argc, argv, prefix, module_config_options,
2188 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2190 if (argc == 1 && command == CHECK_WRITEABLE)
2191 return is_writing_gitmodules_ok() ? 0 : -1;
2193 /* Equivalent to ACTION_GET in builtin/config.c */
2194 if (argc == 2 && command != DO_UNSET)
2195 return print_config_from_gitmodules(the_repository, argv[1]);
2197 /* Equivalent to ACTION_SET in builtin/config.c */
2198 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2199 const char *value = (argc == 3) ? argv[2] : NULL;
2201 if (!is_writing_gitmodules_ok())
2202 die(_("please make sure that the .gitmodules file is in the working tree"));
2204 return config_set_in_gitmodules_file_gently(argv[1], value);
2207 usage_with_options(git_submodule_helper_usage, module_config_options);
2210 #define SUPPORT_SUPER_PREFIX (1<<0)
2212 struct cmd_struct {
2213 const char *cmd;
2214 int (*fn)(int, const char **, const char *);
2215 unsigned option;
2218 static struct cmd_struct commands[] = {
2219 {"list", module_list, 0},
2220 {"name", module_name, 0},
2221 {"clone", module_clone, 0},
2222 {"update-module-mode", module_update_module_mode, 0},
2223 {"update-clone", update_clone, 0},
2224 {"ensure-core-worktree", ensure_core_worktree, 0},
2225 {"relative-path", resolve_relative_path, 0},
2226 {"resolve-relative-url", resolve_relative_url, 0},
2227 {"resolve-relative-url-test", resolve_relative_url_test, 0},
2228 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
2229 {"init", module_init, SUPPORT_SUPER_PREFIX},
2230 {"status", module_status, SUPPORT_SUPER_PREFIX},
2231 {"print-default-remote", print_default_remote, 0},
2232 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
2233 {"deinit", module_deinit, 0},
2234 {"remote-branch", resolve_remote_submodule_branch, 0},
2235 {"push-check", push_check, 0},
2236 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
2237 {"is-active", is_active, 0},
2238 {"check-name", check_name, 0},
2239 {"config", module_config, 0},
2242 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2244 int i;
2245 if (argc < 2 || !strcmp(argv[1], "-h"))
2246 usage("git submodule--helper <command>");
2248 for (i = 0; i < ARRAY_SIZE(commands); i++) {
2249 if (!strcmp(argv[1], commands[i].cmd)) {
2250 if (get_super_prefix() &&
2251 !(commands[i].option & SUPPORT_SUPER_PREFIX))
2252 die(_("%s doesn't support --super-prefix"),
2253 commands[i].cmd);
2254 return commands[i].fn(argc - 1, argv + 1, prefix);
2258 die(_("'%s' is not a valid submodule--helper "
2259 "subcommand"), argv[1]);