1 #define USE_THE_INDEX_VARIABLE
3 #include "repository.h"
6 #include "parse-options.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
21 #include "object-store.h"
24 #include "list-objects-filter-options.h"
26 #define OPT_QUIET (1 << 0)
27 #define OPT_CACHED (1 << 1)
28 #define OPT_RECURSIVE (1 << 2)
29 #define OPT_FORCE (1 << 3)
31 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
34 static int repo_get_default_remote(struct repository
*repo
, char **default_remote
)
37 struct strbuf sb
= STRBUF_INIT
;
38 struct ref_store
*store
= get_main_ref_store(repo
);
39 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
43 return die_message(_("No such ref: %s"), "HEAD");
46 if (!strcmp(refname
, "HEAD")) {
47 *default_remote
= xstrdup("origin");
51 if (!skip_prefix(refname
, "refs/heads/", &refname
))
52 return die_message(_("Expecting a full ref name, got %s"),
55 strbuf_addf(&sb
, "branch.%s.remote", refname
);
56 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
57 *default_remote
= xstrdup("origin");
59 *default_remote
= dest
;
65 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
67 struct repository subrepo
;
70 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
72 return die_message(_("could not get a repository handle for submodule '%s'"),
74 ret
= repo_get_default_remote(&subrepo
, default_remote
);
80 static char *get_default_remote(void)
83 int code
= repo_get_default_remote(the_repository
, &default_remote
);
88 return default_remote
;
91 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
93 char *remoteurl
, *resolved_url
;
94 char *remote
= get_default_remote();
95 struct strbuf remotesb
= STRBUF_INIT
;
97 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
98 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
100 warning(_("could not look up configuration '%s'. "
101 "Assuming this repository is its own "
102 "authoritative upstream."),
104 remoteurl
= xgetcwd();
106 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
110 strbuf_release(&remotesb
);
115 /* the result should be freed by the caller. */
116 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
118 const char *super_prefix
= get_super_prefix();
120 if (prefix
&& super_prefix
) {
121 BUG("cannot have prefix '%s' and superprefix '%s'",
122 prefix
, super_prefix
);
124 struct strbuf sb
= STRBUF_INIT
;
125 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
128 } else if (super_prefix
) {
129 return xstrfmt("%s%s", super_prefix
, path
);
131 return xstrdup(path
);
135 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
137 struct strbuf sb
= STRBUF_INIT
;
140 static const char *describe_bare
[] = { NULL
};
142 static const char *describe_tags
[] = { "--tags", NULL
};
144 static const char *describe_contains
[] = { "--contains", NULL
};
146 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
148 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
150 describe_all_always
, NULL
};
152 for (d
= describe_argv
; *d
; d
++) {
153 struct child_process cp
= CHILD_PROCESS_INIT
;
154 prepare_submodule_repo_env(&cp
.env
);
159 strvec_push(&cp
.args
, "describe");
160 strvec_pushv(&cp
.args
, *d
);
161 strvec_push(&cp
.args
, object_id
);
163 if (!capture_command(&cp
, &sb
, 0)) {
164 strbuf_strip_suffix(&sb
, "\n");
165 return strbuf_detach(&sb
, NULL
);
174 const struct cache_entry
**entries
;
177 #define MODULE_LIST_INIT { 0 }
179 static void module_list_release(struct module_list
*ml
)
184 static int module_list_compute(const char **argv
,
186 struct pathspec
*pathspec
,
187 struct module_list
*list
)
190 char *ps_matched
= NULL
;
192 parse_pathspec(pathspec
, 0,
193 PATHSPEC_PREFER_FULL
,
197 ps_matched
= xcalloc(pathspec
->nr
, 1);
199 if (repo_read_index(the_repository
) < 0)
200 die(_("index file corrupt"));
202 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
203 const struct cache_entry
*ce
= the_index
.cache
[i
];
205 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
207 !S_ISGITLINK(ce
->ce_mode
))
210 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
211 list
->entries
[list
->nr
++] = ce
;
212 while (i
+ 1 < the_index
.cache_nr
&&
213 !strcmp(ce
->name
, the_index
.cache
[i
+ 1]->name
))
215 * Skip entries with the same name in different stages
216 * to make sure an entry is returned only once.
221 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
229 static void module_list_active(struct module_list
*list
)
232 struct module_list active_modules
= MODULE_LIST_INIT
;
234 for (i
= 0; i
< list
->nr
; i
++) {
235 const struct cache_entry
*ce
= list
->entries
[i
];
237 if (!is_submodule_active(the_repository
, ce
->name
))
240 ALLOC_GROW(active_modules
.entries
,
241 active_modules
.nr
+ 1,
242 active_modules
.alloc
);
243 active_modules
.entries
[active_modules
.nr
++] = ce
;
246 module_list_release(list
);
247 *list
= active_modules
;
250 static char *get_up_path(const char *path
)
253 struct strbuf sb
= STRBUF_INIT
;
255 for (i
= count_slashes(path
); i
; i
--)
256 strbuf_addstr(&sb
, "../");
259 * Check if 'path' ends with slash or not
260 * for having the same output for dir/sub_dir
263 if (!is_dir_sep(path
[strlen(path
) - 1]))
264 strbuf_addstr(&sb
, "../");
266 return strbuf_detach(&sb
, NULL
);
269 static void for_each_listed_submodule(const struct module_list
*list
,
270 each_submodule_fn fn
, void *cb_data
)
274 for (i
= 0; i
< list
->nr
; i
++)
275 fn(list
->entries
[i
], cb_data
);
285 #define FOREACH_CB_INIT { 0 }
287 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
290 struct foreach_cb
*info
= cb_data
;
291 const char *path
= list_item
->name
;
292 const struct object_id
*ce_oid
= &list_item
->oid
;
293 const struct submodule
*sub
;
294 struct child_process cp
= CHILD_PROCESS_INIT
;
297 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
299 sub
= submodule_from_path(the_repository
, null_oid(), path
);
302 die(_("No url found for submodule path '%s' in .gitmodules"),
305 if (!is_submodule_populated_gently(path
, NULL
))
308 prepare_submodule_repo_env(&cp
.env
);
311 * For the purpose of executing <command> in the submodule,
312 * separate shell is used for the purpose of running the
319 * NEEDSWORK: the command currently has access to the variables $name,
320 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
321 * contains a single argument. This is done for maintaining a faithful
322 * translation from shell script.
324 if (info
->argc
== 1) {
325 char *toplevel
= xgetcwd();
326 struct strbuf sb
= STRBUF_INIT
;
328 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
329 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
330 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
331 strvec_pushf(&cp
.env
, "sha1=%s",
333 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
336 * Since the path variable was accessible from the script
337 * before porting, it is also made available after porting.
338 * The environment variable "PATH" has a very special purpose
339 * on windows. And since environment variables are
340 * case-insensitive in windows, it interferes with the
341 * existing PATH variable. Hence, to avoid that, we expose
342 * path via the args strvec and not via env.
344 sq_quote_buf(&sb
, path
);
345 strvec_pushf(&cp
.args
, "path=%s; %s",
346 sb
.buf
, info
->argv
[0]);
350 strvec_pushv(&cp
.args
, info
->argv
);
354 printf(_("Entering '%s'\n"), displaypath
);
356 if (info
->argv
[0] && run_command(&cp
))
357 die(_("run_command returned non-zero status for %s\n."),
360 if (info
->recursive
) {
361 struct child_process cpr
= CHILD_PROCESS_INIT
;
365 prepare_submodule_repo_env(&cpr
.env
);
367 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
368 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
369 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
373 strvec_push(&cpr
.args
, "--quiet");
375 strvec_push(&cpr
.args
, "--");
376 strvec_pushv(&cpr
.args
, info
->argv
);
378 if (run_command(&cpr
))
379 die(_("run_command returned non-zero status while "
380 "recursing in the nested submodules of %s\n."),
388 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
390 struct foreach_cb info
= FOREACH_CB_INIT
;
391 struct pathspec pathspec
= { 0 };
392 struct module_list list
= MODULE_LIST_INIT
;
393 struct option module_foreach_options
[] = {
394 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
395 OPT_BOOL(0, "recursive", &info
.recursive
,
396 N_("recurse into nested submodules")),
399 const char *const git_submodule_helper_usage
[] = {
400 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
405 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
406 git_submodule_helper_usage
, 0);
408 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
413 info
.prefix
= prefix
;
415 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
419 module_list_release(&list
);
420 clear_pathspec(&pathspec
);
424 static int starts_with_dot_slash(const char *const path
)
426 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
427 PATH_MATCH_XPLATFORM
);
430 static int starts_with_dot_dot_slash(const char *const path
)
432 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
433 PATH_MATCH_XPLATFORM
);
440 #define INIT_CB_INIT { 0 }
442 static void init_submodule(const char *path
, const char *prefix
,
445 const struct submodule
*sub
;
446 struct strbuf sb
= STRBUF_INIT
;
448 char *url
= NULL
, *displaypath
;
450 displaypath
= get_submodule_displaypath(path
, prefix
);
452 sub
= submodule_from_path(the_repository
, null_oid(), path
);
455 die(_("No url found for submodule path '%s' in .gitmodules"),
459 * NEEDSWORK: In a multi-working-tree world, this needs to be
460 * set in the per-worktree config.
462 * Set active flag for the submodule being initialized
464 if (!is_submodule_active(the_repository
, path
)) {
465 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
466 git_config_set_gently(sb
.buf
, "true");
471 * Copy url setting when it is not set yet.
472 * To look up the url in .git/config, we must not fall back to
473 * .gitmodules, so look it up directly.
475 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
476 if (git_config_get_string(sb
.buf
, &url
)) {
478 die(_("No url found for submodule path '%s' in .gitmodules"),
481 url
= xstrdup(sub
->url
);
483 /* Possibly a url relative to parent */
484 if (starts_with_dot_dot_slash(url
) ||
485 starts_with_dot_slash(url
)) {
488 url
= resolve_relative_url(oldurl
, NULL
, 0);
492 if (git_config_set_gently(sb
.buf
, url
))
493 die(_("Failed to register url for submodule path '%s'"),
495 if (!(flags
& OPT_QUIET
))
497 _("Submodule '%s' (%s) registered for path '%s'\n"),
498 sub
->name
, url
, displaypath
);
502 /* Copy "update" setting when it is not set yet */
503 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
504 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
505 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
506 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
507 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
511 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
514 if (git_config_set_gently(sb
.buf
, upd
))
515 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
522 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
524 struct init_cb
*info
= cb_data
;
526 init_submodule(list_item
->name
, info
->prefix
, info
->flags
);
529 static int module_init(int argc
, const char **argv
, const char *prefix
)
531 struct init_cb info
= INIT_CB_INIT
;
532 struct pathspec pathspec
= { 0 };
533 struct module_list list
= MODULE_LIST_INIT
;
535 struct option module_init_options
[] = {
536 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
539 const char *const git_submodule_helper_usage
[] = {
540 N_("git submodule init [<options>] [<path>]"),
545 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
546 git_submodule_helper_usage
, 0);
548 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
552 * If there are no path args and submodule.active is set then,
553 * by default, only initialize 'active' modules.
555 if (!argc
&& git_config_get_value_multi("submodule.active"))
556 module_list_active(&list
);
558 info
.prefix
= prefix
;
560 info
.flags
|= OPT_QUIET
;
562 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
566 module_list_release(&list
);
567 clear_pathspec(&pathspec
);
575 #define STATUS_CB_INIT { 0 }
577 static void print_status(unsigned int flags
, char state
, const char *path
,
578 const struct object_id
*oid
, const char *displaypath
)
580 if (flags
& OPT_QUIET
)
583 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
585 if (state
== ' ' || state
== '+') {
586 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
589 printf(" (%s)", name
);
596 static int handle_submodule_head_ref(const char *refname UNUSED
,
597 const struct object_id
*oid
,
601 struct object_id
*output
= cb_data
;
609 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
610 unsigned int ce_flags
, const char *prefix
,
614 struct strvec diff_files_args
= STRVEC_INIT
;
615 struct rev_info rev
= REV_INFO_INIT
;
616 int diff_files_result
;
617 struct strbuf buf
= STRBUF_INIT
;
619 struct setup_revision_opt opt
= {
620 .free_removed_argv_elements
= 1,
623 if (!submodule_from_path(the_repository
, null_oid(), path
))
624 die(_("no submodule mapping found in .gitmodules for path '%s'"),
627 displaypath
= get_submodule_displaypath(path
, prefix
);
629 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
630 print_status(flags
, 'U', path
, null_oid(), displaypath
);
634 strbuf_addf(&buf
, "%s/.git", path
);
635 git_dir
= read_gitfile(buf
.buf
);
639 if (!is_submodule_active(the_repository
, path
) ||
640 !is_git_directory(git_dir
)) {
641 print_status(flags
, '-', path
, ce_oid
, displaypath
);
642 strbuf_release(&buf
);
645 strbuf_release(&buf
);
647 strvec_pushl(&diff_files_args
, "diff-files",
648 "--ignore-submodules=dirty", "--quiet", "--",
651 git_config(git_diff_basic_config
, NULL
);
653 repo_init_revisions(the_repository
, &rev
, NULL
);
655 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
656 diff_files_result
= run_diff_files(&rev
, 0);
658 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
659 print_status(flags
, ' ', path
, ce_oid
,
661 } else if (!(flags
& OPT_CACHED
)) {
662 struct object_id oid
;
663 struct ref_store
*refs
= get_submodule_ref_store(path
);
666 print_status(flags
, '-', path
, ce_oid
, displaypath
);
669 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
670 die(_("could not resolve HEAD ref inside the "
671 "submodule '%s'"), path
);
673 print_status(flags
, '+', path
, &oid
, displaypath
);
675 print_status(flags
, '+', path
, ce_oid
, displaypath
);
678 if (flags
& OPT_RECURSIVE
) {
679 struct child_process cpr
= CHILD_PROCESS_INIT
;
683 prepare_submodule_repo_env(&cpr
.env
);
685 strvec_push(&cpr
.args
, "--super-prefix");
686 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
687 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
688 "--recursive", NULL
);
690 if (flags
& OPT_CACHED
)
691 strvec_push(&cpr
.args
, "--cached");
693 if (flags
& OPT_QUIET
)
694 strvec_push(&cpr
.args
, "--quiet");
696 if (run_command(&cpr
))
697 die(_("failed to recurse into submodule '%s'"), path
);
701 strvec_clear(&diff_files_args
);
703 release_revisions(&rev
);
706 static void status_submodule_cb(const struct cache_entry
*list_item
,
709 struct status_cb
*info
= cb_data
;
711 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
712 info
->prefix
, info
->flags
);
715 static int module_status(int argc
, const char **argv
, const char *prefix
)
717 struct status_cb info
= STATUS_CB_INIT
;
718 struct pathspec pathspec
= { 0 };
719 struct module_list list
= MODULE_LIST_INIT
;
721 struct option module_status_options
[] = {
722 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
723 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
724 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
727 const char *const git_submodule_helper_usage
[] = {
728 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
733 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
734 git_submodule_helper_usage
, 0);
736 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
739 info
.prefix
= prefix
;
741 info
.flags
|= OPT_QUIET
;
743 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
747 module_list_release(&list
);
748 clear_pathspec(&pathspec
);
753 unsigned int mod_src
;
754 unsigned int mod_dst
;
755 struct object_id oid_src
;
756 struct object_id oid_dst
;
760 #define MODULE_CB_INIT { 0 }
762 static void module_cb_release(struct module_cb
*mcb
)
767 struct module_cb_list
{
768 struct module_cb
**entries
;
771 #define MODULE_CB_LIST_INIT { 0 }
773 static void module_cb_list_release(struct module_cb_list
*mcbl
)
777 for (i
= 0; i
< mcbl
->nr
; i
++) {
778 struct module_cb
*mcb
= mcbl
->entries
[i
];
780 module_cb_release(mcb
);
790 unsigned int cached
: 1;
791 unsigned int for_status
: 1;
792 unsigned int files
: 1;
795 #define SUMMARY_CB_INIT { 0 }
802 static char *verify_submodule_committish(const char *sm_path
,
803 const char *committish
)
805 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
806 struct strbuf result
= STRBUF_INIT
;
808 cp_rev_parse
.git_cmd
= 1;
809 cp_rev_parse
.dir
= sm_path
;
810 prepare_submodule_repo_env(&cp_rev_parse
.env
);
811 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
812 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
813 strvec_push(&cp_rev_parse
.args
, "--");
815 if (capture_command(&cp_rev_parse
, &result
, 0))
818 strbuf_trim_trailing_newline(&result
);
819 return strbuf_detach(&result
, NULL
);
822 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
823 int total_commits
, const char *displaypath
,
824 const char *src_abbrev
, const char *dst_abbrev
,
827 if (p
->status
== 'T') {
828 if (S_ISGITLINK(p
->mod_dst
))
829 printf(_("* %s %s(blob)->%s(submodule)"),
830 displaypath
, src_abbrev
, dst_abbrev
);
832 printf(_("* %s %s(submodule)->%s(blob)"),
833 displaypath
, src_abbrev
, dst_abbrev
);
835 printf("* %s %s...%s",
836 displaypath
, src_abbrev
, dst_abbrev
);
839 if (total_commits
< 0)
842 printf(" (%d):\n", total_commits
);
845 printf(_("%s"), errmsg
);
846 } else if (total_commits
> 0) {
847 struct child_process cp_log
= CHILD_PROCESS_INIT
;
850 cp_log
.dir
= p
->sm_path
;
851 prepare_submodule_repo_env(&cp_log
.env
);
852 strvec_pushl(&cp_log
.args
, "log", NULL
);
854 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
855 if (info
->summary_limit
> 0)
856 strvec_pushf(&cp_log
.args
, "-%d",
857 info
->summary_limit
);
859 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
860 "--first-parent", NULL
);
861 strvec_pushf(&cp_log
.args
, "%s...%s",
862 src_abbrev
, dst_abbrev
);
863 } else if (S_ISGITLINK(p
->mod_dst
)) {
864 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
865 "-1", dst_abbrev
, NULL
);
867 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
868 "-1", src_abbrev
, NULL
);
870 run_command(&cp_log
);
875 static void generate_submodule_summary(struct summary_cb
*info
,
878 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
879 int missing_src
= 0, missing_dst
= 0;
880 struct strbuf errmsg
= STRBUF_INIT
;
881 int total_commits
= -1;
883 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
884 if (S_ISGITLINK(p
->mod_dst
)) {
885 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
888 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
889 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
891 int fd
= open(p
->sm_path
, O_RDONLY
);
893 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
894 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
896 error(_("couldn't hash object from '%s'"), p
->sm_path
);
898 /* for a submodule removal (mode:0000000), don't warn */
900 warning(_("unexpected mode %o\n"), p
->mod_dst
);
904 if (S_ISGITLINK(p
->mod_src
)) {
905 if (p
->status
!= 'D')
906 src_abbrev
= verify_submodule_committish(p
->sm_path
,
907 oid_to_hex(&p
->oid_src
));
911 * As `rev-parse` failed, we fallback to getting
912 * the abbreviated hash using oid_src. We do
913 * this as we might still need the abbreviated
914 * hash in cases like a submodule type change, etc.
916 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
920 * The source does not point to a submodule.
921 * So, we fallback to getting the abbreviation using
922 * oid_src as we might still need the abbreviated
923 * hash in cases like submodule add, etc.
925 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
928 if (S_ISGITLINK(p
->mod_dst
)) {
929 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
930 oid_to_hex(&p
->oid_dst
));
934 * As `rev-parse` failed, we fallback to getting
935 * the abbreviated hash using oid_dst. We do
936 * this as we might still need the abbreviated
937 * hash in cases like a submodule type change, etc.
939 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
943 * The destination does not point to a submodule.
944 * So, we fallback to getting the abbreviation using
945 * oid_dst as we might still need the abbreviated
946 * hash in cases like a submodule removal, etc.
948 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
951 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
);
953 if (!missing_src
&& !missing_dst
) {
954 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
955 struct strbuf sb_rev_list
= STRBUF_INIT
;
957 strvec_pushl(&cp_rev_list
.args
, "rev-list",
958 "--first-parent", "--count", NULL
);
959 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
960 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
961 src_abbrev
, dst_abbrev
);
963 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
964 src_abbrev
: dst_abbrev
);
965 strvec_push(&cp_rev_list
.args
, "--");
967 cp_rev_list
.git_cmd
= 1;
968 cp_rev_list
.dir
= p
->sm_path
;
969 prepare_submodule_repo_env(&cp_rev_list
.env
);
971 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
972 total_commits
= atoi(sb_rev_list
.buf
);
974 strbuf_release(&sb_rev_list
);
977 * Don't give error msg for modification whose dst is not
978 * submodule, i.e., deleted or changed to blob
980 if (S_ISGITLINK(p
->mod_dst
)) {
981 if (missing_src
&& missing_dst
) {
982 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
983 displaypath
, oid_to_hex(&p
->oid_src
),
984 oid_to_hex(&p
->oid_dst
));
986 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
987 displaypath
, missing_src
?
988 oid_to_hex(&p
->oid_src
) :
989 oid_to_hex(&p
->oid_dst
));
994 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
995 total_commits
, displaypath
, src_abbrev
,
1001 strbuf_release(&errmsg
);
1004 static void prepare_submodule_summary(struct summary_cb
*info
,
1005 struct module_cb_list
*list
)
1008 for (i
= 0; i
< list
->nr
; i
++) {
1009 const struct submodule
*sub
;
1010 struct module_cb
*p
= list
->entries
[i
];
1011 struct strbuf sm_gitdir
= STRBUF_INIT
;
1013 if (p
->status
== 'D' || p
->status
== 'T') {
1014 generate_submodule_summary(info
, p
);
1018 if (info
->for_status
&& p
->status
!= 'A' &&
1019 (sub
= submodule_from_path(the_repository
,
1020 null_oid(), p
->sm_path
))) {
1021 char *config_key
= NULL
;
1025 config_key
= xstrfmt("submodule.%s.ignore",
1027 if (!git_config_get_string_tmp(config_key
, &value
))
1028 ignore_all
= !strcmp(value
, "all");
1029 else if (sub
->ignore
)
1030 ignore_all
= !strcmp(sub
->ignore
, "all");
1037 /* Also show added or modified modules which are checked out */
1038 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1039 if (is_nonbare_repository_dir(&sm_gitdir
))
1040 generate_submodule_summary(info
, p
);
1041 strbuf_release(&sm_gitdir
);
1045 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1046 struct diff_options
*options
,
1050 struct module_cb_list
*list
= data
;
1051 for (i
= 0; i
< q
->nr
; i
++) {
1052 struct diff_filepair
*p
= q
->queue
[i
];
1053 struct module_cb
*temp
;
1055 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1057 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1058 temp
->mod_src
= p
->one
->mode
;
1059 temp
->mod_dst
= p
->two
->mode
;
1060 temp
->oid_src
= p
->one
->oid
;
1061 temp
->oid_dst
= p
->two
->oid
;
1062 temp
->status
= p
->status
;
1063 temp
->sm_path
= xstrdup(p
->one
->path
);
1065 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1066 list
->entries
[list
->nr
++] = temp
;
1070 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1073 case DIFF_INDEX
: return "diff-index";
1074 case DIFF_FILES
: return "diff-files";
1075 default: BUG("bad diff_cmd value %d", diff_cmd
);
1079 static int compute_summary_module_list(struct object_id
*head_oid
,
1080 struct summary_cb
*info
,
1081 enum diff_cmd diff_cmd
)
1083 struct strvec diff_args
= STRVEC_INIT
;
1084 struct rev_info rev
;
1085 struct setup_revision_opt opt
= {
1086 .free_removed_argv_elements
= 1,
1088 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1091 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1093 strvec_push(&diff_args
, "--cached");
1094 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1096 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1097 strvec_push(&diff_args
, "--");
1099 strvec_pushv(&diff_args
, info
->argv
);
1101 git_config(git_diff_basic_config
, NULL
);
1102 init_revisions(&rev
, info
->prefix
);
1104 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1105 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1106 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1107 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1108 rev
.diffopt
.format_callback_data
= &list
;
1110 if (!info
->cached
) {
1111 if (diff_cmd
== DIFF_INDEX
)
1113 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1114 perror("repo_read_index_preload");
1118 } else if (repo_read_index(the_repository
) < 0) {
1119 perror("repo_read_cache");
1124 if (diff_cmd
== DIFF_INDEX
)
1125 run_diff_index(&rev
, info
->cached
);
1127 run_diff_files(&rev
, 0);
1128 prepare_submodule_summary(info
, &list
);
1130 strvec_clear(&diff_args
);
1131 release_revisions(&rev
);
1132 module_cb_list_release(&list
);
1136 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1138 struct summary_cb info
= SUMMARY_CB_INIT
;
1142 int summary_limit
= -1;
1143 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1144 struct object_id head_oid
;
1146 struct option module_summary_options
[] = {
1147 OPT_BOOL(0, "cached", &cached
,
1148 N_("use the commit stored in the index instead of the submodule HEAD")),
1149 OPT_BOOL(0, "files", &files
,
1150 N_("compare the commit in the index with that in the submodule HEAD")),
1151 OPT_BOOL(0, "for-status", &for_status
,
1152 N_("skip submodules with 'ignore_config' value set to 'all'")),
1153 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1154 N_("limit the summary size")),
1157 const char *const git_submodule_helper_usage
[] = {
1158 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1162 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1163 git_submodule_helper_usage
, 0);
1168 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1173 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1174 /* before the first commit: compare with an empty tree */
1175 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1181 if (get_oid("HEAD", &head_oid
))
1182 die(_("could not fetch a revision for HEAD"));
1187 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1188 diff_cmd
= DIFF_FILES
;
1193 info
.prefix
= prefix
;
1194 info
.cached
= !!cached
;
1195 info
.files
= !!files
;
1196 info
.for_status
= !!for_status
;
1197 info
.summary_limit
= summary_limit
;
1199 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1208 #define SYNC_CB_INIT { 0 }
1210 static void sync_submodule(const char *path
, const char *prefix
,
1213 const struct submodule
*sub
;
1214 char *remote_key
= NULL
;
1215 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1216 struct strbuf sb
= STRBUF_INIT
;
1217 char *sub_config_path
= NULL
;
1220 if (!is_submodule_active(the_repository
, path
))
1223 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1225 if (sub
&& sub
->url
) {
1226 if (starts_with_dot_dot_slash(sub
->url
) ||
1227 starts_with_dot_slash(sub
->url
)) {
1228 char *up_path
= get_up_path(path
);
1230 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1231 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1234 sub_origin_url
= xstrdup(sub
->url
);
1235 super_config_url
= xstrdup(sub
->url
);
1238 sub_origin_url
= xstrdup("");
1239 super_config_url
= xstrdup("");
1242 displaypath
= get_submodule_displaypath(path
, prefix
);
1244 if (!(flags
& OPT_QUIET
))
1245 printf(_("Synchronizing submodule url for '%s'\n"),
1249 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1250 if (git_config_set_gently(sb
.buf
, super_config_url
))
1251 die(_("failed to register url for submodule path '%s'"),
1254 if (!is_submodule_populated_gently(path
, NULL
))
1258 code
= get_default_remote_submodule(path
, &default_remote
);
1262 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1263 free(default_remote
);
1265 submodule_to_gitdir(&sb
, path
);
1266 strbuf_addstr(&sb
, "/config");
1268 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1269 die(_("failed to update remote for submodule '%s'"),
1272 if (flags
& OPT_RECURSIVE
) {
1273 struct child_process cpr
= CHILD_PROCESS_INIT
;
1277 prepare_submodule_repo_env(&cpr
.env
);
1279 strvec_push(&cpr
.args
, "--super-prefix");
1280 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1281 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1282 "--recursive", NULL
);
1284 if (flags
& OPT_QUIET
)
1285 strvec_push(&cpr
.args
, "--quiet");
1287 if (run_command(&cpr
))
1288 die(_("failed to recurse into submodule '%s'"),
1293 free(super_config_url
);
1294 free(sub_origin_url
);
1295 strbuf_release(&sb
);
1298 free(sub_config_path
);
1301 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1303 struct sync_cb
*info
= cb_data
;
1305 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1308 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1310 struct sync_cb info
= SYNC_CB_INIT
;
1311 struct pathspec pathspec
= { 0 };
1312 struct module_list list
= MODULE_LIST_INIT
;
1315 struct option module_sync_options
[] = {
1316 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1317 OPT_BOOL(0, "recursive", &recursive
,
1318 N_("recurse into nested submodules")),
1321 const char *const git_submodule_helper_usage
[] = {
1322 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1327 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1328 git_submodule_helper_usage
, 0);
1330 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1333 info
.prefix
= prefix
;
1335 info
.flags
|= OPT_QUIET
;
1337 info
.flags
|= OPT_RECURSIVE
;
1339 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1343 module_list_release(&list
);
1344 clear_pathspec(&pathspec
);
1352 #define DEINIT_CB_INIT { 0 }
1354 static void deinit_submodule(const char *path
, const char *prefix
,
1357 const struct submodule
*sub
;
1358 char *displaypath
= NULL
;
1359 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1360 struct strbuf sb_config
= STRBUF_INIT
;
1361 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1363 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1365 if (!sub
|| !sub
->name
)
1368 displaypath
= get_submodule_displaypath(path
, prefix
);
1370 /* remove the submodule work tree (unless the user already did it) */
1371 if (is_directory(path
)) {
1372 struct strbuf sb_rm
= STRBUF_INIT
;
1375 if (is_directory(sub_git_dir
)) {
1376 if (!(flags
& OPT_QUIET
))
1377 warning(_("Submodule work tree '%s' contains a .git "
1378 "directory. This will be replaced with a "
1379 ".git file by using absorbgitdirs."),
1382 absorb_git_dir_into_superproject(path
);
1386 if (!(flags
& OPT_FORCE
)) {
1387 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1390 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1393 if (run_command(&cp_rm
))
1394 die(_("Submodule work tree '%s' contains local "
1395 "modifications; use '-f' to discard them"),
1399 strbuf_addstr(&sb_rm
, path
);
1401 if (!remove_dir_recursively(&sb_rm
, 0))
1402 format
= _("Cleared directory '%s'\n");
1404 format
= _("Could not remove submodule work tree '%s'\n");
1406 if (!(flags
& OPT_QUIET
))
1407 printf(format
, displaypath
);
1409 submodule_unset_core_worktree(sub
);
1411 strbuf_release(&sb_rm
);
1414 if (mkdir(path
, 0777))
1415 printf(_("could not create empty submodule directory %s"),
1418 cp_config
.git_cmd
= 1;
1419 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1420 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1422 /* remove the .git/config entries (unless the user already did it) */
1423 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1424 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1427 * remove the whole section so we have a clean state when
1428 * the user later decides to init this submodule again
1430 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1431 if (!(flags
& OPT_QUIET
))
1432 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1433 sub
->name
, sub
->url
, displaypath
);
1440 strbuf_release(&sb_config
);
1443 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1446 struct deinit_cb
*info
= cb_data
;
1447 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1450 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1452 struct deinit_cb info
= DEINIT_CB_INIT
;
1453 struct pathspec pathspec
= { 0 };
1454 struct module_list list
= MODULE_LIST_INIT
;
1458 struct option module_deinit_options
[] = {
1459 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1460 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1461 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1464 const char *const git_submodule_helper_usage
[] = {
1465 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1470 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1471 git_submodule_helper_usage
, 0);
1474 error("pathspec and --all are incompatible");
1475 usage_with_options(git_submodule_helper_usage
,
1476 module_deinit_options
);
1480 die(_("Use '--all' if you really want to deinitialize all submodules"));
1482 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1485 info
.prefix
= prefix
;
1487 info
.flags
|= OPT_QUIET
;
1489 info
.flags
|= OPT_FORCE
;
1491 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1495 module_list_release(&list
);
1496 clear_pathspec(&pathspec
);
1500 struct module_clone_data
{
1506 struct list_objects_filter_options
*filter_options
;
1507 unsigned int quiet
: 1;
1508 unsigned int progress
: 1;
1509 unsigned int dissociate
: 1;
1510 unsigned int require_init
: 1;
1513 #define MODULE_CLONE_DATA_INIT { \
1514 .single_branch = -1, \
1517 struct submodule_alternate_setup
{
1518 const char *submodule_name
;
1519 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1520 SUBMODULE_ALTERNATE_ERROR_DIE
,
1521 SUBMODULE_ALTERNATE_ERROR_INFO
,
1522 SUBMODULE_ALTERNATE_ERROR_IGNORE
1524 struct string_list
*reference
;
1526 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1527 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1530 static const char alternate_error_advice
[] = N_(
1531 "An alternate computed from a superproject's alternate is invalid.\n"
1532 "To allow Git to clone without an alternate in such a case, set\n"
1533 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1534 "'--reference-if-able' instead of '--reference'."
1537 static int add_possible_reference_from_superproject(
1538 struct object_directory
*odb
, void *sas_cb
)
1540 struct submodule_alternate_setup
*sas
= sas_cb
;
1544 * If the alternate object store is another repository, try the
1545 * standard layout with .git/(modules/<name>)+/objects
1547 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1548 struct repository alternate
;
1550 struct strbuf sb
= STRBUF_INIT
;
1551 struct strbuf err
= STRBUF_INIT
;
1552 strbuf_add(&sb
, odb
->path
, len
);
1554 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1555 die(_("could not get a repository handle for gitdir '%s'"),
1559 * We need to end the new path with '/' to mark it as a dir,
1560 * otherwise a submodule name containing '/' will be broken
1561 * as the last part of a missing submodule reference would
1562 * be taken as a file name.
1565 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1566 strbuf_addch(&sb
, '/');
1567 repo_clear(&alternate
);
1569 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1571 char *p
= strbuf_detach(&sb
, NULL
);
1573 string_list_append(sas
->reference
, p
)->util
= p
;
1576 switch (sas
->error_mode
) {
1577 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1578 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1579 advise(_(alternate_error_advice
));
1580 die(_("submodule '%s' cannot add alternate: %s"),
1581 sas
->submodule_name
, err
.buf
);
1582 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1583 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1584 sas
->submodule_name
, err
.buf
);
1585 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1589 strbuf_release(&sb
);
1595 static void prepare_possible_alternates(const char *sm_name
,
1596 struct string_list
*reference
)
1598 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1599 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1601 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1605 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1607 if (!error_strategy
)
1608 error_strategy
= xstrdup("die");
1610 sas
.submodule_name
= sm_name
;
1611 sas
.reference
= reference
;
1612 if (!strcmp(error_strategy
, "die"))
1613 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1614 else if (!strcmp(error_strategy
, "info"))
1615 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1616 else if (!strcmp(error_strategy
, "ignore"))
1617 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1619 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1621 if (!strcmp(sm_alternate
, "superproject"))
1622 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1623 else if (!strcmp(sm_alternate
, "no"))
1626 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1629 free(error_strategy
);
1632 static char *clone_submodule_sm_gitdir(const char *name
)
1634 struct strbuf sb
= STRBUF_INIT
;
1637 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1638 sm_gitdir
= absolute_pathdup(sb
.buf
);
1639 strbuf_release(&sb
);
1644 static int clone_submodule(const struct module_clone_data
*clone_data
,
1645 struct string_list
*reference
)
1648 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1649 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1650 struct child_process cp
= CHILD_PROCESS_INIT
;
1651 const char *clone_data_path
= clone_data
->path
;
1652 char *to_free
= NULL
;
1654 if (!is_absolute_path(clone_data
->path
))
1655 clone_data_path
= to_free
= xstrfmt("%s/%s", get_git_work_tree(),
1658 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1659 die(_("refusing to create/use '%s' in another submodule's "
1660 "git dir"), sm_gitdir
);
1662 if (!file_exists(sm_gitdir
)) {
1663 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1664 die(_("could not create directory '%s'"), sm_gitdir
);
1666 prepare_possible_alternates(clone_data
->name
, reference
);
1668 strvec_push(&cp
.args
, "clone");
1669 strvec_push(&cp
.args
, "--no-checkout");
1670 if (clone_data
->quiet
)
1671 strvec_push(&cp
.args
, "--quiet");
1672 if (clone_data
->progress
)
1673 strvec_push(&cp
.args
, "--progress");
1674 if (clone_data
->depth
&& *(clone_data
->depth
))
1675 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1676 if (reference
->nr
) {
1677 struct string_list_item
*item
;
1679 for_each_string_list_item(item
, reference
)
1680 strvec_pushl(&cp
.args
, "--reference",
1681 item
->string
, NULL
);
1683 if (clone_data
->dissociate
)
1684 strvec_push(&cp
.args
, "--dissociate");
1685 if (sm_gitdir
&& *sm_gitdir
)
1686 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1687 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1688 strvec_pushf(&cp
.args
, "--filter=%s",
1689 expand_list_objects_filter_spec(
1690 clone_data
->filter_options
));
1691 if (clone_data
->single_branch
>= 0)
1692 strvec_push(&cp
.args
, clone_data
->single_branch
?
1694 "--no-single-branch");
1696 strvec_push(&cp
.args
, "--");
1697 strvec_push(&cp
.args
, clone_data
->url
);
1698 strvec_push(&cp
.args
, clone_data_path
);
1701 prepare_submodule_repo_env(&cp
.env
);
1704 if(run_command(&cp
))
1705 die(_("clone of '%s' into submodule path '%s' failed"),
1706 clone_data
->url
, clone_data_path
);
1710 if (clone_data
->require_init
&& !access(clone_data_path
, X_OK
) &&
1711 !is_empty_dir(clone_data_path
))
1712 die(_("directory not empty: '%s'"), clone_data_path
);
1713 if (safe_create_leading_directories_const(clone_data_path
) < 0)
1714 die(_("could not create directory '%s'"), clone_data_path
);
1715 path
= xstrfmt("%s/index", sm_gitdir
);
1716 unlink_or_warn(path
);
1720 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1722 p
= git_pathdup_submodule(clone_data_path
, "config");
1724 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1726 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1727 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1729 git_config_set_in_file(p
, "submodule.alternateLocation",
1731 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1733 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1737 free(error_strategy
);
1745 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1747 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1748 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1749 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1750 struct list_objects_filter_options filter_options
=
1751 LIST_OBJECTS_FILTER_INIT
;
1753 struct option module_clone_options
[] = {
1754 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1756 N_("alternative anchor for relative paths")),
1757 OPT_STRING(0, "path", &clone_data
.path
,
1759 N_("where the new submodule will be cloned to")),
1760 OPT_STRING(0, "name", &clone_data
.name
,
1762 N_("name of the new submodule")),
1763 OPT_STRING(0, "url", &clone_data
.url
,
1765 N_("url where to clone the submodule from")),
1766 OPT_STRING_LIST(0, "reference", &reference
,
1768 N_("reference repository")),
1769 OPT_BOOL(0, "dissociate", &dissociate
,
1770 N_("use --reference only while cloning")),
1771 OPT_STRING(0, "depth", &clone_data
.depth
,
1773 N_("depth for shallow clones")),
1774 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1775 OPT_BOOL(0, "progress", &progress
,
1776 N_("force cloning progress")),
1777 OPT_BOOL(0, "require-init", &require_init
,
1778 N_("disallow cloning into non-empty directory")),
1779 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1780 N_("clone only one branch, HEAD or --branch")),
1781 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1784 const char *const git_submodule_helper_usage
[] = {
1785 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1786 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1787 "[--single-branch] [--filter <filter-spec>] "
1788 "--url <url> --path <path>"),
1792 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1793 git_submodule_helper_usage
, 0);
1795 clone_data
.dissociate
= !!dissociate
;
1796 clone_data
.quiet
= !!quiet
;
1797 clone_data
.progress
= !!progress
;
1798 clone_data
.require_init
= !!require_init
;
1799 clone_data
.filter_options
= &filter_options
;
1801 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1802 usage_with_options(git_submodule_helper_usage
,
1803 module_clone_options
);
1805 clone_submodule(&clone_data
, &reference
);
1806 list_objects_filter_release(&filter_options
);
1807 string_list_clear(&reference
, 1);
1811 static int determine_submodule_update_strategy(struct repository
*r
,
1814 enum submodule_update_type update
,
1815 struct submodule_update_strategy
*out
)
1817 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1822 key
= xstrfmt("submodule.%s.update", sub
->name
);
1826 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1827 if (parse_submodule_update_strategy(val
, out
) < 0) {
1828 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1832 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1833 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1834 BUG("how did we read update = !command from .gitmodules?");
1835 out
->type
= sub
->update_strategy
.type
;
1836 out
->command
= sub
->update_strategy
.command
;
1838 out
->type
= SM_UPDATE_CHECKOUT
;
1841 (out
->type
== SM_UPDATE_MERGE
||
1842 out
->type
== SM_UPDATE_REBASE
||
1843 out
->type
== SM_UPDATE_NONE
))
1844 out
->type
= SM_UPDATE_CHECKOUT
;
1852 struct update_clone_data
{
1853 const struct submodule
*sub
;
1854 struct object_id oid
;
1855 unsigned just_cloned
;
1858 struct submodule_update_clone
{
1859 /* index into 'update_data.list', the list of submodules to look into for cloning */
1862 /* configuration parameters which are passed on to the children */
1863 const struct update_data
*update_data
;
1865 /* to be consumed by update_submodule() */
1866 struct update_clone_data
*update_clone
;
1867 int update_clone_nr
; int update_clone_alloc
;
1869 /* If we want to stop as fast as possible and return an error */
1870 unsigned quickstop
: 1;
1872 /* failed clones to be retried again */
1873 const struct cache_entry
**failed_clones
;
1874 int failed_clones_nr
, failed_clones_alloc
;
1876 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1878 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1880 free(suc
->update_clone
);
1881 free(suc
->failed_clones
);
1884 struct update_data
{
1887 enum submodule_update_type update_default
;
1888 struct object_id suboid
;
1889 struct string_list references
;
1890 struct submodule_update_strategy update_strategy
;
1891 struct list_objects_filter_options
*filter_options
;
1892 struct module_list list
;
1896 int recommend_shallow
;
1897 unsigned int require_init
;
1900 unsigned int nofetch
;
1901 unsigned int remote
;
1902 unsigned int progress
;
1903 unsigned int dissociate
;
1905 unsigned int warn_if_uninitialized
;
1906 unsigned int recursive
;
1908 /* copied over from update_clone_data */
1909 struct object_id oid
;
1910 unsigned int just_cloned
;
1911 const char *sm_path
;
1913 #define UPDATE_DATA_INIT { \
1914 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1915 .list = MODULE_LIST_INIT, \
1916 .recommend_shallow = -1, \
1917 .references = STRING_LIST_INIT_DUP, \
1918 .single_branch = -1, \
1922 static void update_data_release(struct update_data
*ud
)
1924 free(ud
->displaypath
);
1925 module_list_release(&ud
->list
);
1928 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1929 struct strbuf
*out
, const char *displaypath
)
1932 * Only mention uninitialized submodules when their
1933 * paths have been specified.
1935 if (suc
->update_data
->warn_if_uninitialized
) {
1937 _("Submodule path '%s' not initialized"),
1939 strbuf_addch(out
, '\n');
1941 _("Maybe you want to use 'update --init'?"));
1942 strbuf_addch(out
, '\n');
1947 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1948 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1950 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1951 struct child_process
*child
,
1952 struct submodule_update_clone
*suc
,
1955 const struct submodule
*sub
= NULL
;
1956 const char *url
= NULL
;
1957 const char *update_string
;
1958 enum submodule_update_type update_type
;
1960 const struct update_data
*ud
= suc
->update_data
;
1961 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
);
1962 struct strbuf sb
= STRBUF_INIT
;
1963 int needs_cloning
= 0;
1964 int need_free_url
= 0;
1967 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
1968 strbuf_addch(out
, '\n');
1972 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
1975 next_submodule_warn_missing(suc
, out
, displaypath
);
1979 key
= xstrfmt("submodule.%s.update", sub
->name
);
1980 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
1981 update_type
= parse_submodule_update_type(update_string
);
1983 update_type
= sub
->update_strategy
.type
;
1987 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
1988 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
1989 && update_type
== SM_UPDATE_NONE
)) {
1990 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
1991 strbuf_addch(out
, '\n');
1995 /* Check if the submodule has been initialized. */
1996 if (!is_submodule_active(the_repository
, ce
->name
)) {
1997 next_submodule_warn_missing(suc
, out
, displaypath
);
2002 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2003 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2004 if (starts_with_dot_slash(sub
->url
) ||
2005 starts_with_dot_dot_slash(sub
->url
)) {
2006 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2013 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2014 needs_cloning
= !file_exists(sb
.buf
);
2016 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2017 suc
->update_clone_alloc
);
2018 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2019 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2020 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2021 suc
->update_clone_nr
++;
2027 child
->no_stdin
= 1;
2028 child
->stdout_to_stderr
= 1;
2030 strvec_push(&child
->args
, "submodule--helper");
2031 strvec_push(&child
->args
, "clone");
2032 if (suc
->update_data
->progress
)
2033 strvec_push(&child
->args
, "--progress");
2034 if (suc
->update_data
->quiet
)
2035 strvec_push(&child
->args
, "--quiet");
2036 if (suc
->update_data
->prefix
)
2037 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2038 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2039 strvec_push(&child
->args
, "--depth=1");
2040 else if (suc
->update_data
->depth
)
2041 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2042 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2043 strvec_pushf(&child
->args
, "--filter=%s",
2044 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2045 if (suc
->update_data
->require_init
)
2046 strvec_push(&child
->args
, "--require-init");
2047 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2048 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2049 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2050 if (suc
->update_data
->references
.nr
) {
2051 struct string_list_item
*item
;
2053 for_each_string_list_item(item
, &suc
->update_data
->references
)
2054 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2056 if (suc
->update_data
->dissociate
)
2057 strvec_push(&child
->args
, "--dissociate");
2058 if (suc
->update_data
->single_branch
>= 0)
2059 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2061 "--no-single-branch");
2065 strbuf_release(&sb
);
2069 return needs_cloning
;
2072 static int update_clone_get_next_task(struct child_process
*child
,
2077 struct submodule_update_clone
*suc
= suc_cb
;
2078 const struct cache_entry
*ce
;
2081 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2082 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2083 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2084 int *p
= xmalloc(sizeof(*p
));
2094 * The loop above tried cloning each submodule once, now try the
2095 * stragglers again, which we can imagine as an extension of the
2098 index
= suc
->current
- suc
->update_data
->list
.nr
;
2099 if (index
< suc
->failed_clones_nr
) {
2102 ce
= suc
->failed_clones
[index
];
2103 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2105 strbuf_addstr(err
, "BUG: submodule considered for "
2106 "cloning, doesn't need cloning "
2110 p
= xmalloc(sizeof(*p
));
2120 static int update_clone_start_failure(struct strbuf
*err
,
2124 struct submodule_update_clone
*suc
= suc_cb
;
2130 static int update_clone_task_finished(int result
,
2135 const struct cache_entry
*ce
;
2136 struct submodule_update_clone
*suc
= suc_cb
;
2137 int *idxP
= idx_task_cb
;
2145 if (idx
< suc
->update_data
->list
.nr
) {
2146 ce
= suc
->update_data
->list
.entries
[idx
];
2147 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2149 strbuf_addch(err
, '\n');
2150 ALLOC_GROW(suc
->failed_clones
,
2151 suc
->failed_clones_nr
+ 1,
2152 suc
->failed_clones_alloc
);
2153 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2156 idx
-= suc
->update_data
->list
.nr
;
2157 ce
= suc
->failed_clones
[idx
];
2158 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2160 strbuf_addch(err
, '\n');
2168 static int git_update_clone_config(const char *var
, const char *value
,
2173 if (!strcmp(var
, "submodule.fetchjobs"))
2174 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2178 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2180 struct child_process cp
= CHILD_PROCESS_INIT
;
2181 struct strbuf rev
= STRBUF_INIT
;
2182 char *hex
= oid_to_hex(oid
);
2187 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2189 prepare_submodule_repo_env(&cp
.env
);
2191 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2197 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2198 const struct object_id
*oid
)
2200 struct child_process cp
= CHILD_PROCESS_INIT
;
2202 prepare_submodule_repo_env(&cp
.env
);
2204 cp
.dir
= module_path
;
2206 strvec_push(&cp
.args
, "fetch");
2208 strvec_push(&cp
.args
, "--quiet");
2210 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2212 char *hex
= oid_to_hex(oid
);
2213 char *remote
= get_default_remote();
2215 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2219 return run_command(&cp
);
2222 static int run_update_command(const struct update_data
*ud
, int subforce
)
2224 struct child_process cp
= CHILD_PROCESS_INIT
;
2225 char *oid
= oid_to_hex(&ud
->oid
);
2228 switch (ud
->update_strategy
.type
) {
2229 case SM_UPDATE_CHECKOUT
:
2231 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2233 strvec_push(&cp
.args
, "-f");
2235 case SM_UPDATE_REBASE
:
2237 strvec_push(&cp
.args
, "rebase");
2239 strvec_push(&cp
.args
, "--quiet");
2241 case SM_UPDATE_MERGE
:
2243 strvec_push(&cp
.args
, "merge");
2245 strvec_push(&cp
.args
, "--quiet");
2247 case SM_UPDATE_COMMAND
:
2249 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2252 BUG("unexpected update strategy type: %d",
2253 ud
->update_strategy
.type
);
2255 strvec_push(&cp
.args
, oid
);
2257 cp
.dir
= ud
->sm_path
;
2258 prepare_submodule_repo_env(&cp
.env
);
2259 if ((ret
= run_command(&cp
))) {
2260 switch (ud
->update_strategy
.type
) {
2261 case SM_UPDATE_CHECKOUT
:
2262 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2263 oid
, ud
->displaypath
);
2264 /* No "ret" assignment, use "git checkout"'s */
2266 case SM_UPDATE_REBASE
:
2267 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2268 oid
, ud
->displaypath
);
2270 case SM_UPDATE_MERGE
:
2271 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2272 oid
, ud
->displaypath
);
2274 case SM_UPDATE_COMMAND
:
2275 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2276 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2279 BUG("unexpected update strategy type: %d",
2280 ud
->update_strategy
.type
);
2289 switch (ud
->update_strategy
.type
) {
2290 case SM_UPDATE_CHECKOUT
:
2291 printf(_("Submodule path '%s': checked out '%s'\n"),
2292 ud
->displaypath
, oid
);
2294 case SM_UPDATE_REBASE
:
2295 printf(_("Submodule path '%s': rebased into '%s'\n"),
2296 ud
->displaypath
, oid
);
2298 case SM_UPDATE_MERGE
:
2299 printf(_("Submodule path '%s': merged in '%s'\n"),
2300 ud
->displaypath
, oid
);
2302 case SM_UPDATE_COMMAND
:
2303 printf(_("Submodule path '%s': '%s %s'\n"),
2304 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2307 BUG("unexpected update strategy type: %d",
2308 ud
->update_strategy
.type
);
2314 static int run_update_procedure(const struct update_data
*ud
)
2316 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2320 * Run fetch only if `oid` isn't present or it
2321 * is not reachable from a ref.
2323 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2324 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2327 _("Unable to fetch in submodule path '%s'; "
2328 "trying to directly fetch %s:"),
2329 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2331 * Now we tried the usual fetch, but `oid` may
2332 * not be reachable from any of the refs.
2334 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2335 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2336 return die_message(_("Fetched in submodule path '%s', but it did not "
2337 "contain %s. Direct fetching of that commit failed."),
2338 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2341 return run_update_command(ud
, subforce
);
2344 static int remote_submodule_branch(const char *path
, const char **branch
)
2346 const struct submodule
*sub
;
2350 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2352 return die_message(_("could not initialize submodule at path '%s'"),
2355 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2356 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2357 *branch
= sub
->branch
;
2365 if (!strcmp(*branch
, ".")) {
2366 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2369 return die_message(_("No such ref: %s"), "HEAD");
2372 if (!strcmp(refname
, "HEAD"))
2373 return die_message(_("Submodule (%s) branch configured to inherit "
2374 "branch from superproject, but the superproject "
2375 "is not on any branch"), sub
->name
);
2377 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2378 return die_message(_("Expecting a full ref name, got %s"),
2385 /* Our "branch" is coming from repo_config_get_string_tmp() */
2389 static int ensure_core_worktree(const char *path
)
2392 struct repository subrepo
;
2394 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2395 return die_message(_("could not get a repository handle for submodule '%s'"),
2398 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2399 char *cfg_file
, *abs_path
;
2400 const char *rel_path
;
2401 struct strbuf sb
= STRBUF_INIT
;
2403 cfg_file
= repo_git_path(&subrepo
, "config");
2405 abs_path
= absolute_pathdup(path
);
2406 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2408 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2412 strbuf_release(&sb
);
2415 repo_clear(&subrepo
);
2419 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2422 case SM_UPDATE_CHECKOUT
:
2424 case SM_UPDATE_MERGE
:
2426 case SM_UPDATE_REBASE
:
2428 case SM_UPDATE_UNSPECIFIED
:
2429 case SM_UPDATE_NONE
:
2430 case SM_UPDATE_COMMAND
:
2433 BUG("unreachable with type %d", type
);
2436 static void update_data_to_args(const struct update_data
*update_data
,
2437 struct strvec
*args
)
2439 enum submodule_update_type update_type
= update_data
->update_default
;
2441 if (update_data
->displaypath
) {
2442 strvec_push(args
, "--super-prefix");
2443 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2445 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2446 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2447 if (update_data
->quiet
)
2448 strvec_push(args
, "--quiet");
2449 if (update_data
->force
)
2450 strvec_push(args
, "--force");
2451 if (update_data
->init
)
2452 strvec_push(args
, "--init");
2453 if (update_data
->remote
)
2454 strvec_push(args
, "--remote");
2455 if (update_data
->nofetch
)
2456 strvec_push(args
, "--no-fetch");
2457 if (update_data
->dissociate
)
2458 strvec_push(args
, "--dissociate");
2459 if (update_data
->progress
)
2460 strvec_push(args
, "--progress");
2461 if (update_data
->require_init
)
2462 strvec_push(args
, "--require-init");
2463 if (update_data
->depth
)
2464 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2465 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2466 strvec_pushf(args
, "--%s",
2467 submodule_update_type_to_label(update_type
));
2469 if (update_data
->references
.nr
) {
2470 struct string_list_item
*item
;
2472 for_each_string_list_item(item
, &update_data
->references
)
2473 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2475 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2476 strvec_pushf(args
, "--filter=%s",
2477 expand_list_objects_filter_spec(
2478 update_data
->filter_options
));
2479 if (update_data
->recommend_shallow
== 0)
2480 strvec_push(args
, "--no-recommend-shallow");
2481 else if (update_data
->recommend_shallow
== 1)
2482 strvec_push(args
, "--recommend-shallow");
2483 if (update_data
->single_branch
>= 0)
2484 strvec_push(args
, update_data
->single_branch
?
2486 "--no-single-branch");
2489 static int update_submodule(struct update_data
*update_data
)
2493 ret
= determine_submodule_update_strategy(the_repository
,
2494 update_data
->just_cloned
,
2495 update_data
->sm_path
,
2496 update_data
->update_default
,
2497 &update_data
->update_strategy
);
2501 if (update_data
->just_cloned
)
2502 oidcpy(&update_data
->suboid
, null_oid());
2503 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2504 return die_message(_("Unable to find current revision in submodule path '%s'"),
2505 update_data
->displaypath
);
2507 if (update_data
->remote
) {
2513 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2516 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2519 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2523 if (!update_data
->nofetch
) {
2524 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2526 return die_message(_("Unable to fetch in submodule path '%s'"),
2527 update_data
->sm_path
);
2530 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2531 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2532 remote_ref
, update_data
->sm_path
);
2537 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2538 ret
= run_update_procedure(update_data
);
2543 if (update_data
->recursive
) {
2544 struct child_process cp
= CHILD_PROCESS_INIT
;
2545 struct update_data next
= *update_data
;
2548 oidcpy(&next
.oid
, null_oid());
2549 oidcpy(&next
.suboid
, null_oid());
2551 cp
.dir
= update_data
->sm_path
;
2553 prepare_submodule_repo_env(&cp
.env
);
2554 update_data_to_args(&next
, &cp
.args
);
2556 ret
= run_command(&cp
);
2558 die_message(_("Failed to recurse into submodule path '%s'"),
2559 update_data
->displaypath
);
2566 static int update_submodules(struct update_data
*update_data
)
2569 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2570 const struct run_process_parallel_opts opts
= {
2571 .tr2_category
= "submodule",
2572 .tr2_label
= "parallel/update",
2574 .processes
= update_data
->max_jobs
,
2576 .get_next_task
= update_clone_get_next_task
,
2577 .start_failure
= update_clone_start_failure
,
2578 .task_finished
= update_clone_task_finished
,
2582 suc
.update_data
= update_data
;
2583 run_processes_parallel(&opts
);
2586 * We saved the output and put it out all at once now.
2588 * - the listener does not have to interleave their (checkout)
2589 * work with our fetching. The writes involved in a
2590 * checkout involve more straightforward sequential I/O.
2591 * - the listener can avoid doing any work if fetching failed.
2593 if (suc
.quickstop
) {
2598 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2599 struct update_clone_data ucd
= suc
.update_clone
[i
];
2602 oidcpy(&update_data
->oid
, &ucd
.oid
);
2603 update_data
->just_cloned
= ucd
.just_cloned
;
2604 update_data
->sm_path
= ucd
.sub
->path
;
2606 code
= ensure_core_worktree(update_data
->sm_path
);
2610 update_data
->displaypath
= get_submodule_displaypath(
2611 update_data
->sm_path
, update_data
->prefix
);
2612 code
= update_submodule(update_data
);
2613 FREE_AND_NULL(update_data
->displaypath
);
2623 submodule_update_clone_release(&suc
);
2624 string_list_clear(&update_data
->references
, 0);
2628 static int module_update(int argc
, const char **argv
, const char *prefix
)
2630 struct pathspec pathspec
= { 0 };
2631 struct pathspec pathspec2
= { 0 };
2632 struct update_data opt
= UPDATE_DATA_INIT
;
2633 struct list_objects_filter_options filter_options
=
2634 LIST_OBJECTS_FILTER_INIT
;
2636 struct option module_update_options
[] = {
2637 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2638 OPT_BOOL(0, "init", &opt
.init
,
2639 N_("initialize uninitialized submodules before update")),
2640 OPT_BOOL(0, "remote", &opt
.remote
,
2641 N_("use SHA-1 of submodule's remote tracking branch")),
2642 OPT_BOOL(0, "recursive", &opt
.recursive
,
2643 N_("traverse submodules recursively")),
2644 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2645 N_("don't fetch new objects from the remote site")),
2646 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2647 N_("use the 'checkout' update strategy (default)"),
2648 SM_UPDATE_CHECKOUT
),
2649 OPT_SET_INT('m', "merge", &opt
.update_default
,
2650 N_("use the 'merge' update strategy"),
2652 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2653 N_("use the 'rebase' update strategy"),
2655 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2656 N_("reference repository")),
2657 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2658 N_("use --reference only while cloning")),
2659 OPT_INTEGER(0, "depth", &opt
.depth
,
2660 N_("create a shallow clone truncated to the "
2661 "specified number of revisions")),
2662 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2663 N_("parallel jobs")),
2664 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2665 N_("whether the initial clone should follow the shallow recommendation")),
2666 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2667 OPT_BOOL(0, "progress", &opt
.progress
,
2668 N_("force cloning progress")),
2669 OPT_BOOL(0, "require-init", &opt
.require_init
,
2670 N_("disallow cloning into non-empty directory, implies --init")),
2671 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2672 N_("clone only one branch, HEAD or --branch")),
2673 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2676 const char *const git_submodule_helper_usage
[] = {
2677 N_("git submodule [--quiet] update"
2678 " [--init [--filter=<filter-spec>]] [--remote]"
2679 " [-N|--no-fetch] [-f|--force]"
2680 " [--checkout|--merge|--rebase]"
2681 " [--[no-]recommend-shallow] [--reference <repository>]"
2682 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2686 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2687 git_config(git_update_clone_config
, &opt
.max_jobs
);
2689 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2690 git_submodule_helper_usage
, 0);
2692 if (opt
.require_init
)
2695 if (filter_options
.choice
&& !opt
.init
) {
2696 usage_with_options(git_submodule_helper_usage
,
2697 module_update_options
);
2700 opt
.filter_options
= &filter_options
;
2701 opt
.prefix
= prefix
;
2703 if (opt
.update_default
)
2704 opt
.update_strategy
.type
= opt
.update_default
;
2706 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2712 opt
.warn_if_uninitialized
= 1;
2715 struct module_list list
= MODULE_LIST_INIT
;
2716 struct init_cb info
= INIT_CB_INIT
;
2718 if (module_list_compute(argv
, opt
.prefix
,
2719 &pathspec2
, &list
) < 0) {
2720 module_list_release(&list
);
2726 * If there are no path args and submodule.active is set then,
2727 * by default, only initialize 'active' modules.
2729 if (!argc
&& git_config_get_value_multi("submodule.active"))
2730 module_list_active(&list
);
2732 info
.prefix
= opt
.prefix
;
2734 info
.flags
|= OPT_QUIET
;
2736 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2737 module_list_release(&list
);
2740 ret
= update_submodules(&opt
);
2742 update_data_release(&opt
);
2743 list_objects_filter_release(&filter_options
);
2744 clear_pathspec(&pathspec
);
2745 clear_pathspec(&pathspec2
);
2749 static int push_check(int argc
, const char **argv
, const char *prefix
)
2751 struct remote
*remote
;
2752 const char *superproject_head
;
2754 int detached_head
= 0;
2755 struct object_id head_oid
;
2758 die("submodule--helper push-check requires at least 2 arguments");
2761 * superproject's resolved head ref.
2762 * if HEAD then the superproject is in a detached head state, otherwise
2763 * it will be the resolved head ref.
2765 superproject_head
= argv
[1];
2768 /* Get the submodule's head ref and determine if it is detached */
2769 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2771 die(_("Failed to resolve HEAD as a valid ref."));
2772 if (!strcmp(head
, "HEAD"))
2776 * The remote must be configured.
2777 * This is to avoid pushing to the exact same URL as the parent.
2779 remote
= pushremote_get(argv
[1]);
2780 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2781 die("remote '%s' not configured", argv
[1]);
2783 /* Check the refspec */
2786 struct ref
*local_refs
= get_local_heads();
2787 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2789 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2791 for (i
= 0; i
< refspec
.nr
; i
++) {
2792 const struct refspec_item
*rs
= &refspec
.items
[i
];
2794 if (rs
->pattern
|| rs
->matching
)
2797 /* LHS must match a single ref */
2798 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2803 * If LHS matches 'HEAD' then we need to ensure
2804 * that it matches the same named branch
2805 * checked out in the superproject.
2807 if (!strcmp(rs
->src
, "HEAD")) {
2808 if (!detached_head
&&
2809 !strcmp(head
, superproject_head
))
2811 die("HEAD does not match the named branch in the superproject");
2815 die("src refspec '%s' must name a ref",
2819 refspec_clear(&refspec
);
2826 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2829 struct pathspec pathspec
= { 0 };
2830 struct module_list list
= MODULE_LIST_INIT
;
2831 struct option embed_gitdir_options
[] = {
2834 const char *const git_submodule_helper_usage
[] = {
2835 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2840 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2841 git_submodule_helper_usage
, 0);
2843 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
2846 for (i
= 0; i
< list
.nr
; i
++)
2847 absorb_git_dir_into_superproject(list
.entries
[i
]->name
);
2851 clear_pathspec(&pathspec
);
2852 module_list_release(&list
);
2856 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2862 struct option options
[] = {
2863 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2866 const char *const usage
[] = {
2867 N_("git submodule set-url [--quiet] <path> <newurl>"),
2871 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2873 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2874 usage_with_options(usage
, options
);
2876 config_name
= xstrfmt("submodule.%s.url", path
);
2878 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2879 sync_submodule(path
, prefix
, quiet
? OPT_QUIET
: 0);
2886 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2888 int opt_default
= 0, ret
;
2889 const char *opt_branch
= NULL
;
2892 struct option options
[] = {
2894 * We accept the `quiet` option for uniformity across subcommands,
2895 * though there is nothing to make less verbose in this subcommand.
2897 OPT_NOOP_NOARG('q', "quiet"),
2899 OPT_BOOL('d', "default", &opt_default
,
2900 N_("set the default tracking branch to master")),
2901 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2902 N_("set the default tracking branch")),
2905 const char *const usage
[] = {
2906 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2907 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2911 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2913 if (!opt_branch
&& !opt_default
)
2914 die(_("--branch or --default required"));
2916 if (opt_branch
&& opt_default
)
2917 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2919 if (argc
!= 1 || !(path
= argv
[0]))
2920 usage_with_options(usage
, options
);
2922 config_name
= xstrfmt("submodule.%s.branch", path
);
2923 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2929 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
2931 enum branch_track track
;
2932 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
2933 struct option options
[] = {
2934 OPT__QUIET(&quiet
, N_("print only error messages")),
2935 OPT__FORCE(&force
, N_("force creation"), 0),
2936 OPT_BOOL(0, "create-reflog", &reflog
,
2937 N_("create the branch's reflog")),
2938 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
2939 N_("set branch tracking configuration"),
2941 parse_opt_tracking_mode
),
2942 OPT__DRY_RUN(&dry_run
,
2943 N_("show whether the branch would be created")),
2946 const char *const usage
[] = {
2947 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2951 git_config(git_default_config
, NULL
);
2952 track
= git_branch_track
;
2953 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2956 usage_with_options(usage
, options
);
2958 if (!quiet
&& !dry_run
)
2959 printf_ln(_("creating branch '%s'"), argv
[0]);
2961 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
2962 force
, reflog
, quiet
, track
, dry_run
);
2969 const char *reference_path
;
2971 const char *sm_name
;
2973 const char *realrepo
;
2975 unsigned int force
: 1;
2976 unsigned int quiet
: 1;
2977 unsigned int progress
: 1;
2978 unsigned int dissociate
: 1;
2980 #define ADD_DATA_INIT { .depth = -1 }
2982 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
2984 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
2985 struct strbuf sb_remote_out
= STRBUF_INIT
;
2987 cp_remote
.git_cmd
= 1;
2988 strvec_pushf(&cp_remote
.env
,
2989 "GIT_DIR=%s", git_dir_path
);
2990 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
2991 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
2992 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
2994 char *line
= sb_remote_out
.buf
;
2996 while ((next_line
= strchr(line
, '\n')) != NULL
) {
2997 size_t len
= next_line
- line
;
2999 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3000 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3001 line
= next_line
+ 1;
3005 strbuf_release(&sb_remote_out
);
3008 static int add_submodule(const struct add_data
*add_data
)
3010 char *submod_gitdir_path
;
3011 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3012 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3015 /* perhaps the path already exists and is already a git repo, else clone it */
3016 if (is_directory(add_data
->sm_path
)) {
3017 struct strbuf sm_path
= STRBUF_INIT
;
3018 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3019 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3020 if (is_nonbare_repository_dir(&sm_path
))
3021 printf(_("Adding existing repo at '%s' to the index\n"),
3024 die(_("'%s' already exists and is not a valid git repo"),
3026 strbuf_release(&sm_path
);
3027 free(submod_gitdir_path
);
3029 struct child_process cp
= CHILD_PROCESS_INIT
;
3031 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3033 if (is_directory(submod_gitdir_path
)) {
3034 if (!add_data
->force
) {
3035 struct strbuf msg
= STRBUF_INIT
;
3038 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3039 "locally with remote(s):\n"),
3042 append_fetch_remotes(&msg
, submod_gitdir_path
);
3043 free(submod_gitdir_path
);
3045 strbuf_addf(&msg
, _("If you want to reuse this local git "
3046 "directory instead of cloning again from\n"
3048 "use the '--force' option. If the local git "
3049 "directory is not the correct repo\n"
3050 "or you are unsure what this means choose "
3051 "another name with the '--name' option."),
3052 add_data
->realrepo
);
3054 die_msg
= strbuf_detach(&msg
, NULL
);
3057 printf(_("Reactivating local git directory for "
3058 "submodule '%s'\n"), add_data
->sm_name
);
3061 free(submod_gitdir_path
);
3063 clone_data
.prefix
= add_data
->prefix
;
3064 clone_data
.path
= add_data
->sm_path
;
3065 clone_data
.name
= add_data
->sm_name
;
3066 clone_data
.url
= add_data
->realrepo
;
3067 clone_data
.quiet
= add_data
->quiet
;
3068 clone_data
.progress
= add_data
->progress
;
3069 if (add_data
->reference_path
) {
3070 char *p
= xstrdup(add_data
->reference_path
);
3072 string_list_append(&reference
, p
)->util
= p
;
3074 clone_data
.dissociate
= add_data
->dissociate
;
3075 if (add_data
->depth
>= 0)
3076 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3078 if (clone_submodule(&clone_data
, &reference
))
3081 prepare_submodule_repo_env(&cp
.env
);
3083 cp
.dir
= add_data
->sm_path
;
3085 * NOTE: we only get here if add_data->force is true, so
3086 * passing --force to checkout is reasonable.
3088 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3090 if (add_data
->branch
) {
3091 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3092 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3095 if (run_command(&cp
))
3096 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3100 string_list_clear(&reference
, 1);
3104 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3109 if (!is_writing_gitmodules_ok())
3110 die(_("please make sure that the .gitmodules file is in the working tree"));
3112 key
= xstrfmt("submodule.%s.%s", name
, var
);
3113 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3119 static void configure_added_submodule(struct add_data
*add_data
)
3123 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3124 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3126 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3127 git_config_set_gently(key
, add_data
->realrepo
);
3130 add_submod
.git_cmd
= 1;
3131 strvec_pushl(&add_submod
.args
, "add",
3132 "--no-warn-embedded-repo", NULL
);
3133 if (add_data
->force
)
3134 strvec_push(&add_submod
.args
, "--force");
3135 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3137 if (run_command(&add_submod
))
3138 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3140 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3141 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3142 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3144 if (add_data
->branch
) {
3145 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3146 "branch", add_data
->branch
))
3147 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3150 add_gitmodules
.git_cmd
= 1;
3151 strvec_pushl(&add_gitmodules
.args
,
3152 "add", "--force", "--", ".gitmodules", NULL
);
3154 if (run_command(&add_gitmodules
))
3155 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3158 * NEEDSWORK: In a multi-working-tree world this needs to be
3159 * set in the per-worktree config.
3162 * NEEDSWORK: In the longer run, we need to get rid of this
3163 * pattern of querying "submodule.active" before calling
3164 * is_submodule_active(), since that function needs to find
3165 * out the value of "submodule.active" again anyway.
3167 if (!git_config_get_string_tmp("submodule.active", &val
)) {
3169 * If the submodule being added isn't already covered by the
3170 * current configured pathspec, set the submodule's active flag
3172 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3173 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3174 git_config_set_gently(key
, "true");
3178 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3179 git_config_set_gently(key
, "true");
3184 static void die_on_index_match(const char *path
, int force
)
3187 const char *args
[] = { path
, NULL
};
3188 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3190 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3191 die(_("index file corrupt"));
3195 char *ps_matched
= xcalloc(ps
.nr
, 1);
3197 /* TODO: audit for interaction with sparse-index. */
3198 ensure_full_index(&the_index
);
3201 * Since there is only one pathspec, we just need
3202 * need to check ps_matched[0] to know if a cache
3205 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
3206 ce_path_match(&the_index
, the_index
.cache
[i
], &ps
,
3209 if (ps_matched
[0]) {
3211 die(_("'%s' already exists in the index"),
3213 if (!S_ISGITLINK(the_index
.cache
[i
]->ce_mode
))
3214 die(_("'%s' already exists in the index "
3215 "and is not a submodule"), path
);
3221 clear_pathspec(&ps
);
3224 static void die_on_repo_without_commits(const char *path
)
3226 struct strbuf sb
= STRBUF_INIT
;
3227 strbuf_addstr(&sb
, path
);
3228 if (is_nonbare_repository_dir(&sb
)) {
3229 struct object_id oid
;
3230 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3231 die(_("'%s' does not have a commit checked out"), path
);
3233 strbuf_release(&sb
);
3236 static int module_add(int argc
, const char **argv
, const char *prefix
)
3238 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3239 struct add_data add_data
= ADD_DATA_INIT
;
3240 char *to_free
= NULL
;
3241 struct option options
[] = {
3242 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3243 N_("branch of repository to add as submodule")),
3244 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3245 PARSE_OPT_NOCOMPLETE
),
3246 OPT__QUIET(&quiet
, N_("print only error messages")),
3247 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3248 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3249 N_("reference repository")),
3250 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3251 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3252 N_("sets the submodule's name to the given string "
3253 "instead of defaulting to its path")),
3254 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3257 const char *const usage
[] = {
3258 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3261 struct strbuf sb
= STRBUF_INIT
;
3264 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3266 if (!is_writing_gitmodules_ok())
3267 die(_("please make sure that the .gitmodules file is in the working tree"));
3269 if (prefix
&& *prefix
&&
3270 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3271 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3273 if (argc
== 0 || argc
> 2)
3274 usage_with_options(usage
, options
);
3276 add_data
.repo
= argv
[0];
3278 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3280 add_data
.sm_path
= xstrdup(argv
[1]);
3282 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3283 char *sm_path
= add_data
.sm_path
;
3285 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3289 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3290 starts_with_dot_slash(add_data
.repo
)) {
3292 die(_("Relative path can only be used from the toplevel "
3293 "of the working tree"));
3295 /* dereference source url relative to parent's url */
3296 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3297 add_data
.realrepo
= to_free
;
3298 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3299 add_data
.realrepo
= add_data
.repo
;
3301 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3307 * multiple //; leading ./; /./; /../;
3309 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3310 strip_dir_trailing_slashes(add_data
.sm_path
);
3312 die_on_index_match(add_data
.sm_path
, force
);
3313 die_on_repo_without_commits(add_data
.sm_path
);
3316 struct child_process cp
= CHILD_PROCESS_INIT
;
3320 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3321 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3322 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3323 strbuf_complete_line(&sb
);
3324 fputs(sb
.buf
, stderr
);
3329 if(!add_data
.sm_name
)
3330 add_data
.sm_name
= add_data
.sm_path
;
3332 if (check_submodule_name(add_data
.sm_name
))
3333 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3335 add_data
.prefix
= prefix
;
3336 add_data
.force
= !!force
;
3337 add_data
.quiet
= !!quiet
;
3338 add_data
.progress
= !!progress
;
3339 add_data
.dissociate
= !!dissociate
;
3341 if (add_submodule(&add_data
))
3343 configure_added_submodule(&add_data
);
3347 free(add_data
.sm_path
);
3349 strbuf_release(&sb
);
3354 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3356 const char *cmd
= argv
[0];
3358 parse_opt_subcommand_fn
*fn
= NULL
;
3359 const char *const usage
[] = {
3360 N_("git submodule--helper <command>"),
3363 struct option options
[] = {
3364 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3365 OPT_SUBCOMMAND("add", &fn
, module_add
),
3366 OPT_SUBCOMMAND("update", &fn
, module_update
),
3367 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3368 OPT_SUBCOMMAND("init", &fn
, module_init
),
3369 OPT_SUBCOMMAND("status", &fn
, module_status
),
3370 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3371 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3372 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3373 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3374 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3375 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3376 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3377 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3380 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3383 if (strcmp(subcmd
, "clone") && strcmp(subcmd
, "update") &&
3384 strcmp(subcmd
, "foreach") && strcmp(subcmd
, "status") &&
3385 strcmp(subcmd
, "sync") && strcmp(subcmd
, "absorbgitdirs") &&
3388 * xstrfmt() rather than "%s %s" to keep the translated
3389 * string identical to git.c's.
3391 die(_("%s doesn't support --super-prefix"),
3392 xstrfmt("'%s %s'", cmd
, subcmd
));
3394 return fn(argc
, argv
, prefix
);