1 #define USE_THE_INDEX_VARIABLE
5 #include "environment.h"
8 #include "repository.h"
10 #include "parse-options.h"
13 #include "preload-index.h"
15 #include "read-cache.h"
17 #include "sparse-index.h"
18 #include "submodule.h"
19 #include "submodule-config.h"
20 #include "string-list.h"
21 #include "run-command.h"
29 #include "object-file.h"
30 #include "object-name.h"
31 #include "object-store.h"
34 #include "list-objects-filter-options.h"
36 #define OPT_QUIET (1 << 0)
37 #define OPT_CACHED (1 << 1)
38 #define OPT_RECURSIVE (1 << 2)
39 #define OPT_FORCE (1 << 3)
41 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
44 static int repo_get_default_remote(struct repository
*repo
, char **default_remote
)
47 struct strbuf sb
= STRBUF_INIT
;
48 struct ref_store
*store
= get_main_ref_store(repo
);
49 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
53 return die_message(_("No such ref: %s"), "HEAD");
56 if (!strcmp(refname
, "HEAD")) {
57 *default_remote
= xstrdup("origin");
61 if (!skip_prefix(refname
, "refs/heads/", &refname
))
62 return die_message(_("Expecting a full ref name, got %s"),
65 strbuf_addf(&sb
, "branch.%s.remote", refname
);
66 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
67 *default_remote
= xstrdup("origin");
69 *default_remote
= dest
;
75 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
77 struct repository subrepo
;
80 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
82 return die_message(_("could not get a repository handle for submodule '%s'"),
84 ret
= repo_get_default_remote(&subrepo
, default_remote
);
90 static char *get_default_remote(void)
93 int code
= repo_get_default_remote(the_repository
, &default_remote
);
98 return default_remote
;
101 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
103 char *remoteurl
, *resolved_url
;
104 char *remote
= get_default_remote();
105 struct strbuf remotesb
= STRBUF_INIT
;
107 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
108 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
110 warning(_("could not look up configuration '%s'. "
111 "Assuming this repository is its own "
112 "authoritative upstream."),
114 remoteurl
= xgetcwd();
116 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
120 strbuf_release(&remotesb
);
125 /* the result should be freed by the caller. */
126 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
127 const char *super_prefix
)
129 if (prefix
&& super_prefix
) {
130 BUG("cannot have prefix '%s' and superprefix '%s'",
131 prefix
, super_prefix
);
133 struct strbuf sb
= STRBUF_INIT
;
134 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
137 } else if (super_prefix
) {
138 return xstrfmt("%s%s", super_prefix
, path
);
140 return xstrdup(path
);
144 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
146 struct strbuf sb
= STRBUF_INIT
;
149 static const char *describe_bare
[] = { NULL
};
151 static const char *describe_tags
[] = { "--tags", NULL
};
153 static const char *describe_contains
[] = { "--contains", NULL
};
155 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
157 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
159 describe_all_always
, NULL
};
161 for (d
= describe_argv
; *d
; d
++) {
162 struct child_process cp
= CHILD_PROCESS_INIT
;
163 prepare_submodule_repo_env(&cp
.env
);
168 strvec_push(&cp
.args
, "describe");
169 strvec_pushv(&cp
.args
, *d
);
170 strvec_push(&cp
.args
, object_id
);
172 if (!capture_command(&cp
, &sb
, 0)) {
173 strbuf_strip_suffix(&sb
, "\n");
174 return strbuf_detach(&sb
, NULL
);
183 const struct cache_entry
**entries
;
186 #define MODULE_LIST_INIT { 0 }
188 static void module_list_release(struct module_list
*ml
)
193 static int module_list_compute(const char **argv
,
195 struct pathspec
*pathspec
,
196 struct module_list
*list
)
199 char *ps_matched
= NULL
;
201 parse_pathspec(pathspec
, 0,
202 PATHSPEC_PREFER_FULL
,
206 ps_matched
= xcalloc(pathspec
->nr
, 1);
208 if (repo_read_index(the_repository
) < 0)
209 die(_("index file corrupt"));
211 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
212 const struct cache_entry
*ce
= the_index
.cache
[i
];
214 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
216 !S_ISGITLINK(ce
->ce_mode
))
219 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
220 list
->entries
[list
->nr
++] = ce
;
221 while (i
+ 1 < the_index
.cache_nr
&&
222 !strcmp(ce
->name
, the_index
.cache
[i
+ 1]->name
))
224 * Skip entries with the same name in different stages
225 * to make sure an entry is returned only once.
230 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
238 static void module_list_active(struct module_list
*list
)
241 struct module_list active_modules
= MODULE_LIST_INIT
;
243 for (i
= 0; i
< list
->nr
; i
++) {
244 const struct cache_entry
*ce
= list
->entries
[i
];
246 if (!is_submodule_active(the_repository
, ce
->name
))
249 ALLOC_GROW(active_modules
.entries
,
250 active_modules
.nr
+ 1,
251 active_modules
.alloc
);
252 active_modules
.entries
[active_modules
.nr
++] = ce
;
255 module_list_release(list
);
256 *list
= active_modules
;
259 static char *get_up_path(const char *path
)
262 struct strbuf sb
= STRBUF_INIT
;
264 for (i
= count_slashes(path
); i
; i
--)
265 strbuf_addstr(&sb
, "../");
268 * Check if 'path' ends with slash or not
269 * for having the same output for dir/sub_dir
272 if (!is_dir_sep(path
[strlen(path
) - 1]))
273 strbuf_addstr(&sb
, "../");
275 return strbuf_detach(&sb
, NULL
);
278 static void for_each_listed_submodule(const struct module_list
*list
,
279 each_submodule_fn fn
, void *cb_data
)
283 for (i
= 0; i
< list
->nr
; i
++)
284 fn(list
->entries
[i
], cb_data
);
291 const char *super_prefix
;
295 #define FOREACH_CB_INIT { 0 }
297 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
300 struct foreach_cb
*info
= cb_data
;
301 const char *path
= list_item
->name
;
302 const struct object_id
*ce_oid
= &list_item
->oid
;
303 const struct submodule
*sub
;
304 struct child_process cp
= CHILD_PROCESS_INIT
;
307 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
310 sub
= submodule_from_path(the_repository
, null_oid(), path
);
313 die(_("No url found for submodule path '%s' in .gitmodules"),
316 if (!is_submodule_populated_gently(path
, NULL
))
319 prepare_submodule_repo_env(&cp
.env
);
322 * For the purpose of executing <command> in the submodule,
323 * separate shell is used for the purpose of running the
330 * NEEDSWORK: the command currently has access to the variables $name,
331 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
332 * contains a single argument. This is done for maintaining a faithful
333 * translation from shell script.
335 if (info
->argc
== 1) {
336 char *toplevel
= xgetcwd();
337 struct strbuf sb
= STRBUF_INIT
;
339 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
340 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
341 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
342 strvec_pushf(&cp
.env
, "sha1=%s",
344 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
347 * Since the path variable was accessible from the script
348 * before porting, it is also made available after porting.
349 * The environment variable "PATH" has a very special purpose
350 * on windows. And since environment variables are
351 * case-insensitive in windows, it interferes with the
352 * existing PATH variable. Hence, to avoid that, we expose
353 * path via the args strvec and not via env.
355 sq_quote_buf(&sb
, path
);
356 strvec_pushf(&cp
.args
, "path=%s; %s",
357 sb
.buf
, info
->argv
[0]);
361 strvec_pushv(&cp
.args
, info
->argv
);
365 printf(_("Entering '%s'\n"), displaypath
);
367 if (info
->argv
[0] && run_command(&cp
))
368 die(_("run_command returned non-zero status for %s\n."),
371 if (info
->recursive
) {
372 struct child_process cpr
= CHILD_PROCESS_INIT
;
376 prepare_submodule_repo_env(&cpr
.env
);
378 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
380 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
381 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
384 strvec_push(&cpr
.args
, "--quiet");
386 strvec_push(&cpr
.args
, "--");
387 strvec_pushv(&cpr
.args
, info
->argv
);
389 if (run_command(&cpr
))
390 die(_("run_command returned non-zero status while "
391 "recursing in the nested submodules of %s\n."),
399 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
401 struct foreach_cb info
= FOREACH_CB_INIT
;
402 struct pathspec pathspec
= { 0 };
403 struct module_list list
= MODULE_LIST_INIT
;
404 struct option module_foreach_options
[] = {
405 OPT__SUPER_PREFIX(&info
.super_prefix
),
406 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
407 OPT_BOOL(0, "recursive", &info
.recursive
,
408 N_("recurse into nested submodules")),
411 const char *const git_submodule_helper_usage
[] = {
412 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
417 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
418 git_submodule_helper_usage
, 0);
420 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
425 info
.prefix
= prefix
;
427 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
431 module_list_release(&list
);
432 clear_pathspec(&pathspec
);
436 static int starts_with_dot_slash(const char *const path
)
438 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
439 PATH_MATCH_XPLATFORM
);
442 static int starts_with_dot_dot_slash(const char *const path
)
444 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
445 PATH_MATCH_XPLATFORM
);
450 const char *super_prefix
;
453 #define INIT_CB_INIT { 0 }
455 static void init_submodule(const char *path
, const char *prefix
,
456 const char *super_prefix
,
459 const struct submodule
*sub
;
460 struct strbuf sb
= STRBUF_INIT
;
462 char *url
= NULL
, *displaypath
;
464 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
466 sub
= submodule_from_path(the_repository
, null_oid(), path
);
469 die(_("No url found for submodule path '%s' in .gitmodules"),
473 * NEEDSWORK: In a multi-working-tree world, this needs to be
474 * set in the per-worktree config.
476 * Set active flag for the submodule being initialized
478 if (!is_submodule_active(the_repository
, path
)) {
479 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
480 git_config_set_gently(sb
.buf
, "true");
485 * Copy url setting when it is not set yet.
486 * To look up the url in .git/config, we must not fall back to
487 * .gitmodules, so look it up directly.
489 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
490 if (git_config_get_string(sb
.buf
, &url
)) {
492 die(_("No url found for submodule path '%s' in .gitmodules"),
495 url
= xstrdup(sub
->url
);
497 /* Possibly a url relative to parent */
498 if (starts_with_dot_dot_slash(url
) ||
499 starts_with_dot_slash(url
)) {
502 url
= resolve_relative_url(oldurl
, NULL
, 0);
506 if (git_config_set_gently(sb
.buf
, url
))
507 die(_("Failed to register url for submodule path '%s'"),
509 if (!(flags
& OPT_QUIET
))
511 _("Submodule '%s' (%s) registered for path '%s'\n"),
512 sub
->name
, url
, displaypath
);
516 /* Copy "update" setting when it is not set yet */
517 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
518 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
519 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
520 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
521 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
525 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
528 if (git_config_set_gently(sb
.buf
, upd
))
529 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
536 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
538 struct init_cb
*info
= cb_data
;
540 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
544 static int module_init(int argc
, const char **argv
, const char *prefix
)
546 struct init_cb info
= INIT_CB_INIT
;
547 struct pathspec pathspec
= { 0 };
548 struct module_list list
= MODULE_LIST_INIT
;
550 struct option module_init_options
[] = {
551 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
554 const char *const git_submodule_helper_usage
[] = {
555 N_("git submodule init [<options>] [<path>]"),
560 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
561 git_submodule_helper_usage
, 0);
563 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
567 * If there are no path args and submodule.active is set then,
568 * by default, only initialize 'active' modules.
570 if (!argc
&& !git_config_get("submodule.active"))
571 module_list_active(&list
);
573 info
.prefix
= prefix
;
575 info
.flags
|= OPT_QUIET
;
577 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
581 module_list_release(&list
);
582 clear_pathspec(&pathspec
);
588 const char *super_prefix
;
591 #define STATUS_CB_INIT { 0 }
593 static void print_status(unsigned int flags
, char state
, const char *path
,
594 const struct object_id
*oid
, const char *displaypath
)
596 if (flags
& OPT_QUIET
)
599 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
601 if (state
== ' ' || state
== '+') {
602 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
605 printf(" (%s)", name
);
612 static int handle_submodule_head_ref(const char *refname UNUSED
,
613 const struct object_id
*oid
,
617 struct object_id
*output
= cb_data
;
625 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
626 unsigned int ce_flags
, const char *prefix
,
627 const char *super_prefix
, unsigned int flags
)
630 struct strvec diff_files_args
= STRVEC_INIT
;
631 struct rev_info rev
= REV_INFO_INIT
;
632 int diff_files_result
;
633 struct strbuf buf
= STRBUF_INIT
;
635 struct setup_revision_opt opt
= {
636 .free_removed_argv_elements
= 1,
639 if (!submodule_from_path(the_repository
, null_oid(), path
))
640 die(_("no submodule mapping found in .gitmodules for path '%s'"),
643 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
645 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
646 print_status(flags
, 'U', path
, null_oid(), displaypath
);
650 strbuf_addf(&buf
, "%s/.git", path
);
651 git_dir
= read_gitfile(buf
.buf
);
655 if (!is_submodule_active(the_repository
, path
) ||
656 !is_git_directory(git_dir
)) {
657 print_status(flags
, '-', path
, ce_oid
, displaypath
);
658 strbuf_release(&buf
);
661 strbuf_release(&buf
);
663 strvec_pushl(&diff_files_args
, "diff-files",
664 "--ignore-submodules=dirty", "--quiet", "--",
667 git_config(git_diff_basic_config
, NULL
);
669 repo_init_revisions(the_repository
, &rev
, NULL
);
671 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
672 diff_files_result
= run_diff_files(&rev
, 0);
674 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
675 print_status(flags
, ' ', path
, ce_oid
,
677 } else if (!(flags
& OPT_CACHED
)) {
678 struct object_id oid
;
679 struct ref_store
*refs
= get_submodule_ref_store(path
);
682 print_status(flags
, '-', path
, ce_oid
, displaypath
);
685 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
686 die(_("could not resolve HEAD ref inside the "
687 "submodule '%s'"), path
);
689 print_status(flags
, '+', path
, &oid
, displaypath
);
691 print_status(flags
, '+', path
, ce_oid
, displaypath
);
694 if (flags
& OPT_RECURSIVE
) {
695 struct child_process cpr
= CHILD_PROCESS_INIT
;
699 prepare_submodule_repo_env(&cpr
.env
);
701 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
702 "--recursive", NULL
);
703 strvec_push(&cpr
.args
, "--super-prefix");
704 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
706 if (flags
& OPT_CACHED
)
707 strvec_push(&cpr
.args
, "--cached");
709 if (flags
& OPT_QUIET
)
710 strvec_push(&cpr
.args
, "--quiet");
712 if (run_command(&cpr
))
713 die(_("failed to recurse into submodule '%s'"), path
);
717 strvec_clear(&diff_files_args
);
719 release_revisions(&rev
);
722 static void status_submodule_cb(const struct cache_entry
*list_item
,
725 struct status_cb
*info
= cb_data
;
727 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
728 info
->prefix
, info
->super_prefix
, info
->flags
);
731 static int module_status(int argc
, const char **argv
, const char *prefix
)
733 struct status_cb info
= STATUS_CB_INIT
;
734 struct pathspec pathspec
= { 0 };
735 struct module_list list
= MODULE_LIST_INIT
;
737 struct option module_status_options
[] = {
738 OPT__SUPER_PREFIX(&info
.super_prefix
),
739 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
740 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
741 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
744 const char *const git_submodule_helper_usage
[] = {
745 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
750 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
751 git_submodule_helper_usage
, 0);
753 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
756 info
.prefix
= prefix
;
758 info
.flags
|= OPT_QUIET
;
760 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
764 module_list_release(&list
);
765 clear_pathspec(&pathspec
);
770 unsigned int mod_src
;
771 unsigned int mod_dst
;
772 struct object_id oid_src
;
773 struct object_id oid_dst
;
777 #define MODULE_CB_INIT { 0 }
779 static void module_cb_release(struct module_cb
*mcb
)
784 struct module_cb_list
{
785 struct module_cb
**entries
;
788 #define MODULE_CB_LIST_INIT { 0 }
790 static void module_cb_list_release(struct module_cb_list
*mcbl
)
794 for (i
= 0; i
< mcbl
->nr
; i
++) {
795 struct module_cb
*mcb
= mcbl
->entries
[i
];
797 module_cb_release(mcb
);
807 const char *super_prefix
;
808 unsigned int cached
: 1;
809 unsigned int for_status
: 1;
810 unsigned int files
: 1;
813 #define SUMMARY_CB_INIT { 0 }
820 static char *verify_submodule_committish(const char *sm_path
,
821 const char *committish
)
823 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
824 struct strbuf result
= STRBUF_INIT
;
826 cp_rev_parse
.git_cmd
= 1;
827 cp_rev_parse
.dir
= sm_path
;
828 prepare_submodule_repo_env(&cp_rev_parse
.env
);
829 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
830 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
831 strvec_push(&cp_rev_parse
.args
, "--");
833 if (capture_command(&cp_rev_parse
, &result
, 0))
836 strbuf_trim_trailing_newline(&result
);
837 return strbuf_detach(&result
, NULL
);
840 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
841 int total_commits
, const char *displaypath
,
842 const char *src_abbrev
, const char *dst_abbrev
,
845 if (p
->status
== 'T') {
846 if (S_ISGITLINK(p
->mod_dst
))
847 printf(_("* %s %s(blob)->%s(submodule)"),
848 displaypath
, src_abbrev
, dst_abbrev
);
850 printf(_("* %s %s(submodule)->%s(blob)"),
851 displaypath
, src_abbrev
, dst_abbrev
);
853 printf("* %s %s...%s",
854 displaypath
, src_abbrev
, dst_abbrev
);
857 if (total_commits
< 0)
860 printf(" (%d):\n", total_commits
);
863 printf(_("%s"), errmsg
);
864 } else if (total_commits
> 0) {
865 struct child_process cp_log
= CHILD_PROCESS_INIT
;
868 cp_log
.dir
= p
->sm_path
;
869 prepare_submodule_repo_env(&cp_log
.env
);
870 strvec_pushl(&cp_log
.args
, "log", NULL
);
872 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
873 if (info
->summary_limit
> 0)
874 strvec_pushf(&cp_log
.args
, "-%d",
875 info
->summary_limit
);
877 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
878 "--first-parent", NULL
);
879 strvec_pushf(&cp_log
.args
, "%s...%s",
880 src_abbrev
, dst_abbrev
);
881 } else if (S_ISGITLINK(p
->mod_dst
)) {
882 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
883 "-1", dst_abbrev
, NULL
);
885 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
886 "-1", src_abbrev
, NULL
);
888 run_command(&cp_log
);
893 static void generate_submodule_summary(struct summary_cb
*info
,
896 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
897 int missing_src
= 0, missing_dst
= 0;
898 struct strbuf errmsg
= STRBUF_INIT
;
899 int total_commits
= -1;
901 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
902 if (S_ISGITLINK(p
->mod_dst
)) {
903 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
906 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
907 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
909 int fd
= open(p
->sm_path
, O_RDONLY
);
911 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
912 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
914 error(_("couldn't hash object from '%s'"), p
->sm_path
);
916 /* for a submodule removal (mode:0000000), don't warn */
918 warning(_("unexpected mode %o\n"), p
->mod_dst
);
922 if (S_ISGITLINK(p
->mod_src
)) {
923 if (p
->status
!= 'D')
924 src_abbrev
= verify_submodule_committish(p
->sm_path
,
925 oid_to_hex(&p
->oid_src
));
929 * As `rev-parse` failed, we fallback to getting
930 * the abbreviated hash using oid_src. We do
931 * this as we might still need the abbreviated
932 * hash in cases like a submodule type change, etc.
934 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
938 * The source does not point to a submodule.
939 * So, we fallback to getting the abbreviation using
940 * oid_src as we might still need the abbreviated
941 * hash in cases like submodule add, etc.
943 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
946 if (S_ISGITLINK(p
->mod_dst
)) {
947 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
948 oid_to_hex(&p
->oid_dst
));
952 * As `rev-parse` failed, we fallback to getting
953 * the abbreviated hash using oid_dst. We do
954 * this as we might still need the abbreviated
955 * hash in cases like a submodule type change, etc.
957 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
961 * The destination does not point to a submodule.
962 * So, we fallback to getting the abbreviation using
963 * oid_dst as we might still need the abbreviated
964 * hash in cases like a submodule removal, etc.
966 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
969 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
972 if (!missing_src
&& !missing_dst
) {
973 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
974 struct strbuf sb_rev_list
= STRBUF_INIT
;
976 strvec_pushl(&cp_rev_list
.args
, "rev-list",
977 "--first-parent", "--count", NULL
);
978 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
979 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
980 src_abbrev
, dst_abbrev
);
982 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
983 src_abbrev
: dst_abbrev
);
984 strvec_push(&cp_rev_list
.args
, "--");
986 cp_rev_list
.git_cmd
= 1;
987 cp_rev_list
.dir
= p
->sm_path
;
988 prepare_submodule_repo_env(&cp_rev_list
.env
);
990 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
991 total_commits
= atoi(sb_rev_list
.buf
);
993 strbuf_release(&sb_rev_list
);
996 * Don't give error msg for modification whose dst is not
997 * submodule, i.e., deleted or changed to blob
999 if (S_ISGITLINK(p
->mod_dst
)) {
1000 if (missing_src
&& missing_dst
) {
1001 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
1002 displaypath
, oid_to_hex(&p
->oid_src
),
1003 oid_to_hex(&p
->oid_dst
));
1005 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
1006 displaypath
, missing_src
?
1007 oid_to_hex(&p
->oid_src
) :
1008 oid_to_hex(&p
->oid_dst
));
1013 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1014 total_commits
, displaypath
, src_abbrev
,
1020 strbuf_release(&errmsg
);
1023 static void prepare_submodule_summary(struct summary_cb
*info
,
1024 struct module_cb_list
*list
)
1027 for (i
= 0; i
< list
->nr
; i
++) {
1028 const struct submodule
*sub
;
1029 struct module_cb
*p
= list
->entries
[i
];
1030 struct strbuf sm_gitdir
= STRBUF_INIT
;
1032 if (p
->status
== 'D' || p
->status
== 'T') {
1033 generate_submodule_summary(info
, p
);
1037 if (info
->for_status
&& p
->status
!= 'A' &&
1038 (sub
= submodule_from_path(the_repository
,
1039 null_oid(), p
->sm_path
))) {
1040 char *config_key
= NULL
;
1044 config_key
= xstrfmt("submodule.%s.ignore",
1046 if (!git_config_get_string_tmp(config_key
, &value
))
1047 ignore_all
= !strcmp(value
, "all");
1048 else if (sub
->ignore
)
1049 ignore_all
= !strcmp(sub
->ignore
, "all");
1056 /* Also show added or modified modules which are checked out */
1057 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1058 if (is_nonbare_repository_dir(&sm_gitdir
))
1059 generate_submodule_summary(info
, p
);
1060 strbuf_release(&sm_gitdir
);
1064 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1065 struct diff_options
*options UNUSED
,
1069 struct module_cb_list
*list
= data
;
1070 for (i
= 0; i
< q
->nr
; i
++) {
1071 struct diff_filepair
*p
= q
->queue
[i
];
1072 struct module_cb
*temp
;
1074 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1076 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1077 temp
->mod_src
= p
->one
->mode
;
1078 temp
->mod_dst
= p
->two
->mode
;
1079 temp
->oid_src
= p
->one
->oid
;
1080 temp
->oid_dst
= p
->two
->oid
;
1081 temp
->status
= p
->status
;
1082 temp
->sm_path
= xstrdup(p
->one
->path
);
1084 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1085 list
->entries
[list
->nr
++] = temp
;
1089 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1092 case DIFF_INDEX
: return "diff-index";
1093 case DIFF_FILES
: return "diff-files";
1094 default: BUG("bad diff_cmd value %d", diff_cmd
);
1098 static int compute_summary_module_list(struct object_id
*head_oid
,
1099 struct summary_cb
*info
,
1100 enum diff_cmd diff_cmd
)
1102 struct strvec diff_args
= STRVEC_INIT
;
1103 struct rev_info rev
;
1104 struct setup_revision_opt opt
= {
1105 .free_removed_argv_elements
= 1,
1107 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1110 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1112 strvec_push(&diff_args
, "--cached");
1113 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1115 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1116 strvec_push(&diff_args
, "--");
1118 strvec_pushv(&diff_args
, info
->argv
);
1120 git_config(git_diff_basic_config
, NULL
);
1121 repo_init_revisions(the_repository
, &rev
, info
->prefix
);
1123 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1124 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1125 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1126 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1127 rev
.diffopt
.format_callback_data
= &list
;
1129 if (!info
->cached
) {
1130 if (diff_cmd
== DIFF_INDEX
)
1132 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1133 perror("repo_read_index_preload");
1137 } else if (repo_read_index(the_repository
) < 0) {
1138 perror("repo_read_cache");
1143 if (diff_cmd
== DIFF_INDEX
)
1144 run_diff_index(&rev
, info
->cached
);
1146 run_diff_files(&rev
, 0);
1147 prepare_submodule_summary(info
, &list
);
1149 strvec_clear(&diff_args
);
1150 release_revisions(&rev
);
1151 module_cb_list_release(&list
);
1155 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1157 struct summary_cb info
= SUMMARY_CB_INIT
;
1161 int summary_limit
= -1;
1162 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1163 struct object_id head_oid
;
1165 struct option module_summary_options
[] = {
1166 OPT_BOOL(0, "cached", &cached
,
1167 N_("use the commit stored in the index instead of the submodule HEAD")),
1168 OPT_BOOL(0, "files", &files
,
1169 N_("compare the commit in the index with that in the submodule HEAD")),
1170 OPT_BOOL(0, "for-status", &for_status
,
1171 N_("skip submodules with 'ignore_config' value set to 'all'")),
1172 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1173 N_("limit the summary size")),
1176 const char *const git_submodule_helper_usage
[] = {
1177 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1181 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1182 git_submodule_helper_usage
, 0);
1187 if (!repo_get_oid(the_repository
, argc
? argv
[0] : "HEAD", &head_oid
)) {
1192 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1193 /* before the first commit: compare with an empty tree */
1194 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1200 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
1201 die(_("could not fetch a revision for HEAD"));
1206 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1207 diff_cmd
= DIFF_FILES
;
1212 info
.prefix
= prefix
;
1213 info
.cached
= !!cached
;
1214 info
.files
= !!files
;
1215 info
.for_status
= !!for_status
;
1216 info
.summary_limit
= summary_limit
;
1218 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1225 const char *super_prefix
;
1228 #define SYNC_CB_INIT { 0 }
1230 static void sync_submodule(const char *path
, const char *prefix
,
1231 const char *super_prefix
, unsigned int flags
)
1233 const struct submodule
*sub
;
1234 char *remote_key
= NULL
;
1235 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1236 struct strbuf sb
= STRBUF_INIT
;
1237 char *sub_config_path
= NULL
;
1240 if (!is_submodule_active(the_repository
, path
))
1243 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1245 if (sub
&& sub
->url
) {
1246 if (starts_with_dot_dot_slash(sub
->url
) ||
1247 starts_with_dot_slash(sub
->url
)) {
1248 char *up_path
= get_up_path(path
);
1250 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1251 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1254 sub_origin_url
= xstrdup(sub
->url
);
1255 super_config_url
= xstrdup(sub
->url
);
1258 sub_origin_url
= xstrdup("");
1259 super_config_url
= xstrdup("");
1262 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1264 if (!(flags
& OPT_QUIET
))
1265 printf(_("Synchronizing submodule url for '%s'\n"),
1269 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1270 if (git_config_set_gently(sb
.buf
, super_config_url
))
1271 die(_("failed to register url for submodule path '%s'"),
1274 if (!is_submodule_populated_gently(path
, NULL
))
1278 code
= get_default_remote_submodule(path
, &default_remote
);
1282 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1283 free(default_remote
);
1285 submodule_to_gitdir(&sb
, path
);
1286 strbuf_addstr(&sb
, "/config");
1288 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1289 die(_("failed to update remote for submodule '%s'"),
1292 if (flags
& OPT_RECURSIVE
) {
1293 struct child_process cpr
= CHILD_PROCESS_INIT
;
1297 prepare_submodule_repo_env(&cpr
.env
);
1299 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1300 "--recursive", NULL
);
1301 strvec_push(&cpr
.args
, "--super-prefix");
1302 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1305 if (flags
& OPT_QUIET
)
1306 strvec_push(&cpr
.args
, "--quiet");
1308 if (run_command(&cpr
))
1309 die(_("failed to recurse into submodule '%s'"),
1314 free(super_config_url
);
1315 free(sub_origin_url
);
1316 strbuf_release(&sb
);
1319 free(sub_config_path
);
1322 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1324 struct sync_cb
*info
= cb_data
;
1326 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1330 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1332 struct sync_cb info
= SYNC_CB_INIT
;
1333 struct pathspec pathspec
= { 0 };
1334 struct module_list list
= MODULE_LIST_INIT
;
1337 struct option module_sync_options
[] = {
1338 OPT__SUPER_PREFIX(&info
.super_prefix
),
1339 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1340 OPT_BOOL(0, "recursive", &recursive
,
1341 N_("recurse into nested submodules")),
1344 const char *const git_submodule_helper_usage
[] = {
1345 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1350 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1351 git_submodule_helper_usage
, 0);
1353 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1356 info
.prefix
= prefix
;
1358 info
.flags
|= OPT_QUIET
;
1360 info
.flags
|= OPT_RECURSIVE
;
1362 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1366 module_list_release(&list
);
1367 clear_pathspec(&pathspec
);
1375 #define DEINIT_CB_INIT { 0 }
1377 static void deinit_submodule(const char *path
, const char *prefix
,
1380 const struct submodule
*sub
;
1381 char *displaypath
= NULL
;
1382 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1383 struct strbuf sb_config
= STRBUF_INIT
;
1384 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1386 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1388 if (!sub
|| !sub
->name
)
1391 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1393 /* remove the submodule work tree (unless the user already did it) */
1394 if (is_directory(path
)) {
1395 struct strbuf sb_rm
= STRBUF_INIT
;
1398 if (is_directory(sub_git_dir
)) {
1399 if (!(flags
& OPT_QUIET
))
1400 warning(_("Submodule work tree '%s' contains a .git "
1401 "directory. This will be replaced with a "
1402 ".git file by using absorbgitdirs."),
1405 absorb_git_dir_into_superproject(path
, NULL
);
1409 if (!(flags
& OPT_FORCE
)) {
1410 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1413 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1416 if (run_command(&cp_rm
))
1417 die(_("Submodule work tree '%s' contains local "
1418 "modifications; use '-f' to discard them"),
1422 strbuf_addstr(&sb_rm
, path
);
1424 if (!remove_dir_recursively(&sb_rm
, 0))
1425 format
= _("Cleared directory '%s'\n");
1427 format
= _("Could not remove submodule work tree '%s'\n");
1429 if (!(flags
& OPT_QUIET
))
1430 printf(format
, displaypath
);
1432 submodule_unset_core_worktree(sub
);
1434 strbuf_release(&sb_rm
);
1437 if (mkdir(path
, 0777))
1438 printf(_("could not create empty submodule directory %s"),
1441 cp_config
.git_cmd
= 1;
1442 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1443 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1445 /* remove the .git/config entries (unless the user already did it) */
1446 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1447 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1450 * remove the whole section so we have a clean state when
1451 * the user later decides to init this submodule again
1453 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1454 if (!(flags
& OPT_QUIET
))
1455 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1456 sub
->name
, sub
->url
, displaypath
);
1463 strbuf_release(&sb_config
);
1466 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1469 struct deinit_cb
*info
= cb_data
;
1470 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1473 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1475 struct deinit_cb info
= DEINIT_CB_INIT
;
1476 struct pathspec pathspec
= { 0 };
1477 struct module_list list
= MODULE_LIST_INIT
;
1481 struct option module_deinit_options
[] = {
1482 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1483 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1484 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1487 const char *const git_submodule_helper_usage
[] = {
1488 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1493 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1494 git_submodule_helper_usage
, 0);
1497 error("pathspec and --all are incompatible");
1498 usage_with_options(git_submodule_helper_usage
,
1499 module_deinit_options
);
1503 die(_("Use '--all' if you really want to deinitialize all submodules"));
1505 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1508 info
.prefix
= prefix
;
1510 info
.flags
|= OPT_QUIET
;
1512 info
.flags
|= OPT_FORCE
;
1514 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1518 module_list_release(&list
);
1519 clear_pathspec(&pathspec
);
1523 struct module_clone_data
{
1529 struct list_objects_filter_options
*filter_options
;
1530 unsigned int quiet
: 1;
1531 unsigned int progress
: 1;
1532 unsigned int dissociate
: 1;
1533 unsigned int require_init
: 1;
1536 #define MODULE_CLONE_DATA_INIT { \
1537 .single_branch = -1, \
1540 struct submodule_alternate_setup
{
1541 const char *submodule_name
;
1542 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1543 SUBMODULE_ALTERNATE_ERROR_DIE
,
1544 SUBMODULE_ALTERNATE_ERROR_INFO
,
1545 SUBMODULE_ALTERNATE_ERROR_IGNORE
1547 struct string_list
*reference
;
1549 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1550 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1553 static const char alternate_error_advice
[] = N_(
1554 "An alternate computed from a superproject's alternate is invalid.\n"
1555 "To allow Git to clone without an alternate in such a case, set\n"
1556 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1557 "'--reference-if-able' instead of '--reference'."
1560 static int add_possible_reference_from_superproject(
1561 struct object_directory
*odb
, void *sas_cb
)
1563 struct submodule_alternate_setup
*sas
= sas_cb
;
1567 * If the alternate object store is another repository, try the
1568 * standard layout with .git/(modules/<name>)+/objects
1570 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1571 struct repository alternate
;
1573 struct strbuf sb
= STRBUF_INIT
;
1574 struct strbuf err
= STRBUF_INIT
;
1575 strbuf_add(&sb
, odb
->path
, len
);
1577 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1578 die(_("could not get a repository handle for gitdir '%s'"),
1582 * We need to end the new path with '/' to mark it as a dir,
1583 * otherwise a submodule name containing '/' will be broken
1584 * as the last part of a missing submodule reference would
1585 * be taken as a file name.
1588 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1589 strbuf_addch(&sb
, '/');
1590 repo_clear(&alternate
);
1592 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1594 char *p
= strbuf_detach(&sb
, NULL
);
1596 string_list_append(sas
->reference
, p
)->util
= p
;
1599 switch (sas
->error_mode
) {
1600 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1601 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1602 advise(_(alternate_error_advice
));
1603 die(_("submodule '%s' cannot add alternate: %s"),
1604 sas
->submodule_name
, err
.buf
);
1605 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1606 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1607 sas
->submodule_name
, err
.buf
);
1608 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1612 strbuf_release(&sb
);
1618 static void prepare_possible_alternates(const char *sm_name
,
1619 struct string_list
*reference
)
1621 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1622 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1624 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1628 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1630 if (!error_strategy
)
1631 error_strategy
= xstrdup("die");
1633 sas
.submodule_name
= sm_name
;
1634 sas
.reference
= reference
;
1635 if (!strcmp(error_strategy
, "die"))
1636 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1637 else if (!strcmp(error_strategy
, "info"))
1638 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1639 else if (!strcmp(error_strategy
, "ignore"))
1640 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1642 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1644 if (!strcmp(sm_alternate
, "superproject"))
1645 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1646 else if (!strcmp(sm_alternate
, "no"))
1649 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1652 free(error_strategy
);
1655 static char *clone_submodule_sm_gitdir(const char *name
)
1657 struct strbuf sb
= STRBUF_INIT
;
1660 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1661 sm_gitdir
= absolute_pathdup(sb
.buf
);
1662 strbuf_release(&sb
);
1667 static int clone_submodule(const struct module_clone_data
*clone_data
,
1668 struct string_list
*reference
)
1671 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1672 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1673 struct child_process cp
= CHILD_PROCESS_INIT
;
1674 const char *clone_data_path
= clone_data
->path
;
1675 char *to_free
= NULL
;
1677 if (!is_absolute_path(clone_data
->path
))
1678 clone_data_path
= to_free
= xstrfmt("%s/%s", get_git_work_tree(),
1681 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1682 die(_("refusing to create/use '%s' in another submodule's "
1683 "git dir"), sm_gitdir
);
1685 if (!file_exists(sm_gitdir
)) {
1686 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1687 die(_("could not create directory '%s'"), sm_gitdir
);
1689 prepare_possible_alternates(clone_data
->name
, reference
);
1691 strvec_push(&cp
.args
, "clone");
1692 strvec_push(&cp
.args
, "--no-checkout");
1693 if (clone_data
->quiet
)
1694 strvec_push(&cp
.args
, "--quiet");
1695 if (clone_data
->progress
)
1696 strvec_push(&cp
.args
, "--progress");
1697 if (clone_data
->depth
&& *(clone_data
->depth
))
1698 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1699 if (reference
->nr
) {
1700 struct string_list_item
*item
;
1702 for_each_string_list_item(item
, reference
)
1703 strvec_pushl(&cp
.args
, "--reference",
1704 item
->string
, NULL
);
1706 if (clone_data
->dissociate
)
1707 strvec_push(&cp
.args
, "--dissociate");
1708 if (sm_gitdir
&& *sm_gitdir
)
1709 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1710 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1711 strvec_pushf(&cp
.args
, "--filter=%s",
1712 expand_list_objects_filter_spec(
1713 clone_data
->filter_options
));
1714 if (clone_data
->single_branch
>= 0)
1715 strvec_push(&cp
.args
, clone_data
->single_branch
?
1717 "--no-single-branch");
1719 strvec_push(&cp
.args
, "--");
1720 strvec_push(&cp
.args
, clone_data
->url
);
1721 strvec_push(&cp
.args
, clone_data_path
);
1724 prepare_submodule_repo_env(&cp
.env
);
1727 if(run_command(&cp
))
1728 die(_("clone of '%s' into submodule path '%s' failed"),
1729 clone_data
->url
, clone_data_path
);
1733 if (clone_data
->require_init
&& !access(clone_data_path
, X_OK
) &&
1734 !is_empty_dir(clone_data_path
))
1735 die(_("directory not empty: '%s'"), clone_data_path
);
1736 if (safe_create_leading_directories_const(clone_data_path
) < 0)
1737 die(_("could not create directory '%s'"), clone_data_path
);
1738 path
= xstrfmt("%s/index", sm_gitdir
);
1739 unlink_or_warn(path
);
1743 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1745 p
= git_pathdup_submodule(clone_data_path
, "config");
1747 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1749 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1750 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1752 git_config_set_in_file(p
, "submodule.alternateLocation",
1754 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1756 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1760 free(error_strategy
);
1768 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1770 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1771 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1772 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1773 struct list_objects_filter_options filter_options
=
1774 LIST_OBJECTS_FILTER_INIT
;
1776 struct option module_clone_options
[] = {
1777 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1779 N_("alternative anchor for relative paths")),
1780 OPT_STRING(0, "path", &clone_data
.path
,
1782 N_("where the new submodule will be cloned to")),
1783 OPT_STRING(0, "name", &clone_data
.name
,
1785 N_("name of the new submodule")),
1786 OPT_STRING(0, "url", &clone_data
.url
,
1788 N_("url where to clone the submodule from")),
1789 OPT_STRING_LIST(0, "reference", &reference
,
1791 N_("reference repository")),
1792 OPT_BOOL(0, "dissociate", &dissociate
,
1793 N_("use --reference only while cloning")),
1794 OPT_STRING(0, "depth", &clone_data
.depth
,
1796 N_("depth for shallow clones")),
1797 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1798 OPT_BOOL(0, "progress", &progress
,
1799 N_("force cloning progress")),
1800 OPT_BOOL(0, "require-init", &require_init
,
1801 N_("disallow cloning into non-empty directory")),
1802 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1803 N_("clone only one branch, HEAD or --branch")),
1804 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1807 const char *const git_submodule_helper_usage
[] = {
1808 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1809 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1810 "[--single-branch] [--filter <filter-spec>] "
1811 "--url <url> --path <path>"),
1815 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1816 git_submodule_helper_usage
, 0);
1818 clone_data
.dissociate
= !!dissociate
;
1819 clone_data
.quiet
= !!quiet
;
1820 clone_data
.progress
= !!progress
;
1821 clone_data
.require_init
= !!require_init
;
1822 clone_data
.filter_options
= &filter_options
;
1824 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1825 usage_with_options(git_submodule_helper_usage
,
1826 module_clone_options
);
1828 clone_submodule(&clone_data
, &reference
);
1829 list_objects_filter_release(&filter_options
);
1830 string_list_clear(&reference
, 1);
1834 static int determine_submodule_update_strategy(struct repository
*r
,
1837 enum submodule_update_type update
,
1838 struct submodule_update_strategy
*out
)
1840 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1845 key
= xstrfmt("submodule.%s.update", sub
->name
);
1849 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1850 if (parse_submodule_update_strategy(val
, out
) < 0) {
1851 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1855 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1856 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1857 BUG("how did we read update = !command from .gitmodules?");
1858 out
->type
= sub
->update_strategy
.type
;
1859 out
->command
= sub
->update_strategy
.command
;
1861 out
->type
= SM_UPDATE_CHECKOUT
;
1864 (out
->type
== SM_UPDATE_MERGE
||
1865 out
->type
== SM_UPDATE_REBASE
||
1866 out
->type
== SM_UPDATE_NONE
))
1867 out
->type
= SM_UPDATE_CHECKOUT
;
1875 struct update_clone_data
{
1876 const struct submodule
*sub
;
1877 struct object_id oid
;
1878 unsigned just_cloned
;
1881 struct submodule_update_clone
{
1882 /* index into 'update_data.list', the list of submodules to look into for cloning */
1885 /* configuration parameters which are passed on to the children */
1886 const struct update_data
*update_data
;
1888 /* to be consumed by update_submodule() */
1889 struct update_clone_data
*update_clone
;
1890 int update_clone_nr
; int update_clone_alloc
;
1892 /* If we want to stop as fast as possible and return an error */
1893 unsigned quickstop
: 1;
1895 /* failed clones to be retried again */
1896 const struct cache_entry
**failed_clones
;
1897 int failed_clones_nr
, failed_clones_alloc
;
1899 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1901 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1903 free(suc
->update_clone
);
1904 free(suc
->failed_clones
);
1907 struct update_data
{
1909 const char *super_prefix
;
1911 enum submodule_update_type update_default
;
1912 struct object_id suboid
;
1913 struct string_list references
;
1914 struct submodule_update_strategy update_strategy
;
1915 struct list_objects_filter_options
*filter_options
;
1916 struct module_list list
;
1920 int recommend_shallow
;
1921 unsigned int require_init
;
1924 unsigned int nofetch
;
1925 unsigned int remote
;
1926 unsigned int progress
;
1927 unsigned int dissociate
;
1929 unsigned int warn_if_uninitialized
;
1930 unsigned int recursive
;
1932 /* copied over from update_clone_data */
1933 struct object_id oid
;
1934 unsigned int just_cloned
;
1935 const char *sm_path
;
1937 #define UPDATE_DATA_INIT { \
1938 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1939 .list = MODULE_LIST_INIT, \
1940 .recommend_shallow = -1, \
1941 .references = STRING_LIST_INIT_DUP, \
1942 .single_branch = -1, \
1946 static void update_data_release(struct update_data
*ud
)
1948 free(ud
->displaypath
);
1949 module_list_release(&ud
->list
);
1952 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1953 struct strbuf
*out
, const char *displaypath
)
1956 * Only mention uninitialized submodules when their
1957 * paths have been specified.
1959 if (suc
->update_data
->warn_if_uninitialized
) {
1961 _("Submodule path '%s' not initialized"),
1963 strbuf_addch(out
, '\n');
1965 _("Maybe you want to use 'update --init'?"));
1966 strbuf_addch(out
, '\n');
1971 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1972 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1974 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1975 struct child_process
*child
,
1976 struct submodule_update_clone
*suc
,
1979 const struct submodule
*sub
= NULL
;
1980 const char *url
= NULL
;
1981 const char *update_string
;
1982 enum submodule_update_type update_type
;
1984 const struct update_data
*ud
= suc
->update_data
;
1985 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
1987 struct strbuf sb
= STRBUF_INIT
;
1988 int needs_cloning
= 0;
1989 int need_free_url
= 0;
1992 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
1993 strbuf_addch(out
, '\n');
1997 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
2000 next_submodule_warn_missing(suc
, out
, displaypath
);
2004 key
= xstrfmt("submodule.%s.update", sub
->name
);
2005 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2006 update_type
= parse_submodule_update_type(update_string
);
2008 update_type
= sub
->update_strategy
.type
;
2012 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2013 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2014 && update_type
== SM_UPDATE_NONE
)) {
2015 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2016 strbuf_addch(out
, '\n');
2020 /* Check if the submodule has been initialized. */
2021 if (!is_submodule_active(the_repository
, ce
->name
)) {
2022 next_submodule_warn_missing(suc
, out
, displaypath
);
2027 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2028 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2029 if (sub
->url
&& (starts_with_dot_slash(sub
->url
) ||
2030 starts_with_dot_dot_slash(sub
->url
))) {
2031 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2038 die(_("cannot clone submodule '%s' without a URL"), sub
->name
);
2041 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2042 needs_cloning
= !file_exists(sb
.buf
);
2044 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2045 suc
->update_clone_alloc
);
2046 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2047 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2048 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2049 suc
->update_clone_nr
++;
2055 child
->no_stdin
= 1;
2056 child
->stdout_to_stderr
= 1;
2058 strvec_push(&child
->args
, "submodule--helper");
2059 strvec_push(&child
->args
, "clone");
2060 if (suc
->update_data
->progress
)
2061 strvec_push(&child
->args
, "--progress");
2062 if (suc
->update_data
->quiet
)
2063 strvec_push(&child
->args
, "--quiet");
2064 if (suc
->update_data
->prefix
)
2065 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2066 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2067 strvec_push(&child
->args
, "--depth=1");
2068 else if (suc
->update_data
->depth
)
2069 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2070 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2071 strvec_pushf(&child
->args
, "--filter=%s",
2072 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2073 if (suc
->update_data
->require_init
)
2074 strvec_push(&child
->args
, "--require-init");
2075 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2076 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2077 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2078 if (suc
->update_data
->references
.nr
) {
2079 struct string_list_item
*item
;
2081 for_each_string_list_item(item
, &suc
->update_data
->references
)
2082 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2084 if (suc
->update_data
->dissociate
)
2085 strvec_push(&child
->args
, "--dissociate");
2086 if (suc
->update_data
->single_branch
>= 0)
2087 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2089 "--no-single-branch");
2093 strbuf_release(&sb
);
2097 return needs_cloning
;
2100 static int update_clone_get_next_task(struct child_process
*child
,
2105 struct submodule_update_clone
*suc
= suc_cb
;
2106 const struct cache_entry
*ce
;
2109 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2110 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2111 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2112 int *p
= xmalloc(sizeof(*p
));
2122 * The loop above tried cloning each submodule once, now try the
2123 * stragglers again, which we can imagine as an extension of the
2126 index
= suc
->current
- suc
->update_data
->list
.nr
;
2127 if (index
< suc
->failed_clones_nr
) {
2130 ce
= suc
->failed_clones
[index
];
2131 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2133 strbuf_addstr(err
, "BUG: submodule considered for "
2134 "cloning, doesn't need cloning "
2138 p
= xmalloc(sizeof(*p
));
2148 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2150 void *idx_task_cb UNUSED
)
2152 struct submodule_update_clone
*suc
= suc_cb
;
2158 static int update_clone_task_finished(int result
,
2163 const struct cache_entry
*ce
;
2164 struct submodule_update_clone
*suc
= suc_cb
;
2165 int *idxP
= idx_task_cb
;
2173 if (idx
< suc
->update_data
->list
.nr
) {
2174 ce
= suc
->update_data
->list
.entries
[idx
];
2175 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2177 strbuf_addch(err
, '\n');
2178 ALLOC_GROW(suc
->failed_clones
,
2179 suc
->failed_clones_nr
+ 1,
2180 suc
->failed_clones_alloc
);
2181 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2184 idx
-= suc
->update_data
->list
.nr
;
2185 ce
= suc
->failed_clones
[idx
];
2186 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2188 strbuf_addch(err
, '\n');
2196 static int git_update_clone_config(const char *var
, const char *value
,
2201 if (!strcmp(var
, "submodule.fetchjobs"))
2202 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2206 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2208 struct child_process cp
= CHILD_PROCESS_INIT
;
2209 struct strbuf rev
= STRBUF_INIT
;
2210 char *hex
= oid_to_hex(oid
);
2215 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2217 prepare_submodule_repo_env(&cp
.env
);
2219 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2225 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2226 const struct object_id
*oid
)
2228 struct child_process cp
= CHILD_PROCESS_INIT
;
2230 prepare_submodule_repo_env(&cp
.env
);
2232 cp
.dir
= module_path
;
2234 strvec_push(&cp
.args
, "fetch");
2236 strvec_push(&cp
.args
, "--quiet");
2238 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2240 char *hex
= oid_to_hex(oid
);
2241 char *remote
= get_default_remote();
2243 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2247 return run_command(&cp
);
2250 static int run_update_command(const struct update_data
*ud
, int subforce
)
2252 struct child_process cp
= CHILD_PROCESS_INIT
;
2253 char *oid
= oid_to_hex(&ud
->oid
);
2256 switch (ud
->update_strategy
.type
) {
2257 case SM_UPDATE_CHECKOUT
:
2259 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2261 strvec_push(&cp
.args
, "-f");
2263 case SM_UPDATE_REBASE
:
2265 strvec_push(&cp
.args
, "rebase");
2267 strvec_push(&cp
.args
, "--quiet");
2269 case SM_UPDATE_MERGE
:
2271 strvec_push(&cp
.args
, "merge");
2273 strvec_push(&cp
.args
, "--quiet");
2275 case SM_UPDATE_COMMAND
:
2277 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2280 BUG("unexpected update strategy type: %d",
2281 ud
->update_strategy
.type
);
2283 strvec_push(&cp
.args
, oid
);
2285 cp
.dir
= ud
->sm_path
;
2286 prepare_submodule_repo_env(&cp
.env
);
2287 if ((ret
= run_command(&cp
))) {
2288 switch (ud
->update_strategy
.type
) {
2289 case SM_UPDATE_CHECKOUT
:
2290 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2291 oid
, ud
->displaypath
);
2292 /* No "ret" assignment, use "git checkout"'s */
2294 case SM_UPDATE_REBASE
:
2295 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2296 oid
, ud
->displaypath
);
2298 case SM_UPDATE_MERGE
:
2299 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2300 oid
, ud
->displaypath
);
2302 case SM_UPDATE_COMMAND
:
2303 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2304 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2307 BUG("unexpected update strategy type: %d",
2308 ud
->update_strategy
.type
);
2317 switch (ud
->update_strategy
.type
) {
2318 case SM_UPDATE_CHECKOUT
:
2319 printf(_("Submodule path '%s': checked out '%s'\n"),
2320 ud
->displaypath
, oid
);
2322 case SM_UPDATE_REBASE
:
2323 printf(_("Submodule path '%s': rebased into '%s'\n"),
2324 ud
->displaypath
, oid
);
2326 case SM_UPDATE_MERGE
:
2327 printf(_("Submodule path '%s': merged in '%s'\n"),
2328 ud
->displaypath
, oid
);
2330 case SM_UPDATE_COMMAND
:
2331 printf(_("Submodule path '%s': '%s %s'\n"),
2332 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2335 BUG("unexpected update strategy type: %d",
2336 ud
->update_strategy
.type
);
2342 static int run_update_procedure(const struct update_data
*ud
)
2344 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2348 * Run fetch only if `oid` isn't present or it
2349 * is not reachable from a ref.
2351 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2352 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2355 _("Unable to fetch in submodule path '%s'; "
2356 "trying to directly fetch %s:"),
2357 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2359 * Now we tried the usual fetch, but `oid` may
2360 * not be reachable from any of the refs.
2362 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2363 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2364 return die_message(_("Fetched in submodule path '%s', but it did not "
2365 "contain %s. Direct fetching of that commit failed."),
2366 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2369 return run_update_command(ud
, subforce
);
2372 static int remote_submodule_branch(const char *path
, const char **branch
)
2374 const struct submodule
*sub
;
2378 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2380 return die_message(_("could not initialize submodule at path '%s'"),
2383 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2384 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2385 *branch
= sub
->branch
;
2393 if (!strcmp(*branch
, ".")) {
2394 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2397 return die_message(_("No such ref: %s"), "HEAD");
2400 if (!strcmp(refname
, "HEAD"))
2401 return die_message(_("Submodule (%s) branch configured to inherit "
2402 "branch from superproject, but the superproject "
2403 "is not on any branch"), sub
->name
);
2405 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2406 return die_message(_("Expecting a full ref name, got %s"),
2413 /* Our "branch" is coming from repo_config_get_string_tmp() */
2417 static int ensure_core_worktree(const char *path
)
2420 struct repository subrepo
;
2422 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2423 return die_message(_("could not get a repository handle for submodule '%s'"),
2426 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2427 char *cfg_file
, *abs_path
;
2428 const char *rel_path
;
2429 struct strbuf sb
= STRBUF_INIT
;
2431 cfg_file
= repo_git_path(&subrepo
, "config");
2433 abs_path
= absolute_pathdup(path
);
2434 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2436 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2440 strbuf_release(&sb
);
2443 repo_clear(&subrepo
);
2447 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2450 case SM_UPDATE_CHECKOUT
:
2452 case SM_UPDATE_MERGE
:
2454 case SM_UPDATE_REBASE
:
2456 case SM_UPDATE_UNSPECIFIED
:
2457 case SM_UPDATE_NONE
:
2458 case SM_UPDATE_COMMAND
:
2461 BUG("unreachable with type %d", type
);
2464 static void update_data_to_args(const struct update_data
*update_data
,
2465 struct strvec
*args
)
2467 enum submodule_update_type update_type
= update_data
->update_default
;
2469 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2470 if (update_data
->displaypath
) {
2471 strvec_push(args
, "--super-prefix");
2472 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2474 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2475 if (update_data
->quiet
)
2476 strvec_push(args
, "--quiet");
2477 if (update_data
->force
)
2478 strvec_push(args
, "--force");
2479 if (update_data
->init
)
2480 strvec_push(args
, "--init");
2481 if (update_data
->remote
)
2482 strvec_push(args
, "--remote");
2483 if (update_data
->nofetch
)
2484 strvec_push(args
, "--no-fetch");
2485 if (update_data
->dissociate
)
2486 strvec_push(args
, "--dissociate");
2487 if (update_data
->progress
)
2488 strvec_push(args
, "--progress");
2489 if (update_data
->require_init
)
2490 strvec_push(args
, "--require-init");
2491 if (update_data
->depth
)
2492 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2493 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2494 strvec_pushf(args
, "--%s",
2495 submodule_update_type_to_label(update_type
));
2497 if (update_data
->references
.nr
) {
2498 struct string_list_item
*item
;
2500 for_each_string_list_item(item
, &update_data
->references
)
2501 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2503 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2504 strvec_pushf(args
, "--filter=%s",
2505 expand_list_objects_filter_spec(
2506 update_data
->filter_options
));
2507 if (update_data
->recommend_shallow
== 0)
2508 strvec_push(args
, "--no-recommend-shallow");
2509 else if (update_data
->recommend_shallow
== 1)
2510 strvec_push(args
, "--recommend-shallow");
2511 if (update_data
->single_branch
>= 0)
2512 strvec_push(args
, update_data
->single_branch
?
2514 "--no-single-branch");
2517 static int update_submodule(struct update_data
*update_data
)
2521 ret
= determine_submodule_update_strategy(the_repository
,
2522 update_data
->just_cloned
,
2523 update_data
->sm_path
,
2524 update_data
->update_default
,
2525 &update_data
->update_strategy
);
2529 if (update_data
->just_cloned
)
2530 oidcpy(&update_data
->suboid
, null_oid());
2531 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2532 return die_message(_("Unable to find current revision in submodule path '%s'"),
2533 update_data
->displaypath
);
2535 if (update_data
->remote
) {
2541 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2544 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2547 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2551 if (!update_data
->nofetch
) {
2552 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2554 return die_message(_("Unable to fetch in submodule path '%s'"),
2555 update_data
->sm_path
);
2558 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2559 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2560 remote_ref
, update_data
->sm_path
);
2565 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2566 ret
= run_update_procedure(update_data
);
2571 if (update_data
->recursive
) {
2572 struct child_process cp
= CHILD_PROCESS_INIT
;
2573 struct update_data next
= *update_data
;
2576 oidcpy(&next
.oid
, null_oid());
2577 oidcpy(&next
.suboid
, null_oid());
2579 cp
.dir
= update_data
->sm_path
;
2581 prepare_submodule_repo_env(&cp
.env
);
2582 update_data_to_args(&next
, &cp
.args
);
2584 ret
= run_command(&cp
);
2586 die_message(_("Failed to recurse into submodule path '%s'"),
2587 update_data
->displaypath
);
2594 static int update_submodules(struct update_data
*update_data
)
2597 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2598 const struct run_process_parallel_opts opts
= {
2599 .tr2_category
= "submodule",
2600 .tr2_label
= "parallel/update",
2602 .processes
= update_data
->max_jobs
,
2604 .get_next_task
= update_clone_get_next_task
,
2605 .start_failure
= update_clone_start_failure
,
2606 .task_finished
= update_clone_task_finished
,
2610 suc
.update_data
= update_data
;
2611 run_processes_parallel(&opts
);
2614 * We saved the output and put it out all at once now.
2616 * - the listener does not have to interleave their (checkout)
2617 * work with our fetching. The writes involved in a
2618 * checkout involve more straightforward sequential I/O.
2619 * - the listener can avoid doing any work if fetching failed.
2621 if (suc
.quickstop
) {
2626 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2627 struct update_clone_data ucd
= suc
.update_clone
[i
];
2630 oidcpy(&update_data
->oid
, &ucd
.oid
);
2631 update_data
->just_cloned
= ucd
.just_cloned
;
2632 update_data
->sm_path
= ucd
.sub
->path
;
2634 code
= ensure_core_worktree(update_data
->sm_path
);
2638 update_data
->displaypath
= get_submodule_displaypath(
2639 update_data
->sm_path
, update_data
->prefix
,
2640 update_data
->super_prefix
);
2641 code
= update_submodule(update_data
);
2642 FREE_AND_NULL(update_data
->displaypath
);
2652 submodule_update_clone_release(&suc
);
2653 string_list_clear(&update_data
->references
, 0);
2657 static int module_update(int argc
, const char **argv
, const char *prefix
)
2659 struct pathspec pathspec
= { 0 };
2660 struct pathspec pathspec2
= { 0 };
2661 struct update_data opt
= UPDATE_DATA_INIT
;
2662 struct list_objects_filter_options filter_options
=
2663 LIST_OBJECTS_FILTER_INIT
;
2665 struct option module_update_options
[] = {
2666 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2667 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2668 OPT_BOOL(0, "init", &opt
.init
,
2669 N_("initialize uninitialized submodules before update")),
2670 OPT_BOOL(0, "remote", &opt
.remote
,
2671 N_("use SHA-1 of submodule's remote tracking branch")),
2672 OPT_BOOL(0, "recursive", &opt
.recursive
,
2673 N_("traverse submodules recursively")),
2674 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2675 N_("don't fetch new objects from the remote site")),
2676 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2677 N_("use the 'checkout' update strategy (default)"),
2678 SM_UPDATE_CHECKOUT
),
2679 OPT_SET_INT('m', "merge", &opt
.update_default
,
2680 N_("use the 'merge' update strategy"),
2682 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2683 N_("use the 'rebase' update strategy"),
2685 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2686 N_("reference repository")),
2687 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2688 N_("use --reference only while cloning")),
2689 OPT_INTEGER(0, "depth", &opt
.depth
,
2690 N_("create a shallow clone truncated to the "
2691 "specified number of revisions")),
2692 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2693 N_("parallel jobs")),
2694 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2695 N_("whether the initial clone should follow the shallow recommendation")),
2696 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2697 OPT_BOOL(0, "progress", &opt
.progress
,
2698 N_("force cloning progress")),
2699 OPT_BOOL(0, "require-init", &opt
.require_init
,
2700 N_("disallow cloning into non-empty directory, implies --init")),
2701 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2702 N_("clone only one branch, HEAD or --branch")),
2703 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2706 const char *const git_submodule_helper_usage
[] = {
2707 N_("git submodule [--quiet] update"
2708 " [--init [--filter=<filter-spec>]] [--remote]"
2709 " [-N|--no-fetch] [-f|--force]"
2710 " [--checkout|--merge|--rebase]"
2711 " [--[no-]recommend-shallow] [--reference <repository>]"
2712 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2716 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2717 git_config(git_update_clone_config
, &opt
.max_jobs
);
2719 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2720 git_submodule_helper_usage
, 0);
2722 if (opt
.require_init
)
2725 if (filter_options
.choice
&& !opt
.init
) {
2726 usage_with_options(git_submodule_helper_usage
,
2727 module_update_options
);
2730 opt
.filter_options
= &filter_options
;
2731 opt
.prefix
= prefix
;
2733 if (opt
.update_default
)
2734 opt
.update_strategy
.type
= opt
.update_default
;
2736 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2742 opt
.warn_if_uninitialized
= 1;
2745 struct module_list list
= MODULE_LIST_INIT
;
2746 struct init_cb info
= INIT_CB_INIT
;
2748 if (module_list_compute(argv
, opt
.prefix
,
2749 &pathspec2
, &list
) < 0) {
2750 module_list_release(&list
);
2756 * If there are no path args and submodule.active is set then,
2757 * by default, only initialize 'active' modules.
2759 if (!argc
&& !git_config_get("submodule.active"))
2760 module_list_active(&list
);
2762 info
.prefix
= opt
.prefix
;
2763 info
.super_prefix
= opt
.super_prefix
;
2765 info
.flags
|= OPT_QUIET
;
2767 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2768 module_list_release(&list
);
2771 ret
= update_submodules(&opt
);
2773 update_data_release(&opt
);
2774 list_objects_filter_release(&filter_options
);
2775 clear_pathspec(&pathspec
);
2776 clear_pathspec(&pathspec2
);
2780 static int push_check(int argc
, const char **argv
, const char *prefix UNUSED
)
2782 struct remote
*remote
;
2783 const char *superproject_head
;
2785 int detached_head
= 0;
2786 struct object_id head_oid
;
2789 die("submodule--helper push-check requires at least 2 arguments");
2792 * superproject's resolved head ref.
2793 * if HEAD then the superproject is in a detached head state, otherwise
2794 * it will be the resolved head ref.
2796 superproject_head
= argv
[1];
2799 /* Get the submodule's head ref and determine if it is detached */
2800 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2802 die(_("Failed to resolve HEAD as a valid ref."));
2803 if (!strcmp(head
, "HEAD"))
2807 * The remote must be configured.
2808 * This is to avoid pushing to the exact same URL as the parent.
2810 remote
= pushremote_get(argv
[1]);
2811 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2812 die("remote '%s' not configured", argv
[1]);
2814 /* Check the refspec */
2817 struct ref
*local_refs
= get_local_heads();
2818 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2820 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2822 for (i
= 0; i
< refspec
.nr
; i
++) {
2823 const struct refspec_item
*rs
= &refspec
.items
[i
];
2825 if (rs
->pattern
|| rs
->matching
)
2828 /* LHS must match a single ref */
2829 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2834 * If LHS matches 'HEAD' then we need to ensure
2835 * that it matches the same named branch
2836 * checked out in the superproject.
2838 if (!strcmp(rs
->src
, "HEAD")) {
2839 if (!detached_head
&&
2840 !strcmp(head
, superproject_head
))
2842 die("HEAD does not match the named branch in the superproject");
2846 die("src refspec '%s' must name a ref",
2850 refspec_clear(&refspec
);
2857 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2860 struct pathspec pathspec
= { 0 };
2861 struct module_list list
= MODULE_LIST_INIT
;
2862 const char *super_prefix
= NULL
;
2863 struct option embed_gitdir_options
[] = {
2864 OPT__SUPER_PREFIX(&super_prefix
),
2867 const char *const git_submodule_helper_usage
[] = {
2868 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2873 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2874 git_submodule_helper_usage
, 0);
2876 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
2879 for (i
= 0; i
< list
.nr
; i
++)
2880 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
2885 clear_pathspec(&pathspec
);
2886 module_list_release(&list
);
2890 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2896 struct option options
[] = {
2897 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2900 const char *const usage
[] = {
2901 N_("git submodule set-url [--quiet] <path> <newurl>"),
2905 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2907 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2908 usage_with_options(usage
, options
);
2910 config_name
= xstrfmt("submodule.%s.url", path
);
2912 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2913 sync_submodule(path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
2920 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2922 int opt_default
= 0, ret
;
2923 const char *opt_branch
= NULL
;
2926 struct option options
[] = {
2928 * We accept the `quiet` option for uniformity across subcommands,
2929 * though there is nothing to make less verbose in this subcommand.
2931 OPT_NOOP_NOARG('q', "quiet"),
2933 OPT_BOOL('d', "default", &opt_default
,
2934 N_("set the default tracking branch to master")),
2935 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2936 N_("set the default tracking branch")),
2939 const char *const usage
[] = {
2940 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2941 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2945 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2947 if (!opt_branch
&& !opt_default
)
2948 die(_("--branch or --default required"));
2950 if (opt_branch
&& opt_default
)
2951 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2953 if (argc
!= 1 || !(path
= argv
[0]))
2954 usage_with_options(usage
, options
);
2956 config_name
= xstrfmt("submodule.%s.branch", path
);
2957 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2963 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
2965 enum branch_track track
;
2966 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
2967 struct option options
[] = {
2968 OPT__QUIET(&quiet
, N_("print only error messages")),
2969 OPT__FORCE(&force
, N_("force creation"), 0),
2970 OPT_BOOL(0, "create-reflog", &reflog
,
2971 N_("create the branch's reflog")),
2972 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
2973 N_("set branch tracking configuration"),
2975 parse_opt_tracking_mode
),
2976 OPT__DRY_RUN(&dry_run
,
2977 N_("show whether the branch would be created")),
2980 const char *const usage
[] = {
2981 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2985 git_config(git_default_config
, NULL
);
2986 track
= git_branch_track
;
2987 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2990 usage_with_options(usage
, options
);
2992 if (!quiet
&& !dry_run
)
2993 printf_ln(_("creating branch '%s'"), argv
[0]);
2995 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
2996 force
, reflog
, quiet
, track
, dry_run
);
3003 const char *reference_path
;
3005 const char *sm_name
;
3007 const char *realrepo
;
3009 unsigned int force
: 1;
3010 unsigned int quiet
: 1;
3011 unsigned int progress
: 1;
3012 unsigned int dissociate
: 1;
3014 #define ADD_DATA_INIT { .depth = -1 }
3016 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3018 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3019 struct strbuf sb_remote_out
= STRBUF_INIT
;
3021 cp_remote
.git_cmd
= 1;
3022 strvec_pushf(&cp_remote
.env
,
3023 "GIT_DIR=%s", git_dir_path
);
3024 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3025 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3026 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3028 char *line
= sb_remote_out
.buf
;
3030 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3031 size_t len
= next_line
- line
;
3033 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3034 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3035 line
= next_line
+ 1;
3039 strbuf_release(&sb_remote_out
);
3042 static int add_submodule(const struct add_data
*add_data
)
3044 char *submod_gitdir_path
;
3045 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3046 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3049 /* perhaps the path already exists and is already a git repo, else clone it */
3050 if (is_directory(add_data
->sm_path
)) {
3051 struct strbuf sm_path
= STRBUF_INIT
;
3052 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3053 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3054 if (is_nonbare_repository_dir(&sm_path
))
3055 printf(_("Adding existing repo at '%s' to the index\n"),
3058 die(_("'%s' already exists and is not a valid git repo"),
3060 strbuf_release(&sm_path
);
3061 free(submod_gitdir_path
);
3063 struct child_process cp
= CHILD_PROCESS_INIT
;
3065 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3067 if (is_directory(submod_gitdir_path
)) {
3068 if (!add_data
->force
) {
3069 struct strbuf msg
= STRBUF_INIT
;
3072 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3073 "locally with remote(s):\n"),
3076 append_fetch_remotes(&msg
, submod_gitdir_path
);
3077 free(submod_gitdir_path
);
3079 strbuf_addf(&msg
, _("If you want to reuse this local git "
3080 "directory instead of cloning again from\n"
3082 "use the '--force' option. If the local git "
3083 "directory is not the correct repo\n"
3084 "or you are unsure what this means choose "
3085 "another name with the '--name' option."),
3086 add_data
->realrepo
);
3088 die_msg
= strbuf_detach(&msg
, NULL
);
3091 printf(_("Reactivating local git directory for "
3092 "submodule '%s'\n"), add_data
->sm_name
);
3095 free(submod_gitdir_path
);
3097 clone_data
.prefix
= add_data
->prefix
;
3098 clone_data
.path
= add_data
->sm_path
;
3099 clone_data
.name
= add_data
->sm_name
;
3100 clone_data
.url
= add_data
->realrepo
;
3101 clone_data
.quiet
= add_data
->quiet
;
3102 clone_data
.progress
= add_data
->progress
;
3103 if (add_data
->reference_path
) {
3104 char *p
= xstrdup(add_data
->reference_path
);
3106 string_list_append(&reference
, p
)->util
= p
;
3108 clone_data
.dissociate
= add_data
->dissociate
;
3109 if (add_data
->depth
>= 0)
3110 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3112 if (clone_submodule(&clone_data
, &reference
))
3115 prepare_submodule_repo_env(&cp
.env
);
3117 cp
.dir
= add_data
->sm_path
;
3119 * NOTE: we only get here if add_data->force is true, so
3120 * passing --force to checkout is reasonable.
3122 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3124 if (add_data
->branch
) {
3125 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3126 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3129 if (run_command(&cp
))
3130 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3134 string_list_clear(&reference
, 1);
3138 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3143 if (!is_writing_gitmodules_ok())
3144 die(_("please make sure that the .gitmodules file is in the working tree"));
3146 key
= xstrfmt("submodule.%s.%s", name
, var
);
3147 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3153 static void configure_added_submodule(struct add_data
*add_data
)
3156 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3157 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3159 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3160 git_config_set_gently(key
, add_data
->realrepo
);
3163 add_submod
.git_cmd
= 1;
3164 strvec_pushl(&add_submod
.args
, "add",
3165 "--no-warn-embedded-repo", NULL
);
3166 if (add_data
->force
)
3167 strvec_push(&add_submod
.args
, "--force");
3168 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3170 if (run_command(&add_submod
))
3171 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3173 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3174 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3175 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3177 if (add_data
->branch
) {
3178 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3179 "branch", add_data
->branch
))
3180 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3183 add_gitmodules
.git_cmd
= 1;
3184 strvec_pushl(&add_gitmodules
.args
,
3185 "add", "--force", "--", ".gitmodules", NULL
);
3187 if (run_command(&add_gitmodules
))
3188 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3191 * NEEDSWORK: In a multi-working-tree world this needs to be
3192 * set in the per-worktree config.
3195 * NEEDSWORK: In the longer run, we need to get rid of this
3196 * pattern of querying "submodule.active" before calling
3197 * is_submodule_active(), since that function needs to find
3198 * out the value of "submodule.active" again anyway.
3200 if (!git_config_get("submodule.active")) {
3202 * If the submodule being added isn't already covered by the
3203 * current configured pathspec, set the submodule's active flag
3205 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3206 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3207 git_config_set_gently(key
, "true");
3211 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3212 git_config_set_gently(key
, "true");
3217 static void die_on_index_match(const char *path
, int force
)
3220 const char *args
[] = { path
, NULL
};
3221 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3223 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3224 die(_("index file corrupt"));
3228 char *ps_matched
= xcalloc(ps
.nr
, 1);
3230 /* TODO: audit for interaction with sparse-index. */
3231 ensure_full_index(&the_index
);
3234 * Since there is only one pathspec, we just need to
3235 * check ps_matched[0] to know if a cache entry matched.
3237 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
3238 ce_path_match(&the_index
, the_index
.cache
[i
], &ps
,
3241 if (ps_matched
[0]) {
3243 die(_("'%s' already exists in the index"),
3245 if (!S_ISGITLINK(the_index
.cache
[i
]->ce_mode
))
3246 die(_("'%s' already exists in the index "
3247 "and is not a submodule"), path
);
3253 clear_pathspec(&ps
);
3256 static void die_on_repo_without_commits(const char *path
)
3258 struct strbuf sb
= STRBUF_INIT
;
3259 strbuf_addstr(&sb
, path
);
3260 if (is_nonbare_repository_dir(&sb
)) {
3261 struct object_id oid
;
3262 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3263 die(_("'%s' does not have a commit checked out"), path
);
3265 strbuf_release(&sb
);
3268 static int module_add(int argc
, const char **argv
, const char *prefix
)
3270 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3271 struct add_data add_data
= ADD_DATA_INIT
;
3272 char *to_free
= NULL
;
3273 struct option options
[] = {
3274 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3275 N_("branch of repository to add as submodule")),
3276 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3277 PARSE_OPT_NOCOMPLETE
),
3278 OPT__QUIET(&quiet
, N_("print only error messages")),
3279 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3280 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3281 N_("reference repository")),
3282 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3283 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3284 N_("sets the submodule's name to the given string "
3285 "instead of defaulting to its path")),
3286 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3289 const char *const usage
[] = {
3290 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3293 struct strbuf sb
= STRBUF_INIT
;
3296 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3298 if (!is_writing_gitmodules_ok())
3299 die(_("please make sure that the .gitmodules file is in the working tree"));
3301 if (prefix
&& *prefix
&&
3302 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3303 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3305 if (argc
== 0 || argc
> 2)
3306 usage_with_options(usage
, options
);
3308 add_data
.repo
= argv
[0];
3310 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3312 add_data
.sm_path
= xstrdup(argv
[1]);
3314 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3315 char *sm_path
= add_data
.sm_path
;
3317 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3321 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3322 starts_with_dot_slash(add_data
.repo
)) {
3324 die(_("Relative path can only be used from the toplevel "
3325 "of the working tree"));
3327 /* dereference source url relative to parent's url */
3328 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3329 add_data
.realrepo
= to_free
;
3330 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3331 add_data
.realrepo
= add_data
.repo
;
3333 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3339 * multiple //; leading ./; /./; /../;
3341 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3342 strip_dir_trailing_slashes(add_data
.sm_path
);
3344 die_on_index_match(add_data
.sm_path
, force
);
3345 die_on_repo_without_commits(add_data
.sm_path
);
3348 struct child_process cp
= CHILD_PROCESS_INIT
;
3352 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3353 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3354 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3355 strbuf_complete_line(&sb
);
3356 fputs(sb
.buf
, stderr
);
3361 if(!add_data
.sm_name
)
3362 add_data
.sm_name
= add_data
.sm_path
;
3364 if (check_submodule_name(add_data
.sm_name
))
3365 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3367 add_data
.prefix
= prefix
;
3368 add_data
.force
= !!force
;
3369 add_data
.quiet
= !!quiet
;
3370 add_data
.progress
= !!progress
;
3371 add_data
.dissociate
= !!dissociate
;
3373 if (add_submodule(&add_data
))
3375 configure_added_submodule(&add_data
);
3379 free(add_data
.sm_path
);
3381 strbuf_release(&sb
);
3386 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3388 parse_opt_subcommand_fn
*fn
= NULL
;
3389 const char *const usage
[] = {
3390 N_("git submodule--helper <command>"),
3393 struct option options
[] = {
3394 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3395 OPT_SUBCOMMAND("add", &fn
, module_add
),
3396 OPT_SUBCOMMAND("update", &fn
, module_update
),
3397 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3398 OPT_SUBCOMMAND("init", &fn
, module_init
),
3399 OPT_SUBCOMMAND("status", &fn
, module_status
),
3400 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3401 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3402 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3403 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3404 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3405 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3406 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3407 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3410 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3412 return fn(argc
, argv
, prefix
);