3 #include "environment.h"
6 #include "repository.h"
8 #include "parse-options.h"
12 #include "preload-index.h"
14 #include "read-cache.h"
16 #include "sparse-index.h"
17 #include "submodule.h"
18 #include "submodule-config.h"
19 #include "string-list.h"
20 #include "run-command.h"
27 #include "object-file.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
32 #include "list-objects-filter-options.h"
34 #define OPT_QUIET (1 << 0)
35 #define OPT_CACHED (1 << 1)
36 #define OPT_RECURSIVE (1 << 2)
37 #define OPT_FORCE (1 << 3)
39 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
42 static int repo_get_default_remote(struct repository
*repo
, char **default_remote
)
45 struct strbuf sb
= STRBUF_INIT
;
46 struct ref_store
*store
= get_main_ref_store(repo
);
47 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
51 return die_message(_("No such ref: %s"), "HEAD");
54 if (!strcmp(refname
, "HEAD")) {
55 *default_remote
= xstrdup("origin");
59 if (!skip_prefix(refname
, "refs/heads/", &refname
))
60 return die_message(_("Expecting a full ref name, got %s"),
63 strbuf_addf(&sb
, "branch.%s.remote", refname
);
64 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
65 *default_remote
= xstrdup("origin");
67 *default_remote
= dest
;
73 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
75 struct repository subrepo
;
78 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
80 return die_message(_("could not get a repository handle for submodule '%s'"),
82 ret
= repo_get_default_remote(&subrepo
, default_remote
);
88 static char *get_default_remote(void)
91 int code
= repo_get_default_remote(the_repository
, &default_remote
);
96 return default_remote
;
99 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
101 char *remoteurl
, *resolved_url
;
102 char *remote
= get_default_remote();
103 struct strbuf remotesb
= STRBUF_INIT
;
105 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
106 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
108 warning(_("could not look up configuration '%s'. "
109 "Assuming this repository is its own "
110 "authoritative upstream."),
112 remoteurl
= xgetcwd();
114 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
118 strbuf_release(&remotesb
);
123 /* the result should be freed by the caller. */
124 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
125 const char *super_prefix
)
127 if (prefix
&& super_prefix
) {
128 BUG("cannot have prefix '%s' and superprefix '%s'",
129 prefix
, super_prefix
);
131 struct strbuf sb
= STRBUF_INIT
;
132 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
135 } else if (super_prefix
) {
136 return xstrfmt("%s%s", super_prefix
, path
);
138 return xstrdup(path
);
142 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
144 struct strbuf sb
= STRBUF_INIT
;
147 static const char *describe_bare
[] = { NULL
};
149 static const char *describe_tags
[] = { "--tags", NULL
};
151 static const char *describe_contains
[] = { "--contains", NULL
};
153 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
155 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
157 describe_all_always
, NULL
};
159 for (d
= describe_argv
; *d
; d
++) {
160 struct child_process cp
= CHILD_PROCESS_INIT
;
161 prepare_submodule_repo_env(&cp
.env
);
166 strvec_push(&cp
.args
, "describe");
167 strvec_pushv(&cp
.args
, *d
);
168 strvec_push(&cp
.args
, object_id
);
170 if (!capture_command(&cp
, &sb
, 0)) {
171 strbuf_strip_suffix(&sb
, "\n");
172 return strbuf_detach(&sb
, NULL
);
181 const struct cache_entry
**entries
;
184 #define MODULE_LIST_INIT { 0 }
186 static void module_list_release(struct module_list
*ml
)
191 static int module_list_compute(const char **argv
,
193 struct pathspec
*pathspec
,
194 struct module_list
*list
)
197 char *ps_matched
= NULL
;
199 parse_pathspec(pathspec
, 0,
200 PATHSPEC_PREFER_FULL
,
204 ps_matched
= xcalloc(pathspec
->nr
, 1);
206 if (repo_read_index(the_repository
) < 0)
207 die(_("index file corrupt"));
209 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
210 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
212 if (!match_pathspec(the_repository
->index
, pathspec
, ce
->name
, ce_namelen(ce
),
214 !S_ISGITLINK(ce
->ce_mode
))
217 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
218 list
->entries
[list
->nr
++] = ce
;
219 while (i
+ 1 < the_repository
->index
->cache_nr
&&
220 !strcmp(ce
->name
, the_repository
->index
->cache
[i
+ 1]->name
))
222 * Skip entries with the same name in different stages
223 * to make sure an entry is returned only once.
228 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
236 static void module_list_active(struct module_list
*list
)
239 struct module_list active_modules
= MODULE_LIST_INIT
;
241 for (i
= 0; i
< list
->nr
; i
++) {
242 const struct cache_entry
*ce
= list
->entries
[i
];
244 if (!is_submodule_active(the_repository
, ce
->name
))
247 ALLOC_GROW(active_modules
.entries
,
248 active_modules
.nr
+ 1,
249 active_modules
.alloc
);
250 active_modules
.entries
[active_modules
.nr
++] = ce
;
253 module_list_release(list
);
254 *list
= active_modules
;
257 static char *get_up_path(const char *path
)
259 struct strbuf sb
= STRBUF_INIT
;
261 strbuf_addstrings(&sb
, "../", count_slashes(path
));
264 * Check if 'path' ends with slash or not
265 * for having the same output for dir/sub_dir
268 if (!is_dir_sep(path
[strlen(path
) - 1]))
269 strbuf_addstr(&sb
, "../");
271 return strbuf_detach(&sb
, NULL
);
274 static void for_each_listed_submodule(const struct module_list
*list
,
275 each_submodule_fn fn
, void *cb_data
)
279 for (i
= 0; i
< list
->nr
; i
++)
280 fn(list
->entries
[i
], cb_data
);
287 const char *super_prefix
;
291 #define FOREACH_CB_INIT { 0 }
293 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
296 struct foreach_cb
*info
= cb_data
;
297 const char *path
= list_item
->name
;
298 const struct object_id
*ce_oid
= &list_item
->oid
;
299 const struct submodule
*sub
;
300 struct child_process cp
= CHILD_PROCESS_INIT
;
303 if (validate_submodule_path(path
) < 0)
306 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
309 sub
= submodule_from_path(the_repository
, null_oid(), path
);
312 die(_("No url found for submodule path '%s' in .gitmodules"),
315 if (!is_submodule_populated_gently(path
, NULL
))
318 prepare_submodule_repo_env(&cp
.env
);
321 * For the purpose of executing <command> in the submodule,
322 * separate shell is used for the purpose of running the
329 * NEEDSWORK: the command currently has access to the variables $name,
330 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
331 * contains a single argument. This is done for maintaining a faithful
332 * translation from shell script.
334 if (info
->argc
== 1) {
335 char *toplevel
= xgetcwd();
336 struct strbuf sb
= STRBUF_INIT
;
338 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
339 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
340 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
341 strvec_pushf(&cp
.env
, "sha1=%s",
343 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
346 * Since the path variable was accessible from the script
347 * before porting, it is also made available after porting.
348 * The environment variable "PATH" has a very special purpose
349 * on windows. And since environment variables are
350 * case-insensitive in windows, it interferes with the
351 * existing PATH variable. Hence, to avoid that, we expose
352 * path via the args strvec and not via env.
354 sq_quote_buf(&sb
, path
);
355 strvec_pushf(&cp
.args
, "path=%s; %s",
356 sb
.buf
, info
->argv
[0]);
360 strvec_pushv(&cp
.args
, info
->argv
);
364 printf(_("Entering '%s'\n"), displaypath
);
366 if (info
->argv
[0] && run_command(&cp
))
367 die(_("run_command returned non-zero status for %s\n."),
370 if (info
->recursive
) {
371 struct child_process cpr
= CHILD_PROCESS_INIT
;
375 prepare_submodule_repo_env(&cpr
.env
);
377 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
379 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
380 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
383 strvec_push(&cpr
.args
, "--quiet");
385 strvec_push(&cpr
.args
, "--");
386 strvec_pushv(&cpr
.args
, info
->argv
);
388 if (run_command(&cpr
))
389 die(_("run_command returned non-zero status while "
390 "recursing in the nested submodules of %s\n."),
398 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
400 struct foreach_cb info
= FOREACH_CB_INIT
;
401 struct pathspec pathspec
= { 0 };
402 struct module_list list
= MODULE_LIST_INIT
;
403 struct option module_foreach_options
[] = {
404 OPT__SUPER_PREFIX(&info
.super_prefix
),
405 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
406 OPT_BOOL(0, "recursive", &info
.recursive
,
407 N_("recurse into nested submodules")),
410 const char *const git_submodule_helper_usage
[] = {
411 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
416 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
417 git_submodule_helper_usage
, 0);
419 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
424 info
.prefix
= prefix
;
426 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
430 module_list_release(&list
);
431 clear_pathspec(&pathspec
);
435 static int starts_with_dot_slash(const char *const path
)
437 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
438 PATH_MATCH_XPLATFORM
);
441 static int starts_with_dot_dot_slash(const char *const path
)
443 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
444 PATH_MATCH_XPLATFORM
);
449 const char *super_prefix
;
452 #define INIT_CB_INIT { 0 }
454 static void init_submodule(const char *path
, const char *prefix
,
455 const char *super_prefix
,
458 const struct submodule
*sub
;
459 struct strbuf sb
= STRBUF_INIT
;
461 char *url
= NULL
, *displaypath
;
463 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
465 sub
= submodule_from_path(the_repository
, null_oid(), path
);
468 die(_("No url found for submodule path '%s' in .gitmodules"),
472 * NEEDSWORK: In a multi-working-tree world, this needs to be
473 * set in the per-worktree config.
475 * Set active flag for the submodule being initialized
477 if (!is_submodule_active(the_repository
, path
)) {
478 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
479 git_config_set_gently(sb
.buf
, "true");
484 * Copy url setting when it is not set yet.
485 * To look up the url in .git/config, we must not fall back to
486 * .gitmodules, so look it up directly.
488 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
489 if (git_config_get_string(sb
.buf
, &url
)) {
491 die(_("No url found for submodule path '%s' in .gitmodules"),
494 url
= xstrdup(sub
->url
);
496 /* Possibly a url relative to parent */
497 if (starts_with_dot_dot_slash(url
) ||
498 starts_with_dot_slash(url
)) {
501 url
= resolve_relative_url(oldurl
, NULL
, 0);
505 if (git_config_set_gently(sb
.buf
, url
))
506 die(_("Failed to register url for submodule path '%s'"),
508 if (!(flags
& OPT_QUIET
))
510 _("Submodule '%s' (%s) registered for path '%s'\n"),
511 sub
->name
, url
, displaypath
);
515 /* Copy "update" setting when it is not set yet */
516 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
517 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
518 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
519 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
520 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
524 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
527 if (git_config_set_gently(sb
.buf
, upd
))
528 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
535 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
537 struct init_cb
*info
= cb_data
;
539 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
543 static int module_init(int argc
, const char **argv
, const char *prefix
)
545 struct init_cb info
= INIT_CB_INIT
;
546 struct pathspec pathspec
= { 0 };
547 struct module_list list
= MODULE_LIST_INIT
;
549 struct option module_init_options
[] = {
550 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
553 const char *const git_submodule_helper_usage
[] = {
554 N_("git submodule init [<options>] [<path>]"),
559 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
560 git_submodule_helper_usage
, 0);
562 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
566 * If there are no path args and submodule.active is set then,
567 * by default, only initialize 'active' modules.
569 if (!argc
&& !git_config_get("submodule.active"))
570 module_list_active(&list
);
572 info
.prefix
= prefix
;
574 info
.flags
|= OPT_QUIET
;
576 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
580 module_list_release(&list
);
581 clear_pathspec(&pathspec
);
587 const char *super_prefix
;
590 #define STATUS_CB_INIT { 0 }
592 static void print_status(unsigned int flags
, char state
, const char *path
,
593 const struct object_id
*oid
, const char *displaypath
)
595 if (flags
& OPT_QUIET
)
598 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
600 if (state
== ' ' || state
== '+') {
601 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
604 printf(" (%s)", name
);
611 static int handle_submodule_head_ref(const char *refname UNUSED
,
612 const struct object_id
*oid
,
616 struct object_id
*output
= cb_data
;
624 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
625 unsigned int ce_flags
, const char *prefix
,
626 const char *super_prefix
, unsigned int flags
)
629 struct strvec diff_files_args
= STRVEC_INIT
;
630 struct rev_info rev
= REV_INFO_INIT
;
631 struct strbuf buf
= STRBUF_INIT
;
633 struct setup_revision_opt opt
= {
634 .free_removed_argv_elements
= 1,
637 if (validate_submodule_path(path
) < 0)
640 if (!submodule_from_path(the_repository
, null_oid(), path
))
641 die(_("no submodule mapping found in .gitmodules for path '%s'"),
644 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
646 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
647 print_status(flags
, 'U', path
, null_oid(), displaypath
);
651 strbuf_addf(&buf
, "%s/.git", path
);
652 git_dir
= read_gitfile(buf
.buf
);
656 if (!is_submodule_active(the_repository
, path
) ||
657 !is_git_directory(git_dir
)) {
658 print_status(flags
, '-', path
, ce_oid
, displaypath
);
659 strbuf_release(&buf
);
662 strbuf_release(&buf
);
664 strvec_pushl(&diff_files_args
, "diff-files",
665 "--ignore-submodules=dirty", "--quiet", "--",
668 git_config(git_diff_basic_config
, NULL
);
670 repo_init_revisions(the_repository
, &rev
, NULL
);
672 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
673 run_diff_files(&rev
, 0);
675 if (!diff_result_code(&rev
.diffopt
)) {
676 print_status(flags
, ' ', path
, ce_oid
,
678 } else if (!(flags
& OPT_CACHED
)) {
679 struct object_id oid
;
680 struct ref_store
*refs
= repo_get_submodule_ref_store(the_repository
,
684 print_status(flags
, '-', path
, ce_oid
, displaypath
);
687 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
688 die(_("could not resolve HEAD ref inside the "
689 "submodule '%s'"), path
);
691 print_status(flags
, '+', path
, &oid
, displaypath
);
693 print_status(flags
, '+', path
, ce_oid
, displaypath
);
696 if (flags
& OPT_RECURSIVE
) {
697 struct child_process cpr
= CHILD_PROCESS_INIT
;
701 prepare_submodule_repo_env(&cpr
.env
);
703 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
704 "--recursive", NULL
);
705 strvec_push(&cpr
.args
, "--super-prefix");
706 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
708 if (flags
& OPT_CACHED
)
709 strvec_push(&cpr
.args
, "--cached");
711 if (flags
& OPT_QUIET
)
712 strvec_push(&cpr
.args
, "--quiet");
714 if (run_command(&cpr
))
715 die(_("failed to recurse into submodule '%s'"), path
);
719 strvec_clear(&diff_files_args
);
721 release_revisions(&rev
);
724 static void status_submodule_cb(const struct cache_entry
*list_item
,
727 struct status_cb
*info
= cb_data
;
729 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
730 info
->prefix
, info
->super_prefix
, info
->flags
);
733 static int module_status(int argc
, const char **argv
, const char *prefix
)
735 struct status_cb info
= STATUS_CB_INIT
;
736 struct pathspec pathspec
= { 0 };
737 struct module_list list
= MODULE_LIST_INIT
;
739 struct option module_status_options
[] = {
740 OPT__SUPER_PREFIX(&info
.super_prefix
),
741 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
742 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
743 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
746 const char *const git_submodule_helper_usage
[] = {
747 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
752 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
753 git_submodule_helper_usage
, 0);
755 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
758 info
.prefix
= prefix
;
760 info
.flags
|= OPT_QUIET
;
762 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
766 module_list_release(&list
);
767 clear_pathspec(&pathspec
);
772 unsigned int mod_src
;
773 unsigned int mod_dst
;
774 struct object_id oid_src
;
775 struct object_id oid_dst
;
779 #define MODULE_CB_INIT { 0 }
781 static void module_cb_release(struct module_cb
*mcb
)
786 struct module_cb_list
{
787 struct module_cb
**entries
;
790 #define MODULE_CB_LIST_INIT { 0 }
792 static void module_cb_list_release(struct module_cb_list
*mcbl
)
796 for (i
= 0; i
< mcbl
->nr
; i
++) {
797 struct module_cb
*mcb
= mcbl
->entries
[i
];
799 module_cb_release(mcb
);
809 const char *super_prefix
;
810 unsigned int cached
: 1;
811 unsigned int for_status
: 1;
812 unsigned int files
: 1;
815 #define SUMMARY_CB_INIT { 0 }
822 static char *verify_submodule_committish(const char *sm_path
,
823 const char *committish
)
825 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
826 struct strbuf result
= STRBUF_INIT
;
828 cp_rev_parse
.git_cmd
= 1;
829 cp_rev_parse
.dir
= sm_path
;
830 prepare_submodule_repo_env(&cp_rev_parse
.env
);
831 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
832 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
833 strvec_push(&cp_rev_parse
.args
, "--");
835 if (capture_command(&cp_rev_parse
, &result
, 0))
838 strbuf_trim_trailing_newline(&result
);
839 return strbuf_detach(&result
, NULL
);
842 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
843 int total_commits
, const char *displaypath
,
844 const char *src_abbrev
, const char *dst_abbrev
,
847 if (p
->status
== 'T') {
848 if (S_ISGITLINK(p
->mod_dst
))
849 printf(_("* %s %s(blob)->%s(submodule)"),
850 displaypath
, src_abbrev
, dst_abbrev
);
852 printf(_("* %s %s(submodule)->%s(blob)"),
853 displaypath
, src_abbrev
, dst_abbrev
);
855 printf("* %s %s...%s",
856 displaypath
, src_abbrev
, dst_abbrev
);
859 if (total_commits
< 0)
862 printf(" (%d):\n", total_commits
);
865 printf(_("%s"), errmsg
);
866 } else if (total_commits
> 0) {
867 struct child_process cp_log
= CHILD_PROCESS_INIT
;
870 cp_log
.dir
= p
->sm_path
;
871 prepare_submodule_repo_env(&cp_log
.env
);
872 strvec_pushl(&cp_log
.args
, "log", NULL
);
874 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
875 if (info
->summary_limit
> 0)
876 strvec_pushf(&cp_log
.args
, "-%d",
877 info
->summary_limit
);
879 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
880 "--first-parent", NULL
);
881 strvec_pushf(&cp_log
.args
, "%s...%s",
882 src_abbrev
, dst_abbrev
);
883 } else if (S_ISGITLINK(p
->mod_dst
)) {
884 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
885 "-1", dst_abbrev
, NULL
);
887 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
888 "-1", src_abbrev
, NULL
);
890 run_command(&cp_log
);
895 static void generate_submodule_summary(struct summary_cb
*info
,
898 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
899 int missing_src
= 0, missing_dst
= 0;
900 struct strbuf errmsg
= STRBUF_INIT
;
901 int total_commits
= -1;
903 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
904 if (S_ISGITLINK(p
->mod_dst
)) {
905 struct ref_store
*refs
= repo_get_submodule_ref_store(the_repository
,
909 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
910 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
912 int fd
= open(p
->sm_path
, O_RDONLY
);
914 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
915 index_fd(the_repository
->index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
917 error(_("couldn't hash object from '%s'"), p
->sm_path
);
919 /* for a submodule removal (mode:0000000), don't warn */
921 warning(_("unexpected mode %o\n"), p
->mod_dst
);
925 if (S_ISGITLINK(p
->mod_src
)) {
926 if (p
->status
!= 'D')
927 src_abbrev
= verify_submodule_committish(p
->sm_path
,
928 oid_to_hex(&p
->oid_src
));
932 * As `rev-parse` failed, we fallback to getting
933 * the abbreviated hash using oid_src. We do
934 * this as we might still need the abbreviated
935 * hash in cases like a submodule type change, etc.
937 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
941 * The source does not point to a submodule.
942 * So, we fallback to getting the abbreviation using
943 * oid_src as we might still need the abbreviated
944 * hash in cases like submodule add, etc.
946 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
949 if (S_ISGITLINK(p
->mod_dst
)) {
950 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
951 oid_to_hex(&p
->oid_dst
));
955 * As `rev-parse` failed, we fallback to getting
956 * the abbreviated hash using oid_dst. We do
957 * this as we might still need the abbreviated
958 * hash in cases like a submodule type change, etc.
960 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
964 * The destination does not point to a submodule.
965 * So, we fallback to getting the abbreviation using
966 * oid_dst as we might still need the abbreviated
967 * hash in cases like a submodule removal, etc.
969 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
972 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
975 if (!missing_src
&& !missing_dst
) {
976 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
977 struct strbuf sb_rev_list
= STRBUF_INIT
;
979 strvec_pushl(&cp_rev_list
.args
, "rev-list",
980 "--first-parent", "--count", NULL
);
981 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
982 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
983 src_abbrev
, dst_abbrev
);
985 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
986 src_abbrev
: dst_abbrev
);
987 strvec_push(&cp_rev_list
.args
, "--");
989 cp_rev_list
.git_cmd
= 1;
990 cp_rev_list
.dir
= p
->sm_path
;
991 prepare_submodule_repo_env(&cp_rev_list
.env
);
993 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
994 total_commits
= atoi(sb_rev_list
.buf
);
996 strbuf_release(&sb_rev_list
);
999 * Don't give error msg for modification whose dst is not
1000 * submodule, i.e., deleted or changed to blob
1002 if (S_ISGITLINK(p
->mod_dst
)) {
1003 if (missing_src
&& missing_dst
) {
1004 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
1005 displaypath
, oid_to_hex(&p
->oid_src
),
1006 oid_to_hex(&p
->oid_dst
));
1008 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
1009 displaypath
, missing_src
?
1010 oid_to_hex(&p
->oid_src
) :
1011 oid_to_hex(&p
->oid_dst
));
1016 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1017 total_commits
, displaypath
, src_abbrev
,
1023 strbuf_release(&errmsg
);
1026 static void prepare_submodule_summary(struct summary_cb
*info
,
1027 struct module_cb_list
*list
)
1030 for (i
= 0; i
< list
->nr
; i
++) {
1031 const struct submodule
*sub
;
1032 struct module_cb
*p
= list
->entries
[i
];
1033 struct strbuf sm_gitdir
= STRBUF_INIT
;
1035 if (p
->status
== 'D' || p
->status
== 'T') {
1036 generate_submodule_summary(info
, p
);
1040 if (info
->for_status
&& p
->status
!= 'A' &&
1041 (sub
= submodule_from_path(the_repository
,
1042 null_oid(), p
->sm_path
))) {
1043 char *config_key
= NULL
;
1047 config_key
= xstrfmt("submodule.%s.ignore",
1049 if (!git_config_get_string_tmp(config_key
, &value
))
1050 ignore_all
= !strcmp(value
, "all");
1051 else if (sub
->ignore
)
1052 ignore_all
= !strcmp(sub
->ignore
, "all");
1059 /* Also show added or modified modules which are checked out */
1060 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1061 if (is_nonbare_repository_dir(&sm_gitdir
))
1062 generate_submodule_summary(info
, p
);
1063 strbuf_release(&sm_gitdir
);
1067 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1068 struct diff_options
*options UNUSED
,
1072 struct module_cb_list
*list
= data
;
1073 for (i
= 0; i
< q
->nr
; i
++) {
1074 struct diff_filepair
*p
= q
->queue
[i
];
1075 struct module_cb
*temp
;
1077 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1079 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1080 temp
->mod_src
= p
->one
->mode
;
1081 temp
->mod_dst
= p
->two
->mode
;
1082 temp
->oid_src
= p
->one
->oid
;
1083 temp
->oid_dst
= p
->two
->oid
;
1084 temp
->status
= p
->status
;
1085 temp
->sm_path
= xstrdup(p
->one
->path
);
1087 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1088 list
->entries
[list
->nr
++] = temp
;
1092 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1095 case DIFF_INDEX
: return "diff-index";
1096 case DIFF_FILES
: return "diff-files";
1097 default: BUG("bad diff_cmd value %d", diff_cmd
);
1101 static int compute_summary_module_list(struct object_id
*head_oid
,
1102 struct summary_cb
*info
,
1103 enum diff_cmd diff_cmd
)
1105 struct strvec diff_args
= STRVEC_INIT
;
1106 struct rev_info rev
;
1107 struct setup_revision_opt opt
= {
1108 .free_removed_argv_elements
= 1,
1110 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1113 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1115 strvec_push(&diff_args
, "--cached");
1116 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1118 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1119 strvec_push(&diff_args
, "--");
1121 strvec_pushv(&diff_args
, info
->argv
);
1123 git_config(git_diff_basic_config
, NULL
);
1124 repo_init_revisions(the_repository
, &rev
, info
->prefix
);
1126 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1127 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1128 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1129 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1130 rev
.diffopt
.format_callback_data
= &list
;
1132 if (!info
->cached
) {
1133 if (diff_cmd
== DIFF_INDEX
)
1135 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1136 perror("repo_read_index_preload");
1140 } else if (repo_read_index(the_repository
) < 0) {
1141 perror("repo_read_cache");
1146 if (diff_cmd
== DIFF_INDEX
)
1147 run_diff_index(&rev
, info
->cached
? DIFF_INDEX_CACHED
: 0);
1149 run_diff_files(&rev
, 0);
1150 prepare_submodule_summary(info
, &list
);
1152 strvec_clear(&diff_args
);
1153 release_revisions(&rev
);
1154 module_cb_list_release(&list
);
1158 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1160 struct summary_cb info
= SUMMARY_CB_INIT
;
1164 int summary_limit
= -1;
1165 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1166 struct object_id head_oid
;
1168 struct option module_summary_options
[] = {
1169 OPT_BOOL(0, "cached", &cached
,
1170 N_("use the commit stored in the index instead of the submodule HEAD")),
1171 OPT_BOOL(0, "files", &files
,
1172 N_("compare the commit in the index with that in the submodule HEAD")),
1173 OPT_BOOL(0, "for-status", &for_status
,
1174 N_("skip submodules with 'ignore_config' value set to 'all'")),
1175 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1176 N_("limit the summary size")),
1179 const char *const git_submodule_helper_usage
[] = {
1180 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1184 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1185 git_submodule_helper_usage
, 0);
1190 if (!repo_get_oid(the_repository
, argc
? argv
[0] : "HEAD", &head_oid
)) {
1195 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1196 /* before the first commit: compare with an empty tree */
1197 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1203 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
1204 die(_("could not fetch a revision for HEAD"));
1209 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1210 diff_cmd
= DIFF_FILES
;
1215 info
.prefix
= prefix
;
1216 info
.cached
= !!cached
;
1217 info
.files
= !!files
;
1218 info
.for_status
= !!for_status
;
1219 info
.summary_limit
= summary_limit
;
1221 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1228 const char *super_prefix
;
1231 #define SYNC_CB_INIT { 0 }
1233 static void sync_submodule(const char *path
, const char *prefix
,
1234 const char *super_prefix
, unsigned int flags
)
1236 const struct submodule
*sub
;
1237 char *remote_key
= NULL
;
1238 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1239 struct strbuf sb
= STRBUF_INIT
;
1240 char *sub_config_path
= NULL
;
1243 if (!is_submodule_active(the_repository
, path
))
1246 if (validate_submodule_path(path
) < 0)
1249 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1251 if (sub
&& sub
->url
) {
1252 if (starts_with_dot_dot_slash(sub
->url
) ||
1253 starts_with_dot_slash(sub
->url
)) {
1254 char *up_path
= get_up_path(path
);
1256 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1257 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1260 sub_origin_url
= xstrdup(sub
->url
);
1261 super_config_url
= xstrdup(sub
->url
);
1264 sub_origin_url
= xstrdup("");
1265 super_config_url
= xstrdup("");
1268 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1270 if (!(flags
& OPT_QUIET
))
1271 printf(_("Synchronizing submodule url for '%s'\n"),
1275 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1276 if (git_config_set_gently(sb
.buf
, super_config_url
))
1277 die(_("failed to register url for submodule path '%s'"),
1280 if (!is_submodule_populated_gently(path
, NULL
))
1284 code
= get_default_remote_submodule(path
, &default_remote
);
1288 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1289 free(default_remote
);
1291 submodule_to_gitdir(&sb
, path
);
1292 strbuf_addstr(&sb
, "/config");
1294 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, NULL
, sub_origin_url
))
1295 die(_("failed to update remote for submodule '%s'"),
1298 if (flags
& OPT_RECURSIVE
) {
1299 struct child_process cpr
= CHILD_PROCESS_INIT
;
1303 prepare_submodule_repo_env(&cpr
.env
);
1305 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1306 "--recursive", NULL
);
1307 strvec_push(&cpr
.args
, "--super-prefix");
1308 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1311 if (flags
& OPT_QUIET
)
1312 strvec_push(&cpr
.args
, "--quiet");
1314 if (run_command(&cpr
))
1315 die(_("failed to recurse into submodule '%s'"),
1320 free(super_config_url
);
1321 free(sub_origin_url
);
1322 strbuf_release(&sb
);
1325 free(sub_config_path
);
1328 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1330 struct sync_cb
*info
= cb_data
;
1332 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1336 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1338 struct sync_cb info
= SYNC_CB_INIT
;
1339 struct pathspec pathspec
= { 0 };
1340 struct module_list list
= MODULE_LIST_INIT
;
1343 struct option module_sync_options
[] = {
1344 OPT__SUPER_PREFIX(&info
.super_prefix
),
1345 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1346 OPT_BOOL(0, "recursive", &recursive
,
1347 N_("recurse into nested submodules")),
1350 const char *const git_submodule_helper_usage
[] = {
1351 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1356 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1357 git_submodule_helper_usage
, 0);
1359 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1362 info
.prefix
= prefix
;
1364 info
.flags
|= OPT_QUIET
;
1366 info
.flags
|= OPT_RECURSIVE
;
1368 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1372 module_list_release(&list
);
1373 clear_pathspec(&pathspec
);
1381 #define DEINIT_CB_INIT { 0 }
1383 static void deinit_submodule(const char *path
, const char *prefix
,
1386 const struct submodule
*sub
;
1387 char *displaypath
= NULL
;
1388 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1389 struct strbuf sb_config
= STRBUF_INIT
;
1390 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1392 if (validate_submodule_path(path
) < 0)
1395 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1397 if (!sub
|| !sub
->name
)
1400 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1402 /* remove the submodule work tree (unless the user already did it) */
1403 if (is_directory(path
)) {
1404 struct strbuf sb_rm
= STRBUF_INIT
;
1407 if (is_directory(sub_git_dir
)) {
1408 if (!(flags
& OPT_QUIET
))
1409 warning(_("Submodule work tree '%s' contains a .git "
1410 "directory. This will be replaced with a "
1411 ".git file by using absorbgitdirs."),
1414 absorb_git_dir_into_superproject(path
, NULL
);
1418 if (!(flags
& OPT_FORCE
)) {
1419 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1422 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1425 if (run_command(&cp_rm
))
1426 die(_("Submodule work tree '%s' contains local "
1427 "modifications; use '-f' to discard them"),
1431 strbuf_addstr(&sb_rm
, path
);
1433 if (!remove_dir_recursively(&sb_rm
, 0))
1434 format
= _("Cleared directory '%s'\n");
1436 format
= _("Could not remove submodule work tree '%s'\n");
1438 if (!(flags
& OPT_QUIET
))
1439 printf(format
, displaypath
);
1441 submodule_unset_core_worktree(sub
);
1443 strbuf_release(&sb_rm
);
1446 if (mkdir(path
, 0777))
1447 printf(_("could not create empty submodule directory %s"),
1450 cp_config
.git_cmd
= 1;
1451 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1452 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1454 /* remove the .git/config entries (unless the user already did it) */
1455 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1456 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1459 * remove the whole section so we have a clean state when
1460 * the user later decides to init this submodule again
1462 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1463 if (!(flags
& OPT_QUIET
))
1464 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1465 sub
->name
, sub
->url
, displaypath
);
1472 strbuf_release(&sb_config
);
1475 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1478 struct deinit_cb
*info
= cb_data
;
1479 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1482 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1484 struct deinit_cb info
= DEINIT_CB_INIT
;
1485 struct pathspec pathspec
= { 0 };
1486 struct module_list list
= MODULE_LIST_INIT
;
1490 struct option module_deinit_options
[] = {
1491 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1492 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1493 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1496 const char *const git_submodule_helper_usage
[] = {
1497 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1502 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1503 git_submodule_helper_usage
, 0);
1506 error("pathspec and --all are incompatible");
1507 usage_with_options(git_submodule_helper_usage
,
1508 module_deinit_options
);
1512 die(_("Use '--all' if you really want to deinitialize all submodules"));
1514 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1517 info
.prefix
= prefix
;
1519 info
.flags
|= OPT_QUIET
;
1521 info
.flags
|= OPT_FORCE
;
1523 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1527 module_list_release(&list
);
1528 clear_pathspec(&pathspec
);
1532 struct module_clone_data
{
1538 struct list_objects_filter_options
*filter_options
;
1539 unsigned int quiet
: 1;
1540 unsigned int progress
: 1;
1541 unsigned int dissociate
: 1;
1542 unsigned int require_init
: 1;
1545 #define MODULE_CLONE_DATA_INIT { \
1546 .single_branch = -1, \
1549 struct submodule_alternate_setup
{
1550 const char *submodule_name
;
1551 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1552 SUBMODULE_ALTERNATE_ERROR_DIE
,
1553 SUBMODULE_ALTERNATE_ERROR_INFO
,
1554 SUBMODULE_ALTERNATE_ERROR_IGNORE
1556 struct string_list
*reference
;
1558 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1559 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1562 static const char alternate_error_advice
[] = N_(
1563 "An alternate computed from a superproject's alternate is invalid.\n"
1564 "To allow Git to clone without an alternate in such a case, set\n"
1565 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1566 "'--reference-if-able' instead of '--reference'."
1569 static int add_possible_reference_from_superproject(
1570 struct object_directory
*odb
, void *sas_cb
)
1572 struct submodule_alternate_setup
*sas
= sas_cb
;
1576 * If the alternate object store is another repository, try the
1577 * standard layout with .git/(modules/<name>)+/objects
1579 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1580 struct repository alternate
;
1582 struct strbuf sb
= STRBUF_INIT
;
1583 struct strbuf err
= STRBUF_INIT
;
1584 strbuf_add(&sb
, odb
->path
, len
);
1586 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1587 die(_("could not get a repository handle for gitdir '%s'"),
1591 * We need to end the new path with '/' to mark it as a dir,
1592 * otherwise a submodule name containing '/' will be broken
1593 * as the last part of a missing submodule reference would
1594 * be taken as a file name.
1597 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1598 strbuf_addch(&sb
, '/');
1599 repo_clear(&alternate
);
1601 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1603 char *p
= strbuf_detach(&sb
, NULL
);
1605 string_list_append(sas
->reference
, p
)->util
= p
;
1608 switch (sas
->error_mode
) {
1609 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1610 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1611 advise(_(alternate_error_advice
));
1612 die(_("submodule '%s' cannot add alternate: %s"),
1613 sas
->submodule_name
, err
.buf
);
1614 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1615 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1616 sas
->submodule_name
, err
.buf
);
1617 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1621 strbuf_release(&sb
);
1627 static void prepare_possible_alternates(const char *sm_name
,
1628 struct string_list
*reference
)
1630 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1631 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1633 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1637 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1639 if (!error_strategy
)
1640 error_strategy
= xstrdup("die");
1642 sas
.submodule_name
= sm_name
;
1643 sas
.reference
= reference
;
1644 if (!strcmp(error_strategy
, "die"))
1645 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1646 else if (!strcmp(error_strategy
, "info"))
1647 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1648 else if (!strcmp(error_strategy
, "ignore"))
1649 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1651 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1653 if (!strcmp(sm_alternate
, "superproject"))
1654 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1655 else if (!strcmp(sm_alternate
, "no"))
1658 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1661 free(error_strategy
);
1664 static char *clone_submodule_sm_gitdir(const char *name
)
1666 struct strbuf sb
= STRBUF_INIT
;
1669 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1670 sm_gitdir
= absolute_pathdup(sb
.buf
);
1671 strbuf_release(&sb
);
1676 static int dir_contains_only_dotgit(const char *path
)
1678 DIR *dir
= opendir(path
);
1685 e
= readdir_skip_dot_and_dotdot(dir
);
1688 else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT
, e
->d_name
) ||
1689 (e
= readdir_skip_dot_and_dotdot(dir
))) {
1690 error("unexpected item '%s' in '%s'", e
->d_name
, path
);
1698 static int clone_submodule(const struct module_clone_data
*clone_data
,
1699 struct string_list
*reference
)
1702 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1703 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1705 struct child_process cp
= CHILD_PROCESS_INIT
;
1706 const char *clone_data_path
= clone_data
->path
;
1707 char *to_free
= NULL
;
1709 if (validate_submodule_path(clone_data_path
) < 0)
1712 if (!is_absolute_path(clone_data
->path
))
1713 clone_data_path
= to_free
= xstrfmt("%s/%s", get_git_work_tree(),
1716 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1717 die(_("refusing to create/use '%s' in another submodule's "
1718 "git dir"), sm_gitdir
);
1720 if (!file_exists(sm_gitdir
)) {
1721 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1722 !is_empty_dir(clone_data_path
))
1723 die(_("directory not empty: '%s'"), clone_data_path
);
1725 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1726 die(_("could not create directory '%s'"), sm_gitdir
);
1728 prepare_possible_alternates(clone_data
->name
, reference
);
1730 strvec_push(&cp
.args
, "clone");
1731 strvec_push(&cp
.args
, "--no-checkout");
1732 if (clone_data
->quiet
)
1733 strvec_push(&cp
.args
, "--quiet");
1734 if (clone_data
->progress
)
1735 strvec_push(&cp
.args
, "--progress");
1736 if (clone_data
->depth
&& *(clone_data
->depth
))
1737 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1738 if (reference
->nr
) {
1739 struct string_list_item
*item
;
1741 for_each_string_list_item(item
, reference
)
1742 strvec_pushl(&cp
.args
, "--reference",
1743 item
->string
, NULL
);
1745 if (clone_data
->dissociate
)
1746 strvec_push(&cp
.args
, "--dissociate");
1747 if (sm_gitdir
&& *sm_gitdir
)
1748 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1749 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1750 strvec_pushf(&cp
.args
, "--filter=%s",
1751 expand_list_objects_filter_spec(
1752 clone_data
->filter_options
));
1753 if (clone_data
->single_branch
>= 0)
1754 strvec_push(&cp
.args
, clone_data
->single_branch
?
1756 "--no-single-branch");
1758 strvec_push(&cp
.args
, "--");
1759 strvec_push(&cp
.args
, clone_data
->url
);
1760 strvec_push(&cp
.args
, clone_data_path
);
1763 prepare_submodule_repo_env(&cp
.env
);
1766 if(run_command(&cp
))
1767 die(_("clone of '%s' into submodule path '%s' failed"),
1768 clone_data
->url
, clone_data_path
);
1770 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1771 !dir_contains_only_dotgit(clone_data_path
)) {
1772 char *dot_git
= xstrfmt("%s/.git", clone_data_path
);
1775 die(_("directory not empty: '%s'"), clone_data_path
);
1780 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1781 !is_empty_dir(clone_data_path
))
1782 die(_("directory not empty: '%s'"), clone_data_path
);
1783 if (safe_create_leading_directories_const(clone_data_path
) < 0)
1784 die(_("could not create directory '%s'"), clone_data_path
);
1785 path
= xstrfmt("%s/index", sm_gitdir
);
1786 unlink_or_warn(path
);
1791 * We already performed this check at the beginning of this function,
1792 * before cloning the objects. This tries to detect racy behavior e.g.
1793 * in parallel clones, where another process could easily have made the
1794 * gitdir nested _after_ it was created.
1796 * To prevent further harm coming from this unintentionally-nested
1797 * gitdir, let's disable it by deleting the `HEAD` file.
1799 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0) {
1800 char *head
= xstrfmt("%s/HEAD", sm_gitdir
);
1803 die(_("refusing to create/use '%s' in another submodule's "
1804 "git dir"), sm_gitdir
);
1807 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1809 p
= git_pathdup_submodule(clone_data_path
, "config");
1811 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1813 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1814 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1816 git_config_set_in_file(p
, "submodule.alternateLocation",
1818 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1820 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1824 free(error_strategy
);
1832 static int module_clone(int argc
, const char **argv
, const char *prefix
)
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
;
1840 struct option module_clone_options
[] = {
1841 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1843 N_("alternative anchor for relative paths")),
1844 OPT_STRING(0, "path", &clone_data
.path
,
1846 N_("where the new submodule will be cloned to")),
1847 OPT_STRING(0, "name", &clone_data
.name
,
1849 N_("name of the new submodule")),
1850 OPT_STRING(0, "url", &clone_data
.url
,
1852 N_("url where to clone the submodule from")),
1853 OPT_STRING_LIST(0, "reference", &reference
,
1855 N_("reference repository")),
1856 OPT_BOOL(0, "dissociate", &dissociate
,
1857 N_("use --reference only while cloning")),
1858 OPT_STRING(0, "depth", &clone_data
.depth
,
1860 N_("depth for shallow clones")),
1861 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1862 OPT_BOOL(0, "progress", &progress
,
1863 N_("force cloning progress")),
1864 OPT_BOOL(0, "require-init", &require_init
,
1865 N_("disallow cloning into non-empty directory")),
1866 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1867 N_("clone only one branch, HEAD or --branch")),
1868 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1871 const char *const git_submodule_helper_usage
[] = {
1872 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1873 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1874 "[--single-branch] [--filter <filter-spec>] "
1875 "--url <url> --path <path>"),
1879 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1880 git_submodule_helper_usage
, 0);
1882 clone_data
.dissociate
= !!dissociate
;
1883 clone_data
.quiet
= !!quiet
;
1884 clone_data
.progress
= !!progress
;
1885 clone_data
.require_init
= !!require_init
;
1886 clone_data
.filter_options
= &filter_options
;
1888 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1889 usage_with_options(git_submodule_helper_usage
,
1890 module_clone_options
);
1892 clone_submodule(&clone_data
, &reference
);
1893 list_objects_filter_release(&filter_options
);
1894 string_list_clear(&reference
, 1);
1898 static int determine_submodule_update_strategy(struct repository
*r
,
1901 enum submodule_update_type update
,
1902 struct submodule_update_strategy
*out
)
1904 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1909 key
= xstrfmt("submodule.%s.update", sub
->name
);
1913 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1914 if (parse_submodule_update_strategy(val
, out
) < 0) {
1915 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1919 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1920 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1921 BUG("how did we read update = !command from .gitmodules?");
1922 out
->type
= sub
->update_strategy
.type
;
1923 out
->command
= sub
->update_strategy
.command
;
1925 out
->type
= SM_UPDATE_CHECKOUT
;
1928 (out
->type
== SM_UPDATE_MERGE
||
1929 out
->type
== SM_UPDATE_REBASE
||
1930 out
->type
== SM_UPDATE_NONE
))
1931 out
->type
= SM_UPDATE_CHECKOUT
;
1939 struct update_clone_data
{
1940 const struct submodule
*sub
;
1941 struct object_id oid
;
1942 unsigned just_cloned
;
1945 struct submodule_update_clone
{
1946 /* index into 'update_data.list', the list of submodules to look into for cloning */
1949 /* configuration parameters which are passed on to the children */
1950 const struct update_data
*update_data
;
1952 /* to be consumed by update_submodule() */
1953 struct update_clone_data
*update_clone
;
1954 int update_clone_nr
; int update_clone_alloc
;
1956 /* If we want to stop as fast as possible and return an error */
1957 unsigned quickstop
: 1;
1959 /* failed clones to be retried again */
1960 const struct cache_entry
**failed_clones
;
1961 int failed_clones_nr
, failed_clones_alloc
;
1963 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1965 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1967 free(suc
->update_clone
);
1968 free(suc
->failed_clones
);
1971 struct update_data
{
1973 const char *super_prefix
;
1975 enum submodule_update_type update_default
;
1976 struct object_id suboid
;
1977 struct string_list references
;
1978 struct submodule_update_strategy update_strategy
;
1979 struct list_objects_filter_options
*filter_options
;
1980 struct module_list list
;
1984 int recommend_shallow
;
1985 unsigned int require_init
;
1988 unsigned int nofetch
;
1989 unsigned int remote
;
1990 unsigned int progress
;
1991 unsigned int dissociate
;
1993 unsigned int warn_if_uninitialized
;
1994 unsigned int recursive
;
1996 /* copied over from update_clone_data */
1997 struct object_id oid
;
1998 unsigned int just_cloned
;
1999 const char *sm_path
;
2001 #define UPDATE_DATA_INIT { \
2002 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2003 .list = MODULE_LIST_INIT, \
2004 .recommend_shallow = -1, \
2005 .references = STRING_LIST_INIT_DUP, \
2006 .single_branch = -1, \
2010 static void update_data_release(struct update_data
*ud
)
2012 free(ud
->displaypath
);
2013 module_list_release(&ud
->list
);
2016 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
2017 struct strbuf
*out
, const char *displaypath
)
2020 * Only mention uninitialized submodules when their
2021 * paths have been specified.
2023 if (suc
->update_data
->warn_if_uninitialized
) {
2025 _("Submodule path '%s' not initialized"),
2027 strbuf_addch(out
, '\n');
2029 _("Maybe you want to use 'update --init'?"));
2030 strbuf_addch(out
, '\n');
2035 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2036 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2038 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
2039 struct child_process
*child
,
2040 struct submodule_update_clone
*suc
,
2043 const struct submodule
*sub
= NULL
;
2044 const char *url
= NULL
;
2045 const char *update_string
;
2046 enum submodule_update_type update_type
;
2048 const struct update_data
*ud
= suc
->update_data
;
2049 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
2051 struct strbuf sb
= STRBUF_INIT
;
2052 int needs_cloning
= 0;
2053 int need_free_url
= 0;
2056 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
2057 strbuf_addch(out
, '\n');
2061 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
2064 next_submodule_warn_missing(suc
, out
, displaypath
);
2068 key
= xstrfmt("submodule.%s.update", sub
->name
);
2069 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2070 update_type
= parse_submodule_update_type(update_string
);
2072 update_type
= sub
->update_strategy
.type
;
2076 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2077 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2078 && update_type
== SM_UPDATE_NONE
)) {
2079 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2080 strbuf_addch(out
, '\n');
2084 /* Check if the submodule has been initialized. */
2085 if (!is_submodule_active(the_repository
, ce
->name
)) {
2086 next_submodule_warn_missing(suc
, out
, displaypath
);
2091 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2092 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2093 if (sub
->url
&& (starts_with_dot_slash(sub
->url
) ||
2094 starts_with_dot_dot_slash(sub
->url
))) {
2095 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2102 die(_("cannot clone submodule '%s' without a URL"), sub
->name
);
2105 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2106 needs_cloning
= !file_exists(sb
.buf
);
2108 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2109 suc
->update_clone_alloc
);
2110 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2111 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2112 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2113 suc
->update_clone_nr
++;
2119 child
->no_stdin
= 1;
2120 child
->stdout_to_stderr
= 1;
2122 strvec_push(&child
->args
, "submodule--helper");
2123 strvec_push(&child
->args
, "clone");
2124 if (suc
->update_data
->progress
)
2125 strvec_push(&child
->args
, "--progress");
2126 if (suc
->update_data
->quiet
)
2127 strvec_push(&child
->args
, "--quiet");
2128 if (suc
->update_data
->prefix
)
2129 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2130 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2131 strvec_push(&child
->args
, "--depth=1");
2132 else if (suc
->update_data
->depth
)
2133 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2134 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2135 strvec_pushf(&child
->args
, "--filter=%s",
2136 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2137 if (suc
->update_data
->require_init
)
2138 strvec_push(&child
->args
, "--require-init");
2139 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2140 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2141 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2142 if (suc
->update_data
->references
.nr
) {
2143 struct string_list_item
*item
;
2145 for_each_string_list_item(item
, &suc
->update_data
->references
)
2146 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2148 if (suc
->update_data
->dissociate
)
2149 strvec_push(&child
->args
, "--dissociate");
2150 if (suc
->update_data
->single_branch
>= 0)
2151 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2153 "--no-single-branch");
2157 strbuf_release(&sb
);
2161 return needs_cloning
;
2164 static int update_clone_get_next_task(struct child_process
*child
,
2169 struct submodule_update_clone
*suc
= suc_cb
;
2170 const struct cache_entry
*ce
;
2173 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2174 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2175 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2176 int *p
= xmalloc(sizeof(*p
));
2186 * The loop above tried cloning each submodule once, now try the
2187 * stragglers again, which we can imagine as an extension of the
2190 index
= suc
->current
- suc
->update_data
->list
.nr
;
2191 if (index
< suc
->failed_clones_nr
) {
2194 ce
= suc
->failed_clones
[index
];
2195 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2197 strbuf_addstr(err
, "BUG: submodule considered for "
2198 "cloning, doesn't need cloning "
2202 p
= xmalloc(sizeof(*p
));
2212 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2214 void *idx_task_cb UNUSED
)
2216 struct submodule_update_clone
*suc
= suc_cb
;
2222 static int update_clone_task_finished(int result
,
2227 const struct cache_entry
*ce
;
2228 struct submodule_update_clone
*suc
= suc_cb
;
2229 int *idxP
= idx_task_cb
;
2237 if (idx
< suc
->update_data
->list
.nr
) {
2238 ce
= suc
->update_data
->list
.entries
[idx
];
2239 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2241 strbuf_addch(err
, '\n');
2242 ALLOC_GROW(suc
->failed_clones
,
2243 suc
->failed_clones_nr
+ 1,
2244 suc
->failed_clones_alloc
);
2245 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2248 idx
-= suc
->update_data
->list
.nr
;
2249 ce
= suc
->failed_clones
[idx
];
2250 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2252 strbuf_addch(err
, '\n');
2260 static int git_update_clone_config(const char *var
, const char *value
,
2261 const struct config_context
*ctx
,
2266 if (!strcmp(var
, "submodule.fetchjobs"))
2267 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
2271 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2273 struct child_process cp
= CHILD_PROCESS_INIT
;
2274 struct strbuf rev
= STRBUF_INIT
;
2275 char *hex
= oid_to_hex(oid
);
2280 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2282 prepare_submodule_repo_env(&cp
.env
);
2284 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2290 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2291 const struct object_id
*oid
)
2293 struct child_process cp
= CHILD_PROCESS_INIT
;
2295 prepare_submodule_repo_env(&cp
.env
);
2297 cp
.dir
= module_path
;
2299 strvec_push(&cp
.args
, "fetch");
2301 strvec_push(&cp
.args
, "--quiet");
2303 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2305 char *hex
= oid_to_hex(oid
);
2306 char *remote
= get_default_remote();
2308 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2312 return run_command(&cp
);
2315 static int run_update_command(const struct update_data
*ud
, int subforce
)
2317 struct child_process cp
= CHILD_PROCESS_INIT
;
2318 char *oid
= oid_to_hex(&ud
->oid
);
2321 switch (ud
->update_strategy
.type
) {
2322 case SM_UPDATE_CHECKOUT
:
2324 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2326 strvec_push(&cp
.args
, "-f");
2328 case SM_UPDATE_REBASE
:
2330 strvec_push(&cp
.args
, "rebase");
2332 strvec_push(&cp
.args
, "--quiet");
2334 case SM_UPDATE_MERGE
:
2336 strvec_push(&cp
.args
, "merge");
2338 strvec_push(&cp
.args
, "--quiet");
2340 case SM_UPDATE_COMMAND
:
2342 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2345 BUG("unexpected update strategy type: %d",
2346 ud
->update_strategy
.type
);
2348 strvec_push(&cp
.args
, oid
);
2350 cp
.dir
= ud
->sm_path
;
2351 prepare_submodule_repo_env(&cp
.env
);
2352 if ((ret
= run_command(&cp
))) {
2353 switch (ud
->update_strategy
.type
) {
2354 case SM_UPDATE_CHECKOUT
:
2355 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2356 oid
, ud
->displaypath
);
2357 /* No "ret" assignment, use "git checkout"'s */
2359 case SM_UPDATE_REBASE
:
2360 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2361 oid
, ud
->displaypath
);
2363 case SM_UPDATE_MERGE
:
2364 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2365 oid
, ud
->displaypath
);
2367 case SM_UPDATE_COMMAND
:
2368 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2369 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2372 BUG("unexpected update strategy type: %d",
2373 ud
->update_strategy
.type
);
2382 switch (ud
->update_strategy
.type
) {
2383 case SM_UPDATE_CHECKOUT
:
2384 printf(_("Submodule path '%s': checked out '%s'\n"),
2385 ud
->displaypath
, oid
);
2387 case SM_UPDATE_REBASE
:
2388 printf(_("Submodule path '%s': rebased into '%s'\n"),
2389 ud
->displaypath
, oid
);
2391 case SM_UPDATE_MERGE
:
2392 printf(_("Submodule path '%s': merged in '%s'\n"),
2393 ud
->displaypath
, oid
);
2395 case SM_UPDATE_COMMAND
:
2396 printf(_("Submodule path '%s': '%s %s'\n"),
2397 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2400 BUG("unexpected update strategy type: %d",
2401 ud
->update_strategy
.type
);
2407 static int run_update_procedure(const struct update_data
*ud
)
2409 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2413 * Run fetch only if `oid` isn't present or it
2414 * is not reachable from a ref.
2416 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2417 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2420 _("Unable to fetch in submodule path '%s'; "
2421 "trying to directly fetch %s:"),
2422 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2424 * Now we tried the usual fetch, but `oid` may
2425 * not be reachable from any of the refs.
2427 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2428 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2429 return die_message(_("Fetched in submodule path '%s', but it did not "
2430 "contain %s. Direct fetching of that commit failed."),
2431 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2434 return run_update_command(ud
, subforce
);
2437 static int remote_submodule_branch(const char *path
, const char **branch
)
2439 const struct submodule
*sub
;
2443 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2445 return die_message(_("could not initialize submodule at path '%s'"),
2448 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2449 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2450 *branch
= sub
->branch
;
2458 if (!strcmp(*branch
, ".")) {
2459 const char *refname
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
2464 return die_message(_("No such ref: %s"), "HEAD");
2467 if (!strcmp(refname
, "HEAD"))
2468 return die_message(_("Submodule (%s) branch configured to inherit "
2469 "branch from superproject, but the superproject "
2470 "is not on any branch"), sub
->name
);
2472 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2473 return die_message(_("Expecting a full ref name, got %s"),
2480 /* Our "branch" is coming from repo_config_get_string_tmp() */
2484 static int ensure_core_worktree(const char *path
)
2487 struct repository subrepo
;
2489 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2490 return die_message(_("could not get a repository handle for submodule '%s'"),
2493 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2494 char *cfg_file
, *abs_path
;
2495 const char *rel_path
;
2496 struct strbuf sb
= STRBUF_INIT
;
2498 cfg_file
= repo_git_path(&subrepo
, "config");
2500 abs_path
= absolute_pathdup(path
);
2501 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2503 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2507 strbuf_release(&sb
);
2510 repo_clear(&subrepo
);
2514 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2517 case SM_UPDATE_CHECKOUT
:
2519 case SM_UPDATE_MERGE
:
2521 case SM_UPDATE_REBASE
:
2523 case SM_UPDATE_UNSPECIFIED
:
2524 case SM_UPDATE_NONE
:
2525 case SM_UPDATE_COMMAND
:
2528 BUG("unreachable with type %d", type
);
2531 static void update_data_to_args(const struct update_data
*update_data
,
2532 struct strvec
*args
)
2534 enum submodule_update_type update_type
= update_data
->update_default
;
2536 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2537 if (update_data
->displaypath
) {
2538 strvec_push(args
, "--super-prefix");
2539 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2541 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2542 if (update_data
->quiet
)
2543 strvec_push(args
, "--quiet");
2544 if (update_data
->force
)
2545 strvec_push(args
, "--force");
2546 if (update_data
->init
)
2547 strvec_push(args
, "--init");
2548 if (update_data
->remote
)
2549 strvec_push(args
, "--remote");
2550 if (update_data
->nofetch
)
2551 strvec_push(args
, "--no-fetch");
2552 if (update_data
->dissociate
)
2553 strvec_push(args
, "--dissociate");
2554 if (update_data
->progress
)
2555 strvec_push(args
, "--progress");
2556 if (update_data
->require_init
)
2557 strvec_push(args
, "--require-init");
2558 if (update_data
->depth
)
2559 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2560 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2561 strvec_pushf(args
, "--%s",
2562 submodule_update_type_to_label(update_type
));
2564 if (update_data
->references
.nr
) {
2565 struct string_list_item
*item
;
2567 for_each_string_list_item(item
, &update_data
->references
)
2568 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2570 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2571 strvec_pushf(args
, "--filter=%s",
2572 expand_list_objects_filter_spec(
2573 update_data
->filter_options
));
2574 if (update_data
->recommend_shallow
== 0)
2575 strvec_push(args
, "--no-recommend-shallow");
2576 else if (update_data
->recommend_shallow
== 1)
2577 strvec_push(args
, "--recommend-shallow");
2578 if (update_data
->single_branch
>= 0)
2579 strvec_push(args
, update_data
->single_branch
?
2581 "--no-single-branch");
2584 static int update_submodule(struct update_data
*update_data
)
2588 if (validate_submodule_path(update_data
->sm_path
) < 0)
2591 ret
= determine_submodule_update_strategy(the_repository
,
2592 update_data
->just_cloned
,
2593 update_data
->sm_path
,
2594 update_data
->update_default
,
2595 &update_data
->update_strategy
);
2599 if (update_data
->just_cloned
)
2600 oidcpy(&update_data
->suboid
, null_oid());
2601 else if (repo_resolve_gitlink_ref(the_repository
, update_data
->sm_path
,
2602 "HEAD", &update_data
->suboid
))
2603 return die_message(_("Unable to find current revision in submodule path '%s'"),
2604 update_data
->displaypath
);
2606 if (update_data
->remote
) {
2612 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2615 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2618 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2622 if (!update_data
->nofetch
) {
2623 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2625 return die_message(_("Unable to fetch in submodule path '%s'"),
2626 update_data
->sm_path
);
2629 if (repo_resolve_gitlink_ref(the_repository
, update_data
->sm_path
,
2630 remote_ref
, &update_data
->oid
))
2631 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2632 remote_ref
, update_data
->sm_path
);
2637 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2638 ret
= run_update_procedure(update_data
);
2643 if (update_data
->recursive
) {
2644 struct child_process cp
= CHILD_PROCESS_INIT
;
2645 struct update_data next
= *update_data
;
2648 oidcpy(&next
.oid
, null_oid());
2649 oidcpy(&next
.suboid
, null_oid());
2651 cp
.dir
= update_data
->sm_path
;
2653 prepare_submodule_repo_env(&cp
.env
);
2654 update_data_to_args(&next
, &cp
.args
);
2656 ret
= run_command(&cp
);
2658 die_message(_("Failed to recurse into submodule path '%s'"),
2659 update_data
->displaypath
);
2666 static int update_submodules(struct update_data
*update_data
)
2669 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2670 const struct run_process_parallel_opts opts
= {
2671 .tr2_category
= "submodule",
2672 .tr2_label
= "parallel/update",
2674 .processes
= update_data
->max_jobs
,
2676 .get_next_task
= update_clone_get_next_task
,
2677 .start_failure
= update_clone_start_failure
,
2678 .task_finished
= update_clone_task_finished
,
2682 suc
.update_data
= update_data
;
2683 run_processes_parallel(&opts
);
2686 * We saved the output and put it out all at once now.
2688 * - the listener does not have to interleave their (checkout)
2689 * work with our fetching. The writes involved in a
2690 * checkout involve more straightforward sequential I/O.
2691 * - the listener can avoid doing any work if fetching failed.
2693 if (suc
.quickstop
) {
2698 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2699 struct update_clone_data ucd
= suc
.update_clone
[i
];
2702 oidcpy(&update_data
->oid
, &ucd
.oid
);
2703 update_data
->just_cloned
= ucd
.just_cloned
;
2704 update_data
->sm_path
= ucd
.sub
->path
;
2707 * Verify that the submodule path does not contain any
2708 * symlinks; if it does, it might have been tampered with.
2709 * TODO: allow exempting it via
2710 * `safe.submodule.path` or something
2712 if (validate_submodule_path(update_data
->sm_path
) < 0)
2715 code
= ensure_core_worktree(update_data
->sm_path
);
2719 update_data
->displaypath
= get_submodule_displaypath(
2720 update_data
->sm_path
, update_data
->prefix
,
2721 update_data
->super_prefix
);
2722 code
= update_submodule(update_data
);
2723 FREE_AND_NULL(update_data
->displaypath
);
2733 submodule_update_clone_release(&suc
);
2734 string_list_clear(&update_data
->references
, 0);
2738 static int module_update(int argc
, const char **argv
, const char *prefix
)
2740 struct pathspec pathspec
= { 0 };
2741 struct pathspec pathspec2
= { 0 };
2742 struct update_data opt
= UPDATE_DATA_INIT
;
2743 struct list_objects_filter_options filter_options
=
2744 LIST_OBJECTS_FILTER_INIT
;
2746 struct option module_update_options
[] = {
2747 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2748 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2749 OPT_BOOL(0, "init", &opt
.init
,
2750 N_("initialize uninitialized submodules before update")),
2751 OPT_BOOL(0, "remote", &opt
.remote
,
2752 N_("use SHA-1 of submodule's remote tracking branch")),
2753 OPT_BOOL(0, "recursive", &opt
.recursive
,
2754 N_("traverse submodules recursively")),
2755 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2756 N_("don't fetch new objects from the remote site")),
2757 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2758 N_("use the 'checkout' update strategy (default)"),
2759 SM_UPDATE_CHECKOUT
),
2760 OPT_SET_INT('m', "merge", &opt
.update_default
,
2761 N_("use the 'merge' update strategy"),
2763 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2764 N_("use the 'rebase' update strategy"),
2766 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2767 N_("reference repository")),
2768 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2769 N_("use --reference only while cloning")),
2770 OPT_INTEGER(0, "depth", &opt
.depth
,
2771 N_("create a shallow clone truncated to the "
2772 "specified number of revisions")),
2773 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2774 N_("parallel jobs")),
2775 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2776 N_("whether the initial clone should follow the shallow recommendation")),
2777 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2778 OPT_BOOL(0, "progress", &opt
.progress
,
2779 N_("force cloning progress")),
2780 OPT_BOOL(0, "require-init", &opt
.require_init
,
2781 N_("disallow cloning into non-empty directory, implies --init")),
2782 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2783 N_("clone only one branch, HEAD or --branch")),
2784 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2787 const char *const git_submodule_helper_usage
[] = {
2788 N_("git submodule [--quiet] update"
2789 " [--init [--filter=<filter-spec>]] [--remote]"
2790 " [-N|--no-fetch] [-f|--force]"
2791 " [--checkout|--merge|--rebase]"
2792 " [--[no-]recommend-shallow] [--reference <repository>]"
2793 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2797 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2798 git_config(git_update_clone_config
, &opt
.max_jobs
);
2800 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2801 git_submodule_helper_usage
, 0);
2803 if (opt
.require_init
)
2806 if (filter_options
.choice
&& !opt
.init
) {
2807 usage_with_options(git_submodule_helper_usage
,
2808 module_update_options
);
2811 opt
.filter_options
= &filter_options
;
2812 opt
.prefix
= prefix
;
2814 if (opt
.update_default
)
2815 opt
.update_strategy
.type
= opt
.update_default
;
2817 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2823 opt
.warn_if_uninitialized
= 1;
2826 struct module_list list
= MODULE_LIST_INIT
;
2827 struct init_cb info
= INIT_CB_INIT
;
2829 if (module_list_compute(argv
, opt
.prefix
,
2830 &pathspec2
, &list
) < 0) {
2831 module_list_release(&list
);
2837 * If there are no path args and submodule.active is set then,
2838 * by default, only initialize 'active' modules.
2840 if (!argc
&& !git_config_get("submodule.active"))
2841 module_list_active(&list
);
2843 info
.prefix
= opt
.prefix
;
2844 info
.super_prefix
= opt
.super_prefix
;
2846 info
.flags
|= OPT_QUIET
;
2848 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2849 module_list_release(&list
);
2852 ret
= update_submodules(&opt
);
2854 update_data_release(&opt
);
2855 list_objects_filter_release(&filter_options
);
2856 clear_pathspec(&pathspec
);
2857 clear_pathspec(&pathspec2
);
2861 static int push_check(int argc
, const char **argv
, const char *prefix UNUSED
)
2863 struct remote
*remote
;
2864 const char *superproject_head
;
2866 int detached_head
= 0;
2867 struct object_id head_oid
;
2870 die("submodule--helper push-check requires at least 2 arguments");
2873 * superproject's resolved head ref.
2874 * if HEAD then the superproject is in a detached head state, otherwise
2875 * it will be the resolved head ref.
2877 superproject_head
= argv
[1];
2880 /* Get the submodule's head ref and determine if it is detached */
2881 head
= refs_resolve_refdup(get_main_ref_store(the_repository
), "HEAD",
2882 0, &head_oid
, NULL
);
2884 die(_("Failed to resolve HEAD as a valid ref."));
2885 if (!strcmp(head
, "HEAD"))
2889 * The remote must be configured.
2890 * This is to avoid pushing to the exact same URL as the parent.
2892 remote
= pushremote_get(argv
[1]);
2893 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2894 die("remote '%s' not configured", argv
[1]);
2896 /* Check the refspec */
2899 struct ref
*local_refs
= get_local_heads();
2900 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2902 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2904 for (i
= 0; i
< refspec
.nr
; i
++) {
2905 const struct refspec_item
*rs
= &refspec
.items
[i
];
2907 if (rs
->pattern
|| rs
->matching
)
2910 /* LHS must match a single ref */
2911 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2916 * If LHS matches 'HEAD' then we need to ensure
2917 * that it matches the same named branch
2918 * checked out in the superproject.
2920 if (!strcmp(rs
->src
, "HEAD")) {
2921 if (!detached_head
&&
2922 !strcmp(head
, superproject_head
))
2924 die("HEAD does not match the named branch in the superproject");
2928 die("src refspec '%s' must name a ref",
2932 refspec_clear(&refspec
);
2939 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2942 struct pathspec pathspec
= { 0 };
2943 struct module_list list
= MODULE_LIST_INIT
;
2944 const char *super_prefix
= NULL
;
2945 struct option embed_gitdir_options
[] = {
2946 OPT__SUPER_PREFIX(&super_prefix
),
2949 const char *const git_submodule_helper_usage
[] = {
2950 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2955 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2956 git_submodule_helper_usage
, 0);
2958 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
2961 for (i
= 0; i
< list
.nr
; i
++)
2962 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
2967 clear_pathspec(&pathspec
);
2968 module_list_release(&list
);
2972 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2978 struct option options
[] = {
2979 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2982 const char *const usage
[] = {
2983 N_("git submodule set-url [--quiet] <path> <newurl>"),
2986 const struct submodule
*sub
;
2988 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2990 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2991 usage_with_options(usage
, options
);
2993 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2996 die(_("no submodule mapping found in .gitmodules for path '%s'"),
2999 config_name
= xstrfmt("submodule.%s.url", sub
->name
);
3000 ret
= config_set_in_gitmodules_file_gently(config_name
, newurl
);
3003 repo_read_gitmodules(the_repository
, 0);
3004 sync_submodule(sub
->path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
3011 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
3013 int opt_default
= 0, ret
;
3014 const char *opt_branch
= NULL
;
3017 struct option options
[] = {
3019 * We accept the `quiet` option for uniformity across subcommands,
3020 * though there is nothing to make less verbose in this subcommand.
3022 OPT_NOOP_NOARG('q', "quiet"),
3024 OPT_BOOL('d', "default", &opt_default
,
3025 N_("set the default tracking branch to master")),
3026 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
3027 N_("set the default tracking branch")),
3030 const char *const usage
[] = {
3031 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3032 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3035 const struct submodule
*sub
;
3037 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3039 if (!opt_branch
&& !opt_default
)
3040 die(_("--branch or --default required"));
3042 if (opt_branch
&& opt_default
)
3043 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3045 if (argc
!= 1 || !(path
= argv
[0]))
3046 usage_with_options(usage
, options
);
3048 sub
= submodule_from_path(the_repository
, null_oid(), path
);
3051 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3054 config_name
= xstrfmt("submodule.%s.branch", sub
->name
);
3055 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
3061 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
3063 enum branch_track track
;
3064 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
3065 struct option options
[] = {
3066 OPT__QUIET(&quiet
, N_("print only error messages")),
3067 OPT__FORCE(&force
, N_("force creation"), 0),
3068 OPT_BOOL(0, "create-reflog", &reflog
,
3069 N_("create the branch's reflog")),
3070 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
3071 N_("set branch tracking configuration"),
3073 parse_opt_tracking_mode
),
3074 OPT__DRY_RUN(&dry_run
,
3075 N_("show whether the branch would be created")),
3078 const char *const usage
[] = {
3079 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3083 git_config(git_default_config
, NULL
);
3084 track
= git_branch_track
;
3085 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3088 usage_with_options(usage
, options
);
3090 if (!quiet
&& !dry_run
)
3091 printf_ln(_("creating branch '%s'"), argv
[0]);
3093 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
3094 force
, reflog
, quiet
, track
, dry_run
);
3101 const char *reference_path
;
3103 const char *sm_name
;
3105 const char *realrepo
;
3107 unsigned int force
: 1;
3108 unsigned int quiet
: 1;
3109 unsigned int progress
: 1;
3110 unsigned int dissociate
: 1;
3112 #define ADD_DATA_INIT { .depth = -1 }
3114 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3116 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3117 struct strbuf sb_remote_out
= STRBUF_INIT
;
3119 cp_remote
.git_cmd
= 1;
3120 strvec_pushf(&cp_remote
.env
,
3121 "GIT_DIR=%s", git_dir_path
);
3122 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3123 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3124 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3126 char *line
= sb_remote_out
.buf
;
3128 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3129 size_t len
= next_line
- line
;
3131 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3132 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3133 line
= next_line
+ 1;
3137 strbuf_release(&sb_remote_out
);
3140 static int add_submodule(const struct add_data
*add_data
)
3142 char *submod_gitdir_path
;
3143 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3144 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3147 /* perhaps the path already exists and is already a git repo, else clone it */
3148 if (is_directory(add_data
->sm_path
)) {
3149 struct strbuf sm_path
= STRBUF_INIT
;
3150 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3151 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3152 if (is_nonbare_repository_dir(&sm_path
))
3153 printf(_("Adding existing repo at '%s' to the index\n"),
3156 die(_("'%s' already exists and is not a valid git repo"),
3158 strbuf_release(&sm_path
);
3159 free(submod_gitdir_path
);
3161 struct child_process cp
= CHILD_PROCESS_INIT
;
3163 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3165 if (is_directory(submod_gitdir_path
)) {
3166 if (!add_data
->force
) {
3167 struct strbuf msg
= STRBUF_INIT
;
3170 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3171 "locally with remote(s):\n"),
3174 append_fetch_remotes(&msg
, submod_gitdir_path
);
3175 free(submod_gitdir_path
);
3177 strbuf_addf(&msg
, _("If you want to reuse this local git "
3178 "directory instead of cloning again from\n"
3180 "use the '--force' option. If the local git "
3181 "directory is not the correct repo\n"
3182 "or you are unsure what this means choose "
3183 "another name with the '--name' option."),
3184 add_data
->realrepo
);
3186 die_msg
= strbuf_detach(&msg
, NULL
);
3189 printf(_("Reactivating local git directory for "
3190 "submodule '%s'\n"), add_data
->sm_name
);
3193 free(submod_gitdir_path
);
3195 clone_data
.prefix
= add_data
->prefix
;
3196 clone_data
.path
= add_data
->sm_path
;
3197 clone_data
.name
= add_data
->sm_name
;
3198 clone_data
.url
= add_data
->realrepo
;
3199 clone_data
.quiet
= add_data
->quiet
;
3200 clone_data
.progress
= add_data
->progress
;
3201 if (add_data
->reference_path
) {
3202 char *p
= xstrdup(add_data
->reference_path
);
3204 string_list_append(&reference
, p
)->util
= p
;
3206 clone_data
.dissociate
= add_data
->dissociate
;
3207 if (add_data
->depth
>= 0)
3208 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3210 if (clone_submodule(&clone_data
, &reference
))
3213 prepare_submodule_repo_env(&cp
.env
);
3215 cp
.dir
= add_data
->sm_path
;
3217 * NOTE: we only get here if add_data->force is true, so
3218 * passing --force to checkout is reasonable.
3220 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3222 if (add_data
->branch
) {
3223 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3224 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3227 if (run_command(&cp
))
3228 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3232 string_list_clear(&reference
, 1);
3236 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3241 if (!is_writing_gitmodules_ok())
3242 die(_("please make sure that the .gitmodules file is in the working tree"));
3244 key
= xstrfmt("submodule.%s.%s", name
, var
);
3245 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3251 static void configure_added_submodule(struct add_data
*add_data
)
3254 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3255 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3257 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3258 git_config_set_gently(key
, add_data
->realrepo
);
3261 add_submod
.git_cmd
= 1;
3262 strvec_pushl(&add_submod
.args
, "add",
3263 "--no-warn-embedded-repo", NULL
);
3264 if (add_data
->force
)
3265 strvec_push(&add_submod
.args
, "--force");
3266 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3268 if (run_command(&add_submod
))
3269 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3271 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3272 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3273 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3275 if (add_data
->branch
) {
3276 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3277 "branch", add_data
->branch
))
3278 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3281 add_gitmodules
.git_cmd
= 1;
3282 strvec_pushl(&add_gitmodules
.args
,
3283 "add", "--force", "--", ".gitmodules", NULL
);
3285 if (run_command(&add_gitmodules
))
3286 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3289 * NEEDSWORK: In a multi-working-tree world this needs to be
3290 * set in the per-worktree config.
3293 * NEEDSWORK: In the longer run, we need to get rid of this
3294 * pattern of querying "submodule.active" before calling
3295 * is_submodule_active(), since that function needs to find
3296 * out the value of "submodule.active" again anyway.
3298 if (!git_config_get("submodule.active")) {
3300 * If the submodule being added isn't already covered by the
3301 * current configured pathspec, set the submodule's active flag
3303 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3304 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3305 git_config_set_gently(key
, "true");
3309 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3310 git_config_set_gently(key
, "true");
3315 static void die_on_index_match(const char *path
, int force
)
3318 const char *args
[] = { path
, NULL
};
3319 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3321 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3322 die(_("index file corrupt"));
3326 char *ps_matched
= xcalloc(ps
.nr
, 1);
3328 /* TODO: audit for interaction with sparse-index. */
3329 ensure_full_index(the_repository
->index
);
3332 * Since there is only one pathspec, we just need to
3333 * check ps_matched[0] to know if a cache entry matched.
3335 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
3336 ce_path_match(the_repository
->index
, the_repository
->index
->cache
[i
], &ps
,
3339 if (ps_matched
[0]) {
3341 die(_("'%s' already exists in the index"),
3343 if (!S_ISGITLINK(the_repository
->index
->cache
[i
]->ce_mode
))
3344 die(_("'%s' already exists in the index "
3345 "and is not a submodule"), path
);
3351 clear_pathspec(&ps
);
3354 static void die_on_repo_without_commits(const char *path
)
3356 struct strbuf sb
= STRBUF_INIT
;
3357 strbuf_addstr(&sb
, path
);
3358 if (is_nonbare_repository_dir(&sb
)) {
3359 struct object_id oid
;
3360 if (repo_resolve_gitlink_ref(the_repository
, path
, "HEAD", &oid
) < 0)
3361 die(_("'%s' does not have a commit checked out"), path
);
3363 strbuf_release(&sb
);
3366 static int module_add(int argc
, const char **argv
, const char *prefix
)
3368 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3369 struct add_data add_data
= ADD_DATA_INIT
;
3370 char *to_free
= NULL
;
3371 struct option options
[] = {
3372 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3373 N_("branch of repository to add as submodule")),
3374 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3375 PARSE_OPT_NOCOMPLETE
),
3376 OPT__QUIET(&quiet
, N_("print only error messages")),
3377 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3378 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3379 N_("reference repository")),
3380 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3381 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3382 N_("sets the submodule's name to the given string "
3383 "instead of defaulting to its path")),
3384 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3387 const char *const usage
[] = {
3388 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3391 struct strbuf sb
= STRBUF_INIT
;
3394 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3396 if (!is_writing_gitmodules_ok())
3397 die(_("please make sure that the .gitmodules file is in the working tree"));
3399 if (prefix
&& *prefix
&&
3400 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3401 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3403 if (argc
== 0 || argc
> 2)
3404 usage_with_options(usage
, options
);
3406 add_data
.repo
= argv
[0];
3408 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3410 add_data
.sm_path
= xstrdup(argv
[1]);
3412 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3413 char *sm_path
= add_data
.sm_path
;
3415 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3419 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3420 starts_with_dot_slash(add_data
.repo
)) {
3422 die(_("Relative path can only be used from the toplevel "
3423 "of the working tree"));
3425 /* dereference source url relative to parent's url */
3426 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3427 add_data
.realrepo
= to_free
;
3428 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3429 add_data
.realrepo
= add_data
.repo
;
3431 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3437 * multiple //; leading ./; /./; /../;
3439 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3440 strip_dir_trailing_slashes(add_data
.sm_path
);
3442 if (validate_submodule_path(add_data
.sm_path
) < 0)
3445 die_on_index_match(add_data
.sm_path
, force
);
3446 die_on_repo_without_commits(add_data
.sm_path
);
3449 struct child_process cp
= CHILD_PROCESS_INIT
;
3453 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3454 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3455 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3456 strbuf_complete_line(&sb
);
3457 fputs(sb
.buf
, stderr
);
3462 if(!add_data
.sm_name
)
3463 add_data
.sm_name
= add_data
.sm_path
;
3465 if (check_submodule_name(add_data
.sm_name
))
3466 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3468 add_data
.prefix
= prefix
;
3469 add_data
.force
= !!force
;
3470 add_data
.quiet
= !!quiet
;
3471 add_data
.progress
= !!progress
;
3472 add_data
.dissociate
= !!dissociate
;
3474 if (add_submodule(&add_data
))
3476 configure_added_submodule(&add_data
);
3480 free(add_data
.sm_path
);
3482 strbuf_release(&sb
);
3487 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3489 parse_opt_subcommand_fn
*fn
= NULL
;
3490 const char *const usage
[] = {
3491 N_("git submodule--helper <command>"),
3494 struct option options
[] = {
3495 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3496 OPT_SUBCOMMAND("add", &fn
, module_add
),
3497 OPT_SUBCOMMAND("update", &fn
, module_update
),
3498 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3499 OPT_SUBCOMMAND("init", &fn
, module_init
),
3500 OPT_SUBCOMMAND("status", &fn
, module_status
),
3501 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3502 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3503 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3504 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3505 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3506 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3507 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3508 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3511 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3513 return fn(argc
, argv
, prefix
);