diff --no-index: fix -R with stdin
[git/debian.git] / builtin / submodule--helper.c
blob6a16208e8a8c34d60a0b1e64d1386d45acb4dea4
1 #define USE_THE_INDEX_VARIABLE
2 #include "builtin.h"
3 #include "abspath.h"
4 #include "alloc.h"
5 #include "environment.h"
6 #include "gettext.h"
7 #include "hex.h"
8 #include "repository.h"
9 #include "cache.h"
10 #include "config.h"
11 #include "parse-options.h"
12 #include "quote.h"
13 #include "pathspec.h"
14 #include "dir.h"
15 #include "setup.h"
16 #include "submodule.h"
17 #include "submodule-config.h"
18 #include "string-list.h"
19 #include "run-command.h"
20 #include "remote.h"
21 #include "refs.h"
22 #include "refspec.h"
23 #include "connect.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.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_index.cache_nr; i++) {
210 const struct cache_entry *ce = the_index.cache[i];
212 if (!match_pathspec(&the_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_index.cache_nr &&
220 !strcmp(ce->name, the_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 int diff_files_result;
631 struct strbuf buf = STRBUF_INIT;
632 const char *git_dir;
633 struct setup_revision_opt opt = {
634 .free_removed_argv_elements = 1,
637 if (!submodule_from_path(the_repository, null_oid(), path))
638 die(_("no submodule mapping found in .gitmodules for path '%s'"),
639 path);
641 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
643 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
644 print_status(flags, 'U', path, null_oid(), displaypath);
645 goto cleanup;
648 strbuf_addf(&buf, "%s/.git", path);
649 git_dir = read_gitfile(buf.buf);
650 if (!git_dir)
651 git_dir = buf.buf;
653 if (!is_submodule_active(the_repository, path) ||
654 !is_git_directory(git_dir)) {
655 print_status(flags, '-', path, ce_oid, displaypath);
656 strbuf_release(&buf);
657 goto cleanup;
659 strbuf_release(&buf);
661 strvec_pushl(&diff_files_args, "diff-files",
662 "--ignore-submodules=dirty", "--quiet", "--",
663 path, NULL);
665 git_config(git_diff_basic_config, NULL);
667 repo_init_revisions(the_repository, &rev, NULL);
668 rev.abbrev = 0;
669 setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt);
670 diff_files_result = run_diff_files(&rev, 0);
672 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
673 print_status(flags, ' ', path, ce_oid,
674 displaypath);
675 } else if (!(flags & OPT_CACHED)) {
676 struct object_id oid;
677 struct ref_store *refs = get_submodule_ref_store(path);
679 if (!refs) {
680 print_status(flags, '-', path, ce_oid, displaypath);
681 goto cleanup;
683 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
684 die(_("could not resolve HEAD ref inside the "
685 "submodule '%s'"), path);
687 print_status(flags, '+', path, &oid, displaypath);
688 } else {
689 print_status(flags, '+', path, ce_oid, displaypath);
692 if (flags & OPT_RECURSIVE) {
693 struct child_process cpr = CHILD_PROCESS_INIT;
695 cpr.git_cmd = 1;
696 cpr.dir = path;
697 prepare_submodule_repo_env(&cpr.env);
699 strvec_pushl(&cpr.args, "submodule--helper", "status",
700 "--recursive", NULL);
701 strvec_push(&cpr.args, "--super-prefix");
702 strvec_pushf(&cpr.args, "%s/", displaypath);
704 if (flags & OPT_CACHED)
705 strvec_push(&cpr.args, "--cached");
707 if (flags & OPT_QUIET)
708 strvec_push(&cpr.args, "--quiet");
710 if (run_command(&cpr))
711 die(_("failed to recurse into submodule '%s'"), path);
714 cleanup:
715 strvec_clear(&diff_files_args);
716 free(displaypath);
717 release_revisions(&rev);
720 static void status_submodule_cb(const struct cache_entry *list_item,
721 void *cb_data)
723 struct status_cb *info = cb_data;
725 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
726 info->prefix, info->super_prefix, info->flags);
729 static int module_status(int argc, const char **argv, const char *prefix)
731 struct status_cb info = STATUS_CB_INIT;
732 struct pathspec pathspec = { 0 };
733 struct module_list list = MODULE_LIST_INIT;
734 int quiet = 0;
735 struct option module_status_options[] = {
736 OPT__SUPER_PREFIX(&info.super_prefix),
737 OPT__QUIET(&quiet, N_("suppress submodule status output")),
738 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
739 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
740 OPT_END()
742 const char *const git_submodule_helper_usage[] = {
743 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
744 NULL
746 int ret = 1;
748 argc = parse_options(argc, argv, prefix, module_status_options,
749 git_submodule_helper_usage, 0);
751 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
752 goto cleanup;
754 info.prefix = prefix;
755 if (quiet)
756 info.flags |= OPT_QUIET;
758 for_each_listed_submodule(&list, status_submodule_cb, &info);
760 ret = 0;
761 cleanup:
762 module_list_release(&list);
763 clear_pathspec(&pathspec);
764 return ret;
767 struct module_cb {
768 unsigned int mod_src;
769 unsigned int mod_dst;
770 struct object_id oid_src;
771 struct object_id oid_dst;
772 char status;
773 char *sm_path;
775 #define MODULE_CB_INIT { 0 }
777 static void module_cb_release(struct module_cb *mcb)
779 free(mcb->sm_path);
782 struct module_cb_list {
783 struct module_cb **entries;
784 int alloc, nr;
786 #define MODULE_CB_LIST_INIT { 0 }
788 static void module_cb_list_release(struct module_cb_list *mcbl)
790 int i;
792 for (i = 0; i < mcbl->nr; i++) {
793 struct module_cb *mcb = mcbl->entries[i];
795 module_cb_release(mcb);
796 free(mcb);
798 free(mcbl->entries);
801 struct summary_cb {
802 int argc;
803 const char **argv;
804 const char *prefix;
805 const char *super_prefix;
806 unsigned int cached: 1;
807 unsigned int for_status: 1;
808 unsigned int files: 1;
809 int summary_limit;
811 #define SUMMARY_CB_INIT { 0 }
813 enum diff_cmd {
814 DIFF_INDEX,
815 DIFF_FILES
818 static char *verify_submodule_committish(const char *sm_path,
819 const char *committish)
821 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
822 struct strbuf result = STRBUF_INIT;
824 cp_rev_parse.git_cmd = 1;
825 cp_rev_parse.dir = sm_path;
826 prepare_submodule_repo_env(&cp_rev_parse.env);
827 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
828 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
829 strvec_push(&cp_rev_parse.args, "--");
831 if (capture_command(&cp_rev_parse, &result, 0))
832 return NULL;
834 strbuf_trim_trailing_newline(&result);
835 return strbuf_detach(&result, NULL);
838 static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
839 int total_commits, const char *displaypath,
840 const char *src_abbrev, const char *dst_abbrev,
841 struct module_cb *p)
843 if (p->status == 'T') {
844 if (S_ISGITLINK(p->mod_dst))
845 printf(_("* %s %s(blob)->%s(submodule)"),
846 displaypath, src_abbrev, dst_abbrev);
847 else
848 printf(_("* %s %s(submodule)->%s(blob)"),
849 displaypath, src_abbrev, dst_abbrev);
850 } else {
851 printf("* %s %s...%s",
852 displaypath, src_abbrev, dst_abbrev);
855 if (total_commits < 0)
856 printf(":\n");
857 else
858 printf(" (%d):\n", total_commits);
860 if (errmsg) {
861 printf(_("%s"), errmsg);
862 } else if (total_commits > 0) {
863 struct child_process cp_log = CHILD_PROCESS_INIT;
865 cp_log.git_cmd = 1;
866 cp_log.dir = p->sm_path;
867 prepare_submodule_repo_env(&cp_log.env);
868 strvec_pushl(&cp_log.args, "log", NULL);
870 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
871 if (info->summary_limit > 0)
872 strvec_pushf(&cp_log.args, "-%d",
873 info->summary_limit);
875 strvec_pushl(&cp_log.args, "--pretty= %m %s",
876 "--first-parent", NULL);
877 strvec_pushf(&cp_log.args, "%s...%s",
878 src_abbrev, dst_abbrev);
879 } else if (S_ISGITLINK(p->mod_dst)) {
880 strvec_pushl(&cp_log.args, "--pretty= > %s",
881 "-1", dst_abbrev, NULL);
882 } else {
883 strvec_pushl(&cp_log.args, "--pretty= < %s",
884 "-1", src_abbrev, NULL);
886 run_command(&cp_log);
888 printf("\n");
891 static void generate_submodule_summary(struct summary_cb *info,
892 struct module_cb *p)
894 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
895 int missing_src = 0, missing_dst = 0;
896 struct strbuf errmsg = STRBUF_INIT;
897 int total_commits = -1;
899 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
900 if (S_ISGITLINK(p->mod_dst)) {
901 struct ref_store *refs = get_submodule_ref_store(p->sm_path);
903 if (refs)
904 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
905 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
906 struct stat st;
907 int fd = open(p->sm_path, O_RDONLY);
909 if (fd < 0 || fstat(fd, &st) < 0 ||
910 index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
911 p->sm_path, 0))
912 error(_("couldn't hash object from '%s'"), p->sm_path);
913 } else {
914 /* for a submodule removal (mode:0000000), don't warn */
915 if (p->mod_dst)
916 warning(_("unexpected mode %o\n"), p->mod_dst);
920 if (S_ISGITLINK(p->mod_src)) {
921 if (p->status != 'D')
922 src_abbrev = verify_submodule_committish(p->sm_path,
923 oid_to_hex(&p->oid_src));
924 if (!src_abbrev) {
925 missing_src = 1;
927 * As `rev-parse` failed, we fallback to getting
928 * the abbreviated hash using oid_src. We do
929 * this as we might still need the abbreviated
930 * hash in cases like a submodule type change, etc.
932 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
934 } else {
936 * The source does not point to a submodule.
937 * So, we fallback to getting the abbreviation using
938 * oid_src as we might still need the abbreviated
939 * hash in cases like submodule add, etc.
941 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
944 if (S_ISGITLINK(p->mod_dst)) {
945 dst_abbrev = verify_submodule_committish(p->sm_path,
946 oid_to_hex(&p->oid_dst));
947 if (!dst_abbrev) {
948 missing_dst = 1;
950 * As `rev-parse` failed, we fallback to getting
951 * the abbreviated hash using oid_dst. We do
952 * this as we might still need the abbreviated
953 * hash in cases like a submodule type change, etc.
955 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
957 } else {
959 * The destination does not point to a submodule.
960 * So, we fallback to getting the abbreviation using
961 * oid_dst as we might still need the abbreviated
962 * hash in cases like a submodule removal, etc.
964 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
967 displaypath = get_submodule_displaypath(p->sm_path, info->prefix,
968 info->super_prefix);
970 if (!missing_src && !missing_dst) {
971 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
972 struct strbuf sb_rev_list = STRBUF_INIT;
974 strvec_pushl(&cp_rev_list.args, "rev-list",
975 "--first-parent", "--count", NULL);
976 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
977 strvec_pushf(&cp_rev_list.args, "%s...%s",
978 src_abbrev, dst_abbrev);
979 else
980 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
981 src_abbrev : dst_abbrev);
982 strvec_push(&cp_rev_list.args, "--");
984 cp_rev_list.git_cmd = 1;
985 cp_rev_list.dir = p->sm_path;
986 prepare_submodule_repo_env(&cp_rev_list.env);
988 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
989 total_commits = atoi(sb_rev_list.buf);
991 strbuf_release(&sb_rev_list);
992 } else {
994 * Don't give error msg for modification whose dst is not
995 * submodule, i.e., deleted or changed to blob
997 if (S_ISGITLINK(p->mod_dst)) {
998 if (missing_src && missing_dst) {
999 strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n",
1000 displaypath, oid_to_hex(&p->oid_src),
1001 oid_to_hex(&p->oid_dst));
1002 } else {
1003 strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n",
1004 displaypath, missing_src ?
1005 oid_to_hex(&p->oid_src) :
1006 oid_to_hex(&p->oid_dst));
1011 print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
1012 total_commits, displaypath, src_abbrev,
1013 dst_abbrev, p);
1015 free(displaypath);
1016 free(src_abbrev);
1017 free(dst_abbrev);
1018 strbuf_release(&errmsg);
1021 static void prepare_submodule_summary(struct summary_cb *info,
1022 struct module_cb_list *list)
1024 int i;
1025 for (i = 0; i < list->nr; i++) {
1026 const struct submodule *sub;
1027 struct module_cb *p = list->entries[i];
1028 struct strbuf sm_gitdir = STRBUF_INIT;
1030 if (p->status == 'D' || p->status == 'T') {
1031 generate_submodule_summary(info, p);
1032 continue;
1035 if (info->for_status && p->status != 'A' &&
1036 (sub = submodule_from_path(the_repository,
1037 null_oid(), p->sm_path))) {
1038 char *config_key = NULL;
1039 const char *value;
1040 int ignore_all = 0;
1042 config_key = xstrfmt("submodule.%s.ignore",
1043 sub->name);
1044 if (!git_config_get_string_tmp(config_key, &value))
1045 ignore_all = !strcmp(value, "all");
1046 else if (sub->ignore)
1047 ignore_all = !strcmp(sub->ignore, "all");
1049 free(config_key);
1050 if (ignore_all)
1051 continue;
1054 /* Also show added or modified modules which are checked out */
1055 strbuf_addstr(&sm_gitdir, p->sm_path);
1056 if (is_nonbare_repository_dir(&sm_gitdir))
1057 generate_submodule_summary(info, p);
1058 strbuf_release(&sm_gitdir);
1062 static void submodule_summary_callback(struct diff_queue_struct *q,
1063 struct diff_options *options UNUSED,
1064 void *data)
1066 int i;
1067 struct module_cb_list *list = data;
1068 for (i = 0; i < q->nr; i++) {
1069 struct diff_filepair *p = q->queue[i];
1070 struct module_cb *temp;
1072 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1073 continue;
1074 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1075 temp->mod_src = p->one->mode;
1076 temp->mod_dst = p->two->mode;
1077 temp->oid_src = p->one->oid;
1078 temp->oid_dst = p->two->oid;
1079 temp->status = p->status;
1080 temp->sm_path = xstrdup(p->one->path);
1082 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1083 list->entries[list->nr++] = temp;
1087 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1089 switch (diff_cmd) {
1090 case DIFF_INDEX: return "diff-index";
1091 case DIFF_FILES: return "diff-files";
1092 default: BUG("bad diff_cmd value %d", diff_cmd);
1096 static int compute_summary_module_list(struct object_id *head_oid,
1097 struct summary_cb *info,
1098 enum diff_cmd diff_cmd)
1100 struct strvec diff_args = STRVEC_INIT;
1101 struct rev_info rev;
1102 struct setup_revision_opt opt = {
1103 .free_removed_argv_elements = 1,
1105 struct module_cb_list list = MODULE_CB_LIST_INIT;
1106 int ret = 0;
1108 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1109 if (info->cached)
1110 strvec_push(&diff_args, "--cached");
1111 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1112 if (head_oid)
1113 strvec_push(&diff_args, oid_to_hex(head_oid));
1114 strvec_push(&diff_args, "--");
1115 if (info->argc)
1116 strvec_pushv(&diff_args, info->argv);
1118 git_config(git_diff_basic_config, NULL);
1119 repo_init_revisions(the_repository, &rev, info->prefix);
1120 rev.abbrev = 0;
1121 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1122 setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
1123 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1124 rev.diffopt.format_callback = submodule_summary_callback;
1125 rev.diffopt.format_callback_data = &list;
1127 if (!info->cached) {
1128 if (diff_cmd == DIFF_INDEX)
1129 setup_work_tree();
1130 if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
1131 perror("repo_read_index_preload");
1132 ret = -1;
1133 goto cleanup;
1135 } else if (repo_read_index(the_repository) < 0) {
1136 perror("repo_read_cache");
1137 ret = -1;
1138 goto cleanup;
1141 if (diff_cmd == DIFF_INDEX)
1142 run_diff_index(&rev, info->cached);
1143 else
1144 run_diff_files(&rev, 0);
1145 prepare_submodule_summary(info, &list);
1146 cleanup:
1147 strvec_clear(&diff_args);
1148 release_revisions(&rev);
1149 module_cb_list_release(&list);
1150 return ret;
1153 static int module_summary(int argc, const char **argv, const char *prefix)
1155 struct summary_cb info = SUMMARY_CB_INIT;
1156 int cached = 0;
1157 int for_status = 0;
1158 int files = 0;
1159 int summary_limit = -1;
1160 enum diff_cmd diff_cmd = DIFF_INDEX;
1161 struct object_id head_oid;
1162 int ret;
1163 struct option module_summary_options[] = {
1164 OPT_BOOL(0, "cached", &cached,
1165 N_("use the commit stored in the index instead of the submodule HEAD")),
1166 OPT_BOOL(0, "files", &files,
1167 N_("compare the commit in the index with that in the submodule HEAD")),
1168 OPT_BOOL(0, "for-status", &for_status,
1169 N_("skip submodules with 'ignore_config' value set to 'all'")),
1170 OPT_INTEGER('n', "summary-limit", &summary_limit,
1171 N_("limit the summary size")),
1172 OPT_END()
1174 const char *const git_submodule_helper_usage[] = {
1175 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1176 NULL
1179 argc = parse_options(argc, argv, prefix, module_summary_options,
1180 git_submodule_helper_usage, 0);
1182 if (!summary_limit)
1183 return 0;
1185 if (!repo_get_oid(the_repository, argc ? argv[0] : "HEAD", &head_oid)) {
1186 if (argc) {
1187 argv++;
1188 argc--;
1190 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1191 /* before the first commit: compare with an empty tree */
1192 oidcpy(&head_oid, the_hash_algo->empty_tree);
1193 if (argc) {
1194 argv++;
1195 argc--;
1197 } else {
1198 if (repo_get_oid(the_repository, "HEAD", &head_oid))
1199 die(_("could not fetch a revision for HEAD"));
1202 if (files) {
1203 if (cached)
1204 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1205 diff_cmd = DIFF_FILES;
1208 info.argc = argc;
1209 info.argv = argv;
1210 info.prefix = prefix;
1211 info.cached = !!cached;
1212 info.files = !!files;
1213 info.for_status = !!for_status;
1214 info.summary_limit = summary_limit;
1216 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1217 &info, diff_cmd);
1218 return ret;
1221 struct sync_cb {
1222 const char *prefix;
1223 const char *super_prefix;
1224 unsigned int flags;
1226 #define SYNC_CB_INIT { 0 }
1228 static void sync_submodule(const char *path, const char *prefix,
1229 const char *super_prefix, unsigned int flags)
1231 const struct submodule *sub;
1232 char *remote_key = NULL;
1233 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1234 struct strbuf sb = STRBUF_INIT;
1235 char *sub_config_path = NULL;
1236 int code;
1238 if (!is_submodule_active(the_repository, path))
1239 return;
1241 sub = submodule_from_path(the_repository, null_oid(), path);
1243 if (sub && sub->url) {
1244 if (starts_with_dot_dot_slash(sub->url) ||
1245 starts_with_dot_slash(sub->url)) {
1246 char *up_path = get_up_path(path);
1248 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1249 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1250 free(up_path);
1251 } else {
1252 sub_origin_url = xstrdup(sub->url);
1253 super_config_url = xstrdup(sub->url);
1255 } else {
1256 sub_origin_url = xstrdup("");
1257 super_config_url = xstrdup("");
1260 displaypath = get_submodule_displaypath(path, prefix, super_prefix);
1262 if (!(flags & OPT_QUIET))
1263 printf(_("Synchronizing submodule url for '%s'\n"),
1264 displaypath);
1266 strbuf_reset(&sb);
1267 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1268 if (git_config_set_gently(sb.buf, super_config_url))
1269 die(_("failed to register url for submodule path '%s'"),
1270 displaypath);
1272 if (!is_submodule_populated_gently(path, NULL))
1273 goto cleanup;
1275 strbuf_reset(&sb);
1276 code = get_default_remote_submodule(path, &default_remote);
1277 if (code)
1278 exit(code);
1280 remote_key = xstrfmt("remote.%s.url", default_remote);
1281 free(default_remote);
1283 submodule_to_gitdir(&sb, path);
1284 strbuf_addstr(&sb, "/config");
1286 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1287 die(_("failed to update remote for submodule '%s'"),
1288 path);
1290 if (flags & OPT_RECURSIVE) {
1291 struct child_process cpr = CHILD_PROCESS_INIT;
1293 cpr.git_cmd = 1;
1294 cpr.dir = path;
1295 prepare_submodule_repo_env(&cpr.env);
1297 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1298 "--recursive", NULL);
1299 strvec_push(&cpr.args, "--super-prefix");
1300 strvec_pushf(&cpr.args, "%s/", displaypath);
1303 if (flags & OPT_QUIET)
1304 strvec_push(&cpr.args, "--quiet");
1306 if (run_command(&cpr))
1307 die(_("failed to recurse into submodule '%s'"),
1308 path);
1311 cleanup:
1312 free(super_config_url);
1313 free(sub_origin_url);
1314 strbuf_release(&sb);
1315 free(remote_key);
1316 free(displaypath);
1317 free(sub_config_path);
1320 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1322 struct sync_cb *info = cb_data;
1324 sync_submodule(list_item->name, info->prefix, info->super_prefix,
1325 info->flags);
1328 static int module_sync(int argc, const char **argv, const char *prefix)
1330 struct sync_cb info = SYNC_CB_INIT;
1331 struct pathspec pathspec = { 0 };
1332 struct module_list list = MODULE_LIST_INIT;
1333 int quiet = 0;
1334 int recursive = 0;
1335 struct option module_sync_options[] = {
1336 OPT__SUPER_PREFIX(&info.super_prefix),
1337 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1338 OPT_BOOL(0, "recursive", &recursive,
1339 N_("recurse into nested submodules")),
1340 OPT_END()
1342 const char *const git_submodule_helper_usage[] = {
1343 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1344 NULL
1346 int ret = 1;
1348 argc = parse_options(argc, argv, prefix, module_sync_options,
1349 git_submodule_helper_usage, 0);
1351 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1352 goto cleanup;
1354 info.prefix = prefix;
1355 if (quiet)
1356 info.flags |= OPT_QUIET;
1357 if (recursive)
1358 info.flags |= OPT_RECURSIVE;
1360 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1362 ret = 0;
1363 cleanup:
1364 module_list_release(&list);
1365 clear_pathspec(&pathspec);
1366 return ret;
1369 struct deinit_cb {
1370 const char *prefix;
1371 unsigned int flags;
1373 #define DEINIT_CB_INIT { 0 }
1375 static void deinit_submodule(const char *path, const char *prefix,
1376 unsigned int flags)
1378 const struct submodule *sub;
1379 char *displaypath = NULL;
1380 struct child_process cp_config = CHILD_PROCESS_INIT;
1381 struct strbuf sb_config = STRBUF_INIT;
1382 char *sub_git_dir = xstrfmt("%s/.git", path);
1384 sub = submodule_from_path(the_repository, null_oid(), path);
1386 if (!sub || !sub->name)
1387 goto cleanup;
1389 displaypath = get_submodule_displaypath(path, prefix, NULL);
1391 /* remove the submodule work tree (unless the user already did it) */
1392 if (is_directory(path)) {
1393 struct strbuf sb_rm = STRBUF_INIT;
1394 const char *format;
1396 if (is_directory(sub_git_dir)) {
1397 if (!(flags & OPT_QUIET))
1398 warning(_("Submodule work tree '%s' contains a .git "
1399 "directory. This will be replaced with a "
1400 ".git file by using absorbgitdirs."),
1401 displaypath);
1403 absorb_git_dir_into_superproject(path, NULL);
1407 if (!(flags & OPT_FORCE)) {
1408 struct child_process cp_rm = CHILD_PROCESS_INIT;
1410 cp_rm.git_cmd = 1;
1411 strvec_pushl(&cp_rm.args, "rm", "-qn",
1412 path, NULL);
1414 if (run_command(&cp_rm))
1415 die(_("Submodule work tree '%s' contains local "
1416 "modifications; use '-f' to discard them"),
1417 displaypath);
1420 strbuf_addstr(&sb_rm, path);
1422 if (!remove_dir_recursively(&sb_rm, 0))
1423 format = _("Cleared directory '%s'\n");
1424 else
1425 format = _("Could not remove submodule work tree '%s'\n");
1427 if (!(flags & OPT_QUIET))
1428 printf(format, displaypath);
1430 submodule_unset_core_worktree(sub);
1432 strbuf_release(&sb_rm);
1435 if (mkdir(path, 0777))
1436 printf(_("could not create empty submodule directory %s"),
1437 displaypath);
1439 cp_config.git_cmd = 1;
1440 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1441 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1443 /* remove the .git/config entries (unless the user already did it) */
1444 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1445 char *sub_key = xstrfmt("submodule.%s", sub->name);
1448 * remove the whole section so we have a clean state when
1449 * the user later decides to init this submodule again
1451 git_config_rename_section_in_file(NULL, sub_key, NULL);
1452 if (!(flags & OPT_QUIET))
1453 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1454 sub->name, sub->url, displaypath);
1455 free(sub_key);
1458 cleanup:
1459 free(displaypath);
1460 free(sub_git_dir);
1461 strbuf_release(&sb_config);
1464 static void deinit_submodule_cb(const struct cache_entry *list_item,
1465 void *cb_data)
1467 struct deinit_cb *info = cb_data;
1468 deinit_submodule(list_item->name, info->prefix, info->flags);
1471 static int module_deinit(int argc, const char **argv, const char *prefix)
1473 struct deinit_cb info = DEINIT_CB_INIT;
1474 struct pathspec pathspec = { 0 };
1475 struct module_list list = MODULE_LIST_INIT;
1476 int quiet = 0;
1477 int force = 0;
1478 int all = 0;
1479 struct option module_deinit_options[] = {
1480 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1481 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1482 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1483 OPT_END()
1485 const char *const git_submodule_helper_usage[] = {
1486 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1487 NULL
1489 int ret = 1;
1491 argc = parse_options(argc, argv, prefix, module_deinit_options,
1492 git_submodule_helper_usage, 0);
1494 if (all && argc) {
1495 error("pathspec and --all are incompatible");
1496 usage_with_options(git_submodule_helper_usage,
1497 module_deinit_options);
1500 if (!argc && !all)
1501 die(_("Use '--all' if you really want to deinitialize all submodules"));
1503 if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1504 goto cleanup;
1506 info.prefix = prefix;
1507 if (quiet)
1508 info.flags |= OPT_QUIET;
1509 if (force)
1510 info.flags |= OPT_FORCE;
1512 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1514 ret = 0;
1515 cleanup:
1516 module_list_release(&list);
1517 clear_pathspec(&pathspec);
1518 return ret;
1521 struct module_clone_data {
1522 const char *prefix;
1523 const char *path;
1524 const char *name;
1525 const char *url;
1526 const char *depth;
1527 struct list_objects_filter_options *filter_options;
1528 unsigned int quiet: 1;
1529 unsigned int progress: 1;
1530 unsigned int dissociate: 1;
1531 unsigned int require_init: 1;
1532 int single_branch;
1534 #define MODULE_CLONE_DATA_INIT { \
1535 .single_branch = -1, \
1538 struct submodule_alternate_setup {
1539 const char *submodule_name;
1540 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1541 SUBMODULE_ALTERNATE_ERROR_DIE,
1542 SUBMODULE_ALTERNATE_ERROR_INFO,
1543 SUBMODULE_ALTERNATE_ERROR_IGNORE
1544 } error_mode;
1545 struct string_list *reference;
1547 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1548 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1551 static const char alternate_error_advice[] = N_(
1552 "An alternate computed from a superproject's alternate is invalid.\n"
1553 "To allow Git to clone without an alternate in such a case, set\n"
1554 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1555 "'--reference-if-able' instead of '--reference'."
1558 static int add_possible_reference_from_superproject(
1559 struct object_directory *odb, void *sas_cb)
1561 struct submodule_alternate_setup *sas = sas_cb;
1562 size_t len;
1565 * If the alternate object store is another repository, try the
1566 * standard layout with .git/(modules/<name>)+/objects
1568 if (strip_suffix(odb->path, "/objects", &len)) {
1569 struct repository alternate;
1570 char *sm_alternate;
1571 struct strbuf sb = STRBUF_INIT;
1572 struct strbuf err = STRBUF_INIT;
1573 strbuf_add(&sb, odb->path, len);
1575 if (repo_init(&alternate, sb.buf, NULL) < 0)
1576 die(_("could not get a repository handle for gitdir '%s'"),
1577 sb.buf);
1580 * We need to end the new path with '/' to mark it as a dir,
1581 * otherwise a submodule name containing '/' will be broken
1582 * as the last part of a missing submodule reference would
1583 * be taken as a file name.
1585 strbuf_reset(&sb);
1586 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1587 strbuf_addch(&sb, '/');
1588 repo_clear(&alternate);
1590 sm_alternate = compute_alternate_path(sb.buf, &err);
1591 if (sm_alternate) {
1592 char *p = strbuf_detach(&sb, NULL);
1594 string_list_append(sas->reference, p)->util = p;
1595 free(sm_alternate);
1596 } else {
1597 switch (sas->error_mode) {
1598 case SUBMODULE_ALTERNATE_ERROR_DIE:
1599 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1600 advise(_(alternate_error_advice));
1601 die(_("submodule '%s' cannot add alternate: %s"),
1602 sas->submodule_name, err.buf);
1603 case SUBMODULE_ALTERNATE_ERROR_INFO:
1604 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1605 sas->submodule_name, err.buf);
1606 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1607 ; /* nothing */
1610 strbuf_release(&sb);
1613 return 0;
1616 static void prepare_possible_alternates(const char *sm_name,
1617 struct string_list *reference)
1619 char *sm_alternate = NULL, *error_strategy = NULL;
1620 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1622 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1623 if (!sm_alternate)
1624 return;
1626 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1628 if (!error_strategy)
1629 error_strategy = xstrdup("die");
1631 sas.submodule_name = sm_name;
1632 sas.reference = reference;
1633 if (!strcmp(error_strategy, "die"))
1634 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1635 else if (!strcmp(error_strategy, "info"))
1636 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1637 else if (!strcmp(error_strategy, "ignore"))
1638 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1639 else
1640 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1642 if (!strcmp(sm_alternate, "superproject"))
1643 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1644 else if (!strcmp(sm_alternate, "no"))
1645 ; /* do nothing */
1646 else
1647 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1649 free(sm_alternate);
1650 free(error_strategy);
1653 static char *clone_submodule_sm_gitdir(const char *name)
1655 struct strbuf sb = STRBUF_INIT;
1656 char *sm_gitdir;
1658 submodule_name_to_gitdir(&sb, the_repository, name);
1659 sm_gitdir = absolute_pathdup(sb.buf);
1660 strbuf_release(&sb);
1662 return sm_gitdir;
1665 static int clone_submodule(const struct module_clone_data *clone_data,
1666 struct string_list *reference)
1668 char *p;
1669 char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1670 char *sm_alternate = NULL, *error_strategy = NULL;
1671 struct child_process cp = CHILD_PROCESS_INIT;
1672 const char *clone_data_path = clone_data->path;
1673 char *to_free = NULL;
1675 if (!is_absolute_path(clone_data->path))
1676 clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
1677 clone_data->path);
1679 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1680 die(_("refusing to create/use '%s' in another submodule's "
1681 "git dir"), sm_gitdir);
1683 if (!file_exists(sm_gitdir)) {
1684 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1685 die(_("could not create directory '%s'"), sm_gitdir);
1687 prepare_possible_alternates(clone_data->name, reference);
1689 strvec_push(&cp.args, "clone");
1690 strvec_push(&cp.args, "--no-checkout");
1691 if (clone_data->quiet)
1692 strvec_push(&cp.args, "--quiet");
1693 if (clone_data->progress)
1694 strvec_push(&cp.args, "--progress");
1695 if (clone_data->depth && *(clone_data->depth))
1696 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1697 if (reference->nr) {
1698 struct string_list_item *item;
1700 for_each_string_list_item(item, reference)
1701 strvec_pushl(&cp.args, "--reference",
1702 item->string, NULL);
1704 if (clone_data->dissociate)
1705 strvec_push(&cp.args, "--dissociate");
1706 if (sm_gitdir && *sm_gitdir)
1707 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1708 if (clone_data->filter_options && clone_data->filter_options->choice)
1709 strvec_pushf(&cp.args, "--filter=%s",
1710 expand_list_objects_filter_spec(
1711 clone_data->filter_options));
1712 if (clone_data->single_branch >= 0)
1713 strvec_push(&cp.args, clone_data->single_branch ?
1714 "--single-branch" :
1715 "--no-single-branch");
1717 strvec_push(&cp.args, "--");
1718 strvec_push(&cp.args, clone_data->url);
1719 strvec_push(&cp.args, clone_data_path);
1721 cp.git_cmd = 1;
1722 prepare_submodule_repo_env(&cp.env);
1723 cp.no_stdin = 1;
1725 if(run_command(&cp))
1726 die(_("clone of '%s' into submodule path '%s' failed"),
1727 clone_data->url, clone_data_path);
1728 } else {
1729 char *path;
1731 if (clone_data->require_init && !access(clone_data_path, X_OK) &&
1732 !is_empty_dir(clone_data_path))
1733 die(_("directory not empty: '%s'"), clone_data_path);
1734 if (safe_create_leading_directories_const(clone_data_path) < 0)
1735 die(_("could not create directory '%s'"), clone_data_path);
1736 path = xstrfmt("%s/index", sm_gitdir);
1737 unlink_or_warn(path);
1738 free(path);
1741 connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1743 p = git_pathdup_submodule(clone_data_path, "config");
1744 if (!p)
1745 die(_("could not get submodule directory for '%s'"), clone_data_path);
1747 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1748 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1749 if (sm_alternate)
1750 git_config_set_in_file(p, "submodule.alternateLocation",
1751 sm_alternate);
1752 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1753 if (error_strategy)
1754 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1755 error_strategy);
1757 free(sm_alternate);
1758 free(error_strategy);
1760 free(sm_gitdir);
1761 free(p);
1762 free(to_free);
1763 return 0;
1766 static int module_clone(int argc, const char **argv, const char *prefix)
1768 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1769 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1770 struct string_list reference = STRING_LIST_INIT_NODUP;
1771 struct list_objects_filter_options filter_options =
1772 LIST_OBJECTS_FILTER_INIT;
1774 struct option module_clone_options[] = {
1775 OPT_STRING(0, "prefix", &clone_data.prefix,
1776 N_("path"),
1777 N_("alternative anchor for relative paths")),
1778 OPT_STRING(0, "path", &clone_data.path,
1779 N_("path"),
1780 N_("where the new submodule will be cloned to")),
1781 OPT_STRING(0, "name", &clone_data.name,
1782 N_("string"),
1783 N_("name of the new submodule")),
1784 OPT_STRING(0, "url", &clone_data.url,
1785 N_("string"),
1786 N_("url where to clone the submodule from")),
1787 OPT_STRING_LIST(0, "reference", &reference,
1788 N_("repo"),
1789 N_("reference repository")),
1790 OPT_BOOL(0, "dissociate", &dissociate,
1791 N_("use --reference only while cloning")),
1792 OPT_STRING(0, "depth", &clone_data.depth,
1793 N_("string"),
1794 N_("depth for shallow clones")),
1795 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1796 OPT_BOOL(0, "progress", &progress,
1797 N_("force cloning progress")),
1798 OPT_BOOL(0, "require-init", &require_init,
1799 N_("disallow cloning into non-empty directory")),
1800 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1801 N_("clone only one branch, HEAD or --branch")),
1802 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1803 OPT_END()
1805 const char *const git_submodule_helper_usage[] = {
1806 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1807 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1808 "[--single-branch] [--filter <filter-spec>] "
1809 "--url <url> --path <path>"),
1810 NULL
1813 argc = parse_options(argc, argv, prefix, module_clone_options,
1814 git_submodule_helper_usage, 0);
1816 clone_data.dissociate = !!dissociate;
1817 clone_data.quiet = !!quiet;
1818 clone_data.progress = !!progress;
1819 clone_data.require_init = !!require_init;
1820 clone_data.filter_options = &filter_options;
1822 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1823 usage_with_options(git_submodule_helper_usage,
1824 module_clone_options);
1826 clone_submodule(&clone_data, &reference);
1827 list_objects_filter_release(&filter_options);
1828 string_list_clear(&reference, 1);
1829 return 0;
1832 static int determine_submodule_update_strategy(struct repository *r,
1833 int just_cloned,
1834 const char *path,
1835 enum submodule_update_type update,
1836 struct submodule_update_strategy *out)
1838 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1839 char *key;
1840 const char *val;
1841 int ret;
1843 key = xstrfmt("submodule.%s.update", sub->name);
1845 if (update) {
1846 out->type = update;
1847 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1848 if (parse_submodule_update_strategy(val, out) < 0) {
1849 ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1850 val, path);
1851 goto cleanup;
1853 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1854 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1855 BUG("how did we read update = !command from .gitmodules?");
1856 out->type = sub->update_strategy.type;
1857 out->command = sub->update_strategy.command;
1858 } else
1859 out->type = SM_UPDATE_CHECKOUT;
1861 if (just_cloned &&
1862 (out->type == SM_UPDATE_MERGE ||
1863 out->type == SM_UPDATE_REBASE ||
1864 out->type == SM_UPDATE_NONE))
1865 out->type = SM_UPDATE_CHECKOUT;
1867 ret = 0;
1868 cleanup:
1869 free(key);
1870 return ret;
1873 struct update_clone_data {
1874 const struct submodule *sub;
1875 struct object_id oid;
1876 unsigned just_cloned;
1879 struct submodule_update_clone {
1880 /* index into 'update_data.list', the list of submodules to look into for cloning */
1881 int current;
1883 /* configuration parameters which are passed on to the children */
1884 const struct update_data *update_data;
1886 /* to be consumed by update_submodule() */
1887 struct update_clone_data *update_clone;
1888 int update_clone_nr; int update_clone_alloc;
1890 /* If we want to stop as fast as possible and return an error */
1891 unsigned quickstop : 1;
1893 /* failed clones to be retried again */
1894 const struct cache_entry **failed_clones;
1895 int failed_clones_nr, failed_clones_alloc;
1897 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1899 static void submodule_update_clone_release(struct submodule_update_clone *suc)
1901 free(suc->update_clone);
1902 free(suc->failed_clones);
1905 struct update_data {
1906 const char *prefix;
1907 const char *super_prefix;
1908 char *displaypath;
1909 enum submodule_update_type update_default;
1910 struct object_id suboid;
1911 struct string_list references;
1912 struct submodule_update_strategy update_strategy;
1913 struct list_objects_filter_options *filter_options;
1914 struct module_list list;
1915 int depth;
1916 int max_jobs;
1917 int single_branch;
1918 int recommend_shallow;
1919 unsigned int require_init;
1920 unsigned int force;
1921 unsigned int quiet;
1922 unsigned int nofetch;
1923 unsigned int remote;
1924 unsigned int progress;
1925 unsigned int dissociate;
1926 unsigned int init;
1927 unsigned int warn_if_uninitialized;
1928 unsigned int recursive;
1930 /* copied over from update_clone_data */
1931 struct object_id oid;
1932 unsigned int just_cloned;
1933 const char *sm_path;
1935 #define UPDATE_DATA_INIT { \
1936 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1937 .list = MODULE_LIST_INIT, \
1938 .recommend_shallow = -1, \
1939 .references = STRING_LIST_INIT_DUP, \
1940 .single_branch = -1, \
1941 .max_jobs = 1, \
1944 static void update_data_release(struct update_data *ud)
1946 free(ud->displaypath);
1947 module_list_release(&ud->list);
1950 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1951 struct strbuf *out, const char *displaypath)
1954 * Only mention uninitialized submodules when their
1955 * paths have been specified.
1957 if (suc->update_data->warn_if_uninitialized) {
1958 strbuf_addf(out,
1959 _("Submodule path '%s' not initialized"),
1960 displaypath);
1961 strbuf_addch(out, '\n');
1962 strbuf_addstr(out,
1963 _("Maybe you want to use 'update --init'?"));
1964 strbuf_addch(out, '\n');
1969 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1970 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1972 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1973 struct child_process *child,
1974 struct submodule_update_clone *suc,
1975 struct strbuf *out)
1977 const struct submodule *sub = NULL;
1978 const char *url = NULL;
1979 const char *update_string;
1980 enum submodule_update_type update_type;
1981 char *key;
1982 const struct update_data *ud = suc->update_data;
1983 char *displaypath = get_submodule_displaypath(ce->name, ud->prefix,
1984 ud->super_prefix);
1985 struct strbuf sb = STRBUF_INIT;
1986 int needs_cloning = 0;
1987 int need_free_url = 0;
1989 if (ce_stage(ce)) {
1990 strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
1991 strbuf_addch(out, '\n');
1992 goto cleanup;
1995 sub = submodule_from_path(the_repository, null_oid(), ce->name);
1997 if (!sub) {
1998 next_submodule_warn_missing(suc, out, displaypath);
1999 goto cleanup;
2002 key = xstrfmt("submodule.%s.update", sub->name);
2003 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
2004 update_type = parse_submodule_update_type(update_string);
2005 } else {
2006 update_type = sub->update_strategy.type;
2008 free(key);
2010 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
2011 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
2012 && update_type == SM_UPDATE_NONE)) {
2013 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
2014 strbuf_addch(out, '\n');
2015 goto cleanup;
2018 /* Check if the submodule has been initialized. */
2019 if (!is_submodule_active(the_repository, ce->name)) {
2020 next_submodule_warn_missing(suc, out, displaypath);
2021 goto cleanup;
2024 strbuf_reset(&sb);
2025 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2026 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2027 if (sub->url && (starts_with_dot_slash(sub->url) ||
2028 starts_with_dot_dot_slash(sub->url))) {
2029 url = resolve_relative_url(sub->url, NULL, 0);
2030 need_free_url = 1;
2031 } else
2032 url = sub->url;
2035 if (!url)
2036 die(_("cannot clone submodule '%s' without a URL"), sub->name);
2038 strbuf_reset(&sb);
2039 strbuf_addf(&sb, "%s/.git", ce->name);
2040 needs_cloning = !file_exists(sb.buf);
2042 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2043 suc->update_clone_alloc);
2044 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2045 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2046 suc->update_clone[suc->update_clone_nr].sub = sub;
2047 suc->update_clone_nr++;
2049 if (!needs_cloning)
2050 goto cleanup;
2052 child->git_cmd = 1;
2053 child->no_stdin = 1;
2054 child->stdout_to_stderr = 1;
2055 child->err = -1;
2056 strvec_push(&child->args, "submodule--helper");
2057 strvec_push(&child->args, "clone");
2058 if (suc->update_data->progress)
2059 strvec_push(&child->args, "--progress");
2060 if (suc->update_data->quiet)
2061 strvec_push(&child->args, "--quiet");
2062 if (suc->update_data->prefix)
2063 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2064 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2065 strvec_push(&child->args, "--depth=1");
2066 else if (suc->update_data->depth)
2067 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2068 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2069 strvec_pushf(&child->args, "--filter=%s",
2070 expand_list_objects_filter_spec(suc->update_data->filter_options));
2071 if (suc->update_data->require_init)
2072 strvec_push(&child->args, "--require-init");
2073 strvec_pushl(&child->args, "--path", sub->path, NULL);
2074 strvec_pushl(&child->args, "--name", sub->name, NULL);
2075 strvec_pushl(&child->args, "--url", url, NULL);
2076 if (suc->update_data->references.nr) {
2077 struct string_list_item *item;
2079 for_each_string_list_item(item, &suc->update_data->references)
2080 strvec_pushl(&child->args, "--reference", item->string, NULL);
2082 if (suc->update_data->dissociate)
2083 strvec_push(&child->args, "--dissociate");
2084 if (suc->update_data->single_branch >= 0)
2085 strvec_push(&child->args, suc->update_data->single_branch ?
2086 "--single-branch" :
2087 "--no-single-branch");
2089 cleanup:
2090 free(displaypath);
2091 strbuf_release(&sb);
2092 if (need_free_url)
2093 free((void*)url);
2095 return needs_cloning;
2098 static int update_clone_get_next_task(struct child_process *child,
2099 struct strbuf *err,
2100 void *suc_cb,
2101 void **idx_task_cb)
2103 struct submodule_update_clone *suc = suc_cb;
2104 const struct cache_entry *ce;
2105 int index;
2107 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2108 ce = suc->update_data->list.entries[suc->current];
2109 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2110 int *p = xmalloc(sizeof(*p));
2112 *p = suc->current;
2113 *idx_task_cb = p;
2114 suc->current++;
2115 return 1;
2120 * The loop above tried cloning each submodule once, now try the
2121 * stragglers again, which we can imagine as an extension of the
2122 * entry list.
2124 index = suc->current - suc->update_data->list.nr;
2125 if (index < suc->failed_clones_nr) {
2126 int *p;
2128 ce = suc->failed_clones[index];
2129 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2130 suc->current ++;
2131 strbuf_addstr(err, "BUG: submodule considered for "
2132 "cloning, doesn't need cloning "
2133 "any more?\n");
2134 return 0;
2136 p = xmalloc(sizeof(*p));
2137 *p = suc->current;
2138 *idx_task_cb = p;
2139 suc->current ++;
2140 return 1;
2143 return 0;
2146 static int update_clone_start_failure(struct strbuf *err UNUSED,
2147 void *suc_cb,
2148 void *idx_task_cb UNUSED)
2150 struct submodule_update_clone *suc = suc_cb;
2152 suc->quickstop = 1;
2153 return 1;
2156 static int update_clone_task_finished(int result,
2157 struct strbuf *err,
2158 void *suc_cb,
2159 void *idx_task_cb)
2161 const struct cache_entry *ce;
2162 struct submodule_update_clone *suc = suc_cb;
2163 int *idxP = idx_task_cb;
2164 int idx = *idxP;
2166 free(idxP);
2168 if (!result)
2169 return 0;
2171 if (idx < suc->update_data->list.nr) {
2172 ce = suc->update_data->list.entries[idx];
2173 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2174 ce->name);
2175 strbuf_addch(err, '\n');
2176 ALLOC_GROW(suc->failed_clones,
2177 suc->failed_clones_nr + 1,
2178 suc->failed_clones_alloc);
2179 suc->failed_clones[suc->failed_clones_nr++] = ce;
2180 return 0;
2181 } else {
2182 idx -= suc->update_data->list.nr;
2183 ce = suc->failed_clones[idx];
2184 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2185 ce->name);
2186 strbuf_addch(err, '\n');
2187 suc->quickstop = 1;
2188 return 1;
2191 return 0;
2194 static int git_update_clone_config(const char *var, const char *value,
2195 void *cb)
2197 int *max_jobs = cb;
2199 if (!strcmp(var, "submodule.fetchjobs"))
2200 *max_jobs = parse_submodule_fetchjobs(var, value);
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;
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
2903 argc = parse_options(argc, argv, prefix, options, usage, 0);
2905 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2906 usage_with_options(usage, options);
2908 config_name = xstrfmt("submodule.%s.url", path);
2910 config_set_in_gitmodules_file_gently(config_name, newurl);
2911 sync_submodule(path, prefix, NULL, quiet ? OPT_QUIET : 0);
2913 free(config_name);
2915 return 0;
2918 static int module_set_branch(int argc, const char **argv, const char *prefix)
2920 int opt_default = 0, ret;
2921 const char *opt_branch = NULL;
2922 const char *path;
2923 char *config_name;
2924 struct option options[] = {
2926 * We accept the `quiet` option for uniformity across subcommands,
2927 * though there is nothing to make less verbose in this subcommand.
2929 OPT_NOOP_NOARG('q', "quiet"),
2931 OPT_BOOL('d', "default", &opt_default,
2932 N_("set the default tracking branch to master")),
2933 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2934 N_("set the default tracking branch")),
2935 OPT_END()
2937 const char *const usage[] = {
2938 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2939 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2940 NULL
2943 argc = parse_options(argc, argv, prefix, options, usage, 0);
2945 if (!opt_branch && !opt_default)
2946 die(_("--branch or --default required"));
2948 if (opt_branch && opt_default)
2949 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2951 if (argc != 1 || !(path = argv[0]))
2952 usage_with_options(usage, options);
2954 config_name = xstrfmt("submodule.%s.branch", path);
2955 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2957 free(config_name);
2958 return !!ret;
2961 static int module_create_branch(int argc, const char **argv, const char *prefix)
2963 enum branch_track track;
2964 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
2965 struct option options[] = {
2966 OPT__QUIET(&quiet, N_("print only error messages")),
2967 OPT__FORCE(&force, N_("force creation"), 0),
2968 OPT_BOOL(0, "create-reflog", &reflog,
2969 N_("create the branch's reflog")),
2970 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
2971 N_("set branch tracking configuration"),
2972 PARSE_OPT_OPTARG,
2973 parse_opt_tracking_mode),
2974 OPT__DRY_RUN(&dry_run,
2975 N_("show whether the branch would be created")),
2976 OPT_END()
2978 const char *const usage[] = {
2979 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2980 NULL
2983 git_config(git_default_config, NULL);
2984 track = git_branch_track;
2985 argc = parse_options(argc, argv, prefix, options, usage, 0);
2987 if (argc != 3)
2988 usage_with_options(usage, options);
2990 if (!quiet && !dry_run)
2991 printf_ln(_("creating branch '%s'"), argv[0]);
2993 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
2994 force, reflog, quiet, track, dry_run);
2995 return 0;
2998 struct add_data {
2999 const char *prefix;
3000 const char *branch;
3001 const char *reference_path;
3002 char *sm_path;
3003 const char *sm_name;
3004 const char *repo;
3005 const char *realrepo;
3006 int depth;
3007 unsigned int force: 1;
3008 unsigned int quiet: 1;
3009 unsigned int progress: 1;
3010 unsigned int dissociate: 1;
3012 #define ADD_DATA_INIT { .depth = -1 }
3014 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3016 struct child_process cp_remote = CHILD_PROCESS_INIT;
3017 struct strbuf sb_remote_out = STRBUF_INIT;
3019 cp_remote.git_cmd = 1;
3020 strvec_pushf(&cp_remote.env,
3021 "GIT_DIR=%s", git_dir_path);
3022 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3023 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3024 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3025 char *next_line;
3026 char *line = sb_remote_out.buf;
3028 while ((next_line = strchr(line, '\n')) != NULL) {
3029 size_t len = next_line - line;
3031 if (strip_suffix_mem(line, &len, " (fetch)"))
3032 strbuf_addf(msg, " %.*s\n", (int)len, line);
3033 line = next_line + 1;
3037 strbuf_release(&sb_remote_out);
3040 static int add_submodule(const struct add_data *add_data)
3042 char *submod_gitdir_path;
3043 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3044 struct string_list reference = STRING_LIST_INIT_NODUP;
3045 int ret = -1;
3047 /* perhaps the path already exists and is already a git repo, else clone it */
3048 if (is_directory(add_data->sm_path)) {
3049 struct strbuf sm_path = STRBUF_INIT;
3050 strbuf_addstr(&sm_path, add_data->sm_path);
3051 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3052 if (is_nonbare_repository_dir(&sm_path))
3053 printf(_("Adding existing repo at '%s' to the index\n"),
3054 add_data->sm_path);
3055 else
3056 die(_("'%s' already exists and is not a valid git repo"),
3057 add_data->sm_path);
3058 strbuf_release(&sm_path);
3059 free(submod_gitdir_path);
3060 } else {
3061 struct child_process cp = CHILD_PROCESS_INIT;
3063 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3065 if (is_directory(submod_gitdir_path)) {
3066 if (!add_data->force) {
3067 struct strbuf msg = STRBUF_INIT;
3068 char *die_msg;
3070 strbuf_addf(&msg, _("A git directory for '%s' is found "
3071 "locally with remote(s):\n"),
3072 add_data->sm_name);
3074 append_fetch_remotes(&msg, submod_gitdir_path);
3075 free(submod_gitdir_path);
3077 strbuf_addf(&msg, _("If you want to reuse this local git "
3078 "directory instead of cloning again from\n"
3079 " %s\n"
3080 "use the '--force' option. If the local git "
3081 "directory is not the correct repo\n"
3082 "or you are unsure what this means choose "
3083 "another name with the '--name' option."),
3084 add_data->realrepo);
3086 die_msg = strbuf_detach(&msg, NULL);
3087 die("%s", die_msg);
3088 } else {
3089 printf(_("Reactivating local git directory for "
3090 "submodule '%s'\n"), add_data->sm_name);
3093 free(submod_gitdir_path);
3095 clone_data.prefix = add_data->prefix;
3096 clone_data.path = add_data->sm_path;
3097 clone_data.name = add_data->sm_name;
3098 clone_data.url = add_data->realrepo;
3099 clone_data.quiet = add_data->quiet;
3100 clone_data.progress = add_data->progress;
3101 if (add_data->reference_path) {
3102 char *p = xstrdup(add_data->reference_path);
3104 string_list_append(&reference, p)->util = p;
3106 clone_data.dissociate = add_data->dissociate;
3107 if (add_data->depth >= 0)
3108 clone_data.depth = xstrfmt("%d", add_data->depth);
3110 if (clone_submodule(&clone_data, &reference))
3111 goto cleanup;
3113 prepare_submodule_repo_env(&cp.env);
3114 cp.git_cmd = 1;
3115 cp.dir = add_data->sm_path;
3117 * NOTE: we only get here if add_data->force is true, so
3118 * passing --force to checkout is reasonable.
3120 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3122 if (add_data->branch) {
3123 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3124 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3127 if (run_command(&cp))
3128 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3130 ret = 0;
3131 cleanup:
3132 string_list_clear(&reference, 1);
3133 return ret;
3136 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3138 char *key;
3139 int ret;
3141 if (!is_writing_gitmodules_ok())
3142 die(_("please make sure that the .gitmodules file is in the working tree"));
3144 key = xstrfmt("submodule.%s.%s", name, var);
3145 ret = config_set_in_gitmodules_file_gently(key, value);
3146 free(key);
3148 return ret;
3151 static void configure_added_submodule(struct add_data *add_data)
3153 char *key;
3154 struct child_process add_submod = CHILD_PROCESS_INIT;
3155 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3157 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3158 git_config_set_gently(key, add_data->realrepo);
3159 free(key);
3161 add_submod.git_cmd = 1;
3162 strvec_pushl(&add_submod.args, "add",
3163 "--no-warn-embedded-repo", NULL);
3164 if (add_data->force)
3165 strvec_push(&add_submod.args, "--force");
3166 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3168 if (run_command(&add_submod))
3169 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3171 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3172 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3173 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3175 if (add_data->branch) {
3176 if (config_submodule_in_gitmodules(add_data->sm_name,
3177 "branch", add_data->branch))
3178 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3181 add_gitmodules.git_cmd = 1;
3182 strvec_pushl(&add_gitmodules.args,
3183 "add", "--force", "--", ".gitmodules", NULL);
3185 if (run_command(&add_gitmodules))
3186 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3189 * NEEDSWORK: In a multi-working-tree world this needs to be
3190 * set in the per-worktree config.
3193 * NEEDSWORK: In the longer run, we need to get rid of this
3194 * pattern of querying "submodule.active" before calling
3195 * is_submodule_active(), since that function needs to find
3196 * out the value of "submodule.active" again anyway.
3198 if (!git_config_get("submodule.active")) {
3200 * If the submodule being added isn't already covered by the
3201 * current configured pathspec, set the submodule's active flag
3203 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3204 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3205 git_config_set_gently(key, "true");
3206 free(key);
3208 } else {
3209 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3210 git_config_set_gently(key, "true");
3211 free(key);
3215 static void die_on_index_match(const char *path, int force)
3217 struct pathspec ps;
3218 const char *args[] = { path, NULL };
3219 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3221 if (repo_read_index_preload(the_repository, NULL, 0) < 0)
3222 die(_("index file corrupt"));
3224 if (ps.nr) {
3225 int i;
3226 char *ps_matched = xcalloc(ps.nr, 1);
3228 /* TODO: audit for interaction with sparse-index. */
3229 ensure_full_index(&the_index);
3232 * Since there is only one pathspec, we just need to
3233 * check ps_matched[0] to know if a cache entry matched.
3235 for (i = 0; i < the_index.cache_nr; i++) {
3236 ce_path_match(&the_index, the_index.cache[i], &ps,
3237 ps_matched);
3239 if (ps_matched[0]) {
3240 if (!force)
3241 die(_("'%s' already exists in the index"),
3242 path);
3243 if (!S_ISGITLINK(the_index.cache[i]->ce_mode))
3244 die(_("'%s' already exists in the index "
3245 "and is not a submodule"), path);
3246 break;
3249 free(ps_matched);
3251 clear_pathspec(&ps);
3254 static void die_on_repo_without_commits(const char *path)
3256 struct strbuf sb = STRBUF_INIT;
3257 strbuf_addstr(&sb, path);
3258 if (is_nonbare_repository_dir(&sb)) {
3259 struct object_id oid;
3260 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3261 die(_("'%s' does not have a commit checked out"), path);
3263 strbuf_release(&sb);
3266 static int module_add(int argc, const char **argv, const char *prefix)
3268 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3269 struct add_data add_data = ADD_DATA_INIT;
3270 char *to_free = NULL;
3271 struct option options[] = {
3272 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3273 N_("branch of repository to add as submodule")),
3274 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3275 PARSE_OPT_NOCOMPLETE),
3276 OPT__QUIET(&quiet, N_("print only error messages")),
3277 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3278 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3279 N_("reference repository")),
3280 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3281 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3282 N_("sets the submodule's name to the given string "
3283 "instead of defaulting to its path")),
3284 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3285 OPT_END()
3287 const char *const usage[] = {
3288 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3289 NULL
3291 struct strbuf sb = STRBUF_INIT;
3292 int ret = 1;
3294 argc = parse_options(argc, argv, prefix, options, usage, 0);
3296 if (!is_writing_gitmodules_ok())
3297 die(_("please make sure that the .gitmodules file is in the working tree"));
3299 if (prefix && *prefix &&
3300 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3301 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3303 if (argc == 0 || argc > 2)
3304 usage_with_options(usage, options);
3306 add_data.repo = argv[0];
3307 if (argc == 1)
3308 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3309 else
3310 add_data.sm_path = xstrdup(argv[1]);
3312 if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
3313 char *sm_path = add_data.sm_path;
3315 add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
3316 free(sm_path);
3319 if (starts_with_dot_dot_slash(add_data.repo) ||
3320 starts_with_dot_slash(add_data.repo)) {
3321 if (prefix)
3322 die(_("Relative path can only be used from the toplevel "
3323 "of the working tree"));
3325 /* dereference source url relative to parent's url */
3326 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3327 add_data.realrepo = to_free;
3328 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3329 add_data.realrepo = add_data.repo;
3330 } else {
3331 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3332 add_data.repo);
3336 * normalize path:
3337 * multiple //; leading ./; /./; /../;
3339 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3340 strip_dir_trailing_slashes(add_data.sm_path);
3342 die_on_index_match(add_data.sm_path, force);
3343 die_on_repo_without_commits(add_data.sm_path);
3345 if (!force) {
3346 struct child_process cp = CHILD_PROCESS_INIT;
3348 cp.git_cmd = 1;
3349 cp.no_stdout = 1;
3350 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3351 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3352 if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3353 strbuf_complete_line(&sb);
3354 fputs(sb.buf, stderr);
3355 goto cleanup;
3359 if(!add_data.sm_name)
3360 add_data.sm_name = add_data.sm_path;
3362 if (check_submodule_name(add_data.sm_name))
3363 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3365 add_data.prefix = prefix;
3366 add_data.force = !!force;
3367 add_data.quiet = !!quiet;
3368 add_data.progress = !!progress;
3369 add_data.dissociate = !!dissociate;
3371 if (add_submodule(&add_data))
3372 goto cleanup;
3373 configure_added_submodule(&add_data);
3375 ret = 0;
3376 cleanup:
3377 free(add_data.sm_path);
3378 free(to_free);
3379 strbuf_release(&sb);
3381 return ret;
3384 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3386 parse_opt_subcommand_fn *fn = NULL;
3387 const char *const usage[] = {
3388 N_("git submodule--helper <command>"),
3389 NULL
3391 struct option options[] = {
3392 OPT_SUBCOMMAND("clone", &fn, module_clone),
3393 OPT_SUBCOMMAND("add", &fn, module_add),
3394 OPT_SUBCOMMAND("update", &fn, module_update),
3395 OPT_SUBCOMMAND("foreach", &fn, module_foreach),
3396 OPT_SUBCOMMAND("init", &fn, module_init),
3397 OPT_SUBCOMMAND("status", &fn, module_status),
3398 OPT_SUBCOMMAND("sync", &fn, module_sync),
3399 OPT_SUBCOMMAND("deinit", &fn, module_deinit),
3400 OPT_SUBCOMMAND("summary", &fn, module_summary),
3401 OPT_SUBCOMMAND("push-check", &fn, push_check),
3402 OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
3403 OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3404 OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3405 OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3406 OPT_END()
3408 argc = parse_options(argc, argv, prefix, options, usage, 0);
3410 return fn(argc, argv, prefix);