Comment important codepaths regarding nuking untracked files/dirs
[git/debian.git] / builtin / submodule--helper.c
blob549129bc1bfbd0b0d382f13e585f9a74b2f389dc
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 "advice.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);
191 if (!up_path || !is_relative) {
192 strbuf_release(&sb);
193 return out;
196 strbuf_reset(&sb);
197 strbuf_addf(&sb, "%s%s", up_path, out);
198 free(out);
199 return strbuf_detach(&sb, NULL);
202 static int resolve_relative_url(int argc, const char **argv, const char *prefix)
204 char *remoteurl = NULL;
205 char *remote = get_default_remote();
206 const char *up_path = NULL;
207 char *res;
208 const char *url;
209 struct strbuf sb = STRBUF_INIT;
211 if (argc != 2 && argc != 3)
212 die("resolve-relative-url only accepts one or two arguments");
214 url = argv[1];
215 strbuf_addf(&sb, "remote.%s.url", remote);
216 free(remote);
218 if (git_config_get_string(sb.buf, &remoteurl))
219 /* the repository is its own authoritative upstream */
220 remoteurl = xgetcwd();
222 if (argc == 3)
223 up_path = argv[2];
225 res = relative_url(remoteurl, url, up_path);
226 puts(res);
227 free(res);
228 free(remoteurl);
229 return 0;
232 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
234 char *remoteurl, *res;
235 const char *up_path, *url;
237 if (argc != 4)
238 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
240 up_path = argv[1];
241 remoteurl = xstrdup(argv[2]);
242 url = argv[3];
244 if (!strcmp(up_path, "(null)"))
245 up_path = NULL;
247 res = relative_url(remoteurl, url, up_path);
248 puts(res);
249 free(res);
250 free(remoteurl);
251 return 0;
254 /* the result should be freed by the caller. */
255 static char *get_submodule_displaypath(const char *path, const char *prefix)
257 const char *super_prefix = get_super_prefix();
259 if (prefix && super_prefix) {
260 BUG("cannot have prefix '%s' and superprefix '%s'",
261 prefix, super_prefix);
262 } else if (prefix) {
263 struct strbuf sb = STRBUF_INIT;
264 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
265 strbuf_release(&sb);
266 return displaypath;
267 } else if (super_prefix) {
268 return xstrfmt("%s%s", super_prefix, path);
269 } else {
270 return xstrdup(path);
274 static char *compute_rev_name(const char *sub_path, const char* object_id)
276 struct strbuf sb = STRBUF_INIT;
277 const char ***d;
279 static const char *describe_bare[] = { NULL };
281 static const char *describe_tags[] = { "--tags", NULL };
283 static const char *describe_contains[] = { "--contains", NULL };
285 static const char *describe_all_always[] = { "--all", "--always", NULL };
287 static const char **describe_argv[] = { describe_bare, describe_tags,
288 describe_contains,
289 describe_all_always, NULL };
291 for (d = describe_argv; *d; d++) {
292 struct child_process cp = CHILD_PROCESS_INIT;
293 prepare_submodule_repo_env(&cp.env_array);
294 cp.dir = sub_path;
295 cp.git_cmd = 1;
296 cp.no_stderr = 1;
298 strvec_push(&cp.args, "describe");
299 strvec_pushv(&cp.args, *d);
300 strvec_push(&cp.args, object_id);
302 if (!capture_command(&cp, &sb, 0)) {
303 strbuf_strip_suffix(&sb, "\n");
304 return strbuf_detach(&sb, NULL);
308 strbuf_release(&sb);
309 return NULL;
312 struct module_list {
313 const struct cache_entry **entries;
314 int alloc, nr;
316 #define MODULE_LIST_INIT { NULL, 0, 0 }
318 static int module_list_compute(int argc, const char **argv,
319 const char *prefix,
320 struct pathspec *pathspec,
321 struct module_list *list)
323 int i, result = 0;
324 char *ps_matched = NULL;
325 parse_pathspec(pathspec, 0,
326 PATHSPEC_PREFER_FULL,
327 prefix, argv);
329 if (pathspec->nr)
330 ps_matched = xcalloc(pathspec->nr, 1);
332 if (read_cache() < 0)
333 die(_("index file corrupt"));
335 for (i = 0; i < active_nr; i++) {
336 const struct cache_entry *ce = active_cache[i];
338 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
339 0, ps_matched, 1) ||
340 !S_ISGITLINK(ce->ce_mode))
341 continue;
343 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
344 list->entries[list->nr++] = ce;
345 while (i + 1 < active_nr &&
346 !strcmp(ce->name, active_cache[i + 1]->name))
348 * Skip entries with the same name in different stages
349 * to make sure an entry is returned only once.
351 i++;
354 if (ps_matched && report_path_error(ps_matched, pathspec))
355 result = -1;
357 free(ps_matched);
359 return result;
362 static void module_list_active(struct module_list *list)
364 int i;
365 struct module_list active_modules = MODULE_LIST_INIT;
367 for (i = 0; i < list->nr; i++) {
368 const struct cache_entry *ce = list->entries[i];
370 if (!is_submodule_active(the_repository, ce->name))
371 continue;
373 ALLOC_GROW(active_modules.entries,
374 active_modules.nr + 1,
375 active_modules.alloc);
376 active_modules.entries[active_modules.nr++] = ce;
379 free(list->entries);
380 *list = active_modules;
383 static char *get_up_path(const char *path)
385 int i;
386 struct strbuf sb = STRBUF_INIT;
388 for (i = count_slashes(path); i; i--)
389 strbuf_addstr(&sb, "../");
392 * Check if 'path' ends with slash or not
393 * for having the same output for dir/sub_dir
394 * and dir/sub_dir/
396 if (!is_dir_sep(path[strlen(path) - 1]))
397 strbuf_addstr(&sb, "../");
399 return strbuf_detach(&sb, NULL);
402 static int module_list(int argc, const char **argv, const char *prefix)
404 int i;
405 struct pathspec pathspec;
406 struct module_list list = MODULE_LIST_INIT;
408 struct option module_list_options[] = {
409 OPT_STRING(0, "prefix", &prefix,
410 N_("path"),
411 N_("alternative anchor for relative paths")),
412 OPT_END()
415 const char *const git_submodule_helper_usage[] = {
416 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
417 NULL
420 argc = parse_options(argc, argv, prefix, module_list_options,
421 git_submodule_helper_usage, 0);
423 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
424 return 1;
426 for (i = 0; i < list.nr; i++) {
427 const struct cache_entry *ce = list.entries[i];
429 if (ce_stage(ce))
430 printf("%06o %s U\t", ce->ce_mode,
431 oid_to_hex(null_oid()));
432 else
433 printf("%06o %s %d\t", ce->ce_mode,
434 oid_to_hex(&ce->oid), ce_stage(ce));
436 fprintf(stdout, "%s\n", ce->name);
438 return 0;
441 static void for_each_listed_submodule(const struct module_list *list,
442 each_submodule_fn fn, void *cb_data)
444 int i;
445 for (i = 0; i < list->nr; i++)
446 fn(list->entries[i], cb_data);
449 struct foreach_cb {
450 int argc;
451 const char **argv;
452 const char *prefix;
453 int quiet;
454 int recursive;
456 #define FOREACH_CB_INIT { 0 }
458 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
459 void *cb_data)
461 struct foreach_cb *info = cb_data;
462 const char *path = list_item->name;
463 const struct object_id *ce_oid = &list_item->oid;
465 const struct submodule *sub;
466 struct child_process cp = CHILD_PROCESS_INIT;
467 char *displaypath;
469 displaypath = get_submodule_displaypath(path, info->prefix);
471 sub = submodule_from_path(the_repository, null_oid(), path);
473 if (!sub)
474 die(_("No url found for submodule path '%s' in .gitmodules"),
475 displaypath);
477 if (!is_submodule_populated_gently(path, NULL))
478 goto cleanup;
480 prepare_submodule_repo_env(&cp.env_array);
483 * For the purpose of executing <command> in the submodule,
484 * separate shell is used for the purpose of running the
485 * child process.
487 cp.use_shell = 1;
488 cp.dir = path;
491 * NEEDSWORK: the command currently has access to the variables $name,
492 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
493 * contains a single argument. This is done for maintaining a faithful
494 * translation from shell script.
496 if (info->argc == 1) {
497 char *toplevel = xgetcwd();
498 struct strbuf sb = STRBUF_INIT;
500 strvec_pushf(&cp.env_array, "name=%s", sub->name);
501 strvec_pushf(&cp.env_array, "sm_path=%s", path);
502 strvec_pushf(&cp.env_array, "displaypath=%s", displaypath);
503 strvec_pushf(&cp.env_array, "sha1=%s",
504 oid_to_hex(ce_oid));
505 strvec_pushf(&cp.env_array, "toplevel=%s", toplevel);
508 * Since the path variable was accessible from the script
509 * before porting, it is also made available after porting.
510 * The environment variable "PATH" has a very special purpose
511 * on windows. And since environment variables are
512 * case-insensitive in windows, it interferes with the
513 * existing PATH variable. Hence, to avoid that, we expose
514 * path via the args strvec and not via env_array.
516 sq_quote_buf(&sb, path);
517 strvec_pushf(&cp.args, "path=%s; %s",
518 sb.buf, info->argv[0]);
519 strbuf_release(&sb);
520 free(toplevel);
521 } else {
522 strvec_pushv(&cp.args, info->argv);
525 if (!info->quiet)
526 printf(_("Entering '%s'\n"), displaypath);
528 if (info->argv[0] && run_command(&cp))
529 die(_("run_command returned non-zero status for %s\n."),
530 displaypath);
532 if (info->recursive) {
533 struct child_process cpr = CHILD_PROCESS_INIT;
535 cpr.git_cmd = 1;
536 cpr.dir = path;
537 prepare_submodule_repo_env(&cpr.env_array);
539 strvec_pushl(&cpr.args, "--super-prefix", NULL);
540 strvec_pushf(&cpr.args, "%s/", displaypath);
541 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
542 NULL);
544 if (info->quiet)
545 strvec_push(&cpr.args, "--quiet");
547 strvec_push(&cpr.args, "--");
548 strvec_pushv(&cpr.args, info->argv);
550 if (run_command(&cpr))
551 die(_("run_command returned non-zero status while "
552 "recursing in the nested submodules of %s\n."),
553 displaypath);
556 cleanup:
557 free(displaypath);
560 static int module_foreach(int argc, const char **argv, const char *prefix)
562 struct foreach_cb info = FOREACH_CB_INIT;
563 struct pathspec pathspec;
564 struct module_list list = MODULE_LIST_INIT;
566 struct option module_foreach_options[] = {
567 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
568 OPT_BOOL(0, "recursive", &info.recursive,
569 N_("recurse into nested submodules")),
570 OPT_END()
573 const char *const git_submodule_helper_usage[] = {
574 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
575 NULL
578 argc = parse_options(argc, argv, prefix, module_foreach_options,
579 git_submodule_helper_usage, 0);
581 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
582 return 1;
584 info.argc = argc;
585 info.argv = argv;
586 info.prefix = prefix;
588 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
590 return 0;
593 static char *compute_submodule_clone_url(const char *rel_url)
595 char *remoteurl, *relurl;
596 char *remote = get_default_remote();
597 struct strbuf remotesb = STRBUF_INIT;
599 strbuf_addf(&remotesb, "remote.%s.url", remote);
600 if (git_config_get_string(remotesb.buf, &remoteurl)) {
601 warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
602 remoteurl = xgetcwd();
604 relurl = relative_url(remoteurl, rel_url, NULL);
606 free(remote);
607 free(remoteurl);
608 strbuf_release(&remotesb);
610 return relurl;
613 struct init_cb {
614 const char *prefix;
615 unsigned int flags;
617 #define INIT_CB_INIT { NULL, 0 }
619 static void init_submodule(const char *path, const char *prefix,
620 unsigned int flags)
622 const struct submodule *sub;
623 struct strbuf sb = STRBUF_INIT;
624 char *upd = NULL, *url = NULL, *displaypath;
626 displaypath = get_submodule_displaypath(path, prefix);
628 sub = submodule_from_path(the_repository, null_oid(), path);
630 if (!sub)
631 die(_("No url found for submodule path '%s' in .gitmodules"),
632 displaypath);
635 * NEEDSWORK: In a multi-working-tree world, this needs to be
636 * set in the per-worktree config.
638 * Set active flag for the submodule being initialized
640 if (!is_submodule_active(the_repository, path)) {
641 strbuf_addf(&sb, "submodule.%s.active", sub->name);
642 git_config_set_gently(sb.buf, "true");
643 strbuf_reset(&sb);
647 * Copy url setting when it is not set yet.
648 * To look up the url in .git/config, we must not fall back to
649 * .gitmodules, so look it up directly.
651 strbuf_addf(&sb, "submodule.%s.url", sub->name);
652 if (git_config_get_string(sb.buf, &url)) {
653 if (!sub->url)
654 die(_("No url found for submodule path '%s' in .gitmodules"),
655 displaypath);
657 url = xstrdup(sub->url);
659 /* Possibly a url relative to parent */
660 if (starts_with_dot_dot_slash(url) ||
661 starts_with_dot_slash(url)) {
662 char *oldurl = url;
663 url = compute_submodule_clone_url(oldurl);
664 free(oldurl);
667 if (git_config_set_gently(sb.buf, url))
668 die(_("Failed to register url for submodule path '%s'"),
669 displaypath);
670 if (!(flags & OPT_QUIET))
671 fprintf(stderr,
672 _("Submodule '%s' (%s) registered for path '%s'\n"),
673 sub->name, url, displaypath);
675 strbuf_reset(&sb);
677 /* Copy "update" setting when it is not set yet */
678 strbuf_addf(&sb, "submodule.%s.update", sub->name);
679 if (git_config_get_string(sb.buf, &upd) &&
680 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
681 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
682 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
683 sub->name);
684 upd = xstrdup("none");
685 } else
686 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
688 if (git_config_set_gently(sb.buf, upd))
689 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
691 strbuf_release(&sb);
692 free(displaypath);
693 free(url);
694 free(upd);
697 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
699 struct init_cb *info = cb_data;
700 init_submodule(list_item->name, info->prefix, info->flags);
703 static int module_init(int argc, const char **argv, const char *prefix)
705 struct init_cb info = INIT_CB_INIT;
706 struct pathspec pathspec;
707 struct module_list list = MODULE_LIST_INIT;
708 int quiet = 0;
710 struct option module_init_options[] = {
711 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
712 OPT_END()
715 const char *const git_submodule_helper_usage[] = {
716 N_("git submodule--helper init [<options>] [<path>]"),
717 NULL
720 argc = parse_options(argc, argv, prefix, module_init_options,
721 git_submodule_helper_usage, 0);
723 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
724 return 1;
727 * If there are no path args and submodule.active is set then,
728 * by default, only initialize 'active' modules.
730 if (!argc && git_config_get_value_multi("submodule.active"))
731 module_list_active(&list);
733 info.prefix = prefix;
734 if (quiet)
735 info.flags |= OPT_QUIET;
737 for_each_listed_submodule(&list, init_submodule_cb, &info);
739 return 0;
742 struct status_cb {
743 const char *prefix;
744 unsigned int flags;
746 #define STATUS_CB_INIT { NULL, 0 }
748 static void print_status(unsigned int flags, char state, const char *path,
749 const struct object_id *oid, const char *displaypath)
751 if (flags & OPT_QUIET)
752 return;
754 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
756 if (state == ' ' || state == '+') {
757 const char *name = compute_rev_name(path, oid_to_hex(oid));
759 if (name)
760 printf(" (%s)", name);
763 printf("\n");
766 static int handle_submodule_head_ref(const char *refname,
767 const struct object_id *oid, int flags,
768 void *cb_data)
770 struct object_id *output = cb_data;
771 if (oid)
772 oidcpy(output, oid);
774 return 0;
777 static void status_submodule(const char *path, const struct object_id *ce_oid,
778 unsigned int ce_flags, const char *prefix,
779 unsigned int flags)
781 char *displaypath;
782 struct strvec diff_files_args = STRVEC_INIT;
783 struct rev_info rev;
784 int diff_files_result;
785 struct strbuf buf = STRBUF_INIT;
786 const char *git_dir;
788 if (!submodule_from_path(the_repository, null_oid(), path))
789 die(_("no submodule mapping found in .gitmodules for path '%s'"),
790 path);
792 displaypath = get_submodule_displaypath(path, prefix);
794 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
795 print_status(flags, 'U', path, null_oid(), displaypath);
796 goto cleanup;
799 strbuf_addf(&buf, "%s/.git", path);
800 git_dir = read_gitfile(buf.buf);
801 if (!git_dir)
802 git_dir = buf.buf;
804 if (!is_submodule_active(the_repository, path) ||
805 !is_git_directory(git_dir)) {
806 print_status(flags, '-', path, ce_oid, displaypath);
807 strbuf_release(&buf);
808 goto cleanup;
810 strbuf_release(&buf);
812 strvec_pushl(&diff_files_args, "diff-files",
813 "--ignore-submodules=dirty", "--quiet", "--",
814 path, NULL);
816 git_config(git_diff_basic_config, NULL);
818 repo_init_revisions(the_repository, &rev, NULL);
819 rev.abbrev = 0;
820 diff_files_args.nr = setup_revisions(diff_files_args.nr,
821 diff_files_args.v,
822 &rev, NULL);
823 diff_files_result = run_diff_files(&rev, 0);
825 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
826 print_status(flags, ' ', path, ce_oid,
827 displaypath);
828 } else if (!(flags & OPT_CACHED)) {
829 struct object_id oid;
830 struct ref_store *refs = get_submodule_ref_store(path);
832 if (!refs) {
833 print_status(flags, '-', path, ce_oid, displaypath);
834 goto cleanup;
836 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
837 die(_("could not resolve HEAD ref inside the "
838 "submodule '%s'"), path);
840 print_status(flags, '+', path, &oid, displaypath);
841 } else {
842 print_status(flags, '+', path, ce_oid, displaypath);
845 if (flags & OPT_RECURSIVE) {
846 struct child_process cpr = CHILD_PROCESS_INIT;
848 cpr.git_cmd = 1;
849 cpr.dir = path;
850 prepare_submodule_repo_env(&cpr.env_array);
852 strvec_push(&cpr.args, "--super-prefix");
853 strvec_pushf(&cpr.args, "%s/", displaypath);
854 strvec_pushl(&cpr.args, "submodule--helper", "status",
855 "--recursive", NULL);
857 if (flags & OPT_CACHED)
858 strvec_push(&cpr.args, "--cached");
860 if (flags & OPT_QUIET)
861 strvec_push(&cpr.args, "--quiet");
863 if (run_command(&cpr))
864 die(_("failed to recurse into submodule '%s'"), path);
867 cleanup:
868 strvec_clear(&diff_files_args);
869 free(displaypath);
872 static void status_submodule_cb(const struct cache_entry *list_item,
873 void *cb_data)
875 struct status_cb *info = cb_data;
876 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
877 info->prefix, info->flags);
880 static int module_status(int argc, const char **argv, const char *prefix)
882 struct status_cb info = STATUS_CB_INIT;
883 struct pathspec pathspec;
884 struct module_list list = MODULE_LIST_INIT;
885 int quiet = 0;
887 struct option module_status_options[] = {
888 OPT__QUIET(&quiet, N_("suppress submodule status output")),
889 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
890 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
891 OPT_END()
894 const char *const git_submodule_helper_usage[] = {
895 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
896 NULL
899 argc = parse_options(argc, argv, prefix, module_status_options,
900 git_submodule_helper_usage, 0);
902 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
903 return 1;
905 info.prefix = prefix;
906 if (quiet)
907 info.flags |= OPT_QUIET;
909 for_each_listed_submodule(&list, status_submodule_cb, &info);
911 return 0;
914 static int module_name(int argc, const char **argv, const char *prefix)
916 const struct submodule *sub;
918 if (argc != 2)
919 usage(_("git submodule--helper name <path>"));
921 sub = submodule_from_path(the_repository, null_oid(), argv[1]);
923 if (!sub)
924 die(_("no submodule mapping found in .gitmodules for path '%s'"),
925 argv[1]);
927 printf("%s\n", sub->name);
929 return 0;
932 struct module_cb {
933 unsigned int mod_src;
934 unsigned int mod_dst;
935 struct object_id oid_src;
936 struct object_id oid_dst;
937 char status;
938 const char *sm_path;
940 #define MODULE_CB_INIT { 0, 0, NULL, NULL, '\0', NULL }
942 struct module_cb_list {
943 struct module_cb **entries;
944 int alloc, nr;
946 #define MODULE_CB_LIST_INIT { NULL, 0, 0 }
948 struct summary_cb {
949 int argc;
950 const char **argv;
951 const char *prefix;
952 unsigned int cached: 1;
953 unsigned int for_status: 1;
954 unsigned int files: 1;
955 int summary_limit;
957 #define SUMMARY_CB_INIT { 0, NULL, NULL, 0, 0, 0, 0 }
959 enum diff_cmd {
960 DIFF_INDEX,
961 DIFF_FILES
964 static char *verify_submodule_committish(const char *sm_path,
965 const char *committish)
967 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
968 struct strbuf result = STRBUF_INIT;
970 cp_rev_parse.git_cmd = 1;
971 cp_rev_parse.dir = sm_path;
972 prepare_submodule_repo_env(&cp_rev_parse.env_array);
973 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
974 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
975 strvec_push(&cp_rev_parse.args, "--");
977 if (capture_command(&cp_rev_parse, &result, 0))
978 return NULL;
980 strbuf_trim_trailing_newline(&result);
981 return strbuf_detach(&result, NULL);
984 static void print_submodule_summary(struct summary_cb *info, char *errmsg,
985 int total_commits, const char *displaypath,
986 const char *src_abbrev, const char *dst_abbrev,
987 struct module_cb *p)
989 if (p->status == 'T') {
990 if (S_ISGITLINK(p->mod_dst))
991 printf(_("* %s %s(blob)->%s(submodule)"),
992 displaypath, src_abbrev, dst_abbrev);
993 else
994 printf(_("* %s %s(submodule)->%s(blob)"),
995 displaypath, src_abbrev, dst_abbrev);
996 } else {
997 printf("* %s %s...%s",
998 displaypath, src_abbrev, dst_abbrev);
1001 if (total_commits < 0)
1002 printf(":\n");
1003 else
1004 printf(" (%d):\n", total_commits);
1006 if (errmsg) {
1007 printf(_("%s"), errmsg);
1008 } else if (total_commits > 0) {
1009 struct child_process cp_log = CHILD_PROCESS_INIT;
1011 cp_log.git_cmd = 1;
1012 cp_log.dir = p->sm_path;
1013 prepare_submodule_repo_env(&cp_log.env_array);
1014 strvec_pushl(&cp_log.args, "log", NULL);
1016 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
1017 if (info->summary_limit > 0)
1018 strvec_pushf(&cp_log.args, "-%d",
1019 info->summary_limit);
1021 strvec_pushl(&cp_log.args, "--pretty= %m %s",
1022 "--first-parent", NULL);
1023 strvec_pushf(&cp_log.args, "%s...%s",
1024 src_abbrev, dst_abbrev);
1025 } else if (S_ISGITLINK(p->mod_dst)) {
1026 strvec_pushl(&cp_log.args, "--pretty= > %s",
1027 "-1", dst_abbrev, NULL);
1028 } else {
1029 strvec_pushl(&cp_log.args, "--pretty= < %s",
1030 "-1", src_abbrev, NULL);
1032 run_command(&cp_log);
1034 printf("\n");
1037 static void generate_submodule_summary(struct summary_cb *info,
1038 struct module_cb *p)
1040 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
1041 int missing_src = 0, missing_dst = 0;
1042 char *errmsg = NULL;
1043 int total_commits = -1;
1045 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
1046 if (S_ISGITLINK(p->mod_dst)) {
1047 struct ref_store *refs = get_submodule_ref_store(p->sm_path);
1048 if (refs)
1049 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
1050 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
1051 struct stat st;
1052 int fd = open(p->sm_path, O_RDONLY);
1054 if (fd < 0 || fstat(fd, &st) < 0 ||
1055 index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
1056 p->sm_path, 0))
1057 error(_("couldn't hash object from '%s'"), p->sm_path);
1058 } else {
1059 /* for a submodule removal (mode:0000000), don't warn */
1060 if (p->mod_dst)
1061 warning(_("unexpected mode %o\n"), p->mod_dst);
1065 if (S_ISGITLINK(p->mod_src)) {
1066 if (p->status != 'D')
1067 src_abbrev = verify_submodule_committish(p->sm_path,
1068 oid_to_hex(&p->oid_src));
1069 if (!src_abbrev) {
1070 missing_src = 1;
1072 * As `rev-parse` failed, we fallback to getting
1073 * the abbreviated hash using oid_src. We do
1074 * this as we might still need the abbreviated
1075 * hash in cases like a submodule type change, etc.
1077 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
1079 } else {
1081 * The source does not point to a submodule.
1082 * So, we fallback to getting the abbreviation using
1083 * oid_src as we might still need the abbreviated
1084 * hash in cases like submodule add, etc.
1086 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
1089 if (S_ISGITLINK(p->mod_dst)) {
1090 dst_abbrev = verify_submodule_committish(p->sm_path,
1091 oid_to_hex(&p->oid_dst));
1092 if (!dst_abbrev) {
1093 missing_dst = 1;
1095 * As `rev-parse` failed, we fallback to getting
1096 * the abbreviated hash using oid_dst. We do
1097 * this as we might still need the abbreviated
1098 * hash in cases like a submodule type change, etc.
1100 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
1102 } else {
1104 * The destination does not point to a submodule.
1105 * So, we fallback to getting the abbreviation using
1106 * oid_dst as we might still need the abbreviated
1107 * hash in cases like a submodule removal, etc.
1109 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
1112 displaypath = get_submodule_displaypath(p->sm_path, info->prefix);
1114 if (!missing_src && !missing_dst) {
1115 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
1116 struct strbuf sb_rev_list = STRBUF_INIT;
1118 strvec_pushl(&cp_rev_list.args, "rev-list",
1119 "--first-parent", "--count", NULL);
1120 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
1121 strvec_pushf(&cp_rev_list.args, "%s...%s",
1122 src_abbrev, dst_abbrev);
1123 else
1124 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
1125 src_abbrev : dst_abbrev);
1126 strvec_push(&cp_rev_list.args, "--");
1128 cp_rev_list.git_cmd = 1;
1129 cp_rev_list.dir = p->sm_path;
1130 prepare_submodule_repo_env(&cp_rev_list.env_array);
1132 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
1133 total_commits = atoi(sb_rev_list.buf);
1135 strbuf_release(&sb_rev_list);
1136 } else {
1138 * Don't give error msg for modification whose dst is not
1139 * submodule, i.e., deleted or changed to blob
1141 if (S_ISGITLINK(p->mod_dst)) {
1142 struct strbuf errmsg_str = STRBUF_INIT;
1143 if (missing_src && missing_dst) {
1144 strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commits %s and %s\n",
1145 displaypath, oid_to_hex(&p->oid_src),
1146 oid_to_hex(&p->oid_dst));
1147 } else {
1148 strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commit %s\n",
1149 displaypath, missing_src ?
1150 oid_to_hex(&p->oid_src) :
1151 oid_to_hex(&p->oid_dst));
1153 errmsg = strbuf_detach(&errmsg_str, NULL);
1157 print_submodule_summary(info, errmsg, total_commits,
1158 displaypath, src_abbrev,
1159 dst_abbrev, p);
1161 free(displaypath);
1162 free(src_abbrev);
1163 free(dst_abbrev);
1166 static void prepare_submodule_summary(struct summary_cb *info,
1167 struct module_cb_list *list)
1169 int i;
1170 for (i = 0; i < list->nr; i++) {
1171 const struct submodule *sub;
1172 struct module_cb *p = list->entries[i];
1173 struct strbuf sm_gitdir = STRBUF_INIT;
1175 if (p->status == 'D' || p->status == 'T') {
1176 generate_submodule_summary(info, p);
1177 continue;
1180 if (info->for_status && p->status != 'A' &&
1181 (sub = submodule_from_path(the_repository,
1182 null_oid(), p->sm_path))) {
1183 char *config_key = NULL;
1184 const char *value;
1185 int ignore_all = 0;
1187 config_key = xstrfmt("submodule.%s.ignore",
1188 sub->name);
1189 if (!git_config_get_string_tmp(config_key, &value))
1190 ignore_all = !strcmp(value, "all");
1191 else if (sub->ignore)
1192 ignore_all = !strcmp(sub->ignore, "all");
1194 free(config_key);
1195 if (ignore_all)
1196 continue;
1199 /* Also show added or modified modules which are checked out */
1200 strbuf_addstr(&sm_gitdir, p->sm_path);
1201 if (is_nonbare_repository_dir(&sm_gitdir))
1202 generate_submodule_summary(info, p);
1203 strbuf_release(&sm_gitdir);
1207 static void submodule_summary_callback(struct diff_queue_struct *q,
1208 struct diff_options *options,
1209 void *data)
1211 int i;
1212 struct module_cb_list *list = data;
1213 for (i = 0; i < q->nr; i++) {
1214 struct diff_filepair *p = q->queue[i];
1215 struct module_cb *temp;
1217 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1218 continue;
1219 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1220 temp->mod_src = p->one->mode;
1221 temp->mod_dst = p->two->mode;
1222 temp->oid_src = p->one->oid;
1223 temp->oid_dst = p->two->oid;
1224 temp->status = p->status;
1225 temp->sm_path = xstrdup(p->one->path);
1227 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1228 list->entries[list->nr++] = temp;
1232 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1234 switch (diff_cmd) {
1235 case DIFF_INDEX: return "diff-index";
1236 case DIFF_FILES: return "diff-files";
1237 default: BUG("bad diff_cmd value %d", diff_cmd);
1241 static int compute_summary_module_list(struct object_id *head_oid,
1242 struct summary_cb *info,
1243 enum diff_cmd diff_cmd)
1245 struct strvec diff_args = STRVEC_INIT;
1246 struct rev_info rev;
1247 struct module_cb_list list = MODULE_CB_LIST_INIT;
1249 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1250 if (info->cached)
1251 strvec_push(&diff_args, "--cached");
1252 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1253 if (head_oid)
1254 strvec_push(&diff_args, oid_to_hex(head_oid));
1255 strvec_push(&diff_args, "--");
1256 if (info->argc)
1257 strvec_pushv(&diff_args, info->argv);
1259 git_config(git_diff_basic_config, NULL);
1260 init_revisions(&rev, info->prefix);
1261 rev.abbrev = 0;
1262 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1263 setup_revisions(diff_args.nr, diff_args.v, &rev, NULL);
1264 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1265 rev.diffopt.format_callback = submodule_summary_callback;
1266 rev.diffopt.format_callback_data = &list;
1268 if (!info->cached) {
1269 if (diff_cmd == DIFF_INDEX)
1270 setup_work_tree();
1271 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1272 perror("read_cache_preload");
1273 return -1;
1275 } else if (read_cache() < 0) {
1276 perror("read_cache");
1277 return -1;
1280 if (diff_cmd == DIFF_INDEX)
1281 run_diff_index(&rev, info->cached);
1282 else
1283 run_diff_files(&rev, 0);
1284 prepare_submodule_summary(info, &list);
1285 strvec_clear(&diff_args);
1286 return 0;
1289 static int module_summary(int argc, const char **argv, const char *prefix)
1291 struct summary_cb info = SUMMARY_CB_INIT;
1292 int cached = 0;
1293 int for_status = 0;
1294 int files = 0;
1295 int summary_limit = -1;
1296 enum diff_cmd diff_cmd = DIFF_INDEX;
1297 struct object_id head_oid;
1298 int ret;
1300 struct option module_summary_options[] = {
1301 OPT_BOOL(0, "cached", &cached,
1302 N_("use the commit stored in the index instead of the submodule HEAD")),
1303 OPT_BOOL(0, "files", &files,
1304 N_("compare the commit in the index with that in the submodule HEAD")),
1305 OPT_BOOL(0, "for-status", &for_status,
1306 N_("skip submodules with 'ignore_config' value set to 'all'")),
1307 OPT_INTEGER('n', "summary-limit", &summary_limit,
1308 N_("limit the summary size")),
1309 OPT_END()
1312 const char *const git_submodule_helper_usage[] = {
1313 N_("git submodule--helper summary [<options>] [<commit>] [--] [<path>]"),
1314 NULL
1317 argc = parse_options(argc, argv, prefix, module_summary_options,
1318 git_submodule_helper_usage, 0);
1320 if (!summary_limit)
1321 return 0;
1323 if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) {
1324 if (argc) {
1325 argv++;
1326 argc--;
1328 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1329 /* before the first commit: compare with an empty tree */
1330 oidcpy(&head_oid, the_hash_algo->empty_tree);
1331 if (argc) {
1332 argv++;
1333 argc--;
1335 } else {
1336 if (get_oid("HEAD", &head_oid))
1337 die(_("could not fetch a revision for HEAD"));
1340 if (files) {
1341 if (cached)
1342 die(_("--cached and --files are mutually exclusive"));
1343 diff_cmd = DIFF_FILES;
1346 info.argc = argc;
1347 info.argv = argv;
1348 info.prefix = prefix;
1349 info.cached = !!cached;
1350 info.files = !!files;
1351 info.for_status = !!for_status;
1352 info.summary_limit = summary_limit;
1354 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1355 &info, diff_cmd);
1356 return ret;
1359 struct sync_cb {
1360 const char *prefix;
1361 unsigned int flags;
1363 #define SYNC_CB_INIT { NULL, 0 }
1365 static void sync_submodule(const char *path, const char *prefix,
1366 unsigned int flags)
1368 const struct submodule *sub;
1369 char *remote_key = NULL;
1370 char *sub_origin_url, *super_config_url, *displaypath;
1371 struct strbuf sb = STRBUF_INIT;
1372 struct child_process cp = CHILD_PROCESS_INIT;
1373 char *sub_config_path = NULL;
1375 if (!is_submodule_active(the_repository, path))
1376 return;
1378 sub = submodule_from_path(the_repository, null_oid(), path);
1380 if (sub && sub->url) {
1381 if (starts_with_dot_dot_slash(sub->url) ||
1382 starts_with_dot_slash(sub->url)) {
1383 char *remote_url, *up_path;
1384 char *remote = get_default_remote();
1385 strbuf_addf(&sb, "remote.%s.url", remote);
1387 if (git_config_get_string(sb.buf, &remote_url))
1388 remote_url = xgetcwd();
1390 up_path = get_up_path(path);
1391 sub_origin_url = relative_url(remote_url, sub->url, up_path);
1392 super_config_url = relative_url(remote_url, sub->url, NULL);
1394 free(remote);
1395 free(up_path);
1396 free(remote_url);
1397 } else {
1398 sub_origin_url = xstrdup(sub->url);
1399 super_config_url = xstrdup(sub->url);
1401 } else {
1402 sub_origin_url = xstrdup("");
1403 super_config_url = xstrdup("");
1406 displaypath = get_submodule_displaypath(path, prefix);
1408 if (!(flags & OPT_QUIET))
1409 printf(_("Synchronizing submodule url for '%s'\n"),
1410 displaypath);
1412 strbuf_reset(&sb);
1413 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1414 if (git_config_set_gently(sb.buf, super_config_url))
1415 die(_("failed to register url for submodule path '%s'"),
1416 displaypath);
1418 if (!is_submodule_populated_gently(path, NULL))
1419 goto cleanup;
1421 prepare_submodule_repo_env(&cp.env_array);
1422 cp.git_cmd = 1;
1423 cp.dir = path;
1424 strvec_pushl(&cp.args, "submodule--helper",
1425 "print-default-remote", NULL);
1427 strbuf_reset(&sb);
1428 if (capture_command(&cp, &sb, 0))
1429 die(_("failed to get the default remote for submodule '%s'"),
1430 path);
1432 strbuf_strip_suffix(&sb, "\n");
1433 remote_key = xstrfmt("remote.%s.url", sb.buf);
1435 strbuf_reset(&sb);
1436 submodule_to_gitdir(&sb, path);
1437 strbuf_addstr(&sb, "/config");
1439 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1440 die(_("failed to update remote for submodule '%s'"),
1441 path);
1443 if (flags & OPT_RECURSIVE) {
1444 struct child_process cpr = CHILD_PROCESS_INIT;
1446 cpr.git_cmd = 1;
1447 cpr.dir = path;
1448 prepare_submodule_repo_env(&cpr.env_array);
1450 strvec_push(&cpr.args, "--super-prefix");
1451 strvec_pushf(&cpr.args, "%s/", displaypath);
1452 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1453 "--recursive", NULL);
1455 if (flags & OPT_QUIET)
1456 strvec_push(&cpr.args, "--quiet");
1458 if (run_command(&cpr))
1459 die(_("failed to recurse into submodule '%s'"),
1460 path);
1463 cleanup:
1464 free(super_config_url);
1465 free(sub_origin_url);
1466 strbuf_release(&sb);
1467 free(remote_key);
1468 free(displaypath);
1469 free(sub_config_path);
1472 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1474 struct sync_cb *info = cb_data;
1475 sync_submodule(list_item->name, info->prefix, info->flags);
1478 static int module_sync(int argc, const char **argv, const char *prefix)
1480 struct sync_cb info = SYNC_CB_INIT;
1481 struct pathspec pathspec;
1482 struct module_list list = MODULE_LIST_INIT;
1483 int quiet = 0;
1484 int recursive = 0;
1486 struct option module_sync_options[] = {
1487 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1488 OPT_BOOL(0, "recursive", &recursive,
1489 N_("recurse into nested submodules")),
1490 OPT_END()
1493 const char *const git_submodule_helper_usage[] = {
1494 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1495 NULL
1498 argc = parse_options(argc, argv, prefix, module_sync_options,
1499 git_submodule_helper_usage, 0);
1501 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1502 return 1;
1504 info.prefix = prefix;
1505 if (quiet)
1506 info.flags |= OPT_QUIET;
1507 if (recursive)
1508 info.flags |= OPT_RECURSIVE;
1510 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1512 return 0;
1515 struct deinit_cb {
1516 const char *prefix;
1517 unsigned int flags;
1519 #define DEINIT_CB_INIT { NULL, 0 }
1521 static void deinit_submodule(const char *path, const char *prefix,
1522 unsigned int flags)
1524 const struct submodule *sub;
1525 char *displaypath = NULL;
1526 struct child_process cp_config = CHILD_PROCESS_INIT;
1527 struct strbuf sb_config = STRBUF_INIT;
1528 char *sub_git_dir = xstrfmt("%s/.git", path);
1530 sub = submodule_from_path(the_repository, null_oid(), path);
1532 if (!sub || !sub->name)
1533 goto cleanup;
1535 displaypath = get_submodule_displaypath(path, prefix);
1537 /* remove the submodule work tree (unless the user already did it) */
1538 if (is_directory(path)) {
1539 struct strbuf sb_rm = STRBUF_INIT;
1540 const char *format;
1543 * protect submodules containing a .git directory
1544 * NEEDSWORK: instead of dying, automatically call
1545 * absorbgitdirs and (possibly) warn.
1547 if (is_directory(sub_git_dir))
1548 die(_("Submodule work tree '%s' contains a .git "
1549 "directory (use 'rm -rf' if you really want "
1550 "to remove it including all of its history)"),
1551 displaypath);
1553 if (!(flags & OPT_FORCE)) {
1554 struct child_process cp_rm = CHILD_PROCESS_INIT;
1555 cp_rm.git_cmd = 1;
1556 strvec_pushl(&cp_rm.args, "rm", "-qn",
1557 path, NULL);
1559 if (run_command(&cp_rm))
1560 die(_("Submodule work tree '%s' contains local "
1561 "modifications; use '-f' to discard them"),
1562 displaypath);
1565 strbuf_addstr(&sb_rm, path);
1567 if (!remove_dir_recursively(&sb_rm, 0))
1568 format = _("Cleared directory '%s'\n");
1569 else
1570 format = _("Could not remove submodule work tree '%s'\n");
1572 if (!(flags & OPT_QUIET))
1573 printf(format, displaypath);
1575 submodule_unset_core_worktree(sub);
1577 strbuf_release(&sb_rm);
1580 if (mkdir(path, 0777))
1581 printf(_("could not create empty submodule directory %s"),
1582 displaypath);
1584 cp_config.git_cmd = 1;
1585 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1586 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1588 /* remove the .git/config entries (unless the user already did it) */
1589 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1590 char *sub_key = xstrfmt("submodule.%s", sub->name);
1592 * remove the whole section so we have a clean state when
1593 * the user later decides to init this submodule again
1595 git_config_rename_section_in_file(NULL, sub_key, NULL);
1596 if (!(flags & OPT_QUIET))
1597 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1598 sub->name, sub->url, displaypath);
1599 free(sub_key);
1602 cleanup:
1603 free(displaypath);
1604 free(sub_git_dir);
1605 strbuf_release(&sb_config);
1608 static void deinit_submodule_cb(const struct cache_entry *list_item,
1609 void *cb_data)
1611 struct deinit_cb *info = cb_data;
1612 deinit_submodule(list_item->name, info->prefix, info->flags);
1615 static int module_deinit(int argc, const char **argv, const char *prefix)
1617 struct deinit_cb info = DEINIT_CB_INIT;
1618 struct pathspec pathspec;
1619 struct module_list list = MODULE_LIST_INIT;
1620 int quiet = 0;
1621 int force = 0;
1622 int all = 0;
1624 struct option module_deinit_options[] = {
1625 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1626 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1627 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1628 OPT_END()
1631 const char *const git_submodule_helper_usage[] = {
1632 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1633 NULL
1636 argc = parse_options(argc, argv, prefix, module_deinit_options,
1637 git_submodule_helper_usage, 0);
1639 if (all && argc) {
1640 error("pathspec and --all are incompatible");
1641 usage_with_options(git_submodule_helper_usage,
1642 module_deinit_options);
1645 if (!argc && !all)
1646 die(_("Use '--all' if you really want to deinitialize all submodules"));
1648 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1649 return 1;
1651 info.prefix = prefix;
1652 if (quiet)
1653 info.flags |= OPT_QUIET;
1654 if (force)
1655 info.flags |= OPT_FORCE;
1657 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1659 return 0;
1662 struct module_clone_data {
1663 const char *prefix;
1664 const char *path;
1665 const char *name;
1666 const char *url;
1667 const char *depth;
1668 struct string_list reference;
1669 unsigned int quiet: 1;
1670 unsigned int progress: 1;
1671 unsigned int dissociate: 1;
1672 unsigned int require_init: 1;
1673 int single_branch;
1675 #define MODULE_CLONE_DATA_INIT { .reference = STRING_LIST_INIT_NODUP, .single_branch = -1 }
1677 struct submodule_alternate_setup {
1678 const char *submodule_name;
1679 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1680 SUBMODULE_ALTERNATE_ERROR_DIE,
1681 SUBMODULE_ALTERNATE_ERROR_INFO,
1682 SUBMODULE_ALTERNATE_ERROR_IGNORE
1683 } error_mode;
1684 struct string_list *reference;
1686 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1687 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1689 static const char alternate_error_advice[] = N_(
1690 "An alternate computed from a superproject's alternate is invalid.\n"
1691 "To allow Git to clone without an alternate in such a case, set\n"
1692 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1693 "'--reference-if-able' instead of '--reference'."
1696 static int add_possible_reference_from_superproject(
1697 struct object_directory *odb, void *sas_cb)
1699 struct submodule_alternate_setup *sas = sas_cb;
1700 size_t len;
1703 * If the alternate object store is another repository, try the
1704 * standard layout with .git/(modules/<name>)+/objects
1706 if (strip_suffix(odb->path, "/objects", &len)) {
1707 char *sm_alternate;
1708 struct strbuf sb = STRBUF_INIT;
1709 struct strbuf err = STRBUF_INIT;
1710 strbuf_add(&sb, odb->path, len);
1713 * We need to end the new path with '/' to mark it as a dir,
1714 * otherwise a submodule name containing '/' will be broken
1715 * as the last part of a missing submodule reference would
1716 * be taken as a file name.
1718 strbuf_addf(&sb, "/modules/%s/", sas->submodule_name);
1720 sm_alternate = compute_alternate_path(sb.buf, &err);
1721 if (sm_alternate) {
1722 string_list_append(sas->reference, xstrdup(sb.buf));
1723 free(sm_alternate);
1724 } else {
1725 switch (sas->error_mode) {
1726 case SUBMODULE_ALTERNATE_ERROR_DIE:
1727 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1728 advise(_(alternate_error_advice));
1729 die(_("submodule '%s' cannot add alternate: %s"),
1730 sas->submodule_name, err.buf);
1731 case SUBMODULE_ALTERNATE_ERROR_INFO:
1732 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1733 sas->submodule_name, err.buf);
1734 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1735 ; /* nothing */
1738 strbuf_release(&sb);
1741 return 0;
1744 static void prepare_possible_alternates(const char *sm_name,
1745 struct string_list *reference)
1747 char *sm_alternate = NULL, *error_strategy = NULL;
1748 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1750 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1751 if (!sm_alternate)
1752 return;
1754 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1756 if (!error_strategy)
1757 error_strategy = xstrdup("die");
1759 sas.submodule_name = sm_name;
1760 sas.reference = reference;
1761 if (!strcmp(error_strategy, "die"))
1762 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1763 else if (!strcmp(error_strategy, "info"))
1764 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1765 else if (!strcmp(error_strategy, "ignore"))
1766 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1767 else
1768 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1770 if (!strcmp(sm_alternate, "superproject"))
1771 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1772 else if (!strcmp(sm_alternate, "no"))
1773 ; /* do nothing */
1774 else
1775 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1777 free(sm_alternate);
1778 free(error_strategy);
1781 static int clone_submodule(struct module_clone_data *clone_data)
1783 char *p, *sm_gitdir;
1784 char *sm_alternate = NULL, *error_strategy = NULL;
1785 struct strbuf sb = STRBUF_INIT;
1786 struct child_process cp = CHILD_PROCESS_INIT;
1788 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), clone_data->name);
1789 sm_gitdir = absolute_pathdup(sb.buf);
1790 strbuf_reset(&sb);
1792 if (!is_absolute_path(clone_data->path)) {
1793 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), clone_data->path);
1794 clone_data->path = strbuf_detach(&sb, NULL);
1795 } else {
1796 clone_data->path = xstrdup(clone_data->path);
1799 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1800 die(_("refusing to create/use '%s' in another submodule's "
1801 "git dir"), sm_gitdir);
1803 if (!file_exists(sm_gitdir)) {
1804 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1805 die(_("could not create directory '%s'"), sm_gitdir);
1807 prepare_possible_alternates(clone_data->name, &clone_data->reference);
1809 strvec_push(&cp.args, "clone");
1810 strvec_push(&cp.args, "--no-checkout");
1811 if (clone_data->quiet)
1812 strvec_push(&cp.args, "--quiet");
1813 if (clone_data->progress)
1814 strvec_push(&cp.args, "--progress");
1815 if (clone_data->depth && *(clone_data->depth))
1816 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1817 if (clone_data->reference.nr) {
1818 struct string_list_item *item;
1819 for_each_string_list_item(item, &clone_data->reference)
1820 strvec_pushl(&cp.args, "--reference",
1821 item->string, NULL);
1823 if (clone_data->dissociate)
1824 strvec_push(&cp.args, "--dissociate");
1825 if (sm_gitdir && *sm_gitdir)
1826 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1827 if (clone_data->single_branch >= 0)
1828 strvec_push(&cp.args, clone_data->single_branch ?
1829 "--single-branch" :
1830 "--no-single-branch");
1832 strvec_push(&cp.args, "--");
1833 strvec_push(&cp.args, clone_data->url);
1834 strvec_push(&cp.args, clone_data->path);
1836 cp.git_cmd = 1;
1837 prepare_submodule_repo_env(&cp.env_array);
1838 cp.no_stdin = 1;
1840 if(run_command(&cp))
1841 die(_("clone of '%s' into submodule path '%s' failed"),
1842 clone_data->url, clone_data->path);
1843 } else {
1844 if (clone_data->require_init && !access(clone_data->path, X_OK) &&
1845 !is_empty_dir(clone_data->path))
1846 die(_("directory not empty: '%s'"), clone_data->path);
1847 if (safe_create_leading_directories_const(clone_data->path) < 0)
1848 die(_("could not create directory '%s'"), clone_data->path);
1849 strbuf_addf(&sb, "%s/index", sm_gitdir);
1850 unlink_or_warn(sb.buf);
1851 strbuf_reset(&sb);
1854 connect_work_tree_and_git_dir(clone_data->path, sm_gitdir, 0);
1856 p = git_pathdup_submodule(clone_data->path, "config");
1857 if (!p)
1858 die(_("could not get submodule directory for '%s'"), clone_data->path);
1860 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1861 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1862 if (sm_alternate)
1863 git_config_set_in_file(p, "submodule.alternateLocation",
1864 sm_alternate);
1865 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1866 if (error_strategy)
1867 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1868 error_strategy);
1870 free(sm_alternate);
1871 free(error_strategy);
1873 strbuf_release(&sb);
1874 free(sm_gitdir);
1875 free(p);
1876 return 0;
1879 static int module_clone(int argc, const char **argv, const char *prefix)
1881 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1882 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1884 struct option module_clone_options[] = {
1885 OPT_STRING(0, "prefix", &clone_data.prefix,
1886 N_("path"),
1887 N_("alternative anchor for relative paths")),
1888 OPT_STRING(0, "path", &clone_data.path,
1889 N_("path"),
1890 N_("where the new submodule will be cloned to")),
1891 OPT_STRING(0, "name", &clone_data.name,
1892 N_("string"),
1893 N_("name of the new submodule")),
1894 OPT_STRING(0, "url", &clone_data.url,
1895 N_("string"),
1896 N_("url where to clone the submodule from")),
1897 OPT_STRING_LIST(0, "reference", &clone_data.reference,
1898 N_("repo"),
1899 N_("reference repository")),
1900 OPT_BOOL(0, "dissociate", &dissociate,
1901 N_("use --reference only while cloning")),
1902 OPT_STRING(0, "depth", &clone_data.depth,
1903 N_("string"),
1904 N_("depth for shallow clones")),
1905 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
1906 OPT_BOOL(0, "progress", &progress,
1907 N_("force cloning progress")),
1908 OPT_BOOL(0, "require-init", &require_init,
1909 N_("disallow cloning into non-empty directory")),
1910 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1911 N_("clone only one branch, HEAD or --branch")),
1912 OPT_END()
1915 const char *const git_submodule_helper_usage[] = {
1916 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1917 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1918 "[--single-branch] "
1919 "--url <url> --path <path>"),
1920 NULL
1923 argc = parse_options(argc, argv, prefix, module_clone_options,
1924 git_submodule_helper_usage, 0);
1926 clone_data.dissociate = !!dissociate;
1927 clone_data.quiet = !!quiet;
1928 clone_data.progress = !!progress;
1929 clone_data.require_init = !!require_init;
1931 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1932 usage_with_options(git_submodule_helper_usage,
1933 module_clone_options);
1935 clone_submodule(&clone_data);
1936 return 0;
1939 static void determine_submodule_update_strategy(struct repository *r,
1940 int just_cloned,
1941 const char *path,
1942 const char *update,
1943 struct submodule_update_strategy *out)
1945 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1946 char *key;
1947 const char *val;
1949 key = xstrfmt("submodule.%s.update", sub->name);
1951 if (update) {
1952 if (parse_submodule_update_strategy(update, out) < 0)
1953 die(_("Invalid update mode '%s' for submodule path '%s'"),
1954 update, path);
1955 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1956 if (parse_submodule_update_strategy(val, out) < 0)
1957 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1958 val, path);
1959 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1960 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1961 BUG("how did we read update = !command from .gitmodules?");
1962 out->type = sub->update_strategy.type;
1963 out->command = sub->update_strategy.command;
1964 } else
1965 out->type = SM_UPDATE_CHECKOUT;
1967 if (just_cloned &&
1968 (out->type == SM_UPDATE_MERGE ||
1969 out->type == SM_UPDATE_REBASE ||
1970 out->type == SM_UPDATE_NONE))
1971 out->type = SM_UPDATE_CHECKOUT;
1973 free(key);
1976 static int module_update_module_mode(int argc, const char **argv, const char *prefix)
1978 const char *path, *update = NULL;
1979 int just_cloned;
1980 struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
1982 if (argc < 3 || argc > 4)
1983 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1985 just_cloned = git_config_int("just_cloned", argv[1]);
1986 path = argv[2];
1988 if (argc == 4)
1989 update = argv[3];
1991 determine_submodule_update_strategy(the_repository,
1992 just_cloned, path, update,
1993 &update_strategy);
1994 fputs(submodule_strategy_to_string(&update_strategy), stdout);
1996 return 0;
1999 struct update_clone_data {
2000 const struct submodule *sub;
2001 struct object_id oid;
2002 unsigned just_cloned;
2005 struct submodule_update_clone {
2006 /* index into 'list', the list of submodules to look into for cloning */
2007 int current;
2008 struct module_list list;
2009 unsigned warn_if_uninitialized : 1;
2011 /* update parameter passed via commandline */
2012 struct submodule_update_strategy update;
2014 /* configuration parameters which are passed on to the children */
2015 int progress;
2016 int quiet;
2017 int recommend_shallow;
2018 struct string_list references;
2019 int dissociate;
2020 unsigned require_init;
2021 const char *depth;
2022 const char *recursive_prefix;
2023 const char *prefix;
2024 int single_branch;
2026 /* to be consumed by git-submodule.sh */
2027 struct update_clone_data *update_clone;
2028 int update_clone_nr; int update_clone_alloc;
2030 /* If we want to stop as fast as possible and return an error */
2031 unsigned quickstop : 1;
2033 /* failed clones to be retried again */
2034 const struct cache_entry **failed_clones;
2035 int failed_clones_nr, failed_clones_alloc;
2037 int max_jobs;
2039 #define SUBMODULE_UPDATE_CLONE_INIT { \
2040 .list = MODULE_LIST_INIT, \
2041 .update = SUBMODULE_UPDATE_STRATEGY_INIT, \
2042 .recommend_shallow = -1, \
2043 .references = STRING_LIST_INIT_DUP, \
2044 .single_branch = -1, \
2045 .max_jobs = 1, \
2049 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
2050 struct strbuf *out, const char *displaypath)
2053 * Only mention uninitialized submodules when their
2054 * paths have been specified.
2056 if (suc->warn_if_uninitialized) {
2057 strbuf_addf(out,
2058 _("Submodule path '%s' not initialized"),
2059 displaypath);
2060 strbuf_addch(out, '\n');
2061 strbuf_addstr(out,
2062 _("Maybe you want to use 'update --init'?"));
2063 strbuf_addch(out, '\n');
2068 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2069 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2071 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
2072 struct child_process *child,
2073 struct submodule_update_clone *suc,
2074 struct strbuf *out)
2076 const struct submodule *sub = NULL;
2077 const char *url = NULL;
2078 const char *update_string;
2079 enum submodule_update_type update_type;
2080 char *key;
2081 struct strbuf displaypath_sb = STRBUF_INIT;
2082 struct strbuf sb = STRBUF_INIT;
2083 const char *displaypath = NULL;
2084 int needs_cloning = 0;
2085 int need_free_url = 0;
2087 if (ce_stage(ce)) {
2088 if (suc->recursive_prefix)
2089 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
2090 else
2091 strbuf_addstr(&sb, ce->name);
2092 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
2093 strbuf_addch(out, '\n');
2094 goto cleanup;
2097 sub = submodule_from_path(the_repository, null_oid(), ce->name);
2099 if (suc->recursive_prefix)
2100 displaypath = relative_path(suc->recursive_prefix,
2101 ce->name, &displaypath_sb);
2102 else
2103 displaypath = ce->name;
2105 if (!sub) {
2106 next_submodule_warn_missing(suc, out, displaypath);
2107 goto cleanup;
2110 key = xstrfmt("submodule.%s.update", sub->name);
2111 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
2112 update_type = parse_submodule_update_type(update_string);
2113 } else {
2114 update_type = sub->update_strategy.type;
2116 free(key);
2118 if (suc->update.type == SM_UPDATE_NONE
2119 || (suc->update.type == SM_UPDATE_UNSPECIFIED
2120 && update_type == SM_UPDATE_NONE)) {
2121 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
2122 strbuf_addch(out, '\n');
2123 goto cleanup;
2126 /* Check if the submodule has been initialized. */
2127 if (!is_submodule_active(the_repository, ce->name)) {
2128 next_submodule_warn_missing(suc, out, displaypath);
2129 goto cleanup;
2132 strbuf_reset(&sb);
2133 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2134 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2135 if (starts_with_dot_slash(sub->url) ||
2136 starts_with_dot_dot_slash(sub->url)) {
2137 url = compute_submodule_clone_url(sub->url);
2138 need_free_url = 1;
2139 } else
2140 url = sub->url;
2143 strbuf_reset(&sb);
2144 strbuf_addf(&sb, "%s/.git", ce->name);
2145 needs_cloning = !file_exists(sb.buf);
2147 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2148 suc->update_clone_alloc);
2149 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2150 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2151 suc->update_clone[suc->update_clone_nr].sub = sub;
2152 suc->update_clone_nr++;
2154 if (!needs_cloning)
2155 goto cleanup;
2157 child->git_cmd = 1;
2158 child->no_stdin = 1;
2159 child->stdout_to_stderr = 1;
2160 child->err = -1;
2161 strvec_push(&child->args, "submodule--helper");
2162 strvec_push(&child->args, "clone");
2163 if (suc->progress)
2164 strvec_push(&child->args, "--progress");
2165 if (suc->quiet)
2166 strvec_push(&child->args, "--quiet");
2167 if (suc->prefix)
2168 strvec_pushl(&child->args, "--prefix", suc->prefix, NULL);
2169 if (suc->recommend_shallow && sub->recommend_shallow == 1)
2170 strvec_push(&child->args, "--depth=1");
2171 if (suc->require_init)
2172 strvec_push(&child->args, "--require-init");
2173 strvec_pushl(&child->args, "--path", sub->path, NULL);
2174 strvec_pushl(&child->args, "--name", sub->name, NULL);
2175 strvec_pushl(&child->args, "--url", url, NULL);
2176 if (suc->references.nr) {
2177 struct string_list_item *item;
2178 for_each_string_list_item(item, &suc->references)
2179 strvec_pushl(&child->args, "--reference", item->string, NULL);
2181 if (suc->dissociate)
2182 strvec_push(&child->args, "--dissociate");
2183 if (suc->depth)
2184 strvec_push(&child->args, suc->depth);
2185 if (suc->single_branch >= 0)
2186 strvec_push(&child->args, suc->single_branch ?
2187 "--single-branch" :
2188 "--no-single-branch");
2190 cleanup:
2191 strbuf_release(&displaypath_sb);
2192 strbuf_release(&sb);
2193 if (need_free_url)
2194 free((void*)url);
2196 return needs_cloning;
2199 static int update_clone_get_next_task(struct child_process *child,
2200 struct strbuf *err,
2201 void *suc_cb,
2202 void **idx_task_cb)
2204 struct submodule_update_clone *suc = suc_cb;
2205 const struct cache_entry *ce;
2206 int index;
2208 for (; suc->current < suc->list.nr; suc->current++) {
2209 ce = suc->list.entries[suc->current];
2210 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2211 int *p = xmalloc(sizeof(*p));
2212 *p = suc->current;
2213 *idx_task_cb = p;
2214 suc->current++;
2215 return 1;
2220 * The loop above tried cloning each submodule once, now try the
2221 * stragglers again, which we can imagine as an extension of the
2222 * entry list.
2224 index = suc->current - suc->list.nr;
2225 if (index < suc->failed_clones_nr) {
2226 int *p;
2227 ce = suc->failed_clones[index];
2228 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2229 suc->current ++;
2230 strbuf_addstr(err, "BUG: submodule considered for "
2231 "cloning, doesn't need cloning "
2232 "any more?\n");
2233 return 0;
2235 p = xmalloc(sizeof(*p));
2236 *p = suc->current;
2237 *idx_task_cb = p;
2238 suc->current ++;
2239 return 1;
2242 return 0;
2245 static int update_clone_start_failure(struct strbuf *err,
2246 void *suc_cb,
2247 void *idx_task_cb)
2249 struct submodule_update_clone *suc = suc_cb;
2250 suc->quickstop = 1;
2251 return 1;
2254 static int update_clone_task_finished(int result,
2255 struct strbuf *err,
2256 void *suc_cb,
2257 void *idx_task_cb)
2259 const struct cache_entry *ce;
2260 struct submodule_update_clone *suc = suc_cb;
2262 int *idxP = idx_task_cb;
2263 int idx = *idxP;
2264 free(idxP);
2266 if (!result)
2267 return 0;
2269 if (idx < suc->list.nr) {
2270 ce = suc->list.entries[idx];
2271 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2272 ce->name);
2273 strbuf_addch(err, '\n');
2274 ALLOC_GROW(suc->failed_clones,
2275 suc->failed_clones_nr + 1,
2276 suc->failed_clones_alloc);
2277 suc->failed_clones[suc->failed_clones_nr++] = ce;
2278 return 0;
2279 } else {
2280 idx -= suc->list.nr;
2281 ce = suc->failed_clones[idx];
2282 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2283 ce->name);
2284 strbuf_addch(err, '\n');
2285 suc->quickstop = 1;
2286 return 1;
2289 return 0;
2292 static int git_update_clone_config(const char *var, const char *value,
2293 void *cb)
2295 int *max_jobs = cb;
2296 if (!strcmp(var, "submodule.fetchjobs"))
2297 *max_jobs = parse_submodule_fetchjobs(var, value);
2298 return 0;
2301 static void update_submodule(struct update_clone_data *ucd)
2303 fprintf(stdout, "dummy %s %d\t%s\n",
2304 oid_to_hex(&ucd->oid),
2305 ucd->just_cloned,
2306 ucd->sub->path);
2309 static int update_submodules(struct submodule_update_clone *suc)
2311 int i;
2313 run_processes_parallel_tr2(suc->max_jobs, update_clone_get_next_task,
2314 update_clone_start_failure,
2315 update_clone_task_finished, suc, "submodule",
2316 "parallel/update");
2319 * We saved the output and put it out all at once now.
2320 * That means:
2321 * - the listener does not have to interleave their (checkout)
2322 * work with our fetching. The writes involved in a
2323 * checkout involve more straightforward sequential I/O.
2324 * - the listener can avoid doing any work if fetching failed.
2326 if (suc->quickstop)
2327 return 1;
2329 for (i = 0; i < suc->update_clone_nr; i++)
2330 update_submodule(&suc->update_clone[i]);
2332 return 0;
2335 static int update_clone(int argc, const char **argv, const char *prefix)
2337 const char *update = NULL;
2338 struct pathspec pathspec;
2339 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2341 struct option module_update_clone_options[] = {
2342 OPT_STRING(0, "prefix", &prefix,
2343 N_("path"),
2344 N_("path into the working tree")),
2345 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
2346 N_("path"),
2347 N_("path into the working tree, across nested "
2348 "submodule boundaries")),
2349 OPT_STRING(0, "update", &update,
2350 N_("string"),
2351 N_("rebase, merge, checkout or none")),
2352 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
2353 N_("reference repository")),
2354 OPT_BOOL(0, "dissociate", &suc.dissociate,
2355 N_("use --reference only while cloning")),
2356 OPT_STRING(0, "depth", &suc.depth, "<depth>",
2357 N_("create a shallow clone truncated to the "
2358 "specified number of revisions")),
2359 OPT_INTEGER('j', "jobs", &suc.max_jobs,
2360 N_("parallel jobs")),
2361 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
2362 N_("whether the initial clone should follow the shallow recommendation")),
2363 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
2364 OPT_BOOL(0, "progress", &suc.progress,
2365 N_("force cloning progress")),
2366 OPT_BOOL(0, "require-init", &suc.require_init,
2367 N_("disallow cloning into non-empty directory")),
2368 OPT_BOOL(0, "single-branch", &suc.single_branch,
2369 N_("clone only one branch, HEAD or --branch")),
2370 OPT_END()
2373 const char *const git_submodule_helper_usage[] = {
2374 N_("git submodule--helper update-clone [--prefix=<path>] [<path>...]"),
2375 NULL
2377 suc.prefix = prefix;
2379 update_clone_config_from_gitmodules(&suc.max_jobs);
2380 git_config(git_update_clone_config, &suc.max_jobs);
2382 argc = parse_options(argc, argv, prefix, module_update_clone_options,
2383 git_submodule_helper_usage, 0);
2385 if (update)
2386 if (parse_submodule_update_strategy(update, &suc.update) < 0)
2387 die(_("bad value for update parameter"));
2389 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
2390 return 1;
2392 if (pathspec.nr)
2393 suc.warn_if_uninitialized = 1;
2395 return update_submodules(&suc);
2398 static int resolve_relative_path(int argc, const char **argv, const char *prefix)
2400 struct strbuf sb = STRBUF_INIT;
2401 if (argc != 3)
2402 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
2404 printf("%s", relative_path(argv[1], argv[2], &sb));
2405 strbuf_release(&sb);
2406 return 0;
2409 static const char *remote_submodule_branch(const char *path)
2411 const struct submodule *sub;
2412 const char *branch = NULL;
2413 char *key;
2415 sub = submodule_from_path(the_repository, null_oid(), path);
2416 if (!sub)
2417 return NULL;
2419 key = xstrfmt("submodule.%s.branch", sub->name);
2420 if (repo_config_get_string_tmp(the_repository, key, &branch))
2421 branch = sub->branch;
2422 free(key);
2424 if (!branch)
2425 return "HEAD";
2427 if (!strcmp(branch, ".")) {
2428 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2430 if (!refname)
2431 die(_("No such ref: %s"), "HEAD");
2433 /* detached HEAD */
2434 if (!strcmp(refname, "HEAD"))
2435 die(_("Submodule (%s) branch configured to inherit "
2436 "branch from superproject, but the superproject "
2437 "is not on any branch"), sub->name);
2439 if (!skip_prefix(refname, "refs/heads/", &refname))
2440 die(_("Expecting a full ref name, got %s"), refname);
2441 return refname;
2444 return branch;
2447 static int resolve_remote_submodule_branch(int argc, const char **argv,
2448 const char *prefix)
2450 const char *ret;
2451 struct strbuf sb = STRBUF_INIT;
2452 if (argc != 2)
2453 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
2455 ret = remote_submodule_branch(argv[1]);
2456 if (!ret)
2457 die("submodule %s doesn't exist", argv[1]);
2459 printf("%s", ret);
2460 strbuf_release(&sb);
2461 return 0;
2464 static int push_check(int argc, const char **argv, const char *prefix)
2466 struct remote *remote;
2467 const char *superproject_head;
2468 char *head;
2469 int detached_head = 0;
2470 struct object_id head_oid;
2472 if (argc < 3)
2473 die("submodule--helper push-check requires at least 2 arguments");
2476 * superproject's resolved head ref.
2477 * if HEAD then the superproject is in a detached head state, otherwise
2478 * it will be the resolved head ref.
2480 superproject_head = argv[1];
2481 argv++;
2482 argc--;
2483 /* Get the submodule's head ref and determine if it is detached */
2484 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2485 if (!head)
2486 die(_("Failed to resolve HEAD as a valid ref."));
2487 if (!strcmp(head, "HEAD"))
2488 detached_head = 1;
2491 * The remote must be configured.
2492 * This is to avoid pushing to the exact same URL as the parent.
2494 remote = pushremote_get(argv[1]);
2495 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2496 die("remote '%s' not configured", argv[1]);
2498 /* Check the refspec */
2499 if (argc > 2) {
2500 int i;
2501 struct ref *local_refs = get_local_heads();
2502 struct refspec refspec = REFSPEC_INIT_PUSH;
2504 refspec_appendn(&refspec, argv + 2, argc - 2);
2506 for (i = 0; i < refspec.nr; i++) {
2507 const struct refspec_item *rs = &refspec.items[i];
2509 if (rs->pattern || rs->matching)
2510 continue;
2512 /* LHS must match a single ref */
2513 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2514 case 1:
2515 break;
2516 case 0:
2518 * If LHS matches 'HEAD' then we need to ensure
2519 * that it matches the same named branch
2520 * checked out in the superproject.
2522 if (!strcmp(rs->src, "HEAD")) {
2523 if (!detached_head &&
2524 !strcmp(head, superproject_head))
2525 break;
2526 die("HEAD does not match the named branch in the superproject");
2528 /* fallthrough */
2529 default:
2530 die("src refspec '%s' must name a ref",
2531 rs->src);
2534 refspec_clear(&refspec);
2536 free(head);
2538 return 0;
2541 static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
2543 const struct submodule *sub;
2544 const char *path;
2545 const char *cw;
2546 struct repository subrepo;
2548 if (argc != 2)
2549 BUG("submodule--helper ensure-core-worktree <path>");
2551 path = argv[1];
2553 sub = submodule_from_path(the_repository, null_oid(), path);
2554 if (!sub)
2555 BUG("We could get the submodule handle before?");
2557 if (repo_submodule_init(&subrepo, the_repository, sub))
2558 die(_("could not get a repository handle for submodule '%s'"), path);
2560 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2561 char *cfg_file, *abs_path;
2562 const char *rel_path;
2563 struct strbuf sb = STRBUF_INIT;
2565 cfg_file = repo_git_path(&subrepo, "config");
2567 abs_path = absolute_pathdup(path);
2568 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2570 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2572 free(cfg_file);
2573 free(abs_path);
2574 strbuf_release(&sb);
2577 return 0;
2580 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2582 int i;
2583 struct pathspec pathspec;
2584 struct module_list list = MODULE_LIST_INIT;
2585 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2587 struct option embed_gitdir_options[] = {
2588 OPT_STRING(0, "prefix", &prefix,
2589 N_("path"),
2590 N_("path into the working tree")),
2591 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2592 ABSORB_GITDIR_RECURSE_SUBMODULES),
2593 OPT_END()
2596 const char *const git_submodule_helper_usage[] = {
2597 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2598 NULL
2601 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2602 git_submodule_helper_usage, 0);
2604 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2605 return 1;
2607 for (i = 0; i < list.nr; i++)
2608 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2610 return 0;
2613 static int is_active(int argc, const char **argv, const char *prefix)
2615 if (argc != 2)
2616 die("submodule--helper is-active takes exactly 1 argument");
2618 return !is_submodule_active(the_repository, argv[1]);
2622 * Exit non-zero if any of the submodule names given on the command line is
2623 * invalid. If no names are given, filter stdin to print only valid names
2624 * (which is primarily intended for testing).
2626 static int check_name(int argc, const char **argv, const char *prefix)
2628 if (argc > 1) {
2629 while (*++argv) {
2630 if (check_submodule_name(*argv) < 0)
2631 return 1;
2633 } else {
2634 struct strbuf buf = STRBUF_INIT;
2635 while (strbuf_getline(&buf, stdin) != EOF) {
2636 if (!check_submodule_name(buf.buf))
2637 printf("%s\n", buf.buf);
2639 strbuf_release(&buf);
2641 return 0;
2644 static int module_config(int argc, const char **argv, const char *prefix)
2646 enum {
2647 CHECK_WRITEABLE = 1,
2648 DO_UNSET = 2
2649 } command = 0;
2651 struct option module_config_options[] = {
2652 OPT_CMDMODE(0, "check-writeable", &command,
2653 N_("check if it is safe to write to the .gitmodules file"),
2654 CHECK_WRITEABLE),
2655 OPT_CMDMODE(0, "unset", &command,
2656 N_("unset the config in the .gitmodules file"),
2657 DO_UNSET),
2658 OPT_END()
2660 const char *const git_submodule_helper_usage[] = {
2661 N_("git submodule--helper config <name> [<value>]"),
2662 N_("git submodule--helper config --unset <name>"),
2663 N_("git submodule--helper config --check-writeable"),
2664 NULL
2667 argc = parse_options(argc, argv, prefix, module_config_options,
2668 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2670 if (argc == 1 && command == CHECK_WRITEABLE)
2671 return is_writing_gitmodules_ok() ? 0 : -1;
2673 /* Equivalent to ACTION_GET in builtin/config.c */
2674 if (argc == 2 && command != DO_UNSET)
2675 return print_config_from_gitmodules(the_repository, argv[1]);
2677 /* Equivalent to ACTION_SET in builtin/config.c */
2678 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2679 const char *value = (argc == 3) ? argv[2] : NULL;
2681 if (!is_writing_gitmodules_ok())
2682 die(_("please make sure that the .gitmodules file is in the working tree"));
2684 return config_set_in_gitmodules_file_gently(argv[1], value);
2687 usage_with_options(git_submodule_helper_usage, module_config_options);
2690 static int module_set_url(int argc, const char **argv, const char *prefix)
2692 int quiet = 0;
2693 const char *newurl;
2694 const char *path;
2695 char *config_name;
2697 struct option options[] = {
2698 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2699 OPT_END()
2701 const char *const usage[] = {
2702 N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
2703 NULL
2706 argc = parse_options(argc, argv, prefix, options, usage, 0);
2708 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2709 usage_with_options(usage, options);
2711 config_name = xstrfmt("submodule.%s.url", path);
2713 config_set_in_gitmodules_file_gently(config_name, newurl);
2714 sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
2716 free(config_name);
2718 return 0;
2721 static int module_set_branch(int argc, const char **argv, const char *prefix)
2723 int opt_default = 0, ret;
2724 const char *opt_branch = NULL;
2725 const char *path;
2726 char *config_name;
2729 * We accept the `quiet` option for uniformity across subcommands,
2730 * though there is nothing to make less verbose in this subcommand.
2732 struct option options[] = {
2733 OPT_NOOP_NOARG('q', "quiet"),
2734 OPT_BOOL('d', "default", &opt_default,
2735 N_("set the default tracking branch to master")),
2736 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2737 N_("set the default tracking branch")),
2738 OPT_END()
2740 const char *const usage[] = {
2741 N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
2742 N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2743 NULL
2746 argc = parse_options(argc, argv, prefix, options, usage, 0);
2748 if (!opt_branch && !opt_default)
2749 die(_("--branch or --default required"));
2751 if (opt_branch && opt_default)
2752 die(_("--branch and --default are mutually exclusive"));
2754 if (argc != 1 || !(path = argv[0]))
2755 usage_with_options(usage, options);
2757 config_name = xstrfmt("submodule.%s.branch", path);
2758 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2760 free(config_name);
2761 return !!ret;
2764 struct add_data {
2765 const char *prefix;
2766 const char *branch;
2767 const char *reference_path;
2768 const char *sm_path;
2769 const char *sm_name;
2770 const char *repo;
2771 const char *realrepo;
2772 int depth;
2773 unsigned int force: 1;
2774 unsigned int quiet: 1;
2775 unsigned int progress: 1;
2776 unsigned int dissociate: 1;
2778 #define ADD_DATA_INIT { .depth = -1 }
2780 static void show_fetch_remotes(FILE *output, const char *git_dir_path)
2782 struct child_process cp_remote = CHILD_PROCESS_INIT;
2783 struct strbuf sb_remote_out = STRBUF_INIT;
2785 cp_remote.git_cmd = 1;
2786 strvec_pushf(&cp_remote.env_array,
2787 "GIT_DIR=%s", git_dir_path);
2788 strvec_push(&cp_remote.env_array, "GIT_WORK_TREE=.");
2789 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
2790 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
2791 char *next_line;
2792 char *line = sb_remote_out.buf;
2793 while ((next_line = strchr(line, '\n')) != NULL) {
2794 size_t len = next_line - line;
2795 if (strip_suffix_mem(line, &len, " (fetch)"))
2796 fprintf(output, " %.*s\n", (int)len, line);
2797 line = next_line + 1;
2801 strbuf_release(&sb_remote_out);
2804 static int add_submodule(const struct add_data *add_data)
2806 char *submod_gitdir_path;
2807 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
2809 /* perhaps the path already exists and is already a git repo, else clone it */
2810 if (is_directory(add_data->sm_path)) {
2811 struct strbuf sm_path = STRBUF_INIT;
2812 strbuf_addstr(&sm_path, add_data->sm_path);
2813 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
2814 if (is_nonbare_repository_dir(&sm_path))
2815 printf(_("Adding existing repo at '%s' to the index\n"),
2816 add_data->sm_path);
2817 else
2818 die(_("'%s' already exists and is not a valid git repo"),
2819 add_data->sm_path);
2820 strbuf_release(&sm_path);
2821 free(submod_gitdir_path);
2822 } else {
2823 struct child_process cp = CHILD_PROCESS_INIT;
2824 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
2826 if (is_directory(submod_gitdir_path)) {
2827 if (!add_data->force) {
2828 fprintf(stderr, _("A git directory for '%s' is found "
2829 "locally with remote(s):"),
2830 add_data->sm_name);
2831 show_fetch_remotes(stderr, submod_gitdir_path);
2832 free(submod_gitdir_path);
2833 die(_("If you want to reuse this local git "
2834 "directory instead of cloning again from\n"
2835 " %s\n"
2836 "use the '--force' option. If the local git "
2837 "directory is not the correct repo\n"
2838 "or if you are unsure what this means, choose "
2839 "another name with the '--name' option.\n"),
2840 add_data->realrepo);
2841 } else {
2842 printf(_("Reactivating local git directory for "
2843 "submodule '%s'\n"), add_data->sm_name);
2846 free(submod_gitdir_path);
2848 clone_data.prefix = add_data->prefix;
2849 clone_data.path = add_data->sm_path;
2850 clone_data.name = add_data->sm_name;
2851 clone_data.url = add_data->realrepo;
2852 clone_data.quiet = add_data->quiet;
2853 clone_data.progress = add_data->progress;
2854 if (add_data->reference_path)
2855 string_list_append(&clone_data.reference,
2856 xstrdup(add_data->reference_path));
2857 clone_data.dissociate = add_data->dissociate;
2858 if (add_data->depth >= 0)
2859 clone_data.depth = xstrfmt("%d", add_data->depth);
2861 if (clone_submodule(&clone_data))
2862 return -1;
2864 prepare_submodule_repo_env(&cp.env_array);
2865 cp.git_cmd = 1;
2866 cp.dir = add_data->sm_path;
2868 * NOTE: we only get here if add_data->force is true, so
2869 * passing --force to checkout is reasonable.
2871 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
2873 if (add_data->branch) {
2874 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
2875 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
2878 if (run_command(&cp))
2879 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
2881 return 0;
2884 static int add_clone(int argc, const char **argv, const char *prefix)
2886 int force = 0, quiet = 0, dissociate = 0, progress = 0;
2887 struct add_data add_data = ADD_DATA_INIT;
2889 struct option options[] = {
2890 OPT_STRING('b', "branch", &add_data.branch,
2891 N_("branch"),
2892 N_("branch of repository to checkout on cloning")),
2893 OPT_STRING(0, "prefix", &prefix,
2894 N_("path"),
2895 N_("alternative anchor for relative paths")),
2896 OPT_STRING(0, "path", &add_data.sm_path,
2897 N_("path"),
2898 N_("where the new submodule will be cloned to")),
2899 OPT_STRING(0, "name", &add_data.sm_name,
2900 N_("string"),
2901 N_("name of the new submodule")),
2902 OPT_STRING(0, "url", &add_data.realrepo,
2903 N_("string"),
2904 N_("url where to clone the submodule from")),
2905 OPT_STRING(0, "reference", &add_data.reference_path,
2906 N_("repo"),
2907 N_("reference repository")),
2908 OPT_BOOL(0, "dissociate", &dissociate,
2909 N_("use --reference only while cloning")),
2910 OPT_INTEGER(0, "depth", &add_data.depth,
2911 N_("depth for shallow clones")),
2912 OPT_BOOL(0, "progress", &progress,
2913 N_("force cloning progress")),
2914 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
2915 PARSE_OPT_NOCOMPLETE),
2916 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
2917 OPT_END()
2920 const char *const usage[] = {
2921 N_("git submodule--helper add-clone [<options>...] "
2922 "--url <url> --path <path> --name <name>"),
2923 NULL
2926 argc = parse_options(argc, argv, prefix, options, usage, 0);
2928 if (argc != 0)
2929 usage_with_options(usage, options);
2931 add_data.prefix = prefix;
2932 add_data.progress = !!progress;
2933 add_data.dissociate = !!dissociate;
2934 add_data.force = !!force;
2935 add_data.quiet = !!quiet;
2937 if (add_submodule(&add_data))
2938 return 1;
2940 return 0;
2943 #define SUPPORT_SUPER_PREFIX (1<<0)
2945 struct cmd_struct {
2946 const char *cmd;
2947 int (*fn)(int, const char **, const char *);
2948 unsigned option;
2951 static struct cmd_struct commands[] = {
2952 {"list", module_list, 0},
2953 {"name", module_name, 0},
2954 {"clone", module_clone, 0},
2955 {"add-clone", add_clone, 0},
2956 {"update-module-mode", module_update_module_mode, 0},
2957 {"update-clone", update_clone, 0},
2958 {"ensure-core-worktree", ensure_core_worktree, 0},
2959 {"relative-path", resolve_relative_path, 0},
2960 {"resolve-relative-url", resolve_relative_url, 0},
2961 {"resolve-relative-url-test", resolve_relative_url_test, 0},
2962 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
2963 {"init", module_init, SUPPORT_SUPER_PREFIX},
2964 {"status", module_status, SUPPORT_SUPER_PREFIX},
2965 {"print-default-remote", print_default_remote, 0},
2966 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
2967 {"deinit", module_deinit, 0},
2968 {"summary", module_summary, SUPPORT_SUPER_PREFIX},
2969 {"remote-branch", resolve_remote_submodule_branch, 0},
2970 {"push-check", push_check, 0},
2971 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
2972 {"is-active", is_active, 0},
2973 {"check-name", check_name, 0},
2974 {"config", module_config, 0},
2975 {"set-url", module_set_url, 0},
2976 {"set-branch", module_set_branch, 0},
2979 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2981 int i;
2982 if (argc < 2 || !strcmp(argv[1], "-h"))
2983 usage("git submodule--helper <command>");
2985 for (i = 0; i < ARRAY_SIZE(commands); i++) {
2986 if (!strcmp(argv[1], commands[i].cmd)) {
2987 if (get_super_prefix() &&
2988 !(commands[i].option & SUPPORT_SUPER_PREFIX))
2989 die(_("%s doesn't support --super-prefix"),
2990 commands[i].cmd);
2991 return commands[i].fn(argc - 1, argv + 1, prefix);
2995 die(_("'%s' is not a valid submodule--helper "
2996 "subcommand"), argv[1]);