1 #define USE_THE_REPOSITORY_VARIABLE
5 #include "environment.h"
10 #include "parse-options.h"
14 #include "preload-index.h"
16 #include "read-cache.h"
18 #include "sparse-index.h"
19 #include "submodule.h"
20 #include "submodule-config.h"
21 #include "string-list.h"
22 #include "run-command.h"
29 #include "object-file.h"
30 #include "object-name.h"
34 #include "list-objects-filter-options.h"
35 #include "wildmatch.h"
38 #define OPT_QUIET (1 << 0)
39 #define OPT_CACHED (1 << 1)
40 #define OPT_RECURSIVE (1 << 2)
41 #define OPT_FORCE (1 << 3)
43 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
46 static char *get_default_remote(void)
48 return xstrdup(repo_default_remote(the_repository
));
51 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
53 char *remoteurl
, *resolved_url
;
54 char *remote
= get_default_remote();
55 struct strbuf remotesb
= STRBUF_INIT
;
57 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
58 if (repo_config_get_string(the_repository
, remotesb
.buf
, &remoteurl
)) {
60 warning(_("could not look up configuration '%s'. "
61 "Assuming this repository is its own "
62 "authoritative upstream."),
64 remoteurl
= xgetcwd();
66 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
70 strbuf_release(&remotesb
);
75 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
77 const struct submodule
*sub
;
78 struct repository subrepo
;
79 const char *remote_name
= NULL
;
82 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), module_path
);
83 if (sub
&& sub
->url
) {
84 url
= xstrdup(sub
->url
);
86 /* Possibly a url relative to parent */
87 if (starts_with_dot_dot_slash(url
) ||
88 starts_with_dot_slash(url
)) {
91 url
= resolve_relative_url(oldurl
, NULL
, 1);
96 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
97 null_oid(the_hash_algo
)) < 0)
98 return die_message(_("could not get a repository handle for submodule '%s'"),
101 /* Look up by URL first */
103 remote_name
= repo_remote_from_url(&subrepo
, url
);
105 remote_name
= repo_default_remote(&subrepo
);
107 *default_remote
= xstrdup(remote_name
);
109 repo_clear(&subrepo
);
115 /* the result should be freed by the caller. */
116 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
117 const char *super_prefix
)
119 if (prefix
&& super_prefix
) {
120 BUG("cannot have prefix '%s' and superprefix '%s'",
121 prefix
, super_prefix
);
123 struct strbuf sb
= STRBUF_INIT
;
124 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
127 } else if (super_prefix
) {
128 return xstrfmt("%s%s", super_prefix
, path
);
130 return xstrdup(path
);
134 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
136 struct strbuf sb
= STRBUF_INIT
;
139 static const char *describe_bare
[] = { NULL
};
141 static const char *describe_tags
[] = { "--tags", NULL
};
143 static const char *describe_contains
[] = { "--contains", NULL
};
145 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
147 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
149 describe_all_always
, NULL
};
151 for (d
= describe_argv
; *d
; d
++) {
152 struct child_process cp
= CHILD_PROCESS_INIT
;
153 prepare_submodule_repo_env(&cp
.env
);
158 strvec_push(&cp
.args
, "describe");
159 strvec_pushv(&cp
.args
, *d
);
160 strvec_push(&cp
.args
, object_id
);
162 if (!capture_command(&cp
, &sb
, 0)) {
163 strbuf_strip_suffix(&sb
, "\n");
164 return strbuf_detach(&sb
, NULL
);
173 const struct cache_entry
**entries
;
176 #define MODULE_LIST_INIT { 0 }
178 static void module_list_release(struct module_list
*ml
)
183 static int module_list_compute(const char **argv
,
185 struct pathspec
*pathspec
,
186 struct module_list
*list
)
189 char *ps_matched
= NULL
;
191 parse_pathspec(pathspec
, 0,
192 PATHSPEC_PREFER_FULL
,
196 ps_matched
= xcalloc(pathspec
->nr
, 1);
198 if (repo_read_index(the_repository
) < 0)
199 die(_("index file corrupt"));
201 for (size_t i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
202 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
204 if (!match_pathspec(the_repository
->index
, pathspec
, ce
->name
, ce_namelen(ce
),
206 !S_ISGITLINK(ce
->ce_mode
))
209 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
210 list
->entries
[list
->nr
++] = ce
;
211 while (i
+ 1 < the_repository
->index
->cache_nr
&&
212 !strcmp(ce
->name
, the_repository
->index
->cache
[i
+ 1]->name
))
214 * Skip entries with the same name in different stages
215 * to make sure an entry is returned only once.
220 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
228 static void module_list_active(struct module_list
*list
)
231 struct module_list active_modules
= MODULE_LIST_INIT
;
233 for (i
= 0; i
< list
->nr
; i
++) {
234 const struct cache_entry
*ce
= list
->entries
[i
];
236 if (!is_submodule_active(the_repository
, ce
->name
))
239 ALLOC_GROW(active_modules
.entries
,
240 active_modules
.nr
+ 1,
241 active_modules
.alloc
);
242 active_modules
.entries
[active_modules
.nr
++] = ce
;
245 module_list_release(list
);
246 *list
= active_modules
;
249 static char *get_up_path(const char *path
)
251 struct strbuf sb
= STRBUF_INIT
;
253 strbuf_addstrings(&sb
, "../", count_slashes(path
));
256 * Check if 'path' ends with slash or not
257 * for having the same output for dir/sub_dir
260 if (!is_dir_sep(path
[strlen(path
) - 1]))
261 strbuf_addstr(&sb
, "../");
263 return strbuf_detach(&sb
, NULL
);
266 static void for_each_listed_submodule(const struct module_list
*list
,
267 each_submodule_fn fn
, void *cb_data
)
271 for (i
= 0; i
< list
->nr
; i
++)
272 fn(list
->entries
[i
], cb_data
);
279 const char *super_prefix
;
283 #define FOREACH_CB_INIT { 0 }
285 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
288 struct foreach_cb
*info
= cb_data
;
289 const char *path
= list_item
->name
;
290 const struct object_id
*ce_oid
= &list_item
->oid
;
291 const struct submodule
*sub
;
292 struct child_process cp
= CHILD_PROCESS_INIT
;
295 if (validate_submodule_path(path
) < 0)
298 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
301 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
304 die(_("No url found for submodule path '%s' in .gitmodules"),
307 if (!is_submodule_populated_gently(path
, NULL
))
310 prepare_submodule_repo_env(&cp
.env
);
313 * For the purpose of executing <command> in the submodule,
314 * separate shell is used for the purpose of running the
321 * NEEDSWORK: the command currently has access to the variables $name,
322 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
323 * contains a single argument. This is done for maintaining a faithful
324 * translation from shell script.
326 if (info
->argc
== 1) {
327 char *toplevel
= xgetcwd();
328 struct strbuf sb
= STRBUF_INIT
;
330 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
331 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
332 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
333 strvec_pushf(&cp
.env
, "sha1=%s",
335 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
338 * Since the path variable was accessible from the script
339 * before porting, it is also made available after porting.
340 * The environment variable "PATH" has a very special purpose
341 * on windows. And since environment variables are
342 * case-insensitive in windows, it interferes with the
343 * existing PATH variable. Hence, to avoid that, we expose
344 * path via the args strvec and not via env.
346 sq_quote_buf(&sb
, path
);
347 strvec_pushf(&cp
.args
, "path=%s; %s",
348 sb
.buf
, info
->argv
[0]);
352 strvec_pushv(&cp
.args
, info
->argv
);
356 printf(_("Entering '%s'\n"), displaypath
);
359 if (run_command(&cp
))
360 die(_("run_command returned non-zero status for %s\n."),
363 child_process_clear(&cp
);
366 if (info
->recursive
) {
367 struct child_process cpr
= CHILD_PROCESS_INIT
;
371 prepare_submodule_repo_env(&cpr
.env
);
373 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
375 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
378 strvec_push(&cpr
.args
, "--quiet");
380 strvec_push(&cpr
.args
, "--");
381 strvec_pushv(&cpr
.args
, info
->argv
);
383 if (run_command(&cpr
))
384 die(_("run_command returned non-zero status while "
385 "recursing in the nested submodules of %s\n."),
393 static int module_foreach(int argc
, const char **argv
, const char *prefix
,
394 struct repository
*repo UNUSED
)
396 struct foreach_cb info
= FOREACH_CB_INIT
;
397 struct pathspec pathspec
= { 0 };
398 struct module_list list
= MODULE_LIST_INIT
;
399 struct option module_foreach_options
[] = {
400 OPT__SUPER_PREFIX(&info
.super_prefix
),
401 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
402 OPT_BOOL(0, "recursive", &info
.recursive
,
403 N_("recurse into nested submodules")),
406 const char *const git_submodule_helper_usage
[] = {
407 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
412 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
413 git_submodule_helper_usage
, 0);
415 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
420 info
.prefix
= prefix
;
422 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
426 module_list_release(&list
);
427 clear_pathspec(&pathspec
);
433 const char *super_prefix
;
436 #define INIT_CB_INIT { 0 }
438 static void init_submodule(const char *path
, const char *prefix
,
439 const char *super_prefix
,
442 const struct submodule
*sub
;
443 struct strbuf sb
= STRBUF_INIT
;
445 char *url
= NULL
, *displaypath
;
447 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
449 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
452 die(_("No url found for submodule path '%s' in .gitmodules"),
456 * NEEDSWORK: In a multi-working-tree world, this needs to be
457 * set in the per-worktree config.
459 * Set active flag for the submodule being initialized
461 if (!is_submodule_active(the_repository
, path
)) {
462 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
463 repo_config_set_gently(the_repository
, sb
.buf
, "true");
468 * Copy url setting when it is not set yet.
469 * To look up the url in .git/config, we must not fall back to
470 * .gitmodules, so look it up directly.
472 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
473 if (repo_config_get_string(the_repository
, sb
.buf
, &url
)) {
475 die(_("No url found for submodule path '%s' in .gitmodules"),
478 url
= xstrdup(sub
->url
);
480 /* Possibly a url relative to parent */
481 if (starts_with_dot_dot_slash(url
) ||
482 starts_with_dot_slash(url
)) {
485 url
= resolve_relative_url(oldurl
, NULL
, 0);
489 if (repo_config_set_gently(the_repository
, sb
.buf
, url
))
490 die(_("Failed to register url for submodule path '%s'"),
492 if (!(flags
& OPT_QUIET
))
494 _("Submodule '%s' (%s) registered for path '%s'\n"),
495 sub
->name
, url
, displaypath
);
499 /* Copy "update" setting when it is not set yet */
500 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
501 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &upd
) &&
502 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
503 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
504 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
508 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
511 if (repo_config_set_gently(the_repository
, sb
.buf
, upd
))
512 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
519 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
521 struct init_cb
*info
= cb_data
;
523 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
527 static int module_init(int argc
, const char **argv
, const char *prefix
,
528 struct repository
*repo UNUSED
)
530 struct init_cb info
= INIT_CB_INIT
;
531 struct pathspec pathspec
= { 0 };
532 struct module_list list
= MODULE_LIST_INIT
;
534 struct option module_init_options
[] = {
535 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
538 const char *const git_submodule_helper_usage
[] = {
539 N_("git submodule init [<options>] [<path>]"),
544 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
545 git_submodule_helper_usage
, 0);
547 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
551 * If there are no path args and submodule.active is set then,
552 * by default, only initialize 'active' modules.
554 if (!argc
&& !repo_config_get(the_repository
, "submodule.active"))
555 module_list_active(&list
);
557 info
.prefix
= prefix
;
559 info
.flags
|= OPT_QUIET
;
561 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
565 module_list_release(&list
);
566 clear_pathspec(&pathspec
);
572 const char *super_prefix
;
575 #define STATUS_CB_INIT { 0 }
577 static void print_status(unsigned int flags
, char state
, const char *path
,
578 const struct object_id
*oid
, const char *displaypath
)
580 if (flags
& OPT_QUIET
)
583 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
585 if (state
== ' ' || state
== '+') {
586 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
589 printf(" (%s)", name
);
596 static int handle_submodule_head_ref(const char *refname UNUSED
,
597 const char *referent UNUSED
,
598 const struct object_id
*oid
,
602 struct object_id
*output
= cb_data
;
610 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
611 unsigned int ce_flags
, const char *prefix
,
612 const char *super_prefix
, unsigned int flags
)
615 struct strvec diff_files_args
= STRVEC_INIT
;
616 struct rev_info rev
= REV_INFO_INIT
;
617 struct strbuf buf
= STRBUF_INIT
;
620 if (validate_submodule_path(path
) < 0)
623 if (!submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
))
624 die(_("no submodule mapping found in .gitmodules for path '%s'"),
627 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
629 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
630 print_status(flags
, 'U', path
, null_oid(the_hash_algo
), displaypath
);
634 strbuf_addf(&buf
, "%s/.git", path
);
635 git_dir
= read_gitfile(buf
.buf
);
639 if (!is_submodule_active(the_repository
, path
) ||
640 !is_git_directory(git_dir
)) {
641 print_status(flags
, '-', path
, ce_oid
, displaypath
);
642 strbuf_release(&buf
);
645 strbuf_release(&buf
);
647 strvec_pushl(&diff_files_args
, "diff-files",
648 "--ignore-submodules=dirty", "--quiet", "--",
651 repo_config(the_repository
, git_diff_basic_config
, NULL
);
653 repo_init_revisions(the_repository
, &rev
, NULL
);
655 setup_revisions_from_strvec(&diff_files_args
, &rev
, NULL
);
656 run_diff_files(&rev
, 0);
658 if (!diff_result_code(&rev
)) {
659 print_status(flags
, ' ', path
, ce_oid
,
661 } else if (!(flags
& OPT_CACHED
)) {
662 struct object_id oid
;
663 struct ref_store
*refs
= repo_get_submodule_ref_store(the_repository
,
667 print_status(flags
, '-', path
, ce_oid
, displaypath
);
670 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
671 die(_("could not resolve HEAD ref inside the "
672 "submodule '%s'"), path
);
674 print_status(flags
, '+', path
, &oid
, displaypath
);
676 print_status(flags
, '+', path
, ce_oid
, displaypath
);
679 if (flags
& OPT_RECURSIVE
) {
680 struct child_process cpr
= CHILD_PROCESS_INIT
;
685 prepare_submodule_repo_env(&cpr
.env
);
687 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
688 "--recursive", NULL
);
689 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
691 if (flags
& OPT_CACHED
)
692 strvec_push(&cpr
.args
, "--cached");
694 if (flags
& OPT_QUIET
)
695 strvec_push(&cpr
.args
, "--quiet");
697 res
= run_command(&cpr
);
698 if (res
== SIGPIPE
+ 128)
701 die(_("failed to recurse into submodule '%s'"), path
);
705 strvec_clear(&diff_files_args
);
707 release_revisions(&rev
);
710 static void status_submodule_cb(const struct cache_entry
*list_item
,
713 struct status_cb
*info
= cb_data
;
715 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
716 info
->prefix
, info
->super_prefix
, info
->flags
);
719 static int module_status(int argc
, const char **argv
, const char *prefix
,
720 struct repository
*repo UNUSED
)
722 struct status_cb info
= STATUS_CB_INIT
;
723 struct pathspec pathspec
= { 0 };
724 struct module_list list
= MODULE_LIST_INIT
;
726 struct option module_status_options
[] = {
727 OPT__SUPER_PREFIX(&info
.super_prefix
),
728 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
729 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
730 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
733 const char *const git_submodule_helper_usage
[] = {
734 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
739 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
740 git_submodule_helper_usage
, 0);
742 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
745 info
.prefix
= prefix
;
747 info
.flags
|= OPT_QUIET
;
749 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
753 module_list_release(&list
);
754 clear_pathspec(&pathspec
);
759 unsigned int mod_src
;
760 unsigned int mod_dst
;
761 struct object_id oid_src
;
762 struct object_id oid_dst
;
766 #define MODULE_CB_INIT { 0 }
768 static void module_cb_release(struct module_cb
*mcb
)
773 struct module_cb_list
{
774 struct module_cb
**entries
;
777 #define MODULE_CB_LIST_INIT { 0 }
779 static void module_cb_list_release(struct module_cb_list
*mcbl
)
783 for (i
= 0; i
< mcbl
->nr
; i
++) {
784 struct module_cb
*mcb
= mcbl
->entries
[i
];
786 module_cb_release(mcb
);
796 const char *super_prefix
;
797 unsigned int cached
: 1;
798 unsigned int for_status
: 1;
799 unsigned int files
: 1;
802 #define SUMMARY_CB_INIT { 0 }
809 static char *verify_submodule_committish(const char *sm_path
,
810 const char *committish
)
812 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
813 struct strbuf result
= STRBUF_INIT
;
815 cp_rev_parse
.git_cmd
= 1;
816 cp_rev_parse
.dir
= sm_path
;
817 prepare_submodule_repo_env(&cp_rev_parse
.env
);
818 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
819 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
820 strvec_push(&cp_rev_parse
.args
, "--");
822 if (capture_command(&cp_rev_parse
, &result
, 0))
825 strbuf_trim_trailing_newline(&result
);
826 return strbuf_detach(&result
, NULL
);
829 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
830 int total_commits
, const char *displaypath
,
831 const char *src_abbrev
, const char *dst_abbrev
,
834 if (p
->status
== 'T') {
835 if (S_ISGITLINK(p
->mod_dst
))
836 printf(_("* %s %s(blob)->%s(submodule)"),
837 displaypath
, src_abbrev
, dst_abbrev
);
839 printf(_("* %s %s(submodule)->%s(blob)"),
840 displaypath
, src_abbrev
, dst_abbrev
);
842 printf("* %s %s...%s",
843 displaypath
, src_abbrev
, dst_abbrev
);
846 if (total_commits
< 0)
849 printf(" (%d):\n", total_commits
);
852 printf(_("%s"), errmsg
);
853 } else if (total_commits
> 0) {
854 struct child_process cp_log
= CHILD_PROCESS_INIT
;
857 cp_log
.dir
= p
->sm_path
;
858 prepare_submodule_repo_env(&cp_log
.env
);
859 strvec_pushl(&cp_log
.args
, "log", NULL
);
861 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
862 if (info
->summary_limit
> 0)
863 strvec_pushf(&cp_log
.args
, "-%d",
864 info
->summary_limit
);
866 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
867 "--first-parent", NULL
);
868 strvec_pushf(&cp_log
.args
, "%s...%s",
869 src_abbrev
, dst_abbrev
);
870 } else if (S_ISGITLINK(p
->mod_dst
)) {
871 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
872 "-1", dst_abbrev
, NULL
);
874 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
875 "-1", src_abbrev
, NULL
);
877 run_command(&cp_log
);
882 static void generate_submodule_summary(struct summary_cb
*info
,
885 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
886 int missing_src
= 0, missing_dst
= 0;
887 struct strbuf errmsg
= STRBUF_INIT
;
888 int total_commits
= -1;
890 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid(the_hash_algo
))) {
891 if (S_ISGITLINK(p
->mod_dst
)) {
892 struct ref_store
*refs
= repo_get_submodule_ref_store(the_repository
,
896 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
897 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
899 int fd
= open(p
->sm_path
, O_RDONLY
);
901 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
902 index_fd(the_repository
->index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
904 error(_("couldn't hash object from '%s'"), p
->sm_path
);
906 /* for a submodule removal (mode:0000000), don't warn */
908 warning(_("unexpected mode %o"), p
->mod_dst
);
912 if (S_ISGITLINK(p
->mod_src
)) {
913 if (p
->status
!= 'D')
914 src_abbrev
= verify_submodule_committish(p
->sm_path
,
915 oid_to_hex(&p
->oid_src
));
919 * As `rev-parse` failed, we fallback to getting
920 * the abbreviated hash using oid_src. We do
921 * this as we might still need the abbreviated
922 * hash in cases like a submodule type change, etc.
924 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
928 * The source does not point to a submodule.
929 * So, we fallback to getting the abbreviation using
930 * oid_src as we might still need the abbreviated
931 * hash in cases like submodule add, etc.
933 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
936 if (S_ISGITLINK(p
->mod_dst
)) {
937 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
938 oid_to_hex(&p
->oid_dst
));
942 * As `rev-parse` failed, we fallback to getting
943 * the abbreviated hash using oid_dst. We do
944 * this as we might still need the abbreviated
945 * hash in cases like a submodule type change, etc.
947 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
951 * The destination does not point to a submodule.
952 * So, we fallback to getting the abbreviation using
953 * oid_dst as we might still need the abbreviated
954 * hash in cases like a submodule removal, etc.
956 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
959 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
962 if (!missing_src
&& !missing_dst
) {
963 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
964 struct strbuf sb_rev_list
= STRBUF_INIT
;
966 strvec_pushl(&cp_rev_list
.args
, "rev-list",
967 "--first-parent", "--count", NULL
);
968 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
969 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
970 src_abbrev
, dst_abbrev
);
972 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
973 src_abbrev
: dst_abbrev
);
974 strvec_push(&cp_rev_list
.args
, "--");
976 cp_rev_list
.git_cmd
= 1;
977 cp_rev_list
.dir
= p
->sm_path
;
978 prepare_submodule_repo_env(&cp_rev_list
.env
);
980 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
981 total_commits
= atoi(sb_rev_list
.buf
);
983 strbuf_release(&sb_rev_list
);
986 * Don't give error msg for modification whose dst is not
987 * submodule, i.e., deleted or changed to blob
989 if (S_ISGITLINK(p
->mod_dst
)) {
990 if (missing_src
&& missing_dst
) {
991 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
992 displaypath
, oid_to_hex(&p
->oid_src
),
993 oid_to_hex(&p
->oid_dst
));
995 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
996 displaypath
, missing_src
?
997 oid_to_hex(&p
->oid_src
) :
998 oid_to_hex(&p
->oid_dst
));
1003 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1004 total_commits
, displaypath
, src_abbrev
,
1010 strbuf_release(&errmsg
);
1013 static void prepare_submodule_summary(struct summary_cb
*info
,
1014 struct module_cb_list
*list
)
1017 for (i
= 0; i
< list
->nr
; i
++) {
1018 const struct submodule
*sub
;
1019 struct module_cb
*p
= list
->entries
[i
];
1020 struct strbuf sm_gitdir
= STRBUF_INIT
;
1022 if (p
->status
== 'D' || p
->status
== 'T') {
1023 generate_submodule_summary(info
, p
);
1027 if (info
->for_status
&& p
->status
!= 'A' &&
1028 (sub
= submodule_from_path(the_repository
,
1029 null_oid(the_hash_algo
), p
->sm_path
))) {
1030 char *config_key
= NULL
;
1034 config_key
= xstrfmt("submodule.%s.ignore",
1036 if (!repo_config_get_string_tmp(the_repository
, config_key
, &value
))
1037 ignore_all
= !strcmp(value
, "all");
1038 else if (sub
->ignore
)
1039 ignore_all
= !strcmp(sub
->ignore
, "all");
1046 /* Also show added or modified modules which are checked out */
1047 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1048 if (is_nonbare_repository_dir(&sm_gitdir
))
1049 generate_submodule_summary(info
, p
);
1050 strbuf_release(&sm_gitdir
);
1054 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1055 struct diff_options
*options UNUSED
,
1059 struct module_cb_list
*list
= data
;
1060 for (i
= 0; i
< q
->nr
; i
++) {
1061 struct diff_filepair
*p
= q
->queue
[i
];
1062 struct module_cb
*temp
;
1064 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1066 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1067 temp
->mod_src
= p
->one
->mode
;
1068 temp
->mod_dst
= p
->two
->mode
;
1069 temp
->oid_src
= p
->one
->oid
;
1070 temp
->oid_dst
= p
->two
->oid
;
1071 temp
->status
= p
->status
;
1072 temp
->sm_path
= xstrdup(p
->one
->path
);
1074 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1075 list
->entries
[list
->nr
++] = temp
;
1079 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1082 case DIFF_INDEX
: return "diff-index";
1083 case DIFF_FILES
: return "diff-files";
1084 default: BUG("bad diff_cmd value %d", diff_cmd
);
1088 static int compute_summary_module_list(struct object_id
*head_oid
,
1089 struct summary_cb
*info
,
1090 enum diff_cmd diff_cmd
)
1092 struct strvec diff_args
= STRVEC_INIT
;
1093 struct rev_info rev
;
1094 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1097 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1099 strvec_push(&diff_args
, "--cached");
1100 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1102 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1103 strvec_push(&diff_args
, "--");
1105 strvec_pushv(&diff_args
, info
->argv
);
1107 repo_config(the_repository
, git_diff_basic_config
, NULL
);
1108 repo_init_revisions(the_repository
, &rev
, info
->prefix
);
1110 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1111 setup_revisions_from_strvec(&diff_args
, &rev
, NULL
);
1112 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1113 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1114 rev
.diffopt
.format_callback_data
= &list
;
1116 if (!info
->cached
) {
1117 if (diff_cmd
== DIFF_INDEX
)
1119 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1120 perror("repo_read_index_preload");
1124 } else if (repo_read_index(the_repository
) < 0) {
1125 perror("repo_read_cache");
1130 if (diff_cmd
== DIFF_INDEX
)
1131 run_diff_index(&rev
, info
->cached
? DIFF_INDEX_CACHED
: 0);
1133 run_diff_files(&rev
, 0);
1134 prepare_submodule_summary(info
, &list
);
1136 strvec_clear(&diff_args
);
1137 release_revisions(&rev
);
1138 module_cb_list_release(&list
);
1142 static int module_summary(int argc
, const char **argv
, const char *prefix
,
1143 struct repository
*repo UNUSED
)
1145 struct summary_cb info
= SUMMARY_CB_INIT
;
1149 int summary_limit
= -1;
1150 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1151 struct object_id head_oid
;
1153 struct option module_summary_options
[] = {
1154 OPT_BOOL(0, "cached", &cached
,
1155 N_("use the commit stored in the index instead of the submodule HEAD")),
1156 OPT_BOOL(0, "files", &files
,
1157 N_("compare the commit in the index with that in the submodule HEAD")),
1158 OPT_BOOL(0, "for-status", &for_status
,
1159 N_("skip submodules with 'ignore_config' value set to 'all'")),
1160 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1161 N_("limit the summary size")),
1164 const char *const git_submodule_helper_usage
[] = {
1165 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1169 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1170 git_submodule_helper_usage
, 0);
1175 if (!repo_get_oid(the_repository
, argc
? argv
[0] : "HEAD", &head_oid
)) {
1180 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1181 /* before the first commit: compare with an empty tree */
1182 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1188 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
1189 die(_("could not fetch a revision for HEAD"));
1194 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1195 diff_cmd
= DIFF_FILES
;
1200 info
.prefix
= prefix
;
1201 info
.cached
= !!cached
;
1202 info
.files
= !!files
;
1203 info
.for_status
= !!for_status
;
1204 info
.summary_limit
= summary_limit
;
1206 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1213 const char *super_prefix
;
1216 #define SYNC_CB_INIT { 0 }
1218 static void sync_submodule(const char *path
, const char *prefix
,
1219 const char *super_prefix
, unsigned int flags
)
1221 const struct submodule
*sub
;
1222 char *remote_key
= NULL
;
1223 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1224 struct strbuf sb
= STRBUF_INIT
;
1225 char *sub_config_path
= NULL
;
1228 if (!is_submodule_active(the_repository
, path
))
1231 if (validate_submodule_path(path
) < 0)
1234 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
1236 if (sub
&& sub
->url
) {
1237 if (starts_with_dot_dot_slash(sub
->url
) ||
1238 starts_with_dot_slash(sub
->url
)) {
1239 char *up_path
= get_up_path(path
);
1241 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1242 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1245 sub_origin_url
= xstrdup(sub
->url
);
1246 super_config_url
= xstrdup(sub
->url
);
1249 sub_origin_url
= xstrdup("");
1250 super_config_url
= xstrdup("");
1253 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1255 if (!(flags
& OPT_QUIET
))
1256 printf(_("Synchronizing submodule url for '%s'\n"),
1260 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1261 if (repo_config_set_gently(the_repository
, sb
.buf
, super_config_url
))
1262 die(_("failed to register url for submodule path '%s'"),
1265 if (!is_submodule_populated_gently(path
, NULL
))
1269 code
= get_default_remote_submodule(path
, &default_remote
);
1273 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1274 free(default_remote
);
1276 submodule_to_gitdir(the_repository
, &sb
, path
);
1277 strbuf_addstr(&sb
, "/config");
1279 if (repo_config_set_in_file_gently(the_repository
, sb
.buf
, remote_key
, NULL
, sub_origin_url
))
1280 die(_("failed to update remote for submodule '%s'"),
1283 if (flags
& OPT_RECURSIVE
) {
1284 struct child_process cpr
= CHILD_PROCESS_INIT
;
1288 prepare_submodule_repo_env(&cpr
.env
);
1290 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1291 "--recursive", NULL
);
1292 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
1294 if (flags
& OPT_QUIET
)
1295 strvec_push(&cpr
.args
, "--quiet");
1297 if (run_command(&cpr
))
1298 die(_("failed to recurse into submodule '%s'"),
1303 free(super_config_url
);
1304 free(sub_origin_url
);
1305 strbuf_release(&sb
);
1308 free(sub_config_path
);
1311 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1313 struct sync_cb
*info
= cb_data
;
1315 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1319 static int module_sync(int argc
, const char **argv
, const char *prefix
,
1320 struct repository
*repo UNUSED
)
1322 struct sync_cb info
= SYNC_CB_INIT
;
1323 struct pathspec pathspec
= { 0 };
1324 struct module_list list
= MODULE_LIST_INIT
;
1327 struct option module_sync_options
[] = {
1328 OPT__SUPER_PREFIX(&info
.super_prefix
),
1329 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1330 OPT_BOOL(0, "recursive", &recursive
,
1331 N_("recurse into nested submodules")),
1334 const char *const git_submodule_helper_usage
[] = {
1335 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1340 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1341 git_submodule_helper_usage
, 0);
1343 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1346 info
.prefix
= prefix
;
1348 info
.flags
|= OPT_QUIET
;
1350 info
.flags
|= OPT_RECURSIVE
;
1352 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1356 module_list_release(&list
);
1357 clear_pathspec(&pathspec
);
1365 #define DEINIT_CB_INIT { 0 }
1367 static void deinit_submodule(const char *path
, const char *prefix
,
1370 const struct submodule
*sub
;
1371 char *displaypath
= NULL
;
1372 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1373 struct strbuf sb_config
= STRBUF_INIT
;
1374 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1376 if (validate_submodule_path(path
) < 0)
1379 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
1381 if (!sub
|| !sub
->name
)
1384 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1386 /* remove the submodule work tree (unless the user already did it) */
1387 if (is_directory(path
)) {
1388 struct strbuf sb_rm
= STRBUF_INIT
;
1391 if (is_directory(sub_git_dir
)) {
1392 if (!(flags
& OPT_QUIET
))
1393 warning(_("Submodule work tree '%s' contains a .git "
1394 "directory. This will be replaced with a "
1395 ".git file by using absorbgitdirs."),
1398 absorb_git_dir_into_superproject(path
, NULL
);
1402 if (!(flags
& OPT_FORCE
)) {
1403 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1406 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1409 if (run_command(&cp_rm
))
1410 die(_("Submodule work tree '%s' contains local "
1411 "modifications; use '-f' to discard them"),
1415 strbuf_addstr(&sb_rm
, path
);
1417 if (!remove_dir_recursively(&sb_rm
, 0))
1418 format
= _("Cleared directory '%s'\n");
1420 format
= _("Could not remove submodule work tree '%s'\n");
1422 if (!(flags
& OPT_QUIET
))
1423 printf(format
, displaypath
);
1425 submodule_unset_core_worktree(sub
);
1427 strbuf_release(&sb_rm
);
1430 if (mkdir(path
, 0777))
1431 printf(_("could not create empty submodule directory %s"),
1434 cp_config
.git_cmd
= 1;
1435 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1436 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1438 /* remove the .git/config entries (unless the user already did it) */
1439 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1440 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1443 * remove the whole section so we have a clean state when
1444 * the user later decides to init this submodule again
1446 repo_config_rename_section_in_file(the_repository
, NULL
, sub_key
, NULL
);
1447 if (!(flags
& OPT_QUIET
))
1448 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1449 sub
->name
, sub
->url
, displaypath
);
1456 strbuf_release(&sb_config
);
1459 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1462 struct deinit_cb
*info
= cb_data
;
1463 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1466 static int module_deinit(int argc
, const char **argv
, const char *prefix
,
1467 struct repository
*repo UNUSED
)
1469 struct deinit_cb info
= DEINIT_CB_INIT
;
1470 struct pathspec pathspec
= { 0 };
1471 struct module_list list
= MODULE_LIST_INIT
;
1475 struct option module_deinit_options
[] = {
1476 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1477 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1478 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1481 const char *const git_submodule_helper_usage
[] = {
1482 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1487 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1488 git_submodule_helper_usage
, 0);
1491 error("pathspec and --all are incompatible");
1492 usage_with_options(git_submodule_helper_usage
,
1493 module_deinit_options
);
1497 die(_("Use '--all' if you really want to deinitialize all submodules"));
1499 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1502 info
.prefix
= prefix
;
1504 info
.flags
|= OPT_QUIET
;
1506 info
.flags
|= OPT_FORCE
;
1508 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1512 module_list_release(&list
);
1513 clear_pathspec(&pathspec
);
1517 struct module_clone_data
{
1523 struct list_objects_filter_options
*filter_options
;
1524 enum ref_storage_format ref_storage_format
;
1525 unsigned int quiet
: 1;
1526 unsigned int progress
: 1;
1527 unsigned int dissociate
: 1;
1528 unsigned int require_init
: 1;
1531 #define MODULE_CLONE_DATA_INIT { \
1532 .single_branch = -1, \
1533 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
1536 struct submodule_alternate_setup
{
1537 const char *submodule_name
;
1538 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1539 SUBMODULE_ALTERNATE_ERROR_DIE
,
1540 SUBMODULE_ALTERNATE_ERROR_INFO
,
1541 SUBMODULE_ALTERNATE_ERROR_IGNORE
1543 struct string_list
*reference
;
1545 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1546 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1549 static const char alternate_error_advice
[] = N_(
1550 "An alternate computed from a superproject's alternate is invalid.\n"
1551 "To allow Git to clone without an alternate in such a case, set\n"
1552 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1553 "'--reference-if-able' instead of '--reference'."
1556 static int add_possible_reference_from_superproject(
1557 struct odb_source
*alt_odb
, void *sas_cb
)
1559 struct submodule_alternate_setup
*sas
= sas_cb
;
1563 * If the alternate object store is another repository, try the
1564 * standard layout with .git/(modules/<name>)+/objects
1566 if (strip_suffix(alt_odb
->path
, "/objects", &len
)) {
1567 struct repository alternate
;
1569 struct strbuf sb
= STRBUF_INIT
;
1570 struct strbuf err
= STRBUF_INIT
;
1571 strbuf_add(&sb
, alt_odb
->path
, len
);
1573 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1574 die(_("could not get a repository handle for gitdir '%s'"),
1578 * We need to end the new path with '/' to mark it as a dir,
1579 * otherwise a submodule name containing '/' will be broken
1580 * as the last part of a missing submodule reference would
1581 * be taken as a file name.
1584 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1585 strbuf_addch(&sb
, '/');
1586 repo_clear(&alternate
);
1588 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1590 char *p
= strbuf_detach(&sb
, NULL
);
1592 string_list_append(sas
->reference
, p
)->util
= p
;
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
:
1609 strbuf_release(&err
);
1610 strbuf_release(&sb
);
1616 static void prepare_possible_alternates(const char *sm_name
,
1617 struct string_list
*reference
)
1619 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1620 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1622 repo_config_get_string(the_repository
, "submodule.alternateLocation", &sm_alternate
);
1626 repo_config_get_string(the_repository
, "submodule.alternateErrorStrategy", &error_strategy
);
1628 if (!error_strategy
)
1629 error_strategy
= xstrdup("die");
1631 sas
.submodule_name
= sm_name
;
1632 sas
.reference
= reference
;
1633 if (!strcmp(error_strategy
, "die"))
1634 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1635 else if (!strcmp(error_strategy
, "info"))
1636 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1637 else if (!strcmp(error_strategy
, "ignore"))
1638 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1640 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1642 if (!strcmp(sm_alternate
, "superproject"))
1643 odb_for_each_alternate(the_repository
->objects
,
1644 add_possible_reference_from_superproject
, &sas
);
1645 else if (!strcmp(sm_alternate
, "no"))
1648 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1651 free(error_strategy
);
1654 static char *clone_submodule_sm_gitdir(const char *name
)
1656 struct strbuf sb
= STRBUF_INIT
;
1659 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1660 sm_gitdir
= absolute_pathdup(sb
.buf
);
1661 strbuf_release(&sb
);
1666 static int dir_contains_only_dotgit(const char *path
)
1668 DIR *dir
= opendir(path
);
1675 e
= readdir_skip_dot_and_dotdot(dir
);
1678 else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT
, e
->d_name
) ||
1679 (e
= readdir_skip_dot_and_dotdot(dir
))) {
1680 error("unexpected item '%s' in '%s'", e
->d_name
, path
);
1688 static int clone_submodule(const struct module_clone_data
*clone_data
,
1689 struct string_list
*reference
)
1692 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1693 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1695 struct child_process cp
= CHILD_PROCESS_INIT
;
1696 const char *clone_data_path
= clone_data
->path
;
1697 char *to_free
= NULL
;
1699 if (validate_submodule_path(clone_data_path
) < 0)
1702 if (!is_absolute_path(clone_data
->path
))
1703 clone_data_path
= to_free
= xstrfmt("%s/%s", repo_get_work_tree(the_repository
),
1706 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1707 die(_("refusing to create/use '%s' in another submodule's "
1708 "git dir"), sm_gitdir
);
1710 if (!file_exists(sm_gitdir
)) {
1711 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1712 !is_empty_dir(clone_data_path
))
1713 die(_("directory not empty: '%s'"), clone_data_path
);
1715 if (safe_create_leading_directories_const(the_repository
, sm_gitdir
) < 0)
1716 die(_("could not create directory '%s'"), sm_gitdir
);
1718 prepare_possible_alternates(clone_data
->name
, reference
);
1720 strvec_push(&cp
.args
, "clone");
1721 strvec_push(&cp
.args
, "--no-checkout");
1722 if (clone_data
->quiet
)
1723 strvec_push(&cp
.args
, "--quiet");
1724 if (clone_data
->progress
)
1725 strvec_push(&cp
.args
, "--progress");
1726 if (clone_data
->depth
> 0)
1727 strvec_pushf(&cp
.args
, "--depth=%d", clone_data
->depth
);
1728 if (reference
->nr
) {
1729 struct string_list_item
*item
;
1731 for_each_string_list_item(item
, reference
)
1732 strvec_pushl(&cp
.args
, "--reference",
1733 item
->string
, NULL
);
1735 if (clone_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
1736 strvec_pushf(&cp
.args
, "--ref-format=%s",
1737 ref_storage_format_to_name(clone_data
->ref_storage_format
));
1738 if (clone_data
->dissociate
)
1739 strvec_push(&cp
.args
, "--dissociate");
1740 if (sm_gitdir
&& *sm_gitdir
)
1741 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1742 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1743 strvec_pushf(&cp
.args
, "--filter=%s",
1744 expand_list_objects_filter_spec(
1745 clone_data
->filter_options
));
1746 if (clone_data
->single_branch
>= 0)
1747 strvec_push(&cp
.args
, clone_data
->single_branch
?
1749 "--no-single-branch");
1751 strvec_push(&cp
.args
, "--");
1752 strvec_push(&cp
.args
, clone_data
->url
);
1753 strvec_push(&cp
.args
, clone_data_path
);
1756 prepare_submodule_repo_env(&cp
.env
);
1759 if(run_command(&cp
))
1760 die(_("clone of '%s' into submodule path '%s' failed"),
1761 clone_data
->url
, clone_data_path
);
1763 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1764 !dir_contains_only_dotgit(clone_data_path
)) {
1765 char *dot_git
= xstrfmt("%s/.git", clone_data_path
);
1768 die(_("directory not empty: '%s'"), clone_data_path
);
1773 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1774 !is_empty_dir(clone_data_path
))
1775 die(_("directory not empty: '%s'"), clone_data_path
);
1776 if (safe_create_leading_directories_const(the_repository
, clone_data_path
) < 0)
1777 die(_("could not create directory '%s'"), clone_data_path
);
1778 path
= xstrfmt("%s/index", sm_gitdir
);
1779 unlink_or_warn(path
);
1784 * We already performed this check at the beginning of this function,
1785 * before cloning the objects. This tries to detect racy behavior e.g.
1786 * in parallel clones, where another process could easily have made the
1787 * gitdir nested _after_ it was created.
1789 * To prevent further harm coming from this unintentionally-nested
1790 * gitdir, let's disable it by deleting the `HEAD` file.
1792 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0) {
1793 char *head
= xstrfmt("%s/HEAD", sm_gitdir
);
1796 die(_("refusing to create/use '%s' in another submodule's "
1797 "git dir"), sm_gitdir
);
1800 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1802 p
= repo_submodule_path(the_repository
, clone_data_path
, "config");
1804 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1806 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1807 repo_config_get_string(the_repository
, "submodule.alternateLocation", &sm_alternate
);
1809 repo_config_set_in_file(the_repository
, p
, "submodule.alternateLocation",
1811 repo_config_get_string(the_repository
, "submodule.alternateErrorStrategy", &error_strategy
);
1813 repo_config_set_in_file(the_repository
, p
, "submodule.alternateErrorStrategy",
1817 free(error_strategy
);
1825 static int module_clone(int argc
, const char **argv
, const char *prefix
,
1826 struct repository
*repo UNUSED
)
1828 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1829 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1830 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1831 struct list_objects_filter_options filter_options
=
1832 LIST_OBJECTS_FILTER_INIT
;
1833 const char *ref_storage_format
= NULL
;
1835 struct option module_clone_options
[] = {
1836 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1838 N_("alternative anchor for relative paths")),
1839 OPT_STRING(0, "path", &clone_data
.path
,
1841 N_("where the new submodule will be cloned to")),
1842 OPT_STRING(0, "name", &clone_data
.name
,
1844 N_("name of the new submodule")),
1845 OPT_STRING(0, "url", &clone_data
.url
,
1847 N_("url where to clone the submodule from")),
1848 OPT_STRING_LIST(0, "reference", &reference
,
1850 N_("reference repository")),
1851 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
1852 N_("specify the reference format to use")),
1853 OPT_BOOL(0, "dissociate", &dissociate
,
1854 N_("use --reference only while cloning")),
1855 OPT_INTEGER(0, "depth", &clone_data
.depth
,
1856 N_("depth for shallow clones")),
1857 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1858 OPT_BOOL(0, "progress", &progress
,
1859 N_("force cloning progress")),
1860 OPT_BOOL(0, "require-init", &require_init
,
1861 N_("disallow cloning into non-empty directory")),
1862 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1863 N_("clone only one branch, HEAD or --branch")),
1864 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1867 const char *const git_submodule_helper_usage
[] = {
1868 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1869 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1870 "[--single-branch] [--filter <filter-spec>] "
1871 "--url <url> --path <path>"),
1875 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1876 git_submodule_helper_usage
, 0);
1878 if (ref_storage_format
) {
1879 clone_data
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
1880 if (clone_data
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
1881 die(_("unknown ref storage format '%s'"), ref_storage_format
);
1883 clone_data
.dissociate
= !!dissociate
;
1884 clone_data
.quiet
= !!quiet
;
1885 clone_data
.progress
= !!progress
;
1886 clone_data
.require_init
= !!require_init
;
1887 clone_data
.filter_options
= &filter_options
;
1889 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1890 usage_with_options(git_submodule_helper_usage
,
1891 module_clone_options
);
1893 clone_submodule(&clone_data
, &reference
);
1894 list_objects_filter_release(&filter_options
);
1895 string_list_clear(&reference
, 1);
1899 static int determine_submodule_update_strategy(struct repository
*r
,
1902 enum submodule_update_type update
,
1903 struct submodule_update_strategy
*out
)
1905 const struct submodule
*sub
= submodule_from_path(r
, null_oid(the_hash_algo
), path
);
1910 key
= xstrfmt("submodule.%s.update", sub
->name
);
1914 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1915 if (parse_submodule_update_strategy(val
, out
) < 0) {
1916 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1920 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1921 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1922 BUG("how did we read update = !command from .gitmodules?");
1923 out
->type
= sub
->update_strategy
.type
;
1924 out
->command
= sub
->update_strategy
.command
;
1926 out
->type
= SM_UPDATE_CHECKOUT
;
1929 (out
->type
== SM_UPDATE_MERGE
||
1930 out
->type
== SM_UPDATE_REBASE
||
1931 out
->type
== SM_UPDATE_NONE
))
1932 out
->type
= SM_UPDATE_CHECKOUT
;
1940 struct update_clone_data
{
1941 const struct submodule
*sub
;
1942 struct object_id oid
;
1943 unsigned just_cloned
;
1946 struct submodule_update_clone
{
1947 /* index into 'update_data.list', the list of submodules to look into for cloning */
1950 /* configuration parameters which are passed on to the children */
1951 const struct update_data
*update_data
;
1953 /* to be consumed by update_submodule() */
1954 struct update_clone_data
*update_clone
;
1955 int update_clone_nr
; int update_clone_alloc
;
1957 /* If we want to stop as fast as possible and return an error */
1958 unsigned quickstop
: 1;
1960 /* failed clones to be retried again */
1961 const struct cache_entry
**failed_clones
;
1962 int failed_clones_nr
, failed_clones_alloc
;
1964 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1966 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1968 free(suc
->update_clone
);
1969 free(suc
->failed_clones
);
1972 struct update_data
{
1974 const char *super_prefix
;
1976 enum submodule_update_type update_default
;
1977 struct object_id suboid
;
1978 struct string_list references
;
1979 struct submodule_update_strategy update_strategy
;
1980 struct list_objects_filter_options
*filter_options
;
1981 struct module_list list
;
1982 enum ref_storage_format ref_storage_format
;
1986 int recommend_shallow
;
1987 unsigned int require_init
;
1990 unsigned int nofetch
;
1991 unsigned int remote
;
1992 unsigned int progress
;
1993 unsigned int dissociate
;
1995 unsigned int warn_if_uninitialized
;
1996 unsigned int recursive
;
1998 /* copied over from update_clone_data */
1999 struct object_id oid
;
2000 unsigned int just_cloned
;
2001 const char *sm_path
;
2003 #define UPDATE_DATA_INIT { \
2004 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2005 .list = MODULE_LIST_INIT, \
2006 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
2007 .recommend_shallow = -1, \
2008 .references = STRING_LIST_INIT_DUP, \
2009 .single_branch = -1, \
2013 static void update_data_release(struct update_data
*ud
)
2015 free(ud
->displaypath
);
2016 submodule_update_strategy_release(&ud
->update_strategy
);
2017 module_list_release(&ud
->list
);
2020 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
2021 struct strbuf
*out
, const char *displaypath
)
2024 * Only mention uninitialized submodules when their
2025 * paths have been specified.
2027 if (suc
->update_data
->warn_if_uninitialized
) {
2029 _("Submodule path '%s' not initialized"),
2031 strbuf_addch(out
, '\n');
2033 _("Maybe you want to use 'update --init'?"));
2034 strbuf_addch(out
, '\n');
2039 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2040 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2042 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
2043 struct child_process
*child
,
2044 struct submodule_update_clone
*suc
,
2047 const struct submodule
*sub
= NULL
;
2048 const char *url
= NULL
;
2049 const char *update_string
;
2050 enum submodule_update_type update_type
;
2052 const struct update_data
*ud
= suc
->update_data
;
2053 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
2055 struct strbuf sb
= STRBUF_INIT
;
2056 int needs_cloning
= 0;
2057 int need_free_url
= 0;
2060 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
2061 strbuf_addch(out
, '\n');
2065 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), ce
->name
);
2068 next_submodule_warn_missing(suc
, out
, displaypath
);
2072 key
= xstrfmt("submodule.%s.update", sub
->name
);
2073 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2074 update_type
= parse_submodule_update_type(update_string
);
2076 update_type
= sub
->update_strategy
.type
;
2080 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2081 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2082 && update_type
== SM_UPDATE_NONE
)) {
2083 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2084 strbuf_addch(out
, '\n');
2088 /* Check if the submodule has been initialized. */
2089 if (!is_submodule_active(the_repository
, ce
->name
)) {
2090 next_submodule_warn_missing(suc
, out
, displaypath
);
2095 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2096 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2097 if (sub
->url
&& (starts_with_dot_slash(sub
->url
) ||
2098 starts_with_dot_dot_slash(sub
->url
))) {
2099 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2106 die(_("cannot clone submodule '%s' without a URL"), sub
->name
);
2109 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2110 needs_cloning
= !file_exists(sb
.buf
);
2112 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2113 suc
->update_clone_alloc
);
2114 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2115 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2116 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2117 suc
->update_clone_nr
++;
2123 child
->no_stdin
= 1;
2124 child
->stdout_to_stderr
= 1;
2126 strvec_push(&child
->args
, "submodule--helper");
2127 strvec_push(&child
->args
, "clone");
2128 if (suc
->update_data
->progress
)
2129 strvec_push(&child
->args
, "--progress");
2130 if (suc
->update_data
->quiet
)
2131 strvec_push(&child
->args
, "--quiet");
2132 if (suc
->update_data
->prefix
)
2133 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2134 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2135 strvec_push(&child
->args
, "--depth=1");
2136 else if (suc
->update_data
->depth
)
2137 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2138 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2139 strvec_pushf(&child
->args
, "--filter=%s",
2140 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2141 if (suc
->update_data
->require_init
)
2142 strvec_push(&child
->args
, "--require-init");
2143 if (suc
->update_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
2144 strvec_pushf(&child
->args
, "--ref-format=%s",
2145 ref_storage_format_to_name(suc
->update_data
->ref_storage_format
));
2146 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2147 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2148 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2149 if (suc
->update_data
->references
.nr
) {
2150 struct string_list_item
*item
;
2152 for_each_string_list_item(item
, &suc
->update_data
->references
)
2153 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2155 if (suc
->update_data
->dissociate
)
2156 strvec_push(&child
->args
, "--dissociate");
2157 if (suc
->update_data
->single_branch
>= 0)
2158 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2160 "--no-single-branch");
2164 strbuf_release(&sb
);
2168 return needs_cloning
;
2171 static int update_clone_get_next_task(struct child_process
*child
,
2176 struct submodule_update_clone
*suc
= suc_cb
;
2177 const struct cache_entry
*ce
;
2180 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2181 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2182 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2183 int *p
= xmalloc(sizeof(*p
));
2193 * The loop above tried cloning each submodule once, now try the
2194 * stragglers again, which we can imagine as an extension of the
2197 index
= suc
->current
- suc
->update_data
->list
.nr
;
2198 if (index
< suc
->failed_clones_nr
) {
2201 ce
= suc
->failed_clones
[index
];
2202 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2204 strbuf_addstr(err
, "BUG: submodule considered for "
2205 "cloning, doesn't need cloning "
2209 p
= xmalloc(sizeof(*p
));
2219 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2221 void *idx_task_cb UNUSED
)
2223 struct submodule_update_clone
*suc
= suc_cb
;
2229 static int update_clone_task_finished(int result
,
2234 const struct cache_entry
*ce
;
2235 struct submodule_update_clone
*suc
= suc_cb
;
2236 int *idxP
= idx_task_cb
;
2244 if (idx
< suc
->update_data
->list
.nr
) {
2245 ce
= suc
->update_data
->list
.entries
[idx
];
2246 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2248 strbuf_addch(err
, '\n');
2249 ALLOC_GROW(suc
->failed_clones
,
2250 suc
->failed_clones_nr
+ 1,
2251 suc
->failed_clones_alloc
);
2252 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2255 idx
-= suc
->update_data
->list
.nr
;
2256 ce
= suc
->failed_clones
[idx
];
2257 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2259 strbuf_addch(err
, '\n');
2267 static int git_update_clone_config(const char *var
, const char *value
,
2268 const struct config_context
*ctx
,
2273 if (!strcmp(var
, "submodule.fetchjobs"))
2274 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
2278 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2280 struct child_process cp
= CHILD_PROCESS_INIT
;
2281 struct strbuf rev
= STRBUF_INIT
;
2282 char *hex
= oid_to_hex(oid
);
2288 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2290 prepare_submodule_repo_env(&cp
.env
);
2292 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2297 strbuf_release(&rev
);
2301 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2302 const struct object_id
*oid
)
2304 struct child_process cp
= CHILD_PROCESS_INIT
;
2306 prepare_submodule_repo_env(&cp
.env
);
2308 cp
.dir
= module_path
;
2310 strvec_push(&cp
.args
, "fetch");
2312 strvec_push(&cp
.args
, "--quiet");
2314 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2316 char *hex
= oid_to_hex(oid
);
2320 code
= get_default_remote_submodule(module_path
, &remote
);
2322 child_process_clear(&cp
);
2326 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2330 return run_command(&cp
);
2333 static int run_update_command(const struct update_data
*ud
, int subforce
)
2335 struct child_process cp
= CHILD_PROCESS_INIT
;
2336 char *oid
= oid_to_hex(&ud
->oid
);
2339 switch (ud
->update_strategy
.type
) {
2340 case SM_UPDATE_CHECKOUT
:
2342 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2344 strvec_push(&cp
.args
, "-f");
2346 case SM_UPDATE_REBASE
:
2348 strvec_push(&cp
.args
, "rebase");
2350 strvec_push(&cp
.args
, "--quiet");
2352 case SM_UPDATE_MERGE
:
2354 strvec_push(&cp
.args
, "merge");
2356 strvec_push(&cp
.args
, "--quiet");
2358 case SM_UPDATE_COMMAND
:
2360 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2363 BUG("unexpected update strategy type: %d",
2364 ud
->update_strategy
.type
);
2366 strvec_push(&cp
.args
, oid
);
2368 cp
.dir
= ud
->sm_path
;
2369 prepare_submodule_repo_env(&cp
.env
);
2370 if ((ret
= run_command(&cp
))) {
2371 switch (ud
->update_strategy
.type
) {
2372 case SM_UPDATE_CHECKOUT
:
2373 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2374 oid
, ud
->displaypath
);
2375 /* No "ret" assignment, use "git checkout"'s */
2377 case SM_UPDATE_REBASE
:
2378 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2379 oid
, ud
->displaypath
);
2381 case SM_UPDATE_MERGE
:
2382 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2383 oid
, ud
->displaypath
);
2385 case SM_UPDATE_COMMAND
:
2386 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2387 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2390 BUG("unexpected update strategy type: %d",
2391 ud
->update_strategy
.type
);
2400 switch (ud
->update_strategy
.type
) {
2401 case SM_UPDATE_CHECKOUT
:
2402 printf(_("Submodule path '%s': checked out '%s'\n"),
2403 ud
->displaypath
, oid
);
2405 case SM_UPDATE_REBASE
:
2406 printf(_("Submodule path '%s': rebased into '%s'\n"),
2407 ud
->displaypath
, oid
);
2409 case SM_UPDATE_MERGE
:
2410 printf(_("Submodule path '%s': merged in '%s'\n"),
2411 ud
->displaypath
, oid
);
2413 case SM_UPDATE_COMMAND
:
2414 printf(_("Submodule path '%s': '%s %s'\n"),
2415 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2418 BUG("unexpected update strategy type: %d",
2419 ud
->update_strategy
.type
);
2425 static int run_update_procedure(const struct update_data
*ud
)
2427 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2431 * Run fetch only if `oid` isn't present or it
2432 * is not reachable from a ref.
2434 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2435 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2438 _("Unable to fetch in submodule path '%s'; "
2439 "trying to directly fetch %s:"),
2440 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2442 * Now we tried the usual fetch, but `oid` may
2443 * not be reachable from any of the refs.
2445 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2446 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2447 return die_message(_("Fetched in submodule path '%s', but it did not "
2448 "contain %s. Direct fetching of that commit failed."),
2449 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2452 return run_update_command(ud
, subforce
);
2455 static int remote_submodule_branch(const char *path
, const char **branch
)
2457 const struct submodule
*sub
;
2461 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
2463 return die_message(_("could not initialize submodule at path '%s'"),
2466 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2467 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2468 *branch
= sub
->branch
;
2476 if (!strcmp(*branch
, ".")) {
2477 const char *refname
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
2482 return die_message(_("No such ref: %s"), "HEAD");
2485 if (!strcmp(refname
, "HEAD"))
2486 return die_message(_("Submodule (%s) branch configured to inherit "
2487 "branch from superproject, but the superproject "
2488 "is not on any branch"), sub
->name
);
2490 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2491 return die_message(_("Expecting a full ref name, got %s"),
2498 /* Our "branch" is coming from repo_config_get_string_tmp() */
2502 static int ensure_core_worktree(const char *path
)
2505 struct repository subrepo
;
2507 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid(the_hash_algo
)))
2508 return die_message(_("could not get a repository handle for submodule '%s'"),
2511 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2512 char *cfg_file
, *abs_path
;
2513 const char *rel_path
;
2514 struct strbuf sb
= STRBUF_INIT
;
2516 cfg_file
= repo_git_path(&subrepo
, "config");
2518 abs_path
= absolute_pathdup(path
);
2519 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2521 repo_config_set_in_file(the_repository
, cfg_file
, "core.worktree", rel_path
);
2525 strbuf_release(&sb
);
2528 repo_clear(&subrepo
);
2532 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2535 case SM_UPDATE_CHECKOUT
:
2537 case SM_UPDATE_MERGE
:
2539 case SM_UPDATE_REBASE
:
2541 case SM_UPDATE_UNSPECIFIED
:
2542 case SM_UPDATE_NONE
:
2543 case SM_UPDATE_COMMAND
:
2546 BUG("unreachable with type %d", type
);
2549 static void update_data_to_args(const struct update_data
*update_data
,
2550 struct strvec
*args
)
2552 enum submodule_update_type update_type
= update_data
->update_default
;
2554 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2555 if (update_data
->displaypath
)
2556 strvec_pushf(args
, "--super-prefix=%s/",
2557 update_data
->displaypath
);
2558 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2559 if (update_data
->quiet
)
2560 strvec_push(args
, "--quiet");
2561 if (update_data
->force
)
2562 strvec_push(args
, "--force");
2563 if (update_data
->init
)
2564 strvec_push(args
, "--init");
2565 if (update_data
->remote
)
2566 strvec_push(args
, "--remote");
2567 if (update_data
->nofetch
)
2568 strvec_push(args
, "--no-fetch");
2569 if (update_data
->dissociate
)
2570 strvec_push(args
, "--dissociate");
2571 if (update_data
->progress
)
2572 strvec_push(args
, "--progress");
2573 if (update_data
->require_init
)
2574 strvec_push(args
, "--require-init");
2575 if (update_data
->depth
)
2576 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2577 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2578 strvec_pushf(args
, "--%s",
2579 submodule_update_type_to_label(update_type
));
2581 if (update_data
->references
.nr
) {
2582 struct string_list_item
*item
;
2584 for_each_string_list_item(item
, &update_data
->references
)
2585 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2587 if (update_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
2588 strvec_pushf(args
, "--ref-format=%s",
2589 ref_storage_format_to_name(update_data
->ref_storage_format
));
2590 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2591 strvec_pushf(args
, "--filter=%s",
2592 expand_list_objects_filter_spec(
2593 update_data
->filter_options
));
2594 if (update_data
->recommend_shallow
== 0)
2595 strvec_push(args
, "--no-recommend-shallow");
2596 else if (update_data
->recommend_shallow
== 1)
2597 strvec_push(args
, "--recommend-shallow");
2598 if (update_data
->single_branch
>= 0)
2599 strvec_push(args
, update_data
->single_branch
?
2601 "--no-single-branch");
2604 static int update_submodule(struct update_data
*update_data
)
2608 if (validate_submodule_path(update_data
->sm_path
) < 0)
2611 ret
= determine_submodule_update_strategy(the_repository
,
2612 update_data
->just_cloned
,
2613 update_data
->sm_path
,
2614 update_data
->update_default
,
2615 &update_data
->update_strategy
);
2619 if (update_data
->just_cloned
)
2620 oidcpy(&update_data
->suboid
, null_oid(the_hash_algo
));
2621 else if (repo_resolve_gitlink_ref(the_repository
, update_data
->sm_path
,
2622 "HEAD", &update_data
->suboid
))
2623 return die_message(_("Unable to find current revision in submodule path '%s'"),
2624 update_data
->displaypath
);
2626 if (update_data
->remote
) {
2632 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2635 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2640 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2644 if (!update_data
->nofetch
) {
2645 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2648 return die_message(_("Unable to fetch in submodule path '%s'"),
2649 update_data
->sm_path
);
2653 if (repo_resolve_gitlink_ref(the_repository
, update_data
->sm_path
,
2654 remote_ref
, &update_data
->oid
)) {
2655 ret
= die_message(_("Unable to find %s revision in submodule path '%s'"),
2656 remote_ref
, update_data
->sm_path
);
2664 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2665 ret
= run_update_procedure(update_data
);
2670 if (update_data
->recursive
) {
2671 struct child_process cp
= CHILD_PROCESS_INIT
;
2672 struct update_data next
= *update_data
;
2675 oidcpy(&next
.oid
, null_oid(the_hash_algo
));
2676 oidcpy(&next
.suboid
, null_oid(the_hash_algo
));
2678 cp
.dir
= update_data
->sm_path
;
2680 prepare_submodule_repo_env(&cp
.env
);
2681 update_data_to_args(&next
, &cp
.args
);
2683 ret
= run_command(&cp
);
2685 die_message(_("Failed to recurse into submodule path '%s'"),
2686 update_data
->displaypath
);
2693 static int update_submodules(struct update_data
*update_data
)
2696 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2697 const struct run_process_parallel_opts opts
= {
2698 .tr2_category
= "submodule",
2699 .tr2_label
= "parallel/update",
2701 .processes
= update_data
->max_jobs
,
2703 .get_next_task
= update_clone_get_next_task
,
2704 .start_failure
= update_clone_start_failure
,
2705 .task_finished
= update_clone_task_finished
,
2709 suc
.update_data
= update_data
;
2710 run_processes_parallel(&opts
);
2713 * We saved the output and put it out all at once now.
2715 * - the listener does not have to interleave their (checkout)
2716 * work with our fetching. The writes involved in a
2717 * checkout involve more straightforward sequential I/O.
2718 * - the listener can avoid doing any work if fetching failed.
2720 if (suc
.quickstop
) {
2725 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2726 struct update_clone_data ucd
= suc
.update_clone
[i
];
2729 oidcpy(&update_data
->oid
, &ucd
.oid
);
2730 update_data
->just_cloned
= ucd
.just_cloned
;
2731 update_data
->sm_path
= ucd
.sub
->path
;
2734 * Verify that the submodule path does not contain any
2735 * symlinks; if it does, it might have been tampered with.
2736 * TODO: allow exempting it via
2737 * `safe.submodule.path` or something
2739 if (validate_submodule_path(update_data
->sm_path
) < 0)
2742 code
= ensure_core_worktree(update_data
->sm_path
);
2746 update_data
->displaypath
= get_submodule_displaypath(
2747 update_data
->sm_path
, update_data
->prefix
,
2748 update_data
->super_prefix
);
2749 code
= update_submodule(update_data
);
2750 FREE_AND_NULL(update_data
->displaypath
);
2760 submodule_update_clone_release(&suc
);
2761 string_list_clear(&update_data
->references
, 0);
2765 static int module_update(int argc
, const char **argv
, const char *prefix
,
2766 struct repository
*repo UNUSED
)
2768 struct pathspec pathspec
= { 0 };
2769 struct pathspec pathspec2
= { 0 };
2770 struct update_data opt
= UPDATE_DATA_INIT
;
2771 struct list_objects_filter_options filter_options
=
2772 LIST_OBJECTS_FILTER_INIT
;
2773 const char *ref_storage_format
= NULL
;
2775 struct option module_update_options
[] = {
2776 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2777 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2778 OPT_BOOL(0, "init", &opt
.init
,
2779 N_("initialize uninitialized submodules before update")),
2780 OPT_BOOL(0, "remote", &opt
.remote
,
2781 N_("use SHA-1 of submodule's remote tracking branch")),
2782 OPT_BOOL(0, "recursive", &opt
.recursive
,
2783 N_("traverse submodules recursively")),
2784 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2785 N_("don't fetch new objects from the remote site")),
2786 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2787 N_("use the 'checkout' update strategy (default)"),
2788 SM_UPDATE_CHECKOUT
),
2789 OPT_SET_INT('m', "merge", &opt
.update_default
,
2790 N_("use the 'merge' update strategy"),
2792 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2793 N_("use the 'rebase' update strategy"),
2795 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2796 N_("reference repository")),
2797 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
2798 N_("specify the reference format to use")),
2799 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2800 N_("use --reference only while cloning")),
2801 OPT_INTEGER(0, "depth", &opt
.depth
,
2802 N_("create a shallow clone truncated to the "
2803 "specified number of revisions")),
2804 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2805 N_("parallel jobs")),
2806 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2807 N_("whether the initial clone should follow the shallow recommendation")),
2808 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2809 OPT_BOOL(0, "progress", &opt
.progress
,
2810 N_("force cloning progress")),
2811 OPT_BOOL(0, "require-init", &opt
.require_init
,
2812 N_("disallow cloning into non-empty directory, implies --init")),
2813 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2814 N_("clone only one branch, HEAD or --branch")),
2815 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2818 const char *const git_submodule_helper_usage
[] = {
2819 N_("git submodule [--quiet] update"
2820 " [--init [--filter=<filter-spec>]] [--remote]"
2821 " [-N|--no-fetch] [-f|--force]"
2822 " [--checkout|--merge|--rebase]"
2823 " [--[no-]recommend-shallow] [--reference <repository>]"
2824 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2828 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2829 repo_config(the_repository
, git_update_clone_config
, &opt
.max_jobs
);
2831 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2832 git_submodule_helper_usage
, 0);
2834 if (opt
.require_init
)
2837 if (filter_options
.choice
&& !opt
.init
) {
2838 usage_with_options(git_submodule_helper_usage
,
2839 module_update_options
);
2842 if (ref_storage_format
) {
2843 opt
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
2844 if (opt
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
2845 die(_("unknown ref storage format '%s'"), ref_storage_format
);
2848 opt
.filter_options
= &filter_options
;
2849 opt
.prefix
= prefix
;
2851 if (opt
.update_default
)
2852 opt
.update_strategy
.type
= opt
.update_default
;
2854 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2860 opt
.warn_if_uninitialized
= 1;
2863 struct module_list list
= MODULE_LIST_INIT
;
2864 struct init_cb info
= INIT_CB_INIT
;
2866 if (module_list_compute(argv
, opt
.prefix
,
2867 &pathspec2
, &list
) < 0) {
2868 module_list_release(&list
);
2874 * If there are no path args and submodule.active is set then,
2875 * by default, only initialize 'active' modules.
2877 if (!argc
&& !repo_config_get(the_repository
, "submodule.active"))
2878 module_list_active(&list
);
2880 info
.prefix
= opt
.prefix
;
2881 info
.super_prefix
= opt
.super_prefix
;
2883 info
.flags
|= OPT_QUIET
;
2885 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2886 module_list_release(&list
);
2889 ret
= update_submodules(&opt
);
2891 update_data_release(&opt
);
2892 list_objects_filter_release(&filter_options
);
2893 clear_pathspec(&pathspec
);
2894 clear_pathspec(&pathspec2
);
2898 static int push_check(int argc
, const char **argv
, const char *prefix UNUSED
,
2899 struct repository
*repo UNUSED
)
2901 struct remote
*remote
;
2902 const char *superproject_head
;
2904 int detached_head
= 0;
2905 struct object_id head_oid
;
2908 die("submodule--helper push-check requires at least 2 arguments");
2911 * superproject's resolved head ref.
2912 * if HEAD then the superproject is in a detached head state, otherwise
2913 * it will be the resolved head ref.
2915 superproject_head
= argv
[1];
2918 /* Get the submodule's head ref and determine if it is detached */
2919 head
= refs_resolve_refdup(get_main_ref_store(the_repository
), "HEAD",
2920 0, &head_oid
, NULL
);
2922 die(_("Failed to resolve HEAD as a valid ref."));
2923 if (!strcmp(head
, "HEAD"))
2927 * The remote must be configured.
2928 * This is to avoid pushing to the exact same URL as the parent.
2930 remote
= pushremote_get(argv
[1]);
2931 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2932 die("remote '%s' not configured", argv
[1]);
2934 /* Check the refspec */
2937 struct ref
*local_refs
= get_local_heads();
2938 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2940 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2942 for (i
= 0; i
< refspec
.nr
; i
++) {
2943 const struct refspec_item
*rs
= &refspec
.items
[i
];
2945 if (rs
->pattern
|| rs
->matching
)
2948 /* LHS must match a single ref */
2949 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2954 * If LHS matches 'HEAD' then we need to ensure
2955 * that it matches the same named branch
2956 * checked out in the superproject.
2958 if (!strcmp(rs
->src
, "HEAD")) {
2959 if (!detached_head
&&
2960 !strcmp(head
, superproject_head
))
2962 die("HEAD does not match the named branch in the superproject");
2966 die("src refspec '%s' must name a ref",
2971 refspec_clear(&refspec
);
2972 free_refs(local_refs
);
2979 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
,
2980 struct repository
*repo UNUSED
)
2983 struct pathspec pathspec
= { 0 };
2984 struct module_list list
= MODULE_LIST_INIT
;
2985 const char *super_prefix
= NULL
;
2986 struct option embed_gitdir_options
[] = {
2987 OPT__SUPER_PREFIX(&super_prefix
),
2990 const char *const git_submodule_helper_usage
[] = {
2991 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2996 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2997 git_submodule_helper_usage
, 0);
2999 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
3002 for (i
= 0; i
< list
.nr
; i
++)
3003 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
3008 clear_pathspec(&pathspec
);
3009 module_list_release(&list
);
3013 static int module_set_url(int argc
, const char **argv
, const char *prefix
,
3014 struct repository
*repo UNUSED
)
3020 struct option options
[] = {
3021 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
3024 const char *const usage
[] = {
3025 N_("git submodule set-url [--quiet] <path> <newurl>"),
3028 const struct submodule
*sub
;
3030 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3032 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
3033 usage_with_options(usage
, options
);
3035 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
3038 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3041 config_name
= xstrfmt("submodule.%s.url", sub
->name
);
3042 ret
= config_set_in_gitmodules_file_gently(config_name
, newurl
);
3045 repo_read_gitmodules(the_repository
, 0);
3046 sync_submodule(sub
->path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
3053 static int module_set_branch(int argc
, const char **argv
, const char *prefix
,
3054 struct repository
*repo UNUSED
)
3056 int opt_default
= 0, ret
;
3057 const char *opt_branch
= NULL
;
3060 struct option options
[] = {
3062 * We accept the `quiet` option for uniformity across subcommands,
3063 * though there is nothing to make less verbose in this subcommand.
3065 OPT_NOOP_NOARG('q', "quiet"),
3067 OPT_BOOL('d', "default", &opt_default
,
3068 N_("set the default tracking branch to master")),
3069 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
3070 N_("set the default tracking branch")),
3073 const char *const usage
[] = {
3074 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3075 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3078 const struct submodule
*sub
;
3080 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3082 if (!opt_branch
&& !opt_default
)
3083 die(_("--branch or --default required"));
3085 if (opt_branch
&& opt_default
)
3086 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3088 if (argc
!= 1 || !(path
= argv
[0]))
3089 usage_with_options(usage
, options
);
3091 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
3094 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3097 config_name
= xstrfmt("submodule.%s.branch", sub
->name
);
3098 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
3104 static int module_create_branch(int argc
, const char **argv
, const char *prefix
,
3105 struct repository
*repo UNUSED
)
3107 enum branch_track track
;
3108 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
3109 struct option options
[] = {
3110 OPT__QUIET(&quiet
, N_("print only error messages")),
3111 OPT__FORCE(&force
, N_("force creation"), 0),
3112 OPT_BOOL(0, "create-reflog", &reflog
,
3113 N_("create the branch's reflog")),
3114 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
3115 N_("set branch tracking configuration"),
3117 parse_opt_tracking_mode
),
3118 OPT__DRY_RUN(&dry_run
,
3119 N_("show whether the branch would be created")),
3122 const char *const usage
[] = {
3123 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3127 repo_config(the_repository
, git_default_config
, NULL
);
3128 track
= git_branch_track
;
3129 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3132 usage_with_options(usage
, options
);
3134 if (!quiet
&& !dry_run
)
3135 printf_ln(_("creating branch '%s'"), argv
[0]);
3137 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
3138 force
, reflog
, quiet
, track
, dry_run
);
3145 const char *reference_path
;
3147 const char *sm_name
;
3149 const char *realrepo
;
3150 enum ref_storage_format ref_storage_format
;
3152 unsigned int force
: 1;
3153 unsigned int quiet
: 1;
3154 unsigned int progress
: 1;
3155 unsigned int dissociate
: 1;
3157 #define ADD_DATA_INIT { \
3159 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
3162 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3164 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3165 struct strbuf sb_remote_out
= STRBUF_INIT
;
3167 cp_remote
.git_cmd
= 1;
3168 strvec_pushf(&cp_remote
.env
,
3169 "GIT_DIR=%s", git_dir_path
);
3170 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3171 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3172 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3174 char *line
= sb_remote_out
.buf
;
3176 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3177 size_t len
= next_line
- line
;
3179 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3180 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3181 line
= next_line
+ 1;
3185 strbuf_release(&sb_remote_out
);
3188 static int add_submodule(const struct add_data
*add_data
)
3190 char *submod_gitdir_path
;
3191 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3192 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3195 /* perhaps the path already exists and is already a git repo, else clone it */
3196 if (is_directory(add_data
->sm_path
)) {
3197 struct strbuf sm_path
= STRBUF_INIT
;
3198 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3199 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3200 if (is_nonbare_repository_dir(&sm_path
))
3201 printf(_("Adding existing repo at '%s' to the index\n"),
3204 die(_("'%s' already exists and is not a valid git repo"),
3206 strbuf_release(&sm_path
);
3207 free(submod_gitdir_path
);
3209 struct child_process cp
= CHILD_PROCESS_INIT
;
3211 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3213 if (is_directory(submod_gitdir_path
)) {
3214 if (!add_data
->force
) {
3215 struct strbuf msg
= STRBUF_INIT
;
3218 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3219 "locally with remote(s):\n"),
3222 append_fetch_remotes(&msg
, submod_gitdir_path
);
3223 free(submod_gitdir_path
);
3225 strbuf_addf(&msg
, _("If you want to reuse this local git "
3226 "directory instead of cloning again from\n"
3228 "use the '--force' option. If the local git "
3229 "directory is not the correct repo\n"
3230 "or you are unsure what this means choose "
3231 "another name with the '--name' option."),
3232 add_data
->realrepo
);
3234 die_msg
= strbuf_detach(&msg
, NULL
);
3237 printf(_("Reactivating local git directory for "
3238 "submodule '%s'\n"), add_data
->sm_name
);
3241 free(submod_gitdir_path
);
3243 clone_data
.prefix
= add_data
->prefix
;
3244 clone_data
.path
= add_data
->sm_path
;
3245 clone_data
.name
= add_data
->sm_name
;
3246 clone_data
.url
= add_data
->realrepo
;
3247 clone_data
.quiet
= add_data
->quiet
;
3248 clone_data
.progress
= add_data
->progress
;
3249 if (add_data
->reference_path
) {
3250 char *p
= xstrdup(add_data
->reference_path
);
3252 string_list_append(&reference
, p
)->util
= p
;
3254 clone_data
.ref_storage_format
= add_data
->ref_storage_format
;
3255 clone_data
.dissociate
= add_data
->dissociate
;
3256 if (add_data
->depth
>= 0)
3257 clone_data
.depth
= add_data
->depth
;
3259 if (clone_submodule(&clone_data
, &reference
))
3262 prepare_submodule_repo_env(&cp
.env
);
3264 cp
.dir
= add_data
->sm_path
;
3266 * NOTE: we only get here if add_data->force is true, so
3267 * passing --force to checkout is reasonable.
3269 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3271 if (add_data
->branch
) {
3272 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3273 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3276 if (run_command(&cp
))
3277 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3282 string_list_clear(&reference
, 1);
3286 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3291 if (!is_writing_gitmodules_ok())
3292 die(_("please make sure that the .gitmodules file is in the working tree"));
3294 key
= xstrfmt("submodule.%s.%s", name
, var
);
3295 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3301 static void configure_added_submodule(struct add_data
*add_data
)
3304 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3305 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3306 const struct string_list
*values
;
3309 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3310 repo_config_set_gently(the_repository
, key
, add_data
->realrepo
);
3313 add_submod
.git_cmd
= 1;
3314 strvec_pushl(&add_submod
.args
, "add",
3315 "--no-warn-embedded-repo", NULL
);
3316 if (add_data
->force
)
3317 strvec_push(&add_submod
.args
, "--force");
3318 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3320 if (run_command(&add_submod
))
3321 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3323 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3324 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3325 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3327 if (add_data
->branch
) {
3328 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3329 "branch", add_data
->branch
))
3330 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3333 add_gitmodules
.git_cmd
= 1;
3334 strvec_pushl(&add_gitmodules
.args
,
3335 "add", "--force", "--", ".gitmodules", NULL
);
3337 if (run_command(&add_gitmodules
))
3338 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3341 * NEEDSWORK: In a multi-working-tree world this needs to be
3342 * set in the per-worktree config.
3345 * NEEDSWORK: In the longer run, we need to get rid of this
3346 * pattern of querying "submodule.active" before calling
3347 * is_submodule_active(), since that function needs to find
3348 * out the value of "submodule.active" again anyway.
3350 if (repo_config_get(the_repository
, "submodule.active") || /* key absent */
3351 repo_config_get_string_multi(the_repository
, "submodule.active", &values
)) {
3353 * If the submodule being added isn't already covered by the
3354 * current configured pathspec, set the submodule's active flag
3356 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3357 repo_config_set_gently(the_repository
, key
, "true");
3360 for (size_t i
= 0; i
< values
->nr
; i
++) {
3361 const char *pat
= values
->items
[i
].string
;
3362 if (!wildmatch(pat
, add_data
->sm_path
, 0)) { /* match found */
3367 if (!matched
) { /* no pattern matched -> force-enable */
3368 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3369 repo_config_set_gently(the_repository
, key
, "true");
3375 static void die_on_index_match(const char *path
, int force
)
3378 const char *args
[] = { path
, NULL
};
3379 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3381 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3382 die(_("index file corrupt"));
3385 char *ps_matched
= xcalloc(ps
.nr
, 1);
3387 /* TODO: audit for interaction with sparse-index. */
3388 ensure_full_index(the_repository
->index
);
3391 * Since there is only one pathspec, we just need to
3392 * check ps_matched[0] to know if a cache entry matched.
3394 for (size_t i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
3395 ce_path_match(the_repository
->index
, the_repository
->index
->cache
[i
], &ps
,
3398 if (ps_matched
[0]) {
3400 die(_("'%s' already exists in the index"),
3402 if (!S_ISGITLINK(the_repository
->index
->cache
[i
]->ce_mode
))
3403 die(_("'%s' already exists in the index "
3404 "and is not a submodule"), path
);
3410 clear_pathspec(&ps
);
3413 static void die_on_repo_without_commits(const char *path
)
3415 struct strbuf sb
= STRBUF_INIT
;
3416 strbuf_addstr(&sb
, path
);
3417 if (is_nonbare_repository_dir(&sb
)) {
3418 struct object_id oid
;
3419 if (repo_resolve_gitlink_ref(the_repository
, path
, "HEAD", &oid
) < 0)
3420 die(_("'%s' does not have a commit checked out"), path
);
3422 strbuf_release(&sb
);
3425 static int module_add(int argc
, const char **argv
, const char *prefix
,
3426 struct repository
*repo UNUSED
)
3428 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3429 struct add_data add_data
= ADD_DATA_INIT
;
3430 const char *ref_storage_format
= NULL
;
3431 char *to_free
= NULL
;
3432 const struct submodule
*existing
;
3433 struct strbuf buf
= STRBUF_INIT
;
3434 char *sm_name_to_free
= NULL
;
3435 struct option options
[] = {
3436 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3437 N_("branch of repository to add as submodule")),
3438 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3439 PARSE_OPT_NOCOMPLETE
),
3440 OPT__QUIET(&quiet
, N_("print only error messages")),
3441 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3442 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3443 N_("reference repository")),
3444 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
3445 N_("specify the reference format to use")),
3446 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3447 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3448 N_("sets the submodule's name to the given string "
3449 "instead of defaulting to its path")),
3450 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3453 const char *const usage
[] = {
3454 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3457 struct strbuf sb
= STRBUF_INIT
;
3460 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3462 if (!is_writing_gitmodules_ok())
3463 die(_("please make sure that the .gitmodules file is in the working tree"));
3465 if (prefix
&& *prefix
&&
3466 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3467 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3469 if (argc
== 0 || argc
> 2)
3470 usage_with_options(usage
, options
);
3472 if (ref_storage_format
) {
3473 add_data
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
3474 if (add_data
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
3475 die(_("unknown ref storage format '%s'"), ref_storage_format
);
3478 add_data
.repo
= argv
[0];
3480 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3482 add_data
.sm_path
= xstrdup(argv
[1]);
3484 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3485 char *sm_path
= add_data
.sm_path
;
3487 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3491 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3492 starts_with_dot_slash(add_data
.repo
)) {
3494 die(_("Relative path can only be used from the toplevel "
3495 "of the working tree"));
3497 /* dereference source url relative to parent's url */
3498 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3499 add_data
.realrepo
= to_free
;
3500 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3501 add_data
.realrepo
= add_data
.repo
;
3503 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3509 * multiple //; leading ./; /./; /../;
3511 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3512 strip_dir_trailing_slashes(add_data
.sm_path
);
3514 if (validate_submodule_path(add_data
.sm_path
) < 0)
3517 die_on_index_match(add_data
.sm_path
, force
);
3518 die_on_repo_without_commits(add_data
.sm_path
);
3521 struct child_process cp
= CHILD_PROCESS_INIT
;
3525 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3526 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3527 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3528 strbuf_complete_line(&sb
);
3529 fputs(sb
.buf
, stderr
);
3534 if(!add_data
.sm_name
)
3535 add_data
.sm_name
= add_data
.sm_path
;
3537 existing
= submodule_from_name(the_repository
,
3538 null_oid(the_hash_algo
),
3541 if (existing
&& strcmp(existing
->path
, add_data
.sm_path
)) {
3543 die(_("submodule name '%s' already used for path '%s'"),
3544 add_data
.sm_name
, existing
->path
);
3546 /* --force: build <name><n> until unique */
3547 for (int i
= 1; ; i
++) {
3549 strbuf_addf(&buf
, "%s%d", add_data
.sm_name
, i
);
3550 if (!submodule_from_name(the_repository
,
3551 null_oid(the_hash_algo
),
3556 add_data
.sm_name
= sm_name_to_free
= strbuf_detach(&buf
, NULL
);
3559 if (check_submodule_name(add_data
.sm_name
))
3560 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3562 add_data
.prefix
= prefix
;
3563 add_data
.force
= !!force
;
3564 add_data
.quiet
= !!quiet
;
3565 add_data
.progress
= !!progress
;
3566 add_data
.dissociate
= !!dissociate
;
3568 if (add_submodule(&add_data
))
3570 configure_added_submodule(&add_data
);
3574 free(sm_name_to_free
);
3575 free(add_data
.sm_path
);
3577 strbuf_release(&sb
);
3582 int cmd_submodule__helper(int argc
,
3585 struct repository
*repo
)
3587 parse_opt_subcommand_fn
*fn
= NULL
;
3588 const char *const usage
[] = {
3589 N_("git submodule--helper <command>"),
3592 struct option options
[] = {
3593 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3594 OPT_SUBCOMMAND("add", &fn
, module_add
),
3595 OPT_SUBCOMMAND("update", &fn
, module_update
),
3596 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3597 OPT_SUBCOMMAND("init", &fn
, module_init
),
3598 OPT_SUBCOMMAND("status", &fn
, module_status
),
3599 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3600 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3601 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3602 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3603 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3604 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3605 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3606 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3609 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3611 return fn(argc
, argv
, prefix
, repo
);