Merge branch 'tb/commit-graph-genv2-upgrade-fix' into maint
[git/debian.git] / builtin / submodule--helper.c
blobc597df7528ee56cd1e35c65f409e73a99769a5ee
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);
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);
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, "name=%s", sub->name);
374 strvec_pushf(&cp.env, "sm_path=%s", path);
375 strvec_pushf(&cp.env, "displaypath=%s", displaypath);
376 strvec_pushf(&cp.env, "sha1=%s",
377 oid_to_hex(ce_oid));
378 strvec_pushf(&cp.env, "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.
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
2211 free(remote);
2214 return run_command(&cp);
2217 static int run_update_command(struct update_data *ud, int subforce)
2219 struct child_process cp = CHILD_PROCESS_INIT;
2220 char *oid = oid_to_hex(&ud->oid);
2221 int must_die_on_failure = 0;
2223 switch (ud->update_strategy.type) {
2224 case SM_UPDATE_CHECKOUT:
2225 cp.git_cmd = 1;
2226 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2227 if (subforce)
2228 strvec_push(&cp.args, "-f");
2229 break;
2230 case SM_UPDATE_REBASE:
2231 cp.git_cmd = 1;
2232 strvec_push(&cp.args, "rebase");
2233 if (ud->quiet)
2234 strvec_push(&cp.args, "--quiet");
2235 must_die_on_failure = 1;
2236 break;
2237 case SM_UPDATE_MERGE:
2238 cp.git_cmd = 1;
2239 strvec_push(&cp.args, "merge");
2240 if (ud->quiet)
2241 strvec_push(&cp.args, "--quiet");
2242 must_die_on_failure = 1;
2243 break;
2244 case SM_UPDATE_COMMAND:
2245 cp.use_shell = 1;
2246 strvec_push(&cp.args, ud->update_strategy.command);
2247 must_die_on_failure = 1;
2248 break;
2249 default:
2250 BUG("unexpected update strategy type: %s",
2251 submodule_strategy_to_string(&ud->update_strategy));
2253 strvec_push(&cp.args, oid);
2255 cp.dir = xstrdup(ud->sm_path);
2256 prepare_submodule_repo_env(&cp.env);
2257 if (run_command(&cp)) {
2258 switch (ud->update_strategy.type) {
2259 case SM_UPDATE_CHECKOUT:
2260 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2261 oid, ud->displaypath);
2262 break;
2263 case SM_UPDATE_REBASE:
2264 die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2265 oid, ud->displaypath);
2266 break;
2267 case SM_UPDATE_MERGE:
2268 die_message(_("Unable to merge '%s' in submodule path '%s'"),
2269 oid, ud->displaypath);
2270 break;
2271 case SM_UPDATE_COMMAND:
2272 die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2273 ud->update_strategy.command, oid, ud->displaypath);
2274 break;
2275 default:
2276 BUG("unexpected update strategy type: %s",
2277 submodule_strategy_to_string(&ud->update_strategy));
2279 if (must_die_on_failure)
2280 exit(128);
2282 /* the command failed, but update must continue */
2283 return 1;
2286 if (ud->quiet)
2287 return 0;
2289 switch (ud->update_strategy.type) {
2290 case SM_UPDATE_CHECKOUT:
2291 printf(_("Submodule path '%s': checked out '%s'\n"),
2292 ud->displaypath, oid);
2293 break;
2294 case SM_UPDATE_REBASE:
2295 printf(_("Submodule path '%s': rebased into '%s'\n"),
2296 ud->displaypath, oid);
2297 break;
2298 case SM_UPDATE_MERGE:
2299 printf(_("Submodule path '%s': merged in '%s'\n"),
2300 ud->displaypath, oid);
2301 break;
2302 case SM_UPDATE_COMMAND:
2303 printf(_("Submodule path '%s': '%s %s'\n"),
2304 ud->displaypath, ud->update_strategy.command, oid);
2305 break;
2306 default:
2307 BUG("unexpected update strategy type: %s",
2308 submodule_strategy_to_string(&ud->update_strategy));
2311 return 0;
2314 static int run_update_procedure(struct update_data *ud)
2316 int subforce = is_null_oid(&ud->suboid) || ud->force;
2318 if (!ud->nofetch) {
2320 * Run fetch only if `oid` isn't present or it
2321 * is not reachable from a ref.
2323 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2324 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2325 !ud->quiet)
2326 fprintf_ln(stderr,
2327 _("Unable to fetch in submodule path '%s'; "
2328 "trying to directly fetch %s:"),
2329 ud->displaypath, oid_to_hex(&ud->oid));
2331 * Now we tried the usual fetch, but `oid` may
2332 * not be reachable from any of the refs.
2334 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2335 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2336 die(_("Fetched in submodule path '%s', but it did not "
2337 "contain %s. Direct fetching of that commit failed."),
2338 ud->displaypath, oid_to_hex(&ud->oid));
2341 return run_update_command(ud, subforce);
2344 static const char *remote_submodule_branch(const char *path)
2346 const struct submodule *sub;
2347 const char *branch = NULL;
2348 char *key;
2350 sub = submodule_from_path(the_repository, null_oid(), path);
2351 if (!sub)
2352 return NULL;
2354 key = xstrfmt("submodule.%s.branch", sub->name);
2355 if (repo_config_get_string_tmp(the_repository, key, &branch))
2356 branch = sub->branch;
2357 free(key);
2359 if (!branch)
2360 return "HEAD";
2362 if (!strcmp(branch, ".")) {
2363 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2365 if (!refname)
2366 die(_("No such ref: %s"), "HEAD");
2368 /* detached HEAD */
2369 if (!strcmp(refname, "HEAD"))
2370 die(_("Submodule (%s) branch configured to inherit "
2371 "branch from superproject, but the superproject "
2372 "is not on any branch"), sub->name);
2374 if (!skip_prefix(refname, "refs/heads/", &refname))
2375 die(_("Expecting a full ref name, got %s"), refname);
2376 return refname;
2379 return branch;
2382 static void ensure_core_worktree(const char *path)
2384 const char *cw;
2385 struct repository subrepo;
2387 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2388 die(_("could not get a repository handle for submodule '%s'"), path);
2390 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2391 char *cfg_file, *abs_path;
2392 const char *rel_path;
2393 struct strbuf sb = STRBUF_INIT;
2395 cfg_file = repo_git_path(&subrepo, "config");
2397 abs_path = absolute_pathdup(path);
2398 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2400 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2402 free(cfg_file);
2403 free(abs_path);
2404 strbuf_release(&sb);
2408 static void update_data_to_args(struct update_data *update_data, struct strvec *args)
2410 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2411 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2412 if (update_data->recursive_prefix)
2413 strvec_pushl(args, "--recursive-prefix",
2414 update_data->recursive_prefix, NULL);
2415 if (update_data->quiet)
2416 strvec_push(args, "--quiet");
2417 if (update_data->force)
2418 strvec_push(args, "--force");
2419 if (update_data->init)
2420 strvec_push(args, "--init");
2421 if (update_data->remote)
2422 strvec_push(args, "--remote");
2423 if (update_data->nofetch)
2424 strvec_push(args, "--no-fetch");
2425 if (update_data->dissociate)
2426 strvec_push(args, "--dissociate");
2427 if (update_data->progress)
2428 strvec_push(args, "--progress");
2429 if (update_data->require_init)
2430 strvec_push(args, "--require-init");
2431 if (update_data->depth)
2432 strvec_pushf(args, "--depth=%d", update_data->depth);
2433 if (update_data->update_default)
2434 strvec_pushl(args, "--update", update_data->update_default, NULL);
2435 if (update_data->references.nr) {
2436 struct string_list_item *item;
2437 for_each_string_list_item(item, &update_data->references)
2438 strvec_pushl(args, "--reference", item->string, NULL);
2440 if (update_data->filter_options && update_data->filter_options->choice)
2441 strvec_pushf(args, "--filter=%s",
2442 expand_list_objects_filter_spec(
2443 update_data->filter_options));
2444 if (update_data->recommend_shallow == 0)
2445 strvec_push(args, "--no-recommend-shallow");
2446 else if (update_data->recommend_shallow == 1)
2447 strvec_push(args, "--recommend-shallow");
2448 if (update_data->single_branch >= 0)
2449 strvec_push(args, update_data->single_branch ?
2450 "--single-branch" :
2451 "--no-single-branch");
2454 static int update_submodule(struct update_data *update_data)
2456 char *prefixed_path;
2458 ensure_core_worktree(update_data->sm_path);
2460 if (update_data->recursive_prefix)
2461 prefixed_path = xstrfmt("%s%s", update_data->recursive_prefix,
2462 update_data->sm_path);
2463 else
2464 prefixed_path = xstrdup(update_data->sm_path);
2466 update_data->displaypath = get_submodule_displaypath(prefixed_path,
2467 update_data->prefix);
2468 free(prefixed_path);
2470 determine_submodule_update_strategy(the_repository, update_data->just_cloned,
2471 update_data->sm_path, update_data->update_default,
2472 &update_data->update_strategy);
2474 if (update_data->just_cloned)
2475 oidcpy(&update_data->suboid, null_oid());
2476 else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2477 die(_("Unable to find current revision in submodule path '%s'"),
2478 update_data->displaypath);
2480 if (update_data->remote) {
2481 char *remote_name = get_default_remote_submodule(update_data->sm_path);
2482 const char *branch = remote_submodule_branch(update_data->sm_path);
2483 char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2485 if (!update_data->nofetch) {
2486 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2487 0, NULL))
2488 die(_("Unable to fetch in submodule path '%s'"),
2489 update_data->sm_path);
2492 if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2493 die(_("Unable to find %s revision in submodule path '%s'"),
2494 remote_ref, update_data->sm_path);
2496 free(remote_ref);
2499 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force)
2500 if (run_update_procedure(update_data))
2501 return 1;
2503 if (update_data->recursive) {
2504 struct child_process cp = CHILD_PROCESS_INIT;
2505 struct update_data next = *update_data;
2506 int res;
2508 if (update_data->recursive_prefix)
2509 prefixed_path = xstrfmt("%s%s/", update_data->recursive_prefix,
2510 update_data->sm_path);
2511 else
2512 prefixed_path = xstrfmt("%s/", update_data->sm_path);
2514 next.recursive_prefix = get_submodule_displaypath(prefixed_path,
2515 update_data->prefix);
2516 next.prefix = NULL;
2517 oidcpy(&next.oid, null_oid());
2518 oidcpy(&next.suboid, null_oid());
2520 cp.dir = update_data->sm_path;
2521 cp.git_cmd = 1;
2522 prepare_submodule_repo_env(&cp.env);
2523 update_data_to_args(&next, &cp.args);
2525 /* die() if child process die()'d */
2526 res = run_command(&cp);
2527 if (!res)
2528 return 0;
2529 die_message(_("Failed to recurse into submodule path '%s'"),
2530 update_data->displaypath);
2531 if (res == 128)
2532 exit(res);
2533 else if (res)
2534 return 1;
2537 return 0;
2540 static int update_submodules(struct update_data *update_data)
2542 int i, res = 0;
2543 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2545 suc.update_data = update_data;
2546 run_processes_parallel_tr2(suc.update_data->max_jobs, update_clone_get_next_task,
2547 update_clone_start_failure,
2548 update_clone_task_finished, &suc, "submodule",
2549 "parallel/update");
2552 * We saved the output and put it out all at once now.
2553 * That means:
2554 * - the listener does not have to interleave their (checkout)
2555 * work with our fetching. The writes involved in a
2556 * checkout involve more straightforward sequential I/O.
2557 * - the listener can avoid doing any work if fetching failed.
2559 if (suc.quickstop) {
2560 res = 1;
2561 goto cleanup;
2564 for (i = 0; i < suc.update_clone_nr; i++) {
2565 struct update_clone_data ucd = suc.update_clone[i];
2567 oidcpy(&update_data->oid, &ucd.oid);
2568 update_data->just_cloned = ucd.just_cloned;
2569 update_data->sm_path = ucd.sub->path;
2571 if (update_submodule(update_data))
2572 res = 1;
2575 cleanup:
2576 string_list_clear(&update_data->references, 0);
2577 return res;
2580 static int module_update(int argc, const char **argv, const char *prefix)
2582 struct pathspec pathspec;
2583 struct update_data opt = UPDATE_DATA_INIT;
2584 struct list_objects_filter_options filter_options;
2585 int ret;
2587 struct option module_update_options[] = {
2588 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2589 OPT_BOOL(0, "init", &opt.init,
2590 N_("initialize uninitialized submodules before update")),
2591 OPT_BOOL(0, "remote", &opt.remote,
2592 N_("use SHA-1 of submodule's remote tracking branch")),
2593 OPT_BOOL(0, "recursive", &opt.recursive,
2594 N_("traverse submodules recursively")),
2595 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2596 N_("don't fetch new objects from the remote site")),
2597 OPT_STRING(0, "prefix", &opt.prefix,
2598 N_("path"),
2599 N_("path into the working tree")),
2600 OPT_STRING(0, "recursive-prefix", &opt.recursive_prefix,
2601 N_("path"),
2602 N_("path into the working tree, across nested "
2603 "submodule boundaries")),
2604 OPT_STRING(0, "update", &opt.update_default,
2605 N_("string"),
2606 N_("rebase, merge, checkout or none")),
2607 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2608 N_("reference repository")),
2609 OPT_BOOL(0, "dissociate", &opt.dissociate,
2610 N_("use --reference only while cloning")),
2611 OPT_INTEGER(0, "depth", &opt.depth,
2612 N_("create a shallow clone truncated to the "
2613 "specified number of revisions")),
2614 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2615 N_("parallel jobs")),
2616 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2617 N_("whether the initial clone should follow the shallow recommendation")),
2618 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2619 OPT_BOOL(0, "progress", &opt.progress,
2620 N_("force cloning progress")),
2621 OPT_BOOL(0, "require-init", &opt.require_init,
2622 N_("disallow cloning into non-empty directory")),
2623 OPT_BOOL(0, "single-branch", &opt.single_branch,
2624 N_("clone only one branch, HEAD or --branch")),
2625 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2626 OPT_END()
2629 const char *const git_submodule_helper_usage[] = {
2630 N_("git submodule [--quiet] update"
2631 " [--init [--filter=<filter-spec>]] [--remote]"
2632 " [-N|--no-fetch] [-f|--force]"
2633 " [--checkout|--merge|--rebase]"
2634 " [--[no-]recommend-shallow] [--reference <repository>]"
2635 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2636 NULL
2639 update_clone_config_from_gitmodules(&opt.max_jobs);
2640 git_config(git_update_clone_config, &opt.max_jobs);
2642 memset(&filter_options, 0, sizeof(filter_options));
2643 argc = parse_options(argc, argv, prefix, module_update_options,
2644 git_submodule_helper_usage, 0);
2646 if (filter_options.choice && !opt.init) {
2647 usage_with_options(git_submodule_helper_usage,
2648 module_update_options);
2651 opt.filter_options = &filter_options;
2653 if (opt.update_default)
2654 if (parse_submodule_update_strategy(opt.update_default,
2655 &opt.update_strategy) < 0)
2656 die(_("bad value for update parameter"));
2658 if (module_list_compute(argc, argv, prefix, &pathspec, &opt.list) < 0) {
2659 list_objects_filter_release(&filter_options);
2660 return 1;
2663 if (pathspec.nr)
2664 opt.warn_if_uninitialized = 1;
2666 if (opt.init) {
2667 struct module_list list = MODULE_LIST_INIT;
2668 struct init_cb info = INIT_CB_INIT;
2670 if (module_list_compute(argc, argv, opt.prefix,
2671 &pathspec, &list) < 0)
2672 return 1;
2675 * If there are no path args and submodule.active is set then,
2676 * by default, only initialize 'active' modules.
2678 if (!argc && git_config_get_value_multi("submodule.active"))
2679 module_list_active(&list);
2681 info.prefix = opt.prefix;
2682 info.superprefix = opt.recursive_prefix;
2683 if (opt.quiet)
2684 info.flags |= OPT_QUIET;
2686 for_each_listed_submodule(&list, init_submodule_cb, &info);
2689 ret = update_submodules(&opt);
2690 list_objects_filter_release(&filter_options);
2691 return ret;
2694 static int push_check(int argc, const char **argv, const char *prefix)
2696 struct remote *remote;
2697 const char *superproject_head;
2698 char *head;
2699 int detached_head = 0;
2700 struct object_id head_oid;
2702 if (argc < 3)
2703 die("submodule--helper push-check requires at least 2 arguments");
2706 * superproject's resolved head ref.
2707 * if HEAD then the superproject is in a detached head state, otherwise
2708 * it will be the resolved head ref.
2710 superproject_head = argv[1];
2711 argv++;
2712 argc--;
2713 /* Get the submodule's head ref and determine if it is detached */
2714 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2715 if (!head)
2716 die(_("Failed to resolve HEAD as a valid ref."));
2717 if (!strcmp(head, "HEAD"))
2718 detached_head = 1;
2721 * The remote must be configured.
2722 * This is to avoid pushing to the exact same URL as the parent.
2724 remote = pushremote_get(argv[1]);
2725 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2726 die("remote '%s' not configured", argv[1]);
2728 /* Check the refspec */
2729 if (argc > 2) {
2730 int i;
2731 struct ref *local_refs = get_local_heads();
2732 struct refspec refspec = REFSPEC_INIT_PUSH;
2734 refspec_appendn(&refspec, argv + 2, argc - 2);
2736 for (i = 0; i < refspec.nr; i++) {
2737 const struct refspec_item *rs = &refspec.items[i];
2739 if (rs->pattern || rs->matching)
2740 continue;
2742 /* LHS must match a single ref */
2743 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2744 case 1:
2745 break;
2746 case 0:
2748 * If LHS matches 'HEAD' then we need to ensure
2749 * that it matches the same named branch
2750 * checked out in the superproject.
2752 if (!strcmp(rs->src, "HEAD")) {
2753 if (!detached_head &&
2754 !strcmp(head, superproject_head))
2755 break;
2756 die("HEAD does not match the named branch in the superproject");
2758 /* fallthrough */
2759 default:
2760 die("src refspec '%s' must name a ref",
2761 rs->src);
2764 refspec_clear(&refspec);
2766 free(head);
2768 return 0;
2771 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2773 int i;
2774 struct pathspec pathspec;
2775 struct module_list list = MODULE_LIST_INIT;
2776 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2778 struct option embed_gitdir_options[] = {
2779 OPT_STRING(0, "prefix", &prefix,
2780 N_("path"),
2781 N_("path into the working tree")),
2782 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2783 ABSORB_GITDIR_RECURSE_SUBMODULES),
2784 OPT_END()
2787 const char *const git_submodule_helper_usage[] = {
2788 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2789 NULL
2792 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2793 git_submodule_helper_usage, 0);
2795 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2796 return 1;
2798 for (i = 0; i < list.nr; i++)
2799 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2801 return 0;
2804 static int is_active(int argc, const char **argv, const char *prefix)
2806 if (argc != 2)
2807 die("submodule--helper is-active takes exactly 1 argument");
2809 return !is_submodule_active(the_repository, argv[1]);
2813 * Exit non-zero if any of the submodule names given on the command line is
2814 * invalid. If no names are given, filter stdin to print only valid names
2815 * (which is primarily intended for testing).
2817 static int check_name(int argc, const char **argv, const char *prefix)
2819 if (argc > 1) {
2820 while (*++argv) {
2821 if (check_submodule_name(*argv) < 0)
2822 return 1;
2824 } else {
2825 struct strbuf buf = STRBUF_INIT;
2826 while (strbuf_getline(&buf, stdin) != EOF) {
2827 if (!check_submodule_name(buf.buf))
2828 printf("%s\n", buf.buf);
2830 strbuf_release(&buf);
2832 return 0;
2835 static int module_config(int argc, const char **argv, const char *prefix)
2837 enum {
2838 CHECK_WRITEABLE = 1,
2839 DO_UNSET = 2
2840 } command = 0;
2842 struct option module_config_options[] = {
2843 OPT_CMDMODE(0, "check-writeable", &command,
2844 N_("check if it is safe to write to the .gitmodules file"),
2845 CHECK_WRITEABLE),
2846 OPT_CMDMODE(0, "unset", &command,
2847 N_("unset the config in the .gitmodules file"),
2848 DO_UNSET),
2849 OPT_END()
2851 const char *const git_submodule_helper_usage[] = {
2852 N_("git submodule--helper config <name> [<value>]"),
2853 N_("git submodule--helper config --unset <name>"),
2854 "git submodule--helper config --check-writeable",
2855 NULL
2858 argc = parse_options(argc, argv, prefix, module_config_options,
2859 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2861 if (argc == 1 && command == CHECK_WRITEABLE)
2862 return is_writing_gitmodules_ok() ? 0 : -1;
2864 /* Equivalent to ACTION_GET in builtin/config.c */
2865 if (argc == 2 && command != DO_UNSET)
2866 return print_config_from_gitmodules(the_repository, argv[1]);
2868 /* Equivalent to ACTION_SET in builtin/config.c */
2869 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2870 const char *value = (argc == 3) ? argv[2] : NULL;
2872 if (!is_writing_gitmodules_ok())
2873 die(_("please make sure that the .gitmodules file is in the working tree"));
2875 return config_set_in_gitmodules_file_gently(argv[1], value);
2878 usage_with_options(git_submodule_helper_usage, module_config_options);
2881 static int module_set_url(int argc, const char **argv, const char *prefix)
2883 int quiet = 0;
2884 const char *newurl;
2885 const char *path;
2886 char *config_name;
2888 struct option options[] = {
2889 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2890 OPT_END()
2892 const char *const usage[] = {
2893 N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
2894 NULL
2897 argc = parse_options(argc, argv, prefix, options, usage, 0);
2899 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2900 usage_with_options(usage, options);
2902 config_name = xstrfmt("submodule.%s.url", path);
2904 config_set_in_gitmodules_file_gently(config_name, newurl);
2905 sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
2907 free(config_name);
2909 return 0;
2912 static int module_set_branch(int argc, const char **argv, const char *prefix)
2914 int opt_default = 0, ret;
2915 const char *opt_branch = NULL;
2916 const char *path;
2917 char *config_name;
2920 * We accept the `quiet` option for uniformity across subcommands,
2921 * though there is nothing to make less verbose in this subcommand.
2923 struct option options[] = {
2924 OPT_NOOP_NOARG('q', "quiet"),
2925 OPT_BOOL('d', "default", &opt_default,
2926 N_("set the default tracking branch to master")),
2927 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2928 N_("set the default tracking branch")),
2929 OPT_END()
2931 const char *const usage[] = {
2932 N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
2933 N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2934 NULL
2937 argc = parse_options(argc, argv, prefix, options, usage, 0);
2939 if (!opt_branch && !opt_default)
2940 die(_("--branch or --default required"));
2942 if (opt_branch && opt_default)
2943 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2945 if (argc != 1 || !(path = argv[0]))
2946 usage_with_options(usage, options);
2948 config_name = xstrfmt("submodule.%s.branch", path);
2949 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2951 free(config_name);
2952 return !!ret;
2955 static int module_create_branch(int argc, const char **argv, const char *prefix)
2957 enum branch_track track;
2958 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
2960 struct option options[] = {
2961 OPT__QUIET(&quiet, N_("print only error messages")),
2962 OPT__FORCE(&force, N_("force creation"), 0),
2963 OPT_BOOL(0, "create-reflog", &reflog,
2964 N_("create the branch's reflog")),
2965 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
2966 N_("set branch tracking configuration"),
2967 PARSE_OPT_OPTARG,
2968 parse_opt_tracking_mode),
2969 OPT__DRY_RUN(&dry_run,
2970 N_("show whether the branch would be created")),
2971 OPT_END()
2973 const char *const usage[] = {
2974 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2975 NULL
2978 git_config(git_default_config, NULL);
2979 track = git_branch_track;
2980 argc = parse_options(argc, argv, prefix, options, usage, 0);
2982 if (argc != 3)
2983 usage_with_options(usage, options);
2985 if (!quiet && !dry_run)
2986 printf_ln(_("creating branch '%s'"), argv[0]);
2988 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
2989 force, reflog, quiet, track, dry_run);
2990 return 0;
2993 struct add_data {
2994 const char *prefix;
2995 const char *branch;
2996 const char *reference_path;
2997 char *sm_path;
2998 const char *sm_name;
2999 const char *repo;
3000 const char *realrepo;
3001 int depth;
3002 unsigned int force: 1;
3003 unsigned int quiet: 1;
3004 unsigned int progress: 1;
3005 unsigned int dissociate: 1;
3007 #define ADD_DATA_INIT { .depth = -1 }
3009 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3011 struct child_process cp_remote = CHILD_PROCESS_INIT;
3012 struct strbuf sb_remote_out = STRBUF_INIT;
3014 cp_remote.git_cmd = 1;
3015 strvec_pushf(&cp_remote.env,
3016 "GIT_DIR=%s", git_dir_path);
3017 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3018 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3019 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3020 char *next_line;
3021 char *line = sb_remote_out.buf;
3022 while ((next_line = strchr(line, '\n')) != NULL) {
3023 size_t len = next_line - line;
3024 if (strip_suffix_mem(line, &len, " (fetch)"))
3025 strbuf_addf(msg, " %.*s\n", (int)len, line);
3026 line = next_line + 1;
3030 strbuf_release(&sb_remote_out);
3033 static int add_submodule(const struct add_data *add_data)
3035 char *submod_gitdir_path;
3036 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3038 /* perhaps the path already exists and is already a git repo, else clone it */
3039 if (is_directory(add_data->sm_path)) {
3040 struct strbuf sm_path = STRBUF_INIT;
3041 strbuf_addstr(&sm_path, add_data->sm_path);
3042 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3043 if (is_nonbare_repository_dir(&sm_path))
3044 printf(_("Adding existing repo at '%s' to the index\n"),
3045 add_data->sm_path);
3046 else
3047 die(_("'%s' already exists and is not a valid git repo"),
3048 add_data->sm_path);
3049 strbuf_release(&sm_path);
3050 free(submod_gitdir_path);
3051 } else {
3052 struct child_process cp = CHILD_PROCESS_INIT;
3053 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3055 if (is_directory(submod_gitdir_path)) {
3056 if (!add_data->force) {
3057 struct strbuf msg = STRBUF_INIT;
3058 char *die_msg;
3060 strbuf_addf(&msg, _("A git directory for '%s' is found "
3061 "locally with remote(s):\n"),
3062 add_data->sm_name);
3064 append_fetch_remotes(&msg, submod_gitdir_path);
3065 free(submod_gitdir_path);
3067 strbuf_addf(&msg, _("If you want to reuse this local git "
3068 "directory instead of cloning again from\n"
3069 " %s\n"
3070 "use the '--force' option. If the local git "
3071 "directory is not the correct repo\n"
3072 "or you are unsure what this means choose "
3073 "another name with the '--name' option."),
3074 add_data->realrepo);
3076 die_msg = strbuf_detach(&msg, NULL);
3077 die("%s", die_msg);
3078 } else {
3079 printf(_("Reactivating local git directory for "
3080 "submodule '%s'\n"), add_data->sm_name);
3083 free(submod_gitdir_path);
3085 clone_data.prefix = add_data->prefix;
3086 clone_data.path = add_data->sm_path;
3087 clone_data.name = add_data->sm_name;
3088 clone_data.url = add_data->realrepo;
3089 clone_data.quiet = add_data->quiet;
3090 clone_data.progress = add_data->progress;
3091 if (add_data->reference_path)
3092 string_list_append(&clone_data.reference,
3093 xstrdup(add_data->reference_path));
3094 clone_data.dissociate = add_data->dissociate;
3095 if (add_data->depth >= 0)
3096 clone_data.depth = xstrfmt("%d", add_data->depth);
3098 if (clone_submodule(&clone_data))
3099 return -1;
3101 prepare_submodule_repo_env(&cp.env);
3102 cp.git_cmd = 1;
3103 cp.dir = add_data->sm_path;
3105 * NOTE: we only get here if add_data->force is true, so
3106 * passing --force to checkout is reasonable.
3108 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3110 if (add_data->branch) {
3111 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3112 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3115 if (run_command(&cp))
3116 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3118 return 0;
3121 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3123 char *key;
3124 int ret;
3126 if (!is_writing_gitmodules_ok())
3127 die(_("please make sure that the .gitmodules file is in the working tree"));
3129 key = xstrfmt("submodule.%s.%s", name, var);
3130 ret = config_set_in_gitmodules_file_gently(key, value);
3131 free(key);
3133 return ret;
3136 static void configure_added_submodule(struct add_data *add_data)
3138 char *key;
3139 char *val = NULL;
3140 struct child_process add_submod = CHILD_PROCESS_INIT;
3141 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3143 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3144 git_config_set_gently(key, add_data->realrepo);
3145 free(key);
3147 add_submod.git_cmd = 1;
3148 strvec_pushl(&add_submod.args, "add",
3149 "--no-warn-embedded-repo", NULL);
3150 if (add_data->force)
3151 strvec_push(&add_submod.args, "--force");
3152 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3154 if (run_command(&add_submod))
3155 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3157 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3158 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3159 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3161 if (add_data->branch) {
3162 if (config_submodule_in_gitmodules(add_data->sm_name,
3163 "branch", add_data->branch))
3164 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3167 add_gitmodules.git_cmd = 1;
3168 strvec_pushl(&add_gitmodules.args,
3169 "add", "--force", "--", ".gitmodules", NULL);
3171 if (run_command(&add_gitmodules))
3172 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3175 * NEEDSWORK: In a multi-working-tree world this needs to be
3176 * set in the per-worktree config.
3179 * NEEDSWORK: In the longer run, we need to get rid of this
3180 * pattern of querying "submodule.active" before calling
3181 * is_submodule_active(), since that function needs to find
3182 * out the value of "submodule.active" again anyway.
3184 if (!git_config_get_string("submodule.active", &val) && val) {
3186 * If the submodule being added isn't already covered by the
3187 * current configured pathspec, set the submodule's active flag
3189 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3190 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3191 git_config_set_gently(key, "true");
3192 free(key);
3194 } else {
3195 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3196 git_config_set_gently(key, "true");
3197 free(key);
3201 static void die_on_index_match(const char *path, int force)
3203 struct pathspec ps;
3204 const char *args[] = { path, NULL };
3205 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3207 if (read_cache_preload(NULL) < 0)
3208 die(_("index file corrupt"));
3210 if (ps.nr) {
3211 int i;
3212 char *ps_matched = xcalloc(ps.nr, 1);
3214 /* TODO: audit for interaction with sparse-index. */
3215 ensure_full_index(&the_index);
3218 * Since there is only one pathspec, we just need
3219 * need to check ps_matched[0] to know if a cache
3220 * entry matched.
3222 for (i = 0; i < active_nr; i++) {
3223 ce_path_match(&the_index, active_cache[i], &ps,
3224 ps_matched);
3226 if (ps_matched[0]) {
3227 if (!force)
3228 die(_("'%s' already exists in the index"),
3229 path);
3230 if (!S_ISGITLINK(active_cache[i]->ce_mode))
3231 die(_("'%s' already exists in the index "
3232 "and is not a submodule"), path);
3233 break;
3236 free(ps_matched);
3238 clear_pathspec(&ps);
3241 static void die_on_repo_without_commits(const char *path)
3243 struct strbuf sb = STRBUF_INIT;
3244 strbuf_addstr(&sb, path);
3245 if (is_nonbare_repository_dir(&sb)) {
3246 struct object_id oid;
3247 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3248 die(_("'%s' does not have a commit checked out"), path);
3250 strbuf_release(&sb);
3253 static int module_add(int argc, const char **argv, const char *prefix)
3255 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3256 struct add_data add_data = ADD_DATA_INIT;
3257 char *to_free = NULL;
3259 struct option options[] = {
3260 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3261 N_("branch of repository to add as submodule")),
3262 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3263 PARSE_OPT_NOCOMPLETE),
3264 OPT__QUIET(&quiet, N_("print only error messages")),
3265 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3266 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3267 N_("reference repository")),
3268 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3269 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3270 N_("sets the submodule's name to the given string "
3271 "instead of defaulting to its path")),
3272 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3273 OPT_END()
3276 const char *const usage[] = {
3277 N_("git submodule--helper add [<options>] [--] <repository> [<path>]"),
3278 NULL
3281 argc = parse_options(argc, argv, prefix, options, usage, 0);
3283 if (!is_writing_gitmodules_ok())
3284 die(_("please make sure that the .gitmodules file is in the working tree"));
3286 if (prefix && *prefix &&
3287 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3288 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3290 if (argc == 0 || argc > 2)
3291 usage_with_options(usage, options);
3293 add_data.repo = argv[0];
3294 if (argc == 1)
3295 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3296 else
3297 add_data.sm_path = xstrdup(argv[1]);
3299 if (prefix && *prefix && !is_absolute_path(add_data.sm_path))
3300 add_data.sm_path = xstrfmt("%s%s", prefix, add_data.sm_path);
3302 if (starts_with_dot_dot_slash(add_data.repo) ||
3303 starts_with_dot_slash(add_data.repo)) {
3304 if (prefix)
3305 die(_("Relative path can only be used from the toplevel "
3306 "of the working tree"));
3308 /* dereference source url relative to parent's url */
3309 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3310 add_data.realrepo = to_free;
3311 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3312 add_data.realrepo = add_data.repo;
3313 } else {
3314 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3315 add_data.repo);
3319 * normalize path:
3320 * multiple //; leading ./; /./; /../;
3322 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3323 strip_dir_trailing_slashes(add_data.sm_path);
3325 die_on_index_match(add_data.sm_path, force);
3326 die_on_repo_without_commits(add_data.sm_path);
3328 if (!force) {
3329 int exit_code = -1;
3330 struct strbuf sb = STRBUF_INIT;
3331 struct child_process cp = CHILD_PROCESS_INIT;
3332 cp.git_cmd = 1;
3333 cp.no_stdout = 1;
3334 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3335 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3336 if ((exit_code = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3337 strbuf_complete_line(&sb);
3338 fputs(sb.buf, stderr);
3339 free(add_data.sm_path);
3340 return exit_code;
3342 strbuf_release(&sb);
3345 if(!add_data.sm_name)
3346 add_data.sm_name = add_data.sm_path;
3348 if (check_submodule_name(add_data.sm_name))
3349 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3351 add_data.prefix = prefix;
3352 add_data.force = !!force;
3353 add_data.quiet = !!quiet;
3354 add_data.progress = !!progress;
3355 add_data.dissociate = !!dissociate;
3357 if (add_submodule(&add_data)) {
3358 free(add_data.sm_path);
3359 return 1;
3361 configure_added_submodule(&add_data);
3362 free(add_data.sm_path);
3363 free(to_free);
3365 return 0;
3368 #define SUPPORT_SUPER_PREFIX (1<<0)
3370 struct cmd_struct {
3371 const char *cmd;
3372 int (*fn)(int, const char **, const char *);
3373 unsigned option;
3376 static struct cmd_struct commands[] = {
3377 {"list", module_list, 0},
3378 {"name", module_name, 0},
3379 {"clone", module_clone, 0},
3380 {"add", module_add, SUPPORT_SUPER_PREFIX},
3381 {"update", module_update, 0},
3382 {"resolve-relative-url-test", resolve_relative_url_test, 0},
3383 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
3384 {"init", module_init, SUPPORT_SUPER_PREFIX},
3385 {"status", module_status, SUPPORT_SUPER_PREFIX},
3386 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
3387 {"deinit", module_deinit, 0},
3388 {"summary", module_summary, SUPPORT_SUPER_PREFIX},
3389 {"push-check", push_check, 0},
3390 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3391 {"is-active", is_active, 0},
3392 {"check-name", check_name, 0},
3393 {"config", module_config, 0},
3394 {"set-url", module_set_url, 0},
3395 {"set-branch", module_set_branch, 0},
3396 {"create-branch", module_create_branch, 0},
3399 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3401 int i;
3402 if (argc < 2 || !strcmp(argv[1], "-h"))
3403 usage("git submodule--helper <command>");
3405 for (i = 0; i < ARRAY_SIZE(commands); i++) {
3406 if (!strcmp(argv[1], commands[i].cmd)) {
3407 if (get_super_prefix() &&
3408 !(commands[i].option & SUPPORT_SUPER_PREFIX))
3409 die(_("%s doesn't support --super-prefix"),
3410 commands[i].cmd);
3411 return commands[i].fn(argc - 1, argv + 1, prefix);
3415 die(_("'%s' is not a valid submodule--helper "
3416 "subcommand"), argv[1]);