debian: new upstream point release
[git/debian.git] / builtin / submodule--helper.c
blob5a71b1ee7ef5d0aa0f883a40b3f96c3da7e95cac
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "abspath.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "repository.h"
8 #include "config.h"
9 #include "parse-options.h"
10 #include "quote.h"
11 #include "path.h"
12 #include "pathspec.h"
13 #include "preload-index.h"
14 #include "dir.h"
15 #include "read-cache.h"
16 #include "setup.h"
17 #include "sparse-index.h"
18 #include "submodule.h"
19 #include "submodule-config.h"
20 #include "string-list.h"
21 #include "run-command.h"
22 #include "remote.h"
23 #include "refs.h"
24 #include "refspec.h"
25 #include "revision.h"
26 #include "diffcore.h"
27 #include "diff.h"
28 #include "object-file.h"
29 #include "object-name.h"
30 #include "object-store-ll.h"
31 #include "advice.h"
32 #include "branch.h"
33 #include "list-objects-filter-options.h"
35 #define OPT_QUIET (1 << 0)
36 #define OPT_CACHED (1 << 1)
37 #define OPT_RECURSIVE (1 << 2)
38 #define OPT_FORCE (1 << 3)
40 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
41 void *cb_data);
43 static int repo_get_default_remote(struct repository *repo, char **default_remote)
45 char *dest = NULL;
46 struct strbuf sb = STRBUF_INIT;
47 struct ref_store *store = get_main_ref_store(repo);
48 const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
49 NULL);
51 if (!refname)
52 return die_message(_("No such ref: %s"), "HEAD");
54 /* detached HEAD */
55 if (!strcmp(refname, "HEAD")) {
56 *default_remote = xstrdup("origin");
57 return 0;
60 if (!skip_prefix(refname, "refs/heads/", &refname))
61 return die_message(_("Expecting a full ref name, got %s"),
62 refname);
64 strbuf_addf(&sb, "branch.%s.remote", refname);
65 if (repo_config_get_string(repo, sb.buf, &dest))
66 *default_remote = xstrdup("origin");
67 else
68 *default_remote = dest;
70 strbuf_release(&sb);
71 return 0;
74 static int get_default_remote_submodule(const char *module_path, char **default_remote)
76 struct repository subrepo;
77 int ret;
79 if (repo_submodule_init(&subrepo, the_repository, module_path,
80 null_oid()) < 0)
81 return die_message(_("could not get a repository handle for submodule '%s'"),
82 module_path);
83 ret = repo_get_default_remote(&subrepo, default_remote);
84 repo_clear(&subrepo);
86 return ret;
89 static char *get_default_remote(void)
91 char *default_remote;
92 int code = repo_get_default_remote(the_repository, &default_remote);
94 if (code)
95 exit(code);
97 return default_remote;
100 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
102 char *remoteurl, *resolved_url;
103 char *remote = get_default_remote();
104 struct strbuf remotesb = STRBUF_INIT;
106 strbuf_addf(&remotesb, "remote.%s.url", remote);
107 if (git_config_get_string(remotesb.buf, &remoteurl)) {
108 if (!quiet)
109 warning(_("could not look up configuration '%s'. "
110 "Assuming this repository is its own "
111 "authoritative upstream."),
112 remotesb.buf);
113 remoteurl = xgetcwd();
115 resolved_url = relative_url(remoteurl, rel_url, up_path);
117 free(remote);
118 free(remoteurl);
119 strbuf_release(&remotesb);
121 return resolved_url;
124 /* the result should be freed by the caller. */
125 static char *get_submodule_displaypath(const char *path, const char *prefix,
126 const char *super_prefix)
128 if (prefix && super_prefix) {
129 BUG("cannot have prefix '%s' and superprefix '%s'",
130 prefix, super_prefix);
131 } else if (prefix) {
132 struct strbuf sb = STRBUF_INIT;
133 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
134 strbuf_release(&sb);
135 return displaypath;
136 } else if (super_prefix) {
137 return xstrfmt("%s%s", super_prefix, path);
138 } else {
139 return xstrdup(path);
143 static char *compute_rev_name(const char *sub_path, const char* object_id)
145 struct strbuf sb = STRBUF_INIT;
146 const char ***d;
148 static const char *describe_bare[] = { NULL };
150 static const char *describe_tags[] = { "--tags", NULL };
152 static const char *describe_contains[] = { "--contains", NULL };
154 static const char *describe_all_always[] = { "--all", "--always", NULL };
156 static const char **describe_argv[] = { describe_bare, describe_tags,
157 describe_contains,
158 describe_all_always, NULL };
160 for (d = describe_argv; *d; d++) {
161 struct child_process cp = CHILD_PROCESS_INIT;
162 prepare_submodule_repo_env(&cp.env);
163 cp.dir = sub_path;
164 cp.git_cmd = 1;
165 cp.no_stderr = 1;
167 strvec_push(&cp.args, "describe");
168 strvec_pushv(&cp.args, *d);
169 strvec_push(&cp.args, object_id);
171 if (!capture_command(&cp, &sb, 0)) {
172 strbuf_strip_suffix(&sb, "\n");
173 return strbuf_detach(&sb, NULL);
177 strbuf_release(&sb);
178 return NULL;
181 struct module_list {
182 const struct cache_entry **entries;
183 int alloc, nr;
185 #define MODULE_LIST_INIT { 0 }
187 static void module_list_release(struct module_list *ml)
189 free(ml->entries);
192 static int module_list_compute(const char **argv,
193 const char *prefix,
194 struct pathspec *pathspec,
195 struct module_list *list)
197 int i, result = 0;
198 char *ps_matched = NULL;
200 parse_pathspec(pathspec, 0,
201 PATHSPEC_PREFER_FULL,
202 prefix, argv);
204 if (pathspec->nr)
205 ps_matched = xcalloc(pathspec->nr, 1);
207 if (repo_read_index(the_repository) < 0)
208 die(_("index file corrupt"));
210 for (i = 0; i < the_index.cache_nr; i++) {
211 const struct cache_entry *ce = the_index.cache[i];
213 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
214 0, ps_matched, 1) ||
215 !S_ISGITLINK(ce->ce_mode))
216 continue;
218 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
219 list->entries[list->nr++] = ce;
220 while (i + 1 < the_index.cache_nr &&
221 !strcmp(ce->name, the_index.cache[i + 1]->name))
223 * Skip entries with the same name in different stages
224 * to make sure an entry is returned only once.
226 i++;
229 if (ps_matched && report_path_error(ps_matched, pathspec))
230 result = -1;
232 free(ps_matched);
234 return result;
237 static void module_list_active(struct module_list *list)
239 int i;
240 struct module_list active_modules = MODULE_LIST_INIT;
242 for (i = 0; i < list->nr; i++) {
243 const struct cache_entry *ce = list->entries[i];
245 if (!is_submodule_active(the_repository, ce->name))
246 continue;
248 ALLOC_GROW(active_modules.entries,
249 active_modules.nr + 1,
250 active_modules.alloc);
251 active_modules.entries[active_modules.nr++] = ce;
254 module_list_release(list);
255 *list = active_modules;
258 static char *get_up_path(const char *path)
260 int i;
261 struct strbuf sb = STRBUF_INIT;
263 for (i = count_slashes(path); i; i--)
264 strbuf_addstr(&sb, "../");
267 * Check if 'path' ends with slash or not
268 * for having the same output for dir/sub_dir
269 * and dir/sub_dir/
271 if (!is_dir_sep(path[strlen(path) - 1]))
272 strbuf_addstr(&sb, "../");
274 return strbuf_detach(&sb, NULL);
277 static void for_each_listed_submodule(const struct module_list *list,
278 each_submodule_fn fn, void *cb_data)
280 int i;
282 for (i = 0; i < list->nr; i++)
283 fn(list->entries[i], cb_data);
286 struct foreach_cb {
287 int argc;
288 const char **argv;
289 const char *prefix;
290 const char *super_prefix;
291 int quiet;
292 int recursive;
294 #define FOREACH_CB_INIT { 0 }
296 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
297 void *cb_data)
299 struct foreach_cb *info = cb_data;
300 const char *path = list_item->name;
301 const struct object_id *ce_oid = &list_item->oid;
302 const struct submodule *sub;
303 struct child_process cp = CHILD_PROCESS_INIT;
304 char *displaypath;
306 if (validate_submodule_path(path) < 0)
307 exit(128);
309 displaypath = get_submodule_displaypath(path, info->prefix,
310 info->super_prefix);
312 sub = submodule_from_path(the_repository, null_oid(), path);
314 if (!sub)
315 die(_("No url found for submodule path '%s' in .gitmodules"),
316 displaypath);
318 if (!is_submodule_populated_gently(path, NULL))
319 goto cleanup;
321 prepare_submodule_repo_env(&cp.env);
324 * For the purpose of executing <command> in the submodule,
325 * separate shell is used for the purpose of running the
326 * child process.
328 cp.use_shell = 1;
329 cp.dir = path;
332 * NEEDSWORK: the command currently has access to the variables $name,
333 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
334 * contains a single argument. This is done for maintaining a faithful
335 * translation from shell script.
337 if (info->argc == 1) {
338 char *toplevel = xgetcwd();
339 struct strbuf sb = STRBUF_INIT;
341 strvec_pushf(&cp.env, "name=%s", sub->name);
342 strvec_pushf(&cp.env, "sm_path=%s", path);
343 strvec_pushf(&cp.env, "displaypath=%s", displaypath);
344 strvec_pushf(&cp.env, "sha1=%s",
345 oid_to_hex(ce_oid));
346 strvec_pushf(&cp.env, "toplevel=%s", toplevel);
349 * Since the path variable was accessible from the script
350 * before porting, it is also made available after porting.
351 * The environment variable "PATH" has a very special purpose
352 * on windows. And since environment variables are
353 * case-insensitive in windows, it interferes with the
354 * existing PATH variable. Hence, to avoid that, we expose
355 * path via the args strvec and not via env.
357 sq_quote_buf(&sb, path);
358 strvec_pushf(&cp.args, "path=%s; %s",
359 sb.buf, info->argv[0]);
360 strbuf_release(&sb);
361 free(toplevel);
362 } else {
363 strvec_pushv(&cp.args, info->argv);
366 if (!info->quiet)
367 printf(_("Entering '%s'\n"), displaypath);
369 if (info->argv[0] && run_command(&cp))
370 die(_("run_command returned non-zero status for %s\n."),
371 displaypath);
373 if (info->recursive) {
374 struct child_process cpr = CHILD_PROCESS_INIT;
376 cpr.git_cmd = 1;
377 cpr.dir = path;
378 prepare_submodule_repo_env(&cpr.env);
380 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
381 NULL);
382 strvec_pushl(&cpr.args, "--super-prefix", NULL);
383 strvec_pushf(&cpr.args, "%s/", displaypath);
385 if (info->quiet)
386 strvec_push(&cpr.args, "--quiet");
388 strvec_push(&cpr.args, "--");
389 strvec_pushv(&cpr.args, info->argv);
391 if (run_command(&cpr))
392 die(_("run_command returned non-zero status while "
393 "recursing in the nested submodules of %s\n."),
394 displaypath);
397 cleanup:
398 free(displaypath);
401 static int module_foreach(int argc, const char **argv, const char *prefix)
403 struct foreach_cb info = FOREACH_CB_INIT;
404 struct pathspec pathspec = { 0 };
405 struct module_list list = MODULE_LIST_INIT;
406 struct option module_foreach_options[] = {
407 OPT__SUPER_PREFIX(&info.super_prefix),
408 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
409 OPT_BOOL(0, "recursive", &info.recursive,
410 N_("recurse into nested submodules")),
411 OPT_END()
413 const char *const git_submodule_helper_usage[] = {
414 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
415 NULL
417 int ret = 1;
419 argc = parse_options(argc, argv, prefix, module_foreach_options,
420 git_submodule_helper_usage, 0);
422 if (module_list_compute(NULL, prefix, &pathspec, &list) < 0)
423 goto cleanup;
425 info.argc = argc;
426 info.argv = argv;
427 info.prefix = prefix;
429 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
431 ret = 0;
432 cleanup:
433 module_list_release(&list);
434 clear_pathspec(&pathspec);
435 return ret;
438 static int starts_with_dot_slash(const char *const path)
440 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
441 PATH_MATCH_XPLATFORM);
444 static int starts_with_dot_dot_slash(const char *const path)
446 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
447 PATH_MATCH_XPLATFORM);
450 struct init_cb {
451 const char *prefix;
452 const char *super_prefix;
453 unsigned int flags;
455 #define INIT_CB_INIT { 0 }
457 static void init_submodule(const char *path, const char *prefix,
458 const char *super_prefix,
459 unsigned int flags)
461 const struct submodule *sub;
462 struct strbuf sb = STRBUF_INIT;
463 const char *upd;
464 char *url = NULL, *displaypath;
466 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
468 sub = submodule_from_path(the_repository, null_oid(), path);
470 if (!sub)
471 die(_("No url found for submodule path '%s' in .gitmodules"),
472 displaypath);
475 * NEEDSWORK: In a multi-working-tree world, this needs to be
476 * set in the per-worktree config.
478 * Set active flag for the submodule being initialized
480 if (!is_submodule_active(the_repository, path)) {
481 strbuf_addf(&sb, "submodule.%s.active", sub->name);
482 git_config_set_gently(sb.buf, "true");
483 strbuf_reset(&sb);
487 * Copy url setting when it is not set yet.
488 * To look up the url in .git/config, we must not fall back to
489 * .gitmodules, so look it up directly.
491 strbuf_addf(&sb, "submodule.%s.url", sub->name);
492 if (git_config_get_string(sb.buf, &url)) {
493 if (!sub->url)
494 die(_("No url found for submodule path '%s' in .gitmodules"),
495 displaypath);
497 url = xstrdup(sub->url);
499 /* Possibly a url relative to parent */
500 if (starts_with_dot_dot_slash(url) ||
501 starts_with_dot_slash(url)) {
502 char *oldurl = url;
504 url = resolve_relative_url(oldurl, NULL, 0);
505 free(oldurl);
508 if (git_config_set_gently(sb.buf, url))
509 die(_("Failed to register url for submodule path '%s'"),
510 displaypath);
511 if (!(flags & OPT_QUIET))
512 fprintf(stderr,
513 _("Submodule '%s' (%s) registered for path '%s'\n"),
514 sub->name, url, displaypath);
516 strbuf_reset(&sb);
518 /* Copy "update" setting when it is not set yet */
519 strbuf_addf(&sb, "submodule.%s.update", sub->name);
520 if (git_config_get_string_tmp(sb.buf, &upd) &&
521 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
522 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
523 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
524 sub->name);
525 upd = "none";
526 } else {
527 upd = submodule_update_type_to_string(sub->update_strategy.type);
530 if (git_config_set_gently(sb.buf, upd))
531 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
533 strbuf_release(&sb);
534 free(displaypath);
535 free(url);
538 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
540 struct init_cb *info = cb_data;
542 init_submodule(list_item->name, info->prefix, info->super_prefix,
543 info->flags);
546 static int module_init(int argc, const char **argv, const char *prefix)
548 struct init_cb info = INIT_CB_INIT;
549 struct pathspec pathspec = { 0 };
550 struct module_list list = MODULE_LIST_INIT;
551 int quiet = 0;
552 struct option module_init_options[] = {
553 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
554 OPT_END()
556 const char *const git_submodule_helper_usage[] = {
557 N_("git submodule init [<options>] [<path>]"),
558 NULL
560 int ret = 1;
562 argc = parse_options(argc, argv, prefix, module_init_options,
563 git_submodule_helper_usage, 0);
565 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
566 goto cleanup;
569 * If there are no path args and submodule.active is set then,
570 * by default, only initialize 'active' modules.
572 if (!argc && !git_config_get("submodule.active"))
573 module_list_active(&list);
575 info.prefix = prefix;
576 if (quiet)
577 info.flags |= OPT_QUIET;
579 for_each_listed_submodule(&list, init_submodule_cb, &info);
581 ret = 0;
582 cleanup:
583 module_list_release(&list);
584 clear_pathspec(&pathspec);
585 return ret;
588 struct status_cb {
589 const char *prefix;
590 const char *super_prefix;
591 unsigned int flags;
593 #define STATUS_CB_INIT { 0 }
595 static void print_status(unsigned int flags, char state, const char *path,
596 const struct object_id *oid, const char *displaypath)
598 if (flags & OPT_QUIET)
599 return;
601 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
603 if (state == ' ' || state == '+') {
604 char *name = compute_rev_name(path, oid_to_hex(oid));
606 if (name)
607 printf(" (%s)", name);
608 free(name);
611 printf("\n");
614 static int handle_submodule_head_ref(const char *refname UNUSED,
615 const struct object_id *oid,
616 int flags UNUSED,
617 void *cb_data)
619 struct object_id *output = cb_data;
621 if (oid)
622 oidcpy(output, oid);
624 return 0;
627 static void status_submodule(const char *path, const struct object_id *ce_oid,
628 unsigned int ce_flags, const char *prefix,
629 const char *super_prefix, unsigned int flags)
631 char *displaypath;
632 struct strvec diff_files_args = STRVEC_INIT;
633 struct rev_info rev = REV_INFO_INIT;
634 struct strbuf buf = STRBUF_INIT;
635 const char *git_dir;
636 struct setup_revision_opt opt = {
637 .free_removed_argv_elements = 1,
640 if (validate_submodule_path(path) < 0)
641 exit(128);
643 if (!submodule_from_path(the_repository, null_oid(), path))
644 die(_("no submodule mapping found in .gitmodules for path '%s'"),
645 path);
647 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
649 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
650 print_status(flags, 'U', path, null_oid(), displaypath);
651 goto cleanup;
654 strbuf_addf(&buf, "%s/.git", path);
655 git_dir = read_gitfile(buf.buf);
656 if (!git_dir)
657 git_dir = buf.buf;
659 if (!is_submodule_active(the_repository, path) ||
660 !is_git_directory(git_dir)) {
661 print_status(flags, '-', path, ce_oid, displaypath);
662 strbuf_release(&buf);
663 goto cleanup;
665 strbuf_release(&buf);
667 strvec_pushl(&diff_files_args, "diff-files",
668 "--ignore-submodules=dirty", "--quiet", "--",
669 path, NULL);
671 git_config(git_diff_basic_config, NULL);
673 repo_init_revisions(the_repository, &rev, NULL);
674 rev.abbrev = 0;
675 setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt);
676 run_diff_files(&rev, 0);
678 if (!diff_result_code(&rev.diffopt)) {
679 print_status(flags, ' ', path, ce_oid,
680 displaypath);
681 } else if (!(flags & OPT_CACHED)) {
682 struct object_id oid;
683 struct ref_store *refs = get_submodule_ref_store(path);
685 if (!refs) {
686 print_status(flags, '-', path, ce_oid, displaypath);
687 goto cleanup;
689 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
690 die(_("could not resolve HEAD ref inside the "
691 "submodule '%s'"), path);
693 print_status(flags, '+', path, &oid, displaypath);
694 } else {
695 print_status(flags, '+', path, ce_oid, displaypath);
698 if (flags & OPT_RECURSIVE) {
699 struct child_process cpr = CHILD_PROCESS_INIT;
701 cpr.git_cmd = 1;
702 cpr.dir = path;
703 prepare_submodule_repo_env(&cpr.env);
705 strvec_pushl(&cpr.args, "submodule--helper", "status",
706 "--recursive", NULL);
707 strvec_push(&cpr.args, "--super-prefix");
708 strvec_pushf(&cpr.args, "%s/", displaypath);
710 if (flags & OPT_CACHED)
711 strvec_push(&cpr.args, "--cached");
713 if (flags & OPT_QUIET)
714 strvec_push(&cpr.args, "--quiet");
716 if (run_command(&cpr))
717 die(_("failed to recurse into submodule '%s'"), path);
720 cleanup:
721 strvec_clear(&diff_files_args);
722 free(displaypath);
723 release_revisions(&rev);
726 static void status_submodule_cb(const struct cache_entry *list_item,
727 void *cb_data)
729 struct status_cb *info = cb_data;
731 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
732 info->prefix, info->super_prefix, info->flags);
735 static int module_status(int argc, const char **argv, const char *prefix)
737 struct status_cb info = STATUS_CB_INIT;
738 struct pathspec pathspec = { 0 };
739 struct module_list list = MODULE_LIST_INIT;
740 int quiet = 0;
741 struct option module_status_options[] = {
742 OPT__SUPER_PREFIX(&info.super_prefix),
743 OPT__QUIET(&quiet, N_("suppress submodule status output")),
744 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
745 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
746 OPT_END()
748 const char *const git_submodule_helper_usage[] = {
749 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
750 NULL
752 int ret = 1;
754 argc = parse_options(argc, argv, prefix, module_status_options,
755 git_submodule_helper_usage, 0);
757 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
758 goto cleanup;
760 info.prefix = prefix;
761 if (quiet)
762 info.flags |= OPT_QUIET;
764 for_each_listed_submodule(&list, status_submodule_cb, &info);
766 ret = 0;
767 cleanup:
768 module_list_release(&list);
769 clear_pathspec(&pathspec);
770 return ret;
773 struct module_cb {
774 unsigned int mod_src;
775 unsigned int mod_dst;
776 struct object_id oid_src;
777 struct object_id oid_dst;
778 char status;
779 char *sm_path;
781 #define MODULE_CB_INIT { 0 }
783 static void module_cb_release(struct module_cb *mcb)
785 free(mcb->sm_path);
788 struct module_cb_list {
789 struct module_cb **entries;
790 int alloc, nr;
792 #define MODULE_CB_LIST_INIT { 0 }
794 static void module_cb_list_release(struct module_cb_list *mcbl)
796 int i;
798 for (i = 0; i < mcbl->nr; i++) {
799 struct module_cb *mcb = mcbl->entries[i];
801 module_cb_release(mcb);
802 free(mcb);
804 free(mcbl->entries);
807 struct summary_cb {
808 int argc;
809 const char **argv;
810 const char *prefix;
811 const char *super_prefix;
812 unsigned int cached: 1;
813 unsigned int for_status: 1;
814 unsigned int files: 1;
815 int summary_limit;
817 #define SUMMARY_CB_INIT { 0 }
819 enum diff_cmd {
820 DIFF_INDEX,
821 DIFF_FILES
824 static char *verify_submodule_committish(const char *sm_path,
825 const char *committish)
827 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
828 struct strbuf result = STRBUF_INIT;
830 cp_rev_parse.git_cmd = 1;
831 cp_rev_parse.dir = sm_path;
832 prepare_submodule_repo_env(&cp_rev_parse.env);
833 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
834 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
835 strvec_push(&cp_rev_parse.args, "--");
837 if (capture_command(&cp_rev_parse, &result, 0))
838 return NULL;
840 strbuf_trim_trailing_newline(&result);
841 return strbuf_detach(&result, NULL);
844 static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
845 int total_commits, const char *displaypath,
846 const char *src_abbrev, const char *dst_abbrev,
847 struct module_cb *p)
849 if (p->status == 'T') {
850 if (S_ISGITLINK(p->mod_dst))
851 printf(_("* %s %s(blob)->%s(submodule)"),
852 displaypath, src_abbrev, dst_abbrev);
853 else
854 printf(_("* %s %s(submodule)->%s(blob)"),
855 displaypath, src_abbrev, dst_abbrev);
856 } else {
857 printf("* %s %s...%s",
858 displaypath, src_abbrev, dst_abbrev);
861 if (total_commits < 0)
862 printf(":\n");
863 else
864 printf(" (%d):\n", total_commits);
866 if (errmsg) {
867 printf(_("%s"), errmsg);
868 } else if (total_commits > 0) {
869 struct child_process cp_log = CHILD_PROCESS_INIT;
871 cp_log.git_cmd = 1;
872 cp_log.dir = p->sm_path;
873 prepare_submodule_repo_env(&cp_log.env);
874 strvec_pushl(&cp_log.args, "log", NULL);
876 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
877 if (info->summary_limit > 0)
878 strvec_pushf(&cp_log.args, "-%d",
879 info->summary_limit);
881 strvec_pushl(&cp_log.args, "--pretty= %m %s",
882 "--first-parent", NULL);
883 strvec_pushf(&cp_log.args, "%s...%s",
884 src_abbrev, dst_abbrev);
885 } else if (S_ISGITLINK(p->mod_dst)) {
886 strvec_pushl(&cp_log.args, "--pretty= > %s",
887 "-1", dst_abbrev, NULL);
888 } else {
889 strvec_pushl(&cp_log.args, "--pretty= < %s",
890 "-1", src_abbrev, NULL);
892 run_command(&cp_log);
894 printf("\n");
897 static void generate_submodule_summary(struct summary_cb *info,
898 struct module_cb *p)
900 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
901 int missing_src = 0, missing_dst = 0;
902 struct strbuf errmsg = STRBUF_INIT;
903 int total_commits = -1;
905 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
906 if (S_ISGITLINK(p->mod_dst)) {
907 struct ref_store *refs = get_submodule_ref_store(p->sm_path);
909 if (refs)
910 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
911 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
912 struct stat st;
913 int fd = open(p->sm_path, O_RDONLY);
915 if (fd < 0 || fstat(fd, &st) < 0 ||
916 index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
917 p->sm_path, 0))
918 error(_("couldn't hash object from '%s'"), p->sm_path);
919 } else {
920 /* for a submodule removal (mode:0000000), don't warn */
921 if (p->mod_dst)
922 warning(_("unexpected mode %o\n"), p->mod_dst);
926 if (S_ISGITLINK(p->mod_src)) {
927 if (p->status != 'D')
928 src_abbrev = verify_submodule_committish(p->sm_path,
929 oid_to_hex(&p->oid_src));
930 if (!src_abbrev) {
931 missing_src = 1;
933 * As `rev-parse` failed, we fallback to getting
934 * the abbreviated hash using oid_src. We do
935 * this as we might still need the abbreviated
936 * hash in cases like a submodule type change, etc.
938 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
940 } else {
942 * The source does not point to a submodule.
943 * So, we fallback to getting the abbreviation using
944 * oid_src as we might still need the abbreviated
945 * hash in cases like submodule add, etc.
947 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
950 if (S_ISGITLINK(p->mod_dst)) {
951 dst_abbrev = verify_submodule_committish(p->sm_path,
952 oid_to_hex(&p->oid_dst));
953 if (!dst_abbrev) {
954 missing_dst = 1;
956 * As `rev-parse` failed, we fallback to getting
957 * the abbreviated hash using oid_dst. We do
958 * this as we might still need the abbreviated
959 * hash in cases like a submodule type change, etc.
961 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
963 } else {
965 * The destination does not point to a submodule.
966 * So, we fallback to getting the abbreviation using
967 * oid_dst as we might still need the abbreviated
968 * hash in cases like a submodule removal, etc.
970 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
973 displaypath = get_submodule_displaypath(p->sm_path, info->prefix,
974 info->super_prefix);
976 if (!missing_src && !missing_dst) {
977 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
978 struct strbuf sb_rev_list = STRBUF_INIT;
980 strvec_pushl(&cp_rev_list.args, "rev-list",
981 "--first-parent", "--count", NULL);
982 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
983 strvec_pushf(&cp_rev_list.args, "%s...%s",
984 src_abbrev, dst_abbrev);
985 else
986 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
987 src_abbrev : dst_abbrev);
988 strvec_push(&cp_rev_list.args, "--");
990 cp_rev_list.git_cmd = 1;
991 cp_rev_list.dir = p->sm_path;
992 prepare_submodule_repo_env(&cp_rev_list.env);
994 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
995 total_commits = atoi(sb_rev_list.buf);
997 strbuf_release(&sb_rev_list);
998 } else {
1000 * Don't give error msg for modification whose dst is not
1001 * submodule, i.e., deleted or changed to blob
1003 if (S_ISGITLINK(p->mod_dst)) {
1004 if (missing_src && missing_dst) {
1005 strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n",
1006 displaypath, oid_to_hex(&p->oid_src),
1007 oid_to_hex(&p->oid_dst));
1008 } else {
1009 strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n",
1010 displaypath, missing_src ?
1011 oid_to_hex(&p->oid_src) :
1012 oid_to_hex(&p->oid_dst));
1017 print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
1018 total_commits, displaypath, src_abbrev,
1019 dst_abbrev, p);
1021 free(displaypath);
1022 free(src_abbrev);
1023 free(dst_abbrev);
1024 strbuf_release(&errmsg);
1027 static void prepare_submodule_summary(struct summary_cb *info,
1028 struct module_cb_list *list)
1030 int i;
1031 for (i = 0; i < list->nr; i++) {
1032 const struct submodule *sub;
1033 struct module_cb *p = list->entries[i];
1034 struct strbuf sm_gitdir = STRBUF_INIT;
1036 if (p->status == 'D' || p->status == 'T') {
1037 generate_submodule_summary(info, p);
1038 continue;
1041 if (info->for_status && p->status != 'A' &&
1042 (sub = submodule_from_path(the_repository,
1043 null_oid(), p->sm_path))) {
1044 char *config_key = NULL;
1045 const char *value;
1046 int ignore_all = 0;
1048 config_key = xstrfmt("submodule.%s.ignore",
1049 sub->name);
1050 if (!git_config_get_string_tmp(config_key, &value))
1051 ignore_all = !strcmp(value, "all");
1052 else if (sub->ignore)
1053 ignore_all = !strcmp(sub->ignore, "all");
1055 free(config_key);
1056 if (ignore_all)
1057 continue;
1060 /* Also show added or modified modules which are checked out */
1061 strbuf_addstr(&sm_gitdir, p->sm_path);
1062 if (is_nonbare_repository_dir(&sm_gitdir))
1063 generate_submodule_summary(info, p);
1064 strbuf_release(&sm_gitdir);
1068 static void submodule_summary_callback(struct diff_queue_struct *q,
1069 struct diff_options *options UNUSED,
1070 void *data)
1072 int i;
1073 struct module_cb_list *list = data;
1074 for (i = 0; i < q->nr; i++) {
1075 struct diff_filepair *p = q->queue[i];
1076 struct module_cb *temp;
1078 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1079 continue;
1080 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1081 temp->mod_src = p->one->mode;
1082 temp->mod_dst = p->two->mode;
1083 temp->oid_src = p->one->oid;
1084 temp->oid_dst = p->two->oid;
1085 temp->status = p->status;
1086 temp->sm_path = xstrdup(p->one->path);
1088 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1089 list->entries[list->nr++] = temp;
1093 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1095 switch (diff_cmd) {
1096 case DIFF_INDEX: return "diff-index";
1097 case DIFF_FILES: return "diff-files";
1098 default: BUG("bad diff_cmd value %d", diff_cmd);
1102 static int compute_summary_module_list(struct object_id *head_oid,
1103 struct summary_cb *info,
1104 enum diff_cmd diff_cmd)
1106 struct strvec diff_args = STRVEC_INIT;
1107 struct rev_info rev;
1108 struct setup_revision_opt opt = {
1109 .free_removed_argv_elements = 1,
1111 struct module_cb_list list = MODULE_CB_LIST_INIT;
1112 int ret = 0;
1114 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1115 if (info->cached)
1116 strvec_push(&diff_args, "--cached");
1117 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1118 if (head_oid)
1119 strvec_push(&diff_args, oid_to_hex(head_oid));
1120 strvec_push(&diff_args, "--");
1121 if (info->argc)
1122 strvec_pushv(&diff_args, info->argv);
1124 git_config(git_diff_basic_config, NULL);
1125 repo_init_revisions(the_repository, &rev, info->prefix);
1126 rev.abbrev = 0;
1127 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1128 setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
1129 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1130 rev.diffopt.format_callback = submodule_summary_callback;
1131 rev.diffopt.format_callback_data = &list;
1133 if (!info->cached) {
1134 if (diff_cmd == DIFF_INDEX)
1135 setup_work_tree();
1136 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
1137 perror("repo_read_index_preload");
1138 ret = -1;
1139 goto cleanup;
1141 } else if (repo_read_index(the_repository) < 0) {
1142 perror("repo_read_cache");
1143 ret = -1;
1144 goto cleanup;
1147 if (diff_cmd == DIFF_INDEX)
1148 run_diff_index(&rev, info->cached ? DIFF_INDEX_CACHED : 0);
1149 else
1150 run_diff_files(&rev, 0);
1151 prepare_submodule_summary(info, &list);
1152 cleanup:
1153 strvec_clear(&diff_args);
1154 release_revisions(&rev);
1155 module_cb_list_release(&list);
1156 return ret;
1159 static int module_summary(int argc, const char **argv, const char *prefix)
1161 struct summary_cb info = SUMMARY_CB_INIT;
1162 int cached = 0;
1163 int for_status = 0;
1164 int files = 0;
1165 int summary_limit = -1;
1166 enum diff_cmd diff_cmd = DIFF_INDEX;
1167 struct object_id head_oid;
1168 int ret;
1169 struct option module_summary_options[] = {
1170 OPT_BOOL(0, "cached", &cached,
1171 N_("use the commit stored in the index instead of the submodule HEAD")),
1172 OPT_BOOL(0, "files", &files,
1173 N_("compare the commit in the index with that in the submodule HEAD")),
1174 OPT_BOOL(0, "for-status", &for_status,
1175 N_("skip submodules with 'ignore_config' value set to 'all'")),
1176 OPT_INTEGER('n', "summary-limit", &summary_limit,
1177 N_("limit the summary size")),
1178 OPT_END()
1180 const char *const git_submodule_helper_usage[] = {
1181 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1182 NULL
1185 argc = parse_options(argc, argv, prefix, module_summary_options,
1186 git_submodule_helper_usage, 0);
1188 if (!summary_limit)
1189 return 0;
1191 if (!repo_get_oid(the_repository, argc ? argv[0] : "HEAD", &head_oid)) {
1192 if (argc) {
1193 argv++;
1194 argc--;
1196 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1197 /* before the first commit: compare with an empty tree */
1198 oidcpy(&head_oid, the_hash_algo->empty_tree);
1199 if (argc) {
1200 argv++;
1201 argc--;
1203 } else {
1204 if (repo_get_oid(the_repository, "HEAD", &head_oid))
1205 die(_("could not fetch a revision for HEAD"));
1208 if (files) {
1209 if (cached)
1210 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1211 diff_cmd = DIFF_FILES;
1214 info.argc = argc;
1215 info.argv = argv;
1216 info.prefix = prefix;
1217 info.cached = !!cached;
1218 info.files = !!files;
1219 info.for_status = !!for_status;
1220 info.summary_limit = summary_limit;
1222 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1223 &info, diff_cmd);
1224 return ret;
1227 struct sync_cb {
1228 const char *prefix;
1229 const char *super_prefix;
1230 unsigned int flags;
1232 #define SYNC_CB_INIT { 0 }
1234 static void sync_submodule(const char *path, const char *prefix,
1235 const char *super_prefix, unsigned int flags)
1237 const struct submodule *sub;
1238 char *remote_key = NULL;
1239 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1240 struct strbuf sb = STRBUF_INIT;
1241 char *sub_config_path = NULL;
1242 int code;
1244 if (!is_submodule_active(the_repository, path))
1245 return;
1247 if (validate_submodule_path(path) < 0)
1248 exit(128);
1250 sub = submodule_from_path(the_repository, null_oid(), path);
1252 if (sub && sub->url) {
1253 if (starts_with_dot_dot_slash(sub->url) ||
1254 starts_with_dot_slash(sub->url)) {
1255 char *up_path = get_up_path(path);
1257 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1258 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1259 free(up_path);
1260 } else {
1261 sub_origin_url = xstrdup(sub->url);
1262 super_config_url = xstrdup(sub->url);
1264 } else {
1265 sub_origin_url = xstrdup("");
1266 super_config_url = xstrdup("");
1269 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
1271 if (!(flags & OPT_QUIET))
1272 printf(_("Synchronizing submodule url for '%s'\n"),
1273 displaypath);
1275 strbuf_reset(&sb);
1276 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1277 if (git_config_set_gently(sb.buf, super_config_url))
1278 die(_("failed to register url for submodule path '%s'"),
1279 displaypath);
1281 if (!is_submodule_populated_gently(path, NULL))
1282 goto cleanup;
1284 strbuf_reset(&sb);
1285 code = get_default_remote_submodule(path, &default_remote);
1286 if (code)
1287 exit(code);
1289 remote_key = xstrfmt("remote.%s.url", default_remote);
1290 free(default_remote);
1292 submodule_to_gitdir(&sb, path);
1293 strbuf_addstr(&sb, "/config");
1295 if (git_config_set_in_file_gently(sb.buf, remote_key, NULL, sub_origin_url))
1296 die(_("failed to update remote for submodule '%s'"),
1297 path);
1299 if (flags & OPT_RECURSIVE) {
1300 struct child_process cpr = CHILD_PROCESS_INIT;
1302 cpr.git_cmd = 1;
1303 cpr.dir = path;
1304 prepare_submodule_repo_env(&cpr.env);
1306 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1307 "--recursive", NULL);
1308 strvec_push(&cpr.args, "--super-prefix");
1309 strvec_pushf(&cpr.args, "%s/", displaypath);
1312 if (flags & OPT_QUIET)
1313 strvec_push(&cpr.args, "--quiet");
1315 if (run_command(&cpr))
1316 die(_("failed to recurse into submodule '%s'"),
1317 path);
1320 cleanup:
1321 free(super_config_url);
1322 free(sub_origin_url);
1323 strbuf_release(&sb);
1324 free(remote_key);
1325 free(displaypath);
1326 free(sub_config_path);
1329 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1331 struct sync_cb *info = cb_data;
1333 sync_submodule(list_item->name, info->prefix, info->super_prefix,
1334 info->flags);
1337 static int module_sync(int argc, const char **argv, const char *prefix)
1339 struct sync_cb info = SYNC_CB_INIT;
1340 struct pathspec pathspec = { 0 };
1341 struct module_list list = MODULE_LIST_INIT;
1342 int quiet = 0;
1343 int recursive = 0;
1344 struct option module_sync_options[] = {
1345 OPT__SUPER_PREFIX(&info.super_prefix),
1346 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1347 OPT_BOOL(0, "recursive", &recursive,
1348 N_("recurse into nested submodules")),
1349 OPT_END()
1351 const char *const git_submodule_helper_usage[] = {
1352 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1353 NULL
1355 int ret = 1;
1357 argc = parse_options(argc, argv, prefix, module_sync_options,
1358 git_submodule_helper_usage, 0);
1360 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1361 goto cleanup;
1363 info.prefix = prefix;
1364 if (quiet)
1365 info.flags |= OPT_QUIET;
1366 if (recursive)
1367 info.flags |= OPT_RECURSIVE;
1369 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1371 ret = 0;
1372 cleanup:
1373 module_list_release(&list);
1374 clear_pathspec(&pathspec);
1375 return ret;
1378 struct deinit_cb {
1379 const char *prefix;
1380 unsigned int flags;
1382 #define DEINIT_CB_INIT { 0 }
1384 static void deinit_submodule(const char *path, const char *prefix,
1385 unsigned int flags)
1387 const struct submodule *sub;
1388 char *displaypath = NULL;
1389 struct child_process cp_config = CHILD_PROCESS_INIT;
1390 struct strbuf sb_config = STRBUF_INIT;
1391 char *sub_git_dir = xstrfmt("%s/.git", path);
1393 if (validate_submodule_path(path) < 0)
1394 exit(128);
1396 sub = submodule_from_path(the_repository, null_oid(), path);
1398 if (!sub || !sub->name)
1399 goto cleanup;
1401 displaypath = get_submodule_displaypath(path, prefix, NULL);
1403 /* remove the submodule work tree (unless the user already did it) */
1404 if (is_directory(path)) {
1405 struct strbuf sb_rm = STRBUF_INIT;
1406 const char *format;
1408 if (is_directory(sub_git_dir)) {
1409 if (!(flags & OPT_QUIET))
1410 warning(_("Submodule work tree '%s' contains a .git "
1411 "directory. This will be replaced with a "
1412 ".git file by using absorbgitdirs."),
1413 displaypath);
1415 absorb_git_dir_into_superproject(path, NULL);
1419 if (!(flags & OPT_FORCE)) {
1420 struct child_process cp_rm = CHILD_PROCESS_INIT;
1422 cp_rm.git_cmd = 1;
1423 strvec_pushl(&cp_rm.args, "rm", "-qn",
1424 path, NULL);
1426 if (run_command(&cp_rm))
1427 die(_("Submodule work tree '%s' contains local "
1428 "modifications; use '-f' to discard them"),
1429 displaypath);
1432 strbuf_addstr(&sb_rm, path);
1434 if (!remove_dir_recursively(&sb_rm, 0))
1435 format = _("Cleared directory '%s'\n");
1436 else
1437 format = _("Could not remove submodule work tree '%s'\n");
1439 if (!(flags & OPT_QUIET))
1440 printf(format, displaypath);
1442 submodule_unset_core_worktree(sub);
1444 strbuf_release(&sb_rm);
1447 if (mkdir(path, 0777))
1448 printf(_("could not create empty submodule directory %s"),
1449 displaypath);
1451 cp_config.git_cmd = 1;
1452 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1453 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1455 /* remove the .git/config entries (unless the user already did it) */
1456 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1457 char *sub_key = xstrfmt("submodule.%s", sub->name);
1460 * remove the whole section so we have a clean state when
1461 * the user later decides to init this submodule again
1463 git_config_rename_section_in_file(NULL, sub_key, NULL);
1464 if (!(flags & OPT_QUIET))
1465 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1466 sub->name, sub->url, displaypath);
1467 free(sub_key);
1470 cleanup:
1471 free(displaypath);
1472 free(sub_git_dir);
1473 strbuf_release(&sb_config);
1476 static void deinit_submodule_cb(const struct cache_entry *list_item,
1477 void *cb_data)
1479 struct deinit_cb *info = cb_data;
1480 deinit_submodule(list_item->name, info->prefix, info->flags);
1483 static int module_deinit(int argc, const char **argv, const char *prefix)
1485 struct deinit_cb info = DEINIT_CB_INIT;
1486 struct pathspec pathspec = { 0 };
1487 struct module_list list = MODULE_LIST_INIT;
1488 int quiet = 0;
1489 int force = 0;
1490 int all = 0;
1491 struct option module_deinit_options[] = {
1492 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1493 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1494 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1495 OPT_END()
1497 const char *const git_submodule_helper_usage[] = {
1498 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1499 NULL
1501 int ret = 1;
1503 argc = parse_options(argc, argv, prefix, module_deinit_options,
1504 git_submodule_helper_usage, 0);
1506 if (all && argc) {
1507 error("pathspec and --all are incompatible");
1508 usage_with_options(git_submodule_helper_usage,
1509 module_deinit_options);
1512 if (!argc && !all)
1513 die(_("Use '--all' if you really want to deinitialize all submodules"));
1515 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1516 goto cleanup;
1518 info.prefix = prefix;
1519 if (quiet)
1520 info.flags |= OPT_QUIET;
1521 if (force)
1522 info.flags |= OPT_FORCE;
1524 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1526 ret = 0;
1527 cleanup:
1528 module_list_release(&list);
1529 clear_pathspec(&pathspec);
1530 return ret;
1533 struct module_clone_data {
1534 const char *prefix;
1535 const char *path;
1536 const char *name;
1537 const char *url;
1538 const char *depth;
1539 struct list_objects_filter_options *filter_options;
1540 unsigned int quiet: 1;
1541 unsigned int progress: 1;
1542 unsigned int dissociate: 1;
1543 unsigned int require_init: 1;
1544 int single_branch;
1546 #define MODULE_CLONE_DATA_INIT { \
1547 .single_branch = -1, \
1550 struct submodule_alternate_setup {
1551 const char *submodule_name;
1552 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1553 SUBMODULE_ALTERNATE_ERROR_DIE,
1554 SUBMODULE_ALTERNATE_ERROR_INFO,
1555 SUBMODULE_ALTERNATE_ERROR_IGNORE
1556 } error_mode;
1557 struct string_list *reference;
1559 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1560 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1563 static const char alternate_error_advice[] = N_(
1564 "An alternate computed from a superproject's alternate is invalid.\n"
1565 "To allow Git to clone without an alternate in such a case, set\n"
1566 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1567 "'--reference-if-able' instead of '--reference'."
1570 static int add_possible_reference_from_superproject(
1571 struct object_directory *odb, void *sas_cb)
1573 struct submodule_alternate_setup *sas = sas_cb;
1574 size_t len;
1577 * If the alternate object store is another repository, try the
1578 * standard layout with .git/(modules/<name>)+/objects
1580 if (strip_suffix(odb->path, "/objects", &len)) {
1581 struct repository alternate;
1582 char *sm_alternate;
1583 struct strbuf sb = STRBUF_INIT;
1584 struct strbuf err = STRBUF_INIT;
1585 strbuf_add(&sb, odb->path, len);
1587 if (repo_init(&alternate, sb.buf, NULL) < 0)
1588 die(_("could not get a repository handle for gitdir '%s'"),
1589 sb.buf);
1592 * We need to end the new path with '/' to mark it as a dir,
1593 * otherwise a submodule name containing '/' will be broken
1594 * as the last part of a missing submodule reference would
1595 * be taken as a file name.
1597 strbuf_reset(&sb);
1598 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1599 strbuf_addch(&sb, '/');
1600 repo_clear(&alternate);
1602 sm_alternate = compute_alternate_path(sb.buf, &err);
1603 if (sm_alternate) {
1604 char *p = strbuf_detach(&sb, NULL);
1606 string_list_append(sas->reference, p)->util = p;
1607 free(sm_alternate);
1608 } else {
1609 switch (sas->error_mode) {
1610 case SUBMODULE_ALTERNATE_ERROR_DIE:
1611 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1612 advise(_(alternate_error_advice));
1613 die(_("submodule '%s' cannot add alternate: %s"),
1614 sas->submodule_name, err.buf);
1615 case SUBMODULE_ALTERNATE_ERROR_INFO:
1616 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1617 sas->submodule_name, err.buf);
1618 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1619 ; /* nothing */
1622 strbuf_release(&sb);
1625 return 0;
1628 static void prepare_possible_alternates(const char *sm_name,
1629 struct string_list *reference)
1631 char *sm_alternate = NULL, *error_strategy = NULL;
1632 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1634 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1635 if (!sm_alternate)
1636 return;
1638 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1640 if (!error_strategy)
1641 error_strategy = xstrdup("die");
1643 sas.submodule_name = sm_name;
1644 sas.reference = reference;
1645 if (!strcmp(error_strategy, "die"))
1646 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1647 else if (!strcmp(error_strategy, "info"))
1648 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1649 else if (!strcmp(error_strategy, "ignore"))
1650 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1651 else
1652 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1654 if (!strcmp(sm_alternate, "superproject"))
1655 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1656 else if (!strcmp(sm_alternate, "no"))
1657 ; /* do nothing */
1658 else
1659 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1661 free(sm_alternate);
1662 free(error_strategy);
1665 static char *clone_submodule_sm_gitdir(const char *name)
1667 struct strbuf sb = STRBUF_INIT;
1668 char *sm_gitdir;
1670 submodule_name_to_gitdir(&sb, the_repository, name);
1671 sm_gitdir = absolute_pathdup(sb.buf);
1672 strbuf_release(&sb);
1674 return sm_gitdir;
1677 static int dir_contains_only_dotgit(const char *path)
1679 DIR *dir = opendir(path);
1680 struct dirent *e;
1681 int ret = 1;
1683 if (!dir)
1684 return 0;
1686 e = readdir_skip_dot_and_dotdot(dir);
1687 if (!e)
1688 ret = 0;
1689 else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT, e->d_name) ||
1690 (e = readdir_skip_dot_and_dotdot(dir))) {
1691 error("unexpected item '%s' in '%s'", e->d_name, path);
1692 ret = 0;
1695 closedir(dir);
1696 return ret;
1699 static int clone_submodule(const struct module_clone_data *clone_data,
1700 struct string_list *reference)
1702 char *p;
1703 char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1704 char *sm_alternate = NULL, *error_strategy = NULL;
1705 struct stat st;
1706 struct child_process cp = CHILD_PROCESS_INIT;
1707 const char *clone_data_path = clone_data->path;
1708 char *to_free = NULL;
1710 if (validate_submodule_path(clone_data_path) < 0)
1711 exit(128);
1713 if (!is_absolute_path(clone_data->path))
1714 clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
1715 clone_data->path);
1717 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1718 die(_("refusing to create/use '%s' in another submodule's "
1719 "git dir"), sm_gitdir);
1721 if (!file_exists(sm_gitdir)) {
1722 if (clone_data->require_init && !stat(clone_data_path, &st) &&
1723 !is_empty_dir(clone_data_path))
1724 die(_("directory not empty: '%s'"), clone_data_path);
1726 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1727 die(_("could not create directory '%s'"), sm_gitdir);
1729 prepare_possible_alternates(clone_data->name, reference);
1731 strvec_push(&cp.args, "clone");
1732 strvec_push(&cp.args, "--no-checkout");
1733 if (clone_data->quiet)
1734 strvec_push(&cp.args, "--quiet");
1735 if (clone_data->progress)
1736 strvec_push(&cp.args, "--progress");
1737 if (clone_data->depth && *(clone_data->depth))
1738 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1739 if (reference->nr) {
1740 struct string_list_item *item;
1742 for_each_string_list_item(item, reference)
1743 strvec_pushl(&cp.args, "--reference",
1744 item->string, NULL);
1746 if (clone_data->dissociate)
1747 strvec_push(&cp.args, "--dissociate");
1748 if (sm_gitdir && *sm_gitdir)
1749 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1750 if (clone_data->filter_options && clone_data->filter_options->choice)
1751 strvec_pushf(&cp.args, "--filter=%s",
1752 expand_list_objects_filter_spec(
1753 clone_data->filter_options));
1754 if (clone_data->single_branch >= 0)
1755 strvec_push(&cp.args, clone_data->single_branch ?
1756 "--single-branch" :
1757 "--no-single-branch");
1759 strvec_push(&cp.args, "--");
1760 strvec_push(&cp.args, clone_data->url);
1761 strvec_push(&cp.args, clone_data_path);
1763 cp.git_cmd = 1;
1764 prepare_submodule_repo_env(&cp.env);
1765 cp.no_stdin = 1;
1767 if(run_command(&cp))
1768 die(_("clone of '%s' into submodule path '%s' failed"),
1769 clone_data->url, clone_data_path);
1771 if (clone_data->require_init && !stat(clone_data_path, &st) &&
1772 !dir_contains_only_dotgit(clone_data_path)) {
1773 char *dot_git = xstrfmt("%s/.git", clone_data_path);
1774 unlink(dot_git);
1775 free(dot_git);
1776 die(_("directory not empty: '%s'"), clone_data_path);
1778 } else {
1779 char *path;
1781 if (clone_data->require_init && !stat(clone_data_path, &st) &&
1782 !is_empty_dir(clone_data_path))
1783 die(_("directory not empty: '%s'"), clone_data_path);
1784 if (safe_create_leading_directories_const(clone_data_path) < 0)
1785 die(_("could not create directory '%s'"), clone_data_path);
1786 path = xstrfmt("%s/index", sm_gitdir);
1787 unlink_or_warn(path);
1788 free(path);
1792 * We already performed this check at the beginning of this function,
1793 * before cloning the objects. This tries to detect racy behavior e.g.
1794 * in parallel clones, where another process could easily have made the
1795 * gitdir nested _after_ it was created.
1797 * To prevent further harm coming from this unintentionally-nested
1798 * gitdir, let's disable it by deleting the `HEAD` file.
1800 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) {
1801 char *head = xstrfmt("%s/HEAD", sm_gitdir);
1802 unlink(head);
1803 free(head);
1804 die(_("refusing to create/use '%s' in another submodule's "
1805 "git dir"), sm_gitdir);
1808 connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1810 p = git_pathdup_submodule(clone_data_path, "config");
1811 if (!p)
1812 die(_("could not get submodule directory for '%s'"), clone_data_path);
1814 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1815 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1816 if (sm_alternate)
1817 git_config_set_in_file(p, "submodule.alternateLocation",
1818 sm_alternate);
1819 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1820 if (error_strategy)
1821 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1822 error_strategy);
1824 free(sm_alternate);
1825 free(error_strategy);
1827 free(sm_gitdir);
1828 free(p);
1829 free(to_free);
1830 return 0;
1833 static int module_clone(int argc, const char **argv, const char *prefix)
1835 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1836 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1837 struct string_list reference = STRING_LIST_INIT_NODUP;
1838 struct list_objects_filter_options filter_options =
1839 LIST_OBJECTS_FILTER_INIT;
1841 struct option module_clone_options[] = {
1842 OPT_STRING(0, "prefix", &clone_data.prefix,
1843 N_("path"),
1844 N_("alternative anchor for relative paths")),
1845 OPT_STRING(0, "path", &clone_data.path,
1846 N_("path"),
1847 N_("where the new submodule will be cloned to")),
1848 OPT_STRING(0, "name", &clone_data.name,
1849 N_("string"),
1850 N_("name of the new submodule")),
1851 OPT_STRING(0, "url", &clone_data.url,
1852 N_("string"),
1853 N_("url where to clone the submodule from")),
1854 OPT_STRING_LIST(0, "reference", &reference,
1855 N_("repo"),
1856 N_("reference repository")),
1857 OPT_BOOL(0, "dissociate", &dissociate,
1858 N_("use --reference only while cloning")),
1859 OPT_STRING(0, "depth", &clone_data.depth,
1860 N_("string"),
1861 N_("depth for shallow clones")),
1862 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1863 OPT_BOOL(0, "progress", &progress,
1864 N_("force cloning progress")),
1865 OPT_BOOL(0, "require-init", &require_init,
1866 N_("disallow cloning into non-empty directory")),
1867 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1868 N_("clone only one branch, HEAD or --branch")),
1869 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1870 OPT_END()
1872 const char *const git_submodule_helper_usage[] = {
1873 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1874 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1875 "[--single-branch] [--filter <filter-spec>] "
1876 "--url <url> --path <path>"),
1877 NULL
1880 argc = parse_options(argc, argv, prefix, module_clone_options,
1881 git_submodule_helper_usage, 0);
1883 clone_data.dissociate = !!dissociate;
1884 clone_data.quiet = !!quiet;
1885 clone_data.progress = !!progress;
1886 clone_data.require_init = !!require_init;
1887 clone_data.filter_options = &filter_options;
1889 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1890 usage_with_options(git_submodule_helper_usage,
1891 module_clone_options);
1893 clone_submodule(&clone_data, &reference);
1894 list_objects_filter_release(&filter_options);
1895 string_list_clear(&reference, 1);
1896 return 0;
1899 static int determine_submodule_update_strategy(struct repository *r,
1900 int just_cloned,
1901 const char *path,
1902 enum submodule_update_type update,
1903 struct submodule_update_strategy *out)
1905 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1906 char *key;
1907 const char *val;
1908 int ret;
1910 key = xstrfmt("submodule.%s.update", sub->name);
1912 if (update) {
1913 out->type = update;
1914 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1915 if (parse_submodule_update_strategy(val, out) < 0) {
1916 ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1917 val, path);
1918 goto cleanup;
1920 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1921 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1922 BUG("how did we read update = !command from .gitmodules?");
1923 out->type = sub->update_strategy.type;
1924 out->command = sub->update_strategy.command;
1925 } else
1926 out->type = SM_UPDATE_CHECKOUT;
1928 if (just_cloned &&
1929 (out->type == SM_UPDATE_MERGE ||
1930 out->type == SM_UPDATE_REBASE ||
1931 out->type == SM_UPDATE_NONE))
1932 out->type = SM_UPDATE_CHECKOUT;
1934 ret = 0;
1935 cleanup:
1936 free(key);
1937 return ret;
1940 struct update_clone_data {
1941 const struct submodule *sub;
1942 struct object_id oid;
1943 unsigned just_cloned;
1946 struct submodule_update_clone {
1947 /* index into 'update_data.list', the list of submodules to look into for cloning */
1948 int current;
1950 /* configuration parameters which are passed on to the children */
1951 const struct update_data *update_data;
1953 /* to be consumed by update_submodule() */
1954 struct update_clone_data *update_clone;
1955 int update_clone_nr; int update_clone_alloc;
1957 /* If we want to stop as fast as possible and return an error */
1958 unsigned quickstop : 1;
1960 /* failed clones to be retried again */
1961 const struct cache_entry **failed_clones;
1962 int failed_clones_nr, failed_clones_alloc;
1964 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1966 static void submodule_update_clone_release(struct submodule_update_clone *suc)
1968 free(suc->update_clone);
1969 free(suc->failed_clones);
1972 struct update_data {
1973 const char *prefix;
1974 const char *super_prefix;
1975 char *displaypath;
1976 enum submodule_update_type update_default;
1977 struct object_id suboid;
1978 struct string_list references;
1979 struct submodule_update_strategy update_strategy;
1980 struct list_objects_filter_options *filter_options;
1981 struct module_list list;
1982 int depth;
1983 int max_jobs;
1984 int single_branch;
1985 int recommend_shallow;
1986 unsigned int require_init;
1987 unsigned int force;
1988 unsigned int quiet;
1989 unsigned int nofetch;
1990 unsigned int remote;
1991 unsigned int progress;
1992 unsigned int dissociate;
1993 unsigned int init;
1994 unsigned int warn_if_uninitialized;
1995 unsigned int recursive;
1997 /* copied over from update_clone_data */
1998 struct object_id oid;
1999 unsigned int just_cloned;
2000 const char *sm_path;
2002 #define UPDATE_DATA_INIT { \
2003 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2004 .list = MODULE_LIST_INIT, \
2005 .recommend_shallow = -1, \
2006 .references = STRING_LIST_INIT_DUP, \
2007 .single_branch = -1, \
2008 .max_jobs = 1, \
2011 static void update_data_release(struct update_data *ud)
2013 free(ud->displaypath);
2014 module_list_release(&ud->list);
2017 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
2018 struct strbuf *out, const char *displaypath)
2021 * Only mention uninitialized submodules when their
2022 * paths have been specified.
2024 if (suc->update_data->warn_if_uninitialized) {
2025 strbuf_addf(out,
2026 _("Submodule path '%s' not initialized"),
2027 displaypath);
2028 strbuf_addch(out, '\n');
2029 strbuf_addstr(out,
2030 _("Maybe you want to use 'update --init'?"));
2031 strbuf_addch(out, '\n');
2036 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2037 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2039 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
2040 struct child_process *child,
2041 struct submodule_update_clone *suc,
2042 struct strbuf *out)
2044 const struct submodule *sub = NULL;
2045 const char *url = NULL;
2046 const char *update_string;
2047 enum submodule_update_type update_type;
2048 char *key;
2049 const struct update_data *ud = suc->update_data;
2050 char *displaypath = get_submodule_displaypath(ce->name, ud->prefix,
2051 ud->super_prefix);
2052 struct strbuf sb = STRBUF_INIT;
2053 int needs_cloning = 0;
2054 int need_free_url = 0;
2056 if (ce_stage(ce)) {
2057 strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
2058 strbuf_addch(out, '\n');
2059 goto cleanup;
2062 sub = submodule_from_path(the_repository, null_oid(), ce->name);
2064 if (!sub) {
2065 next_submodule_warn_missing(suc, out, displaypath);
2066 goto cleanup;
2069 key = xstrfmt("submodule.%s.update", sub->name);
2070 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
2071 update_type = parse_submodule_update_type(update_string);
2072 } else {
2073 update_type = sub->update_strategy.type;
2075 free(key);
2077 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
2078 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
2079 && update_type == SM_UPDATE_NONE)) {
2080 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
2081 strbuf_addch(out, '\n');
2082 goto cleanup;
2085 /* Check if the submodule has been initialized. */
2086 if (!is_submodule_active(the_repository, ce->name)) {
2087 next_submodule_warn_missing(suc, out, displaypath);
2088 goto cleanup;
2091 strbuf_reset(&sb);
2092 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2093 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2094 if (sub->url && (starts_with_dot_slash(sub->url) ||
2095 starts_with_dot_dot_slash(sub->url))) {
2096 url = resolve_relative_url(sub->url, NULL, 0);
2097 need_free_url = 1;
2098 } else
2099 url = sub->url;
2102 if (!url)
2103 die(_("cannot clone submodule '%s' without a URL"), sub->name);
2105 strbuf_reset(&sb);
2106 strbuf_addf(&sb, "%s/.git", ce->name);
2107 needs_cloning = !file_exists(sb.buf);
2109 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2110 suc->update_clone_alloc);
2111 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2112 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2113 suc->update_clone[suc->update_clone_nr].sub = sub;
2114 suc->update_clone_nr++;
2116 if (!needs_cloning)
2117 goto cleanup;
2119 child->git_cmd = 1;
2120 child->no_stdin = 1;
2121 child->stdout_to_stderr = 1;
2122 child->err = -1;
2123 strvec_push(&child->args, "submodule--helper");
2124 strvec_push(&child->args, "clone");
2125 if (suc->update_data->progress)
2126 strvec_push(&child->args, "--progress");
2127 if (suc->update_data->quiet)
2128 strvec_push(&child->args, "--quiet");
2129 if (suc->update_data->prefix)
2130 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2131 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2132 strvec_push(&child->args, "--depth=1");
2133 else if (suc->update_data->depth)
2134 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2135 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2136 strvec_pushf(&child->args, "--filter=%s",
2137 expand_list_objects_filter_spec(suc->update_data->filter_options));
2138 if (suc->update_data->require_init)
2139 strvec_push(&child->args, "--require-init");
2140 strvec_pushl(&child->args, "--path", sub->path, NULL);
2141 strvec_pushl(&child->args, "--name", sub->name, NULL);
2142 strvec_pushl(&child->args, "--url", url, NULL);
2143 if (suc->update_data->references.nr) {
2144 struct string_list_item *item;
2146 for_each_string_list_item(item, &suc->update_data->references)
2147 strvec_pushl(&child->args, "--reference", item->string, NULL);
2149 if (suc->update_data->dissociate)
2150 strvec_push(&child->args, "--dissociate");
2151 if (suc->update_data->single_branch >= 0)
2152 strvec_push(&child->args, suc->update_data->single_branch ?
2153 "--single-branch" :
2154 "--no-single-branch");
2156 cleanup:
2157 free(displaypath);
2158 strbuf_release(&sb);
2159 if (need_free_url)
2160 free((void*)url);
2162 return needs_cloning;
2165 static int update_clone_get_next_task(struct child_process *child,
2166 struct strbuf *err,
2167 void *suc_cb,
2168 void **idx_task_cb)
2170 struct submodule_update_clone *suc = suc_cb;
2171 const struct cache_entry *ce;
2172 int index;
2174 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2175 ce = suc->update_data->list.entries[suc->current];
2176 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2177 int *p = xmalloc(sizeof(*p));
2179 *p = suc->current;
2180 *idx_task_cb = p;
2181 suc->current++;
2182 return 1;
2187 * The loop above tried cloning each submodule once, now try the
2188 * stragglers again, which we can imagine as an extension of the
2189 * entry list.
2191 index = suc->current - suc->update_data->list.nr;
2192 if (index < suc->failed_clones_nr) {
2193 int *p;
2195 ce = suc->failed_clones[index];
2196 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2197 suc->current ++;
2198 strbuf_addstr(err, "BUG: submodule considered for "
2199 "cloning, doesn't need cloning "
2200 "any more?\n");
2201 return 0;
2203 p = xmalloc(sizeof(*p));
2204 *p = suc->current;
2205 *idx_task_cb = p;
2206 suc->current ++;
2207 return 1;
2210 return 0;
2213 static int update_clone_start_failure(struct strbuf *err UNUSED,
2214 void *suc_cb,
2215 void *idx_task_cb UNUSED)
2217 struct submodule_update_clone *suc = suc_cb;
2219 suc->quickstop = 1;
2220 return 1;
2223 static int update_clone_task_finished(int result,
2224 struct strbuf *err,
2225 void *suc_cb,
2226 void *idx_task_cb)
2228 const struct cache_entry *ce;
2229 struct submodule_update_clone *suc = suc_cb;
2230 int *idxP = idx_task_cb;
2231 int idx = *idxP;
2233 free(idxP);
2235 if (!result)
2236 return 0;
2238 if (idx < suc->update_data->list.nr) {
2239 ce = suc->update_data->list.entries[idx];
2240 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2241 ce->name);
2242 strbuf_addch(err, '\n');
2243 ALLOC_GROW(suc->failed_clones,
2244 suc->failed_clones_nr + 1,
2245 suc->failed_clones_alloc);
2246 suc->failed_clones[suc->failed_clones_nr++] = ce;
2247 return 0;
2248 } else {
2249 idx -= suc->update_data->list.nr;
2250 ce = suc->failed_clones[idx];
2251 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2252 ce->name);
2253 strbuf_addch(err, '\n');
2254 suc->quickstop = 1;
2255 return 1;
2258 return 0;
2261 static int git_update_clone_config(const char *var, const char *value,
2262 const struct config_context *ctx,
2263 void *cb)
2265 int *max_jobs = cb;
2267 if (!strcmp(var, "submodule.fetchjobs"))
2268 *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
2269 return 0;
2272 static int is_tip_reachable(const char *path, const struct object_id *oid)
2274 struct child_process cp = CHILD_PROCESS_INIT;
2275 struct strbuf rev = STRBUF_INIT;
2276 char *hex = oid_to_hex(oid);
2278 cp.git_cmd = 1;
2279 cp.dir = path;
2280 cp.no_stderr = 1;
2281 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2283 prepare_submodule_repo_env(&cp.env);
2285 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2286 return 0;
2288 return 1;
2291 static int fetch_in_submodule(const char *module_path, int depth, int quiet,
2292 const struct object_id *oid)
2294 struct child_process cp = CHILD_PROCESS_INIT;
2296 prepare_submodule_repo_env(&cp.env);
2297 cp.git_cmd = 1;
2298 cp.dir = module_path;
2300 strvec_push(&cp.args, "fetch");
2301 if (quiet)
2302 strvec_push(&cp.args, "--quiet");
2303 if (depth)
2304 strvec_pushf(&cp.args, "--depth=%d", depth);
2305 if (oid) {
2306 char *hex = oid_to_hex(oid);
2307 char *remote = get_default_remote();
2309 strvec_pushl(&cp.args, remote, hex, NULL);
2310 free(remote);
2313 return run_command(&cp);
2316 static int run_update_command(const struct update_data *ud, int subforce)
2318 struct child_process cp = CHILD_PROCESS_INIT;
2319 char *oid = oid_to_hex(&ud->oid);
2320 int ret;
2322 switch (ud->update_strategy.type) {
2323 case SM_UPDATE_CHECKOUT:
2324 cp.git_cmd = 1;
2325 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2326 if (subforce)
2327 strvec_push(&cp.args, "-f");
2328 break;
2329 case SM_UPDATE_REBASE:
2330 cp.git_cmd = 1;
2331 strvec_push(&cp.args, "rebase");
2332 if (ud->quiet)
2333 strvec_push(&cp.args, "--quiet");
2334 break;
2335 case SM_UPDATE_MERGE:
2336 cp.git_cmd = 1;
2337 strvec_push(&cp.args, "merge");
2338 if (ud->quiet)
2339 strvec_push(&cp.args, "--quiet");
2340 break;
2341 case SM_UPDATE_COMMAND:
2342 cp.use_shell = 1;
2343 strvec_push(&cp.args, ud->update_strategy.command);
2344 break;
2345 default:
2346 BUG("unexpected update strategy type: %d",
2347 ud->update_strategy.type);
2349 strvec_push(&cp.args, oid);
2351 cp.dir = ud->sm_path;
2352 prepare_submodule_repo_env(&cp.env);
2353 if ((ret = run_command(&cp))) {
2354 switch (ud->update_strategy.type) {
2355 case SM_UPDATE_CHECKOUT:
2356 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2357 oid, ud->displaypath);
2358 /* No "ret" assignment, use "git checkout"'s */
2359 break;
2360 case SM_UPDATE_REBASE:
2361 ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2362 oid, ud->displaypath);
2363 break;
2364 case SM_UPDATE_MERGE:
2365 ret = die_message(_("Unable to merge '%s' in submodule path '%s'"),
2366 oid, ud->displaypath);
2367 break;
2368 case SM_UPDATE_COMMAND:
2369 ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2370 ud->update_strategy.command, oid, ud->displaypath);
2371 break;
2372 default:
2373 BUG("unexpected update strategy type: %d",
2374 ud->update_strategy.type);
2377 return ret;
2380 if (ud->quiet)
2381 return 0;
2383 switch (ud->update_strategy.type) {
2384 case SM_UPDATE_CHECKOUT:
2385 printf(_("Submodule path '%s': checked out '%s'\n"),
2386 ud->displaypath, oid);
2387 break;
2388 case SM_UPDATE_REBASE:
2389 printf(_("Submodule path '%s': rebased into '%s'\n"),
2390 ud->displaypath, oid);
2391 break;
2392 case SM_UPDATE_MERGE:
2393 printf(_("Submodule path '%s': merged in '%s'\n"),
2394 ud->displaypath, oid);
2395 break;
2396 case SM_UPDATE_COMMAND:
2397 printf(_("Submodule path '%s': '%s %s'\n"),
2398 ud->displaypath, ud->update_strategy.command, oid);
2399 break;
2400 default:
2401 BUG("unexpected update strategy type: %d",
2402 ud->update_strategy.type);
2405 return 0;
2408 static int run_update_procedure(const struct update_data *ud)
2410 int subforce = is_null_oid(&ud->suboid) || ud->force;
2412 if (!ud->nofetch) {
2414 * Run fetch only if `oid` isn't present or it
2415 * is not reachable from a ref.
2417 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2418 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2419 !ud->quiet)
2420 fprintf_ln(stderr,
2421 _("Unable to fetch in submodule path '%s'; "
2422 "trying to directly fetch %s:"),
2423 ud->displaypath, oid_to_hex(&ud->oid));
2425 * Now we tried the usual fetch, but `oid` may
2426 * not be reachable from any of the refs.
2428 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2429 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2430 return die_message(_("Fetched in submodule path '%s', but it did not "
2431 "contain %s. Direct fetching of that commit failed."),
2432 ud->displaypath, oid_to_hex(&ud->oid));
2435 return run_update_command(ud, subforce);
2438 static int remote_submodule_branch(const char *path, const char **branch)
2440 const struct submodule *sub;
2441 char *key;
2442 *branch = NULL;
2444 sub = submodule_from_path(the_repository, null_oid(), path);
2445 if (!sub)
2446 return die_message(_("could not initialize submodule at path '%s'"),
2447 path);
2449 key = xstrfmt("submodule.%s.branch", sub->name);
2450 if (repo_config_get_string_tmp(the_repository, key, branch))
2451 *branch = sub->branch;
2452 free(key);
2454 if (!*branch) {
2455 *branch = "HEAD";
2456 return 0;
2459 if (!strcmp(*branch, ".")) {
2460 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2462 if (!refname)
2463 return die_message(_("No such ref: %s"), "HEAD");
2465 /* detached HEAD */
2466 if (!strcmp(refname, "HEAD"))
2467 return die_message(_("Submodule (%s) branch configured to inherit "
2468 "branch from superproject, but the superproject "
2469 "is not on any branch"), sub->name);
2471 if (!skip_prefix(refname, "refs/heads/", &refname))
2472 return die_message(_("Expecting a full ref name, got %s"),
2473 refname);
2475 *branch = refname;
2476 return 0;
2479 /* Our "branch" is coming from repo_config_get_string_tmp() */
2480 return 0;
2483 static int ensure_core_worktree(const char *path)
2485 const char *cw;
2486 struct repository subrepo;
2488 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2489 return die_message(_("could not get a repository handle for submodule '%s'"),
2490 path);
2492 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2493 char *cfg_file, *abs_path;
2494 const char *rel_path;
2495 struct strbuf sb = STRBUF_INIT;
2497 cfg_file = repo_git_path(&subrepo, "config");
2499 abs_path = absolute_pathdup(path);
2500 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2502 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2504 free(cfg_file);
2505 free(abs_path);
2506 strbuf_release(&sb);
2509 repo_clear(&subrepo);
2510 return 0;
2513 static const char *submodule_update_type_to_label(enum submodule_update_type type)
2515 switch (type) {
2516 case SM_UPDATE_CHECKOUT:
2517 return "checkout";
2518 case SM_UPDATE_MERGE:
2519 return "merge";
2520 case SM_UPDATE_REBASE:
2521 return "rebase";
2522 case SM_UPDATE_UNSPECIFIED:
2523 case SM_UPDATE_NONE:
2524 case SM_UPDATE_COMMAND:
2525 break;
2527 BUG("unreachable with type %d", type);
2530 static void update_data_to_args(const struct update_data *update_data,
2531 struct strvec *args)
2533 enum submodule_update_type update_type = update_data->update_default;
2535 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2536 if (update_data->displaypath) {
2537 strvec_push(args, "--super-prefix");
2538 strvec_pushf(args, "%s/", update_data->displaypath);
2540 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2541 if (update_data->quiet)
2542 strvec_push(args, "--quiet");
2543 if (update_data->force)
2544 strvec_push(args, "--force");
2545 if (update_data->init)
2546 strvec_push(args, "--init");
2547 if (update_data->remote)
2548 strvec_push(args, "--remote");
2549 if (update_data->nofetch)
2550 strvec_push(args, "--no-fetch");
2551 if (update_data->dissociate)
2552 strvec_push(args, "--dissociate");
2553 if (update_data->progress)
2554 strvec_push(args, "--progress");
2555 if (update_data->require_init)
2556 strvec_push(args, "--require-init");
2557 if (update_data->depth)
2558 strvec_pushf(args, "--depth=%d", update_data->depth);
2559 if (update_type != SM_UPDATE_UNSPECIFIED)
2560 strvec_pushf(args, "--%s",
2561 submodule_update_type_to_label(update_type));
2563 if (update_data->references.nr) {
2564 struct string_list_item *item;
2566 for_each_string_list_item(item, &update_data->references)
2567 strvec_pushl(args, "--reference", item->string, NULL);
2569 if (update_data->filter_options && update_data->filter_options->choice)
2570 strvec_pushf(args, "--filter=%s",
2571 expand_list_objects_filter_spec(
2572 update_data->filter_options));
2573 if (update_data->recommend_shallow == 0)
2574 strvec_push(args, "--no-recommend-shallow");
2575 else if (update_data->recommend_shallow == 1)
2576 strvec_push(args, "--recommend-shallow");
2577 if (update_data->single_branch >= 0)
2578 strvec_push(args, update_data->single_branch ?
2579 "--single-branch" :
2580 "--no-single-branch");
2583 static int update_submodule(struct update_data *update_data)
2585 int ret;
2587 if (validate_submodule_path(update_data->sm_path) < 0)
2588 return -1;
2590 ret = determine_submodule_update_strategy(the_repository,
2591 update_data->just_cloned,
2592 update_data->sm_path,
2593 update_data->update_default,
2594 &update_data->update_strategy);
2595 if (ret)
2596 return ret;
2598 if (update_data->just_cloned)
2599 oidcpy(&update_data->suboid, null_oid());
2600 else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2601 return die_message(_("Unable to find current revision in submodule path '%s'"),
2602 update_data->displaypath);
2604 if (update_data->remote) {
2605 char *remote_name;
2606 const char *branch;
2607 char *remote_ref;
2608 int code;
2610 code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2611 if (code)
2612 return code;
2613 code = remote_submodule_branch(update_data->sm_path, &branch);
2614 if (code)
2615 return code;
2616 remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2618 free(remote_name);
2620 if (!update_data->nofetch) {
2621 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2622 0, NULL))
2623 return die_message(_("Unable to fetch in submodule path '%s'"),
2624 update_data->sm_path);
2627 if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2628 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2629 remote_ref, update_data->sm_path);
2631 free(remote_ref);
2634 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2635 ret = run_update_procedure(update_data);
2636 if (ret)
2637 return ret;
2640 if (update_data->recursive) {
2641 struct child_process cp = CHILD_PROCESS_INIT;
2642 struct update_data next = *update_data;
2644 next.prefix = NULL;
2645 oidcpy(&next.oid, null_oid());
2646 oidcpy(&next.suboid, null_oid());
2648 cp.dir = update_data->sm_path;
2649 cp.git_cmd = 1;
2650 prepare_submodule_repo_env(&cp.env);
2651 update_data_to_args(&next, &cp.args);
2653 ret = run_command(&cp);
2654 if (ret)
2655 die_message(_("Failed to recurse into submodule path '%s'"),
2656 update_data->displaypath);
2657 return ret;
2660 return 0;
2663 static int update_submodules(struct update_data *update_data)
2665 int i, ret = 0;
2666 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2667 const struct run_process_parallel_opts opts = {
2668 .tr2_category = "submodule",
2669 .tr2_label = "parallel/update",
2671 .processes = update_data->max_jobs,
2673 .get_next_task = update_clone_get_next_task,
2674 .start_failure = update_clone_start_failure,
2675 .task_finished = update_clone_task_finished,
2676 .data = &suc,
2679 suc.update_data = update_data;
2680 run_processes_parallel(&opts);
2683 * We saved the output and put it out all at once now.
2684 * That means:
2685 * - the listener does not have to interleave their (checkout)
2686 * work with our fetching. The writes involved in a
2687 * checkout involve more straightforward sequential I/O.
2688 * - the listener can avoid doing any work if fetching failed.
2690 if (suc.quickstop) {
2691 ret = 1;
2692 goto cleanup;
2695 for (i = 0; i < suc.update_clone_nr; i++) {
2696 struct update_clone_data ucd = suc.update_clone[i];
2697 int code = 128;
2699 oidcpy(&update_data->oid, &ucd.oid);
2700 update_data->just_cloned = ucd.just_cloned;
2701 update_data->sm_path = ucd.sub->path;
2704 * Verify that the submodule path does not contain any
2705 * symlinks; if it does, it might have been tampered with.
2706 * TODO: allow exempting it via
2707 * `safe.submodule.path` or something
2709 if (validate_submodule_path(update_data->sm_path) < 0)
2710 goto fail;
2712 code = ensure_core_worktree(update_data->sm_path);
2713 if (code)
2714 goto fail;
2716 update_data->displaypath = get_submodule_displaypath(
2717 update_data->sm_path, update_data->prefix,
2718 update_data->super_prefix);
2719 code = update_submodule(update_data);
2720 FREE_AND_NULL(update_data->displaypath);
2721 fail:
2722 if (!code)
2723 continue;
2724 ret = code;
2725 if (ret == 128)
2726 goto cleanup;
2729 cleanup:
2730 submodule_update_clone_release(&suc);
2731 string_list_clear(&update_data->references, 0);
2732 return ret;
2735 static int module_update(int argc, const char **argv, const char *prefix)
2737 struct pathspec pathspec = { 0 };
2738 struct pathspec pathspec2 = { 0 };
2739 struct update_data opt = UPDATE_DATA_INIT;
2740 struct list_objects_filter_options filter_options =
2741 LIST_OBJECTS_FILTER_INIT;
2742 int ret;
2743 struct option module_update_options[] = {
2744 OPT__SUPER_PREFIX(&opt.super_prefix),
2745 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2746 OPT_BOOL(0, "init", &opt.init,
2747 N_("initialize uninitialized submodules before update")),
2748 OPT_BOOL(0, "remote", &opt.remote,
2749 N_("use SHA-1 of submodule's remote tracking branch")),
2750 OPT_BOOL(0, "recursive", &opt.recursive,
2751 N_("traverse submodules recursively")),
2752 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2753 N_("don't fetch new objects from the remote site")),
2754 OPT_SET_INT(0, "checkout", &opt.update_default,
2755 N_("use the 'checkout' update strategy (default)"),
2756 SM_UPDATE_CHECKOUT),
2757 OPT_SET_INT('m', "merge", &opt.update_default,
2758 N_("use the 'merge' update strategy"),
2759 SM_UPDATE_MERGE),
2760 OPT_SET_INT('r', "rebase", &opt.update_default,
2761 N_("use the 'rebase' update strategy"),
2762 SM_UPDATE_REBASE),
2763 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2764 N_("reference repository")),
2765 OPT_BOOL(0, "dissociate", &opt.dissociate,
2766 N_("use --reference only while cloning")),
2767 OPT_INTEGER(0, "depth", &opt.depth,
2768 N_("create a shallow clone truncated to the "
2769 "specified number of revisions")),
2770 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2771 N_("parallel jobs")),
2772 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2773 N_("whether the initial clone should follow the shallow recommendation")),
2774 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2775 OPT_BOOL(0, "progress", &opt.progress,
2776 N_("force cloning progress")),
2777 OPT_BOOL(0, "require-init", &opt.require_init,
2778 N_("disallow cloning into non-empty directory, implies --init")),
2779 OPT_BOOL(0, "single-branch", &opt.single_branch,
2780 N_("clone only one branch, HEAD or --branch")),
2781 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2782 OPT_END()
2784 const char *const git_submodule_helper_usage[] = {
2785 N_("git submodule [--quiet] update"
2786 " [--init [--filter=<filter-spec>]] [--remote]"
2787 " [-N|--no-fetch] [-f|--force]"
2788 " [--checkout|--merge|--rebase]"
2789 " [--[no-]recommend-shallow] [--reference <repository>]"
2790 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2791 NULL
2794 update_clone_config_from_gitmodules(&opt.max_jobs);
2795 git_config(git_update_clone_config, &opt.max_jobs);
2797 argc = parse_options(argc, argv, prefix, module_update_options,
2798 git_submodule_helper_usage, 0);
2800 if (opt.require_init)
2801 opt.init = 1;
2803 if (filter_options.choice && !opt.init) {
2804 usage_with_options(git_submodule_helper_usage,
2805 module_update_options);
2808 opt.filter_options = &filter_options;
2809 opt.prefix = prefix;
2811 if (opt.update_default)
2812 opt.update_strategy.type = opt.update_default;
2814 if (module_list_compute(argv, prefix, &pathspec, &opt.list) < 0) {
2815 ret = 1;
2816 goto cleanup;
2819 if (pathspec.nr)
2820 opt.warn_if_uninitialized = 1;
2822 if (opt.init) {
2823 struct module_list list = MODULE_LIST_INIT;
2824 struct init_cb info = INIT_CB_INIT;
2826 if (module_list_compute(argv, opt.prefix,
2827 &pathspec2, &list) < 0) {
2828 module_list_release(&list);
2829 ret = 1;
2830 goto cleanup;
2834 * If there are no path args and submodule.active is set then,
2835 * by default, only initialize 'active' modules.
2837 if (!argc && !git_config_get("submodule.active"))
2838 module_list_active(&list);
2840 info.prefix = opt.prefix;
2841 info.super_prefix = opt.super_prefix;
2842 if (opt.quiet)
2843 info.flags |= OPT_QUIET;
2845 for_each_listed_submodule(&list, init_submodule_cb, &info);
2846 module_list_release(&list);
2849 ret = update_submodules(&opt);
2850 cleanup:
2851 update_data_release(&opt);
2852 list_objects_filter_release(&filter_options);
2853 clear_pathspec(&pathspec);
2854 clear_pathspec(&pathspec2);
2855 return ret;
2858 static int push_check(int argc, const char **argv, const char *prefix UNUSED)
2860 struct remote *remote;
2861 const char *superproject_head;
2862 char *head;
2863 int detached_head = 0;
2864 struct object_id head_oid;
2866 if (argc < 3)
2867 die("submodule--helper push-check requires at least 2 arguments");
2870 * superproject's resolved head ref.
2871 * if HEAD then the superproject is in a detached head state, otherwise
2872 * it will be the resolved head ref.
2874 superproject_head = argv[1];
2875 argv++;
2876 argc--;
2877 /* Get the submodule's head ref and determine if it is detached */
2878 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2879 if (!head)
2880 die(_("Failed to resolve HEAD as a valid ref."));
2881 if (!strcmp(head, "HEAD"))
2882 detached_head = 1;
2885 * The remote must be configured.
2886 * This is to avoid pushing to the exact same URL as the parent.
2888 remote = pushremote_get(argv[1]);
2889 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2890 die("remote '%s' not configured", argv[1]);
2892 /* Check the refspec */
2893 if (argc > 2) {
2894 int i;
2895 struct ref *local_refs = get_local_heads();
2896 struct refspec refspec = REFSPEC_INIT_PUSH;
2898 refspec_appendn(&refspec, argv + 2, argc - 2);
2900 for (i = 0; i < refspec.nr; i++) {
2901 const struct refspec_item *rs = &refspec.items[i];
2903 if (rs->pattern || rs->matching)
2904 continue;
2906 /* LHS must match a single ref */
2907 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2908 case 1:
2909 break;
2910 case 0:
2912 * If LHS matches 'HEAD' then we need to ensure
2913 * that it matches the same named branch
2914 * checked out in the superproject.
2916 if (!strcmp(rs->src, "HEAD")) {
2917 if (!detached_head &&
2918 !strcmp(head, superproject_head))
2919 break;
2920 die("HEAD does not match the named branch in the superproject");
2922 /* fallthrough */
2923 default:
2924 die("src refspec '%s' must name a ref",
2925 rs->src);
2928 refspec_clear(&refspec);
2930 free(head);
2932 return 0;
2935 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2937 int i;
2938 struct pathspec pathspec = { 0 };
2939 struct module_list list = MODULE_LIST_INIT;
2940 const char *super_prefix = NULL;
2941 struct option embed_gitdir_options[] = {
2942 OPT__SUPER_PREFIX(&super_prefix),
2943 OPT_END()
2945 const char *const git_submodule_helper_usage[] = {
2946 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2947 NULL
2949 int ret = 1;
2951 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2952 git_submodule_helper_usage, 0);
2954 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
2955 goto cleanup;
2957 for (i = 0; i < list.nr; i++)
2958 absorb_git_dir_into_superproject(list.entries[i]->name,
2959 super_prefix);
2961 ret = 0;
2962 cleanup:
2963 clear_pathspec(&pathspec);
2964 module_list_release(&list);
2965 return ret;
2968 static int module_set_url(int argc, const char **argv, const char *prefix)
2970 int quiet = 0, ret;
2971 const char *newurl;
2972 const char *path;
2973 char *config_name;
2974 struct option options[] = {
2975 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2976 OPT_END()
2978 const char *const usage[] = {
2979 N_("git submodule set-url [--quiet] <path> <newurl>"),
2980 NULL
2982 const struct submodule *sub;
2984 argc = parse_options(argc, argv, prefix, options, usage, 0);
2986 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2987 usage_with_options(usage, options);
2989 sub = submodule_from_path(the_repository, null_oid(), path);
2991 if (!sub)
2992 die(_("no submodule mapping found in .gitmodules for path '%s'"),
2993 path);
2995 config_name = xstrfmt("submodule.%s.url", sub->name);
2996 ret = config_set_in_gitmodules_file_gently(config_name, newurl);
2998 if (!ret) {
2999 repo_read_gitmodules(the_repository, 0);
3000 sync_submodule(sub->path, prefix, NULL, quiet ? OPT_QUIET : 0);
3003 free(config_name);
3004 return !!ret;
3007 static int module_set_branch(int argc, const char **argv, const char *prefix)
3009 int opt_default = 0, ret;
3010 const char *opt_branch = NULL;
3011 const char *path;
3012 char *config_name;
3013 struct option options[] = {
3015 * We accept the `quiet` option for uniformity across subcommands,
3016 * though there is nothing to make less verbose in this subcommand.
3018 OPT_NOOP_NOARG('q', "quiet"),
3020 OPT_BOOL('d', "default", &opt_default,
3021 N_("set the default tracking branch to master")),
3022 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
3023 N_("set the default tracking branch")),
3024 OPT_END()
3026 const char *const usage[] = {
3027 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3028 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3029 NULL
3031 const struct submodule *sub;
3033 argc = parse_options(argc, argv, prefix, options, usage, 0);
3035 if (!opt_branch && !opt_default)
3036 die(_("--branch or --default required"));
3038 if (opt_branch && opt_default)
3039 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3041 if (argc != 1 || !(path = argv[0]))
3042 usage_with_options(usage, options);
3044 sub = submodule_from_path(the_repository, null_oid(), path);
3046 if (!sub)
3047 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3048 path);
3050 config_name = xstrfmt("submodule.%s.branch", sub->name);
3051 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
3053 free(config_name);
3054 return !!ret;
3057 static int module_create_branch(int argc, const char **argv, const char *prefix)
3059 enum branch_track track;
3060 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
3061 struct option options[] = {
3062 OPT__QUIET(&quiet, N_("print only error messages")),
3063 OPT__FORCE(&force, N_("force creation"), 0),
3064 OPT_BOOL(0, "create-reflog", &reflog,
3065 N_("create the branch's reflog")),
3066 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
3067 N_("set branch tracking configuration"),
3068 PARSE_OPT_OPTARG,
3069 parse_opt_tracking_mode),
3070 OPT__DRY_RUN(&dry_run,
3071 N_("show whether the branch would be created")),
3072 OPT_END()
3074 const char *const usage[] = {
3075 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3076 NULL
3079 git_config(git_default_config, NULL);
3080 track = git_branch_track;
3081 argc = parse_options(argc, argv, prefix, options, usage, 0);
3083 if (argc != 3)
3084 usage_with_options(usage, options);
3086 if (!quiet && !dry_run)
3087 printf_ln(_("creating branch '%s'"), argv[0]);
3089 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
3090 force, reflog, quiet, track, dry_run);
3091 return 0;
3094 struct add_data {
3095 const char *prefix;
3096 const char *branch;
3097 const char *reference_path;
3098 char *sm_path;
3099 const char *sm_name;
3100 const char *repo;
3101 const char *realrepo;
3102 int depth;
3103 unsigned int force: 1;
3104 unsigned int quiet: 1;
3105 unsigned int progress: 1;
3106 unsigned int dissociate: 1;
3108 #define ADD_DATA_INIT { .depth = -1 }
3110 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3112 struct child_process cp_remote = CHILD_PROCESS_INIT;
3113 struct strbuf sb_remote_out = STRBUF_INIT;
3115 cp_remote.git_cmd = 1;
3116 strvec_pushf(&cp_remote.env,
3117 "GIT_DIR=%s", git_dir_path);
3118 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3119 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3120 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3121 char *next_line;
3122 char *line = sb_remote_out.buf;
3124 while ((next_line = strchr(line, '\n')) != NULL) {
3125 size_t len = next_line - line;
3127 if (strip_suffix_mem(line, &len, " (fetch)"))
3128 strbuf_addf(msg, " %.*s\n", (int)len, line);
3129 line = next_line + 1;
3133 strbuf_release(&sb_remote_out);
3136 static int add_submodule(const struct add_data *add_data)
3138 char *submod_gitdir_path;
3139 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3140 struct string_list reference = STRING_LIST_INIT_NODUP;
3141 int ret = -1;
3143 /* perhaps the path already exists and is already a git repo, else clone it */
3144 if (is_directory(add_data->sm_path)) {
3145 struct strbuf sm_path = STRBUF_INIT;
3146 strbuf_addstr(&sm_path, add_data->sm_path);
3147 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3148 if (is_nonbare_repository_dir(&sm_path))
3149 printf(_("Adding existing repo at '%s' to the index\n"),
3150 add_data->sm_path);
3151 else
3152 die(_("'%s' already exists and is not a valid git repo"),
3153 add_data->sm_path);
3154 strbuf_release(&sm_path);
3155 free(submod_gitdir_path);
3156 } else {
3157 struct child_process cp = CHILD_PROCESS_INIT;
3159 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3161 if (is_directory(submod_gitdir_path)) {
3162 if (!add_data->force) {
3163 struct strbuf msg = STRBUF_INIT;
3164 char *die_msg;
3166 strbuf_addf(&msg, _("A git directory for '%s' is found "
3167 "locally with remote(s):\n"),
3168 add_data->sm_name);
3170 append_fetch_remotes(&msg, submod_gitdir_path);
3171 free(submod_gitdir_path);
3173 strbuf_addf(&msg, _("If you want to reuse this local git "
3174 "directory instead of cloning again from\n"
3175 " %s\n"
3176 "use the '--force' option. If the local git "
3177 "directory is not the correct repo\n"
3178 "or you are unsure what this means choose "
3179 "another name with the '--name' option."),
3180 add_data->realrepo);
3182 die_msg = strbuf_detach(&msg, NULL);
3183 die("%s", die_msg);
3184 } else {
3185 printf(_("Reactivating local git directory for "
3186 "submodule '%s'\n"), add_data->sm_name);
3189 free(submod_gitdir_path);
3191 clone_data.prefix = add_data->prefix;
3192 clone_data.path = add_data->sm_path;
3193 clone_data.name = add_data->sm_name;
3194 clone_data.url = add_data->realrepo;
3195 clone_data.quiet = add_data->quiet;
3196 clone_data.progress = add_data->progress;
3197 if (add_data->reference_path) {
3198 char *p = xstrdup(add_data->reference_path);
3200 string_list_append(&reference, p)->util = p;
3202 clone_data.dissociate = add_data->dissociate;
3203 if (add_data->depth >= 0)
3204 clone_data.depth = xstrfmt("%d", add_data->depth);
3206 if (clone_submodule(&clone_data, &reference))
3207 goto cleanup;
3209 prepare_submodule_repo_env(&cp.env);
3210 cp.git_cmd = 1;
3211 cp.dir = add_data->sm_path;
3213 * NOTE: we only get here if add_data->force is true, so
3214 * passing --force to checkout is reasonable.
3216 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3218 if (add_data->branch) {
3219 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3220 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3223 if (run_command(&cp))
3224 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3226 ret = 0;
3227 cleanup:
3228 string_list_clear(&reference, 1);
3229 return ret;
3232 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3234 char *key;
3235 int ret;
3237 if (!is_writing_gitmodules_ok())
3238 die(_("please make sure that the .gitmodules file is in the working tree"));
3240 key = xstrfmt("submodule.%s.%s", name, var);
3241 ret = config_set_in_gitmodules_file_gently(key, value);
3242 free(key);
3244 return ret;
3247 static void configure_added_submodule(struct add_data *add_data)
3249 char *key;
3250 struct child_process add_submod = CHILD_PROCESS_INIT;
3251 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3253 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3254 git_config_set_gently(key, add_data->realrepo);
3255 free(key);
3257 add_submod.git_cmd = 1;
3258 strvec_pushl(&add_submod.args, "add",
3259 "--no-warn-embedded-repo", NULL);
3260 if (add_data->force)
3261 strvec_push(&add_submod.args, "--force");
3262 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3264 if (run_command(&add_submod))
3265 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3267 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3268 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3269 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3271 if (add_data->branch) {
3272 if (config_submodule_in_gitmodules(add_data->sm_name,
3273 "branch", add_data->branch))
3274 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3277 add_gitmodules.git_cmd = 1;
3278 strvec_pushl(&add_gitmodules.args,
3279 "add", "--force", "--", ".gitmodules", NULL);
3281 if (run_command(&add_gitmodules))
3282 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3285 * NEEDSWORK: In a multi-working-tree world this needs to be
3286 * set in the per-worktree config.
3289 * NEEDSWORK: In the longer run, we need to get rid of this
3290 * pattern of querying "submodule.active" before calling
3291 * is_submodule_active(), since that function needs to find
3292 * out the value of "submodule.active" again anyway.
3294 if (!git_config_get("submodule.active")) {
3296 * If the submodule being added isn't already covered by the
3297 * current configured pathspec, set the submodule's active flag
3299 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3300 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3301 git_config_set_gently(key, "true");
3302 free(key);
3304 } else {
3305 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3306 git_config_set_gently(key, "true");
3307 free(key);
3311 static void die_on_index_match(const char *path, int force)
3313 struct pathspec ps;
3314 const char *args[] = { path, NULL };
3315 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3317 if (repo_read_index_preload(the_repository, NULL, 0) < 0)
3318 die(_("index file corrupt"));
3320 if (ps.nr) {
3321 int i;
3322 char *ps_matched = xcalloc(ps.nr, 1);
3324 /* TODO: audit for interaction with sparse-index. */
3325 ensure_full_index(&the_index);
3328 * Since there is only one pathspec, we just need to
3329 * check ps_matched[0] to know if a cache entry matched.
3331 for (i = 0; i < the_index.cache_nr; i++) {
3332 ce_path_match(&the_index, the_index.cache[i], &ps,
3333 ps_matched);
3335 if (ps_matched[0]) {
3336 if (!force)
3337 die(_("'%s' already exists in the index"),
3338 path);
3339 if (!S_ISGITLINK(the_index.cache[i]->ce_mode))
3340 die(_("'%s' already exists in the index "
3341 "and is not a submodule"), path);
3342 break;
3345 free(ps_matched);
3347 clear_pathspec(&ps);
3350 static void die_on_repo_without_commits(const char *path)
3352 struct strbuf sb = STRBUF_INIT;
3353 strbuf_addstr(&sb, path);
3354 if (is_nonbare_repository_dir(&sb)) {
3355 struct object_id oid;
3356 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3357 die(_("'%s' does not have a commit checked out"), path);
3359 strbuf_release(&sb);
3362 static int module_add(int argc, const char **argv, const char *prefix)
3364 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3365 struct add_data add_data = ADD_DATA_INIT;
3366 char *to_free = NULL;
3367 struct option options[] = {
3368 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3369 N_("branch of repository to add as submodule")),
3370 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3371 PARSE_OPT_NOCOMPLETE),
3372 OPT__QUIET(&quiet, N_("print only error messages")),
3373 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3374 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3375 N_("reference repository")),
3376 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3377 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3378 N_("sets the submodule's name to the given string "
3379 "instead of defaulting to its path")),
3380 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3381 OPT_END()
3383 const char *const usage[] = {
3384 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3385 NULL
3387 struct strbuf sb = STRBUF_INIT;
3388 int ret = 1;
3390 argc = parse_options(argc, argv, prefix, options, usage, 0);
3392 if (!is_writing_gitmodules_ok())
3393 die(_("please make sure that the .gitmodules file is in the working tree"));
3395 if (prefix && *prefix &&
3396 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3397 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3399 if (argc == 0 || argc > 2)
3400 usage_with_options(usage, options);
3402 add_data.repo = argv[0];
3403 if (argc == 1)
3404 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3405 else
3406 add_data.sm_path = xstrdup(argv[1]);
3408 if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
3409 char *sm_path = add_data.sm_path;
3411 add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
3412 free(sm_path);
3415 if (starts_with_dot_dot_slash(add_data.repo) ||
3416 starts_with_dot_slash(add_data.repo)) {
3417 if (prefix)
3418 die(_("Relative path can only be used from the toplevel "
3419 "of the working tree"));
3421 /* dereference source url relative to parent's url */
3422 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3423 add_data.realrepo = to_free;
3424 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3425 add_data.realrepo = add_data.repo;
3426 } else {
3427 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3428 add_data.repo);
3432 * normalize path:
3433 * multiple //; leading ./; /./; /../;
3435 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3436 strip_dir_trailing_slashes(add_data.sm_path);
3438 if (validate_submodule_path(add_data.sm_path) < 0)
3439 exit(128);
3441 die_on_index_match(add_data.sm_path, force);
3442 die_on_repo_without_commits(add_data.sm_path);
3444 if (!force) {
3445 struct child_process cp = CHILD_PROCESS_INIT;
3447 cp.git_cmd = 1;
3448 cp.no_stdout = 1;
3449 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3450 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3451 if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3452 strbuf_complete_line(&sb);
3453 fputs(sb.buf, stderr);
3454 goto cleanup;
3458 if(!add_data.sm_name)
3459 add_data.sm_name = add_data.sm_path;
3461 if (check_submodule_name(add_data.sm_name))
3462 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3464 add_data.prefix = prefix;
3465 add_data.force = !!force;
3466 add_data.quiet = !!quiet;
3467 add_data.progress = !!progress;
3468 add_data.dissociate = !!dissociate;
3470 if (add_submodule(&add_data))
3471 goto cleanup;
3472 configure_added_submodule(&add_data);
3474 ret = 0;
3475 cleanup:
3476 free(add_data.sm_path);
3477 free(to_free);
3478 strbuf_release(&sb);
3480 return ret;
3483 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3485 parse_opt_subcommand_fn *fn = NULL;
3486 const char *const usage[] = {
3487 N_("git submodule--helper <command>"),
3488 NULL
3490 struct option options[] = {
3491 OPT_SUBCOMMAND("clone", &fn, module_clone),
3492 OPT_SUBCOMMAND("add", &fn, module_add),
3493 OPT_SUBCOMMAND("update", &fn, module_update),
3494 OPT_SUBCOMMAND("foreach", &fn, module_foreach),
3495 OPT_SUBCOMMAND("init", &fn, module_init),
3496 OPT_SUBCOMMAND("status", &fn, module_status),
3497 OPT_SUBCOMMAND("sync", &fn, module_sync),
3498 OPT_SUBCOMMAND("deinit", &fn, module_deinit),
3499 OPT_SUBCOMMAND("summary", &fn, module_summary),
3500 OPT_SUBCOMMAND("push-check", &fn, push_check),
3501 OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
3502 OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3503 OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3504 OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3505 OPT_END()
3507 argc = parse_options(argc, argv, prefix, options, usage, 0);
3509 return fn(argc, argv, prefix);