1 #define USE_THE_REPOSITORY_VARIABLE
5 #include "environment.h"
10 #include "parse-options.h"
14 #include "preload-index.h"
16 #include "read-cache.h"
18 #include "sparse-index.h"
19 #include "submodule.h"
20 #include "submodule-config.h"
21 #include "string-list.h"
22 #include "run-command.h"
29 #include "object-file.h"
30 #include "object-name.h"
34 #include "list-objects-filter-options.h"
35 #include "wildmatch.h"
38 #define OPT_QUIET (1 << 0)
39 #define OPT_CACHED (1 << 1)
40 #define OPT_RECURSIVE (1 << 2)
41 #define OPT_FORCE (1 << 3)
43 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
46 static char *get_default_remote(void)
48 return xstrdup(repo_default_remote(the_repository
));
51 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
53 char *remoteurl
, *resolved_url
;
54 char *remote
= get_default_remote();
55 struct strbuf remotesb
= STRBUF_INIT
;
57 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
58 if (repo_config_get_string(the_repository
, remotesb
.buf
, &remoteurl
)) {
60 warning(_("could not look up configuration '%s'. "
61 "Assuming this repository is its own "
62 "authoritative upstream."),
64 remoteurl
= xgetcwd();
66 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
70 strbuf_release(&remotesb
);
75 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
77 const struct submodule
*sub
;
78 struct repository subrepo
;
79 const char *remote_name
= NULL
;
82 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), module_path
);
83 if (sub
&& sub
->url
) {
84 url
= xstrdup(sub
->url
);
86 /* Possibly a url relative to parent */
87 if (starts_with_dot_dot_slash(url
) ||
88 starts_with_dot_slash(url
)) {
91 url
= resolve_relative_url(oldurl
, NULL
, 1);
96 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
97 null_oid(the_hash_algo
)) < 0)
98 return die_message(_("could not get a repository handle for submodule '%s'"),
101 /* Look up by URL first */
103 remote_name
= repo_remote_from_url(&subrepo
, url
);
105 remote_name
= repo_default_remote(&subrepo
);
107 *default_remote
= xstrdup(remote_name
);
109 repo_clear(&subrepo
);
115 /* the result should be freed by the caller. */
116 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
117 const char *super_prefix
)
119 if (prefix
&& super_prefix
) {
120 BUG("cannot have prefix '%s' and superprefix '%s'",
121 prefix
, super_prefix
);
123 struct strbuf sb
= STRBUF_INIT
;
124 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
127 } else if (super_prefix
) {
128 return xstrfmt("%s%s", super_prefix
, path
);
130 return xstrdup(path
);
134 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
136 struct strbuf sb
= STRBUF_INIT
;
139 static const char *describe_bare
[] = { NULL
};
141 static const char *describe_tags
[] = { "--tags", NULL
};
143 static const char *describe_contains
[] = { "--contains", NULL
};
145 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
147 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
149 describe_all_always
, NULL
};
151 for (d
= describe_argv
; *d
; d
++) {
152 struct child_process cp
= CHILD_PROCESS_INIT
;
153 prepare_submodule_repo_env(&cp
.env
);
158 strvec_push(&cp
.args
, "describe");
159 strvec_pushv(&cp
.args
, *d
);
160 strvec_push(&cp
.args
, object_id
);
162 if (!capture_command(&cp
, &sb
, 0)) {
163 strbuf_strip_suffix(&sb
, "\n");
164 return strbuf_detach(&sb
, NULL
);
173 const struct cache_entry
**entries
;
176 #define MODULE_LIST_INIT { 0 }
178 static void module_list_release(struct module_list
*ml
)
183 static int module_list_compute(const char **argv
,
185 struct pathspec
*pathspec
,
186 struct module_list
*list
)
189 char *ps_matched
= NULL
;
191 parse_pathspec(pathspec
, 0,
192 PATHSPEC_PREFER_FULL
,
196 ps_matched
= xcalloc(pathspec
->nr
, 1);
198 if (repo_read_index(the_repository
) < 0)
199 die(_("index file corrupt"));
201 for (size_t i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
202 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
204 if (!match_pathspec(the_repository
->index
, pathspec
, ce
->name
, ce_namelen(ce
),
206 !S_ISGITLINK(ce
->ce_mode
))
209 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
210 list
->entries
[list
->nr
++] = ce
;
211 while (i
+ 1 < the_repository
->index
->cache_nr
&&
212 !strcmp(ce
->name
, the_repository
->index
->cache
[i
+ 1]->name
))
214 * Skip entries with the same name in different stages
215 * to make sure an entry is returned only once.
220 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
228 static void module_list_active(struct module_list
*list
)
231 struct module_list active_modules
= MODULE_LIST_INIT
;
233 for (i
= 0; i
< list
->nr
; i
++) {
234 const struct cache_entry
*ce
= list
->entries
[i
];
236 if (!is_submodule_active(the_repository
, ce
->name
))
239 ALLOC_GROW(active_modules
.entries
,
240 active_modules
.nr
+ 1,
241 active_modules
.alloc
);
242 active_modules
.entries
[active_modules
.nr
++] = ce
;
245 module_list_release(list
);
246 *list
= active_modules
;
249 static char *get_up_path(const char *path
)
251 struct strbuf sb
= STRBUF_INIT
;
253 strbuf_addstrings(&sb
, "../", count_slashes(path
));
256 * Check if 'path' ends with slash or not
257 * for having the same output for dir/sub_dir
260 if (!is_dir_sep(path
[strlen(path
) - 1]))
261 strbuf_addstr(&sb
, "../");
263 return strbuf_detach(&sb
, NULL
);
266 static void for_each_listed_submodule(const struct module_list
*list
,
267 each_submodule_fn fn
, void *cb_data
)
271 for (i
= 0; i
< list
->nr
; i
++)
272 fn(list
->entries
[i
], cb_data
);
279 const char *super_prefix
;
283 #define FOREACH_CB_INIT { 0 }
285 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
288 struct foreach_cb
*info
= cb_data
;
289 const char *path
= list_item
->name
;
290 const struct object_id
*ce_oid
= &list_item
->oid
;
291 const struct submodule
*sub
;
292 struct child_process cp
= CHILD_PROCESS_INIT
;
295 if (validate_submodule_path(path
) < 0)
298 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
301 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
304 die(_("No url found for submodule path '%s' in .gitmodules"),
307 if (!is_submodule_populated_gently(path
, NULL
))
310 prepare_submodule_repo_env(&cp
.env
);
313 * For the purpose of executing <command> in the submodule,
314 * separate shell is used for the purpose of running the
321 * NEEDSWORK: the command currently has access to the variables $name,
322 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
323 * contains a single argument. This is done for maintaining a faithful
324 * translation from shell script.
326 if (info
->argc
== 1) {
327 char *toplevel
= xgetcwd();
328 struct strbuf sb
= STRBUF_INIT
;
330 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
331 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
332 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
333 strvec_pushf(&cp
.env
, "sha1=%s",
335 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
338 * Since the path variable was accessible from the script
339 * before porting, it is also made available after porting.
340 * The environment variable "PATH" has a very special purpose
341 * on windows. And since environment variables are
342 * case-insensitive in windows, it interferes with the
343 * existing PATH variable. Hence, to avoid that, we expose
344 * path via the args strvec and not via env.
346 sq_quote_buf(&sb
, path
);
347 strvec_pushf(&cp
.args
, "path=%s; %s",
348 sb
.buf
, info
->argv
[0]);
352 strvec_pushv(&cp
.args
, info
->argv
);
356 printf(_("Entering '%s'\n"), displaypath
);
359 if (run_command(&cp
))
360 die(_("run_command returned non-zero status for %s\n."),
363 child_process_clear(&cp
);
366 if (info
->recursive
) {
367 struct child_process cpr
= CHILD_PROCESS_INIT
;
371 prepare_submodule_repo_env(&cpr
.env
);
373 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
375 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
378 strvec_push(&cpr
.args
, "--quiet");
380 strvec_push(&cpr
.args
, "--");
381 strvec_pushv(&cpr
.args
, info
->argv
);
383 if (run_command(&cpr
))
384 die(_("run_command returned non-zero status while "
385 "recursing in the nested submodules of %s\n."),
393 static int module_foreach(int argc
, const char **argv
, const char *prefix
,
394 struct repository
*repo UNUSED
)
396 struct foreach_cb info
= FOREACH_CB_INIT
;
397 struct pathspec pathspec
= { 0 };
398 struct module_list list
= MODULE_LIST_INIT
;
399 struct option module_foreach_options
[] = {
400 OPT__SUPER_PREFIX(&info
.super_prefix
),
401 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
402 OPT_BOOL(0, "recursive", &info
.recursive
,
403 N_("recurse into nested submodules")),
406 const char *const git_submodule_helper_usage
[] = {
407 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
412 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
413 git_submodule_helper_usage
, 0);
415 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
420 info
.prefix
= prefix
;
422 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
426 module_list_release(&list
);
427 clear_pathspec(&pathspec
);
433 const char *super_prefix
;
436 #define INIT_CB_INIT { 0 }
438 static void init_submodule(const char *path
, const char *prefix
,
439 const char *super_prefix
,
442 const struct submodule
*sub
;
443 struct strbuf sb
= STRBUF_INIT
;
445 char *url
= NULL
, *displaypath
;
447 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
449 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
452 die(_("No url found for submodule path '%s' in .gitmodules"),
456 * NEEDSWORK: In a multi-working-tree world, this needs to be
457 * set in the per-worktree config.
459 * Set active flag for the submodule being initialized
461 if (!is_submodule_active(the_repository
, path
)) {
462 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
463 repo_config_set_gently(the_repository
, sb
.buf
, "true");
468 * Copy url setting when it is not set yet.
469 * To look up the url in .git/config, we must not fall back to
470 * .gitmodules, so look it up directly.
472 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
473 if (repo_config_get_string(the_repository
, sb
.buf
, &url
)) {
475 die(_("No url found for submodule path '%s' in .gitmodules"),
478 url
= xstrdup(sub
->url
);
480 /* Possibly a url relative to parent */
481 if (starts_with_dot_dot_slash(url
) ||
482 starts_with_dot_slash(url
)) {
485 url
= resolve_relative_url(oldurl
, NULL
, 0);
489 if (repo_config_set_gently(the_repository
, sb
.buf
, url
))
490 die(_("Failed to register url for submodule path '%s'"),
492 if (!(flags
& OPT_QUIET
))
494 _("Submodule '%s' (%s) registered for path '%s'\n"),
495 sub
->name
, url
, displaypath
);
499 /* Copy "update" setting when it is not set yet */
500 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
501 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &upd
) &&
502 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
503 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
504 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
508 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
511 if (repo_config_set_gently(the_repository
, sb
.buf
, upd
))
512 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
519 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
521 struct init_cb
*info
= cb_data
;
523 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
527 static int module_init(int argc
, const char **argv
, const char *prefix
,
528 struct repository
*repo UNUSED
)
530 struct init_cb info
= INIT_CB_INIT
;
531 struct pathspec pathspec
= { 0 };
532 struct module_list list
= MODULE_LIST_INIT
;
534 struct option module_init_options
[] = {
535 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
538 const char *const git_submodule_helper_usage
[] = {
539 N_("git submodule init [<options>] [<path>]"),
544 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
545 git_submodule_helper_usage
, 0);
547 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
551 * If there are no path args and submodule.active is set then,
552 * by default, only initialize 'active' modules.
554 if (!argc
&& !repo_config_get(the_repository
, "submodule.active"))
555 module_list_active(&list
);
557 info
.prefix
= prefix
;
559 info
.flags
|= OPT_QUIET
;
561 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
565 module_list_release(&list
);
566 clear_pathspec(&pathspec
);
572 const char *super_prefix
;
575 #define STATUS_CB_INIT { 0 }
577 static void print_status(unsigned int flags
, char state
, const char *path
,
578 const struct object_id
*oid
, const char *displaypath
)
580 if (flags
& OPT_QUIET
)
583 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
585 if (state
== ' ' || state
== '+') {
586 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
589 printf(" (%s)", name
);
596 static int handle_submodule_head_ref(const char *refname UNUSED
,
597 const char *referent UNUSED
,
598 const struct object_id
*oid
,
602 struct object_id
*output
= cb_data
;
610 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
611 unsigned int ce_flags
, const char *prefix
,
612 const char *super_prefix
, unsigned int flags
)
615 struct strvec diff_files_args
= STRVEC_INIT
;
616 struct rev_info rev
= REV_INFO_INIT
;
617 struct strbuf buf
= STRBUF_INIT
;
619 struct setup_revision_opt opt
= {
620 .free_removed_argv_elements
= 1,
623 if (validate_submodule_path(path
) < 0)
626 if (!submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
))
627 die(_("no submodule mapping found in .gitmodules for path '%s'"),
630 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
632 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
633 print_status(flags
, 'U', path
, null_oid(the_hash_algo
), displaypath
);
637 strbuf_addf(&buf
, "%s/.git", path
);
638 git_dir
= read_gitfile(buf
.buf
);
642 if (!is_submodule_active(the_repository
, path
) ||
643 !is_git_directory(git_dir
)) {
644 print_status(flags
, '-', path
, ce_oid
, displaypath
);
645 strbuf_release(&buf
);
648 strbuf_release(&buf
);
650 strvec_pushl(&diff_files_args
, "diff-files",
651 "--ignore-submodules=dirty", "--quiet", "--",
654 repo_config(the_repository
, git_diff_basic_config
, NULL
);
656 repo_init_revisions(the_repository
, &rev
, NULL
);
658 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
659 run_diff_files(&rev
, 0);
661 if (!diff_result_code(&rev
)) {
662 print_status(flags
, ' ', path
, ce_oid
,
664 } else if (!(flags
& OPT_CACHED
)) {
665 struct object_id oid
;
666 struct ref_store
*refs
= repo_get_submodule_ref_store(the_repository
,
670 print_status(flags
, '-', path
, ce_oid
, displaypath
);
673 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
674 die(_("could not resolve HEAD ref inside the "
675 "submodule '%s'"), path
);
677 print_status(flags
, '+', path
, &oid
, displaypath
);
679 print_status(flags
, '+', path
, ce_oid
, displaypath
);
682 if (flags
& OPT_RECURSIVE
) {
683 struct child_process cpr
= CHILD_PROCESS_INIT
;
688 prepare_submodule_repo_env(&cpr
.env
);
690 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
691 "--recursive", NULL
);
692 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
694 if (flags
& OPT_CACHED
)
695 strvec_push(&cpr
.args
, "--cached");
697 if (flags
& OPT_QUIET
)
698 strvec_push(&cpr
.args
, "--quiet");
700 res
= run_command(&cpr
);
701 if (res
== SIGPIPE
+ 128)
704 die(_("failed to recurse into submodule '%s'"), path
);
708 strvec_clear(&diff_files_args
);
710 release_revisions(&rev
);
713 static void status_submodule_cb(const struct cache_entry
*list_item
,
716 struct status_cb
*info
= cb_data
;
718 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
719 info
->prefix
, info
->super_prefix
, info
->flags
);
722 static int module_status(int argc
, const char **argv
, const char *prefix
,
723 struct repository
*repo UNUSED
)
725 struct status_cb info
= STATUS_CB_INIT
;
726 struct pathspec pathspec
= { 0 };
727 struct module_list list
= MODULE_LIST_INIT
;
729 struct option module_status_options
[] = {
730 OPT__SUPER_PREFIX(&info
.super_prefix
),
731 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
732 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
733 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
736 const char *const git_submodule_helper_usage
[] = {
737 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
742 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
743 git_submodule_helper_usage
, 0);
745 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
748 info
.prefix
= prefix
;
750 info
.flags
|= OPT_QUIET
;
752 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
756 module_list_release(&list
);
757 clear_pathspec(&pathspec
);
762 unsigned int mod_src
;
763 unsigned int mod_dst
;
764 struct object_id oid_src
;
765 struct object_id oid_dst
;
769 #define MODULE_CB_INIT { 0 }
771 static void module_cb_release(struct module_cb
*mcb
)
776 struct module_cb_list
{
777 struct module_cb
**entries
;
780 #define MODULE_CB_LIST_INIT { 0 }
782 static void module_cb_list_release(struct module_cb_list
*mcbl
)
786 for (i
= 0; i
< mcbl
->nr
; i
++) {
787 struct module_cb
*mcb
= mcbl
->entries
[i
];
789 module_cb_release(mcb
);
799 const char *super_prefix
;
800 unsigned int cached
: 1;
801 unsigned int for_status
: 1;
802 unsigned int files
: 1;
805 #define SUMMARY_CB_INIT { 0 }
812 static char *verify_submodule_committish(const char *sm_path
,
813 const char *committish
)
815 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
816 struct strbuf result
= STRBUF_INIT
;
818 cp_rev_parse
.git_cmd
= 1;
819 cp_rev_parse
.dir
= sm_path
;
820 prepare_submodule_repo_env(&cp_rev_parse
.env
);
821 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
822 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
823 strvec_push(&cp_rev_parse
.args
, "--");
825 if (capture_command(&cp_rev_parse
, &result
, 0))
828 strbuf_trim_trailing_newline(&result
);
829 return strbuf_detach(&result
, NULL
);
832 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
833 int total_commits
, const char *displaypath
,
834 const char *src_abbrev
, const char *dst_abbrev
,
837 if (p
->status
== 'T') {
838 if (S_ISGITLINK(p
->mod_dst
))
839 printf(_("* %s %s(blob)->%s(submodule)"),
840 displaypath
, src_abbrev
, dst_abbrev
);
842 printf(_("* %s %s(submodule)->%s(blob)"),
843 displaypath
, src_abbrev
, dst_abbrev
);
845 printf("* %s %s...%s",
846 displaypath
, src_abbrev
, dst_abbrev
);
849 if (total_commits
< 0)
852 printf(" (%d):\n", total_commits
);
855 printf(_("%s"), errmsg
);
856 } else if (total_commits
> 0) {
857 struct child_process cp_log
= CHILD_PROCESS_INIT
;
860 cp_log
.dir
= p
->sm_path
;
861 prepare_submodule_repo_env(&cp_log
.env
);
862 strvec_pushl(&cp_log
.args
, "log", NULL
);
864 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
865 if (info
->summary_limit
> 0)
866 strvec_pushf(&cp_log
.args
, "-%d",
867 info
->summary_limit
);
869 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
870 "--first-parent", NULL
);
871 strvec_pushf(&cp_log
.args
, "%s...%s",
872 src_abbrev
, dst_abbrev
);
873 } else if (S_ISGITLINK(p
->mod_dst
)) {
874 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
875 "-1", dst_abbrev
, NULL
);
877 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
878 "-1", src_abbrev
, NULL
);
880 run_command(&cp_log
);
885 static void generate_submodule_summary(struct summary_cb
*info
,
888 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
889 int missing_src
= 0, missing_dst
= 0;
890 struct strbuf errmsg
= STRBUF_INIT
;
891 int total_commits
= -1;
893 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid(the_hash_algo
))) {
894 if (S_ISGITLINK(p
->mod_dst
)) {
895 struct ref_store
*refs
= repo_get_submodule_ref_store(the_repository
,
899 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
900 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
902 int fd
= open(p
->sm_path
, O_RDONLY
);
904 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
905 index_fd(the_repository
->index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
907 error(_("couldn't hash object from '%s'"), p
->sm_path
);
909 /* for a submodule removal (mode:0000000), don't warn */
911 warning(_("unexpected mode %o"), p
->mod_dst
);
915 if (S_ISGITLINK(p
->mod_src
)) {
916 if (p
->status
!= 'D')
917 src_abbrev
= verify_submodule_committish(p
->sm_path
,
918 oid_to_hex(&p
->oid_src
));
922 * As `rev-parse` failed, we fallback to getting
923 * the abbreviated hash using oid_src. We do
924 * this as we might still need the abbreviated
925 * hash in cases like a submodule type change, etc.
927 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
931 * The source does not point to a submodule.
932 * So, we fallback to getting the abbreviation using
933 * oid_src as we might still need the abbreviated
934 * hash in cases like submodule add, etc.
936 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
939 if (S_ISGITLINK(p
->mod_dst
)) {
940 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
941 oid_to_hex(&p
->oid_dst
));
945 * As `rev-parse` failed, we fallback to getting
946 * the abbreviated hash using oid_dst. We do
947 * this as we might still need the abbreviated
948 * hash in cases like a submodule type change, etc.
950 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
954 * The destination does not point to a submodule.
955 * So, we fallback to getting the abbreviation using
956 * oid_dst as we might still need the abbreviated
957 * hash in cases like a submodule removal, etc.
959 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
962 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
965 if (!missing_src
&& !missing_dst
) {
966 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
967 struct strbuf sb_rev_list
= STRBUF_INIT
;
969 strvec_pushl(&cp_rev_list
.args
, "rev-list",
970 "--first-parent", "--count", NULL
);
971 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
972 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
973 src_abbrev
, dst_abbrev
);
975 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
976 src_abbrev
: dst_abbrev
);
977 strvec_push(&cp_rev_list
.args
, "--");
979 cp_rev_list
.git_cmd
= 1;
980 cp_rev_list
.dir
= p
->sm_path
;
981 prepare_submodule_repo_env(&cp_rev_list
.env
);
983 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
984 total_commits
= atoi(sb_rev_list
.buf
);
986 strbuf_release(&sb_rev_list
);
989 * Don't give error msg for modification whose dst is not
990 * submodule, i.e., deleted or changed to blob
992 if (S_ISGITLINK(p
->mod_dst
)) {
993 if (missing_src
&& missing_dst
) {
994 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
995 displaypath
, oid_to_hex(&p
->oid_src
),
996 oid_to_hex(&p
->oid_dst
));
998 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
999 displaypath
, missing_src
?
1000 oid_to_hex(&p
->oid_src
) :
1001 oid_to_hex(&p
->oid_dst
));
1006 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1007 total_commits
, displaypath
, src_abbrev
,
1013 strbuf_release(&errmsg
);
1016 static void prepare_submodule_summary(struct summary_cb
*info
,
1017 struct module_cb_list
*list
)
1020 for (i
= 0; i
< list
->nr
; i
++) {
1021 const struct submodule
*sub
;
1022 struct module_cb
*p
= list
->entries
[i
];
1023 struct strbuf sm_gitdir
= STRBUF_INIT
;
1025 if (p
->status
== 'D' || p
->status
== 'T') {
1026 generate_submodule_summary(info
, p
);
1030 if (info
->for_status
&& p
->status
!= 'A' &&
1031 (sub
= submodule_from_path(the_repository
,
1032 null_oid(the_hash_algo
), p
->sm_path
))) {
1033 char *config_key
= NULL
;
1037 config_key
= xstrfmt("submodule.%s.ignore",
1039 if (!repo_config_get_string_tmp(the_repository
, config_key
, &value
))
1040 ignore_all
= !strcmp(value
, "all");
1041 else if (sub
->ignore
)
1042 ignore_all
= !strcmp(sub
->ignore
, "all");
1049 /* Also show added or modified modules which are checked out */
1050 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1051 if (is_nonbare_repository_dir(&sm_gitdir
))
1052 generate_submodule_summary(info
, p
);
1053 strbuf_release(&sm_gitdir
);
1057 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1058 struct diff_options
*options UNUSED
,
1062 struct module_cb_list
*list
= data
;
1063 for (i
= 0; i
< q
->nr
; i
++) {
1064 struct diff_filepair
*p
= q
->queue
[i
];
1065 struct module_cb
*temp
;
1067 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1069 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1070 temp
->mod_src
= p
->one
->mode
;
1071 temp
->mod_dst
= p
->two
->mode
;
1072 temp
->oid_src
= p
->one
->oid
;
1073 temp
->oid_dst
= p
->two
->oid
;
1074 temp
->status
= p
->status
;
1075 temp
->sm_path
= xstrdup(p
->one
->path
);
1077 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1078 list
->entries
[list
->nr
++] = temp
;
1082 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1085 case DIFF_INDEX
: return "diff-index";
1086 case DIFF_FILES
: return "diff-files";
1087 default: BUG("bad diff_cmd value %d", diff_cmd
);
1091 static int compute_summary_module_list(struct object_id
*head_oid
,
1092 struct summary_cb
*info
,
1093 enum diff_cmd diff_cmd
)
1095 struct strvec diff_args
= STRVEC_INIT
;
1096 struct rev_info rev
;
1097 struct setup_revision_opt opt
= {
1098 .free_removed_argv_elements
= 1,
1100 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1103 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1105 strvec_push(&diff_args
, "--cached");
1106 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1108 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1109 strvec_push(&diff_args
, "--");
1111 strvec_pushv(&diff_args
, info
->argv
);
1113 repo_config(the_repository
, git_diff_basic_config
, NULL
);
1114 repo_init_revisions(the_repository
, &rev
, info
->prefix
);
1116 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1117 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1118 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1119 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1120 rev
.diffopt
.format_callback_data
= &list
;
1122 if (!info
->cached
) {
1123 if (diff_cmd
== DIFF_INDEX
)
1125 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1126 perror("repo_read_index_preload");
1130 } else if (repo_read_index(the_repository
) < 0) {
1131 perror("repo_read_cache");
1136 if (diff_cmd
== DIFF_INDEX
)
1137 run_diff_index(&rev
, info
->cached
? DIFF_INDEX_CACHED
: 0);
1139 run_diff_files(&rev
, 0);
1140 prepare_submodule_summary(info
, &list
);
1142 strvec_clear(&diff_args
);
1143 release_revisions(&rev
);
1144 module_cb_list_release(&list
);
1148 static int module_summary(int argc
, const char **argv
, const char *prefix
,
1149 struct repository
*repo UNUSED
)
1151 struct summary_cb info
= SUMMARY_CB_INIT
;
1155 int summary_limit
= -1;
1156 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1157 struct object_id head_oid
;
1159 struct option module_summary_options
[] = {
1160 OPT_BOOL(0, "cached", &cached
,
1161 N_("use the commit stored in the index instead of the submodule HEAD")),
1162 OPT_BOOL(0, "files", &files
,
1163 N_("compare the commit in the index with that in the submodule HEAD")),
1164 OPT_BOOL(0, "for-status", &for_status
,
1165 N_("skip submodules with 'ignore_config' value set to 'all'")),
1166 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1167 N_("limit the summary size")),
1170 const char *const git_submodule_helper_usage
[] = {
1171 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1175 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1176 git_submodule_helper_usage
, 0);
1181 if (!repo_get_oid(the_repository
, argc
? argv
[0] : "HEAD", &head_oid
)) {
1186 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1187 /* before the first commit: compare with an empty tree */
1188 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1194 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
1195 die(_("could not fetch a revision for HEAD"));
1200 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1201 diff_cmd
= DIFF_FILES
;
1206 info
.prefix
= prefix
;
1207 info
.cached
= !!cached
;
1208 info
.files
= !!files
;
1209 info
.for_status
= !!for_status
;
1210 info
.summary_limit
= summary_limit
;
1212 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1219 const char *super_prefix
;
1222 #define SYNC_CB_INIT { 0 }
1224 static void sync_submodule(const char *path
, const char *prefix
,
1225 const char *super_prefix
, unsigned int flags
)
1227 const struct submodule
*sub
;
1228 char *remote_key
= NULL
;
1229 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1230 struct strbuf sb
= STRBUF_INIT
;
1231 char *sub_config_path
= NULL
;
1234 if (!is_submodule_active(the_repository
, path
))
1237 if (validate_submodule_path(path
) < 0)
1240 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
1242 if (sub
&& sub
->url
) {
1243 if (starts_with_dot_dot_slash(sub
->url
) ||
1244 starts_with_dot_slash(sub
->url
)) {
1245 char *up_path
= get_up_path(path
);
1247 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1248 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1251 sub_origin_url
= xstrdup(sub
->url
);
1252 super_config_url
= xstrdup(sub
->url
);
1255 sub_origin_url
= xstrdup("");
1256 super_config_url
= xstrdup("");
1259 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1261 if (!(flags
& OPT_QUIET
))
1262 printf(_("Synchronizing submodule url for '%s'\n"),
1266 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1267 if (repo_config_set_gently(the_repository
, sb
.buf
, super_config_url
))
1268 die(_("failed to register url for submodule path '%s'"),
1271 if (!is_submodule_populated_gently(path
, NULL
))
1275 code
= get_default_remote_submodule(path
, &default_remote
);
1279 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1280 free(default_remote
);
1282 submodule_to_gitdir(the_repository
, &sb
, path
);
1283 strbuf_addstr(&sb
, "/config");
1285 if (repo_config_set_in_file_gently(the_repository
, sb
.buf
, remote_key
, NULL
, sub_origin_url
))
1286 die(_("failed to update remote for submodule '%s'"),
1289 if (flags
& OPT_RECURSIVE
) {
1290 struct child_process cpr
= CHILD_PROCESS_INIT
;
1294 prepare_submodule_repo_env(&cpr
.env
);
1296 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1297 "--recursive", NULL
);
1298 strvec_pushf(&cpr
.args
, "--super-prefix=%s/", displaypath
);
1300 if (flags
& OPT_QUIET
)
1301 strvec_push(&cpr
.args
, "--quiet");
1303 if (run_command(&cpr
))
1304 die(_("failed to recurse into submodule '%s'"),
1309 free(super_config_url
);
1310 free(sub_origin_url
);
1311 strbuf_release(&sb
);
1314 free(sub_config_path
);
1317 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1319 struct sync_cb
*info
= cb_data
;
1321 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1325 static int module_sync(int argc
, const char **argv
, const char *prefix
,
1326 struct repository
*repo UNUSED
)
1328 struct sync_cb info
= SYNC_CB_INIT
;
1329 struct pathspec pathspec
= { 0 };
1330 struct module_list list
= MODULE_LIST_INIT
;
1333 struct option module_sync_options
[] = {
1334 OPT__SUPER_PREFIX(&info
.super_prefix
),
1335 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1336 OPT_BOOL(0, "recursive", &recursive
,
1337 N_("recurse into nested submodules")),
1340 const char *const git_submodule_helper_usage
[] = {
1341 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1346 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1347 git_submodule_helper_usage
, 0);
1349 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1352 info
.prefix
= prefix
;
1354 info
.flags
|= OPT_QUIET
;
1356 info
.flags
|= OPT_RECURSIVE
;
1358 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1362 module_list_release(&list
);
1363 clear_pathspec(&pathspec
);
1371 #define DEINIT_CB_INIT { 0 }
1373 static void deinit_submodule(const char *path
, const char *prefix
,
1376 const struct submodule
*sub
;
1377 char *displaypath
= NULL
;
1378 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1379 struct strbuf sb_config
= STRBUF_INIT
;
1380 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1382 if (validate_submodule_path(path
) < 0)
1385 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
1387 if (!sub
|| !sub
->name
)
1390 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1392 /* remove the submodule work tree (unless the user already did it) */
1393 if (is_directory(path
)) {
1394 struct strbuf sb_rm
= STRBUF_INIT
;
1397 if (is_directory(sub_git_dir
)) {
1398 if (!(flags
& OPT_QUIET
))
1399 warning(_("Submodule work tree '%s' contains a .git "
1400 "directory. This will be replaced with a "
1401 ".git file by using absorbgitdirs."),
1404 absorb_git_dir_into_superproject(path
, NULL
);
1408 if (!(flags
& OPT_FORCE
)) {
1409 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1412 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1415 if (run_command(&cp_rm
))
1416 die(_("Submodule work tree '%s' contains local "
1417 "modifications; use '-f' to discard them"),
1421 strbuf_addstr(&sb_rm
, path
);
1423 if (!remove_dir_recursively(&sb_rm
, 0))
1424 format
= _("Cleared directory '%s'\n");
1426 format
= _("Could not remove submodule work tree '%s'\n");
1428 if (!(flags
& OPT_QUIET
))
1429 printf(format
, displaypath
);
1431 submodule_unset_core_worktree(sub
);
1433 strbuf_release(&sb_rm
);
1436 if (mkdir(path
, 0777))
1437 printf(_("could not create empty submodule directory %s"),
1440 cp_config
.git_cmd
= 1;
1441 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1442 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1444 /* remove the .git/config entries (unless the user already did it) */
1445 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1446 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1449 * remove the whole section so we have a clean state when
1450 * the user later decides to init this submodule again
1452 repo_config_rename_section_in_file(the_repository
, NULL
, sub_key
, NULL
);
1453 if (!(flags
& OPT_QUIET
))
1454 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1455 sub
->name
, sub
->url
, displaypath
);
1462 strbuf_release(&sb_config
);
1465 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1468 struct deinit_cb
*info
= cb_data
;
1469 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1472 static int module_deinit(int argc
, const char **argv
, const char *prefix
,
1473 struct repository
*repo UNUSED
)
1475 struct deinit_cb info
= DEINIT_CB_INIT
;
1476 struct pathspec pathspec
= { 0 };
1477 struct module_list list
= MODULE_LIST_INIT
;
1481 struct option module_deinit_options
[] = {
1482 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1483 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1484 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1487 const char *const git_submodule_helper_usage
[] = {
1488 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1493 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1494 git_submodule_helper_usage
, 0);
1497 error("pathspec and --all are incompatible");
1498 usage_with_options(git_submodule_helper_usage
,
1499 module_deinit_options
);
1503 die(_("Use '--all' if you really want to deinitialize all submodules"));
1505 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1508 info
.prefix
= prefix
;
1510 info
.flags
|= OPT_QUIET
;
1512 info
.flags
|= OPT_FORCE
;
1514 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1518 module_list_release(&list
);
1519 clear_pathspec(&pathspec
);
1523 struct module_clone_data
{
1529 struct list_objects_filter_options
*filter_options
;
1530 enum ref_storage_format ref_storage_format
;
1531 unsigned int quiet
: 1;
1532 unsigned int progress
: 1;
1533 unsigned int dissociate
: 1;
1534 unsigned int require_init
: 1;
1537 #define MODULE_CLONE_DATA_INIT { \
1538 .single_branch = -1, \
1539 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
1542 struct submodule_alternate_setup
{
1543 const char *submodule_name
;
1544 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1545 SUBMODULE_ALTERNATE_ERROR_DIE
,
1546 SUBMODULE_ALTERNATE_ERROR_INFO
,
1547 SUBMODULE_ALTERNATE_ERROR_IGNORE
1549 struct string_list
*reference
;
1551 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1552 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1555 static const char alternate_error_advice
[] = N_(
1556 "An alternate computed from a superproject's alternate is invalid.\n"
1557 "To allow Git to clone without an alternate in such a case, set\n"
1558 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1559 "'--reference-if-able' instead of '--reference'."
1562 static int add_possible_reference_from_superproject(
1563 struct odb_source
*alt_odb
, void *sas_cb
)
1565 struct submodule_alternate_setup
*sas
= sas_cb
;
1569 * If the alternate object store is another repository, try the
1570 * standard layout with .git/(modules/<name>)+/objects
1572 if (strip_suffix(alt_odb
->path
, "/objects", &len
)) {
1573 struct repository alternate
;
1575 struct strbuf sb
= STRBUF_INIT
;
1576 struct strbuf err
= STRBUF_INIT
;
1577 strbuf_add(&sb
, alt_odb
->path
, len
);
1579 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1580 die(_("could not get a repository handle for gitdir '%s'"),
1584 * We need to end the new path with '/' to mark it as a dir,
1585 * otherwise a submodule name containing '/' will be broken
1586 * as the last part of a missing submodule reference would
1587 * be taken as a file name.
1590 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1591 strbuf_addch(&sb
, '/');
1592 repo_clear(&alternate
);
1594 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1596 char *p
= strbuf_detach(&sb
, NULL
);
1598 string_list_append(sas
->reference
, p
)->util
= p
;
1601 switch (sas
->error_mode
) {
1602 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1603 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1604 advise(_(alternate_error_advice
));
1605 die(_("submodule '%s' cannot add alternate: %s"),
1606 sas
->submodule_name
, err
.buf
);
1607 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1608 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1609 sas
->submodule_name
, err
.buf
);
1610 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1615 strbuf_release(&err
);
1616 strbuf_release(&sb
);
1622 static void prepare_possible_alternates(const char *sm_name
,
1623 struct string_list
*reference
)
1625 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1626 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1628 repo_config_get_string(the_repository
, "submodule.alternateLocation", &sm_alternate
);
1632 repo_config_get_string(the_repository
, "submodule.alternateErrorStrategy", &error_strategy
);
1634 if (!error_strategy
)
1635 error_strategy
= xstrdup("die");
1637 sas
.submodule_name
= sm_name
;
1638 sas
.reference
= reference
;
1639 if (!strcmp(error_strategy
, "die"))
1640 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1641 else if (!strcmp(error_strategy
, "info"))
1642 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1643 else if (!strcmp(error_strategy
, "ignore"))
1644 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1646 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1648 if (!strcmp(sm_alternate
, "superproject"))
1649 odb_for_each_alternate(the_repository
->objects
,
1650 add_possible_reference_from_superproject
, &sas
);
1651 else if (!strcmp(sm_alternate
, "no"))
1654 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1657 free(error_strategy
);
1660 static char *clone_submodule_sm_gitdir(const char *name
)
1662 struct strbuf sb
= STRBUF_INIT
;
1665 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1666 sm_gitdir
= absolute_pathdup(sb
.buf
);
1667 strbuf_release(&sb
);
1672 static int dir_contains_only_dotgit(const char *path
)
1674 DIR *dir
= opendir(path
);
1681 e
= readdir_skip_dot_and_dotdot(dir
);
1684 else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT
, e
->d_name
) ||
1685 (e
= readdir_skip_dot_and_dotdot(dir
))) {
1686 error("unexpected item '%s' in '%s'", e
->d_name
, path
);
1694 static int clone_submodule(const struct module_clone_data
*clone_data
,
1695 struct string_list
*reference
)
1698 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1699 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1701 struct child_process cp
= CHILD_PROCESS_INIT
;
1702 const char *clone_data_path
= clone_data
->path
;
1703 char *to_free
= NULL
;
1705 if (validate_submodule_path(clone_data_path
) < 0)
1708 if (!is_absolute_path(clone_data
->path
))
1709 clone_data_path
= to_free
= xstrfmt("%s/%s", repo_get_work_tree(the_repository
),
1712 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1713 die(_("refusing to create/use '%s' in another submodule's "
1714 "git dir"), sm_gitdir
);
1716 if (!file_exists(sm_gitdir
)) {
1717 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1718 !is_empty_dir(clone_data_path
))
1719 die(_("directory not empty: '%s'"), clone_data_path
);
1721 if (safe_create_leading_directories_const(the_repository
, sm_gitdir
) < 0)
1722 die(_("could not create directory '%s'"), sm_gitdir
);
1724 prepare_possible_alternates(clone_data
->name
, reference
);
1726 strvec_push(&cp
.args
, "clone");
1727 strvec_push(&cp
.args
, "--no-checkout");
1728 if (clone_data
->quiet
)
1729 strvec_push(&cp
.args
, "--quiet");
1730 if (clone_data
->progress
)
1731 strvec_push(&cp
.args
, "--progress");
1732 if (clone_data
->depth
> 0)
1733 strvec_pushf(&cp
.args
, "--depth=%d", clone_data
->depth
);
1734 if (reference
->nr
) {
1735 struct string_list_item
*item
;
1737 for_each_string_list_item(item
, reference
)
1738 strvec_pushl(&cp
.args
, "--reference",
1739 item
->string
, NULL
);
1741 if (clone_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
1742 strvec_pushf(&cp
.args
, "--ref-format=%s",
1743 ref_storage_format_to_name(clone_data
->ref_storage_format
));
1744 if (clone_data
->dissociate
)
1745 strvec_push(&cp
.args
, "--dissociate");
1746 if (sm_gitdir
&& *sm_gitdir
)
1747 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1748 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1749 strvec_pushf(&cp
.args
, "--filter=%s",
1750 expand_list_objects_filter_spec(
1751 clone_data
->filter_options
));
1752 if (clone_data
->single_branch
>= 0)
1753 strvec_push(&cp
.args
, clone_data
->single_branch
?
1755 "--no-single-branch");
1757 strvec_push(&cp
.args
, "--");
1758 strvec_push(&cp
.args
, clone_data
->url
);
1759 strvec_push(&cp
.args
, clone_data_path
);
1762 prepare_submodule_repo_env(&cp
.env
);
1765 if(run_command(&cp
))
1766 die(_("clone of '%s' into submodule path '%s' failed"),
1767 clone_data
->url
, clone_data_path
);
1769 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1770 !dir_contains_only_dotgit(clone_data_path
)) {
1771 char *dot_git
= xstrfmt("%s/.git", clone_data_path
);
1774 die(_("directory not empty: '%s'"), clone_data_path
);
1779 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1780 !is_empty_dir(clone_data_path
))
1781 die(_("directory not empty: '%s'"), clone_data_path
);
1782 if (safe_create_leading_directories_const(the_repository
, clone_data_path
) < 0)
1783 die(_("could not create directory '%s'"), clone_data_path
);
1784 path
= xstrfmt("%s/index", sm_gitdir
);
1785 unlink_or_warn(path
);
1790 * We already performed this check at the beginning of this function,
1791 * before cloning the objects. This tries to detect racy behavior e.g.
1792 * in parallel clones, where another process could easily have made the
1793 * gitdir nested _after_ it was created.
1795 * To prevent further harm coming from this unintentionally-nested
1796 * gitdir, let's disable it by deleting the `HEAD` file.
1798 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0) {
1799 char *head
= xstrfmt("%s/HEAD", sm_gitdir
);
1802 die(_("refusing to create/use '%s' in another submodule's "
1803 "git dir"), sm_gitdir
);
1806 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1808 p
= repo_submodule_path(the_repository
, clone_data_path
, "config");
1810 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1812 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1813 repo_config_get_string(the_repository
, "submodule.alternateLocation", &sm_alternate
);
1815 repo_config_set_in_file(the_repository
, p
, "submodule.alternateLocation",
1817 repo_config_get_string(the_repository
, "submodule.alternateErrorStrategy", &error_strategy
);
1819 repo_config_set_in_file(the_repository
, p
, "submodule.alternateErrorStrategy",
1823 free(error_strategy
);
1831 static int module_clone(int argc
, const char **argv
, const char *prefix
,
1832 struct repository
*repo UNUSED
)
1834 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1835 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1836 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1837 struct list_objects_filter_options filter_options
=
1838 LIST_OBJECTS_FILTER_INIT
;
1839 const char *ref_storage_format
= NULL
;
1841 struct option module_clone_options
[] = {
1842 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1844 N_("alternative anchor for relative paths")),
1845 OPT_STRING(0, "path", &clone_data
.path
,
1847 N_("where the new submodule will be cloned to")),
1848 OPT_STRING(0, "name", &clone_data
.name
,
1850 N_("name of the new submodule")),
1851 OPT_STRING(0, "url", &clone_data
.url
,
1853 N_("url where to clone the submodule from")),
1854 OPT_STRING_LIST(0, "reference", &reference
,
1856 N_("reference repository")),
1857 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
1858 N_("specify the reference format to use")),
1859 OPT_BOOL(0, "dissociate", &dissociate
,
1860 N_("use --reference only while cloning")),
1861 OPT_INTEGER(0, "depth", &clone_data
.depth
,
1862 N_("depth for shallow clones")),
1863 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1864 OPT_BOOL(0, "progress", &progress
,
1865 N_("force cloning progress")),
1866 OPT_BOOL(0, "require-init", &require_init
,
1867 N_("disallow cloning into non-empty directory")),
1868 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1869 N_("clone only one branch, HEAD or --branch")),
1870 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1873 const char *const git_submodule_helper_usage
[] = {
1874 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1875 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1876 "[--single-branch] [--filter <filter-spec>] "
1877 "--url <url> --path <path>"),
1881 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1882 git_submodule_helper_usage
, 0);
1884 if (ref_storage_format
) {
1885 clone_data
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
1886 if (clone_data
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
1887 die(_("unknown ref storage format '%s'"), ref_storage_format
);
1889 clone_data
.dissociate
= !!dissociate
;
1890 clone_data
.quiet
= !!quiet
;
1891 clone_data
.progress
= !!progress
;
1892 clone_data
.require_init
= !!require_init
;
1893 clone_data
.filter_options
= &filter_options
;
1895 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1896 usage_with_options(git_submodule_helper_usage
,
1897 module_clone_options
);
1899 clone_submodule(&clone_data
, &reference
);
1900 list_objects_filter_release(&filter_options
);
1901 string_list_clear(&reference
, 1);
1905 static int determine_submodule_update_strategy(struct repository
*r
,
1908 enum submodule_update_type update
,
1909 struct submodule_update_strategy
*out
)
1911 const struct submodule
*sub
= submodule_from_path(r
, null_oid(the_hash_algo
), path
);
1916 key
= xstrfmt("submodule.%s.update", sub
->name
);
1920 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1921 if (parse_submodule_update_strategy(val
, out
) < 0) {
1922 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1926 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1927 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1928 BUG("how did we read update = !command from .gitmodules?");
1929 out
->type
= sub
->update_strategy
.type
;
1930 out
->command
= sub
->update_strategy
.command
;
1932 out
->type
= SM_UPDATE_CHECKOUT
;
1935 (out
->type
== SM_UPDATE_MERGE
||
1936 out
->type
== SM_UPDATE_REBASE
||
1937 out
->type
== SM_UPDATE_NONE
))
1938 out
->type
= SM_UPDATE_CHECKOUT
;
1946 struct update_clone_data
{
1947 const struct submodule
*sub
;
1948 struct object_id oid
;
1949 unsigned just_cloned
;
1952 struct submodule_update_clone
{
1953 /* index into 'update_data.list', the list of submodules to look into for cloning */
1956 /* configuration parameters which are passed on to the children */
1957 const struct update_data
*update_data
;
1959 /* to be consumed by update_submodule() */
1960 struct update_clone_data
*update_clone
;
1961 int update_clone_nr
; int update_clone_alloc
;
1963 /* If we want to stop as fast as possible and return an error */
1964 unsigned quickstop
: 1;
1966 /* failed clones to be retried again */
1967 const struct cache_entry
**failed_clones
;
1968 int failed_clones_nr
, failed_clones_alloc
;
1970 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1972 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1974 free(suc
->update_clone
);
1975 free(suc
->failed_clones
);
1978 struct update_data
{
1980 const char *super_prefix
;
1982 enum submodule_update_type update_default
;
1983 struct object_id suboid
;
1984 struct string_list references
;
1985 struct submodule_update_strategy update_strategy
;
1986 struct list_objects_filter_options
*filter_options
;
1987 struct module_list list
;
1988 enum ref_storage_format ref_storage_format
;
1992 int recommend_shallow
;
1993 unsigned int require_init
;
1996 unsigned int nofetch
;
1997 unsigned int remote
;
1998 unsigned int progress
;
1999 unsigned int dissociate
;
2001 unsigned int warn_if_uninitialized
;
2002 unsigned int recursive
;
2004 /* copied over from update_clone_data */
2005 struct object_id oid
;
2006 unsigned int just_cloned
;
2007 const char *sm_path
;
2009 #define UPDATE_DATA_INIT { \
2010 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2011 .list = MODULE_LIST_INIT, \
2012 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
2013 .recommend_shallow = -1, \
2014 .references = STRING_LIST_INIT_DUP, \
2015 .single_branch = -1, \
2019 static void update_data_release(struct update_data
*ud
)
2021 free(ud
->displaypath
);
2022 submodule_update_strategy_release(&ud
->update_strategy
);
2023 module_list_release(&ud
->list
);
2026 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
2027 struct strbuf
*out
, const char *displaypath
)
2030 * Only mention uninitialized submodules when their
2031 * paths have been specified.
2033 if (suc
->update_data
->warn_if_uninitialized
) {
2035 _("Submodule path '%s' not initialized"),
2037 strbuf_addch(out
, '\n');
2039 _("Maybe you want to use 'update --init'?"));
2040 strbuf_addch(out
, '\n');
2045 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2046 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2048 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
2049 struct child_process
*child
,
2050 struct submodule_update_clone
*suc
,
2053 const struct submodule
*sub
= NULL
;
2054 const char *url
= NULL
;
2055 const char *update_string
;
2056 enum submodule_update_type update_type
;
2058 const struct update_data
*ud
= suc
->update_data
;
2059 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
2061 struct strbuf sb
= STRBUF_INIT
;
2062 int needs_cloning
= 0;
2063 int need_free_url
= 0;
2066 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
2067 strbuf_addch(out
, '\n');
2071 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), ce
->name
);
2074 next_submodule_warn_missing(suc
, out
, displaypath
);
2078 key
= xstrfmt("submodule.%s.update", sub
->name
);
2079 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2080 update_type
= parse_submodule_update_type(update_string
);
2082 update_type
= sub
->update_strategy
.type
;
2086 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2087 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2088 && update_type
== SM_UPDATE_NONE
)) {
2089 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2090 strbuf_addch(out
, '\n');
2094 /* Check if the submodule has been initialized. */
2095 if (!is_submodule_active(the_repository
, ce
->name
)) {
2096 next_submodule_warn_missing(suc
, out
, displaypath
);
2101 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2102 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2103 if (sub
->url
&& (starts_with_dot_slash(sub
->url
) ||
2104 starts_with_dot_dot_slash(sub
->url
))) {
2105 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2112 die(_("cannot clone submodule '%s' without a URL"), sub
->name
);
2115 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2116 needs_cloning
= !file_exists(sb
.buf
);
2118 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2119 suc
->update_clone_alloc
);
2120 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2121 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2122 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2123 suc
->update_clone_nr
++;
2129 child
->no_stdin
= 1;
2130 child
->stdout_to_stderr
= 1;
2132 strvec_push(&child
->args
, "submodule--helper");
2133 strvec_push(&child
->args
, "clone");
2134 if (suc
->update_data
->progress
)
2135 strvec_push(&child
->args
, "--progress");
2136 if (suc
->update_data
->quiet
)
2137 strvec_push(&child
->args
, "--quiet");
2138 if (suc
->update_data
->prefix
)
2139 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2140 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2141 strvec_push(&child
->args
, "--depth=1");
2142 else if (suc
->update_data
->depth
)
2143 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2144 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2145 strvec_pushf(&child
->args
, "--filter=%s",
2146 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2147 if (suc
->update_data
->require_init
)
2148 strvec_push(&child
->args
, "--require-init");
2149 if (suc
->update_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
2150 strvec_pushf(&child
->args
, "--ref-format=%s",
2151 ref_storage_format_to_name(suc
->update_data
->ref_storage_format
));
2152 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2153 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2154 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2155 if (suc
->update_data
->references
.nr
) {
2156 struct string_list_item
*item
;
2158 for_each_string_list_item(item
, &suc
->update_data
->references
)
2159 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2161 if (suc
->update_data
->dissociate
)
2162 strvec_push(&child
->args
, "--dissociate");
2163 if (suc
->update_data
->single_branch
>= 0)
2164 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2166 "--no-single-branch");
2170 strbuf_release(&sb
);
2174 return needs_cloning
;
2177 static int update_clone_get_next_task(struct child_process
*child
,
2182 struct submodule_update_clone
*suc
= suc_cb
;
2183 const struct cache_entry
*ce
;
2186 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2187 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2188 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2189 int *p
= xmalloc(sizeof(*p
));
2199 * The loop above tried cloning each submodule once, now try the
2200 * stragglers again, which we can imagine as an extension of the
2203 index
= suc
->current
- suc
->update_data
->list
.nr
;
2204 if (index
< suc
->failed_clones_nr
) {
2207 ce
= suc
->failed_clones
[index
];
2208 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2210 strbuf_addstr(err
, "BUG: submodule considered for "
2211 "cloning, doesn't need cloning "
2215 p
= xmalloc(sizeof(*p
));
2225 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2227 void *idx_task_cb UNUSED
)
2229 struct submodule_update_clone
*suc
= suc_cb
;
2235 static int update_clone_task_finished(int result
,
2240 const struct cache_entry
*ce
;
2241 struct submodule_update_clone
*suc
= suc_cb
;
2242 int *idxP
= idx_task_cb
;
2250 if (idx
< suc
->update_data
->list
.nr
) {
2251 ce
= suc
->update_data
->list
.entries
[idx
];
2252 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2254 strbuf_addch(err
, '\n');
2255 ALLOC_GROW(suc
->failed_clones
,
2256 suc
->failed_clones_nr
+ 1,
2257 suc
->failed_clones_alloc
);
2258 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2261 idx
-= suc
->update_data
->list
.nr
;
2262 ce
= suc
->failed_clones
[idx
];
2263 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2265 strbuf_addch(err
, '\n');
2273 static int git_update_clone_config(const char *var
, const char *value
,
2274 const struct config_context
*ctx
,
2279 if (!strcmp(var
, "submodule.fetchjobs"))
2280 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
2284 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2286 struct child_process cp
= CHILD_PROCESS_INIT
;
2287 struct strbuf rev
= STRBUF_INIT
;
2288 char *hex
= oid_to_hex(oid
);
2294 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2296 prepare_submodule_repo_env(&cp
.env
);
2298 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2303 strbuf_release(&rev
);
2307 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2308 const struct object_id
*oid
)
2310 struct child_process cp
= CHILD_PROCESS_INIT
;
2312 prepare_submodule_repo_env(&cp
.env
);
2314 cp
.dir
= module_path
;
2316 strvec_push(&cp
.args
, "fetch");
2318 strvec_push(&cp
.args
, "--quiet");
2320 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2322 char *hex
= oid_to_hex(oid
);
2326 code
= get_default_remote_submodule(module_path
, &remote
);
2328 child_process_clear(&cp
);
2332 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2336 return run_command(&cp
);
2339 static int run_update_command(const struct update_data
*ud
, int subforce
)
2341 struct child_process cp
= CHILD_PROCESS_INIT
;
2342 char *oid
= oid_to_hex(&ud
->oid
);
2345 switch (ud
->update_strategy
.type
) {
2346 case SM_UPDATE_CHECKOUT
:
2348 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2350 strvec_push(&cp
.args
, "-f");
2352 case SM_UPDATE_REBASE
:
2354 strvec_push(&cp
.args
, "rebase");
2356 strvec_push(&cp
.args
, "--quiet");
2358 case SM_UPDATE_MERGE
:
2360 strvec_push(&cp
.args
, "merge");
2362 strvec_push(&cp
.args
, "--quiet");
2364 case SM_UPDATE_COMMAND
:
2366 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2369 BUG("unexpected update strategy type: %d",
2370 ud
->update_strategy
.type
);
2372 strvec_push(&cp
.args
, oid
);
2374 cp
.dir
= ud
->sm_path
;
2375 prepare_submodule_repo_env(&cp
.env
);
2376 if ((ret
= run_command(&cp
))) {
2377 switch (ud
->update_strategy
.type
) {
2378 case SM_UPDATE_CHECKOUT
:
2379 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2380 oid
, ud
->displaypath
);
2381 /* No "ret" assignment, use "git checkout"'s */
2383 case SM_UPDATE_REBASE
:
2384 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2385 oid
, ud
->displaypath
);
2387 case SM_UPDATE_MERGE
:
2388 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2389 oid
, ud
->displaypath
);
2391 case SM_UPDATE_COMMAND
:
2392 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2393 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2396 BUG("unexpected update strategy type: %d",
2397 ud
->update_strategy
.type
);
2406 switch (ud
->update_strategy
.type
) {
2407 case SM_UPDATE_CHECKOUT
:
2408 printf(_("Submodule path '%s': checked out '%s'\n"),
2409 ud
->displaypath
, oid
);
2411 case SM_UPDATE_REBASE
:
2412 printf(_("Submodule path '%s': rebased into '%s'\n"),
2413 ud
->displaypath
, oid
);
2415 case SM_UPDATE_MERGE
:
2416 printf(_("Submodule path '%s': merged in '%s'\n"),
2417 ud
->displaypath
, oid
);
2419 case SM_UPDATE_COMMAND
:
2420 printf(_("Submodule path '%s': '%s %s'\n"),
2421 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2424 BUG("unexpected update strategy type: %d",
2425 ud
->update_strategy
.type
);
2431 static int run_update_procedure(const struct update_data
*ud
)
2433 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2437 * Run fetch only if `oid` isn't present or it
2438 * is not reachable from a ref.
2440 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2441 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2444 _("Unable to fetch in submodule path '%s'; "
2445 "trying to directly fetch %s:"),
2446 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2448 * Now we tried the usual fetch, but `oid` may
2449 * not be reachable from any of the refs.
2451 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2452 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2453 return die_message(_("Fetched in submodule path '%s', but it did not "
2454 "contain %s. Direct fetching of that commit failed."),
2455 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2458 return run_update_command(ud
, subforce
);
2461 static int remote_submodule_branch(const char *path
, const char **branch
)
2463 const struct submodule
*sub
;
2467 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
2469 return die_message(_("could not initialize submodule at path '%s'"),
2472 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2473 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2474 *branch
= sub
->branch
;
2482 if (!strcmp(*branch
, ".")) {
2483 const char *refname
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
2488 return die_message(_("No such ref: %s"), "HEAD");
2491 if (!strcmp(refname
, "HEAD"))
2492 return die_message(_("Submodule (%s) branch configured to inherit "
2493 "branch from superproject, but the superproject "
2494 "is not on any branch"), sub
->name
);
2496 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2497 return die_message(_("Expecting a full ref name, got %s"),
2504 /* Our "branch" is coming from repo_config_get_string_tmp() */
2508 static int ensure_core_worktree(const char *path
)
2511 struct repository subrepo
;
2513 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid(the_hash_algo
)))
2514 return die_message(_("could not get a repository handle for submodule '%s'"),
2517 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2518 char *cfg_file
, *abs_path
;
2519 const char *rel_path
;
2520 struct strbuf sb
= STRBUF_INIT
;
2522 cfg_file
= repo_git_path(&subrepo
, "config");
2524 abs_path
= absolute_pathdup(path
);
2525 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2527 repo_config_set_in_file(the_repository
, cfg_file
, "core.worktree", rel_path
);
2531 strbuf_release(&sb
);
2534 repo_clear(&subrepo
);
2538 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2541 case SM_UPDATE_CHECKOUT
:
2543 case SM_UPDATE_MERGE
:
2545 case SM_UPDATE_REBASE
:
2547 case SM_UPDATE_UNSPECIFIED
:
2548 case SM_UPDATE_NONE
:
2549 case SM_UPDATE_COMMAND
:
2552 BUG("unreachable with type %d", type
);
2555 static void update_data_to_args(const struct update_data
*update_data
,
2556 struct strvec
*args
)
2558 enum submodule_update_type update_type
= update_data
->update_default
;
2560 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2561 if (update_data
->displaypath
)
2562 strvec_pushf(args
, "--super-prefix=%s/",
2563 update_data
->displaypath
);
2564 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2565 if (update_data
->quiet
)
2566 strvec_push(args
, "--quiet");
2567 if (update_data
->force
)
2568 strvec_push(args
, "--force");
2569 if (update_data
->init
)
2570 strvec_push(args
, "--init");
2571 if (update_data
->remote
)
2572 strvec_push(args
, "--remote");
2573 if (update_data
->nofetch
)
2574 strvec_push(args
, "--no-fetch");
2575 if (update_data
->dissociate
)
2576 strvec_push(args
, "--dissociate");
2577 if (update_data
->progress
)
2578 strvec_push(args
, "--progress");
2579 if (update_data
->require_init
)
2580 strvec_push(args
, "--require-init");
2581 if (update_data
->depth
)
2582 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2583 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2584 strvec_pushf(args
, "--%s",
2585 submodule_update_type_to_label(update_type
));
2587 if (update_data
->references
.nr
) {
2588 struct string_list_item
*item
;
2590 for_each_string_list_item(item
, &update_data
->references
)
2591 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2593 if (update_data
->ref_storage_format
!= REF_STORAGE_FORMAT_UNKNOWN
)
2594 strvec_pushf(args
, "--ref-format=%s",
2595 ref_storage_format_to_name(update_data
->ref_storage_format
));
2596 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2597 strvec_pushf(args
, "--filter=%s",
2598 expand_list_objects_filter_spec(
2599 update_data
->filter_options
));
2600 if (update_data
->recommend_shallow
== 0)
2601 strvec_push(args
, "--no-recommend-shallow");
2602 else if (update_data
->recommend_shallow
== 1)
2603 strvec_push(args
, "--recommend-shallow");
2604 if (update_data
->single_branch
>= 0)
2605 strvec_push(args
, update_data
->single_branch
?
2607 "--no-single-branch");
2610 static int update_submodule(struct update_data
*update_data
)
2614 if (validate_submodule_path(update_data
->sm_path
) < 0)
2617 ret
= determine_submodule_update_strategy(the_repository
,
2618 update_data
->just_cloned
,
2619 update_data
->sm_path
,
2620 update_data
->update_default
,
2621 &update_data
->update_strategy
);
2625 if (update_data
->just_cloned
)
2626 oidcpy(&update_data
->suboid
, null_oid(the_hash_algo
));
2627 else if (repo_resolve_gitlink_ref(the_repository
, update_data
->sm_path
,
2628 "HEAD", &update_data
->suboid
))
2629 return die_message(_("Unable to find current revision in submodule path '%s'"),
2630 update_data
->displaypath
);
2632 if (update_data
->remote
) {
2638 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2641 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2646 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2650 if (!update_data
->nofetch
) {
2651 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2654 return die_message(_("Unable to fetch in submodule path '%s'"),
2655 update_data
->sm_path
);
2659 if (repo_resolve_gitlink_ref(the_repository
, update_data
->sm_path
,
2660 remote_ref
, &update_data
->oid
)) {
2661 ret
= die_message(_("Unable to find %s revision in submodule path '%s'"),
2662 remote_ref
, update_data
->sm_path
);
2670 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2671 ret
= run_update_procedure(update_data
);
2676 if (update_data
->recursive
) {
2677 struct child_process cp
= CHILD_PROCESS_INIT
;
2678 struct update_data next
= *update_data
;
2681 oidcpy(&next
.oid
, null_oid(the_hash_algo
));
2682 oidcpy(&next
.suboid
, null_oid(the_hash_algo
));
2684 cp
.dir
= update_data
->sm_path
;
2686 prepare_submodule_repo_env(&cp
.env
);
2687 update_data_to_args(&next
, &cp
.args
);
2689 ret
= run_command(&cp
);
2691 die_message(_("Failed to recurse into submodule path '%s'"),
2692 update_data
->displaypath
);
2699 static int update_submodules(struct update_data
*update_data
)
2702 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2703 const struct run_process_parallel_opts opts
= {
2704 .tr2_category
= "submodule",
2705 .tr2_label
= "parallel/update",
2707 .processes
= update_data
->max_jobs
,
2709 .get_next_task
= update_clone_get_next_task
,
2710 .start_failure
= update_clone_start_failure
,
2711 .task_finished
= update_clone_task_finished
,
2715 suc
.update_data
= update_data
;
2716 run_processes_parallel(&opts
);
2719 * We saved the output and put it out all at once now.
2721 * - the listener does not have to interleave their (checkout)
2722 * work with our fetching. The writes involved in a
2723 * checkout involve more straightforward sequential I/O.
2724 * - the listener can avoid doing any work if fetching failed.
2726 if (suc
.quickstop
) {
2731 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2732 struct update_clone_data ucd
= suc
.update_clone
[i
];
2735 oidcpy(&update_data
->oid
, &ucd
.oid
);
2736 update_data
->just_cloned
= ucd
.just_cloned
;
2737 update_data
->sm_path
= ucd
.sub
->path
;
2740 * Verify that the submodule path does not contain any
2741 * symlinks; if it does, it might have been tampered with.
2742 * TODO: allow exempting it via
2743 * `safe.submodule.path` or something
2745 if (validate_submodule_path(update_data
->sm_path
) < 0)
2748 code
= ensure_core_worktree(update_data
->sm_path
);
2752 update_data
->displaypath
= get_submodule_displaypath(
2753 update_data
->sm_path
, update_data
->prefix
,
2754 update_data
->super_prefix
);
2755 code
= update_submodule(update_data
);
2756 FREE_AND_NULL(update_data
->displaypath
);
2766 submodule_update_clone_release(&suc
);
2767 string_list_clear(&update_data
->references
, 0);
2771 static int module_update(int argc
, const char **argv
, const char *prefix
,
2772 struct repository
*repo UNUSED
)
2774 struct pathspec pathspec
= { 0 };
2775 struct pathspec pathspec2
= { 0 };
2776 struct update_data opt
= UPDATE_DATA_INIT
;
2777 struct list_objects_filter_options filter_options
=
2778 LIST_OBJECTS_FILTER_INIT
;
2779 const char *ref_storage_format
= NULL
;
2781 struct option module_update_options
[] = {
2782 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2783 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2784 OPT_BOOL(0, "init", &opt
.init
,
2785 N_("initialize uninitialized submodules before update")),
2786 OPT_BOOL(0, "remote", &opt
.remote
,
2787 N_("use SHA-1 of submodule's remote tracking branch")),
2788 OPT_BOOL(0, "recursive", &opt
.recursive
,
2789 N_("traverse submodules recursively")),
2790 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2791 N_("don't fetch new objects from the remote site")),
2792 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2793 N_("use the 'checkout' update strategy (default)"),
2794 SM_UPDATE_CHECKOUT
),
2795 OPT_SET_INT('m', "merge", &opt
.update_default
,
2796 N_("use the 'merge' update strategy"),
2798 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2799 N_("use the 'rebase' update strategy"),
2801 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2802 N_("reference repository")),
2803 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
2804 N_("specify the reference format to use")),
2805 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2806 N_("use --reference only while cloning")),
2807 OPT_INTEGER(0, "depth", &opt
.depth
,
2808 N_("create a shallow clone truncated to the "
2809 "specified number of revisions")),
2810 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2811 N_("parallel jobs")),
2812 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2813 N_("whether the initial clone should follow the shallow recommendation")),
2814 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2815 OPT_BOOL(0, "progress", &opt
.progress
,
2816 N_("force cloning progress")),
2817 OPT_BOOL(0, "require-init", &opt
.require_init
,
2818 N_("disallow cloning into non-empty directory, implies --init")),
2819 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2820 N_("clone only one branch, HEAD or --branch")),
2821 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2824 const char *const git_submodule_helper_usage
[] = {
2825 N_("git submodule [--quiet] update"
2826 " [--init [--filter=<filter-spec>]] [--remote]"
2827 " [-N|--no-fetch] [-f|--force]"
2828 " [--checkout|--merge|--rebase]"
2829 " [--[no-]recommend-shallow] [--reference <repository>]"
2830 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2834 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2835 repo_config(the_repository
, git_update_clone_config
, &opt
.max_jobs
);
2837 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2838 git_submodule_helper_usage
, 0);
2840 if (opt
.require_init
)
2843 if (filter_options
.choice
&& !opt
.init
) {
2844 usage_with_options(git_submodule_helper_usage
,
2845 module_update_options
);
2848 if (ref_storage_format
) {
2849 opt
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
2850 if (opt
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
2851 die(_("unknown ref storage format '%s'"), ref_storage_format
);
2854 opt
.filter_options
= &filter_options
;
2855 opt
.prefix
= prefix
;
2857 if (opt
.update_default
)
2858 opt
.update_strategy
.type
= opt
.update_default
;
2860 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2866 opt
.warn_if_uninitialized
= 1;
2869 struct module_list list
= MODULE_LIST_INIT
;
2870 struct init_cb info
= INIT_CB_INIT
;
2872 if (module_list_compute(argv
, opt
.prefix
,
2873 &pathspec2
, &list
) < 0) {
2874 module_list_release(&list
);
2880 * If there are no path args and submodule.active is set then,
2881 * by default, only initialize 'active' modules.
2883 if (!argc
&& !repo_config_get(the_repository
, "submodule.active"))
2884 module_list_active(&list
);
2886 info
.prefix
= opt
.prefix
;
2887 info
.super_prefix
= opt
.super_prefix
;
2889 info
.flags
|= OPT_QUIET
;
2891 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2892 module_list_release(&list
);
2895 ret
= update_submodules(&opt
);
2897 update_data_release(&opt
);
2898 list_objects_filter_release(&filter_options
);
2899 clear_pathspec(&pathspec
);
2900 clear_pathspec(&pathspec2
);
2904 static int push_check(int argc
, const char **argv
, const char *prefix UNUSED
,
2905 struct repository
*repo UNUSED
)
2907 struct remote
*remote
;
2908 const char *superproject_head
;
2910 int detached_head
= 0;
2911 struct object_id head_oid
;
2914 die("submodule--helper push-check requires at least 2 arguments");
2917 * superproject's resolved head ref.
2918 * if HEAD then the superproject is in a detached head state, otherwise
2919 * it will be the resolved head ref.
2921 superproject_head
= argv
[1];
2924 /* Get the submodule's head ref and determine if it is detached */
2925 head
= refs_resolve_refdup(get_main_ref_store(the_repository
), "HEAD",
2926 0, &head_oid
, NULL
);
2928 die(_("Failed to resolve HEAD as a valid ref."));
2929 if (!strcmp(head
, "HEAD"))
2933 * The remote must be configured.
2934 * This is to avoid pushing to the exact same URL as the parent.
2936 remote
= pushremote_get(argv
[1]);
2937 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2938 die("remote '%s' not configured", argv
[1]);
2940 /* Check the refspec */
2943 struct ref
*local_refs
= get_local_heads();
2944 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2946 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2948 for (i
= 0; i
< refspec
.nr
; i
++) {
2949 const struct refspec_item
*rs
= &refspec
.items
[i
];
2951 if (rs
->pattern
|| rs
->matching
)
2954 /* LHS must match a single ref */
2955 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2960 * If LHS matches 'HEAD' then we need to ensure
2961 * that it matches the same named branch
2962 * checked out in the superproject.
2964 if (!strcmp(rs
->src
, "HEAD")) {
2965 if (!detached_head
&&
2966 !strcmp(head
, superproject_head
))
2968 die("HEAD does not match the named branch in the superproject");
2972 die("src refspec '%s' must name a ref",
2977 refspec_clear(&refspec
);
2978 free_refs(local_refs
);
2985 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
,
2986 struct repository
*repo UNUSED
)
2989 struct pathspec pathspec
= { 0 };
2990 struct module_list list
= MODULE_LIST_INIT
;
2991 const char *super_prefix
= NULL
;
2992 struct option embed_gitdir_options
[] = {
2993 OPT__SUPER_PREFIX(&super_prefix
),
2996 const char *const git_submodule_helper_usage
[] = {
2997 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
3002 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
3003 git_submodule_helper_usage
, 0);
3005 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
3008 for (i
= 0; i
< list
.nr
; i
++)
3009 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
3014 clear_pathspec(&pathspec
);
3015 module_list_release(&list
);
3019 static int module_set_url(int argc
, const char **argv
, const char *prefix
,
3020 struct repository
*repo UNUSED
)
3026 struct option options
[] = {
3027 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
3030 const char *const usage
[] = {
3031 N_("git submodule set-url [--quiet] <path> <newurl>"),
3034 const struct submodule
*sub
;
3036 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3038 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
3039 usage_with_options(usage
, options
);
3041 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
3044 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3047 config_name
= xstrfmt("submodule.%s.url", sub
->name
);
3048 ret
= config_set_in_gitmodules_file_gently(config_name
, newurl
);
3051 repo_read_gitmodules(the_repository
, 0);
3052 sync_submodule(sub
->path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
3059 static int module_set_branch(int argc
, const char **argv
, const char *prefix
,
3060 struct repository
*repo UNUSED
)
3062 int opt_default
= 0, ret
;
3063 const char *opt_branch
= NULL
;
3066 struct option options
[] = {
3068 * We accept the `quiet` option for uniformity across subcommands,
3069 * though there is nothing to make less verbose in this subcommand.
3071 OPT_NOOP_NOARG('q', "quiet"),
3073 OPT_BOOL('d', "default", &opt_default
,
3074 N_("set the default tracking branch to master")),
3075 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
3076 N_("set the default tracking branch")),
3079 const char *const usage
[] = {
3080 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3081 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3084 const struct submodule
*sub
;
3086 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3088 if (!opt_branch
&& !opt_default
)
3089 die(_("--branch or --default required"));
3091 if (opt_branch
&& opt_default
)
3092 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3094 if (argc
!= 1 || !(path
= argv
[0]))
3095 usage_with_options(usage
, options
);
3097 sub
= submodule_from_path(the_repository
, null_oid(the_hash_algo
), path
);
3100 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3103 config_name
= xstrfmt("submodule.%s.branch", sub
->name
);
3104 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
3110 static int module_create_branch(int argc
, const char **argv
, const char *prefix
,
3111 struct repository
*repo UNUSED
)
3113 enum branch_track track
;
3114 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
3115 struct option options
[] = {
3116 OPT__QUIET(&quiet
, N_("print only error messages")),
3117 OPT__FORCE(&force
, N_("force creation"), 0),
3118 OPT_BOOL(0, "create-reflog", &reflog
,
3119 N_("create the branch's reflog")),
3120 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
3121 N_("set branch tracking configuration"),
3123 parse_opt_tracking_mode
),
3124 OPT__DRY_RUN(&dry_run
,
3125 N_("show whether the branch would be created")),
3128 const char *const usage
[] = {
3129 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3133 repo_config(the_repository
, git_default_config
, NULL
);
3134 track
= git_branch_track
;
3135 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3138 usage_with_options(usage
, options
);
3140 if (!quiet
&& !dry_run
)
3141 printf_ln(_("creating branch '%s'"), argv
[0]);
3143 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
3144 force
, reflog
, quiet
, track
, dry_run
);
3151 const char *reference_path
;
3153 const char *sm_name
;
3155 const char *realrepo
;
3156 enum ref_storage_format ref_storage_format
;
3158 unsigned int force
: 1;
3159 unsigned int quiet
: 1;
3160 unsigned int progress
: 1;
3161 unsigned int dissociate
: 1;
3163 #define ADD_DATA_INIT { \
3165 .ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
3168 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3170 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3171 struct strbuf sb_remote_out
= STRBUF_INIT
;
3173 cp_remote
.git_cmd
= 1;
3174 strvec_pushf(&cp_remote
.env
,
3175 "GIT_DIR=%s", git_dir_path
);
3176 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3177 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3178 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3180 char *line
= sb_remote_out
.buf
;
3182 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3183 size_t len
= next_line
- line
;
3185 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3186 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3187 line
= next_line
+ 1;
3191 strbuf_release(&sb_remote_out
);
3194 static int add_submodule(const struct add_data
*add_data
)
3196 char *submod_gitdir_path
;
3197 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3198 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3201 /* perhaps the path already exists and is already a git repo, else clone it */
3202 if (is_directory(add_data
->sm_path
)) {
3203 struct strbuf sm_path
= STRBUF_INIT
;
3204 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3205 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3206 if (is_nonbare_repository_dir(&sm_path
))
3207 printf(_("Adding existing repo at '%s' to the index\n"),
3210 die(_("'%s' already exists and is not a valid git repo"),
3212 strbuf_release(&sm_path
);
3213 free(submod_gitdir_path
);
3215 struct child_process cp
= CHILD_PROCESS_INIT
;
3217 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3219 if (is_directory(submod_gitdir_path
)) {
3220 if (!add_data
->force
) {
3221 struct strbuf msg
= STRBUF_INIT
;
3224 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3225 "locally with remote(s):\n"),
3228 append_fetch_remotes(&msg
, submod_gitdir_path
);
3229 free(submod_gitdir_path
);
3231 strbuf_addf(&msg
, _("If you want to reuse this local git "
3232 "directory instead of cloning again from\n"
3234 "use the '--force' option. If the local git "
3235 "directory is not the correct repo\n"
3236 "or you are unsure what this means choose "
3237 "another name with the '--name' option."),
3238 add_data
->realrepo
);
3240 die_msg
= strbuf_detach(&msg
, NULL
);
3243 printf(_("Reactivating local git directory for "
3244 "submodule '%s'\n"), add_data
->sm_name
);
3247 free(submod_gitdir_path
);
3249 clone_data
.prefix
= add_data
->prefix
;
3250 clone_data
.path
= add_data
->sm_path
;
3251 clone_data
.name
= add_data
->sm_name
;
3252 clone_data
.url
= add_data
->realrepo
;
3253 clone_data
.quiet
= add_data
->quiet
;
3254 clone_data
.progress
= add_data
->progress
;
3255 if (add_data
->reference_path
) {
3256 char *p
= xstrdup(add_data
->reference_path
);
3258 string_list_append(&reference
, p
)->util
= p
;
3260 clone_data
.ref_storage_format
= add_data
->ref_storage_format
;
3261 clone_data
.dissociate
= add_data
->dissociate
;
3262 if (add_data
->depth
>= 0)
3263 clone_data
.depth
= add_data
->depth
;
3265 if (clone_submodule(&clone_data
, &reference
))
3268 prepare_submodule_repo_env(&cp
.env
);
3270 cp
.dir
= add_data
->sm_path
;
3272 * NOTE: we only get here if add_data->force is true, so
3273 * passing --force to checkout is reasonable.
3275 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3277 if (add_data
->branch
) {
3278 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3279 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3282 if (run_command(&cp
))
3283 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3288 string_list_clear(&reference
, 1);
3292 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3297 if (!is_writing_gitmodules_ok())
3298 die(_("please make sure that the .gitmodules file is in the working tree"));
3300 key
= xstrfmt("submodule.%s.%s", name
, var
);
3301 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3307 static void configure_added_submodule(struct add_data
*add_data
)
3310 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3311 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3312 const struct string_list
*values
;
3315 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3316 repo_config_set_gently(the_repository
, key
, add_data
->realrepo
);
3319 add_submod
.git_cmd
= 1;
3320 strvec_pushl(&add_submod
.args
, "add",
3321 "--no-warn-embedded-repo", NULL
);
3322 if (add_data
->force
)
3323 strvec_push(&add_submod
.args
, "--force");
3324 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3326 if (run_command(&add_submod
))
3327 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3329 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3330 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3331 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3333 if (add_data
->branch
) {
3334 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3335 "branch", add_data
->branch
))
3336 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3339 add_gitmodules
.git_cmd
= 1;
3340 strvec_pushl(&add_gitmodules
.args
,
3341 "add", "--force", "--", ".gitmodules", NULL
);
3343 if (run_command(&add_gitmodules
))
3344 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3347 * NEEDSWORK: In a multi-working-tree world this needs to be
3348 * set in the per-worktree config.
3351 * NEEDSWORK: In the longer run, we need to get rid of this
3352 * pattern of querying "submodule.active" before calling
3353 * is_submodule_active(), since that function needs to find
3354 * out the value of "submodule.active" again anyway.
3356 if (repo_config_get(the_repository
, "submodule.active") || /* key absent */
3357 repo_config_get_string_multi(the_repository
, "submodule.active", &values
)) {
3359 * If the submodule being added isn't already covered by the
3360 * current configured pathspec, set the submodule's active flag
3362 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3363 repo_config_set_gently(the_repository
, key
, "true");
3366 for (size_t i
= 0; i
< values
->nr
; i
++) {
3367 const char *pat
= values
->items
[i
].string
;
3368 if (!wildmatch(pat
, add_data
->sm_path
, 0)) { /* match found */
3373 if (!matched
) { /* no pattern matched -> force-enable */
3374 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3375 repo_config_set_gently(the_repository
, key
, "true");
3381 static void die_on_index_match(const char *path
, int force
)
3384 const char *args
[] = { path
, NULL
};
3385 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3387 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3388 die(_("index file corrupt"));
3391 char *ps_matched
= xcalloc(ps
.nr
, 1);
3393 /* TODO: audit for interaction with sparse-index. */
3394 ensure_full_index(the_repository
->index
);
3397 * Since there is only one pathspec, we just need to
3398 * check ps_matched[0] to know if a cache entry matched.
3400 for (size_t i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
3401 ce_path_match(the_repository
->index
, the_repository
->index
->cache
[i
], &ps
,
3404 if (ps_matched
[0]) {
3406 die(_("'%s' already exists in the index"),
3408 if (!S_ISGITLINK(the_repository
->index
->cache
[i
]->ce_mode
))
3409 die(_("'%s' already exists in the index "
3410 "and is not a submodule"), path
);
3416 clear_pathspec(&ps
);
3419 static void die_on_repo_without_commits(const char *path
)
3421 struct strbuf sb
= STRBUF_INIT
;
3422 strbuf_addstr(&sb
, path
);
3423 if (is_nonbare_repository_dir(&sb
)) {
3424 struct object_id oid
;
3425 if (repo_resolve_gitlink_ref(the_repository
, path
, "HEAD", &oid
) < 0)
3426 die(_("'%s' does not have a commit checked out"), path
);
3428 strbuf_release(&sb
);
3431 static int module_add(int argc
, const char **argv
, const char *prefix
,
3432 struct repository
*repo UNUSED
)
3434 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3435 struct add_data add_data
= ADD_DATA_INIT
;
3436 const char *ref_storage_format
= NULL
;
3437 char *to_free
= NULL
;
3438 const struct submodule
*existing
;
3439 struct strbuf buf
= STRBUF_INIT
;
3440 char *sm_name_to_free
= NULL
;
3441 struct option options
[] = {
3442 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3443 N_("branch of repository to add as submodule")),
3444 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3445 PARSE_OPT_NOCOMPLETE
),
3446 OPT__QUIET(&quiet
, N_("print only error messages")),
3447 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3448 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3449 N_("reference repository")),
3450 OPT_STRING(0, "ref-format", &ref_storage_format
, N_("format"),
3451 N_("specify the reference format to use")),
3452 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3453 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3454 N_("sets the submodule's name to the given string "
3455 "instead of defaulting to its path")),
3456 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3459 const char *const usage
[] = {
3460 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3463 struct strbuf sb
= STRBUF_INIT
;
3466 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3468 if (!is_writing_gitmodules_ok())
3469 die(_("please make sure that the .gitmodules file is in the working tree"));
3471 if (prefix
&& *prefix
&&
3472 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3473 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3475 if (argc
== 0 || argc
> 2)
3476 usage_with_options(usage
, options
);
3478 if (ref_storage_format
) {
3479 add_data
.ref_storage_format
= ref_storage_format_by_name(ref_storage_format
);
3480 if (add_data
.ref_storage_format
== REF_STORAGE_FORMAT_UNKNOWN
)
3481 die(_("unknown ref storage format '%s'"), ref_storage_format
);
3484 add_data
.repo
= argv
[0];
3486 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3488 add_data
.sm_path
= xstrdup(argv
[1]);
3490 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3491 char *sm_path
= add_data
.sm_path
;
3493 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3497 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3498 starts_with_dot_slash(add_data
.repo
)) {
3500 die(_("Relative path can only be used from the toplevel "
3501 "of the working tree"));
3503 /* dereference source url relative to parent's url */
3504 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3505 add_data
.realrepo
= to_free
;
3506 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3507 add_data
.realrepo
= add_data
.repo
;
3509 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3515 * multiple //; leading ./; /./; /../;
3517 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3518 strip_dir_trailing_slashes(add_data
.sm_path
);
3520 if (validate_submodule_path(add_data
.sm_path
) < 0)
3523 die_on_index_match(add_data
.sm_path
, force
);
3524 die_on_repo_without_commits(add_data
.sm_path
);
3527 struct child_process cp
= CHILD_PROCESS_INIT
;
3531 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3532 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3533 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3534 strbuf_complete_line(&sb
);
3535 fputs(sb
.buf
, stderr
);
3540 if(!add_data
.sm_name
)
3541 add_data
.sm_name
= add_data
.sm_path
;
3543 existing
= submodule_from_name(the_repository
,
3544 null_oid(the_hash_algo
),
3547 if (existing
&& strcmp(existing
->path
, add_data
.sm_path
)) {
3549 die(_("submodule name '%s' already used for path '%s'"),
3550 add_data
.sm_name
, existing
->path
);
3552 /* --force: build <name><n> until unique */
3553 for (int i
= 1; ; i
++) {
3555 strbuf_addf(&buf
, "%s%d", add_data
.sm_name
, i
);
3556 if (!submodule_from_name(the_repository
,
3557 null_oid(the_hash_algo
),
3562 add_data
.sm_name
= sm_name_to_free
= strbuf_detach(&buf
, NULL
);
3565 if (check_submodule_name(add_data
.sm_name
))
3566 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3568 add_data
.prefix
= prefix
;
3569 add_data
.force
= !!force
;
3570 add_data
.quiet
= !!quiet
;
3571 add_data
.progress
= !!progress
;
3572 add_data
.dissociate
= !!dissociate
;
3574 if (add_submodule(&add_data
))
3576 configure_added_submodule(&add_data
);
3580 free(sm_name_to_free
);
3581 free(add_data
.sm_path
);
3583 strbuf_release(&sb
);
3588 int cmd_submodule__helper(int argc
,
3591 struct repository
*repo
)
3593 parse_opt_subcommand_fn
*fn
= NULL
;
3594 const char *const usage
[] = {
3595 N_("git submodule--helper <command>"),
3598 struct option options
[] = {
3599 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3600 OPT_SUBCOMMAND("add", &fn
, module_add
),
3601 OPT_SUBCOMMAND("update", &fn
, module_update
),
3602 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3603 OPT_SUBCOMMAND("init", &fn
, module_init
),
3604 OPT_SUBCOMMAND("status", &fn
, module_status
),
3605 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3606 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3607 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3608 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3609 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3610 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3611 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3612 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3615 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3617 return fn(argc
, argv
, prefix
, repo
);