Merge branch 'fh/transport-push-leakfix'
[alt-git.git] / builtin / submodule--helper.c
blobc6c2ba1b6dc49c4d7ab398948dfad835b009b0a1
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"
23 #include "branch.h"
24 #include "list-objects-filter-options.h"
26 #define OPT_QUIET (1 << 0)
27 #define OPT_CACHED (1 << 1)
28 #define OPT_RECURSIVE (1 << 2)
29 #define OPT_FORCE (1 << 3)
31 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
32 void *cb_data);
34 static char *repo_get_default_remote(struct repository *repo)
36 char *dest = NULL, *ret;
37 struct strbuf sb = STRBUF_INIT;
38 struct ref_store *store = get_main_ref_store(repo);
39 const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
40 NULL);
42 if (!refname)
43 die(_("No such ref: %s"), "HEAD");
45 /* detached HEAD */
46 if (!strcmp(refname, "HEAD"))
47 return xstrdup("origin");
49 if (!skip_prefix(refname, "refs/heads/", &refname))
50 die(_("Expecting a full ref name, got %s"), refname);
52 strbuf_addf(&sb, "branch.%s.remote", refname);
53 if (repo_config_get_string(repo, sb.buf, &dest))
54 ret = xstrdup("origin");
55 else
56 ret = dest;
58 strbuf_release(&sb);
59 return ret;
62 static char *get_default_remote_submodule(const char *module_path)
64 struct repository subrepo;
66 repo_submodule_init(&subrepo, the_repository, module_path, null_oid());
67 return repo_get_default_remote(&subrepo);
70 static char *get_default_remote(void)
72 return repo_get_default_remote(the_repository);
75 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
77 char *remoteurl, *resolved_url;
78 char *remote = get_default_remote();
79 struct strbuf remotesb = STRBUF_INIT;
81 strbuf_addf(&remotesb, "remote.%s.url", remote);
82 if (git_config_get_string(remotesb.buf, &remoteurl)) {
83 if (!quiet)
84 warning(_("could not look up configuration '%s'. "
85 "Assuming this repository is its own "
86 "authoritative upstream."),
87 remotesb.buf);
88 remoteurl = xgetcwd();
90 resolved_url = relative_url(remoteurl, rel_url, up_path);
92 free(remote);
93 free(remoteurl);
94 strbuf_release(&remotesb);
96 return resolved_url;
99 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
101 char *remoteurl, *res;
102 const char *up_path, *url;
104 if (argc != 4)
105 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
107 up_path = argv[1];
108 remoteurl = xstrdup(argv[2]);
109 url = argv[3];
111 if (!strcmp(up_path, "(null)"))
112 up_path = NULL;
114 res = relative_url(remoteurl, url, up_path);
115 puts(res);
116 free(res);
117 free(remoteurl);
118 return 0;
121 static char *do_get_submodule_displaypath(const char *path,
122 const char *prefix,
123 const char *super_prefix)
125 if (prefix && super_prefix) {
126 BUG("cannot have prefix '%s' and superprefix '%s'",
127 prefix, super_prefix);
128 } else if (prefix) {
129 struct strbuf sb = STRBUF_INIT;
130 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
131 strbuf_release(&sb);
132 return displaypath;
133 } else if (super_prefix) {
134 return xstrfmt("%s%s", super_prefix, path);
135 } else {
136 return xstrdup(path);
140 /* the result should be freed by the caller. */
141 static char *get_submodule_displaypath(const char *path, const char *prefix)
143 const char *super_prefix = get_super_prefix();
144 return do_get_submodule_displaypath(path, prefix, super_prefix);
147 static char *compute_rev_name(const char *sub_path, const char* object_id)
149 struct strbuf sb = STRBUF_INIT;
150 const char ***d;
152 static const char *describe_bare[] = { NULL };
154 static const char *describe_tags[] = { "--tags", NULL };
156 static const char *describe_contains[] = { "--contains", NULL };
158 static const char *describe_all_always[] = { "--all", "--always", NULL };
160 static const char **describe_argv[] = { describe_bare, describe_tags,
161 describe_contains,
162 describe_all_always, NULL };
164 for (d = describe_argv; *d; d++) {
165 struct child_process cp = CHILD_PROCESS_INIT;
166 prepare_submodule_repo_env(&cp.env_array);
167 cp.dir = sub_path;
168 cp.git_cmd = 1;
169 cp.no_stderr = 1;
171 strvec_push(&cp.args, "describe");
172 strvec_pushv(&cp.args, *d);
173 strvec_push(&cp.args, object_id);
175 if (!capture_command(&cp, &sb, 0)) {
176 strbuf_strip_suffix(&sb, "\n");
177 return strbuf_detach(&sb, NULL);
181 strbuf_release(&sb);
182 return NULL;
185 struct module_list {
186 const struct cache_entry **entries;
187 int alloc, nr;
189 #define MODULE_LIST_INIT { 0 }
191 static int module_list_compute(int argc, const char **argv,
192 const char *prefix,
193 struct pathspec *pathspec,
194 struct module_list *list)
196 int i, result = 0;
197 char *ps_matched = NULL;
198 parse_pathspec(pathspec, 0,
199 PATHSPEC_PREFER_FULL,
200 prefix, argv);
202 if (pathspec->nr)
203 ps_matched = xcalloc(pathspec->nr, 1);
205 if (read_cache() < 0)
206 die(_("index file corrupt"));
208 for (i = 0; i < active_nr; i++) {
209 const struct cache_entry *ce = active_cache[i];
211 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
212 0, ps_matched, 1) ||
213 !S_ISGITLINK(ce->ce_mode))
214 continue;
216 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
217 list->entries[list->nr++] = ce;
218 while (i + 1 < active_nr &&
219 !strcmp(ce->name, active_cache[i + 1]->name))
221 * Skip entries with the same name in different stages
222 * to make sure an entry is returned only once.
224 i++;
227 if (ps_matched && report_path_error(ps_matched, pathspec))
228 result = -1;
230 free(ps_matched);
232 return result;
235 static void module_list_active(struct module_list *list)
237 int i;
238 struct module_list active_modules = MODULE_LIST_INIT;
240 for (i = 0; i < list->nr; i++) {
241 const struct cache_entry *ce = list->entries[i];
243 if (!is_submodule_active(the_repository, ce->name))
244 continue;
246 ALLOC_GROW(active_modules.entries,
247 active_modules.nr + 1,
248 active_modules.alloc);
249 active_modules.entries[active_modules.nr++] = ce;
252 free(list->entries);
253 *list = active_modules;
256 static char *get_up_path(const char *path)
258 int i;
259 struct strbuf sb = STRBUF_INIT;
261 for (i = count_slashes(path); i; i--)
262 strbuf_addstr(&sb, "../");
265 * Check if 'path' ends with slash or not
266 * for having the same output for dir/sub_dir
267 * and dir/sub_dir/
269 if (!is_dir_sep(path[strlen(path) - 1]))
270 strbuf_addstr(&sb, "../");
272 return strbuf_detach(&sb, NULL);
275 static int module_list(int argc, const char **argv, const char *prefix)
277 int i;
278 struct pathspec pathspec;
279 struct module_list list = MODULE_LIST_INIT;
281 struct option module_list_options[] = {
282 OPT_STRING(0, "prefix", &prefix,
283 N_("path"),
284 N_("alternative anchor for relative paths")),
285 OPT_END()
288 const char *const git_submodule_helper_usage[] = {
289 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
290 NULL
293 argc = parse_options(argc, argv, prefix, module_list_options,
294 git_submodule_helper_usage, 0);
296 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
297 return 1;
299 for (i = 0; i < list.nr; i++) {
300 const struct cache_entry *ce = list.entries[i];
302 if (ce_stage(ce))
303 printf("%06o %s U\t", ce->ce_mode,
304 oid_to_hex(null_oid()));
305 else
306 printf("%06o %s %d\t", ce->ce_mode,
307 oid_to_hex(&ce->oid), ce_stage(ce));
309 fprintf(stdout, "%s\n", ce->name);
311 return 0;
314 static void for_each_listed_submodule(const struct module_list *list,
315 each_submodule_fn fn, void *cb_data)
317 int i;
318 for (i = 0; i < list->nr; i++)
319 fn(list->entries[i], cb_data);
322 struct foreach_cb {
323 int argc;
324 const char **argv;
325 const char *prefix;
326 int quiet;
327 int recursive;
329 #define FOREACH_CB_INIT { 0 }
331 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
332 void *cb_data)
334 struct foreach_cb *info = cb_data;
335 const char *path = list_item->name;
336 const struct object_id *ce_oid = &list_item->oid;
338 const struct submodule *sub;
339 struct child_process cp = CHILD_PROCESS_INIT;
340 char *displaypath;
342 displaypath = get_submodule_displaypath(path, info->prefix);
344 sub = submodule_from_path(the_repository, null_oid(), path);
346 if (!sub)
347 die(_("No url found for submodule path '%s' in .gitmodules"),
348 displaypath);
350 if (!is_submodule_populated_gently(path, NULL))
351 goto cleanup;
353 prepare_submodule_repo_env(&cp.env_array);
356 * For the purpose of executing <command> in the submodule,
357 * separate shell is used for the purpose of running the
358 * child process.
360 cp.use_shell = 1;
361 cp.dir = path;
364 * NEEDSWORK: the command currently has access to the variables $name,
365 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
366 * contains a single argument. This is done for maintaining a faithful
367 * translation from shell script.
369 if (info->argc == 1) {
370 char *toplevel = xgetcwd();
371 struct strbuf sb = STRBUF_INIT;
373 strvec_pushf(&cp.env_array, "name=%s", sub->name);
374 strvec_pushf(&cp.env_array, "sm_path=%s", path);
375 strvec_pushf(&cp.env_array, "displaypath=%s", displaypath);
376 strvec_pushf(&cp.env_array, "sha1=%s",
377 oid_to_hex(ce_oid));
378 strvec_pushf(&cp.env_array, "toplevel=%s", toplevel);
381 * Since the path variable was accessible from the script
382 * before porting, it is also made available after porting.
383 * The environment variable "PATH" has a very special purpose
384 * on windows. And since environment variables are
385 * case-insensitive in windows, it interferes with the
386 * existing PATH variable. Hence, to avoid that, we expose
387 * path via the args strvec and not via env_array.
389 sq_quote_buf(&sb, path);
390 strvec_pushf(&cp.args, "path=%s; %s",
391 sb.buf, info->argv[0]);
392 strbuf_release(&sb);
393 free(toplevel);
394 } else {
395 strvec_pushv(&cp.args, info->argv);
398 if (!info->quiet)
399 printf(_("Entering '%s'\n"), displaypath);
401 if (info->argv[0] && run_command(&cp))
402 die(_("run_command returned non-zero status for %s\n."),
403 displaypath);
405 if (info->recursive) {
406 struct child_process cpr = CHILD_PROCESS_INIT;
408 cpr.git_cmd = 1;
409 cpr.dir = path;
410 prepare_submodule_repo_env(&cpr.env_array);
412 strvec_pushl(&cpr.args, "--super-prefix", NULL);
413 strvec_pushf(&cpr.args, "%s/", displaypath);
414 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
415 NULL);
417 if (info->quiet)
418 strvec_push(&cpr.args, "--quiet");
420 strvec_push(&cpr.args, "--");
421 strvec_pushv(&cpr.args, info->argv);
423 if (run_command(&cpr))
424 die(_("run_command returned non-zero status while "
425 "recursing in the nested submodules of %s\n."),
426 displaypath);
429 cleanup:
430 free(displaypath);
433 static int module_foreach(int argc, const char **argv, const char *prefix)
435 struct foreach_cb info = FOREACH_CB_INIT;
436 struct pathspec pathspec;
437 struct module_list list = MODULE_LIST_INIT;
439 struct option module_foreach_options[] = {
440 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
441 OPT_BOOL(0, "recursive", &info.recursive,
442 N_("recurse into nested submodules")),
443 OPT_END()
446 const char *const git_submodule_helper_usage[] = {
447 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
448 NULL
451 argc = parse_options(argc, argv, prefix, module_foreach_options,
452 git_submodule_helper_usage, 0);
454 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
455 return 1;
457 info.argc = argc;
458 info.argv = argv;
459 info.prefix = prefix;
461 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
463 return 0;
466 static int starts_with_dot_slash(const char *const path)
468 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
469 PATH_MATCH_XPLATFORM);
472 static int starts_with_dot_dot_slash(const char *const path)
474 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
475 PATH_MATCH_XPLATFORM);
478 struct init_cb {
479 const char *prefix;
480 const char *superprefix;
481 unsigned int flags;
483 #define INIT_CB_INIT { 0 }
485 static void init_submodule(const char *path, const char *prefix,
486 const char *superprefix, unsigned int flags)
488 const struct submodule *sub;
489 struct strbuf sb = STRBUF_INIT;
490 char *upd = NULL, *url = NULL, *displaypath;
492 /* try superprefix from the environment, if it is not passed explicitly */
493 if (!superprefix)
494 superprefix = get_super_prefix();
495 displaypath = do_get_submodule_displaypath(path, prefix, superprefix);
497 sub = submodule_from_path(the_repository, null_oid(), path);
499 if (!sub)
500 die(_("No url found for submodule path '%s' in .gitmodules"),
501 displaypath);
504 * NEEDSWORK: In a multi-working-tree world, this needs to be
505 * set in the per-worktree config.
507 * Set active flag for the submodule being initialized
509 if (!is_submodule_active(the_repository, path)) {
510 strbuf_addf(&sb, "submodule.%s.active", sub->name);
511 git_config_set_gently(sb.buf, "true");
512 strbuf_reset(&sb);
516 * Copy url setting when it is not set yet.
517 * To look up the url in .git/config, we must not fall back to
518 * .gitmodules, so look it up directly.
520 strbuf_addf(&sb, "submodule.%s.url", sub->name);
521 if (git_config_get_string(sb.buf, &url)) {
522 if (!sub->url)
523 die(_("No url found for submodule path '%s' in .gitmodules"),
524 displaypath);
526 url = xstrdup(sub->url);
528 /* Possibly a url relative to parent */
529 if (starts_with_dot_dot_slash(url) ||
530 starts_with_dot_slash(url)) {
531 char *oldurl = url;
532 url = resolve_relative_url(oldurl, NULL, 0);
533 free(oldurl);
536 if (git_config_set_gently(sb.buf, url))
537 die(_("Failed to register url for submodule path '%s'"),
538 displaypath);
539 if (!(flags & OPT_QUIET))
540 fprintf(stderr,
541 _("Submodule '%s' (%s) registered for path '%s'\n"),
542 sub->name, url, displaypath);
544 strbuf_reset(&sb);
546 /* Copy "update" setting when it is not set yet */
547 strbuf_addf(&sb, "submodule.%s.update", sub->name);
548 if (git_config_get_string(sb.buf, &upd) &&
549 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
550 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
551 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
552 sub->name);
553 upd = xstrdup("none");
554 } else
555 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
557 if (git_config_set_gently(sb.buf, upd))
558 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
560 strbuf_release(&sb);
561 free(displaypath);
562 free(url);
563 free(upd);
566 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
568 struct init_cb *info = cb_data;
569 init_submodule(list_item->name, info->prefix, info->superprefix, info->flags);
572 static int module_init(int argc, const char **argv, const char *prefix)
574 struct init_cb info = INIT_CB_INIT;
575 struct pathspec pathspec;
576 struct module_list list = MODULE_LIST_INIT;
577 int quiet = 0;
579 struct option module_init_options[] = {
580 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
581 OPT_END()
584 const char *const git_submodule_helper_usage[] = {
585 N_("git submodule--helper init [<options>] [<path>]"),
586 NULL
589 argc = parse_options(argc, argv, prefix, module_init_options,
590 git_submodule_helper_usage, 0);
592 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
593 return 1;
596 * If there are no path args and submodule.active is set then,
597 * by default, only initialize 'active' modules.
599 if (!argc && git_config_get_value_multi("submodule.active"))
600 module_list_active(&list);
602 info.prefix = prefix;
603 if (quiet)
604 info.flags |= OPT_QUIET;
606 for_each_listed_submodule(&list, init_submodule_cb, &info);
608 return 0;
611 struct status_cb {
612 const char *prefix;
613 unsigned int flags;
615 #define STATUS_CB_INIT { 0 }
617 static void print_status(unsigned int flags, char state, const char *path,
618 const struct object_id *oid, const char *displaypath)
620 if (flags & OPT_QUIET)
621 return;
623 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
625 if (state == ' ' || state == '+') {
626 const char *name = compute_rev_name(path, oid_to_hex(oid));
628 if (name)
629 printf(" (%s)", name);
632 printf("\n");
635 static int handle_submodule_head_ref(const char *refname,
636 const struct object_id *oid, int flags,
637 void *cb_data)
639 struct object_id *output = cb_data;
640 if (oid)
641 oidcpy(output, oid);
643 return 0;
646 static void status_submodule(const char *path, const struct object_id *ce_oid,
647 unsigned int ce_flags, const char *prefix,
648 unsigned int flags)
650 char *displaypath;
651 struct strvec diff_files_args = STRVEC_INIT;
652 struct rev_info rev = REV_INFO_INIT;
653 int diff_files_result;
654 struct strbuf buf = STRBUF_INIT;
655 const char *git_dir;
657 if (!submodule_from_path(the_repository, null_oid(), path))
658 die(_("no submodule mapping found in .gitmodules for path '%s'"),
659 path);
661 displaypath = get_submodule_displaypath(path, prefix);
663 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
664 print_status(flags, 'U', path, null_oid(), displaypath);
665 goto cleanup;
668 strbuf_addf(&buf, "%s/.git", path);
669 git_dir = read_gitfile(buf.buf);
670 if (!git_dir)
671 git_dir = buf.buf;
673 if (!is_submodule_active(the_repository, path) ||
674 !is_git_directory(git_dir)) {
675 print_status(flags, '-', path, ce_oid, displaypath);
676 strbuf_release(&buf);
677 goto cleanup;
679 strbuf_release(&buf);
681 strvec_pushl(&diff_files_args, "diff-files",
682 "--ignore-submodules=dirty", "--quiet", "--",
683 path, NULL);
685 git_config(git_diff_basic_config, NULL);
687 repo_init_revisions(the_repository, &rev, NULL);
688 rev.abbrev = 0;
689 diff_files_args.nr = setup_revisions(diff_files_args.nr,
690 diff_files_args.v,
691 &rev, NULL);
692 diff_files_result = run_diff_files(&rev, 0);
694 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
695 print_status(flags, ' ', path, ce_oid,
696 displaypath);
697 } else if (!(flags & OPT_CACHED)) {
698 struct object_id oid;
699 struct ref_store *refs = get_submodule_ref_store(path);
701 if (!refs) {
702 print_status(flags, '-', path, ce_oid, displaypath);
703 goto cleanup;
705 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
706 die(_("could not resolve HEAD ref inside the "
707 "submodule '%s'"), path);
709 print_status(flags, '+', path, &oid, displaypath);
710 } else {
711 print_status(flags, '+', path, ce_oid, displaypath);
714 if (flags & OPT_RECURSIVE) {
715 struct child_process cpr = CHILD_PROCESS_INIT;
717 cpr.git_cmd = 1;
718 cpr.dir = path;
719 prepare_submodule_repo_env(&cpr.env_array);
721 strvec_push(&cpr.args, "--super-prefix");
722 strvec_pushf(&cpr.args, "%s/", displaypath);
723 strvec_pushl(&cpr.args, "submodule--helper", "status",
724 "--recursive", NULL);
726 if (flags & OPT_CACHED)
727 strvec_push(&cpr.args, "--cached");
729 if (flags & OPT_QUIET)
730 strvec_push(&cpr.args, "--quiet");
732 if (run_command(&cpr))
733 die(_("failed to recurse into submodule '%s'"), path);
736 cleanup:
737 strvec_clear(&diff_files_args);
738 free(displaypath);
739 release_revisions(&rev);
742 static void status_submodule_cb(const struct cache_entry *list_item,
743 void *cb_data)
745 struct status_cb *info = cb_data;
746 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
747 info->prefix, info->flags);
750 static int module_status(int argc, const char **argv, const char *prefix)
752 struct status_cb info = STATUS_CB_INIT;
753 struct pathspec pathspec;
754 struct module_list list = MODULE_LIST_INIT;
755 int quiet = 0;
757 struct option module_status_options[] = {
758 OPT__QUIET(&quiet, N_("suppress submodule status output")),
759 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
760 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
761 OPT_END()
764 const char *const git_submodule_helper_usage[] = {
765 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
766 NULL
769 argc = parse_options(argc, argv, prefix, module_status_options,
770 git_submodule_helper_usage, 0);
772 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
773 return 1;
775 info.prefix = prefix;
776 if (quiet)
777 info.flags |= OPT_QUIET;
779 for_each_listed_submodule(&list, status_submodule_cb, &info);
781 return 0;
784 static int module_name(int argc, const char **argv, const char *prefix)
786 const struct submodule *sub;
788 if (argc != 2)
789 usage(_("git submodule--helper name <path>"));
791 sub = submodule_from_path(the_repository, null_oid(), argv[1]);
793 if (!sub)
794 die(_("no submodule mapping found in .gitmodules for path '%s'"),
795 argv[1]);
797 printf("%s\n", sub->name);
799 return 0;
802 struct module_cb {
803 unsigned int mod_src;
804 unsigned int mod_dst;
805 struct object_id oid_src;
806 struct object_id oid_dst;
807 char status;
808 const char *sm_path;
810 #define MODULE_CB_INIT { 0 }
812 struct module_cb_list {
813 struct module_cb **entries;
814 int alloc, nr;
816 #define MODULE_CB_LIST_INIT { 0 }
818 struct summary_cb {
819 int argc;
820 const char **argv;
821 const char *prefix;
822 unsigned int cached: 1;
823 unsigned int for_status: 1;
824 unsigned int files: 1;
825 int summary_limit;
827 #define SUMMARY_CB_INIT { 0 }
829 enum diff_cmd {
830 DIFF_INDEX,
831 DIFF_FILES
834 static char *verify_submodule_committish(const char *sm_path,
835 const char *committish)
837 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
838 struct strbuf result = STRBUF_INIT;
840 cp_rev_parse.git_cmd = 1;
841 cp_rev_parse.dir = sm_path;
842 prepare_submodule_repo_env(&cp_rev_parse.env_array);
843 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
844 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
845 strvec_push(&cp_rev_parse.args, "--");
847 if (capture_command(&cp_rev_parse, &result, 0))
848 return NULL;
850 strbuf_trim_trailing_newline(&result);
851 return strbuf_detach(&result, NULL);
854 static void print_submodule_summary(struct summary_cb *info, char *errmsg,
855 int total_commits, const char *displaypath,
856 const char *src_abbrev, const char *dst_abbrev,
857 struct module_cb *p)
859 if (p->status == 'T') {
860 if (S_ISGITLINK(p->mod_dst))
861 printf(_("* %s %s(blob)->%s(submodule)"),
862 displaypath, src_abbrev, dst_abbrev);
863 else
864 printf(_("* %s %s(submodule)->%s(blob)"),
865 displaypath, src_abbrev, dst_abbrev);
866 } else {
867 printf("* %s %s...%s",
868 displaypath, src_abbrev, dst_abbrev);
871 if (total_commits < 0)
872 printf(":\n");
873 else
874 printf(" (%d):\n", total_commits);
876 if (errmsg) {
877 printf(_("%s"), errmsg);
878 } else if (total_commits > 0) {
879 struct child_process cp_log = CHILD_PROCESS_INIT;
881 cp_log.git_cmd = 1;
882 cp_log.dir = p->sm_path;
883 prepare_submodule_repo_env(&cp_log.env_array);
884 strvec_pushl(&cp_log.args, "log", NULL);
886 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
887 if (info->summary_limit > 0)
888 strvec_pushf(&cp_log.args, "-%d",
889 info->summary_limit);
891 strvec_pushl(&cp_log.args, "--pretty= %m %s",
892 "--first-parent", NULL);
893 strvec_pushf(&cp_log.args, "%s...%s",
894 src_abbrev, dst_abbrev);
895 } else if (S_ISGITLINK(p->mod_dst)) {
896 strvec_pushl(&cp_log.args, "--pretty= > %s",
897 "-1", dst_abbrev, NULL);
898 } else {
899 strvec_pushl(&cp_log.args, "--pretty= < %s",
900 "-1", src_abbrev, NULL);
902 run_command(&cp_log);
904 printf("\n");
907 static void generate_submodule_summary(struct summary_cb *info,
908 struct module_cb *p)
910 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
911 int missing_src = 0, missing_dst = 0;
912 char *errmsg = NULL;
913 int total_commits = -1;
915 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
916 if (S_ISGITLINK(p->mod_dst)) {
917 struct ref_store *refs = get_submodule_ref_store(p->sm_path);
918 if (refs)
919 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
920 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
921 struct stat st;
922 int fd = open(p->sm_path, O_RDONLY);
924 if (fd < 0 || fstat(fd, &st) < 0 ||
925 index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
926 p->sm_path, 0))
927 error(_("couldn't hash object from '%s'"), p->sm_path);
928 } else {
929 /* for a submodule removal (mode:0000000), don't warn */
930 if (p->mod_dst)
931 warning(_("unexpected mode %o\n"), p->mod_dst);
935 if (S_ISGITLINK(p->mod_src)) {
936 if (p->status != 'D')
937 src_abbrev = verify_submodule_committish(p->sm_path,
938 oid_to_hex(&p->oid_src));
939 if (!src_abbrev) {
940 missing_src = 1;
942 * As `rev-parse` failed, we fallback to getting
943 * the abbreviated hash using oid_src. We do
944 * this as we might still need the abbreviated
945 * hash in cases like a submodule type change, etc.
947 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
949 } else {
951 * The source does not point to a submodule.
952 * So, we fallback to getting the abbreviation using
953 * oid_src as we might still need the abbreviated
954 * hash in cases like submodule add, etc.
956 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
959 if (S_ISGITLINK(p->mod_dst)) {
960 dst_abbrev = verify_submodule_committish(p->sm_path,
961 oid_to_hex(&p->oid_dst));
962 if (!dst_abbrev) {
963 missing_dst = 1;
965 * As `rev-parse` failed, we fallback to getting
966 * the abbreviated hash using oid_dst. We do
967 * this as we might still need the abbreviated
968 * hash in cases like a submodule type change, etc.
970 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
972 } else {
974 * The destination does not point to a submodule.
975 * So, we fallback to getting the abbreviation using
976 * oid_dst as we might still need the abbreviated
977 * hash in cases like a submodule removal, etc.
979 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
982 displaypath = get_submodule_displaypath(p->sm_path, info->prefix);
984 if (!missing_src && !missing_dst) {
985 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
986 struct strbuf sb_rev_list = STRBUF_INIT;
988 strvec_pushl(&cp_rev_list.args, "rev-list",
989 "--first-parent", "--count", NULL);
990 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
991 strvec_pushf(&cp_rev_list.args, "%s...%s",
992 src_abbrev, dst_abbrev);
993 else
994 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
995 src_abbrev : dst_abbrev);
996 strvec_push(&cp_rev_list.args, "--");
998 cp_rev_list.git_cmd = 1;
999 cp_rev_list.dir = p->sm_path;
1000 prepare_submodule_repo_env(&cp_rev_list.env_array);
1002 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
1003 total_commits = atoi(sb_rev_list.buf);
1005 strbuf_release(&sb_rev_list);
1006 } else {
1008 * Don't give error msg for modification whose dst is not
1009 * submodule, i.e., deleted or changed to blob
1011 if (S_ISGITLINK(p->mod_dst)) {
1012 struct strbuf errmsg_str = STRBUF_INIT;
1013 if (missing_src && missing_dst) {
1014 strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commits %s and %s\n",
1015 displaypath, oid_to_hex(&p->oid_src),
1016 oid_to_hex(&p->oid_dst));
1017 } else {
1018 strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commit %s\n",
1019 displaypath, missing_src ?
1020 oid_to_hex(&p->oid_src) :
1021 oid_to_hex(&p->oid_dst));
1023 errmsg = strbuf_detach(&errmsg_str, NULL);
1027 print_submodule_summary(info, errmsg, total_commits,
1028 displaypath, src_abbrev,
1029 dst_abbrev, p);
1031 free(displaypath);
1032 free(src_abbrev);
1033 free(dst_abbrev);
1036 static void prepare_submodule_summary(struct summary_cb *info,
1037 struct module_cb_list *list)
1039 int i;
1040 for (i = 0; i < list->nr; i++) {
1041 const struct submodule *sub;
1042 struct module_cb *p = list->entries[i];
1043 struct strbuf sm_gitdir = STRBUF_INIT;
1045 if (p->status == 'D' || p->status == 'T') {
1046 generate_submodule_summary(info, p);
1047 continue;
1050 if (info->for_status && p->status != 'A' &&
1051 (sub = submodule_from_path(the_repository,
1052 null_oid(), p->sm_path))) {
1053 char *config_key = NULL;
1054 const char *value;
1055 int ignore_all = 0;
1057 config_key = xstrfmt("submodule.%s.ignore",
1058 sub->name);
1059 if (!git_config_get_string_tmp(config_key, &value))
1060 ignore_all = !strcmp(value, "all");
1061 else if (sub->ignore)
1062 ignore_all = !strcmp(sub->ignore, "all");
1064 free(config_key);
1065 if (ignore_all)
1066 continue;
1069 /* Also show added or modified modules which are checked out */
1070 strbuf_addstr(&sm_gitdir, p->sm_path);
1071 if (is_nonbare_repository_dir(&sm_gitdir))
1072 generate_submodule_summary(info, p);
1073 strbuf_release(&sm_gitdir);
1077 static void submodule_summary_callback(struct diff_queue_struct *q,
1078 struct diff_options *options,
1079 void *data)
1081 int i;
1082 struct module_cb_list *list = data;
1083 for (i = 0; i < q->nr; i++) {
1084 struct diff_filepair *p = q->queue[i];
1085 struct module_cb *temp;
1087 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1088 continue;
1089 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1090 temp->mod_src = p->one->mode;
1091 temp->mod_dst = p->two->mode;
1092 temp->oid_src = p->one->oid;
1093 temp->oid_dst = p->two->oid;
1094 temp->status = p->status;
1095 temp->sm_path = xstrdup(p->one->path);
1097 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1098 list->entries[list->nr++] = temp;
1102 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1104 switch (diff_cmd) {
1105 case DIFF_INDEX: return "diff-index";
1106 case DIFF_FILES: return "diff-files";
1107 default: BUG("bad diff_cmd value %d", diff_cmd);
1111 static int compute_summary_module_list(struct object_id *head_oid,
1112 struct summary_cb *info,
1113 enum diff_cmd diff_cmd)
1115 struct strvec diff_args = STRVEC_INIT;
1116 struct rev_info rev;
1117 struct module_cb_list list = MODULE_CB_LIST_INIT;
1118 int ret = 0;
1120 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1121 if (info->cached)
1122 strvec_push(&diff_args, "--cached");
1123 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1124 if (head_oid)
1125 strvec_push(&diff_args, oid_to_hex(head_oid));
1126 strvec_push(&diff_args, "--");
1127 if (info->argc)
1128 strvec_pushv(&diff_args, info->argv);
1130 git_config(git_diff_basic_config, NULL);
1131 init_revisions(&rev, info->prefix);
1132 rev.abbrev = 0;
1133 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1134 setup_revisions(diff_args.nr, diff_args.v, &rev, NULL);
1135 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1136 rev.diffopt.format_callback = submodule_summary_callback;
1137 rev.diffopt.format_callback_data = &list;
1139 if (!info->cached) {
1140 if (diff_cmd == DIFF_INDEX)
1141 setup_work_tree();
1142 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1143 perror("read_cache_preload");
1144 ret = -1;
1145 goto cleanup;
1147 } else if (read_cache() < 0) {
1148 perror("read_cache");
1149 ret = -1;
1150 goto cleanup;
1153 if (diff_cmd == DIFF_INDEX)
1154 run_diff_index(&rev, info->cached);
1155 else
1156 run_diff_files(&rev, 0);
1157 prepare_submodule_summary(info, &list);
1158 cleanup:
1159 strvec_clear(&diff_args);
1160 release_revisions(&rev);
1161 return ret;
1164 static int module_summary(int argc, const char **argv, const char *prefix)
1166 struct summary_cb info = SUMMARY_CB_INIT;
1167 int cached = 0;
1168 int for_status = 0;
1169 int files = 0;
1170 int summary_limit = -1;
1171 enum diff_cmd diff_cmd = DIFF_INDEX;
1172 struct object_id head_oid;
1173 int ret;
1175 struct option module_summary_options[] = {
1176 OPT_BOOL(0, "cached", &cached,
1177 N_("use the commit stored in the index instead of the submodule HEAD")),
1178 OPT_BOOL(0, "files", &files,
1179 N_("compare the commit in the index with that in the submodule HEAD")),
1180 OPT_BOOL(0, "for-status", &for_status,
1181 N_("skip submodules with 'ignore_config' value set to 'all'")),
1182 OPT_INTEGER('n', "summary-limit", &summary_limit,
1183 N_("limit the summary size")),
1184 OPT_END()
1187 const char *const git_submodule_helper_usage[] = {
1188 N_("git submodule--helper summary [<options>] [<commit>] [--] [<path>]"),
1189 NULL
1192 argc = parse_options(argc, argv, prefix, module_summary_options,
1193 git_submodule_helper_usage, 0);
1195 if (!summary_limit)
1196 return 0;
1198 if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) {
1199 if (argc) {
1200 argv++;
1201 argc--;
1203 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1204 /* before the first commit: compare with an empty tree */
1205 oidcpy(&head_oid, the_hash_algo->empty_tree);
1206 if (argc) {
1207 argv++;
1208 argc--;
1210 } else {
1211 if (get_oid("HEAD", &head_oid))
1212 die(_("could not fetch a revision for HEAD"));
1215 if (files) {
1216 if (cached)
1217 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1218 diff_cmd = DIFF_FILES;
1221 info.argc = argc;
1222 info.argv = argv;
1223 info.prefix = prefix;
1224 info.cached = !!cached;
1225 info.files = !!files;
1226 info.for_status = !!for_status;
1227 info.summary_limit = summary_limit;
1229 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1230 &info, diff_cmd);
1231 return ret;
1234 struct sync_cb {
1235 const char *prefix;
1236 unsigned int flags;
1238 #define SYNC_CB_INIT { 0 }
1240 static void sync_submodule(const char *path, const char *prefix,
1241 unsigned int flags)
1243 const struct submodule *sub;
1244 char *remote_key = NULL;
1245 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1246 struct strbuf sb = STRBUF_INIT;
1247 char *sub_config_path = NULL;
1249 if (!is_submodule_active(the_repository, path))
1250 return;
1252 sub = submodule_from_path(the_repository, null_oid(), path);
1254 if (sub && sub->url) {
1255 if (starts_with_dot_dot_slash(sub->url) ||
1256 starts_with_dot_slash(sub->url)) {
1257 char *up_path = get_up_path(path);
1258 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1259 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1260 free(up_path);
1261 } else {
1262 sub_origin_url = xstrdup(sub->url);
1263 super_config_url = xstrdup(sub->url);
1265 } else {
1266 sub_origin_url = xstrdup("");
1267 super_config_url = xstrdup("");
1270 displaypath = get_submodule_displaypath(path, prefix);
1272 if (!(flags & OPT_QUIET))
1273 printf(_("Synchronizing submodule url for '%s'\n"),
1274 displaypath);
1276 strbuf_reset(&sb);
1277 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1278 if (git_config_set_gently(sb.buf, super_config_url))
1279 die(_("failed to register url for submodule path '%s'"),
1280 displaypath);
1282 if (!is_submodule_populated_gently(path, NULL))
1283 goto cleanup;
1285 strbuf_reset(&sb);
1286 default_remote = get_default_remote_submodule(path);
1287 if (!default_remote)
1288 die(_("failed to get the default remote for submodule '%s'"),
1289 path);
1291 remote_key = xstrfmt("remote.%s.url", default_remote);
1292 free(default_remote);
1294 submodule_to_gitdir(&sb, path);
1295 strbuf_addstr(&sb, "/config");
1297 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1298 die(_("failed to update remote for submodule '%s'"),
1299 path);
1301 if (flags & OPT_RECURSIVE) {
1302 struct child_process cpr = CHILD_PROCESS_INIT;
1304 cpr.git_cmd = 1;
1305 cpr.dir = path;
1306 prepare_submodule_repo_env(&cpr.env_array);
1308 strvec_push(&cpr.args, "--super-prefix");
1309 strvec_pushf(&cpr.args, "%s/", displaypath);
1310 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1311 "--recursive", NULL);
1313 if (flags & OPT_QUIET)
1314 strvec_push(&cpr.args, "--quiet");
1316 if (run_command(&cpr))
1317 die(_("failed to recurse into submodule '%s'"),
1318 path);
1321 cleanup:
1322 free(super_config_url);
1323 free(sub_origin_url);
1324 strbuf_release(&sb);
1325 free(remote_key);
1326 free(displaypath);
1327 free(sub_config_path);
1330 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1332 struct sync_cb *info = cb_data;
1333 sync_submodule(list_item->name, info->prefix, info->flags);
1336 static int module_sync(int argc, const char **argv, const char *prefix)
1338 struct sync_cb info = SYNC_CB_INIT;
1339 struct pathspec pathspec;
1340 struct module_list list = MODULE_LIST_INIT;
1341 int quiet = 0;
1342 int recursive = 0;
1344 struct option module_sync_options[] = {
1345 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1346 OPT_BOOL(0, "recursive", &recursive,
1347 N_("recurse into nested submodules")),
1348 OPT_END()
1351 const char *const git_submodule_helper_usage[] = {
1352 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1353 NULL
1356 argc = parse_options(argc, argv, prefix, module_sync_options,
1357 git_submodule_helper_usage, 0);
1359 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1360 return 1;
1362 info.prefix = prefix;
1363 if (quiet)
1364 info.flags |= OPT_QUIET;
1365 if (recursive)
1366 info.flags |= OPT_RECURSIVE;
1368 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1370 return 0;
1373 struct deinit_cb {
1374 const char *prefix;
1375 unsigned int flags;
1377 #define DEINIT_CB_INIT { 0 }
1379 static void deinit_submodule(const char *path, const char *prefix,
1380 unsigned int flags)
1382 const struct submodule *sub;
1383 char *displaypath = NULL;
1384 struct child_process cp_config = CHILD_PROCESS_INIT;
1385 struct strbuf sb_config = STRBUF_INIT;
1386 char *sub_git_dir = xstrfmt("%s/.git", path);
1388 sub = submodule_from_path(the_repository, null_oid(), path);
1390 if (!sub || !sub->name)
1391 goto cleanup;
1393 displaypath = get_submodule_displaypath(path, prefix);
1395 /* remove the submodule work tree (unless the user already did it) */
1396 if (is_directory(path)) {
1397 struct strbuf sb_rm = STRBUF_INIT;
1398 const char *format;
1400 if (is_directory(sub_git_dir)) {
1401 if (!(flags & OPT_QUIET))
1402 warning(_("Submodule work tree '%s' contains a .git "
1403 "directory. This will be replaced with a "
1404 ".git file by using absorbgitdirs."),
1405 displaypath);
1407 absorb_git_dir_into_superproject(path,
1408 ABSORB_GITDIR_RECURSE_SUBMODULES);
1412 if (!(flags & OPT_FORCE)) {
1413 struct child_process cp_rm = CHILD_PROCESS_INIT;
1414 cp_rm.git_cmd = 1;
1415 strvec_pushl(&cp_rm.args, "rm", "-qn",
1416 path, NULL);
1418 if (run_command(&cp_rm))
1419 die(_("Submodule work tree '%s' contains local "
1420 "modifications; use '-f' to discard them"),
1421 displaypath);
1424 strbuf_addstr(&sb_rm, path);
1426 if (!remove_dir_recursively(&sb_rm, 0))
1427 format = _("Cleared directory '%s'\n");
1428 else
1429 format = _("Could not remove submodule work tree '%s'\n");
1431 if (!(flags & OPT_QUIET))
1432 printf(format, displaypath);
1434 submodule_unset_core_worktree(sub);
1436 strbuf_release(&sb_rm);
1439 if (mkdir(path, 0777))
1440 printf(_("could not create empty submodule directory %s"),
1441 displaypath);
1443 cp_config.git_cmd = 1;
1444 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1445 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1447 /* remove the .git/config entries (unless the user already did it) */
1448 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1449 char *sub_key = xstrfmt("submodule.%s", sub->name);
1451 * remove the whole section so we have a clean state when
1452 * the user later decides to init this submodule again
1454 git_config_rename_section_in_file(NULL, sub_key, NULL);
1455 if (!(flags & OPT_QUIET))
1456 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1457 sub->name, sub->url, displaypath);
1458 free(sub_key);
1461 cleanup:
1462 free(displaypath);
1463 free(sub_git_dir);
1464 strbuf_release(&sb_config);
1467 static void deinit_submodule_cb(const struct cache_entry *list_item,
1468 void *cb_data)
1470 struct deinit_cb *info = cb_data;
1471 deinit_submodule(list_item->name, info->prefix, info->flags);
1474 static int module_deinit(int argc, const char **argv, const char *prefix)
1476 struct deinit_cb info = DEINIT_CB_INIT;
1477 struct pathspec pathspec;
1478 struct module_list list = MODULE_LIST_INIT;
1479 int quiet = 0;
1480 int force = 0;
1481 int all = 0;
1483 struct option module_deinit_options[] = {
1484 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1485 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1486 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1487 OPT_END()
1490 const char *const git_submodule_helper_usage[] = {
1491 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1492 NULL
1495 argc = parse_options(argc, argv, prefix, module_deinit_options,
1496 git_submodule_helper_usage, 0);
1498 if (all && argc) {
1499 error("pathspec and --all are incompatible");
1500 usage_with_options(git_submodule_helper_usage,
1501 module_deinit_options);
1504 if (!argc && !all)
1505 die(_("Use '--all' if you really want to deinitialize all submodules"));
1507 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1508 return 1;
1510 info.prefix = prefix;
1511 if (quiet)
1512 info.flags |= OPT_QUIET;
1513 if (force)
1514 info.flags |= OPT_FORCE;
1516 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1518 return 0;
1521 struct module_clone_data {
1522 const char *prefix;
1523 const char *path;
1524 const char *name;
1525 const char *url;
1526 const char *depth;
1527 struct list_objects_filter_options *filter_options;
1528 struct string_list reference;
1529 unsigned int quiet: 1;
1530 unsigned int progress: 1;
1531 unsigned int dissociate: 1;
1532 unsigned int require_init: 1;
1533 int single_branch;
1535 #define MODULE_CLONE_DATA_INIT { \
1536 .reference = STRING_LIST_INIT_NODUP, \
1537 .single_branch = -1, \
1540 struct submodule_alternate_setup {
1541 const char *submodule_name;
1542 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1543 SUBMODULE_ALTERNATE_ERROR_DIE,
1544 SUBMODULE_ALTERNATE_ERROR_INFO,
1545 SUBMODULE_ALTERNATE_ERROR_IGNORE
1546 } error_mode;
1547 struct string_list *reference;
1549 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1550 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1553 static const char alternate_error_advice[] = N_(
1554 "An alternate computed from a superproject's alternate is invalid.\n"
1555 "To allow Git to clone without an alternate in such a case, set\n"
1556 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1557 "'--reference-if-able' instead of '--reference'."
1560 static int add_possible_reference_from_superproject(
1561 struct object_directory *odb, void *sas_cb)
1563 struct submodule_alternate_setup *sas = sas_cb;
1564 size_t len;
1567 * If the alternate object store is another repository, try the
1568 * standard layout with .git/(modules/<name>)+/objects
1570 if (strip_suffix(odb->path, "/objects", &len)) {
1571 struct repository alternate;
1572 char *sm_alternate;
1573 struct strbuf sb = STRBUF_INIT;
1574 struct strbuf err = STRBUF_INIT;
1575 strbuf_add(&sb, odb->path, len);
1577 repo_init(&alternate, sb.buf, NULL);
1580 * We need to end the new path with '/' to mark it as a dir,
1581 * otherwise a submodule name containing '/' will be broken
1582 * as the last part of a missing submodule reference would
1583 * be taken as a file name.
1585 strbuf_reset(&sb);
1586 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1587 strbuf_addch(&sb, '/');
1588 repo_clear(&alternate);
1590 sm_alternate = compute_alternate_path(sb.buf, &err);
1591 if (sm_alternate) {
1592 string_list_append(sas->reference, xstrdup(sb.buf));
1593 free(sm_alternate);
1594 } else {
1595 switch (sas->error_mode) {
1596 case SUBMODULE_ALTERNATE_ERROR_DIE:
1597 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1598 advise(_(alternate_error_advice));
1599 die(_("submodule '%s' cannot add alternate: %s"),
1600 sas->submodule_name, err.buf);
1601 case SUBMODULE_ALTERNATE_ERROR_INFO:
1602 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1603 sas->submodule_name, err.buf);
1604 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1605 ; /* nothing */
1608 strbuf_release(&sb);
1611 return 0;
1614 static void prepare_possible_alternates(const char *sm_name,
1615 struct string_list *reference)
1617 char *sm_alternate = NULL, *error_strategy = NULL;
1618 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1620 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1621 if (!sm_alternate)
1622 return;
1624 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1626 if (!error_strategy)
1627 error_strategy = xstrdup("die");
1629 sas.submodule_name = sm_name;
1630 sas.reference = reference;
1631 if (!strcmp(error_strategy, "die"))
1632 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1633 else if (!strcmp(error_strategy, "info"))
1634 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1635 else if (!strcmp(error_strategy, "ignore"))
1636 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1637 else
1638 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1640 if (!strcmp(sm_alternate, "superproject"))
1641 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1642 else if (!strcmp(sm_alternate, "no"))
1643 ; /* do nothing */
1644 else
1645 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1647 free(sm_alternate);
1648 free(error_strategy);
1651 static int clone_submodule(struct module_clone_data *clone_data)
1653 char *p, *sm_gitdir;
1654 char *sm_alternate = NULL, *error_strategy = NULL;
1655 struct strbuf sb = STRBUF_INIT;
1656 struct child_process cp = CHILD_PROCESS_INIT;
1658 submodule_name_to_gitdir(&sb, the_repository, clone_data->name);
1659 sm_gitdir = absolute_pathdup(sb.buf);
1660 strbuf_reset(&sb);
1662 if (!is_absolute_path(clone_data->path)) {
1663 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), clone_data->path);
1664 clone_data->path = strbuf_detach(&sb, NULL);
1665 } else {
1666 clone_data->path = xstrdup(clone_data->path);
1669 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1670 die(_("refusing to create/use '%s' in another submodule's "
1671 "git dir"), sm_gitdir);
1673 if (!file_exists(sm_gitdir)) {
1674 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1675 die(_("could not create directory '%s'"), sm_gitdir);
1677 prepare_possible_alternates(clone_data->name, &clone_data->reference);
1679 strvec_push(&cp.args, "clone");
1680 strvec_push(&cp.args, "--no-checkout");
1681 if (clone_data->quiet)
1682 strvec_push(&cp.args, "--quiet");
1683 if (clone_data->progress)
1684 strvec_push(&cp.args, "--progress");
1685 if (clone_data->depth && *(clone_data->depth))
1686 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1687 if (clone_data->reference.nr) {
1688 struct string_list_item *item;
1689 for_each_string_list_item(item, &clone_data->reference)
1690 strvec_pushl(&cp.args, "--reference",
1691 item->string, NULL);
1693 if (clone_data->dissociate)
1694 strvec_push(&cp.args, "--dissociate");
1695 if (sm_gitdir && *sm_gitdir)
1696 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1697 if (clone_data->filter_options && clone_data->filter_options->choice)
1698 strvec_pushf(&cp.args, "--filter=%s",
1699 expand_list_objects_filter_spec(
1700 clone_data->filter_options));
1701 if (clone_data->single_branch >= 0)
1702 strvec_push(&cp.args, clone_data->single_branch ?
1703 "--single-branch" :
1704 "--no-single-branch");
1706 strvec_push(&cp.args, "--");
1707 strvec_push(&cp.args, clone_data->url);
1708 strvec_push(&cp.args, clone_data->path);
1710 cp.git_cmd = 1;
1711 prepare_submodule_repo_env(&cp.env_array);
1712 cp.no_stdin = 1;
1714 if(run_command(&cp))
1715 die(_("clone of '%s' into submodule path '%s' failed"),
1716 clone_data->url, clone_data->path);
1717 } else {
1718 if (clone_data->require_init && !access(clone_data->path, X_OK) &&
1719 !is_empty_dir(clone_data->path))
1720 die(_("directory not empty: '%s'"), clone_data->path);
1721 if (safe_create_leading_directories_const(clone_data->path) < 0)
1722 die(_("could not create directory '%s'"), clone_data->path);
1723 strbuf_addf(&sb, "%s/index", sm_gitdir);
1724 unlink_or_warn(sb.buf);
1725 strbuf_reset(&sb);
1728 connect_work_tree_and_git_dir(clone_data->path, sm_gitdir, 0);
1730 p = git_pathdup_submodule(clone_data->path, "config");
1731 if (!p)
1732 die(_("could not get submodule directory for '%s'"), clone_data->path);
1734 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1735 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1736 if (sm_alternate)
1737 git_config_set_in_file(p, "submodule.alternateLocation",
1738 sm_alternate);
1739 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1740 if (error_strategy)
1741 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1742 error_strategy);
1744 free(sm_alternate);
1745 free(error_strategy);
1747 strbuf_release(&sb);
1748 free(sm_gitdir);
1749 free(p);
1750 return 0;
1753 static int module_clone(int argc, const char **argv, const char *prefix)
1755 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1756 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1757 struct list_objects_filter_options filter_options;
1759 struct option module_clone_options[] = {
1760 OPT_STRING(0, "prefix", &clone_data.prefix,
1761 N_("path"),
1762 N_("alternative anchor for relative paths")),
1763 OPT_STRING(0, "path", &clone_data.path,
1764 N_("path"),
1765 N_("where the new submodule will be cloned to")),
1766 OPT_STRING(0, "name", &clone_data.name,
1767 N_("string"),
1768 N_("name of the new submodule")),
1769 OPT_STRING(0, "url", &clone_data.url,
1770 N_("string"),
1771 N_("url where to clone the submodule from")),
1772 OPT_STRING_LIST(0, "reference", &clone_data.reference,
1773 N_("repo"),
1774 N_("reference repository")),
1775 OPT_BOOL(0, "dissociate", &dissociate,
1776 N_("use --reference only while cloning")),
1777 OPT_STRING(0, "depth", &clone_data.depth,
1778 N_("string"),
1779 N_("depth for shallow clones")),
1780 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1781 OPT_BOOL(0, "progress", &progress,
1782 N_("force cloning progress")),
1783 OPT_BOOL(0, "require-init", &require_init,
1784 N_("disallow cloning into non-empty directory")),
1785 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1786 N_("clone only one branch, HEAD or --branch")),
1787 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1788 OPT_END()
1791 const char *const git_submodule_helper_usage[] = {
1792 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1793 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1794 "[--single-branch] [--filter <filter-spec>] "
1795 "--url <url> --path <path>"),
1796 NULL
1799 memset(&filter_options, 0, sizeof(filter_options));
1800 argc = parse_options(argc, argv, prefix, module_clone_options,
1801 git_submodule_helper_usage, 0);
1803 clone_data.dissociate = !!dissociate;
1804 clone_data.quiet = !!quiet;
1805 clone_data.progress = !!progress;
1806 clone_data.require_init = !!require_init;
1807 clone_data.filter_options = &filter_options;
1809 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1810 usage_with_options(git_submodule_helper_usage,
1811 module_clone_options);
1813 clone_submodule(&clone_data);
1814 list_objects_filter_release(&filter_options);
1815 return 0;
1818 static void determine_submodule_update_strategy(struct repository *r,
1819 int just_cloned,
1820 const char *path,
1821 const char *update,
1822 struct submodule_update_strategy *out)
1824 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1825 char *key;
1826 const char *val;
1828 key = xstrfmt("submodule.%s.update", sub->name);
1830 if (update) {
1831 if (parse_submodule_update_strategy(update, out) < 0)
1832 die(_("Invalid update mode '%s' for submodule path '%s'"),
1833 update, path);
1834 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1835 if (parse_submodule_update_strategy(val, out) < 0)
1836 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1837 val, path);
1838 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1839 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1840 BUG("how did we read update = !command from .gitmodules?");
1841 out->type = sub->update_strategy.type;
1842 out->command = sub->update_strategy.command;
1843 } else
1844 out->type = SM_UPDATE_CHECKOUT;
1846 if (just_cloned &&
1847 (out->type == SM_UPDATE_MERGE ||
1848 out->type == SM_UPDATE_REBASE ||
1849 out->type == SM_UPDATE_NONE))
1850 out->type = SM_UPDATE_CHECKOUT;
1852 free(key);
1855 struct update_clone_data {
1856 const struct submodule *sub;
1857 struct object_id oid;
1858 unsigned just_cloned;
1861 struct submodule_update_clone {
1862 /* index into 'update_data.list', the list of submodules to look into for cloning */
1863 int current;
1865 /* configuration parameters which are passed on to the children */
1866 struct update_data *update_data;
1868 /* to be consumed by update_submodule() */
1869 struct update_clone_data *update_clone;
1870 int update_clone_nr; int update_clone_alloc;
1872 /* If we want to stop as fast as possible and return an error */
1873 unsigned quickstop : 1;
1875 /* failed clones to be retried again */
1876 const struct cache_entry **failed_clones;
1877 int failed_clones_nr, failed_clones_alloc;
1879 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1881 struct update_data {
1882 const char *prefix;
1883 const char *recursive_prefix;
1884 const char *displaypath;
1885 const char *update_default;
1886 struct object_id suboid;
1887 struct string_list references;
1888 struct submodule_update_strategy update_strategy;
1889 struct list_objects_filter_options *filter_options;
1890 struct module_list list;
1891 int depth;
1892 int max_jobs;
1893 int single_branch;
1894 int recommend_shallow;
1895 unsigned int require_init;
1896 unsigned int force;
1897 unsigned int quiet;
1898 unsigned int nofetch;
1899 unsigned int remote;
1900 unsigned int progress;
1901 unsigned int dissociate;
1902 unsigned int init;
1903 unsigned int warn_if_uninitialized;
1904 unsigned int recursive;
1906 /* copied over from update_clone_data */
1907 struct object_id oid;
1908 unsigned int just_cloned;
1909 const char *sm_path;
1911 #define UPDATE_DATA_INIT { \
1912 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1913 .list = MODULE_LIST_INIT, \
1914 .recommend_shallow = -1, \
1915 .references = STRING_LIST_INIT_DUP, \
1916 .single_branch = -1, \
1917 .max_jobs = 1, \
1920 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1921 struct strbuf *out, const char *displaypath)
1924 * Only mention uninitialized submodules when their
1925 * paths have been specified.
1927 if (suc->update_data->warn_if_uninitialized) {
1928 strbuf_addf(out,
1929 _("Submodule path '%s' not initialized"),
1930 displaypath);
1931 strbuf_addch(out, '\n');
1932 strbuf_addstr(out,
1933 _("Maybe you want to use 'update --init'?"));
1934 strbuf_addch(out, '\n');
1939 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1940 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1942 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1943 struct child_process *child,
1944 struct submodule_update_clone *suc,
1945 struct strbuf *out)
1947 const struct submodule *sub = NULL;
1948 const char *url = NULL;
1949 const char *update_string;
1950 enum submodule_update_type update_type;
1951 char *key;
1952 struct strbuf displaypath_sb = STRBUF_INIT;
1953 struct strbuf sb = STRBUF_INIT;
1954 const char *displaypath = NULL;
1955 int needs_cloning = 0;
1956 int need_free_url = 0;
1958 if (ce_stage(ce)) {
1959 if (suc->update_data->recursive_prefix)
1960 strbuf_addf(&sb, "%s/%s", suc->update_data->recursive_prefix, ce->name);
1961 else
1962 strbuf_addstr(&sb, ce->name);
1963 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1964 strbuf_addch(out, '\n');
1965 goto cleanup;
1968 sub = submodule_from_path(the_repository, null_oid(), ce->name);
1970 if (suc->update_data->recursive_prefix)
1971 displaypath = relative_path(suc->update_data->recursive_prefix,
1972 ce->name, &displaypath_sb);
1973 else
1974 displaypath = ce->name;
1976 if (!sub) {
1977 next_submodule_warn_missing(suc, out, displaypath);
1978 goto cleanup;
1981 key = xstrfmt("submodule.%s.update", sub->name);
1982 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
1983 update_type = parse_submodule_update_type(update_string);
1984 } else {
1985 update_type = sub->update_strategy.type;
1987 free(key);
1989 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
1990 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
1991 && update_type == SM_UPDATE_NONE)) {
1992 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1993 strbuf_addch(out, '\n');
1994 goto cleanup;
1997 /* Check if the submodule has been initialized. */
1998 if (!is_submodule_active(the_repository, ce->name)) {
1999 next_submodule_warn_missing(suc, out, displaypath);
2000 goto cleanup;
2003 strbuf_reset(&sb);
2004 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2005 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2006 if (starts_with_dot_slash(sub->url) ||
2007 starts_with_dot_dot_slash(sub->url)) {
2008 url = resolve_relative_url(sub->url, NULL, 0);
2009 need_free_url = 1;
2010 } else
2011 url = sub->url;
2014 strbuf_reset(&sb);
2015 strbuf_addf(&sb, "%s/.git", ce->name);
2016 needs_cloning = !file_exists(sb.buf);
2018 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2019 suc->update_clone_alloc);
2020 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2021 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2022 suc->update_clone[suc->update_clone_nr].sub = sub;
2023 suc->update_clone_nr++;
2025 if (!needs_cloning)
2026 goto cleanup;
2028 child->git_cmd = 1;
2029 child->no_stdin = 1;
2030 child->stdout_to_stderr = 1;
2031 child->err = -1;
2032 strvec_push(&child->args, "submodule--helper");
2033 strvec_push(&child->args, "clone");
2034 if (suc->update_data->progress)
2035 strvec_push(&child->args, "--progress");
2036 if (suc->update_data->quiet)
2037 strvec_push(&child->args, "--quiet");
2038 if (suc->update_data->prefix)
2039 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2040 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2041 strvec_push(&child->args, "--depth=1");
2042 else if (suc->update_data->depth)
2043 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2044 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2045 strvec_pushf(&child->args, "--filter=%s",
2046 expand_list_objects_filter_spec(suc->update_data->filter_options));
2047 if (suc->update_data->require_init)
2048 strvec_push(&child->args, "--require-init");
2049 strvec_pushl(&child->args, "--path", sub->path, NULL);
2050 strvec_pushl(&child->args, "--name", sub->name, NULL);
2051 strvec_pushl(&child->args, "--url", url, NULL);
2052 if (suc->update_data->references.nr) {
2053 struct string_list_item *item;
2054 for_each_string_list_item(item, &suc->update_data->references)
2055 strvec_pushl(&child->args, "--reference", item->string, NULL);
2057 if (suc->update_data->dissociate)
2058 strvec_push(&child->args, "--dissociate");
2059 if (suc->update_data->single_branch >= 0)
2060 strvec_push(&child->args, suc->update_data->single_branch ?
2061 "--single-branch" :
2062 "--no-single-branch");
2064 cleanup:
2065 strbuf_release(&displaypath_sb);
2066 strbuf_release(&sb);
2067 if (need_free_url)
2068 free((void*)url);
2070 return needs_cloning;
2073 static int update_clone_get_next_task(struct child_process *child,
2074 struct strbuf *err,
2075 void *suc_cb,
2076 void **idx_task_cb)
2078 struct submodule_update_clone *suc = suc_cb;
2079 const struct cache_entry *ce;
2080 int index;
2082 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2083 ce = suc->update_data->list.entries[suc->current];
2084 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2085 int *p = xmalloc(sizeof(*p));
2086 *p = suc->current;
2087 *idx_task_cb = p;
2088 suc->current++;
2089 return 1;
2094 * The loop above tried cloning each submodule once, now try the
2095 * stragglers again, which we can imagine as an extension of the
2096 * entry list.
2098 index = suc->current - suc->update_data->list.nr;
2099 if (index < suc->failed_clones_nr) {
2100 int *p;
2101 ce = suc->failed_clones[index];
2102 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2103 suc->current ++;
2104 strbuf_addstr(err, "BUG: submodule considered for "
2105 "cloning, doesn't need cloning "
2106 "any more?\n");
2107 return 0;
2109 p = xmalloc(sizeof(*p));
2110 *p = suc->current;
2111 *idx_task_cb = p;
2112 suc->current ++;
2113 return 1;
2116 return 0;
2119 static int update_clone_start_failure(struct strbuf *err,
2120 void *suc_cb,
2121 void *idx_task_cb)
2123 struct submodule_update_clone *suc = suc_cb;
2124 suc->quickstop = 1;
2125 return 1;
2128 static int update_clone_task_finished(int result,
2129 struct strbuf *err,
2130 void *suc_cb,
2131 void *idx_task_cb)
2133 const struct cache_entry *ce;
2134 struct submodule_update_clone *suc = suc_cb;
2136 int *idxP = idx_task_cb;
2137 int idx = *idxP;
2138 free(idxP);
2140 if (!result)
2141 return 0;
2143 if (idx < suc->update_data->list.nr) {
2144 ce = suc->update_data->list.entries[idx];
2145 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2146 ce->name);
2147 strbuf_addch(err, '\n');
2148 ALLOC_GROW(suc->failed_clones,
2149 suc->failed_clones_nr + 1,
2150 suc->failed_clones_alloc);
2151 suc->failed_clones[suc->failed_clones_nr++] = ce;
2152 return 0;
2153 } else {
2154 idx -= suc->update_data->list.nr;
2155 ce = suc->failed_clones[idx];
2156 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2157 ce->name);
2158 strbuf_addch(err, '\n');
2159 suc->quickstop = 1;
2160 return 1;
2163 return 0;
2166 static int git_update_clone_config(const char *var, const char *value,
2167 void *cb)
2169 int *max_jobs = cb;
2170 if (!strcmp(var, "submodule.fetchjobs"))
2171 *max_jobs = parse_submodule_fetchjobs(var, value);
2172 return 0;
2175 static int is_tip_reachable(const char *path, struct object_id *oid)
2177 struct child_process cp = CHILD_PROCESS_INIT;
2178 struct strbuf rev = STRBUF_INIT;
2179 char *hex = oid_to_hex(oid);
2181 cp.git_cmd = 1;
2182 cp.dir = xstrdup(path);
2183 cp.no_stderr = 1;
2184 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2186 prepare_submodule_repo_env(&cp.env_array);
2188 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2189 return 0;
2191 return 1;
2194 static int fetch_in_submodule(const char *module_path, int depth, int quiet, struct object_id *oid)
2196 struct child_process cp = CHILD_PROCESS_INIT;
2198 prepare_submodule_repo_env(&cp.env_array);
2199 cp.git_cmd = 1;
2200 cp.dir = xstrdup(module_path);
2202 strvec_push(&cp.args, "fetch");
2203 if (quiet)
2204 strvec_push(&cp.args, "--quiet");
2205 if (depth)
2206 strvec_pushf(&cp.args, "--depth=%d", depth);
2207 if (oid) {
2208 char *hex = oid_to_hex(oid);
2209 char *remote = get_default_remote();
2210 strvec_pushl(&cp.args, remote, hex, NULL);
2213 return run_command(&cp);
2216 static int run_update_command(struct update_data *ud, int subforce)
2218 struct child_process cp = CHILD_PROCESS_INIT;
2219 char *oid = oid_to_hex(&ud->oid);
2220 int must_die_on_failure = 0;
2222 switch (ud->update_strategy.type) {
2223 case SM_UPDATE_CHECKOUT:
2224 cp.git_cmd = 1;
2225 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2226 if (subforce)
2227 strvec_push(&cp.args, "-f");
2228 break;
2229 case SM_UPDATE_REBASE:
2230 cp.git_cmd = 1;
2231 strvec_push(&cp.args, "rebase");
2232 if (ud->quiet)
2233 strvec_push(&cp.args, "--quiet");
2234 must_die_on_failure = 1;
2235 break;
2236 case SM_UPDATE_MERGE:
2237 cp.git_cmd = 1;
2238 strvec_push(&cp.args, "merge");
2239 if (ud->quiet)
2240 strvec_push(&cp.args, "--quiet");
2241 must_die_on_failure = 1;
2242 break;
2243 case SM_UPDATE_COMMAND:
2244 cp.use_shell = 1;
2245 strvec_push(&cp.args, ud->update_strategy.command);
2246 must_die_on_failure = 1;
2247 break;
2248 default:
2249 BUG("unexpected update strategy type: %s",
2250 submodule_strategy_to_string(&ud->update_strategy));
2252 strvec_push(&cp.args, oid);
2254 cp.dir = xstrdup(ud->sm_path);
2255 prepare_submodule_repo_env(&cp.env_array);
2256 if (run_command(&cp)) {
2257 switch (ud->update_strategy.type) {
2258 case SM_UPDATE_CHECKOUT:
2259 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2260 oid, ud->displaypath);
2261 break;
2262 case SM_UPDATE_REBASE:
2263 die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2264 oid, ud->displaypath);
2265 break;
2266 case SM_UPDATE_MERGE:
2267 die_message(_("Unable to merge '%s' in submodule path '%s'"),
2268 oid, ud->displaypath);
2269 break;
2270 case SM_UPDATE_COMMAND:
2271 die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2272 ud->update_strategy.command, oid, ud->displaypath);
2273 break;
2274 default:
2275 BUG("unexpected update strategy type: %s",
2276 submodule_strategy_to_string(&ud->update_strategy));
2278 if (must_die_on_failure)
2279 exit(128);
2281 /* the command failed, but update must continue */
2282 return 1;
2285 if (ud->quiet)
2286 return 0;
2288 switch (ud->update_strategy.type) {
2289 case SM_UPDATE_CHECKOUT:
2290 printf(_("Submodule path '%s': checked out '%s'\n"),
2291 ud->displaypath, oid);
2292 break;
2293 case SM_UPDATE_REBASE:
2294 printf(_("Submodule path '%s': rebased into '%s'\n"),
2295 ud->displaypath, oid);
2296 break;
2297 case SM_UPDATE_MERGE:
2298 printf(_("Submodule path '%s': merged in '%s'\n"),
2299 ud->displaypath, oid);
2300 break;
2301 case SM_UPDATE_COMMAND:
2302 printf(_("Submodule path '%s': '%s %s'\n"),
2303 ud->displaypath, ud->update_strategy.command, oid);
2304 break;
2305 default:
2306 BUG("unexpected update strategy type: %s",
2307 submodule_strategy_to_string(&ud->update_strategy));
2310 return 0;
2313 static int run_update_procedure(struct update_data *ud)
2315 int subforce = is_null_oid(&ud->suboid) || ud->force;
2317 if (!ud->nofetch) {
2319 * Run fetch only if `oid` isn't present or it
2320 * is not reachable from a ref.
2322 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2323 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2324 !ud->quiet)
2325 fprintf_ln(stderr,
2326 _("Unable to fetch in submodule path '%s'; "
2327 "trying to directly fetch %s:"),
2328 ud->displaypath, oid_to_hex(&ud->oid));
2330 * Now we tried the usual fetch, but `oid` may
2331 * not be reachable from any of the refs.
2333 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2334 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2335 die(_("Fetched in submodule path '%s', but it did not "
2336 "contain %s. Direct fetching of that commit failed."),
2337 ud->displaypath, oid_to_hex(&ud->oid));
2340 return run_update_command(ud, subforce);
2343 static const char *remote_submodule_branch(const char *path)
2345 const struct submodule *sub;
2346 const char *branch = NULL;
2347 char *key;
2349 sub = submodule_from_path(the_repository, null_oid(), path);
2350 if (!sub)
2351 return NULL;
2353 key = xstrfmt("submodule.%s.branch", sub->name);
2354 if (repo_config_get_string_tmp(the_repository, key, &branch))
2355 branch = sub->branch;
2356 free(key);
2358 if (!branch)
2359 return "HEAD";
2361 if (!strcmp(branch, ".")) {
2362 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2364 if (!refname)
2365 die(_("No such ref: %s"), "HEAD");
2367 /* detached HEAD */
2368 if (!strcmp(refname, "HEAD"))
2369 die(_("Submodule (%s) branch configured to inherit "
2370 "branch from superproject, but the superproject "
2371 "is not on any branch"), sub->name);
2373 if (!skip_prefix(refname, "refs/heads/", &refname))
2374 die(_("Expecting a full ref name, got %s"), refname);
2375 return refname;
2378 return branch;
2381 static void ensure_core_worktree(const char *path)
2383 const char *cw;
2384 struct repository subrepo;
2386 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2387 die(_("could not get a repository handle for submodule '%s'"), path);
2389 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2390 char *cfg_file, *abs_path;
2391 const char *rel_path;
2392 struct strbuf sb = STRBUF_INIT;
2394 cfg_file = repo_git_path(&subrepo, "config");
2396 abs_path = absolute_pathdup(path);
2397 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2399 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2401 free(cfg_file);
2402 free(abs_path);
2403 strbuf_release(&sb);
2407 static void update_data_to_args(struct update_data *update_data, struct strvec *args)
2409 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2410 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2411 if (update_data->recursive_prefix)
2412 strvec_pushl(args, "--recursive-prefix",
2413 update_data->recursive_prefix, NULL);
2414 if (update_data->quiet)
2415 strvec_push(args, "--quiet");
2416 if (update_data->force)
2417 strvec_push(args, "--force");
2418 if (update_data->init)
2419 strvec_push(args, "--init");
2420 if (update_data->remote)
2421 strvec_push(args, "--remote");
2422 if (update_data->nofetch)
2423 strvec_push(args, "--no-fetch");
2424 if (update_data->dissociate)
2425 strvec_push(args, "--dissociate");
2426 if (update_data->progress)
2427 strvec_push(args, "--progress");
2428 if (update_data->require_init)
2429 strvec_push(args, "--require-init");
2430 if (update_data->depth)
2431 strvec_pushf(args, "--depth=%d", update_data->depth);
2432 if (update_data->update_default)
2433 strvec_pushl(args, "--update", update_data->update_default, NULL);
2434 if (update_data->references.nr) {
2435 struct string_list_item *item;
2436 for_each_string_list_item(item, &update_data->references)
2437 strvec_pushl(args, "--reference", item->string, NULL);
2439 if (update_data->filter_options && update_data->filter_options->choice)
2440 strvec_pushf(args, "--filter=%s",
2441 expand_list_objects_filter_spec(
2442 update_data->filter_options));
2443 if (update_data->recommend_shallow == 0)
2444 strvec_push(args, "--no-recommend-shallow");
2445 else if (update_data->recommend_shallow == 1)
2446 strvec_push(args, "--recommend-shallow");
2447 if (update_data->single_branch >= 0)
2448 strvec_push(args, update_data->single_branch ?
2449 "--single-branch" :
2450 "--no-single-branch");
2453 static int update_submodule(struct update_data *update_data)
2455 char *prefixed_path;
2457 ensure_core_worktree(update_data->sm_path);
2459 if (update_data->recursive_prefix)
2460 prefixed_path = xstrfmt("%s%s", update_data->recursive_prefix,
2461 update_data->sm_path);
2462 else
2463 prefixed_path = xstrdup(update_data->sm_path);
2465 update_data->displaypath = get_submodule_displaypath(prefixed_path,
2466 update_data->prefix);
2467 free(prefixed_path);
2469 determine_submodule_update_strategy(the_repository, update_data->just_cloned,
2470 update_data->sm_path, update_data->update_default,
2471 &update_data->update_strategy);
2473 if (update_data->just_cloned)
2474 oidcpy(&update_data->suboid, null_oid());
2475 else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2476 die(_("Unable to find current revision in submodule path '%s'"),
2477 update_data->displaypath);
2479 if (update_data->remote) {
2480 char *remote_name = get_default_remote_submodule(update_data->sm_path);
2481 const char *branch = remote_submodule_branch(update_data->sm_path);
2482 char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2484 if (!update_data->nofetch) {
2485 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2486 0, NULL))
2487 die(_("Unable to fetch in submodule path '%s'"),
2488 update_data->sm_path);
2491 if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2492 die(_("Unable to find %s revision in submodule path '%s'"),
2493 remote_ref, update_data->sm_path);
2495 free(remote_ref);
2498 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force)
2499 if (run_update_procedure(update_data))
2500 return 1;
2502 if (update_data->recursive) {
2503 struct child_process cp = CHILD_PROCESS_INIT;
2504 struct update_data next = *update_data;
2505 int res;
2507 if (update_data->recursive_prefix)
2508 prefixed_path = xstrfmt("%s%s/", update_data->recursive_prefix,
2509 update_data->sm_path);
2510 else
2511 prefixed_path = xstrfmt("%s/", update_data->sm_path);
2513 next.recursive_prefix = get_submodule_displaypath(prefixed_path,
2514 update_data->prefix);
2515 next.prefix = NULL;
2516 oidcpy(&next.oid, null_oid());
2517 oidcpy(&next.suboid, null_oid());
2519 cp.dir = update_data->sm_path;
2520 cp.git_cmd = 1;
2521 prepare_submodule_repo_env(&cp.env_array);
2522 update_data_to_args(&next, &cp.args);
2524 /* die() if child process die()'d */
2525 res = run_command(&cp);
2526 if (!res)
2527 return 0;
2528 die_message(_("Failed to recurse into submodule path '%s'"),
2529 update_data->displaypath);
2530 if (res == 128)
2531 exit(res);
2532 else if (res)
2533 return 1;
2536 return 0;
2539 static int update_submodules(struct update_data *update_data)
2541 int i, res = 0;
2542 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2544 suc.update_data = update_data;
2545 run_processes_parallel_tr2(suc.update_data->max_jobs, update_clone_get_next_task,
2546 update_clone_start_failure,
2547 update_clone_task_finished, &suc, "submodule",
2548 "parallel/update");
2551 * We saved the output and put it out all at once now.
2552 * That means:
2553 * - the listener does not have to interleave their (checkout)
2554 * work with our fetching. The writes involved in a
2555 * checkout involve more straightforward sequential I/O.
2556 * - the listener can avoid doing any work if fetching failed.
2558 if (suc.quickstop) {
2559 res = 1;
2560 goto cleanup;
2563 for (i = 0; i < suc.update_clone_nr; i++) {
2564 struct update_clone_data ucd = suc.update_clone[i];
2566 oidcpy(&update_data->oid, &ucd.oid);
2567 update_data->just_cloned = ucd.just_cloned;
2568 update_data->sm_path = ucd.sub->path;
2570 if (update_submodule(update_data))
2571 res = 1;
2574 cleanup:
2575 string_list_clear(&update_data->references, 0);
2576 return res;
2579 static int module_update(int argc, const char **argv, const char *prefix)
2581 struct pathspec pathspec;
2582 struct update_data opt = UPDATE_DATA_INIT;
2583 struct list_objects_filter_options filter_options;
2584 int ret;
2586 struct option module_update_options[] = {
2587 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2588 OPT_BOOL(0, "init", &opt.init,
2589 N_("initialize uninitialized submodules before update")),
2590 OPT_BOOL(0, "remote", &opt.remote,
2591 N_("use SHA-1 of submodule's remote tracking branch")),
2592 OPT_BOOL(0, "recursive", &opt.recursive,
2593 N_("traverse submodules recursively")),
2594 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2595 N_("don't fetch new objects from the remote site")),
2596 OPT_STRING(0, "prefix", &opt.prefix,
2597 N_("path"),
2598 N_("path into the working tree")),
2599 OPT_STRING(0, "recursive-prefix", &opt.recursive_prefix,
2600 N_("path"),
2601 N_("path into the working tree, across nested "
2602 "submodule boundaries")),
2603 OPT_STRING(0, "update", &opt.update_default,
2604 N_("string"),
2605 N_("rebase, merge, checkout or none")),
2606 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2607 N_("reference repository")),
2608 OPT_BOOL(0, "dissociate", &opt.dissociate,
2609 N_("use --reference only while cloning")),
2610 OPT_INTEGER(0, "depth", &opt.depth,
2611 N_("create a shallow clone truncated to the "
2612 "specified number of revisions")),
2613 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2614 N_("parallel jobs")),
2615 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2616 N_("whether the initial clone should follow the shallow recommendation")),
2617 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2618 OPT_BOOL(0, "progress", &opt.progress,
2619 N_("force cloning progress")),
2620 OPT_BOOL(0, "require-init", &opt.require_init,
2621 N_("disallow cloning into non-empty directory")),
2622 OPT_BOOL(0, "single-branch", &opt.single_branch,
2623 N_("clone only one branch, HEAD or --branch")),
2624 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2625 OPT_END()
2628 const char *const git_submodule_helper_usage[] = {
2629 N_("git submodule [--quiet] update"
2630 " [--init [--filter=<filter-spec>]] [--remote]"
2631 " [-N|--no-fetch] [-f|--force]"
2632 " [--checkout|--merge|--rebase]"
2633 " [--[no-]recommend-shallow] [--reference <repository>]"
2634 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2635 NULL
2638 update_clone_config_from_gitmodules(&opt.max_jobs);
2639 git_config(git_update_clone_config, &opt.max_jobs);
2641 memset(&filter_options, 0, sizeof(filter_options));
2642 argc = parse_options(argc, argv, prefix, module_update_options,
2643 git_submodule_helper_usage, 0);
2645 if (filter_options.choice && !opt.init) {
2646 usage_with_options(git_submodule_helper_usage,
2647 module_update_options);
2650 opt.filter_options = &filter_options;
2652 if (opt.update_default)
2653 if (parse_submodule_update_strategy(opt.update_default,
2654 &opt.update_strategy) < 0)
2655 die(_("bad value for update parameter"));
2657 if (module_list_compute(argc, argv, prefix, &pathspec, &opt.list) < 0) {
2658 list_objects_filter_release(&filter_options);
2659 return 1;
2662 if (pathspec.nr)
2663 opt.warn_if_uninitialized = 1;
2665 if (opt.init) {
2666 struct module_list list = MODULE_LIST_INIT;
2667 struct init_cb info = INIT_CB_INIT;
2669 if (module_list_compute(argc, argv, opt.prefix,
2670 &pathspec, &list) < 0)
2671 return 1;
2674 * If there are no path args and submodule.active is set then,
2675 * by default, only initialize 'active' modules.
2677 if (!argc && git_config_get_value_multi("submodule.active"))
2678 module_list_active(&list);
2680 info.prefix = opt.prefix;
2681 info.superprefix = opt.recursive_prefix;
2682 if (opt.quiet)
2683 info.flags |= OPT_QUIET;
2685 for_each_listed_submodule(&list, init_submodule_cb, &info);
2688 ret = update_submodules(&opt);
2689 list_objects_filter_release(&filter_options);
2690 return ret;
2693 static int push_check(int argc, const char **argv, const char *prefix)
2695 struct remote *remote;
2696 const char *superproject_head;
2697 char *head;
2698 int detached_head = 0;
2699 struct object_id head_oid;
2701 if (argc < 3)
2702 die("submodule--helper push-check requires at least 2 arguments");
2705 * superproject's resolved head ref.
2706 * if HEAD then the superproject is in a detached head state, otherwise
2707 * it will be the resolved head ref.
2709 superproject_head = argv[1];
2710 argv++;
2711 argc--;
2712 /* Get the submodule's head ref and determine if it is detached */
2713 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2714 if (!head)
2715 die(_("Failed to resolve HEAD as a valid ref."));
2716 if (!strcmp(head, "HEAD"))
2717 detached_head = 1;
2720 * The remote must be configured.
2721 * This is to avoid pushing to the exact same URL as the parent.
2723 remote = pushremote_get(argv[1]);
2724 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2725 die("remote '%s' not configured", argv[1]);
2727 /* Check the refspec */
2728 if (argc > 2) {
2729 int i;
2730 struct ref *local_refs = get_local_heads();
2731 struct refspec refspec = REFSPEC_INIT_PUSH;
2733 refspec_appendn(&refspec, argv + 2, argc - 2);
2735 for (i = 0; i < refspec.nr; i++) {
2736 const struct refspec_item *rs = &refspec.items[i];
2738 if (rs->pattern || rs->matching)
2739 continue;
2741 /* LHS must match a single ref */
2742 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2743 case 1:
2744 break;
2745 case 0:
2747 * If LHS matches 'HEAD' then we need to ensure
2748 * that it matches the same named branch
2749 * checked out in the superproject.
2751 if (!strcmp(rs->src, "HEAD")) {
2752 if (!detached_head &&
2753 !strcmp(head, superproject_head))
2754 break;
2755 die("HEAD does not match the named branch in the superproject");
2757 /* fallthrough */
2758 default:
2759 die("src refspec '%s' must name a ref",
2760 rs->src);
2763 refspec_clear(&refspec);
2765 free(head);
2767 return 0;
2770 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2772 int i;
2773 struct pathspec pathspec;
2774 struct module_list list = MODULE_LIST_INIT;
2775 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2777 struct option embed_gitdir_options[] = {
2778 OPT_STRING(0, "prefix", &prefix,
2779 N_("path"),
2780 N_("path into the working tree")),
2781 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2782 ABSORB_GITDIR_RECURSE_SUBMODULES),
2783 OPT_END()
2786 const char *const git_submodule_helper_usage[] = {
2787 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2788 NULL
2791 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2792 git_submodule_helper_usage, 0);
2794 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2795 return 1;
2797 for (i = 0; i < list.nr; i++)
2798 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2800 return 0;
2803 static int is_active(int argc, const char **argv, const char *prefix)
2805 if (argc != 2)
2806 die("submodule--helper is-active takes exactly 1 argument");
2808 return !is_submodule_active(the_repository, argv[1]);
2812 * Exit non-zero if any of the submodule names given on the command line is
2813 * invalid. If no names are given, filter stdin to print only valid names
2814 * (which is primarily intended for testing).
2816 static int check_name(int argc, const char **argv, const char *prefix)
2818 if (argc > 1) {
2819 while (*++argv) {
2820 if (check_submodule_name(*argv) < 0)
2821 return 1;
2823 } else {
2824 struct strbuf buf = STRBUF_INIT;
2825 while (strbuf_getline(&buf, stdin) != EOF) {
2826 if (!check_submodule_name(buf.buf))
2827 printf("%s\n", buf.buf);
2829 strbuf_release(&buf);
2831 return 0;
2834 static int module_config(int argc, const char **argv, const char *prefix)
2836 enum {
2837 CHECK_WRITEABLE = 1,
2838 DO_UNSET = 2
2839 } command = 0;
2841 struct option module_config_options[] = {
2842 OPT_CMDMODE(0, "check-writeable", &command,
2843 N_("check if it is safe to write to the .gitmodules file"),
2844 CHECK_WRITEABLE),
2845 OPT_CMDMODE(0, "unset", &command,
2846 N_("unset the config in the .gitmodules file"),
2847 DO_UNSET),
2848 OPT_END()
2850 const char *const git_submodule_helper_usage[] = {
2851 N_("git submodule--helper config <name> [<value>]"),
2852 N_("git submodule--helper config --unset <name>"),
2853 "git submodule--helper config --check-writeable",
2854 NULL
2857 argc = parse_options(argc, argv, prefix, module_config_options,
2858 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2860 if (argc == 1 && command == CHECK_WRITEABLE)
2861 return is_writing_gitmodules_ok() ? 0 : -1;
2863 /* Equivalent to ACTION_GET in builtin/config.c */
2864 if (argc == 2 && command != DO_UNSET)
2865 return print_config_from_gitmodules(the_repository, argv[1]);
2867 /* Equivalent to ACTION_SET in builtin/config.c */
2868 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2869 const char *value = (argc == 3) ? argv[2] : NULL;
2871 if (!is_writing_gitmodules_ok())
2872 die(_("please make sure that the .gitmodules file is in the working tree"));
2874 return config_set_in_gitmodules_file_gently(argv[1], value);
2877 usage_with_options(git_submodule_helper_usage, module_config_options);
2880 static int module_set_url(int argc, const char **argv, const char *prefix)
2882 int quiet = 0;
2883 const char *newurl;
2884 const char *path;
2885 char *config_name;
2887 struct option options[] = {
2888 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2889 OPT_END()
2891 const char *const usage[] = {
2892 N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
2893 NULL
2896 argc = parse_options(argc, argv, prefix, options, usage, 0);
2898 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2899 usage_with_options(usage, options);
2901 config_name = xstrfmt("submodule.%s.url", path);
2903 config_set_in_gitmodules_file_gently(config_name, newurl);
2904 sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
2906 free(config_name);
2908 return 0;
2911 static int module_set_branch(int argc, const char **argv, const char *prefix)
2913 int opt_default = 0, ret;
2914 const char *opt_branch = NULL;
2915 const char *path;
2916 char *config_name;
2919 * We accept the `quiet` option for uniformity across subcommands,
2920 * though there is nothing to make less verbose in this subcommand.
2922 struct option options[] = {
2923 OPT_NOOP_NOARG('q', "quiet"),
2924 OPT_BOOL('d', "default", &opt_default,
2925 N_("set the default tracking branch to master")),
2926 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2927 N_("set the default tracking branch")),
2928 OPT_END()
2930 const char *const usage[] = {
2931 N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
2932 N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2933 NULL
2936 argc = parse_options(argc, argv, prefix, options, usage, 0);
2938 if (!opt_branch && !opt_default)
2939 die(_("--branch or --default required"));
2941 if (opt_branch && opt_default)
2942 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2944 if (argc != 1 || !(path = argv[0]))
2945 usage_with_options(usage, options);
2947 config_name = xstrfmt("submodule.%s.branch", path);
2948 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2950 free(config_name);
2951 return !!ret;
2954 static int module_create_branch(int argc, const char **argv, const char *prefix)
2956 enum branch_track track;
2957 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
2959 struct option options[] = {
2960 OPT__QUIET(&quiet, N_("print only error messages")),
2961 OPT__FORCE(&force, N_("force creation"), 0),
2962 OPT_BOOL(0, "create-reflog", &reflog,
2963 N_("create the branch's reflog")),
2964 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
2965 N_("set branch tracking configuration"),
2966 PARSE_OPT_OPTARG,
2967 parse_opt_tracking_mode),
2968 OPT__DRY_RUN(&dry_run,
2969 N_("show whether the branch would be created")),
2970 OPT_END()
2972 const char *const usage[] = {
2973 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2974 NULL
2977 git_config(git_default_config, NULL);
2978 track = git_branch_track;
2979 argc = parse_options(argc, argv, prefix, options, usage, 0);
2981 if (argc != 3)
2982 usage_with_options(usage, options);
2984 if (!quiet && !dry_run)
2985 printf_ln(_("creating branch '%s'"), argv[0]);
2987 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
2988 force, reflog, quiet, track, dry_run);
2989 return 0;
2992 struct add_data {
2993 const char *prefix;
2994 const char *branch;
2995 const char *reference_path;
2996 char *sm_path;
2997 const char *sm_name;
2998 const char *repo;
2999 const char *realrepo;
3000 int depth;
3001 unsigned int force: 1;
3002 unsigned int quiet: 1;
3003 unsigned int progress: 1;
3004 unsigned int dissociate: 1;
3006 #define ADD_DATA_INIT { .depth = -1 }
3008 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3010 struct child_process cp_remote = CHILD_PROCESS_INIT;
3011 struct strbuf sb_remote_out = STRBUF_INIT;
3013 cp_remote.git_cmd = 1;
3014 strvec_pushf(&cp_remote.env_array,
3015 "GIT_DIR=%s", git_dir_path);
3016 strvec_push(&cp_remote.env_array, "GIT_WORK_TREE=.");
3017 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3018 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3019 char *next_line;
3020 char *line = sb_remote_out.buf;
3021 while ((next_line = strchr(line, '\n')) != NULL) {
3022 size_t len = next_line - line;
3023 if (strip_suffix_mem(line, &len, " (fetch)"))
3024 strbuf_addf(msg, " %.*s\n", (int)len, line);
3025 line = next_line + 1;
3029 strbuf_release(&sb_remote_out);
3032 static int add_submodule(const struct add_data *add_data)
3034 char *submod_gitdir_path;
3035 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3037 /* perhaps the path already exists and is already a git repo, else clone it */
3038 if (is_directory(add_data->sm_path)) {
3039 struct strbuf sm_path = STRBUF_INIT;
3040 strbuf_addstr(&sm_path, add_data->sm_path);
3041 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3042 if (is_nonbare_repository_dir(&sm_path))
3043 printf(_("Adding existing repo at '%s' to the index\n"),
3044 add_data->sm_path);
3045 else
3046 die(_("'%s' already exists and is not a valid git repo"),
3047 add_data->sm_path);
3048 strbuf_release(&sm_path);
3049 free(submod_gitdir_path);
3050 } else {
3051 struct child_process cp = CHILD_PROCESS_INIT;
3052 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3054 if (is_directory(submod_gitdir_path)) {
3055 if (!add_data->force) {
3056 struct strbuf msg = STRBUF_INIT;
3057 char *die_msg;
3059 strbuf_addf(&msg, _("A git directory for '%s' is found "
3060 "locally with remote(s):\n"),
3061 add_data->sm_name);
3063 append_fetch_remotes(&msg, submod_gitdir_path);
3064 free(submod_gitdir_path);
3066 strbuf_addf(&msg, _("If you want to reuse this local git "
3067 "directory instead of cloning again from\n"
3068 " %s\n"
3069 "use the '--force' option. If the local git "
3070 "directory is not the correct repo\n"
3071 "or you are unsure what this means choose "
3072 "another name with the '--name' option."),
3073 add_data->realrepo);
3075 die_msg = strbuf_detach(&msg, NULL);
3076 die("%s", die_msg);
3077 } else {
3078 printf(_("Reactivating local git directory for "
3079 "submodule '%s'\n"), add_data->sm_name);
3082 free(submod_gitdir_path);
3084 clone_data.prefix = add_data->prefix;
3085 clone_data.path = add_data->sm_path;
3086 clone_data.name = add_data->sm_name;
3087 clone_data.url = add_data->realrepo;
3088 clone_data.quiet = add_data->quiet;
3089 clone_data.progress = add_data->progress;
3090 if (add_data->reference_path)
3091 string_list_append(&clone_data.reference,
3092 xstrdup(add_data->reference_path));
3093 clone_data.dissociate = add_data->dissociate;
3094 if (add_data->depth >= 0)
3095 clone_data.depth = xstrfmt("%d", add_data->depth);
3097 if (clone_submodule(&clone_data))
3098 return -1;
3100 prepare_submodule_repo_env(&cp.env_array);
3101 cp.git_cmd = 1;
3102 cp.dir = add_data->sm_path;
3104 * NOTE: we only get here if add_data->force is true, so
3105 * passing --force to checkout is reasonable.
3107 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3109 if (add_data->branch) {
3110 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3111 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3114 if (run_command(&cp))
3115 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3117 return 0;
3120 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3122 char *key;
3123 int ret;
3125 if (!is_writing_gitmodules_ok())
3126 die(_("please make sure that the .gitmodules file is in the working tree"));
3128 key = xstrfmt("submodule.%s.%s", name, var);
3129 ret = config_set_in_gitmodules_file_gently(key, value);
3130 free(key);
3132 return ret;
3135 static void configure_added_submodule(struct add_data *add_data)
3137 char *key;
3138 char *val = NULL;
3139 struct child_process add_submod = CHILD_PROCESS_INIT;
3140 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3142 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3143 git_config_set_gently(key, add_data->realrepo);
3144 free(key);
3146 add_submod.git_cmd = 1;
3147 strvec_pushl(&add_submod.args, "add",
3148 "--no-warn-embedded-repo", NULL);
3149 if (add_data->force)
3150 strvec_push(&add_submod.args, "--force");
3151 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3153 if (run_command(&add_submod))
3154 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3156 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3157 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3158 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3160 if (add_data->branch) {
3161 if (config_submodule_in_gitmodules(add_data->sm_name,
3162 "branch", add_data->branch))
3163 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3166 add_gitmodules.git_cmd = 1;
3167 strvec_pushl(&add_gitmodules.args,
3168 "add", "--force", "--", ".gitmodules", NULL);
3170 if (run_command(&add_gitmodules))
3171 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3174 * NEEDSWORK: In a multi-working-tree world this needs to be
3175 * set in the per-worktree config.
3178 * NEEDSWORK: In the longer run, we need to get rid of this
3179 * pattern of querying "submodule.active" before calling
3180 * is_submodule_active(), since that function needs to find
3181 * out the value of "submodule.active" again anyway.
3183 if (!git_config_get_string("submodule.active", &val) && val) {
3185 * If the submodule being added isn't already covered by the
3186 * current configured pathspec, set the submodule's active flag
3188 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3189 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3190 git_config_set_gently(key, "true");
3191 free(key);
3193 } else {
3194 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3195 git_config_set_gently(key, "true");
3196 free(key);
3200 static void die_on_index_match(const char *path, int force)
3202 struct pathspec ps;
3203 const char *args[] = { path, NULL };
3204 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3206 if (read_cache_preload(NULL) < 0)
3207 die(_("index file corrupt"));
3209 if (ps.nr) {
3210 int i;
3211 char *ps_matched = xcalloc(ps.nr, 1);
3213 /* TODO: audit for interaction with sparse-index. */
3214 ensure_full_index(&the_index);
3217 * Since there is only one pathspec, we just need
3218 * need to check ps_matched[0] to know if a cache
3219 * entry matched.
3221 for (i = 0; i < active_nr; i++) {
3222 ce_path_match(&the_index, active_cache[i], &ps,
3223 ps_matched);
3225 if (ps_matched[0]) {
3226 if (!force)
3227 die(_("'%s' already exists in the index"),
3228 path);
3229 if (!S_ISGITLINK(active_cache[i]->ce_mode))
3230 die(_("'%s' already exists in the index "
3231 "and is not a submodule"), path);
3232 break;
3235 free(ps_matched);
3237 clear_pathspec(&ps);
3240 static void die_on_repo_without_commits(const char *path)
3242 struct strbuf sb = STRBUF_INIT;
3243 strbuf_addstr(&sb, path);
3244 if (is_nonbare_repository_dir(&sb)) {
3245 struct object_id oid;
3246 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3247 die(_("'%s' does not have a commit checked out"), path);
3249 strbuf_release(&sb);
3252 static int module_add(int argc, const char **argv, const char *prefix)
3254 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3255 struct add_data add_data = ADD_DATA_INIT;
3256 char *to_free = NULL;
3258 struct option options[] = {
3259 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3260 N_("branch of repository to add as submodule")),
3261 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3262 PARSE_OPT_NOCOMPLETE),
3263 OPT__QUIET(&quiet, N_("print only error messages")),
3264 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3265 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3266 N_("reference repository")),
3267 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3268 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3269 N_("sets the submodule's name to the given string "
3270 "instead of defaulting to its path")),
3271 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3272 OPT_END()
3275 const char *const usage[] = {
3276 N_("git submodule--helper add [<options>] [--] <repository> [<path>]"),
3277 NULL
3280 argc = parse_options(argc, argv, prefix, options, usage, 0);
3282 if (!is_writing_gitmodules_ok())
3283 die(_("please make sure that the .gitmodules file is in the working tree"));
3285 if (prefix && *prefix &&
3286 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3287 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3289 if (argc == 0 || argc > 2)
3290 usage_with_options(usage, options);
3292 add_data.repo = argv[0];
3293 if (argc == 1)
3294 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3295 else
3296 add_data.sm_path = xstrdup(argv[1]);
3298 if (prefix && *prefix && !is_absolute_path(add_data.sm_path))
3299 add_data.sm_path = xstrfmt("%s%s", prefix, add_data.sm_path);
3301 if (starts_with_dot_dot_slash(add_data.repo) ||
3302 starts_with_dot_slash(add_data.repo)) {
3303 if (prefix)
3304 die(_("Relative path can only be used from the toplevel "
3305 "of the working tree"));
3307 /* dereference source url relative to parent's url */
3308 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3309 add_data.realrepo = to_free;
3310 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3311 add_data.realrepo = add_data.repo;
3312 } else {
3313 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3314 add_data.repo);
3318 * normalize path:
3319 * multiple //; leading ./; /./; /../;
3321 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3322 strip_dir_trailing_slashes(add_data.sm_path);
3324 die_on_index_match(add_data.sm_path, force);
3325 die_on_repo_without_commits(add_data.sm_path);
3327 if (!force) {
3328 int exit_code = -1;
3329 struct strbuf sb = STRBUF_INIT;
3330 struct child_process cp = CHILD_PROCESS_INIT;
3331 cp.git_cmd = 1;
3332 cp.no_stdout = 1;
3333 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3334 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3335 if ((exit_code = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3336 strbuf_complete_line(&sb);
3337 fputs(sb.buf, stderr);
3338 free(add_data.sm_path);
3339 return exit_code;
3341 strbuf_release(&sb);
3344 if(!add_data.sm_name)
3345 add_data.sm_name = add_data.sm_path;
3347 if (check_submodule_name(add_data.sm_name))
3348 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3350 add_data.prefix = prefix;
3351 add_data.force = !!force;
3352 add_data.quiet = !!quiet;
3353 add_data.progress = !!progress;
3354 add_data.dissociate = !!dissociate;
3356 if (add_submodule(&add_data)) {
3357 free(add_data.sm_path);
3358 return 1;
3360 configure_added_submodule(&add_data);
3361 free(add_data.sm_path);
3362 free(to_free);
3364 return 0;
3367 #define SUPPORT_SUPER_PREFIX (1<<0)
3369 struct cmd_struct {
3370 const char *cmd;
3371 int (*fn)(int, const char **, const char *);
3372 unsigned option;
3375 static struct cmd_struct commands[] = {
3376 {"list", module_list, 0},
3377 {"name", module_name, 0},
3378 {"clone", module_clone, 0},
3379 {"add", module_add, SUPPORT_SUPER_PREFIX},
3380 {"update", module_update, 0},
3381 {"resolve-relative-url-test", resolve_relative_url_test, 0},
3382 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
3383 {"init", module_init, SUPPORT_SUPER_PREFIX},
3384 {"status", module_status, SUPPORT_SUPER_PREFIX},
3385 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
3386 {"deinit", module_deinit, 0},
3387 {"summary", module_summary, SUPPORT_SUPER_PREFIX},
3388 {"push-check", push_check, 0},
3389 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3390 {"is-active", is_active, 0},
3391 {"check-name", check_name, 0},
3392 {"config", module_config, 0},
3393 {"set-url", module_set_url, 0},
3394 {"set-branch", module_set_branch, 0},
3395 {"create-branch", module_create_branch, 0},
3398 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3400 int i;
3401 if (argc < 2 || !strcmp(argv[1], "-h"))
3402 usage("git submodule--helper <command>");
3404 for (i = 0; i < ARRAY_SIZE(commands); i++) {
3405 if (!strcmp(argv[1], commands[i].cmd)) {
3406 if (get_super_prefix() &&
3407 !(commands[i].option & SUPPORT_SUPER_PREFIX))
3408 die(_("%s doesn't support --super-prefix"),
3409 commands[i].cmd);
3410 return commands[i].fn(argc - 1, argv + 1, prefix);
3414 die(_("'%s' is not a valid submodule--helper "
3415 "subcommand"), argv[1]);