test-lib: add a GIT_TEST_PASSING_SANITIZE_LEAK=check mode
[git.git] / builtin / submodule--helper.c
blobfac52ade5e1792dff7ea717bbc28321fd2489509
1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
2 #include "builtin.h"
3 #include "repository.h"
4 #include "cache.h"
5 #include "config.h"
6 #include "parse-options.h"
7 #include "quote.h"
8 #include "pathspec.h"
9 #include "dir.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
14 #include "remote.h"
15 #include "refs.h"
16 #include "refspec.h"
17 #include "connect.h"
18 #include "revision.h"
19 #include "diffcore.h"
20 #include "diff.h"
21 #include "object-store.h"
22 #include "advice.h"
23 #include "branch.h"
24 #include "list-objects-filter-options.h"
26 #define OPT_QUIET (1 << 0)
27 #define OPT_CACHED (1 << 1)
28 #define OPT_RECURSIVE (1 << 2)
29 #define OPT_FORCE (1 << 3)
31 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
32 void *cb_data);
34 static char *repo_get_default_remote(struct repository *repo)
36 char *dest = NULL, *ret;
37 struct strbuf sb = STRBUF_INIT;
38 struct ref_store *store = get_main_ref_store(repo);
39 const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
40 NULL);
42 if (!refname)
43 die(_("No such ref: %s"), "HEAD");
45 /* detached HEAD */
46 if (!strcmp(refname, "HEAD"))
47 return xstrdup("origin");
49 if (!skip_prefix(refname, "refs/heads/", &refname))
50 die(_("Expecting a full ref name, got %s"), refname);
52 strbuf_addf(&sb, "branch.%s.remote", refname);
53 if (repo_config_get_string(repo, sb.buf, &dest))
54 ret = xstrdup("origin");
55 else
56 ret = dest;
58 strbuf_release(&sb);
59 return ret;
62 static char *get_default_remote_submodule(const char *module_path)
64 struct repository subrepo;
66 repo_submodule_init(&subrepo, the_repository, module_path, null_oid());
67 return repo_get_default_remote(&subrepo);
70 static char *get_default_remote(void)
72 return repo_get_default_remote(the_repository);
75 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
77 char *remoteurl, *resolved_url;
78 char *remote = get_default_remote();
79 struct strbuf remotesb = STRBUF_INIT;
81 strbuf_addf(&remotesb, "remote.%s.url", remote);
82 if (git_config_get_string(remotesb.buf, &remoteurl)) {
83 if (!quiet)
84 warning(_("could not look up configuration '%s'. "
85 "Assuming this repository is its own "
86 "authoritative upstream."),
87 remotesb.buf);
88 remoteurl = xgetcwd();
90 resolved_url = relative_url(remoteurl, rel_url, up_path);
92 free(remote);
93 free(remoteurl);
94 strbuf_release(&remotesb);
96 return resolved_url;
99 static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
101 char *remoteurl, *res;
102 const char *up_path, *url;
104 if (argc != 4)
105 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
107 up_path = argv[1];
108 remoteurl = xstrdup(argv[2]);
109 url = argv[3];
111 if (!strcmp(up_path, "(null)"))
112 up_path = NULL;
114 res = relative_url(remoteurl, url, up_path);
115 puts(res);
116 free(res);
117 free(remoteurl);
118 return 0;
121 /* the result should be freed by the caller. */
122 static char *get_submodule_displaypath(const char *path, const char *prefix)
124 const char *super_prefix = get_super_prefix();
126 if (prefix && super_prefix) {
127 BUG("cannot have prefix '%s' and superprefix '%s'",
128 prefix, super_prefix);
129 } else if (prefix) {
130 struct strbuf sb = STRBUF_INIT;
131 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
132 strbuf_release(&sb);
133 return displaypath;
134 } else if (super_prefix) {
135 return xstrfmt("%s%s", super_prefix, path);
136 } else {
137 return xstrdup(path);
141 static char *compute_rev_name(const char *sub_path, const char* object_id)
143 struct strbuf sb = STRBUF_INIT;
144 const char ***d;
146 static const char *describe_bare[] = { NULL };
148 static const char *describe_tags[] = { "--tags", NULL };
150 static const char *describe_contains[] = { "--contains", NULL };
152 static const char *describe_all_always[] = { "--all", "--always", NULL };
154 static const char **describe_argv[] = { describe_bare, describe_tags,
155 describe_contains,
156 describe_all_always, NULL };
158 for (d = describe_argv; *d; d++) {
159 struct child_process cp = CHILD_PROCESS_INIT;
160 prepare_submodule_repo_env(&cp.env);
161 cp.dir = sub_path;
162 cp.git_cmd = 1;
163 cp.no_stderr = 1;
165 strvec_push(&cp.args, "describe");
166 strvec_pushv(&cp.args, *d);
167 strvec_push(&cp.args, object_id);
169 if (!capture_command(&cp, &sb, 0)) {
170 strbuf_strip_suffix(&sb, "\n");
171 return strbuf_detach(&sb, NULL);
175 strbuf_release(&sb);
176 return NULL;
179 struct module_list {
180 const struct cache_entry **entries;
181 int alloc, nr;
183 #define MODULE_LIST_INIT { 0 }
185 static int module_list_compute(int argc, const char **argv,
186 const char *prefix,
187 struct pathspec *pathspec,
188 struct module_list *list)
190 int i, result = 0;
191 char *ps_matched = NULL;
192 parse_pathspec(pathspec, 0,
193 PATHSPEC_PREFER_FULL,
194 prefix, argv);
196 if (pathspec->nr)
197 ps_matched = xcalloc(pathspec->nr, 1);
199 if (read_cache() < 0)
200 die(_("index file corrupt"));
202 for (i = 0; i < active_nr; i++) {
203 const struct cache_entry *ce = active_cache[i];
205 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
206 0, ps_matched, 1) ||
207 !S_ISGITLINK(ce->ce_mode))
208 continue;
210 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
211 list->entries[list->nr++] = ce;
212 while (i + 1 < active_nr &&
213 !strcmp(ce->name, active_cache[i + 1]->name))
215 * Skip entries with the same name in different stages
216 * to make sure an entry is returned only once.
218 i++;
221 if (ps_matched && report_path_error(ps_matched, pathspec))
222 result = -1;
224 free(ps_matched);
226 return result;
229 static void module_list_active(struct module_list *list)
231 int i;
232 struct module_list active_modules = MODULE_LIST_INIT;
234 for (i = 0; i < list->nr; i++) {
235 const struct cache_entry *ce = list->entries[i];
237 if (!is_submodule_active(the_repository, ce->name))
238 continue;
240 ALLOC_GROW(active_modules.entries,
241 active_modules.nr + 1,
242 active_modules.alloc);
243 active_modules.entries[active_modules.nr++] = ce;
246 free(list->entries);
247 *list = active_modules;
250 static char *get_up_path(const char *path)
252 int i;
253 struct strbuf sb = STRBUF_INIT;
255 for (i = count_slashes(path); i; i--)
256 strbuf_addstr(&sb, "../");
259 * Check if 'path' ends with slash or not
260 * for having the same output for dir/sub_dir
261 * and dir/sub_dir/
263 if (!is_dir_sep(path[strlen(path) - 1]))
264 strbuf_addstr(&sb, "../");
266 return strbuf_detach(&sb, NULL);
269 static int module_list(int argc, const char **argv, const char *prefix)
271 int i;
272 struct pathspec pathspec;
273 struct module_list list = MODULE_LIST_INIT;
275 struct option module_list_options[] = {
276 OPT_STRING(0, "prefix", &prefix,
277 N_("path"),
278 N_("alternative anchor for relative paths")),
279 OPT_END()
282 const char *const git_submodule_helper_usage[] = {
283 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
284 NULL
287 argc = parse_options(argc, argv, prefix, module_list_options,
288 git_submodule_helper_usage, 0);
290 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
291 return 1;
293 for (i = 0; i < list.nr; i++) {
294 const struct cache_entry *ce = list.entries[i];
296 if (ce_stage(ce))
297 printf("%06o %s U\t", ce->ce_mode,
298 oid_to_hex(null_oid()));
299 else
300 printf("%06o %s %d\t", ce->ce_mode,
301 oid_to_hex(&ce->oid), ce_stage(ce));
303 fprintf(stdout, "%s\n", ce->name);
305 return 0;
308 static void for_each_listed_submodule(const struct module_list *list,
309 each_submodule_fn fn, void *cb_data)
311 int i;
312 for (i = 0; i < list->nr; i++)
313 fn(list->entries[i], cb_data);
316 struct foreach_cb {
317 int argc;
318 const char **argv;
319 const char *prefix;
320 int quiet;
321 int recursive;
323 #define FOREACH_CB_INIT { 0 }
325 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
326 void *cb_data)
328 struct foreach_cb *info = cb_data;
329 const char *path = list_item->name;
330 const struct object_id *ce_oid = &list_item->oid;
332 const struct submodule *sub;
333 struct child_process cp = CHILD_PROCESS_INIT;
334 char *displaypath;
336 displaypath = get_submodule_displaypath(path, info->prefix);
338 sub = submodule_from_path(the_repository, null_oid(), path);
340 if (!sub)
341 die(_("No url found for submodule path '%s' in .gitmodules"),
342 displaypath);
344 if (!is_submodule_populated_gently(path, NULL))
345 goto cleanup;
347 prepare_submodule_repo_env(&cp.env);
350 * For the purpose of executing <command> in the submodule,
351 * separate shell is used for the purpose of running the
352 * child process.
354 cp.use_shell = 1;
355 cp.dir = path;
358 * NEEDSWORK: the command currently has access to the variables $name,
359 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
360 * contains a single argument. This is done for maintaining a faithful
361 * translation from shell script.
363 if (info->argc == 1) {
364 char *toplevel = xgetcwd();
365 struct strbuf sb = STRBUF_INIT;
367 strvec_pushf(&cp.env, "name=%s", sub->name);
368 strvec_pushf(&cp.env, "sm_path=%s", path);
369 strvec_pushf(&cp.env, "displaypath=%s", displaypath);
370 strvec_pushf(&cp.env, "sha1=%s",
371 oid_to_hex(ce_oid));
372 strvec_pushf(&cp.env, "toplevel=%s", toplevel);
375 * Since the path variable was accessible from the script
376 * before porting, it is also made available after porting.
377 * The environment variable "PATH" has a very special purpose
378 * on windows. And since environment variables are
379 * case-insensitive in windows, it interferes with the
380 * existing PATH variable. Hence, to avoid that, we expose
381 * path via the args strvec and not via env.
383 sq_quote_buf(&sb, path);
384 strvec_pushf(&cp.args, "path=%s; %s",
385 sb.buf, info->argv[0]);
386 strbuf_release(&sb);
387 free(toplevel);
388 } else {
389 strvec_pushv(&cp.args, info->argv);
392 if (!info->quiet)
393 printf(_("Entering '%s'\n"), displaypath);
395 if (info->argv[0] && run_command(&cp))
396 die(_("run_command returned non-zero status for %s\n."),
397 displaypath);
399 if (info->recursive) {
400 struct child_process cpr = CHILD_PROCESS_INIT;
402 cpr.git_cmd = 1;
403 cpr.dir = path;
404 prepare_submodule_repo_env(&cpr.env);
406 strvec_pushl(&cpr.args, "--super-prefix", NULL);
407 strvec_pushf(&cpr.args, "%s/", displaypath);
408 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
409 NULL);
411 if (info->quiet)
412 strvec_push(&cpr.args, "--quiet");
414 strvec_push(&cpr.args, "--");
415 strvec_pushv(&cpr.args, info->argv);
417 if (run_command(&cpr))
418 die(_("run_command returned non-zero status while "
419 "recursing in the nested submodules of %s\n."),
420 displaypath);
423 cleanup:
424 free(displaypath);
427 static int module_foreach(int argc, const char **argv, const char *prefix)
429 struct foreach_cb info = FOREACH_CB_INIT;
430 struct pathspec pathspec;
431 struct module_list list = MODULE_LIST_INIT;
433 struct option module_foreach_options[] = {
434 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
435 OPT_BOOL(0, "recursive", &info.recursive,
436 N_("recurse into nested submodules")),
437 OPT_END()
440 const char *const git_submodule_helper_usage[] = {
441 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
442 NULL
445 argc = parse_options(argc, argv, prefix, module_foreach_options,
446 git_submodule_helper_usage, 0);
448 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
449 return 1;
451 info.argc = argc;
452 info.argv = argv;
453 info.prefix = prefix;
455 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
457 return 0;
460 static int starts_with_dot_slash(const char *const path)
462 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
463 PATH_MATCH_XPLATFORM);
466 static int starts_with_dot_dot_slash(const char *const path)
468 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
469 PATH_MATCH_XPLATFORM);
472 struct init_cb {
473 const char *prefix;
474 unsigned int flags;
476 #define INIT_CB_INIT { 0 }
478 static void init_submodule(const char *path, const char *prefix,
479 unsigned int flags)
481 const struct submodule *sub;
482 struct strbuf sb = STRBUF_INIT;
483 char *upd = NULL, *url = NULL, *displaypath;
485 displaypath = get_submodule_displaypath(path, prefix);
487 sub = submodule_from_path(the_repository, null_oid(), path);
489 if (!sub)
490 die(_("No url found for submodule path '%s' in .gitmodules"),
491 displaypath);
494 * NEEDSWORK: In a multi-working-tree world, this needs to be
495 * set in the per-worktree config.
497 * Set active flag for the submodule being initialized
499 if (!is_submodule_active(the_repository, path)) {
500 strbuf_addf(&sb, "submodule.%s.active", sub->name);
501 git_config_set_gently(sb.buf, "true");
502 strbuf_reset(&sb);
506 * Copy url setting when it is not set yet.
507 * To look up the url in .git/config, we must not fall back to
508 * .gitmodules, so look it up directly.
510 strbuf_addf(&sb, "submodule.%s.url", sub->name);
511 if (git_config_get_string(sb.buf, &url)) {
512 if (!sub->url)
513 die(_("No url found for submodule path '%s' in .gitmodules"),
514 displaypath);
516 url = xstrdup(sub->url);
518 /* Possibly a url relative to parent */
519 if (starts_with_dot_dot_slash(url) ||
520 starts_with_dot_slash(url)) {
521 char *oldurl = url;
522 url = resolve_relative_url(oldurl, NULL, 0);
523 free(oldurl);
526 if (git_config_set_gently(sb.buf, url))
527 die(_("Failed to register url for submodule path '%s'"),
528 displaypath);
529 if (!(flags & OPT_QUIET))
530 fprintf(stderr,
531 _("Submodule '%s' (%s) registered for path '%s'\n"),
532 sub->name, url, displaypath);
534 strbuf_reset(&sb);
536 /* Copy "update" setting when it is not set yet */
537 strbuf_addf(&sb, "submodule.%s.update", sub->name);
538 if (git_config_get_string(sb.buf, &upd) &&
539 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
540 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
541 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
542 sub->name);
543 upd = xstrdup("none");
544 } else
545 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
547 if (git_config_set_gently(sb.buf, upd))
548 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
550 strbuf_release(&sb);
551 free(displaypath);
552 free(url);
553 free(upd);
556 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
558 struct init_cb *info = cb_data;
559 init_submodule(list_item->name, info->prefix, info->flags);
562 static int module_init(int argc, const char **argv, const char *prefix)
564 struct init_cb info = INIT_CB_INIT;
565 struct pathspec pathspec;
566 struct module_list list = MODULE_LIST_INIT;
567 int quiet = 0;
569 struct option module_init_options[] = {
570 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
571 OPT_END()
574 const char *const git_submodule_helper_usage[] = {
575 N_("git submodule init [<options>] [<path>]"),
576 NULL
579 argc = parse_options(argc, argv, prefix, module_init_options,
580 git_submodule_helper_usage, 0);
582 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
583 return 1;
586 * If there are no path args and submodule.active is set then,
587 * by default, only initialize 'active' modules.
589 if (!argc && git_config_get_value_multi("submodule.active"))
590 module_list_active(&list);
592 info.prefix = prefix;
593 if (quiet)
594 info.flags |= OPT_QUIET;
596 for_each_listed_submodule(&list, init_submodule_cb, &info);
598 return 0;
601 struct status_cb {
602 const char *prefix;
603 unsigned int flags;
605 #define STATUS_CB_INIT { 0 }
607 static void print_status(unsigned int flags, char state, const char *path,
608 const struct object_id *oid, const char *displaypath)
610 if (flags & OPT_QUIET)
611 return;
613 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
615 if (state == ' ' || state == '+') {
616 const char *name = compute_rev_name(path, oid_to_hex(oid));
618 if (name)
619 printf(" (%s)", name);
622 printf("\n");
625 static int handle_submodule_head_ref(const char *refname,
626 const struct object_id *oid, int flags,
627 void *cb_data)
629 struct object_id *output = cb_data;
630 if (oid)
631 oidcpy(output, oid);
633 return 0;
636 static void status_submodule(const char *path, const struct object_id *ce_oid,
637 unsigned int ce_flags, const char *prefix,
638 unsigned int flags)
640 char *displaypath;
641 struct strvec diff_files_args = STRVEC_INIT;
642 struct rev_info rev = REV_INFO_INIT;
643 int diff_files_result;
644 struct strbuf buf = STRBUF_INIT;
645 const char *git_dir;
647 if (!submodule_from_path(the_repository, null_oid(), path))
648 die(_("no submodule mapping found in .gitmodules for path '%s'"),
649 path);
651 displaypath = get_submodule_displaypath(path, prefix);
653 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
654 print_status(flags, 'U', path, null_oid(), displaypath);
655 goto cleanup;
658 strbuf_addf(&buf, "%s/.git", path);
659 git_dir = read_gitfile(buf.buf);
660 if (!git_dir)
661 git_dir = buf.buf;
663 if (!is_submodule_active(the_repository, path) ||
664 !is_git_directory(git_dir)) {
665 print_status(flags, '-', path, ce_oid, displaypath);
666 strbuf_release(&buf);
667 goto cleanup;
669 strbuf_release(&buf);
671 strvec_pushl(&diff_files_args, "diff-files",
672 "--ignore-submodules=dirty", "--quiet", "--",
673 path, NULL);
675 git_config(git_diff_basic_config, NULL);
677 repo_init_revisions(the_repository, &rev, NULL);
678 rev.abbrev = 0;
679 diff_files_args.nr = setup_revisions(diff_files_args.nr,
680 diff_files_args.v,
681 &rev, NULL);
682 diff_files_result = run_diff_files(&rev, 0);
684 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
685 print_status(flags, ' ', path, ce_oid,
686 displaypath);
687 } else if (!(flags & OPT_CACHED)) {
688 struct object_id oid;
689 struct ref_store *refs = get_submodule_ref_store(path);
691 if (!refs) {
692 print_status(flags, '-', path, ce_oid, displaypath);
693 goto cleanup;
695 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
696 die(_("could not resolve HEAD ref inside the "
697 "submodule '%s'"), path);
699 print_status(flags, '+', path, &oid, displaypath);
700 } else {
701 print_status(flags, '+', path, ce_oid, displaypath);
704 if (flags & OPT_RECURSIVE) {
705 struct child_process cpr = CHILD_PROCESS_INIT;
707 cpr.git_cmd = 1;
708 cpr.dir = path;
709 prepare_submodule_repo_env(&cpr.env);
711 strvec_push(&cpr.args, "--super-prefix");
712 strvec_pushf(&cpr.args, "%s/", displaypath);
713 strvec_pushl(&cpr.args, "submodule--helper", "status",
714 "--recursive", NULL);
716 if (flags & OPT_CACHED)
717 strvec_push(&cpr.args, "--cached");
719 if (flags & OPT_QUIET)
720 strvec_push(&cpr.args, "--quiet");
722 if (run_command(&cpr))
723 die(_("failed to recurse into submodule '%s'"), path);
726 cleanup:
727 strvec_clear(&diff_files_args);
728 free(displaypath);
729 release_revisions(&rev);
732 static void status_submodule_cb(const struct cache_entry *list_item,
733 void *cb_data)
735 struct status_cb *info = cb_data;
736 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
737 info->prefix, info->flags);
740 static int module_status(int argc, const char **argv, const char *prefix)
742 struct status_cb info = STATUS_CB_INIT;
743 struct pathspec pathspec;
744 struct module_list list = MODULE_LIST_INIT;
745 int quiet = 0;
747 struct option module_status_options[] = {
748 OPT__QUIET(&quiet, N_("suppress submodule status output")),
749 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
750 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
751 OPT_END()
754 const char *const git_submodule_helper_usage[] = {
755 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
756 NULL
759 argc = parse_options(argc, argv, prefix, module_status_options,
760 git_submodule_helper_usage, 0);
762 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
763 return 1;
765 info.prefix = prefix;
766 if (quiet)
767 info.flags |= OPT_QUIET;
769 for_each_listed_submodule(&list, status_submodule_cb, &info);
771 return 0;
774 static int module_name(int argc, const char **argv, const char *prefix)
776 const struct submodule *sub;
778 if (argc != 2)
779 usage(_("git submodule--helper name <path>"));
781 sub = submodule_from_path(the_repository, null_oid(), argv[1]);
783 if (!sub)
784 die(_("no submodule mapping found in .gitmodules for path '%s'"),
785 argv[1]);
787 printf("%s\n", sub->name);
789 return 0;
792 struct module_cb {
793 unsigned int mod_src;
794 unsigned int mod_dst;
795 struct object_id oid_src;
796 struct object_id oid_dst;
797 char status;
798 const char *sm_path;
800 #define MODULE_CB_INIT { 0 }
802 struct module_cb_list {
803 struct module_cb **entries;
804 int alloc, nr;
806 #define MODULE_CB_LIST_INIT { 0 }
808 struct summary_cb {
809 int argc;
810 const char **argv;
811 const char *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, 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 char *errmsg = NULL;
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);
908 if (refs)
909 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
910 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
911 struct stat st;
912 int fd = open(p->sm_path, O_RDONLY);
914 if (fd < 0 || fstat(fd, &st) < 0 ||
915 index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
916 p->sm_path, 0))
917 error(_("couldn't hash object from '%s'"), p->sm_path);
918 } else {
919 /* for a submodule removal (mode:0000000), don't warn */
920 if (p->mod_dst)
921 warning(_("unexpected mode %o\n"), p->mod_dst);
925 if (S_ISGITLINK(p->mod_src)) {
926 if (p->status != 'D')
927 src_abbrev = verify_submodule_committish(p->sm_path,
928 oid_to_hex(&p->oid_src));
929 if (!src_abbrev) {
930 missing_src = 1;
932 * As `rev-parse` failed, we fallback to getting
933 * the abbreviated hash using oid_src. We do
934 * this as we might still need the abbreviated
935 * hash in cases like a submodule type change, etc.
937 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
939 } else {
941 * The source does not point to a submodule.
942 * So, we fallback to getting the abbreviation using
943 * oid_src as we might still need the abbreviated
944 * hash in cases like submodule add, etc.
946 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
949 if (S_ISGITLINK(p->mod_dst)) {
950 dst_abbrev = verify_submodule_committish(p->sm_path,
951 oid_to_hex(&p->oid_dst));
952 if (!dst_abbrev) {
953 missing_dst = 1;
955 * As `rev-parse` failed, we fallback to getting
956 * the abbreviated hash using oid_dst. We do
957 * this as we might still need the abbreviated
958 * hash in cases like a submodule type change, etc.
960 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
962 } else {
964 * The destination does not point to a submodule.
965 * So, we fallback to getting the abbreviation using
966 * oid_dst as we might still need the abbreviated
967 * hash in cases like a submodule removal, etc.
969 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
972 displaypath = get_submodule_displaypath(p->sm_path, info->prefix);
974 if (!missing_src && !missing_dst) {
975 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
976 struct strbuf sb_rev_list = STRBUF_INIT;
978 strvec_pushl(&cp_rev_list.args, "rev-list",
979 "--first-parent", "--count", NULL);
980 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
981 strvec_pushf(&cp_rev_list.args, "%s...%s",
982 src_abbrev, dst_abbrev);
983 else
984 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
985 src_abbrev : dst_abbrev);
986 strvec_push(&cp_rev_list.args, "--");
988 cp_rev_list.git_cmd = 1;
989 cp_rev_list.dir = p->sm_path;
990 prepare_submodule_repo_env(&cp_rev_list.env);
992 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
993 total_commits = atoi(sb_rev_list.buf);
995 strbuf_release(&sb_rev_list);
996 } else {
998 * Don't give error msg for modification whose dst is not
999 * submodule, i.e., deleted or changed to blob
1001 if (S_ISGITLINK(p->mod_dst)) {
1002 struct strbuf errmsg_str = STRBUF_INIT;
1003 if (missing_src && missing_dst) {
1004 strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commits %s and %s\n",
1005 displaypath, oid_to_hex(&p->oid_src),
1006 oid_to_hex(&p->oid_dst));
1007 } else {
1008 strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commit %s\n",
1009 displaypath, missing_src ?
1010 oid_to_hex(&p->oid_src) :
1011 oid_to_hex(&p->oid_dst));
1013 errmsg = strbuf_detach(&errmsg_str, NULL);
1017 print_submodule_summary(info, errmsg, total_commits,
1018 displaypath, src_abbrev,
1019 dst_abbrev, p);
1021 free(displaypath);
1022 free(src_abbrev);
1023 free(dst_abbrev);
1026 static void prepare_submodule_summary(struct summary_cb *info,
1027 struct module_cb_list *list)
1029 int i;
1030 for (i = 0; i < list->nr; i++) {
1031 const struct submodule *sub;
1032 struct module_cb *p = list->entries[i];
1033 struct strbuf sm_gitdir = STRBUF_INIT;
1035 if (p->status == 'D' || p->status == 'T') {
1036 generate_submodule_summary(info, p);
1037 continue;
1040 if (info->for_status && p->status != 'A' &&
1041 (sub = submodule_from_path(the_repository,
1042 null_oid(), p->sm_path))) {
1043 char *config_key = NULL;
1044 const char *value;
1045 int ignore_all = 0;
1047 config_key = xstrfmt("submodule.%s.ignore",
1048 sub->name);
1049 if (!git_config_get_string_tmp(config_key, &value))
1050 ignore_all = !strcmp(value, "all");
1051 else if (sub->ignore)
1052 ignore_all = !strcmp(sub->ignore, "all");
1054 free(config_key);
1055 if (ignore_all)
1056 continue;
1059 /* Also show added or modified modules which are checked out */
1060 strbuf_addstr(&sm_gitdir, p->sm_path);
1061 if (is_nonbare_repository_dir(&sm_gitdir))
1062 generate_submodule_summary(info, p);
1063 strbuf_release(&sm_gitdir);
1067 static void submodule_summary_callback(struct diff_queue_struct *q,
1068 struct diff_options *options,
1069 void *data)
1071 int i;
1072 struct module_cb_list *list = data;
1073 for (i = 0; i < q->nr; i++) {
1074 struct diff_filepair *p = q->queue[i];
1075 struct module_cb *temp;
1077 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1078 continue;
1079 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1080 temp->mod_src = p->one->mode;
1081 temp->mod_dst = p->two->mode;
1082 temp->oid_src = p->one->oid;
1083 temp->oid_dst = p->two->oid;
1084 temp->status = p->status;
1085 temp->sm_path = xstrdup(p->one->path);
1087 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1088 list->entries[list->nr++] = temp;
1092 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1094 switch (diff_cmd) {
1095 case DIFF_INDEX: return "diff-index";
1096 case DIFF_FILES: return "diff-files";
1097 default: BUG("bad diff_cmd value %d", diff_cmd);
1101 static int compute_summary_module_list(struct object_id *head_oid,
1102 struct summary_cb *info,
1103 enum diff_cmd diff_cmd)
1105 struct strvec diff_args = STRVEC_INIT;
1106 struct rev_info rev;
1107 struct module_cb_list list = MODULE_CB_LIST_INIT;
1108 int ret = 0;
1110 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1111 if (info->cached)
1112 strvec_push(&diff_args, "--cached");
1113 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1114 if (head_oid)
1115 strvec_push(&diff_args, oid_to_hex(head_oid));
1116 strvec_push(&diff_args, "--");
1117 if (info->argc)
1118 strvec_pushv(&diff_args, info->argv);
1120 git_config(git_diff_basic_config, NULL);
1121 init_revisions(&rev, info->prefix);
1122 rev.abbrev = 0;
1123 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1124 setup_revisions(diff_args.nr, diff_args.v, &rev, NULL);
1125 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1126 rev.diffopt.format_callback = submodule_summary_callback;
1127 rev.diffopt.format_callback_data = &list;
1129 if (!info->cached) {
1130 if (diff_cmd == DIFF_INDEX)
1131 setup_work_tree();
1132 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1133 perror("read_cache_preload");
1134 ret = -1;
1135 goto cleanup;
1137 } else if (read_cache() < 0) {
1138 perror("read_cache");
1139 ret = -1;
1140 goto cleanup;
1143 if (diff_cmd == DIFF_INDEX)
1144 run_diff_index(&rev, info->cached);
1145 else
1146 run_diff_files(&rev, 0);
1147 prepare_submodule_summary(info, &list);
1148 cleanup:
1149 strvec_clear(&diff_args);
1150 release_revisions(&rev);
1151 return ret;
1154 static int module_summary(int argc, const char **argv, const char *prefix)
1156 struct summary_cb info = SUMMARY_CB_INIT;
1157 int cached = 0;
1158 int for_status = 0;
1159 int files = 0;
1160 int summary_limit = -1;
1161 enum diff_cmd diff_cmd = DIFF_INDEX;
1162 struct object_id head_oid;
1163 int ret;
1165 struct option module_summary_options[] = {
1166 OPT_BOOL(0, "cached", &cached,
1167 N_("use the commit stored in the index instead of the submodule HEAD")),
1168 OPT_BOOL(0, "files", &files,
1169 N_("compare the commit in the index with that in the submodule HEAD")),
1170 OPT_BOOL(0, "for-status", &for_status,
1171 N_("skip submodules with 'ignore_config' value set to 'all'")),
1172 OPT_INTEGER('n', "summary-limit", &summary_limit,
1173 N_("limit the summary size")),
1174 OPT_END()
1177 const char *const git_submodule_helper_usage[] = {
1178 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1179 NULL
1182 argc = parse_options(argc, argv, prefix, module_summary_options,
1183 git_submodule_helper_usage, 0);
1185 if (!summary_limit)
1186 return 0;
1188 if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) {
1189 if (argc) {
1190 argv++;
1191 argc--;
1193 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1194 /* before the first commit: compare with an empty tree */
1195 oidcpy(&head_oid, the_hash_algo->empty_tree);
1196 if (argc) {
1197 argv++;
1198 argc--;
1200 } else {
1201 if (get_oid("HEAD", &head_oid))
1202 die(_("could not fetch a revision for HEAD"));
1205 if (files) {
1206 if (cached)
1207 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1208 diff_cmd = DIFF_FILES;
1211 info.argc = argc;
1212 info.argv = argv;
1213 info.prefix = prefix;
1214 info.cached = !!cached;
1215 info.files = !!files;
1216 info.for_status = !!for_status;
1217 info.summary_limit = summary_limit;
1219 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1220 &info, diff_cmd);
1221 return ret;
1224 struct sync_cb {
1225 const char *prefix;
1226 unsigned int flags;
1228 #define SYNC_CB_INIT { 0 }
1230 static void sync_submodule(const char *path, const char *prefix,
1231 unsigned int flags)
1233 const struct submodule *sub;
1234 char *remote_key = NULL;
1235 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1236 struct strbuf sb = STRBUF_INIT;
1237 char *sub_config_path = NULL;
1239 if (!is_submodule_active(the_repository, path))
1240 return;
1242 sub = submodule_from_path(the_repository, null_oid(), path);
1244 if (sub && sub->url) {
1245 if (starts_with_dot_dot_slash(sub->url) ||
1246 starts_with_dot_slash(sub->url)) {
1247 char *up_path = get_up_path(path);
1248 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1249 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1250 free(up_path);
1251 } else {
1252 sub_origin_url = xstrdup(sub->url);
1253 super_config_url = xstrdup(sub->url);
1255 } else {
1256 sub_origin_url = xstrdup("");
1257 super_config_url = xstrdup("");
1260 displaypath = get_submodule_displaypath(path, prefix);
1262 if (!(flags & OPT_QUIET))
1263 printf(_("Synchronizing submodule url for '%s'\n"),
1264 displaypath);
1266 strbuf_reset(&sb);
1267 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1268 if (git_config_set_gently(sb.buf, super_config_url))
1269 die(_("failed to register url for submodule path '%s'"),
1270 displaypath);
1272 if (!is_submodule_populated_gently(path, NULL))
1273 goto cleanup;
1275 strbuf_reset(&sb);
1276 default_remote = get_default_remote_submodule(path);
1277 if (!default_remote)
1278 die(_("failed to get the default remote for submodule '%s'"),
1279 path);
1281 remote_key = xstrfmt("remote.%s.url", default_remote);
1282 free(default_remote);
1284 submodule_to_gitdir(&sb, path);
1285 strbuf_addstr(&sb, "/config");
1287 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1288 die(_("failed to update remote for submodule '%s'"),
1289 path);
1291 if (flags & OPT_RECURSIVE) {
1292 struct child_process cpr = CHILD_PROCESS_INIT;
1294 cpr.git_cmd = 1;
1295 cpr.dir = path;
1296 prepare_submodule_repo_env(&cpr.env);
1298 strvec_push(&cpr.args, "--super-prefix");
1299 strvec_pushf(&cpr.args, "%s/", displaypath);
1300 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1301 "--recursive", NULL);
1303 if (flags & OPT_QUIET)
1304 strvec_push(&cpr.args, "--quiet");
1306 if (run_command(&cpr))
1307 die(_("failed to recurse into submodule '%s'"),
1308 path);
1311 cleanup:
1312 free(super_config_url);
1313 free(sub_origin_url);
1314 strbuf_release(&sb);
1315 free(remote_key);
1316 free(displaypath);
1317 free(sub_config_path);
1320 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1322 struct sync_cb *info = cb_data;
1323 sync_submodule(list_item->name, info->prefix, info->flags);
1326 static int module_sync(int argc, const char **argv, const char *prefix)
1328 struct sync_cb info = SYNC_CB_INIT;
1329 struct pathspec pathspec;
1330 struct module_list list = MODULE_LIST_INIT;
1331 int quiet = 0;
1332 int recursive = 0;
1334 struct option module_sync_options[] = {
1335 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1336 OPT_BOOL(0, "recursive", &recursive,
1337 N_("recurse into nested submodules")),
1338 OPT_END()
1341 const char *const git_submodule_helper_usage[] = {
1342 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1343 NULL
1346 argc = parse_options(argc, argv, prefix, module_sync_options,
1347 git_submodule_helper_usage, 0);
1349 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1350 return 1;
1352 info.prefix = prefix;
1353 if (quiet)
1354 info.flags |= OPT_QUIET;
1355 if (recursive)
1356 info.flags |= OPT_RECURSIVE;
1358 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1360 return 0;
1363 struct deinit_cb {
1364 const char *prefix;
1365 unsigned int flags;
1367 #define DEINIT_CB_INIT { 0 }
1369 static void deinit_submodule(const char *path, const char *prefix,
1370 unsigned int flags)
1372 const struct submodule *sub;
1373 char *displaypath = NULL;
1374 struct child_process cp_config = CHILD_PROCESS_INIT;
1375 struct strbuf sb_config = STRBUF_INIT;
1376 char *sub_git_dir = xstrfmt("%s/.git", path);
1378 sub = submodule_from_path(the_repository, null_oid(), path);
1380 if (!sub || !sub->name)
1381 goto cleanup;
1383 displaypath = get_submodule_displaypath(path, prefix);
1385 /* remove the submodule work tree (unless the user already did it) */
1386 if (is_directory(path)) {
1387 struct strbuf sb_rm = STRBUF_INIT;
1388 const char *format;
1390 if (is_directory(sub_git_dir)) {
1391 if (!(flags & OPT_QUIET))
1392 warning(_("Submodule work tree '%s' contains a .git "
1393 "directory. This will be replaced with a "
1394 ".git file by using absorbgitdirs."),
1395 displaypath);
1397 absorb_git_dir_into_superproject(path,
1398 ABSORB_GITDIR_RECURSE_SUBMODULES);
1402 if (!(flags & OPT_FORCE)) {
1403 struct child_process cp_rm = CHILD_PROCESS_INIT;
1404 cp_rm.git_cmd = 1;
1405 strvec_pushl(&cp_rm.args, "rm", "-qn",
1406 path, NULL);
1408 if (run_command(&cp_rm))
1409 die(_("Submodule work tree '%s' contains local "
1410 "modifications; use '-f' to discard them"),
1411 displaypath);
1414 strbuf_addstr(&sb_rm, path);
1416 if (!remove_dir_recursively(&sb_rm, 0))
1417 format = _("Cleared directory '%s'\n");
1418 else
1419 format = _("Could not remove submodule work tree '%s'\n");
1421 if (!(flags & OPT_QUIET))
1422 printf(format, displaypath);
1424 submodule_unset_core_worktree(sub);
1426 strbuf_release(&sb_rm);
1429 if (mkdir(path, 0777))
1430 printf(_("could not create empty submodule directory %s"),
1431 displaypath);
1433 cp_config.git_cmd = 1;
1434 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1435 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1437 /* remove the .git/config entries (unless the user already did it) */
1438 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1439 char *sub_key = xstrfmt("submodule.%s", sub->name);
1441 * remove the whole section so we have a clean state when
1442 * the user later decides to init this submodule again
1444 git_config_rename_section_in_file(NULL, sub_key, NULL);
1445 if (!(flags & OPT_QUIET))
1446 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1447 sub->name, sub->url, displaypath);
1448 free(sub_key);
1451 cleanup:
1452 free(displaypath);
1453 free(sub_git_dir);
1454 strbuf_release(&sb_config);
1457 static void deinit_submodule_cb(const struct cache_entry *list_item,
1458 void *cb_data)
1460 struct deinit_cb *info = cb_data;
1461 deinit_submodule(list_item->name, info->prefix, info->flags);
1464 static int module_deinit(int argc, const char **argv, const char *prefix)
1466 struct deinit_cb info = DEINIT_CB_INIT;
1467 struct pathspec pathspec;
1468 struct module_list list = MODULE_LIST_INIT;
1469 int quiet = 0;
1470 int force = 0;
1471 int all = 0;
1473 struct option module_deinit_options[] = {
1474 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1475 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1476 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1477 OPT_END()
1480 const char *const git_submodule_helper_usage[] = {
1481 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1482 NULL
1485 argc = parse_options(argc, argv, prefix, module_deinit_options,
1486 git_submodule_helper_usage, 0);
1488 if (all && argc) {
1489 error("pathspec and --all are incompatible");
1490 usage_with_options(git_submodule_helper_usage,
1491 module_deinit_options);
1494 if (!argc && !all)
1495 die(_("Use '--all' if you really want to deinitialize all submodules"));
1497 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1498 return 1;
1500 info.prefix = prefix;
1501 if (quiet)
1502 info.flags |= OPT_QUIET;
1503 if (force)
1504 info.flags |= OPT_FORCE;
1506 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1508 return 0;
1511 struct module_clone_data {
1512 const char *prefix;
1513 const char *path;
1514 const char *name;
1515 const char *url;
1516 const char *depth;
1517 struct list_objects_filter_options *filter_options;
1518 struct string_list reference;
1519 unsigned int quiet: 1;
1520 unsigned int progress: 1;
1521 unsigned int dissociate: 1;
1522 unsigned int require_init: 1;
1523 int single_branch;
1525 #define MODULE_CLONE_DATA_INIT { \
1526 .reference = STRING_LIST_INIT_NODUP, \
1527 .single_branch = -1, \
1530 struct submodule_alternate_setup {
1531 const char *submodule_name;
1532 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1533 SUBMODULE_ALTERNATE_ERROR_DIE,
1534 SUBMODULE_ALTERNATE_ERROR_INFO,
1535 SUBMODULE_ALTERNATE_ERROR_IGNORE
1536 } error_mode;
1537 struct string_list *reference;
1539 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1540 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1543 static const char alternate_error_advice[] = N_(
1544 "An alternate computed from a superproject's alternate is invalid.\n"
1545 "To allow Git to clone without an alternate in such a case, set\n"
1546 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1547 "'--reference-if-able' instead of '--reference'."
1550 static int add_possible_reference_from_superproject(
1551 struct object_directory *odb, void *sas_cb)
1553 struct submodule_alternate_setup *sas = sas_cb;
1554 size_t len;
1557 * If the alternate object store is another repository, try the
1558 * standard layout with .git/(modules/<name>)+/objects
1560 if (strip_suffix(odb->path, "/objects", &len)) {
1561 struct repository alternate;
1562 char *sm_alternate;
1563 struct strbuf sb = STRBUF_INIT;
1564 struct strbuf err = STRBUF_INIT;
1565 strbuf_add(&sb, odb->path, len);
1567 repo_init(&alternate, sb.buf, NULL);
1570 * We need to end the new path with '/' to mark it as a dir,
1571 * otherwise a submodule name containing '/' will be broken
1572 * as the last part of a missing submodule reference would
1573 * be taken as a file name.
1575 strbuf_reset(&sb);
1576 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1577 strbuf_addch(&sb, '/');
1578 repo_clear(&alternate);
1580 sm_alternate = compute_alternate_path(sb.buf, &err);
1581 if (sm_alternate) {
1582 string_list_append(sas->reference, xstrdup(sb.buf));
1583 free(sm_alternate);
1584 } else {
1585 switch (sas->error_mode) {
1586 case SUBMODULE_ALTERNATE_ERROR_DIE:
1587 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1588 advise(_(alternate_error_advice));
1589 die(_("submodule '%s' cannot add alternate: %s"),
1590 sas->submodule_name, err.buf);
1591 case SUBMODULE_ALTERNATE_ERROR_INFO:
1592 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1593 sas->submodule_name, err.buf);
1594 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1595 ; /* nothing */
1598 strbuf_release(&sb);
1601 return 0;
1604 static void prepare_possible_alternates(const char *sm_name,
1605 struct string_list *reference)
1607 char *sm_alternate = NULL, *error_strategy = NULL;
1608 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1610 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1611 if (!sm_alternate)
1612 return;
1614 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1616 if (!error_strategy)
1617 error_strategy = xstrdup("die");
1619 sas.submodule_name = sm_name;
1620 sas.reference = reference;
1621 if (!strcmp(error_strategy, "die"))
1622 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1623 else if (!strcmp(error_strategy, "info"))
1624 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1625 else if (!strcmp(error_strategy, "ignore"))
1626 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1627 else
1628 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1630 if (!strcmp(sm_alternate, "superproject"))
1631 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1632 else if (!strcmp(sm_alternate, "no"))
1633 ; /* do nothing */
1634 else
1635 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1637 free(sm_alternate);
1638 free(error_strategy);
1641 static int clone_submodule(struct module_clone_data *clone_data)
1643 char *p, *sm_gitdir;
1644 char *sm_alternate = NULL, *error_strategy = NULL;
1645 struct strbuf sb = STRBUF_INIT;
1646 struct child_process cp = CHILD_PROCESS_INIT;
1648 submodule_name_to_gitdir(&sb, the_repository, clone_data->name);
1649 sm_gitdir = absolute_pathdup(sb.buf);
1650 strbuf_reset(&sb);
1652 if (!is_absolute_path(clone_data->path)) {
1653 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), clone_data->path);
1654 clone_data->path = strbuf_detach(&sb, NULL);
1655 } else {
1656 clone_data->path = xstrdup(clone_data->path);
1659 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1660 die(_("refusing to create/use '%s' in another submodule's "
1661 "git dir"), sm_gitdir);
1663 if (!file_exists(sm_gitdir)) {
1664 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1665 die(_("could not create directory '%s'"), sm_gitdir);
1667 prepare_possible_alternates(clone_data->name, &clone_data->reference);
1669 strvec_push(&cp.args, "clone");
1670 strvec_push(&cp.args, "--no-checkout");
1671 if (clone_data->quiet)
1672 strvec_push(&cp.args, "--quiet");
1673 if (clone_data->progress)
1674 strvec_push(&cp.args, "--progress");
1675 if (clone_data->depth && *(clone_data->depth))
1676 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1677 if (clone_data->reference.nr) {
1678 struct string_list_item *item;
1679 for_each_string_list_item(item, &clone_data->reference)
1680 strvec_pushl(&cp.args, "--reference",
1681 item->string, NULL);
1683 if (clone_data->dissociate)
1684 strvec_push(&cp.args, "--dissociate");
1685 if (sm_gitdir && *sm_gitdir)
1686 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1687 if (clone_data->filter_options && clone_data->filter_options->choice)
1688 strvec_pushf(&cp.args, "--filter=%s",
1689 expand_list_objects_filter_spec(
1690 clone_data->filter_options));
1691 if (clone_data->single_branch >= 0)
1692 strvec_push(&cp.args, clone_data->single_branch ?
1693 "--single-branch" :
1694 "--no-single-branch");
1696 strvec_push(&cp.args, "--");
1697 strvec_push(&cp.args, clone_data->url);
1698 strvec_push(&cp.args, clone_data->path);
1700 cp.git_cmd = 1;
1701 prepare_submodule_repo_env(&cp.env);
1702 cp.no_stdin = 1;
1704 if(run_command(&cp))
1705 die(_("clone of '%s' into submodule path '%s' failed"),
1706 clone_data->url, clone_data->path);
1707 } else {
1708 if (clone_data->require_init && !access(clone_data->path, X_OK) &&
1709 !is_empty_dir(clone_data->path))
1710 die(_("directory not empty: '%s'"), clone_data->path);
1711 if (safe_create_leading_directories_const(clone_data->path) < 0)
1712 die(_("could not create directory '%s'"), clone_data->path);
1713 strbuf_addf(&sb, "%s/index", sm_gitdir);
1714 unlink_or_warn(sb.buf);
1715 strbuf_reset(&sb);
1718 connect_work_tree_and_git_dir(clone_data->path, sm_gitdir, 0);
1720 p = git_pathdup_submodule(clone_data->path, "config");
1721 if (!p)
1722 die(_("could not get submodule directory for '%s'"), clone_data->path);
1724 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1725 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1726 if (sm_alternate)
1727 git_config_set_in_file(p, "submodule.alternateLocation",
1728 sm_alternate);
1729 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1730 if (error_strategy)
1731 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1732 error_strategy);
1734 free(sm_alternate);
1735 free(error_strategy);
1737 strbuf_release(&sb);
1738 free(sm_gitdir);
1739 free(p);
1740 return 0;
1743 static int module_clone(int argc, const char **argv, const char *prefix)
1745 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1746 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1747 struct list_objects_filter_options filter_options;
1749 struct option module_clone_options[] = {
1750 OPT_STRING(0, "prefix", &clone_data.prefix,
1751 N_("path"),
1752 N_("alternative anchor for relative paths")),
1753 OPT_STRING(0, "path", &clone_data.path,
1754 N_("path"),
1755 N_("where the new submodule will be cloned to")),
1756 OPT_STRING(0, "name", &clone_data.name,
1757 N_("string"),
1758 N_("name of the new submodule")),
1759 OPT_STRING(0, "url", &clone_data.url,
1760 N_("string"),
1761 N_("url where to clone the submodule from")),
1762 OPT_STRING_LIST(0, "reference", &clone_data.reference,
1763 N_("repo"),
1764 N_("reference repository")),
1765 OPT_BOOL(0, "dissociate", &dissociate,
1766 N_("use --reference only while cloning")),
1767 OPT_STRING(0, "depth", &clone_data.depth,
1768 N_("string"),
1769 N_("depth for shallow clones")),
1770 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1771 OPT_BOOL(0, "progress", &progress,
1772 N_("force cloning progress")),
1773 OPT_BOOL(0, "require-init", &require_init,
1774 N_("disallow cloning into non-empty directory")),
1775 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1776 N_("clone only one branch, HEAD or --branch")),
1777 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1778 OPT_END()
1781 const char *const git_submodule_helper_usage[] = {
1782 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1783 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1784 "[--single-branch] [--filter <filter-spec>] "
1785 "--url <url> --path <path>"),
1786 NULL
1789 memset(&filter_options, 0, sizeof(filter_options));
1790 argc = parse_options(argc, argv, prefix, module_clone_options,
1791 git_submodule_helper_usage, 0);
1793 clone_data.dissociate = !!dissociate;
1794 clone_data.quiet = !!quiet;
1795 clone_data.progress = !!progress;
1796 clone_data.require_init = !!require_init;
1797 clone_data.filter_options = &filter_options;
1799 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1800 usage_with_options(git_submodule_helper_usage,
1801 module_clone_options);
1803 clone_submodule(&clone_data);
1804 list_objects_filter_release(&filter_options);
1805 return 0;
1808 static void determine_submodule_update_strategy(struct repository *r,
1809 int just_cloned,
1810 const char *path,
1811 enum submodule_update_type update,
1812 struct submodule_update_strategy *out)
1814 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1815 char *key;
1816 const char *val;
1818 key = xstrfmt("submodule.%s.update", sub->name);
1820 if (update) {
1821 out->type = update;
1822 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1823 if (parse_submodule_update_strategy(val, out) < 0)
1824 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1825 val, path);
1826 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1827 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1828 BUG("how did we read update = !command from .gitmodules?");
1829 out->type = sub->update_strategy.type;
1830 out->command = sub->update_strategy.command;
1831 } else
1832 out->type = SM_UPDATE_CHECKOUT;
1834 if (just_cloned &&
1835 (out->type == SM_UPDATE_MERGE ||
1836 out->type == SM_UPDATE_REBASE ||
1837 out->type == SM_UPDATE_NONE))
1838 out->type = SM_UPDATE_CHECKOUT;
1840 free(key);
1843 struct update_clone_data {
1844 const struct submodule *sub;
1845 struct object_id oid;
1846 unsigned just_cloned;
1849 struct submodule_update_clone {
1850 /* index into 'update_data.list', the list of submodules to look into for cloning */
1851 int current;
1853 /* configuration parameters which are passed on to the children */
1854 struct update_data *update_data;
1856 /* to be consumed by update_submodule() */
1857 struct update_clone_data *update_clone;
1858 int update_clone_nr; int update_clone_alloc;
1860 /* If we want to stop as fast as possible and return an error */
1861 unsigned quickstop : 1;
1863 /* failed clones to be retried again */
1864 const struct cache_entry **failed_clones;
1865 int failed_clones_nr, failed_clones_alloc;
1867 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1869 struct update_data {
1870 const char *prefix;
1871 const char *displaypath;
1872 enum submodule_update_type update_default;
1873 struct object_id suboid;
1874 struct string_list references;
1875 struct submodule_update_strategy update_strategy;
1876 struct list_objects_filter_options *filter_options;
1877 struct module_list list;
1878 int depth;
1879 int max_jobs;
1880 int single_branch;
1881 int recommend_shallow;
1882 unsigned int require_init;
1883 unsigned int force;
1884 unsigned int quiet;
1885 unsigned int nofetch;
1886 unsigned int remote;
1887 unsigned int progress;
1888 unsigned int dissociate;
1889 unsigned int init;
1890 unsigned int warn_if_uninitialized;
1891 unsigned int recursive;
1893 /* copied over from update_clone_data */
1894 struct object_id oid;
1895 unsigned int just_cloned;
1896 const char *sm_path;
1898 #define UPDATE_DATA_INIT { \
1899 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1900 .list = MODULE_LIST_INIT, \
1901 .recommend_shallow = -1, \
1902 .references = STRING_LIST_INIT_DUP, \
1903 .single_branch = -1, \
1904 .max_jobs = 1, \
1907 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1908 struct strbuf *out, const char *displaypath)
1911 * Only mention uninitialized submodules when their
1912 * paths have been specified.
1914 if (suc->update_data->warn_if_uninitialized) {
1915 strbuf_addf(out,
1916 _("Submodule path '%s' not initialized"),
1917 displaypath);
1918 strbuf_addch(out, '\n');
1919 strbuf_addstr(out,
1920 _("Maybe you want to use 'update --init'?"));
1921 strbuf_addch(out, '\n');
1926 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1927 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1929 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1930 struct child_process *child,
1931 struct submodule_update_clone *suc,
1932 struct strbuf *out)
1934 const struct submodule *sub = NULL;
1935 const char *url = NULL;
1936 const char *update_string;
1937 enum submodule_update_type update_type;
1938 char *key;
1939 struct update_data *ud = suc->update_data;
1940 char *displaypath = get_submodule_displaypath(ce->name, ud->prefix);
1941 struct strbuf sb = STRBUF_INIT;
1942 int needs_cloning = 0;
1943 int need_free_url = 0;
1945 if (ce_stage(ce)) {
1946 strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
1947 strbuf_addch(out, '\n');
1948 goto cleanup;
1951 sub = submodule_from_path(the_repository, null_oid(), ce->name);
1953 if (!sub) {
1954 next_submodule_warn_missing(suc, out, displaypath);
1955 goto cleanup;
1958 key = xstrfmt("submodule.%s.update", sub->name);
1959 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
1960 update_type = parse_submodule_update_type(update_string);
1961 } else {
1962 update_type = sub->update_strategy.type;
1964 free(key);
1966 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
1967 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
1968 && update_type == SM_UPDATE_NONE)) {
1969 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1970 strbuf_addch(out, '\n');
1971 goto cleanup;
1974 /* Check if the submodule has been initialized. */
1975 if (!is_submodule_active(the_repository, ce->name)) {
1976 next_submodule_warn_missing(suc, out, displaypath);
1977 goto cleanup;
1980 strbuf_reset(&sb);
1981 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1982 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
1983 if (starts_with_dot_slash(sub->url) ||
1984 starts_with_dot_dot_slash(sub->url)) {
1985 url = resolve_relative_url(sub->url, NULL, 0);
1986 need_free_url = 1;
1987 } else
1988 url = sub->url;
1991 strbuf_reset(&sb);
1992 strbuf_addf(&sb, "%s/.git", ce->name);
1993 needs_cloning = !file_exists(sb.buf);
1995 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
1996 suc->update_clone_alloc);
1997 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
1998 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
1999 suc->update_clone[suc->update_clone_nr].sub = sub;
2000 suc->update_clone_nr++;
2002 if (!needs_cloning)
2003 goto cleanup;
2005 child->git_cmd = 1;
2006 child->no_stdin = 1;
2007 child->stdout_to_stderr = 1;
2008 child->err = -1;
2009 strvec_push(&child->args, "submodule--helper");
2010 strvec_push(&child->args, "clone");
2011 if (suc->update_data->progress)
2012 strvec_push(&child->args, "--progress");
2013 if (suc->update_data->quiet)
2014 strvec_push(&child->args, "--quiet");
2015 if (suc->update_data->prefix)
2016 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2017 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2018 strvec_push(&child->args, "--depth=1");
2019 else if (suc->update_data->depth)
2020 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2021 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2022 strvec_pushf(&child->args, "--filter=%s",
2023 expand_list_objects_filter_spec(suc->update_data->filter_options));
2024 if (suc->update_data->require_init)
2025 strvec_push(&child->args, "--require-init");
2026 strvec_pushl(&child->args, "--path", sub->path, NULL);
2027 strvec_pushl(&child->args, "--name", sub->name, NULL);
2028 strvec_pushl(&child->args, "--url", url, NULL);
2029 if (suc->update_data->references.nr) {
2030 struct string_list_item *item;
2031 for_each_string_list_item(item, &suc->update_data->references)
2032 strvec_pushl(&child->args, "--reference", item->string, NULL);
2034 if (suc->update_data->dissociate)
2035 strvec_push(&child->args, "--dissociate");
2036 if (suc->update_data->single_branch >= 0)
2037 strvec_push(&child->args, suc->update_data->single_branch ?
2038 "--single-branch" :
2039 "--no-single-branch");
2041 cleanup:
2042 free(displaypath);
2043 strbuf_release(&sb);
2044 if (need_free_url)
2045 free((void*)url);
2047 return needs_cloning;
2050 static int update_clone_get_next_task(struct child_process *child,
2051 struct strbuf *err,
2052 void *suc_cb,
2053 void **idx_task_cb)
2055 struct submodule_update_clone *suc = suc_cb;
2056 const struct cache_entry *ce;
2057 int index;
2059 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2060 ce = suc->update_data->list.entries[suc->current];
2061 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2062 int *p = xmalloc(sizeof(*p));
2063 *p = suc->current;
2064 *idx_task_cb = p;
2065 suc->current++;
2066 return 1;
2071 * The loop above tried cloning each submodule once, now try the
2072 * stragglers again, which we can imagine as an extension of the
2073 * entry list.
2075 index = suc->current - suc->update_data->list.nr;
2076 if (index < suc->failed_clones_nr) {
2077 int *p;
2078 ce = suc->failed_clones[index];
2079 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2080 suc->current ++;
2081 strbuf_addstr(err, "BUG: submodule considered for "
2082 "cloning, doesn't need cloning "
2083 "any more?\n");
2084 return 0;
2086 p = xmalloc(sizeof(*p));
2087 *p = suc->current;
2088 *idx_task_cb = p;
2089 suc->current ++;
2090 return 1;
2093 return 0;
2096 static int update_clone_start_failure(struct strbuf *err,
2097 void *suc_cb,
2098 void *idx_task_cb)
2100 struct submodule_update_clone *suc = suc_cb;
2101 suc->quickstop = 1;
2102 return 1;
2105 static int update_clone_task_finished(int result,
2106 struct strbuf *err,
2107 void *suc_cb,
2108 void *idx_task_cb)
2110 const struct cache_entry *ce;
2111 struct submodule_update_clone *suc = suc_cb;
2113 int *idxP = idx_task_cb;
2114 int idx = *idxP;
2115 free(idxP);
2117 if (!result)
2118 return 0;
2120 if (idx < suc->update_data->list.nr) {
2121 ce = suc->update_data->list.entries[idx];
2122 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2123 ce->name);
2124 strbuf_addch(err, '\n');
2125 ALLOC_GROW(suc->failed_clones,
2126 suc->failed_clones_nr + 1,
2127 suc->failed_clones_alloc);
2128 suc->failed_clones[suc->failed_clones_nr++] = ce;
2129 return 0;
2130 } else {
2131 idx -= suc->update_data->list.nr;
2132 ce = suc->failed_clones[idx];
2133 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2134 ce->name);
2135 strbuf_addch(err, '\n');
2136 suc->quickstop = 1;
2137 return 1;
2140 return 0;
2143 static int git_update_clone_config(const char *var, const char *value,
2144 void *cb)
2146 int *max_jobs = cb;
2147 if (!strcmp(var, "submodule.fetchjobs"))
2148 *max_jobs = parse_submodule_fetchjobs(var, value);
2149 return 0;
2152 static int is_tip_reachable(const char *path, struct object_id *oid)
2154 struct child_process cp = CHILD_PROCESS_INIT;
2155 struct strbuf rev = STRBUF_INIT;
2156 char *hex = oid_to_hex(oid);
2158 cp.git_cmd = 1;
2159 cp.dir = xstrdup(path);
2160 cp.no_stderr = 1;
2161 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2163 prepare_submodule_repo_env(&cp.env);
2165 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2166 return 0;
2168 return 1;
2171 static int fetch_in_submodule(const char *module_path, int depth, int quiet, struct object_id *oid)
2173 struct child_process cp = CHILD_PROCESS_INIT;
2175 prepare_submodule_repo_env(&cp.env);
2176 cp.git_cmd = 1;
2177 cp.dir = xstrdup(module_path);
2179 strvec_push(&cp.args, "fetch");
2180 if (quiet)
2181 strvec_push(&cp.args, "--quiet");
2182 if (depth)
2183 strvec_pushf(&cp.args, "--depth=%d", depth);
2184 if (oid) {
2185 char *hex = oid_to_hex(oid);
2186 char *remote = get_default_remote();
2187 strvec_pushl(&cp.args, remote, hex, NULL);
2188 free(remote);
2191 return run_command(&cp);
2194 static int run_update_command(struct update_data *ud, int subforce)
2196 struct child_process cp = CHILD_PROCESS_INIT;
2197 char *oid = oid_to_hex(&ud->oid);
2198 int must_die_on_failure = 0;
2200 switch (ud->update_strategy.type) {
2201 case SM_UPDATE_CHECKOUT:
2202 cp.git_cmd = 1;
2203 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2204 if (subforce)
2205 strvec_push(&cp.args, "-f");
2206 break;
2207 case SM_UPDATE_REBASE:
2208 cp.git_cmd = 1;
2209 strvec_push(&cp.args, "rebase");
2210 if (ud->quiet)
2211 strvec_push(&cp.args, "--quiet");
2212 must_die_on_failure = 1;
2213 break;
2214 case SM_UPDATE_MERGE:
2215 cp.git_cmd = 1;
2216 strvec_push(&cp.args, "merge");
2217 if (ud->quiet)
2218 strvec_push(&cp.args, "--quiet");
2219 must_die_on_failure = 1;
2220 break;
2221 case SM_UPDATE_COMMAND:
2222 cp.use_shell = 1;
2223 strvec_push(&cp.args, ud->update_strategy.command);
2224 must_die_on_failure = 1;
2225 break;
2226 default:
2227 BUG("unexpected update strategy type: %s",
2228 submodule_strategy_to_string(&ud->update_strategy));
2230 strvec_push(&cp.args, oid);
2232 cp.dir = xstrdup(ud->sm_path);
2233 prepare_submodule_repo_env(&cp.env);
2234 if (run_command(&cp)) {
2235 switch (ud->update_strategy.type) {
2236 case SM_UPDATE_CHECKOUT:
2237 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2238 oid, ud->displaypath);
2239 break;
2240 case SM_UPDATE_REBASE:
2241 die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2242 oid, ud->displaypath);
2243 break;
2244 case SM_UPDATE_MERGE:
2245 die_message(_("Unable to merge '%s' in submodule path '%s'"),
2246 oid, ud->displaypath);
2247 break;
2248 case SM_UPDATE_COMMAND:
2249 die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2250 ud->update_strategy.command, oid, ud->displaypath);
2251 break;
2252 default:
2253 BUG("unexpected update strategy type: %s",
2254 submodule_strategy_to_string(&ud->update_strategy));
2256 if (must_die_on_failure)
2257 exit(128);
2259 /* the command failed, but update must continue */
2260 return 1;
2263 if (ud->quiet)
2264 return 0;
2266 switch (ud->update_strategy.type) {
2267 case SM_UPDATE_CHECKOUT:
2268 printf(_("Submodule path '%s': checked out '%s'\n"),
2269 ud->displaypath, oid);
2270 break;
2271 case SM_UPDATE_REBASE:
2272 printf(_("Submodule path '%s': rebased into '%s'\n"),
2273 ud->displaypath, oid);
2274 break;
2275 case SM_UPDATE_MERGE:
2276 printf(_("Submodule path '%s': merged in '%s'\n"),
2277 ud->displaypath, oid);
2278 break;
2279 case SM_UPDATE_COMMAND:
2280 printf(_("Submodule path '%s': '%s %s'\n"),
2281 ud->displaypath, ud->update_strategy.command, oid);
2282 break;
2283 default:
2284 BUG("unexpected update strategy type: %s",
2285 submodule_strategy_to_string(&ud->update_strategy));
2288 return 0;
2291 static int run_update_procedure(struct update_data *ud)
2293 int subforce = is_null_oid(&ud->suboid) || ud->force;
2295 if (!ud->nofetch) {
2297 * Run fetch only if `oid` isn't present or it
2298 * is not reachable from a ref.
2300 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2301 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2302 !ud->quiet)
2303 fprintf_ln(stderr,
2304 _("Unable to fetch in submodule path '%s'; "
2305 "trying to directly fetch %s:"),
2306 ud->displaypath, oid_to_hex(&ud->oid));
2308 * Now we tried the usual fetch, but `oid` may
2309 * not be reachable from any of the refs.
2311 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2312 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2313 die(_("Fetched in submodule path '%s', but it did not "
2314 "contain %s. Direct fetching of that commit failed."),
2315 ud->displaypath, oid_to_hex(&ud->oid));
2318 return run_update_command(ud, subforce);
2321 static const char *remote_submodule_branch(const char *path)
2323 const struct submodule *sub;
2324 const char *branch = NULL;
2325 char *key;
2327 sub = submodule_from_path(the_repository, null_oid(), path);
2328 if (!sub)
2329 return NULL;
2331 key = xstrfmt("submodule.%s.branch", sub->name);
2332 if (repo_config_get_string_tmp(the_repository, key, &branch))
2333 branch = sub->branch;
2334 free(key);
2336 if (!branch)
2337 return "HEAD";
2339 if (!strcmp(branch, ".")) {
2340 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2342 if (!refname)
2343 die(_("No such ref: %s"), "HEAD");
2345 /* detached HEAD */
2346 if (!strcmp(refname, "HEAD"))
2347 die(_("Submodule (%s) branch configured to inherit "
2348 "branch from superproject, but the superproject "
2349 "is not on any branch"), sub->name);
2351 if (!skip_prefix(refname, "refs/heads/", &refname))
2352 die(_("Expecting a full ref name, got %s"), refname);
2353 return refname;
2356 return branch;
2359 static void ensure_core_worktree(const char *path)
2361 const char *cw;
2362 struct repository subrepo;
2364 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2365 die(_("could not get a repository handle for submodule '%s'"), path);
2367 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2368 char *cfg_file, *abs_path;
2369 const char *rel_path;
2370 struct strbuf sb = STRBUF_INIT;
2372 cfg_file = repo_git_path(&subrepo, "config");
2374 abs_path = absolute_pathdup(path);
2375 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2377 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2379 free(cfg_file);
2380 free(abs_path);
2381 strbuf_release(&sb);
2385 static const char *submodule_update_type_to_label(enum submodule_update_type type)
2387 switch (type) {
2388 case SM_UPDATE_CHECKOUT:
2389 return "checkout";
2390 case SM_UPDATE_MERGE:
2391 return "merge";
2392 case SM_UPDATE_REBASE:
2393 return "rebase";
2394 case SM_UPDATE_UNSPECIFIED:
2395 case SM_UPDATE_NONE:
2396 case SM_UPDATE_COMMAND:
2397 break;
2399 BUG("unreachable with type %d", type);
2402 static void update_data_to_args(struct update_data *update_data, struct strvec *args)
2404 enum submodule_update_type update_type = update_data->update_default;
2406 if (update_data->displaypath) {
2407 strvec_push(args, "--super-prefix");
2408 strvec_pushf(args, "%s/", update_data->displaypath);
2410 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2411 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2412 if (update_data->quiet)
2413 strvec_push(args, "--quiet");
2414 if (update_data->force)
2415 strvec_push(args, "--force");
2416 if (update_data->init)
2417 strvec_push(args, "--init");
2418 if (update_data->remote)
2419 strvec_push(args, "--remote");
2420 if (update_data->nofetch)
2421 strvec_push(args, "--no-fetch");
2422 if (update_data->dissociate)
2423 strvec_push(args, "--dissociate");
2424 if (update_data->progress)
2425 strvec_push(args, "--progress");
2426 if (update_data->require_init)
2427 strvec_push(args, "--require-init");
2428 if (update_data->depth)
2429 strvec_pushf(args, "--depth=%d", update_data->depth);
2430 if (update_type != SM_UPDATE_UNSPECIFIED)
2431 strvec_pushf(args, "--%s",
2432 submodule_update_type_to_label(update_type));
2434 if (update_data->references.nr) {
2435 struct string_list_item *item;
2436 for_each_string_list_item(item, &update_data->references)
2437 strvec_pushl(args, "--reference", item->string, NULL);
2439 if (update_data->filter_options && update_data->filter_options->choice)
2440 strvec_pushf(args, "--filter=%s",
2441 expand_list_objects_filter_spec(
2442 update_data->filter_options));
2443 if (update_data->recommend_shallow == 0)
2444 strvec_push(args, "--no-recommend-shallow");
2445 else if (update_data->recommend_shallow == 1)
2446 strvec_push(args, "--recommend-shallow");
2447 if (update_data->single_branch >= 0)
2448 strvec_push(args, update_data->single_branch ?
2449 "--single-branch" :
2450 "--no-single-branch");
2453 static int update_submodule(struct update_data *update_data)
2455 ensure_core_worktree(update_data->sm_path);
2457 update_data->displaypath = get_submodule_displaypath(
2458 update_data->sm_path, update_data->prefix);
2460 determine_submodule_update_strategy(the_repository, update_data->just_cloned,
2461 update_data->sm_path, update_data->update_default,
2462 &update_data->update_strategy);
2464 if (update_data->just_cloned)
2465 oidcpy(&update_data->suboid, null_oid());
2466 else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2467 die(_("Unable to find current revision in submodule path '%s'"),
2468 update_data->displaypath);
2470 if (update_data->remote) {
2471 char *remote_name = get_default_remote_submodule(update_data->sm_path);
2472 const char *branch = remote_submodule_branch(update_data->sm_path);
2473 char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2475 if (!update_data->nofetch) {
2476 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2477 0, NULL))
2478 die(_("Unable to fetch in submodule path '%s'"),
2479 update_data->sm_path);
2482 if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2483 die(_("Unable to find %s revision in submodule path '%s'"),
2484 remote_ref, update_data->sm_path);
2486 free(remote_ref);
2489 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force)
2490 if (run_update_procedure(update_data))
2491 return 1;
2493 if (update_data->recursive) {
2494 struct child_process cp = CHILD_PROCESS_INIT;
2495 struct update_data next = *update_data;
2496 int res;
2498 next.prefix = NULL;
2499 oidcpy(&next.oid, null_oid());
2500 oidcpy(&next.suboid, null_oid());
2502 cp.dir = update_data->sm_path;
2503 cp.git_cmd = 1;
2504 prepare_submodule_repo_env(&cp.env);
2505 update_data_to_args(&next, &cp.args);
2507 /* die() if child process die()'d */
2508 res = run_command(&cp);
2509 if (!res)
2510 return 0;
2511 die_message(_("Failed to recurse into submodule path '%s'"),
2512 update_data->displaypath);
2513 if (res == 128)
2514 exit(res);
2515 else if (res)
2516 return 1;
2519 return 0;
2522 static int update_submodules(struct update_data *update_data)
2524 int i, res = 0;
2525 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2527 suc.update_data = update_data;
2528 run_processes_parallel_tr2(suc.update_data->max_jobs, update_clone_get_next_task,
2529 update_clone_start_failure,
2530 update_clone_task_finished, &suc, "submodule",
2531 "parallel/update");
2534 * We saved the output and put it out all at once now.
2535 * That means:
2536 * - the listener does not have to interleave their (checkout)
2537 * work with our fetching. The writes involved in a
2538 * checkout involve more straightforward sequential I/O.
2539 * - the listener can avoid doing any work if fetching failed.
2541 if (suc.quickstop) {
2542 res = 1;
2543 goto cleanup;
2546 for (i = 0; i < suc.update_clone_nr; i++) {
2547 struct update_clone_data ucd = suc.update_clone[i];
2549 oidcpy(&update_data->oid, &ucd.oid);
2550 update_data->just_cloned = ucd.just_cloned;
2551 update_data->sm_path = ucd.sub->path;
2553 if (update_submodule(update_data))
2554 res = 1;
2557 cleanup:
2558 string_list_clear(&update_data->references, 0);
2559 return res;
2562 static int module_update(int argc, const char **argv, const char *prefix)
2564 struct pathspec pathspec;
2565 struct update_data opt = UPDATE_DATA_INIT;
2566 struct list_objects_filter_options filter_options;
2567 int ret;
2569 struct option module_update_options[] = {
2570 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2571 OPT_BOOL(0, "init", &opt.init,
2572 N_("initialize uninitialized submodules before update")),
2573 OPT_BOOL(0, "remote", &opt.remote,
2574 N_("use SHA-1 of submodule's remote tracking branch")),
2575 OPT_BOOL(0, "recursive", &opt.recursive,
2576 N_("traverse submodules recursively")),
2577 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2578 N_("don't fetch new objects from the remote site")),
2579 OPT_STRING(0, "prefix", &opt.prefix,
2580 N_("path"),
2581 N_("path into the working tree")),
2582 OPT_SET_INT(0, "checkout", &opt.update_default,
2583 N_("use the 'checkout' update strategy (default)"),
2584 SM_UPDATE_CHECKOUT),
2585 OPT_SET_INT('m', "merge", &opt.update_default,
2586 N_("use the 'merge' update strategy"),
2587 SM_UPDATE_MERGE),
2588 OPT_SET_INT('r', "rebase", &opt.update_default,
2589 N_("use the 'rebase' update strategy"),
2590 SM_UPDATE_REBASE),
2591 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2592 N_("reference repository")),
2593 OPT_BOOL(0, "dissociate", &opt.dissociate,
2594 N_("use --reference only while cloning")),
2595 OPT_INTEGER(0, "depth", &opt.depth,
2596 N_("create a shallow clone truncated to the "
2597 "specified number of revisions")),
2598 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2599 N_("parallel jobs")),
2600 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2601 N_("whether the initial clone should follow the shallow recommendation")),
2602 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2603 OPT_BOOL(0, "progress", &opt.progress,
2604 N_("force cloning progress")),
2605 OPT_BOOL(0, "require-init", &opt.require_init,
2606 N_("disallow cloning into non-empty directory, implies --init")),
2607 OPT_BOOL(0, "single-branch", &opt.single_branch,
2608 N_("clone only one branch, HEAD or --branch")),
2609 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2610 OPT_END()
2613 const char *const git_submodule_helper_usage[] = {
2614 N_("git submodule [--quiet] update"
2615 " [--init [--filter=<filter-spec>]] [--remote]"
2616 " [-N|--no-fetch] [-f|--force]"
2617 " [--checkout|--merge|--rebase]"
2618 " [--[no-]recommend-shallow] [--reference <repository>]"
2619 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2620 NULL
2623 update_clone_config_from_gitmodules(&opt.max_jobs);
2624 git_config(git_update_clone_config, &opt.max_jobs);
2626 memset(&filter_options, 0, sizeof(filter_options));
2627 argc = parse_options(argc, argv, prefix, module_update_options,
2628 git_submodule_helper_usage, 0);
2630 if (opt.require_init)
2631 opt.init = 1;
2633 if (filter_options.choice && !opt.init) {
2634 usage_with_options(git_submodule_helper_usage,
2635 module_update_options);
2638 opt.filter_options = &filter_options;
2640 if (opt.update_default)
2641 opt.update_strategy.type = opt.update_default;
2643 if (module_list_compute(argc, argv, prefix, &pathspec, &opt.list) < 0) {
2644 list_objects_filter_release(&filter_options);
2645 return 1;
2648 if (pathspec.nr)
2649 opt.warn_if_uninitialized = 1;
2651 if (opt.init) {
2652 struct module_list list = MODULE_LIST_INIT;
2653 struct init_cb info = INIT_CB_INIT;
2655 if (module_list_compute(argc, argv, opt.prefix,
2656 &pathspec, &list) < 0)
2657 return 1;
2660 * If there are no path args and submodule.active is set then,
2661 * by default, only initialize 'active' modules.
2663 if (!argc && git_config_get_value_multi("submodule.active"))
2664 module_list_active(&list);
2666 info.prefix = opt.prefix;
2667 if (opt.quiet)
2668 info.flags |= OPT_QUIET;
2670 for_each_listed_submodule(&list, init_submodule_cb, &info);
2673 ret = update_submodules(&opt);
2674 list_objects_filter_release(&filter_options);
2675 return ret;
2678 static int push_check(int argc, const char **argv, const char *prefix)
2680 struct remote *remote;
2681 const char *superproject_head;
2682 char *head;
2683 int detached_head = 0;
2684 struct object_id head_oid;
2686 if (argc < 3)
2687 die("submodule--helper push-check requires at least 2 arguments");
2690 * superproject's resolved head ref.
2691 * if HEAD then the superproject is in a detached head state, otherwise
2692 * it will be the resolved head ref.
2694 superproject_head = argv[1];
2695 argv++;
2696 argc--;
2697 /* Get the submodule's head ref and determine if it is detached */
2698 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2699 if (!head)
2700 die(_("Failed to resolve HEAD as a valid ref."));
2701 if (!strcmp(head, "HEAD"))
2702 detached_head = 1;
2705 * The remote must be configured.
2706 * This is to avoid pushing to the exact same URL as the parent.
2708 remote = pushremote_get(argv[1]);
2709 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2710 die("remote '%s' not configured", argv[1]);
2712 /* Check the refspec */
2713 if (argc > 2) {
2714 int i;
2715 struct ref *local_refs = get_local_heads();
2716 struct refspec refspec = REFSPEC_INIT_PUSH;
2718 refspec_appendn(&refspec, argv + 2, argc - 2);
2720 for (i = 0; i < refspec.nr; i++) {
2721 const struct refspec_item *rs = &refspec.items[i];
2723 if (rs->pattern || rs->matching)
2724 continue;
2726 /* LHS must match a single ref */
2727 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2728 case 1:
2729 break;
2730 case 0:
2732 * If LHS matches 'HEAD' then we need to ensure
2733 * that it matches the same named branch
2734 * checked out in the superproject.
2736 if (!strcmp(rs->src, "HEAD")) {
2737 if (!detached_head &&
2738 !strcmp(head, superproject_head))
2739 break;
2740 die("HEAD does not match the named branch in the superproject");
2742 /* fallthrough */
2743 default:
2744 die("src refspec '%s' must name a ref",
2745 rs->src);
2748 refspec_clear(&refspec);
2750 free(head);
2752 return 0;
2755 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2757 int i;
2758 struct pathspec pathspec;
2759 struct module_list list = MODULE_LIST_INIT;
2760 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2762 struct option embed_gitdir_options[] = {
2763 OPT_STRING(0, "prefix", &prefix,
2764 N_("path"),
2765 N_("path into the working tree")),
2766 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2767 ABSORB_GITDIR_RECURSE_SUBMODULES),
2768 OPT_END()
2771 const char *const git_submodule_helper_usage[] = {
2772 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2773 NULL
2776 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2777 git_submodule_helper_usage, 0);
2779 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2780 return 1;
2782 for (i = 0; i < list.nr; i++)
2783 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2785 return 0;
2788 static int is_active(int argc, const char **argv, const char *prefix)
2790 if (argc != 2)
2791 die("submodule--helper is-active takes exactly 1 argument");
2793 return !is_submodule_active(the_repository, argv[1]);
2797 * Exit non-zero if any of the submodule names given on the command line is
2798 * invalid. If no names are given, filter stdin to print only valid names
2799 * (which is primarily intended for testing).
2801 static int check_name(int argc, const char **argv, const char *prefix)
2803 if (argc > 1) {
2804 while (*++argv) {
2805 if (check_submodule_name(*argv) < 0)
2806 return 1;
2808 } else {
2809 struct strbuf buf = STRBUF_INIT;
2810 while (strbuf_getline(&buf, stdin) != EOF) {
2811 if (!check_submodule_name(buf.buf))
2812 printf("%s\n", buf.buf);
2814 strbuf_release(&buf);
2816 return 0;
2819 static int module_config(int argc, const char **argv, const char *prefix)
2821 enum {
2822 CHECK_WRITEABLE = 1,
2823 DO_UNSET = 2
2824 } command = 0;
2826 struct option module_config_options[] = {
2827 OPT_CMDMODE(0, "check-writeable", &command,
2828 N_("check if it is safe to write to the .gitmodules file"),
2829 CHECK_WRITEABLE),
2830 OPT_CMDMODE(0, "unset", &command,
2831 N_("unset the config in the .gitmodules file"),
2832 DO_UNSET),
2833 OPT_END()
2835 const char *const git_submodule_helper_usage[] = {
2836 N_("git submodule--helper config <name> [<value>]"),
2837 N_("git submodule--helper config --unset <name>"),
2838 "git submodule--helper config --check-writeable",
2839 NULL
2842 argc = parse_options(argc, argv, prefix, module_config_options,
2843 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2845 if (argc == 1 && command == CHECK_WRITEABLE)
2846 return is_writing_gitmodules_ok() ? 0 : -1;
2848 /* Equivalent to ACTION_GET in builtin/config.c */
2849 if (argc == 2 && command != DO_UNSET)
2850 return print_config_from_gitmodules(the_repository, argv[1]);
2852 /* Equivalent to ACTION_SET in builtin/config.c */
2853 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2854 const char *value = (argc == 3) ? argv[2] : NULL;
2856 if (!is_writing_gitmodules_ok())
2857 die(_("please make sure that the .gitmodules file is in the working tree"));
2859 return config_set_in_gitmodules_file_gently(argv[1], value);
2862 usage_with_options(git_submodule_helper_usage, module_config_options);
2865 static int module_set_url(int argc, const char **argv, const char *prefix)
2867 int quiet = 0;
2868 const char *newurl;
2869 const char *path;
2870 char *config_name;
2872 struct option options[] = {
2873 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2874 OPT_END()
2876 const char *const usage[] = {
2877 N_("git submodule set-url [--quiet] <path> <newurl>"),
2878 NULL
2881 argc = parse_options(argc, argv, prefix, options, usage, 0);
2883 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2884 usage_with_options(usage, options);
2886 config_name = xstrfmt("submodule.%s.url", path);
2888 config_set_in_gitmodules_file_gently(config_name, newurl);
2889 sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
2891 free(config_name);
2893 return 0;
2896 static int module_set_branch(int argc, const char **argv, const char *prefix)
2898 int opt_default = 0, ret;
2899 const char *opt_branch = NULL;
2900 const char *path;
2901 char *config_name;
2904 * We accept the `quiet` option for uniformity across subcommands,
2905 * though there is nothing to make less verbose in this subcommand.
2907 struct option options[] = {
2908 OPT_NOOP_NOARG('q', "quiet"),
2909 OPT_BOOL('d', "default", &opt_default,
2910 N_("set the default tracking branch to master")),
2911 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2912 N_("set the default tracking branch")),
2913 OPT_END()
2915 const char *const usage[] = {
2916 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2917 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2918 NULL
2921 argc = parse_options(argc, argv, prefix, options, usage, 0);
2923 if (!opt_branch && !opt_default)
2924 die(_("--branch or --default required"));
2926 if (opt_branch && opt_default)
2927 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2929 if (argc != 1 || !(path = argv[0]))
2930 usage_with_options(usage, options);
2932 config_name = xstrfmt("submodule.%s.branch", path);
2933 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2935 free(config_name);
2936 return !!ret;
2939 static int module_create_branch(int argc, const char **argv, const char *prefix)
2941 enum branch_track track;
2942 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
2944 struct option options[] = {
2945 OPT__QUIET(&quiet, N_("print only error messages")),
2946 OPT__FORCE(&force, N_("force creation"), 0),
2947 OPT_BOOL(0, "create-reflog", &reflog,
2948 N_("create the branch's reflog")),
2949 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
2950 N_("set branch tracking configuration"),
2951 PARSE_OPT_OPTARG,
2952 parse_opt_tracking_mode),
2953 OPT__DRY_RUN(&dry_run,
2954 N_("show whether the branch would be created")),
2955 OPT_END()
2957 const char *const usage[] = {
2958 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2959 NULL
2962 git_config(git_default_config, NULL);
2963 track = git_branch_track;
2964 argc = parse_options(argc, argv, prefix, options, usage, 0);
2966 if (argc != 3)
2967 usage_with_options(usage, options);
2969 if (!quiet && !dry_run)
2970 printf_ln(_("creating branch '%s'"), argv[0]);
2972 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
2973 force, reflog, quiet, track, dry_run);
2974 return 0;
2977 struct add_data {
2978 const char *prefix;
2979 const char *branch;
2980 const char *reference_path;
2981 char *sm_path;
2982 const char *sm_name;
2983 const char *repo;
2984 const char *realrepo;
2985 int depth;
2986 unsigned int force: 1;
2987 unsigned int quiet: 1;
2988 unsigned int progress: 1;
2989 unsigned int dissociate: 1;
2991 #define ADD_DATA_INIT { .depth = -1 }
2993 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
2995 struct child_process cp_remote = CHILD_PROCESS_INIT;
2996 struct strbuf sb_remote_out = STRBUF_INIT;
2998 cp_remote.git_cmd = 1;
2999 strvec_pushf(&cp_remote.env,
3000 "GIT_DIR=%s", git_dir_path);
3001 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3002 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3003 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3004 char *next_line;
3005 char *line = sb_remote_out.buf;
3006 while ((next_line = strchr(line, '\n')) != NULL) {
3007 size_t len = next_line - line;
3008 if (strip_suffix_mem(line, &len, " (fetch)"))
3009 strbuf_addf(msg, " %.*s\n", (int)len, line);
3010 line = next_line + 1;
3014 strbuf_release(&sb_remote_out);
3017 static int add_submodule(const struct add_data *add_data)
3019 char *submod_gitdir_path;
3020 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3022 /* perhaps the path already exists and is already a git repo, else clone it */
3023 if (is_directory(add_data->sm_path)) {
3024 struct strbuf sm_path = STRBUF_INIT;
3025 strbuf_addstr(&sm_path, add_data->sm_path);
3026 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3027 if (is_nonbare_repository_dir(&sm_path))
3028 printf(_("Adding existing repo at '%s' to the index\n"),
3029 add_data->sm_path);
3030 else
3031 die(_("'%s' already exists and is not a valid git repo"),
3032 add_data->sm_path);
3033 strbuf_release(&sm_path);
3034 free(submod_gitdir_path);
3035 } else {
3036 struct child_process cp = CHILD_PROCESS_INIT;
3037 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3039 if (is_directory(submod_gitdir_path)) {
3040 if (!add_data->force) {
3041 struct strbuf msg = STRBUF_INIT;
3042 char *die_msg;
3044 strbuf_addf(&msg, _("A git directory for '%s' is found "
3045 "locally with remote(s):\n"),
3046 add_data->sm_name);
3048 append_fetch_remotes(&msg, submod_gitdir_path);
3049 free(submod_gitdir_path);
3051 strbuf_addf(&msg, _("If you want to reuse this local git "
3052 "directory instead of cloning again from\n"
3053 " %s\n"
3054 "use the '--force' option. If the local git "
3055 "directory is not the correct repo\n"
3056 "or you are unsure what this means choose "
3057 "another name with the '--name' option."),
3058 add_data->realrepo);
3060 die_msg = strbuf_detach(&msg, NULL);
3061 die("%s", die_msg);
3062 } else {
3063 printf(_("Reactivating local git directory for "
3064 "submodule '%s'\n"), add_data->sm_name);
3067 free(submod_gitdir_path);
3069 clone_data.prefix = add_data->prefix;
3070 clone_data.path = add_data->sm_path;
3071 clone_data.name = add_data->sm_name;
3072 clone_data.url = add_data->realrepo;
3073 clone_data.quiet = add_data->quiet;
3074 clone_data.progress = add_data->progress;
3075 if (add_data->reference_path)
3076 string_list_append(&clone_data.reference,
3077 xstrdup(add_data->reference_path));
3078 clone_data.dissociate = add_data->dissociate;
3079 if (add_data->depth >= 0)
3080 clone_data.depth = xstrfmt("%d", add_data->depth);
3082 if (clone_submodule(&clone_data))
3083 return -1;
3085 prepare_submodule_repo_env(&cp.env);
3086 cp.git_cmd = 1;
3087 cp.dir = add_data->sm_path;
3089 * NOTE: we only get here if add_data->force is true, so
3090 * passing --force to checkout is reasonable.
3092 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3094 if (add_data->branch) {
3095 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3096 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3099 if (run_command(&cp))
3100 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3102 return 0;
3105 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3107 char *key;
3108 int ret;
3110 if (!is_writing_gitmodules_ok())
3111 die(_("please make sure that the .gitmodules file is in the working tree"));
3113 key = xstrfmt("submodule.%s.%s", name, var);
3114 ret = config_set_in_gitmodules_file_gently(key, value);
3115 free(key);
3117 return ret;
3120 static void configure_added_submodule(struct add_data *add_data)
3122 char *key;
3123 char *val = NULL;
3124 struct child_process add_submod = CHILD_PROCESS_INIT;
3125 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3127 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3128 git_config_set_gently(key, add_data->realrepo);
3129 free(key);
3131 add_submod.git_cmd = 1;
3132 strvec_pushl(&add_submod.args, "add",
3133 "--no-warn-embedded-repo", NULL);
3134 if (add_data->force)
3135 strvec_push(&add_submod.args, "--force");
3136 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3138 if (run_command(&add_submod))
3139 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3141 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3142 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3143 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3145 if (add_data->branch) {
3146 if (config_submodule_in_gitmodules(add_data->sm_name,
3147 "branch", add_data->branch))
3148 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3151 add_gitmodules.git_cmd = 1;
3152 strvec_pushl(&add_gitmodules.args,
3153 "add", "--force", "--", ".gitmodules", NULL);
3155 if (run_command(&add_gitmodules))
3156 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3159 * NEEDSWORK: In a multi-working-tree world this needs to be
3160 * set in the per-worktree config.
3163 * NEEDSWORK: In the longer run, we need to get rid of this
3164 * pattern of querying "submodule.active" before calling
3165 * is_submodule_active(), since that function needs to find
3166 * out the value of "submodule.active" again anyway.
3168 if (!git_config_get_string("submodule.active", &val) && val) {
3170 * If the submodule being added isn't already covered by the
3171 * current configured pathspec, set the submodule's active flag
3173 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3174 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3175 git_config_set_gently(key, "true");
3176 free(key);
3178 } else {
3179 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3180 git_config_set_gently(key, "true");
3181 free(key);
3185 static void die_on_index_match(const char *path, int force)
3187 struct pathspec ps;
3188 const char *args[] = { path, NULL };
3189 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3191 if (read_cache_preload(NULL) < 0)
3192 die(_("index file corrupt"));
3194 if (ps.nr) {
3195 int i;
3196 char *ps_matched = xcalloc(ps.nr, 1);
3198 /* TODO: audit for interaction with sparse-index. */
3199 ensure_full_index(&the_index);
3202 * Since there is only one pathspec, we just need
3203 * need to check ps_matched[0] to know if a cache
3204 * entry matched.
3206 for (i = 0; i < active_nr; i++) {
3207 ce_path_match(&the_index, active_cache[i], &ps,
3208 ps_matched);
3210 if (ps_matched[0]) {
3211 if (!force)
3212 die(_("'%s' already exists in the index"),
3213 path);
3214 if (!S_ISGITLINK(active_cache[i]->ce_mode))
3215 die(_("'%s' already exists in the index "
3216 "and is not a submodule"), path);
3217 break;
3220 free(ps_matched);
3222 clear_pathspec(&ps);
3225 static void die_on_repo_without_commits(const char *path)
3227 struct strbuf sb = STRBUF_INIT;
3228 strbuf_addstr(&sb, path);
3229 if (is_nonbare_repository_dir(&sb)) {
3230 struct object_id oid;
3231 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3232 die(_("'%s' does not have a commit checked out"), path);
3234 strbuf_release(&sb);
3237 static int module_add(int argc, const char **argv, const char *prefix)
3239 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3240 struct add_data add_data = ADD_DATA_INIT;
3241 char *to_free = NULL;
3243 struct option options[] = {
3244 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3245 N_("branch of repository to add as submodule")),
3246 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3247 PARSE_OPT_NOCOMPLETE),
3248 OPT__QUIET(&quiet, N_("print only error messages")),
3249 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3250 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3251 N_("reference repository")),
3252 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3253 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3254 N_("sets the submodule's name to the given string "
3255 "instead of defaulting to its path")),
3256 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3257 OPT_END()
3260 const char *const usage[] = {
3261 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3262 NULL
3265 argc = parse_options(argc, argv, prefix, options, usage, 0);
3267 if (!is_writing_gitmodules_ok())
3268 die(_("please make sure that the .gitmodules file is in the working tree"));
3270 if (prefix && *prefix &&
3271 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3272 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3274 if (argc == 0 || argc > 2)
3275 usage_with_options(usage, options);
3277 add_data.repo = argv[0];
3278 if (argc == 1)
3279 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3280 else
3281 add_data.sm_path = xstrdup(argv[1]);
3283 if (prefix && *prefix && !is_absolute_path(add_data.sm_path))
3284 add_data.sm_path = xstrfmt("%s%s", prefix, add_data.sm_path);
3286 if (starts_with_dot_dot_slash(add_data.repo) ||
3287 starts_with_dot_slash(add_data.repo)) {
3288 if (prefix)
3289 die(_("Relative path can only be used from the toplevel "
3290 "of the working tree"));
3292 /* dereference source url relative to parent's url */
3293 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3294 add_data.realrepo = to_free;
3295 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3296 add_data.realrepo = add_data.repo;
3297 } else {
3298 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3299 add_data.repo);
3303 * normalize path:
3304 * multiple //; leading ./; /./; /../;
3306 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3307 strip_dir_trailing_slashes(add_data.sm_path);
3309 die_on_index_match(add_data.sm_path, force);
3310 die_on_repo_without_commits(add_data.sm_path);
3312 if (!force) {
3313 int exit_code = -1;
3314 struct strbuf sb = STRBUF_INIT;
3315 struct child_process cp = CHILD_PROCESS_INIT;
3316 cp.git_cmd = 1;
3317 cp.no_stdout = 1;
3318 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3319 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3320 if ((exit_code = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3321 strbuf_complete_line(&sb);
3322 fputs(sb.buf, stderr);
3323 free(add_data.sm_path);
3324 return exit_code;
3326 strbuf_release(&sb);
3329 if(!add_data.sm_name)
3330 add_data.sm_name = add_data.sm_path;
3332 if (check_submodule_name(add_data.sm_name))
3333 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3335 add_data.prefix = prefix;
3336 add_data.force = !!force;
3337 add_data.quiet = !!quiet;
3338 add_data.progress = !!progress;
3339 add_data.dissociate = !!dissociate;
3341 if (add_submodule(&add_data)) {
3342 free(add_data.sm_path);
3343 return 1;
3345 configure_added_submodule(&add_data);
3346 free(add_data.sm_path);
3347 free(to_free);
3349 return 0;
3352 #define SUPPORT_SUPER_PREFIX (1<<0)
3354 struct cmd_struct {
3355 const char *cmd;
3356 int (*fn)(int, const char **, const char *);
3357 unsigned option;
3360 static struct cmd_struct commands[] = {
3361 {"list", module_list, 0},
3362 {"name", module_name, 0},
3363 {"clone", module_clone, SUPPORT_SUPER_PREFIX},
3364 {"add", module_add, 0},
3365 {"update", module_update, SUPPORT_SUPER_PREFIX},
3366 {"resolve-relative-url-test", resolve_relative_url_test, 0},
3367 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
3368 {"init", module_init, 0},
3369 {"status", module_status, SUPPORT_SUPER_PREFIX},
3370 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
3371 {"deinit", module_deinit, 0},
3372 {"summary", module_summary, 0},
3373 {"push-check", push_check, 0},
3374 {"absorbgitdirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3375 {"is-active", is_active, 0},
3376 {"check-name", check_name, 0},
3377 {"config", module_config, 0},
3378 {"set-url", module_set_url, 0},
3379 {"set-branch", module_set_branch, 0},
3380 {"create-branch", module_create_branch, 0},
3383 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3385 int i;
3386 if (argc < 2 || !strcmp(argv[1], "-h"))
3387 usage("git submodule--helper <command>");
3389 for (i = 0; i < ARRAY_SIZE(commands); i++) {
3390 if (!strcmp(argv[1], commands[i].cmd)) {
3391 if (get_super_prefix() &&
3392 !(commands[i].option & SUPPORT_SUPER_PREFIX))
3393 die(_("%s doesn't support --super-prefix"),
3394 commands[i].cmd);
3395 return commands[i].fn(argc - 1, argv + 1, prefix);
3399 die(_("'%s' is not a valid submodule--helper "
3400 "subcommand"), argv[1]);