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"
31 #include "object-store.h"
34 #include "list-objects-filter-options.h"
36 #define OPT_QUIET (1 << 0)
37 #define OPT_CACHED (1 << 1)
38 #define OPT_RECURSIVE (1 << 2)
39 #define OPT_FORCE (1 << 3)
41 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
44 static char *get_default_remote(void)
46 return xstrdup(repo_default_remote(the_repository
));
49 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
51 char *remoteurl
, *resolved_url
;
52 char *remote
= get_default_remote();
53 struct strbuf remotesb
= STRBUF_INIT
;
55 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
56 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
58 warning(_("could not look up configuration '%s'. "
59 "Assuming this repository is its own "
60 "authoritative upstream."),
62 remoteurl
= xgetcwd();
64 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
68 strbuf_release(&remotesb
);
73 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
75 const struct submodule
*sub
;
76 struct repository subrepo
;
77 const char *remote_name
= NULL
;
80 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), module_path
);
81 if (sub
&& sub
->url
) {
82 url
= xstrdup(sub
->url
);
84 /* Possibly a url relative to parent */
85 if (starts_with_dot_dot_slash(url
) ||
86 starts_with_dot_slash(url
)) {
89 url
= resolve_relative_url(oldurl
, NULL
, 1);
94 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
95 null_oid(the_hash_algo
)) < 0)
96 return die_message(_("could not get a repository handle for submodule '%s'"),
99 /* Look up by URL first */
101 remote_name
= repo_remote_from_url(&subrepo
, url
);
103 remote_name
= repo_default_remote(&subrepo
);
105 *default_remote
= xstrdup(remote_name
);
107 repo_clear(&subrepo
);
113 /* the result should be freed by the caller. */
114 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
115 const char *super_prefix
)
117 if (prefix
&& super_prefix
) {
118 BUG("cannot have prefix '%s' and superprefix '%s'",
119 prefix
, super_prefix
);
121 struct strbuf sb
= STRBUF_INIT
;
122 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
125 } else if (super_prefix
) {
126 return xstrfmt("%s%s", super_prefix
, path
);
128 return xstrdup(path
);
132 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
134 struct strbuf sb
= STRBUF_INIT
;
137 static const char *describe_bare
[] = { NULL
};
139 static const char *describe_tags
[] = { "--tags", NULL
};
141 static const char *describe_contains
[] = { "--contains", NULL
};
143 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
145 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
147 describe_all_always
, NULL
};
149 for (d
= describe_argv
; *d
; d
++) {
150 struct child_process cp
= CHILD_PROCESS_INIT
;
151 prepare_submodule_repo_env(&cp
.env
);
156 strvec_push(&cp
.args
, "describe");
157 strvec_pushv(&cp
.args
, *d
);
158 strvec_push(&cp
.args
, object_id
);
160 if (!capture_command(&cp
, &sb
, 0)) {
161 strbuf_strip_suffix(&sb
, "\n");
162 return strbuf_detach(&sb
, NULL
);
171 const struct cache_entry
**entries
;
174 #define MODULE_LIST_INIT { 0 }
176 static void module_list_release(struct module_list
*ml
)
181 static int module_list_compute(const char **argv
,
183 struct pathspec
*pathspec
,
184 struct module_list
*list
)
187 char *ps_matched
= NULL
;
189 parse_pathspec(pathspec
, 0,
190 PATHSPEC_PREFER_FULL
,
194 ps_matched
= xcalloc(pathspec
->nr
, 1);
196 if (repo_read_index(the_repository
) < 0)
197 die(_("index file corrupt"));
199 for (size_t i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
200 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
202 if (!match_pathspec(the_repository
->index
, pathspec
, ce
->name
, ce_namelen(ce
),
204 !S_ISGITLINK(ce
->ce_mode
))
207 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
208 list
->entries
[list
->nr
++] = ce
;
209 while (i
+ 1 < the_repository
->index
->cache_nr
&&
210 !strcmp(ce
->name
, the_repository
->index
->cache
[i
+ 1]->name
))
212 * Skip entries with the same name in different stages
213 * to make sure an entry is returned only once.
218 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
226 static void module_list_active(struct module_list
*list
)
229 struct module_list active_modules
= MODULE_LIST_INIT
;
231 for (i
= 0; i
< list
->nr
; i
++) {
232 const struct cache_entry
*ce
= list
->entries
[i
];
234 if (!is_submodule_active(the_repository
, ce
->name
))
237 ALLOC_GROW(active_modules
.entries
,
238 active_modules
.nr
+ 1,
239 active_modules
.alloc
);
240 active_modules
.entries
[active_modules
.nr
++] = ce
;
243 module_list_release(list
);
244 *list
= active_modules
;
247 static char *get_up_path(const char *path
)
249 struct strbuf sb
= STRBUF_INIT
;
251 strbuf_addstrings(&sb
, "../", count_slashes(path
));
254 * Check if 'path' ends with slash or not
255 * for having the same output for dir/sub_dir
258 if (!is_dir_sep(path
[strlen(path
) - 1]))
259 strbuf_addstr(&sb
, "../");
261 return strbuf_detach(&sb
, NULL
);
264 static void for_each_listed_submodule(const struct module_list
*list
,
265 each_submodule_fn fn
, void *cb_data
)
269 for (i
= 0; i
< list
->nr
; i
++)
270 fn(list
->entries
[i
], cb_data
);
277 const char *super_prefix
;
281 #define FOREACH_CB_INIT { 0 }
283 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
286 struct foreach_cb
*info
= cb_data
;
287 const char *path
= list_item
->name
;
288 const struct object_id
*ce_oid
= &list_item
->oid
;
289 const struct submodule
*sub
;
290 struct child_process cp
= CHILD_PROCESS_INIT
;
293 if (validate_submodule_path(path
) < 0)
296 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
299 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
302 die(_("No url found for submodule path '%s' in .gitmodules"),
305 if (!is_submodule_populated_gently(path
, NULL
))
308 prepare_submodule_repo_env(&cp
.env
);
311 * For the purpose of executing <command> in the submodule,
312 * separate shell is used for the purpose of running the
319 * NEEDSWORK: the command currently has access to the variables $name,
320 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
321 * contains a single argument. This is done for maintaining a faithful
322 * translation from shell script.
324 if (info
->argc
== 1) {
325 char *toplevel
= xgetcwd();
326 struct strbuf sb
= STRBUF_INIT
;
328 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
329 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
330 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
331 strvec_pushf(&cp
.env
, "sha1=%s",
333 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
336 * Since the path variable was accessible from the script
337 * before porting, it is also made available after porting.
338 * The environment variable "PATH" has a very special purpose
339 * on windows. And since environment variables are
340 * case-insensitive in windows, it interferes with the
341 * existing PATH variable. Hence, to avoid that, we expose
342 * path via the args strvec and not via env.
344 sq_quote_buf(&sb
, path
);
345 strvec_pushf(&cp
.args
, "path=%s; %s",
346 sb
.buf
, info
->argv
[0]);
350 strvec_pushv(&cp
.args
, info
->argv
);
354 printf(_("Entering '%s'\n"), displaypath
);
357 if (run_command(&cp
))
358 die(_("run_command returned non-zero status for %s\n."),
361 child_process_clear(&cp
);
364 if (info
->recursive
) {
365 struct child_process cpr
= CHILD_PROCESS_INIT
;
369 prepare_submodule_repo_env(&cpr
.env
);
371 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
373 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
376 strvec_push(&cpr
.args
, "--quiet");
378 strvec_push(&cpr
.args
, "--");
379 strvec_pushv(&cpr
.args
, info
->argv
);
381 if (run_command(&cpr
))
382 die(_("run_command returned non-zero status while "
383 "recursing in the nested submodules of %s\n."),
391 static int module_foreach(int argc
, const char **argv
, const char *prefix
,
392 struct repository
*repo UNUSED
)
394 struct foreach_cb info
= FOREACH_CB_INIT
;
395 struct pathspec pathspec
= { 0 };
396 struct module_list list
= MODULE_LIST_INIT
;
397 struct option module_foreach_options
[] = {
398 OPT__SUPER_PREFIX(&info
.super_prefix
),
399 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
400 OPT_BOOL(0, "recursive", &info
.recursive
,
401 N_("recurse into nested submodules")),
404 const char *const git_submodule_helper_usage
[] = {
405 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
410 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
411 git_submodule_helper_usage
, 0);
413 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
418 info
.prefix
= prefix
;
420 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
424 module_list_release(&list
);
425 clear_pathspec(&pathspec
);
431 const char *super_prefix
;
434 #define INIT_CB_INIT { 0 }
436 static void init_submodule(const char *path
, const char *prefix
,
437 const char *super_prefix
,
440 const struct submodule
*sub
;
441 struct strbuf sb
= STRBUF_INIT
;
443 char *url
= NULL
, *displaypath
;
445 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
447 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
450 die(_("No url found for submodule path '%s' in .gitmodules"),
454 * NEEDSWORK: In a multi-working-tree world, this needs to be
455 * set in the per-worktree config.
457 * Set active flag for the submodule being initialized
459 if (!is_submodule_active(the_repository
, path
)) {
460 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
461 git_config_set_gently(sb
.buf
, "true");
466 * Copy url setting when it is not set yet.
467 * To look up the url in .git/config, we must not fall back to
468 * .gitmodules, so look it up directly.
470 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
471 if (git_config_get_string(sb
.buf
, &url
)) {
473 die(_("No url found for submodule path '%s' in .gitmodules"),
476 url
= xstrdup(sub
->url
);
478 /* Possibly a url relative to parent */
479 if (starts_with_dot_dot_slash(url
) ||
480 starts_with_dot_slash(url
)) {
483 url
= resolve_relative_url(oldurl
, NULL
, 0);
487 if (git_config_set_gently(sb
.buf
, url
))
488 die(_("Failed to register url for submodule path '%s'"),
490 if (!(flags
& OPT_QUIET
))
492 _("Submodule '%s' (%s) registered for path '%s'\n"),
493 sub
->name
, url
, displaypath
);
497 /* Copy "update" setting when it is not set yet */
498 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
499 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
500 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
501 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
502 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
506 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
509 if (git_config_set_gently(sb
.buf
, upd
))
510 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
517 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
519 struct init_cb
*info
= cb_data
;
521 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
525 static int module_init(int argc
, const char **argv
, const char *prefix
,
526 struct repository
*repo UNUSED
)
528 struct init_cb info
= INIT_CB_INIT
;
529 struct pathspec pathspec
= { 0 };
530 struct module_list list
= MODULE_LIST_INIT
;
532 struct option module_init_options
[] = {
533 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
536 const char *const git_submodule_helper_usage
[] = {
537 N_("git submodule init [<options>] [<path>]"),
542 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
543 git_submodule_helper_usage
, 0);
545 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
549 * If there are no path args and submodule.active is set then,
550 * by default, only initialize 'active' modules.
552 if (!argc
&& !git_config_get("submodule.active"))
553 module_list_active(&list
);
555 info
.prefix
= prefix
;
557 info
.flags
|= OPT_QUIET
;
559 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
563 module_list_release(&list
);
564 clear_pathspec(&pathspec
);
570 const char *super_prefix
;
573 #define STATUS_CB_INIT { 0 }
575 static void print_status(unsigned int flags
, char state
, const char *path
,
576 const struct object_id
*oid
, const char *displaypath
)
578 if (flags
& OPT_QUIET
)
581 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
583 if (state
== ' ' || state
== '+') {
584 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
587 printf(" (%s)", name
);
594 static int handle_submodule_head_ref(const char *refname UNUSED
,
595 const char *referent UNUSED
,
596 const struct object_id
*oid
,
600 struct object_id
*output
= cb_data
;
608 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
609 unsigned int ce_flags
, const char *prefix
,
610 const char *super_prefix
, unsigned int flags
)
613 struct strvec diff_files_args
= STRVEC_INIT
;
614 struct rev_info rev
= REV_INFO_INIT
;
615 struct strbuf buf
= STRBUF_INIT
;
617 struct setup_revision_opt opt
= {
618 .free_removed_argv_elements
= 1,
621 if (validate_submodule_path(path
) < 0)
624 if (!submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
))
625 die(_("no submodule mapping found in .gitmodules for path '%s'"),
628 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
630 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
631 print_status(flags
, 'U', path
, null_oid(the_hash_algo
), displaypath
);
635 strbuf_addf(&buf
, "%s/.git", path
);
636 git_dir
= read_gitfile(buf
.buf
);
640 if (!is_submodule_active(the_repository
, path
) ||
641 !is_git_directory(git_dir
)) {
642 print_status(flags
, '-', path
, ce_oid
, displaypath
);
643 strbuf_release(&buf
);
646 strbuf_release(&buf
);
648 strvec_pushl(&diff_files_args
, "diff-files",
649 "--ignore-submodules=dirty", "--quiet", "--",
652 git_config(git_diff_basic_config
, NULL
);
654 repo_init_revisions(the_repository
, &rev
, NULL
);
656 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
657 run_diff_files(&rev
, 0);
659 if (!diff_result_code(&rev
)) {
660 print_status(flags
, ' ', path
, ce_oid
,
662 } else if (!(flags
& OPT_CACHED
)) {
663 struct object_id oid
;
664 struct ref_store
*refs
= repo_get_submodule_ref_store(the_repository
,
668 print_status(flags
, '-', path
, ce_oid
, displaypath
);
671 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
672 die(_("could not resolve HEAD ref inside the "
673 "submodule '%s'"), path
);
675 print_status(flags
, '+', path
, &oid
, displaypath
);
677 print_status(flags
, '+', path
, ce_oid
, displaypath
);
680 if (flags
& OPT_RECURSIVE
) {
681 struct child_process cpr
= CHILD_PROCESS_INIT
;
686 prepare_submodule_repo_env(&cpr
.env
);
688 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
689 "--recursive", NULL
);
690 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
692 if (flags
& OPT_CACHED
)
693 strvec_push(&cpr
.args
, "--cached");
695 if (flags
& OPT_QUIET
)
696 strvec_push(&cpr
.args
, "--quiet");
698 res
= run_command(&cpr
);
699 if (res
== SIGPIPE
+ 128)
702 die(_("failed to recurse into submodule '%s'"), path
);
706 strvec_clear(&diff_files_args
);
708 release_revisions(&rev
);
711 static void status_submodule_cb(const struct cache_entry
*list_item
,
714 struct status_cb
*info
= cb_data
;
716 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
717 info
->prefix
, info
->super_prefix
, info
->flags
);
720 static int module_status(int argc
, const char **argv
, const char *prefix
,
721 struct repository
*repo UNUSED
)
723 struct status_cb info
= STATUS_CB_INIT
;
724 struct pathspec pathspec
= { 0 };
725 struct module_list list
= MODULE_LIST_INIT
;
727 struct option module_status_options
[] = {
728 OPT__SUPER_PREFIX(&info
.super_prefix
),
729 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
730 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
731 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
734 const char *const git_submodule_helper_usage
[] = {
735 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
740 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
741 git_submodule_helper_usage
, 0);
743 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
746 info
.prefix
= prefix
;
748 info
.flags
|= OPT_QUIET
;
750 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
754 module_list_release(&list
);
755 clear_pathspec(&pathspec
);
760 unsigned int mod_src
;
761 unsigned int mod_dst
;
762 struct object_id oid_src
;
763 struct object_id oid_dst
;
767 #define MODULE_CB_INIT { 0 }
769 static void module_cb_release(struct module_cb
*mcb
)
774 struct module_cb_list
{
775 struct module_cb
**entries
;
778 #define MODULE_CB_LIST_INIT { 0 }
780 static void module_cb_list_release(struct module_cb_list
*mcbl
)
784 for (i
= 0; i
< mcbl
->nr
; i
++) {
785 struct module_cb
*mcb
= mcbl
->entries
[i
];
787 module_cb_release(mcb
);
797 const char *super_prefix
;
798 unsigned int cached
: 1;
799 unsigned int for_status
: 1;
800 unsigned int files
: 1;
803 #define SUMMARY_CB_INIT { 0 }
810 static char *verify_submodule_committish(const char *sm_path
,
811 const char *committish
)
813 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
814 struct strbuf result
= STRBUF_INIT
;
816 cp_rev_parse
.git_cmd
= 1;
817 cp_rev_parse
.dir
= sm_path
;
818 prepare_submodule_repo_env(&cp_rev_parse
.env
);
819 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
820 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
821 strvec_push(&cp_rev_parse
.args
, "--");
823 if (capture_command(&cp_rev_parse
, &result
, 0))
826 strbuf_trim_trailing_newline(&result
);
827 return strbuf_detach(&result
, NULL
);
830 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
831 int total_commits
, const char *displaypath
,
832 const char *src_abbrev
, const char *dst_abbrev
,
835 if (p
->status
== 'T') {
836 if (S_ISGITLINK(p
->mod_dst
))
837 printf(_("* %s %s(blob)->%s(submodule)"),
838 displaypath
, src_abbrev
, dst_abbrev
);
840 printf(_("* %s %s(submodule)->%s(blob)"),
841 displaypath
, src_abbrev
, dst_abbrev
);
843 printf("* %s %s...%s",
844 displaypath
, src_abbrev
, dst_abbrev
);
847 if (total_commits
< 0)
850 printf(" (%d):\n", total_commits
);
853 printf(_("%s"), errmsg
);
854 } else if (total_commits
> 0) {
855 struct child_process cp_log
= CHILD_PROCESS_INIT
;
858 cp_log
.dir
= p
->sm_path
;
859 prepare_submodule_repo_env(&cp_log
.env
);
860 strvec_pushl(&cp_log
.args
, "log", NULL
);
862 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
863 if (info
->summary_limit
> 0)
864 strvec_pushf(&cp_log
.args
, "-%d",
865 info
->summary_limit
);
867 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
868 "--first-parent", NULL
);
869 strvec_pushf(&cp_log
.args
, "%s...%s",
870 src_abbrev
, dst_abbrev
);
871 } else if (S_ISGITLINK(p
->mod_dst
)) {
872 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
873 "-1", dst_abbrev
, NULL
);
875 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
876 "-1", src_abbrev
, NULL
);
878 run_command(&cp_log
);
883 static void generate_submodule_summary(struct summary_cb
*info
,
886 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
887 int missing_src
= 0, missing_dst
= 0;
888 struct strbuf errmsg
= STRBUF_INIT
;
889 int total_commits
= -1;
891 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid(the_hash_algo
))) {
892 if (S_ISGITLINK(p
->mod_dst
)) {
893 struct ref_store
*refs
= repo_get_submodule_ref_store(the_repository
,
897 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
898 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
900 int fd
= open(p
->sm_path
, O_RDONLY
);
902 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
903 index_fd(the_repository
->index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
905 error(_("couldn't hash object from '%s'"), p
->sm_path
);
907 /* for a submodule removal (mode:0000000), don't warn */
909 warning(_("unexpected mode %o"), p
->mod_dst
);
913 if (S_ISGITLINK(p
->mod_src
)) {
914 if (p
->status
!= 'D')
915 src_abbrev
= verify_submodule_committish(p
->sm_path
,
916 oid_to_hex(&p
->oid_src
));
920 * As `rev-parse` failed, we fallback to getting
921 * the abbreviated hash using oid_src. We do
922 * this as we might still need the abbreviated
923 * hash in cases like a submodule type change, etc.
925 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
929 * The source does not point to a submodule.
930 * So, we fallback to getting the abbreviation using
931 * oid_src as we might still need the abbreviated
932 * hash in cases like submodule add, etc.
934 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
937 if (S_ISGITLINK(p
->mod_dst
)) {
938 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
939 oid_to_hex(&p
->oid_dst
));
943 * As `rev-parse` failed, we fallback to getting
944 * the abbreviated hash using oid_dst. We do
945 * this as we might still need the abbreviated
946 * hash in cases like a submodule type change, etc.
948 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
952 * The destination does not point to a submodule.
953 * So, we fallback to getting the abbreviation using
954 * oid_dst as we might still need the abbreviated
955 * hash in cases like a submodule removal, etc.
957 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
960 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
963 if (!missing_src
&& !missing_dst
) {
964 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
965 struct strbuf sb_rev_list
= STRBUF_INIT
;
967 strvec_pushl(&cp_rev_list
.args
, "rev-list",
968 "--first-parent", "--count", NULL
);
969 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
970 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
971 src_abbrev
, dst_abbrev
);
973 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
974 src_abbrev
: dst_abbrev
);
975 strvec_push(&cp_rev_list
.args
, "--");
977 cp_rev_list
.git_cmd
= 1;
978 cp_rev_list
.dir
= p
->sm_path
;
979 prepare_submodule_repo_env(&cp_rev_list
.env
);
981 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
982 total_commits
= atoi(sb_rev_list
.buf
);
984 strbuf_release(&sb_rev_list
);
987 * Don't give error msg for modification whose dst is not
988 * submodule, i.e., deleted or changed to blob
990 if (S_ISGITLINK(p
->mod_dst
)) {
991 if (missing_src
&& missing_dst
) {
992 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
993 displaypath
, oid_to_hex(&p
->oid_src
),
994 oid_to_hex(&p
->oid_dst
));
996 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
997 displaypath
, missing_src
?
998 oid_to_hex(&p
->oid_src
) :
999 oid_to_hex(&p
->oid_dst
));
1004 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1005 total_commits
, displaypath
, src_abbrev
,
1011 strbuf_release(&errmsg
);
1014 static void prepare_submodule_summary(struct summary_cb
*info
,
1015 struct module_cb_list
*list
)
1018 for (i
= 0; i
< list
->nr
; i
++) {
1019 const struct submodule
*sub
;
1020 struct module_cb
*p
= list
->entries
[i
];
1021 struct strbuf sm_gitdir
= STRBUF_INIT
;
1023 if (p
->status
== 'D' || p
->status
== 'T') {
1024 generate_submodule_summary(info
, p
);
1028 if (info
->for_status
&& p
->status
!= 'A' &&
1029 (sub
= submodule_from_path(the_repository
,
1030 null_oid(the_hash_algo
), p
->sm_path
))) {
1031 char *config_key
= NULL
;
1035 config_key
= xstrfmt("submodule.%s.ignore",
1037 if (!git_config_get_string_tmp(config_key
, &value
))
1038 ignore_all
= !strcmp(value
, "all");
1039 else if (sub
->ignore
)
1040 ignore_all
= !strcmp(sub
->ignore
, "all");
1047 /* Also show added or modified modules which are checked out */
1048 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1049 if (is_nonbare_repository_dir(&sm_gitdir
))
1050 generate_submodule_summary(info
, p
);
1051 strbuf_release(&sm_gitdir
);
1055 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1056 struct diff_options
*options UNUSED
,
1060 struct module_cb_list
*list
= data
;
1061 for (i
= 0; i
< q
->nr
; i
++) {
1062 struct diff_filepair
*p
= q
->queue
[i
];
1063 struct module_cb
*temp
;
1065 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1067 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1068 temp
->mod_src
= p
->one
->mode
;
1069 temp
->mod_dst
= p
->two
->mode
;
1070 temp
->oid_src
= p
->one
->oid
;
1071 temp
->oid_dst
= p
->two
->oid
;
1072 temp
->status
= p
->status
;
1073 temp
->sm_path
= xstrdup(p
->one
->path
);
1075 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1076 list
->entries
[list
->nr
++] = temp
;
1080 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1083 case DIFF_INDEX
: return "diff-index";
1084 case DIFF_FILES
: return "diff-files";
1085 default: BUG("bad diff_cmd value %d", diff_cmd
);
1089 static int compute_summary_module_list(struct object_id
*head_oid
,
1090 struct summary_cb
*info
,
1091 enum diff_cmd diff_cmd
)
1093 struct strvec diff_args
= STRVEC_INIT
;
1094 struct rev_info rev
;
1095 struct setup_revision_opt opt
= {
1096 .free_removed_argv_elements
= 1,
1098 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1101 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1103 strvec_push(&diff_args
, "--cached");
1104 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1106 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1107 strvec_push(&diff_args
, "--");
1109 strvec_pushv(&diff_args
, info
->argv
);
1111 git_config(git_diff_basic_config
, NULL
);
1112 repo_init_revisions(the_repository
, &rev
, info
->prefix
);
1114 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1115 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1116 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1117 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1118 rev
.diffopt
.format_callback_data
= &list
;
1120 if (!info
->cached
) {
1121 if (diff_cmd
== DIFF_INDEX
)
1123 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1124 perror("repo_read_index_preload");
1128 } else if (repo_read_index(the_repository
) < 0) {
1129 perror("repo_read_cache");
1134 if (diff_cmd
== DIFF_INDEX
)
1135 run_diff_index(&rev
, info
->cached
? DIFF_INDEX_CACHED
: 0);
1137 run_diff_files(&rev
, 0);
1138 prepare_submodule_summary(info
, &list
);
1140 strvec_clear(&diff_args
);
1141 release_revisions(&rev
);
1142 module_cb_list_release(&list
);
1146 static int module_summary(int argc
, const char **argv
, const char *prefix
,
1147 struct repository
*repo UNUSED
)
1149 struct summary_cb info
= SUMMARY_CB_INIT
;
1153 int summary_limit
= -1;
1154 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1155 struct object_id head_oid
;
1157 struct option module_summary_options
[] = {
1158 OPT_BOOL(0, "cached", &cached
,
1159 N_("use the commit stored in the index instead of the submodule HEAD")),
1160 OPT_BOOL(0, "files", &files
,
1161 N_("compare the commit in the index with that in the submodule HEAD")),
1162 OPT_BOOL(0, "for-status", &for_status
,
1163 N_("skip submodules with 'ignore_config' value set to 'all'")),
1164 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1165 N_("limit the summary size")),
1168 const char *const git_submodule_helper_usage
[] = {
1169 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1173 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1174 git_submodule_helper_usage
, 0);
1179 if (!repo_get_oid(the_repository
, argc
? argv
[0] : "HEAD", &head_oid
)) {
1184 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1185 /* before the first commit: compare with an empty tree */
1186 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1192 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
1193 die(_("could not fetch a revision for HEAD"));
1198 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1199 diff_cmd
= DIFF_FILES
;
1204 info
.prefix
= prefix
;
1205 info
.cached
= !!cached
;
1206 info
.files
= !!files
;
1207 info
.for_status
= !!for_status
;
1208 info
.summary_limit
= summary_limit
;
1210 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1217 const char *super_prefix
;
1220 #define SYNC_CB_INIT { 0 }
1222 static void sync_submodule(const char *path
, const char *prefix
,
1223 const char *super_prefix
, unsigned int flags
)
1225 const struct submodule
*sub
;
1226 char *remote_key
= NULL
;
1227 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1228 struct strbuf sb
= STRBUF_INIT
;
1229 char *sub_config_path
= NULL
;
1232 if (!is_submodule_active(the_repository
, path
))
1235 if (validate_submodule_path(path
) < 0)
1238 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
1240 if (sub
&& sub
->url
) {
1241 if (starts_with_dot_dot_slash(sub
->url
) ||
1242 starts_with_dot_slash(sub
->url
)) {
1243 char *up_path
= get_up_path(path
);
1245 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1246 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1249 sub_origin_url
= xstrdup(sub
->url
);
1250 super_config_url
= xstrdup(sub
->url
);
1253 sub_origin_url
= xstrdup("");
1254 super_config_url
= xstrdup("");
1257 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1259 if (!(flags
& OPT_QUIET
))
1260 printf(_("Synchronizing submodule url for '%s'\n"),
1264 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1265 if (git_config_set_gently(sb
.buf
, super_config_url
))
1266 die(_("failed to register url for submodule path '%s'"),
1269 if (!is_submodule_populated_gently(path
, NULL
))
1273 code
= get_default_remote_submodule(path
, &default_remote
);
1277 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1278 free(default_remote
);
1280 submodule_to_gitdir(the_repository
, &sb
, path
);
1281 strbuf_addstr(&sb
, "/config");
1283 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, NULL
, sub_origin_url
))
1284 die(_("failed to update remote for submodule '%s'"),
1287 if (flags
& OPT_RECURSIVE
) {
1288 struct child_process cpr
= CHILD_PROCESS_INIT
;
1292 prepare_submodule_repo_env(&cpr
.env
);
1294 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1295 "--recursive", NULL
);
1296 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
1298 if (flags
& OPT_QUIET
)
1299 strvec_push(&cpr
.args
, "--quiet");
1301 if (run_command(&cpr
))
1302 die(_("failed to recurse into submodule '%s'"),
1307 free(super_config_url
);
1308 free(sub_origin_url
);
1309 strbuf_release(&sb
);
1312 free(sub_config_path
);
1315 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1317 struct sync_cb
*info
= cb_data
;
1319 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1323 static int module_sync(int argc
, const char **argv
, const char *prefix
,
1324 struct repository
*repo UNUSED
)
1326 struct sync_cb info
= SYNC_CB_INIT
;
1327 struct pathspec pathspec
= { 0 };
1328 struct module_list list
= MODULE_LIST_INIT
;
1331 struct option module_sync_options
[] = {
1332 OPT__SUPER_PREFIX(&info
.super_prefix
),
1333 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1334 OPT_BOOL(0, "recursive", &recursive
,
1335 N_("recurse into nested submodules")),
1338 const char *const git_submodule_helper_usage
[] = {
1339 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1344 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1345 git_submodule_helper_usage
, 0);
1347 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1350 info
.prefix
= prefix
;
1352 info
.flags
|= OPT_QUIET
;
1354 info
.flags
|= OPT_RECURSIVE
;
1356 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1360 module_list_release(&list
);
1361 clear_pathspec(&pathspec
);
1369 #define DEINIT_CB_INIT { 0 }
1371 static void deinit_submodule(const char *path
, const char *prefix
,
1374 const struct submodule
*sub
;
1375 char *displaypath
= NULL
;
1376 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1377 struct strbuf sb_config
= STRBUF_INIT
;
1378 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1380 if (validate_submodule_path(path
) < 0)
1383 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
1385 if (!sub
|| !sub
->name
)
1388 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1390 /* remove the submodule work tree (unless the user already did it) */
1391 if (is_directory(path
)) {
1392 struct strbuf sb_rm
= STRBUF_INIT
;
1395 if (is_directory(sub_git_dir
)) {
1396 if (!(flags
& OPT_QUIET
))
1397 warning(_("Submodule work tree '%s' contains a .git "
1398 "directory. This will be replaced with a "
1399 ".git file by using absorbgitdirs."),
1402 absorb_git_dir_into_superproject(path
, NULL
);
1406 if (!(flags
& OPT_FORCE
)) {
1407 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1410 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1413 if (run_command(&cp_rm
))
1414 die(_("Submodule work tree '%s' contains local "
1415 "modifications; use '-f' to discard them"),
1419 strbuf_addstr(&sb_rm
, path
);
1421 if (!remove_dir_recursively(&sb_rm
, 0))
1422 format
= _("Cleared directory '%s'\n");
1424 format
= _("Could not remove submodule work tree '%s'\n");
1426 if (!(flags
& OPT_QUIET
))
1427 printf(format
, displaypath
);
1429 submodule_unset_core_worktree(sub
);
1431 strbuf_release(&sb_rm
);
1434 if (mkdir(path
, 0777))
1435 printf(_("could not create empty submodule directory %s"),
1438 cp_config
.git_cmd
= 1;
1439 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1440 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1442 /* remove the .git/config entries (unless the user already did it) */
1443 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1444 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1447 * remove the whole section so we have a clean state when
1448 * the user later decides to init this submodule again
1450 repo_config_rename_section_in_file(the_repository
, NULL
, sub_key
, NULL
);
1451 if (!(flags
& OPT_QUIET
))
1452 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1453 sub
->name
, sub
->url
, displaypath
);
1460 strbuf_release(&sb_config
);
1463 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1466 struct deinit_cb
*info
= cb_data
;
1467 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1470 static int module_deinit(int argc
, const char **argv
, const char *prefix
,
1471 struct repository
*repo UNUSED
)
1473 struct deinit_cb info
= DEINIT_CB_INIT
;
1474 struct pathspec pathspec
= { 0 };
1475 struct module_list list
= MODULE_LIST_INIT
;
1479 struct option module_deinit_options
[] = {
1480 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1481 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1482 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1485 const char *const git_submodule_helper_usage
[] = {
1486 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1491 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1492 git_submodule_helper_usage
, 0);
1495 error("pathspec and --all are incompatible");
1496 usage_with_options(git_submodule_helper_usage
,
1497 module_deinit_options
);
1501 die(_("Use '--all' if you really want to deinitialize all submodules"));
1503 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1506 info
.prefix
= prefix
;
1508 info
.flags
|= OPT_QUIET
;
1510 info
.flags
|= OPT_FORCE
;
1512 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1516 module_list_release(&list
);
1517 clear_pathspec(&pathspec
);
1521 struct module_clone_data
{
1527 struct list_objects_filter_options
*filter_options
;
1528 enum ref_storage_format ref_storage_format
;
1529 unsigned int quiet
: 1;
1530 unsigned int progress
: 1;
1531 unsigned int dissociate
: 1;
1532 unsigned int require_init
: 1;
1535 #define MODULE_CLONE_DATA_INIT { \
1536 .single_branch = -1, \
1537 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
1540 struct submodule_alternate_setup
{
1541 const char *submodule_name
;
1542 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1543 SUBMODULE_ALTERNATE_ERROR_DIE
,
1544 SUBMODULE_ALTERNATE_ERROR_INFO
,
1545 SUBMODULE_ALTERNATE_ERROR_IGNORE
1547 struct string_list
*reference
;
1549 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1550 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1553 static const char alternate_error_advice
[] = N_(
1554 "An alternate computed from a superproject's alternate is invalid.\n"
1555 "To allow Git to clone without an alternate in such a case, set\n"
1556 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1557 "'--reference-if-able' instead of '--reference'."
1560 static int add_possible_reference_from_superproject(
1561 struct object_directory
*odb
, void *sas_cb
)
1563 struct submodule_alternate_setup
*sas
= sas_cb
;
1567 * If the alternate object store is another repository, try the
1568 * standard layout with .git/(modules/<name>)+/objects
1570 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1571 struct repository alternate
;
1573 struct strbuf sb
= STRBUF_INIT
;
1574 struct strbuf err
= STRBUF_INIT
;
1575 strbuf_add(&sb
, odb
->path
, len
);
1577 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1578 die(_("could not get a repository handle for gitdir '%s'"),
1582 * We need to end the new path with '/' to mark it as a dir,
1583 * otherwise a submodule name containing '/' will be broken
1584 * as the last part of a missing submodule reference would
1585 * be taken as a file name.
1588 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1589 strbuf_addch(&sb
, '/');
1590 repo_clear(&alternate
);
1592 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1594 char *p
= strbuf_detach(&sb
, NULL
);
1596 string_list_append(sas
->reference
, p
)->util
= p
;
1599 switch (sas
->error_mode
) {
1600 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1601 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1602 advise(_(alternate_error_advice
));
1603 die(_("submodule '%s' cannot add alternate: %s"),
1604 sas
->submodule_name
, err
.buf
);
1605 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1606 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1607 sas
->submodule_name
, err
.buf
);
1608 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1613 strbuf_release(&err
);
1614 strbuf_release(&sb
);
1620 static void prepare_possible_alternates(const char *sm_name
,
1621 struct string_list
*reference
)
1623 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1624 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1626 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1630 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1632 if (!error_strategy
)
1633 error_strategy
= xstrdup("die");
1635 sas
.submodule_name
= sm_name
;
1636 sas
.reference
= reference
;
1637 if (!strcmp(error_strategy
, "die"))
1638 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1639 else if (!strcmp(error_strategy
, "info"))
1640 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1641 else if (!strcmp(error_strategy
, "ignore"))
1642 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1644 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1646 if (!strcmp(sm_alternate
, "superproject"))
1647 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1648 else if (!strcmp(sm_alternate
, "no"))
1651 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1654 free(error_strategy
);
1657 static char *clone_submodule_sm_gitdir(const char *name
)
1659 struct strbuf sb
= STRBUF_INIT
;
1662 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1663 sm_gitdir
= absolute_pathdup(sb
.buf
);
1664 strbuf_release(&sb
);
1669 static int dir_contains_only_dotgit(const char *path
)
1671 DIR *dir
= opendir(path
);
1678 e
= readdir_skip_dot_and_dotdot(dir
);
1681 else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT
, e
->d_name
) ||
1682 (e
= readdir_skip_dot_and_dotdot(dir
))) {
1683 error("unexpected item '%s' in '%s'", e
->d_name
, path
);
1691 static int clone_submodule(const struct module_clone_data
*clone_data
,
1692 struct string_list
*reference
)
1695 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1696 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1698 struct child_process cp
= CHILD_PROCESS_INIT
;
1699 const char *clone_data_path
= clone_data
->path
;
1700 char *to_free
= NULL
;
1702 if (validate_submodule_path(clone_data_path
) < 0)
1705 if (!is_absolute_path(clone_data
->path
))
1706 clone_data_path
= to_free
= xstrfmt("%s/%s", repo_get_work_tree(the_repository
),
1709 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1710 die(_("refusing to create/use '%s' in another submodule's "
1711 "git dir"), sm_gitdir
);
1713 if (!file_exists(sm_gitdir
)) {
1714 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1715 !is_empty_dir(clone_data_path
))
1716 die(_("directory not empty: '%s'"), clone_data_path
);
1718 if (safe_create_leading_directories_const(the_repository
, sm_gitdir
) < 0)
1719 die(_("could not create directory '%s'"), sm_gitdir
);
1721 prepare_possible_alternates(clone_data
->name
, reference
);
1723 strvec_push(&cp
.args
, "clone");
1724 strvec_push(&cp
.args
, "--no-checkout");
1725 if (clone_data
->quiet
)
1726 strvec_push(&cp
.args
, "--quiet");
1727 if (clone_data
->progress
)
1728 strvec_push(&cp
.args
, "--progress");
1729 if (clone_data
->depth
> 0)
1730 strvec_pushf(&cp
.args
, "--depth=%d", clone_data
->depth
);
1731 if (reference
->nr
) {
1732 struct string_list_item
*item
;
1734 for_each_string_list_item(item
, reference
)
1735 strvec_pushl(&cp
.args
, "--reference",
1736 item
->string
, NULL
);
1738 if (clone_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
1739 strvec_pushf(&cp
.args
, "--ref-format=%s",
1740 ref_storage_format_to_name(clone_data
->ref_storage_format
));
1741 if (clone_data
->dissociate
)
1742 strvec_push(&cp
.args
, "--dissociate");
1743 if (sm_gitdir
&& *sm_gitdir
)
1744 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1745 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1746 strvec_pushf(&cp
.args
, "--filter=%s",
1747 expand_list_objects_filter_spec(
1748 clone_data
->filter_options
));
1749 if (clone_data
->single_branch
>= 0)
1750 strvec_push(&cp
.args
, clone_data
->single_branch
?
1752 "--no-single-branch");
1754 strvec_push(&cp
.args
, "--");
1755 strvec_push(&cp
.args
, clone_data
->url
);
1756 strvec_push(&cp
.args
, clone_data_path
);
1759 prepare_submodule_repo_env(&cp
.env
);
1762 if(run_command(&cp
))
1763 die(_("clone of '%s' into submodule path '%s' failed"),
1764 clone_data
->url
, clone_data_path
);
1766 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1767 !dir_contains_only_dotgit(clone_data_path
)) {
1768 char *dot_git
= xstrfmt("%s/.git", clone_data_path
);
1771 die(_("directory not empty: '%s'"), clone_data_path
);
1776 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1777 !is_empty_dir(clone_data_path
))
1778 die(_("directory not empty: '%s'"), clone_data_path
);
1779 if (safe_create_leading_directories_const(the_repository
, clone_data_path
) < 0)
1780 die(_("could not create directory '%s'"), clone_data_path
);
1781 path
= xstrfmt("%s/index", sm_gitdir
);
1782 unlink_or_warn(path
);
1787 * We already performed this check at the beginning of this function,
1788 * before cloning the objects. This tries to detect racy behavior e.g.
1789 * in parallel clones, where another process could easily have made the
1790 * gitdir nested _after_ it was created.
1792 * To prevent further harm coming from this unintentionally-nested
1793 * gitdir, let's disable it by deleting the `HEAD` file.
1795 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0) {
1796 char *head
= xstrfmt("%s/HEAD", sm_gitdir
);
1799 die(_("refusing to create/use '%s' in another submodule's "
1800 "git dir"), sm_gitdir
);
1803 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1805 p
= repo_submodule_path(the_repository
, clone_data_path
, "config");
1807 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1809 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1810 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1812 git_config_set_in_file(p
, "submodule.alternateLocation",
1814 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1816 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1820 free(error_strategy
);
1828 static int module_clone(int argc
, const char **argv
, const char *prefix
,
1829 struct repository
*repo UNUSED
)
1831 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1832 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1833 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1834 struct list_objects_filter_options filter_options
=
1835 LIST_OBJECTS_FILTER_INIT
;
1836 const char *ref_storage_format
= NULL
;
1838 struct option module_clone_options
[] = {
1839 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1841 N_("alternative anchor for relative paths")),
1842 OPT_STRING(0, "path", &clone_data
.path
,
1844 N_("where the new submodule will be cloned to")),
1845 OPT_STRING(0, "name", &clone_data
.name
,
1847 N_("name of the new submodule")),
1848 OPT_STRING(0, "url", &clone_data
.url
,
1850 N_("url where to clone the submodule from")),
1851 OPT_STRING_LIST(0, "reference", &reference
,
1853 N_("reference repository")),
1854 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
1855 N_("specify the reference format to use")),
1856 OPT_BOOL(0, "dissociate", &dissociate
,
1857 N_("use --reference only while cloning")),
1858 OPT_INTEGER(0, "depth", &clone_data
.depth
,
1859 N_("depth for shallow clones")),
1860 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1861 OPT_BOOL(0, "progress", &progress
,
1862 N_("force cloning progress")),
1863 OPT_BOOL(0, "require-init", &require_init
,
1864 N_("disallow cloning into non-empty directory")),
1865 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1866 N_("clone only one branch, HEAD or --branch")),
1867 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1870 const char *const git_submodule_helper_usage
[] = {
1871 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1872 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1873 "[--single-branch] [--filter <filter-spec>] "
1874 "--url <url> --path <path>"),
1878 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1879 git_submodule_helper_usage
, 0);
1881 if (ref_storage_format
) {
1882 clone_data
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
1883 if (clone_data
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
1884 die(_("unknown ref storage format '%s'"), ref_storage_format
);
1886 clone_data
.dissociate
= !!dissociate
;
1887 clone_data
.quiet
= !!quiet
;
1888 clone_data
.progress
= !!progress
;
1889 clone_data
.require_init
= !!require_init
;
1890 clone_data
.filter_options
= &filter_options
;
1892 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1893 usage_with_options(git_submodule_helper_usage
,
1894 module_clone_options
);
1896 clone_submodule(&clone_data
, &reference
);
1897 list_objects_filter_release(&filter_options
);
1898 string_list_clear(&reference
, 1);
1902 static int determine_submodule_update_strategy(struct repository
*r
,
1905 enum submodule_update_type update
,
1906 struct submodule_update_strategy
*out
)
1908 const struct submodule
*sub
= submodule_from_path(r
, null_oid(the_hash_algo
), path
);
1913 key
= xstrfmt("submodule.%s.update", sub
->name
);
1917 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1918 if (parse_submodule_update_strategy(val
, out
) < 0) {
1919 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1923 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1924 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1925 BUG("how did we read update = !command from .gitmodules?");
1926 out
->type
= sub
->update_strategy
.type
;
1927 out
->command
= sub
->update_strategy
.command
;
1929 out
->type
= SM_UPDATE_CHECKOUT
;
1932 (out
->type
== SM_UPDATE_MERGE
||
1933 out
->type
== SM_UPDATE_REBASE
||
1934 out
->type
== SM_UPDATE_NONE
))
1935 out
->type
= SM_UPDATE_CHECKOUT
;
1943 struct update_clone_data
{
1944 const struct submodule
*sub
;
1945 struct object_id oid
;
1946 unsigned just_cloned
;
1949 struct submodule_update_clone
{
1950 /* index into 'update_data.list', the list of submodules to look into for cloning */
1953 /* configuration parameters which are passed on to the children */
1954 const struct update_data
*update_data
;
1956 /* to be consumed by update_submodule() */
1957 struct update_clone_data
*update_clone
;
1958 int update_clone_nr
; int update_clone_alloc
;
1960 /* If we want to stop as fast as possible and return an error */
1961 unsigned quickstop
: 1;
1963 /* failed clones to be retried again */
1964 const struct cache_entry
**failed_clones
;
1965 int failed_clones_nr
, failed_clones_alloc
;
1967 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1969 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1971 free(suc
->update_clone
);
1972 free(suc
->failed_clones
);
1975 struct update_data
{
1977 const char *super_prefix
;
1979 enum submodule_update_type update_default
;
1980 struct object_id suboid
;
1981 struct string_list references
;
1982 struct submodule_update_strategy update_strategy
;
1983 struct list_objects_filter_options
*filter_options
;
1984 struct module_list list
;
1985 enum ref_storage_format ref_storage_format
;
1989 int recommend_shallow
;
1990 unsigned int require_init
;
1993 unsigned int nofetch
;
1994 unsigned int remote
;
1995 unsigned int progress
;
1996 unsigned int dissociate
;
1998 unsigned int warn_if_uninitialized
;
1999 unsigned int recursive
;
2001 /* copied over from update_clone_data */
2002 struct object_id oid
;
2003 unsigned int just_cloned
;
2004 const char *sm_path
;
2006 #define UPDATE_DATA_INIT { \
2007 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2008 .list = MODULE_LIST_INIT, \
2009 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
2010 .recommend_shallow = -1, \
2011 .references = STRING_LIST_INIT_DUP, \
2012 .single_branch = -1, \
2016 static void update_data_release(struct update_data
*ud
)
2018 free(ud
->displaypath
);
2019 submodule_update_strategy_release(&ud
->update_strategy
);
2020 module_list_release(&ud
->list
);
2023 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
2024 struct strbuf
*out
, const char *displaypath
)
2027 * Only mention uninitialized submodules when their
2028 * paths have been specified.
2030 if (suc
->update_data
->warn_if_uninitialized
) {
2032 _("Submodule path '%s' not initialized"),
2034 strbuf_addch(out
, '\n');
2036 _("Maybe you want to use 'update --init'?"));
2037 strbuf_addch(out
, '\n');
2042 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2043 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2045 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
2046 struct child_process
*child
,
2047 struct submodule_update_clone
*suc
,
2050 const struct submodule
*sub
= NULL
;
2051 const char *url
= NULL
;
2052 const char *update_string
;
2053 enum submodule_update_type update_type
;
2055 const struct update_data
*ud
= suc
->update_data
;
2056 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
2058 struct strbuf sb
= STRBUF_INIT
;
2059 int needs_cloning
= 0;
2060 int need_free_url
= 0;
2063 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
2064 strbuf_addch(out
, '\n');
2068 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), ce
->name
);
2071 next_submodule_warn_missing(suc
, out
, displaypath
);
2075 key
= xstrfmt("submodule.%s.update", sub
->name
);
2076 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2077 update_type
= parse_submodule_update_type(update_string
);
2079 update_type
= sub
->update_strategy
.type
;
2083 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2084 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2085 && update_type
== SM_UPDATE_NONE
)) {
2086 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2087 strbuf_addch(out
, '\n');
2091 /* Check if the submodule has been initialized. */
2092 if (!is_submodule_active(the_repository
, ce
->name
)) {
2093 next_submodule_warn_missing(suc
, out
, displaypath
);
2098 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2099 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2100 if (sub
->url
&& (starts_with_dot_slash(sub
->url
) ||
2101 starts_with_dot_dot_slash(sub
->url
))) {
2102 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2109 die(_("cannot clone submodule '%s' without a URL"), sub
->name
);
2112 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2113 needs_cloning
= !file_exists(sb
.buf
);
2115 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2116 suc
->update_clone_alloc
);
2117 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2118 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2119 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2120 suc
->update_clone_nr
++;
2126 child
->no_stdin
= 1;
2127 child
->stdout_to_stderr
= 1;
2129 strvec_push(&child
->args
, "submodule--helper");
2130 strvec_push(&child
->args
, "clone");
2131 if (suc
->update_data
->progress
)
2132 strvec_push(&child
->args
, "--progress");
2133 if (suc
->update_data
->quiet
)
2134 strvec_push(&child
->args
, "--quiet");
2135 if (suc
->update_data
->prefix
)
2136 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2137 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2138 strvec_push(&child
->args
, "--depth=1");
2139 else if (suc
->update_data
->depth
)
2140 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2141 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2142 strvec_pushf(&child
->args
, "--filter=%s",
2143 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2144 if (suc
->update_data
->require_init
)
2145 strvec_push(&child
->args
, "--require-init");
2146 if (suc
->update_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
2147 strvec_pushf(&child
->args
, "--ref-format=%s",
2148 ref_storage_format_to_name(suc
->update_data
->ref_storage_format
));
2149 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2150 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2151 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2152 if (suc
->update_data
->references
.nr
) {
2153 struct string_list_item
*item
;
2155 for_each_string_list_item(item
, &suc
->update_data
->references
)
2156 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2158 if (suc
->update_data
->dissociate
)
2159 strvec_push(&child
->args
, "--dissociate");
2160 if (suc
->update_data
->single_branch
>= 0)
2161 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2163 "--no-single-branch");
2167 strbuf_release(&sb
);
2171 return needs_cloning
;
2174 static int update_clone_get_next_task(struct child_process
*child
,
2179 struct submodule_update_clone
*suc
= suc_cb
;
2180 const struct cache_entry
*ce
;
2183 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2184 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2185 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2186 int *p
= xmalloc(sizeof(*p
));
2196 * The loop above tried cloning each submodule once, now try the
2197 * stragglers again, which we can imagine as an extension of the
2200 index
= suc
->current
- suc
->update_data
->list
.nr
;
2201 if (index
< suc
->failed_clones_nr
) {
2204 ce
= suc
->failed_clones
[index
];
2205 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2207 strbuf_addstr(err
, "BUG: submodule considered for "
2208 "cloning, doesn't need cloning "
2212 p
= xmalloc(sizeof(*p
));
2222 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2224 void *idx_task_cb UNUSED
)
2226 struct submodule_update_clone
*suc
= suc_cb
;
2232 static int update_clone_task_finished(int result
,
2237 const struct cache_entry
*ce
;
2238 struct submodule_update_clone
*suc
= suc_cb
;
2239 int *idxP
= idx_task_cb
;
2247 if (idx
< suc
->update_data
->list
.nr
) {
2248 ce
= suc
->update_data
->list
.entries
[idx
];
2249 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2251 strbuf_addch(err
, '\n');
2252 ALLOC_GROW(suc
->failed_clones
,
2253 suc
->failed_clones_nr
+ 1,
2254 suc
->failed_clones_alloc
);
2255 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2258 idx
-= suc
->update_data
->list
.nr
;
2259 ce
= suc
->failed_clones
[idx
];
2260 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2262 strbuf_addch(err
, '\n');
2270 static int git_update_clone_config(const char *var
, const char *value
,
2271 const struct config_context
*ctx
,
2276 if (!strcmp(var
, "submodule.fetchjobs"))
2277 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
2281 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2283 struct child_process cp
= CHILD_PROCESS_INIT
;
2284 struct strbuf rev
= STRBUF_INIT
;
2285 char *hex
= oid_to_hex(oid
);
2291 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2293 prepare_submodule_repo_env(&cp
.env
);
2295 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2300 strbuf_release(&rev
);
2304 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2305 const struct object_id
*oid
)
2307 struct child_process cp
= CHILD_PROCESS_INIT
;
2309 prepare_submodule_repo_env(&cp
.env
);
2311 cp
.dir
= module_path
;
2313 strvec_push(&cp
.args
, "fetch");
2315 strvec_push(&cp
.args
, "--quiet");
2317 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2319 char *hex
= oid_to_hex(oid
);
2323 code
= get_default_remote_submodule(module_path
, &remote
);
2325 child_process_clear(&cp
);
2329 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2333 return run_command(&cp
);
2336 static int run_update_command(const struct update_data
*ud
, int subforce
)
2338 struct child_process cp
= CHILD_PROCESS_INIT
;
2339 char *oid
= oid_to_hex(&ud
->oid
);
2342 switch (ud
->update_strategy
.type
) {
2343 case SM_UPDATE_CHECKOUT
:
2345 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2347 strvec_push(&cp
.args
, "-f");
2349 case SM_UPDATE_REBASE
:
2351 strvec_push(&cp
.args
, "rebase");
2353 strvec_push(&cp
.args
, "--quiet");
2355 case SM_UPDATE_MERGE
:
2357 strvec_push(&cp
.args
, "merge");
2359 strvec_push(&cp
.args
, "--quiet");
2361 case SM_UPDATE_COMMAND
:
2363 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2366 BUG("unexpected update strategy type: %d",
2367 ud
->update_strategy
.type
);
2369 strvec_push(&cp
.args
, oid
);
2371 cp
.dir
= ud
->sm_path
;
2372 prepare_submodule_repo_env(&cp
.env
);
2373 if ((ret
= run_command(&cp
))) {
2374 switch (ud
->update_strategy
.type
) {
2375 case SM_UPDATE_CHECKOUT
:
2376 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2377 oid
, ud
->displaypath
);
2378 /* No "ret" assignment, use "git checkout"'s */
2380 case SM_UPDATE_REBASE
:
2381 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2382 oid
, ud
->displaypath
);
2384 case SM_UPDATE_MERGE
:
2385 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2386 oid
, ud
->displaypath
);
2388 case SM_UPDATE_COMMAND
:
2389 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2390 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2393 BUG("unexpected update strategy type: %d",
2394 ud
->update_strategy
.type
);
2403 switch (ud
->update_strategy
.type
) {
2404 case SM_UPDATE_CHECKOUT
:
2405 printf(_("Submodule path '%s': checked out '%s'\n"),
2406 ud
->displaypath
, oid
);
2408 case SM_UPDATE_REBASE
:
2409 printf(_("Submodule path '%s': rebased into '%s'\n"),
2410 ud
->displaypath
, oid
);
2412 case SM_UPDATE_MERGE
:
2413 printf(_("Submodule path '%s': merged in '%s'\n"),
2414 ud
->displaypath
, oid
);
2416 case SM_UPDATE_COMMAND
:
2417 printf(_("Submodule path '%s': '%s %s'\n"),
2418 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2421 BUG("unexpected update strategy type: %d",
2422 ud
->update_strategy
.type
);
2428 static int run_update_procedure(const struct update_data
*ud
)
2430 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2434 * Run fetch only if `oid` isn't present or it
2435 * is not reachable from a ref.
2437 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2438 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2441 _("Unable to fetch in submodule path '%s'; "
2442 "trying to directly fetch %s:"),
2443 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2445 * Now we tried the usual fetch, but `oid` may
2446 * not be reachable from any of the refs.
2448 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2449 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2450 return die_message(_("Fetched in submodule path '%s', but it did not "
2451 "contain %s. Direct fetching of that commit failed."),
2452 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2455 return run_update_command(ud
, subforce
);
2458 static int remote_submodule_branch(const char *path
, const char **branch
)
2460 const struct submodule
*sub
;
2464 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
2466 return die_message(_("could not initialize submodule at path '%s'"),
2469 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2470 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2471 *branch
= sub
->branch
;
2479 if (!strcmp(*branch
, ".")) {
2480 const char *refname
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
2485 return die_message(_("No such ref: %s"), "HEAD");
2488 if (!strcmp(refname
, "HEAD"))
2489 return die_message(_("Submodule (%s) branch configured to inherit "
2490 "branch from superproject, but the superproject "
2491 "is not on any branch"), sub
->name
);
2493 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2494 return die_message(_("Expecting a full ref name, got %s"),
2501 /* Our "branch" is coming from repo_config_get_string_tmp() */
2505 static int ensure_core_worktree(const char *path
)
2508 struct repository subrepo
;
2510 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid(the_hash_algo
)))
2511 return die_message(_("could not get a repository handle for submodule '%s'"),
2514 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2515 char *cfg_file
, *abs_path
;
2516 const char *rel_path
;
2517 struct strbuf sb
= STRBUF_INIT
;
2519 cfg_file
= repo_git_path(&subrepo
, "config");
2521 abs_path
= absolute_pathdup(path
);
2522 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2524 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2528 strbuf_release(&sb
);
2531 repo_clear(&subrepo
);
2535 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2538 case SM_UPDATE_CHECKOUT
:
2540 case SM_UPDATE_MERGE
:
2542 case SM_UPDATE_REBASE
:
2544 case SM_UPDATE_UNSPECIFIED
:
2545 case SM_UPDATE_NONE
:
2546 case SM_UPDATE_COMMAND
:
2549 BUG("unreachable with type %d", type
);
2552 static void update_data_to_args(const struct update_data
*update_data
,
2553 struct strvec
*args
)
2555 enum submodule_update_type update_type
= update_data
->update_default
;
2557 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2558 if (update_data
->displaypath
)
2559 strvec_pushf(args
, "--super-prefix=%s/",
2560 update_data
->displaypath
);
2561 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2562 if (update_data
->quiet
)
2563 strvec_push(args
, "--quiet");
2564 if (update_data
->force
)
2565 strvec_push(args
, "--force");
2566 if (update_data
->init
)
2567 strvec_push(args
, "--init");
2568 if (update_data
->remote
)
2569 strvec_push(args
, "--remote");
2570 if (update_data
->nofetch
)
2571 strvec_push(args
, "--no-fetch");
2572 if (update_data
->dissociate
)
2573 strvec_push(args
, "--dissociate");
2574 if (update_data
->progress
)
2575 strvec_push(args
, "--progress");
2576 if (update_data
->require_init
)
2577 strvec_push(args
, "--require-init");
2578 if (update_data
->depth
)
2579 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2580 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2581 strvec_pushf(args
, "--%s",
2582 submodule_update_type_to_label(update_type
));
2584 if (update_data
->references
.nr
) {
2585 struct string_list_item
*item
;
2587 for_each_string_list_item(item
, &update_data
->references
)
2588 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2590 if (update_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
2591 strvec_pushf(args
, "--ref-format=%s",
2592 ref_storage_format_to_name(update_data
->ref_storage_format
));
2593 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2594 strvec_pushf(args
, "--filter=%s",
2595 expand_list_objects_filter_spec(
2596 update_data
->filter_options
));
2597 if (update_data
->recommend_shallow
== 0)
2598 strvec_push(args
, "--no-recommend-shallow");
2599 else if (update_data
->recommend_shallow
== 1)
2600 strvec_push(args
, "--recommend-shallow");
2601 if (update_data
->single_branch
>= 0)
2602 strvec_push(args
, update_data
->single_branch
?
2604 "--no-single-branch");
2607 static int update_submodule(struct update_data
*update_data
)
2611 if (validate_submodule_path(update_data
->sm_path
) < 0)
2614 ret
= determine_submodule_update_strategy(the_repository
,
2615 update_data
->just_cloned
,
2616 update_data
->sm_path
,
2617 update_data
->update_default
,
2618 &update_data
->update_strategy
);
2622 if (update_data
->just_cloned
)
2623 oidcpy(&update_data
->suboid
, null_oid(the_hash_algo
));
2624 else if (repo_resolve_gitlink_ref(the_repository
, update_data
->sm_path
,
2625 "HEAD", &update_data
->suboid
))
2626 return die_message(_("Unable to find current revision in submodule path '%s'"),
2627 update_data
->displaypath
);
2629 if (update_data
->remote
) {
2635 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2638 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2643 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2647 if (!update_data
->nofetch
) {
2648 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2651 return die_message(_("Unable to fetch in submodule path '%s'"),
2652 update_data
->sm_path
);
2656 if (repo_resolve_gitlink_ref(the_repository
, update_data
->sm_path
,
2657 remote_ref
, &update_data
->oid
)) {
2658 ret
= die_message(_("Unable to find %s revision in submodule path '%s'"),
2659 remote_ref
, update_data
->sm_path
);
2667 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2668 ret
= run_update_procedure(update_data
);
2673 if (update_data
->recursive
) {
2674 struct child_process cp
= CHILD_PROCESS_INIT
;
2675 struct update_data next
= *update_data
;
2678 oidcpy(&next
.oid
, null_oid(the_hash_algo
));
2679 oidcpy(&next
.suboid
, null_oid(the_hash_algo
));
2681 cp
.dir
= update_data
->sm_path
;
2683 prepare_submodule_repo_env(&cp
.env
);
2684 update_data_to_args(&next
, &cp
.args
);
2686 ret
= run_command(&cp
);
2688 die_message(_("Failed to recurse into submodule path '%s'"),
2689 update_data
->displaypath
);
2696 static int update_submodules(struct update_data
*update_data
)
2699 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2700 const struct run_process_parallel_opts opts
= {
2701 .tr2_category
= "submodule",
2702 .tr2_label
= "parallel/update",
2704 .processes
= update_data
->max_jobs
,
2706 .get_next_task
= update_clone_get_next_task
,
2707 .start_failure
= update_clone_start_failure
,
2708 .task_finished
= update_clone_task_finished
,
2712 suc
.update_data
= update_data
;
2713 run_processes_parallel(&opts
);
2716 * We saved the output and put it out all at once now.
2718 * - the listener does not have to interleave their (checkout)
2719 * work with our fetching. The writes involved in a
2720 * checkout involve more straightforward sequential I/O.
2721 * - the listener can avoid doing any work if fetching failed.
2723 if (suc
.quickstop
) {
2728 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2729 struct update_clone_data ucd
= suc
.update_clone
[i
];
2732 oidcpy(&update_data
->oid
, &ucd
.oid
);
2733 update_data
->just_cloned
= ucd
.just_cloned
;
2734 update_data
->sm_path
= ucd
.sub
->path
;
2737 * Verify that the submodule path does not contain any
2738 * symlinks; if it does, it might have been tampered with.
2739 * TODO: allow exempting it via
2740 * `safe.submodule.path` or something
2742 if (validate_submodule_path(update_data
->sm_path
) < 0)
2745 code
= ensure_core_worktree(update_data
->sm_path
);
2749 update_data
->displaypath
= get_submodule_displaypath(
2750 update_data
->sm_path
, update_data
->prefix
,
2751 update_data
->super_prefix
);
2752 code
= update_submodule(update_data
);
2753 FREE_AND_NULL(update_data
->displaypath
);
2763 submodule_update_clone_release(&suc
);
2764 string_list_clear(&update_data
->references
, 0);
2768 static int module_update(int argc
, const char **argv
, const char *prefix
,
2769 struct repository
*repo UNUSED
)
2771 struct pathspec pathspec
= { 0 };
2772 struct pathspec pathspec2
= { 0 };
2773 struct update_data opt
= UPDATE_DATA_INIT
;
2774 struct list_objects_filter_options filter_options
=
2775 LIST_OBJECTS_FILTER_INIT
;
2776 const char *ref_storage_format
= NULL
;
2778 struct option module_update_options
[] = {
2779 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2780 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2781 OPT_BOOL(0, "init", &opt
.init
,
2782 N_("initialize uninitialized submodules before update")),
2783 OPT_BOOL(0, "remote", &opt
.remote
,
2784 N_("use SHA-1 of submodule's remote tracking branch")),
2785 OPT_BOOL(0, "recursive", &opt
.recursive
,
2786 N_("traverse submodules recursively")),
2787 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2788 N_("don't fetch new objects from the remote site")),
2789 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2790 N_("use the 'checkout' update strategy (default)"),
2791 SM_UPDATE_CHECKOUT
),
2792 OPT_SET_INT('m', "merge", &opt
.update_default
,
2793 N_("use the 'merge' update strategy"),
2795 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2796 N_("use the 'rebase' update strategy"),
2798 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2799 N_("reference repository")),
2800 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
2801 N_("specify the reference format to use")),
2802 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2803 N_("use --reference only while cloning")),
2804 OPT_INTEGER(0, "depth", &opt
.depth
,
2805 N_("create a shallow clone truncated to the "
2806 "specified number of revisions")),
2807 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2808 N_("parallel jobs")),
2809 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2810 N_("whether the initial clone should follow the shallow recommendation")),
2811 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2812 OPT_BOOL(0, "progress", &opt
.progress
,
2813 N_("force cloning progress")),
2814 OPT_BOOL(0, "require-init", &opt
.require_init
,
2815 N_("disallow cloning into non-empty directory, implies --init")),
2816 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2817 N_("clone only one branch, HEAD or --branch")),
2818 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2821 const char *const git_submodule_helper_usage
[] = {
2822 N_("git submodule [--quiet] update"
2823 " [--init [--filter=<filter-spec>]] [--remote]"
2824 " [-N|--no-fetch] [-f|--force]"
2825 " [--checkout|--merge|--rebase]"
2826 " [--[no-]recommend-shallow] [--reference <repository>]"
2827 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2831 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2832 git_config(git_update_clone_config
, &opt
.max_jobs
);
2834 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2835 git_submodule_helper_usage
, 0);
2837 if (opt
.require_init
)
2840 if (filter_options
.choice
&& !opt
.init
) {
2841 usage_with_options(git_submodule_helper_usage
,
2842 module_update_options
);
2845 if (ref_storage_format
) {
2846 opt
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
2847 if (opt
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
2848 die(_("unknown ref storage format '%s'"), ref_storage_format
);
2851 opt
.filter_options
= &filter_options
;
2852 opt
.prefix
= prefix
;
2854 if (opt
.update_default
)
2855 opt
.update_strategy
.type
= opt
.update_default
;
2857 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2863 opt
.warn_if_uninitialized
= 1;
2866 struct module_list list
= MODULE_LIST_INIT
;
2867 struct init_cb info
= INIT_CB_INIT
;
2869 if (module_list_compute(argv
, opt
.prefix
,
2870 &pathspec2
, &list
) < 0) {
2871 module_list_release(&list
);
2877 * If there are no path args and submodule.active is set then,
2878 * by default, only initialize 'active' modules.
2880 if (!argc
&& !git_config_get("submodule.active"))
2881 module_list_active(&list
);
2883 info
.prefix
= opt
.prefix
;
2884 info
.super_prefix
= opt
.super_prefix
;
2886 info
.flags
|= OPT_QUIET
;
2888 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2889 module_list_release(&list
);
2892 ret
= update_submodules(&opt
);
2894 update_data_release(&opt
);
2895 list_objects_filter_release(&filter_options
);
2896 clear_pathspec(&pathspec
);
2897 clear_pathspec(&pathspec2
);
2901 static int push_check(int argc
, const char **argv
, const char *prefix UNUSED
,
2902 struct repository
*repo UNUSED
)
2904 struct remote
*remote
;
2905 const char *superproject_head
;
2907 int detached_head
= 0;
2908 struct object_id head_oid
;
2911 die("submodule--helper push-check requires at least 2 arguments");
2914 * superproject's resolved head ref.
2915 * if HEAD then the superproject is in a detached head state, otherwise
2916 * it will be the resolved head ref.
2918 superproject_head
= argv
[1];
2921 /* Get the submodule's head ref and determine if it is detached */
2922 head
= refs_resolve_refdup(get_main_ref_store(the_repository
), "HEAD",
2923 0, &head_oid
, NULL
);
2925 die(_("Failed to resolve HEAD as a valid ref."));
2926 if (!strcmp(head
, "HEAD"))
2930 * The remote must be configured.
2931 * This is to avoid pushing to the exact same URL as the parent.
2933 remote
= pushremote_get(argv
[1]);
2934 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2935 die("remote '%s' not configured", argv
[1]);
2937 /* Check the refspec */
2940 struct ref
*local_refs
= get_local_heads();
2941 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2943 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2945 for (i
= 0; i
< refspec
.nr
; i
++) {
2946 const struct refspec_item
*rs
= &refspec
.items
[i
];
2948 if (rs
->pattern
|| rs
->matching
)
2951 /* LHS must match a single ref */
2952 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2957 * If LHS matches 'HEAD' then we need to ensure
2958 * that it matches the same named branch
2959 * checked out in the superproject.
2961 if (!strcmp(rs
->src
, "HEAD")) {
2962 if (!detached_head
&&
2963 !strcmp(head
, superproject_head
))
2965 die("HEAD does not match the named branch in the superproject");
2969 die("src refspec '%s' must name a ref",
2974 refspec_clear(&refspec
);
2975 free_refs(local_refs
);
2982 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
,
2983 struct repository
*repo UNUSED
)
2986 struct pathspec pathspec
= { 0 };
2987 struct module_list list
= MODULE_LIST_INIT
;
2988 const char *super_prefix
= NULL
;
2989 struct option embed_gitdir_options
[] = {
2990 OPT__SUPER_PREFIX(&super_prefix
),
2993 const char *const git_submodule_helper_usage
[] = {
2994 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2999 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
3000 git_submodule_helper_usage
, 0);
3002 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
3005 for (i
= 0; i
< list
.nr
; i
++)
3006 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
3011 clear_pathspec(&pathspec
);
3012 module_list_release(&list
);
3016 static int module_set_url(int argc
, const char **argv
, const char *prefix
,
3017 struct repository
*repo UNUSED
)
3023 struct option options
[] = {
3024 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
3027 const char *const usage
[] = {
3028 N_("git submodule set-url [--quiet] <path> <newurl>"),
3031 const struct submodule
*sub
;
3033 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3035 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
3036 usage_with_options(usage
, options
);
3038 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
3041 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3044 config_name
= xstrfmt("submodule.%s.url", sub
->name
);
3045 ret
= config_set_in_gitmodules_file_gently(config_name
, newurl
);
3048 repo_read_gitmodules(the_repository
, 0);
3049 sync_submodule(sub
->path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
3056 static int module_set_branch(int argc
, const char **argv
, const char *prefix
,
3057 struct repository
*repo UNUSED
)
3059 int opt_default
= 0, ret
;
3060 const char *opt_branch
= NULL
;
3063 struct option options
[] = {
3065 * We accept the `quiet` option for uniformity across subcommands,
3066 * though there is nothing to make less verbose in this subcommand.
3068 OPT_NOOP_NOARG('q', "quiet"),
3070 OPT_BOOL('d', "default", &opt_default
,
3071 N_("set the default tracking branch to master")),
3072 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
3073 N_("set the default tracking branch")),
3076 const char *const usage
[] = {
3077 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3078 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3081 const struct submodule
*sub
;
3083 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3085 if (!opt_branch
&& !opt_default
)
3086 die(_("--branch or --default required"));
3088 if (opt_branch
&& opt_default
)
3089 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3091 if (argc
!= 1 || !(path
= argv
[0]))
3092 usage_with_options(usage
, options
);
3094 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
3097 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3100 config_name
= xstrfmt("submodule.%s.branch", sub
->name
);
3101 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
3107 static int module_create_branch(int argc
, const char **argv
, const char *prefix
,
3108 struct repository
*repo UNUSED
)
3110 enum branch_track track
;
3111 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
3112 struct option options
[] = {
3113 OPT__QUIET(&quiet
, N_("print only error messages")),
3114 OPT__FORCE(&force
, N_("force creation"), 0),
3115 OPT_BOOL(0, "create-reflog", &reflog
,
3116 N_("create the branch's reflog")),
3117 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
3118 N_("set branch tracking configuration"),
3120 parse_opt_tracking_mode
),
3121 OPT__DRY_RUN(&dry_run
,
3122 N_("show whether the branch would be created")),
3125 const char *const usage
[] = {
3126 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3130 git_config(git_default_config
, NULL
);
3131 track
= git_branch_track
;
3132 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3135 usage_with_options(usage
, options
);
3137 if (!quiet
&& !dry_run
)
3138 printf_ln(_("creating branch '%s'"), argv
[0]);
3140 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
3141 force
, reflog
, quiet
, track
, dry_run
);
3148 const char *reference_path
;
3150 const char *sm_name
;
3152 const char *realrepo
;
3153 enum ref_storage_format ref_storage_format
;
3155 unsigned int force
: 1;
3156 unsigned int quiet
: 1;
3157 unsigned int progress
: 1;
3158 unsigned int dissociate
: 1;
3160 #define ADD_DATA_INIT { \
3162 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
3165 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3167 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3168 struct strbuf sb_remote_out
= STRBUF_INIT
;
3170 cp_remote
.git_cmd
= 1;
3171 strvec_pushf(&cp_remote
.env
,
3172 "GIT_DIR=%s", git_dir_path
);
3173 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3174 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3175 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3177 char *line
= sb_remote_out
.buf
;
3179 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3180 size_t len
= next_line
- line
;
3182 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3183 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3184 line
= next_line
+ 1;
3188 strbuf_release(&sb_remote_out
);
3191 static int add_submodule(const struct add_data
*add_data
)
3193 char *submod_gitdir_path
;
3194 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3195 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3198 /* perhaps the path already exists and is already a git repo, else clone it */
3199 if (is_directory(add_data
->sm_path
)) {
3200 struct strbuf sm_path
= STRBUF_INIT
;
3201 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3202 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3203 if (is_nonbare_repository_dir(&sm_path
))
3204 printf(_("Adding existing repo at '%s' to the index\n"),
3207 die(_("'%s' already exists and is not a valid git repo"),
3209 strbuf_release(&sm_path
);
3210 free(submod_gitdir_path
);
3212 struct child_process cp
= CHILD_PROCESS_INIT
;
3214 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3216 if (is_directory(submod_gitdir_path
)) {
3217 if (!add_data
->force
) {
3218 struct strbuf msg
= STRBUF_INIT
;
3221 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3222 "locally with remote(s):\n"),
3225 append_fetch_remotes(&msg
, submod_gitdir_path
);
3226 free(submod_gitdir_path
);
3228 strbuf_addf(&msg
, _("If you want to reuse this local git "
3229 "directory instead of cloning again from\n"
3231 "use the '--force' option. If the local git "
3232 "directory is not the correct repo\n"
3233 "or you are unsure what this means choose "
3234 "another name with the '--name' option."),
3235 add_data
->realrepo
);
3237 die_msg
= strbuf_detach(&msg
, NULL
);
3240 printf(_("Reactivating local git directory for "
3241 "submodule '%s'\n"), add_data
->sm_name
);
3244 free(submod_gitdir_path
);
3246 clone_data
.prefix
= add_data
->prefix
;
3247 clone_data
.path
= add_data
->sm_path
;
3248 clone_data
.name
= add_data
->sm_name
;
3249 clone_data
.url
= add_data
->realrepo
;
3250 clone_data
.quiet
= add_data
->quiet
;
3251 clone_data
.progress
= add_data
->progress
;
3252 if (add_data
->reference_path
) {
3253 char *p
= xstrdup(add_data
->reference_path
);
3255 string_list_append(&reference
, p
)->util
= p
;
3257 clone_data
.ref_storage_format
= add_data
->ref_storage_format
;
3258 clone_data
.dissociate
= add_data
->dissociate
;
3259 if (add_data
->depth
>= 0)
3260 clone_data
.depth
= add_data
->depth
;
3262 if (clone_submodule(&clone_data
, &reference
))
3265 prepare_submodule_repo_env(&cp
.env
);
3267 cp
.dir
= add_data
->sm_path
;
3269 * NOTE: we only get here if add_data->force is true, so
3270 * passing --force to checkout is reasonable.
3272 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3274 if (add_data
->branch
) {
3275 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3276 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3279 if (run_command(&cp
))
3280 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3285 string_list_clear(&reference
, 1);
3289 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3294 if (!is_writing_gitmodules_ok())
3295 die(_("please make sure that the .gitmodules file is in the working tree"));
3297 key
= xstrfmt("submodule.%s.%s", name
, var
);
3298 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3304 static void configure_added_submodule(struct add_data
*add_data
)
3307 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3308 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3310 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3311 git_config_set_gently(key
, add_data
->realrepo
);
3314 add_submod
.git_cmd
= 1;
3315 strvec_pushl(&add_submod
.args
, "add",
3316 "--no-warn-embedded-repo", NULL
);
3317 if (add_data
->force
)
3318 strvec_push(&add_submod
.args
, "--force");
3319 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3321 if (run_command(&add_submod
))
3322 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3324 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3325 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3326 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3328 if (add_data
->branch
) {
3329 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3330 "branch", add_data
->branch
))
3331 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3334 add_gitmodules
.git_cmd
= 1;
3335 strvec_pushl(&add_gitmodules
.args
,
3336 "add", "--force", "--", ".gitmodules", NULL
);
3338 if (run_command(&add_gitmodules
))
3339 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3342 * NEEDSWORK: In a multi-working-tree world this needs to be
3343 * set in the per-worktree config.
3346 * NEEDSWORK: In the longer run, we need to get rid of this
3347 * pattern of querying "submodule.active" before calling
3348 * is_submodule_active(), since that function needs to find
3349 * out the value of "submodule.active" again anyway.
3351 if (!git_config_get("submodule.active")) {
3353 * If the submodule being added isn't already covered by the
3354 * current configured pathspec, set the submodule's active flag
3356 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3357 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3358 git_config_set_gently(key
, "true");
3362 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3363 git_config_set_gently(key
, "true");
3368 static void die_on_index_match(const char *path
, int force
)
3371 const char *args
[] = { path
, NULL
};
3372 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3374 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3375 die(_("index file corrupt"));
3378 char *ps_matched
= xcalloc(ps
.nr
, 1);
3380 /* TODO: audit for interaction with sparse-index. */
3381 ensure_full_index(the_repository
->index
);
3384 * Since there is only one pathspec, we just need to
3385 * check ps_matched[0] to know if a cache entry matched.
3387 for (size_t i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
3388 ce_path_match(the_repository
->index
, the_repository
->index
->cache
[i
], &ps
,
3391 if (ps_matched
[0]) {
3393 die(_("'%s' already exists in the index"),
3395 if (!S_ISGITLINK(the_repository
->index
->cache
[i
]->ce_mode
))
3396 die(_("'%s' already exists in the index "
3397 "and is not a submodule"), path
);
3403 clear_pathspec(&ps
);
3406 static void die_on_repo_without_commits(const char *path
)
3408 struct strbuf sb
= STRBUF_INIT
;
3409 strbuf_addstr(&sb
, path
);
3410 if (is_nonbare_repository_dir(&sb
)) {
3411 struct object_id oid
;
3412 if (repo_resolve_gitlink_ref(the_repository
, path
, "HEAD", &oid
) < 0)
3413 die(_("'%s' does not have a commit checked out"), path
);
3415 strbuf_release(&sb
);
3418 static int module_add(int argc
, const char **argv
, const char *prefix
,
3419 struct repository
*repo UNUSED
)
3421 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3422 struct add_data add_data
= ADD_DATA_INIT
;
3423 const char *ref_storage_format
= NULL
;
3424 char *to_free
= NULL
;
3425 struct option options
[] = {
3426 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3427 N_("branch of repository to add as submodule")),
3428 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3429 PARSE_OPT_NOCOMPLETE
),
3430 OPT__QUIET(&quiet
, N_("print only error messages")),
3431 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3432 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3433 N_("reference repository")),
3434 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
3435 N_("specify the reference format to use")),
3436 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3437 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3438 N_("sets the submodule's name to the given string "
3439 "instead of defaulting to its path")),
3440 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3443 const char *const usage
[] = {
3444 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3447 struct strbuf sb
= STRBUF_INIT
;
3450 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3452 if (!is_writing_gitmodules_ok())
3453 die(_("please make sure that the .gitmodules file is in the working tree"));
3455 if (prefix
&& *prefix
&&
3456 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3457 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3459 if (argc
== 0 || argc
> 2)
3460 usage_with_options(usage
, options
);
3462 if (ref_storage_format
) {
3463 add_data
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
3464 if (add_data
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
3465 die(_("unknown ref storage format '%s'"), ref_storage_format
);
3468 add_data
.repo
= argv
[0];
3470 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3472 add_data
.sm_path
= xstrdup(argv
[1]);
3474 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3475 char *sm_path
= add_data
.sm_path
;
3477 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3481 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3482 starts_with_dot_slash(add_data
.repo
)) {
3484 die(_("Relative path can only be used from the toplevel "
3485 "of the working tree"));
3487 /* dereference source url relative to parent's url */
3488 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3489 add_data
.realrepo
= to_free
;
3490 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3491 add_data
.realrepo
= add_data
.repo
;
3493 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3499 * multiple //; leading ./; /./; /../;
3501 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3502 strip_dir_trailing_slashes(add_data
.sm_path
);
3504 if (validate_submodule_path(add_data
.sm_path
) < 0)
3507 die_on_index_match(add_data
.sm_path
, force
);
3508 die_on_repo_without_commits(add_data
.sm_path
);
3511 struct child_process cp
= CHILD_PROCESS_INIT
;
3515 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3516 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3517 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3518 strbuf_complete_line(&sb
);
3519 fputs(sb
.buf
, stderr
);
3524 if(!add_data
.sm_name
)
3525 add_data
.sm_name
= add_data
.sm_path
;
3527 if (check_submodule_name(add_data
.sm_name
))
3528 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3530 add_data
.prefix
= prefix
;
3531 add_data
.force
= !!force
;
3532 add_data
.quiet
= !!quiet
;
3533 add_data
.progress
= !!progress
;
3534 add_data
.dissociate
= !!dissociate
;
3536 if (add_submodule(&add_data
))
3538 configure_added_submodule(&add_data
);
3542 free(add_data
.sm_path
);
3544 strbuf_release(&sb
);
3549 int cmd_submodule__helper(int argc
,
3552 struct repository
*repo
)
3554 parse_opt_subcommand_fn
*fn
= NULL
;
3555 const char *const usage
[] = {
3556 N_("git submodule--helper <command>"),
3559 struct option options
[] = {
3560 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3561 OPT_SUBCOMMAND("add", &fn
, module_add
),
3562 OPT_SUBCOMMAND("update", &fn
, module_update
),
3563 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3564 OPT_SUBCOMMAND("init", &fn
, module_init
),
3565 OPT_SUBCOMMAND("status", &fn
, module_status
),
3566 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3567 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3568 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3569 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3570 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3571 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3572 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3573 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3576 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3578 return fn(argc
, argv
, prefix
, repo
);