1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
3 #include "repository.h"
6 #include "parse-options.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
21 #include "object-store.h"
24 #include "list-objects-filter-options.h"
26 #define OPT_QUIET (1 << 0)
27 #define OPT_CACHED (1 << 1)
28 #define OPT_RECURSIVE (1 << 2)
29 #define OPT_FORCE (1 << 3)
31 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
34 static int repo_get_default_remote(struct repository
*repo
, char **default_remote
)
37 struct strbuf sb
= STRBUF_INIT
;
38 struct ref_store
*store
= get_main_ref_store(repo
);
39 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
43 return die_message(_("No such ref: %s"), "HEAD");
46 if (!strcmp(refname
, "HEAD")) {
47 *default_remote
= xstrdup("origin");
51 if (!skip_prefix(refname
, "refs/heads/", &refname
))
52 return die_message(_("Expecting a full ref name, got %s"),
55 strbuf_addf(&sb
, "branch.%s.remote", refname
);
56 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
57 *default_remote
= xstrdup("origin");
59 *default_remote
= dest
;
65 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
67 struct repository subrepo
;
69 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
71 return die_message(_("could not get a repository handle for submodule '%s'"),
73 return repo_get_default_remote(&subrepo
, default_remote
);
76 static char *get_default_remote(void)
79 int code
= repo_get_default_remote(the_repository
, &default_remote
);
84 return default_remote
;
87 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
89 char *remoteurl
, *resolved_url
;
90 char *remote
= get_default_remote();
91 struct strbuf remotesb
= STRBUF_INIT
;
93 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
94 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
96 warning(_("could not look up configuration '%s'. "
97 "Assuming this repository is its own "
98 "authoritative upstream."),
100 remoteurl
= xgetcwd();
102 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
106 strbuf_release(&remotesb
);
111 /* the result should be freed by the caller. */
112 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
114 const char *super_prefix
= get_super_prefix();
116 if (prefix
&& super_prefix
) {
117 BUG("cannot have prefix '%s' and superprefix '%s'",
118 prefix
, super_prefix
);
120 struct strbuf sb
= STRBUF_INIT
;
121 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
124 } else if (super_prefix
) {
125 return xstrfmt("%s%s", super_prefix
, path
);
127 return xstrdup(path
);
131 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
133 struct strbuf sb
= STRBUF_INIT
;
136 static const char *describe_bare
[] = { NULL
};
138 static const char *describe_tags
[] = { "--tags", NULL
};
140 static const char *describe_contains
[] = { "--contains", NULL
};
142 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
144 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
146 describe_all_always
, NULL
};
148 for (d
= describe_argv
; *d
; d
++) {
149 struct child_process cp
= CHILD_PROCESS_INIT
;
150 prepare_submodule_repo_env(&cp
.env
);
155 strvec_push(&cp
.args
, "describe");
156 strvec_pushv(&cp
.args
, *d
);
157 strvec_push(&cp
.args
, object_id
);
159 if (!capture_command(&cp
, &sb
, 0)) {
160 strbuf_strip_suffix(&sb
, "\n");
161 return strbuf_detach(&sb
, NULL
);
170 const struct cache_entry
**entries
;
173 #define MODULE_LIST_INIT { 0 }
175 static int module_list_compute(int argc
, const char **argv
,
177 struct pathspec
*pathspec
,
178 struct module_list
*list
)
181 char *ps_matched
= NULL
;
183 parse_pathspec(pathspec
, 0,
184 PATHSPEC_PREFER_FULL
,
188 ps_matched
= xcalloc(pathspec
->nr
, 1);
190 if (read_cache() < 0)
191 die(_("index file corrupt"));
193 for (i
= 0; i
< active_nr
; i
++) {
194 const struct cache_entry
*ce
= active_cache
[i
];
196 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
198 !S_ISGITLINK(ce
->ce_mode
))
201 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
202 list
->entries
[list
->nr
++] = ce
;
203 while (i
+ 1 < active_nr
&&
204 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
206 * Skip entries with the same name in different stages
207 * to make sure an entry is returned only once.
212 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
220 static void module_list_active(struct module_list
*list
)
223 struct module_list active_modules
= MODULE_LIST_INIT
;
225 for (i
= 0; i
< list
->nr
; i
++) {
226 const struct cache_entry
*ce
= list
->entries
[i
];
228 if (!is_submodule_active(the_repository
, ce
->name
))
231 ALLOC_GROW(active_modules
.entries
,
232 active_modules
.nr
+ 1,
233 active_modules
.alloc
);
234 active_modules
.entries
[active_modules
.nr
++] = ce
;
238 *list
= active_modules
;
241 static char *get_up_path(const char *path
)
244 struct strbuf sb
= STRBUF_INIT
;
246 for (i
= count_slashes(path
); i
; i
--)
247 strbuf_addstr(&sb
, "../");
250 * Check if 'path' ends with slash or not
251 * for having the same output for dir/sub_dir
254 if (!is_dir_sep(path
[strlen(path
) - 1]))
255 strbuf_addstr(&sb
, "../");
257 return strbuf_detach(&sb
, NULL
);
260 static void for_each_listed_submodule(const struct module_list
*list
,
261 each_submodule_fn fn
, void *cb_data
)
265 for (i
= 0; i
< list
->nr
; i
++)
266 fn(list
->entries
[i
], cb_data
);
276 #define FOREACH_CB_INIT { 0 }
278 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
281 struct foreach_cb
*info
= cb_data
;
282 const char *path
= list_item
->name
;
283 const struct object_id
*ce_oid
= &list_item
->oid
;
284 const struct submodule
*sub
;
285 struct child_process cp
= CHILD_PROCESS_INIT
;
288 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
290 sub
= submodule_from_path(the_repository
, null_oid(), path
);
293 die(_("No url found for submodule path '%s' in .gitmodules"),
296 if (!is_submodule_populated_gently(path
, NULL
))
299 prepare_submodule_repo_env(&cp
.env
);
302 * For the purpose of executing <command> in the submodule,
303 * separate shell is used for the purpose of running the
310 * NEEDSWORK: the command currently has access to the variables $name,
311 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
312 * contains a single argument. This is done for maintaining a faithful
313 * translation from shell script.
315 if (info
->argc
== 1) {
316 char *toplevel
= xgetcwd();
317 struct strbuf sb
= STRBUF_INIT
;
319 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
320 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
321 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
322 strvec_pushf(&cp
.env
, "sha1=%s",
324 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
327 * Since the path variable was accessible from the script
328 * before porting, it is also made available after porting.
329 * The environment variable "PATH" has a very special purpose
330 * on windows. And since environment variables are
331 * case-insensitive in windows, it interferes with the
332 * existing PATH variable. Hence, to avoid that, we expose
333 * path via the args strvec and not via env.
335 sq_quote_buf(&sb
, path
);
336 strvec_pushf(&cp
.args
, "path=%s; %s",
337 sb
.buf
, info
->argv
[0]);
341 strvec_pushv(&cp
.args
, info
->argv
);
345 printf(_("Entering '%s'\n"), displaypath
);
347 if (info
->argv
[0] && run_command(&cp
))
348 die(_("run_command returned non-zero status for %s\n."),
351 if (info
->recursive
) {
352 struct child_process cpr
= CHILD_PROCESS_INIT
;
356 prepare_submodule_repo_env(&cpr
.env
);
358 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
359 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
360 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
364 strvec_push(&cpr
.args
, "--quiet");
366 strvec_push(&cpr
.args
, "--");
367 strvec_pushv(&cpr
.args
, info
->argv
);
369 if (run_command(&cpr
))
370 die(_("run_command returned non-zero status while "
371 "recursing in the nested submodules of %s\n."),
379 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
381 struct foreach_cb info
= FOREACH_CB_INIT
;
382 struct pathspec pathspec
;
383 struct module_list list
= MODULE_LIST_INIT
;
384 struct option module_foreach_options
[] = {
385 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
386 OPT_BOOL(0, "recursive", &info
.recursive
,
387 N_("recurse into nested submodules")),
390 const char *const git_submodule_helper_usage
[] = {
391 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
395 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
396 git_submodule_helper_usage
, 0);
398 if (module_list_compute(0, NULL
, prefix
, &pathspec
, &list
) < 0)
403 info
.prefix
= prefix
;
405 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
410 static int starts_with_dot_slash(const char *const path
)
412 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
413 PATH_MATCH_XPLATFORM
);
416 static int starts_with_dot_dot_slash(const char *const path
)
418 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
419 PATH_MATCH_XPLATFORM
);
426 #define INIT_CB_INIT { 0 }
428 static void init_submodule(const char *path
, const char *prefix
,
431 const struct submodule
*sub
;
432 struct strbuf sb
= STRBUF_INIT
;
434 char *url
= NULL
, *displaypath
;
436 displaypath
= get_submodule_displaypath(path
, prefix
);
438 sub
= submodule_from_path(the_repository
, null_oid(), path
);
441 die(_("No url found for submodule path '%s' in .gitmodules"),
445 * NEEDSWORK: In a multi-working-tree world, this needs to be
446 * set in the per-worktree config.
448 * Set active flag for the submodule being initialized
450 if (!is_submodule_active(the_repository
, path
)) {
451 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
452 git_config_set_gently(sb
.buf
, "true");
457 * Copy url setting when it is not set yet.
458 * To look up the url in .git/config, we must not fall back to
459 * .gitmodules, so look it up directly.
461 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
462 if (git_config_get_string(sb
.buf
, &url
)) {
464 die(_("No url found for submodule path '%s' in .gitmodules"),
467 url
= xstrdup(sub
->url
);
469 /* Possibly a url relative to parent */
470 if (starts_with_dot_dot_slash(url
) ||
471 starts_with_dot_slash(url
)) {
474 url
= resolve_relative_url(oldurl
, NULL
, 0);
478 if (git_config_set_gently(sb
.buf
, url
))
479 die(_("Failed to register url for submodule path '%s'"),
481 if (!(flags
& OPT_QUIET
))
483 _("Submodule '%s' (%s) registered for path '%s'\n"),
484 sub
->name
, url
, displaypath
);
488 /* Copy "update" setting when it is not set yet */
489 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
490 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
491 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
492 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
493 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
497 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
500 if (git_config_set_gently(sb
.buf
, upd
))
501 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
508 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
510 struct init_cb
*info
= cb_data
;
512 init_submodule(list_item
->name
, info
->prefix
, info
->flags
);
515 static int module_init(int argc
, const char **argv
, const char *prefix
)
517 struct init_cb info
= INIT_CB_INIT
;
518 struct pathspec pathspec
;
519 struct module_list list
= MODULE_LIST_INIT
;
521 struct option module_init_options
[] = {
522 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
525 const char *const git_submodule_helper_usage
[] = {
526 N_("git submodule init [<options>] [<path>]"),
530 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
531 git_submodule_helper_usage
, 0);
533 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
537 * If there are no path args and submodule.active is set then,
538 * by default, only initialize 'active' modules.
540 if (!argc
&& git_config_get_value_multi("submodule.active"))
541 module_list_active(&list
);
543 info
.prefix
= prefix
;
545 info
.flags
|= OPT_QUIET
;
547 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
556 #define STATUS_CB_INIT { 0 }
558 static void print_status(unsigned int flags
, char state
, const char *path
,
559 const struct object_id
*oid
, const char *displaypath
)
561 if (flags
& OPT_QUIET
)
564 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
566 if (state
== ' ' || state
== '+') {
567 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
570 printf(" (%s)", name
);
576 static int handle_submodule_head_ref(const char *refname
,
577 const struct object_id
*oid
, int flags
,
580 struct object_id
*output
= cb_data
;
588 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
589 unsigned int ce_flags
, const char *prefix
,
593 struct strvec diff_files_args
= STRVEC_INIT
;
594 struct rev_info rev
= REV_INFO_INIT
;
595 int diff_files_result
;
596 struct strbuf buf
= STRBUF_INIT
;
599 if (!submodule_from_path(the_repository
, null_oid(), path
))
600 die(_("no submodule mapping found in .gitmodules for path '%s'"),
603 displaypath
= get_submodule_displaypath(path
, prefix
);
605 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
606 print_status(flags
, 'U', path
, null_oid(), displaypath
);
610 strbuf_addf(&buf
, "%s/.git", path
);
611 git_dir
= read_gitfile(buf
.buf
);
615 if (!is_submodule_active(the_repository
, path
) ||
616 !is_git_directory(git_dir
)) {
617 print_status(flags
, '-', path
, ce_oid
, displaypath
);
618 strbuf_release(&buf
);
621 strbuf_release(&buf
);
623 strvec_pushl(&diff_files_args
, "diff-files",
624 "--ignore-submodules=dirty", "--quiet", "--",
627 git_config(git_diff_basic_config
, NULL
);
629 repo_init_revisions(the_repository
, &rev
, NULL
);
631 diff_files_args
.nr
= setup_revisions(diff_files_args
.nr
,
634 diff_files_result
= run_diff_files(&rev
, 0);
636 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
637 print_status(flags
, ' ', path
, ce_oid
,
639 } else if (!(flags
& OPT_CACHED
)) {
640 struct object_id oid
;
641 struct ref_store
*refs
= get_submodule_ref_store(path
);
644 print_status(flags
, '-', path
, ce_oid
, displaypath
);
647 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
648 die(_("could not resolve HEAD ref inside the "
649 "submodule '%s'"), path
);
651 print_status(flags
, '+', path
, &oid
, displaypath
);
653 print_status(flags
, '+', path
, ce_oid
, displaypath
);
656 if (flags
& OPT_RECURSIVE
) {
657 struct child_process cpr
= CHILD_PROCESS_INIT
;
661 prepare_submodule_repo_env(&cpr
.env
);
663 strvec_push(&cpr
.args
, "--super-prefix");
664 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
665 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
666 "--recursive", NULL
);
668 if (flags
& OPT_CACHED
)
669 strvec_push(&cpr
.args
, "--cached");
671 if (flags
& OPT_QUIET
)
672 strvec_push(&cpr
.args
, "--quiet");
674 if (run_command(&cpr
))
675 die(_("failed to recurse into submodule '%s'"), path
);
679 strvec_clear(&diff_files_args
);
681 release_revisions(&rev
);
684 static void status_submodule_cb(const struct cache_entry
*list_item
,
687 struct status_cb
*info
= cb_data
;
689 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
690 info
->prefix
, info
->flags
);
693 static int module_status(int argc
, const char **argv
, const char *prefix
)
695 struct status_cb info
= STATUS_CB_INIT
;
696 struct pathspec pathspec
;
697 struct module_list list
= MODULE_LIST_INIT
;
699 struct option module_status_options
[] = {
700 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
701 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
702 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
705 const char *const git_submodule_helper_usage
[] = {
706 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
710 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
711 git_submodule_helper_usage
, 0);
713 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
716 info
.prefix
= prefix
;
718 info
.flags
|= OPT_QUIET
;
720 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
726 unsigned int mod_src
;
727 unsigned int mod_dst
;
728 struct object_id oid_src
;
729 struct object_id oid_dst
;
733 #define MODULE_CB_INIT { 0 }
735 struct module_cb_list
{
736 struct module_cb
**entries
;
739 #define MODULE_CB_LIST_INIT { 0 }
745 unsigned int cached
: 1;
746 unsigned int for_status
: 1;
747 unsigned int files
: 1;
750 #define SUMMARY_CB_INIT { 0 }
757 static char *verify_submodule_committish(const char *sm_path
,
758 const char *committish
)
760 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
761 struct strbuf result
= STRBUF_INIT
;
763 cp_rev_parse
.git_cmd
= 1;
764 cp_rev_parse
.dir
= sm_path
;
765 prepare_submodule_repo_env(&cp_rev_parse
.env
);
766 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
767 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
768 strvec_push(&cp_rev_parse
.args
, "--");
770 if (capture_command(&cp_rev_parse
, &result
, 0))
773 strbuf_trim_trailing_newline(&result
);
774 return strbuf_detach(&result
, NULL
);
777 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
778 int total_commits
, const char *displaypath
,
779 const char *src_abbrev
, const char *dst_abbrev
,
782 if (p
->status
== 'T') {
783 if (S_ISGITLINK(p
->mod_dst
))
784 printf(_("* %s %s(blob)->%s(submodule)"),
785 displaypath
, src_abbrev
, dst_abbrev
);
787 printf(_("* %s %s(submodule)->%s(blob)"),
788 displaypath
, src_abbrev
, dst_abbrev
);
790 printf("* %s %s...%s",
791 displaypath
, src_abbrev
, dst_abbrev
);
794 if (total_commits
< 0)
797 printf(" (%d):\n", total_commits
);
800 printf(_("%s"), errmsg
);
801 } else if (total_commits
> 0) {
802 struct child_process cp_log
= CHILD_PROCESS_INIT
;
805 cp_log
.dir
= p
->sm_path
;
806 prepare_submodule_repo_env(&cp_log
.env
);
807 strvec_pushl(&cp_log
.args
, "log", NULL
);
809 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
810 if (info
->summary_limit
> 0)
811 strvec_pushf(&cp_log
.args
, "-%d",
812 info
->summary_limit
);
814 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
815 "--first-parent", NULL
);
816 strvec_pushf(&cp_log
.args
, "%s...%s",
817 src_abbrev
, dst_abbrev
);
818 } else if (S_ISGITLINK(p
->mod_dst
)) {
819 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
820 "-1", dst_abbrev
, NULL
);
822 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
823 "-1", src_abbrev
, NULL
);
825 run_command(&cp_log
);
830 static void generate_submodule_summary(struct summary_cb
*info
,
833 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
834 int missing_src
= 0, missing_dst
= 0;
835 struct strbuf errmsg
= STRBUF_INIT
;
836 int total_commits
= -1;
838 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
839 if (S_ISGITLINK(p
->mod_dst
)) {
840 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
843 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
844 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
846 int fd
= open(p
->sm_path
, O_RDONLY
);
848 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
849 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
851 error(_("couldn't hash object from '%s'"), p
->sm_path
);
853 /* for a submodule removal (mode:0000000), don't warn */
855 warning(_("unexpected mode %o\n"), p
->mod_dst
);
859 if (S_ISGITLINK(p
->mod_src
)) {
860 if (p
->status
!= 'D')
861 src_abbrev
= verify_submodule_committish(p
->sm_path
,
862 oid_to_hex(&p
->oid_src
));
866 * As `rev-parse` failed, we fallback to getting
867 * the abbreviated hash using oid_src. We do
868 * this as we might still need the abbreviated
869 * hash in cases like a submodule type change, etc.
871 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
875 * The source does not point to a submodule.
876 * So, we fallback to getting the abbreviation using
877 * oid_src as we might still need the abbreviated
878 * hash in cases like submodule add, etc.
880 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
883 if (S_ISGITLINK(p
->mod_dst
)) {
884 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
885 oid_to_hex(&p
->oid_dst
));
889 * As `rev-parse` failed, we fallback to getting
890 * the abbreviated hash using oid_dst. We do
891 * this as we might still need the abbreviated
892 * hash in cases like a submodule type change, etc.
894 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
898 * The destination does not point to a submodule.
899 * So, we fallback to getting the abbreviation using
900 * oid_dst as we might still need the abbreviated
901 * hash in cases like a submodule removal, etc.
903 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
906 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
);
908 if (!missing_src
&& !missing_dst
) {
909 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
910 struct strbuf sb_rev_list
= STRBUF_INIT
;
912 strvec_pushl(&cp_rev_list
.args
, "rev-list",
913 "--first-parent", "--count", NULL
);
914 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
915 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
916 src_abbrev
, dst_abbrev
);
918 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
919 src_abbrev
: dst_abbrev
);
920 strvec_push(&cp_rev_list
.args
, "--");
922 cp_rev_list
.git_cmd
= 1;
923 cp_rev_list
.dir
= p
->sm_path
;
924 prepare_submodule_repo_env(&cp_rev_list
.env
);
926 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
927 total_commits
= atoi(sb_rev_list
.buf
);
929 strbuf_release(&sb_rev_list
);
932 * Don't give error msg for modification whose dst is not
933 * submodule, i.e., deleted or changed to blob
935 if (S_ISGITLINK(p
->mod_dst
)) {
936 if (missing_src
&& missing_dst
) {
937 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
938 displaypath
, oid_to_hex(&p
->oid_src
),
939 oid_to_hex(&p
->oid_dst
));
941 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
942 displaypath
, missing_src
?
943 oid_to_hex(&p
->oid_src
) :
944 oid_to_hex(&p
->oid_dst
));
949 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
950 total_commits
, displaypath
, src_abbrev
,
958 static void prepare_submodule_summary(struct summary_cb
*info
,
959 struct module_cb_list
*list
)
962 for (i
= 0; i
< list
->nr
; i
++) {
963 const struct submodule
*sub
;
964 struct module_cb
*p
= list
->entries
[i
];
965 struct strbuf sm_gitdir
= STRBUF_INIT
;
967 if (p
->status
== 'D' || p
->status
== 'T') {
968 generate_submodule_summary(info
, p
);
972 if (info
->for_status
&& p
->status
!= 'A' &&
973 (sub
= submodule_from_path(the_repository
,
974 null_oid(), p
->sm_path
))) {
975 char *config_key
= NULL
;
979 config_key
= xstrfmt("submodule.%s.ignore",
981 if (!git_config_get_string_tmp(config_key
, &value
))
982 ignore_all
= !strcmp(value
, "all");
983 else if (sub
->ignore
)
984 ignore_all
= !strcmp(sub
->ignore
, "all");
991 /* Also show added or modified modules which are checked out */
992 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
993 if (is_nonbare_repository_dir(&sm_gitdir
))
994 generate_submodule_summary(info
, p
);
995 strbuf_release(&sm_gitdir
);
999 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1000 struct diff_options
*options
,
1004 struct module_cb_list
*list
= data
;
1005 for (i
= 0; i
< q
->nr
; i
++) {
1006 struct diff_filepair
*p
= q
->queue
[i
];
1007 struct module_cb
*temp
;
1009 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1011 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1012 temp
->mod_src
= p
->one
->mode
;
1013 temp
->mod_dst
= p
->two
->mode
;
1014 temp
->oid_src
= p
->one
->oid
;
1015 temp
->oid_dst
= p
->two
->oid
;
1016 temp
->status
= p
->status
;
1017 temp
->sm_path
= xstrdup(p
->one
->path
);
1019 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1020 list
->entries
[list
->nr
++] = temp
;
1024 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1027 case DIFF_INDEX
: return "diff-index";
1028 case DIFF_FILES
: return "diff-files";
1029 default: BUG("bad diff_cmd value %d", diff_cmd
);
1033 static int compute_summary_module_list(struct object_id
*head_oid
,
1034 struct summary_cb
*info
,
1035 enum diff_cmd diff_cmd
)
1037 struct strvec diff_args
= STRVEC_INIT
;
1038 struct rev_info rev
;
1039 struct setup_revision_opt opt
= {
1040 .free_removed_argv_elements
= 1,
1042 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1045 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1047 strvec_push(&diff_args
, "--cached");
1048 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1050 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1051 strvec_push(&diff_args
, "--");
1053 strvec_pushv(&diff_args
, info
->argv
);
1055 git_config(git_diff_basic_config
, NULL
);
1056 init_revisions(&rev
, info
->prefix
);
1058 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1059 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1060 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1061 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1062 rev
.diffopt
.format_callback_data
= &list
;
1064 if (!info
->cached
) {
1065 if (diff_cmd
== DIFF_INDEX
)
1067 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1068 perror("read_cache_preload");
1072 } else if (read_cache() < 0) {
1073 perror("read_cache");
1078 if (diff_cmd
== DIFF_INDEX
)
1079 run_diff_index(&rev
, info
->cached
);
1081 run_diff_files(&rev
, 0);
1082 prepare_submodule_summary(info
, &list
);
1084 strvec_clear(&diff_args
);
1085 release_revisions(&rev
);
1089 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1091 struct summary_cb info
= SUMMARY_CB_INIT
;
1095 int summary_limit
= -1;
1096 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1097 struct object_id head_oid
;
1099 struct option module_summary_options
[] = {
1100 OPT_BOOL(0, "cached", &cached
,
1101 N_("use the commit stored in the index instead of the submodule HEAD")),
1102 OPT_BOOL(0, "files", &files
,
1103 N_("compare the commit in the index with that in the submodule HEAD")),
1104 OPT_BOOL(0, "for-status", &for_status
,
1105 N_("skip submodules with 'ignore_config' value set to 'all'")),
1106 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1107 N_("limit the summary size")),
1110 const char *const git_submodule_helper_usage
[] = {
1111 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1115 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1116 git_submodule_helper_usage
, 0);
1121 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1126 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1127 /* before the first commit: compare with an empty tree */
1128 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1134 if (get_oid("HEAD", &head_oid
))
1135 die(_("could not fetch a revision for HEAD"));
1140 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1141 diff_cmd
= DIFF_FILES
;
1146 info
.prefix
= prefix
;
1147 info
.cached
= !!cached
;
1148 info
.files
= !!files
;
1149 info
.for_status
= !!for_status
;
1150 info
.summary_limit
= summary_limit
;
1152 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1161 #define SYNC_CB_INIT { 0 }
1163 static void sync_submodule(const char *path
, const char *prefix
,
1166 const struct submodule
*sub
;
1167 char *remote_key
= NULL
;
1168 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1169 struct strbuf sb
= STRBUF_INIT
;
1170 char *sub_config_path
= NULL
;
1173 if (!is_submodule_active(the_repository
, path
))
1176 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1178 if (sub
&& sub
->url
) {
1179 if (starts_with_dot_dot_slash(sub
->url
) ||
1180 starts_with_dot_slash(sub
->url
)) {
1181 char *up_path
= get_up_path(path
);
1183 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1184 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1187 sub_origin_url
= xstrdup(sub
->url
);
1188 super_config_url
= xstrdup(sub
->url
);
1191 sub_origin_url
= xstrdup("");
1192 super_config_url
= xstrdup("");
1195 displaypath
= get_submodule_displaypath(path
, prefix
);
1197 if (!(flags
& OPT_QUIET
))
1198 printf(_("Synchronizing submodule url for '%s'\n"),
1202 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1203 if (git_config_set_gently(sb
.buf
, super_config_url
))
1204 die(_("failed to register url for submodule path '%s'"),
1207 if (!is_submodule_populated_gently(path
, NULL
))
1211 code
= get_default_remote_submodule(path
, &default_remote
);
1215 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1216 free(default_remote
);
1218 submodule_to_gitdir(&sb
, path
);
1219 strbuf_addstr(&sb
, "/config");
1221 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1222 die(_("failed to update remote for submodule '%s'"),
1225 if (flags
& OPT_RECURSIVE
) {
1226 struct child_process cpr
= CHILD_PROCESS_INIT
;
1230 prepare_submodule_repo_env(&cpr
.env
);
1232 strvec_push(&cpr
.args
, "--super-prefix");
1233 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1234 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1235 "--recursive", NULL
);
1237 if (flags
& OPT_QUIET
)
1238 strvec_push(&cpr
.args
, "--quiet");
1240 if (run_command(&cpr
))
1241 die(_("failed to recurse into submodule '%s'"),
1246 free(super_config_url
);
1247 free(sub_origin_url
);
1248 strbuf_release(&sb
);
1251 free(sub_config_path
);
1254 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1256 struct sync_cb
*info
= cb_data
;
1258 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1261 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1263 struct sync_cb info
= SYNC_CB_INIT
;
1264 struct pathspec pathspec
;
1265 struct module_list list
= MODULE_LIST_INIT
;
1268 struct option module_sync_options
[] = {
1269 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1270 OPT_BOOL(0, "recursive", &recursive
,
1271 N_("recurse into nested submodules")),
1274 const char *const git_submodule_helper_usage
[] = {
1275 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1279 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1280 git_submodule_helper_usage
, 0);
1282 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1285 info
.prefix
= prefix
;
1287 info
.flags
|= OPT_QUIET
;
1289 info
.flags
|= OPT_RECURSIVE
;
1291 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1300 #define DEINIT_CB_INIT { 0 }
1302 static void deinit_submodule(const char *path
, const char *prefix
,
1305 const struct submodule
*sub
;
1306 char *displaypath
= NULL
;
1307 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1308 struct strbuf sb_config
= STRBUF_INIT
;
1309 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1311 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1313 if (!sub
|| !sub
->name
)
1316 displaypath
= get_submodule_displaypath(path
, prefix
);
1318 /* remove the submodule work tree (unless the user already did it) */
1319 if (is_directory(path
)) {
1320 struct strbuf sb_rm
= STRBUF_INIT
;
1323 if (is_directory(sub_git_dir
)) {
1324 if (!(flags
& OPT_QUIET
))
1325 warning(_("Submodule work tree '%s' contains a .git "
1326 "directory. This will be replaced with a "
1327 ".git file by using absorbgitdirs."),
1330 absorb_git_dir_into_superproject(path
,
1331 ABSORB_GITDIR_RECURSE_SUBMODULES
);
1335 if (!(flags
& OPT_FORCE
)) {
1336 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1339 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1342 if (run_command(&cp_rm
))
1343 die(_("Submodule work tree '%s' contains local "
1344 "modifications; use '-f' to discard them"),
1348 strbuf_addstr(&sb_rm
, path
);
1350 if (!remove_dir_recursively(&sb_rm
, 0))
1351 format
= _("Cleared directory '%s'\n");
1353 format
= _("Could not remove submodule work tree '%s'\n");
1355 if (!(flags
& OPT_QUIET
))
1356 printf(format
, displaypath
);
1358 submodule_unset_core_worktree(sub
);
1360 strbuf_release(&sb_rm
);
1363 if (mkdir(path
, 0777))
1364 printf(_("could not create empty submodule directory %s"),
1367 cp_config
.git_cmd
= 1;
1368 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1369 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1371 /* remove the .git/config entries (unless the user already did it) */
1372 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1373 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1376 * remove the whole section so we have a clean state when
1377 * the user later decides to init this submodule again
1379 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1380 if (!(flags
& OPT_QUIET
))
1381 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1382 sub
->name
, sub
->url
, displaypath
);
1389 strbuf_release(&sb_config
);
1392 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1395 struct deinit_cb
*info
= cb_data
;
1396 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1399 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1401 struct deinit_cb info
= DEINIT_CB_INIT
;
1402 struct pathspec pathspec
;
1403 struct module_list list
= MODULE_LIST_INIT
;
1407 struct option module_deinit_options
[] = {
1408 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1409 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1410 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1413 const char *const git_submodule_helper_usage
[] = {
1414 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1418 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1419 git_submodule_helper_usage
, 0);
1422 error("pathspec and --all are incompatible");
1423 usage_with_options(git_submodule_helper_usage
,
1424 module_deinit_options
);
1428 die(_("Use '--all' if you really want to deinitialize all submodules"));
1430 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1433 info
.prefix
= prefix
;
1435 info
.flags
|= OPT_QUIET
;
1437 info
.flags
|= OPT_FORCE
;
1439 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1444 struct module_clone_data
{
1450 struct list_objects_filter_options
*filter_options
;
1451 unsigned int quiet
: 1;
1452 unsigned int progress
: 1;
1453 unsigned int dissociate
: 1;
1454 unsigned int require_init
: 1;
1457 #define MODULE_CLONE_DATA_INIT { \
1458 .single_branch = -1, \
1461 struct submodule_alternate_setup
{
1462 const char *submodule_name
;
1463 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1464 SUBMODULE_ALTERNATE_ERROR_DIE
,
1465 SUBMODULE_ALTERNATE_ERROR_INFO
,
1466 SUBMODULE_ALTERNATE_ERROR_IGNORE
1468 struct string_list
*reference
;
1470 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1471 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1474 static const char alternate_error_advice
[] = N_(
1475 "An alternate computed from a superproject's alternate is invalid.\n"
1476 "To allow Git to clone without an alternate in such a case, set\n"
1477 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1478 "'--reference-if-able' instead of '--reference'."
1481 static int add_possible_reference_from_superproject(
1482 struct object_directory
*odb
, void *sas_cb
)
1484 struct submodule_alternate_setup
*sas
= sas_cb
;
1488 * If the alternate object store is another repository, try the
1489 * standard layout with .git/(modules/<name>)+/objects
1491 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1492 struct repository alternate
;
1494 struct strbuf sb
= STRBUF_INIT
;
1495 struct strbuf err
= STRBUF_INIT
;
1496 strbuf_add(&sb
, odb
->path
, len
);
1498 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1499 die(_("could not get a repository handle for gitdir '%s'"),
1503 * We need to end the new path with '/' to mark it as a dir,
1504 * otherwise a submodule name containing '/' will be broken
1505 * as the last part of a missing submodule reference would
1506 * be taken as a file name.
1509 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1510 strbuf_addch(&sb
, '/');
1511 repo_clear(&alternate
);
1513 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1515 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1518 switch (sas
->error_mode
) {
1519 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1520 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1521 advise(_(alternate_error_advice
));
1522 die(_("submodule '%s' cannot add alternate: %s"),
1523 sas
->submodule_name
, err
.buf
);
1524 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1525 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1526 sas
->submodule_name
, err
.buf
);
1527 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1531 strbuf_release(&sb
);
1537 static void prepare_possible_alternates(const char *sm_name
,
1538 struct string_list
*reference
)
1540 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1541 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1543 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1547 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1549 if (!error_strategy
)
1550 error_strategy
= xstrdup("die");
1552 sas
.submodule_name
= sm_name
;
1553 sas
.reference
= reference
;
1554 if (!strcmp(error_strategy
, "die"))
1555 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1556 else if (!strcmp(error_strategy
, "info"))
1557 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1558 else if (!strcmp(error_strategy
, "ignore"))
1559 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1561 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1563 if (!strcmp(sm_alternate
, "superproject"))
1564 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1565 else if (!strcmp(sm_alternate
, "no"))
1568 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1571 free(error_strategy
);
1574 static char *clone_submodule_sm_gitdir(const char *name
)
1576 struct strbuf sb
= STRBUF_INIT
;
1579 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1580 sm_gitdir
= absolute_pathdup(sb
.buf
);
1581 strbuf_release(&sb
);
1586 static int clone_submodule(const struct module_clone_data
*clone_data
,
1587 struct string_list
*reference
)
1590 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1591 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1592 struct child_process cp
= CHILD_PROCESS_INIT
;
1593 const char *clone_data_path
;
1595 if (!is_absolute_path(clone_data
->path
))
1596 clone_data_path
= xstrfmt("%s/%s", get_git_work_tree(),
1599 clone_data_path
= xstrdup(clone_data
->path
);
1601 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1602 die(_("refusing to create/use '%s' in another submodule's "
1603 "git dir"), sm_gitdir
);
1605 if (!file_exists(sm_gitdir
)) {
1606 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1607 die(_("could not create directory '%s'"), sm_gitdir
);
1609 prepare_possible_alternates(clone_data
->name
, reference
);
1611 strvec_push(&cp
.args
, "clone");
1612 strvec_push(&cp
.args
, "--no-checkout");
1613 if (clone_data
->quiet
)
1614 strvec_push(&cp
.args
, "--quiet");
1615 if (clone_data
->progress
)
1616 strvec_push(&cp
.args
, "--progress");
1617 if (clone_data
->depth
&& *(clone_data
->depth
))
1618 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1619 if (reference
->nr
) {
1620 struct string_list_item
*item
;
1622 for_each_string_list_item(item
, reference
)
1623 strvec_pushl(&cp
.args
, "--reference",
1624 item
->string
, NULL
);
1626 if (clone_data
->dissociate
)
1627 strvec_push(&cp
.args
, "--dissociate");
1628 if (sm_gitdir
&& *sm_gitdir
)
1629 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1630 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1631 strvec_pushf(&cp
.args
, "--filter=%s",
1632 expand_list_objects_filter_spec(
1633 clone_data
->filter_options
));
1634 if (clone_data
->single_branch
>= 0)
1635 strvec_push(&cp
.args
, clone_data
->single_branch
?
1637 "--no-single-branch");
1639 strvec_push(&cp
.args
, "--");
1640 strvec_push(&cp
.args
, clone_data
->url
);
1641 strvec_push(&cp
.args
, clone_data_path
);
1644 prepare_submodule_repo_env(&cp
.env
);
1647 if(run_command(&cp
))
1648 die(_("clone of '%s' into submodule path '%s' failed"),
1649 clone_data
->url
, clone_data_path
);
1653 if (clone_data
->require_init
&& !access(clone_data_path
, X_OK
) &&
1654 !is_empty_dir(clone_data_path
))
1655 die(_("directory not empty: '%s'"), clone_data_path
);
1656 if (safe_create_leading_directories_const(clone_data_path
) < 0)
1657 die(_("could not create directory '%s'"), clone_data_path
);
1658 path
= xstrfmt("%s/index", sm_gitdir
);
1659 unlink_or_warn(path
);
1663 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1665 p
= git_pathdup_submodule(clone_data_path
, "config");
1667 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1669 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1670 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1672 git_config_set_in_file(p
, "submodule.alternateLocation",
1674 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1676 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1680 free(error_strategy
);
1687 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1689 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1690 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1691 struct list_objects_filter_options filter_options
= { 0 };
1692 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1693 struct option module_clone_options
[] = {
1694 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1696 N_("alternative anchor for relative paths")),
1697 OPT_STRING(0, "path", &clone_data
.path
,
1699 N_("where the new submodule will be cloned to")),
1700 OPT_STRING(0, "name", &clone_data
.name
,
1702 N_("name of the new submodule")),
1703 OPT_STRING(0, "url", &clone_data
.url
,
1705 N_("url where to clone the submodule from")),
1706 OPT_STRING_LIST(0, "reference", &reference
,
1708 N_("reference repository")),
1709 OPT_BOOL(0, "dissociate", &dissociate
,
1710 N_("use --reference only while cloning")),
1711 OPT_STRING(0, "depth", &clone_data
.depth
,
1713 N_("depth for shallow clones")),
1714 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1715 OPT_BOOL(0, "progress", &progress
,
1716 N_("force cloning progress")),
1717 OPT_BOOL(0, "require-init", &require_init
,
1718 N_("disallow cloning into non-empty directory")),
1719 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1720 N_("clone only one branch, HEAD or --branch")),
1721 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1724 const char *const git_submodule_helper_usage
[] = {
1725 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1726 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1727 "[--single-branch] [--filter <filter-spec>] "
1728 "--url <url> --path <path>"),
1732 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1733 git_submodule_helper_usage
, 0);
1735 clone_data
.dissociate
= !!dissociate
;
1736 clone_data
.quiet
= !!quiet
;
1737 clone_data
.progress
= !!progress
;
1738 clone_data
.require_init
= !!require_init
;
1739 clone_data
.filter_options
= &filter_options
;
1741 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1742 usage_with_options(git_submodule_helper_usage
,
1743 module_clone_options
);
1745 clone_submodule(&clone_data
, &reference
);
1746 list_objects_filter_release(&filter_options
);
1750 static int determine_submodule_update_strategy(struct repository
*r
,
1753 enum submodule_update_type update
,
1754 struct submodule_update_strategy
*out
)
1756 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1761 key
= xstrfmt("submodule.%s.update", sub
->name
);
1765 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1766 if (parse_submodule_update_strategy(val
, out
) < 0) {
1767 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1771 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1772 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1773 BUG("how did we read update = !command from .gitmodules?");
1774 out
->type
= sub
->update_strategy
.type
;
1775 out
->command
= sub
->update_strategy
.command
;
1777 out
->type
= SM_UPDATE_CHECKOUT
;
1780 (out
->type
== SM_UPDATE_MERGE
||
1781 out
->type
== SM_UPDATE_REBASE
||
1782 out
->type
== SM_UPDATE_NONE
))
1783 out
->type
= SM_UPDATE_CHECKOUT
;
1791 struct update_clone_data
{
1792 const struct submodule
*sub
;
1793 struct object_id oid
;
1794 unsigned just_cloned
;
1797 struct submodule_update_clone
{
1798 /* index into 'update_data.list', the list of submodules to look into for cloning */
1801 /* configuration parameters which are passed on to the children */
1802 const struct update_data
*update_data
;
1804 /* to be consumed by update_submodule() */
1805 struct update_clone_data
*update_clone
;
1806 int update_clone_nr
; int update_clone_alloc
;
1808 /* If we want to stop as fast as possible and return an error */
1809 unsigned quickstop
: 1;
1811 /* failed clones to be retried again */
1812 const struct cache_entry
**failed_clones
;
1813 int failed_clones_nr
, failed_clones_alloc
;
1815 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1817 struct update_data
{
1819 const char *displaypath
;
1820 enum submodule_update_type update_default
;
1821 struct object_id suboid
;
1822 struct string_list references
;
1823 struct submodule_update_strategy update_strategy
;
1824 struct list_objects_filter_options
*filter_options
;
1825 struct module_list list
;
1829 int recommend_shallow
;
1830 unsigned int require_init
;
1833 unsigned int nofetch
;
1834 unsigned int remote
;
1835 unsigned int progress
;
1836 unsigned int dissociate
;
1838 unsigned int warn_if_uninitialized
;
1839 unsigned int recursive
;
1841 /* copied over from update_clone_data */
1842 struct object_id oid
;
1843 unsigned int just_cloned
;
1844 const char *sm_path
;
1846 #define UPDATE_DATA_INIT { \
1847 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1848 .list = MODULE_LIST_INIT, \
1849 .recommend_shallow = -1, \
1850 .references = STRING_LIST_INIT_DUP, \
1851 .single_branch = -1, \
1855 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1856 struct strbuf
*out
, const char *displaypath
)
1859 * Only mention uninitialized submodules when their
1860 * paths have been specified.
1862 if (suc
->update_data
->warn_if_uninitialized
) {
1864 _("Submodule path '%s' not initialized"),
1866 strbuf_addch(out
, '\n');
1868 _("Maybe you want to use 'update --init'?"));
1869 strbuf_addch(out
, '\n');
1874 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1875 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1877 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1878 struct child_process
*child
,
1879 struct submodule_update_clone
*suc
,
1882 const struct submodule
*sub
= NULL
;
1883 const char *url
= NULL
;
1884 const char *update_string
;
1885 enum submodule_update_type update_type
;
1887 const struct update_data
*ud
= suc
->update_data
;
1888 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
);
1889 struct strbuf sb
= STRBUF_INIT
;
1890 int needs_cloning
= 0;
1891 int need_free_url
= 0;
1894 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
1895 strbuf_addch(out
, '\n');
1899 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
1902 next_submodule_warn_missing(suc
, out
, displaypath
);
1906 key
= xstrfmt("submodule.%s.update", sub
->name
);
1907 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
1908 update_type
= parse_submodule_update_type(update_string
);
1910 update_type
= sub
->update_strategy
.type
;
1914 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
1915 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
1916 && update_type
== SM_UPDATE_NONE
)) {
1917 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
1918 strbuf_addch(out
, '\n');
1922 /* Check if the submodule has been initialized. */
1923 if (!is_submodule_active(the_repository
, ce
->name
)) {
1924 next_submodule_warn_missing(suc
, out
, displaypath
);
1929 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1930 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
1931 if (starts_with_dot_slash(sub
->url
) ||
1932 starts_with_dot_dot_slash(sub
->url
)) {
1933 url
= resolve_relative_url(sub
->url
, NULL
, 0);
1940 strbuf_addf(&sb
, "%s/.git", ce
->name
);
1941 needs_cloning
= !file_exists(sb
.buf
);
1943 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
1944 suc
->update_clone_alloc
);
1945 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
1946 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
1947 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
1948 suc
->update_clone_nr
++;
1954 child
->no_stdin
= 1;
1955 child
->stdout_to_stderr
= 1;
1957 strvec_push(&child
->args
, "submodule--helper");
1958 strvec_push(&child
->args
, "clone");
1959 if (suc
->update_data
->progress
)
1960 strvec_push(&child
->args
, "--progress");
1961 if (suc
->update_data
->quiet
)
1962 strvec_push(&child
->args
, "--quiet");
1963 if (suc
->update_data
->prefix
)
1964 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
1965 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
1966 strvec_push(&child
->args
, "--depth=1");
1967 else if (suc
->update_data
->depth
)
1968 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
1969 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
1970 strvec_pushf(&child
->args
, "--filter=%s",
1971 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
1972 if (suc
->update_data
->require_init
)
1973 strvec_push(&child
->args
, "--require-init");
1974 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
1975 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
1976 strvec_pushl(&child
->args
, "--url", url
, NULL
);
1977 if (suc
->update_data
->references
.nr
) {
1978 struct string_list_item
*item
;
1980 for_each_string_list_item(item
, &suc
->update_data
->references
)
1981 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
1983 if (suc
->update_data
->dissociate
)
1984 strvec_push(&child
->args
, "--dissociate");
1985 if (suc
->update_data
->single_branch
>= 0)
1986 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
1988 "--no-single-branch");
1992 strbuf_release(&sb
);
1996 return needs_cloning
;
1999 static int update_clone_get_next_task(struct child_process
*child
,
2004 struct submodule_update_clone
*suc
= suc_cb
;
2005 const struct cache_entry
*ce
;
2008 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2009 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2010 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2011 int *p
= xmalloc(sizeof(*p
));
2021 * The loop above tried cloning each submodule once, now try the
2022 * stragglers again, which we can imagine as an extension of the
2025 index
= suc
->current
- suc
->update_data
->list
.nr
;
2026 if (index
< suc
->failed_clones_nr
) {
2029 ce
= suc
->failed_clones
[index
];
2030 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2032 strbuf_addstr(err
, "BUG: submodule considered for "
2033 "cloning, doesn't need cloning "
2037 p
= xmalloc(sizeof(*p
));
2047 static int update_clone_start_failure(struct strbuf
*err
,
2051 struct submodule_update_clone
*suc
= suc_cb
;
2057 static int update_clone_task_finished(int result
,
2062 const struct cache_entry
*ce
;
2063 struct submodule_update_clone
*suc
= suc_cb
;
2064 int *idxP
= idx_task_cb
;
2072 if (idx
< suc
->update_data
->list
.nr
) {
2073 ce
= suc
->update_data
->list
.entries
[idx
];
2074 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2076 strbuf_addch(err
, '\n');
2077 ALLOC_GROW(suc
->failed_clones
,
2078 suc
->failed_clones_nr
+ 1,
2079 suc
->failed_clones_alloc
);
2080 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2083 idx
-= suc
->update_data
->list
.nr
;
2084 ce
= suc
->failed_clones
[idx
];
2085 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2087 strbuf_addch(err
, '\n');
2095 static int git_update_clone_config(const char *var
, const char *value
,
2100 if (!strcmp(var
, "submodule.fetchjobs"))
2101 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2105 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2107 struct child_process cp
= CHILD_PROCESS_INIT
;
2108 struct strbuf rev
= STRBUF_INIT
;
2109 char *hex
= oid_to_hex(oid
);
2112 cp
.dir
= xstrdup(path
);
2114 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2116 prepare_submodule_repo_env(&cp
.env
);
2118 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2124 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2125 const struct object_id
*oid
)
2127 struct child_process cp
= CHILD_PROCESS_INIT
;
2129 prepare_submodule_repo_env(&cp
.env
);
2131 cp
.dir
= xstrdup(module_path
);
2133 strvec_push(&cp
.args
, "fetch");
2135 strvec_push(&cp
.args
, "--quiet");
2137 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2139 char *hex
= oid_to_hex(oid
);
2140 char *remote
= get_default_remote();
2142 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2146 return run_command(&cp
);
2149 static int run_update_command(const struct update_data
*ud
, int subforce
)
2151 struct child_process cp
= CHILD_PROCESS_INIT
;
2152 char *oid
= oid_to_hex(&ud
->oid
);
2155 switch (ud
->update_strategy
.type
) {
2156 case SM_UPDATE_CHECKOUT
:
2158 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2160 strvec_push(&cp
.args
, "-f");
2162 case SM_UPDATE_REBASE
:
2164 strvec_push(&cp
.args
, "rebase");
2166 strvec_push(&cp
.args
, "--quiet");
2168 case SM_UPDATE_MERGE
:
2170 strvec_push(&cp
.args
, "merge");
2172 strvec_push(&cp
.args
, "--quiet");
2174 case SM_UPDATE_COMMAND
:
2176 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2179 BUG("unexpected update strategy type: %d",
2180 ud
->update_strategy
.type
);
2182 strvec_push(&cp
.args
, oid
);
2184 cp
.dir
= xstrdup(ud
->sm_path
);
2185 prepare_submodule_repo_env(&cp
.env
);
2186 if ((ret
= run_command(&cp
))) {
2187 switch (ud
->update_strategy
.type
) {
2188 case SM_UPDATE_CHECKOUT
:
2189 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2190 oid
, ud
->displaypath
);
2191 /* No "ret" assignment, use "git checkout"'s */
2193 case SM_UPDATE_REBASE
:
2194 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2195 oid
, ud
->displaypath
);
2197 case SM_UPDATE_MERGE
:
2198 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2199 oid
, ud
->displaypath
);
2201 case SM_UPDATE_COMMAND
:
2202 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2203 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2206 BUG("unexpected update strategy type: %d",
2207 ud
->update_strategy
.type
);
2216 switch (ud
->update_strategy
.type
) {
2217 case SM_UPDATE_CHECKOUT
:
2218 printf(_("Submodule path '%s': checked out '%s'\n"),
2219 ud
->displaypath
, oid
);
2221 case SM_UPDATE_REBASE
:
2222 printf(_("Submodule path '%s': rebased into '%s'\n"),
2223 ud
->displaypath
, oid
);
2225 case SM_UPDATE_MERGE
:
2226 printf(_("Submodule path '%s': merged in '%s'\n"),
2227 ud
->displaypath
, oid
);
2229 case SM_UPDATE_COMMAND
:
2230 printf(_("Submodule path '%s': '%s %s'\n"),
2231 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2234 BUG("unexpected update strategy type: %d",
2235 ud
->update_strategy
.type
);
2241 static int run_update_procedure(const struct update_data
*ud
)
2243 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2247 * Run fetch only if `oid` isn't present or it
2248 * is not reachable from a ref.
2250 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2251 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2254 _("Unable to fetch in submodule path '%s'; "
2255 "trying to directly fetch %s:"),
2256 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2258 * Now we tried the usual fetch, but `oid` may
2259 * not be reachable from any of the refs.
2261 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2262 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2263 return die_message(_("Fetched in submodule path '%s', but it did not "
2264 "contain %s. Direct fetching of that commit failed."),
2265 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2268 return run_update_command(ud
, subforce
);
2271 static int remote_submodule_branch(const char *path
, const char **branch
)
2273 const struct submodule
*sub
;
2277 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2279 return die_message(_("could not initialize submodule at path '%s'"),
2282 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2283 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2284 *branch
= sub
->branch
;
2292 if (!strcmp(*branch
, ".")) {
2293 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2296 return die_message(_("No such ref: %s"), "HEAD");
2299 if (!strcmp(refname
, "HEAD"))
2300 return die_message(_("Submodule (%s) branch configured to inherit "
2301 "branch from superproject, but the superproject "
2302 "is not on any branch"), sub
->name
);
2304 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2305 return die_message(_("Expecting a full ref name, got %s"),
2312 /* Our "branch" is coming from repo_config_get_string_tmp() */
2316 static int ensure_core_worktree(const char *path
)
2319 struct repository subrepo
;
2321 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2322 return die_message(_("could not get a repository handle for submodule '%s'"),
2325 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2326 char *cfg_file
, *abs_path
;
2327 const char *rel_path
;
2328 struct strbuf sb
= STRBUF_INIT
;
2330 cfg_file
= repo_git_path(&subrepo
, "config");
2332 abs_path
= absolute_pathdup(path
);
2333 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2335 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2339 strbuf_release(&sb
);
2345 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2348 case SM_UPDATE_CHECKOUT
:
2350 case SM_UPDATE_MERGE
:
2352 case SM_UPDATE_REBASE
:
2354 case SM_UPDATE_UNSPECIFIED
:
2355 case SM_UPDATE_NONE
:
2356 case SM_UPDATE_COMMAND
:
2359 BUG("unreachable with type %d", type
);
2362 static void update_data_to_args(const struct update_data
*update_data
,
2363 struct strvec
*args
)
2365 enum submodule_update_type update_type
= update_data
->update_default
;
2367 if (update_data
->displaypath
) {
2368 strvec_push(args
, "--super-prefix");
2369 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2371 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2372 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2373 if (update_data
->quiet
)
2374 strvec_push(args
, "--quiet");
2375 if (update_data
->force
)
2376 strvec_push(args
, "--force");
2377 if (update_data
->init
)
2378 strvec_push(args
, "--init");
2379 if (update_data
->remote
)
2380 strvec_push(args
, "--remote");
2381 if (update_data
->nofetch
)
2382 strvec_push(args
, "--no-fetch");
2383 if (update_data
->dissociate
)
2384 strvec_push(args
, "--dissociate");
2385 if (update_data
->progress
)
2386 strvec_push(args
, "--progress");
2387 if (update_data
->require_init
)
2388 strvec_push(args
, "--require-init");
2389 if (update_data
->depth
)
2390 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2391 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2392 strvec_pushf(args
, "--%s",
2393 submodule_update_type_to_label(update_type
));
2395 if (update_data
->references
.nr
) {
2396 struct string_list_item
*item
;
2398 for_each_string_list_item(item
, &update_data
->references
)
2399 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2401 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2402 strvec_pushf(args
, "--filter=%s",
2403 expand_list_objects_filter_spec(
2404 update_data
->filter_options
));
2405 if (update_data
->recommend_shallow
== 0)
2406 strvec_push(args
, "--no-recommend-shallow");
2407 else if (update_data
->recommend_shallow
== 1)
2408 strvec_push(args
, "--recommend-shallow");
2409 if (update_data
->single_branch
>= 0)
2410 strvec_push(args
, update_data
->single_branch
?
2412 "--no-single-branch");
2415 static int update_submodule(struct update_data
*update_data
)
2419 ret
= ensure_core_worktree(update_data
->sm_path
);
2423 update_data
->displaypath
= get_submodule_displaypath(
2424 update_data
->sm_path
, update_data
->prefix
);
2426 ret
= determine_submodule_update_strategy(the_repository
,
2427 update_data
->just_cloned
,
2428 update_data
->sm_path
,
2429 update_data
->update_default
,
2430 &update_data
->update_strategy
);
2434 if (update_data
->just_cloned
)
2435 oidcpy(&update_data
->suboid
, null_oid());
2436 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2437 return die_message(_("Unable to find current revision in submodule path '%s'"),
2438 update_data
->displaypath
);
2440 if (update_data
->remote
) {
2446 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2449 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2452 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2454 if (!update_data
->nofetch
) {
2455 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2457 return die_message(_("Unable to fetch in submodule path '%s'"),
2458 update_data
->sm_path
);
2461 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2462 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2463 remote_ref
, update_data
->sm_path
);
2468 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2469 ret
= run_update_procedure(update_data
);
2474 if (update_data
->recursive
) {
2475 struct child_process cp
= CHILD_PROCESS_INIT
;
2476 struct update_data next
= *update_data
;
2479 oidcpy(&next
.oid
, null_oid());
2480 oidcpy(&next
.suboid
, null_oid());
2482 cp
.dir
= update_data
->sm_path
;
2484 prepare_submodule_repo_env(&cp
.env
);
2485 update_data_to_args(&next
, &cp
.args
);
2487 ret
= run_command(&cp
);
2489 die_message(_("Failed to recurse into submodule path '%s'"),
2490 update_data
->displaypath
);
2497 static int update_submodules(struct update_data
*update_data
)
2500 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2502 suc
.update_data
= update_data
;
2503 run_processes_parallel_tr2(suc
.update_data
->max_jobs
, update_clone_get_next_task
,
2504 update_clone_start_failure
,
2505 update_clone_task_finished
, &suc
, "submodule",
2509 * We saved the output and put it out all at once now.
2511 * - the listener does not have to interleave their (checkout)
2512 * work with our fetching. The writes involved in a
2513 * checkout involve more straightforward sequential I/O.
2514 * - the listener can avoid doing any work if fetching failed.
2516 if (suc
.quickstop
) {
2521 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2522 struct update_clone_data ucd
= suc
.update_clone
[i
];
2525 oidcpy(&update_data
->oid
, &ucd
.oid
);
2526 update_data
->just_cloned
= ucd
.just_cloned
;
2527 update_data
->sm_path
= ucd
.sub
->path
;
2529 code
= update_submodule(update_data
);
2538 string_list_clear(&update_data
->references
, 0);
2542 static int module_update(int argc
, const char **argv
, const char *prefix
)
2544 struct pathspec pathspec
;
2545 struct update_data opt
= UPDATE_DATA_INIT
;
2546 struct list_objects_filter_options filter_options
= { 0 };
2548 struct option module_update_options
[] = {
2549 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2550 OPT_BOOL(0, "init", &opt
.init
,
2551 N_("initialize uninitialized submodules before update")),
2552 OPT_BOOL(0, "remote", &opt
.remote
,
2553 N_("use SHA-1 of submodule's remote tracking branch")),
2554 OPT_BOOL(0, "recursive", &opt
.recursive
,
2555 N_("traverse submodules recursively")),
2556 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2557 N_("don't fetch new objects from the remote site")),
2558 OPT_STRING(0, "prefix", &opt
.prefix
,
2560 N_("path into the working tree")),
2561 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2562 N_("use the 'checkout' update strategy (default)"),
2563 SM_UPDATE_CHECKOUT
),
2564 OPT_SET_INT('m', "merge", &opt
.update_default
,
2565 N_("use the 'merge' update strategy"),
2567 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2568 N_("use the 'rebase' update strategy"),
2570 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2571 N_("reference repository")),
2572 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2573 N_("use --reference only while cloning")),
2574 OPT_INTEGER(0, "depth", &opt
.depth
,
2575 N_("create a shallow clone truncated to the "
2576 "specified number of revisions")),
2577 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2578 N_("parallel jobs")),
2579 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2580 N_("whether the initial clone should follow the shallow recommendation")),
2581 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2582 OPT_BOOL(0, "progress", &opt
.progress
,
2583 N_("force cloning progress")),
2584 OPT_BOOL(0, "require-init", &opt
.require_init
,
2585 N_("disallow cloning into non-empty directory, implies --init")),
2586 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2587 N_("clone only one branch, HEAD or --branch")),
2588 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2591 const char *const git_submodule_helper_usage
[] = {
2592 N_("git submodule [--quiet] update"
2593 " [--init [--filter=<filter-spec>]] [--remote]"
2594 " [-N|--no-fetch] [-f|--force]"
2595 " [--checkout|--merge|--rebase]"
2596 " [--[no-]recommend-shallow] [--reference <repository>]"
2597 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2601 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2602 git_config(git_update_clone_config
, &opt
.max_jobs
);
2604 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2605 git_submodule_helper_usage
, 0);
2607 if (opt
.require_init
)
2610 if (filter_options
.choice
&& !opt
.init
) {
2611 usage_with_options(git_submodule_helper_usage
,
2612 module_update_options
);
2615 opt
.filter_options
= &filter_options
;
2617 if (opt
.update_default
)
2618 opt
.update_strategy
.type
= opt
.update_default
;
2620 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2621 list_objects_filter_release(&filter_options
);
2626 opt
.warn_if_uninitialized
= 1;
2629 struct module_list list
= MODULE_LIST_INIT
;
2630 struct init_cb info
= INIT_CB_INIT
;
2632 if (module_list_compute(argc
, argv
, opt
.prefix
,
2633 &pathspec
, &list
) < 0)
2637 * If there are no path args and submodule.active is set then,
2638 * by default, only initialize 'active' modules.
2640 if (!argc
&& git_config_get_value_multi("submodule.active"))
2641 module_list_active(&list
);
2643 info
.prefix
= opt
.prefix
;
2645 info
.flags
|= OPT_QUIET
;
2647 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2650 ret
= update_submodules(&opt
);
2651 list_objects_filter_release(&filter_options
);
2655 static int push_check(int argc
, const char **argv
, const char *prefix
)
2657 struct remote
*remote
;
2658 const char *superproject_head
;
2660 int detached_head
= 0;
2661 struct object_id head_oid
;
2664 die("submodule--helper push-check requires at least 2 arguments");
2667 * superproject's resolved head ref.
2668 * if HEAD then the superproject is in a detached head state, otherwise
2669 * it will be the resolved head ref.
2671 superproject_head
= argv
[1];
2674 /* Get the submodule's head ref and determine if it is detached */
2675 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2677 die(_("Failed to resolve HEAD as a valid ref."));
2678 if (!strcmp(head
, "HEAD"))
2682 * The remote must be configured.
2683 * This is to avoid pushing to the exact same URL as the parent.
2685 remote
= pushremote_get(argv
[1]);
2686 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2687 die("remote '%s' not configured", argv
[1]);
2689 /* Check the refspec */
2692 struct ref
*local_refs
= get_local_heads();
2693 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2695 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2697 for (i
= 0; i
< refspec
.nr
; i
++) {
2698 const struct refspec_item
*rs
= &refspec
.items
[i
];
2700 if (rs
->pattern
|| rs
->matching
)
2703 /* LHS must match a single ref */
2704 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2709 * If LHS matches 'HEAD' then we need to ensure
2710 * that it matches the same named branch
2711 * checked out in the superproject.
2713 if (!strcmp(rs
->src
, "HEAD")) {
2714 if (!detached_head
&&
2715 !strcmp(head
, superproject_head
))
2717 die("HEAD does not match the named branch in the superproject");
2721 die("src refspec '%s' must name a ref",
2725 refspec_clear(&refspec
);
2732 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2735 struct pathspec pathspec
;
2736 struct module_list list
= MODULE_LIST_INIT
;
2737 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
2738 struct option embed_gitdir_options
[] = {
2739 OPT_STRING(0, "prefix", &prefix
,
2741 N_("path into the working tree")),
2742 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
2743 ABSORB_GITDIR_RECURSE_SUBMODULES
),
2746 const char *const git_submodule_helper_usage
[] = {
2747 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2751 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2752 git_submodule_helper_usage
, 0);
2754 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
2757 for (i
= 0; i
< list
.nr
; i
++)
2758 absorb_git_dir_into_superproject(list
.entries
[i
]->name
, flags
);
2763 static int module_config(int argc
, const char **argv
, const char *prefix
)
2766 CHECK_WRITEABLE
= 1,
2769 struct option module_config_options
[] = {
2770 OPT_CMDMODE(0, "check-writeable", &command
,
2771 N_("check if it is safe to write to the .gitmodules file"),
2773 OPT_CMDMODE(0, "unset", &command
,
2774 N_("unset the config in the .gitmodules file"),
2778 const char *const git_submodule_helper_usage
[] = {
2779 N_("git submodule--helper config <name> [<value>]"),
2780 N_("git submodule--helper config --unset <name>"),
2781 "git submodule--helper config --check-writeable",
2785 argc
= parse_options(argc
, argv
, prefix
, module_config_options
,
2786 git_submodule_helper_usage
, PARSE_OPT_KEEP_ARGV0
);
2788 if (argc
== 1 && command
== CHECK_WRITEABLE
)
2789 return is_writing_gitmodules_ok() ? 0 : -1;
2791 /* Equivalent to ACTION_GET in builtin/config.c */
2792 if (argc
== 2 && command
!= DO_UNSET
)
2793 return print_config_from_gitmodules(the_repository
, argv
[1]);
2795 /* Equivalent to ACTION_SET in builtin/config.c */
2796 if (argc
== 3 || (argc
== 2 && command
== DO_UNSET
)) {
2797 const char *value
= (argc
== 3) ? argv
[2] : NULL
;
2799 if (!is_writing_gitmodules_ok())
2800 die(_("please make sure that the .gitmodules file is in the working tree"));
2802 return config_set_in_gitmodules_file_gently(argv
[1], value
);
2805 usage_with_options(git_submodule_helper_usage
, module_config_options
);
2808 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2814 struct option options
[] = {
2815 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2818 const char *const usage
[] = {
2819 N_("git submodule set-url [--quiet] <path> <newurl>"),
2823 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2825 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2826 usage_with_options(usage
, options
);
2828 config_name
= xstrfmt("submodule.%s.url", path
);
2830 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2831 sync_submodule(path
, prefix
, quiet
? OPT_QUIET
: 0);
2838 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2840 int opt_default
= 0, ret
;
2841 const char *opt_branch
= NULL
;
2844 struct option options
[] = {
2846 * We accept the `quiet` option for uniformity across subcommands,
2847 * though there is nothing to make less verbose in this subcommand.
2849 OPT_NOOP_NOARG('q', "quiet"),
2851 OPT_BOOL('d', "default", &opt_default
,
2852 N_("set the default tracking branch to master")),
2853 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2854 N_("set the default tracking branch")),
2857 const char *const usage
[] = {
2858 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2859 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2863 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2865 if (!opt_branch
&& !opt_default
)
2866 die(_("--branch or --default required"));
2868 if (opt_branch
&& opt_default
)
2869 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2871 if (argc
!= 1 || !(path
= argv
[0]))
2872 usage_with_options(usage
, options
);
2874 config_name
= xstrfmt("submodule.%s.branch", path
);
2875 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2881 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
2883 enum branch_track track
;
2884 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
2885 struct option options
[] = {
2886 OPT__QUIET(&quiet
, N_("print only error messages")),
2887 OPT__FORCE(&force
, N_("force creation"), 0),
2888 OPT_BOOL(0, "create-reflog", &reflog
,
2889 N_("create the branch's reflog")),
2890 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
2891 N_("set branch tracking configuration"),
2893 parse_opt_tracking_mode
),
2894 OPT__DRY_RUN(&dry_run
,
2895 N_("show whether the branch would be created")),
2898 const char *const usage
[] = {
2899 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2903 git_config(git_default_config
, NULL
);
2904 track
= git_branch_track
;
2905 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2908 usage_with_options(usage
, options
);
2910 if (!quiet
&& !dry_run
)
2911 printf_ln(_("creating branch '%s'"), argv
[0]);
2913 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
2914 force
, reflog
, quiet
, track
, dry_run
);
2921 const char *reference_path
;
2923 const char *sm_name
;
2925 const char *realrepo
;
2927 unsigned int force
: 1;
2928 unsigned int quiet
: 1;
2929 unsigned int progress
: 1;
2930 unsigned int dissociate
: 1;
2932 #define ADD_DATA_INIT { .depth = -1 }
2934 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
2936 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
2937 struct strbuf sb_remote_out
= STRBUF_INIT
;
2939 cp_remote
.git_cmd
= 1;
2940 strvec_pushf(&cp_remote
.env
,
2941 "GIT_DIR=%s", git_dir_path
);
2942 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
2943 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
2944 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
2946 char *line
= sb_remote_out
.buf
;
2948 while ((next_line
= strchr(line
, '\n')) != NULL
) {
2949 size_t len
= next_line
- line
;
2951 if (strip_suffix_mem(line
, &len
, " (fetch)"))
2952 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
2953 line
= next_line
+ 1;
2957 strbuf_release(&sb_remote_out
);
2960 static int add_submodule(const struct add_data
*add_data
)
2962 char *submod_gitdir_path
;
2963 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
2964 struct string_list reference
= STRING_LIST_INIT_NODUP
;
2966 /* perhaps the path already exists and is already a git repo, else clone it */
2967 if (is_directory(add_data
->sm_path
)) {
2968 struct strbuf sm_path
= STRBUF_INIT
;
2969 strbuf_addstr(&sm_path
, add_data
->sm_path
);
2970 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
2971 if (is_nonbare_repository_dir(&sm_path
))
2972 printf(_("Adding existing repo at '%s' to the index\n"),
2975 die(_("'%s' already exists and is not a valid git repo"),
2977 strbuf_release(&sm_path
);
2978 free(submod_gitdir_path
);
2980 struct child_process cp
= CHILD_PROCESS_INIT
;
2982 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
2984 if (is_directory(submod_gitdir_path
)) {
2985 if (!add_data
->force
) {
2986 struct strbuf msg
= STRBUF_INIT
;
2989 strbuf_addf(&msg
, _("A git directory for '%s' is found "
2990 "locally with remote(s):\n"),
2993 append_fetch_remotes(&msg
, submod_gitdir_path
);
2994 free(submod_gitdir_path
);
2996 strbuf_addf(&msg
, _("If you want to reuse this local git "
2997 "directory instead of cloning again from\n"
2999 "use the '--force' option. If the local git "
3000 "directory is not the correct repo\n"
3001 "or you are unsure what this means choose "
3002 "another name with the '--name' option."),
3003 add_data
->realrepo
);
3005 die_msg
= strbuf_detach(&msg
, NULL
);
3008 printf(_("Reactivating local git directory for "
3009 "submodule '%s'\n"), add_data
->sm_name
);
3012 free(submod_gitdir_path
);
3014 clone_data
.prefix
= add_data
->prefix
;
3015 clone_data
.path
= add_data
->sm_path
;
3016 clone_data
.name
= add_data
->sm_name
;
3017 clone_data
.url
= add_data
->realrepo
;
3018 clone_data
.quiet
= add_data
->quiet
;
3019 clone_data
.progress
= add_data
->progress
;
3020 if (add_data
->reference_path
)
3021 string_list_append(&reference
,
3022 xstrdup(add_data
->reference_path
));
3023 clone_data
.dissociate
= add_data
->dissociate
;
3024 if (add_data
->depth
>= 0)
3025 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3027 if (clone_submodule(&clone_data
, &reference
))
3030 prepare_submodule_repo_env(&cp
.env
);
3032 cp
.dir
= add_data
->sm_path
;
3034 * NOTE: we only get here if add_data->force is true, so
3035 * passing --force to checkout is reasonable.
3037 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3039 if (add_data
->branch
) {
3040 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3041 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3044 if (run_command(&cp
))
3045 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3050 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3055 if (!is_writing_gitmodules_ok())
3056 die(_("please make sure that the .gitmodules file is in the working tree"));
3058 key
= xstrfmt("submodule.%s.%s", name
, var
);
3059 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3065 static void configure_added_submodule(struct add_data
*add_data
)
3069 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3070 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3072 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3073 git_config_set_gently(key
, add_data
->realrepo
);
3076 add_submod
.git_cmd
= 1;
3077 strvec_pushl(&add_submod
.args
, "add",
3078 "--no-warn-embedded-repo", NULL
);
3079 if (add_data
->force
)
3080 strvec_push(&add_submod
.args
, "--force");
3081 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3083 if (run_command(&add_submod
))
3084 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3086 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3087 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3088 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3090 if (add_data
->branch
) {
3091 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3092 "branch", add_data
->branch
))
3093 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3096 add_gitmodules
.git_cmd
= 1;
3097 strvec_pushl(&add_gitmodules
.args
,
3098 "add", "--force", "--", ".gitmodules", NULL
);
3100 if (run_command(&add_gitmodules
))
3101 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3104 * NEEDSWORK: In a multi-working-tree world this needs to be
3105 * set in the per-worktree config.
3108 * NEEDSWORK: In the longer run, we need to get rid of this
3109 * pattern of querying "submodule.active" before calling
3110 * is_submodule_active(), since that function needs to find
3111 * out the value of "submodule.active" again anyway.
3113 if (!git_config_get_string("submodule.active", &val
)) {
3115 * If the submodule being added isn't already covered by the
3116 * current configured pathspec, set the submodule's active flag
3118 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3119 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3120 git_config_set_gently(key
, "true");
3124 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3125 git_config_set_gently(key
, "true");
3130 static void die_on_index_match(const char *path
, int force
)
3133 const char *args
[] = { path
, NULL
};
3134 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3136 if (read_cache_preload(NULL
) < 0)
3137 die(_("index file corrupt"));
3141 char *ps_matched
= xcalloc(ps
.nr
, 1);
3143 /* TODO: audit for interaction with sparse-index. */
3144 ensure_full_index(&the_index
);
3147 * Since there is only one pathspec, we just need
3148 * need to check ps_matched[0] to know if a cache
3151 for (i
= 0; i
< active_nr
; i
++) {
3152 ce_path_match(&the_index
, active_cache
[i
], &ps
,
3155 if (ps_matched
[0]) {
3157 die(_("'%s' already exists in the index"),
3159 if (!S_ISGITLINK(active_cache
[i
]->ce_mode
))
3160 die(_("'%s' already exists in the index "
3161 "and is not a submodule"), path
);
3167 clear_pathspec(&ps
);
3170 static void die_on_repo_without_commits(const char *path
)
3172 struct strbuf sb
= STRBUF_INIT
;
3173 strbuf_addstr(&sb
, path
);
3174 if (is_nonbare_repository_dir(&sb
)) {
3175 struct object_id oid
;
3176 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3177 die(_("'%s' does not have a commit checked out"), path
);
3179 strbuf_release(&sb
);
3182 static int module_add(int argc
, const char **argv
, const char *prefix
)
3184 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3185 struct add_data add_data
= ADD_DATA_INIT
;
3186 char *to_free
= NULL
;
3187 struct option options
[] = {
3188 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3189 N_("branch of repository to add as submodule")),
3190 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3191 PARSE_OPT_NOCOMPLETE
),
3192 OPT__QUIET(&quiet
, N_("print only error messages")),
3193 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3194 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3195 N_("reference repository")),
3196 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3197 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3198 N_("sets the submodule's name to the given string "
3199 "instead of defaulting to its path")),
3200 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3203 const char *const usage
[] = {
3204 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3208 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3210 if (!is_writing_gitmodules_ok())
3211 die(_("please make sure that the .gitmodules file is in the working tree"));
3213 if (prefix
&& *prefix
&&
3214 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3215 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3217 if (argc
== 0 || argc
> 2)
3218 usage_with_options(usage
, options
);
3220 add_data
.repo
= argv
[0];
3222 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3224 add_data
.sm_path
= xstrdup(argv
[1]);
3226 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
))
3227 add_data
.sm_path
= xstrfmt("%s%s", prefix
, add_data
.sm_path
);
3229 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3230 starts_with_dot_slash(add_data
.repo
)) {
3232 die(_("Relative path can only be used from the toplevel "
3233 "of the working tree"));
3235 /* dereference source url relative to parent's url */
3236 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3237 add_data
.realrepo
= to_free
;
3238 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3239 add_data
.realrepo
= add_data
.repo
;
3241 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3247 * multiple //; leading ./; /./; /../;
3249 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3250 strip_dir_trailing_slashes(add_data
.sm_path
);
3252 die_on_index_match(add_data
.sm_path
, force
);
3253 die_on_repo_without_commits(add_data
.sm_path
);
3257 struct strbuf sb
= STRBUF_INIT
;
3258 struct child_process cp
= CHILD_PROCESS_INIT
;
3262 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3263 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3264 if ((exit_code
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3265 strbuf_complete_line(&sb
);
3266 fputs(sb
.buf
, stderr
);
3267 free(add_data
.sm_path
);
3270 strbuf_release(&sb
);
3273 if(!add_data
.sm_name
)
3274 add_data
.sm_name
= add_data
.sm_path
;
3276 if (check_submodule_name(add_data
.sm_name
))
3277 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3279 add_data
.prefix
= prefix
;
3280 add_data
.force
= !!force
;
3281 add_data
.quiet
= !!quiet
;
3282 add_data
.progress
= !!progress
;
3283 add_data
.dissociate
= !!dissociate
;
3285 if (add_submodule(&add_data
)) {
3286 free(add_data
.sm_path
);
3289 configure_added_submodule(&add_data
);
3290 free(add_data
.sm_path
);
3296 #define SUPPORT_SUPER_PREFIX (1<<0)
3300 int (*fn
)(int, const char **, const char *);
3304 static struct cmd_struct commands
[] = {
3305 {"clone", module_clone
, SUPPORT_SUPER_PREFIX
},
3306 {"add", module_add
, 0},
3307 {"update", module_update
, SUPPORT_SUPER_PREFIX
},
3308 {"foreach", module_foreach
, SUPPORT_SUPER_PREFIX
},
3309 {"init", module_init
, 0},
3310 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
3311 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
3312 {"deinit", module_deinit
, 0},
3313 {"summary", module_summary
, 0},
3314 {"push-check", push_check
, 0},
3315 {"absorbgitdirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
3316 {"config", module_config
, 0},
3317 {"set-url", module_set_url
, 0},
3318 {"set-branch", module_set_branch
, 0},
3319 {"create-branch", module_create_branch
, 0},
3322 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3325 if (argc
< 2 || !strcmp(argv
[1], "-h"))
3326 usage("git submodule--helper <command>");
3328 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
3329 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
3330 if (get_super_prefix() &&
3331 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
3332 die(_("%s doesn't support --super-prefix"),
3334 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
3338 die(_("'%s' is not a valid submodule--helper "
3339 "subcommand"), argv
[1]);