1 #define USE_THE_INDEX_VARIABLE
5 #include "repository.h"
8 #include "parse-options.h"
12 #include "submodule.h"
13 #include "submodule-config.h"
14 #include "string-list.h"
15 #include "run-command.h"
23 #include "object-store.h"
26 #include "list-objects-filter-options.h"
28 #define OPT_QUIET (1 << 0)
29 #define OPT_CACHED (1 << 1)
30 #define OPT_RECURSIVE (1 << 2)
31 #define OPT_FORCE (1 << 3)
33 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
36 static int repo_get_default_remote(struct repository
*repo
, char **default_remote
)
39 struct strbuf sb
= STRBUF_INIT
;
40 struct ref_store
*store
= get_main_ref_store(repo
);
41 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
45 return die_message(_("No such ref: %s"), "HEAD");
48 if (!strcmp(refname
, "HEAD")) {
49 *default_remote
= xstrdup("origin");
53 if (!skip_prefix(refname
, "refs/heads/", &refname
))
54 return die_message(_("Expecting a full ref name, got %s"),
57 strbuf_addf(&sb
, "branch.%s.remote", refname
);
58 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
59 *default_remote
= xstrdup("origin");
61 *default_remote
= dest
;
67 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
69 struct repository subrepo
;
72 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
74 return die_message(_("could not get a repository handle for submodule '%s'"),
76 ret
= repo_get_default_remote(&subrepo
, default_remote
);
82 static char *get_default_remote(void)
85 int code
= repo_get_default_remote(the_repository
, &default_remote
);
90 return default_remote
;
93 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
95 char *remoteurl
, *resolved_url
;
96 char *remote
= get_default_remote();
97 struct strbuf remotesb
= STRBUF_INIT
;
99 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
100 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
102 warning(_("could not look up configuration '%s'. "
103 "Assuming this repository is its own "
104 "authoritative upstream."),
106 remoteurl
= xgetcwd();
108 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
112 strbuf_release(&remotesb
);
117 /* the result should be freed by the caller. */
118 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
119 const char *super_prefix
)
121 if (prefix
&& super_prefix
) {
122 BUG("cannot have prefix '%s' and superprefix '%s'",
123 prefix
, super_prefix
);
125 struct strbuf sb
= STRBUF_INIT
;
126 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
129 } else if (super_prefix
) {
130 return xstrfmt("%s%s", super_prefix
, path
);
132 return xstrdup(path
);
136 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
138 struct strbuf sb
= STRBUF_INIT
;
141 static const char *describe_bare
[] = { NULL
};
143 static const char *describe_tags
[] = { "--tags", NULL
};
145 static const char *describe_contains
[] = { "--contains", NULL
};
147 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
149 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
151 describe_all_always
, NULL
};
153 for (d
= describe_argv
; *d
; d
++) {
154 struct child_process cp
= CHILD_PROCESS_INIT
;
155 prepare_submodule_repo_env(&cp
.env
);
160 strvec_push(&cp
.args
, "describe");
161 strvec_pushv(&cp
.args
, *d
);
162 strvec_push(&cp
.args
, object_id
);
164 if (!capture_command(&cp
, &sb
, 0)) {
165 strbuf_strip_suffix(&sb
, "\n");
166 return strbuf_detach(&sb
, NULL
);
175 const struct cache_entry
**entries
;
178 #define MODULE_LIST_INIT { 0 }
180 static void module_list_release(struct module_list
*ml
)
185 static int module_list_compute(const char **argv
,
187 struct pathspec
*pathspec
,
188 struct module_list
*list
)
191 char *ps_matched
= NULL
;
193 parse_pathspec(pathspec
, 0,
194 PATHSPEC_PREFER_FULL
,
198 ps_matched
= xcalloc(pathspec
->nr
, 1);
200 if (repo_read_index(the_repository
) < 0)
201 die(_("index file corrupt"));
203 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
204 const struct cache_entry
*ce
= the_index
.cache
[i
];
206 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
208 !S_ISGITLINK(ce
->ce_mode
))
211 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
212 list
->entries
[list
->nr
++] = ce
;
213 while (i
+ 1 < the_index
.cache_nr
&&
214 !strcmp(ce
->name
, the_index
.cache
[i
+ 1]->name
))
216 * Skip entries with the same name in different stages
217 * to make sure an entry is returned only once.
222 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
230 static void module_list_active(struct module_list
*list
)
233 struct module_list active_modules
= MODULE_LIST_INIT
;
235 for (i
= 0; i
< list
->nr
; i
++) {
236 const struct cache_entry
*ce
= list
->entries
[i
];
238 if (!is_submodule_active(the_repository
, ce
->name
))
241 ALLOC_GROW(active_modules
.entries
,
242 active_modules
.nr
+ 1,
243 active_modules
.alloc
);
244 active_modules
.entries
[active_modules
.nr
++] = ce
;
247 module_list_release(list
);
248 *list
= active_modules
;
251 static char *get_up_path(const char *path
)
254 struct strbuf sb
= STRBUF_INIT
;
256 for (i
= count_slashes(path
); i
; i
--)
257 strbuf_addstr(&sb
, "../");
260 * Check if 'path' ends with slash or not
261 * for having the same output for dir/sub_dir
264 if (!is_dir_sep(path
[strlen(path
) - 1]))
265 strbuf_addstr(&sb
, "../");
267 return strbuf_detach(&sb
, NULL
);
270 static void for_each_listed_submodule(const struct module_list
*list
,
271 each_submodule_fn fn
, void *cb_data
)
275 for (i
= 0; i
< list
->nr
; i
++)
276 fn(list
->entries
[i
], cb_data
);
283 const char *super_prefix
;
287 #define FOREACH_CB_INIT { 0 }
289 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
292 struct foreach_cb
*info
= cb_data
;
293 const char *path
= list_item
->name
;
294 const struct object_id
*ce_oid
= &list_item
->oid
;
295 const struct submodule
*sub
;
296 struct child_process cp
= CHILD_PROCESS_INIT
;
299 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
302 sub
= submodule_from_path(the_repository
, null_oid(), path
);
305 die(_("No url found for submodule path '%s' in .gitmodules"),
308 if (!is_submodule_populated_gently(path
, NULL
))
311 prepare_submodule_repo_env(&cp
.env
);
314 * For the purpose of executing <command> in the submodule,
315 * separate shell is used for the purpose of running the
322 * NEEDSWORK: the command currently has access to the variables $name,
323 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
324 * contains a single argument. This is done for maintaining a faithful
325 * translation from shell script.
327 if (info
->argc
== 1) {
328 char *toplevel
= xgetcwd();
329 struct strbuf sb
= STRBUF_INIT
;
331 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
332 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
333 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
334 strvec_pushf(&cp
.env
, "sha1=%s",
336 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
339 * Since the path variable was accessible from the script
340 * before porting, it is also made available after porting.
341 * The environment variable "PATH" has a very special purpose
342 * on windows. And since environment variables are
343 * case-insensitive in windows, it interferes with the
344 * existing PATH variable. Hence, to avoid that, we expose
345 * path via the args strvec and not via env.
347 sq_quote_buf(&sb
, path
);
348 strvec_pushf(&cp
.args
, "path=%s; %s",
349 sb
.buf
, info
->argv
[0]);
353 strvec_pushv(&cp
.args
, info
->argv
);
357 printf(_("Entering '%s'\n"), displaypath
);
359 if (info
->argv
[0] && run_command(&cp
))
360 die(_("run_command returned non-zero status for %s\n."),
363 if (info
->recursive
) {
364 struct child_process cpr
= CHILD_PROCESS_INIT
;
368 prepare_submodule_repo_env(&cpr
.env
);
370 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
372 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
373 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
376 strvec_push(&cpr
.args
, "--quiet");
378 strvec_push(&cpr
.args
, "--");
379 strvec_pushv(&cpr
.args
, info
->argv
);
381 if (run_command(&cpr
))
382 die(_("run_command returned non-zero status while "
383 "recursing in the nested submodules of %s\n."),
391 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
393 struct foreach_cb info
= FOREACH_CB_INIT
;
394 struct pathspec pathspec
= { 0 };
395 struct module_list list
= MODULE_LIST_INIT
;
396 struct option module_foreach_options
[] = {
397 OPT__SUPER_PREFIX(&info
.super_prefix
),
398 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
399 OPT_BOOL(0, "recursive", &info
.recursive
,
400 N_("recurse into nested submodules")),
403 const char *const git_submodule_helper_usage
[] = {
404 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
409 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
410 git_submodule_helper_usage
, 0);
412 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
417 info
.prefix
= prefix
;
419 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
423 module_list_release(&list
);
424 clear_pathspec(&pathspec
);
428 static int starts_with_dot_slash(const char *const path
)
430 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
431 PATH_MATCH_XPLATFORM
);
434 static int starts_with_dot_dot_slash(const char *const path
)
436 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
437 PATH_MATCH_XPLATFORM
);
442 const char *super_prefix
;
445 #define INIT_CB_INIT { 0 }
447 static void init_submodule(const char *path
, const char *prefix
,
448 const char *super_prefix
,
451 const struct submodule
*sub
;
452 struct strbuf sb
= STRBUF_INIT
;
454 char *url
= NULL
, *displaypath
;
456 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
458 sub
= submodule_from_path(the_repository
, null_oid(), path
);
461 die(_("No url found for submodule path '%s' in .gitmodules"),
465 * NEEDSWORK: In a multi-working-tree world, this needs to be
466 * set in the per-worktree config.
468 * Set active flag for the submodule being initialized
470 if (!is_submodule_active(the_repository
, path
)) {
471 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
472 git_config_set_gently(sb
.buf
, "true");
477 * Copy url setting when it is not set yet.
478 * To look up the url in .git/config, we must not fall back to
479 * .gitmodules, so look it up directly.
481 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
482 if (git_config_get_string(sb
.buf
, &url
)) {
484 die(_("No url found for submodule path '%s' in .gitmodules"),
487 url
= xstrdup(sub
->url
);
489 /* Possibly a url relative to parent */
490 if (starts_with_dot_dot_slash(url
) ||
491 starts_with_dot_slash(url
)) {
494 url
= resolve_relative_url(oldurl
, NULL
, 0);
498 if (git_config_set_gently(sb
.buf
, url
))
499 die(_("Failed to register url for submodule path '%s'"),
501 if (!(flags
& OPT_QUIET
))
503 _("Submodule '%s' (%s) registered for path '%s'\n"),
504 sub
->name
, url
, displaypath
);
508 /* Copy "update" setting when it is not set yet */
509 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
510 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
511 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
512 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
513 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
517 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
520 if (git_config_set_gently(sb
.buf
, upd
))
521 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
528 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
530 struct init_cb
*info
= cb_data
;
532 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
536 static int module_init(int argc
, const char **argv
, const char *prefix
)
538 struct init_cb info
= INIT_CB_INIT
;
539 struct pathspec pathspec
= { 0 };
540 struct module_list list
= MODULE_LIST_INIT
;
542 struct option module_init_options
[] = {
543 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
546 const char *const git_submodule_helper_usage
[] = {
547 N_("git submodule init [<options>] [<path>]"),
552 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
553 git_submodule_helper_usage
, 0);
555 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
559 * If there are no path args and submodule.active is set then,
560 * by default, only initialize 'active' modules.
562 if (!argc
&& !git_config_get("submodule.active"))
563 module_list_active(&list
);
565 info
.prefix
= prefix
;
567 info
.flags
|= OPT_QUIET
;
569 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
573 module_list_release(&list
);
574 clear_pathspec(&pathspec
);
580 const char *super_prefix
;
583 #define STATUS_CB_INIT { 0 }
585 static void print_status(unsigned int flags
, char state
, const char *path
,
586 const struct object_id
*oid
, const char *displaypath
)
588 if (flags
& OPT_QUIET
)
591 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
593 if (state
== ' ' || state
== '+') {
594 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
597 printf(" (%s)", name
);
604 static int handle_submodule_head_ref(const char *refname UNUSED
,
605 const struct object_id
*oid
,
609 struct object_id
*output
= cb_data
;
617 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
618 unsigned int ce_flags
, const char *prefix
,
619 const char *super_prefix
, unsigned int flags
)
622 struct strvec diff_files_args
= STRVEC_INIT
;
623 struct rev_info rev
= REV_INFO_INIT
;
624 int diff_files_result
;
625 struct strbuf buf
= STRBUF_INIT
;
627 struct setup_revision_opt opt
= {
628 .free_removed_argv_elements
= 1,
631 if (!submodule_from_path(the_repository
, null_oid(), path
))
632 die(_("no submodule mapping found in .gitmodules for path '%s'"),
635 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
637 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
638 print_status(flags
, 'U', path
, null_oid(), displaypath
);
642 strbuf_addf(&buf
, "%s/.git", path
);
643 git_dir
= read_gitfile(buf
.buf
);
647 if (!is_submodule_active(the_repository
, path
) ||
648 !is_git_directory(git_dir
)) {
649 print_status(flags
, '-', path
, ce_oid
, displaypath
);
650 strbuf_release(&buf
);
653 strbuf_release(&buf
);
655 strvec_pushl(&diff_files_args
, "diff-files",
656 "--ignore-submodules=dirty", "--quiet", "--",
659 git_config(git_diff_basic_config
, NULL
);
661 repo_init_revisions(the_repository
, &rev
, NULL
);
663 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
664 diff_files_result
= run_diff_files(&rev
, 0);
666 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
667 print_status(flags
, ' ', path
, ce_oid
,
669 } else if (!(flags
& OPT_CACHED
)) {
670 struct object_id oid
;
671 struct ref_store
*refs
= get_submodule_ref_store(path
);
674 print_status(flags
, '-', path
, ce_oid
, displaypath
);
677 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
678 die(_("could not resolve HEAD ref inside the "
679 "submodule '%s'"), path
);
681 print_status(flags
, '+', path
, &oid
, displaypath
);
683 print_status(flags
, '+', path
, ce_oid
, displaypath
);
686 if (flags
& OPT_RECURSIVE
) {
687 struct child_process cpr
= CHILD_PROCESS_INIT
;
691 prepare_submodule_repo_env(&cpr
.env
);
693 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
694 "--recursive", NULL
);
695 strvec_push(&cpr
.args
, "--super-prefix");
696 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
698 if (flags
& OPT_CACHED
)
699 strvec_push(&cpr
.args
, "--cached");
701 if (flags
& OPT_QUIET
)
702 strvec_push(&cpr
.args
, "--quiet");
704 if (run_command(&cpr
))
705 die(_("failed to recurse into submodule '%s'"), path
);
709 strvec_clear(&diff_files_args
);
711 release_revisions(&rev
);
714 static void status_submodule_cb(const struct cache_entry
*list_item
,
717 struct status_cb
*info
= cb_data
;
719 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
720 info
->prefix
, info
->super_prefix
, info
->flags
);
723 static int module_status(int argc
, const char **argv
, const char *prefix
)
725 struct status_cb info
= STATUS_CB_INIT
;
726 struct pathspec pathspec
= { 0 };
727 struct module_list list
= MODULE_LIST_INIT
;
729 struct option module_status_options
[] = {
730 OPT__SUPER_PREFIX(&info
.super_prefix
),
731 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
732 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
733 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
736 const char *const git_submodule_helper_usage
[] = {
737 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
742 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
743 git_submodule_helper_usage
, 0);
745 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
748 info
.prefix
= prefix
;
750 info
.flags
|= OPT_QUIET
;
752 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
756 module_list_release(&list
);
757 clear_pathspec(&pathspec
);
762 unsigned int mod_src
;
763 unsigned int mod_dst
;
764 struct object_id oid_src
;
765 struct object_id oid_dst
;
769 #define MODULE_CB_INIT { 0 }
771 static void module_cb_release(struct module_cb
*mcb
)
776 struct module_cb_list
{
777 struct module_cb
**entries
;
780 #define MODULE_CB_LIST_INIT { 0 }
782 static void module_cb_list_release(struct module_cb_list
*mcbl
)
786 for (i
= 0; i
< mcbl
->nr
; i
++) {
787 struct module_cb
*mcb
= mcbl
->entries
[i
];
789 module_cb_release(mcb
);
799 const char *super_prefix
;
800 unsigned int cached
: 1;
801 unsigned int for_status
: 1;
802 unsigned int files
: 1;
805 #define SUMMARY_CB_INIT { 0 }
812 static char *verify_submodule_committish(const char *sm_path
,
813 const char *committish
)
815 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
816 struct strbuf result
= STRBUF_INIT
;
818 cp_rev_parse
.git_cmd
= 1;
819 cp_rev_parse
.dir
= sm_path
;
820 prepare_submodule_repo_env(&cp_rev_parse
.env
);
821 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
822 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
823 strvec_push(&cp_rev_parse
.args
, "--");
825 if (capture_command(&cp_rev_parse
, &result
, 0))
828 strbuf_trim_trailing_newline(&result
);
829 return strbuf_detach(&result
, NULL
);
832 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
833 int total_commits
, const char *displaypath
,
834 const char *src_abbrev
, const char *dst_abbrev
,
837 if (p
->status
== 'T') {
838 if (S_ISGITLINK(p
->mod_dst
))
839 printf(_("* %s %s(blob)->%s(submodule)"),
840 displaypath
, src_abbrev
, dst_abbrev
);
842 printf(_("* %s %s(submodule)->%s(blob)"),
843 displaypath
, src_abbrev
, dst_abbrev
);
845 printf("* %s %s...%s",
846 displaypath
, src_abbrev
, dst_abbrev
);
849 if (total_commits
< 0)
852 printf(" (%d):\n", total_commits
);
855 printf(_("%s"), errmsg
);
856 } else if (total_commits
> 0) {
857 struct child_process cp_log
= CHILD_PROCESS_INIT
;
860 cp_log
.dir
= p
->sm_path
;
861 prepare_submodule_repo_env(&cp_log
.env
);
862 strvec_pushl(&cp_log
.args
, "log", NULL
);
864 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
865 if (info
->summary_limit
> 0)
866 strvec_pushf(&cp_log
.args
, "-%d",
867 info
->summary_limit
);
869 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
870 "--first-parent", NULL
);
871 strvec_pushf(&cp_log
.args
, "%s...%s",
872 src_abbrev
, dst_abbrev
);
873 } else if (S_ISGITLINK(p
->mod_dst
)) {
874 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
875 "-1", dst_abbrev
, NULL
);
877 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
878 "-1", src_abbrev
, NULL
);
880 run_command(&cp_log
);
885 static void generate_submodule_summary(struct summary_cb
*info
,
888 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
889 int missing_src
= 0, missing_dst
= 0;
890 struct strbuf errmsg
= STRBUF_INIT
;
891 int total_commits
= -1;
893 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
894 if (S_ISGITLINK(p
->mod_dst
)) {
895 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
898 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
899 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
901 int fd
= open(p
->sm_path
, O_RDONLY
);
903 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
904 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
906 error(_("couldn't hash object from '%s'"), p
->sm_path
);
908 /* for a submodule removal (mode:0000000), don't warn */
910 warning(_("unexpected mode %o\n"), p
->mod_dst
);
914 if (S_ISGITLINK(p
->mod_src
)) {
915 if (p
->status
!= 'D')
916 src_abbrev
= verify_submodule_committish(p
->sm_path
,
917 oid_to_hex(&p
->oid_src
));
921 * As `rev-parse` failed, we fallback to getting
922 * the abbreviated hash using oid_src. We do
923 * this as we might still need the abbreviated
924 * hash in cases like a submodule type change, etc.
926 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
930 * The source does not point to a submodule.
931 * So, we fallback to getting the abbreviation using
932 * oid_src as we might still need the abbreviated
933 * hash in cases like submodule add, etc.
935 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
938 if (S_ISGITLINK(p
->mod_dst
)) {
939 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
940 oid_to_hex(&p
->oid_dst
));
944 * As `rev-parse` failed, we fallback to getting
945 * the abbreviated hash using oid_dst. We do
946 * this as we might still need the abbreviated
947 * hash in cases like a submodule type change, etc.
949 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
953 * The destination does not point to a submodule.
954 * So, we fallback to getting the abbreviation using
955 * oid_dst as we might still need the abbreviated
956 * hash in cases like a submodule removal, etc.
958 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
961 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
964 if (!missing_src
&& !missing_dst
) {
965 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
966 struct strbuf sb_rev_list
= STRBUF_INIT
;
968 strvec_pushl(&cp_rev_list
.args
, "rev-list",
969 "--first-parent", "--count", NULL
);
970 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
971 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
972 src_abbrev
, dst_abbrev
);
974 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
975 src_abbrev
: dst_abbrev
);
976 strvec_push(&cp_rev_list
.args
, "--");
978 cp_rev_list
.git_cmd
= 1;
979 cp_rev_list
.dir
= p
->sm_path
;
980 prepare_submodule_repo_env(&cp_rev_list
.env
);
982 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
983 total_commits
= atoi(sb_rev_list
.buf
);
985 strbuf_release(&sb_rev_list
);
988 * Don't give error msg for modification whose dst is not
989 * submodule, i.e., deleted or changed to blob
991 if (S_ISGITLINK(p
->mod_dst
)) {
992 if (missing_src
&& missing_dst
) {
993 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
994 displaypath
, oid_to_hex(&p
->oid_src
),
995 oid_to_hex(&p
->oid_dst
));
997 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
998 displaypath
, missing_src
?
999 oid_to_hex(&p
->oid_src
) :
1000 oid_to_hex(&p
->oid_dst
));
1005 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1006 total_commits
, displaypath
, src_abbrev
,
1012 strbuf_release(&errmsg
);
1015 static void prepare_submodule_summary(struct summary_cb
*info
,
1016 struct module_cb_list
*list
)
1019 for (i
= 0; i
< list
->nr
; i
++) {
1020 const struct submodule
*sub
;
1021 struct module_cb
*p
= list
->entries
[i
];
1022 struct strbuf sm_gitdir
= STRBUF_INIT
;
1024 if (p
->status
== 'D' || p
->status
== 'T') {
1025 generate_submodule_summary(info
, p
);
1029 if (info
->for_status
&& p
->status
!= 'A' &&
1030 (sub
= submodule_from_path(the_repository
,
1031 null_oid(), p
->sm_path
))) {
1032 char *config_key
= NULL
;
1036 config_key
= xstrfmt("submodule.%s.ignore",
1038 if (!git_config_get_string_tmp(config_key
, &value
))
1039 ignore_all
= !strcmp(value
, "all");
1040 else if (sub
->ignore
)
1041 ignore_all
= !strcmp(sub
->ignore
, "all");
1048 /* Also show added or modified modules which are checked out */
1049 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1050 if (is_nonbare_repository_dir(&sm_gitdir
))
1051 generate_submodule_summary(info
, p
);
1052 strbuf_release(&sm_gitdir
);
1056 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1057 struct diff_options
*options UNUSED
,
1061 struct module_cb_list
*list
= data
;
1062 for (i
= 0; i
< q
->nr
; i
++) {
1063 struct diff_filepair
*p
= q
->queue
[i
];
1064 struct module_cb
*temp
;
1066 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1068 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1069 temp
->mod_src
= p
->one
->mode
;
1070 temp
->mod_dst
= p
->two
->mode
;
1071 temp
->oid_src
= p
->one
->oid
;
1072 temp
->oid_dst
= p
->two
->oid
;
1073 temp
->status
= p
->status
;
1074 temp
->sm_path
= xstrdup(p
->one
->path
);
1076 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1077 list
->entries
[list
->nr
++] = temp
;
1081 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1084 case DIFF_INDEX
: return "diff-index";
1085 case DIFF_FILES
: return "diff-files";
1086 default: BUG("bad diff_cmd value %d", diff_cmd
);
1090 static int compute_summary_module_list(struct object_id
*head_oid
,
1091 struct summary_cb
*info
,
1092 enum diff_cmd diff_cmd
)
1094 struct strvec diff_args
= STRVEC_INIT
;
1095 struct rev_info rev
;
1096 struct setup_revision_opt opt
= {
1097 .free_removed_argv_elements
= 1,
1099 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1102 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1104 strvec_push(&diff_args
, "--cached");
1105 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1107 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1108 strvec_push(&diff_args
, "--");
1110 strvec_pushv(&diff_args
, info
->argv
);
1112 git_config(git_diff_basic_config
, NULL
);
1113 init_revisions(&rev
, info
->prefix
);
1115 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1116 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1117 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1118 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1119 rev
.diffopt
.format_callback_data
= &list
;
1121 if (!info
->cached
) {
1122 if (diff_cmd
== DIFF_INDEX
)
1124 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1125 perror("repo_read_index_preload");
1129 } else if (repo_read_index(the_repository
) < 0) {
1130 perror("repo_read_cache");
1135 if (diff_cmd
== DIFF_INDEX
)
1136 run_diff_index(&rev
, info
->cached
);
1138 run_diff_files(&rev
, 0);
1139 prepare_submodule_summary(info
, &list
);
1141 strvec_clear(&diff_args
);
1142 release_revisions(&rev
);
1143 module_cb_list_release(&list
);
1147 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1149 struct summary_cb info
= SUMMARY_CB_INIT
;
1153 int summary_limit
= -1;
1154 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1155 struct object_id head_oid
;
1157 struct option module_summary_options
[] = {
1158 OPT_BOOL(0, "cached", &cached
,
1159 N_("use the commit stored in the index instead of the submodule HEAD")),
1160 OPT_BOOL(0, "files", &files
,
1161 N_("compare the commit in the index with that in the submodule HEAD")),
1162 OPT_BOOL(0, "for-status", &for_status
,
1163 N_("skip submodules with 'ignore_config' value set to 'all'")),
1164 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1165 N_("limit the summary size")),
1168 const char *const git_submodule_helper_usage
[] = {
1169 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1173 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1174 git_submodule_helper_usage
, 0);
1179 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1184 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1185 /* before the first commit: compare with an empty tree */
1186 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1192 if (get_oid("HEAD", &head_oid
))
1193 die(_("could not fetch a revision for HEAD"));
1198 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1199 diff_cmd
= DIFF_FILES
;
1204 info
.prefix
= prefix
;
1205 info
.cached
= !!cached
;
1206 info
.files
= !!files
;
1207 info
.for_status
= !!for_status
;
1208 info
.summary_limit
= summary_limit
;
1210 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1217 const char *super_prefix
;
1220 #define SYNC_CB_INIT { 0 }
1222 static void sync_submodule(const char *path
, const char *prefix
,
1223 const char *super_prefix
, unsigned int flags
)
1225 const struct submodule
*sub
;
1226 char *remote_key
= NULL
;
1227 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1228 struct strbuf sb
= STRBUF_INIT
;
1229 char *sub_config_path
= NULL
;
1232 if (!is_submodule_active(the_repository
, path
))
1235 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1237 if (sub
&& sub
->url
) {
1238 if (starts_with_dot_dot_slash(sub
->url
) ||
1239 starts_with_dot_slash(sub
->url
)) {
1240 char *up_path
= get_up_path(path
);
1242 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1243 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1246 sub_origin_url
= xstrdup(sub
->url
);
1247 super_config_url
= xstrdup(sub
->url
);
1250 sub_origin_url
= xstrdup("");
1251 super_config_url
= xstrdup("");
1254 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1256 if (!(flags
& OPT_QUIET
))
1257 printf(_("Synchronizing submodule url for '%s'\n"),
1261 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1262 if (git_config_set_gently(sb
.buf
, super_config_url
))
1263 die(_("failed to register url for submodule path '%s'"),
1266 if (!is_submodule_populated_gently(path
, NULL
))
1270 code
= get_default_remote_submodule(path
, &default_remote
);
1274 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1275 free(default_remote
);
1277 submodule_to_gitdir(&sb
, path
);
1278 strbuf_addstr(&sb
, "/config");
1280 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1281 die(_("failed to update remote for submodule '%s'"),
1284 if (flags
& OPT_RECURSIVE
) {
1285 struct child_process cpr
= CHILD_PROCESS_INIT
;
1289 prepare_submodule_repo_env(&cpr
.env
);
1291 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1292 "--recursive", NULL
);
1293 strvec_push(&cpr
.args
, "--super-prefix");
1294 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1297 if (flags
& OPT_QUIET
)
1298 strvec_push(&cpr
.args
, "--quiet");
1300 if (run_command(&cpr
))
1301 die(_("failed to recurse into submodule '%s'"),
1306 free(super_config_url
);
1307 free(sub_origin_url
);
1308 strbuf_release(&sb
);
1311 free(sub_config_path
);
1314 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1316 struct sync_cb
*info
= cb_data
;
1318 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1322 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1324 struct sync_cb info
= SYNC_CB_INIT
;
1325 struct pathspec pathspec
= { 0 };
1326 struct module_list list
= MODULE_LIST_INIT
;
1329 struct option module_sync_options
[] = {
1330 OPT__SUPER_PREFIX(&info
.super_prefix
),
1331 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1332 OPT_BOOL(0, "recursive", &recursive
,
1333 N_("recurse into nested submodules")),
1336 const char *const git_submodule_helper_usage
[] = {
1337 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1342 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1343 git_submodule_helper_usage
, 0);
1345 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1348 info
.prefix
= prefix
;
1350 info
.flags
|= OPT_QUIET
;
1352 info
.flags
|= OPT_RECURSIVE
;
1354 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1358 module_list_release(&list
);
1359 clear_pathspec(&pathspec
);
1367 #define DEINIT_CB_INIT { 0 }
1369 static void deinit_submodule(const char *path
, const char *prefix
,
1372 const struct submodule
*sub
;
1373 char *displaypath
= NULL
;
1374 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1375 struct strbuf sb_config
= STRBUF_INIT
;
1376 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1378 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1380 if (!sub
|| !sub
->name
)
1383 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1385 /* remove the submodule work tree (unless the user already did it) */
1386 if (is_directory(path
)) {
1387 struct strbuf sb_rm
= STRBUF_INIT
;
1390 if (is_directory(sub_git_dir
)) {
1391 if (!(flags
& OPT_QUIET
))
1392 warning(_("Submodule work tree '%s' contains a .git "
1393 "directory. This will be replaced with a "
1394 ".git file by using absorbgitdirs."),
1397 absorb_git_dir_into_superproject(path
, NULL
);
1401 if (!(flags
& OPT_FORCE
)) {
1402 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1405 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1408 if (run_command(&cp_rm
))
1409 die(_("Submodule work tree '%s' contains local "
1410 "modifications; use '-f' to discard them"),
1414 strbuf_addstr(&sb_rm
, path
);
1416 if (!remove_dir_recursively(&sb_rm
, 0))
1417 format
= _("Cleared directory '%s'\n");
1419 format
= _("Could not remove submodule work tree '%s'\n");
1421 if (!(flags
& OPT_QUIET
))
1422 printf(format
, displaypath
);
1424 submodule_unset_core_worktree(sub
);
1426 strbuf_release(&sb_rm
);
1429 if (mkdir(path
, 0777))
1430 printf(_("could not create empty submodule directory %s"),
1433 cp_config
.git_cmd
= 1;
1434 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1435 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1437 /* remove the .git/config entries (unless the user already did it) */
1438 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1439 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1442 * remove the whole section so we have a clean state when
1443 * the user later decides to init this submodule again
1445 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1446 if (!(flags
& OPT_QUIET
))
1447 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1448 sub
->name
, sub
->url
, displaypath
);
1455 strbuf_release(&sb_config
);
1458 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1461 struct deinit_cb
*info
= cb_data
;
1462 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1465 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1467 struct deinit_cb info
= DEINIT_CB_INIT
;
1468 struct pathspec pathspec
= { 0 };
1469 struct module_list list
= MODULE_LIST_INIT
;
1473 struct option module_deinit_options
[] = {
1474 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1475 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1476 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1479 const char *const git_submodule_helper_usage
[] = {
1480 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1485 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1486 git_submodule_helper_usage
, 0);
1489 error("pathspec and --all are incompatible");
1490 usage_with_options(git_submodule_helper_usage
,
1491 module_deinit_options
);
1495 die(_("Use '--all' if you really want to deinitialize all submodules"));
1497 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1500 info
.prefix
= prefix
;
1502 info
.flags
|= OPT_QUIET
;
1504 info
.flags
|= OPT_FORCE
;
1506 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1510 module_list_release(&list
);
1511 clear_pathspec(&pathspec
);
1515 struct module_clone_data
{
1521 struct list_objects_filter_options
*filter_options
;
1522 unsigned int quiet
: 1;
1523 unsigned int progress
: 1;
1524 unsigned int dissociate
: 1;
1525 unsigned int require_init
: 1;
1528 #define MODULE_CLONE_DATA_INIT { \
1529 .single_branch = -1, \
1532 struct submodule_alternate_setup
{
1533 const char *submodule_name
;
1534 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1535 SUBMODULE_ALTERNATE_ERROR_DIE
,
1536 SUBMODULE_ALTERNATE_ERROR_INFO
,
1537 SUBMODULE_ALTERNATE_ERROR_IGNORE
1539 struct string_list
*reference
;
1541 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1542 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1545 static const char alternate_error_advice
[] = N_(
1546 "An alternate computed from a superproject's alternate is invalid.\n"
1547 "To allow Git to clone without an alternate in such a case, set\n"
1548 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1549 "'--reference-if-able' instead of '--reference'."
1552 static int add_possible_reference_from_superproject(
1553 struct object_directory
*odb
, void *sas_cb
)
1555 struct submodule_alternate_setup
*sas
= sas_cb
;
1559 * If the alternate object store is another repository, try the
1560 * standard layout with .git/(modules/<name>)+/objects
1562 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1563 struct repository alternate
;
1565 struct strbuf sb
= STRBUF_INIT
;
1566 struct strbuf err
= STRBUF_INIT
;
1567 strbuf_add(&sb
, odb
->path
, len
);
1569 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1570 die(_("could not get a repository handle for gitdir '%s'"),
1574 * We need to end the new path with '/' to mark it as a dir,
1575 * otherwise a submodule name containing '/' will be broken
1576 * as the last part of a missing submodule reference would
1577 * be taken as a file name.
1580 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1581 strbuf_addch(&sb
, '/');
1582 repo_clear(&alternate
);
1584 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1586 char *p
= strbuf_detach(&sb
, NULL
);
1588 string_list_append(sas
->reference
, p
)->util
= p
;
1591 switch (sas
->error_mode
) {
1592 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1593 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1594 advise(_(alternate_error_advice
));
1595 die(_("submodule '%s' cannot add alternate: %s"),
1596 sas
->submodule_name
, err
.buf
);
1597 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1598 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1599 sas
->submodule_name
, err
.buf
);
1600 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1604 strbuf_release(&sb
);
1610 static void prepare_possible_alternates(const char *sm_name
,
1611 struct string_list
*reference
)
1613 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1614 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1616 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1620 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1622 if (!error_strategy
)
1623 error_strategy
= xstrdup("die");
1625 sas
.submodule_name
= sm_name
;
1626 sas
.reference
= reference
;
1627 if (!strcmp(error_strategy
, "die"))
1628 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1629 else if (!strcmp(error_strategy
, "info"))
1630 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1631 else if (!strcmp(error_strategy
, "ignore"))
1632 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1634 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1636 if (!strcmp(sm_alternate
, "superproject"))
1637 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1638 else if (!strcmp(sm_alternate
, "no"))
1641 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1644 free(error_strategy
);
1647 static char *clone_submodule_sm_gitdir(const char *name
)
1649 struct strbuf sb
= STRBUF_INIT
;
1652 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1653 sm_gitdir
= absolute_pathdup(sb
.buf
);
1654 strbuf_release(&sb
);
1659 static int clone_submodule(const struct module_clone_data
*clone_data
,
1660 struct string_list
*reference
)
1663 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1664 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1665 struct child_process cp
= CHILD_PROCESS_INIT
;
1666 const char *clone_data_path
= clone_data
->path
;
1667 char *to_free
= NULL
;
1669 if (!is_absolute_path(clone_data
->path
))
1670 clone_data_path
= to_free
= xstrfmt("%s/%s", get_git_work_tree(),
1673 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1674 die(_("refusing to create/use '%s' in another submodule's "
1675 "git dir"), sm_gitdir
);
1677 if (!file_exists(sm_gitdir
)) {
1678 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1679 die(_("could not create directory '%s'"), sm_gitdir
);
1681 prepare_possible_alternates(clone_data
->name
, reference
);
1683 strvec_push(&cp
.args
, "clone");
1684 strvec_push(&cp
.args
, "--no-checkout");
1685 if (clone_data
->quiet
)
1686 strvec_push(&cp
.args
, "--quiet");
1687 if (clone_data
->progress
)
1688 strvec_push(&cp
.args
, "--progress");
1689 if (clone_data
->depth
&& *(clone_data
->depth
))
1690 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1691 if (reference
->nr
) {
1692 struct string_list_item
*item
;
1694 for_each_string_list_item(item
, reference
)
1695 strvec_pushl(&cp
.args
, "--reference",
1696 item
->string
, NULL
);
1698 if (clone_data
->dissociate
)
1699 strvec_push(&cp
.args
, "--dissociate");
1700 if (sm_gitdir
&& *sm_gitdir
)
1701 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1702 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1703 strvec_pushf(&cp
.args
, "--filter=%s",
1704 expand_list_objects_filter_spec(
1705 clone_data
->filter_options
));
1706 if (clone_data
->single_branch
>= 0)
1707 strvec_push(&cp
.args
, clone_data
->single_branch
?
1709 "--no-single-branch");
1711 strvec_push(&cp
.args
, "--");
1712 strvec_push(&cp
.args
, clone_data
->url
);
1713 strvec_push(&cp
.args
, clone_data_path
);
1716 prepare_submodule_repo_env(&cp
.env
);
1719 if(run_command(&cp
))
1720 die(_("clone of '%s' into submodule path '%s' failed"),
1721 clone_data
->url
, clone_data_path
);
1725 if (clone_data
->require_init
&& !access(clone_data_path
, X_OK
) &&
1726 !is_empty_dir(clone_data_path
))
1727 die(_("directory not empty: '%s'"), clone_data_path
);
1728 if (safe_create_leading_directories_const(clone_data_path
) < 0)
1729 die(_("could not create directory '%s'"), clone_data_path
);
1730 path
= xstrfmt("%s/index", sm_gitdir
);
1731 unlink_or_warn(path
);
1735 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1737 p
= git_pathdup_submodule(clone_data_path
, "config");
1739 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1741 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1742 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1744 git_config_set_in_file(p
, "submodule.alternateLocation",
1746 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1748 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1752 free(error_strategy
);
1760 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1762 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1763 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1764 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1765 struct list_objects_filter_options filter_options
=
1766 LIST_OBJECTS_FILTER_INIT
;
1768 struct option module_clone_options
[] = {
1769 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1771 N_("alternative anchor for relative paths")),
1772 OPT_STRING(0, "path", &clone_data
.path
,
1774 N_("where the new submodule will be cloned to")),
1775 OPT_STRING(0, "name", &clone_data
.name
,
1777 N_("name of the new submodule")),
1778 OPT_STRING(0, "url", &clone_data
.url
,
1780 N_("url where to clone the submodule from")),
1781 OPT_STRING_LIST(0, "reference", &reference
,
1783 N_("reference repository")),
1784 OPT_BOOL(0, "dissociate", &dissociate
,
1785 N_("use --reference only while cloning")),
1786 OPT_STRING(0, "depth", &clone_data
.depth
,
1788 N_("depth for shallow clones")),
1789 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1790 OPT_BOOL(0, "progress", &progress
,
1791 N_("force cloning progress")),
1792 OPT_BOOL(0, "require-init", &require_init
,
1793 N_("disallow cloning into non-empty directory")),
1794 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1795 N_("clone only one branch, HEAD or --branch")),
1796 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1799 const char *const git_submodule_helper_usage
[] = {
1800 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1801 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1802 "[--single-branch] [--filter <filter-spec>] "
1803 "--url <url> --path <path>"),
1807 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1808 git_submodule_helper_usage
, 0);
1810 clone_data
.dissociate
= !!dissociate
;
1811 clone_data
.quiet
= !!quiet
;
1812 clone_data
.progress
= !!progress
;
1813 clone_data
.require_init
= !!require_init
;
1814 clone_data
.filter_options
= &filter_options
;
1816 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1817 usage_with_options(git_submodule_helper_usage
,
1818 module_clone_options
);
1820 clone_submodule(&clone_data
, &reference
);
1821 list_objects_filter_release(&filter_options
);
1822 string_list_clear(&reference
, 1);
1826 static int determine_submodule_update_strategy(struct repository
*r
,
1829 enum submodule_update_type update
,
1830 struct submodule_update_strategy
*out
)
1832 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1837 key
= xstrfmt("submodule.%s.update", sub
->name
);
1841 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1842 if (parse_submodule_update_strategy(val
, out
) < 0) {
1843 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1847 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1848 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1849 BUG("how did we read update = !command from .gitmodules?");
1850 out
->type
= sub
->update_strategy
.type
;
1851 out
->command
= sub
->update_strategy
.command
;
1853 out
->type
= SM_UPDATE_CHECKOUT
;
1856 (out
->type
== SM_UPDATE_MERGE
||
1857 out
->type
== SM_UPDATE_REBASE
||
1858 out
->type
== SM_UPDATE_NONE
))
1859 out
->type
= SM_UPDATE_CHECKOUT
;
1867 struct update_clone_data
{
1868 const struct submodule
*sub
;
1869 struct object_id oid
;
1870 unsigned just_cloned
;
1873 struct submodule_update_clone
{
1874 /* index into 'update_data.list', the list of submodules to look into for cloning */
1877 /* configuration parameters which are passed on to the children */
1878 const struct update_data
*update_data
;
1880 /* to be consumed by update_submodule() */
1881 struct update_clone_data
*update_clone
;
1882 int update_clone_nr
; int update_clone_alloc
;
1884 /* If we want to stop as fast as possible and return an error */
1885 unsigned quickstop
: 1;
1887 /* failed clones to be retried again */
1888 const struct cache_entry
**failed_clones
;
1889 int failed_clones_nr
, failed_clones_alloc
;
1891 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1893 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1895 free(suc
->update_clone
);
1896 free(suc
->failed_clones
);
1899 struct update_data
{
1901 const char *super_prefix
;
1903 enum submodule_update_type update_default
;
1904 struct object_id suboid
;
1905 struct string_list references
;
1906 struct submodule_update_strategy update_strategy
;
1907 struct list_objects_filter_options
*filter_options
;
1908 struct module_list list
;
1912 int recommend_shallow
;
1913 unsigned int require_init
;
1916 unsigned int nofetch
;
1917 unsigned int remote
;
1918 unsigned int progress
;
1919 unsigned int dissociate
;
1921 unsigned int warn_if_uninitialized
;
1922 unsigned int recursive
;
1924 /* copied over from update_clone_data */
1925 struct object_id oid
;
1926 unsigned int just_cloned
;
1927 const char *sm_path
;
1929 #define UPDATE_DATA_INIT { \
1930 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1931 .list = MODULE_LIST_INIT, \
1932 .recommend_shallow = -1, \
1933 .references = STRING_LIST_INIT_DUP, \
1934 .single_branch = -1, \
1938 static void update_data_release(struct update_data
*ud
)
1940 free(ud
->displaypath
);
1941 module_list_release(&ud
->list
);
1944 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1945 struct strbuf
*out
, const char *displaypath
)
1948 * Only mention uninitialized submodules when their
1949 * paths have been specified.
1951 if (suc
->update_data
->warn_if_uninitialized
) {
1953 _("Submodule path '%s' not initialized"),
1955 strbuf_addch(out
, '\n');
1957 _("Maybe you want to use 'update --init'?"));
1958 strbuf_addch(out
, '\n');
1963 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1964 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1966 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1967 struct child_process
*child
,
1968 struct submodule_update_clone
*suc
,
1971 const struct submodule
*sub
= NULL
;
1972 const char *url
= NULL
;
1973 const char *update_string
;
1974 enum submodule_update_type update_type
;
1976 const struct update_data
*ud
= suc
->update_data
;
1977 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
1979 struct strbuf sb
= STRBUF_INIT
;
1980 int needs_cloning
= 0;
1981 int need_free_url
= 0;
1984 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
1985 strbuf_addch(out
, '\n');
1989 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
1992 next_submodule_warn_missing(suc
, out
, displaypath
);
1996 key
= xstrfmt("submodule.%s.update", sub
->name
);
1997 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
1998 update_type
= parse_submodule_update_type(update_string
);
2000 update_type
= sub
->update_strategy
.type
;
2004 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2005 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2006 && update_type
== SM_UPDATE_NONE
)) {
2007 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2008 strbuf_addch(out
, '\n');
2012 /* Check if the submodule has been initialized. */
2013 if (!is_submodule_active(the_repository
, ce
->name
)) {
2014 next_submodule_warn_missing(suc
, out
, displaypath
);
2019 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2020 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2021 if (starts_with_dot_slash(sub
->url
) ||
2022 starts_with_dot_dot_slash(sub
->url
)) {
2023 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2030 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2031 needs_cloning
= !file_exists(sb
.buf
);
2033 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2034 suc
->update_clone_alloc
);
2035 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2036 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2037 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2038 suc
->update_clone_nr
++;
2044 child
->no_stdin
= 1;
2045 child
->stdout_to_stderr
= 1;
2047 strvec_push(&child
->args
, "submodule--helper");
2048 strvec_push(&child
->args
, "clone");
2049 if (suc
->update_data
->progress
)
2050 strvec_push(&child
->args
, "--progress");
2051 if (suc
->update_data
->quiet
)
2052 strvec_push(&child
->args
, "--quiet");
2053 if (suc
->update_data
->prefix
)
2054 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2055 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2056 strvec_push(&child
->args
, "--depth=1");
2057 else if (suc
->update_data
->depth
)
2058 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2059 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2060 strvec_pushf(&child
->args
, "--filter=%s",
2061 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2062 if (suc
->update_data
->require_init
)
2063 strvec_push(&child
->args
, "--require-init");
2064 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2065 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2066 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2067 if (suc
->update_data
->references
.nr
) {
2068 struct string_list_item
*item
;
2070 for_each_string_list_item(item
, &suc
->update_data
->references
)
2071 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2073 if (suc
->update_data
->dissociate
)
2074 strvec_push(&child
->args
, "--dissociate");
2075 if (suc
->update_data
->single_branch
>= 0)
2076 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2078 "--no-single-branch");
2082 strbuf_release(&sb
);
2086 return needs_cloning
;
2089 static int update_clone_get_next_task(struct child_process
*child
,
2094 struct submodule_update_clone
*suc
= suc_cb
;
2095 const struct cache_entry
*ce
;
2098 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2099 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2100 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2101 int *p
= xmalloc(sizeof(*p
));
2111 * The loop above tried cloning each submodule once, now try the
2112 * stragglers again, which we can imagine as an extension of the
2115 index
= suc
->current
- suc
->update_data
->list
.nr
;
2116 if (index
< suc
->failed_clones_nr
) {
2119 ce
= suc
->failed_clones
[index
];
2120 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2122 strbuf_addstr(err
, "BUG: submodule considered for "
2123 "cloning, doesn't need cloning "
2127 p
= xmalloc(sizeof(*p
));
2137 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2139 void *idx_task_cb UNUSED
)
2141 struct submodule_update_clone
*suc
= suc_cb
;
2147 static int update_clone_task_finished(int result
,
2152 const struct cache_entry
*ce
;
2153 struct submodule_update_clone
*suc
= suc_cb
;
2154 int *idxP
= idx_task_cb
;
2162 if (idx
< suc
->update_data
->list
.nr
) {
2163 ce
= suc
->update_data
->list
.entries
[idx
];
2164 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2166 strbuf_addch(err
, '\n');
2167 ALLOC_GROW(suc
->failed_clones
,
2168 suc
->failed_clones_nr
+ 1,
2169 suc
->failed_clones_alloc
);
2170 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2173 idx
-= suc
->update_data
->list
.nr
;
2174 ce
= suc
->failed_clones
[idx
];
2175 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2177 strbuf_addch(err
, '\n');
2185 static int git_update_clone_config(const char *var
, const char *value
,
2190 if (!strcmp(var
, "submodule.fetchjobs"))
2191 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2195 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2197 struct child_process cp
= CHILD_PROCESS_INIT
;
2198 struct strbuf rev
= STRBUF_INIT
;
2199 char *hex
= oid_to_hex(oid
);
2204 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2206 prepare_submodule_repo_env(&cp
.env
);
2208 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2214 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2215 const struct object_id
*oid
)
2217 struct child_process cp
= CHILD_PROCESS_INIT
;
2219 prepare_submodule_repo_env(&cp
.env
);
2221 cp
.dir
= module_path
;
2223 strvec_push(&cp
.args
, "fetch");
2225 strvec_push(&cp
.args
, "--quiet");
2227 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2229 char *hex
= oid_to_hex(oid
);
2230 char *remote
= get_default_remote();
2232 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2236 return run_command(&cp
);
2239 static int run_update_command(const struct update_data
*ud
, int subforce
)
2241 struct child_process cp
= CHILD_PROCESS_INIT
;
2242 char *oid
= oid_to_hex(&ud
->oid
);
2245 switch (ud
->update_strategy
.type
) {
2246 case SM_UPDATE_CHECKOUT
:
2248 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2250 strvec_push(&cp
.args
, "-f");
2252 case SM_UPDATE_REBASE
:
2254 strvec_push(&cp
.args
, "rebase");
2256 strvec_push(&cp
.args
, "--quiet");
2258 case SM_UPDATE_MERGE
:
2260 strvec_push(&cp
.args
, "merge");
2262 strvec_push(&cp
.args
, "--quiet");
2264 case SM_UPDATE_COMMAND
:
2266 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2269 BUG("unexpected update strategy type: %d",
2270 ud
->update_strategy
.type
);
2272 strvec_push(&cp
.args
, oid
);
2274 cp
.dir
= ud
->sm_path
;
2275 prepare_submodule_repo_env(&cp
.env
);
2276 if ((ret
= run_command(&cp
))) {
2277 switch (ud
->update_strategy
.type
) {
2278 case SM_UPDATE_CHECKOUT
:
2279 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2280 oid
, ud
->displaypath
);
2281 /* No "ret" assignment, use "git checkout"'s */
2283 case SM_UPDATE_REBASE
:
2284 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2285 oid
, ud
->displaypath
);
2287 case SM_UPDATE_MERGE
:
2288 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2289 oid
, ud
->displaypath
);
2291 case SM_UPDATE_COMMAND
:
2292 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2293 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2296 BUG("unexpected update strategy type: %d",
2297 ud
->update_strategy
.type
);
2306 switch (ud
->update_strategy
.type
) {
2307 case SM_UPDATE_CHECKOUT
:
2308 printf(_("Submodule path '%s': checked out '%s'\n"),
2309 ud
->displaypath
, oid
);
2311 case SM_UPDATE_REBASE
:
2312 printf(_("Submodule path '%s': rebased into '%s'\n"),
2313 ud
->displaypath
, oid
);
2315 case SM_UPDATE_MERGE
:
2316 printf(_("Submodule path '%s': merged in '%s'\n"),
2317 ud
->displaypath
, oid
);
2319 case SM_UPDATE_COMMAND
:
2320 printf(_("Submodule path '%s': '%s %s'\n"),
2321 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2324 BUG("unexpected update strategy type: %d",
2325 ud
->update_strategy
.type
);
2331 static int run_update_procedure(const struct update_data
*ud
)
2333 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2337 * Run fetch only if `oid` isn't present or it
2338 * is not reachable from a ref.
2340 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2341 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2344 _("Unable to fetch in submodule path '%s'; "
2345 "trying to directly fetch %s:"),
2346 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2348 * Now we tried the usual fetch, but `oid` may
2349 * not be reachable from any of the refs.
2351 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2352 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2353 return die_message(_("Fetched in submodule path '%s', but it did not "
2354 "contain %s. Direct fetching of that commit failed."),
2355 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2358 return run_update_command(ud
, subforce
);
2361 static int remote_submodule_branch(const char *path
, const char **branch
)
2363 const struct submodule
*sub
;
2367 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2369 return die_message(_("could not initialize submodule at path '%s'"),
2372 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2373 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2374 *branch
= sub
->branch
;
2382 if (!strcmp(*branch
, ".")) {
2383 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2386 return die_message(_("No such ref: %s"), "HEAD");
2389 if (!strcmp(refname
, "HEAD"))
2390 return die_message(_("Submodule (%s) branch configured to inherit "
2391 "branch from superproject, but the superproject "
2392 "is not on any branch"), sub
->name
);
2394 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2395 return die_message(_("Expecting a full ref name, got %s"),
2402 /* Our "branch" is coming from repo_config_get_string_tmp() */
2406 static int ensure_core_worktree(const char *path
)
2409 struct repository subrepo
;
2411 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2412 return die_message(_("could not get a repository handle for submodule '%s'"),
2415 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2416 char *cfg_file
, *abs_path
;
2417 const char *rel_path
;
2418 struct strbuf sb
= STRBUF_INIT
;
2420 cfg_file
= repo_git_path(&subrepo
, "config");
2422 abs_path
= absolute_pathdup(path
);
2423 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2425 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2429 strbuf_release(&sb
);
2432 repo_clear(&subrepo
);
2436 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2439 case SM_UPDATE_CHECKOUT
:
2441 case SM_UPDATE_MERGE
:
2443 case SM_UPDATE_REBASE
:
2445 case SM_UPDATE_UNSPECIFIED
:
2446 case SM_UPDATE_NONE
:
2447 case SM_UPDATE_COMMAND
:
2450 BUG("unreachable with type %d", type
);
2453 static void update_data_to_args(const struct update_data
*update_data
,
2454 struct strvec
*args
)
2456 enum submodule_update_type update_type
= update_data
->update_default
;
2458 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2459 if (update_data
->displaypath
) {
2460 strvec_push(args
, "--super-prefix");
2461 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2463 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2464 if (update_data
->quiet
)
2465 strvec_push(args
, "--quiet");
2466 if (update_data
->force
)
2467 strvec_push(args
, "--force");
2468 if (update_data
->init
)
2469 strvec_push(args
, "--init");
2470 if (update_data
->remote
)
2471 strvec_push(args
, "--remote");
2472 if (update_data
->nofetch
)
2473 strvec_push(args
, "--no-fetch");
2474 if (update_data
->dissociate
)
2475 strvec_push(args
, "--dissociate");
2476 if (update_data
->progress
)
2477 strvec_push(args
, "--progress");
2478 if (update_data
->require_init
)
2479 strvec_push(args
, "--require-init");
2480 if (update_data
->depth
)
2481 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2482 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2483 strvec_pushf(args
, "--%s",
2484 submodule_update_type_to_label(update_type
));
2486 if (update_data
->references
.nr
) {
2487 struct string_list_item
*item
;
2489 for_each_string_list_item(item
, &update_data
->references
)
2490 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2492 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2493 strvec_pushf(args
, "--filter=%s",
2494 expand_list_objects_filter_spec(
2495 update_data
->filter_options
));
2496 if (update_data
->recommend_shallow
== 0)
2497 strvec_push(args
, "--no-recommend-shallow");
2498 else if (update_data
->recommend_shallow
== 1)
2499 strvec_push(args
, "--recommend-shallow");
2500 if (update_data
->single_branch
>= 0)
2501 strvec_push(args
, update_data
->single_branch
?
2503 "--no-single-branch");
2506 static int update_submodule(struct update_data
*update_data
)
2510 ret
= determine_submodule_update_strategy(the_repository
,
2511 update_data
->just_cloned
,
2512 update_data
->sm_path
,
2513 update_data
->update_default
,
2514 &update_data
->update_strategy
);
2518 if (update_data
->just_cloned
)
2519 oidcpy(&update_data
->suboid
, null_oid());
2520 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2521 return die_message(_("Unable to find current revision in submodule path '%s'"),
2522 update_data
->displaypath
);
2524 if (update_data
->remote
) {
2530 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2533 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2536 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2540 if (!update_data
->nofetch
) {
2541 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2543 return die_message(_("Unable to fetch in submodule path '%s'"),
2544 update_data
->sm_path
);
2547 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2548 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2549 remote_ref
, update_data
->sm_path
);
2554 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2555 ret
= run_update_procedure(update_data
);
2560 if (update_data
->recursive
) {
2561 struct child_process cp
= CHILD_PROCESS_INIT
;
2562 struct update_data next
= *update_data
;
2565 oidcpy(&next
.oid
, null_oid());
2566 oidcpy(&next
.suboid
, null_oid());
2568 cp
.dir
= update_data
->sm_path
;
2570 prepare_submodule_repo_env(&cp
.env
);
2571 update_data_to_args(&next
, &cp
.args
);
2573 ret
= run_command(&cp
);
2575 die_message(_("Failed to recurse into submodule path '%s'"),
2576 update_data
->displaypath
);
2583 static int update_submodules(struct update_data
*update_data
)
2586 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2587 const struct run_process_parallel_opts opts
= {
2588 .tr2_category
= "submodule",
2589 .tr2_label
= "parallel/update",
2591 .processes
= update_data
->max_jobs
,
2593 .get_next_task
= update_clone_get_next_task
,
2594 .start_failure
= update_clone_start_failure
,
2595 .task_finished
= update_clone_task_finished
,
2599 suc
.update_data
= update_data
;
2600 run_processes_parallel(&opts
);
2603 * We saved the output and put it out all at once now.
2605 * - the listener does not have to interleave their (checkout)
2606 * work with our fetching. The writes involved in a
2607 * checkout involve more straightforward sequential I/O.
2608 * - the listener can avoid doing any work if fetching failed.
2610 if (suc
.quickstop
) {
2615 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2616 struct update_clone_data ucd
= suc
.update_clone
[i
];
2619 oidcpy(&update_data
->oid
, &ucd
.oid
);
2620 update_data
->just_cloned
= ucd
.just_cloned
;
2621 update_data
->sm_path
= ucd
.sub
->path
;
2623 code
= ensure_core_worktree(update_data
->sm_path
);
2627 update_data
->displaypath
= get_submodule_displaypath(
2628 update_data
->sm_path
, update_data
->prefix
,
2629 update_data
->super_prefix
);
2630 code
= update_submodule(update_data
);
2631 FREE_AND_NULL(update_data
->displaypath
);
2641 submodule_update_clone_release(&suc
);
2642 string_list_clear(&update_data
->references
, 0);
2646 static int module_update(int argc
, const char **argv
, const char *prefix
)
2648 struct pathspec pathspec
= { 0 };
2649 struct pathspec pathspec2
= { 0 };
2650 struct update_data opt
= UPDATE_DATA_INIT
;
2651 struct list_objects_filter_options filter_options
=
2652 LIST_OBJECTS_FILTER_INIT
;
2654 struct option module_update_options
[] = {
2655 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2656 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2657 OPT_BOOL(0, "init", &opt
.init
,
2658 N_("initialize uninitialized submodules before update")),
2659 OPT_BOOL(0, "remote", &opt
.remote
,
2660 N_("use SHA-1 of submodule's remote tracking branch")),
2661 OPT_BOOL(0, "recursive", &opt
.recursive
,
2662 N_("traverse submodules recursively")),
2663 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2664 N_("don't fetch new objects from the remote site")),
2665 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2666 N_("use the 'checkout' update strategy (default)"),
2667 SM_UPDATE_CHECKOUT
),
2668 OPT_SET_INT('m', "merge", &opt
.update_default
,
2669 N_("use the 'merge' update strategy"),
2671 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2672 N_("use the 'rebase' update strategy"),
2674 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2675 N_("reference repository")),
2676 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2677 N_("use --reference only while cloning")),
2678 OPT_INTEGER(0, "depth", &opt
.depth
,
2679 N_("create a shallow clone truncated to the "
2680 "specified number of revisions")),
2681 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2682 N_("parallel jobs")),
2683 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2684 N_("whether the initial clone should follow the shallow recommendation")),
2685 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2686 OPT_BOOL(0, "progress", &opt
.progress
,
2687 N_("force cloning progress")),
2688 OPT_BOOL(0, "require-init", &opt
.require_init
,
2689 N_("disallow cloning into non-empty directory, implies --init")),
2690 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2691 N_("clone only one branch, HEAD or --branch")),
2692 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2695 const char *const git_submodule_helper_usage
[] = {
2696 N_("git submodule [--quiet] update"
2697 " [--init [--filter=<filter-spec>]] [--remote]"
2698 " [-N|--no-fetch] [-f|--force]"
2699 " [--checkout|--merge|--rebase]"
2700 " [--[no-]recommend-shallow] [--reference <repository>]"
2701 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2705 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2706 git_config(git_update_clone_config
, &opt
.max_jobs
);
2708 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2709 git_submodule_helper_usage
, 0);
2711 if (opt
.require_init
)
2714 if (filter_options
.choice
&& !opt
.init
) {
2715 usage_with_options(git_submodule_helper_usage
,
2716 module_update_options
);
2719 opt
.filter_options
= &filter_options
;
2720 opt
.prefix
= prefix
;
2722 if (opt
.update_default
)
2723 opt
.update_strategy
.type
= opt
.update_default
;
2725 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2731 opt
.warn_if_uninitialized
= 1;
2734 struct module_list list
= MODULE_LIST_INIT
;
2735 struct init_cb info
= INIT_CB_INIT
;
2737 if (module_list_compute(argv
, opt
.prefix
,
2738 &pathspec2
, &list
) < 0) {
2739 module_list_release(&list
);
2745 * If there are no path args and submodule.active is set then,
2746 * by default, only initialize 'active' modules.
2748 if (!argc
&& !git_config_get("submodule.active"))
2749 module_list_active(&list
);
2751 info
.prefix
= opt
.prefix
;
2752 info
.super_prefix
= opt
.super_prefix
;
2754 info
.flags
|= OPT_QUIET
;
2756 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2757 module_list_release(&list
);
2760 ret
= update_submodules(&opt
);
2762 update_data_release(&opt
);
2763 list_objects_filter_release(&filter_options
);
2764 clear_pathspec(&pathspec
);
2765 clear_pathspec(&pathspec2
);
2769 static int push_check(int argc
, const char **argv
, const char *prefix UNUSED
)
2771 struct remote
*remote
;
2772 const char *superproject_head
;
2774 int detached_head
= 0;
2775 struct object_id head_oid
;
2778 die("submodule--helper push-check requires at least 2 arguments");
2781 * superproject's resolved head ref.
2782 * if HEAD then the superproject is in a detached head state, otherwise
2783 * it will be the resolved head ref.
2785 superproject_head
= argv
[1];
2788 /* Get the submodule's head ref and determine if it is detached */
2789 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2791 die(_("Failed to resolve HEAD as a valid ref."));
2792 if (!strcmp(head
, "HEAD"))
2796 * The remote must be configured.
2797 * This is to avoid pushing to the exact same URL as the parent.
2799 remote
= pushremote_get(argv
[1]);
2800 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2801 die("remote '%s' not configured", argv
[1]);
2803 /* Check the refspec */
2806 struct ref
*local_refs
= get_local_heads();
2807 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2809 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2811 for (i
= 0; i
< refspec
.nr
; i
++) {
2812 const struct refspec_item
*rs
= &refspec
.items
[i
];
2814 if (rs
->pattern
|| rs
->matching
)
2817 /* LHS must match a single ref */
2818 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2823 * If LHS matches 'HEAD' then we need to ensure
2824 * that it matches the same named branch
2825 * checked out in the superproject.
2827 if (!strcmp(rs
->src
, "HEAD")) {
2828 if (!detached_head
&&
2829 !strcmp(head
, superproject_head
))
2831 die("HEAD does not match the named branch in the superproject");
2835 die("src refspec '%s' must name a ref",
2839 refspec_clear(&refspec
);
2846 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2849 struct pathspec pathspec
= { 0 };
2850 struct module_list list
= MODULE_LIST_INIT
;
2851 const char *super_prefix
= NULL
;
2852 struct option embed_gitdir_options
[] = {
2853 OPT__SUPER_PREFIX(&super_prefix
),
2856 const char *const git_submodule_helper_usage
[] = {
2857 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2862 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2863 git_submodule_helper_usage
, 0);
2865 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
2868 for (i
= 0; i
< list
.nr
; i
++)
2869 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
2874 clear_pathspec(&pathspec
);
2875 module_list_release(&list
);
2879 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2885 struct option options
[] = {
2886 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2889 const char *const usage
[] = {
2890 N_("git submodule set-url [--quiet] <path> <newurl>"),
2894 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2896 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2897 usage_with_options(usage
, options
);
2899 config_name
= xstrfmt("submodule.%s.url", path
);
2901 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2902 sync_submodule(path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
2909 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2911 int opt_default
= 0, ret
;
2912 const char *opt_branch
= NULL
;
2915 struct option options
[] = {
2917 * We accept the `quiet` option for uniformity across subcommands,
2918 * though there is nothing to make less verbose in this subcommand.
2920 OPT_NOOP_NOARG('q', "quiet"),
2922 OPT_BOOL('d', "default", &opt_default
,
2923 N_("set the default tracking branch to master")),
2924 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2925 N_("set the default tracking branch")),
2928 const char *const usage
[] = {
2929 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2930 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2934 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2936 if (!opt_branch
&& !opt_default
)
2937 die(_("--branch or --default required"));
2939 if (opt_branch
&& opt_default
)
2940 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2942 if (argc
!= 1 || !(path
= argv
[0]))
2943 usage_with_options(usage
, options
);
2945 config_name
= xstrfmt("submodule.%s.branch", path
);
2946 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2952 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
2954 enum branch_track track
;
2955 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
2956 struct option options
[] = {
2957 OPT__QUIET(&quiet
, N_("print only error messages")),
2958 OPT__FORCE(&force
, N_("force creation"), 0),
2959 OPT_BOOL(0, "create-reflog", &reflog
,
2960 N_("create the branch's reflog")),
2961 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
2962 N_("set branch tracking configuration"),
2964 parse_opt_tracking_mode
),
2965 OPT__DRY_RUN(&dry_run
,
2966 N_("show whether the branch would be created")),
2969 const char *const usage
[] = {
2970 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2974 git_config(git_default_config
, NULL
);
2975 track
= git_branch_track
;
2976 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2979 usage_with_options(usage
, options
);
2981 if (!quiet
&& !dry_run
)
2982 printf_ln(_("creating branch '%s'"), argv
[0]);
2984 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
2985 force
, reflog
, quiet
, track
, dry_run
);
2992 const char *reference_path
;
2994 const char *sm_name
;
2996 const char *realrepo
;
2998 unsigned int force
: 1;
2999 unsigned int quiet
: 1;
3000 unsigned int progress
: 1;
3001 unsigned int dissociate
: 1;
3003 #define ADD_DATA_INIT { .depth = -1 }
3005 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3007 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3008 struct strbuf sb_remote_out
= STRBUF_INIT
;
3010 cp_remote
.git_cmd
= 1;
3011 strvec_pushf(&cp_remote
.env
,
3012 "GIT_DIR=%s", git_dir_path
);
3013 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3014 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3015 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3017 char *line
= sb_remote_out
.buf
;
3019 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3020 size_t len
= next_line
- line
;
3022 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3023 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3024 line
= next_line
+ 1;
3028 strbuf_release(&sb_remote_out
);
3031 static int add_submodule(const struct add_data
*add_data
)
3033 char *submod_gitdir_path
;
3034 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3035 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3038 /* perhaps the path already exists and is already a git repo, else clone it */
3039 if (is_directory(add_data
->sm_path
)) {
3040 struct strbuf sm_path
= STRBUF_INIT
;
3041 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3042 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3043 if (is_nonbare_repository_dir(&sm_path
))
3044 printf(_("Adding existing repo at '%s' to the index\n"),
3047 die(_("'%s' already exists and is not a valid git repo"),
3049 strbuf_release(&sm_path
);
3050 free(submod_gitdir_path
);
3052 struct child_process cp
= CHILD_PROCESS_INIT
;
3054 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3056 if (is_directory(submod_gitdir_path
)) {
3057 if (!add_data
->force
) {
3058 struct strbuf msg
= STRBUF_INIT
;
3061 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3062 "locally with remote(s):\n"),
3065 append_fetch_remotes(&msg
, submod_gitdir_path
);
3066 free(submod_gitdir_path
);
3068 strbuf_addf(&msg
, _("If you want to reuse this local git "
3069 "directory instead of cloning again from\n"
3071 "use the '--force' option. If the local git "
3072 "directory is not the correct repo\n"
3073 "or you are unsure what this means choose "
3074 "another name with the '--name' option."),
3075 add_data
->realrepo
);
3077 die_msg
= strbuf_detach(&msg
, NULL
);
3080 printf(_("Reactivating local git directory for "
3081 "submodule '%s'\n"), add_data
->sm_name
);
3084 free(submod_gitdir_path
);
3086 clone_data
.prefix
= add_data
->prefix
;
3087 clone_data
.path
= add_data
->sm_path
;
3088 clone_data
.name
= add_data
->sm_name
;
3089 clone_data
.url
= add_data
->realrepo
;
3090 clone_data
.quiet
= add_data
->quiet
;
3091 clone_data
.progress
= add_data
->progress
;
3092 if (add_data
->reference_path
) {
3093 char *p
= xstrdup(add_data
->reference_path
);
3095 string_list_append(&reference
, p
)->util
= p
;
3097 clone_data
.dissociate
= add_data
->dissociate
;
3098 if (add_data
->depth
>= 0)
3099 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3101 if (clone_submodule(&clone_data
, &reference
))
3104 prepare_submodule_repo_env(&cp
.env
);
3106 cp
.dir
= add_data
->sm_path
;
3108 * NOTE: we only get here if add_data->force is true, so
3109 * passing --force to checkout is reasonable.
3111 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3113 if (add_data
->branch
) {
3114 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3115 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3118 if (run_command(&cp
))
3119 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3123 string_list_clear(&reference
, 1);
3127 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3132 if (!is_writing_gitmodules_ok())
3133 die(_("please make sure that the .gitmodules file is in the working tree"));
3135 key
= xstrfmt("submodule.%s.%s", name
, var
);
3136 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3142 static void configure_added_submodule(struct add_data
*add_data
)
3145 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3146 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3148 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3149 git_config_set_gently(key
, add_data
->realrepo
);
3152 add_submod
.git_cmd
= 1;
3153 strvec_pushl(&add_submod
.args
, "add",
3154 "--no-warn-embedded-repo", NULL
);
3155 if (add_data
->force
)
3156 strvec_push(&add_submod
.args
, "--force");
3157 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3159 if (run_command(&add_submod
))
3160 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3162 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3163 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3164 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3166 if (add_data
->branch
) {
3167 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3168 "branch", add_data
->branch
))
3169 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3172 add_gitmodules
.git_cmd
= 1;
3173 strvec_pushl(&add_gitmodules
.args
,
3174 "add", "--force", "--", ".gitmodules", NULL
);
3176 if (run_command(&add_gitmodules
))
3177 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3180 * NEEDSWORK: In a multi-working-tree world this needs to be
3181 * set in the per-worktree config.
3184 * NEEDSWORK: In the longer run, we need to get rid of this
3185 * pattern of querying "submodule.active" before calling
3186 * is_submodule_active(), since that function needs to find
3187 * out the value of "submodule.active" again anyway.
3189 if (!git_config_get("submodule.active")) {
3191 * If the submodule being added isn't already covered by the
3192 * current configured pathspec, set the submodule's active flag
3194 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3195 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3196 git_config_set_gently(key
, "true");
3200 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3201 git_config_set_gently(key
, "true");
3206 static void die_on_index_match(const char *path
, int force
)
3209 const char *args
[] = { path
, NULL
};
3210 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3212 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3213 die(_("index file corrupt"));
3217 char *ps_matched
= xcalloc(ps
.nr
, 1);
3219 /* TODO: audit for interaction with sparse-index. */
3220 ensure_full_index(&the_index
);
3223 * Since there is only one pathspec, we just need to
3224 * check ps_matched[0] to know if a cache entry matched.
3226 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
3227 ce_path_match(&the_index
, the_index
.cache
[i
], &ps
,
3230 if (ps_matched
[0]) {
3232 die(_("'%s' already exists in the index"),
3234 if (!S_ISGITLINK(the_index
.cache
[i
]->ce_mode
))
3235 die(_("'%s' already exists in the index "
3236 "and is not a submodule"), path
);
3242 clear_pathspec(&ps
);
3245 static void die_on_repo_without_commits(const char *path
)
3247 struct strbuf sb
= STRBUF_INIT
;
3248 strbuf_addstr(&sb
, path
);
3249 if (is_nonbare_repository_dir(&sb
)) {
3250 struct object_id oid
;
3251 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3252 die(_("'%s' does not have a commit checked out"), path
);
3254 strbuf_release(&sb
);
3257 static int module_add(int argc
, const char **argv
, const char *prefix
)
3259 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3260 struct add_data add_data
= ADD_DATA_INIT
;
3261 char *to_free
= NULL
;
3262 struct option options
[] = {
3263 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3264 N_("branch of repository to add as submodule")),
3265 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3266 PARSE_OPT_NOCOMPLETE
),
3267 OPT__QUIET(&quiet
, N_("print only error messages")),
3268 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3269 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3270 N_("reference repository")),
3271 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3272 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3273 N_("sets the submodule's name to the given string "
3274 "instead of defaulting to its path")),
3275 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3278 const char *const usage
[] = {
3279 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3282 struct strbuf sb
= STRBUF_INIT
;
3285 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3287 if (!is_writing_gitmodules_ok())
3288 die(_("please make sure that the .gitmodules file is in the working tree"));
3290 if (prefix
&& *prefix
&&
3291 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3292 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3294 if (argc
== 0 || argc
> 2)
3295 usage_with_options(usage
, options
);
3297 add_data
.repo
= argv
[0];
3299 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3301 add_data
.sm_path
= xstrdup(argv
[1]);
3303 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3304 char *sm_path
= add_data
.sm_path
;
3306 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3310 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3311 starts_with_dot_slash(add_data
.repo
)) {
3313 die(_("Relative path can only be used from the toplevel "
3314 "of the working tree"));
3316 /* dereference source url relative to parent's url */
3317 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3318 add_data
.realrepo
= to_free
;
3319 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3320 add_data
.realrepo
= add_data
.repo
;
3322 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3328 * multiple //; leading ./; /./; /../;
3330 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3331 strip_dir_trailing_slashes(add_data
.sm_path
);
3333 die_on_index_match(add_data
.sm_path
, force
);
3334 die_on_repo_without_commits(add_data
.sm_path
);
3337 struct child_process cp
= CHILD_PROCESS_INIT
;
3341 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3342 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3343 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3344 strbuf_complete_line(&sb
);
3345 fputs(sb
.buf
, stderr
);
3350 if(!add_data
.sm_name
)
3351 add_data
.sm_name
= add_data
.sm_path
;
3353 if (check_submodule_name(add_data
.sm_name
))
3354 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3356 add_data
.prefix
= prefix
;
3357 add_data
.force
= !!force
;
3358 add_data
.quiet
= !!quiet
;
3359 add_data
.progress
= !!progress
;
3360 add_data
.dissociate
= !!dissociate
;
3362 if (add_submodule(&add_data
))
3364 configure_added_submodule(&add_data
);
3368 free(add_data
.sm_path
);
3370 strbuf_release(&sb
);
3375 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3377 parse_opt_subcommand_fn
*fn
= NULL
;
3378 const char *const usage
[] = {
3379 N_("git submodule--helper <command>"),
3382 struct option options
[] = {
3383 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3384 OPT_SUBCOMMAND("add", &fn
, module_add
),
3385 OPT_SUBCOMMAND("update", &fn
, module_update
),
3386 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3387 OPT_SUBCOMMAND("init", &fn
, module_init
),
3388 OPT_SUBCOMMAND("status", &fn
, module_status
),
3389 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3390 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3391 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3392 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3393 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3394 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3395 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3396 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3399 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3401 return fn(argc
, argv
, prefix
);