Merge branch 'ab/submodule-helper-prep'
[git/debian.git] / builtin / submodule--helper.c
blobc4149f11e7f77be6a7b5cf06357208c12d05cfc8
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 int repo_get_default_remote(struct repository *repo, char **default_remote)
36 char *dest = NULL;
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 return die_message(_("No such ref: %s"), "HEAD");
45 /* detached HEAD */
46 if (!strcmp(refname, "HEAD")) {
47 *default_remote = xstrdup("origin");
48 return 0;
51 if (!skip_prefix(refname, "refs/heads/", &refname))
52 return die_message(_("Expecting a full ref name, got %s"),
53 refname);
55 strbuf_addf(&sb, "branch.%s.remote", refname);
56 if (repo_config_get_string(repo, sb.buf, &dest))
57 *default_remote = xstrdup("origin");
58 else
59 *default_remote = dest;
61 strbuf_release(&sb);
62 return 0;
65 static int get_default_remote_submodule(const char *module_path, char **default_remote)
67 struct repository subrepo;
69 if (repo_submodule_init(&subrepo, the_repository, module_path,
70 null_oid()) < 0)
71 return die_message(_("could not get a repository handle for submodule '%s'"),
72 module_path);
73 return repo_get_default_remote(&subrepo, default_remote);
76 static char *get_default_remote(void)
78 char *default_remote;
79 int code = repo_get_default_remote(the_repository, &default_remote);
81 if (code)
82 exit(code);
84 return default_remote;
87 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
89 char *remoteurl, *resolved_url;
90 char *remote = get_default_remote();
91 struct strbuf remotesb = STRBUF_INIT;
93 strbuf_addf(&remotesb, "remote.%s.url", remote);
94 if (git_config_get_string(remotesb.buf, &remoteurl)) {
95 if (!quiet)
96 warning(_("could not look up configuration '%s'. "
97 "Assuming this repository is its own "
98 "authoritative upstream."),
99 remotesb.buf);
100 remoteurl = xgetcwd();
102 resolved_url = relative_url(remoteurl, rel_url, up_path);
104 free(remote);
105 free(remoteurl);
106 strbuf_release(&remotesb);
108 return resolved_url;
111 /* the result should be freed by the caller. */
112 static char *get_submodule_displaypath(const char *path, const char *prefix)
114 const char *super_prefix = get_super_prefix();
116 if (prefix && super_prefix) {
117 BUG("cannot have prefix '%s' and superprefix '%s'",
118 prefix, super_prefix);
119 } else if (prefix) {
120 struct strbuf sb = STRBUF_INIT;
121 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
122 strbuf_release(&sb);
123 return displaypath;
124 } else if (super_prefix) {
125 return xstrfmt("%s%s", super_prefix, path);
126 } else {
127 return xstrdup(path);
131 static char *compute_rev_name(const char *sub_path, const char* object_id)
133 struct strbuf sb = STRBUF_INIT;
134 const char ***d;
136 static const char *describe_bare[] = { NULL };
138 static const char *describe_tags[] = { "--tags", NULL };
140 static const char *describe_contains[] = { "--contains", NULL };
142 static const char *describe_all_always[] = { "--all", "--always", NULL };
144 static const char **describe_argv[] = { describe_bare, describe_tags,
145 describe_contains,
146 describe_all_always, NULL };
148 for (d = describe_argv; *d; d++) {
149 struct child_process cp = CHILD_PROCESS_INIT;
150 prepare_submodule_repo_env(&cp.env);
151 cp.dir = sub_path;
152 cp.git_cmd = 1;
153 cp.no_stderr = 1;
155 strvec_push(&cp.args, "describe");
156 strvec_pushv(&cp.args, *d);
157 strvec_push(&cp.args, object_id);
159 if (!capture_command(&cp, &sb, 0)) {
160 strbuf_strip_suffix(&sb, "\n");
161 return strbuf_detach(&sb, NULL);
165 strbuf_release(&sb);
166 return NULL;
169 struct module_list {
170 const struct cache_entry **entries;
171 int alloc, nr;
173 #define MODULE_LIST_INIT { 0 }
175 static int module_list_compute(int argc, const char **argv,
176 const char *prefix,
177 struct pathspec *pathspec,
178 struct module_list *list)
180 int i, result = 0;
181 char *ps_matched = NULL;
183 parse_pathspec(pathspec, 0,
184 PATHSPEC_PREFER_FULL,
185 prefix, argv);
187 if (pathspec->nr)
188 ps_matched = xcalloc(pathspec->nr, 1);
190 if (read_cache() < 0)
191 die(_("index file corrupt"));
193 for (i = 0; i < active_nr; i++) {
194 const struct cache_entry *ce = active_cache[i];
196 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
197 0, ps_matched, 1) ||
198 !S_ISGITLINK(ce->ce_mode))
199 continue;
201 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
202 list->entries[list->nr++] = ce;
203 while (i + 1 < active_nr &&
204 !strcmp(ce->name, active_cache[i + 1]->name))
206 * Skip entries with the same name in different stages
207 * to make sure an entry is returned only once.
209 i++;
212 if (ps_matched && report_path_error(ps_matched, pathspec))
213 result = -1;
215 free(ps_matched);
217 return result;
220 static void module_list_active(struct module_list *list)
222 int i;
223 struct module_list active_modules = MODULE_LIST_INIT;
225 for (i = 0; i < list->nr; i++) {
226 const struct cache_entry *ce = list->entries[i];
228 if (!is_submodule_active(the_repository, ce->name))
229 continue;
231 ALLOC_GROW(active_modules.entries,
232 active_modules.nr + 1,
233 active_modules.alloc);
234 active_modules.entries[active_modules.nr++] = ce;
237 free(list->entries);
238 *list = active_modules;
241 static char *get_up_path(const char *path)
243 int i;
244 struct strbuf sb = STRBUF_INIT;
246 for (i = count_slashes(path); i; i--)
247 strbuf_addstr(&sb, "../");
250 * Check if 'path' ends with slash or not
251 * for having the same output for dir/sub_dir
252 * and dir/sub_dir/
254 if (!is_dir_sep(path[strlen(path) - 1]))
255 strbuf_addstr(&sb, "../");
257 return strbuf_detach(&sb, NULL);
260 static void for_each_listed_submodule(const struct module_list *list,
261 each_submodule_fn fn, void *cb_data)
263 int i;
265 for (i = 0; i < list->nr; i++)
266 fn(list->entries[i], cb_data);
269 struct foreach_cb {
270 int argc;
271 const char **argv;
272 const char *prefix;
273 int quiet;
274 int recursive;
276 #define FOREACH_CB_INIT { 0 }
278 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
279 void *cb_data)
281 struct foreach_cb *info = cb_data;
282 const char *path = list_item->name;
283 const struct object_id *ce_oid = &list_item->oid;
284 const struct submodule *sub;
285 struct child_process cp = CHILD_PROCESS_INIT;
286 char *displaypath;
288 displaypath = get_submodule_displaypath(path, info->prefix);
290 sub = submodule_from_path(the_repository, null_oid(), path);
292 if (!sub)
293 die(_("No url found for submodule path '%s' in .gitmodules"),
294 displaypath);
296 if (!is_submodule_populated_gently(path, NULL))
297 goto cleanup;
299 prepare_submodule_repo_env(&cp.env);
302 * For the purpose of executing <command> in the submodule,
303 * separate shell is used for the purpose of running the
304 * child process.
306 cp.use_shell = 1;
307 cp.dir = path;
310 * NEEDSWORK: the command currently has access to the variables $name,
311 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
312 * contains a single argument. This is done for maintaining a faithful
313 * translation from shell script.
315 if (info->argc == 1) {
316 char *toplevel = xgetcwd();
317 struct strbuf sb = STRBUF_INIT;
319 strvec_pushf(&cp.env, "name=%s", sub->name);
320 strvec_pushf(&cp.env, "sm_path=%s", path);
321 strvec_pushf(&cp.env, "displaypath=%s", displaypath);
322 strvec_pushf(&cp.env, "sha1=%s",
323 oid_to_hex(ce_oid));
324 strvec_pushf(&cp.env, "toplevel=%s", toplevel);
327 * Since the path variable was accessible from the script
328 * before porting, it is also made available after porting.
329 * The environment variable "PATH" has a very special purpose
330 * on windows. And since environment variables are
331 * case-insensitive in windows, it interferes with the
332 * existing PATH variable. Hence, to avoid that, we expose
333 * path via the args strvec and not via env.
335 sq_quote_buf(&sb, path);
336 strvec_pushf(&cp.args, "path=%s; %s",
337 sb.buf, info->argv[0]);
338 strbuf_release(&sb);
339 free(toplevel);
340 } else {
341 strvec_pushv(&cp.args, info->argv);
344 if (!info->quiet)
345 printf(_("Entering '%s'\n"), displaypath);
347 if (info->argv[0] && run_command(&cp))
348 die(_("run_command returned non-zero status for %s\n."),
349 displaypath);
351 if (info->recursive) {
352 struct child_process cpr = CHILD_PROCESS_INIT;
354 cpr.git_cmd = 1;
355 cpr.dir = path;
356 prepare_submodule_repo_env(&cpr.env);
358 strvec_pushl(&cpr.args, "--super-prefix", NULL);
359 strvec_pushf(&cpr.args, "%s/", displaypath);
360 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
361 NULL);
363 if (info->quiet)
364 strvec_push(&cpr.args, "--quiet");
366 strvec_push(&cpr.args, "--");
367 strvec_pushv(&cpr.args, info->argv);
369 if (run_command(&cpr))
370 die(_("run_command returned non-zero status while "
371 "recursing in the nested submodules of %s\n."),
372 displaypath);
375 cleanup:
376 free(displaypath);
379 static int module_foreach(int argc, const char **argv, const char *prefix)
381 struct foreach_cb info = FOREACH_CB_INIT;
382 struct pathspec pathspec;
383 struct module_list list = MODULE_LIST_INIT;
384 struct option module_foreach_options[] = {
385 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
386 OPT_BOOL(0, "recursive", &info.recursive,
387 N_("recurse into nested submodules")),
388 OPT_END()
390 const char *const git_submodule_helper_usage[] = {
391 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
392 NULL
395 argc = parse_options(argc, argv, prefix, module_foreach_options,
396 git_submodule_helper_usage, 0);
398 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
399 return 1;
401 info.argc = argc;
402 info.argv = argv;
403 info.prefix = prefix;
405 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
407 return 0;
410 static int starts_with_dot_slash(const char *const path)
412 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
413 PATH_MATCH_XPLATFORM);
416 static int starts_with_dot_dot_slash(const char *const path)
418 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
419 PATH_MATCH_XPLATFORM);
422 struct init_cb {
423 const char *prefix;
424 unsigned int flags;
426 #define INIT_CB_INIT { 0 }
428 static void init_submodule(const char *path, const char *prefix,
429 unsigned int flags)
431 const struct submodule *sub;
432 struct strbuf sb = STRBUF_INIT;
433 const char *upd;
434 char *url = NULL, *displaypath;
436 displaypath = get_submodule_displaypath(path, prefix);
438 sub = submodule_from_path(the_repository, null_oid(), path);
440 if (!sub)
441 die(_("No url found for submodule path '%s' in .gitmodules"),
442 displaypath);
445 * NEEDSWORK: In a multi-working-tree world, this needs to be
446 * set in the per-worktree config.
448 * Set active flag for the submodule being initialized
450 if (!is_submodule_active(the_repository, path)) {
451 strbuf_addf(&sb, "submodule.%s.active", sub->name);
452 git_config_set_gently(sb.buf, "true");
453 strbuf_reset(&sb);
457 * Copy url setting when it is not set yet.
458 * To look up the url in .git/config, we must not fall back to
459 * .gitmodules, so look it up directly.
461 strbuf_addf(&sb, "submodule.%s.url", sub->name);
462 if (git_config_get_string(sb.buf, &url)) {
463 if (!sub->url)
464 die(_("No url found for submodule path '%s' in .gitmodules"),
465 displaypath);
467 url = xstrdup(sub->url);
469 /* Possibly a url relative to parent */
470 if (starts_with_dot_dot_slash(url) ||
471 starts_with_dot_slash(url)) {
472 char *oldurl = url;
474 url = resolve_relative_url(oldurl, NULL, 0);
475 free(oldurl);
478 if (git_config_set_gently(sb.buf, url))
479 die(_("Failed to register url for submodule path '%s'"),
480 displaypath);
481 if (!(flags & OPT_QUIET))
482 fprintf(stderr,
483 _("Submodule '%s' (%s) registered for path '%s'\n"),
484 sub->name, url, displaypath);
486 strbuf_reset(&sb);
488 /* Copy "update" setting when it is not set yet */
489 strbuf_addf(&sb, "submodule.%s.update", sub->name);
490 if (git_config_get_string_tmp(sb.buf, &upd) &&
491 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
492 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
493 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
494 sub->name);
495 upd = "none";
496 } else {
497 upd = submodule_update_type_to_string(sub->update_strategy.type);
500 if (git_config_set_gently(sb.buf, upd))
501 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
503 strbuf_release(&sb);
504 free(displaypath);
505 free(url);
508 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
510 struct init_cb *info = cb_data;
512 init_submodule(list_item->name, info->prefix, info->flags);
515 static int module_init(int argc, const char **argv, const char *prefix)
517 struct init_cb info = INIT_CB_INIT;
518 struct pathspec pathspec;
519 struct module_list list = MODULE_LIST_INIT;
520 int quiet = 0;
521 struct option module_init_options[] = {
522 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
523 OPT_END()
525 const char *const git_submodule_helper_usage[] = {
526 N_("git submodule init [<options>] [<path>]"),
527 NULL
530 argc = parse_options(argc, argv, prefix, module_init_options,
531 git_submodule_helper_usage, 0);
533 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
534 return 1;
537 * If there are no path args and submodule.active is set then,
538 * by default, only initialize 'active' modules.
540 if (!argc && git_config_get_value_multi("submodule.active"))
541 module_list_active(&list);
543 info.prefix = prefix;
544 if (quiet)
545 info.flags |= OPT_QUIET;
547 for_each_listed_submodule(&list, init_submodule_cb, &info);
549 return 0;
552 struct status_cb {
553 const char *prefix;
554 unsigned int flags;
556 #define STATUS_CB_INIT { 0 }
558 static void print_status(unsigned int flags, char state, const char *path,
559 const struct object_id *oid, const char *displaypath)
561 if (flags & OPT_QUIET)
562 return;
564 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
566 if (state == ' ' || state == '+') {
567 const char *name = compute_rev_name(path, oid_to_hex(oid));
569 if (name)
570 printf(" (%s)", name);
573 printf("\n");
576 static int handle_submodule_head_ref(const char *refname,
577 const struct object_id *oid, int flags,
578 void *cb_data)
580 struct object_id *output = cb_data;
582 if (oid)
583 oidcpy(output, oid);
585 return 0;
588 static void status_submodule(const char *path, const struct object_id *ce_oid,
589 unsigned int ce_flags, const char *prefix,
590 unsigned int flags)
592 char *displaypath;
593 struct strvec diff_files_args = STRVEC_INIT;
594 struct rev_info rev = REV_INFO_INIT;
595 int diff_files_result;
596 struct strbuf buf = STRBUF_INIT;
597 const char *git_dir;
599 if (!submodule_from_path(the_repository, null_oid(), path))
600 die(_("no submodule mapping found in .gitmodules for path '%s'"),
601 path);
603 displaypath = get_submodule_displaypath(path, prefix);
605 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
606 print_status(flags, 'U', path, null_oid(), displaypath);
607 goto cleanup;
610 strbuf_addf(&buf, "%s/.git", path);
611 git_dir = read_gitfile(buf.buf);
612 if (!git_dir)
613 git_dir = buf.buf;
615 if (!is_submodule_active(the_repository, path) ||
616 !is_git_directory(git_dir)) {
617 print_status(flags, '-', path, ce_oid, displaypath);
618 strbuf_release(&buf);
619 goto cleanup;
621 strbuf_release(&buf);
623 strvec_pushl(&diff_files_args, "diff-files",
624 "--ignore-submodules=dirty", "--quiet", "--",
625 path, NULL);
627 git_config(git_diff_basic_config, NULL);
629 repo_init_revisions(the_repository, &rev, NULL);
630 rev.abbrev = 0;
631 diff_files_args.nr = setup_revisions(diff_files_args.nr,
632 diff_files_args.v,
633 &rev, NULL);
634 diff_files_result = run_diff_files(&rev, 0);
636 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
637 print_status(flags, ' ', path, ce_oid,
638 displaypath);
639 } else if (!(flags & OPT_CACHED)) {
640 struct object_id oid;
641 struct ref_store *refs = get_submodule_ref_store(path);
643 if (!refs) {
644 print_status(flags, '-', path, ce_oid, displaypath);
645 goto cleanup;
647 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
648 die(_("could not resolve HEAD ref inside the "
649 "submodule '%s'"), path);
651 print_status(flags, '+', path, &oid, displaypath);
652 } else {
653 print_status(flags, '+', path, ce_oid, displaypath);
656 if (flags & OPT_RECURSIVE) {
657 struct child_process cpr = CHILD_PROCESS_INIT;
659 cpr.git_cmd = 1;
660 cpr.dir = path;
661 prepare_submodule_repo_env(&cpr.env);
663 strvec_push(&cpr.args, "--super-prefix");
664 strvec_pushf(&cpr.args, "%s/", displaypath);
665 strvec_pushl(&cpr.args, "submodule--helper", "status",
666 "--recursive", NULL);
668 if (flags & OPT_CACHED)
669 strvec_push(&cpr.args, "--cached");
671 if (flags & OPT_QUIET)
672 strvec_push(&cpr.args, "--quiet");
674 if (run_command(&cpr))
675 die(_("failed to recurse into submodule '%s'"), path);
678 cleanup:
679 strvec_clear(&diff_files_args);
680 free(displaypath);
681 release_revisions(&rev);
684 static void status_submodule_cb(const struct cache_entry *list_item,
685 void *cb_data)
687 struct status_cb *info = cb_data;
689 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
690 info->prefix, info->flags);
693 static int module_status(int argc, const char **argv, const char *prefix)
695 struct status_cb info = STATUS_CB_INIT;
696 struct pathspec pathspec;
697 struct module_list list = MODULE_LIST_INIT;
698 int quiet = 0;
699 struct option module_status_options[] = {
700 OPT__QUIET(&quiet, N_("suppress submodule status output")),
701 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
702 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
703 OPT_END()
705 const char *const git_submodule_helper_usage[] = {
706 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
707 NULL
710 argc = parse_options(argc, argv, prefix, module_status_options,
711 git_submodule_helper_usage, 0);
713 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
714 return 1;
716 info.prefix = prefix;
717 if (quiet)
718 info.flags |= OPT_QUIET;
720 for_each_listed_submodule(&list, status_submodule_cb, &info);
722 return 0;
725 struct module_cb {
726 unsigned int mod_src;
727 unsigned int mod_dst;
728 struct object_id oid_src;
729 struct object_id oid_dst;
730 char status;
731 const char *sm_path;
733 #define MODULE_CB_INIT { 0 }
735 struct module_cb_list {
736 struct module_cb **entries;
737 int alloc, nr;
739 #define MODULE_CB_LIST_INIT { 0 }
741 struct summary_cb {
742 int argc;
743 const char **argv;
744 const char *prefix;
745 unsigned int cached: 1;
746 unsigned int for_status: 1;
747 unsigned int files: 1;
748 int summary_limit;
750 #define SUMMARY_CB_INIT { 0 }
752 enum diff_cmd {
753 DIFF_INDEX,
754 DIFF_FILES
757 static char *verify_submodule_committish(const char *sm_path,
758 const char *committish)
760 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
761 struct strbuf result = STRBUF_INIT;
763 cp_rev_parse.git_cmd = 1;
764 cp_rev_parse.dir = sm_path;
765 prepare_submodule_repo_env(&cp_rev_parse.env);
766 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
767 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
768 strvec_push(&cp_rev_parse.args, "--");
770 if (capture_command(&cp_rev_parse, &result, 0))
771 return NULL;
773 strbuf_trim_trailing_newline(&result);
774 return strbuf_detach(&result, NULL);
777 static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
778 int total_commits, const char *displaypath,
779 const char *src_abbrev, const char *dst_abbrev,
780 struct module_cb *p)
782 if (p->status == 'T') {
783 if (S_ISGITLINK(p->mod_dst))
784 printf(_("* %s %s(blob)->%s(submodule)"),
785 displaypath, src_abbrev, dst_abbrev);
786 else
787 printf(_("* %s %s(submodule)->%s(blob)"),
788 displaypath, src_abbrev, dst_abbrev);
789 } else {
790 printf("* %s %s...%s",
791 displaypath, src_abbrev, dst_abbrev);
794 if (total_commits < 0)
795 printf(":\n");
796 else
797 printf(" (%d):\n", total_commits);
799 if (errmsg) {
800 printf(_("%s"), errmsg);
801 } else if (total_commits > 0) {
802 struct child_process cp_log = CHILD_PROCESS_INIT;
804 cp_log.git_cmd = 1;
805 cp_log.dir = p->sm_path;
806 prepare_submodule_repo_env(&cp_log.env);
807 strvec_pushl(&cp_log.args, "log", NULL);
809 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
810 if (info->summary_limit > 0)
811 strvec_pushf(&cp_log.args, "-%d",
812 info->summary_limit);
814 strvec_pushl(&cp_log.args, "--pretty= %m %s",
815 "--first-parent", NULL);
816 strvec_pushf(&cp_log.args, "%s...%s",
817 src_abbrev, dst_abbrev);
818 } else if (S_ISGITLINK(p->mod_dst)) {
819 strvec_pushl(&cp_log.args, "--pretty= > %s",
820 "-1", dst_abbrev, NULL);
821 } else {
822 strvec_pushl(&cp_log.args, "--pretty= < %s",
823 "-1", src_abbrev, NULL);
825 run_command(&cp_log);
827 printf("\n");
830 static void generate_submodule_summary(struct summary_cb *info,
831 struct module_cb *p)
833 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
834 int missing_src = 0, missing_dst = 0;
835 struct strbuf errmsg = STRBUF_INIT;
836 int total_commits = -1;
838 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
839 if (S_ISGITLINK(p->mod_dst)) {
840 struct ref_store *refs = get_submodule_ref_store(p->sm_path);
842 if (refs)
843 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
844 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
845 struct stat st;
846 int fd = open(p->sm_path, O_RDONLY);
848 if (fd < 0 || fstat(fd, &st) < 0 ||
849 index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
850 p->sm_path, 0))
851 error(_("couldn't hash object from '%s'"), p->sm_path);
852 } else {
853 /* for a submodule removal (mode:0000000), don't warn */
854 if (p->mod_dst)
855 warning(_("unexpected mode %o\n"), p->mod_dst);
859 if (S_ISGITLINK(p->mod_src)) {
860 if (p->status != 'D')
861 src_abbrev = verify_submodule_committish(p->sm_path,
862 oid_to_hex(&p->oid_src));
863 if (!src_abbrev) {
864 missing_src = 1;
866 * As `rev-parse` failed, we fallback to getting
867 * the abbreviated hash using oid_src. We do
868 * this as we might still need the abbreviated
869 * hash in cases like a submodule type change, etc.
871 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
873 } else {
875 * The source does not point to a submodule.
876 * So, we fallback to getting the abbreviation using
877 * oid_src as we might still need the abbreviated
878 * hash in cases like submodule add, etc.
880 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
883 if (S_ISGITLINK(p->mod_dst)) {
884 dst_abbrev = verify_submodule_committish(p->sm_path,
885 oid_to_hex(&p->oid_dst));
886 if (!dst_abbrev) {
887 missing_dst = 1;
889 * As `rev-parse` failed, we fallback to getting
890 * the abbreviated hash using oid_dst. We do
891 * this as we might still need the abbreviated
892 * hash in cases like a submodule type change, etc.
894 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
896 } else {
898 * The destination does not point to a submodule.
899 * So, we fallback to getting the abbreviation using
900 * oid_dst as we might still need the abbreviated
901 * hash in cases like a submodule removal, etc.
903 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
906 displaypath = get_submodule_displaypath(p->sm_path, info->prefix);
908 if (!missing_src && !missing_dst) {
909 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
910 struct strbuf sb_rev_list = STRBUF_INIT;
912 strvec_pushl(&cp_rev_list.args, "rev-list",
913 "--first-parent", "--count", NULL);
914 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
915 strvec_pushf(&cp_rev_list.args, "%s...%s",
916 src_abbrev, dst_abbrev);
917 else
918 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
919 src_abbrev : dst_abbrev);
920 strvec_push(&cp_rev_list.args, "--");
922 cp_rev_list.git_cmd = 1;
923 cp_rev_list.dir = p->sm_path;
924 prepare_submodule_repo_env(&cp_rev_list.env);
926 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
927 total_commits = atoi(sb_rev_list.buf);
929 strbuf_release(&sb_rev_list);
930 } else {
932 * Don't give error msg for modification whose dst is not
933 * submodule, i.e., deleted or changed to blob
935 if (S_ISGITLINK(p->mod_dst)) {
936 if (missing_src && missing_dst) {
937 strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n",
938 displaypath, oid_to_hex(&p->oid_src),
939 oid_to_hex(&p->oid_dst));
940 } else {
941 strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n",
942 displaypath, missing_src ?
943 oid_to_hex(&p->oid_src) :
944 oid_to_hex(&p->oid_dst));
949 print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
950 total_commits, displaypath, src_abbrev,
951 dst_abbrev, p);
953 free(displaypath);
954 free(src_abbrev);
955 free(dst_abbrev);
958 static void prepare_submodule_summary(struct summary_cb *info,
959 struct module_cb_list *list)
961 int i;
962 for (i = 0; i < list->nr; i++) {
963 const struct submodule *sub;
964 struct module_cb *p = list->entries[i];
965 struct strbuf sm_gitdir = STRBUF_INIT;
967 if (p->status == 'D' || p->status == 'T') {
968 generate_submodule_summary(info, p);
969 continue;
972 if (info->for_status && p->status != 'A' &&
973 (sub = submodule_from_path(the_repository,
974 null_oid(), p->sm_path))) {
975 char *config_key = NULL;
976 const char *value;
977 int ignore_all = 0;
979 config_key = xstrfmt("submodule.%s.ignore",
980 sub->name);
981 if (!git_config_get_string_tmp(config_key, &value))
982 ignore_all = !strcmp(value, "all");
983 else if (sub->ignore)
984 ignore_all = !strcmp(sub->ignore, "all");
986 free(config_key);
987 if (ignore_all)
988 continue;
991 /* Also show added or modified modules which are checked out */
992 strbuf_addstr(&sm_gitdir, p->sm_path);
993 if (is_nonbare_repository_dir(&sm_gitdir))
994 generate_submodule_summary(info, p);
995 strbuf_release(&sm_gitdir);
999 static void submodule_summary_callback(struct diff_queue_struct *q,
1000 struct diff_options *options,
1001 void *data)
1003 int i;
1004 struct module_cb_list *list = data;
1005 for (i = 0; i < q->nr; i++) {
1006 struct diff_filepair *p = q->queue[i];
1007 struct module_cb *temp;
1009 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1010 continue;
1011 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1012 temp->mod_src = p->one->mode;
1013 temp->mod_dst = p->two->mode;
1014 temp->oid_src = p->one->oid;
1015 temp->oid_dst = p->two->oid;
1016 temp->status = p->status;
1017 temp->sm_path = xstrdup(p->one->path);
1019 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1020 list->entries[list->nr++] = temp;
1024 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1026 switch (diff_cmd) {
1027 case DIFF_INDEX: return "diff-index";
1028 case DIFF_FILES: return "diff-files";
1029 default: BUG("bad diff_cmd value %d", diff_cmd);
1033 static int compute_summary_module_list(struct object_id *head_oid,
1034 struct summary_cb *info,
1035 enum diff_cmd diff_cmd)
1037 struct strvec diff_args = STRVEC_INIT;
1038 struct rev_info rev;
1039 struct setup_revision_opt opt = {
1040 .free_removed_argv_elements = 1,
1042 struct module_cb_list list = MODULE_CB_LIST_INIT;
1043 int ret = 0;
1045 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1046 if (info->cached)
1047 strvec_push(&diff_args, "--cached");
1048 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1049 if (head_oid)
1050 strvec_push(&diff_args, oid_to_hex(head_oid));
1051 strvec_push(&diff_args, "--");
1052 if (info->argc)
1053 strvec_pushv(&diff_args, info->argv);
1055 git_config(git_diff_basic_config, NULL);
1056 init_revisions(&rev, info->prefix);
1057 rev.abbrev = 0;
1058 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1059 setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
1060 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1061 rev.diffopt.format_callback = submodule_summary_callback;
1062 rev.diffopt.format_callback_data = &list;
1064 if (!info->cached) {
1065 if (diff_cmd == DIFF_INDEX)
1066 setup_work_tree();
1067 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1068 perror("read_cache_preload");
1069 ret = -1;
1070 goto cleanup;
1072 } else if (read_cache() < 0) {
1073 perror("read_cache");
1074 ret = -1;
1075 goto cleanup;
1078 if (diff_cmd == DIFF_INDEX)
1079 run_diff_index(&rev, info->cached);
1080 else
1081 run_diff_files(&rev, 0);
1082 prepare_submodule_summary(info, &list);
1083 cleanup:
1084 strvec_clear(&diff_args);
1085 release_revisions(&rev);
1086 return ret;
1089 static int module_summary(int argc, const char **argv, const char *prefix)
1091 struct summary_cb info = SUMMARY_CB_INIT;
1092 int cached = 0;
1093 int for_status = 0;
1094 int files = 0;
1095 int summary_limit = -1;
1096 enum diff_cmd diff_cmd = DIFF_INDEX;
1097 struct object_id head_oid;
1098 int ret;
1099 struct option module_summary_options[] = {
1100 OPT_BOOL(0, "cached", &cached,
1101 N_("use the commit stored in the index instead of the submodule HEAD")),
1102 OPT_BOOL(0, "files", &files,
1103 N_("compare the commit in the index with that in the submodule HEAD")),
1104 OPT_BOOL(0, "for-status", &for_status,
1105 N_("skip submodules with 'ignore_config' value set to 'all'")),
1106 OPT_INTEGER('n', "summary-limit", &summary_limit,
1107 N_("limit the summary size")),
1108 OPT_END()
1110 const char *const git_submodule_helper_usage[] = {
1111 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1112 NULL
1115 argc = parse_options(argc, argv, prefix, module_summary_options,
1116 git_submodule_helper_usage, 0);
1118 if (!summary_limit)
1119 return 0;
1121 if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) {
1122 if (argc) {
1123 argv++;
1124 argc--;
1126 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1127 /* before the first commit: compare with an empty tree */
1128 oidcpy(&head_oid, the_hash_algo->empty_tree);
1129 if (argc) {
1130 argv++;
1131 argc--;
1133 } else {
1134 if (get_oid("HEAD", &head_oid))
1135 die(_("could not fetch a revision for HEAD"));
1138 if (files) {
1139 if (cached)
1140 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1141 diff_cmd = DIFF_FILES;
1144 info.argc = argc;
1145 info.argv = argv;
1146 info.prefix = prefix;
1147 info.cached = !!cached;
1148 info.files = !!files;
1149 info.for_status = !!for_status;
1150 info.summary_limit = summary_limit;
1152 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1153 &info, diff_cmd);
1154 return ret;
1157 struct sync_cb {
1158 const char *prefix;
1159 unsigned int flags;
1161 #define SYNC_CB_INIT { 0 }
1163 static void sync_submodule(const char *path, const char *prefix,
1164 unsigned int flags)
1166 const struct submodule *sub;
1167 char *remote_key = NULL;
1168 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1169 struct strbuf sb = STRBUF_INIT;
1170 char *sub_config_path = NULL;
1171 int code;
1173 if (!is_submodule_active(the_repository, path))
1174 return;
1176 sub = submodule_from_path(the_repository, null_oid(), path);
1178 if (sub && sub->url) {
1179 if (starts_with_dot_dot_slash(sub->url) ||
1180 starts_with_dot_slash(sub->url)) {
1181 char *up_path = get_up_path(path);
1183 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1184 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1185 free(up_path);
1186 } else {
1187 sub_origin_url = xstrdup(sub->url);
1188 super_config_url = xstrdup(sub->url);
1190 } else {
1191 sub_origin_url = xstrdup("");
1192 super_config_url = xstrdup("");
1195 displaypath = get_submodule_displaypath(path, prefix);
1197 if (!(flags & OPT_QUIET))
1198 printf(_("Synchronizing submodule url for '%s'\n"),
1199 displaypath);
1201 strbuf_reset(&sb);
1202 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1203 if (git_config_set_gently(sb.buf, super_config_url))
1204 die(_("failed to register url for submodule path '%s'"),
1205 displaypath);
1207 if (!is_submodule_populated_gently(path, NULL))
1208 goto cleanup;
1210 strbuf_reset(&sb);
1211 code = get_default_remote_submodule(path, &default_remote);
1212 if (code)
1213 exit(code);
1215 remote_key = xstrfmt("remote.%s.url", default_remote);
1216 free(default_remote);
1218 submodule_to_gitdir(&sb, path);
1219 strbuf_addstr(&sb, "/config");
1221 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1222 die(_("failed to update remote for submodule '%s'"),
1223 path);
1225 if (flags & OPT_RECURSIVE) {
1226 struct child_process cpr = CHILD_PROCESS_INIT;
1228 cpr.git_cmd = 1;
1229 cpr.dir = path;
1230 prepare_submodule_repo_env(&cpr.env);
1232 strvec_push(&cpr.args, "--super-prefix");
1233 strvec_pushf(&cpr.args, "%s/", displaypath);
1234 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1235 "--recursive", NULL);
1237 if (flags & OPT_QUIET)
1238 strvec_push(&cpr.args, "--quiet");
1240 if (run_command(&cpr))
1241 die(_("failed to recurse into submodule '%s'"),
1242 path);
1245 cleanup:
1246 free(super_config_url);
1247 free(sub_origin_url);
1248 strbuf_release(&sb);
1249 free(remote_key);
1250 free(displaypath);
1251 free(sub_config_path);
1254 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1256 struct sync_cb *info = cb_data;
1258 sync_submodule(list_item->name, info->prefix, info->flags);
1261 static int module_sync(int argc, const char **argv, const char *prefix)
1263 struct sync_cb info = SYNC_CB_INIT;
1264 struct pathspec pathspec;
1265 struct module_list list = MODULE_LIST_INIT;
1266 int quiet = 0;
1267 int recursive = 0;
1268 struct option module_sync_options[] = {
1269 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1270 OPT_BOOL(0, "recursive", &recursive,
1271 N_("recurse into nested submodules")),
1272 OPT_END()
1274 const char *const git_submodule_helper_usage[] = {
1275 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1276 NULL
1279 argc = parse_options(argc, argv, prefix, module_sync_options,
1280 git_submodule_helper_usage, 0);
1282 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1283 return 1;
1285 info.prefix = prefix;
1286 if (quiet)
1287 info.flags |= OPT_QUIET;
1288 if (recursive)
1289 info.flags |= OPT_RECURSIVE;
1291 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1293 return 0;
1296 struct deinit_cb {
1297 const char *prefix;
1298 unsigned int flags;
1300 #define DEINIT_CB_INIT { 0 }
1302 static void deinit_submodule(const char *path, const char *prefix,
1303 unsigned int flags)
1305 const struct submodule *sub;
1306 char *displaypath = NULL;
1307 struct child_process cp_config = CHILD_PROCESS_INIT;
1308 struct strbuf sb_config = STRBUF_INIT;
1309 char *sub_git_dir = xstrfmt("%s/.git", path);
1311 sub = submodule_from_path(the_repository, null_oid(), path);
1313 if (!sub || !sub->name)
1314 goto cleanup;
1316 displaypath = get_submodule_displaypath(path, prefix);
1318 /* remove the submodule work tree (unless the user already did it) */
1319 if (is_directory(path)) {
1320 struct strbuf sb_rm = STRBUF_INIT;
1321 const char *format;
1323 if (is_directory(sub_git_dir)) {
1324 if (!(flags & OPT_QUIET))
1325 warning(_("Submodule work tree '%s' contains a .git "
1326 "directory. This will be replaced with a "
1327 ".git file by using absorbgitdirs."),
1328 displaypath);
1330 absorb_git_dir_into_superproject(path,
1331 ABSORB_GITDIR_RECURSE_SUBMODULES);
1335 if (!(flags & OPT_FORCE)) {
1336 struct child_process cp_rm = CHILD_PROCESS_INIT;
1338 cp_rm.git_cmd = 1;
1339 strvec_pushl(&cp_rm.args, "rm", "-qn",
1340 path, NULL);
1342 if (run_command(&cp_rm))
1343 die(_("Submodule work tree '%s' contains local "
1344 "modifications; use '-f' to discard them"),
1345 displaypath);
1348 strbuf_addstr(&sb_rm, path);
1350 if (!remove_dir_recursively(&sb_rm, 0))
1351 format = _("Cleared directory '%s'\n");
1352 else
1353 format = _("Could not remove submodule work tree '%s'\n");
1355 if (!(flags & OPT_QUIET))
1356 printf(format, displaypath);
1358 submodule_unset_core_worktree(sub);
1360 strbuf_release(&sb_rm);
1363 if (mkdir(path, 0777))
1364 printf(_("could not create empty submodule directory %s"),
1365 displaypath);
1367 cp_config.git_cmd = 1;
1368 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1369 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1371 /* remove the .git/config entries (unless the user already did it) */
1372 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1373 char *sub_key = xstrfmt("submodule.%s", sub->name);
1376 * remove the whole section so we have a clean state when
1377 * the user later decides to init this submodule again
1379 git_config_rename_section_in_file(NULL, sub_key, NULL);
1380 if (!(flags & OPT_QUIET))
1381 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1382 sub->name, sub->url, displaypath);
1383 free(sub_key);
1386 cleanup:
1387 free(displaypath);
1388 free(sub_git_dir);
1389 strbuf_release(&sb_config);
1392 static void deinit_submodule_cb(const struct cache_entry *list_item,
1393 void *cb_data)
1395 struct deinit_cb *info = cb_data;
1396 deinit_submodule(list_item->name, info->prefix, info->flags);
1399 static int module_deinit(int argc, const char **argv, const char *prefix)
1401 struct deinit_cb info = DEINIT_CB_INIT;
1402 struct pathspec pathspec;
1403 struct module_list list = MODULE_LIST_INIT;
1404 int quiet = 0;
1405 int force = 0;
1406 int all = 0;
1407 struct option module_deinit_options[] = {
1408 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1409 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1410 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1411 OPT_END()
1413 const char *const git_submodule_helper_usage[] = {
1414 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1415 NULL
1418 argc = parse_options(argc, argv, prefix, module_deinit_options,
1419 git_submodule_helper_usage, 0);
1421 if (all && argc) {
1422 error("pathspec and --all are incompatible");
1423 usage_with_options(git_submodule_helper_usage,
1424 module_deinit_options);
1427 if (!argc && !all)
1428 die(_("Use '--all' if you really want to deinitialize all submodules"));
1430 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1431 return 1;
1433 info.prefix = prefix;
1434 if (quiet)
1435 info.flags |= OPT_QUIET;
1436 if (force)
1437 info.flags |= OPT_FORCE;
1439 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1441 return 0;
1444 struct module_clone_data {
1445 const char *prefix;
1446 const char *path;
1447 const char *name;
1448 const char *url;
1449 const char *depth;
1450 struct list_objects_filter_options *filter_options;
1451 unsigned int quiet: 1;
1452 unsigned int progress: 1;
1453 unsigned int dissociate: 1;
1454 unsigned int require_init: 1;
1455 int single_branch;
1457 #define MODULE_CLONE_DATA_INIT { \
1458 .single_branch = -1, \
1461 struct submodule_alternate_setup {
1462 const char *submodule_name;
1463 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1464 SUBMODULE_ALTERNATE_ERROR_DIE,
1465 SUBMODULE_ALTERNATE_ERROR_INFO,
1466 SUBMODULE_ALTERNATE_ERROR_IGNORE
1467 } error_mode;
1468 struct string_list *reference;
1470 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1471 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1474 static const char alternate_error_advice[] = N_(
1475 "An alternate computed from a superproject's alternate is invalid.\n"
1476 "To allow Git to clone without an alternate in such a case, set\n"
1477 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1478 "'--reference-if-able' instead of '--reference'."
1481 static int add_possible_reference_from_superproject(
1482 struct object_directory *odb, void *sas_cb)
1484 struct submodule_alternate_setup *sas = sas_cb;
1485 size_t len;
1488 * If the alternate object store is another repository, try the
1489 * standard layout with .git/(modules/<name>)+/objects
1491 if (strip_suffix(odb->path, "/objects", &len)) {
1492 struct repository alternate;
1493 char *sm_alternate;
1494 struct strbuf sb = STRBUF_INIT;
1495 struct strbuf err = STRBUF_INIT;
1496 strbuf_add(&sb, odb->path, len);
1498 if (repo_init(&alternate, sb.buf, NULL) < 0)
1499 die(_("could not get a repository handle for gitdir '%s'"),
1500 sb.buf);
1503 * We need to end the new path with '/' to mark it as a dir,
1504 * otherwise a submodule name containing '/' will be broken
1505 * as the last part of a missing submodule reference would
1506 * be taken as a file name.
1508 strbuf_reset(&sb);
1509 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1510 strbuf_addch(&sb, '/');
1511 repo_clear(&alternate);
1513 sm_alternate = compute_alternate_path(sb.buf, &err);
1514 if (sm_alternate) {
1515 string_list_append(sas->reference, xstrdup(sb.buf));
1516 free(sm_alternate);
1517 } else {
1518 switch (sas->error_mode) {
1519 case SUBMODULE_ALTERNATE_ERROR_DIE:
1520 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1521 advise(_(alternate_error_advice));
1522 die(_("submodule '%s' cannot add alternate: %s"),
1523 sas->submodule_name, err.buf);
1524 case SUBMODULE_ALTERNATE_ERROR_INFO:
1525 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1526 sas->submodule_name, err.buf);
1527 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1528 ; /* nothing */
1531 strbuf_release(&sb);
1534 return 0;
1537 static void prepare_possible_alternates(const char *sm_name,
1538 struct string_list *reference)
1540 char *sm_alternate = NULL, *error_strategy = NULL;
1541 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1543 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1544 if (!sm_alternate)
1545 return;
1547 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1549 if (!error_strategy)
1550 error_strategy = xstrdup("die");
1552 sas.submodule_name = sm_name;
1553 sas.reference = reference;
1554 if (!strcmp(error_strategy, "die"))
1555 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1556 else if (!strcmp(error_strategy, "info"))
1557 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1558 else if (!strcmp(error_strategy, "ignore"))
1559 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1560 else
1561 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1563 if (!strcmp(sm_alternate, "superproject"))
1564 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1565 else if (!strcmp(sm_alternate, "no"))
1566 ; /* do nothing */
1567 else
1568 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1570 free(sm_alternate);
1571 free(error_strategy);
1574 static char *clone_submodule_sm_gitdir(const char *name)
1576 struct strbuf sb = STRBUF_INIT;
1577 char *sm_gitdir;
1579 submodule_name_to_gitdir(&sb, the_repository, name);
1580 sm_gitdir = absolute_pathdup(sb.buf);
1581 strbuf_release(&sb);
1583 return sm_gitdir;
1586 static int clone_submodule(const struct module_clone_data *clone_data,
1587 struct string_list *reference)
1589 char *p;
1590 char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1591 char *sm_alternate = NULL, *error_strategy = NULL;
1592 struct child_process cp = CHILD_PROCESS_INIT;
1593 const char *clone_data_path;
1595 if (!is_absolute_path(clone_data->path))
1596 clone_data_path = xstrfmt("%s/%s", get_git_work_tree(),
1597 clone_data->path);
1598 else
1599 clone_data_path = xstrdup(clone_data->path);
1601 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1602 die(_("refusing to create/use '%s' in another submodule's "
1603 "git dir"), sm_gitdir);
1605 if (!file_exists(sm_gitdir)) {
1606 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1607 die(_("could not create directory '%s'"), sm_gitdir);
1609 prepare_possible_alternates(clone_data->name, reference);
1611 strvec_push(&cp.args, "clone");
1612 strvec_push(&cp.args, "--no-checkout");
1613 if (clone_data->quiet)
1614 strvec_push(&cp.args, "--quiet");
1615 if (clone_data->progress)
1616 strvec_push(&cp.args, "--progress");
1617 if (clone_data->depth && *(clone_data->depth))
1618 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1619 if (reference->nr) {
1620 struct string_list_item *item;
1622 for_each_string_list_item(item, reference)
1623 strvec_pushl(&cp.args, "--reference",
1624 item->string, NULL);
1626 if (clone_data->dissociate)
1627 strvec_push(&cp.args, "--dissociate");
1628 if (sm_gitdir && *sm_gitdir)
1629 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1630 if (clone_data->filter_options && clone_data->filter_options->choice)
1631 strvec_pushf(&cp.args, "--filter=%s",
1632 expand_list_objects_filter_spec(
1633 clone_data->filter_options));
1634 if (clone_data->single_branch >= 0)
1635 strvec_push(&cp.args, clone_data->single_branch ?
1636 "--single-branch" :
1637 "--no-single-branch");
1639 strvec_push(&cp.args, "--");
1640 strvec_push(&cp.args, clone_data->url);
1641 strvec_push(&cp.args, clone_data_path);
1643 cp.git_cmd = 1;
1644 prepare_submodule_repo_env(&cp.env);
1645 cp.no_stdin = 1;
1647 if(run_command(&cp))
1648 die(_("clone of '%s' into submodule path '%s' failed"),
1649 clone_data->url, clone_data_path);
1650 } else {
1651 char *path;
1653 if (clone_data->require_init && !access(clone_data_path, X_OK) &&
1654 !is_empty_dir(clone_data_path))
1655 die(_("directory not empty: '%s'"), clone_data_path);
1656 if (safe_create_leading_directories_const(clone_data_path) < 0)
1657 die(_("could not create directory '%s'"), clone_data_path);
1658 path = xstrfmt("%s/index", sm_gitdir);
1659 unlink_or_warn(path);
1660 free(path);
1663 connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1665 p = git_pathdup_submodule(clone_data_path, "config");
1666 if (!p)
1667 die(_("could not get submodule directory for '%s'"), clone_data_path);
1669 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1670 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1671 if (sm_alternate)
1672 git_config_set_in_file(p, "submodule.alternateLocation",
1673 sm_alternate);
1674 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1675 if (error_strategy)
1676 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1677 error_strategy);
1679 free(sm_alternate);
1680 free(error_strategy);
1682 free(sm_gitdir);
1683 free(p);
1684 return 0;
1687 static int module_clone(int argc, const char **argv, const char *prefix)
1689 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1690 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1691 struct list_objects_filter_options filter_options = { 0 };
1692 struct string_list reference = STRING_LIST_INIT_NODUP;
1693 struct option module_clone_options[] = {
1694 OPT_STRING(0, "prefix", &clone_data.prefix,
1695 N_("path"),
1696 N_("alternative anchor for relative paths")),
1697 OPT_STRING(0, "path", &clone_data.path,
1698 N_("path"),
1699 N_("where the new submodule will be cloned to")),
1700 OPT_STRING(0, "name", &clone_data.name,
1701 N_("string"),
1702 N_("name of the new submodule")),
1703 OPT_STRING(0, "url", &clone_data.url,
1704 N_("string"),
1705 N_("url where to clone the submodule from")),
1706 OPT_STRING_LIST(0, "reference", &reference,
1707 N_("repo"),
1708 N_("reference repository")),
1709 OPT_BOOL(0, "dissociate", &dissociate,
1710 N_("use --reference only while cloning")),
1711 OPT_STRING(0, "depth", &clone_data.depth,
1712 N_("string"),
1713 N_("depth for shallow clones")),
1714 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1715 OPT_BOOL(0, "progress", &progress,
1716 N_("force cloning progress")),
1717 OPT_BOOL(0, "require-init", &require_init,
1718 N_("disallow cloning into non-empty directory")),
1719 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1720 N_("clone only one branch, HEAD or --branch")),
1721 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1722 OPT_END()
1724 const char *const git_submodule_helper_usage[] = {
1725 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1726 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1727 "[--single-branch] [--filter <filter-spec>] "
1728 "--url <url> --path <path>"),
1729 NULL
1732 argc = parse_options(argc, argv, prefix, module_clone_options,
1733 git_submodule_helper_usage, 0);
1735 clone_data.dissociate = !!dissociate;
1736 clone_data.quiet = !!quiet;
1737 clone_data.progress = !!progress;
1738 clone_data.require_init = !!require_init;
1739 clone_data.filter_options = &filter_options;
1741 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1742 usage_with_options(git_submodule_helper_usage,
1743 module_clone_options);
1745 clone_submodule(&clone_data, &reference);
1746 list_objects_filter_release(&filter_options);
1747 return 0;
1750 static int determine_submodule_update_strategy(struct repository *r,
1751 int just_cloned,
1752 const char *path,
1753 enum submodule_update_type update,
1754 struct submodule_update_strategy *out)
1756 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1757 char *key;
1758 const char *val;
1759 int ret;
1761 key = xstrfmt("submodule.%s.update", sub->name);
1763 if (update) {
1764 out->type = update;
1765 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1766 if (parse_submodule_update_strategy(val, out) < 0) {
1767 ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1768 val, path);
1769 goto cleanup;
1771 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1772 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1773 BUG("how did we read update = !command from .gitmodules?");
1774 out->type = sub->update_strategy.type;
1775 out->command = sub->update_strategy.command;
1776 } else
1777 out->type = SM_UPDATE_CHECKOUT;
1779 if (just_cloned &&
1780 (out->type == SM_UPDATE_MERGE ||
1781 out->type == SM_UPDATE_REBASE ||
1782 out->type == SM_UPDATE_NONE))
1783 out->type = SM_UPDATE_CHECKOUT;
1785 ret = 0;
1786 cleanup:
1787 free(key);
1788 return ret;
1791 struct update_clone_data {
1792 const struct submodule *sub;
1793 struct object_id oid;
1794 unsigned just_cloned;
1797 struct submodule_update_clone {
1798 /* index into 'update_data.list', the list of submodules to look into for cloning */
1799 int current;
1801 /* configuration parameters which are passed on to the children */
1802 const struct update_data *update_data;
1804 /* to be consumed by update_submodule() */
1805 struct update_clone_data *update_clone;
1806 int update_clone_nr; int update_clone_alloc;
1808 /* If we want to stop as fast as possible and return an error */
1809 unsigned quickstop : 1;
1811 /* failed clones to be retried again */
1812 const struct cache_entry **failed_clones;
1813 int failed_clones_nr, failed_clones_alloc;
1815 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1817 struct update_data {
1818 const char *prefix;
1819 const char *displaypath;
1820 enum submodule_update_type update_default;
1821 struct object_id suboid;
1822 struct string_list references;
1823 struct submodule_update_strategy update_strategy;
1824 struct list_objects_filter_options *filter_options;
1825 struct module_list list;
1826 int depth;
1827 int max_jobs;
1828 int single_branch;
1829 int recommend_shallow;
1830 unsigned int require_init;
1831 unsigned int force;
1832 unsigned int quiet;
1833 unsigned int nofetch;
1834 unsigned int remote;
1835 unsigned int progress;
1836 unsigned int dissociate;
1837 unsigned int init;
1838 unsigned int warn_if_uninitialized;
1839 unsigned int recursive;
1841 /* copied over from update_clone_data */
1842 struct object_id oid;
1843 unsigned int just_cloned;
1844 const char *sm_path;
1846 #define UPDATE_DATA_INIT { \
1847 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1848 .list = MODULE_LIST_INIT, \
1849 .recommend_shallow = -1, \
1850 .references = STRING_LIST_INIT_DUP, \
1851 .single_branch = -1, \
1852 .max_jobs = 1, \
1855 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1856 struct strbuf *out, const char *displaypath)
1859 * Only mention uninitialized submodules when their
1860 * paths have been specified.
1862 if (suc->update_data->warn_if_uninitialized) {
1863 strbuf_addf(out,
1864 _("Submodule path '%s' not initialized"),
1865 displaypath);
1866 strbuf_addch(out, '\n');
1867 strbuf_addstr(out,
1868 _("Maybe you want to use 'update --init'?"));
1869 strbuf_addch(out, '\n');
1874 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1875 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1877 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1878 struct child_process *child,
1879 struct submodule_update_clone *suc,
1880 struct strbuf *out)
1882 const struct submodule *sub = NULL;
1883 const char *url = NULL;
1884 const char *update_string;
1885 enum submodule_update_type update_type;
1886 char *key;
1887 const struct update_data *ud = suc->update_data;
1888 char *displaypath = get_submodule_displaypath(ce->name, ud->prefix);
1889 struct strbuf sb = STRBUF_INIT;
1890 int needs_cloning = 0;
1891 int need_free_url = 0;
1893 if (ce_stage(ce)) {
1894 strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
1895 strbuf_addch(out, '\n');
1896 goto cleanup;
1899 sub = submodule_from_path(the_repository, null_oid(), ce->name);
1901 if (!sub) {
1902 next_submodule_warn_missing(suc, out, displaypath);
1903 goto cleanup;
1906 key = xstrfmt("submodule.%s.update", sub->name);
1907 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
1908 update_type = parse_submodule_update_type(update_string);
1909 } else {
1910 update_type = sub->update_strategy.type;
1912 free(key);
1914 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
1915 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
1916 && update_type == SM_UPDATE_NONE)) {
1917 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1918 strbuf_addch(out, '\n');
1919 goto cleanup;
1922 /* Check if the submodule has been initialized. */
1923 if (!is_submodule_active(the_repository, ce->name)) {
1924 next_submodule_warn_missing(suc, out, displaypath);
1925 goto cleanup;
1928 strbuf_reset(&sb);
1929 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1930 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
1931 if (starts_with_dot_slash(sub->url) ||
1932 starts_with_dot_dot_slash(sub->url)) {
1933 url = resolve_relative_url(sub->url, NULL, 0);
1934 need_free_url = 1;
1935 } else
1936 url = sub->url;
1939 strbuf_reset(&sb);
1940 strbuf_addf(&sb, "%s/.git", ce->name);
1941 needs_cloning = !file_exists(sb.buf);
1943 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
1944 suc->update_clone_alloc);
1945 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
1946 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
1947 suc->update_clone[suc->update_clone_nr].sub = sub;
1948 suc->update_clone_nr++;
1950 if (!needs_cloning)
1951 goto cleanup;
1953 child->git_cmd = 1;
1954 child->no_stdin = 1;
1955 child->stdout_to_stderr = 1;
1956 child->err = -1;
1957 strvec_push(&child->args, "submodule--helper");
1958 strvec_push(&child->args, "clone");
1959 if (suc->update_data->progress)
1960 strvec_push(&child->args, "--progress");
1961 if (suc->update_data->quiet)
1962 strvec_push(&child->args, "--quiet");
1963 if (suc->update_data->prefix)
1964 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
1965 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
1966 strvec_push(&child->args, "--depth=1");
1967 else if (suc->update_data->depth)
1968 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
1969 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
1970 strvec_pushf(&child->args, "--filter=%s",
1971 expand_list_objects_filter_spec(suc->update_data->filter_options));
1972 if (suc->update_data->require_init)
1973 strvec_push(&child->args, "--require-init");
1974 strvec_pushl(&child->args, "--path", sub->path, NULL);
1975 strvec_pushl(&child->args, "--name", sub->name, NULL);
1976 strvec_pushl(&child->args, "--url", url, NULL);
1977 if (suc->update_data->references.nr) {
1978 struct string_list_item *item;
1980 for_each_string_list_item(item, &suc->update_data->references)
1981 strvec_pushl(&child->args, "--reference", item->string, NULL);
1983 if (suc->update_data->dissociate)
1984 strvec_push(&child->args, "--dissociate");
1985 if (suc->update_data->single_branch >= 0)
1986 strvec_push(&child->args, suc->update_data->single_branch ?
1987 "--single-branch" :
1988 "--no-single-branch");
1990 cleanup:
1991 free(displaypath);
1992 strbuf_release(&sb);
1993 if (need_free_url)
1994 free((void*)url);
1996 return needs_cloning;
1999 static int update_clone_get_next_task(struct child_process *child,
2000 struct strbuf *err,
2001 void *suc_cb,
2002 void **idx_task_cb)
2004 struct submodule_update_clone *suc = suc_cb;
2005 const struct cache_entry *ce;
2006 int index;
2008 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2009 ce = suc->update_data->list.entries[suc->current];
2010 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2011 int *p = xmalloc(sizeof(*p));
2013 *p = suc->current;
2014 *idx_task_cb = p;
2015 suc->current++;
2016 return 1;
2021 * The loop above tried cloning each submodule once, now try the
2022 * stragglers again, which we can imagine as an extension of the
2023 * entry list.
2025 index = suc->current - suc->update_data->list.nr;
2026 if (index < suc->failed_clones_nr) {
2027 int *p;
2029 ce = suc->failed_clones[index];
2030 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2031 suc->current ++;
2032 strbuf_addstr(err, "BUG: submodule considered for "
2033 "cloning, doesn't need cloning "
2034 "any more?\n");
2035 return 0;
2037 p = xmalloc(sizeof(*p));
2038 *p = suc->current;
2039 *idx_task_cb = p;
2040 suc->current ++;
2041 return 1;
2044 return 0;
2047 static int update_clone_start_failure(struct strbuf *err,
2048 void *suc_cb,
2049 void *idx_task_cb)
2051 struct submodule_update_clone *suc = suc_cb;
2053 suc->quickstop = 1;
2054 return 1;
2057 static int update_clone_task_finished(int result,
2058 struct strbuf *err,
2059 void *suc_cb,
2060 void *idx_task_cb)
2062 const struct cache_entry *ce;
2063 struct submodule_update_clone *suc = suc_cb;
2064 int *idxP = idx_task_cb;
2065 int idx = *idxP;
2067 free(idxP);
2069 if (!result)
2070 return 0;
2072 if (idx < suc->update_data->list.nr) {
2073 ce = suc->update_data->list.entries[idx];
2074 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2075 ce->name);
2076 strbuf_addch(err, '\n');
2077 ALLOC_GROW(suc->failed_clones,
2078 suc->failed_clones_nr + 1,
2079 suc->failed_clones_alloc);
2080 suc->failed_clones[suc->failed_clones_nr++] = ce;
2081 return 0;
2082 } else {
2083 idx -= suc->update_data->list.nr;
2084 ce = suc->failed_clones[idx];
2085 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2086 ce->name);
2087 strbuf_addch(err, '\n');
2088 suc->quickstop = 1;
2089 return 1;
2092 return 0;
2095 static int git_update_clone_config(const char *var, const char *value,
2096 void *cb)
2098 int *max_jobs = cb;
2100 if (!strcmp(var, "submodule.fetchjobs"))
2101 *max_jobs = parse_submodule_fetchjobs(var, value);
2102 return 0;
2105 static int is_tip_reachable(const char *path, const struct object_id *oid)
2107 struct child_process cp = CHILD_PROCESS_INIT;
2108 struct strbuf rev = STRBUF_INIT;
2109 char *hex = oid_to_hex(oid);
2111 cp.git_cmd = 1;
2112 cp.dir = xstrdup(path);
2113 cp.no_stderr = 1;
2114 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2116 prepare_submodule_repo_env(&cp.env);
2118 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2119 return 0;
2121 return 1;
2124 static int fetch_in_submodule(const char *module_path, int depth, int quiet,
2125 const struct object_id *oid)
2127 struct child_process cp = CHILD_PROCESS_INIT;
2129 prepare_submodule_repo_env(&cp.env);
2130 cp.git_cmd = 1;
2131 cp.dir = xstrdup(module_path);
2133 strvec_push(&cp.args, "fetch");
2134 if (quiet)
2135 strvec_push(&cp.args, "--quiet");
2136 if (depth)
2137 strvec_pushf(&cp.args, "--depth=%d", depth);
2138 if (oid) {
2139 char *hex = oid_to_hex(oid);
2140 char *remote = get_default_remote();
2142 strvec_pushl(&cp.args, remote, hex, NULL);
2143 free(remote);
2146 return run_command(&cp);
2149 static int run_update_command(const struct update_data *ud, int subforce)
2151 struct child_process cp = CHILD_PROCESS_INIT;
2152 char *oid = oid_to_hex(&ud->oid);
2153 int ret;
2155 switch (ud->update_strategy.type) {
2156 case SM_UPDATE_CHECKOUT:
2157 cp.git_cmd = 1;
2158 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2159 if (subforce)
2160 strvec_push(&cp.args, "-f");
2161 break;
2162 case SM_UPDATE_REBASE:
2163 cp.git_cmd = 1;
2164 strvec_push(&cp.args, "rebase");
2165 if (ud->quiet)
2166 strvec_push(&cp.args, "--quiet");
2167 break;
2168 case SM_UPDATE_MERGE:
2169 cp.git_cmd = 1;
2170 strvec_push(&cp.args, "merge");
2171 if (ud->quiet)
2172 strvec_push(&cp.args, "--quiet");
2173 break;
2174 case SM_UPDATE_COMMAND:
2175 cp.use_shell = 1;
2176 strvec_push(&cp.args, ud->update_strategy.command);
2177 break;
2178 default:
2179 BUG("unexpected update strategy type: %d",
2180 ud->update_strategy.type);
2182 strvec_push(&cp.args, oid);
2184 cp.dir = xstrdup(ud->sm_path);
2185 prepare_submodule_repo_env(&cp.env);
2186 if ((ret = run_command(&cp))) {
2187 switch (ud->update_strategy.type) {
2188 case SM_UPDATE_CHECKOUT:
2189 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2190 oid, ud->displaypath);
2191 /* No "ret" assignment, use "git checkout"'s */
2192 break;
2193 case SM_UPDATE_REBASE:
2194 ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2195 oid, ud->displaypath);
2196 break;
2197 case SM_UPDATE_MERGE:
2198 ret = die_message(_("Unable to merge '%s' in submodule path '%s'"),
2199 oid, ud->displaypath);
2200 break;
2201 case SM_UPDATE_COMMAND:
2202 ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2203 ud->update_strategy.command, oid, ud->displaypath);
2204 break;
2205 default:
2206 BUG("unexpected update strategy type: %d",
2207 ud->update_strategy.type);
2210 return ret;
2213 if (ud->quiet)
2214 return 0;
2216 switch (ud->update_strategy.type) {
2217 case SM_UPDATE_CHECKOUT:
2218 printf(_("Submodule path '%s': checked out '%s'\n"),
2219 ud->displaypath, oid);
2220 break;
2221 case SM_UPDATE_REBASE:
2222 printf(_("Submodule path '%s': rebased into '%s'\n"),
2223 ud->displaypath, oid);
2224 break;
2225 case SM_UPDATE_MERGE:
2226 printf(_("Submodule path '%s': merged in '%s'\n"),
2227 ud->displaypath, oid);
2228 break;
2229 case SM_UPDATE_COMMAND:
2230 printf(_("Submodule path '%s': '%s %s'\n"),
2231 ud->displaypath, ud->update_strategy.command, oid);
2232 break;
2233 default:
2234 BUG("unexpected update strategy type: %d",
2235 ud->update_strategy.type);
2238 return 0;
2241 static int run_update_procedure(const struct update_data *ud)
2243 int subforce = is_null_oid(&ud->suboid) || ud->force;
2245 if (!ud->nofetch) {
2247 * Run fetch only if `oid` isn't present or it
2248 * is not reachable from a ref.
2250 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2251 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2252 !ud->quiet)
2253 fprintf_ln(stderr,
2254 _("Unable to fetch in submodule path '%s'; "
2255 "trying to directly fetch %s:"),
2256 ud->displaypath, oid_to_hex(&ud->oid));
2258 * Now we tried the usual fetch, but `oid` may
2259 * not be reachable from any of the refs.
2261 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2262 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2263 return die_message(_("Fetched in submodule path '%s', but it did not "
2264 "contain %s. Direct fetching of that commit failed."),
2265 ud->displaypath, oid_to_hex(&ud->oid));
2268 return run_update_command(ud, subforce);
2271 static int remote_submodule_branch(const char *path, const char **branch)
2273 const struct submodule *sub;
2274 char *key;
2275 *branch = NULL;
2277 sub = submodule_from_path(the_repository, null_oid(), path);
2278 if (!sub)
2279 return die_message(_("could not initialize submodule at path '%s'"),
2280 path);
2282 key = xstrfmt("submodule.%s.branch", sub->name);
2283 if (repo_config_get_string_tmp(the_repository, key, branch))
2284 *branch = sub->branch;
2285 free(key);
2287 if (!*branch) {
2288 *branch = "HEAD";
2289 return 0;
2292 if (!strcmp(*branch, ".")) {
2293 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2295 if (!refname)
2296 return die_message(_("No such ref: %s"), "HEAD");
2298 /* detached HEAD */
2299 if (!strcmp(refname, "HEAD"))
2300 return die_message(_("Submodule (%s) branch configured to inherit "
2301 "branch from superproject, but the superproject "
2302 "is not on any branch"), sub->name);
2304 if (!skip_prefix(refname, "refs/heads/", &refname))
2305 return die_message(_("Expecting a full ref name, got %s"),
2306 refname);
2308 *branch = refname;
2309 return 0;
2312 /* Our "branch" is coming from repo_config_get_string_tmp() */
2313 return 0;
2316 static int ensure_core_worktree(const char *path)
2318 const char *cw;
2319 struct repository subrepo;
2321 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2322 return die_message(_("could not get a repository handle for submodule '%s'"),
2323 path);
2325 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2326 char *cfg_file, *abs_path;
2327 const char *rel_path;
2328 struct strbuf sb = STRBUF_INIT;
2330 cfg_file = repo_git_path(&subrepo, "config");
2332 abs_path = absolute_pathdup(path);
2333 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2335 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2337 free(cfg_file);
2338 free(abs_path);
2339 strbuf_release(&sb);
2342 return 0;
2345 static const char *submodule_update_type_to_label(enum submodule_update_type type)
2347 switch (type) {
2348 case SM_UPDATE_CHECKOUT:
2349 return "checkout";
2350 case SM_UPDATE_MERGE:
2351 return "merge";
2352 case SM_UPDATE_REBASE:
2353 return "rebase";
2354 case SM_UPDATE_UNSPECIFIED:
2355 case SM_UPDATE_NONE:
2356 case SM_UPDATE_COMMAND:
2357 break;
2359 BUG("unreachable with type %d", type);
2362 static void update_data_to_args(const struct update_data *update_data,
2363 struct strvec *args)
2365 enum submodule_update_type update_type = update_data->update_default;
2367 if (update_data->displaypath) {
2368 strvec_push(args, "--super-prefix");
2369 strvec_pushf(args, "%s/", update_data->displaypath);
2371 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2372 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2373 if (update_data->quiet)
2374 strvec_push(args, "--quiet");
2375 if (update_data->force)
2376 strvec_push(args, "--force");
2377 if (update_data->init)
2378 strvec_push(args, "--init");
2379 if (update_data->remote)
2380 strvec_push(args, "--remote");
2381 if (update_data->nofetch)
2382 strvec_push(args, "--no-fetch");
2383 if (update_data->dissociate)
2384 strvec_push(args, "--dissociate");
2385 if (update_data->progress)
2386 strvec_push(args, "--progress");
2387 if (update_data->require_init)
2388 strvec_push(args, "--require-init");
2389 if (update_data->depth)
2390 strvec_pushf(args, "--depth=%d", update_data->depth);
2391 if (update_type != SM_UPDATE_UNSPECIFIED)
2392 strvec_pushf(args, "--%s",
2393 submodule_update_type_to_label(update_type));
2395 if (update_data->references.nr) {
2396 struct string_list_item *item;
2398 for_each_string_list_item(item, &update_data->references)
2399 strvec_pushl(args, "--reference", item->string, NULL);
2401 if (update_data->filter_options && update_data->filter_options->choice)
2402 strvec_pushf(args, "--filter=%s",
2403 expand_list_objects_filter_spec(
2404 update_data->filter_options));
2405 if (update_data->recommend_shallow == 0)
2406 strvec_push(args, "--no-recommend-shallow");
2407 else if (update_data->recommend_shallow == 1)
2408 strvec_push(args, "--recommend-shallow");
2409 if (update_data->single_branch >= 0)
2410 strvec_push(args, update_data->single_branch ?
2411 "--single-branch" :
2412 "--no-single-branch");
2415 static int update_submodule(struct update_data *update_data)
2417 int ret;
2419 ret = ensure_core_worktree(update_data->sm_path);
2420 if (ret)
2421 return ret;
2423 update_data->displaypath = get_submodule_displaypath(
2424 update_data->sm_path, update_data->prefix);
2426 ret = determine_submodule_update_strategy(the_repository,
2427 update_data->just_cloned,
2428 update_data->sm_path,
2429 update_data->update_default,
2430 &update_data->update_strategy);
2431 if (ret)
2432 return ret;
2434 if (update_data->just_cloned)
2435 oidcpy(&update_data->suboid, null_oid());
2436 else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2437 return die_message(_("Unable to find current revision in submodule path '%s'"),
2438 update_data->displaypath);
2440 if (update_data->remote) {
2441 char *remote_name;
2442 const char *branch;
2443 char *remote_ref;
2444 int code;
2446 code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2447 if (code)
2448 return code;
2449 code = remote_submodule_branch(update_data->sm_path, &branch);
2450 if (code)
2451 return code;
2452 remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2454 if (!update_data->nofetch) {
2455 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2456 0, NULL))
2457 return die_message(_("Unable to fetch in submodule path '%s'"),
2458 update_data->sm_path);
2461 if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2462 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2463 remote_ref, update_data->sm_path);
2465 free(remote_ref);
2468 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2469 ret = run_update_procedure(update_data);
2470 if (ret)
2471 return ret;
2474 if (update_data->recursive) {
2475 struct child_process cp = CHILD_PROCESS_INIT;
2476 struct update_data next = *update_data;
2478 next.prefix = NULL;
2479 oidcpy(&next.oid, null_oid());
2480 oidcpy(&next.suboid, null_oid());
2482 cp.dir = update_data->sm_path;
2483 cp.git_cmd = 1;
2484 prepare_submodule_repo_env(&cp.env);
2485 update_data_to_args(&next, &cp.args);
2487 ret = run_command(&cp);
2488 if (ret)
2489 die_message(_("Failed to recurse into submodule path '%s'"),
2490 update_data->displaypath);
2491 return ret;
2494 return 0;
2497 static int update_submodules(struct update_data *update_data)
2499 int i, ret = 0;
2500 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2502 suc.update_data = update_data;
2503 run_processes_parallel_tr2(suc.update_data->max_jobs, update_clone_get_next_task,
2504 update_clone_start_failure,
2505 update_clone_task_finished, &suc, "submodule",
2506 "parallel/update");
2509 * We saved the output and put it out all at once now.
2510 * That means:
2511 * - the listener does not have to interleave their (checkout)
2512 * work with our fetching. The writes involved in a
2513 * checkout involve more straightforward sequential I/O.
2514 * - the listener can avoid doing any work if fetching failed.
2516 if (suc.quickstop) {
2517 ret = 1;
2518 goto cleanup;
2521 for (i = 0; i < suc.update_clone_nr; i++) {
2522 struct update_clone_data ucd = suc.update_clone[i];
2523 int code;
2525 oidcpy(&update_data->oid, &ucd.oid);
2526 update_data->just_cloned = ucd.just_cloned;
2527 update_data->sm_path = ucd.sub->path;
2529 code = update_submodule(update_data);
2530 if (!code)
2531 continue;
2532 ret = code;
2533 if (ret == 128)
2534 goto cleanup;
2537 cleanup:
2538 string_list_clear(&update_data->references, 0);
2539 return ret;
2542 static int module_update(int argc, const char **argv, const char *prefix)
2544 struct pathspec pathspec;
2545 struct update_data opt = UPDATE_DATA_INIT;
2546 struct list_objects_filter_options filter_options = { 0 };
2547 int ret;
2548 struct option module_update_options[] = {
2549 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2550 OPT_BOOL(0, "init", &opt.init,
2551 N_("initialize uninitialized submodules before update")),
2552 OPT_BOOL(0, "remote", &opt.remote,
2553 N_("use SHA-1 of submodule's remote tracking branch")),
2554 OPT_BOOL(0, "recursive", &opt.recursive,
2555 N_("traverse submodules recursively")),
2556 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2557 N_("don't fetch new objects from the remote site")),
2558 OPT_STRING(0, "prefix", &opt.prefix,
2559 N_("path"),
2560 N_("path into the working tree")),
2561 OPT_SET_INT(0, "checkout", &opt.update_default,
2562 N_("use the 'checkout' update strategy (default)"),
2563 SM_UPDATE_CHECKOUT),
2564 OPT_SET_INT('m', "merge", &opt.update_default,
2565 N_("use the 'merge' update strategy"),
2566 SM_UPDATE_MERGE),
2567 OPT_SET_INT('r', "rebase", &opt.update_default,
2568 N_("use the 'rebase' update strategy"),
2569 SM_UPDATE_REBASE),
2570 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2571 N_("reference repository")),
2572 OPT_BOOL(0, "dissociate", &opt.dissociate,
2573 N_("use --reference only while cloning")),
2574 OPT_INTEGER(0, "depth", &opt.depth,
2575 N_("create a shallow clone truncated to the "
2576 "specified number of revisions")),
2577 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2578 N_("parallel jobs")),
2579 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2580 N_("whether the initial clone should follow the shallow recommendation")),
2581 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2582 OPT_BOOL(0, "progress", &opt.progress,
2583 N_("force cloning progress")),
2584 OPT_BOOL(0, "require-init", &opt.require_init,
2585 N_("disallow cloning into non-empty directory, implies --init")),
2586 OPT_BOOL(0, "single-branch", &opt.single_branch,
2587 N_("clone only one branch, HEAD or --branch")),
2588 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2589 OPT_END()
2591 const char *const git_submodule_helper_usage[] = {
2592 N_("git submodule [--quiet] update"
2593 " [--init [--filter=<filter-spec>]] [--remote]"
2594 " [-N|--no-fetch] [-f|--force]"
2595 " [--checkout|--merge|--rebase]"
2596 " [--[no-]recommend-shallow] [--reference <repository>]"
2597 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2598 NULL
2601 update_clone_config_from_gitmodules(&opt.max_jobs);
2602 git_config(git_update_clone_config, &opt.max_jobs);
2604 argc = parse_options(argc, argv, prefix, module_update_options,
2605 git_submodule_helper_usage, 0);
2607 if (opt.require_init)
2608 opt.init = 1;
2610 if (filter_options.choice && !opt.init) {
2611 usage_with_options(git_submodule_helper_usage,
2612 module_update_options);
2615 opt.filter_options = &filter_options;
2617 if (opt.update_default)
2618 opt.update_strategy.type = opt.update_default;
2620 if (module_list_compute(argc, argv, prefix, &pathspec, &opt.list) < 0) {
2621 list_objects_filter_release(&filter_options);
2622 return 1;
2625 if (pathspec.nr)
2626 opt.warn_if_uninitialized = 1;
2628 if (opt.init) {
2629 struct module_list list = MODULE_LIST_INIT;
2630 struct init_cb info = INIT_CB_INIT;
2632 if (module_list_compute(argc, argv, opt.prefix,
2633 &pathspec, &list) < 0)
2634 return 1;
2637 * If there are no path args and submodule.active is set then,
2638 * by default, only initialize 'active' modules.
2640 if (!argc && git_config_get_value_multi("submodule.active"))
2641 module_list_active(&list);
2643 info.prefix = opt.prefix;
2644 if (opt.quiet)
2645 info.flags |= OPT_QUIET;
2647 for_each_listed_submodule(&list, init_submodule_cb, &info);
2650 ret = update_submodules(&opt);
2651 list_objects_filter_release(&filter_options);
2652 return ret;
2655 static int push_check(int argc, const char **argv, const char *prefix)
2657 struct remote *remote;
2658 const char *superproject_head;
2659 char *head;
2660 int detached_head = 0;
2661 struct object_id head_oid;
2663 if (argc < 3)
2664 die("submodule--helper push-check requires at least 2 arguments");
2667 * superproject's resolved head ref.
2668 * if HEAD then the superproject is in a detached head state, otherwise
2669 * it will be the resolved head ref.
2671 superproject_head = argv[1];
2672 argv++;
2673 argc--;
2674 /* Get the submodule's head ref and determine if it is detached */
2675 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2676 if (!head)
2677 die(_("Failed to resolve HEAD as a valid ref."));
2678 if (!strcmp(head, "HEAD"))
2679 detached_head = 1;
2682 * The remote must be configured.
2683 * This is to avoid pushing to the exact same URL as the parent.
2685 remote = pushremote_get(argv[1]);
2686 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2687 die("remote '%s' not configured", argv[1]);
2689 /* Check the refspec */
2690 if (argc > 2) {
2691 int i;
2692 struct ref *local_refs = get_local_heads();
2693 struct refspec refspec = REFSPEC_INIT_PUSH;
2695 refspec_appendn(&refspec, argv + 2, argc - 2);
2697 for (i = 0; i < refspec.nr; i++) {
2698 const struct refspec_item *rs = &refspec.items[i];
2700 if (rs->pattern || rs->matching)
2701 continue;
2703 /* LHS must match a single ref */
2704 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2705 case 1:
2706 break;
2707 case 0:
2709 * If LHS matches 'HEAD' then we need to ensure
2710 * that it matches the same named branch
2711 * checked out in the superproject.
2713 if (!strcmp(rs->src, "HEAD")) {
2714 if (!detached_head &&
2715 !strcmp(head, superproject_head))
2716 break;
2717 die("HEAD does not match the named branch in the superproject");
2719 /* fallthrough */
2720 default:
2721 die("src refspec '%s' must name a ref",
2722 rs->src);
2725 refspec_clear(&refspec);
2727 free(head);
2729 return 0;
2732 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2734 int i;
2735 struct pathspec pathspec;
2736 struct module_list list = MODULE_LIST_INIT;
2737 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2738 struct option embed_gitdir_options[] = {
2739 OPT_STRING(0, "prefix", &prefix,
2740 N_("path"),
2741 N_("path into the working tree")),
2742 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2743 ABSORB_GITDIR_RECURSE_SUBMODULES),
2744 OPT_END()
2746 const char *const git_submodule_helper_usage[] = {
2747 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2748 NULL
2751 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2752 git_submodule_helper_usage, 0);
2754 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2755 return 1;
2757 for (i = 0; i < list.nr; i++)
2758 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2760 return 0;
2763 static int module_config(int argc, const char **argv, const char *prefix)
2765 enum {
2766 CHECK_WRITEABLE = 1,
2767 DO_UNSET = 2
2768 } command = 0;
2769 struct option module_config_options[] = {
2770 OPT_CMDMODE(0, "check-writeable", &command,
2771 N_("check if it is safe to write to the .gitmodules file"),
2772 CHECK_WRITEABLE),
2773 OPT_CMDMODE(0, "unset", &command,
2774 N_("unset the config in the .gitmodules file"),
2775 DO_UNSET),
2776 OPT_END()
2778 const char *const git_submodule_helper_usage[] = {
2779 N_("git submodule--helper config <name> [<value>]"),
2780 N_("git submodule--helper config --unset <name>"),
2781 "git submodule--helper config --check-writeable",
2782 NULL
2785 argc = parse_options(argc, argv, prefix, module_config_options,
2786 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2788 if (argc == 1 && command == CHECK_WRITEABLE)
2789 return is_writing_gitmodules_ok() ? 0 : -1;
2791 /* Equivalent to ACTION_GET in builtin/config.c */
2792 if (argc == 2 && command != DO_UNSET)
2793 return print_config_from_gitmodules(the_repository, argv[1]);
2795 /* Equivalent to ACTION_SET in builtin/config.c */
2796 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2797 const char *value = (argc == 3) ? argv[2] : NULL;
2799 if (!is_writing_gitmodules_ok())
2800 die(_("please make sure that the .gitmodules file is in the working tree"));
2802 return config_set_in_gitmodules_file_gently(argv[1], value);
2805 usage_with_options(git_submodule_helper_usage, module_config_options);
2808 static int module_set_url(int argc, const char **argv, const char *prefix)
2810 int quiet = 0;
2811 const char *newurl;
2812 const char *path;
2813 char *config_name;
2814 struct option options[] = {
2815 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2816 OPT_END()
2818 const char *const usage[] = {
2819 N_("git submodule set-url [--quiet] <path> <newurl>"),
2820 NULL
2823 argc = parse_options(argc, argv, prefix, options, usage, 0);
2825 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2826 usage_with_options(usage, options);
2828 config_name = xstrfmt("submodule.%s.url", path);
2830 config_set_in_gitmodules_file_gently(config_name, newurl);
2831 sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
2833 free(config_name);
2835 return 0;
2838 static int module_set_branch(int argc, const char **argv, const char *prefix)
2840 int opt_default = 0, ret;
2841 const char *opt_branch = NULL;
2842 const char *path;
2843 char *config_name;
2844 struct option options[] = {
2846 * We accept the `quiet` option for uniformity across subcommands,
2847 * though there is nothing to make less verbose in this subcommand.
2849 OPT_NOOP_NOARG('q', "quiet"),
2851 OPT_BOOL('d', "default", &opt_default,
2852 N_("set the default tracking branch to master")),
2853 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2854 N_("set the default tracking branch")),
2855 OPT_END()
2857 const char *const usage[] = {
2858 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2859 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2860 NULL
2863 argc = parse_options(argc, argv, prefix, options, usage, 0);
2865 if (!opt_branch && !opt_default)
2866 die(_("--branch or --default required"));
2868 if (opt_branch && opt_default)
2869 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2871 if (argc != 1 || !(path = argv[0]))
2872 usage_with_options(usage, options);
2874 config_name = xstrfmt("submodule.%s.branch", path);
2875 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2877 free(config_name);
2878 return !!ret;
2881 static int module_create_branch(int argc, const char **argv, const char *prefix)
2883 enum branch_track track;
2884 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
2885 struct option options[] = {
2886 OPT__QUIET(&quiet, N_("print only error messages")),
2887 OPT__FORCE(&force, N_("force creation"), 0),
2888 OPT_BOOL(0, "create-reflog", &reflog,
2889 N_("create the branch's reflog")),
2890 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
2891 N_("set branch tracking configuration"),
2892 PARSE_OPT_OPTARG,
2893 parse_opt_tracking_mode),
2894 OPT__DRY_RUN(&dry_run,
2895 N_("show whether the branch would be created")),
2896 OPT_END()
2898 const char *const usage[] = {
2899 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2900 NULL
2903 git_config(git_default_config, NULL);
2904 track = git_branch_track;
2905 argc = parse_options(argc, argv, prefix, options, usage, 0);
2907 if (argc != 3)
2908 usage_with_options(usage, options);
2910 if (!quiet && !dry_run)
2911 printf_ln(_("creating branch '%s'"), argv[0]);
2913 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
2914 force, reflog, quiet, track, dry_run);
2915 return 0;
2918 struct add_data {
2919 const char *prefix;
2920 const char *branch;
2921 const char *reference_path;
2922 char *sm_path;
2923 const char *sm_name;
2924 const char *repo;
2925 const char *realrepo;
2926 int depth;
2927 unsigned int force: 1;
2928 unsigned int quiet: 1;
2929 unsigned int progress: 1;
2930 unsigned int dissociate: 1;
2932 #define ADD_DATA_INIT { .depth = -1 }
2934 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
2936 struct child_process cp_remote = CHILD_PROCESS_INIT;
2937 struct strbuf sb_remote_out = STRBUF_INIT;
2939 cp_remote.git_cmd = 1;
2940 strvec_pushf(&cp_remote.env,
2941 "GIT_DIR=%s", git_dir_path);
2942 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
2943 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
2944 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
2945 char *next_line;
2946 char *line = sb_remote_out.buf;
2948 while ((next_line = strchr(line, '\n')) != NULL) {
2949 size_t len = next_line - line;
2951 if (strip_suffix_mem(line, &len, " (fetch)"))
2952 strbuf_addf(msg, " %.*s\n", (int)len, line);
2953 line = next_line + 1;
2957 strbuf_release(&sb_remote_out);
2960 static int add_submodule(const struct add_data *add_data)
2962 char *submod_gitdir_path;
2963 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
2964 struct string_list reference = STRING_LIST_INIT_NODUP;
2966 /* perhaps the path already exists and is already a git repo, else clone it */
2967 if (is_directory(add_data->sm_path)) {
2968 struct strbuf sm_path = STRBUF_INIT;
2969 strbuf_addstr(&sm_path, add_data->sm_path);
2970 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
2971 if (is_nonbare_repository_dir(&sm_path))
2972 printf(_("Adding existing repo at '%s' to the index\n"),
2973 add_data->sm_path);
2974 else
2975 die(_("'%s' already exists and is not a valid git repo"),
2976 add_data->sm_path);
2977 strbuf_release(&sm_path);
2978 free(submod_gitdir_path);
2979 } else {
2980 struct child_process cp = CHILD_PROCESS_INIT;
2982 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
2984 if (is_directory(submod_gitdir_path)) {
2985 if (!add_data->force) {
2986 struct strbuf msg = STRBUF_INIT;
2987 char *die_msg;
2989 strbuf_addf(&msg, _("A git directory for '%s' is found "
2990 "locally with remote(s):\n"),
2991 add_data->sm_name);
2993 append_fetch_remotes(&msg, submod_gitdir_path);
2994 free(submod_gitdir_path);
2996 strbuf_addf(&msg, _("If you want to reuse this local git "
2997 "directory instead of cloning again from\n"
2998 " %s\n"
2999 "use the '--force' option. If the local git "
3000 "directory is not the correct repo\n"
3001 "or you are unsure what this means choose "
3002 "another name with the '--name' option."),
3003 add_data->realrepo);
3005 die_msg = strbuf_detach(&msg, NULL);
3006 die("%s", die_msg);
3007 } else {
3008 printf(_("Reactivating local git directory for "
3009 "submodule '%s'\n"), add_data->sm_name);
3012 free(submod_gitdir_path);
3014 clone_data.prefix = add_data->prefix;
3015 clone_data.path = add_data->sm_path;
3016 clone_data.name = add_data->sm_name;
3017 clone_data.url = add_data->realrepo;
3018 clone_data.quiet = add_data->quiet;
3019 clone_data.progress = add_data->progress;
3020 if (add_data->reference_path)
3021 string_list_append(&reference,
3022 xstrdup(add_data->reference_path));
3023 clone_data.dissociate = add_data->dissociate;
3024 if (add_data->depth >= 0)
3025 clone_data.depth = xstrfmt("%d", add_data->depth);
3027 if (clone_submodule(&clone_data, &reference))
3028 return -1;
3030 prepare_submodule_repo_env(&cp.env);
3031 cp.git_cmd = 1;
3032 cp.dir = add_data->sm_path;
3034 * NOTE: we only get here if add_data->force is true, so
3035 * passing --force to checkout is reasonable.
3037 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3039 if (add_data->branch) {
3040 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3041 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3044 if (run_command(&cp))
3045 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3047 return 0;
3050 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3052 char *key;
3053 int ret;
3055 if (!is_writing_gitmodules_ok())
3056 die(_("please make sure that the .gitmodules file is in the working tree"));
3058 key = xstrfmt("submodule.%s.%s", name, var);
3059 ret = config_set_in_gitmodules_file_gently(key, value);
3060 free(key);
3062 return ret;
3065 static void configure_added_submodule(struct add_data *add_data)
3067 char *key;
3068 char *val = NULL;
3069 struct child_process add_submod = CHILD_PROCESS_INIT;
3070 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3072 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3073 git_config_set_gently(key, add_data->realrepo);
3074 free(key);
3076 add_submod.git_cmd = 1;
3077 strvec_pushl(&add_submod.args, "add",
3078 "--no-warn-embedded-repo", NULL);
3079 if (add_data->force)
3080 strvec_push(&add_submod.args, "--force");
3081 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3083 if (run_command(&add_submod))
3084 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3086 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3087 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3088 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3090 if (add_data->branch) {
3091 if (config_submodule_in_gitmodules(add_data->sm_name,
3092 "branch", add_data->branch))
3093 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3096 add_gitmodules.git_cmd = 1;
3097 strvec_pushl(&add_gitmodules.args,
3098 "add", "--force", "--", ".gitmodules", NULL);
3100 if (run_command(&add_gitmodules))
3101 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3104 * NEEDSWORK: In a multi-working-tree world this needs to be
3105 * set in the per-worktree config.
3108 * NEEDSWORK: In the longer run, we need to get rid of this
3109 * pattern of querying "submodule.active" before calling
3110 * is_submodule_active(), since that function needs to find
3111 * out the value of "submodule.active" again anyway.
3113 if (!git_config_get_string("submodule.active", &val)) {
3115 * If the submodule being added isn't already covered by the
3116 * current configured pathspec, set the submodule's active flag
3118 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3119 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3120 git_config_set_gently(key, "true");
3121 free(key);
3123 } else {
3124 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3125 git_config_set_gently(key, "true");
3126 free(key);
3130 static void die_on_index_match(const char *path, int force)
3132 struct pathspec ps;
3133 const char *args[] = { path, NULL };
3134 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3136 if (read_cache_preload(NULL) < 0)
3137 die(_("index file corrupt"));
3139 if (ps.nr) {
3140 int i;
3141 char *ps_matched = xcalloc(ps.nr, 1);
3143 /* TODO: audit for interaction with sparse-index. */
3144 ensure_full_index(&the_index);
3147 * Since there is only one pathspec, we just need
3148 * need to check ps_matched[0] to know if a cache
3149 * entry matched.
3151 for (i = 0; i < active_nr; i++) {
3152 ce_path_match(&the_index, active_cache[i], &ps,
3153 ps_matched);
3155 if (ps_matched[0]) {
3156 if (!force)
3157 die(_("'%s' already exists in the index"),
3158 path);
3159 if (!S_ISGITLINK(active_cache[i]->ce_mode))
3160 die(_("'%s' already exists in the index "
3161 "and is not a submodule"), path);
3162 break;
3165 free(ps_matched);
3167 clear_pathspec(&ps);
3170 static void die_on_repo_without_commits(const char *path)
3172 struct strbuf sb = STRBUF_INIT;
3173 strbuf_addstr(&sb, path);
3174 if (is_nonbare_repository_dir(&sb)) {
3175 struct object_id oid;
3176 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3177 die(_("'%s' does not have a commit checked out"), path);
3179 strbuf_release(&sb);
3182 static int module_add(int argc, const char **argv, const char *prefix)
3184 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3185 struct add_data add_data = ADD_DATA_INIT;
3186 char *to_free = NULL;
3187 struct option options[] = {
3188 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3189 N_("branch of repository to add as submodule")),
3190 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3191 PARSE_OPT_NOCOMPLETE),
3192 OPT__QUIET(&quiet, N_("print only error messages")),
3193 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3194 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3195 N_("reference repository")),
3196 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3197 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3198 N_("sets the submodule's name to the given string "
3199 "instead of defaulting to its path")),
3200 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3201 OPT_END()
3203 const char *const usage[] = {
3204 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3205 NULL
3208 argc = parse_options(argc, argv, prefix, options, usage, 0);
3210 if (!is_writing_gitmodules_ok())
3211 die(_("please make sure that the .gitmodules file is in the working tree"));
3213 if (prefix && *prefix &&
3214 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3215 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3217 if (argc == 0 || argc > 2)
3218 usage_with_options(usage, options);
3220 add_data.repo = argv[0];
3221 if (argc == 1)
3222 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3223 else
3224 add_data.sm_path = xstrdup(argv[1]);
3226 if (prefix && *prefix && !is_absolute_path(add_data.sm_path))
3227 add_data.sm_path = xstrfmt("%s%s", prefix, add_data.sm_path);
3229 if (starts_with_dot_dot_slash(add_data.repo) ||
3230 starts_with_dot_slash(add_data.repo)) {
3231 if (prefix)
3232 die(_("Relative path can only be used from the toplevel "
3233 "of the working tree"));
3235 /* dereference source url relative to parent's url */
3236 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3237 add_data.realrepo = to_free;
3238 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3239 add_data.realrepo = add_data.repo;
3240 } else {
3241 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3242 add_data.repo);
3246 * normalize path:
3247 * multiple //; leading ./; /./; /../;
3249 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3250 strip_dir_trailing_slashes(add_data.sm_path);
3252 die_on_index_match(add_data.sm_path, force);
3253 die_on_repo_without_commits(add_data.sm_path);
3255 if (!force) {
3256 int exit_code = -1;
3257 struct strbuf sb = STRBUF_INIT;
3258 struct child_process cp = CHILD_PROCESS_INIT;
3260 cp.git_cmd = 1;
3261 cp.no_stdout = 1;
3262 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3263 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3264 if ((exit_code = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3265 strbuf_complete_line(&sb);
3266 fputs(sb.buf, stderr);
3267 free(add_data.sm_path);
3268 return exit_code;
3270 strbuf_release(&sb);
3273 if(!add_data.sm_name)
3274 add_data.sm_name = add_data.sm_path;
3276 if (check_submodule_name(add_data.sm_name))
3277 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3279 add_data.prefix = prefix;
3280 add_data.force = !!force;
3281 add_data.quiet = !!quiet;
3282 add_data.progress = !!progress;
3283 add_data.dissociate = !!dissociate;
3285 if (add_submodule(&add_data)) {
3286 free(add_data.sm_path);
3287 return 1;
3289 configure_added_submodule(&add_data);
3290 free(add_data.sm_path);
3291 free(to_free);
3293 return 0;
3296 #define SUPPORT_SUPER_PREFIX (1<<0)
3298 struct cmd_struct {
3299 const char *cmd;
3300 int (*fn)(int, const char **, const char *);
3301 unsigned option;
3304 static struct cmd_struct commands[] = {
3305 {"clone", module_clone, SUPPORT_SUPER_PREFIX},
3306 {"add", module_add, 0},
3307 {"update", module_update, SUPPORT_SUPER_PREFIX},
3308 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
3309 {"init", module_init, 0},
3310 {"status", module_status, SUPPORT_SUPER_PREFIX},
3311 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
3312 {"deinit", module_deinit, 0},
3313 {"summary", module_summary, 0},
3314 {"push-check", push_check, 0},
3315 {"absorbgitdirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3316 {"config", module_config, 0},
3317 {"set-url", module_set_url, 0},
3318 {"set-branch", module_set_branch, 0},
3319 {"create-branch", module_create_branch, 0},
3322 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3324 int i;
3325 if (argc < 2 || !strcmp(argv[1], "-h"))
3326 usage("git submodule--helper <command>");
3328 for (i = 0; i < ARRAY_SIZE(commands); i++) {
3329 if (!strcmp(argv[1], commands[i].cmd)) {
3330 if (get_super_prefix() &&
3331 !(commands[i].option & SUPPORT_SUPER_PREFIX))
3332 die(_("%s doesn't support --super-prefix"),
3333 commands[i].cmd);
3334 return commands[i].fn(argc - 1, argv + 1, prefix);
3338 die(_("'%s' is not a valid submodule--helper "
3339 "subcommand"), argv[1]);