The second batch
[git.git] / builtin / submodule--helper.c
blob93f4b9d72663d5294ce9d678db8b7871f4bf41f7
1 #include "builtin.h"
2 #include "abspath.h"
3 #include "environment.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "repository.h"
7 #include "config.h"
8 #include "parse-options.h"
9 #include "quote.h"
10 #include "path.h"
11 #include "pathspec.h"
12 #include "preload-index.h"
13 #include "dir.h"
14 #include "read-cache.h"
15 #include "setup.h"
16 #include "sparse-index.h"
17 #include "submodule.h"
18 #include "submodule-config.h"
19 #include "string-list.h"
20 #include "run-command.h"
21 #include "remote.h"
22 #include "refs.h"
23 #include "refspec.h"
24 #include "revision.h"
25 #include "diffcore.h"
26 #include "diff.h"
27 #include "object-file.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
30 #include "advice.h"
31 #include "branch.h"
32 #include "list-objects-filter-options.h"
34 #define OPT_QUIET (1 << 0)
35 #define OPT_CACHED (1 << 1)
36 #define OPT_RECURSIVE (1 << 2)
37 #define OPT_FORCE (1 << 3)
39 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
40 void *cb_data);
42 static int repo_get_default_remote(struct repository *repo, char **default_remote)
44 char *dest = NULL;
45 struct strbuf sb = STRBUF_INIT;
46 struct ref_store *store = get_main_ref_store(repo);
47 const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
48 NULL);
50 if (!refname)
51 return die_message(_("No such ref: %s"), "HEAD");
53 /* detached HEAD */
54 if (!strcmp(refname, "HEAD")) {
55 *default_remote = xstrdup("origin");
56 return 0;
59 if (!skip_prefix(refname, "refs/heads/", &refname))
60 return die_message(_("Expecting a full ref name, got %s"),
61 refname);
63 strbuf_addf(&sb, "branch.%s.remote", refname);
64 if (repo_config_get_string(repo, sb.buf, &dest))
65 *default_remote = xstrdup("origin");
66 else
67 *default_remote = dest;
69 strbuf_release(&sb);
70 return 0;
73 static int get_default_remote_submodule(const char *module_path, char **default_remote)
75 struct repository subrepo;
76 int ret;
78 if (repo_submodule_init(&subrepo, the_repository, module_path,
79 null_oid()) < 0)
80 return die_message(_("could not get a repository handle for submodule '%s'"),
81 module_path);
82 ret = repo_get_default_remote(&subrepo, default_remote);
83 repo_clear(&subrepo);
85 return ret;
88 static char *get_default_remote(void)
90 char *default_remote;
91 int code = repo_get_default_remote(the_repository, &default_remote);
93 if (code)
94 exit(code);
96 return default_remote;
99 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
101 char *remoteurl, *resolved_url;
102 char *remote = get_default_remote();
103 struct strbuf remotesb = STRBUF_INIT;
105 strbuf_addf(&remotesb, "remote.%s.url", remote);
106 if (git_config_get_string(remotesb.buf, &remoteurl)) {
107 if (!quiet)
108 warning(_("could not look up configuration '%s'. "
109 "Assuming this repository is its own "
110 "authoritative upstream."),
111 remotesb.buf);
112 remoteurl = xgetcwd();
114 resolved_url = relative_url(remoteurl, rel_url, up_path);
116 free(remote);
117 free(remoteurl);
118 strbuf_release(&remotesb);
120 return resolved_url;
123 /* the result should be freed by the caller. */
124 static char *get_submodule_displaypath(const char *path, const char *prefix,
125 const char *super_prefix)
127 if (prefix && super_prefix) {
128 BUG("cannot have prefix '%s' and superprefix '%s'",
129 prefix, super_prefix);
130 } else if (prefix) {
131 struct strbuf sb = STRBUF_INIT;
132 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
133 strbuf_release(&sb);
134 return displaypath;
135 } else if (super_prefix) {
136 return xstrfmt("%s%s", super_prefix, path);
137 } else {
138 return xstrdup(path);
142 static char *compute_rev_name(const char *sub_path, const char* object_id)
144 struct strbuf sb = STRBUF_INIT;
145 const char ***d;
147 static const char *describe_bare[] = { NULL };
149 static const char *describe_tags[] = { "--tags", NULL };
151 static const char *describe_contains[] = { "--contains", NULL };
153 static const char *describe_all_always[] = { "--all", "--always", NULL };
155 static const char **describe_argv[] = { describe_bare, describe_tags,
156 describe_contains,
157 describe_all_always, NULL };
159 for (d = describe_argv; *d; d++) {
160 struct child_process cp = CHILD_PROCESS_INIT;
161 prepare_submodule_repo_env(&cp.env);
162 cp.dir = sub_path;
163 cp.git_cmd = 1;
164 cp.no_stderr = 1;
166 strvec_push(&cp.args, "describe");
167 strvec_pushv(&cp.args, *d);
168 strvec_push(&cp.args, object_id);
170 if (!capture_command(&cp, &sb, 0)) {
171 strbuf_strip_suffix(&sb, "\n");
172 return strbuf_detach(&sb, NULL);
176 strbuf_release(&sb);
177 return NULL;
180 struct module_list {
181 const struct cache_entry **entries;
182 int alloc, nr;
184 #define MODULE_LIST_INIT { 0 }
186 static void module_list_release(struct module_list *ml)
188 free(ml->entries);
191 static int module_list_compute(const char **argv,
192 const char *prefix,
193 struct pathspec *pathspec,
194 struct module_list *list)
196 int i, result = 0;
197 char *ps_matched = NULL;
199 parse_pathspec(pathspec, 0,
200 PATHSPEC_PREFER_FULL,
201 prefix, argv);
203 if (pathspec->nr)
204 ps_matched = xcalloc(pathspec->nr, 1);
206 if (repo_read_index(the_repository) < 0)
207 die(_("index file corrupt"));
209 for (i = 0; i < the_repository->index->cache_nr; i++) {
210 const struct cache_entry *ce = the_repository->index->cache[i];
212 if (!match_pathspec(the_repository->index, pathspec, ce->name, ce_namelen(ce),
213 0, ps_matched, 1) ||
214 !S_ISGITLINK(ce->ce_mode))
215 continue;
217 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
218 list->entries[list->nr++] = ce;
219 while (i + 1 < the_repository->index->cache_nr &&
220 !strcmp(ce->name, the_repository->index->cache[i + 1]->name))
222 * Skip entries with the same name in different stages
223 * to make sure an entry is returned only once.
225 i++;
228 if (ps_matched && report_path_error(ps_matched, pathspec))
229 result = -1;
231 free(ps_matched);
233 return result;
236 static void module_list_active(struct module_list *list)
238 int i;
239 struct module_list active_modules = MODULE_LIST_INIT;
241 for (i = 0; i < list->nr; i++) {
242 const struct cache_entry *ce = list->entries[i];
244 if (!is_submodule_active(the_repository, ce->name))
245 continue;
247 ALLOC_GROW(active_modules.entries,
248 active_modules.nr + 1,
249 active_modules.alloc);
250 active_modules.entries[active_modules.nr++] = ce;
253 module_list_release(list);
254 *list = active_modules;
257 static char *get_up_path(const char *path)
259 int i;
260 struct strbuf sb = STRBUF_INIT;
262 for (i = count_slashes(path); i; i--)
263 strbuf_addstr(&sb, "../");
266 * Check if 'path' ends with slash or not
267 * for having the same output for dir/sub_dir
268 * and dir/sub_dir/
270 if (!is_dir_sep(path[strlen(path) - 1]))
271 strbuf_addstr(&sb, "../");
273 return strbuf_detach(&sb, NULL);
276 static void for_each_listed_submodule(const struct module_list *list,
277 each_submodule_fn fn, void *cb_data)
279 int i;
281 for (i = 0; i < list->nr; i++)
282 fn(list->entries[i], cb_data);
285 struct foreach_cb {
286 int argc;
287 const char **argv;
288 const char *prefix;
289 const char *super_prefix;
290 int quiet;
291 int recursive;
293 #define FOREACH_CB_INIT { 0 }
295 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
296 void *cb_data)
298 struct foreach_cb *info = cb_data;
299 const char *path = list_item->name;
300 const struct object_id *ce_oid = &list_item->oid;
301 const struct submodule *sub;
302 struct child_process cp = CHILD_PROCESS_INIT;
303 char *displaypath;
305 displaypath = get_submodule_displaypath(path, info->prefix,
306 info->super_prefix);
308 sub = submodule_from_path(the_repository, null_oid(), path);
310 if (!sub)
311 die(_("No url found for submodule path '%s' in .gitmodules"),
312 displaypath);
314 if (!is_submodule_populated_gently(path, NULL))
315 goto cleanup;
317 prepare_submodule_repo_env(&cp.env);
320 * For the purpose of executing <command> in the submodule,
321 * separate shell is used for the purpose of running the
322 * child process.
324 cp.use_shell = 1;
325 cp.dir = path;
328 * NEEDSWORK: the command currently has access to the variables $name,
329 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
330 * contains a single argument. This is done for maintaining a faithful
331 * translation from shell script.
333 if (info->argc == 1) {
334 char *toplevel = xgetcwd();
335 struct strbuf sb = STRBUF_INIT;
337 strvec_pushf(&cp.env, "name=%s", sub->name);
338 strvec_pushf(&cp.env, "sm_path=%s", path);
339 strvec_pushf(&cp.env, "displaypath=%s", displaypath);
340 strvec_pushf(&cp.env, "sha1=%s",
341 oid_to_hex(ce_oid));
342 strvec_pushf(&cp.env, "toplevel=%s", toplevel);
345 * Since the path variable was accessible from the script
346 * before porting, it is also made available after porting.
347 * The environment variable "PATH" has a very special purpose
348 * on windows. And since environment variables are
349 * case-insensitive in windows, it interferes with the
350 * existing PATH variable. Hence, to avoid that, we expose
351 * path via the args strvec and not via env.
353 sq_quote_buf(&sb, path);
354 strvec_pushf(&cp.args, "path=%s; %s",
355 sb.buf, info->argv[0]);
356 strbuf_release(&sb);
357 free(toplevel);
358 } else {
359 strvec_pushv(&cp.args, info->argv);
362 if (!info->quiet)
363 printf(_("Entering '%s'\n"), displaypath);
365 if (info->argv[0] && run_command(&cp))
366 die(_("run_command returned non-zero status for %s\n."),
367 displaypath);
369 if (info->recursive) {
370 struct child_process cpr = CHILD_PROCESS_INIT;
372 cpr.git_cmd = 1;
373 cpr.dir = path;
374 prepare_submodule_repo_env(&cpr.env);
376 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
377 NULL);
378 strvec_pushl(&cpr.args, "--super-prefix", NULL);
379 strvec_pushf(&cpr.args, "%s/", displaypath);
381 if (info->quiet)
382 strvec_push(&cpr.args, "--quiet");
384 strvec_push(&cpr.args, "--");
385 strvec_pushv(&cpr.args, info->argv);
387 if (run_command(&cpr))
388 die(_("run_command returned non-zero status while "
389 "recursing in the nested submodules of %s\n."),
390 displaypath);
393 cleanup:
394 free(displaypath);
397 static int module_foreach(int argc, const char **argv, const char *prefix)
399 struct foreach_cb info = FOREACH_CB_INIT;
400 struct pathspec pathspec = { 0 };
401 struct module_list list = MODULE_LIST_INIT;
402 struct option module_foreach_options[] = {
403 OPT__SUPER_PREFIX(&info.super_prefix),
404 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
405 OPT_BOOL(0, "recursive", &info.recursive,
406 N_("recurse into nested submodules")),
407 OPT_END()
409 const char *const git_submodule_helper_usage[] = {
410 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
411 NULL
413 int ret = 1;
415 argc = parse_options(argc, argv, prefix, module_foreach_options,
416 git_submodule_helper_usage, 0);
418 if (module_list_compute(NULL, prefix, &pathspec, &list) < 0)
419 goto cleanup;
421 info.argc = argc;
422 info.argv = argv;
423 info.prefix = prefix;
425 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
427 ret = 0;
428 cleanup:
429 module_list_release(&list);
430 clear_pathspec(&pathspec);
431 return ret;
434 static int starts_with_dot_slash(const char *const path)
436 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
437 PATH_MATCH_XPLATFORM);
440 static int starts_with_dot_dot_slash(const char *const path)
442 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
443 PATH_MATCH_XPLATFORM);
446 struct init_cb {
447 const char *prefix;
448 const char *super_prefix;
449 unsigned int flags;
451 #define INIT_CB_INIT { 0 }
453 static void init_submodule(const char *path, const char *prefix,
454 const char *super_prefix,
455 unsigned int flags)
457 const struct submodule *sub;
458 struct strbuf sb = STRBUF_INIT;
459 const char *upd;
460 char *url = NULL, *displaypath;
462 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
464 sub = submodule_from_path(the_repository, null_oid(), path);
466 if (!sub)
467 die(_("No url found for submodule path '%s' in .gitmodules"),
468 displaypath);
471 * NEEDSWORK: In a multi-working-tree world, this needs to be
472 * set in the per-worktree config.
474 * Set active flag for the submodule being initialized
476 if (!is_submodule_active(the_repository, path)) {
477 strbuf_addf(&sb, "submodule.%s.active", sub->name);
478 git_config_set_gently(sb.buf, "true");
479 strbuf_reset(&sb);
483 * Copy url setting when it is not set yet.
484 * To look up the url in .git/config, we must not fall back to
485 * .gitmodules, so look it up directly.
487 strbuf_addf(&sb, "submodule.%s.url", sub->name);
488 if (git_config_get_string(sb.buf, &url)) {
489 if (!sub->url)
490 die(_("No url found for submodule path '%s' in .gitmodules"),
491 displaypath);
493 url = xstrdup(sub->url);
495 /* Possibly a url relative to parent */
496 if (starts_with_dot_dot_slash(url) ||
497 starts_with_dot_slash(url)) {
498 char *oldurl = url;
500 url = resolve_relative_url(oldurl, NULL, 0);
501 free(oldurl);
504 if (git_config_set_gently(sb.buf, url))
505 die(_("Failed to register url for submodule path '%s'"),
506 displaypath);
507 if (!(flags & OPT_QUIET))
508 fprintf(stderr,
509 _("Submodule '%s' (%s) registered for path '%s'\n"),
510 sub->name, url, displaypath);
512 strbuf_reset(&sb);
514 /* Copy "update" setting when it is not set yet */
515 strbuf_addf(&sb, "submodule.%s.update", sub->name);
516 if (git_config_get_string_tmp(sb.buf, &upd) &&
517 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
518 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
519 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
520 sub->name);
521 upd = "none";
522 } else {
523 upd = submodule_update_type_to_string(sub->update_strategy.type);
526 if (git_config_set_gently(sb.buf, upd))
527 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
529 strbuf_release(&sb);
530 free(displaypath);
531 free(url);
534 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
536 struct init_cb *info = cb_data;
538 init_submodule(list_item->name, info->prefix, info->super_prefix,
539 info->flags);
542 static int module_init(int argc, const char **argv, const char *prefix)
544 struct init_cb info = INIT_CB_INIT;
545 struct pathspec pathspec = { 0 };
546 struct module_list list = MODULE_LIST_INIT;
547 int quiet = 0;
548 struct option module_init_options[] = {
549 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
550 OPT_END()
552 const char *const git_submodule_helper_usage[] = {
553 N_("git submodule init [<options>] [<path>]"),
554 NULL
556 int ret = 1;
558 argc = parse_options(argc, argv, prefix, module_init_options,
559 git_submodule_helper_usage, 0);
561 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
562 goto cleanup;
565 * If there are no path args and submodule.active is set then,
566 * by default, only initialize 'active' modules.
568 if (!argc && !git_config_get("submodule.active"))
569 module_list_active(&list);
571 info.prefix = prefix;
572 if (quiet)
573 info.flags |= OPT_QUIET;
575 for_each_listed_submodule(&list, init_submodule_cb, &info);
577 ret = 0;
578 cleanup:
579 module_list_release(&list);
580 clear_pathspec(&pathspec);
581 return ret;
584 struct status_cb {
585 const char *prefix;
586 const char *super_prefix;
587 unsigned int flags;
589 #define STATUS_CB_INIT { 0 }
591 static void print_status(unsigned int flags, char state, const char *path,
592 const struct object_id *oid, const char *displaypath)
594 if (flags & OPT_QUIET)
595 return;
597 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
599 if (state == ' ' || state == '+') {
600 char *name = compute_rev_name(path, oid_to_hex(oid));
602 if (name)
603 printf(" (%s)", name);
604 free(name);
607 printf("\n");
610 static int handle_submodule_head_ref(const char *refname UNUSED,
611 const struct object_id *oid,
612 int flags UNUSED,
613 void *cb_data)
615 struct object_id *output = cb_data;
617 if (oid)
618 oidcpy(output, oid);
620 return 0;
623 static void status_submodule(const char *path, const struct object_id *ce_oid,
624 unsigned int ce_flags, const char *prefix,
625 const char *super_prefix, unsigned int flags)
627 char *displaypath;
628 struct strvec diff_files_args = STRVEC_INIT;
629 struct rev_info rev = REV_INFO_INIT;
630 struct strbuf buf = STRBUF_INIT;
631 const char *git_dir;
632 struct setup_revision_opt opt = {
633 .free_removed_argv_elements = 1,
636 if (!submodule_from_path(the_repository, null_oid(), path))
637 die(_("no submodule mapping found in .gitmodules for path '%s'"),
638 path);
640 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
642 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
643 print_status(flags, 'U', path, null_oid(), displaypath);
644 goto cleanup;
647 strbuf_addf(&buf, "%s/.git", path);
648 git_dir = read_gitfile(buf.buf);
649 if (!git_dir)
650 git_dir = buf.buf;
652 if (!is_submodule_active(the_repository, path) ||
653 !is_git_directory(git_dir)) {
654 print_status(flags, '-', path, ce_oid, displaypath);
655 strbuf_release(&buf);
656 goto cleanup;
658 strbuf_release(&buf);
660 strvec_pushl(&diff_files_args, "diff-files",
661 "--ignore-submodules=dirty", "--quiet", "--",
662 path, NULL);
664 git_config(git_diff_basic_config, NULL);
666 repo_init_revisions(the_repository, &rev, NULL);
667 rev.abbrev = 0;
668 setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt);
669 run_diff_files(&rev, 0);
671 if (!diff_result_code(&rev.diffopt)) {
672 print_status(flags, ' ', path, ce_oid,
673 displaypath);
674 } else if (!(flags & OPT_CACHED)) {
675 struct object_id oid;
676 struct ref_store *refs = get_submodule_ref_store(path);
678 if (!refs) {
679 print_status(flags, '-', path, ce_oid, displaypath);
680 goto cleanup;
682 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
683 die(_("could not resolve HEAD ref inside the "
684 "submodule '%s'"), path);
686 print_status(flags, '+', path, &oid, displaypath);
687 } else {
688 print_status(flags, '+', path, ce_oid, displaypath);
691 if (flags & OPT_RECURSIVE) {
692 struct child_process cpr = CHILD_PROCESS_INIT;
694 cpr.git_cmd = 1;
695 cpr.dir = path;
696 prepare_submodule_repo_env(&cpr.env);
698 strvec_pushl(&cpr.args, "submodule--helper", "status",
699 "--recursive", NULL);
700 strvec_push(&cpr.args, "--super-prefix");
701 strvec_pushf(&cpr.args, "%s/", displaypath);
703 if (flags & OPT_CACHED)
704 strvec_push(&cpr.args, "--cached");
706 if (flags & OPT_QUIET)
707 strvec_push(&cpr.args, "--quiet");
709 if (run_command(&cpr))
710 die(_("failed to recurse into submodule '%s'"), path);
713 cleanup:
714 strvec_clear(&diff_files_args);
715 free(displaypath);
716 release_revisions(&rev);
719 static void status_submodule_cb(const struct cache_entry *list_item,
720 void *cb_data)
722 struct status_cb *info = cb_data;
724 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
725 info->prefix, info->super_prefix, info->flags);
728 static int module_status(int argc, const char **argv, const char *prefix)
730 struct status_cb info = STATUS_CB_INIT;
731 struct pathspec pathspec = { 0 };
732 struct module_list list = MODULE_LIST_INIT;
733 int quiet = 0;
734 struct option module_status_options[] = {
735 OPT__SUPER_PREFIX(&info.super_prefix),
736 OPT__QUIET(&quiet, N_("suppress submodule status output")),
737 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
738 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
739 OPT_END()
741 const char *const git_submodule_helper_usage[] = {
742 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
743 NULL
745 int ret = 1;
747 argc = parse_options(argc, argv, prefix, module_status_options,
748 git_submodule_helper_usage, 0);
750 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
751 goto cleanup;
753 info.prefix = prefix;
754 if (quiet)
755 info.flags |= OPT_QUIET;
757 for_each_listed_submodule(&list, status_submodule_cb, &info);
759 ret = 0;
760 cleanup:
761 module_list_release(&list);
762 clear_pathspec(&pathspec);
763 return ret;
766 struct module_cb {
767 unsigned int mod_src;
768 unsigned int mod_dst;
769 struct object_id oid_src;
770 struct object_id oid_dst;
771 char status;
772 char *sm_path;
774 #define MODULE_CB_INIT { 0 }
776 static void module_cb_release(struct module_cb *mcb)
778 free(mcb->sm_path);
781 struct module_cb_list {
782 struct module_cb **entries;
783 int alloc, nr;
785 #define MODULE_CB_LIST_INIT { 0 }
787 static void module_cb_list_release(struct module_cb_list *mcbl)
789 int i;
791 for (i = 0; i < mcbl->nr; i++) {
792 struct module_cb *mcb = mcbl->entries[i];
794 module_cb_release(mcb);
795 free(mcb);
797 free(mcbl->entries);
800 struct summary_cb {
801 int argc;
802 const char **argv;
803 const char *prefix;
804 const char *super_prefix;
805 unsigned int cached: 1;
806 unsigned int for_status: 1;
807 unsigned int files: 1;
808 int summary_limit;
810 #define SUMMARY_CB_INIT { 0 }
812 enum diff_cmd {
813 DIFF_INDEX,
814 DIFF_FILES
817 static char *verify_submodule_committish(const char *sm_path,
818 const char *committish)
820 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
821 struct strbuf result = STRBUF_INIT;
823 cp_rev_parse.git_cmd = 1;
824 cp_rev_parse.dir = sm_path;
825 prepare_submodule_repo_env(&cp_rev_parse.env);
826 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
827 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
828 strvec_push(&cp_rev_parse.args, "--");
830 if (capture_command(&cp_rev_parse, &result, 0))
831 return NULL;
833 strbuf_trim_trailing_newline(&result);
834 return strbuf_detach(&result, NULL);
837 static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
838 int total_commits, const char *displaypath,
839 const char *src_abbrev, const char *dst_abbrev,
840 struct module_cb *p)
842 if (p->status == 'T') {
843 if (S_ISGITLINK(p->mod_dst))
844 printf(_("* %s %s(blob)->%s(submodule)"),
845 displaypath, src_abbrev, dst_abbrev);
846 else
847 printf(_("* %s %s(submodule)->%s(blob)"),
848 displaypath, src_abbrev, dst_abbrev);
849 } else {
850 printf("* %s %s...%s",
851 displaypath, src_abbrev, dst_abbrev);
854 if (total_commits < 0)
855 printf(":\n");
856 else
857 printf(" (%d):\n", total_commits);
859 if (errmsg) {
860 printf(_("%s"), errmsg);
861 } else if (total_commits > 0) {
862 struct child_process cp_log = CHILD_PROCESS_INIT;
864 cp_log.git_cmd = 1;
865 cp_log.dir = p->sm_path;
866 prepare_submodule_repo_env(&cp_log.env);
867 strvec_pushl(&cp_log.args, "log", NULL);
869 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
870 if (info->summary_limit > 0)
871 strvec_pushf(&cp_log.args, "-%d",
872 info->summary_limit);
874 strvec_pushl(&cp_log.args, "--pretty= %m %s",
875 "--first-parent", NULL);
876 strvec_pushf(&cp_log.args, "%s...%s",
877 src_abbrev, dst_abbrev);
878 } else if (S_ISGITLINK(p->mod_dst)) {
879 strvec_pushl(&cp_log.args, "--pretty= > %s",
880 "-1", dst_abbrev, NULL);
881 } else {
882 strvec_pushl(&cp_log.args, "--pretty= < %s",
883 "-1", src_abbrev, NULL);
885 run_command(&cp_log);
887 printf("\n");
890 static void generate_submodule_summary(struct summary_cb *info,
891 struct module_cb *p)
893 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
894 int missing_src = 0, missing_dst = 0;
895 struct strbuf errmsg = STRBUF_INIT;
896 int total_commits = -1;
898 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
899 if (S_ISGITLINK(p->mod_dst)) {
900 struct ref_store *refs = get_submodule_ref_store(p->sm_path);
902 if (refs)
903 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
904 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
905 struct stat st;
906 int fd = open(p->sm_path, O_RDONLY);
908 if (fd < 0 || fstat(fd, &st) < 0 ||
909 index_fd(the_repository->index, &p->oid_dst, fd, &st, OBJ_BLOB,
910 p->sm_path, 0))
911 error(_("couldn't hash object from '%s'"), p->sm_path);
912 } else {
913 /* for a submodule removal (mode:0000000), don't warn */
914 if (p->mod_dst)
915 warning(_("unexpected mode %o\n"), p->mod_dst);
919 if (S_ISGITLINK(p->mod_src)) {
920 if (p->status != 'D')
921 src_abbrev = verify_submodule_committish(p->sm_path,
922 oid_to_hex(&p->oid_src));
923 if (!src_abbrev) {
924 missing_src = 1;
926 * As `rev-parse` failed, we fallback to getting
927 * the abbreviated hash using oid_src. We do
928 * this as we might still need the abbreviated
929 * hash in cases like a submodule type change, etc.
931 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
933 } else {
935 * The source does not point to a submodule.
936 * So, we fallback to getting the abbreviation using
937 * oid_src as we might still need the abbreviated
938 * hash in cases like submodule add, etc.
940 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
943 if (S_ISGITLINK(p->mod_dst)) {
944 dst_abbrev = verify_submodule_committish(p->sm_path,
945 oid_to_hex(&p->oid_dst));
946 if (!dst_abbrev) {
947 missing_dst = 1;
949 * As `rev-parse` failed, we fallback to getting
950 * the abbreviated hash using oid_dst. We do
951 * this as we might still need the abbreviated
952 * hash in cases like a submodule type change, etc.
954 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
956 } else {
958 * The destination does not point to a submodule.
959 * So, we fallback to getting the abbreviation using
960 * oid_dst as we might still need the abbreviated
961 * hash in cases like a submodule removal, etc.
963 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
966 displaypath = get_submodule_displaypath(p->sm_path, info->prefix,
967 info->super_prefix);
969 if (!missing_src && !missing_dst) {
970 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
971 struct strbuf sb_rev_list = STRBUF_INIT;
973 strvec_pushl(&cp_rev_list.args, "rev-list",
974 "--first-parent", "--count", NULL);
975 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
976 strvec_pushf(&cp_rev_list.args, "%s...%s",
977 src_abbrev, dst_abbrev);
978 else
979 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
980 src_abbrev : dst_abbrev);
981 strvec_push(&cp_rev_list.args, "--");
983 cp_rev_list.git_cmd = 1;
984 cp_rev_list.dir = p->sm_path;
985 prepare_submodule_repo_env(&cp_rev_list.env);
987 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
988 total_commits = atoi(sb_rev_list.buf);
990 strbuf_release(&sb_rev_list);
991 } else {
993 * Don't give error msg for modification whose dst is not
994 * submodule, i.e., deleted or changed to blob
996 if (S_ISGITLINK(p->mod_dst)) {
997 if (missing_src && missing_dst) {
998 strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n",
999 displaypath, oid_to_hex(&p->oid_src),
1000 oid_to_hex(&p->oid_dst));
1001 } else {
1002 strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n",
1003 displaypath, missing_src ?
1004 oid_to_hex(&p->oid_src) :
1005 oid_to_hex(&p->oid_dst));
1010 print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
1011 total_commits, displaypath, src_abbrev,
1012 dst_abbrev, p);
1014 free(displaypath);
1015 free(src_abbrev);
1016 free(dst_abbrev);
1017 strbuf_release(&errmsg);
1020 static void prepare_submodule_summary(struct summary_cb *info,
1021 struct module_cb_list *list)
1023 int i;
1024 for (i = 0; i < list->nr; i++) {
1025 const struct submodule *sub;
1026 struct module_cb *p = list->entries[i];
1027 struct strbuf sm_gitdir = STRBUF_INIT;
1029 if (p->status == 'D' || p->status == 'T') {
1030 generate_submodule_summary(info, p);
1031 continue;
1034 if (info->for_status && p->status != 'A' &&
1035 (sub = submodule_from_path(the_repository,
1036 null_oid(), p->sm_path))) {
1037 char *config_key = NULL;
1038 const char *value;
1039 int ignore_all = 0;
1041 config_key = xstrfmt("submodule.%s.ignore",
1042 sub->name);
1043 if (!git_config_get_string_tmp(config_key, &value))
1044 ignore_all = !strcmp(value, "all");
1045 else if (sub->ignore)
1046 ignore_all = !strcmp(sub->ignore, "all");
1048 free(config_key);
1049 if (ignore_all)
1050 continue;
1053 /* Also show added or modified modules which are checked out */
1054 strbuf_addstr(&sm_gitdir, p->sm_path);
1055 if (is_nonbare_repository_dir(&sm_gitdir))
1056 generate_submodule_summary(info, p);
1057 strbuf_release(&sm_gitdir);
1061 static void submodule_summary_callback(struct diff_queue_struct *q,
1062 struct diff_options *options UNUSED,
1063 void *data)
1065 int i;
1066 struct module_cb_list *list = data;
1067 for (i = 0; i < q->nr; i++) {
1068 struct diff_filepair *p = q->queue[i];
1069 struct module_cb *temp;
1071 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1072 continue;
1073 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1074 temp->mod_src = p->one->mode;
1075 temp->mod_dst = p->two->mode;
1076 temp->oid_src = p->one->oid;
1077 temp->oid_dst = p->two->oid;
1078 temp->status = p->status;
1079 temp->sm_path = xstrdup(p->one->path);
1081 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1082 list->entries[list->nr++] = temp;
1086 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1088 switch (diff_cmd) {
1089 case DIFF_INDEX: return "diff-index";
1090 case DIFF_FILES: return "diff-files";
1091 default: BUG("bad diff_cmd value %d", diff_cmd);
1095 static int compute_summary_module_list(struct object_id *head_oid,
1096 struct summary_cb *info,
1097 enum diff_cmd diff_cmd)
1099 struct strvec diff_args = STRVEC_INIT;
1100 struct rev_info rev;
1101 struct setup_revision_opt opt = {
1102 .free_removed_argv_elements = 1,
1104 struct module_cb_list list = MODULE_CB_LIST_INIT;
1105 int ret = 0;
1107 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1108 if (info->cached)
1109 strvec_push(&diff_args, "--cached");
1110 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1111 if (head_oid)
1112 strvec_push(&diff_args, oid_to_hex(head_oid));
1113 strvec_push(&diff_args, "--");
1114 if (info->argc)
1115 strvec_pushv(&diff_args, info->argv);
1117 git_config(git_diff_basic_config, NULL);
1118 repo_init_revisions(the_repository, &rev, info->prefix);
1119 rev.abbrev = 0;
1120 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1121 setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
1122 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1123 rev.diffopt.format_callback = submodule_summary_callback;
1124 rev.diffopt.format_callback_data = &list;
1126 if (!info->cached) {
1127 if (diff_cmd == DIFF_INDEX)
1128 setup_work_tree();
1129 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
1130 perror("repo_read_index_preload");
1131 ret = -1;
1132 goto cleanup;
1134 } else if (repo_read_index(the_repository) < 0) {
1135 perror("repo_read_cache");
1136 ret = -1;
1137 goto cleanup;
1140 if (diff_cmd == DIFF_INDEX)
1141 run_diff_index(&rev, info->cached ? DIFF_INDEX_CACHED : 0);
1142 else
1143 run_diff_files(&rev, 0);
1144 prepare_submodule_summary(info, &list);
1145 cleanup:
1146 strvec_clear(&diff_args);
1147 release_revisions(&rev);
1148 module_cb_list_release(&list);
1149 return ret;
1152 static int module_summary(int argc, const char **argv, const char *prefix)
1154 struct summary_cb info = SUMMARY_CB_INIT;
1155 int cached = 0;
1156 int for_status = 0;
1157 int files = 0;
1158 int summary_limit = -1;
1159 enum diff_cmd diff_cmd = DIFF_INDEX;
1160 struct object_id head_oid;
1161 int ret;
1162 struct option module_summary_options[] = {
1163 OPT_BOOL(0, "cached", &cached,
1164 N_("use the commit stored in the index instead of the submodule HEAD")),
1165 OPT_BOOL(0, "files", &files,
1166 N_("compare the commit in the index with that in the submodule HEAD")),
1167 OPT_BOOL(0, "for-status", &for_status,
1168 N_("skip submodules with 'ignore_config' value set to 'all'")),
1169 OPT_INTEGER('n', "summary-limit", &summary_limit,
1170 N_("limit the summary size")),
1171 OPT_END()
1173 const char *const git_submodule_helper_usage[] = {
1174 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1175 NULL
1178 argc = parse_options(argc, argv, prefix, module_summary_options,
1179 git_submodule_helper_usage, 0);
1181 if (!summary_limit)
1182 return 0;
1184 if (!repo_get_oid(the_repository, argc ? argv[0] : "HEAD", &head_oid)) {
1185 if (argc) {
1186 argv++;
1187 argc--;
1189 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1190 /* before the first commit: compare with an empty tree */
1191 oidcpy(&head_oid, the_hash_algo->empty_tree);
1192 if (argc) {
1193 argv++;
1194 argc--;
1196 } else {
1197 if (repo_get_oid(the_repository, "HEAD", &head_oid))
1198 die(_("could not fetch a revision for HEAD"));
1201 if (files) {
1202 if (cached)
1203 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1204 diff_cmd = DIFF_FILES;
1207 info.argc = argc;
1208 info.argv = argv;
1209 info.prefix = prefix;
1210 info.cached = !!cached;
1211 info.files = !!files;
1212 info.for_status = !!for_status;
1213 info.summary_limit = summary_limit;
1215 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1216 &info, diff_cmd);
1217 return ret;
1220 struct sync_cb {
1221 const char *prefix;
1222 const char *super_prefix;
1223 unsigned int flags;
1225 #define SYNC_CB_INIT { 0 }
1227 static void sync_submodule(const char *path, const char *prefix,
1228 const char *super_prefix, unsigned int flags)
1230 const struct submodule *sub;
1231 char *remote_key = NULL;
1232 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1233 struct strbuf sb = STRBUF_INIT;
1234 char *sub_config_path = NULL;
1235 int code;
1237 if (!is_submodule_active(the_repository, path))
1238 return;
1240 sub = submodule_from_path(the_repository, null_oid(), path);
1242 if (sub && sub->url) {
1243 if (starts_with_dot_dot_slash(sub->url) ||
1244 starts_with_dot_slash(sub->url)) {
1245 char *up_path = get_up_path(path);
1247 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1248 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1249 free(up_path);
1250 } else {
1251 sub_origin_url = xstrdup(sub->url);
1252 super_config_url = xstrdup(sub->url);
1254 } else {
1255 sub_origin_url = xstrdup("");
1256 super_config_url = xstrdup("");
1259 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
1261 if (!(flags & OPT_QUIET))
1262 printf(_("Synchronizing submodule url for '%s'\n"),
1263 displaypath);
1265 strbuf_reset(&sb);
1266 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1267 if (git_config_set_gently(sb.buf, super_config_url))
1268 die(_("failed to register url for submodule path '%s'"),
1269 displaypath);
1271 if (!is_submodule_populated_gently(path, NULL))
1272 goto cleanup;
1274 strbuf_reset(&sb);
1275 code = get_default_remote_submodule(path, &default_remote);
1276 if (code)
1277 exit(code);
1279 remote_key = xstrfmt("remote.%s.url", default_remote);
1280 free(default_remote);
1282 submodule_to_gitdir(&sb, path);
1283 strbuf_addstr(&sb, "/config");
1285 if (git_config_set_in_file_gently(sb.buf, remote_key, NULL, sub_origin_url))
1286 die(_("failed to update remote for submodule '%s'"),
1287 path);
1289 if (flags & OPT_RECURSIVE) {
1290 struct child_process cpr = CHILD_PROCESS_INIT;
1292 cpr.git_cmd = 1;
1293 cpr.dir = path;
1294 prepare_submodule_repo_env(&cpr.env);
1296 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1297 "--recursive", NULL);
1298 strvec_push(&cpr.args, "--super-prefix");
1299 strvec_pushf(&cpr.args, "%s/", displaypath);
1302 if (flags & OPT_QUIET)
1303 strvec_push(&cpr.args, "--quiet");
1305 if (run_command(&cpr))
1306 die(_("failed to recurse into submodule '%s'"),
1307 path);
1310 cleanup:
1311 free(super_config_url);
1312 free(sub_origin_url);
1313 strbuf_release(&sb);
1314 free(remote_key);
1315 free(displaypath);
1316 free(sub_config_path);
1319 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1321 struct sync_cb *info = cb_data;
1323 sync_submodule(list_item->name, info->prefix, info->super_prefix,
1324 info->flags);
1327 static int module_sync(int argc, const char **argv, const char *prefix)
1329 struct sync_cb info = SYNC_CB_INIT;
1330 struct pathspec pathspec = { 0 };
1331 struct module_list list = MODULE_LIST_INIT;
1332 int quiet = 0;
1333 int recursive = 0;
1334 struct option module_sync_options[] = {
1335 OPT__SUPER_PREFIX(&info.super_prefix),
1336 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1337 OPT_BOOL(0, "recursive", &recursive,
1338 N_("recurse into nested submodules")),
1339 OPT_END()
1341 const char *const git_submodule_helper_usage[] = {
1342 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1343 NULL
1345 int ret = 1;
1347 argc = parse_options(argc, argv, prefix, module_sync_options,
1348 git_submodule_helper_usage, 0);
1350 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1351 goto cleanup;
1353 info.prefix = prefix;
1354 if (quiet)
1355 info.flags |= OPT_QUIET;
1356 if (recursive)
1357 info.flags |= OPT_RECURSIVE;
1359 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1361 ret = 0;
1362 cleanup:
1363 module_list_release(&list);
1364 clear_pathspec(&pathspec);
1365 return ret;
1368 struct deinit_cb {
1369 const char *prefix;
1370 unsigned int flags;
1372 #define DEINIT_CB_INIT { 0 }
1374 static void deinit_submodule(const char *path, const char *prefix,
1375 unsigned int flags)
1377 const struct submodule *sub;
1378 char *displaypath = NULL;
1379 struct child_process cp_config = CHILD_PROCESS_INIT;
1380 struct strbuf sb_config = STRBUF_INIT;
1381 char *sub_git_dir = xstrfmt("%s/.git", path);
1383 sub = submodule_from_path(the_repository, null_oid(), path);
1385 if (!sub || !sub->name)
1386 goto cleanup;
1388 displaypath = get_submodule_displaypath(path, prefix, NULL);
1390 /* remove the submodule work tree (unless the user already did it) */
1391 if (is_directory(path)) {
1392 struct strbuf sb_rm = STRBUF_INIT;
1393 const char *format;
1395 if (is_directory(sub_git_dir)) {
1396 if (!(flags & OPT_QUIET))
1397 warning(_("Submodule work tree '%s' contains a .git "
1398 "directory. This will be replaced with a "
1399 ".git file by using absorbgitdirs."),
1400 displaypath);
1402 absorb_git_dir_into_superproject(path, NULL);
1406 if (!(flags & OPT_FORCE)) {
1407 struct child_process cp_rm = CHILD_PROCESS_INIT;
1409 cp_rm.git_cmd = 1;
1410 strvec_pushl(&cp_rm.args, "rm", "-qn",
1411 path, NULL);
1413 if (run_command(&cp_rm))
1414 die(_("Submodule work tree '%s' contains local "
1415 "modifications; use '-f' to discard them"),
1416 displaypath);
1419 strbuf_addstr(&sb_rm, path);
1421 if (!remove_dir_recursively(&sb_rm, 0))
1422 format = _("Cleared directory '%s'\n");
1423 else
1424 format = _("Could not remove submodule work tree '%s'\n");
1426 if (!(flags & OPT_QUIET))
1427 printf(format, displaypath);
1429 submodule_unset_core_worktree(sub);
1431 strbuf_release(&sb_rm);
1434 if (mkdir(path, 0777))
1435 printf(_("could not create empty submodule directory %s"),
1436 displaypath);
1438 cp_config.git_cmd = 1;
1439 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1440 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1442 /* remove the .git/config entries (unless the user already did it) */
1443 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1444 char *sub_key = xstrfmt("submodule.%s", sub->name);
1447 * remove the whole section so we have a clean state when
1448 * the user later decides to init this submodule again
1450 git_config_rename_section_in_file(NULL, sub_key, NULL);
1451 if (!(flags & OPT_QUIET))
1452 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1453 sub->name, sub->url, displaypath);
1454 free(sub_key);
1457 cleanup:
1458 free(displaypath);
1459 free(sub_git_dir);
1460 strbuf_release(&sb_config);
1463 static void deinit_submodule_cb(const struct cache_entry *list_item,
1464 void *cb_data)
1466 struct deinit_cb *info = cb_data;
1467 deinit_submodule(list_item->name, info->prefix, info->flags);
1470 static int module_deinit(int argc, const char **argv, const char *prefix)
1472 struct deinit_cb info = DEINIT_CB_INIT;
1473 struct pathspec pathspec = { 0 };
1474 struct module_list list = MODULE_LIST_INIT;
1475 int quiet = 0;
1476 int force = 0;
1477 int all = 0;
1478 struct option module_deinit_options[] = {
1479 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1480 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1481 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1482 OPT_END()
1484 const char *const git_submodule_helper_usage[] = {
1485 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1486 NULL
1488 int ret = 1;
1490 argc = parse_options(argc, argv, prefix, module_deinit_options,
1491 git_submodule_helper_usage, 0);
1493 if (all && argc) {
1494 error("pathspec and --all are incompatible");
1495 usage_with_options(git_submodule_helper_usage,
1496 module_deinit_options);
1499 if (!argc && !all)
1500 die(_("Use '--all' if you really want to deinitialize all submodules"));
1502 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1503 goto cleanup;
1505 info.prefix = prefix;
1506 if (quiet)
1507 info.flags |= OPT_QUIET;
1508 if (force)
1509 info.flags |= OPT_FORCE;
1511 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1513 ret = 0;
1514 cleanup:
1515 module_list_release(&list);
1516 clear_pathspec(&pathspec);
1517 return ret;
1520 struct module_clone_data {
1521 const char *prefix;
1522 const char *path;
1523 const char *name;
1524 const char *url;
1525 const char *depth;
1526 struct list_objects_filter_options *filter_options;
1527 unsigned int quiet: 1;
1528 unsigned int progress: 1;
1529 unsigned int dissociate: 1;
1530 unsigned int require_init: 1;
1531 int single_branch;
1533 #define MODULE_CLONE_DATA_INIT { \
1534 .single_branch = -1, \
1537 struct submodule_alternate_setup {
1538 const char *submodule_name;
1539 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1540 SUBMODULE_ALTERNATE_ERROR_DIE,
1541 SUBMODULE_ALTERNATE_ERROR_INFO,
1542 SUBMODULE_ALTERNATE_ERROR_IGNORE
1543 } error_mode;
1544 struct string_list *reference;
1546 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1547 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1550 static const char alternate_error_advice[] = N_(
1551 "An alternate computed from a superproject's alternate is invalid.\n"
1552 "To allow Git to clone without an alternate in such a case, set\n"
1553 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1554 "'--reference-if-able' instead of '--reference'."
1557 static int add_possible_reference_from_superproject(
1558 struct object_directory *odb, void *sas_cb)
1560 struct submodule_alternate_setup *sas = sas_cb;
1561 size_t len;
1564 * If the alternate object store is another repository, try the
1565 * standard layout with .git/(modules/<name>)+/objects
1567 if (strip_suffix(odb->path, "/objects", &len)) {
1568 struct repository alternate;
1569 char *sm_alternate;
1570 struct strbuf sb = STRBUF_INIT;
1571 struct strbuf err = STRBUF_INIT;
1572 strbuf_add(&sb, odb->path, len);
1574 if (repo_init(&alternate, sb.buf, NULL) < 0)
1575 die(_("could not get a repository handle for gitdir '%s'"),
1576 sb.buf);
1579 * We need to end the new path with '/' to mark it as a dir,
1580 * otherwise a submodule name containing '/' will be broken
1581 * as the last part of a missing submodule reference would
1582 * be taken as a file name.
1584 strbuf_reset(&sb);
1585 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1586 strbuf_addch(&sb, '/');
1587 repo_clear(&alternate);
1589 sm_alternate = compute_alternate_path(sb.buf, &err);
1590 if (sm_alternate) {
1591 char *p = strbuf_detach(&sb, NULL);
1593 string_list_append(sas->reference, p)->util = p;
1594 free(sm_alternate);
1595 } else {
1596 switch (sas->error_mode) {
1597 case SUBMODULE_ALTERNATE_ERROR_DIE:
1598 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1599 advise(_(alternate_error_advice));
1600 die(_("submodule '%s' cannot add alternate: %s"),
1601 sas->submodule_name, err.buf);
1602 case SUBMODULE_ALTERNATE_ERROR_INFO:
1603 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1604 sas->submodule_name, err.buf);
1605 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1606 ; /* nothing */
1609 strbuf_release(&sb);
1612 return 0;
1615 static void prepare_possible_alternates(const char *sm_name,
1616 struct string_list *reference)
1618 char *sm_alternate = NULL, *error_strategy = NULL;
1619 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1621 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1622 if (!sm_alternate)
1623 return;
1625 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1627 if (!error_strategy)
1628 error_strategy = xstrdup("die");
1630 sas.submodule_name = sm_name;
1631 sas.reference = reference;
1632 if (!strcmp(error_strategy, "die"))
1633 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1634 else if (!strcmp(error_strategy, "info"))
1635 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1636 else if (!strcmp(error_strategy, "ignore"))
1637 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1638 else
1639 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1641 if (!strcmp(sm_alternate, "superproject"))
1642 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1643 else if (!strcmp(sm_alternate, "no"))
1644 ; /* do nothing */
1645 else
1646 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1648 free(sm_alternate);
1649 free(error_strategy);
1652 static char *clone_submodule_sm_gitdir(const char *name)
1654 struct strbuf sb = STRBUF_INIT;
1655 char *sm_gitdir;
1657 submodule_name_to_gitdir(&sb, the_repository, name);
1658 sm_gitdir = absolute_pathdup(sb.buf);
1659 strbuf_release(&sb);
1661 return sm_gitdir;
1664 static int clone_submodule(const struct module_clone_data *clone_data,
1665 struct string_list *reference)
1667 char *p;
1668 char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1669 char *sm_alternate = NULL, *error_strategy = NULL;
1670 struct child_process cp = CHILD_PROCESS_INIT;
1671 const char *clone_data_path = clone_data->path;
1672 char *to_free = NULL;
1674 if (!is_absolute_path(clone_data->path))
1675 clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
1676 clone_data->path);
1678 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1679 die(_("refusing to create/use '%s' in another submodule's "
1680 "git dir"), sm_gitdir);
1682 if (!file_exists(sm_gitdir)) {
1683 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1684 die(_("could not create directory '%s'"), sm_gitdir);
1686 prepare_possible_alternates(clone_data->name, reference);
1688 strvec_push(&cp.args, "clone");
1689 strvec_push(&cp.args, "--no-checkout");
1690 if (clone_data->quiet)
1691 strvec_push(&cp.args, "--quiet");
1692 if (clone_data->progress)
1693 strvec_push(&cp.args, "--progress");
1694 if (clone_data->depth && *(clone_data->depth))
1695 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1696 if (reference->nr) {
1697 struct string_list_item *item;
1699 for_each_string_list_item(item, reference)
1700 strvec_pushl(&cp.args, "--reference",
1701 item->string, NULL);
1703 if (clone_data->dissociate)
1704 strvec_push(&cp.args, "--dissociate");
1705 if (sm_gitdir && *sm_gitdir)
1706 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1707 if (clone_data->filter_options && clone_data->filter_options->choice)
1708 strvec_pushf(&cp.args, "--filter=%s",
1709 expand_list_objects_filter_spec(
1710 clone_data->filter_options));
1711 if (clone_data->single_branch >= 0)
1712 strvec_push(&cp.args, clone_data->single_branch ?
1713 "--single-branch" :
1714 "--no-single-branch");
1716 strvec_push(&cp.args, "--");
1717 strvec_push(&cp.args, clone_data->url);
1718 strvec_push(&cp.args, clone_data_path);
1720 cp.git_cmd = 1;
1721 prepare_submodule_repo_env(&cp.env);
1722 cp.no_stdin = 1;
1724 if(run_command(&cp))
1725 die(_("clone of '%s' into submodule path '%s' failed"),
1726 clone_data->url, clone_data_path);
1727 } else {
1728 char *path;
1730 if (clone_data->require_init && !access(clone_data_path, X_OK) &&
1731 !is_empty_dir(clone_data_path))
1732 die(_("directory not empty: '%s'"), clone_data_path);
1733 if (safe_create_leading_directories_const(clone_data_path) < 0)
1734 die(_("could not create directory '%s'"), clone_data_path);
1735 path = xstrfmt("%s/index", sm_gitdir);
1736 unlink_or_warn(path);
1737 free(path);
1740 connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1742 p = git_pathdup_submodule(clone_data_path, "config");
1743 if (!p)
1744 die(_("could not get submodule directory for '%s'"), clone_data_path);
1746 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1747 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1748 if (sm_alternate)
1749 git_config_set_in_file(p, "submodule.alternateLocation",
1750 sm_alternate);
1751 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1752 if (error_strategy)
1753 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1754 error_strategy);
1756 free(sm_alternate);
1757 free(error_strategy);
1759 free(sm_gitdir);
1760 free(p);
1761 free(to_free);
1762 return 0;
1765 static int module_clone(int argc, const char **argv, const char *prefix)
1767 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1768 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1769 struct string_list reference = STRING_LIST_INIT_NODUP;
1770 struct list_objects_filter_options filter_options =
1771 LIST_OBJECTS_FILTER_INIT;
1773 struct option module_clone_options[] = {
1774 OPT_STRING(0, "prefix", &clone_data.prefix,
1775 N_("path"),
1776 N_("alternative anchor for relative paths")),
1777 OPT_STRING(0, "path", &clone_data.path,
1778 N_("path"),
1779 N_("where the new submodule will be cloned to")),
1780 OPT_STRING(0, "name", &clone_data.name,
1781 N_("string"),
1782 N_("name of the new submodule")),
1783 OPT_STRING(0, "url", &clone_data.url,
1784 N_("string"),
1785 N_("url where to clone the submodule from")),
1786 OPT_STRING_LIST(0, "reference", &reference,
1787 N_("repo"),
1788 N_("reference repository")),
1789 OPT_BOOL(0, "dissociate", &dissociate,
1790 N_("use --reference only while cloning")),
1791 OPT_STRING(0, "depth", &clone_data.depth,
1792 N_("string"),
1793 N_("depth for shallow clones")),
1794 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1795 OPT_BOOL(0, "progress", &progress,
1796 N_("force cloning progress")),
1797 OPT_BOOL(0, "require-init", &require_init,
1798 N_("disallow cloning into non-empty directory")),
1799 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1800 N_("clone only one branch, HEAD or --branch")),
1801 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1802 OPT_END()
1804 const char *const git_submodule_helper_usage[] = {
1805 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1806 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1807 "[--single-branch] [--filter <filter-spec>] "
1808 "--url <url> --path <path>"),
1809 NULL
1812 argc = parse_options(argc, argv, prefix, module_clone_options,
1813 git_submodule_helper_usage, 0);
1815 clone_data.dissociate = !!dissociate;
1816 clone_data.quiet = !!quiet;
1817 clone_data.progress = !!progress;
1818 clone_data.require_init = !!require_init;
1819 clone_data.filter_options = &filter_options;
1821 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1822 usage_with_options(git_submodule_helper_usage,
1823 module_clone_options);
1825 clone_submodule(&clone_data, &reference);
1826 list_objects_filter_release(&filter_options);
1827 string_list_clear(&reference, 1);
1828 return 0;
1831 static int determine_submodule_update_strategy(struct repository *r,
1832 int just_cloned,
1833 const char *path,
1834 enum submodule_update_type update,
1835 struct submodule_update_strategy *out)
1837 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1838 char *key;
1839 const char *val;
1840 int ret;
1842 key = xstrfmt("submodule.%s.update", sub->name);
1844 if (update) {
1845 out->type = update;
1846 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1847 if (parse_submodule_update_strategy(val, out) < 0) {
1848 ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1849 val, path);
1850 goto cleanup;
1852 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1853 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1854 BUG("how did we read update = !command from .gitmodules?");
1855 out->type = sub->update_strategy.type;
1856 out->command = sub->update_strategy.command;
1857 } else
1858 out->type = SM_UPDATE_CHECKOUT;
1860 if (just_cloned &&
1861 (out->type == SM_UPDATE_MERGE ||
1862 out->type == SM_UPDATE_REBASE ||
1863 out->type == SM_UPDATE_NONE))
1864 out->type = SM_UPDATE_CHECKOUT;
1866 ret = 0;
1867 cleanup:
1868 free(key);
1869 return ret;
1872 struct update_clone_data {
1873 const struct submodule *sub;
1874 struct object_id oid;
1875 unsigned just_cloned;
1878 struct submodule_update_clone {
1879 /* index into 'update_data.list', the list of submodules to look into for cloning */
1880 int current;
1882 /* configuration parameters which are passed on to the children */
1883 const struct update_data *update_data;
1885 /* to be consumed by update_submodule() */
1886 struct update_clone_data *update_clone;
1887 int update_clone_nr; int update_clone_alloc;
1889 /* If we want to stop as fast as possible and return an error */
1890 unsigned quickstop : 1;
1892 /* failed clones to be retried again */
1893 const struct cache_entry **failed_clones;
1894 int failed_clones_nr, failed_clones_alloc;
1896 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1898 static void submodule_update_clone_release(struct submodule_update_clone *suc)
1900 free(suc->update_clone);
1901 free(suc->failed_clones);
1904 struct update_data {
1905 const char *prefix;
1906 const char *super_prefix;
1907 char *displaypath;
1908 enum submodule_update_type update_default;
1909 struct object_id suboid;
1910 struct string_list references;
1911 struct submodule_update_strategy update_strategy;
1912 struct list_objects_filter_options *filter_options;
1913 struct module_list list;
1914 int depth;
1915 int max_jobs;
1916 int single_branch;
1917 int recommend_shallow;
1918 unsigned int require_init;
1919 unsigned int force;
1920 unsigned int quiet;
1921 unsigned int nofetch;
1922 unsigned int remote;
1923 unsigned int progress;
1924 unsigned int dissociate;
1925 unsigned int init;
1926 unsigned int warn_if_uninitialized;
1927 unsigned int recursive;
1929 /* copied over from update_clone_data */
1930 struct object_id oid;
1931 unsigned int just_cloned;
1932 const char *sm_path;
1934 #define UPDATE_DATA_INIT { \
1935 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1936 .list = MODULE_LIST_INIT, \
1937 .recommend_shallow = -1, \
1938 .references = STRING_LIST_INIT_DUP, \
1939 .single_branch = -1, \
1940 .max_jobs = 1, \
1943 static void update_data_release(struct update_data *ud)
1945 free(ud->displaypath);
1946 module_list_release(&ud->list);
1949 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1950 struct strbuf *out, const char *displaypath)
1953 * Only mention uninitialized submodules when their
1954 * paths have been specified.
1956 if (suc->update_data->warn_if_uninitialized) {
1957 strbuf_addf(out,
1958 _("Submodule path '%s' not initialized"),
1959 displaypath);
1960 strbuf_addch(out, '\n');
1961 strbuf_addstr(out,
1962 _("Maybe you want to use 'update --init'?"));
1963 strbuf_addch(out, '\n');
1968 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1969 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1971 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1972 struct child_process *child,
1973 struct submodule_update_clone *suc,
1974 struct strbuf *out)
1976 const struct submodule *sub = NULL;
1977 const char *url = NULL;
1978 const char *update_string;
1979 enum submodule_update_type update_type;
1980 char *key;
1981 const struct update_data *ud = suc->update_data;
1982 char *displaypath = get_submodule_displaypath(ce->name, ud->prefix,
1983 ud->super_prefix);
1984 struct strbuf sb = STRBUF_INIT;
1985 int needs_cloning = 0;
1986 int need_free_url = 0;
1988 if (ce_stage(ce)) {
1989 strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
1990 strbuf_addch(out, '\n');
1991 goto cleanup;
1994 sub = submodule_from_path(the_repository, null_oid(), ce->name);
1996 if (!sub) {
1997 next_submodule_warn_missing(suc, out, displaypath);
1998 goto cleanup;
2001 key = xstrfmt("submodule.%s.update", sub->name);
2002 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
2003 update_type = parse_submodule_update_type(update_string);
2004 } else {
2005 update_type = sub->update_strategy.type;
2007 free(key);
2009 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
2010 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
2011 && update_type == SM_UPDATE_NONE)) {
2012 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
2013 strbuf_addch(out, '\n');
2014 goto cleanup;
2017 /* Check if the submodule has been initialized. */
2018 if (!is_submodule_active(the_repository, ce->name)) {
2019 next_submodule_warn_missing(suc, out, displaypath);
2020 goto cleanup;
2023 strbuf_reset(&sb);
2024 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2025 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2026 if (sub->url && (starts_with_dot_slash(sub->url) ||
2027 starts_with_dot_dot_slash(sub->url))) {
2028 url = resolve_relative_url(sub->url, NULL, 0);
2029 need_free_url = 1;
2030 } else
2031 url = sub->url;
2034 if (!url)
2035 die(_("cannot clone submodule '%s' without a URL"), sub->name);
2037 strbuf_reset(&sb);
2038 strbuf_addf(&sb, "%s/.git", ce->name);
2039 needs_cloning = !file_exists(sb.buf);
2041 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2042 suc->update_clone_alloc);
2043 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2044 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2045 suc->update_clone[suc->update_clone_nr].sub = sub;
2046 suc->update_clone_nr++;
2048 if (!needs_cloning)
2049 goto cleanup;
2051 child->git_cmd = 1;
2052 child->no_stdin = 1;
2053 child->stdout_to_stderr = 1;
2054 child->err = -1;
2055 strvec_push(&child->args, "submodule--helper");
2056 strvec_push(&child->args, "clone");
2057 if (suc->update_data->progress)
2058 strvec_push(&child->args, "--progress");
2059 if (suc->update_data->quiet)
2060 strvec_push(&child->args, "--quiet");
2061 if (suc->update_data->prefix)
2062 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2063 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2064 strvec_push(&child->args, "--depth=1");
2065 else if (suc->update_data->depth)
2066 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2067 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2068 strvec_pushf(&child->args, "--filter=%s",
2069 expand_list_objects_filter_spec(suc->update_data->filter_options));
2070 if (suc->update_data->require_init)
2071 strvec_push(&child->args, "--require-init");
2072 strvec_pushl(&child->args, "--path", sub->path, NULL);
2073 strvec_pushl(&child->args, "--name", sub->name, NULL);
2074 strvec_pushl(&child->args, "--url", url, NULL);
2075 if (suc->update_data->references.nr) {
2076 struct string_list_item *item;
2078 for_each_string_list_item(item, &suc->update_data->references)
2079 strvec_pushl(&child->args, "--reference", item->string, NULL);
2081 if (suc->update_data->dissociate)
2082 strvec_push(&child->args, "--dissociate");
2083 if (suc->update_data->single_branch >= 0)
2084 strvec_push(&child->args, suc->update_data->single_branch ?
2085 "--single-branch" :
2086 "--no-single-branch");
2088 cleanup:
2089 free(displaypath);
2090 strbuf_release(&sb);
2091 if (need_free_url)
2092 free((void*)url);
2094 return needs_cloning;
2097 static int update_clone_get_next_task(struct child_process *child,
2098 struct strbuf *err,
2099 void *suc_cb,
2100 void **idx_task_cb)
2102 struct submodule_update_clone *suc = suc_cb;
2103 const struct cache_entry *ce;
2104 int index;
2106 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2107 ce = suc->update_data->list.entries[suc->current];
2108 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2109 int *p = xmalloc(sizeof(*p));
2111 *p = suc->current;
2112 *idx_task_cb = p;
2113 suc->current++;
2114 return 1;
2119 * The loop above tried cloning each submodule once, now try the
2120 * stragglers again, which we can imagine as an extension of the
2121 * entry list.
2123 index = suc->current - suc->update_data->list.nr;
2124 if (index < suc->failed_clones_nr) {
2125 int *p;
2127 ce = suc->failed_clones[index];
2128 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2129 suc->current ++;
2130 strbuf_addstr(err, "BUG: submodule considered for "
2131 "cloning, doesn't need cloning "
2132 "any more?\n");
2133 return 0;
2135 p = xmalloc(sizeof(*p));
2136 *p = suc->current;
2137 *idx_task_cb = p;
2138 suc->current ++;
2139 return 1;
2142 return 0;
2145 static int update_clone_start_failure(struct strbuf *err UNUSED,
2146 void *suc_cb,
2147 void *idx_task_cb UNUSED)
2149 struct submodule_update_clone *suc = suc_cb;
2151 suc->quickstop = 1;
2152 return 1;
2155 static int update_clone_task_finished(int result,
2156 struct strbuf *err,
2157 void *suc_cb,
2158 void *idx_task_cb)
2160 const struct cache_entry *ce;
2161 struct submodule_update_clone *suc = suc_cb;
2162 int *idxP = idx_task_cb;
2163 int idx = *idxP;
2165 free(idxP);
2167 if (!result)
2168 return 0;
2170 if (idx < suc->update_data->list.nr) {
2171 ce = suc->update_data->list.entries[idx];
2172 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2173 ce->name);
2174 strbuf_addch(err, '\n');
2175 ALLOC_GROW(suc->failed_clones,
2176 suc->failed_clones_nr + 1,
2177 suc->failed_clones_alloc);
2178 suc->failed_clones[suc->failed_clones_nr++] = ce;
2179 return 0;
2180 } else {
2181 idx -= suc->update_data->list.nr;
2182 ce = suc->failed_clones[idx];
2183 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2184 ce->name);
2185 strbuf_addch(err, '\n');
2186 suc->quickstop = 1;
2187 return 1;
2190 return 0;
2193 static int git_update_clone_config(const char *var, const char *value,
2194 const struct config_context *ctx,
2195 void *cb)
2197 int *max_jobs = cb;
2199 if (!strcmp(var, "submodule.fetchjobs"))
2200 *max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
2201 return 0;
2204 static int is_tip_reachable(const char *path, const struct object_id *oid)
2206 struct child_process cp = CHILD_PROCESS_INIT;
2207 struct strbuf rev = STRBUF_INIT;
2208 char *hex = oid_to_hex(oid);
2210 cp.git_cmd = 1;
2211 cp.dir = path;
2212 cp.no_stderr = 1;
2213 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2215 prepare_submodule_repo_env(&cp.env);
2217 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2218 return 0;
2220 return 1;
2223 static int fetch_in_submodule(const char *module_path, int depth, int quiet,
2224 const struct object_id *oid)
2226 struct child_process cp = CHILD_PROCESS_INIT;
2228 prepare_submodule_repo_env(&cp.env);
2229 cp.git_cmd = 1;
2230 cp.dir = module_path;
2232 strvec_push(&cp.args, "fetch");
2233 if (quiet)
2234 strvec_push(&cp.args, "--quiet");
2235 if (depth)
2236 strvec_pushf(&cp.args, "--depth=%d", depth);
2237 if (oid) {
2238 char *hex = oid_to_hex(oid);
2239 char *remote = get_default_remote();
2241 strvec_pushl(&cp.args, remote, hex, NULL);
2242 free(remote);
2245 return run_command(&cp);
2248 static int run_update_command(const struct update_data *ud, int subforce)
2250 struct child_process cp = CHILD_PROCESS_INIT;
2251 char *oid = oid_to_hex(&ud->oid);
2252 int ret;
2254 switch (ud->update_strategy.type) {
2255 case SM_UPDATE_CHECKOUT:
2256 cp.git_cmd = 1;
2257 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2258 if (subforce)
2259 strvec_push(&cp.args, "-f");
2260 break;
2261 case SM_UPDATE_REBASE:
2262 cp.git_cmd = 1;
2263 strvec_push(&cp.args, "rebase");
2264 if (ud->quiet)
2265 strvec_push(&cp.args, "--quiet");
2266 break;
2267 case SM_UPDATE_MERGE:
2268 cp.git_cmd = 1;
2269 strvec_push(&cp.args, "merge");
2270 if (ud->quiet)
2271 strvec_push(&cp.args, "--quiet");
2272 break;
2273 case SM_UPDATE_COMMAND:
2274 cp.use_shell = 1;
2275 strvec_push(&cp.args, ud->update_strategy.command);
2276 break;
2277 default:
2278 BUG("unexpected update strategy type: %d",
2279 ud->update_strategy.type);
2281 strvec_push(&cp.args, oid);
2283 cp.dir = ud->sm_path;
2284 prepare_submodule_repo_env(&cp.env);
2285 if ((ret = run_command(&cp))) {
2286 switch (ud->update_strategy.type) {
2287 case SM_UPDATE_CHECKOUT:
2288 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2289 oid, ud->displaypath);
2290 /* No "ret" assignment, use "git checkout"'s */
2291 break;
2292 case SM_UPDATE_REBASE:
2293 ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2294 oid, ud->displaypath);
2295 break;
2296 case SM_UPDATE_MERGE:
2297 ret = die_message(_("Unable to merge '%s' in submodule path '%s'"),
2298 oid, ud->displaypath);
2299 break;
2300 case SM_UPDATE_COMMAND:
2301 ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2302 ud->update_strategy.command, oid, ud->displaypath);
2303 break;
2304 default:
2305 BUG("unexpected update strategy type: %d",
2306 ud->update_strategy.type);
2309 return ret;
2312 if (ud->quiet)
2313 return 0;
2315 switch (ud->update_strategy.type) {
2316 case SM_UPDATE_CHECKOUT:
2317 printf(_("Submodule path '%s': checked out '%s'\n"),
2318 ud->displaypath, oid);
2319 break;
2320 case SM_UPDATE_REBASE:
2321 printf(_("Submodule path '%s': rebased into '%s'\n"),
2322 ud->displaypath, oid);
2323 break;
2324 case SM_UPDATE_MERGE:
2325 printf(_("Submodule path '%s': merged in '%s'\n"),
2326 ud->displaypath, oid);
2327 break;
2328 case SM_UPDATE_COMMAND:
2329 printf(_("Submodule path '%s': '%s %s'\n"),
2330 ud->displaypath, ud->update_strategy.command, oid);
2331 break;
2332 default:
2333 BUG("unexpected update strategy type: %d",
2334 ud->update_strategy.type);
2337 return 0;
2340 static int run_update_procedure(const struct update_data *ud)
2342 int subforce = is_null_oid(&ud->suboid) || ud->force;
2344 if (!ud->nofetch) {
2346 * Run fetch only if `oid` isn't present or it
2347 * is not reachable from a ref.
2349 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2350 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2351 !ud->quiet)
2352 fprintf_ln(stderr,
2353 _("Unable to fetch in submodule path '%s'; "
2354 "trying to directly fetch %s:"),
2355 ud->displaypath, oid_to_hex(&ud->oid));
2357 * Now we tried the usual fetch, but `oid` may
2358 * not be reachable from any of the refs.
2360 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2361 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2362 return die_message(_("Fetched in submodule path '%s', but it did not "
2363 "contain %s. Direct fetching of that commit failed."),
2364 ud->displaypath, oid_to_hex(&ud->oid));
2367 return run_update_command(ud, subforce);
2370 static int remote_submodule_branch(const char *path, const char **branch)
2372 const struct submodule *sub;
2373 char *key;
2374 *branch = NULL;
2376 sub = submodule_from_path(the_repository, null_oid(), path);
2377 if (!sub)
2378 return die_message(_("could not initialize submodule at path '%s'"),
2379 path);
2381 key = xstrfmt("submodule.%s.branch", sub->name);
2382 if (repo_config_get_string_tmp(the_repository, key, branch))
2383 *branch = sub->branch;
2384 free(key);
2386 if (!*branch) {
2387 *branch = "HEAD";
2388 return 0;
2391 if (!strcmp(*branch, ".")) {
2392 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2394 if (!refname)
2395 return die_message(_("No such ref: %s"), "HEAD");
2397 /* detached HEAD */
2398 if (!strcmp(refname, "HEAD"))
2399 return die_message(_("Submodule (%s) branch configured to inherit "
2400 "branch from superproject, but the superproject "
2401 "is not on any branch"), sub->name);
2403 if (!skip_prefix(refname, "refs/heads/", &refname))
2404 return die_message(_("Expecting a full ref name, got %s"),
2405 refname);
2407 *branch = refname;
2408 return 0;
2411 /* Our "branch" is coming from repo_config_get_string_tmp() */
2412 return 0;
2415 static int ensure_core_worktree(const char *path)
2417 const char *cw;
2418 struct repository subrepo;
2420 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2421 return die_message(_("could not get a repository handle for submodule '%s'"),
2422 path);
2424 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2425 char *cfg_file, *abs_path;
2426 const char *rel_path;
2427 struct strbuf sb = STRBUF_INIT;
2429 cfg_file = repo_git_path(&subrepo, "config");
2431 abs_path = absolute_pathdup(path);
2432 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2434 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2436 free(cfg_file);
2437 free(abs_path);
2438 strbuf_release(&sb);
2441 repo_clear(&subrepo);
2442 return 0;
2445 static const char *submodule_update_type_to_label(enum submodule_update_type type)
2447 switch (type) {
2448 case SM_UPDATE_CHECKOUT:
2449 return "checkout";
2450 case SM_UPDATE_MERGE:
2451 return "merge";
2452 case SM_UPDATE_REBASE:
2453 return "rebase";
2454 case SM_UPDATE_UNSPECIFIED:
2455 case SM_UPDATE_NONE:
2456 case SM_UPDATE_COMMAND:
2457 break;
2459 BUG("unreachable with type %d", type);
2462 static void update_data_to_args(const struct update_data *update_data,
2463 struct strvec *args)
2465 enum submodule_update_type update_type = update_data->update_default;
2467 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2468 if (update_data->displaypath) {
2469 strvec_push(args, "--super-prefix");
2470 strvec_pushf(args, "%s/", update_data->displaypath);
2472 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2473 if (update_data->quiet)
2474 strvec_push(args, "--quiet");
2475 if (update_data->force)
2476 strvec_push(args, "--force");
2477 if (update_data->init)
2478 strvec_push(args, "--init");
2479 if (update_data->remote)
2480 strvec_push(args, "--remote");
2481 if (update_data->nofetch)
2482 strvec_push(args, "--no-fetch");
2483 if (update_data->dissociate)
2484 strvec_push(args, "--dissociate");
2485 if (update_data->progress)
2486 strvec_push(args, "--progress");
2487 if (update_data->require_init)
2488 strvec_push(args, "--require-init");
2489 if (update_data->depth)
2490 strvec_pushf(args, "--depth=%d", update_data->depth);
2491 if (update_type != SM_UPDATE_UNSPECIFIED)
2492 strvec_pushf(args, "--%s",
2493 submodule_update_type_to_label(update_type));
2495 if (update_data->references.nr) {
2496 struct string_list_item *item;
2498 for_each_string_list_item(item, &update_data->references)
2499 strvec_pushl(args, "--reference", item->string, NULL);
2501 if (update_data->filter_options && update_data->filter_options->choice)
2502 strvec_pushf(args, "--filter=%s",
2503 expand_list_objects_filter_spec(
2504 update_data->filter_options));
2505 if (update_data->recommend_shallow == 0)
2506 strvec_push(args, "--no-recommend-shallow");
2507 else if (update_data->recommend_shallow == 1)
2508 strvec_push(args, "--recommend-shallow");
2509 if (update_data->single_branch >= 0)
2510 strvec_push(args, update_data->single_branch ?
2511 "--single-branch" :
2512 "--no-single-branch");
2515 static int update_submodule(struct update_data *update_data)
2517 int ret;
2519 ret = determine_submodule_update_strategy(the_repository,
2520 update_data->just_cloned,
2521 update_data->sm_path,
2522 update_data->update_default,
2523 &update_data->update_strategy);
2524 if (ret)
2525 return ret;
2527 if (update_data->just_cloned)
2528 oidcpy(&update_data->suboid, null_oid());
2529 else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2530 return die_message(_("Unable to find current revision in submodule path '%s'"),
2531 update_data->displaypath);
2533 if (update_data->remote) {
2534 char *remote_name;
2535 const char *branch;
2536 char *remote_ref;
2537 int code;
2539 code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2540 if (code)
2541 return code;
2542 code = remote_submodule_branch(update_data->sm_path, &branch);
2543 if (code)
2544 return code;
2545 remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2547 free(remote_name);
2549 if (!update_data->nofetch) {
2550 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2551 0, NULL))
2552 return die_message(_("Unable to fetch in submodule path '%s'"),
2553 update_data->sm_path);
2556 if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2557 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2558 remote_ref, update_data->sm_path);
2560 free(remote_ref);
2563 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2564 ret = run_update_procedure(update_data);
2565 if (ret)
2566 return ret;
2569 if (update_data->recursive) {
2570 struct child_process cp = CHILD_PROCESS_INIT;
2571 struct update_data next = *update_data;
2573 next.prefix = NULL;
2574 oidcpy(&next.oid, null_oid());
2575 oidcpy(&next.suboid, null_oid());
2577 cp.dir = update_data->sm_path;
2578 cp.git_cmd = 1;
2579 prepare_submodule_repo_env(&cp.env);
2580 update_data_to_args(&next, &cp.args);
2582 ret = run_command(&cp);
2583 if (ret)
2584 die_message(_("Failed to recurse into submodule path '%s'"),
2585 update_data->displaypath);
2586 return ret;
2589 return 0;
2592 static int update_submodules(struct update_data *update_data)
2594 int i, ret = 0;
2595 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2596 const struct run_process_parallel_opts opts = {
2597 .tr2_category = "submodule",
2598 .tr2_label = "parallel/update",
2600 .processes = update_data->max_jobs,
2602 .get_next_task = update_clone_get_next_task,
2603 .start_failure = update_clone_start_failure,
2604 .task_finished = update_clone_task_finished,
2605 .data = &suc,
2608 suc.update_data = update_data;
2609 run_processes_parallel(&opts);
2612 * We saved the output and put it out all at once now.
2613 * That means:
2614 * - the listener does not have to interleave their (checkout)
2615 * work with our fetching. The writes involved in a
2616 * checkout involve more straightforward sequential I/O.
2617 * - the listener can avoid doing any work if fetching failed.
2619 if (suc.quickstop) {
2620 ret = 1;
2621 goto cleanup;
2624 for (i = 0; i < suc.update_clone_nr; i++) {
2625 struct update_clone_data ucd = suc.update_clone[i];
2626 int code;
2628 oidcpy(&update_data->oid, &ucd.oid);
2629 update_data->just_cloned = ucd.just_cloned;
2630 update_data->sm_path = ucd.sub->path;
2632 code = ensure_core_worktree(update_data->sm_path);
2633 if (code)
2634 goto fail;
2636 update_data->displaypath = get_submodule_displaypath(
2637 update_data->sm_path, update_data->prefix,
2638 update_data->super_prefix);
2639 code = update_submodule(update_data);
2640 FREE_AND_NULL(update_data->displaypath);
2641 fail:
2642 if (!code)
2643 continue;
2644 ret = code;
2645 if (ret == 128)
2646 goto cleanup;
2649 cleanup:
2650 submodule_update_clone_release(&suc);
2651 string_list_clear(&update_data->references, 0);
2652 return ret;
2655 static int module_update(int argc, const char **argv, const char *prefix)
2657 struct pathspec pathspec = { 0 };
2658 struct pathspec pathspec2 = { 0 };
2659 struct update_data opt = UPDATE_DATA_INIT;
2660 struct list_objects_filter_options filter_options =
2661 LIST_OBJECTS_FILTER_INIT;
2662 int ret;
2663 struct option module_update_options[] = {
2664 OPT__SUPER_PREFIX(&opt.super_prefix),
2665 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2666 OPT_BOOL(0, "init", &opt.init,
2667 N_("initialize uninitialized submodules before update")),
2668 OPT_BOOL(0, "remote", &opt.remote,
2669 N_("use SHA-1 of submodule's remote tracking branch")),
2670 OPT_BOOL(0, "recursive", &opt.recursive,
2671 N_("traverse submodules recursively")),
2672 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2673 N_("don't fetch new objects from the remote site")),
2674 OPT_SET_INT(0, "checkout", &opt.update_default,
2675 N_("use the 'checkout' update strategy (default)"),
2676 SM_UPDATE_CHECKOUT),
2677 OPT_SET_INT('m', "merge", &opt.update_default,
2678 N_("use the 'merge' update strategy"),
2679 SM_UPDATE_MERGE),
2680 OPT_SET_INT('r', "rebase", &opt.update_default,
2681 N_("use the 'rebase' update strategy"),
2682 SM_UPDATE_REBASE),
2683 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2684 N_("reference repository")),
2685 OPT_BOOL(0, "dissociate", &opt.dissociate,
2686 N_("use --reference only while cloning")),
2687 OPT_INTEGER(0, "depth", &opt.depth,
2688 N_("create a shallow clone truncated to the "
2689 "specified number of revisions")),
2690 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2691 N_("parallel jobs")),
2692 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2693 N_("whether the initial clone should follow the shallow recommendation")),
2694 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2695 OPT_BOOL(0, "progress", &opt.progress,
2696 N_("force cloning progress")),
2697 OPT_BOOL(0, "require-init", &opt.require_init,
2698 N_("disallow cloning into non-empty directory, implies --init")),
2699 OPT_BOOL(0, "single-branch", &opt.single_branch,
2700 N_("clone only one branch, HEAD or --branch")),
2701 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2702 OPT_END()
2704 const char *const git_submodule_helper_usage[] = {
2705 N_("git submodule [--quiet] update"
2706 " [--init [--filter=<filter-spec>]] [--remote]"
2707 " [-N|--no-fetch] [-f|--force]"
2708 " [--checkout|--merge|--rebase]"
2709 " [--[no-]recommend-shallow] [--reference <repository>]"
2710 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2711 NULL
2714 update_clone_config_from_gitmodules(&opt.max_jobs);
2715 git_config(git_update_clone_config, &opt.max_jobs);
2717 argc = parse_options(argc, argv, prefix, module_update_options,
2718 git_submodule_helper_usage, 0);
2720 if (opt.require_init)
2721 opt.init = 1;
2723 if (filter_options.choice && !opt.init) {
2724 usage_with_options(git_submodule_helper_usage,
2725 module_update_options);
2728 opt.filter_options = &filter_options;
2729 opt.prefix = prefix;
2731 if (opt.update_default)
2732 opt.update_strategy.type = opt.update_default;
2734 if (module_list_compute(argv, prefix, &pathspec, &opt.list) < 0) {
2735 ret = 1;
2736 goto cleanup;
2739 if (pathspec.nr)
2740 opt.warn_if_uninitialized = 1;
2742 if (opt.init) {
2743 struct module_list list = MODULE_LIST_INIT;
2744 struct init_cb info = INIT_CB_INIT;
2746 if (module_list_compute(argv, opt.prefix,
2747 &pathspec2, &list) < 0) {
2748 module_list_release(&list);
2749 ret = 1;
2750 goto cleanup;
2754 * If there are no path args and submodule.active is set then,
2755 * by default, only initialize 'active' modules.
2757 if (!argc && !git_config_get("submodule.active"))
2758 module_list_active(&list);
2760 info.prefix = opt.prefix;
2761 info.super_prefix = opt.super_prefix;
2762 if (opt.quiet)
2763 info.flags |= OPT_QUIET;
2765 for_each_listed_submodule(&list, init_submodule_cb, &info);
2766 module_list_release(&list);
2769 ret = update_submodules(&opt);
2770 cleanup:
2771 update_data_release(&opt);
2772 list_objects_filter_release(&filter_options);
2773 clear_pathspec(&pathspec);
2774 clear_pathspec(&pathspec2);
2775 return ret;
2778 static int push_check(int argc, const char **argv, const char *prefix UNUSED)
2780 struct remote *remote;
2781 const char *superproject_head;
2782 char *head;
2783 int detached_head = 0;
2784 struct object_id head_oid;
2786 if (argc < 3)
2787 die("submodule--helper push-check requires at least 2 arguments");
2790 * superproject's resolved head ref.
2791 * if HEAD then the superproject is in a detached head state, otherwise
2792 * it will be the resolved head ref.
2794 superproject_head = argv[1];
2795 argv++;
2796 argc--;
2797 /* Get the submodule's head ref and determine if it is detached */
2798 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2799 if (!head)
2800 die(_("Failed to resolve HEAD as a valid ref."));
2801 if (!strcmp(head, "HEAD"))
2802 detached_head = 1;
2805 * The remote must be configured.
2806 * This is to avoid pushing to the exact same URL as the parent.
2808 remote = pushremote_get(argv[1]);
2809 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2810 die("remote '%s' not configured", argv[1]);
2812 /* Check the refspec */
2813 if (argc > 2) {
2814 int i;
2815 struct ref *local_refs = get_local_heads();
2816 struct refspec refspec = REFSPEC_INIT_PUSH;
2818 refspec_appendn(&refspec, argv + 2, argc - 2);
2820 for (i = 0; i < refspec.nr; i++) {
2821 const struct refspec_item *rs = &refspec.items[i];
2823 if (rs->pattern || rs->matching)
2824 continue;
2826 /* LHS must match a single ref */
2827 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2828 case 1:
2829 break;
2830 case 0:
2832 * If LHS matches 'HEAD' then we need to ensure
2833 * that it matches the same named branch
2834 * checked out in the superproject.
2836 if (!strcmp(rs->src, "HEAD")) {
2837 if (!detached_head &&
2838 !strcmp(head, superproject_head))
2839 break;
2840 die("HEAD does not match the named branch in the superproject");
2842 /* fallthrough */
2843 default:
2844 die("src refspec '%s' must name a ref",
2845 rs->src);
2848 refspec_clear(&refspec);
2850 free(head);
2852 return 0;
2855 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2857 int i;
2858 struct pathspec pathspec = { 0 };
2859 struct module_list list = MODULE_LIST_INIT;
2860 const char *super_prefix = NULL;
2861 struct option embed_gitdir_options[] = {
2862 OPT__SUPER_PREFIX(&super_prefix),
2863 OPT_END()
2865 const char *const git_submodule_helper_usage[] = {
2866 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2867 NULL
2869 int ret = 1;
2871 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2872 git_submodule_helper_usage, 0);
2874 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
2875 goto cleanup;
2877 for (i = 0; i < list.nr; i++)
2878 absorb_git_dir_into_superproject(list.entries[i]->name,
2879 super_prefix);
2881 ret = 0;
2882 cleanup:
2883 clear_pathspec(&pathspec);
2884 module_list_release(&list);
2885 return ret;
2888 static int module_set_url(int argc, const char **argv, const char *prefix)
2890 int quiet = 0, ret;
2891 const char *newurl;
2892 const char *path;
2893 char *config_name;
2894 struct option options[] = {
2895 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2896 OPT_END()
2898 const char *const usage[] = {
2899 N_("git submodule set-url [--quiet] <path> <newurl>"),
2900 NULL
2902 const struct submodule *sub;
2904 argc = parse_options(argc, argv, prefix, options, usage, 0);
2906 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2907 usage_with_options(usage, options);
2909 sub = submodule_from_path(the_repository, null_oid(), path);
2911 if (!sub)
2912 die(_("no submodule mapping found in .gitmodules for path '%s'"),
2913 path);
2915 config_name = xstrfmt("submodule.%s.url", sub->name);
2916 ret = config_set_in_gitmodules_file_gently(config_name, newurl);
2918 if (!ret) {
2919 repo_read_gitmodules(the_repository, 0);
2920 sync_submodule(sub->path, prefix, NULL, quiet ? OPT_QUIET : 0);
2923 free(config_name);
2924 return !!ret;
2927 static int module_set_branch(int argc, const char **argv, const char *prefix)
2929 int opt_default = 0, ret;
2930 const char *opt_branch = NULL;
2931 const char *path;
2932 char *config_name;
2933 struct option options[] = {
2935 * We accept the `quiet` option for uniformity across subcommands,
2936 * though there is nothing to make less verbose in this subcommand.
2938 OPT_NOOP_NOARG('q', "quiet"),
2940 OPT_BOOL('d', "default", &opt_default,
2941 N_("set the default tracking branch to master")),
2942 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2943 N_("set the default tracking branch")),
2944 OPT_END()
2946 const char *const usage[] = {
2947 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2948 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2949 NULL
2951 const struct submodule *sub;
2953 argc = parse_options(argc, argv, prefix, options, usage, 0);
2955 if (!opt_branch && !opt_default)
2956 die(_("--branch or --default required"));
2958 if (opt_branch && opt_default)
2959 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2961 if (argc != 1 || !(path = argv[0]))
2962 usage_with_options(usage, options);
2964 sub = submodule_from_path(the_repository, null_oid(), path);
2966 if (!sub)
2967 die(_("no submodule mapping found in .gitmodules for path '%s'"),
2968 path);
2970 config_name = xstrfmt("submodule.%s.branch", sub->name);
2971 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2973 free(config_name);
2974 return !!ret;
2977 static int module_create_branch(int argc, const char **argv, const char *prefix)
2979 enum branch_track track;
2980 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
2981 struct option options[] = {
2982 OPT__QUIET(&quiet, N_("print only error messages")),
2983 OPT__FORCE(&force, N_("force creation"), 0),
2984 OPT_BOOL(0, "create-reflog", &reflog,
2985 N_("create the branch's reflog")),
2986 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
2987 N_("set branch tracking configuration"),
2988 PARSE_OPT_OPTARG,
2989 parse_opt_tracking_mode),
2990 OPT__DRY_RUN(&dry_run,
2991 N_("show whether the branch would be created")),
2992 OPT_END()
2994 const char *const usage[] = {
2995 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2996 NULL
2999 git_config(git_default_config, NULL);
3000 track = git_branch_track;
3001 argc = parse_options(argc, argv, prefix, options, usage, 0);
3003 if (argc != 3)
3004 usage_with_options(usage, options);
3006 if (!quiet && !dry_run)
3007 printf_ln(_("creating branch '%s'"), argv[0]);
3009 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
3010 force, reflog, quiet, track, dry_run);
3011 return 0;
3014 struct add_data {
3015 const char *prefix;
3016 const char *branch;
3017 const char *reference_path;
3018 char *sm_path;
3019 const char *sm_name;
3020 const char *repo;
3021 const char *realrepo;
3022 int depth;
3023 unsigned int force: 1;
3024 unsigned int quiet: 1;
3025 unsigned int progress: 1;
3026 unsigned int dissociate: 1;
3028 #define ADD_DATA_INIT { .depth = -1 }
3030 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3032 struct child_process cp_remote = CHILD_PROCESS_INIT;
3033 struct strbuf sb_remote_out = STRBUF_INIT;
3035 cp_remote.git_cmd = 1;
3036 strvec_pushf(&cp_remote.env,
3037 "GIT_DIR=%s", git_dir_path);
3038 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3039 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3040 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3041 char *next_line;
3042 char *line = sb_remote_out.buf;
3044 while ((next_line = strchr(line, '\n')) != NULL) {
3045 size_t len = next_line - line;
3047 if (strip_suffix_mem(line, &len, " (fetch)"))
3048 strbuf_addf(msg, " %.*s\n", (int)len, line);
3049 line = next_line + 1;
3053 strbuf_release(&sb_remote_out);
3056 static int add_submodule(const struct add_data *add_data)
3058 char *submod_gitdir_path;
3059 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3060 struct string_list reference = STRING_LIST_INIT_NODUP;
3061 int ret = -1;
3063 /* perhaps the path already exists and is already a git repo, else clone it */
3064 if (is_directory(add_data->sm_path)) {
3065 struct strbuf sm_path = STRBUF_INIT;
3066 strbuf_addstr(&sm_path, add_data->sm_path);
3067 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3068 if (is_nonbare_repository_dir(&sm_path))
3069 printf(_("Adding existing repo at '%s' to the index\n"),
3070 add_data->sm_path);
3071 else
3072 die(_("'%s' already exists and is not a valid git repo"),
3073 add_data->sm_path);
3074 strbuf_release(&sm_path);
3075 free(submod_gitdir_path);
3076 } else {
3077 struct child_process cp = CHILD_PROCESS_INIT;
3079 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3081 if (is_directory(submod_gitdir_path)) {
3082 if (!add_data->force) {
3083 struct strbuf msg = STRBUF_INIT;
3084 char *die_msg;
3086 strbuf_addf(&msg, _("A git directory for '%s' is found "
3087 "locally with remote(s):\n"),
3088 add_data->sm_name);
3090 append_fetch_remotes(&msg, submod_gitdir_path);
3091 free(submod_gitdir_path);
3093 strbuf_addf(&msg, _("If you want to reuse this local git "
3094 "directory instead of cloning again from\n"
3095 " %s\n"
3096 "use the '--force' option. If the local git "
3097 "directory is not the correct repo\n"
3098 "or you are unsure what this means choose "
3099 "another name with the '--name' option."),
3100 add_data->realrepo);
3102 die_msg = strbuf_detach(&msg, NULL);
3103 die("%s", die_msg);
3104 } else {
3105 printf(_("Reactivating local git directory for "
3106 "submodule '%s'\n"), add_data->sm_name);
3109 free(submod_gitdir_path);
3111 clone_data.prefix = add_data->prefix;
3112 clone_data.path = add_data->sm_path;
3113 clone_data.name = add_data->sm_name;
3114 clone_data.url = add_data->realrepo;
3115 clone_data.quiet = add_data->quiet;
3116 clone_data.progress = add_data->progress;
3117 if (add_data->reference_path) {
3118 char *p = xstrdup(add_data->reference_path);
3120 string_list_append(&reference, p)->util = p;
3122 clone_data.dissociate = add_data->dissociate;
3123 if (add_data->depth >= 0)
3124 clone_data.depth = xstrfmt("%d", add_data->depth);
3126 if (clone_submodule(&clone_data, &reference))
3127 goto cleanup;
3129 prepare_submodule_repo_env(&cp.env);
3130 cp.git_cmd = 1;
3131 cp.dir = add_data->sm_path;
3133 * NOTE: we only get here if add_data->force is true, so
3134 * passing --force to checkout is reasonable.
3136 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3138 if (add_data->branch) {
3139 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3140 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3143 if (run_command(&cp))
3144 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3146 ret = 0;
3147 cleanup:
3148 string_list_clear(&reference, 1);
3149 return ret;
3152 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3154 char *key;
3155 int ret;
3157 if (!is_writing_gitmodules_ok())
3158 die(_("please make sure that the .gitmodules file is in the working tree"));
3160 key = xstrfmt("submodule.%s.%s", name, var);
3161 ret = config_set_in_gitmodules_file_gently(key, value);
3162 free(key);
3164 return ret;
3167 static void configure_added_submodule(struct add_data *add_data)
3169 char *key;
3170 struct child_process add_submod = CHILD_PROCESS_INIT;
3171 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3173 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3174 git_config_set_gently(key, add_data->realrepo);
3175 free(key);
3177 add_submod.git_cmd = 1;
3178 strvec_pushl(&add_submod.args, "add",
3179 "--no-warn-embedded-repo", NULL);
3180 if (add_data->force)
3181 strvec_push(&add_submod.args, "--force");
3182 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3184 if (run_command(&add_submod))
3185 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3187 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3188 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3189 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3191 if (add_data->branch) {
3192 if (config_submodule_in_gitmodules(add_data->sm_name,
3193 "branch", add_data->branch))
3194 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3197 add_gitmodules.git_cmd = 1;
3198 strvec_pushl(&add_gitmodules.args,
3199 "add", "--force", "--", ".gitmodules", NULL);
3201 if (run_command(&add_gitmodules))
3202 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3205 * NEEDSWORK: In a multi-working-tree world this needs to be
3206 * set in the per-worktree config.
3209 * NEEDSWORK: In the longer run, we need to get rid of this
3210 * pattern of querying "submodule.active" before calling
3211 * is_submodule_active(), since that function needs to find
3212 * out the value of "submodule.active" again anyway.
3214 if (!git_config_get("submodule.active")) {
3216 * If the submodule being added isn't already covered by the
3217 * current configured pathspec, set the submodule's active flag
3219 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3220 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3221 git_config_set_gently(key, "true");
3222 free(key);
3224 } else {
3225 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3226 git_config_set_gently(key, "true");
3227 free(key);
3231 static void die_on_index_match(const char *path, int force)
3233 struct pathspec ps;
3234 const char *args[] = { path, NULL };
3235 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3237 if (repo_read_index_preload(the_repository, NULL, 0) < 0)
3238 die(_("index file corrupt"));
3240 if (ps.nr) {
3241 int i;
3242 char *ps_matched = xcalloc(ps.nr, 1);
3244 /* TODO: audit for interaction with sparse-index. */
3245 ensure_full_index(the_repository->index);
3248 * Since there is only one pathspec, we just need to
3249 * check ps_matched[0] to know if a cache entry matched.
3251 for (i = 0; i < the_repository->index->cache_nr; i++) {
3252 ce_path_match(the_repository->index, the_repository->index->cache[i], &ps,
3253 ps_matched);
3255 if (ps_matched[0]) {
3256 if (!force)
3257 die(_("'%s' already exists in the index"),
3258 path);
3259 if (!S_ISGITLINK(the_repository->index->cache[i]->ce_mode))
3260 die(_("'%s' already exists in the index "
3261 "and is not a submodule"), path);
3262 break;
3265 free(ps_matched);
3267 clear_pathspec(&ps);
3270 static void die_on_repo_without_commits(const char *path)
3272 struct strbuf sb = STRBUF_INIT;
3273 strbuf_addstr(&sb, path);
3274 if (is_nonbare_repository_dir(&sb)) {
3275 struct object_id oid;
3276 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3277 die(_("'%s' does not have a commit checked out"), path);
3279 strbuf_release(&sb);
3282 static int module_add(int argc, const char **argv, const char *prefix)
3284 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3285 struct add_data add_data = ADD_DATA_INIT;
3286 char *to_free = NULL;
3287 struct option options[] = {
3288 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3289 N_("branch of repository to add as submodule")),
3290 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3291 PARSE_OPT_NOCOMPLETE),
3292 OPT__QUIET(&quiet, N_("print only error messages")),
3293 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3294 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3295 N_("reference repository")),
3296 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3297 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3298 N_("sets the submodule's name to the given string "
3299 "instead of defaulting to its path")),
3300 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3301 OPT_END()
3303 const char *const usage[] = {
3304 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3305 NULL
3307 struct strbuf sb = STRBUF_INIT;
3308 int ret = 1;
3310 argc = parse_options(argc, argv, prefix, options, usage, 0);
3312 if (!is_writing_gitmodules_ok())
3313 die(_("please make sure that the .gitmodules file is in the working tree"));
3315 if (prefix && *prefix &&
3316 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3317 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3319 if (argc == 0 || argc > 2)
3320 usage_with_options(usage, options);
3322 add_data.repo = argv[0];
3323 if (argc == 1)
3324 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3325 else
3326 add_data.sm_path = xstrdup(argv[1]);
3328 if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
3329 char *sm_path = add_data.sm_path;
3331 add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
3332 free(sm_path);
3335 if (starts_with_dot_dot_slash(add_data.repo) ||
3336 starts_with_dot_slash(add_data.repo)) {
3337 if (prefix)
3338 die(_("Relative path can only be used from the toplevel "
3339 "of the working tree"));
3341 /* dereference source url relative to parent's url */
3342 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3343 add_data.realrepo = to_free;
3344 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3345 add_data.realrepo = add_data.repo;
3346 } else {
3347 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3348 add_data.repo);
3352 * normalize path:
3353 * multiple //; leading ./; /./; /../;
3355 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3356 strip_dir_trailing_slashes(add_data.sm_path);
3358 die_on_index_match(add_data.sm_path, force);
3359 die_on_repo_without_commits(add_data.sm_path);
3361 if (!force) {
3362 struct child_process cp = CHILD_PROCESS_INIT;
3364 cp.git_cmd = 1;
3365 cp.no_stdout = 1;
3366 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3367 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3368 if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3369 strbuf_complete_line(&sb);
3370 fputs(sb.buf, stderr);
3371 goto cleanup;
3375 if(!add_data.sm_name)
3376 add_data.sm_name = add_data.sm_path;
3378 if (check_submodule_name(add_data.sm_name))
3379 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3381 add_data.prefix = prefix;
3382 add_data.force = !!force;
3383 add_data.quiet = !!quiet;
3384 add_data.progress = !!progress;
3385 add_data.dissociate = !!dissociate;
3387 if (add_submodule(&add_data))
3388 goto cleanup;
3389 configure_added_submodule(&add_data);
3391 ret = 0;
3392 cleanup:
3393 free(add_data.sm_path);
3394 free(to_free);
3395 strbuf_release(&sb);
3397 return ret;
3400 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3402 parse_opt_subcommand_fn *fn = NULL;
3403 const char *const usage[] = {
3404 N_("git submodule--helper <command>"),
3405 NULL
3407 struct option options[] = {
3408 OPT_SUBCOMMAND("clone", &fn, module_clone),
3409 OPT_SUBCOMMAND("add", &fn, module_add),
3410 OPT_SUBCOMMAND("update", &fn, module_update),
3411 OPT_SUBCOMMAND("foreach", &fn, module_foreach),
3412 OPT_SUBCOMMAND("init", &fn, module_init),
3413 OPT_SUBCOMMAND("status", &fn, module_status),
3414 OPT_SUBCOMMAND("sync", &fn, module_sync),
3415 OPT_SUBCOMMAND("deinit", &fn, module_deinit),
3416 OPT_SUBCOMMAND("summary", &fn, module_summary),
3417 OPT_SUBCOMMAND("push-check", &fn, push_check),
3418 OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
3419 OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3420 OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3421 OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3422 OPT_END()
3424 argc = parse_options(argc, argv, prefix, options, usage, 0);
3426 return fn(argc, argv, prefix);