1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
3 #include "repository.h"
6 #include "parse-options.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
21 #include "object-store.h"
24 #include "list-objects-filter-options.h"
26 #define OPT_QUIET (1 << 0)
27 #define OPT_CACHED (1 << 1)
28 #define OPT_RECURSIVE (1 << 2)
29 #define OPT_FORCE (1 << 3)
31 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
34 static char *repo_get_default_remote(struct repository
*repo
)
36 char *dest
= NULL
, *ret
;
37 struct strbuf sb
= STRBUF_INIT
;
38 struct ref_store
*store
= get_main_ref_store(repo
);
39 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
43 die(_("No such ref: %s"), "HEAD");
46 if (!strcmp(refname
, "HEAD"))
47 return xstrdup("origin");
49 if (!skip_prefix(refname
, "refs/heads/", &refname
))
50 die(_("Expecting a full ref name, got %s"), refname
);
52 strbuf_addf(&sb
, "branch.%s.remote", refname
);
53 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
54 ret
= xstrdup("origin");
62 static char *get_default_remote_submodule(const char *module_path
)
64 struct repository subrepo
;
66 repo_submodule_init(&subrepo
, the_repository
, module_path
, null_oid());
67 return repo_get_default_remote(&subrepo
);
70 static char *get_default_remote(void)
72 return repo_get_default_remote(the_repository
);
75 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
77 char *remoteurl
, *resolved_url
;
78 char *remote
= get_default_remote();
79 struct strbuf remotesb
= STRBUF_INIT
;
81 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
82 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
84 warning(_("could not look up configuration '%s'. "
85 "Assuming this repository is its own "
86 "authoritative upstream."),
88 remoteurl
= xgetcwd();
90 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
94 strbuf_release(&remotesb
);
99 static int resolve_relative_url_test(int argc
, const char **argv
, const char *prefix
)
101 char *remoteurl
, *res
;
102 const char *up_path
, *url
;
105 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
108 remoteurl
= xstrdup(argv
[2]);
111 if (!strcmp(up_path
, "(null)"))
114 res
= relative_url(remoteurl
, url
, up_path
);
121 static char *do_get_submodule_displaypath(const char *path
,
123 const char *super_prefix
)
125 if (prefix
&& super_prefix
) {
126 BUG("cannot have prefix '%s' and superprefix '%s'",
127 prefix
, super_prefix
);
129 struct strbuf sb
= STRBUF_INIT
;
130 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
133 } else if (super_prefix
) {
134 return xstrfmt("%s%s", super_prefix
, path
);
136 return xstrdup(path
);
140 /* the result should be freed by the caller. */
141 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
143 const char *super_prefix
= get_super_prefix();
144 return do_get_submodule_displaypath(path
, prefix
, super_prefix
);
147 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
149 struct strbuf sb
= STRBUF_INIT
;
152 static const char *describe_bare
[] = { NULL
};
154 static const char *describe_tags
[] = { "--tags", NULL
};
156 static const char *describe_contains
[] = { "--contains", NULL
};
158 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
160 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
162 describe_all_always
, NULL
};
164 for (d
= describe_argv
; *d
; d
++) {
165 struct child_process cp
= CHILD_PROCESS_INIT
;
166 prepare_submodule_repo_env(&cp
.env
);
171 strvec_push(&cp
.args
, "describe");
172 strvec_pushv(&cp
.args
, *d
);
173 strvec_push(&cp
.args
, object_id
);
175 if (!capture_command(&cp
, &sb
, 0)) {
176 strbuf_strip_suffix(&sb
, "\n");
177 return strbuf_detach(&sb
, NULL
);
186 const struct cache_entry
**entries
;
189 #define MODULE_LIST_INIT { 0 }
191 static int module_list_compute(int argc
, const char **argv
,
193 struct pathspec
*pathspec
,
194 struct module_list
*list
)
197 char *ps_matched
= NULL
;
198 parse_pathspec(pathspec
, 0,
199 PATHSPEC_PREFER_FULL
,
203 ps_matched
= xcalloc(pathspec
->nr
, 1);
205 if (read_cache() < 0)
206 die(_("index file corrupt"));
208 for (i
= 0; i
< active_nr
; i
++) {
209 const struct cache_entry
*ce
= active_cache
[i
];
211 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
213 !S_ISGITLINK(ce
->ce_mode
))
216 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
217 list
->entries
[list
->nr
++] = ce
;
218 while (i
+ 1 < active_nr
&&
219 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
221 * Skip entries with the same name in different stages
222 * to make sure an entry is returned only once.
227 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
235 static void module_list_active(struct module_list
*list
)
238 struct module_list active_modules
= MODULE_LIST_INIT
;
240 for (i
= 0; i
< list
->nr
; i
++) {
241 const struct cache_entry
*ce
= list
->entries
[i
];
243 if (!is_submodule_active(the_repository
, ce
->name
))
246 ALLOC_GROW(active_modules
.entries
,
247 active_modules
.nr
+ 1,
248 active_modules
.alloc
);
249 active_modules
.entries
[active_modules
.nr
++] = ce
;
253 *list
= active_modules
;
256 static char *get_up_path(const char *path
)
259 struct strbuf sb
= STRBUF_INIT
;
261 for (i
= count_slashes(path
); i
; i
--)
262 strbuf_addstr(&sb
, "../");
265 * Check if 'path' ends with slash or not
266 * for having the same output for dir/sub_dir
269 if (!is_dir_sep(path
[strlen(path
) - 1]))
270 strbuf_addstr(&sb
, "../");
272 return strbuf_detach(&sb
, NULL
);
275 static int module_list(int argc
, const char **argv
, const char *prefix
)
278 struct pathspec pathspec
;
279 struct module_list list
= MODULE_LIST_INIT
;
281 struct option module_list_options
[] = {
282 OPT_STRING(0, "prefix", &prefix
,
284 N_("alternative anchor for relative paths")),
288 const char *const git_submodule_helper_usage
[] = {
289 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
293 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
294 git_submodule_helper_usage
, 0);
296 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
299 for (i
= 0; i
< list
.nr
; i
++) {
300 const struct cache_entry
*ce
= list
.entries
[i
];
303 printf("%06o %s U\t", ce
->ce_mode
,
304 oid_to_hex(null_oid()));
306 printf("%06o %s %d\t", ce
->ce_mode
,
307 oid_to_hex(&ce
->oid
), ce_stage(ce
));
309 fprintf(stdout
, "%s\n", ce
->name
);
314 static void for_each_listed_submodule(const struct module_list
*list
,
315 each_submodule_fn fn
, void *cb_data
)
318 for (i
= 0; i
< list
->nr
; i
++)
319 fn(list
->entries
[i
], cb_data
);
329 #define FOREACH_CB_INIT { 0 }
331 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
334 struct foreach_cb
*info
= cb_data
;
335 const char *path
= list_item
->name
;
336 const struct object_id
*ce_oid
= &list_item
->oid
;
338 const struct submodule
*sub
;
339 struct child_process cp
= CHILD_PROCESS_INIT
;
342 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
344 sub
= submodule_from_path(the_repository
, null_oid(), path
);
347 die(_("No url found for submodule path '%s' in .gitmodules"),
350 if (!is_submodule_populated_gently(path
, NULL
))
353 prepare_submodule_repo_env(&cp
.env
);
356 * For the purpose of executing <command> in the submodule,
357 * separate shell is used for the purpose of running the
364 * NEEDSWORK: the command currently has access to the variables $name,
365 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
366 * contains a single argument. This is done for maintaining a faithful
367 * translation from shell script.
369 if (info
->argc
== 1) {
370 char *toplevel
= xgetcwd();
371 struct strbuf sb
= STRBUF_INIT
;
373 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
374 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
375 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
376 strvec_pushf(&cp
.env
, "sha1=%s",
378 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
381 * Since the path variable was accessible from the script
382 * before porting, it is also made available after porting.
383 * The environment variable "PATH" has a very special purpose
384 * on windows. And since environment variables are
385 * case-insensitive in windows, it interferes with the
386 * existing PATH variable. Hence, to avoid that, we expose
387 * path via the args strvec and not via env.
389 sq_quote_buf(&sb
, path
);
390 strvec_pushf(&cp
.args
, "path=%s; %s",
391 sb
.buf
, info
->argv
[0]);
395 strvec_pushv(&cp
.args
, info
->argv
);
399 printf(_("Entering '%s'\n"), displaypath
);
401 if (info
->argv
[0] && run_command(&cp
))
402 die(_("run_command returned non-zero status for %s\n."),
405 if (info
->recursive
) {
406 struct child_process cpr
= CHILD_PROCESS_INIT
;
410 prepare_submodule_repo_env(&cpr
.env
);
412 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
413 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
414 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
418 strvec_push(&cpr
.args
, "--quiet");
420 strvec_push(&cpr
.args
, "--");
421 strvec_pushv(&cpr
.args
, info
->argv
);
423 if (run_command(&cpr
))
424 die(_("run_command returned non-zero status while "
425 "recursing in the nested submodules of %s\n."),
433 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
435 struct foreach_cb info
= FOREACH_CB_INIT
;
436 struct pathspec pathspec
;
437 struct module_list list
= MODULE_LIST_INIT
;
439 struct option module_foreach_options
[] = {
440 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
441 OPT_BOOL(0, "recursive", &info
.recursive
,
442 N_("recurse into nested submodules")),
446 const char *const git_submodule_helper_usage
[] = {
447 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
451 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
452 git_submodule_helper_usage
, 0);
454 if (module_list_compute(0, NULL
, prefix
, &pathspec
, &list
) < 0)
459 info
.prefix
= prefix
;
461 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
466 static int starts_with_dot_slash(const char *const path
)
468 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
469 PATH_MATCH_XPLATFORM
);
472 static int starts_with_dot_dot_slash(const char *const path
)
474 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
475 PATH_MATCH_XPLATFORM
);
480 const char *superprefix
;
483 #define INIT_CB_INIT { 0 }
485 static void init_submodule(const char *path
, const char *prefix
,
486 const char *superprefix
, unsigned int flags
)
488 const struct submodule
*sub
;
489 struct strbuf sb
= STRBUF_INIT
;
490 char *upd
= NULL
, *url
= NULL
, *displaypath
;
492 /* try superprefix from the environment, if it is not passed explicitly */
494 superprefix
= get_super_prefix();
495 displaypath
= do_get_submodule_displaypath(path
, prefix
, superprefix
);
497 sub
= submodule_from_path(the_repository
, null_oid(), path
);
500 die(_("No url found for submodule path '%s' in .gitmodules"),
504 * NEEDSWORK: In a multi-working-tree world, this needs to be
505 * set in the per-worktree config.
507 * Set active flag for the submodule being initialized
509 if (!is_submodule_active(the_repository
, path
)) {
510 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
511 git_config_set_gently(sb
.buf
, "true");
516 * Copy url setting when it is not set yet.
517 * To look up the url in .git/config, we must not fall back to
518 * .gitmodules, so look it up directly.
520 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
521 if (git_config_get_string(sb
.buf
, &url
)) {
523 die(_("No url found for submodule path '%s' in .gitmodules"),
526 url
= xstrdup(sub
->url
);
528 /* Possibly a url relative to parent */
529 if (starts_with_dot_dot_slash(url
) ||
530 starts_with_dot_slash(url
)) {
532 url
= resolve_relative_url(oldurl
, NULL
, 0);
536 if (git_config_set_gently(sb
.buf
, url
))
537 die(_("Failed to register url for submodule path '%s'"),
539 if (!(flags
& OPT_QUIET
))
541 _("Submodule '%s' (%s) registered for path '%s'\n"),
542 sub
->name
, url
, displaypath
);
546 /* Copy "update" setting when it is not set yet */
547 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
548 if (git_config_get_string(sb
.buf
, &upd
) &&
549 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
550 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
551 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
553 upd
= xstrdup("none");
555 upd
= xstrdup(submodule_strategy_to_string(&sub
->update_strategy
));
557 if (git_config_set_gently(sb
.buf
, upd
))
558 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
566 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
568 struct init_cb
*info
= cb_data
;
569 init_submodule(list_item
->name
, info
->prefix
, info
->superprefix
, info
->flags
);
572 static int module_init(int argc
, const char **argv
, const char *prefix
)
574 struct init_cb info
= INIT_CB_INIT
;
575 struct pathspec pathspec
;
576 struct module_list list
= MODULE_LIST_INIT
;
579 struct option module_init_options
[] = {
580 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
584 const char *const git_submodule_helper_usage
[] = {
585 N_("git submodule--helper init [<options>] [<path>]"),
589 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
590 git_submodule_helper_usage
, 0);
592 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
596 * If there are no path args and submodule.active is set then,
597 * by default, only initialize 'active' modules.
599 if (!argc
&& git_config_get_value_multi("submodule.active"))
600 module_list_active(&list
);
602 info
.prefix
= prefix
;
604 info
.flags
|= OPT_QUIET
;
606 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
615 #define STATUS_CB_INIT { 0 }
617 static void print_status(unsigned int flags
, char state
, const char *path
,
618 const struct object_id
*oid
, const char *displaypath
)
620 if (flags
& OPT_QUIET
)
623 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
625 if (state
== ' ' || state
== '+') {
626 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
629 printf(" (%s)", name
);
635 static int handle_submodule_head_ref(const char *refname
,
636 const struct object_id
*oid
, int flags
,
639 struct object_id
*output
= cb_data
;
646 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
647 unsigned int ce_flags
, const char *prefix
,
651 struct strvec diff_files_args
= STRVEC_INIT
;
652 struct rev_info rev
= REV_INFO_INIT
;
653 int diff_files_result
;
654 struct strbuf buf
= STRBUF_INIT
;
657 if (!submodule_from_path(the_repository
, null_oid(), path
))
658 die(_("no submodule mapping found in .gitmodules for path '%s'"),
661 displaypath
= get_submodule_displaypath(path
, prefix
);
663 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
664 print_status(flags
, 'U', path
, null_oid(), displaypath
);
668 strbuf_addf(&buf
, "%s/.git", path
);
669 git_dir
= read_gitfile(buf
.buf
);
673 if (!is_submodule_active(the_repository
, path
) ||
674 !is_git_directory(git_dir
)) {
675 print_status(flags
, '-', path
, ce_oid
, displaypath
);
676 strbuf_release(&buf
);
679 strbuf_release(&buf
);
681 strvec_pushl(&diff_files_args
, "diff-files",
682 "--ignore-submodules=dirty", "--quiet", "--",
685 git_config(git_diff_basic_config
, NULL
);
687 repo_init_revisions(the_repository
, &rev
, NULL
);
689 diff_files_args
.nr
= setup_revisions(diff_files_args
.nr
,
692 diff_files_result
= run_diff_files(&rev
, 0);
694 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
695 print_status(flags
, ' ', path
, ce_oid
,
697 } else if (!(flags
& OPT_CACHED
)) {
698 struct object_id oid
;
699 struct ref_store
*refs
= get_submodule_ref_store(path
);
702 print_status(flags
, '-', path
, ce_oid
, displaypath
);
705 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
706 die(_("could not resolve HEAD ref inside the "
707 "submodule '%s'"), path
);
709 print_status(flags
, '+', path
, &oid
, displaypath
);
711 print_status(flags
, '+', path
, ce_oid
, displaypath
);
714 if (flags
& OPT_RECURSIVE
) {
715 struct child_process cpr
= CHILD_PROCESS_INIT
;
719 prepare_submodule_repo_env(&cpr
.env
);
721 strvec_push(&cpr
.args
, "--super-prefix");
722 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
723 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
724 "--recursive", NULL
);
726 if (flags
& OPT_CACHED
)
727 strvec_push(&cpr
.args
, "--cached");
729 if (flags
& OPT_QUIET
)
730 strvec_push(&cpr
.args
, "--quiet");
732 if (run_command(&cpr
))
733 die(_("failed to recurse into submodule '%s'"), path
);
737 strvec_clear(&diff_files_args
);
739 release_revisions(&rev
);
742 static void status_submodule_cb(const struct cache_entry
*list_item
,
745 struct status_cb
*info
= cb_data
;
746 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
747 info
->prefix
, info
->flags
);
750 static int module_status(int argc
, const char **argv
, const char *prefix
)
752 struct status_cb info
= STATUS_CB_INIT
;
753 struct pathspec pathspec
;
754 struct module_list list
= MODULE_LIST_INIT
;
757 struct option module_status_options
[] = {
758 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
759 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
760 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
764 const char *const git_submodule_helper_usage
[] = {
765 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
769 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
770 git_submodule_helper_usage
, 0);
772 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
775 info
.prefix
= prefix
;
777 info
.flags
|= OPT_QUIET
;
779 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
784 static int module_name(int argc
, const char **argv
, const char *prefix
)
786 const struct submodule
*sub
;
789 usage(_("git submodule--helper name <path>"));
791 sub
= submodule_from_path(the_repository
, null_oid(), argv
[1]);
794 die(_("no submodule mapping found in .gitmodules for path '%s'"),
797 printf("%s\n", sub
->name
);
803 unsigned int mod_src
;
804 unsigned int mod_dst
;
805 struct object_id oid_src
;
806 struct object_id oid_dst
;
810 #define MODULE_CB_INIT { 0 }
812 struct module_cb_list
{
813 struct module_cb
**entries
;
816 #define MODULE_CB_LIST_INIT { 0 }
822 unsigned int cached
: 1;
823 unsigned int for_status
: 1;
824 unsigned int files
: 1;
827 #define SUMMARY_CB_INIT { 0 }
834 static char *verify_submodule_committish(const char *sm_path
,
835 const char *committish
)
837 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
838 struct strbuf result
= STRBUF_INIT
;
840 cp_rev_parse
.git_cmd
= 1;
841 cp_rev_parse
.dir
= sm_path
;
842 prepare_submodule_repo_env(&cp_rev_parse
.env
);
843 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
844 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
845 strvec_push(&cp_rev_parse
.args
, "--");
847 if (capture_command(&cp_rev_parse
, &result
, 0))
850 strbuf_trim_trailing_newline(&result
);
851 return strbuf_detach(&result
, NULL
);
854 static void print_submodule_summary(struct summary_cb
*info
, char *errmsg
,
855 int total_commits
, const char *displaypath
,
856 const char *src_abbrev
, const char *dst_abbrev
,
859 if (p
->status
== 'T') {
860 if (S_ISGITLINK(p
->mod_dst
))
861 printf(_("* %s %s(blob)->%s(submodule)"),
862 displaypath
, src_abbrev
, dst_abbrev
);
864 printf(_("* %s %s(submodule)->%s(blob)"),
865 displaypath
, src_abbrev
, dst_abbrev
);
867 printf("* %s %s...%s",
868 displaypath
, src_abbrev
, dst_abbrev
);
871 if (total_commits
< 0)
874 printf(" (%d):\n", total_commits
);
877 printf(_("%s"), errmsg
);
878 } else if (total_commits
> 0) {
879 struct child_process cp_log
= CHILD_PROCESS_INIT
;
882 cp_log
.dir
= p
->sm_path
;
883 prepare_submodule_repo_env(&cp_log
.env
);
884 strvec_pushl(&cp_log
.args
, "log", NULL
);
886 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
887 if (info
->summary_limit
> 0)
888 strvec_pushf(&cp_log
.args
, "-%d",
889 info
->summary_limit
);
891 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
892 "--first-parent", NULL
);
893 strvec_pushf(&cp_log
.args
, "%s...%s",
894 src_abbrev
, dst_abbrev
);
895 } else if (S_ISGITLINK(p
->mod_dst
)) {
896 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
897 "-1", dst_abbrev
, NULL
);
899 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
900 "-1", src_abbrev
, NULL
);
902 run_command(&cp_log
);
907 static void generate_submodule_summary(struct summary_cb
*info
,
910 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
911 int missing_src
= 0, missing_dst
= 0;
913 int total_commits
= -1;
915 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
916 if (S_ISGITLINK(p
->mod_dst
)) {
917 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
919 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
920 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
922 int fd
= open(p
->sm_path
, O_RDONLY
);
924 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
925 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
927 error(_("couldn't hash object from '%s'"), p
->sm_path
);
929 /* for a submodule removal (mode:0000000), don't warn */
931 warning(_("unexpected mode %o\n"), p
->mod_dst
);
935 if (S_ISGITLINK(p
->mod_src
)) {
936 if (p
->status
!= 'D')
937 src_abbrev
= verify_submodule_committish(p
->sm_path
,
938 oid_to_hex(&p
->oid_src
));
942 * As `rev-parse` failed, we fallback to getting
943 * the abbreviated hash using oid_src. We do
944 * this as we might still need the abbreviated
945 * hash in cases like a submodule type change, etc.
947 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
951 * The source does not point to a submodule.
952 * So, we fallback to getting the abbreviation using
953 * oid_src as we might still need the abbreviated
954 * hash in cases like submodule add, etc.
956 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
959 if (S_ISGITLINK(p
->mod_dst
)) {
960 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
961 oid_to_hex(&p
->oid_dst
));
965 * As `rev-parse` failed, we fallback to getting
966 * the abbreviated hash using oid_dst. We do
967 * this as we might still need the abbreviated
968 * hash in cases like a submodule type change, etc.
970 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
974 * The destination does not point to a submodule.
975 * So, we fallback to getting the abbreviation using
976 * oid_dst as we might still need the abbreviated
977 * hash in cases like a submodule removal, etc.
979 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
982 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
);
984 if (!missing_src
&& !missing_dst
) {
985 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
986 struct strbuf sb_rev_list
= STRBUF_INIT
;
988 strvec_pushl(&cp_rev_list
.args
, "rev-list",
989 "--first-parent", "--count", NULL
);
990 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
991 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
992 src_abbrev
, dst_abbrev
);
994 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
995 src_abbrev
: dst_abbrev
);
996 strvec_push(&cp_rev_list
.args
, "--");
998 cp_rev_list
.git_cmd
= 1;
999 cp_rev_list
.dir
= p
->sm_path
;
1000 prepare_submodule_repo_env(&cp_rev_list
.env
);
1002 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
1003 total_commits
= atoi(sb_rev_list
.buf
);
1005 strbuf_release(&sb_rev_list
);
1008 * Don't give error msg for modification whose dst is not
1009 * submodule, i.e., deleted or changed to blob
1011 if (S_ISGITLINK(p
->mod_dst
)) {
1012 struct strbuf errmsg_str
= STRBUF_INIT
;
1013 if (missing_src
&& missing_dst
) {
1014 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commits %s and %s\n",
1015 displaypath
, oid_to_hex(&p
->oid_src
),
1016 oid_to_hex(&p
->oid_dst
));
1018 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commit %s\n",
1019 displaypath
, missing_src
?
1020 oid_to_hex(&p
->oid_src
) :
1021 oid_to_hex(&p
->oid_dst
));
1023 errmsg
= strbuf_detach(&errmsg_str
, NULL
);
1027 print_submodule_summary(info
, errmsg
, total_commits
,
1028 displaypath
, src_abbrev
,
1036 static void prepare_submodule_summary(struct summary_cb
*info
,
1037 struct module_cb_list
*list
)
1040 for (i
= 0; i
< list
->nr
; i
++) {
1041 const struct submodule
*sub
;
1042 struct module_cb
*p
= list
->entries
[i
];
1043 struct strbuf sm_gitdir
= STRBUF_INIT
;
1045 if (p
->status
== 'D' || p
->status
== 'T') {
1046 generate_submodule_summary(info
, p
);
1050 if (info
->for_status
&& p
->status
!= 'A' &&
1051 (sub
= submodule_from_path(the_repository
,
1052 null_oid(), p
->sm_path
))) {
1053 char *config_key
= NULL
;
1057 config_key
= xstrfmt("submodule.%s.ignore",
1059 if (!git_config_get_string_tmp(config_key
, &value
))
1060 ignore_all
= !strcmp(value
, "all");
1061 else if (sub
->ignore
)
1062 ignore_all
= !strcmp(sub
->ignore
, "all");
1069 /* Also show added or modified modules which are checked out */
1070 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1071 if (is_nonbare_repository_dir(&sm_gitdir
))
1072 generate_submodule_summary(info
, p
);
1073 strbuf_release(&sm_gitdir
);
1077 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1078 struct diff_options
*options
,
1082 struct module_cb_list
*list
= data
;
1083 for (i
= 0; i
< q
->nr
; i
++) {
1084 struct diff_filepair
*p
= q
->queue
[i
];
1085 struct module_cb
*temp
;
1087 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1089 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1090 temp
->mod_src
= p
->one
->mode
;
1091 temp
->mod_dst
= p
->two
->mode
;
1092 temp
->oid_src
= p
->one
->oid
;
1093 temp
->oid_dst
= p
->two
->oid
;
1094 temp
->status
= p
->status
;
1095 temp
->sm_path
= xstrdup(p
->one
->path
);
1097 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1098 list
->entries
[list
->nr
++] = temp
;
1102 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1105 case DIFF_INDEX
: return "diff-index";
1106 case DIFF_FILES
: return "diff-files";
1107 default: BUG("bad diff_cmd value %d", diff_cmd
);
1111 static int compute_summary_module_list(struct object_id
*head_oid
,
1112 struct summary_cb
*info
,
1113 enum diff_cmd diff_cmd
)
1115 struct strvec diff_args
= STRVEC_INIT
;
1116 struct rev_info rev
;
1117 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1120 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1122 strvec_push(&diff_args
, "--cached");
1123 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1125 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1126 strvec_push(&diff_args
, "--");
1128 strvec_pushv(&diff_args
, info
->argv
);
1130 git_config(git_diff_basic_config
, NULL
);
1131 init_revisions(&rev
, info
->prefix
);
1133 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1134 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, NULL
);
1135 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1136 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1137 rev
.diffopt
.format_callback_data
= &list
;
1139 if (!info
->cached
) {
1140 if (diff_cmd
== DIFF_INDEX
)
1142 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1143 perror("read_cache_preload");
1147 } else if (read_cache() < 0) {
1148 perror("read_cache");
1153 if (diff_cmd
== DIFF_INDEX
)
1154 run_diff_index(&rev
, info
->cached
);
1156 run_diff_files(&rev
, 0);
1157 prepare_submodule_summary(info
, &list
);
1159 strvec_clear(&diff_args
);
1160 release_revisions(&rev
);
1164 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1166 struct summary_cb info
= SUMMARY_CB_INIT
;
1170 int summary_limit
= -1;
1171 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1172 struct object_id head_oid
;
1175 struct option module_summary_options
[] = {
1176 OPT_BOOL(0, "cached", &cached
,
1177 N_("use the commit stored in the index instead of the submodule HEAD")),
1178 OPT_BOOL(0, "files", &files
,
1179 N_("compare the commit in the index with that in the submodule HEAD")),
1180 OPT_BOOL(0, "for-status", &for_status
,
1181 N_("skip submodules with 'ignore_config' value set to 'all'")),
1182 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1183 N_("limit the summary size")),
1187 const char *const git_submodule_helper_usage
[] = {
1188 N_("git submodule--helper summary [<options>] [<commit>] [--] [<path>]"),
1192 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1193 git_submodule_helper_usage
, 0);
1198 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1203 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1204 /* before the first commit: compare with an empty tree */
1205 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1211 if (get_oid("HEAD", &head_oid
))
1212 die(_("could not fetch a revision for HEAD"));
1217 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1218 diff_cmd
= DIFF_FILES
;
1223 info
.prefix
= prefix
;
1224 info
.cached
= !!cached
;
1225 info
.files
= !!files
;
1226 info
.for_status
= !!for_status
;
1227 info
.summary_limit
= summary_limit
;
1229 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1238 #define SYNC_CB_INIT { 0 }
1240 static void sync_submodule(const char *path
, const char *prefix
,
1243 const struct submodule
*sub
;
1244 char *remote_key
= NULL
;
1245 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1246 struct strbuf sb
= STRBUF_INIT
;
1247 char *sub_config_path
= NULL
;
1249 if (!is_submodule_active(the_repository
, path
))
1252 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1254 if (sub
&& sub
->url
) {
1255 if (starts_with_dot_dot_slash(sub
->url
) ||
1256 starts_with_dot_slash(sub
->url
)) {
1257 char *up_path
= get_up_path(path
);
1258 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1259 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1262 sub_origin_url
= xstrdup(sub
->url
);
1263 super_config_url
= xstrdup(sub
->url
);
1266 sub_origin_url
= xstrdup("");
1267 super_config_url
= xstrdup("");
1270 displaypath
= get_submodule_displaypath(path
, prefix
);
1272 if (!(flags
& OPT_QUIET
))
1273 printf(_("Synchronizing submodule url for '%s'\n"),
1277 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1278 if (git_config_set_gently(sb
.buf
, super_config_url
))
1279 die(_("failed to register url for submodule path '%s'"),
1282 if (!is_submodule_populated_gently(path
, NULL
))
1286 default_remote
= get_default_remote_submodule(path
);
1287 if (!default_remote
)
1288 die(_("failed to get the default remote for submodule '%s'"),
1291 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1292 free(default_remote
);
1294 submodule_to_gitdir(&sb
, path
);
1295 strbuf_addstr(&sb
, "/config");
1297 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1298 die(_("failed to update remote for submodule '%s'"),
1301 if (flags
& OPT_RECURSIVE
) {
1302 struct child_process cpr
= CHILD_PROCESS_INIT
;
1306 prepare_submodule_repo_env(&cpr
.env
);
1308 strvec_push(&cpr
.args
, "--super-prefix");
1309 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1310 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1311 "--recursive", NULL
);
1313 if (flags
& OPT_QUIET
)
1314 strvec_push(&cpr
.args
, "--quiet");
1316 if (run_command(&cpr
))
1317 die(_("failed to recurse into submodule '%s'"),
1322 free(super_config_url
);
1323 free(sub_origin_url
);
1324 strbuf_release(&sb
);
1327 free(sub_config_path
);
1330 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1332 struct sync_cb
*info
= cb_data
;
1333 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1336 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1338 struct sync_cb info
= SYNC_CB_INIT
;
1339 struct pathspec pathspec
;
1340 struct module_list list
= MODULE_LIST_INIT
;
1344 struct option module_sync_options
[] = {
1345 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1346 OPT_BOOL(0, "recursive", &recursive
,
1347 N_("recurse into nested submodules")),
1351 const char *const git_submodule_helper_usage
[] = {
1352 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1356 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1357 git_submodule_helper_usage
, 0);
1359 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1362 info
.prefix
= prefix
;
1364 info
.flags
|= OPT_QUIET
;
1366 info
.flags
|= OPT_RECURSIVE
;
1368 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1377 #define DEINIT_CB_INIT { 0 }
1379 static void deinit_submodule(const char *path
, const char *prefix
,
1382 const struct submodule
*sub
;
1383 char *displaypath
= NULL
;
1384 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1385 struct strbuf sb_config
= STRBUF_INIT
;
1386 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1388 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1390 if (!sub
|| !sub
->name
)
1393 displaypath
= get_submodule_displaypath(path
, prefix
);
1395 /* remove the submodule work tree (unless the user already did it) */
1396 if (is_directory(path
)) {
1397 struct strbuf sb_rm
= STRBUF_INIT
;
1400 if (is_directory(sub_git_dir
)) {
1401 if (!(flags
& OPT_QUIET
))
1402 warning(_("Submodule work tree '%s' contains a .git "
1403 "directory. This will be replaced with a "
1404 ".git file by using absorbgitdirs."),
1407 absorb_git_dir_into_superproject(path
,
1408 ABSORB_GITDIR_RECURSE_SUBMODULES
);
1412 if (!(flags
& OPT_FORCE
)) {
1413 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1415 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1418 if (run_command(&cp_rm
))
1419 die(_("Submodule work tree '%s' contains local "
1420 "modifications; use '-f' to discard them"),
1424 strbuf_addstr(&sb_rm
, path
);
1426 if (!remove_dir_recursively(&sb_rm
, 0))
1427 format
= _("Cleared directory '%s'\n");
1429 format
= _("Could not remove submodule work tree '%s'\n");
1431 if (!(flags
& OPT_QUIET
))
1432 printf(format
, displaypath
);
1434 submodule_unset_core_worktree(sub
);
1436 strbuf_release(&sb_rm
);
1439 if (mkdir(path
, 0777))
1440 printf(_("could not create empty submodule directory %s"),
1443 cp_config
.git_cmd
= 1;
1444 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1445 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1447 /* remove the .git/config entries (unless the user already did it) */
1448 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1449 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1451 * remove the whole section so we have a clean state when
1452 * the user later decides to init this submodule again
1454 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1455 if (!(flags
& OPT_QUIET
))
1456 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1457 sub
->name
, sub
->url
, displaypath
);
1464 strbuf_release(&sb_config
);
1467 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1470 struct deinit_cb
*info
= cb_data
;
1471 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1474 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1476 struct deinit_cb info
= DEINIT_CB_INIT
;
1477 struct pathspec pathspec
;
1478 struct module_list list
= MODULE_LIST_INIT
;
1483 struct option module_deinit_options
[] = {
1484 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1485 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1486 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1490 const char *const git_submodule_helper_usage
[] = {
1491 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1495 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1496 git_submodule_helper_usage
, 0);
1499 error("pathspec and --all are incompatible");
1500 usage_with_options(git_submodule_helper_usage
,
1501 module_deinit_options
);
1505 die(_("Use '--all' if you really want to deinitialize all submodules"));
1507 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1510 info
.prefix
= prefix
;
1512 info
.flags
|= OPT_QUIET
;
1514 info
.flags
|= OPT_FORCE
;
1516 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1521 struct module_clone_data
{
1527 struct list_objects_filter_options
*filter_options
;
1528 struct string_list reference
;
1529 unsigned int quiet
: 1;
1530 unsigned int progress
: 1;
1531 unsigned int dissociate
: 1;
1532 unsigned int require_init
: 1;
1535 #define MODULE_CLONE_DATA_INIT { \
1536 .reference = STRING_LIST_INIT_NODUP, \
1537 .single_branch = -1, \
1540 struct submodule_alternate_setup
{
1541 const char *submodule_name
;
1542 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1543 SUBMODULE_ALTERNATE_ERROR_DIE
,
1544 SUBMODULE_ALTERNATE_ERROR_INFO
,
1545 SUBMODULE_ALTERNATE_ERROR_IGNORE
1547 struct string_list
*reference
;
1549 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1550 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1553 static const char alternate_error_advice
[] = N_(
1554 "An alternate computed from a superproject's alternate is invalid.\n"
1555 "To allow Git to clone without an alternate in such a case, set\n"
1556 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1557 "'--reference-if-able' instead of '--reference'."
1560 static int add_possible_reference_from_superproject(
1561 struct object_directory
*odb
, void *sas_cb
)
1563 struct submodule_alternate_setup
*sas
= sas_cb
;
1567 * If the alternate object store is another repository, try the
1568 * standard layout with .git/(modules/<name>)+/objects
1570 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1571 struct repository alternate
;
1573 struct strbuf sb
= STRBUF_INIT
;
1574 struct strbuf err
= STRBUF_INIT
;
1575 strbuf_add(&sb
, odb
->path
, len
);
1577 repo_init(&alternate
, sb
.buf
, NULL
);
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.
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
);
1592 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1595 switch (sas
->error_mode
) {
1596 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1597 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1598 advise(_(alternate_error_advice
));
1599 die(_("submodule '%s' cannot add alternate: %s"),
1600 sas
->submodule_name
, err
.buf
);
1601 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1602 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1603 sas
->submodule_name
, err
.buf
);
1604 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1608 strbuf_release(&sb
);
1614 static void prepare_possible_alternates(const char *sm_name
,
1615 struct string_list
*reference
)
1617 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1618 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1620 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1624 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1626 if (!error_strategy
)
1627 error_strategy
= xstrdup("die");
1629 sas
.submodule_name
= sm_name
;
1630 sas
.reference
= reference
;
1631 if (!strcmp(error_strategy
, "die"))
1632 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1633 else if (!strcmp(error_strategy
, "info"))
1634 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1635 else if (!strcmp(error_strategy
, "ignore"))
1636 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1638 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1640 if (!strcmp(sm_alternate
, "superproject"))
1641 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1642 else if (!strcmp(sm_alternate
, "no"))
1645 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1648 free(error_strategy
);
1651 static int clone_submodule(struct module_clone_data
*clone_data
)
1653 char *p
, *sm_gitdir
;
1654 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1655 struct strbuf sb
= STRBUF_INIT
;
1656 struct child_process cp
= CHILD_PROCESS_INIT
;
1658 submodule_name_to_gitdir(&sb
, the_repository
, clone_data
->name
);
1659 sm_gitdir
= absolute_pathdup(sb
.buf
);
1662 if (!is_absolute_path(clone_data
->path
)) {
1663 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), clone_data
->path
);
1664 clone_data
->path
= strbuf_detach(&sb
, NULL
);
1666 clone_data
->path
= xstrdup(clone_data
->path
);
1669 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1670 die(_("refusing to create/use '%s' in another submodule's "
1671 "git dir"), sm_gitdir
);
1673 if (!file_exists(sm_gitdir
)) {
1674 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1675 die(_("could not create directory '%s'"), sm_gitdir
);
1677 prepare_possible_alternates(clone_data
->name
, &clone_data
->reference
);
1679 strvec_push(&cp
.args
, "clone");
1680 strvec_push(&cp
.args
, "--no-checkout");
1681 if (clone_data
->quiet
)
1682 strvec_push(&cp
.args
, "--quiet");
1683 if (clone_data
->progress
)
1684 strvec_push(&cp
.args
, "--progress");
1685 if (clone_data
->depth
&& *(clone_data
->depth
))
1686 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1687 if (clone_data
->reference
.nr
) {
1688 struct string_list_item
*item
;
1689 for_each_string_list_item(item
, &clone_data
->reference
)
1690 strvec_pushl(&cp
.args
, "--reference",
1691 item
->string
, NULL
);
1693 if (clone_data
->dissociate
)
1694 strvec_push(&cp
.args
, "--dissociate");
1695 if (sm_gitdir
&& *sm_gitdir
)
1696 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1697 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1698 strvec_pushf(&cp
.args
, "--filter=%s",
1699 expand_list_objects_filter_spec(
1700 clone_data
->filter_options
));
1701 if (clone_data
->single_branch
>= 0)
1702 strvec_push(&cp
.args
, clone_data
->single_branch
?
1704 "--no-single-branch");
1706 strvec_push(&cp
.args
, "--");
1707 strvec_push(&cp
.args
, clone_data
->url
);
1708 strvec_push(&cp
.args
, clone_data
->path
);
1711 prepare_submodule_repo_env(&cp
.env
);
1714 if(run_command(&cp
))
1715 die(_("clone of '%s' into submodule path '%s' failed"),
1716 clone_data
->url
, clone_data
->path
);
1718 if (clone_data
->require_init
&& !access(clone_data
->path
, X_OK
) &&
1719 !is_empty_dir(clone_data
->path
))
1720 die(_("directory not empty: '%s'"), clone_data
->path
);
1721 if (safe_create_leading_directories_const(clone_data
->path
) < 0)
1722 die(_("could not create directory '%s'"), clone_data
->path
);
1723 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1724 unlink_or_warn(sb
.buf
);
1728 connect_work_tree_and_git_dir(clone_data
->path
, sm_gitdir
, 0);
1730 p
= git_pathdup_submodule(clone_data
->path
, "config");
1732 die(_("could not get submodule directory for '%s'"), clone_data
->path
);
1734 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1735 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1737 git_config_set_in_file(p
, "submodule.alternateLocation",
1739 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1741 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1745 free(error_strategy
);
1747 strbuf_release(&sb
);
1753 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1755 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1756 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1757 struct list_objects_filter_options filter_options
;
1759 struct option module_clone_options
[] = {
1760 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1762 N_("alternative anchor for relative paths")),
1763 OPT_STRING(0, "path", &clone_data
.path
,
1765 N_("where the new submodule will be cloned to")),
1766 OPT_STRING(0, "name", &clone_data
.name
,
1768 N_("name of the new submodule")),
1769 OPT_STRING(0, "url", &clone_data
.url
,
1771 N_("url where to clone the submodule from")),
1772 OPT_STRING_LIST(0, "reference", &clone_data
.reference
,
1774 N_("reference repository")),
1775 OPT_BOOL(0, "dissociate", &dissociate
,
1776 N_("use --reference only while cloning")),
1777 OPT_STRING(0, "depth", &clone_data
.depth
,
1779 N_("depth for shallow clones")),
1780 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1781 OPT_BOOL(0, "progress", &progress
,
1782 N_("force cloning progress")),
1783 OPT_BOOL(0, "require-init", &require_init
,
1784 N_("disallow cloning into non-empty directory")),
1785 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1786 N_("clone only one branch, HEAD or --branch")),
1787 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1791 const char *const git_submodule_helper_usage
[] = {
1792 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1793 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1794 "[--single-branch] [--filter <filter-spec>] "
1795 "--url <url> --path <path>"),
1799 memset(&filter_options
, 0, sizeof(filter_options
));
1800 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1801 git_submodule_helper_usage
, 0);
1803 clone_data
.dissociate
= !!dissociate
;
1804 clone_data
.quiet
= !!quiet
;
1805 clone_data
.progress
= !!progress
;
1806 clone_data
.require_init
= !!require_init
;
1807 clone_data
.filter_options
= &filter_options
;
1809 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1810 usage_with_options(git_submodule_helper_usage
,
1811 module_clone_options
);
1813 clone_submodule(&clone_data
);
1814 list_objects_filter_release(&filter_options
);
1818 static void determine_submodule_update_strategy(struct repository
*r
,
1822 struct submodule_update_strategy
*out
)
1824 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1828 key
= xstrfmt("submodule.%s.update", sub
->name
);
1831 if (parse_submodule_update_strategy(update
, out
) < 0)
1832 die(_("Invalid update mode '%s' for submodule path '%s'"),
1834 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1835 if (parse_submodule_update_strategy(val
, out
) < 0)
1836 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1838 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1839 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1840 BUG("how did we read update = !command from .gitmodules?");
1841 out
->type
= sub
->update_strategy
.type
;
1842 out
->command
= sub
->update_strategy
.command
;
1844 out
->type
= SM_UPDATE_CHECKOUT
;
1847 (out
->type
== SM_UPDATE_MERGE
||
1848 out
->type
== SM_UPDATE_REBASE
||
1849 out
->type
== SM_UPDATE_NONE
))
1850 out
->type
= SM_UPDATE_CHECKOUT
;
1855 struct update_clone_data
{
1856 const struct submodule
*sub
;
1857 struct object_id oid
;
1858 unsigned just_cloned
;
1861 struct submodule_update_clone
{
1862 /* index into 'update_data.list', the list of submodules to look into for cloning */
1865 /* configuration parameters which are passed on to the children */
1866 struct update_data
*update_data
;
1868 /* to be consumed by update_submodule() */
1869 struct update_clone_data
*update_clone
;
1870 int update_clone_nr
; int update_clone_alloc
;
1872 /* If we want to stop as fast as possible and return an error */
1873 unsigned quickstop
: 1;
1875 /* failed clones to be retried again */
1876 const struct cache_entry
**failed_clones
;
1877 int failed_clones_nr
, failed_clones_alloc
;
1879 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1881 struct update_data
{
1883 const char *recursive_prefix
;
1884 const char *displaypath
;
1885 const char *update_default
;
1886 struct object_id suboid
;
1887 struct string_list references
;
1888 struct submodule_update_strategy update_strategy
;
1889 struct list_objects_filter_options
*filter_options
;
1890 struct module_list list
;
1894 int recommend_shallow
;
1895 unsigned int require_init
;
1898 unsigned int nofetch
;
1899 unsigned int remote
;
1900 unsigned int progress
;
1901 unsigned int dissociate
;
1903 unsigned int warn_if_uninitialized
;
1904 unsigned int recursive
;
1906 /* copied over from update_clone_data */
1907 struct object_id oid
;
1908 unsigned int just_cloned
;
1909 const char *sm_path
;
1911 #define UPDATE_DATA_INIT { \
1912 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1913 .list = MODULE_LIST_INIT, \
1914 .recommend_shallow = -1, \
1915 .references = STRING_LIST_INIT_DUP, \
1916 .single_branch = -1, \
1920 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1921 struct strbuf
*out
, const char *displaypath
)
1924 * Only mention uninitialized submodules when their
1925 * paths have been specified.
1927 if (suc
->update_data
->warn_if_uninitialized
) {
1929 _("Submodule path '%s' not initialized"),
1931 strbuf_addch(out
, '\n');
1933 _("Maybe you want to use 'update --init'?"));
1934 strbuf_addch(out
, '\n');
1939 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1940 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1942 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1943 struct child_process
*child
,
1944 struct submodule_update_clone
*suc
,
1947 const struct submodule
*sub
= NULL
;
1948 const char *url
= NULL
;
1949 const char *update_string
;
1950 enum submodule_update_type update_type
;
1952 struct strbuf displaypath_sb
= STRBUF_INIT
;
1953 struct strbuf sb
= STRBUF_INIT
;
1954 const char *displaypath
= NULL
;
1955 int needs_cloning
= 0;
1956 int need_free_url
= 0;
1959 if (suc
->update_data
->recursive_prefix
)
1960 strbuf_addf(&sb
, "%s/%s", suc
->update_data
->recursive_prefix
, ce
->name
);
1962 strbuf_addstr(&sb
, ce
->name
);
1963 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
1964 strbuf_addch(out
, '\n');
1968 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
1970 if (suc
->update_data
->recursive_prefix
)
1971 displaypath
= relative_path(suc
->update_data
->recursive_prefix
,
1972 ce
->name
, &displaypath_sb
);
1974 displaypath
= ce
->name
;
1977 next_submodule_warn_missing(suc
, out
, displaypath
);
1981 key
= xstrfmt("submodule.%s.update", sub
->name
);
1982 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
1983 update_type
= parse_submodule_update_type(update_string
);
1985 update_type
= sub
->update_strategy
.type
;
1989 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
1990 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
1991 && update_type
== SM_UPDATE_NONE
)) {
1992 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
1993 strbuf_addch(out
, '\n');
1997 /* Check if the submodule has been initialized. */
1998 if (!is_submodule_active(the_repository
, ce
->name
)) {
1999 next_submodule_warn_missing(suc
, out
, displaypath
);
2004 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2005 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2006 if (starts_with_dot_slash(sub
->url
) ||
2007 starts_with_dot_dot_slash(sub
->url
)) {
2008 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2015 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2016 needs_cloning
= !file_exists(sb
.buf
);
2018 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2019 suc
->update_clone_alloc
);
2020 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2021 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2022 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2023 suc
->update_clone_nr
++;
2029 child
->no_stdin
= 1;
2030 child
->stdout_to_stderr
= 1;
2032 strvec_push(&child
->args
, "submodule--helper");
2033 strvec_push(&child
->args
, "clone");
2034 if (suc
->update_data
->progress
)
2035 strvec_push(&child
->args
, "--progress");
2036 if (suc
->update_data
->quiet
)
2037 strvec_push(&child
->args
, "--quiet");
2038 if (suc
->update_data
->prefix
)
2039 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2040 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2041 strvec_push(&child
->args
, "--depth=1");
2042 else if (suc
->update_data
->depth
)
2043 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2044 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2045 strvec_pushf(&child
->args
, "--filter=%s",
2046 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2047 if (suc
->update_data
->require_init
)
2048 strvec_push(&child
->args
, "--require-init");
2049 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2050 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2051 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2052 if (suc
->update_data
->references
.nr
) {
2053 struct string_list_item
*item
;
2054 for_each_string_list_item(item
, &suc
->update_data
->references
)
2055 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2057 if (suc
->update_data
->dissociate
)
2058 strvec_push(&child
->args
, "--dissociate");
2059 if (suc
->update_data
->single_branch
>= 0)
2060 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2062 "--no-single-branch");
2065 strbuf_release(&displaypath_sb
);
2066 strbuf_release(&sb
);
2070 return needs_cloning
;
2073 static int update_clone_get_next_task(struct child_process
*child
,
2078 struct submodule_update_clone
*suc
= suc_cb
;
2079 const struct cache_entry
*ce
;
2082 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2083 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2084 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2085 int *p
= xmalloc(sizeof(*p
));
2094 * The loop above tried cloning each submodule once, now try the
2095 * stragglers again, which we can imagine as an extension of the
2098 index
= suc
->current
- suc
->update_data
->list
.nr
;
2099 if (index
< suc
->failed_clones_nr
) {
2101 ce
= suc
->failed_clones
[index
];
2102 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2104 strbuf_addstr(err
, "BUG: submodule considered for "
2105 "cloning, doesn't need cloning "
2109 p
= xmalloc(sizeof(*p
));
2119 static int update_clone_start_failure(struct strbuf
*err
,
2123 struct submodule_update_clone
*suc
= suc_cb
;
2128 static int update_clone_task_finished(int result
,
2133 const struct cache_entry
*ce
;
2134 struct submodule_update_clone
*suc
= suc_cb
;
2136 int *idxP
= idx_task_cb
;
2143 if (idx
< suc
->update_data
->list
.nr
) {
2144 ce
= suc
->update_data
->list
.entries
[idx
];
2145 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2147 strbuf_addch(err
, '\n');
2148 ALLOC_GROW(suc
->failed_clones
,
2149 suc
->failed_clones_nr
+ 1,
2150 suc
->failed_clones_alloc
);
2151 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2154 idx
-= suc
->update_data
->list
.nr
;
2155 ce
= suc
->failed_clones
[idx
];
2156 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2158 strbuf_addch(err
, '\n');
2166 static int git_update_clone_config(const char *var
, const char *value
,
2170 if (!strcmp(var
, "submodule.fetchjobs"))
2171 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2175 static int is_tip_reachable(const char *path
, struct object_id
*oid
)
2177 struct child_process cp
= CHILD_PROCESS_INIT
;
2178 struct strbuf rev
= STRBUF_INIT
;
2179 char *hex
= oid_to_hex(oid
);
2182 cp
.dir
= xstrdup(path
);
2184 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2186 prepare_submodule_repo_env(&cp
.env
);
2188 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2194 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
, struct object_id
*oid
)
2196 struct child_process cp
= CHILD_PROCESS_INIT
;
2198 prepare_submodule_repo_env(&cp
.env
);
2200 cp
.dir
= xstrdup(module_path
);
2202 strvec_push(&cp
.args
, "fetch");
2204 strvec_push(&cp
.args
, "--quiet");
2206 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2208 char *hex
= oid_to_hex(oid
);
2209 char *remote
= get_default_remote();
2210 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2214 return run_command(&cp
);
2217 static int run_update_command(struct update_data
*ud
, int subforce
)
2219 struct child_process cp
= CHILD_PROCESS_INIT
;
2220 char *oid
= oid_to_hex(&ud
->oid
);
2221 int must_die_on_failure
= 0;
2223 switch (ud
->update_strategy
.type
) {
2224 case SM_UPDATE_CHECKOUT
:
2226 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2228 strvec_push(&cp
.args
, "-f");
2230 case SM_UPDATE_REBASE
:
2232 strvec_push(&cp
.args
, "rebase");
2234 strvec_push(&cp
.args
, "--quiet");
2235 must_die_on_failure
= 1;
2237 case SM_UPDATE_MERGE
:
2239 strvec_push(&cp
.args
, "merge");
2241 strvec_push(&cp
.args
, "--quiet");
2242 must_die_on_failure
= 1;
2244 case SM_UPDATE_COMMAND
:
2246 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2247 must_die_on_failure
= 1;
2250 BUG("unexpected update strategy type: %s",
2251 submodule_strategy_to_string(&ud
->update_strategy
));
2253 strvec_push(&cp
.args
, oid
);
2255 cp
.dir
= xstrdup(ud
->sm_path
);
2256 prepare_submodule_repo_env(&cp
.env
);
2257 if (run_command(&cp
)) {
2258 switch (ud
->update_strategy
.type
) {
2259 case SM_UPDATE_CHECKOUT
:
2260 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2261 oid
, ud
->displaypath
);
2263 case SM_UPDATE_REBASE
:
2264 die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2265 oid
, ud
->displaypath
);
2267 case SM_UPDATE_MERGE
:
2268 die_message(_("Unable to merge '%s' in submodule path '%s'"),
2269 oid
, ud
->displaypath
);
2271 case SM_UPDATE_COMMAND
:
2272 die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2273 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2276 BUG("unexpected update strategy type: %s",
2277 submodule_strategy_to_string(&ud
->update_strategy
));
2279 if (must_die_on_failure
)
2282 /* the command failed, but update must continue */
2289 switch (ud
->update_strategy
.type
) {
2290 case SM_UPDATE_CHECKOUT
:
2291 printf(_("Submodule path '%s': checked out '%s'\n"),
2292 ud
->displaypath
, oid
);
2294 case SM_UPDATE_REBASE
:
2295 printf(_("Submodule path '%s': rebased into '%s'\n"),
2296 ud
->displaypath
, oid
);
2298 case SM_UPDATE_MERGE
:
2299 printf(_("Submodule path '%s': merged in '%s'\n"),
2300 ud
->displaypath
, oid
);
2302 case SM_UPDATE_COMMAND
:
2303 printf(_("Submodule path '%s': '%s %s'\n"),
2304 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2307 BUG("unexpected update strategy type: %s",
2308 submodule_strategy_to_string(&ud
->update_strategy
));
2314 static int run_update_procedure(struct update_data
*ud
)
2316 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2320 * Run fetch only if `oid` isn't present or it
2321 * is not reachable from a ref.
2323 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2324 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2327 _("Unable to fetch in submodule path '%s'; "
2328 "trying to directly fetch %s:"),
2329 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2331 * Now we tried the usual fetch, but `oid` may
2332 * not be reachable from any of the refs.
2334 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2335 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2336 die(_("Fetched in submodule path '%s', but it did not "
2337 "contain %s. Direct fetching of that commit failed."),
2338 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2341 return run_update_command(ud
, subforce
);
2344 static const char *remote_submodule_branch(const char *path
)
2346 const struct submodule
*sub
;
2347 const char *branch
= NULL
;
2350 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2354 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2355 if (repo_config_get_string_tmp(the_repository
, key
, &branch
))
2356 branch
= sub
->branch
;
2362 if (!strcmp(branch
, ".")) {
2363 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2366 die(_("No such ref: %s"), "HEAD");
2369 if (!strcmp(refname
, "HEAD"))
2370 die(_("Submodule (%s) branch configured to inherit "
2371 "branch from superproject, but the superproject "
2372 "is not on any branch"), sub
->name
);
2374 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2375 die(_("Expecting a full ref name, got %s"), refname
);
2382 static void ensure_core_worktree(const char *path
)
2385 struct repository subrepo
;
2387 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2388 die(_("could not get a repository handle for submodule '%s'"), path
);
2390 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2391 char *cfg_file
, *abs_path
;
2392 const char *rel_path
;
2393 struct strbuf sb
= STRBUF_INIT
;
2395 cfg_file
= repo_git_path(&subrepo
, "config");
2397 abs_path
= absolute_pathdup(path
);
2398 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2400 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2404 strbuf_release(&sb
);
2408 static void update_data_to_args(struct update_data
*update_data
, struct strvec
*args
)
2410 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2411 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2412 if (update_data
->recursive_prefix
)
2413 strvec_pushl(args
, "--recursive-prefix",
2414 update_data
->recursive_prefix
, NULL
);
2415 if (update_data
->quiet
)
2416 strvec_push(args
, "--quiet");
2417 if (update_data
->force
)
2418 strvec_push(args
, "--force");
2419 if (update_data
->init
)
2420 strvec_push(args
, "--init");
2421 if (update_data
->remote
)
2422 strvec_push(args
, "--remote");
2423 if (update_data
->nofetch
)
2424 strvec_push(args
, "--no-fetch");
2425 if (update_data
->dissociate
)
2426 strvec_push(args
, "--dissociate");
2427 if (update_data
->progress
)
2428 strvec_push(args
, "--progress");
2429 if (update_data
->require_init
)
2430 strvec_push(args
, "--require-init");
2431 if (update_data
->depth
)
2432 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2433 if (update_data
->update_default
)
2434 strvec_pushl(args
, "--update", update_data
->update_default
, NULL
);
2435 if (update_data
->references
.nr
) {
2436 struct string_list_item
*item
;
2437 for_each_string_list_item(item
, &update_data
->references
)
2438 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2440 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2441 strvec_pushf(args
, "--filter=%s",
2442 expand_list_objects_filter_spec(
2443 update_data
->filter_options
));
2444 if (update_data
->recommend_shallow
== 0)
2445 strvec_push(args
, "--no-recommend-shallow");
2446 else if (update_data
->recommend_shallow
== 1)
2447 strvec_push(args
, "--recommend-shallow");
2448 if (update_data
->single_branch
>= 0)
2449 strvec_push(args
, update_data
->single_branch
?
2451 "--no-single-branch");
2454 static int update_submodule(struct update_data
*update_data
)
2456 char *prefixed_path
;
2458 ensure_core_worktree(update_data
->sm_path
);
2460 if (update_data
->recursive_prefix
)
2461 prefixed_path
= xstrfmt("%s%s", update_data
->recursive_prefix
,
2462 update_data
->sm_path
);
2464 prefixed_path
= xstrdup(update_data
->sm_path
);
2466 update_data
->displaypath
= get_submodule_displaypath(prefixed_path
,
2467 update_data
->prefix
);
2468 free(prefixed_path
);
2470 determine_submodule_update_strategy(the_repository
, update_data
->just_cloned
,
2471 update_data
->sm_path
, update_data
->update_default
,
2472 &update_data
->update_strategy
);
2474 if (update_data
->just_cloned
)
2475 oidcpy(&update_data
->suboid
, null_oid());
2476 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2477 die(_("Unable to find current revision in submodule path '%s'"),
2478 update_data
->displaypath
);
2480 if (update_data
->remote
) {
2481 char *remote_name
= get_default_remote_submodule(update_data
->sm_path
);
2482 const char *branch
= remote_submodule_branch(update_data
->sm_path
);
2483 char *remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2485 if (!update_data
->nofetch
) {
2486 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2488 die(_("Unable to fetch in submodule path '%s'"),
2489 update_data
->sm_path
);
2492 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2493 die(_("Unable to find %s revision in submodule path '%s'"),
2494 remote_ref
, update_data
->sm_path
);
2499 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
)
2500 if (run_update_procedure(update_data
))
2503 if (update_data
->recursive
) {
2504 struct child_process cp
= CHILD_PROCESS_INIT
;
2505 struct update_data next
= *update_data
;
2508 if (update_data
->recursive_prefix
)
2509 prefixed_path
= xstrfmt("%s%s/", update_data
->recursive_prefix
,
2510 update_data
->sm_path
);
2512 prefixed_path
= xstrfmt("%s/", update_data
->sm_path
);
2514 next
.recursive_prefix
= get_submodule_displaypath(prefixed_path
,
2515 update_data
->prefix
);
2517 oidcpy(&next
.oid
, null_oid());
2518 oidcpy(&next
.suboid
, null_oid());
2520 cp
.dir
= update_data
->sm_path
;
2522 prepare_submodule_repo_env(&cp
.env
);
2523 update_data_to_args(&next
, &cp
.args
);
2525 /* die() if child process die()'d */
2526 res
= run_command(&cp
);
2529 die_message(_("Failed to recurse into submodule path '%s'"),
2530 update_data
->displaypath
);
2540 static int update_submodules(struct update_data
*update_data
)
2543 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2545 suc
.update_data
= update_data
;
2546 run_processes_parallel_tr2(suc
.update_data
->max_jobs
, update_clone_get_next_task
,
2547 update_clone_start_failure
,
2548 update_clone_task_finished
, &suc
, "submodule",
2552 * We saved the output and put it out all at once now.
2554 * - the listener does not have to interleave their (checkout)
2555 * work with our fetching. The writes involved in a
2556 * checkout involve more straightforward sequential I/O.
2557 * - the listener can avoid doing any work if fetching failed.
2559 if (suc
.quickstop
) {
2564 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2565 struct update_clone_data ucd
= suc
.update_clone
[i
];
2567 oidcpy(&update_data
->oid
, &ucd
.oid
);
2568 update_data
->just_cloned
= ucd
.just_cloned
;
2569 update_data
->sm_path
= ucd
.sub
->path
;
2571 if (update_submodule(update_data
))
2576 string_list_clear(&update_data
->references
, 0);
2580 static int module_update(int argc
, const char **argv
, const char *prefix
)
2582 struct pathspec pathspec
;
2583 struct update_data opt
= UPDATE_DATA_INIT
;
2584 struct list_objects_filter_options filter_options
;
2587 struct option module_update_options
[] = {
2588 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2589 OPT_BOOL(0, "init", &opt
.init
,
2590 N_("initialize uninitialized submodules before update")),
2591 OPT_BOOL(0, "remote", &opt
.remote
,
2592 N_("use SHA-1 of submodule's remote tracking branch")),
2593 OPT_BOOL(0, "recursive", &opt
.recursive
,
2594 N_("traverse submodules recursively")),
2595 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2596 N_("don't fetch new objects from the remote site")),
2597 OPT_STRING(0, "prefix", &opt
.prefix
,
2599 N_("path into the working tree")),
2600 OPT_STRING(0, "recursive-prefix", &opt
.recursive_prefix
,
2602 N_("path into the working tree, across nested "
2603 "submodule boundaries")),
2604 OPT_STRING(0, "update", &opt
.update_default
,
2606 N_("rebase, merge, checkout or none")),
2607 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2608 N_("reference repository")),
2609 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2610 N_("use --reference only while cloning")),
2611 OPT_INTEGER(0, "depth", &opt
.depth
,
2612 N_("create a shallow clone truncated to the "
2613 "specified number of revisions")),
2614 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2615 N_("parallel jobs")),
2616 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2617 N_("whether the initial clone should follow the shallow recommendation")),
2618 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2619 OPT_BOOL(0, "progress", &opt
.progress
,
2620 N_("force cloning progress")),
2621 OPT_BOOL(0, "require-init", &opt
.require_init
,
2622 N_("disallow cloning into non-empty directory")),
2623 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2624 N_("clone only one branch, HEAD or --branch")),
2625 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2629 const char *const git_submodule_helper_usage
[] = {
2630 N_("git submodule [--quiet] update"
2631 " [--init [--filter=<filter-spec>]] [--remote]"
2632 " [-N|--no-fetch] [-f|--force]"
2633 " [--checkout|--merge|--rebase]"
2634 " [--[no-]recommend-shallow] [--reference <repository>]"
2635 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2639 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2640 git_config(git_update_clone_config
, &opt
.max_jobs
);
2642 memset(&filter_options
, 0, sizeof(filter_options
));
2643 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2644 git_submodule_helper_usage
, 0);
2646 if (filter_options
.choice
&& !opt
.init
) {
2647 usage_with_options(git_submodule_helper_usage
,
2648 module_update_options
);
2651 opt
.filter_options
= &filter_options
;
2653 if (opt
.update_default
)
2654 if (parse_submodule_update_strategy(opt
.update_default
,
2655 &opt
.update_strategy
) < 0)
2656 die(_("bad value for update parameter"));
2658 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2659 list_objects_filter_release(&filter_options
);
2664 opt
.warn_if_uninitialized
= 1;
2667 struct module_list list
= MODULE_LIST_INIT
;
2668 struct init_cb info
= INIT_CB_INIT
;
2670 if (module_list_compute(argc
, argv
, opt
.prefix
,
2671 &pathspec
, &list
) < 0)
2675 * If there are no path args and submodule.active is set then,
2676 * by default, only initialize 'active' modules.
2678 if (!argc
&& git_config_get_value_multi("submodule.active"))
2679 module_list_active(&list
);
2681 info
.prefix
= opt
.prefix
;
2682 info
.superprefix
= opt
.recursive_prefix
;
2684 info
.flags
|= OPT_QUIET
;
2686 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2689 ret
= update_submodules(&opt
);
2690 list_objects_filter_release(&filter_options
);
2694 static int push_check(int argc
, const char **argv
, const char *prefix
)
2696 struct remote
*remote
;
2697 const char *superproject_head
;
2699 int detached_head
= 0;
2700 struct object_id head_oid
;
2703 die("submodule--helper push-check requires at least 2 arguments");
2706 * superproject's resolved head ref.
2707 * if HEAD then the superproject is in a detached head state, otherwise
2708 * it will be the resolved head ref.
2710 superproject_head
= argv
[1];
2713 /* Get the submodule's head ref and determine if it is detached */
2714 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2716 die(_("Failed to resolve HEAD as a valid ref."));
2717 if (!strcmp(head
, "HEAD"))
2721 * The remote must be configured.
2722 * This is to avoid pushing to the exact same URL as the parent.
2724 remote
= pushremote_get(argv
[1]);
2725 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2726 die("remote '%s' not configured", argv
[1]);
2728 /* Check the refspec */
2731 struct ref
*local_refs
= get_local_heads();
2732 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2734 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2736 for (i
= 0; i
< refspec
.nr
; i
++) {
2737 const struct refspec_item
*rs
= &refspec
.items
[i
];
2739 if (rs
->pattern
|| rs
->matching
)
2742 /* LHS must match a single ref */
2743 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2748 * If LHS matches 'HEAD' then we need to ensure
2749 * that it matches the same named branch
2750 * checked out in the superproject.
2752 if (!strcmp(rs
->src
, "HEAD")) {
2753 if (!detached_head
&&
2754 !strcmp(head
, superproject_head
))
2756 die("HEAD does not match the named branch in the superproject");
2760 die("src refspec '%s' must name a ref",
2764 refspec_clear(&refspec
);
2771 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2774 struct pathspec pathspec
;
2775 struct module_list list
= MODULE_LIST_INIT
;
2776 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
2778 struct option embed_gitdir_options
[] = {
2779 OPT_STRING(0, "prefix", &prefix
,
2781 N_("path into the working tree")),
2782 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
2783 ABSORB_GITDIR_RECURSE_SUBMODULES
),
2787 const char *const git_submodule_helper_usage
[] = {
2788 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2792 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2793 git_submodule_helper_usage
, 0);
2795 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
2798 for (i
= 0; i
< list
.nr
; i
++)
2799 absorb_git_dir_into_superproject(list
.entries
[i
]->name
, flags
);
2804 static int is_active(int argc
, const char **argv
, const char *prefix
)
2807 die("submodule--helper is-active takes exactly 1 argument");
2809 return !is_submodule_active(the_repository
, argv
[1]);
2813 * Exit non-zero if any of the submodule names given on the command line is
2814 * invalid. If no names are given, filter stdin to print only valid names
2815 * (which is primarily intended for testing).
2817 static int check_name(int argc
, const char **argv
, const char *prefix
)
2821 if (check_submodule_name(*argv
) < 0)
2825 struct strbuf buf
= STRBUF_INIT
;
2826 while (strbuf_getline(&buf
, stdin
) != EOF
) {
2827 if (!check_submodule_name(buf
.buf
))
2828 printf("%s\n", buf
.buf
);
2830 strbuf_release(&buf
);
2835 static int module_config(int argc
, const char **argv
, const char *prefix
)
2838 CHECK_WRITEABLE
= 1,
2842 struct option module_config_options
[] = {
2843 OPT_CMDMODE(0, "check-writeable", &command
,
2844 N_("check if it is safe to write to the .gitmodules file"),
2846 OPT_CMDMODE(0, "unset", &command
,
2847 N_("unset the config in the .gitmodules file"),
2851 const char *const git_submodule_helper_usage
[] = {
2852 N_("git submodule--helper config <name> [<value>]"),
2853 N_("git submodule--helper config --unset <name>"),
2854 "git submodule--helper config --check-writeable",
2858 argc
= parse_options(argc
, argv
, prefix
, module_config_options
,
2859 git_submodule_helper_usage
, PARSE_OPT_KEEP_ARGV0
);
2861 if (argc
== 1 && command
== CHECK_WRITEABLE
)
2862 return is_writing_gitmodules_ok() ? 0 : -1;
2864 /* Equivalent to ACTION_GET in builtin/config.c */
2865 if (argc
== 2 && command
!= DO_UNSET
)
2866 return print_config_from_gitmodules(the_repository
, argv
[1]);
2868 /* Equivalent to ACTION_SET in builtin/config.c */
2869 if (argc
== 3 || (argc
== 2 && command
== DO_UNSET
)) {
2870 const char *value
= (argc
== 3) ? argv
[2] : NULL
;
2872 if (!is_writing_gitmodules_ok())
2873 die(_("please make sure that the .gitmodules file is in the working tree"));
2875 return config_set_in_gitmodules_file_gently(argv
[1], value
);
2878 usage_with_options(git_submodule_helper_usage
, module_config_options
);
2881 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2888 struct option options
[] = {
2889 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2892 const char *const usage
[] = {
2893 N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
2897 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2899 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2900 usage_with_options(usage
, options
);
2902 config_name
= xstrfmt("submodule.%s.url", path
);
2904 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2905 sync_submodule(path
, prefix
, quiet
? OPT_QUIET
: 0);
2912 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2914 int opt_default
= 0, ret
;
2915 const char *opt_branch
= NULL
;
2920 * We accept the `quiet` option for uniformity across subcommands,
2921 * though there is nothing to make less verbose in this subcommand.
2923 struct option options
[] = {
2924 OPT_NOOP_NOARG('q', "quiet"),
2925 OPT_BOOL('d', "default", &opt_default
,
2926 N_("set the default tracking branch to master")),
2927 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2928 N_("set the default tracking branch")),
2931 const char *const usage
[] = {
2932 N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
2933 N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2937 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2939 if (!opt_branch
&& !opt_default
)
2940 die(_("--branch or --default required"));
2942 if (opt_branch
&& opt_default
)
2943 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2945 if (argc
!= 1 || !(path
= argv
[0]))
2946 usage_with_options(usage
, options
);
2948 config_name
= xstrfmt("submodule.%s.branch", path
);
2949 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2955 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
2957 enum branch_track track
;
2958 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
2960 struct option options
[] = {
2961 OPT__QUIET(&quiet
, N_("print only error messages")),
2962 OPT__FORCE(&force
, N_("force creation"), 0),
2963 OPT_BOOL(0, "create-reflog", &reflog
,
2964 N_("create the branch's reflog")),
2965 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
2966 N_("set branch tracking configuration"),
2968 parse_opt_tracking_mode
),
2969 OPT__DRY_RUN(&dry_run
,
2970 N_("show whether the branch would be created")),
2973 const char *const usage
[] = {
2974 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2978 git_config(git_default_config
, NULL
);
2979 track
= git_branch_track
;
2980 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2983 usage_with_options(usage
, options
);
2985 if (!quiet
&& !dry_run
)
2986 printf_ln(_("creating branch '%s'"), argv
[0]);
2988 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
2989 force
, reflog
, quiet
, track
, dry_run
);
2996 const char *reference_path
;
2998 const char *sm_name
;
3000 const char *realrepo
;
3002 unsigned int force
: 1;
3003 unsigned int quiet
: 1;
3004 unsigned int progress
: 1;
3005 unsigned int dissociate
: 1;
3007 #define ADD_DATA_INIT { .depth = -1 }
3009 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3011 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3012 struct strbuf sb_remote_out
= STRBUF_INIT
;
3014 cp_remote
.git_cmd
= 1;
3015 strvec_pushf(&cp_remote
.env
,
3016 "GIT_DIR=%s", git_dir_path
);
3017 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3018 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3019 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3021 char *line
= sb_remote_out
.buf
;
3022 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3023 size_t len
= next_line
- line
;
3024 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3025 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3026 line
= next_line
+ 1;
3030 strbuf_release(&sb_remote_out
);
3033 static int add_submodule(const struct add_data
*add_data
)
3035 char *submod_gitdir_path
;
3036 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3038 /* perhaps the path already exists and is already a git repo, else clone it */
3039 if (is_directory(add_data
->sm_path
)) {
3040 struct strbuf sm_path
= STRBUF_INIT
;
3041 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3042 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3043 if (is_nonbare_repository_dir(&sm_path
))
3044 printf(_("Adding existing repo at '%s' to the index\n"),
3047 die(_("'%s' already exists and is not a valid git repo"),
3049 strbuf_release(&sm_path
);
3050 free(submod_gitdir_path
);
3052 struct child_process cp
= CHILD_PROCESS_INIT
;
3053 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3055 if (is_directory(submod_gitdir_path
)) {
3056 if (!add_data
->force
) {
3057 struct strbuf msg
= STRBUF_INIT
;
3060 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3061 "locally with remote(s):\n"),
3064 append_fetch_remotes(&msg
, submod_gitdir_path
);
3065 free(submod_gitdir_path
);
3067 strbuf_addf(&msg
, _("If you want to reuse this local git "
3068 "directory instead of cloning again from\n"
3070 "use the '--force' option. If the local git "
3071 "directory is not the correct repo\n"
3072 "or you are unsure what this means choose "
3073 "another name with the '--name' option."),
3074 add_data
->realrepo
);
3076 die_msg
= strbuf_detach(&msg
, NULL
);
3079 printf(_("Reactivating local git directory for "
3080 "submodule '%s'\n"), add_data
->sm_name
);
3083 free(submod_gitdir_path
);
3085 clone_data
.prefix
= add_data
->prefix
;
3086 clone_data
.path
= add_data
->sm_path
;
3087 clone_data
.name
= add_data
->sm_name
;
3088 clone_data
.url
= add_data
->realrepo
;
3089 clone_data
.quiet
= add_data
->quiet
;
3090 clone_data
.progress
= add_data
->progress
;
3091 if (add_data
->reference_path
)
3092 string_list_append(&clone_data
.reference
,
3093 xstrdup(add_data
->reference_path
));
3094 clone_data
.dissociate
= add_data
->dissociate
;
3095 if (add_data
->depth
>= 0)
3096 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3098 if (clone_submodule(&clone_data
))
3101 prepare_submodule_repo_env(&cp
.env
);
3103 cp
.dir
= add_data
->sm_path
;
3105 * NOTE: we only get here if add_data->force is true, so
3106 * passing --force to checkout is reasonable.
3108 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3110 if (add_data
->branch
) {
3111 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3112 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3115 if (run_command(&cp
))
3116 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3121 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3126 if (!is_writing_gitmodules_ok())
3127 die(_("please make sure that the .gitmodules file is in the working tree"));
3129 key
= xstrfmt("submodule.%s.%s", name
, var
);
3130 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3136 static void configure_added_submodule(struct add_data
*add_data
)
3140 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3141 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3143 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3144 git_config_set_gently(key
, add_data
->realrepo
);
3147 add_submod
.git_cmd
= 1;
3148 strvec_pushl(&add_submod
.args
, "add",
3149 "--no-warn-embedded-repo", NULL
);
3150 if (add_data
->force
)
3151 strvec_push(&add_submod
.args
, "--force");
3152 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3154 if (run_command(&add_submod
))
3155 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3157 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3158 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3159 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3161 if (add_data
->branch
) {
3162 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3163 "branch", add_data
->branch
))
3164 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3167 add_gitmodules
.git_cmd
= 1;
3168 strvec_pushl(&add_gitmodules
.args
,
3169 "add", "--force", "--", ".gitmodules", NULL
);
3171 if (run_command(&add_gitmodules
))
3172 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3175 * NEEDSWORK: In a multi-working-tree world this needs to be
3176 * set in the per-worktree config.
3179 * NEEDSWORK: In the longer run, we need to get rid of this
3180 * pattern of querying "submodule.active" before calling
3181 * is_submodule_active(), since that function needs to find
3182 * out the value of "submodule.active" again anyway.
3184 if (!git_config_get_string("submodule.active", &val
) && val
) {
3186 * If the submodule being added isn't already covered by the
3187 * current configured pathspec, set the submodule's active flag
3189 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3190 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3191 git_config_set_gently(key
, "true");
3195 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3196 git_config_set_gently(key
, "true");
3201 static void die_on_index_match(const char *path
, int force
)
3204 const char *args
[] = { path
, NULL
};
3205 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3207 if (read_cache_preload(NULL
) < 0)
3208 die(_("index file corrupt"));
3212 char *ps_matched
= xcalloc(ps
.nr
, 1);
3214 /* TODO: audit for interaction with sparse-index. */
3215 ensure_full_index(&the_index
);
3218 * Since there is only one pathspec, we just need
3219 * need to check ps_matched[0] to know if a cache
3222 for (i
= 0; i
< active_nr
; i
++) {
3223 ce_path_match(&the_index
, active_cache
[i
], &ps
,
3226 if (ps_matched
[0]) {
3228 die(_("'%s' already exists in the index"),
3230 if (!S_ISGITLINK(active_cache
[i
]->ce_mode
))
3231 die(_("'%s' already exists in the index "
3232 "and is not a submodule"), path
);
3238 clear_pathspec(&ps
);
3241 static void die_on_repo_without_commits(const char *path
)
3243 struct strbuf sb
= STRBUF_INIT
;
3244 strbuf_addstr(&sb
, path
);
3245 if (is_nonbare_repository_dir(&sb
)) {
3246 struct object_id oid
;
3247 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3248 die(_("'%s' does not have a commit checked out"), path
);
3250 strbuf_release(&sb
);
3253 static int module_add(int argc
, const char **argv
, const char *prefix
)
3255 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3256 struct add_data add_data
= ADD_DATA_INIT
;
3257 char *to_free
= NULL
;
3259 struct option options
[] = {
3260 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3261 N_("branch of repository to add as submodule")),
3262 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3263 PARSE_OPT_NOCOMPLETE
),
3264 OPT__QUIET(&quiet
, N_("print only error messages")),
3265 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3266 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3267 N_("reference repository")),
3268 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3269 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3270 N_("sets the submodule's name to the given string "
3271 "instead of defaulting to its path")),
3272 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3276 const char *const usage
[] = {
3277 N_("git submodule--helper add [<options>] [--] <repository> [<path>]"),
3281 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3283 if (!is_writing_gitmodules_ok())
3284 die(_("please make sure that the .gitmodules file is in the working tree"));
3286 if (prefix
&& *prefix
&&
3287 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3288 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3290 if (argc
== 0 || argc
> 2)
3291 usage_with_options(usage
, options
);
3293 add_data
.repo
= argv
[0];
3295 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3297 add_data
.sm_path
= xstrdup(argv
[1]);
3299 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
))
3300 add_data
.sm_path
= xstrfmt("%s%s", prefix
, add_data
.sm_path
);
3302 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3303 starts_with_dot_slash(add_data
.repo
)) {
3305 die(_("Relative path can only be used from the toplevel "
3306 "of the working tree"));
3308 /* dereference source url relative to parent's url */
3309 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3310 add_data
.realrepo
= to_free
;
3311 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3312 add_data
.realrepo
= add_data
.repo
;
3314 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3320 * multiple //; leading ./; /./; /../;
3322 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3323 strip_dir_trailing_slashes(add_data
.sm_path
);
3325 die_on_index_match(add_data
.sm_path
, force
);
3326 die_on_repo_without_commits(add_data
.sm_path
);
3330 struct strbuf sb
= STRBUF_INIT
;
3331 struct child_process cp
= CHILD_PROCESS_INIT
;
3334 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3335 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3336 if ((exit_code
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3337 strbuf_complete_line(&sb
);
3338 fputs(sb
.buf
, stderr
);
3339 free(add_data
.sm_path
);
3342 strbuf_release(&sb
);
3345 if(!add_data
.sm_name
)
3346 add_data
.sm_name
= add_data
.sm_path
;
3348 if (check_submodule_name(add_data
.sm_name
))
3349 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3351 add_data
.prefix
= prefix
;
3352 add_data
.force
= !!force
;
3353 add_data
.quiet
= !!quiet
;
3354 add_data
.progress
= !!progress
;
3355 add_data
.dissociate
= !!dissociate
;
3357 if (add_submodule(&add_data
)) {
3358 free(add_data
.sm_path
);
3361 configure_added_submodule(&add_data
);
3362 free(add_data
.sm_path
);
3368 #define SUPPORT_SUPER_PREFIX (1<<0)
3372 int (*fn
)(int, const char **, const char *);
3376 static struct cmd_struct commands
[] = {
3377 {"list", module_list
, 0},
3378 {"name", module_name
, 0},
3379 {"clone", module_clone
, 0},
3380 {"add", module_add
, SUPPORT_SUPER_PREFIX
},
3381 {"update", module_update
, 0},
3382 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
3383 {"foreach", module_foreach
, SUPPORT_SUPER_PREFIX
},
3384 {"init", module_init
, SUPPORT_SUPER_PREFIX
},
3385 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
3386 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
3387 {"deinit", module_deinit
, 0},
3388 {"summary", module_summary
, SUPPORT_SUPER_PREFIX
},
3389 {"push-check", push_check
, 0},
3390 {"absorb-git-dirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
3391 {"is-active", is_active
, 0},
3392 {"check-name", check_name
, 0},
3393 {"config", module_config
, 0},
3394 {"set-url", module_set_url
, 0},
3395 {"set-branch", module_set_branch
, 0},
3396 {"create-branch", module_create_branch
, 0},
3399 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3402 if (argc
< 2 || !strcmp(argv
[1], "-h"))
3403 usage("git submodule--helper <command>");
3405 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
3406 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
3407 if (get_super_prefix() &&
3408 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
3409 die(_("%s doesn't support --super-prefix"),
3411 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
3415 die(_("'%s' is not a valid submodule--helper "
3416 "subcommand"), argv
[1]);