1 #define USE_THE_INDEX_VARIABLE
5 #include "environment.h"
8 #include "repository.h"
11 #include "parse-options.h"
16 #include "submodule.h"
17 #include "submodule-config.h"
18 #include "string-list.h"
19 #include "run-command.h"
27 #include "object-store.h"
30 #include "list-objects-filter-options.h"
32 #define OPT_QUIET (1 << 0)
33 #define OPT_CACHED (1 << 1)
34 #define OPT_RECURSIVE (1 << 2)
35 #define OPT_FORCE (1 << 3)
37 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
40 static int repo_get_default_remote(struct repository
*repo
, char **default_remote
)
43 struct strbuf sb
= STRBUF_INIT
;
44 struct ref_store
*store
= get_main_ref_store(repo
);
45 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
49 return die_message(_("No such ref: %s"), "HEAD");
52 if (!strcmp(refname
, "HEAD")) {
53 *default_remote
= xstrdup("origin");
57 if (!skip_prefix(refname
, "refs/heads/", &refname
))
58 return die_message(_("Expecting a full ref name, got %s"),
61 strbuf_addf(&sb
, "branch.%s.remote", refname
);
62 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
63 *default_remote
= xstrdup("origin");
65 *default_remote
= dest
;
71 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
73 struct repository subrepo
;
76 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
78 return die_message(_("could not get a repository handle for submodule '%s'"),
80 ret
= repo_get_default_remote(&subrepo
, default_remote
);
86 static char *get_default_remote(void)
89 int code
= repo_get_default_remote(the_repository
, &default_remote
);
94 return default_remote
;
97 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
99 char *remoteurl
, *resolved_url
;
100 char *remote
= get_default_remote();
101 struct strbuf remotesb
= STRBUF_INIT
;
103 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
104 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
106 warning(_("could not look up configuration '%s'. "
107 "Assuming this repository is its own "
108 "authoritative upstream."),
110 remoteurl
= xgetcwd();
112 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
116 strbuf_release(&remotesb
);
121 /* the result should be freed by the caller. */
122 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
123 const char *super_prefix
)
125 if (prefix
&& super_prefix
) {
126 BUG("cannot have prefix '%s' and superprefix '%s'",
127 prefix
, super_prefix
);
129 struct strbuf sb
= STRBUF_INIT
;
130 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
133 } else if (super_prefix
) {
134 return xstrfmt("%s%s", super_prefix
, path
);
136 return xstrdup(path
);
140 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
142 struct strbuf sb
= STRBUF_INIT
;
145 static const char *describe_bare
[] = { NULL
};
147 static const char *describe_tags
[] = { "--tags", NULL
};
149 static const char *describe_contains
[] = { "--contains", NULL
};
151 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
153 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
155 describe_all_always
, NULL
};
157 for (d
= describe_argv
; *d
; d
++) {
158 struct child_process cp
= CHILD_PROCESS_INIT
;
159 prepare_submodule_repo_env(&cp
.env
);
164 strvec_push(&cp
.args
, "describe");
165 strvec_pushv(&cp
.args
, *d
);
166 strvec_push(&cp
.args
, object_id
);
168 if (!capture_command(&cp
, &sb
, 0)) {
169 strbuf_strip_suffix(&sb
, "\n");
170 return strbuf_detach(&sb
, NULL
);
179 const struct cache_entry
**entries
;
182 #define MODULE_LIST_INIT { 0 }
184 static void module_list_release(struct module_list
*ml
)
189 static int module_list_compute(const char **argv
,
191 struct pathspec
*pathspec
,
192 struct module_list
*list
)
195 char *ps_matched
= NULL
;
197 parse_pathspec(pathspec
, 0,
198 PATHSPEC_PREFER_FULL
,
202 ps_matched
= xcalloc(pathspec
->nr
, 1);
204 if (repo_read_index(the_repository
) < 0)
205 die(_("index file corrupt"));
207 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
208 const struct cache_entry
*ce
= the_index
.cache
[i
];
210 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
212 !S_ISGITLINK(ce
->ce_mode
))
215 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
216 list
->entries
[list
->nr
++] = ce
;
217 while (i
+ 1 < the_index
.cache_nr
&&
218 !strcmp(ce
->name
, the_index
.cache
[i
+ 1]->name
))
220 * Skip entries with the same name in different stages
221 * to make sure an entry is returned only once.
226 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
234 static void module_list_active(struct module_list
*list
)
237 struct module_list active_modules
= MODULE_LIST_INIT
;
239 for (i
= 0; i
< list
->nr
; i
++) {
240 const struct cache_entry
*ce
= list
->entries
[i
];
242 if (!is_submodule_active(the_repository
, ce
->name
))
245 ALLOC_GROW(active_modules
.entries
,
246 active_modules
.nr
+ 1,
247 active_modules
.alloc
);
248 active_modules
.entries
[active_modules
.nr
++] = ce
;
251 module_list_release(list
);
252 *list
= active_modules
;
255 static char *get_up_path(const char *path
)
258 struct strbuf sb
= STRBUF_INIT
;
260 for (i
= count_slashes(path
); i
; i
--)
261 strbuf_addstr(&sb
, "../");
264 * Check if 'path' ends with slash or not
265 * for having the same output for dir/sub_dir
268 if (!is_dir_sep(path
[strlen(path
) - 1]))
269 strbuf_addstr(&sb
, "../");
271 return strbuf_detach(&sb
, NULL
);
274 static void for_each_listed_submodule(const struct module_list
*list
,
275 each_submodule_fn fn
, void *cb_data
)
279 for (i
= 0; i
< list
->nr
; i
++)
280 fn(list
->entries
[i
], cb_data
);
287 const char *super_prefix
;
291 #define FOREACH_CB_INIT { 0 }
293 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
296 struct foreach_cb
*info
= cb_data
;
297 const char *path
= list_item
->name
;
298 const struct object_id
*ce_oid
= &list_item
->oid
;
299 const struct submodule
*sub
;
300 struct child_process cp
= CHILD_PROCESS_INIT
;
303 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
306 sub
= submodule_from_path(the_repository
, null_oid(), path
);
309 die(_("No url found for submodule path '%s' in .gitmodules"),
312 if (!is_submodule_populated_gently(path
, NULL
))
315 prepare_submodule_repo_env(&cp
.env
);
318 * For the purpose of executing <command> in the submodule,
319 * separate shell is used for the purpose of running the
326 * NEEDSWORK: the command currently has access to the variables $name,
327 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
328 * contains a single argument. This is done for maintaining a faithful
329 * translation from shell script.
331 if (info
->argc
== 1) {
332 char *toplevel
= xgetcwd();
333 struct strbuf sb
= STRBUF_INIT
;
335 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
336 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
337 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
338 strvec_pushf(&cp
.env
, "sha1=%s",
340 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
343 * Since the path variable was accessible from the script
344 * before porting, it is also made available after porting.
345 * The environment variable "PATH" has a very special purpose
346 * on windows. And since environment variables are
347 * case-insensitive in windows, it interferes with the
348 * existing PATH variable. Hence, to avoid that, we expose
349 * path via the args strvec and not via env.
351 sq_quote_buf(&sb
, path
);
352 strvec_pushf(&cp
.args
, "path=%s; %s",
353 sb
.buf
, info
->argv
[0]);
357 strvec_pushv(&cp
.args
, info
->argv
);
361 printf(_("Entering '%s'\n"), displaypath
);
363 if (info
->argv
[0] && run_command(&cp
))
364 die(_("run_command returned non-zero status for %s\n."),
367 if (info
->recursive
) {
368 struct child_process cpr
= CHILD_PROCESS_INIT
;
372 prepare_submodule_repo_env(&cpr
.env
);
374 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
376 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
377 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
380 strvec_push(&cpr
.args
, "--quiet");
382 strvec_push(&cpr
.args
, "--");
383 strvec_pushv(&cpr
.args
, info
->argv
);
385 if (run_command(&cpr
))
386 die(_("run_command returned non-zero status while "
387 "recursing in the nested submodules of %s\n."),
395 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
397 struct foreach_cb info
= FOREACH_CB_INIT
;
398 struct pathspec pathspec
= { 0 };
399 struct module_list list
= MODULE_LIST_INIT
;
400 struct option module_foreach_options
[] = {
401 OPT__SUPER_PREFIX(&info
.super_prefix
),
402 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
403 OPT_BOOL(0, "recursive", &info
.recursive
,
404 N_("recurse into nested submodules")),
407 const char *const git_submodule_helper_usage
[] = {
408 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
413 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
414 git_submodule_helper_usage
, 0);
416 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
421 info
.prefix
= prefix
;
423 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
427 module_list_release(&list
);
428 clear_pathspec(&pathspec
);
432 static int starts_with_dot_slash(const char *const path
)
434 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
435 PATH_MATCH_XPLATFORM
);
438 static int starts_with_dot_dot_slash(const char *const path
)
440 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
441 PATH_MATCH_XPLATFORM
);
446 const char *super_prefix
;
449 #define INIT_CB_INIT { 0 }
451 static void init_submodule(const char *path
, const char *prefix
,
452 const char *super_prefix
,
455 const struct submodule
*sub
;
456 struct strbuf sb
= STRBUF_INIT
;
458 char *url
= NULL
, *displaypath
;
460 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
462 sub
= submodule_from_path(the_repository
, null_oid(), path
);
465 die(_("No url found for submodule path '%s' in .gitmodules"),
469 * NEEDSWORK: In a multi-working-tree world, this needs to be
470 * set in the per-worktree config.
472 * Set active flag for the submodule being initialized
474 if (!is_submodule_active(the_repository
, path
)) {
475 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
476 git_config_set_gently(sb
.buf
, "true");
481 * Copy url setting when it is not set yet.
482 * To look up the url in .git/config, we must not fall back to
483 * .gitmodules, so look it up directly.
485 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
486 if (git_config_get_string(sb
.buf
, &url
)) {
488 die(_("No url found for submodule path '%s' in .gitmodules"),
491 url
= xstrdup(sub
->url
);
493 /* Possibly a url relative to parent */
494 if (starts_with_dot_dot_slash(url
) ||
495 starts_with_dot_slash(url
)) {
498 url
= resolve_relative_url(oldurl
, NULL
, 0);
502 if (git_config_set_gently(sb
.buf
, url
))
503 die(_("Failed to register url for submodule path '%s'"),
505 if (!(flags
& OPT_QUIET
))
507 _("Submodule '%s' (%s) registered for path '%s'\n"),
508 sub
->name
, url
, displaypath
);
512 /* Copy "update" setting when it is not set yet */
513 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
514 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
515 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
516 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
517 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
521 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
524 if (git_config_set_gently(sb
.buf
, upd
))
525 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
532 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
534 struct init_cb
*info
= cb_data
;
536 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
540 static int module_init(int argc
, const char **argv
, const char *prefix
)
542 struct init_cb info
= INIT_CB_INIT
;
543 struct pathspec pathspec
= { 0 };
544 struct module_list list
= MODULE_LIST_INIT
;
546 struct option module_init_options
[] = {
547 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
550 const char *const git_submodule_helper_usage
[] = {
551 N_("git submodule init [<options>] [<path>]"),
556 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
557 git_submodule_helper_usage
, 0);
559 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
563 * If there are no path args and submodule.active is set then,
564 * by default, only initialize 'active' modules.
566 if (!argc
&& git_config_get_value_multi("submodule.active"))
567 module_list_active(&list
);
569 info
.prefix
= prefix
;
571 info
.flags
|= OPT_QUIET
;
573 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
577 module_list_release(&list
);
578 clear_pathspec(&pathspec
);
584 const char *super_prefix
;
587 #define STATUS_CB_INIT { 0 }
589 static void print_status(unsigned int flags
, char state
, const char *path
,
590 const struct object_id
*oid
, const char *displaypath
)
592 if (flags
& OPT_QUIET
)
595 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
597 if (state
== ' ' || state
== '+') {
598 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
601 printf(" (%s)", name
);
608 static int handle_submodule_head_ref(const char *refname UNUSED
,
609 const struct object_id
*oid
,
613 struct object_id
*output
= cb_data
;
621 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
622 unsigned int ce_flags
, const char *prefix
,
623 const char *super_prefix
, unsigned int flags
)
626 struct strvec diff_files_args
= STRVEC_INIT
;
627 struct rev_info rev
= REV_INFO_INIT
;
628 int diff_files_result
;
629 struct strbuf buf
= STRBUF_INIT
;
631 struct setup_revision_opt opt
= {
632 .free_removed_argv_elements
= 1,
635 if (!submodule_from_path(the_repository
, null_oid(), path
))
636 die(_("no submodule mapping found in .gitmodules for path '%s'"),
639 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
641 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
642 print_status(flags
, 'U', path
, null_oid(), displaypath
);
646 strbuf_addf(&buf
, "%s/.git", path
);
647 git_dir
= read_gitfile(buf
.buf
);
651 if (!is_submodule_active(the_repository
, path
) ||
652 !is_git_directory(git_dir
)) {
653 print_status(flags
, '-', path
, ce_oid
, displaypath
);
654 strbuf_release(&buf
);
657 strbuf_release(&buf
);
659 strvec_pushl(&diff_files_args
, "diff-files",
660 "--ignore-submodules=dirty", "--quiet", "--",
663 git_config(git_diff_basic_config
, NULL
);
665 repo_init_revisions(the_repository
, &rev
, NULL
);
667 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
668 diff_files_result
= run_diff_files(&rev
, 0);
670 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
671 print_status(flags
, ' ', path
, ce_oid
,
673 } else if (!(flags
& OPT_CACHED
)) {
674 struct object_id oid
;
675 struct ref_store
*refs
= get_submodule_ref_store(path
);
678 print_status(flags
, '-', path
, ce_oid
, displaypath
);
681 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
682 die(_("could not resolve HEAD ref inside the "
683 "submodule '%s'"), path
);
685 print_status(flags
, '+', path
, &oid
, displaypath
);
687 print_status(flags
, '+', path
, ce_oid
, displaypath
);
690 if (flags
& OPT_RECURSIVE
) {
691 struct child_process cpr
= CHILD_PROCESS_INIT
;
695 prepare_submodule_repo_env(&cpr
.env
);
697 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
698 "--recursive", NULL
);
699 strvec_push(&cpr
.args
, "--super-prefix");
700 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
702 if (flags
& OPT_CACHED
)
703 strvec_push(&cpr
.args
, "--cached");
705 if (flags
& OPT_QUIET
)
706 strvec_push(&cpr
.args
, "--quiet");
708 if (run_command(&cpr
))
709 die(_("failed to recurse into submodule '%s'"), path
);
713 strvec_clear(&diff_files_args
);
715 release_revisions(&rev
);
718 static void status_submodule_cb(const struct cache_entry
*list_item
,
721 struct status_cb
*info
= cb_data
;
723 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
724 info
->prefix
, info
->super_prefix
, info
->flags
);
727 static int module_status(int argc
, const char **argv
, const char *prefix
)
729 struct status_cb info
= STATUS_CB_INIT
;
730 struct pathspec pathspec
= { 0 };
731 struct module_list list
= MODULE_LIST_INIT
;
733 struct option module_status_options
[] = {
734 OPT__SUPER_PREFIX(&info
.super_prefix
),
735 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
736 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
737 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
740 const char *const git_submodule_helper_usage
[] = {
741 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
746 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
747 git_submodule_helper_usage
, 0);
749 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
752 info
.prefix
= prefix
;
754 info
.flags
|= OPT_QUIET
;
756 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
760 module_list_release(&list
);
761 clear_pathspec(&pathspec
);
766 unsigned int mod_src
;
767 unsigned int mod_dst
;
768 struct object_id oid_src
;
769 struct object_id oid_dst
;
773 #define MODULE_CB_INIT { 0 }
775 static void module_cb_release(struct module_cb
*mcb
)
780 struct module_cb_list
{
781 struct module_cb
**entries
;
784 #define MODULE_CB_LIST_INIT { 0 }
786 static void module_cb_list_release(struct module_cb_list
*mcbl
)
790 for (i
= 0; i
< mcbl
->nr
; i
++) {
791 struct module_cb
*mcb
= mcbl
->entries
[i
];
793 module_cb_release(mcb
);
803 const char *super_prefix
;
804 unsigned int cached
: 1;
805 unsigned int for_status
: 1;
806 unsigned int files
: 1;
809 #define SUMMARY_CB_INIT { 0 }
816 static char *verify_submodule_committish(const char *sm_path
,
817 const char *committish
)
819 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
820 struct strbuf result
= STRBUF_INIT
;
822 cp_rev_parse
.git_cmd
= 1;
823 cp_rev_parse
.dir
= sm_path
;
824 prepare_submodule_repo_env(&cp_rev_parse
.env
);
825 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
826 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
827 strvec_push(&cp_rev_parse
.args
, "--");
829 if (capture_command(&cp_rev_parse
, &result
, 0))
832 strbuf_trim_trailing_newline(&result
);
833 return strbuf_detach(&result
, NULL
);
836 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
837 int total_commits
, const char *displaypath
,
838 const char *src_abbrev
, const char *dst_abbrev
,
841 if (p
->status
== 'T') {
842 if (S_ISGITLINK(p
->mod_dst
))
843 printf(_("* %s %s(blob)->%s(submodule)"),
844 displaypath
, src_abbrev
, dst_abbrev
);
846 printf(_("* %s %s(submodule)->%s(blob)"),
847 displaypath
, src_abbrev
, dst_abbrev
);
849 printf("* %s %s...%s",
850 displaypath
, src_abbrev
, dst_abbrev
);
853 if (total_commits
< 0)
856 printf(" (%d):\n", total_commits
);
859 printf(_("%s"), errmsg
);
860 } else if (total_commits
> 0) {
861 struct child_process cp_log
= CHILD_PROCESS_INIT
;
864 cp_log
.dir
= p
->sm_path
;
865 prepare_submodule_repo_env(&cp_log
.env
);
866 strvec_pushl(&cp_log
.args
, "log", NULL
);
868 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
869 if (info
->summary_limit
> 0)
870 strvec_pushf(&cp_log
.args
, "-%d",
871 info
->summary_limit
);
873 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
874 "--first-parent", NULL
);
875 strvec_pushf(&cp_log
.args
, "%s...%s",
876 src_abbrev
, dst_abbrev
);
877 } else if (S_ISGITLINK(p
->mod_dst
)) {
878 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
879 "-1", dst_abbrev
, NULL
);
881 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
882 "-1", src_abbrev
, NULL
);
884 run_command(&cp_log
);
889 static void generate_submodule_summary(struct summary_cb
*info
,
892 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
893 int missing_src
= 0, missing_dst
= 0;
894 struct strbuf errmsg
= STRBUF_INIT
;
895 int total_commits
= -1;
897 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
898 if (S_ISGITLINK(p
->mod_dst
)) {
899 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
902 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
903 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
905 int fd
= open(p
->sm_path
, O_RDONLY
);
907 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
908 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
910 error(_("couldn't hash object from '%s'"), p
->sm_path
);
912 /* for a submodule removal (mode:0000000), don't warn */
914 warning(_("unexpected mode %o\n"), p
->mod_dst
);
918 if (S_ISGITLINK(p
->mod_src
)) {
919 if (p
->status
!= 'D')
920 src_abbrev
= verify_submodule_committish(p
->sm_path
,
921 oid_to_hex(&p
->oid_src
));
925 * As `rev-parse` failed, we fallback to getting
926 * the abbreviated hash using oid_src. We do
927 * this as we might still need the abbreviated
928 * hash in cases like a submodule type change, etc.
930 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
934 * The source does not point to a submodule.
935 * So, we fallback to getting the abbreviation using
936 * oid_src as we might still need the abbreviated
937 * hash in cases like submodule add, etc.
939 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
942 if (S_ISGITLINK(p
->mod_dst
)) {
943 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
944 oid_to_hex(&p
->oid_dst
));
948 * As `rev-parse` failed, we fallback to getting
949 * the abbreviated hash using oid_dst. We do
950 * this as we might still need the abbreviated
951 * hash in cases like a submodule type change, etc.
953 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
957 * The destination does not point to a submodule.
958 * So, we fallback to getting the abbreviation using
959 * oid_dst as we might still need the abbreviated
960 * hash in cases like a submodule removal, etc.
962 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
965 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
968 if (!missing_src
&& !missing_dst
) {
969 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
970 struct strbuf sb_rev_list
= STRBUF_INIT
;
972 strvec_pushl(&cp_rev_list
.args
, "rev-list",
973 "--first-parent", "--count", NULL
);
974 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
975 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
976 src_abbrev
, dst_abbrev
);
978 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
979 src_abbrev
: dst_abbrev
);
980 strvec_push(&cp_rev_list
.args
, "--");
982 cp_rev_list
.git_cmd
= 1;
983 cp_rev_list
.dir
= p
->sm_path
;
984 prepare_submodule_repo_env(&cp_rev_list
.env
);
986 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
987 total_commits
= atoi(sb_rev_list
.buf
);
989 strbuf_release(&sb_rev_list
);
992 * Don't give error msg for modification whose dst is not
993 * submodule, i.e., deleted or changed to blob
995 if (S_ISGITLINK(p
->mod_dst
)) {
996 if (missing_src
&& missing_dst
) {
997 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
998 displaypath
, oid_to_hex(&p
->oid_src
),
999 oid_to_hex(&p
->oid_dst
));
1001 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
1002 displaypath
, missing_src
?
1003 oid_to_hex(&p
->oid_src
) :
1004 oid_to_hex(&p
->oid_dst
));
1009 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1010 total_commits
, displaypath
, src_abbrev
,
1016 strbuf_release(&errmsg
);
1019 static void prepare_submodule_summary(struct summary_cb
*info
,
1020 struct module_cb_list
*list
)
1023 for (i
= 0; i
< list
->nr
; i
++) {
1024 const struct submodule
*sub
;
1025 struct module_cb
*p
= list
->entries
[i
];
1026 struct strbuf sm_gitdir
= STRBUF_INIT
;
1028 if (p
->status
== 'D' || p
->status
== 'T') {
1029 generate_submodule_summary(info
, p
);
1033 if (info
->for_status
&& p
->status
!= 'A' &&
1034 (sub
= submodule_from_path(the_repository
,
1035 null_oid(), p
->sm_path
))) {
1036 char *config_key
= NULL
;
1040 config_key
= xstrfmt("submodule.%s.ignore",
1042 if (!git_config_get_string_tmp(config_key
, &value
))
1043 ignore_all
= !strcmp(value
, "all");
1044 else if (sub
->ignore
)
1045 ignore_all
= !strcmp(sub
->ignore
, "all");
1052 /* Also show added or modified modules which are checked out */
1053 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1054 if (is_nonbare_repository_dir(&sm_gitdir
))
1055 generate_submodule_summary(info
, p
);
1056 strbuf_release(&sm_gitdir
);
1060 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1061 struct diff_options
*options UNUSED
,
1065 struct module_cb_list
*list
= data
;
1066 for (i
= 0; i
< q
->nr
; i
++) {
1067 struct diff_filepair
*p
= q
->queue
[i
];
1068 struct module_cb
*temp
;
1070 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1072 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1073 temp
->mod_src
= p
->one
->mode
;
1074 temp
->mod_dst
= p
->two
->mode
;
1075 temp
->oid_src
= p
->one
->oid
;
1076 temp
->oid_dst
= p
->two
->oid
;
1077 temp
->status
= p
->status
;
1078 temp
->sm_path
= xstrdup(p
->one
->path
);
1080 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1081 list
->entries
[list
->nr
++] = temp
;
1085 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1088 case DIFF_INDEX
: return "diff-index";
1089 case DIFF_FILES
: return "diff-files";
1090 default: BUG("bad diff_cmd value %d", diff_cmd
);
1094 static int compute_summary_module_list(struct object_id
*head_oid
,
1095 struct summary_cb
*info
,
1096 enum diff_cmd diff_cmd
)
1098 struct strvec diff_args
= STRVEC_INIT
;
1099 struct rev_info rev
;
1100 struct setup_revision_opt opt
= {
1101 .free_removed_argv_elements
= 1,
1103 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1106 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1108 strvec_push(&diff_args
, "--cached");
1109 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1111 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1112 strvec_push(&diff_args
, "--");
1114 strvec_pushv(&diff_args
, info
->argv
);
1116 git_config(git_diff_basic_config
, NULL
);
1117 init_revisions(&rev
, info
->prefix
);
1119 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1120 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1121 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1122 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1123 rev
.diffopt
.format_callback_data
= &list
;
1125 if (!info
->cached
) {
1126 if (diff_cmd
== DIFF_INDEX
)
1128 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1129 perror("repo_read_index_preload");
1133 } else if (repo_read_index(the_repository
) < 0) {
1134 perror("repo_read_cache");
1139 if (diff_cmd
== DIFF_INDEX
)
1140 run_diff_index(&rev
, info
->cached
);
1142 run_diff_files(&rev
, 0);
1143 prepare_submodule_summary(info
, &list
);
1145 strvec_clear(&diff_args
);
1146 release_revisions(&rev
);
1147 module_cb_list_release(&list
);
1151 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1153 struct summary_cb info
= SUMMARY_CB_INIT
;
1157 int summary_limit
= -1;
1158 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1159 struct object_id head_oid
;
1161 struct option module_summary_options
[] = {
1162 OPT_BOOL(0, "cached", &cached
,
1163 N_("use the commit stored in the index instead of the submodule HEAD")),
1164 OPT_BOOL(0, "files", &files
,
1165 N_("compare the commit in the index with that in the submodule HEAD")),
1166 OPT_BOOL(0, "for-status", &for_status
,
1167 N_("skip submodules with 'ignore_config' value set to 'all'")),
1168 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1169 N_("limit the summary size")),
1172 const char *const git_submodule_helper_usage
[] = {
1173 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1177 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1178 git_submodule_helper_usage
, 0);
1183 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1188 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1189 /* before the first commit: compare with an empty tree */
1190 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1196 if (get_oid("HEAD", &head_oid
))
1197 die(_("could not fetch a revision for HEAD"));
1202 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1203 diff_cmd
= DIFF_FILES
;
1208 info
.prefix
= prefix
;
1209 info
.cached
= !!cached
;
1210 info
.files
= !!files
;
1211 info
.for_status
= !!for_status
;
1212 info
.summary_limit
= summary_limit
;
1214 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1221 const char *super_prefix
;
1224 #define SYNC_CB_INIT { 0 }
1226 static void sync_submodule(const char *path
, const char *prefix
,
1227 const char *super_prefix
, unsigned int flags
)
1229 const struct submodule
*sub
;
1230 char *remote_key
= NULL
;
1231 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1232 struct strbuf sb
= STRBUF_INIT
;
1233 char *sub_config_path
= NULL
;
1236 if (!is_submodule_active(the_repository
, path
))
1239 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1241 if (sub
&& sub
->url
) {
1242 if (starts_with_dot_dot_slash(sub
->url
) ||
1243 starts_with_dot_slash(sub
->url
)) {
1244 char *up_path
= get_up_path(path
);
1246 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1247 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1250 sub_origin_url
= xstrdup(sub
->url
);
1251 super_config_url
= xstrdup(sub
->url
);
1254 sub_origin_url
= xstrdup("");
1255 super_config_url
= xstrdup("");
1258 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1260 if (!(flags
& OPT_QUIET
))
1261 printf(_("Synchronizing submodule url for '%s'\n"),
1265 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1266 if (git_config_set_gently(sb
.buf
, super_config_url
))
1267 die(_("failed to register url for submodule path '%s'"),
1270 if (!is_submodule_populated_gently(path
, NULL
))
1274 code
= get_default_remote_submodule(path
, &default_remote
);
1278 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1279 free(default_remote
);
1281 submodule_to_gitdir(&sb
, path
);
1282 strbuf_addstr(&sb
, "/config");
1284 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1285 die(_("failed to update remote for submodule '%s'"),
1288 if (flags
& OPT_RECURSIVE
) {
1289 struct child_process cpr
= CHILD_PROCESS_INIT
;
1293 prepare_submodule_repo_env(&cpr
.env
);
1295 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1296 "--recursive", NULL
);
1297 strvec_push(&cpr
.args
, "--super-prefix");
1298 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1301 if (flags
& OPT_QUIET
)
1302 strvec_push(&cpr
.args
, "--quiet");
1304 if (run_command(&cpr
))
1305 die(_("failed to recurse into submodule '%s'"),
1310 free(super_config_url
);
1311 free(sub_origin_url
);
1312 strbuf_release(&sb
);
1315 free(sub_config_path
);
1318 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1320 struct sync_cb
*info
= cb_data
;
1322 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1326 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1328 struct sync_cb info
= SYNC_CB_INIT
;
1329 struct pathspec pathspec
= { 0 };
1330 struct module_list list
= MODULE_LIST_INIT
;
1333 struct option module_sync_options
[] = {
1334 OPT__SUPER_PREFIX(&info
.super_prefix
),
1335 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1336 OPT_BOOL(0, "recursive", &recursive
,
1337 N_("recurse into nested submodules")),
1340 const char *const git_submodule_helper_usage
[] = {
1341 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1346 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1347 git_submodule_helper_usage
, 0);
1349 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1352 info
.prefix
= prefix
;
1354 info
.flags
|= OPT_QUIET
;
1356 info
.flags
|= OPT_RECURSIVE
;
1358 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1362 module_list_release(&list
);
1363 clear_pathspec(&pathspec
);
1371 #define DEINIT_CB_INIT { 0 }
1373 static void deinit_submodule(const char *path
, const char *prefix
,
1376 const struct submodule
*sub
;
1377 char *displaypath
= NULL
;
1378 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1379 struct strbuf sb_config
= STRBUF_INIT
;
1380 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1382 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1384 if (!sub
|| !sub
->name
)
1387 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1389 /* remove the submodule work tree (unless the user already did it) */
1390 if (is_directory(path
)) {
1391 struct strbuf sb_rm
= STRBUF_INIT
;
1394 if (is_directory(sub_git_dir
)) {
1395 if (!(flags
& OPT_QUIET
))
1396 warning(_("Submodule work tree '%s' contains a .git "
1397 "directory. This will be replaced with a "
1398 ".git file by using absorbgitdirs."),
1401 absorb_git_dir_into_superproject(path
, NULL
);
1405 if (!(flags
& OPT_FORCE
)) {
1406 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1409 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1412 if (run_command(&cp_rm
))
1413 die(_("Submodule work tree '%s' contains local "
1414 "modifications; use '-f' to discard them"),
1418 strbuf_addstr(&sb_rm
, path
);
1420 if (!remove_dir_recursively(&sb_rm
, 0))
1421 format
= _("Cleared directory '%s'\n");
1423 format
= _("Could not remove submodule work tree '%s'\n");
1425 if (!(flags
& OPT_QUIET
))
1426 printf(format
, displaypath
);
1428 submodule_unset_core_worktree(sub
);
1430 strbuf_release(&sb_rm
);
1433 if (mkdir(path
, 0777))
1434 printf(_("could not create empty submodule directory %s"),
1437 cp_config
.git_cmd
= 1;
1438 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1439 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1441 /* remove the .git/config entries (unless the user already did it) */
1442 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1443 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1446 * remove the whole section so we have a clean state when
1447 * the user later decides to init this submodule again
1449 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1450 if (!(flags
& OPT_QUIET
))
1451 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1452 sub
->name
, sub
->url
, displaypath
);
1459 strbuf_release(&sb_config
);
1462 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1465 struct deinit_cb
*info
= cb_data
;
1466 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1469 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1471 struct deinit_cb info
= DEINIT_CB_INIT
;
1472 struct pathspec pathspec
= { 0 };
1473 struct module_list list
= MODULE_LIST_INIT
;
1477 struct option module_deinit_options
[] = {
1478 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1479 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1480 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1483 const char *const git_submodule_helper_usage
[] = {
1484 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1489 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1490 git_submodule_helper_usage
, 0);
1493 error("pathspec and --all are incompatible");
1494 usage_with_options(git_submodule_helper_usage
,
1495 module_deinit_options
);
1499 die(_("Use '--all' if you really want to deinitialize all submodules"));
1501 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1504 info
.prefix
= prefix
;
1506 info
.flags
|= OPT_QUIET
;
1508 info
.flags
|= OPT_FORCE
;
1510 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1514 module_list_release(&list
);
1515 clear_pathspec(&pathspec
);
1519 struct module_clone_data
{
1525 struct list_objects_filter_options
*filter_options
;
1526 unsigned int quiet
: 1;
1527 unsigned int progress
: 1;
1528 unsigned int dissociate
: 1;
1529 unsigned int require_init
: 1;
1532 #define MODULE_CLONE_DATA_INIT { \
1533 .single_branch = -1, \
1536 struct submodule_alternate_setup
{
1537 const char *submodule_name
;
1538 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1539 SUBMODULE_ALTERNATE_ERROR_DIE
,
1540 SUBMODULE_ALTERNATE_ERROR_INFO
,
1541 SUBMODULE_ALTERNATE_ERROR_IGNORE
1543 struct string_list
*reference
;
1545 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1546 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1549 static const char alternate_error_advice
[] = N_(
1550 "An alternate computed from a superproject's alternate is invalid.\n"
1551 "To allow Git to clone without an alternate in such a case, set\n"
1552 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1553 "'--reference-if-able' instead of '--reference'."
1556 static int add_possible_reference_from_superproject(
1557 struct object_directory
*odb
, void *sas_cb
)
1559 struct submodule_alternate_setup
*sas
= sas_cb
;
1563 * If the alternate object store is another repository, try the
1564 * standard layout with .git/(modules/<name>)+/objects
1566 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1567 struct repository alternate
;
1569 struct strbuf sb
= STRBUF_INIT
;
1570 struct strbuf err
= STRBUF_INIT
;
1571 strbuf_add(&sb
, odb
->path
, len
);
1573 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1574 die(_("could not get a repository handle for gitdir '%s'"),
1578 * We need to end the new path with '/' to mark it as a dir,
1579 * otherwise a submodule name containing '/' will be broken
1580 * as the last part of a missing submodule reference would
1581 * be taken as a file name.
1584 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1585 strbuf_addch(&sb
, '/');
1586 repo_clear(&alternate
);
1588 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1590 char *p
= strbuf_detach(&sb
, NULL
);
1592 string_list_append(sas
->reference
, p
)->util
= p
;
1595 switch (sas
->error_mode
) {
1596 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1597 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1598 advise(_(alternate_error_advice
));
1599 die(_("submodule '%s' cannot add alternate: %s"),
1600 sas
->submodule_name
, err
.buf
);
1601 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1602 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1603 sas
->submodule_name
, err
.buf
);
1604 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1608 strbuf_release(&sb
);
1614 static void prepare_possible_alternates(const char *sm_name
,
1615 struct string_list
*reference
)
1617 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1618 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1620 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1624 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1626 if (!error_strategy
)
1627 error_strategy
= xstrdup("die");
1629 sas
.submodule_name
= sm_name
;
1630 sas
.reference
= reference
;
1631 if (!strcmp(error_strategy
, "die"))
1632 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1633 else if (!strcmp(error_strategy
, "info"))
1634 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1635 else if (!strcmp(error_strategy
, "ignore"))
1636 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1638 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1640 if (!strcmp(sm_alternate
, "superproject"))
1641 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1642 else if (!strcmp(sm_alternate
, "no"))
1645 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1648 free(error_strategy
);
1651 static char *clone_submodule_sm_gitdir(const char *name
)
1653 struct strbuf sb
= STRBUF_INIT
;
1656 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1657 sm_gitdir
= absolute_pathdup(sb
.buf
);
1658 strbuf_release(&sb
);
1663 static int clone_submodule(const struct module_clone_data
*clone_data
,
1664 struct string_list
*reference
)
1667 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1668 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1669 struct child_process cp
= CHILD_PROCESS_INIT
;
1670 const char *clone_data_path
= clone_data
->path
;
1671 char *to_free
= NULL
;
1673 if (!is_absolute_path(clone_data
->path
))
1674 clone_data_path
= to_free
= xstrfmt("%s/%s", get_git_work_tree(),
1677 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1678 die(_("refusing to create/use '%s' in another submodule's "
1679 "git dir"), sm_gitdir
);
1681 if (!file_exists(sm_gitdir
)) {
1682 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1683 die(_("could not create directory '%s'"), sm_gitdir
);
1685 prepare_possible_alternates(clone_data
->name
, reference
);
1687 strvec_push(&cp
.args
, "clone");
1688 strvec_push(&cp
.args
, "--no-checkout");
1689 if (clone_data
->quiet
)
1690 strvec_push(&cp
.args
, "--quiet");
1691 if (clone_data
->progress
)
1692 strvec_push(&cp
.args
, "--progress");
1693 if (clone_data
->depth
&& *(clone_data
->depth
))
1694 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1695 if (reference
->nr
) {
1696 struct string_list_item
*item
;
1698 for_each_string_list_item(item
, reference
)
1699 strvec_pushl(&cp
.args
, "--reference",
1700 item
->string
, NULL
);
1702 if (clone_data
->dissociate
)
1703 strvec_push(&cp
.args
, "--dissociate");
1704 if (sm_gitdir
&& *sm_gitdir
)
1705 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1706 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1707 strvec_pushf(&cp
.args
, "--filter=%s",
1708 expand_list_objects_filter_spec(
1709 clone_data
->filter_options
));
1710 if (clone_data
->single_branch
>= 0)
1711 strvec_push(&cp
.args
, clone_data
->single_branch
?
1713 "--no-single-branch");
1715 strvec_push(&cp
.args
, "--");
1716 strvec_push(&cp
.args
, clone_data
->url
);
1717 strvec_push(&cp
.args
, clone_data_path
);
1720 prepare_submodule_repo_env(&cp
.env
);
1723 if(run_command(&cp
))
1724 die(_("clone of '%s' into submodule path '%s' failed"),
1725 clone_data
->url
, clone_data_path
);
1729 if (clone_data
->require_init
&& !access(clone_data_path
, X_OK
) &&
1730 !is_empty_dir(clone_data_path
))
1731 die(_("directory not empty: '%s'"), clone_data_path
);
1732 if (safe_create_leading_directories_const(clone_data_path
) < 0)
1733 die(_("could not create directory '%s'"), clone_data_path
);
1734 path
= xstrfmt("%s/index", sm_gitdir
);
1735 unlink_or_warn(path
);
1739 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1741 p
= git_pathdup_submodule(clone_data_path
, "config");
1743 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1745 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1746 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1748 git_config_set_in_file(p
, "submodule.alternateLocation",
1750 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1752 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1756 free(error_strategy
);
1764 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1766 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1767 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1768 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1769 struct list_objects_filter_options filter_options
=
1770 LIST_OBJECTS_FILTER_INIT
;
1772 struct option module_clone_options
[] = {
1773 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1775 N_("alternative anchor for relative paths")),
1776 OPT_STRING(0, "path", &clone_data
.path
,
1778 N_("where the new submodule will be cloned to")),
1779 OPT_STRING(0, "name", &clone_data
.name
,
1781 N_("name of the new submodule")),
1782 OPT_STRING(0, "url", &clone_data
.url
,
1784 N_("url where to clone the submodule from")),
1785 OPT_STRING_LIST(0, "reference", &reference
,
1787 N_("reference repository")),
1788 OPT_BOOL(0, "dissociate", &dissociate
,
1789 N_("use --reference only while cloning")),
1790 OPT_STRING(0, "depth", &clone_data
.depth
,
1792 N_("depth for shallow clones")),
1793 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1794 OPT_BOOL(0, "progress", &progress
,
1795 N_("force cloning progress")),
1796 OPT_BOOL(0, "require-init", &require_init
,
1797 N_("disallow cloning into non-empty directory")),
1798 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1799 N_("clone only one branch, HEAD or --branch")),
1800 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1803 const char *const git_submodule_helper_usage
[] = {
1804 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1805 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1806 "[--single-branch] [--filter <filter-spec>] "
1807 "--url <url> --path <path>"),
1811 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1812 git_submodule_helper_usage
, 0);
1814 clone_data
.dissociate
= !!dissociate
;
1815 clone_data
.quiet
= !!quiet
;
1816 clone_data
.progress
= !!progress
;
1817 clone_data
.require_init
= !!require_init
;
1818 clone_data
.filter_options
= &filter_options
;
1820 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1821 usage_with_options(git_submodule_helper_usage
,
1822 module_clone_options
);
1824 clone_submodule(&clone_data
, &reference
);
1825 list_objects_filter_release(&filter_options
);
1826 string_list_clear(&reference
, 1);
1830 static int determine_submodule_update_strategy(struct repository
*r
,
1833 enum submodule_update_type update
,
1834 struct submodule_update_strategy
*out
)
1836 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1841 key
= xstrfmt("submodule.%s.update", sub
->name
);
1845 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1846 if (parse_submodule_update_strategy(val
, out
) < 0) {
1847 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1851 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1852 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1853 BUG("how did we read update = !command from .gitmodules?");
1854 out
->type
= sub
->update_strategy
.type
;
1855 out
->command
= sub
->update_strategy
.command
;
1857 out
->type
= SM_UPDATE_CHECKOUT
;
1860 (out
->type
== SM_UPDATE_MERGE
||
1861 out
->type
== SM_UPDATE_REBASE
||
1862 out
->type
== SM_UPDATE_NONE
))
1863 out
->type
= SM_UPDATE_CHECKOUT
;
1871 struct update_clone_data
{
1872 const struct submodule
*sub
;
1873 struct object_id oid
;
1874 unsigned just_cloned
;
1877 struct submodule_update_clone
{
1878 /* index into 'update_data.list', the list of submodules to look into for cloning */
1881 /* configuration parameters which are passed on to the children */
1882 const struct update_data
*update_data
;
1884 /* to be consumed by update_submodule() */
1885 struct update_clone_data
*update_clone
;
1886 int update_clone_nr
; int update_clone_alloc
;
1888 /* If we want to stop as fast as possible and return an error */
1889 unsigned quickstop
: 1;
1891 /* failed clones to be retried again */
1892 const struct cache_entry
**failed_clones
;
1893 int failed_clones_nr
, failed_clones_alloc
;
1895 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1897 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1899 free(suc
->update_clone
);
1900 free(suc
->failed_clones
);
1903 struct update_data
{
1905 const char *super_prefix
;
1907 enum submodule_update_type update_default
;
1908 struct object_id suboid
;
1909 struct string_list references
;
1910 struct submodule_update_strategy update_strategy
;
1911 struct list_objects_filter_options
*filter_options
;
1912 struct module_list list
;
1916 int recommend_shallow
;
1917 unsigned int require_init
;
1920 unsigned int nofetch
;
1921 unsigned int remote
;
1922 unsigned int progress
;
1923 unsigned int dissociate
;
1925 unsigned int warn_if_uninitialized
;
1926 unsigned int recursive
;
1928 /* copied over from update_clone_data */
1929 struct object_id oid
;
1930 unsigned int just_cloned
;
1931 const char *sm_path
;
1933 #define UPDATE_DATA_INIT { \
1934 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1935 .list = MODULE_LIST_INIT, \
1936 .recommend_shallow = -1, \
1937 .references = STRING_LIST_INIT_DUP, \
1938 .single_branch = -1, \
1942 static void update_data_release(struct update_data
*ud
)
1944 free(ud
->displaypath
);
1945 module_list_release(&ud
->list
);
1948 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1949 struct strbuf
*out
, const char *displaypath
)
1952 * Only mention uninitialized submodules when their
1953 * paths have been specified.
1955 if (suc
->update_data
->warn_if_uninitialized
) {
1957 _("Submodule path '%s' not initialized"),
1959 strbuf_addch(out
, '\n');
1961 _("Maybe you want to use 'update --init'?"));
1962 strbuf_addch(out
, '\n');
1967 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1968 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1970 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1971 struct child_process
*child
,
1972 struct submodule_update_clone
*suc
,
1975 const struct submodule
*sub
= NULL
;
1976 const char *url
= NULL
;
1977 const char *update_string
;
1978 enum submodule_update_type update_type
;
1980 const struct update_data
*ud
= suc
->update_data
;
1981 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
1983 struct strbuf sb
= STRBUF_INIT
;
1984 int needs_cloning
= 0;
1985 int need_free_url
= 0;
1988 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
1989 strbuf_addch(out
, '\n');
1993 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
1996 next_submodule_warn_missing(suc
, out
, displaypath
);
2000 key
= xstrfmt("submodule.%s.update", sub
->name
);
2001 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2002 update_type
= parse_submodule_update_type(update_string
);
2004 update_type
= sub
->update_strategy
.type
;
2008 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2009 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2010 && update_type
== SM_UPDATE_NONE
)) {
2011 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2012 strbuf_addch(out
, '\n');
2016 /* Check if the submodule has been initialized. */
2017 if (!is_submodule_active(the_repository
, ce
->name
)) {
2018 next_submodule_warn_missing(suc
, out
, displaypath
);
2023 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2024 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2025 if (starts_with_dot_slash(sub
->url
) ||
2026 starts_with_dot_dot_slash(sub
->url
)) {
2027 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2034 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2035 needs_cloning
= !file_exists(sb
.buf
);
2037 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2038 suc
->update_clone_alloc
);
2039 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2040 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2041 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2042 suc
->update_clone_nr
++;
2048 child
->no_stdin
= 1;
2049 child
->stdout_to_stderr
= 1;
2051 strvec_push(&child
->args
, "submodule--helper");
2052 strvec_push(&child
->args
, "clone");
2053 if (suc
->update_data
->progress
)
2054 strvec_push(&child
->args
, "--progress");
2055 if (suc
->update_data
->quiet
)
2056 strvec_push(&child
->args
, "--quiet");
2057 if (suc
->update_data
->prefix
)
2058 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2059 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2060 strvec_push(&child
->args
, "--depth=1");
2061 else if (suc
->update_data
->depth
)
2062 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2063 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2064 strvec_pushf(&child
->args
, "--filter=%s",
2065 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2066 if (suc
->update_data
->require_init
)
2067 strvec_push(&child
->args
, "--require-init");
2068 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2069 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2070 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2071 if (suc
->update_data
->references
.nr
) {
2072 struct string_list_item
*item
;
2074 for_each_string_list_item(item
, &suc
->update_data
->references
)
2075 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2077 if (suc
->update_data
->dissociate
)
2078 strvec_push(&child
->args
, "--dissociate");
2079 if (suc
->update_data
->single_branch
>= 0)
2080 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2082 "--no-single-branch");
2086 strbuf_release(&sb
);
2090 return needs_cloning
;
2093 static int update_clone_get_next_task(struct child_process
*child
,
2098 struct submodule_update_clone
*suc
= suc_cb
;
2099 const struct cache_entry
*ce
;
2102 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2103 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2104 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2105 int *p
= xmalloc(sizeof(*p
));
2115 * The loop above tried cloning each submodule once, now try the
2116 * stragglers again, which we can imagine as an extension of the
2119 index
= suc
->current
- suc
->update_data
->list
.nr
;
2120 if (index
< suc
->failed_clones_nr
) {
2123 ce
= suc
->failed_clones
[index
];
2124 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2126 strbuf_addstr(err
, "BUG: submodule considered for "
2127 "cloning, doesn't need cloning "
2131 p
= xmalloc(sizeof(*p
));
2141 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2143 void *idx_task_cb UNUSED
)
2145 struct submodule_update_clone
*suc
= suc_cb
;
2151 static int update_clone_task_finished(int result
,
2156 const struct cache_entry
*ce
;
2157 struct submodule_update_clone
*suc
= suc_cb
;
2158 int *idxP
= idx_task_cb
;
2166 if (idx
< suc
->update_data
->list
.nr
) {
2167 ce
= suc
->update_data
->list
.entries
[idx
];
2168 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2170 strbuf_addch(err
, '\n');
2171 ALLOC_GROW(suc
->failed_clones
,
2172 suc
->failed_clones_nr
+ 1,
2173 suc
->failed_clones_alloc
);
2174 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2177 idx
-= suc
->update_data
->list
.nr
;
2178 ce
= suc
->failed_clones
[idx
];
2179 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2181 strbuf_addch(err
, '\n');
2189 static int git_update_clone_config(const char *var
, const char *value
,
2194 if (!strcmp(var
, "submodule.fetchjobs"))
2195 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2199 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2201 struct child_process cp
= CHILD_PROCESS_INIT
;
2202 struct strbuf rev
= STRBUF_INIT
;
2203 char *hex
= oid_to_hex(oid
);
2208 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2210 prepare_submodule_repo_env(&cp
.env
);
2212 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2218 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2219 const struct object_id
*oid
)
2221 struct child_process cp
= CHILD_PROCESS_INIT
;
2223 prepare_submodule_repo_env(&cp
.env
);
2225 cp
.dir
= module_path
;
2227 strvec_push(&cp
.args
, "fetch");
2229 strvec_push(&cp
.args
, "--quiet");
2231 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2233 char *hex
= oid_to_hex(oid
);
2234 char *remote
= get_default_remote();
2236 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2240 return run_command(&cp
);
2243 static int run_update_command(const struct update_data
*ud
, int subforce
)
2245 struct child_process cp
= CHILD_PROCESS_INIT
;
2246 char *oid
= oid_to_hex(&ud
->oid
);
2249 switch (ud
->update_strategy
.type
) {
2250 case SM_UPDATE_CHECKOUT
:
2252 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2254 strvec_push(&cp
.args
, "-f");
2256 case SM_UPDATE_REBASE
:
2258 strvec_push(&cp
.args
, "rebase");
2260 strvec_push(&cp
.args
, "--quiet");
2262 case SM_UPDATE_MERGE
:
2264 strvec_push(&cp
.args
, "merge");
2266 strvec_push(&cp
.args
, "--quiet");
2268 case SM_UPDATE_COMMAND
:
2270 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2273 BUG("unexpected update strategy type: %d",
2274 ud
->update_strategy
.type
);
2276 strvec_push(&cp
.args
, oid
);
2278 cp
.dir
= ud
->sm_path
;
2279 prepare_submodule_repo_env(&cp
.env
);
2280 if ((ret
= run_command(&cp
))) {
2281 switch (ud
->update_strategy
.type
) {
2282 case SM_UPDATE_CHECKOUT
:
2283 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2284 oid
, ud
->displaypath
);
2285 /* No "ret" assignment, use "git checkout"'s */
2287 case SM_UPDATE_REBASE
:
2288 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2289 oid
, ud
->displaypath
);
2291 case SM_UPDATE_MERGE
:
2292 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2293 oid
, ud
->displaypath
);
2295 case SM_UPDATE_COMMAND
:
2296 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2297 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2300 BUG("unexpected update strategy type: %d",
2301 ud
->update_strategy
.type
);
2310 switch (ud
->update_strategy
.type
) {
2311 case SM_UPDATE_CHECKOUT
:
2312 printf(_("Submodule path '%s': checked out '%s'\n"),
2313 ud
->displaypath
, oid
);
2315 case SM_UPDATE_REBASE
:
2316 printf(_("Submodule path '%s': rebased into '%s'\n"),
2317 ud
->displaypath
, oid
);
2319 case SM_UPDATE_MERGE
:
2320 printf(_("Submodule path '%s': merged in '%s'\n"),
2321 ud
->displaypath
, oid
);
2323 case SM_UPDATE_COMMAND
:
2324 printf(_("Submodule path '%s': '%s %s'\n"),
2325 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2328 BUG("unexpected update strategy type: %d",
2329 ud
->update_strategy
.type
);
2335 static int run_update_procedure(const struct update_data
*ud
)
2337 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2341 * Run fetch only if `oid` isn't present or it
2342 * is not reachable from a ref.
2344 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2345 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2348 _("Unable to fetch in submodule path '%s'; "
2349 "trying to directly fetch %s:"),
2350 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2352 * Now we tried the usual fetch, but `oid` may
2353 * not be reachable from any of the refs.
2355 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2356 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2357 return die_message(_("Fetched in submodule path '%s', but it did not "
2358 "contain %s. Direct fetching of that commit failed."),
2359 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2362 return run_update_command(ud
, subforce
);
2365 static int remote_submodule_branch(const char *path
, const char **branch
)
2367 const struct submodule
*sub
;
2371 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2373 return die_message(_("could not initialize submodule at path '%s'"),
2376 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2377 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2378 *branch
= sub
->branch
;
2386 if (!strcmp(*branch
, ".")) {
2387 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2390 return die_message(_("No such ref: %s"), "HEAD");
2393 if (!strcmp(refname
, "HEAD"))
2394 return die_message(_("Submodule (%s) branch configured to inherit "
2395 "branch from superproject, but the superproject "
2396 "is not on any branch"), sub
->name
);
2398 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2399 return die_message(_("Expecting a full ref name, got %s"),
2406 /* Our "branch" is coming from repo_config_get_string_tmp() */
2410 static int ensure_core_worktree(const char *path
)
2413 struct repository subrepo
;
2415 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2416 return die_message(_("could not get a repository handle for submodule '%s'"),
2419 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2420 char *cfg_file
, *abs_path
;
2421 const char *rel_path
;
2422 struct strbuf sb
= STRBUF_INIT
;
2424 cfg_file
= repo_git_path(&subrepo
, "config");
2426 abs_path
= absolute_pathdup(path
);
2427 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2429 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2433 strbuf_release(&sb
);
2436 repo_clear(&subrepo
);
2440 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2443 case SM_UPDATE_CHECKOUT
:
2445 case SM_UPDATE_MERGE
:
2447 case SM_UPDATE_REBASE
:
2449 case SM_UPDATE_UNSPECIFIED
:
2450 case SM_UPDATE_NONE
:
2451 case SM_UPDATE_COMMAND
:
2454 BUG("unreachable with type %d", type
);
2457 static void update_data_to_args(const struct update_data
*update_data
,
2458 struct strvec
*args
)
2460 enum submodule_update_type update_type
= update_data
->update_default
;
2462 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2463 if (update_data
->displaypath
) {
2464 strvec_push(args
, "--super-prefix");
2465 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2467 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2468 if (update_data
->quiet
)
2469 strvec_push(args
, "--quiet");
2470 if (update_data
->force
)
2471 strvec_push(args
, "--force");
2472 if (update_data
->init
)
2473 strvec_push(args
, "--init");
2474 if (update_data
->remote
)
2475 strvec_push(args
, "--remote");
2476 if (update_data
->nofetch
)
2477 strvec_push(args
, "--no-fetch");
2478 if (update_data
->dissociate
)
2479 strvec_push(args
, "--dissociate");
2480 if (update_data
->progress
)
2481 strvec_push(args
, "--progress");
2482 if (update_data
->require_init
)
2483 strvec_push(args
, "--require-init");
2484 if (update_data
->depth
)
2485 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2486 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2487 strvec_pushf(args
, "--%s",
2488 submodule_update_type_to_label(update_type
));
2490 if (update_data
->references
.nr
) {
2491 struct string_list_item
*item
;
2493 for_each_string_list_item(item
, &update_data
->references
)
2494 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2496 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2497 strvec_pushf(args
, "--filter=%s",
2498 expand_list_objects_filter_spec(
2499 update_data
->filter_options
));
2500 if (update_data
->recommend_shallow
== 0)
2501 strvec_push(args
, "--no-recommend-shallow");
2502 else if (update_data
->recommend_shallow
== 1)
2503 strvec_push(args
, "--recommend-shallow");
2504 if (update_data
->single_branch
>= 0)
2505 strvec_push(args
, update_data
->single_branch
?
2507 "--no-single-branch");
2510 static int update_submodule(struct update_data
*update_data
)
2514 ret
= determine_submodule_update_strategy(the_repository
,
2515 update_data
->just_cloned
,
2516 update_data
->sm_path
,
2517 update_data
->update_default
,
2518 &update_data
->update_strategy
);
2522 if (update_data
->just_cloned
)
2523 oidcpy(&update_data
->suboid
, null_oid());
2524 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2525 return die_message(_("Unable to find current revision in submodule path '%s'"),
2526 update_data
->displaypath
);
2528 if (update_data
->remote
) {
2534 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2537 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2540 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2544 if (!update_data
->nofetch
) {
2545 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2547 return die_message(_("Unable to fetch in submodule path '%s'"),
2548 update_data
->sm_path
);
2551 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2552 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2553 remote_ref
, update_data
->sm_path
);
2558 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2559 ret
= run_update_procedure(update_data
);
2564 if (update_data
->recursive
) {
2565 struct child_process cp
= CHILD_PROCESS_INIT
;
2566 struct update_data next
= *update_data
;
2569 oidcpy(&next
.oid
, null_oid());
2570 oidcpy(&next
.suboid
, null_oid());
2572 cp
.dir
= update_data
->sm_path
;
2574 prepare_submodule_repo_env(&cp
.env
);
2575 update_data_to_args(&next
, &cp
.args
);
2577 ret
= run_command(&cp
);
2579 die_message(_("Failed to recurse into submodule path '%s'"),
2580 update_data
->displaypath
);
2587 static int update_submodules(struct update_data
*update_data
)
2590 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2591 const struct run_process_parallel_opts opts
= {
2592 .tr2_category
= "submodule",
2593 .tr2_label
= "parallel/update",
2595 .processes
= update_data
->max_jobs
,
2597 .get_next_task
= update_clone_get_next_task
,
2598 .start_failure
= update_clone_start_failure
,
2599 .task_finished
= update_clone_task_finished
,
2603 suc
.update_data
= update_data
;
2604 run_processes_parallel(&opts
);
2607 * We saved the output and put it out all at once now.
2609 * - the listener does not have to interleave their (checkout)
2610 * work with our fetching. The writes involved in a
2611 * checkout involve more straightforward sequential I/O.
2612 * - the listener can avoid doing any work if fetching failed.
2614 if (suc
.quickstop
) {
2619 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2620 struct update_clone_data ucd
= suc
.update_clone
[i
];
2623 oidcpy(&update_data
->oid
, &ucd
.oid
);
2624 update_data
->just_cloned
= ucd
.just_cloned
;
2625 update_data
->sm_path
= ucd
.sub
->path
;
2627 code
= ensure_core_worktree(update_data
->sm_path
);
2631 update_data
->displaypath
= get_submodule_displaypath(
2632 update_data
->sm_path
, update_data
->prefix
,
2633 update_data
->super_prefix
);
2634 code
= update_submodule(update_data
);
2635 FREE_AND_NULL(update_data
->displaypath
);
2645 submodule_update_clone_release(&suc
);
2646 string_list_clear(&update_data
->references
, 0);
2650 static int module_update(int argc
, const char **argv
, const char *prefix
)
2652 struct pathspec pathspec
= { 0 };
2653 struct pathspec pathspec2
= { 0 };
2654 struct update_data opt
= UPDATE_DATA_INIT
;
2655 struct list_objects_filter_options filter_options
=
2656 LIST_OBJECTS_FILTER_INIT
;
2658 struct option module_update_options
[] = {
2659 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2660 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2661 OPT_BOOL(0, "init", &opt
.init
,
2662 N_("initialize uninitialized submodules before update")),
2663 OPT_BOOL(0, "remote", &opt
.remote
,
2664 N_("use SHA-1 of submodule's remote tracking branch")),
2665 OPT_BOOL(0, "recursive", &opt
.recursive
,
2666 N_("traverse submodules recursively")),
2667 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2668 N_("don't fetch new objects from the remote site")),
2669 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2670 N_("use the 'checkout' update strategy (default)"),
2671 SM_UPDATE_CHECKOUT
),
2672 OPT_SET_INT('m', "merge", &opt
.update_default
,
2673 N_("use the 'merge' update strategy"),
2675 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2676 N_("use the 'rebase' update strategy"),
2678 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2679 N_("reference repository")),
2680 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2681 N_("use --reference only while cloning")),
2682 OPT_INTEGER(0, "depth", &opt
.depth
,
2683 N_("create a shallow clone truncated to the "
2684 "specified number of revisions")),
2685 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2686 N_("parallel jobs")),
2687 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2688 N_("whether the initial clone should follow the shallow recommendation")),
2689 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2690 OPT_BOOL(0, "progress", &opt
.progress
,
2691 N_("force cloning progress")),
2692 OPT_BOOL(0, "require-init", &opt
.require_init
,
2693 N_("disallow cloning into non-empty directory, implies --init")),
2694 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2695 N_("clone only one branch, HEAD or --branch")),
2696 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2699 const char *const git_submodule_helper_usage
[] = {
2700 N_("git submodule [--quiet] update"
2701 " [--init [--filter=<filter-spec>]] [--remote]"
2702 " [-N|--no-fetch] [-f|--force]"
2703 " [--checkout|--merge|--rebase]"
2704 " [--[no-]recommend-shallow] [--reference <repository>]"
2705 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2709 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2710 git_config(git_update_clone_config
, &opt
.max_jobs
);
2712 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2713 git_submodule_helper_usage
, 0);
2715 if (opt
.require_init
)
2718 if (filter_options
.choice
&& !opt
.init
) {
2719 usage_with_options(git_submodule_helper_usage
,
2720 module_update_options
);
2723 opt
.filter_options
= &filter_options
;
2724 opt
.prefix
= prefix
;
2726 if (opt
.update_default
)
2727 opt
.update_strategy
.type
= opt
.update_default
;
2729 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2735 opt
.warn_if_uninitialized
= 1;
2738 struct module_list list
= MODULE_LIST_INIT
;
2739 struct init_cb info
= INIT_CB_INIT
;
2741 if (module_list_compute(argv
, opt
.prefix
,
2742 &pathspec2
, &list
) < 0) {
2743 module_list_release(&list
);
2749 * If there are no path args and submodule.active is set then,
2750 * by default, only initialize 'active' modules.
2752 if (!argc
&& git_config_get_value_multi("submodule.active"))
2753 module_list_active(&list
);
2755 info
.prefix
= opt
.prefix
;
2756 info
.super_prefix
= opt
.super_prefix
;
2758 info
.flags
|= OPT_QUIET
;
2760 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2761 module_list_release(&list
);
2764 ret
= update_submodules(&opt
);
2766 update_data_release(&opt
);
2767 list_objects_filter_release(&filter_options
);
2768 clear_pathspec(&pathspec
);
2769 clear_pathspec(&pathspec2
);
2773 static int push_check(int argc
, const char **argv
, const char *prefix
)
2775 struct remote
*remote
;
2776 const char *superproject_head
;
2778 int detached_head
= 0;
2779 struct object_id head_oid
;
2782 die("submodule--helper push-check requires at least 2 arguments");
2785 * superproject's resolved head ref.
2786 * if HEAD then the superproject is in a detached head state, otherwise
2787 * it will be the resolved head ref.
2789 superproject_head
= argv
[1];
2792 /* Get the submodule's head ref and determine if it is detached */
2793 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2795 die(_("Failed to resolve HEAD as a valid ref."));
2796 if (!strcmp(head
, "HEAD"))
2800 * The remote must be configured.
2801 * This is to avoid pushing to the exact same URL as the parent.
2803 remote
= pushremote_get(argv
[1]);
2804 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2805 die("remote '%s' not configured", argv
[1]);
2807 /* Check the refspec */
2810 struct ref
*local_refs
= get_local_heads();
2811 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2813 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2815 for (i
= 0; i
< refspec
.nr
; i
++) {
2816 const struct refspec_item
*rs
= &refspec
.items
[i
];
2818 if (rs
->pattern
|| rs
->matching
)
2821 /* LHS must match a single ref */
2822 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2827 * If LHS matches 'HEAD' then we need to ensure
2828 * that it matches the same named branch
2829 * checked out in the superproject.
2831 if (!strcmp(rs
->src
, "HEAD")) {
2832 if (!detached_head
&&
2833 !strcmp(head
, superproject_head
))
2835 die("HEAD does not match the named branch in the superproject");
2839 die("src refspec '%s' must name a ref",
2843 refspec_clear(&refspec
);
2850 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2853 struct pathspec pathspec
= { 0 };
2854 struct module_list list
= MODULE_LIST_INIT
;
2855 const char *super_prefix
= NULL
;
2856 struct option embed_gitdir_options
[] = {
2857 OPT__SUPER_PREFIX(&super_prefix
),
2860 const char *const git_submodule_helper_usage
[] = {
2861 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2866 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2867 git_submodule_helper_usage
, 0);
2869 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
2872 for (i
= 0; i
< list
.nr
; i
++)
2873 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
2878 clear_pathspec(&pathspec
);
2879 module_list_release(&list
);
2883 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2889 struct option options
[] = {
2890 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2893 const char *const usage
[] = {
2894 N_("git submodule set-url [--quiet] <path> <newurl>"),
2898 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2900 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2901 usage_with_options(usage
, options
);
2903 config_name
= xstrfmt("submodule.%s.url", path
);
2905 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2906 sync_submodule(path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
2913 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2915 int opt_default
= 0, ret
;
2916 const char *opt_branch
= NULL
;
2919 struct option options
[] = {
2921 * We accept the `quiet` option for uniformity across subcommands,
2922 * though there is nothing to make less verbose in this subcommand.
2924 OPT_NOOP_NOARG('q', "quiet"),
2926 OPT_BOOL('d', "default", &opt_default
,
2927 N_("set the default tracking branch to master")),
2928 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2929 N_("set the default tracking branch")),
2932 const char *const usage
[] = {
2933 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2934 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2938 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2940 if (!opt_branch
&& !opt_default
)
2941 die(_("--branch or --default required"));
2943 if (opt_branch
&& opt_default
)
2944 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2946 if (argc
!= 1 || !(path
= argv
[0]))
2947 usage_with_options(usage
, options
);
2949 config_name
= xstrfmt("submodule.%s.branch", path
);
2950 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2956 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
2958 enum branch_track track
;
2959 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
2960 struct option options
[] = {
2961 OPT__QUIET(&quiet
, N_("print only error messages")),
2962 OPT__FORCE(&force
, N_("force creation"), 0),
2963 OPT_BOOL(0, "create-reflog", &reflog
,
2964 N_("create the branch's reflog")),
2965 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
2966 N_("set branch tracking configuration"),
2968 parse_opt_tracking_mode
),
2969 OPT__DRY_RUN(&dry_run
,
2970 N_("show whether the branch would be created")),
2973 const char *const usage
[] = {
2974 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2978 git_config(git_default_config
, NULL
);
2979 track
= git_branch_track
;
2980 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2983 usage_with_options(usage
, options
);
2985 if (!quiet
&& !dry_run
)
2986 printf_ln(_("creating branch '%s'"), argv
[0]);
2988 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
2989 force
, reflog
, quiet
, track
, dry_run
);
2996 const char *reference_path
;
2998 const char *sm_name
;
3000 const char *realrepo
;
3002 unsigned int force
: 1;
3003 unsigned int quiet
: 1;
3004 unsigned int progress
: 1;
3005 unsigned int dissociate
: 1;
3007 #define ADD_DATA_INIT { .depth = -1 }
3009 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3011 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3012 struct strbuf sb_remote_out
= STRBUF_INIT
;
3014 cp_remote
.git_cmd
= 1;
3015 strvec_pushf(&cp_remote
.env
,
3016 "GIT_DIR=%s", git_dir_path
);
3017 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3018 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3019 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3021 char *line
= sb_remote_out
.buf
;
3023 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3024 size_t len
= next_line
- line
;
3026 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3027 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3028 line
= next_line
+ 1;
3032 strbuf_release(&sb_remote_out
);
3035 static int add_submodule(const struct add_data
*add_data
)
3037 char *submod_gitdir_path
;
3038 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3039 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3042 /* perhaps the path already exists and is already a git repo, else clone it */
3043 if (is_directory(add_data
->sm_path
)) {
3044 struct strbuf sm_path
= STRBUF_INIT
;
3045 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3046 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3047 if (is_nonbare_repository_dir(&sm_path
))
3048 printf(_("Adding existing repo at '%s' to the index\n"),
3051 die(_("'%s' already exists and is not a valid git repo"),
3053 strbuf_release(&sm_path
);
3054 free(submod_gitdir_path
);
3056 struct child_process cp
= CHILD_PROCESS_INIT
;
3058 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3060 if (is_directory(submod_gitdir_path
)) {
3061 if (!add_data
->force
) {
3062 struct strbuf msg
= STRBUF_INIT
;
3065 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3066 "locally with remote(s):\n"),
3069 append_fetch_remotes(&msg
, submod_gitdir_path
);
3070 free(submod_gitdir_path
);
3072 strbuf_addf(&msg
, _("If you want to reuse this local git "
3073 "directory instead of cloning again from\n"
3075 "use the '--force' option. If the local git "
3076 "directory is not the correct repo\n"
3077 "or you are unsure what this means choose "
3078 "another name with the '--name' option."),
3079 add_data
->realrepo
);
3081 die_msg
= strbuf_detach(&msg
, NULL
);
3084 printf(_("Reactivating local git directory for "
3085 "submodule '%s'\n"), add_data
->sm_name
);
3088 free(submod_gitdir_path
);
3090 clone_data
.prefix
= add_data
->prefix
;
3091 clone_data
.path
= add_data
->sm_path
;
3092 clone_data
.name
= add_data
->sm_name
;
3093 clone_data
.url
= add_data
->realrepo
;
3094 clone_data
.quiet
= add_data
->quiet
;
3095 clone_data
.progress
= add_data
->progress
;
3096 if (add_data
->reference_path
) {
3097 char *p
= xstrdup(add_data
->reference_path
);
3099 string_list_append(&reference
, p
)->util
= p
;
3101 clone_data
.dissociate
= add_data
->dissociate
;
3102 if (add_data
->depth
>= 0)
3103 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3105 if (clone_submodule(&clone_data
, &reference
))
3108 prepare_submodule_repo_env(&cp
.env
);
3110 cp
.dir
= add_data
->sm_path
;
3112 * NOTE: we only get here if add_data->force is true, so
3113 * passing --force to checkout is reasonable.
3115 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3117 if (add_data
->branch
) {
3118 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3119 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3122 if (run_command(&cp
))
3123 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3127 string_list_clear(&reference
, 1);
3131 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3136 if (!is_writing_gitmodules_ok())
3137 die(_("please make sure that the .gitmodules file is in the working tree"));
3139 key
= xstrfmt("submodule.%s.%s", name
, var
);
3140 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3146 static void configure_added_submodule(struct add_data
*add_data
)
3150 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3151 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3153 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3154 git_config_set_gently(key
, add_data
->realrepo
);
3157 add_submod
.git_cmd
= 1;
3158 strvec_pushl(&add_submod
.args
, "add",
3159 "--no-warn-embedded-repo", NULL
);
3160 if (add_data
->force
)
3161 strvec_push(&add_submod
.args
, "--force");
3162 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3164 if (run_command(&add_submod
))
3165 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3167 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3168 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3169 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3171 if (add_data
->branch
) {
3172 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3173 "branch", add_data
->branch
))
3174 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3177 add_gitmodules
.git_cmd
= 1;
3178 strvec_pushl(&add_gitmodules
.args
,
3179 "add", "--force", "--", ".gitmodules", NULL
);
3181 if (run_command(&add_gitmodules
))
3182 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3185 * NEEDSWORK: In a multi-working-tree world this needs to be
3186 * set in the per-worktree config.
3189 * NEEDSWORK: In the longer run, we need to get rid of this
3190 * pattern of querying "submodule.active" before calling
3191 * is_submodule_active(), since that function needs to find
3192 * out the value of "submodule.active" again anyway.
3194 if (!git_config_get_string_tmp("submodule.active", &val
)) {
3196 * If the submodule being added isn't already covered by the
3197 * current configured pathspec, set the submodule's active flag
3199 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3200 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3201 git_config_set_gently(key
, "true");
3205 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3206 git_config_set_gently(key
, "true");
3211 static void die_on_index_match(const char *path
, int force
)
3214 const char *args
[] = { path
, NULL
};
3215 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3217 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3218 die(_("index file corrupt"));
3222 char *ps_matched
= xcalloc(ps
.nr
, 1);
3224 /* TODO: audit for interaction with sparse-index. */
3225 ensure_full_index(&the_index
);
3228 * Since there is only one pathspec, we just need to
3229 * check ps_matched[0] to know if a cache entry matched.
3231 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
3232 ce_path_match(&the_index
, the_index
.cache
[i
], &ps
,
3235 if (ps_matched
[0]) {
3237 die(_("'%s' already exists in the index"),
3239 if (!S_ISGITLINK(the_index
.cache
[i
]->ce_mode
))
3240 die(_("'%s' already exists in the index "
3241 "and is not a submodule"), path
);
3247 clear_pathspec(&ps
);
3250 static void die_on_repo_without_commits(const char *path
)
3252 struct strbuf sb
= STRBUF_INIT
;
3253 strbuf_addstr(&sb
, path
);
3254 if (is_nonbare_repository_dir(&sb
)) {
3255 struct object_id oid
;
3256 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3257 die(_("'%s' does not have a commit checked out"), path
);
3259 strbuf_release(&sb
);
3262 static int module_add(int argc
, const char **argv
, const char *prefix
)
3264 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3265 struct add_data add_data
= ADD_DATA_INIT
;
3266 char *to_free
= NULL
;
3267 struct option options
[] = {
3268 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3269 N_("branch of repository to add as submodule")),
3270 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3271 PARSE_OPT_NOCOMPLETE
),
3272 OPT__QUIET(&quiet
, N_("print only error messages")),
3273 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3274 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3275 N_("reference repository")),
3276 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3277 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3278 N_("sets the submodule's name to the given string "
3279 "instead of defaulting to its path")),
3280 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3283 const char *const usage
[] = {
3284 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3287 struct strbuf sb
= STRBUF_INIT
;
3290 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3292 if (!is_writing_gitmodules_ok())
3293 die(_("please make sure that the .gitmodules file is in the working tree"));
3295 if (prefix
&& *prefix
&&
3296 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3297 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3299 if (argc
== 0 || argc
> 2)
3300 usage_with_options(usage
, options
);
3302 add_data
.repo
= argv
[0];
3304 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3306 add_data
.sm_path
= xstrdup(argv
[1]);
3308 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3309 char *sm_path
= add_data
.sm_path
;
3311 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3315 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3316 starts_with_dot_slash(add_data
.repo
)) {
3318 die(_("Relative path can only be used from the toplevel "
3319 "of the working tree"));
3321 /* dereference source url relative to parent's url */
3322 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3323 add_data
.realrepo
= to_free
;
3324 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3325 add_data
.realrepo
= add_data
.repo
;
3327 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3333 * multiple //; leading ./; /./; /../;
3335 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3336 strip_dir_trailing_slashes(add_data
.sm_path
);
3338 die_on_index_match(add_data
.sm_path
, force
);
3339 die_on_repo_without_commits(add_data
.sm_path
);
3342 struct child_process cp
= CHILD_PROCESS_INIT
;
3346 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3347 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3348 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3349 strbuf_complete_line(&sb
);
3350 fputs(sb
.buf
, stderr
);
3355 if(!add_data
.sm_name
)
3356 add_data
.sm_name
= add_data
.sm_path
;
3358 if (check_submodule_name(add_data
.sm_name
))
3359 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3361 add_data
.prefix
= prefix
;
3362 add_data
.force
= !!force
;
3363 add_data
.quiet
= !!quiet
;
3364 add_data
.progress
= !!progress
;
3365 add_data
.dissociate
= !!dissociate
;
3367 if (add_submodule(&add_data
))
3369 configure_added_submodule(&add_data
);
3373 free(add_data
.sm_path
);
3375 strbuf_release(&sb
);
3380 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3382 parse_opt_subcommand_fn
*fn
= NULL
;
3383 const char *const usage
[] = {
3384 N_("git submodule--helper <command>"),
3387 struct option options
[] = {
3388 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3389 OPT_SUBCOMMAND("add", &fn
, module_add
),
3390 OPT_SUBCOMMAND("update", &fn
, module_update
),
3391 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3392 OPT_SUBCOMMAND("init", &fn
, module_init
),
3393 OPT_SUBCOMMAND("status", &fn
, module_status
),
3394 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3395 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3396 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3397 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3398 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3399 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3400 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3401 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3404 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3406 return fn(argc
, argv
, prefix
);