Merge tag 'v2.39.2' into debian-sid
[git/debian.git] / builtin / submodule--helper.c
blob05f2c9bc9858d98ae855b566d6128030e002fa18
1 #define USE_THE_INDEX_VARIABLE
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;
68 int ret;
70 if (repo_submodule_init(&subrepo, the_repository, module_path,
71 null_oid()) < 0)
72 return die_message(_("could not get a repository handle for submodule '%s'"),
73 module_path);
74 ret = repo_get_default_remote(&subrepo, default_remote);
75 repo_clear(&subrepo);
77 return ret;
80 static char *get_default_remote(void)
82 char *default_remote;
83 int code = repo_get_default_remote(the_repository, &default_remote);
85 if (code)
86 exit(code);
88 return default_remote;
91 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
93 char *remoteurl, *resolved_url;
94 char *remote = get_default_remote();
95 struct strbuf remotesb = STRBUF_INIT;
97 strbuf_addf(&remotesb, "remote.%s.url", remote);
98 if (git_config_get_string(remotesb.buf, &remoteurl)) {
99 if (!quiet)
100 warning(_("could not look up configuration '%s'. "
101 "Assuming this repository is its own "
102 "authoritative upstream."),
103 remotesb.buf);
104 remoteurl = xgetcwd();
106 resolved_url = relative_url(remoteurl, rel_url, up_path);
108 free(remote);
109 free(remoteurl);
110 strbuf_release(&remotesb);
112 return resolved_url;
115 /* the result should be freed by the caller. */
116 static char *get_submodule_displaypath(const char *path, const char *prefix)
118 const char *super_prefix = get_super_prefix();
120 if (prefix && super_prefix) {
121 BUG("cannot have prefix '%s' and superprefix '%s'",
122 prefix, super_prefix);
123 } else if (prefix) {
124 struct strbuf sb = STRBUF_INIT;
125 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
126 strbuf_release(&sb);
127 return displaypath;
128 } else if (super_prefix) {
129 return xstrfmt("%s%s", super_prefix, path);
130 } else {
131 return xstrdup(path);
135 static char *compute_rev_name(const char *sub_path, const char* object_id)
137 struct strbuf sb = STRBUF_INIT;
138 const char ***d;
140 static const char *describe_bare[] = { NULL };
142 static const char *describe_tags[] = { "--tags", NULL };
144 static const char *describe_contains[] = { "--contains", NULL };
146 static const char *describe_all_always[] = { "--all", "--always", NULL };
148 static const char **describe_argv[] = { describe_bare, describe_tags,
149 describe_contains,
150 describe_all_always, NULL };
152 for (d = describe_argv; *d; d++) {
153 struct child_process cp = CHILD_PROCESS_INIT;
154 prepare_submodule_repo_env(&cp.env);
155 cp.dir = sub_path;
156 cp.git_cmd = 1;
157 cp.no_stderr = 1;
159 strvec_push(&cp.args, "describe");
160 strvec_pushv(&cp.args, *d);
161 strvec_push(&cp.args, object_id);
163 if (!capture_command(&cp, &sb, 0)) {
164 strbuf_strip_suffix(&sb, "\n");
165 return strbuf_detach(&sb, NULL);
169 strbuf_release(&sb);
170 return NULL;
173 struct module_list {
174 const struct cache_entry **entries;
175 int alloc, nr;
177 #define MODULE_LIST_INIT { 0 }
179 static void module_list_release(struct module_list *ml)
181 free(ml->entries);
184 static int module_list_compute(const char **argv,
185 const char *prefix,
186 struct pathspec *pathspec,
187 struct module_list *list)
189 int i, result = 0;
190 char *ps_matched = NULL;
192 parse_pathspec(pathspec, 0,
193 PATHSPEC_PREFER_FULL,
194 prefix, argv);
196 if (pathspec->nr)
197 ps_matched = xcalloc(pathspec->nr, 1);
199 if (repo_read_index(the_repository) < 0)
200 die(_("index file corrupt"));
202 for (i = 0; i < the_index.cache_nr; i++) {
203 const struct cache_entry *ce = the_index.cache[i];
205 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
206 0, ps_matched, 1) ||
207 !S_ISGITLINK(ce->ce_mode))
208 continue;
210 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
211 list->entries[list->nr++] = ce;
212 while (i + 1 < the_index.cache_nr &&
213 !strcmp(ce->name, the_index.cache[i + 1]->name))
215 * Skip entries with the same name in different stages
216 * to make sure an entry is returned only once.
218 i++;
221 if (ps_matched && report_path_error(ps_matched, pathspec))
222 result = -1;
224 free(ps_matched);
226 return result;
229 static void module_list_active(struct module_list *list)
231 int i;
232 struct module_list active_modules = MODULE_LIST_INIT;
234 for (i = 0; i < list->nr; i++) {
235 const struct cache_entry *ce = list->entries[i];
237 if (!is_submodule_active(the_repository, ce->name))
238 continue;
240 ALLOC_GROW(active_modules.entries,
241 active_modules.nr + 1,
242 active_modules.alloc);
243 active_modules.entries[active_modules.nr++] = ce;
246 module_list_release(list);
247 *list = active_modules;
250 static char *get_up_path(const char *path)
252 int i;
253 struct strbuf sb = STRBUF_INIT;
255 for (i = count_slashes(path); i; i--)
256 strbuf_addstr(&sb, "../");
259 * Check if 'path' ends with slash or not
260 * for having the same output for dir/sub_dir
261 * and dir/sub_dir/
263 if (!is_dir_sep(path[strlen(path) - 1]))
264 strbuf_addstr(&sb, "../");
266 return strbuf_detach(&sb, NULL);
269 static void for_each_listed_submodule(const struct module_list *list,
270 each_submodule_fn fn, void *cb_data)
272 int i;
274 for (i = 0; i < list->nr; i++)
275 fn(list->entries[i], cb_data);
278 struct foreach_cb {
279 int argc;
280 const char **argv;
281 const char *prefix;
282 int quiet;
283 int recursive;
285 #define FOREACH_CB_INIT { 0 }
287 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
288 void *cb_data)
290 struct foreach_cb *info = cb_data;
291 const char *path = list_item->name;
292 const struct object_id *ce_oid = &list_item->oid;
293 const struct submodule *sub;
294 struct child_process cp = CHILD_PROCESS_INIT;
295 char *displaypath;
297 displaypath = get_submodule_displaypath(path, info->prefix);
299 sub = submodule_from_path(the_repository, null_oid(), path);
301 if (!sub)
302 die(_("No url found for submodule path '%s' in .gitmodules"),
303 displaypath);
305 if (!is_submodule_populated_gently(path, NULL))
306 goto cleanup;
308 prepare_submodule_repo_env(&cp.env);
311 * For the purpose of executing <command> in the submodule,
312 * separate shell is used for the purpose of running the
313 * child process.
315 cp.use_shell = 1;
316 cp.dir = path;
319 * NEEDSWORK: the command currently has access to the variables $name,
320 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
321 * contains a single argument. This is done for maintaining a faithful
322 * translation from shell script.
324 if (info->argc == 1) {
325 char *toplevel = xgetcwd();
326 struct strbuf sb = STRBUF_INIT;
328 strvec_pushf(&cp.env, "name=%s", sub->name);
329 strvec_pushf(&cp.env, "sm_path=%s", path);
330 strvec_pushf(&cp.env, "displaypath=%s", displaypath);
331 strvec_pushf(&cp.env, "sha1=%s",
332 oid_to_hex(ce_oid));
333 strvec_pushf(&cp.env, "toplevel=%s", toplevel);
336 * Since the path variable was accessible from the script
337 * before porting, it is also made available after porting.
338 * The environment variable "PATH" has a very special purpose
339 * on windows. And since environment variables are
340 * case-insensitive in windows, it interferes with the
341 * existing PATH variable. Hence, to avoid that, we expose
342 * path via the args strvec and not via env.
344 sq_quote_buf(&sb, path);
345 strvec_pushf(&cp.args, "path=%s; %s",
346 sb.buf, info->argv[0]);
347 strbuf_release(&sb);
348 free(toplevel);
349 } else {
350 strvec_pushv(&cp.args, info->argv);
353 if (!info->quiet)
354 printf(_("Entering '%s'\n"), displaypath);
356 if (info->argv[0] && run_command(&cp))
357 die(_("run_command returned non-zero status for %s\n."),
358 displaypath);
360 if (info->recursive) {
361 struct child_process cpr = CHILD_PROCESS_INIT;
363 cpr.git_cmd = 1;
364 cpr.dir = path;
365 prepare_submodule_repo_env(&cpr.env);
367 strvec_pushl(&cpr.args, "--super-prefix", NULL);
368 strvec_pushf(&cpr.args, "%s/", displaypath);
369 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
370 NULL);
372 if (info->quiet)
373 strvec_push(&cpr.args, "--quiet");
375 strvec_push(&cpr.args, "--");
376 strvec_pushv(&cpr.args, info->argv);
378 if (run_command(&cpr))
379 die(_("run_command returned non-zero status while "
380 "recursing in the nested submodules of %s\n."),
381 displaypath);
384 cleanup:
385 free(displaypath);
388 static int module_foreach(int argc, const char **argv, const char *prefix)
390 struct foreach_cb info = FOREACH_CB_INIT;
391 struct pathspec pathspec = { 0 };
392 struct module_list list = MODULE_LIST_INIT;
393 struct option module_foreach_options[] = {
394 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
395 OPT_BOOL(0, "recursive", &info.recursive,
396 N_("recurse into nested submodules")),
397 OPT_END()
399 const char *const git_submodule_helper_usage[] = {
400 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
401 NULL
403 int ret = 1;
405 argc = parse_options(argc, argv, prefix, module_foreach_options,
406 git_submodule_helper_usage, 0);
408 if (module_list_compute(NULL, prefix, &pathspec, &list) < 0)
409 goto cleanup;
411 info.argc = argc;
412 info.argv = argv;
413 info.prefix = prefix;
415 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
417 ret = 0;
418 cleanup:
419 module_list_release(&list);
420 clear_pathspec(&pathspec);
421 return ret;
424 static int starts_with_dot_slash(const char *const path)
426 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
427 PATH_MATCH_XPLATFORM);
430 static int starts_with_dot_dot_slash(const char *const path)
432 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
433 PATH_MATCH_XPLATFORM);
436 struct init_cb {
437 const char *prefix;
438 unsigned int flags;
440 #define INIT_CB_INIT { 0 }
442 static void init_submodule(const char *path, const char *prefix,
443 unsigned int flags)
445 const struct submodule *sub;
446 struct strbuf sb = STRBUF_INIT;
447 const char *upd;
448 char *url = NULL, *displaypath;
450 displaypath = get_submodule_displaypath(path, prefix);
452 sub = submodule_from_path(the_repository, null_oid(), path);
454 if (!sub)
455 die(_("No url found for submodule path '%s' in .gitmodules"),
456 displaypath);
459 * NEEDSWORK: In a multi-working-tree world, this needs to be
460 * set in the per-worktree config.
462 * Set active flag for the submodule being initialized
464 if (!is_submodule_active(the_repository, path)) {
465 strbuf_addf(&sb, "submodule.%s.active", sub->name);
466 git_config_set_gently(sb.buf, "true");
467 strbuf_reset(&sb);
471 * Copy url setting when it is not set yet.
472 * To look up the url in .git/config, we must not fall back to
473 * .gitmodules, so look it up directly.
475 strbuf_addf(&sb, "submodule.%s.url", sub->name);
476 if (git_config_get_string(sb.buf, &url)) {
477 if (!sub->url)
478 die(_("No url found for submodule path '%s' in .gitmodules"),
479 displaypath);
481 url = xstrdup(sub->url);
483 /* Possibly a url relative to parent */
484 if (starts_with_dot_dot_slash(url) ||
485 starts_with_dot_slash(url)) {
486 char *oldurl = url;
488 url = resolve_relative_url(oldurl, NULL, 0);
489 free(oldurl);
492 if (git_config_set_gently(sb.buf, url))
493 die(_("Failed to register url for submodule path '%s'"),
494 displaypath);
495 if (!(flags & OPT_QUIET))
496 fprintf(stderr,
497 _("Submodule '%s' (%s) registered for path '%s'\n"),
498 sub->name, url, displaypath);
500 strbuf_reset(&sb);
502 /* Copy "update" setting when it is not set yet */
503 strbuf_addf(&sb, "submodule.%s.update", sub->name);
504 if (git_config_get_string_tmp(sb.buf, &upd) &&
505 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
506 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
507 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
508 sub->name);
509 upd = "none";
510 } else {
511 upd = submodule_update_type_to_string(sub->update_strategy.type);
514 if (git_config_set_gently(sb.buf, upd))
515 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
517 strbuf_release(&sb);
518 free(displaypath);
519 free(url);
522 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
524 struct init_cb *info = cb_data;
526 init_submodule(list_item->name, info->prefix, info->flags);
529 static int module_init(int argc, const char **argv, const char *prefix)
531 struct init_cb info = INIT_CB_INIT;
532 struct pathspec pathspec = { 0 };
533 struct module_list list = MODULE_LIST_INIT;
534 int quiet = 0;
535 struct option module_init_options[] = {
536 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
537 OPT_END()
539 const char *const git_submodule_helper_usage[] = {
540 N_("git submodule init [<options>] [<path>]"),
541 NULL
543 int ret = 1;
545 argc = parse_options(argc, argv, prefix, module_init_options,
546 git_submodule_helper_usage, 0);
548 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
549 goto cleanup;
552 * If there are no path args and submodule.active is set then,
553 * by default, only initialize 'active' modules.
555 if (!argc && git_config_get_value_multi("submodule.active"))
556 module_list_active(&list);
558 info.prefix = prefix;
559 if (quiet)
560 info.flags |= OPT_QUIET;
562 for_each_listed_submodule(&list, init_submodule_cb, &info);
564 ret = 0;
565 cleanup:
566 module_list_release(&list);
567 clear_pathspec(&pathspec);
568 return ret;
571 struct status_cb {
572 const char *prefix;
573 unsigned int flags;
575 #define STATUS_CB_INIT { 0 }
577 static void print_status(unsigned int flags, char state, const char *path,
578 const struct object_id *oid, const char *displaypath)
580 if (flags & OPT_QUIET)
581 return;
583 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
585 if (state == ' ' || state == '+') {
586 char *name = compute_rev_name(path, oid_to_hex(oid));
588 if (name)
589 printf(" (%s)", name);
590 free(name);
593 printf("\n");
596 static int handle_submodule_head_ref(const char *refname UNUSED,
597 const struct object_id *oid,
598 int flags UNUSED,
599 void *cb_data)
601 struct object_id *output = cb_data;
603 if (oid)
604 oidcpy(output, oid);
606 return 0;
609 static void status_submodule(const char *path, const struct object_id *ce_oid,
610 unsigned int ce_flags, const char *prefix,
611 unsigned int flags)
613 char *displaypath;
614 struct strvec diff_files_args = STRVEC_INIT;
615 struct rev_info rev = REV_INFO_INIT;
616 int diff_files_result;
617 struct strbuf buf = STRBUF_INIT;
618 const char *git_dir;
619 struct setup_revision_opt opt = {
620 .free_removed_argv_elements = 1,
623 if (!submodule_from_path(the_repository, null_oid(), path))
624 die(_("no submodule mapping found in .gitmodules for path '%s'"),
625 path);
627 displaypath = get_submodule_displaypath(path, prefix);
629 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
630 print_status(flags, 'U', path, null_oid(), displaypath);
631 goto cleanup;
634 strbuf_addf(&buf, "%s/.git", path);
635 git_dir = read_gitfile(buf.buf);
636 if (!git_dir)
637 git_dir = buf.buf;
639 if (!is_submodule_active(the_repository, path) ||
640 !is_git_directory(git_dir)) {
641 print_status(flags, '-', path, ce_oid, displaypath);
642 strbuf_release(&buf);
643 goto cleanup;
645 strbuf_release(&buf);
647 strvec_pushl(&diff_files_args, "diff-files",
648 "--ignore-submodules=dirty", "--quiet", "--",
649 path, NULL);
651 git_config(git_diff_basic_config, NULL);
653 repo_init_revisions(the_repository, &rev, NULL);
654 rev.abbrev = 0;
655 setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt);
656 diff_files_result = run_diff_files(&rev, 0);
658 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
659 print_status(flags, ' ', path, ce_oid,
660 displaypath);
661 } else if (!(flags & OPT_CACHED)) {
662 struct object_id oid;
663 struct ref_store *refs = get_submodule_ref_store(path);
665 if (!refs) {
666 print_status(flags, '-', path, ce_oid, displaypath);
667 goto cleanup;
669 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
670 die(_("could not resolve HEAD ref inside the "
671 "submodule '%s'"), path);
673 print_status(flags, '+', path, &oid, displaypath);
674 } else {
675 print_status(flags, '+', path, ce_oid, displaypath);
678 if (flags & OPT_RECURSIVE) {
679 struct child_process cpr = CHILD_PROCESS_INIT;
681 cpr.git_cmd = 1;
682 cpr.dir = path;
683 prepare_submodule_repo_env(&cpr.env);
685 strvec_push(&cpr.args, "--super-prefix");
686 strvec_pushf(&cpr.args, "%s/", displaypath);
687 strvec_pushl(&cpr.args, "submodule--helper", "status",
688 "--recursive", NULL);
690 if (flags & OPT_CACHED)
691 strvec_push(&cpr.args, "--cached");
693 if (flags & OPT_QUIET)
694 strvec_push(&cpr.args, "--quiet");
696 if (run_command(&cpr))
697 die(_("failed to recurse into submodule '%s'"), path);
700 cleanup:
701 strvec_clear(&diff_files_args);
702 free(displaypath);
703 release_revisions(&rev);
706 static void status_submodule_cb(const struct cache_entry *list_item,
707 void *cb_data)
709 struct status_cb *info = cb_data;
711 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
712 info->prefix, info->flags);
715 static int module_status(int argc, const char **argv, const char *prefix)
717 struct status_cb info = STATUS_CB_INIT;
718 struct pathspec pathspec = { 0 };
719 struct module_list list = MODULE_LIST_INIT;
720 int quiet = 0;
721 struct option module_status_options[] = {
722 OPT__QUIET(&quiet, N_("suppress submodule status output")),
723 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
724 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
725 OPT_END()
727 const char *const git_submodule_helper_usage[] = {
728 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
729 NULL
731 int ret = 1;
733 argc = parse_options(argc, argv, prefix, module_status_options,
734 git_submodule_helper_usage, 0);
736 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
737 goto cleanup;
739 info.prefix = prefix;
740 if (quiet)
741 info.flags |= OPT_QUIET;
743 for_each_listed_submodule(&list, status_submodule_cb, &info);
745 ret = 0;
746 cleanup:
747 module_list_release(&list);
748 clear_pathspec(&pathspec);
749 return ret;
752 struct module_cb {
753 unsigned int mod_src;
754 unsigned int mod_dst;
755 struct object_id oid_src;
756 struct object_id oid_dst;
757 char status;
758 char *sm_path;
760 #define MODULE_CB_INIT { 0 }
762 static void module_cb_release(struct module_cb *mcb)
764 free(mcb->sm_path);
767 struct module_cb_list {
768 struct module_cb **entries;
769 int alloc, nr;
771 #define MODULE_CB_LIST_INIT { 0 }
773 static void module_cb_list_release(struct module_cb_list *mcbl)
775 int i;
777 for (i = 0; i < mcbl->nr; i++) {
778 struct module_cb *mcb = mcbl->entries[i];
780 module_cb_release(mcb);
781 free(mcb);
783 free(mcbl->entries);
786 struct summary_cb {
787 int argc;
788 const char **argv;
789 const char *prefix;
790 unsigned int cached: 1;
791 unsigned int for_status: 1;
792 unsigned int files: 1;
793 int summary_limit;
795 #define SUMMARY_CB_INIT { 0 }
797 enum diff_cmd {
798 DIFF_INDEX,
799 DIFF_FILES
802 static char *verify_submodule_committish(const char *sm_path,
803 const char *committish)
805 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
806 struct strbuf result = STRBUF_INIT;
808 cp_rev_parse.git_cmd = 1;
809 cp_rev_parse.dir = sm_path;
810 prepare_submodule_repo_env(&cp_rev_parse.env);
811 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
812 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
813 strvec_push(&cp_rev_parse.args, "--");
815 if (capture_command(&cp_rev_parse, &result, 0))
816 return NULL;
818 strbuf_trim_trailing_newline(&result);
819 return strbuf_detach(&result, NULL);
822 static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
823 int total_commits, const char *displaypath,
824 const char *src_abbrev, const char *dst_abbrev,
825 struct module_cb *p)
827 if (p->status == 'T') {
828 if (S_ISGITLINK(p->mod_dst))
829 printf(_("* %s %s(blob)->%s(submodule)"),
830 displaypath, src_abbrev, dst_abbrev);
831 else
832 printf(_("* %s %s(submodule)->%s(blob)"),
833 displaypath, src_abbrev, dst_abbrev);
834 } else {
835 printf("* %s %s...%s",
836 displaypath, src_abbrev, dst_abbrev);
839 if (total_commits < 0)
840 printf(":\n");
841 else
842 printf(" (%d):\n", total_commits);
844 if (errmsg) {
845 printf(_("%s"), errmsg);
846 } else if (total_commits > 0) {
847 struct child_process cp_log = CHILD_PROCESS_INIT;
849 cp_log.git_cmd = 1;
850 cp_log.dir = p->sm_path;
851 prepare_submodule_repo_env(&cp_log.env);
852 strvec_pushl(&cp_log.args, "log", NULL);
854 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
855 if (info->summary_limit > 0)
856 strvec_pushf(&cp_log.args, "-%d",
857 info->summary_limit);
859 strvec_pushl(&cp_log.args, "--pretty= %m %s",
860 "--first-parent", NULL);
861 strvec_pushf(&cp_log.args, "%s...%s",
862 src_abbrev, dst_abbrev);
863 } else if (S_ISGITLINK(p->mod_dst)) {
864 strvec_pushl(&cp_log.args, "--pretty= > %s",
865 "-1", dst_abbrev, NULL);
866 } else {
867 strvec_pushl(&cp_log.args, "--pretty= < %s",
868 "-1", src_abbrev, NULL);
870 run_command(&cp_log);
872 printf("\n");
875 static void generate_submodule_summary(struct summary_cb *info,
876 struct module_cb *p)
878 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
879 int missing_src = 0, missing_dst = 0;
880 struct strbuf errmsg = STRBUF_INIT;
881 int total_commits = -1;
883 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
884 if (S_ISGITLINK(p->mod_dst)) {
885 struct ref_store *refs = get_submodule_ref_store(p->sm_path);
887 if (refs)
888 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
889 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
890 struct stat st;
891 int fd = open(p->sm_path, O_RDONLY);
893 if (fd < 0 || fstat(fd, &st) < 0 ||
894 index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
895 p->sm_path, 0))
896 error(_("couldn't hash object from '%s'"), p->sm_path);
897 } else {
898 /* for a submodule removal (mode:0000000), don't warn */
899 if (p->mod_dst)
900 warning(_("unexpected mode %o\n"), p->mod_dst);
904 if (S_ISGITLINK(p->mod_src)) {
905 if (p->status != 'D')
906 src_abbrev = verify_submodule_committish(p->sm_path,
907 oid_to_hex(&p->oid_src));
908 if (!src_abbrev) {
909 missing_src = 1;
911 * As `rev-parse` failed, we fallback to getting
912 * the abbreviated hash using oid_src. We do
913 * this as we might still need the abbreviated
914 * hash in cases like a submodule type change, etc.
916 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
918 } else {
920 * The source does not point to a submodule.
921 * So, we fallback to getting the abbreviation using
922 * oid_src as we might still need the abbreviated
923 * hash in cases like submodule add, etc.
925 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
928 if (S_ISGITLINK(p->mod_dst)) {
929 dst_abbrev = verify_submodule_committish(p->sm_path,
930 oid_to_hex(&p->oid_dst));
931 if (!dst_abbrev) {
932 missing_dst = 1;
934 * As `rev-parse` failed, we fallback to getting
935 * the abbreviated hash using oid_dst. We do
936 * this as we might still need the abbreviated
937 * hash in cases like a submodule type change, etc.
939 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
941 } else {
943 * The destination does not point to a submodule.
944 * So, we fallback to getting the abbreviation using
945 * oid_dst as we might still need the abbreviated
946 * hash in cases like a submodule removal, etc.
948 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
951 displaypath = get_submodule_displaypath(p->sm_path, info->prefix);
953 if (!missing_src && !missing_dst) {
954 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
955 struct strbuf sb_rev_list = STRBUF_INIT;
957 strvec_pushl(&cp_rev_list.args, "rev-list",
958 "--first-parent", "--count", NULL);
959 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
960 strvec_pushf(&cp_rev_list.args, "%s...%s",
961 src_abbrev, dst_abbrev);
962 else
963 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
964 src_abbrev : dst_abbrev);
965 strvec_push(&cp_rev_list.args, "--");
967 cp_rev_list.git_cmd = 1;
968 cp_rev_list.dir = p->sm_path;
969 prepare_submodule_repo_env(&cp_rev_list.env);
971 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
972 total_commits = atoi(sb_rev_list.buf);
974 strbuf_release(&sb_rev_list);
975 } else {
977 * Don't give error msg for modification whose dst is not
978 * submodule, i.e., deleted or changed to blob
980 if (S_ISGITLINK(p->mod_dst)) {
981 if (missing_src && missing_dst) {
982 strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n",
983 displaypath, oid_to_hex(&p->oid_src),
984 oid_to_hex(&p->oid_dst));
985 } else {
986 strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n",
987 displaypath, missing_src ?
988 oid_to_hex(&p->oid_src) :
989 oid_to_hex(&p->oid_dst));
994 print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
995 total_commits, displaypath, src_abbrev,
996 dst_abbrev, p);
998 free(displaypath);
999 free(src_abbrev);
1000 free(dst_abbrev);
1001 strbuf_release(&errmsg);
1004 static void prepare_submodule_summary(struct summary_cb *info,
1005 struct module_cb_list *list)
1007 int i;
1008 for (i = 0; i < list->nr; i++) {
1009 const struct submodule *sub;
1010 struct module_cb *p = list->entries[i];
1011 struct strbuf sm_gitdir = STRBUF_INIT;
1013 if (p->status == 'D' || p->status == 'T') {
1014 generate_submodule_summary(info, p);
1015 continue;
1018 if (info->for_status && p->status != 'A' &&
1019 (sub = submodule_from_path(the_repository,
1020 null_oid(), p->sm_path))) {
1021 char *config_key = NULL;
1022 const char *value;
1023 int ignore_all = 0;
1025 config_key = xstrfmt("submodule.%s.ignore",
1026 sub->name);
1027 if (!git_config_get_string_tmp(config_key, &value))
1028 ignore_all = !strcmp(value, "all");
1029 else if (sub->ignore)
1030 ignore_all = !strcmp(sub->ignore, "all");
1032 free(config_key);
1033 if (ignore_all)
1034 continue;
1037 /* Also show added or modified modules which are checked out */
1038 strbuf_addstr(&sm_gitdir, p->sm_path);
1039 if (is_nonbare_repository_dir(&sm_gitdir))
1040 generate_submodule_summary(info, p);
1041 strbuf_release(&sm_gitdir);
1045 static void submodule_summary_callback(struct diff_queue_struct *q,
1046 struct diff_options *options,
1047 void *data)
1049 int i;
1050 struct module_cb_list *list = data;
1051 for (i = 0; i < q->nr; i++) {
1052 struct diff_filepair *p = q->queue[i];
1053 struct module_cb *temp;
1055 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1056 continue;
1057 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1058 temp->mod_src = p->one->mode;
1059 temp->mod_dst = p->two->mode;
1060 temp->oid_src = p->one->oid;
1061 temp->oid_dst = p->two->oid;
1062 temp->status = p->status;
1063 temp->sm_path = xstrdup(p->one->path);
1065 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1066 list->entries[list->nr++] = temp;
1070 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1072 switch (diff_cmd) {
1073 case DIFF_INDEX: return "diff-index";
1074 case DIFF_FILES: return "diff-files";
1075 default: BUG("bad diff_cmd value %d", diff_cmd);
1079 static int compute_summary_module_list(struct object_id *head_oid,
1080 struct summary_cb *info,
1081 enum diff_cmd diff_cmd)
1083 struct strvec diff_args = STRVEC_INIT;
1084 struct rev_info rev;
1085 struct setup_revision_opt opt = {
1086 .free_removed_argv_elements = 1,
1088 struct module_cb_list list = MODULE_CB_LIST_INIT;
1089 int ret = 0;
1091 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1092 if (info->cached)
1093 strvec_push(&diff_args, "--cached");
1094 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1095 if (head_oid)
1096 strvec_push(&diff_args, oid_to_hex(head_oid));
1097 strvec_push(&diff_args, "--");
1098 if (info->argc)
1099 strvec_pushv(&diff_args, info->argv);
1101 git_config(git_diff_basic_config, NULL);
1102 init_revisions(&rev, info->prefix);
1103 rev.abbrev = 0;
1104 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1105 setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
1106 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1107 rev.diffopt.format_callback = submodule_summary_callback;
1108 rev.diffopt.format_callback_data = &list;
1110 if (!info->cached) {
1111 if (diff_cmd == DIFF_INDEX)
1112 setup_work_tree();
1113 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
1114 perror("repo_read_index_preload");
1115 ret = -1;
1116 goto cleanup;
1118 } else if (repo_read_index(the_repository) < 0) {
1119 perror("repo_read_cache");
1120 ret = -1;
1121 goto cleanup;
1124 if (diff_cmd == DIFF_INDEX)
1125 run_diff_index(&rev, info->cached);
1126 else
1127 run_diff_files(&rev, 0);
1128 prepare_submodule_summary(info, &list);
1129 cleanup:
1130 strvec_clear(&diff_args);
1131 release_revisions(&rev);
1132 module_cb_list_release(&list);
1133 return ret;
1136 static int module_summary(int argc, const char **argv, const char *prefix)
1138 struct summary_cb info = SUMMARY_CB_INIT;
1139 int cached = 0;
1140 int for_status = 0;
1141 int files = 0;
1142 int summary_limit = -1;
1143 enum diff_cmd diff_cmd = DIFF_INDEX;
1144 struct object_id head_oid;
1145 int ret;
1146 struct option module_summary_options[] = {
1147 OPT_BOOL(0, "cached", &cached,
1148 N_("use the commit stored in the index instead of the submodule HEAD")),
1149 OPT_BOOL(0, "files", &files,
1150 N_("compare the commit in the index with that in the submodule HEAD")),
1151 OPT_BOOL(0, "for-status", &for_status,
1152 N_("skip submodules with 'ignore_config' value set to 'all'")),
1153 OPT_INTEGER('n', "summary-limit", &summary_limit,
1154 N_("limit the summary size")),
1155 OPT_END()
1157 const char *const git_submodule_helper_usage[] = {
1158 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1159 NULL
1162 argc = parse_options(argc, argv, prefix, module_summary_options,
1163 git_submodule_helper_usage, 0);
1165 if (!summary_limit)
1166 return 0;
1168 if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) {
1169 if (argc) {
1170 argv++;
1171 argc--;
1173 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1174 /* before the first commit: compare with an empty tree */
1175 oidcpy(&head_oid, the_hash_algo->empty_tree);
1176 if (argc) {
1177 argv++;
1178 argc--;
1180 } else {
1181 if (get_oid("HEAD", &head_oid))
1182 die(_("could not fetch a revision for HEAD"));
1185 if (files) {
1186 if (cached)
1187 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1188 diff_cmd = DIFF_FILES;
1191 info.argc = argc;
1192 info.argv = argv;
1193 info.prefix = prefix;
1194 info.cached = !!cached;
1195 info.files = !!files;
1196 info.for_status = !!for_status;
1197 info.summary_limit = summary_limit;
1199 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1200 &info, diff_cmd);
1201 return ret;
1204 struct sync_cb {
1205 const char *prefix;
1206 unsigned int flags;
1208 #define SYNC_CB_INIT { 0 }
1210 static void sync_submodule(const char *path, const char *prefix,
1211 unsigned int flags)
1213 const struct submodule *sub;
1214 char *remote_key = NULL;
1215 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1216 struct strbuf sb = STRBUF_INIT;
1217 char *sub_config_path = NULL;
1218 int code;
1220 if (!is_submodule_active(the_repository, path))
1221 return;
1223 sub = submodule_from_path(the_repository, null_oid(), path);
1225 if (sub && sub->url) {
1226 if (starts_with_dot_dot_slash(sub->url) ||
1227 starts_with_dot_slash(sub->url)) {
1228 char *up_path = get_up_path(path);
1230 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1231 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1232 free(up_path);
1233 } else {
1234 sub_origin_url = xstrdup(sub->url);
1235 super_config_url = xstrdup(sub->url);
1237 } else {
1238 sub_origin_url = xstrdup("");
1239 super_config_url = xstrdup("");
1242 displaypath = get_submodule_displaypath(path, prefix);
1244 if (!(flags & OPT_QUIET))
1245 printf(_("Synchronizing submodule url for '%s'\n"),
1246 displaypath);
1248 strbuf_reset(&sb);
1249 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1250 if (git_config_set_gently(sb.buf, super_config_url))
1251 die(_("failed to register url for submodule path '%s'"),
1252 displaypath);
1254 if (!is_submodule_populated_gently(path, NULL))
1255 goto cleanup;
1257 strbuf_reset(&sb);
1258 code = get_default_remote_submodule(path, &default_remote);
1259 if (code)
1260 exit(code);
1262 remote_key = xstrfmt("remote.%s.url", default_remote);
1263 free(default_remote);
1265 submodule_to_gitdir(&sb, path);
1266 strbuf_addstr(&sb, "/config");
1268 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1269 die(_("failed to update remote for submodule '%s'"),
1270 path);
1272 if (flags & OPT_RECURSIVE) {
1273 struct child_process cpr = CHILD_PROCESS_INIT;
1275 cpr.git_cmd = 1;
1276 cpr.dir = path;
1277 prepare_submodule_repo_env(&cpr.env);
1279 strvec_push(&cpr.args, "--super-prefix");
1280 strvec_pushf(&cpr.args, "%s/", displaypath);
1281 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1282 "--recursive", NULL);
1284 if (flags & OPT_QUIET)
1285 strvec_push(&cpr.args, "--quiet");
1287 if (run_command(&cpr))
1288 die(_("failed to recurse into submodule '%s'"),
1289 path);
1292 cleanup:
1293 free(super_config_url);
1294 free(sub_origin_url);
1295 strbuf_release(&sb);
1296 free(remote_key);
1297 free(displaypath);
1298 free(sub_config_path);
1301 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1303 struct sync_cb *info = cb_data;
1305 sync_submodule(list_item->name, info->prefix, info->flags);
1308 static int module_sync(int argc, const char **argv, const char *prefix)
1310 struct sync_cb info = SYNC_CB_INIT;
1311 struct pathspec pathspec = { 0 };
1312 struct module_list list = MODULE_LIST_INIT;
1313 int quiet = 0;
1314 int recursive = 0;
1315 struct option module_sync_options[] = {
1316 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1317 OPT_BOOL(0, "recursive", &recursive,
1318 N_("recurse into nested submodules")),
1319 OPT_END()
1321 const char *const git_submodule_helper_usage[] = {
1322 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1323 NULL
1325 int ret = 1;
1327 argc = parse_options(argc, argv, prefix, module_sync_options,
1328 git_submodule_helper_usage, 0);
1330 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1331 goto cleanup;
1333 info.prefix = prefix;
1334 if (quiet)
1335 info.flags |= OPT_QUIET;
1336 if (recursive)
1337 info.flags |= OPT_RECURSIVE;
1339 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1341 ret = 0;
1342 cleanup:
1343 module_list_release(&list);
1344 clear_pathspec(&pathspec);
1345 return ret;
1348 struct deinit_cb {
1349 const char *prefix;
1350 unsigned int flags;
1352 #define DEINIT_CB_INIT { 0 }
1354 static void deinit_submodule(const char *path, const char *prefix,
1355 unsigned int flags)
1357 const struct submodule *sub;
1358 char *displaypath = NULL;
1359 struct child_process cp_config = CHILD_PROCESS_INIT;
1360 struct strbuf sb_config = STRBUF_INIT;
1361 char *sub_git_dir = xstrfmt("%s/.git", path);
1363 sub = submodule_from_path(the_repository, null_oid(), path);
1365 if (!sub || !sub->name)
1366 goto cleanup;
1368 displaypath = get_submodule_displaypath(path, prefix);
1370 /* remove the submodule work tree (unless the user already did it) */
1371 if (is_directory(path)) {
1372 struct strbuf sb_rm = STRBUF_INIT;
1373 const char *format;
1375 if (is_directory(sub_git_dir)) {
1376 if (!(flags & OPT_QUIET))
1377 warning(_("Submodule work tree '%s' contains a .git "
1378 "directory. This will be replaced with a "
1379 ".git file by using absorbgitdirs."),
1380 displaypath);
1382 absorb_git_dir_into_superproject(path);
1386 if (!(flags & OPT_FORCE)) {
1387 struct child_process cp_rm = CHILD_PROCESS_INIT;
1389 cp_rm.git_cmd = 1;
1390 strvec_pushl(&cp_rm.args, "rm", "-qn",
1391 path, NULL);
1393 if (run_command(&cp_rm))
1394 die(_("Submodule work tree '%s' contains local "
1395 "modifications; use '-f' to discard them"),
1396 displaypath);
1399 strbuf_addstr(&sb_rm, path);
1401 if (!remove_dir_recursively(&sb_rm, 0))
1402 format = _("Cleared directory '%s'\n");
1403 else
1404 format = _("Could not remove submodule work tree '%s'\n");
1406 if (!(flags & OPT_QUIET))
1407 printf(format, displaypath);
1409 submodule_unset_core_worktree(sub);
1411 strbuf_release(&sb_rm);
1414 if (mkdir(path, 0777))
1415 printf(_("could not create empty submodule directory %s"),
1416 displaypath);
1418 cp_config.git_cmd = 1;
1419 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1420 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1422 /* remove the .git/config entries (unless the user already did it) */
1423 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1424 char *sub_key = xstrfmt("submodule.%s", sub->name);
1427 * remove the whole section so we have a clean state when
1428 * the user later decides to init this submodule again
1430 git_config_rename_section_in_file(NULL, sub_key, NULL);
1431 if (!(flags & OPT_QUIET))
1432 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1433 sub->name, sub->url, displaypath);
1434 free(sub_key);
1437 cleanup:
1438 free(displaypath);
1439 free(sub_git_dir);
1440 strbuf_release(&sb_config);
1443 static void deinit_submodule_cb(const struct cache_entry *list_item,
1444 void *cb_data)
1446 struct deinit_cb *info = cb_data;
1447 deinit_submodule(list_item->name, info->prefix, info->flags);
1450 static int module_deinit(int argc, const char **argv, const char *prefix)
1452 struct deinit_cb info = DEINIT_CB_INIT;
1453 struct pathspec pathspec = { 0 };
1454 struct module_list list = MODULE_LIST_INIT;
1455 int quiet = 0;
1456 int force = 0;
1457 int all = 0;
1458 struct option module_deinit_options[] = {
1459 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1460 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1461 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1462 OPT_END()
1464 const char *const git_submodule_helper_usage[] = {
1465 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1466 NULL
1468 int ret = 1;
1470 argc = parse_options(argc, argv, prefix, module_deinit_options,
1471 git_submodule_helper_usage, 0);
1473 if (all && argc) {
1474 error("pathspec and --all are incompatible");
1475 usage_with_options(git_submodule_helper_usage,
1476 module_deinit_options);
1479 if (!argc && !all)
1480 die(_("Use '--all' if you really want to deinitialize all submodules"));
1482 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1483 goto cleanup;
1485 info.prefix = prefix;
1486 if (quiet)
1487 info.flags |= OPT_QUIET;
1488 if (force)
1489 info.flags |= OPT_FORCE;
1491 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1493 ret = 0;
1494 cleanup:
1495 module_list_release(&list);
1496 clear_pathspec(&pathspec);
1497 return ret;
1500 struct module_clone_data {
1501 const char *prefix;
1502 const char *path;
1503 const char *name;
1504 const char *url;
1505 const char *depth;
1506 struct list_objects_filter_options *filter_options;
1507 unsigned int quiet: 1;
1508 unsigned int progress: 1;
1509 unsigned int dissociate: 1;
1510 unsigned int require_init: 1;
1511 int single_branch;
1513 #define MODULE_CLONE_DATA_INIT { \
1514 .single_branch = -1, \
1517 struct submodule_alternate_setup {
1518 const char *submodule_name;
1519 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1520 SUBMODULE_ALTERNATE_ERROR_DIE,
1521 SUBMODULE_ALTERNATE_ERROR_INFO,
1522 SUBMODULE_ALTERNATE_ERROR_IGNORE
1523 } error_mode;
1524 struct string_list *reference;
1526 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1527 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1530 static const char alternate_error_advice[] = N_(
1531 "An alternate computed from a superproject's alternate is invalid.\n"
1532 "To allow Git to clone without an alternate in such a case, set\n"
1533 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1534 "'--reference-if-able' instead of '--reference'."
1537 static int add_possible_reference_from_superproject(
1538 struct object_directory *odb, void *sas_cb)
1540 struct submodule_alternate_setup *sas = sas_cb;
1541 size_t len;
1544 * If the alternate object store is another repository, try the
1545 * standard layout with .git/(modules/<name>)+/objects
1547 if (strip_suffix(odb->path, "/objects", &len)) {
1548 struct repository alternate;
1549 char *sm_alternate;
1550 struct strbuf sb = STRBUF_INIT;
1551 struct strbuf err = STRBUF_INIT;
1552 strbuf_add(&sb, odb->path, len);
1554 if (repo_init(&alternate, sb.buf, NULL) < 0)
1555 die(_("could not get a repository handle for gitdir '%s'"),
1556 sb.buf);
1559 * We need to end the new path with '/' to mark it as a dir,
1560 * otherwise a submodule name containing '/' will be broken
1561 * as the last part of a missing submodule reference would
1562 * be taken as a file name.
1564 strbuf_reset(&sb);
1565 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1566 strbuf_addch(&sb, '/');
1567 repo_clear(&alternate);
1569 sm_alternate = compute_alternate_path(sb.buf, &err);
1570 if (sm_alternate) {
1571 char *p = strbuf_detach(&sb, NULL);
1573 string_list_append(sas->reference, p)->util = p;
1574 free(sm_alternate);
1575 } else {
1576 switch (sas->error_mode) {
1577 case SUBMODULE_ALTERNATE_ERROR_DIE:
1578 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1579 advise(_(alternate_error_advice));
1580 die(_("submodule '%s' cannot add alternate: %s"),
1581 sas->submodule_name, err.buf);
1582 case SUBMODULE_ALTERNATE_ERROR_INFO:
1583 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1584 sas->submodule_name, err.buf);
1585 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1586 ; /* nothing */
1589 strbuf_release(&sb);
1592 return 0;
1595 static void prepare_possible_alternates(const char *sm_name,
1596 struct string_list *reference)
1598 char *sm_alternate = NULL, *error_strategy = NULL;
1599 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1601 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1602 if (!sm_alternate)
1603 return;
1605 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1607 if (!error_strategy)
1608 error_strategy = xstrdup("die");
1610 sas.submodule_name = sm_name;
1611 sas.reference = reference;
1612 if (!strcmp(error_strategy, "die"))
1613 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1614 else if (!strcmp(error_strategy, "info"))
1615 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1616 else if (!strcmp(error_strategy, "ignore"))
1617 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1618 else
1619 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1621 if (!strcmp(sm_alternate, "superproject"))
1622 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1623 else if (!strcmp(sm_alternate, "no"))
1624 ; /* do nothing */
1625 else
1626 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1628 free(sm_alternate);
1629 free(error_strategy);
1632 static char *clone_submodule_sm_gitdir(const char *name)
1634 struct strbuf sb = STRBUF_INIT;
1635 char *sm_gitdir;
1637 submodule_name_to_gitdir(&sb, the_repository, name);
1638 sm_gitdir = absolute_pathdup(sb.buf);
1639 strbuf_release(&sb);
1641 return sm_gitdir;
1644 static int clone_submodule(const struct module_clone_data *clone_data,
1645 struct string_list *reference)
1647 char *p;
1648 char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1649 char *sm_alternate = NULL, *error_strategy = NULL;
1650 struct child_process cp = CHILD_PROCESS_INIT;
1651 const char *clone_data_path = clone_data->path;
1652 char *to_free = NULL;
1654 if (!is_absolute_path(clone_data->path))
1655 clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
1656 clone_data->path);
1658 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1659 die(_("refusing to create/use '%s' in another submodule's "
1660 "git dir"), sm_gitdir);
1662 if (!file_exists(sm_gitdir)) {
1663 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1664 die(_("could not create directory '%s'"), sm_gitdir);
1666 prepare_possible_alternates(clone_data->name, reference);
1668 strvec_push(&cp.args, "clone");
1669 strvec_push(&cp.args, "--no-checkout");
1670 if (clone_data->quiet)
1671 strvec_push(&cp.args, "--quiet");
1672 if (clone_data->progress)
1673 strvec_push(&cp.args, "--progress");
1674 if (clone_data->depth && *(clone_data->depth))
1675 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1676 if (reference->nr) {
1677 struct string_list_item *item;
1679 for_each_string_list_item(item, reference)
1680 strvec_pushl(&cp.args, "--reference",
1681 item->string, NULL);
1683 if (clone_data->dissociate)
1684 strvec_push(&cp.args, "--dissociate");
1685 if (sm_gitdir && *sm_gitdir)
1686 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1687 if (clone_data->filter_options && clone_data->filter_options->choice)
1688 strvec_pushf(&cp.args, "--filter=%s",
1689 expand_list_objects_filter_spec(
1690 clone_data->filter_options));
1691 if (clone_data->single_branch >= 0)
1692 strvec_push(&cp.args, clone_data->single_branch ?
1693 "--single-branch" :
1694 "--no-single-branch");
1696 strvec_push(&cp.args, "--");
1697 strvec_push(&cp.args, clone_data->url);
1698 strvec_push(&cp.args, clone_data_path);
1700 cp.git_cmd = 1;
1701 prepare_submodule_repo_env(&cp.env);
1702 cp.no_stdin = 1;
1704 if(run_command(&cp))
1705 die(_("clone of '%s' into submodule path '%s' failed"),
1706 clone_data->url, clone_data_path);
1707 } else {
1708 char *path;
1710 if (clone_data->require_init && !access(clone_data_path, X_OK) &&
1711 !is_empty_dir(clone_data_path))
1712 die(_("directory not empty: '%s'"), clone_data_path);
1713 if (safe_create_leading_directories_const(clone_data_path) < 0)
1714 die(_("could not create directory '%s'"), clone_data_path);
1715 path = xstrfmt("%s/index", sm_gitdir);
1716 unlink_or_warn(path);
1717 free(path);
1720 connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1722 p = git_pathdup_submodule(clone_data_path, "config");
1723 if (!p)
1724 die(_("could not get submodule directory for '%s'"), clone_data_path);
1726 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1727 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1728 if (sm_alternate)
1729 git_config_set_in_file(p, "submodule.alternateLocation",
1730 sm_alternate);
1731 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1732 if (error_strategy)
1733 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1734 error_strategy);
1736 free(sm_alternate);
1737 free(error_strategy);
1739 free(sm_gitdir);
1740 free(p);
1741 free(to_free);
1742 return 0;
1745 static int module_clone(int argc, const char **argv, const char *prefix)
1747 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1748 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1749 struct string_list reference = STRING_LIST_INIT_NODUP;
1750 struct list_objects_filter_options filter_options =
1751 LIST_OBJECTS_FILTER_INIT;
1753 struct option module_clone_options[] = {
1754 OPT_STRING(0, "prefix", &clone_data.prefix,
1755 N_("path"),
1756 N_("alternative anchor for relative paths")),
1757 OPT_STRING(0, "path", &clone_data.path,
1758 N_("path"),
1759 N_("where the new submodule will be cloned to")),
1760 OPT_STRING(0, "name", &clone_data.name,
1761 N_("string"),
1762 N_("name of the new submodule")),
1763 OPT_STRING(0, "url", &clone_data.url,
1764 N_("string"),
1765 N_("url where to clone the submodule from")),
1766 OPT_STRING_LIST(0, "reference", &reference,
1767 N_("repo"),
1768 N_("reference repository")),
1769 OPT_BOOL(0, "dissociate", &dissociate,
1770 N_("use --reference only while cloning")),
1771 OPT_STRING(0, "depth", &clone_data.depth,
1772 N_("string"),
1773 N_("depth for shallow clones")),
1774 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1775 OPT_BOOL(0, "progress", &progress,
1776 N_("force cloning progress")),
1777 OPT_BOOL(0, "require-init", &require_init,
1778 N_("disallow cloning into non-empty directory")),
1779 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1780 N_("clone only one branch, HEAD or --branch")),
1781 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1782 OPT_END()
1784 const char *const git_submodule_helper_usage[] = {
1785 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1786 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1787 "[--single-branch] [--filter <filter-spec>] "
1788 "--url <url> --path <path>"),
1789 NULL
1792 argc = parse_options(argc, argv, prefix, module_clone_options,
1793 git_submodule_helper_usage, 0);
1795 clone_data.dissociate = !!dissociate;
1796 clone_data.quiet = !!quiet;
1797 clone_data.progress = !!progress;
1798 clone_data.require_init = !!require_init;
1799 clone_data.filter_options = &filter_options;
1801 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1802 usage_with_options(git_submodule_helper_usage,
1803 module_clone_options);
1805 clone_submodule(&clone_data, &reference);
1806 list_objects_filter_release(&filter_options);
1807 string_list_clear(&reference, 1);
1808 return 0;
1811 static int determine_submodule_update_strategy(struct repository *r,
1812 int just_cloned,
1813 const char *path,
1814 enum submodule_update_type update,
1815 struct submodule_update_strategy *out)
1817 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1818 char *key;
1819 const char *val;
1820 int ret;
1822 key = xstrfmt("submodule.%s.update", sub->name);
1824 if (update) {
1825 out->type = update;
1826 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1827 if (parse_submodule_update_strategy(val, out) < 0) {
1828 ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1829 val, path);
1830 goto cleanup;
1832 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1833 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1834 BUG("how did we read update = !command from .gitmodules?");
1835 out->type = sub->update_strategy.type;
1836 out->command = sub->update_strategy.command;
1837 } else
1838 out->type = SM_UPDATE_CHECKOUT;
1840 if (just_cloned &&
1841 (out->type == SM_UPDATE_MERGE ||
1842 out->type == SM_UPDATE_REBASE ||
1843 out->type == SM_UPDATE_NONE))
1844 out->type = SM_UPDATE_CHECKOUT;
1846 ret = 0;
1847 cleanup:
1848 free(key);
1849 return ret;
1852 struct update_clone_data {
1853 const struct submodule *sub;
1854 struct object_id oid;
1855 unsigned just_cloned;
1858 struct submodule_update_clone {
1859 /* index into 'update_data.list', the list of submodules to look into for cloning */
1860 int current;
1862 /* configuration parameters which are passed on to the children */
1863 const struct update_data *update_data;
1865 /* to be consumed by update_submodule() */
1866 struct update_clone_data *update_clone;
1867 int update_clone_nr; int update_clone_alloc;
1869 /* If we want to stop as fast as possible and return an error */
1870 unsigned quickstop : 1;
1872 /* failed clones to be retried again */
1873 const struct cache_entry **failed_clones;
1874 int failed_clones_nr, failed_clones_alloc;
1876 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1878 static void submodule_update_clone_release(struct submodule_update_clone *suc)
1880 free(suc->update_clone);
1881 free(suc->failed_clones);
1884 struct update_data {
1885 const char *prefix;
1886 char *displaypath;
1887 enum submodule_update_type update_default;
1888 struct object_id suboid;
1889 struct string_list references;
1890 struct submodule_update_strategy update_strategy;
1891 struct list_objects_filter_options *filter_options;
1892 struct module_list list;
1893 int depth;
1894 int max_jobs;
1895 int single_branch;
1896 int recommend_shallow;
1897 unsigned int require_init;
1898 unsigned int force;
1899 unsigned int quiet;
1900 unsigned int nofetch;
1901 unsigned int remote;
1902 unsigned int progress;
1903 unsigned int dissociate;
1904 unsigned int init;
1905 unsigned int warn_if_uninitialized;
1906 unsigned int recursive;
1908 /* copied over from update_clone_data */
1909 struct object_id oid;
1910 unsigned int just_cloned;
1911 const char *sm_path;
1913 #define UPDATE_DATA_INIT { \
1914 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1915 .list = MODULE_LIST_INIT, \
1916 .recommend_shallow = -1, \
1917 .references = STRING_LIST_INIT_DUP, \
1918 .single_branch = -1, \
1919 .max_jobs = 1, \
1922 static void update_data_release(struct update_data *ud)
1924 free(ud->displaypath);
1925 module_list_release(&ud->list);
1928 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1929 struct strbuf *out, const char *displaypath)
1932 * Only mention uninitialized submodules when their
1933 * paths have been specified.
1935 if (suc->update_data->warn_if_uninitialized) {
1936 strbuf_addf(out,
1937 _("Submodule path '%s' not initialized"),
1938 displaypath);
1939 strbuf_addch(out, '\n');
1940 strbuf_addstr(out,
1941 _("Maybe you want to use 'update --init'?"));
1942 strbuf_addch(out, '\n');
1947 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1948 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1950 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1951 struct child_process *child,
1952 struct submodule_update_clone *suc,
1953 struct strbuf *out)
1955 const struct submodule *sub = NULL;
1956 const char *url = NULL;
1957 const char *update_string;
1958 enum submodule_update_type update_type;
1959 char *key;
1960 const struct update_data *ud = suc->update_data;
1961 char *displaypath = get_submodule_displaypath(ce->name, ud->prefix);
1962 struct strbuf sb = STRBUF_INIT;
1963 int needs_cloning = 0;
1964 int need_free_url = 0;
1966 if (ce_stage(ce)) {
1967 strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
1968 strbuf_addch(out, '\n');
1969 goto cleanup;
1972 sub = submodule_from_path(the_repository, null_oid(), ce->name);
1974 if (!sub) {
1975 next_submodule_warn_missing(suc, out, displaypath);
1976 goto cleanup;
1979 key = xstrfmt("submodule.%s.update", sub->name);
1980 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
1981 update_type = parse_submodule_update_type(update_string);
1982 } else {
1983 update_type = sub->update_strategy.type;
1985 free(key);
1987 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
1988 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
1989 && update_type == SM_UPDATE_NONE)) {
1990 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1991 strbuf_addch(out, '\n');
1992 goto cleanup;
1995 /* Check if the submodule has been initialized. */
1996 if (!is_submodule_active(the_repository, ce->name)) {
1997 next_submodule_warn_missing(suc, out, displaypath);
1998 goto cleanup;
2001 strbuf_reset(&sb);
2002 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2003 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2004 if (starts_with_dot_slash(sub->url) ||
2005 starts_with_dot_dot_slash(sub->url)) {
2006 url = resolve_relative_url(sub->url, NULL, 0);
2007 need_free_url = 1;
2008 } else
2009 url = sub->url;
2012 strbuf_reset(&sb);
2013 strbuf_addf(&sb, "%s/.git", ce->name);
2014 needs_cloning = !file_exists(sb.buf);
2016 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2017 suc->update_clone_alloc);
2018 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2019 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2020 suc->update_clone[suc->update_clone_nr].sub = sub;
2021 suc->update_clone_nr++;
2023 if (!needs_cloning)
2024 goto cleanup;
2026 child->git_cmd = 1;
2027 child->no_stdin = 1;
2028 child->stdout_to_stderr = 1;
2029 child->err = -1;
2030 strvec_push(&child->args, "submodule--helper");
2031 strvec_push(&child->args, "clone");
2032 if (suc->update_data->progress)
2033 strvec_push(&child->args, "--progress");
2034 if (suc->update_data->quiet)
2035 strvec_push(&child->args, "--quiet");
2036 if (suc->update_data->prefix)
2037 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2038 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2039 strvec_push(&child->args, "--depth=1");
2040 else if (suc->update_data->depth)
2041 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2042 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2043 strvec_pushf(&child->args, "--filter=%s",
2044 expand_list_objects_filter_spec(suc->update_data->filter_options));
2045 if (suc->update_data->require_init)
2046 strvec_push(&child->args, "--require-init");
2047 strvec_pushl(&child->args, "--path", sub->path, NULL);
2048 strvec_pushl(&child->args, "--name", sub->name, NULL);
2049 strvec_pushl(&child->args, "--url", url, NULL);
2050 if (suc->update_data->references.nr) {
2051 struct string_list_item *item;
2053 for_each_string_list_item(item, &suc->update_data->references)
2054 strvec_pushl(&child->args, "--reference", item->string, NULL);
2056 if (suc->update_data->dissociate)
2057 strvec_push(&child->args, "--dissociate");
2058 if (suc->update_data->single_branch >= 0)
2059 strvec_push(&child->args, suc->update_data->single_branch ?
2060 "--single-branch" :
2061 "--no-single-branch");
2063 cleanup:
2064 free(displaypath);
2065 strbuf_release(&sb);
2066 if (need_free_url)
2067 free((void*)url);
2069 return needs_cloning;
2072 static int update_clone_get_next_task(struct child_process *child,
2073 struct strbuf *err,
2074 void *suc_cb,
2075 void **idx_task_cb)
2077 struct submodule_update_clone *suc = suc_cb;
2078 const struct cache_entry *ce;
2079 int index;
2081 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2082 ce = suc->update_data->list.entries[suc->current];
2083 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2084 int *p = xmalloc(sizeof(*p));
2086 *p = suc->current;
2087 *idx_task_cb = p;
2088 suc->current++;
2089 return 1;
2094 * The loop above tried cloning each submodule once, now try the
2095 * stragglers again, which we can imagine as an extension of the
2096 * entry list.
2098 index = suc->current - suc->update_data->list.nr;
2099 if (index < suc->failed_clones_nr) {
2100 int *p;
2102 ce = suc->failed_clones[index];
2103 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2104 suc->current ++;
2105 strbuf_addstr(err, "BUG: submodule considered for "
2106 "cloning, doesn't need cloning "
2107 "any more?\n");
2108 return 0;
2110 p = xmalloc(sizeof(*p));
2111 *p = suc->current;
2112 *idx_task_cb = p;
2113 suc->current ++;
2114 return 1;
2117 return 0;
2120 static int update_clone_start_failure(struct strbuf *err,
2121 void *suc_cb,
2122 void *idx_task_cb)
2124 struct submodule_update_clone *suc = suc_cb;
2126 suc->quickstop = 1;
2127 return 1;
2130 static int update_clone_task_finished(int result,
2131 struct strbuf *err,
2132 void *suc_cb,
2133 void *idx_task_cb)
2135 const struct cache_entry *ce;
2136 struct submodule_update_clone *suc = suc_cb;
2137 int *idxP = idx_task_cb;
2138 int idx = *idxP;
2140 free(idxP);
2142 if (!result)
2143 return 0;
2145 if (idx < suc->update_data->list.nr) {
2146 ce = suc->update_data->list.entries[idx];
2147 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2148 ce->name);
2149 strbuf_addch(err, '\n');
2150 ALLOC_GROW(suc->failed_clones,
2151 suc->failed_clones_nr + 1,
2152 suc->failed_clones_alloc);
2153 suc->failed_clones[suc->failed_clones_nr++] = ce;
2154 return 0;
2155 } else {
2156 idx -= suc->update_data->list.nr;
2157 ce = suc->failed_clones[idx];
2158 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2159 ce->name);
2160 strbuf_addch(err, '\n');
2161 suc->quickstop = 1;
2162 return 1;
2165 return 0;
2168 static int git_update_clone_config(const char *var, const char *value,
2169 void *cb)
2171 int *max_jobs = cb;
2173 if (!strcmp(var, "submodule.fetchjobs"))
2174 *max_jobs = parse_submodule_fetchjobs(var, value);
2175 return 0;
2178 static int is_tip_reachable(const char *path, const struct object_id *oid)
2180 struct child_process cp = CHILD_PROCESS_INIT;
2181 struct strbuf rev = STRBUF_INIT;
2182 char *hex = oid_to_hex(oid);
2184 cp.git_cmd = 1;
2185 cp.dir = path;
2186 cp.no_stderr = 1;
2187 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2189 prepare_submodule_repo_env(&cp.env);
2191 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2192 return 0;
2194 return 1;
2197 static int fetch_in_submodule(const char *module_path, int depth, int quiet,
2198 const struct object_id *oid)
2200 struct child_process cp = CHILD_PROCESS_INIT;
2202 prepare_submodule_repo_env(&cp.env);
2203 cp.git_cmd = 1;
2204 cp.dir = module_path;
2206 strvec_push(&cp.args, "fetch");
2207 if (quiet)
2208 strvec_push(&cp.args, "--quiet");
2209 if (depth)
2210 strvec_pushf(&cp.args, "--depth=%d", depth);
2211 if (oid) {
2212 char *hex = oid_to_hex(oid);
2213 char *remote = get_default_remote();
2215 strvec_pushl(&cp.args, remote, hex, NULL);
2216 free(remote);
2219 return run_command(&cp);
2222 static int run_update_command(const struct update_data *ud, int subforce)
2224 struct child_process cp = CHILD_PROCESS_INIT;
2225 char *oid = oid_to_hex(&ud->oid);
2226 int ret;
2228 switch (ud->update_strategy.type) {
2229 case SM_UPDATE_CHECKOUT:
2230 cp.git_cmd = 1;
2231 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2232 if (subforce)
2233 strvec_push(&cp.args, "-f");
2234 break;
2235 case SM_UPDATE_REBASE:
2236 cp.git_cmd = 1;
2237 strvec_push(&cp.args, "rebase");
2238 if (ud->quiet)
2239 strvec_push(&cp.args, "--quiet");
2240 break;
2241 case SM_UPDATE_MERGE:
2242 cp.git_cmd = 1;
2243 strvec_push(&cp.args, "merge");
2244 if (ud->quiet)
2245 strvec_push(&cp.args, "--quiet");
2246 break;
2247 case SM_UPDATE_COMMAND:
2248 cp.use_shell = 1;
2249 strvec_push(&cp.args, ud->update_strategy.command);
2250 break;
2251 default:
2252 BUG("unexpected update strategy type: %d",
2253 ud->update_strategy.type);
2255 strvec_push(&cp.args, oid);
2257 cp.dir = ud->sm_path;
2258 prepare_submodule_repo_env(&cp.env);
2259 if ((ret = run_command(&cp))) {
2260 switch (ud->update_strategy.type) {
2261 case SM_UPDATE_CHECKOUT:
2262 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2263 oid, ud->displaypath);
2264 /* No "ret" assignment, use "git checkout"'s */
2265 break;
2266 case SM_UPDATE_REBASE:
2267 ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2268 oid, ud->displaypath);
2269 break;
2270 case SM_UPDATE_MERGE:
2271 ret = die_message(_("Unable to merge '%s' in submodule path '%s'"),
2272 oid, ud->displaypath);
2273 break;
2274 case SM_UPDATE_COMMAND:
2275 ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2276 ud->update_strategy.command, oid, ud->displaypath);
2277 break;
2278 default:
2279 BUG("unexpected update strategy type: %d",
2280 ud->update_strategy.type);
2283 return ret;
2286 if (ud->quiet)
2287 return 0;
2289 switch (ud->update_strategy.type) {
2290 case SM_UPDATE_CHECKOUT:
2291 printf(_("Submodule path '%s': checked out '%s'\n"),
2292 ud->displaypath, oid);
2293 break;
2294 case SM_UPDATE_REBASE:
2295 printf(_("Submodule path '%s': rebased into '%s'\n"),
2296 ud->displaypath, oid);
2297 break;
2298 case SM_UPDATE_MERGE:
2299 printf(_("Submodule path '%s': merged in '%s'\n"),
2300 ud->displaypath, oid);
2301 break;
2302 case SM_UPDATE_COMMAND:
2303 printf(_("Submodule path '%s': '%s %s'\n"),
2304 ud->displaypath, ud->update_strategy.command, oid);
2305 break;
2306 default:
2307 BUG("unexpected update strategy type: %d",
2308 ud->update_strategy.type);
2311 return 0;
2314 static int run_update_procedure(const struct update_data *ud)
2316 int subforce = is_null_oid(&ud->suboid) || ud->force;
2318 if (!ud->nofetch) {
2320 * Run fetch only if `oid` isn't present or it
2321 * is not reachable from a ref.
2323 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2324 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2325 !ud->quiet)
2326 fprintf_ln(stderr,
2327 _("Unable to fetch in submodule path '%s'; "
2328 "trying to directly fetch %s:"),
2329 ud->displaypath, oid_to_hex(&ud->oid));
2331 * Now we tried the usual fetch, but `oid` may
2332 * not be reachable from any of the refs.
2334 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2335 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2336 return die_message(_("Fetched in submodule path '%s', but it did not "
2337 "contain %s. Direct fetching of that commit failed."),
2338 ud->displaypath, oid_to_hex(&ud->oid));
2341 return run_update_command(ud, subforce);
2344 static int remote_submodule_branch(const char *path, const char **branch)
2346 const struct submodule *sub;
2347 char *key;
2348 *branch = NULL;
2350 sub = submodule_from_path(the_repository, null_oid(), path);
2351 if (!sub)
2352 return die_message(_("could not initialize submodule at path '%s'"),
2353 path);
2355 key = xstrfmt("submodule.%s.branch", sub->name);
2356 if (repo_config_get_string_tmp(the_repository, key, branch))
2357 *branch = sub->branch;
2358 free(key);
2360 if (!*branch) {
2361 *branch = "HEAD";
2362 return 0;
2365 if (!strcmp(*branch, ".")) {
2366 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2368 if (!refname)
2369 return die_message(_("No such ref: %s"), "HEAD");
2371 /* detached HEAD */
2372 if (!strcmp(refname, "HEAD"))
2373 return die_message(_("Submodule (%s) branch configured to inherit "
2374 "branch from superproject, but the superproject "
2375 "is not on any branch"), sub->name);
2377 if (!skip_prefix(refname, "refs/heads/", &refname))
2378 return die_message(_("Expecting a full ref name, got %s"),
2379 refname);
2381 *branch = refname;
2382 return 0;
2385 /* Our "branch" is coming from repo_config_get_string_tmp() */
2386 return 0;
2389 static int ensure_core_worktree(const char *path)
2391 const char *cw;
2392 struct repository subrepo;
2394 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2395 return die_message(_("could not get a repository handle for submodule '%s'"),
2396 path);
2398 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2399 char *cfg_file, *abs_path;
2400 const char *rel_path;
2401 struct strbuf sb = STRBUF_INIT;
2403 cfg_file = repo_git_path(&subrepo, "config");
2405 abs_path = absolute_pathdup(path);
2406 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2408 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2410 free(cfg_file);
2411 free(abs_path);
2412 strbuf_release(&sb);
2415 repo_clear(&subrepo);
2416 return 0;
2419 static const char *submodule_update_type_to_label(enum submodule_update_type type)
2421 switch (type) {
2422 case SM_UPDATE_CHECKOUT:
2423 return "checkout";
2424 case SM_UPDATE_MERGE:
2425 return "merge";
2426 case SM_UPDATE_REBASE:
2427 return "rebase";
2428 case SM_UPDATE_UNSPECIFIED:
2429 case SM_UPDATE_NONE:
2430 case SM_UPDATE_COMMAND:
2431 break;
2433 BUG("unreachable with type %d", type);
2436 static void update_data_to_args(const struct update_data *update_data,
2437 struct strvec *args)
2439 enum submodule_update_type update_type = update_data->update_default;
2441 if (update_data->displaypath) {
2442 strvec_push(args, "--super-prefix");
2443 strvec_pushf(args, "%s/", update_data->displaypath);
2445 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2446 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2447 if (update_data->quiet)
2448 strvec_push(args, "--quiet");
2449 if (update_data->force)
2450 strvec_push(args, "--force");
2451 if (update_data->init)
2452 strvec_push(args, "--init");
2453 if (update_data->remote)
2454 strvec_push(args, "--remote");
2455 if (update_data->nofetch)
2456 strvec_push(args, "--no-fetch");
2457 if (update_data->dissociate)
2458 strvec_push(args, "--dissociate");
2459 if (update_data->progress)
2460 strvec_push(args, "--progress");
2461 if (update_data->require_init)
2462 strvec_push(args, "--require-init");
2463 if (update_data->depth)
2464 strvec_pushf(args, "--depth=%d", update_data->depth);
2465 if (update_type != SM_UPDATE_UNSPECIFIED)
2466 strvec_pushf(args, "--%s",
2467 submodule_update_type_to_label(update_type));
2469 if (update_data->references.nr) {
2470 struct string_list_item *item;
2472 for_each_string_list_item(item, &update_data->references)
2473 strvec_pushl(args, "--reference", item->string, NULL);
2475 if (update_data->filter_options && update_data->filter_options->choice)
2476 strvec_pushf(args, "--filter=%s",
2477 expand_list_objects_filter_spec(
2478 update_data->filter_options));
2479 if (update_data->recommend_shallow == 0)
2480 strvec_push(args, "--no-recommend-shallow");
2481 else if (update_data->recommend_shallow == 1)
2482 strvec_push(args, "--recommend-shallow");
2483 if (update_data->single_branch >= 0)
2484 strvec_push(args, update_data->single_branch ?
2485 "--single-branch" :
2486 "--no-single-branch");
2489 static int update_submodule(struct update_data *update_data)
2491 int ret;
2493 ret = determine_submodule_update_strategy(the_repository,
2494 update_data->just_cloned,
2495 update_data->sm_path,
2496 update_data->update_default,
2497 &update_data->update_strategy);
2498 if (ret)
2499 return ret;
2501 if (update_data->just_cloned)
2502 oidcpy(&update_data->suboid, null_oid());
2503 else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2504 return die_message(_("Unable to find current revision in submodule path '%s'"),
2505 update_data->displaypath);
2507 if (update_data->remote) {
2508 char *remote_name;
2509 const char *branch;
2510 char *remote_ref;
2511 int code;
2513 code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2514 if (code)
2515 return code;
2516 code = remote_submodule_branch(update_data->sm_path, &branch);
2517 if (code)
2518 return code;
2519 remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2521 free(remote_name);
2523 if (!update_data->nofetch) {
2524 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2525 0, NULL))
2526 return die_message(_("Unable to fetch in submodule path '%s'"),
2527 update_data->sm_path);
2530 if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2531 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2532 remote_ref, update_data->sm_path);
2534 free(remote_ref);
2537 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2538 ret = run_update_procedure(update_data);
2539 if (ret)
2540 return ret;
2543 if (update_data->recursive) {
2544 struct child_process cp = CHILD_PROCESS_INIT;
2545 struct update_data next = *update_data;
2547 next.prefix = NULL;
2548 oidcpy(&next.oid, null_oid());
2549 oidcpy(&next.suboid, null_oid());
2551 cp.dir = update_data->sm_path;
2552 cp.git_cmd = 1;
2553 prepare_submodule_repo_env(&cp.env);
2554 update_data_to_args(&next, &cp.args);
2556 ret = run_command(&cp);
2557 if (ret)
2558 die_message(_("Failed to recurse into submodule path '%s'"),
2559 update_data->displaypath);
2560 return ret;
2563 return 0;
2566 static int update_submodules(struct update_data *update_data)
2568 int i, ret = 0;
2569 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2570 const struct run_process_parallel_opts opts = {
2571 .tr2_category = "submodule",
2572 .tr2_label = "parallel/update",
2574 .processes = update_data->max_jobs,
2576 .get_next_task = update_clone_get_next_task,
2577 .start_failure = update_clone_start_failure,
2578 .task_finished = update_clone_task_finished,
2579 .data = &suc,
2582 suc.update_data = update_data;
2583 run_processes_parallel(&opts);
2586 * We saved the output and put it out all at once now.
2587 * That means:
2588 * - the listener does not have to interleave their (checkout)
2589 * work with our fetching. The writes involved in a
2590 * checkout involve more straightforward sequential I/O.
2591 * - the listener can avoid doing any work if fetching failed.
2593 if (suc.quickstop) {
2594 ret = 1;
2595 goto cleanup;
2598 for (i = 0; i < suc.update_clone_nr; i++) {
2599 struct update_clone_data ucd = suc.update_clone[i];
2600 int code;
2602 oidcpy(&update_data->oid, &ucd.oid);
2603 update_data->just_cloned = ucd.just_cloned;
2604 update_data->sm_path = ucd.sub->path;
2606 code = ensure_core_worktree(update_data->sm_path);
2607 if (code)
2608 goto fail;
2610 update_data->displaypath = get_submodule_displaypath(
2611 update_data->sm_path, update_data->prefix);
2612 code = update_submodule(update_data);
2613 FREE_AND_NULL(update_data->displaypath);
2614 fail:
2615 if (!code)
2616 continue;
2617 ret = code;
2618 if (ret == 128)
2619 goto cleanup;
2622 cleanup:
2623 submodule_update_clone_release(&suc);
2624 string_list_clear(&update_data->references, 0);
2625 return ret;
2628 static int module_update(int argc, const char **argv, const char *prefix)
2630 struct pathspec pathspec = { 0 };
2631 struct pathspec pathspec2 = { 0 };
2632 struct update_data opt = UPDATE_DATA_INIT;
2633 struct list_objects_filter_options filter_options =
2634 LIST_OBJECTS_FILTER_INIT;
2635 int ret;
2636 struct option module_update_options[] = {
2637 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2638 OPT_BOOL(0, "init", &opt.init,
2639 N_("initialize uninitialized submodules before update")),
2640 OPT_BOOL(0, "remote", &opt.remote,
2641 N_("use SHA-1 of submodule's remote tracking branch")),
2642 OPT_BOOL(0, "recursive", &opt.recursive,
2643 N_("traverse submodules recursively")),
2644 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2645 N_("don't fetch new objects from the remote site")),
2646 OPT_SET_INT(0, "checkout", &opt.update_default,
2647 N_("use the 'checkout' update strategy (default)"),
2648 SM_UPDATE_CHECKOUT),
2649 OPT_SET_INT('m', "merge", &opt.update_default,
2650 N_("use the 'merge' update strategy"),
2651 SM_UPDATE_MERGE),
2652 OPT_SET_INT('r', "rebase", &opt.update_default,
2653 N_("use the 'rebase' update strategy"),
2654 SM_UPDATE_REBASE),
2655 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2656 N_("reference repository")),
2657 OPT_BOOL(0, "dissociate", &opt.dissociate,
2658 N_("use --reference only while cloning")),
2659 OPT_INTEGER(0, "depth", &opt.depth,
2660 N_("create a shallow clone truncated to the "
2661 "specified number of revisions")),
2662 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2663 N_("parallel jobs")),
2664 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2665 N_("whether the initial clone should follow the shallow recommendation")),
2666 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2667 OPT_BOOL(0, "progress", &opt.progress,
2668 N_("force cloning progress")),
2669 OPT_BOOL(0, "require-init", &opt.require_init,
2670 N_("disallow cloning into non-empty directory, implies --init")),
2671 OPT_BOOL(0, "single-branch", &opt.single_branch,
2672 N_("clone only one branch, HEAD or --branch")),
2673 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2674 OPT_END()
2676 const char *const git_submodule_helper_usage[] = {
2677 N_("git submodule [--quiet] update"
2678 " [--init [--filter=<filter-spec>]] [--remote]"
2679 " [-N|--no-fetch] [-f|--force]"
2680 " [--checkout|--merge|--rebase]"
2681 " [--[no-]recommend-shallow] [--reference <repository>]"
2682 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2683 NULL
2686 update_clone_config_from_gitmodules(&opt.max_jobs);
2687 git_config(git_update_clone_config, &opt.max_jobs);
2689 argc = parse_options(argc, argv, prefix, module_update_options,
2690 git_submodule_helper_usage, 0);
2692 if (opt.require_init)
2693 opt.init = 1;
2695 if (filter_options.choice && !opt.init) {
2696 usage_with_options(git_submodule_helper_usage,
2697 module_update_options);
2700 opt.filter_options = &filter_options;
2701 opt.prefix = prefix;
2703 if (opt.update_default)
2704 opt.update_strategy.type = opt.update_default;
2706 if (module_list_compute(argv, prefix, &pathspec, &opt.list) < 0) {
2707 ret = 1;
2708 goto cleanup;
2711 if (pathspec.nr)
2712 opt.warn_if_uninitialized = 1;
2714 if (opt.init) {
2715 struct module_list list = MODULE_LIST_INIT;
2716 struct init_cb info = INIT_CB_INIT;
2718 if (module_list_compute(argv, opt.prefix,
2719 &pathspec2, &list) < 0) {
2720 module_list_release(&list);
2721 ret = 1;
2722 goto cleanup;
2726 * If there are no path args and submodule.active is set then,
2727 * by default, only initialize 'active' modules.
2729 if (!argc && git_config_get_value_multi("submodule.active"))
2730 module_list_active(&list);
2732 info.prefix = opt.prefix;
2733 if (opt.quiet)
2734 info.flags |= OPT_QUIET;
2736 for_each_listed_submodule(&list, init_submodule_cb, &info);
2737 module_list_release(&list);
2740 ret = update_submodules(&opt);
2741 cleanup:
2742 update_data_release(&opt);
2743 list_objects_filter_release(&filter_options);
2744 clear_pathspec(&pathspec);
2745 clear_pathspec(&pathspec2);
2746 return ret;
2749 static int push_check(int argc, const char **argv, const char *prefix)
2751 struct remote *remote;
2752 const char *superproject_head;
2753 char *head;
2754 int detached_head = 0;
2755 struct object_id head_oid;
2757 if (argc < 3)
2758 die("submodule--helper push-check requires at least 2 arguments");
2761 * superproject's resolved head ref.
2762 * if HEAD then the superproject is in a detached head state, otherwise
2763 * it will be the resolved head ref.
2765 superproject_head = argv[1];
2766 argv++;
2767 argc--;
2768 /* Get the submodule's head ref and determine if it is detached */
2769 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2770 if (!head)
2771 die(_("Failed to resolve HEAD as a valid ref."));
2772 if (!strcmp(head, "HEAD"))
2773 detached_head = 1;
2776 * The remote must be configured.
2777 * This is to avoid pushing to the exact same URL as the parent.
2779 remote = pushremote_get(argv[1]);
2780 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2781 die("remote '%s' not configured", argv[1]);
2783 /* Check the refspec */
2784 if (argc > 2) {
2785 int i;
2786 struct ref *local_refs = get_local_heads();
2787 struct refspec refspec = REFSPEC_INIT_PUSH;
2789 refspec_appendn(&refspec, argv + 2, argc - 2);
2791 for (i = 0; i < refspec.nr; i++) {
2792 const struct refspec_item *rs = &refspec.items[i];
2794 if (rs->pattern || rs->matching)
2795 continue;
2797 /* LHS must match a single ref */
2798 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2799 case 1:
2800 break;
2801 case 0:
2803 * If LHS matches 'HEAD' then we need to ensure
2804 * that it matches the same named branch
2805 * checked out in the superproject.
2807 if (!strcmp(rs->src, "HEAD")) {
2808 if (!detached_head &&
2809 !strcmp(head, superproject_head))
2810 break;
2811 die("HEAD does not match the named branch in the superproject");
2813 /* fallthrough */
2814 default:
2815 die("src refspec '%s' must name a ref",
2816 rs->src);
2819 refspec_clear(&refspec);
2821 free(head);
2823 return 0;
2826 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2828 int i;
2829 struct pathspec pathspec = { 0 };
2830 struct module_list list = MODULE_LIST_INIT;
2831 struct option embed_gitdir_options[] = {
2832 OPT_END()
2834 const char *const git_submodule_helper_usage[] = {
2835 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2836 NULL
2838 int ret = 1;
2840 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2841 git_submodule_helper_usage, 0);
2843 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
2844 goto cleanup;
2846 for (i = 0; i < list.nr; i++)
2847 absorb_git_dir_into_superproject(list.entries[i]->name);
2849 ret = 0;
2850 cleanup:
2851 clear_pathspec(&pathspec);
2852 module_list_release(&list);
2853 return ret;
2856 static int module_set_url(int argc, const char **argv, const char *prefix)
2858 int quiet = 0;
2859 const char *newurl;
2860 const char *path;
2861 char *config_name;
2862 struct option options[] = {
2863 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2864 OPT_END()
2866 const char *const usage[] = {
2867 N_("git submodule set-url [--quiet] <path> <newurl>"),
2868 NULL
2871 argc = parse_options(argc, argv, prefix, options, usage, 0);
2873 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2874 usage_with_options(usage, options);
2876 config_name = xstrfmt("submodule.%s.url", path);
2878 config_set_in_gitmodules_file_gently(config_name, newurl);
2879 sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
2881 free(config_name);
2883 return 0;
2886 static int module_set_branch(int argc, const char **argv, const char *prefix)
2888 int opt_default = 0, ret;
2889 const char *opt_branch = NULL;
2890 const char *path;
2891 char *config_name;
2892 struct option options[] = {
2894 * We accept the `quiet` option for uniformity across subcommands,
2895 * though there is nothing to make less verbose in this subcommand.
2897 OPT_NOOP_NOARG('q', "quiet"),
2899 OPT_BOOL('d', "default", &opt_default,
2900 N_("set the default tracking branch to master")),
2901 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2902 N_("set the default tracking branch")),
2903 OPT_END()
2905 const char *const usage[] = {
2906 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2907 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2908 NULL
2911 argc = parse_options(argc, argv, prefix, options, usage, 0);
2913 if (!opt_branch && !opt_default)
2914 die(_("--branch or --default required"));
2916 if (opt_branch && opt_default)
2917 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2919 if (argc != 1 || !(path = argv[0]))
2920 usage_with_options(usage, options);
2922 config_name = xstrfmt("submodule.%s.branch", path);
2923 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2925 free(config_name);
2926 return !!ret;
2929 static int module_create_branch(int argc, const char **argv, const char *prefix)
2931 enum branch_track track;
2932 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
2933 struct option options[] = {
2934 OPT__QUIET(&quiet, N_("print only error messages")),
2935 OPT__FORCE(&force, N_("force creation"), 0),
2936 OPT_BOOL(0, "create-reflog", &reflog,
2937 N_("create the branch's reflog")),
2938 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
2939 N_("set branch tracking configuration"),
2940 PARSE_OPT_OPTARG,
2941 parse_opt_tracking_mode),
2942 OPT__DRY_RUN(&dry_run,
2943 N_("show whether the branch would be created")),
2944 OPT_END()
2946 const char *const usage[] = {
2947 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2948 NULL
2951 git_config(git_default_config, NULL);
2952 track = git_branch_track;
2953 argc = parse_options(argc, argv, prefix, options, usage, 0);
2955 if (argc != 3)
2956 usage_with_options(usage, options);
2958 if (!quiet && !dry_run)
2959 printf_ln(_("creating branch '%s'"), argv[0]);
2961 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
2962 force, reflog, quiet, track, dry_run);
2963 return 0;
2966 struct add_data {
2967 const char *prefix;
2968 const char *branch;
2969 const char *reference_path;
2970 char *sm_path;
2971 const char *sm_name;
2972 const char *repo;
2973 const char *realrepo;
2974 int depth;
2975 unsigned int force: 1;
2976 unsigned int quiet: 1;
2977 unsigned int progress: 1;
2978 unsigned int dissociate: 1;
2980 #define ADD_DATA_INIT { .depth = -1 }
2982 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
2984 struct child_process cp_remote = CHILD_PROCESS_INIT;
2985 struct strbuf sb_remote_out = STRBUF_INIT;
2987 cp_remote.git_cmd = 1;
2988 strvec_pushf(&cp_remote.env,
2989 "GIT_DIR=%s", git_dir_path);
2990 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
2991 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
2992 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
2993 char *next_line;
2994 char *line = sb_remote_out.buf;
2996 while ((next_line = strchr(line, '\n')) != NULL) {
2997 size_t len = next_line - line;
2999 if (strip_suffix_mem(line, &len, " (fetch)"))
3000 strbuf_addf(msg, " %.*s\n", (int)len, line);
3001 line = next_line + 1;
3005 strbuf_release(&sb_remote_out);
3008 static int add_submodule(const struct add_data *add_data)
3010 char *submod_gitdir_path;
3011 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3012 struct string_list reference = STRING_LIST_INIT_NODUP;
3013 int ret = -1;
3015 /* perhaps the path already exists and is already a git repo, else clone it */
3016 if (is_directory(add_data->sm_path)) {
3017 struct strbuf sm_path = STRBUF_INIT;
3018 strbuf_addstr(&sm_path, add_data->sm_path);
3019 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3020 if (is_nonbare_repository_dir(&sm_path))
3021 printf(_("Adding existing repo at '%s' to the index\n"),
3022 add_data->sm_path);
3023 else
3024 die(_("'%s' already exists and is not a valid git repo"),
3025 add_data->sm_path);
3026 strbuf_release(&sm_path);
3027 free(submod_gitdir_path);
3028 } else {
3029 struct child_process cp = CHILD_PROCESS_INIT;
3031 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3033 if (is_directory(submod_gitdir_path)) {
3034 if (!add_data->force) {
3035 struct strbuf msg = STRBUF_INIT;
3036 char *die_msg;
3038 strbuf_addf(&msg, _("A git directory for '%s' is found "
3039 "locally with remote(s):\n"),
3040 add_data->sm_name);
3042 append_fetch_remotes(&msg, submod_gitdir_path);
3043 free(submod_gitdir_path);
3045 strbuf_addf(&msg, _("If you want to reuse this local git "
3046 "directory instead of cloning again from\n"
3047 " %s\n"
3048 "use the '--force' option. If the local git "
3049 "directory is not the correct repo\n"
3050 "or you are unsure what this means choose "
3051 "another name with the '--name' option."),
3052 add_data->realrepo);
3054 die_msg = strbuf_detach(&msg, NULL);
3055 die("%s", die_msg);
3056 } else {
3057 printf(_("Reactivating local git directory for "
3058 "submodule '%s'\n"), add_data->sm_name);
3061 free(submod_gitdir_path);
3063 clone_data.prefix = add_data->prefix;
3064 clone_data.path = add_data->sm_path;
3065 clone_data.name = add_data->sm_name;
3066 clone_data.url = add_data->realrepo;
3067 clone_data.quiet = add_data->quiet;
3068 clone_data.progress = add_data->progress;
3069 if (add_data->reference_path) {
3070 char *p = xstrdup(add_data->reference_path);
3072 string_list_append(&reference, p)->util = p;
3074 clone_data.dissociate = add_data->dissociate;
3075 if (add_data->depth >= 0)
3076 clone_data.depth = xstrfmt("%d", add_data->depth);
3078 if (clone_submodule(&clone_data, &reference))
3079 goto cleanup;
3081 prepare_submodule_repo_env(&cp.env);
3082 cp.git_cmd = 1;
3083 cp.dir = add_data->sm_path;
3085 * NOTE: we only get here if add_data->force is true, so
3086 * passing --force to checkout is reasonable.
3088 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3090 if (add_data->branch) {
3091 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3092 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3095 if (run_command(&cp))
3096 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3098 ret = 0;
3099 cleanup:
3100 string_list_clear(&reference, 1);
3101 return ret;
3104 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3106 char *key;
3107 int ret;
3109 if (!is_writing_gitmodules_ok())
3110 die(_("please make sure that the .gitmodules file is in the working tree"));
3112 key = xstrfmt("submodule.%s.%s", name, var);
3113 ret = config_set_in_gitmodules_file_gently(key, value);
3114 free(key);
3116 return ret;
3119 static void configure_added_submodule(struct add_data *add_data)
3121 char *key;
3122 const char *val;
3123 struct child_process add_submod = CHILD_PROCESS_INIT;
3124 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3126 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3127 git_config_set_gently(key, add_data->realrepo);
3128 free(key);
3130 add_submod.git_cmd = 1;
3131 strvec_pushl(&add_submod.args, "add",
3132 "--no-warn-embedded-repo", NULL);
3133 if (add_data->force)
3134 strvec_push(&add_submod.args, "--force");
3135 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3137 if (run_command(&add_submod))
3138 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3140 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3141 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3142 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3144 if (add_data->branch) {
3145 if (config_submodule_in_gitmodules(add_data->sm_name,
3146 "branch", add_data->branch))
3147 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3150 add_gitmodules.git_cmd = 1;
3151 strvec_pushl(&add_gitmodules.args,
3152 "add", "--force", "--", ".gitmodules", NULL);
3154 if (run_command(&add_gitmodules))
3155 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3158 * NEEDSWORK: In a multi-working-tree world this needs to be
3159 * set in the per-worktree config.
3162 * NEEDSWORK: In the longer run, we need to get rid of this
3163 * pattern of querying "submodule.active" before calling
3164 * is_submodule_active(), since that function needs to find
3165 * out the value of "submodule.active" again anyway.
3167 if (!git_config_get_string_tmp("submodule.active", &val)) {
3169 * If the submodule being added isn't already covered by the
3170 * current configured pathspec, set the submodule's active flag
3172 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3173 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3174 git_config_set_gently(key, "true");
3175 free(key);
3177 } else {
3178 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3179 git_config_set_gently(key, "true");
3180 free(key);
3184 static void die_on_index_match(const char *path, int force)
3186 struct pathspec ps;
3187 const char *args[] = { path, NULL };
3188 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3190 if (repo_read_index_preload(the_repository, NULL, 0) < 0)
3191 die(_("index file corrupt"));
3193 if (ps.nr) {
3194 int i;
3195 char *ps_matched = xcalloc(ps.nr, 1);
3197 /* TODO: audit for interaction with sparse-index. */
3198 ensure_full_index(&the_index);
3201 * Since there is only one pathspec, we just need
3202 * need to check ps_matched[0] to know if a cache
3203 * entry matched.
3205 for (i = 0; i < the_index.cache_nr; i++) {
3206 ce_path_match(&the_index, the_index.cache[i], &ps,
3207 ps_matched);
3209 if (ps_matched[0]) {
3210 if (!force)
3211 die(_("'%s' already exists in the index"),
3212 path);
3213 if (!S_ISGITLINK(the_index.cache[i]->ce_mode))
3214 die(_("'%s' already exists in the index "
3215 "and is not a submodule"), path);
3216 break;
3219 free(ps_matched);
3221 clear_pathspec(&ps);
3224 static void die_on_repo_without_commits(const char *path)
3226 struct strbuf sb = STRBUF_INIT;
3227 strbuf_addstr(&sb, path);
3228 if (is_nonbare_repository_dir(&sb)) {
3229 struct object_id oid;
3230 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3231 die(_("'%s' does not have a commit checked out"), path);
3233 strbuf_release(&sb);
3236 static int module_add(int argc, const char **argv, const char *prefix)
3238 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3239 struct add_data add_data = ADD_DATA_INIT;
3240 char *to_free = NULL;
3241 struct option options[] = {
3242 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3243 N_("branch of repository to add as submodule")),
3244 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3245 PARSE_OPT_NOCOMPLETE),
3246 OPT__QUIET(&quiet, N_("print only error messages")),
3247 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3248 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3249 N_("reference repository")),
3250 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3251 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3252 N_("sets the submodule's name to the given string "
3253 "instead of defaulting to its path")),
3254 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3255 OPT_END()
3257 const char *const usage[] = {
3258 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3259 NULL
3261 struct strbuf sb = STRBUF_INIT;
3262 int ret = 1;
3264 argc = parse_options(argc, argv, prefix, options, usage, 0);
3266 if (!is_writing_gitmodules_ok())
3267 die(_("please make sure that the .gitmodules file is in the working tree"));
3269 if (prefix && *prefix &&
3270 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3271 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3273 if (argc == 0 || argc > 2)
3274 usage_with_options(usage, options);
3276 add_data.repo = argv[0];
3277 if (argc == 1)
3278 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3279 else
3280 add_data.sm_path = xstrdup(argv[1]);
3282 if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
3283 char *sm_path = add_data.sm_path;
3285 add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
3286 free(sm_path);
3289 if (starts_with_dot_dot_slash(add_data.repo) ||
3290 starts_with_dot_slash(add_data.repo)) {
3291 if (prefix)
3292 die(_("Relative path can only be used from the toplevel "
3293 "of the working tree"));
3295 /* dereference source url relative to parent's url */
3296 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3297 add_data.realrepo = to_free;
3298 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3299 add_data.realrepo = add_data.repo;
3300 } else {
3301 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3302 add_data.repo);
3306 * normalize path:
3307 * multiple //; leading ./; /./; /../;
3309 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3310 strip_dir_trailing_slashes(add_data.sm_path);
3312 die_on_index_match(add_data.sm_path, force);
3313 die_on_repo_without_commits(add_data.sm_path);
3315 if (!force) {
3316 struct child_process cp = CHILD_PROCESS_INIT;
3318 cp.git_cmd = 1;
3319 cp.no_stdout = 1;
3320 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3321 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3322 if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3323 strbuf_complete_line(&sb);
3324 fputs(sb.buf, stderr);
3325 goto cleanup;
3329 if(!add_data.sm_name)
3330 add_data.sm_name = add_data.sm_path;
3332 if (check_submodule_name(add_data.sm_name))
3333 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3335 add_data.prefix = prefix;
3336 add_data.force = !!force;
3337 add_data.quiet = !!quiet;
3338 add_data.progress = !!progress;
3339 add_data.dissociate = !!dissociate;
3341 if (add_submodule(&add_data))
3342 goto cleanup;
3343 configure_added_submodule(&add_data);
3345 ret = 0;
3346 cleanup:
3347 free(add_data.sm_path);
3348 free(to_free);
3349 strbuf_release(&sb);
3351 return ret;
3354 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3356 const char *cmd = argv[0];
3357 const char *subcmd;
3358 parse_opt_subcommand_fn *fn = NULL;
3359 const char *const usage[] = {
3360 N_("git submodule--helper <command>"),
3361 NULL
3363 struct option options[] = {
3364 OPT_SUBCOMMAND("clone", &fn, module_clone),
3365 OPT_SUBCOMMAND("add", &fn, module_add),
3366 OPT_SUBCOMMAND("update", &fn, module_update),
3367 OPT_SUBCOMMAND("foreach", &fn, module_foreach),
3368 OPT_SUBCOMMAND("init", &fn, module_init),
3369 OPT_SUBCOMMAND("status", &fn, module_status),
3370 OPT_SUBCOMMAND("sync", &fn, module_sync),
3371 OPT_SUBCOMMAND("deinit", &fn, module_deinit),
3372 OPT_SUBCOMMAND("summary", &fn, module_summary),
3373 OPT_SUBCOMMAND("push-check", &fn, push_check),
3374 OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
3375 OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3376 OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3377 OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3378 OPT_END()
3380 argc = parse_options(argc, argv, prefix, options, usage, 0);
3381 subcmd = argv[0];
3383 if (strcmp(subcmd, "clone") && strcmp(subcmd, "update") &&
3384 strcmp(subcmd, "foreach") && strcmp(subcmd, "status") &&
3385 strcmp(subcmd, "sync") && strcmp(subcmd, "absorbgitdirs") &&
3386 get_super_prefix())
3388 * xstrfmt() rather than "%s %s" to keep the translated
3389 * string identical to git.c's.
3391 die(_("%s doesn't support --super-prefix"),
3392 xstrfmt("'%s %s'", cmd, subcmd));
3394 return fn(argc, argv, prefix);