3 #include "environment.h"
6 #include "repository.h"
8 #include "parse-options.h"
12 #include "preload-index.h"
14 #include "read-cache.h"
16 #include "sparse-index.h"
17 #include "submodule.h"
18 #include "submodule-config.h"
19 #include "string-list.h"
20 #include "run-command.h"
27 #include "object-file.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
32 #include "list-objects-filter-options.h"
34 #define OPT_QUIET (1 << 0)
35 #define OPT_CACHED (1 << 1)
36 #define OPT_RECURSIVE (1 << 2)
37 #define OPT_FORCE (1 << 3)
39 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
42 static int repo_get_default_remote(struct repository
*repo
, char **default_remote
)
45 struct strbuf sb
= STRBUF_INIT
;
46 struct ref_store
*store
= get_main_ref_store(repo
);
47 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
51 return die_message(_("No such ref: %s"), "HEAD");
54 if (!strcmp(refname
, "HEAD")) {
55 *default_remote
= xstrdup("origin");
59 if (!skip_prefix(refname
, "refs/heads/", &refname
))
60 return die_message(_("Expecting a full ref name, got %s"),
63 strbuf_addf(&sb
, "branch.%s.remote", refname
);
64 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
65 *default_remote
= xstrdup("origin");
67 *default_remote
= dest
;
73 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
75 struct repository subrepo
;
78 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
80 return die_message(_("could not get a repository handle for submodule '%s'"),
82 ret
= repo_get_default_remote(&subrepo
, default_remote
);
88 static char *get_default_remote(void)
91 int code
= repo_get_default_remote(the_repository
, &default_remote
);
96 return default_remote
;
99 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
101 char *remoteurl
, *resolved_url
;
102 char *remote
= get_default_remote();
103 struct strbuf remotesb
= STRBUF_INIT
;
105 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
106 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
108 warning(_("could not look up configuration '%s'. "
109 "Assuming this repository is its own "
110 "authoritative upstream."),
112 remoteurl
= xgetcwd();
114 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
118 strbuf_release(&remotesb
);
123 /* the result should be freed by the caller. */
124 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
125 const char *super_prefix
)
127 if (prefix
&& super_prefix
) {
128 BUG("cannot have prefix '%s' and superprefix '%s'",
129 prefix
, super_prefix
);
131 struct strbuf sb
= STRBUF_INIT
;
132 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
135 } else if (super_prefix
) {
136 return xstrfmt("%s%s", super_prefix
, path
);
138 return xstrdup(path
);
142 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
144 struct strbuf sb
= STRBUF_INIT
;
147 static const char *describe_bare
[] = { NULL
};
149 static const char *describe_tags
[] = { "--tags", NULL
};
151 static const char *describe_contains
[] = { "--contains", NULL
};
153 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
155 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
157 describe_all_always
, NULL
};
159 for (d
= describe_argv
; *d
; d
++) {
160 struct child_process cp
= CHILD_PROCESS_INIT
;
161 prepare_submodule_repo_env(&cp
.env
);
166 strvec_push(&cp
.args
, "describe");
167 strvec_pushv(&cp
.args
, *d
);
168 strvec_push(&cp
.args
, object_id
);
170 if (!capture_command(&cp
, &sb
, 0)) {
171 strbuf_strip_suffix(&sb
, "\n");
172 return strbuf_detach(&sb
, NULL
);
181 const struct cache_entry
**entries
;
184 #define MODULE_LIST_INIT { 0 }
186 static void module_list_release(struct module_list
*ml
)
191 static int module_list_compute(const char **argv
,
193 struct pathspec
*pathspec
,
194 struct module_list
*list
)
197 char *ps_matched
= NULL
;
199 parse_pathspec(pathspec
, 0,
200 PATHSPEC_PREFER_FULL
,
204 ps_matched
= xcalloc(pathspec
->nr
, 1);
206 if (repo_read_index(the_repository
) < 0)
207 die(_("index file corrupt"));
209 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
210 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
212 if (!match_pathspec(the_repository
->index
, pathspec
, ce
->name
, ce_namelen(ce
),
214 !S_ISGITLINK(ce
->ce_mode
))
217 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
218 list
->entries
[list
->nr
++] = ce
;
219 while (i
+ 1 < the_repository
->index
->cache_nr
&&
220 !strcmp(ce
->name
, the_repository
->index
->cache
[i
+ 1]->name
))
222 * Skip entries with the same name in different stages
223 * to make sure an entry is returned only once.
228 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
236 static void module_list_active(struct module_list
*list
)
239 struct module_list active_modules
= MODULE_LIST_INIT
;
241 for (i
= 0; i
< list
->nr
; i
++) {
242 const struct cache_entry
*ce
= list
->entries
[i
];
244 if (!is_submodule_active(the_repository
, ce
->name
))
247 ALLOC_GROW(active_modules
.entries
,
248 active_modules
.nr
+ 1,
249 active_modules
.alloc
);
250 active_modules
.entries
[active_modules
.nr
++] = ce
;
253 module_list_release(list
);
254 *list
= active_modules
;
257 static char *get_up_path(const char *path
)
260 struct strbuf sb
= STRBUF_INIT
;
262 for (i
= count_slashes(path
); i
; i
--)
263 strbuf_addstr(&sb
, "../");
266 * Check if 'path' ends with slash or not
267 * for having the same output for dir/sub_dir
270 if (!is_dir_sep(path
[strlen(path
) - 1]))
271 strbuf_addstr(&sb
, "../");
273 return strbuf_detach(&sb
, NULL
);
276 static void for_each_listed_submodule(const struct module_list
*list
,
277 each_submodule_fn fn
, void *cb_data
)
281 for (i
= 0; i
< list
->nr
; i
++)
282 fn(list
->entries
[i
], cb_data
);
289 const char *super_prefix
;
293 #define FOREACH_CB_INIT { 0 }
295 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
298 struct foreach_cb
*info
= cb_data
;
299 const char *path
= list_item
->name
;
300 const struct object_id
*ce_oid
= &list_item
->oid
;
301 const struct submodule
*sub
;
302 struct child_process cp
= CHILD_PROCESS_INIT
;
305 if (validate_submodule_path(path
) < 0)
308 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
311 sub
= submodule_from_path(the_repository
, null_oid(), path
);
314 die(_("No url found for submodule path '%s' in .gitmodules"),
317 if (!is_submodule_populated_gently(path
, NULL
))
320 prepare_submodule_repo_env(&cp
.env
);
323 * For the purpose of executing <command> in the submodule,
324 * separate shell is used for the purpose of running the
331 * NEEDSWORK: the command currently has access to the variables $name,
332 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
333 * contains a single argument. This is done for maintaining a faithful
334 * translation from shell script.
336 if (info
->argc
== 1) {
337 char *toplevel
= xgetcwd();
338 struct strbuf sb
= STRBUF_INIT
;
340 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
341 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
342 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
343 strvec_pushf(&cp
.env
, "sha1=%s",
345 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
348 * Since the path variable was accessible from the script
349 * before porting, it is also made available after porting.
350 * The environment variable "PATH" has a very special purpose
351 * on windows. And since environment variables are
352 * case-insensitive in windows, it interferes with the
353 * existing PATH variable. Hence, to avoid that, we expose
354 * path via the args strvec and not via env.
356 sq_quote_buf(&sb
, path
);
357 strvec_pushf(&cp
.args
, "path=%s; %s",
358 sb
.buf
, info
->argv
[0]);
362 strvec_pushv(&cp
.args
, info
->argv
);
366 printf(_("Entering '%s'\n"), displaypath
);
368 if (info
->argv
[0] && run_command(&cp
))
369 die(_("run_command returned non-zero status for %s\n."),
372 if (info
->recursive
) {
373 struct child_process cpr
= CHILD_PROCESS_INIT
;
377 prepare_submodule_repo_env(&cpr
.env
);
379 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
381 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
382 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
385 strvec_push(&cpr
.args
, "--quiet");
387 strvec_push(&cpr
.args
, "--");
388 strvec_pushv(&cpr
.args
, info
->argv
);
390 if (run_command(&cpr
))
391 die(_("run_command returned non-zero status while "
392 "recursing in the nested submodules of %s\n."),
400 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
402 struct foreach_cb info
= FOREACH_CB_INIT
;
403 struct pathspec pathspec
= { 0 };
404 struct module_list list
= MODULE_LIST_INIT
;
405 struct option module_foreach_options
[] = {
406 OPT__SUPER_PREFIX(&info
.super_prefix
),
407 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
408 OPT_BOOL(0, "recursive", &info
.recursive
,
409 N_("recurse into nested submodules")),
412 const char *const git_submodule_helper_usage
[] = {
413 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
418 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
419 git_submodule_helper_usage
, 0);
421 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
426 info
.prefix
= prefix
;
428 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
432 module_list_release(&list
);
433 clear_pathspec(&pathspec
);
437 static int starts_with_dot_slash(const char *const path
)
439 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
440 PATH_MATCH_XPLATFORM
);
443 static int starts_with_dot_dot_slash(const char *const path
)
445 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
446 PATH_MATCH_XPLATFORM
);
451 const char *super_prefix
;
454 #define INIT_CB_INIT { 0 }
456 static void init_submodule(const char *path
, const char *prefix
,
457 const char *super_prefix
,
460 const struct submodule
*sub
;
461 struct strbuf sb
= STRBUF_INIT
;
463 char *url
= NULL
, *displaypath
;
465 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
467 sub
= submodule_from_path(the_repository
, null_oid(), path
);
470 die(_("No url found for submodule path '%s' in .gitmodules"),
474 * NEEDSWORK: In a multi-working-tree world, this needs to be
475 * set in the per-worktree config.
477 * Set active flag for the submodule being initialized
479 if (!is_submodule_active(the_repository
, path
)) {
480 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
481 git_config_set_gently(sb
.buf
, "true");
486 * Copy url setting when it is not set yet.
487 * To look up the url in .git/config, we must not fall back to
488 * .gitmodules, so look it up directly.
490 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
491 if (git_config_get_string(sb
.buf
, &url
)) {
493 die(_("No url found for submodule path '%s' in .gitmodules"),
496 url
= xstrdup(sub
->url
);
498 /* Possibly a url relative to parent */
499 if (starts_with_dot_dot_slash(url
) ||
500 starts_with_dot_slash(url
)) {
503 url
= resolve_relative_url(oldurl
, NULL
, 0);
507 if (git_config_set_gently(sb
.buf
, url
))
508 die(_("Failed to register url for submodule path '%s'"),
510 if (!(flags
& OPT_QUIET
))
512 _("Submodule '%s' (%s) registered for path '%s'\n"),
513 sub
->name
, url
, displaypath
);
517 /* Copy "update" setting when it is not set yet */
518 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
519 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
520 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
521 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
522 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
526 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
529 if (git_config_set_gently(sb
.buf
, upd
))
530 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
537 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
539 struct init_cb
*info
= cb_data
;
541 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
545 static int module_init(int argc
, const char **argv
, const char *prefix
)
547 struct init_cb info
= INIT_CB_INIT
;
548 struct pathspec pathspec
= { 0 };
549 struct module_list list
= MODULE_LIST_INIT
;
551 struct option module_init_options
[] = {
552 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
555 const char *const git_submodule_helper_usage
[] = {
556 N_("git submodule init [<options>] [<path>]"),
561 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
562 git_submodule_helper_usage
, 0);
564 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
568 * If there are no path args and submodule.active is set then,
569 * by default, only initialize 'active' modules.
571 if (!argc
&& !git_config_get("submodule.active"))
572 module_list_active(&list
);
574 info
.prefix
= prefix
;
576 info
.flags
|= OPT_QUIET
;
578 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
582 module_list_release(&list
);
583 clear_pathspec(&pathspec
);
589 const char *super_prefix
;
592 #define STATUS_CB_INIT { 0 }
594 static void print_status(unsigned int flags
, char state
, const char *path
,
595 const struct object_id
*oid
, const char *displaypath
)
597 if (flags
& OPT_QUIET
)
600 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
602 if (state
== ' ' || state
== '+') {
603 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
606 printf(" (%s)", name
);
613 static int handle_submodule_head_ref(const char *refname UNUSED
,
614 const struct object_id
*oid
,
618 struct object_id
*output
= cb_data
;
626 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
627 unsigned int ce_flags
, const char *prefix
,
628 const char *super_prefix
, unsigned int flags
)
631 struct strvec diff_files_args
= STRVEC_INIT
;
632 struct rev_info rev
= REV_INFO_INIT
;
633 struct strbuf buf
= STRBUF_INIT
;
635 struct setup_revision_opt opt
= {
636 .free_removed_argv_elements
= 1,
639 if (validate_submodule_path(path
) < 0)
642 if (!submodule_from_path(the_repository
, null_oid(), path
))
643 die(_("no submodule mapping found in .gitmodules for path '%s'"),
646 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
648 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
649 print_status(flags
, 'U', path
, null_oid(), displaypath
);
653 strbuf_addf(&buf
, "%s/.git", path
);
654 git_dir
= read_gitfile(buf
.buf
);
658 if (!is_submodule_active(the_repository
, path
) ||
659 !is_git_directory(git_dir
)) {
660 print_status(flags
, '-', path
, ce_oid
, displaypath
);
661 strbuf_release(&buf
);
664 strbuf_release(&buf
);
666 strvec_pushl(&diff_files_args
, "diff-files",
667 "--ignore-submodules=dirty", "--quiet", "--",
670 git_config(git_diff_basic_config
, NULL
);
672 repo_init_revisions(the_repository
, &rev
, NULL
);
674 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
675 run_diff_files(&rev
, 0);
677 if (!diff_result_code(&rev
.diffopt
)) {
678 print_status(flags
, ' ', path
, ce_oid
,
680 } else if (!(flags
& OPT_CACHED
)) {
681 struct object_id oid
;
682 struct ref_store
*refs
= get_submodule_ref_store(path
);
685 print_status(flags
, '-', path
, ce_oid
, displaypath
);
688 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
689 die(_("could not resolve HEAD ref inside the "
690 "submodule '%s'"), path
);
692 print_status(flags
, '+', path
, &oid
, displaypath
);
694 print_status(flags
, '+', path
, ce_oid
, displaypath
);
697 if (flags
& OPT_RECURSIVE
) {
698 struct child_process cpr
= CHILD_PROCESS_INIT
;
702 prepare_submodule_repo_env(&cpr
.env
);
704 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
705 "--recursive", NULL
);
706 strvec_push(&cpr
.args
, "--super-prefix");
707 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
709 if (flags
& OPT_CACHED
)
710 strvec_push(&cpr
.args
, "--cached");
712 if (flags
& OPT_QUIET
)
713 strvec_push(&cpr
.args
, "--quiet");
715 if (run_command(&cpr
))
716 die(_("failed to recurse into submodule '%s'"), path
);
720 strvec_clear(&diff_files_args
);
722 release_revisions(&rev
);
725 static void status_submodule_cb(const struct cache_entry
*list_item
,
728 struct status_cb
*info
= cb_data
;
730 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
731 info
->prefix
, info
->super_prefix
, info
->flags
);
734 static int module_status(int argc
, const char **argv
, const char *prefix
)
736 struct status_cb info
= STATUS_CB_INIT
;
737 struct pathspec pathspec
= { 0 };
738 struct module_list list
= MODULE_LIST_INIT
;
740 struct option module_status_options
[] = {
741 OPT__SUPER_PREFIX(&info
.super_prefix
),
742 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
743 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
744 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
747 const char *const git_submodule_helper_usage
[] = {
748 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
753 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
754 git_submodule_helper_usage
, 0);
756 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
759 info
.prefix
= prefix
;
761 info
.flags
|= OPT_QUIET
;
763 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
767 module_list_release(&list
);
768 clear_pathspec(&pathspec
);
773 unsigned int mod_src
;
774 unsigned int mod_dst
;
775 struct object_id oid_src
;
776 struct object_id oid_dst
;
780 #define MODULE_CB_INIT { 0 }
782 static void module_cb_release(struct module_cb
*mcb
)
787 struct module_cb_list
{
788 struct module_cb
**entries
;
791 #define MODULE_CB_LIST_INIT { 0 }
793 static void module_cb_list_release(struct module_cb_list
*mcbl
)
797 for (i
= 0; i
< mcbl
->nr
; i
++) {
798 struct module_cb
*mcb
= mcbl
->entries
[i
];
800 module_cb_release(mcb
);
810 const char *super_prefix
;
811 unsigned int cached
: 1;
812 unsigned int for_status
: 1;
813 unsigned int files
: 1;
816 #define SUMMARY_CB_INIT { 0 }
823 static char *verify_submodule_committish(const char *sm_path
,
824 const char *committish
)
826 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
827 struct strbuf result
= STRBUF_INIT
;
829 cp_rev_parse
.git_cmd
= 1;
830 cp_rev_parse
.dir
= sm_path
;
831 prepare_submodule_repo_env(&cp_rev_parse
.env
);
832 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
833 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
834 strvec_push(&cp_rev_parse
.args
, "--");
836 if (capture_command(&cp_rev_parse
, &result
, 0))
839 strbuf_trim_trailing_newline(&result
);
840 return strbuf_detach(&result
, NULL
);
843 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
844 int total_commits
, const char *displaypath
,
845 const char *src_abbrev
, const char *dst_abbrev
,
848 if (p
->status
== 'T') {
849 if (S_ISGITLINK(p
->mod_dst
))
850 printf(_("* %s %s(blob)->%s(submodule)"),
851 displaypath
, src_abbrev
, dst_abbrev
);
853 printf(_("* %s %s(submodule)->%s(blob)"),
854 displaypath
, src_abbrev
, dst_abbrev
);
856 printf("* %s %s...%s",
857 displaypath
, src_abbrev
, dst_abbrev
);
860 if (total_commits
< 0)
863 printf(" (%d):\n", total_commits
);
866 printf(_("%s"), errmsg
);
867 } else if (total_commits
> 0) {
868 struct child_process cp_log
= CHILD_PROCESS_INIT
;
871 cp_log
.dir
= p
->sm_path
;
872 prepare_submodule_repo_env(&cp_log
.env
);
873 strvec_pushl(&cp_log
.args
, "log", NULL
);
875 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
876 if (info
->summary_limit
> 0)
877 strvec_pushf(&cp_log
.args
, "-%d",
878 info
->summary_limit
);
880 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
881 "--first-parent", NULL
);
882 strvec_pushf(&cp_log
.args
, "%s...%s",
883 src_abbrev
, dst_abbrev
);
884 } else if (S_ISGITLINK(p
->mod_dst
)) {
885 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
886 "-1", dst_abbrev
, NULL
);
888 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
889 "-1", src_abbrev
, NULL
);
891 run_command(&cp_log
);
896 static void generate_submodule_summary(struct summary_cb
*info
,
899 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
900 int missing_src
= 0, missing_dst
= 0;
901 struct strbuf errmsg
= STRBUF_INIT
;
902 int total_commits
= -1;
904 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
905 if (S_ISGITLINK(p
->mod_dst
)) {
906 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
909 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
910 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
912 int fd
= open(p
->sm_path
, O_RDONLY
);
914 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
915 index_fd(the_repository
->index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
917 error(_("couldn't hash object from '%s'"), p
->sm_path
);
919 /* for a submodule removal (mode:0000000), don't warn */
921 warning(_("unexpected mode %o\n"), p
->mod_dst
);
925 if (S_ISGITLINK(p
->mod_src
)) {
926 if (p
->status
!= 'D')
927 src_abbrev
= verify_submodule_committish(p
->sm_path
,
928 oid_to_hex(&p
->oid_src
));
932 * As `rev-parse` failed, we fallback to getting
933 * the abbreviated hash using oid_src. We do
934 * this as we might still need the abbreviated
935 * hash in cases like a submodule type change, etc.
937 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
941 * The source does not point to a submodule.
942 * So, we fallback to getting the abbreviation using
943 * oid_src as we might still need the abbreviated
944 * hash in cases like submodule add, etc.
946 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
949 if (S_ISGITLINK(p
->mod_dst
)) {
950 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
951 oid_to_hex(&p
->oid_dst
));
955 * As `rev-parse` failed, we fallback to getting
956 * the abbreviated hash using oid_dst. We do
957 * this as we might still need the abbreviated
958 * hash in cases like a submodule type change, etc.
960 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
964 * The destination does not point to a submodule.
965 * So, we fallback to getting the abbreviation using
966 * oid_dst as we might still need the abbreviated
967 * hash in cases like a submodule removal, etc.
969 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
972 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
975 if (!missing_src
&& !missing_dst
) {
976 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
977 struct strbuf sb_rev_list
= STRBUF_INIT
;
979 strvec_pushl(&cp_rev_list
.args
, "rev-list",
980 "--first-parent", "--count", NULL
);
981 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
982 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
983 src_abbrev
, dst_abbrev
);
985 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
986 src_abbrev
: dst_abbrev
);
987 strvec_push(&cp_rev_list
.args
, "--");
989 cp_rev_list
.git_cmd
= 1;
990 cp_rev_list
.dir
= p
->sm_path
;
991 prepare_submodule_repo_env(&cp_rev_list
.env
);
993 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
994 total_commits
= atoi(sb_rev_list
.buf
);
996 strbuf_release(&sb_rev_list
);
999 * Don't give error msg for modification whose dst is not
1000 * submodule, i.e., deleted or changed to blob
1002 if (S_ISGITLINK(p
->mod_dst
)) {
1003 if (missing_src
&& missing_dst
) {
1004 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
1005 displaypath
, oid_to_hex(&p
->oid_src
),
1006 oid_to_hex(&p
->oid_dst
));
1008 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
1009 displaypath
, missing_src
?
1010 oid_to_hex(&p
->oid_src
) :
1011 oid_to_hex(&p
->oid_dst
));
1016 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1017 total_commits
, displaypath
, src_abbrev
,
1023 strbuf_release(&errmsg
);
1026 static void prepare_submodule_summary(struct summary_cb
*info
,
1027 struct module_cb_list
*list
)
1030 for (i
= 0; i
< list
->nr
; i
++) {
1031 const struct submodule
*sub
;
1032 struct module_cb
*p
= list
->entries
[i
];
1033 struct strbuf sm_gitdir
= STRBUF_INIT
;
1035 if (p
->status
== 'D' || p
->status
== 'T') {
1036 generate_submodule_summary(info
, p
);
1040 if (info
->for_status
&& p
->status
!= 'A' &&
1041 (sub
= submodule_from_path(the_repository
,
1042 null_oid(), p
->sm_path
))) {
1043 char *config_key
= NULL
;
1047 config_key
= xstrfmt("submodule.%s.ignore",
1049 if (!git_config_get_string_tmp(config_key
, &value
))
1050 ignore_all
= !strcmp(value
, "all");
1051 else if (sub
->ignore
)
1052 ignore_all
= !strcmp(sub
->ignore
, "all");
1059 /* Also show added or modified modules which are checked out */
1060 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1061 if (is_nonbare_repository_dir(&sm_gitdir
))
1062 generate_submodule_summary(info
, p
);
1063 strbuf_release(&sm_gitdir
);
1067 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1068 struct diff_options
*options UNUSED
,
1072 struct module_cb_list
*list
= data
;
1073 for (i
= 0; i
< q
->nr
; i
++) {
1074 struct diff_filepair
*p
= q
->queue
[i
];
1075 struct module_cb
*temp
;
1077 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1079 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1080 temp
->mod_src
= p
->one
->mode
;
1081 temp
->mod_dst
= p
->two
->mode
;
1082 temp
->oid_src
= p
->one
->oid
;
1083 temp
->oid_dst
= p
->two
->oid
;
1084 temp
->status
= p
->status
;
1085 temp
->sm_path
= xstrdup(p
->one
->path
);
1087 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1088 list
->entries
[list
->nr
++] = temp
;
1092 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1095 case DIFF_INDEX
: return "diff-index";
1096 case DIFF_FILES
: return "diff-files";
1097 default: BUG("bad diff_cmd value %d", diff_cmd
);
1101 static int compute_summary_module_list(struct object_id
*head_oid
,
1102 struct summary_cb
*info
,
1103 enum diff_cmd diff_cmd
)
1105 struct strvec diff_args
= STRVEC_INIT
;
1106 struct rev_info rev
;
1107 struct setup_revision_opt opt
= {
1108 .free_removed_argv_elements
= 1,
1110 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1113 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1115 strvec_push(&diff_args
, "--cached");
1116 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1118 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1119 strvec_push(&diff_args
, "--");
1121 strvec_pushv(&diff_args
, info
->argv
);
1123 git_config(git_diff_basic_config
, NULL
);
1124 repo_init_revisions(the_repository
, &rev
, info
->prefix
);
1126 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1127 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1128 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1129 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1130 rev
.diffopt
.format_callback_data
= &list
;
1132 if (!info
->cached
) {
1133 if (diff_cmd
== DIFF_INDEX
)
1135 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1136 perror("repo_read_index_preload");
1140 } else if (repo_read_index(the_repository
) < 0) {
1141 perror("repo_read_cache");
1146 if (diff_cmd
== DIFF_INDEX
)
1147 run_diff_index(&rev
, info
->cached
? DIFF_INDEX_CACHED
: 0);
1149 run_diff_files(&rev
, 0);
1150 prepare_submodule_summary(info
, &list
);
1152 strvec_clear(&diff_args
);
1153 release_revisions(&rev
);
1154 module_cb_list_release(&list
);
1158 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1160 struct summary_cb info
= SUMMARY_CB_INIT
;
1164 int summary_limit
= -1;
1165 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1166 struct object_id head_oid
;
1168 struct option module_summary_options
[] = {
1169 OPT_BOOL(0, "cached", &cached
,
1170 N_("use the commit stored in the index instead of the submodule HEAD")),
1171 OPT_BOOL(0, "files", &files
,
1172 N_("compare the commit in the index with that in the submodule HEAD")),
1173 OPT_BOOL(0, "for-status", &for_status
,
1174 N_("skip submodules with 'ignore_config' value set to 'all'")),
1175 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1176 N_("limit the summary size")),
1179 const char *const git_submodule_helper_usage
[] = {
1180 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1184 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1185 git_submodule_helper_usage
, 0);
1190 if (!repo_get_oid(the_repository
, argc
? argv
[0] : "HEAD", &head_oid
)) {
1195 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1196 /* before the first commit: compare with an empty tree */
1197 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1203 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
1204 die(_("could not fetch a revision for HEAD"));
1209 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1210 diff_cmd
= DIFF_FILES
;
1215 info
.prefix
= prefix
;
1216 info
.cached
= !!cached
;
1217 info
.files
= !!files
;
1218 info
.for_status
= !!for_status
;
1219 info
.summary_limit
= summary_limit
;
1221 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1228 const char *super_prefix
;
1231 #define SYNC_CB_INIT { 0 }
1233 static void sync_submodule(const char *path
, const char *prefix
,
1234 const char *super_prefix
, unsigned int flags
)
1236 const struct submodule
*sub
;
1237 char *remote_key
= NULL
;
1238 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1239 struct strbuf sb
= STRBUF_INIT
;
1240 char *sub_config_path
= NULL
;
1243 if (!is_submodule_active(the_repository
, path
))
1246 if (validate_submodule_path(path
) < 0)
1249 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1251 if (sub
&& sub
->url
) {
1252 if (starts_with_dot_dot_slash(sub
->url
) ||
1253 starts_with_dot_slash(sub
->url
)) {
1254 char *up_path
= get_up_path(path
);
1256 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1257 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1260 sub_origin_url
= xstrdup(sub
->url
);
1261 super_config_url
= xstrdup(sub
->url
);
1264 sub_origin_url
= xstrdup("");
1265 super_config_url
= xstrdup("");
1268 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1270 if (!(flags
& OPT_QUIET
))
1271 printf(_("Synchronizing submodule url for '%s'\n"),
1275 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1276 if (git_config_set_gently(sb
.buf
, super_config_url
))
1277 die(_("failed to register url for submodule path '%s'"),
1280 if (!is_submodule_populated_gently(path
, NULL
))
1284 code
= get_default_remote_submodule(path
, &default_remote
);
1288 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1289 free(default_remote
);
1291 submodule_to_gitdir(&sb
, path
);
1292 strbuf_addstr(&sb
, "/config");
1294 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, NULL
, sub_origin_url
))
1295 die(_("failed to update remote for submodule '%s'"),
1298 if (flags
& OPT_RECURSIVE
) {
1299 struct child_process cpr
= CHILD_PROCESS_INIT
;
1303 prepare_submodule_repo_env(&cpr
.env
);
1305 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1306 "--recursive", NULL
);
1307 strvec_push(&cpr
.args
, "--super-prefix");
1308 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1311 if (flags
& OPT_QUIET
)
1312 strvec_push(&cpr
.args
, "--quiet");
1314 if (run_command(&cpr
))
1315 die(_("failed to recurse into submodule '%s'"),
1320 free(super_config_url
);
1321 free(sub_origin_url
);
1322 strbuf_release(&sb
);
1325 free(sub_config_path
);
1328 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1330 struct sync_cb
*info
= cb_data
;
1332 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1336 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1338 struct sync_cb info
= SYNC_CB_INIT
;
1339 struct pathspec pathspec
= { 0 };
1340 struct module_list list
= MODULE_LIST_INIT
;
1343 struct option module_sync_options
[] = {
1344 OPT__SUPER_PREFIX(&info
.super_prefix
),
1345 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1346 OPT_BOOL(0, "recursive", &recursive
,
1347 N_("recurse into nested submodules")),
1350 const char *const git_submodule_helper_usage
[] = {
1351 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1356 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1357 git_submodule_helper_usage
, 0);
1359 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1362 info
.prefix
= prefix
;
1364 info
.flags
|= OPT_QUIET
;
1366 info
.flags
|= OPT_RECURSIVE
;
1368 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1372 module_list_release(&list
);
1373 clear_pathspec(&pathspec
);
1381 #define DEINIT_CB_INIT { 0 }
1383 static void deinit_submodule(const char *path
, const char *prefix
,
1386 const struct submodule
*sub
;
1387 char *displaypath
= NULL
;
1388 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1389 struct strbuf sb_config
= STRBUF_INIT
;
1390 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1392 if (validate_submodule_path(path
) < 0)
1395 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1397 if (!sub
|| !sub
->name
)
1400 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1402 /* remove the submodule work tree (unless the user already did it) */
1403 if (is_directory(path
)) {
1404 struct strbuf sb_rm
= STRBUF_INIT
;
1407 if (is_directory(sub_git_dir
)) {
1408 if (!(flags
& OPT_QUIET
))
1409 warning(_("Submodule work tree '%s' contains a .git "
1410 "directory. This will be replaced with a "
1411 ".git file by using absorbgitdirs."),
1414 absorb_git_dir_into_superproject(path
, NULL
);
1418 if (!(flags
& OPT_FORCE
)) {
1419 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1422 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1425 if (run_command(&cp_rm
))
1426 die(_("Submodule work tree '%s' contains local "
1427 "modifications; use '-f' to discard them"),
1431 strbuf_addstr(&sb_rm
, path
);
1433 if (!remove_dir_recursively(&sb_rm
, 0))
1434 format
= _("Cleared directory '%s'\n");
1436 format
= _("Could not remove submodule work tree '%s'\n");
1438 if (!(flags
& OPT_QUIET
))
1439 printf(format
, displaypath
);
1441 submodule_unset_core_worktree(sub
);
1443 strbuf_release(&sb_rm
);
1446 if (mkdir(path
, 0777))
1447 printf(_("could not create empty submodule directory %s"),
1450 cp_config
.git_cmd
= 1;
1451 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1452 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1454 /* remove the .git/config entries (unless the user already did it) */
1455 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1456 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1459 * remove the whole section so we have a clean state when
1460 * the user later decides to init this submodule again
1462 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1463 if (!(flags
& OPT_QUIET
))
1464 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1465 sub
->name
, sub
->url
, displaypath
);
1472 strbuf_release(&sb_config
);
1475 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1478 struct deinit_cb
*info
= cb_data
;
1479 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1482 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1484 struct deinit_cb info
= DEINIT_CB_INIT
;
1485 struct pathspec pathspec
= { 0 };
1486 struct module_list list
= MODULE_LIST_INIT
;
1490 struct option module_deinit_options
[] = {
1491 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1492 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1493 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1496 const char *const git_submodule_helper_usage
[] = {
1497 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1502 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1503 git_submodule_helper_usage
, 0);
1506 error("pathspec and --all are incompatible");
1507 usage_with_options(git_submodule_helper_usage
,
1508 module_deinit_options
);
1512 die(_("Use '--all' if you really want to deinitialize all submodules"));
1514 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1517 info
.prefix
= prefix
;
1519 info
.flags
|= OPT_QUIET
;
1521 info
.flags
|= OPT_FORCE
;
1523 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1527 module_list_release(&list
);
1528 clear_pathspec(&pathspec
);
1532 struct module_clone_data
{
1538 struct list_objects_filter_options
*filter_options
;
1539 unsigned int quiet
: 1;
1540 unsigned int progress
: 1;
1541 unsigned int dissociate
: 1;
1542 unsigned int require_init
: 1;
1545 #define MODULE_CLONE_DATA_INIT { \
1546 .single_branch = -1, \
1549 struct submodule_alternate_setup
{
1550 const char *submodule_name
;
1551 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1552 SUBMODULE_ALTERNATE_ERROR_DIE
,
1553 SUBMODULE_ALTERNATE_ERROR_INFO
,
1554 SUBMODULE_ALTERNATE_ERROR_IGNORE
1556 struct string_list
*reference
;
1558 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1559 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1562 static const char alternate_error_advice
[] = N_(
1563 "An alternate computed from a superproject's alternate is invalid.\n"
1564 "To allow Git to clone without an alternate in such a case, set\n"
1565 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1566 "'--reference-if-able' instead of '--reference'."
1569 static int add_possible_reference_from_superproject(
1570 struct object_directory
*odb
, void *sas_cb
)
1572 struct submodule_alternate_setup
*sas
= sas_cb
;
1576 * If the alternate object store is another repository, try the
1577 * standard layout with .git/(modules/<name>)+/objects
1579 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1580 struct repository alternate
;
1582 struct strbuf sb
= STRBUF_INIT
;
1583 struct strbuf err
= STRBUF_INIT
;
1584 strbuf_add(&sb
, odb
->path
, len
);
1586 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1587 die(_("could not get a repository handle for gitdir '%s'"),
1591 * We need to end the new path with '/' to mark it as a dir,
1592 * otherwise a submodule name containing '/' will be broken
1593 * as the last part of a missing submodule reference would
1594 * be taken as a file name.
1597 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1598 strbuf_addch(&sb
, '/');
1599 repo_clear(&alternate
);
1601 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1603 char *p
= strbuf_detach(&sb
, NULL
);
1605 string_list_append(sas
->reference
, p
)->util
= p
;
1608 switch (sas
->error_mode
) {
1609 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1610 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1611 advise(_(alternate_error_advice
));
1612 die(_("submodule '%s' cannot add alternate: %s"),
1613 sas
->submodule_name
, err
.buf
);
1614 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1615 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1616 sas
->submodule_name
, err
.buf
);
1617 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1621 strbuf_release(&sb
);
1627 static void prepare_possible_alternates(const char *sm_name
,
1628 struct string_list
*reference
)
1630 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1631 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1633 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1637 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1639 if (!error_strategy
)
1640 error_strategy
= xstrdup("die");
1642 sas
.submodule_name
= sm_name
;
1643 sas
.reference
= reference
;
1644 if (!strcmp(error_strategy
, "die"))
1645 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1646 else if (!strcmp(error_strategy
, "info"))
1647 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1648 else if (!strcmp(error_strategy
, "ignore"))
1649 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1651 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1653 if (!strcmp(sm_alternate
, "superproject"))
1654 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1655 else if (!strcmp(sm_alternate
, "no"))
1658 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1661 free(error_strategy
);
1664 static char *clone_submodule_sm_gitdir(const char *name
)
1666 struct strbuf sb
= STRBUF_INIT
;
1669 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1670 sm_gitdir
= absolute_pathdup(sb
.buf
);
1671 strbuf_release(&sb
);
1676 static int dir_contains_only_dotgit(const char *path
)
1678 DIR *dir
= opendir(path
);
1685 e
= readdir_skip_dot_and_dotdot(dir
);
1688 else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT
, e
->d_name
) ||
1689 (e
= readdir_skip_dot_and_dotdot(dir
))) {
1690 error("unexpected item '%s' in '%s'", e
->d_name
, path
);
1698 static int clone_submodule(const struct module_clone_data
*clone_data
,
1699 struct string_list
*reference
)
1702 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1703 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1705 struct child_process cp
= CHILD_PROCESS_INIT
;
1706 const char *clone_data_path
= clone_data
->path
;
1707 char *to_free
= NULL
;
1709 if (validate_submodule_path(clone_data_path
) < 0)
1712 if (!is_absolute_path(clone_data
->path
))
1713 clone_data_path
= to_free
= xstrfmt("%s/%s", get_git_work_tree(),
1716 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1717 die(_("refusing to create/use '%s' in another submodule's "
1718 "git dir"), sm_gitdir
);
1720 if (!file_exists(sm_gitdir
)) {
1721 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1722 !is_empty_dir(clone_data_path
))
1723 die(_("directory not empty: '%s'"), clone_data_path
);
1725 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1726 die(_("could not create directory '%s'"), sm_gitdir
);
1728 prepare_possible_alternates(clone_data
->name
, reference
);
1730 strvec_push(&cp
.args
, "clone");
1731 strvec_push(&cp
.args
, "--no-checkout");
1732 if (clone_data
->quiet
)
1733 strvec_push(&cp
.args
, "--quiet");
1734 if (clone_data
->progress
)
1735 strvec_push(&cp
.args
, "--progress");
1736 if (clone_data
->depth
&& *(clone_data
->depth
))
1737 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1738 if (reference
->nr
) {
1739 struct string_list_item
*item
;
1741 for_each_string_list_item(item
, reference
)
1742 strvec_pushl(&cp
.args
, "--reference",
1743 item
->string
, NULL
);
1745 if (clone_data
->dissociate
)
1746 strvec_push(&cp
.args
, "--dissociate");
1747 if (sm_gitdir
&& *sm_gitdir
)
1748 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1749 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1750 strvec_pushf(&cp
.args
, "--filter=%s",
1751 expand_list_objects_filter_spec(
1752 clone_data
->filter_options
));
1753 if (clone_data
->single_branch
>= 0)
1754 strvec_push(&cp
.args
, clone_data
->single_branch
?
1756 "--no-single-branch");
1758 strvec_push(&cp
.args
, "--");
1759 strvec_push(&cp
.args
, clone_data
->url
);
1760 strvec_push(&cp
.args
, clone_data_path
);
1763 prepare_submodule_repo_env(&cp
.env
);
1766 if(run_command(&cp
))
1767 die(_("clone of '%s' into submodule path '%s' failed"),
1768 clone_data
->url
, clone_data_path
);
1770 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1771 !dir_contains_only_dotgit(clone_data_path
)) {
1772 char *dot_git
= xstrfmt("%s/.git", clone_data_path
);
1775 die(_("directory not empty: '%s'"), clone_data_path
);
1780 if (clone_data
->require_init
&& !stat(clone_data_path
, &st
) &&
1781 !is_empty_dir(clone_data_path
))
1782 die(_("directory not empty: '%s'"), clone_data_path
);
1783 if (safe_create_leading_directories_const(clone_data_path
) < 0)
1784 die(_("could not create directory '%s'"), clone_data_path
);
1785 path
= xstrfmt("%s/index", sm_gitdir
);
1786 unlink_or_warn(path
);
1791 * We already performed this check at the beginning of this function,
1792 * before cloning the objects. This tries to detect racy behavior e.g.
1793 * in parallel clones, where another process could easily have made the
1794 * gitdir nested _after_ it was created.
1796 * To prevent further harm coming from this unintentionally-nested
1797 * gitdir, let's disable it by deleting the `HEAD` file.
1799 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0) {
1800 char *head
= xstrfmt("%s/HEAD", sm_gitdir
);
1803 die(_("refusing to create/use '%s' in another submodule's "
1804 "git dir"), sm_gitdir
);
1807 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1809 p
= git_pathdup_submodule(clone_data_path
, "config");
1811 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1813 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1814 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1816 git_config_set_in_file(p
, "submodule.alternateLocation",
1818 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1820 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1824 free(error_strategy
);
1832 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1834 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1835 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1836 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1837 struct list_objects_filter_options filter_options
=
1838 LIST_OBJECTS_FILTER_INIT
;
1840 struct option module_clone_options
[] = {
1841 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1843 N_("alternative anchor for relative paths")),
1844 OPT_STRING(0, "path", &clone_data
.path
,
1846 N_("where the new submodule will be cloned to")),
1847 OPT_STRING(0, "name", &clone_data
.name
,
1849 N_("name of the new submodule")),
1850 OPT_STRING(0, "url", &clone_data
.url
,
1852 N_("url where to clone the submodule from")),
1853 OPT_STRING_LIST(0, "reference", &reference
,
1855 N_("reference repository")),
1856 OPT_BOOL(0, "dissociate", &dissociate
,
1857 N_("use --reference only while cloning")),
1858 OPT_STRING(0, "depth", &clone_data
.depth
,
1860 N_("depth for shallow clones")),
1861 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1862 OPT_BOOL(0, "progress", &progress
,
1863 N_("force cloning progress")),
1864 OPT_BOOL(0, "require-init", &require_init
,
1865 N_("disallow cloning into non-empty directory")),
1866 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1867 N_("clone only one branch, HEAD or --branch")),
1868 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1871 const char *const git_submodule_helper_usage
[] = {
1872 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1873 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1874 "[--single-branch] [--filter <filter-spec>] "
1875 "--url <url> --path <path>"),
1879 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1880 git_submodule_helper_usage
, 0);
1882 clone_data
.dissociate
= !!dissociate
;
1883 clone_data
.quiet
= !!quiet
;
1884 clone_data
.progress
= !!progress
;
1885 clone_data
.require_init
= !!require_init
;
1886 clone_data
.filter_options
= &filter_options
;
1888 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1889 usage_with_options(git_submodule_helper_usage
,
1890 module_clone_options
);
1892 clone_submodule(&clone_data
, &reference
);
1893 list_objects_filter_release(&filter_options
);
1894 string_list_clear(&reference
, 1);
1898 static int determine_submodule_update_strategy(struct repository
*r
,
1901 enum submodule_update_type update
,
1902 struct submodule_update_strategy
*out
)
1904 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1909 key
= xstrfmt("submodule.%s.update", sub
->name
);
1913 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1914 if (parse_submodule_update_strategy(val
, out
) < 0) {
1915 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1919 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1920 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1921 BUG("how did we read update = !command from .gitmodules?");
1922 out
->type
= sub
->update_strategy
.type
;
1923 out
->command
= sub
->update_strategy
.command
;
1925 out
->type
= SM_UPDATE_CHECKOUT
;
1928 (out
->type
== SM_UPDATE_MERGE
||
1929 out
->type
== SM_UPDATE_REBASE
||
1930 out
->type
== SM_UPDATE_NONE
))
1931 out
->type
= SM_UPDATE_CHECKOUT
;
1939 struct update_clone_data
{
1940 const struct submodule
*sub
;
1941 struct object_id oid
;
1942 unsigned just_cloned
;
1945 struct submodule_update_clone
{
1946 /* index into 'update_data.list', the list of submodules to look into for cloning */
1949 /* configuration parameters which are passed on to the children */
1950 const struct update_data
*update_data
;
1952 /* to be consumed by update_submodule() */
1953 struct update_clone_data
*update_clone
;
1954 int update_clone_nr
; int update_clone_alloc
;
1956 /* If we want to stop as fast as possible and return an error */
1957 unsigned quickstop
: 1;
1959 /* failed clones to be retried again */
1960 const struct cache_entry
**failed_clones
;
1961 int failed_clones_nr
, failed_clones_alloc
;
1963 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1965 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1967 free(suc
->update_clone
);
1968 free(suc
->failed_clones
);
1971 struct update_data
{
1973 const char *super_prefix
;
1975 enum submodule_update_type update_default
;
1976 struct object_id suboid
;
1977 struct string_list references
;
1978 struct submodule_update_strategy update_strategy
;
1979 struct list_objects_filter_options
*filter_options
;
1980 struct module_list list
;
1984 int recommend_shallow
;
1985 unsigned int require_init
;
1988 unsigned int nofetch
;
1989 unsigned int remote
;
1990 unsigned int progress
;
1991 unsigned int dissociate
;
1993 unsigned int warn_if_uninitialized
;
1994 unsigned int recursive
;
1996 /* copied over from update_clone_data */
1997 struct object_id oid
;
1998 unsigned int just_cloned
;
1999 const char *sm_path
;
2001 #define UPDATE_DATA_INIT { \
2002 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2003 .list = MODULE_LIST_INIT, \
2004 .recommend_shallow = -1, \
2005 .references = STRING_LIST_INIT_DUP, \
2006 .single_branch = -1, \
2010 static void update_data_release(struct update_data
*ud
)
2012 free(ud
->displaypath
);
2013 module_list_release(&ud
->list
);
2016 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
2017 struct strbuf
*out
, const char *displaypath
)
2020 * Only mention uninitialized submodules when their
2021 * paths have been specified.
2023 if (suc
->update_data
->warn_if_uninitialized
) {
2025 _("Submodule path '%s' not initialized"),
2027 strbuf_addch(out
, '\n');
2029 _("Maybe you want to use 'update --init'?"));
2030 strbuf_addch(out
, '\n');
2035 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2036 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2038 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
2039 struct child_process
*child
,
2040 struct submodule_update_clone
*suc
,
2043 const struct submodule
*sub
= NULL
;
2044 const char *url
= NULL
;
2045 const char *update_string
;
2046 enum submodule_update_type update_type
;
2048 const struct update_data
*ud
= suc
->update_data
;
2049 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
2051 struct strbuf sb
= STRBUF_INIT
;
2052 int needs_cloning
= 0;
2053 int need_free_url
= 0;
2056 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
2057 strbuf_addch(out
, '\n');
2061 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
2064 next_submodule_warn_missing(suc
, out
, displaypath
);
2068 key
= xstrfmt("submodule.%s.update", sub
->name
);
2069 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2070 update_type
= parse_submodule_update_type(update_string
);
2072 update_type
= sub
->update_strategy
.type
;
2076 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2077 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2078 && update_type
== SM_UPDATE_NONE
)) {
2079 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2080 strbuf_addch(out
, '\n');
2084 /* Check if the submodule has been initialized. */
2085 if (!is_submodule_active(the_repository
, ce
->name
)) {
2086 next_submodule_warn_missing(suc
, out
, displaypath
);
2091 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2092 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2093 if (sub
->url
&& (starts_with_dot_slash(sub
->url
) ||
2094 starts_with_dot_dot_slash(sub
->url
))) {
2095 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2102 die(_("cannot clone submodule '%s' without a URL"), sub
->name
);
2105 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2106 needs_cloning
= !file_exists(sb
.buf
);
2108 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2109 suc
->update_clone_alloc
);
2110 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2111 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2112 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2113 suc
->update_clone_nr
++;
2119 child
->no_stdin
= 1;
2120 child
->stdout_to_stderr
= 1;
2122 strvec_push(&child
->args
, "submodule--helper");
2123 strvec_push(&child
->args
, "clone");
2124 if (suc
->update_data
->progress
)
2125 strvec_push(&child
->args
, "--progress");
2126 if (suc
->update_data
->quiet
)
2127 strvec_push(&child
->args
, "--quiet");
2128 if (suc
->update_data
->prefix
)
2129 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2130 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2131 strvec_push(&child
->args
, "--depth=1");
2132 else if (suc
->update_data
->depth
)
2133 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2134 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2135 strvec_pushf(&child
->args
, "--filter=%s",
2136 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2137 if (suc
->update_data
->require_init
)
2138 strvec_push(&child
->args
, "--require-init");
2139 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2140 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2141 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2142 if (suc
->update_data
->references
.nr
) {
2143 struct string_list_item
*item
;
2145 for_each_string_list_item(item
, &suc
->update_data
->references
)
2146 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2148 if (suc
->update_data
->dissociate
)
2149 strvec_push(&child
->args
, "--dissociate");
2150 if (suc
->update_data
->single_branch
>= 0)
2151 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2153 "--no-single-branch");
2157 strbuf_release(&sb
);
2161 return needs_cloning
;
2164 static int update_clone_get_next_task(struct child_process
*child
,
2169 struct submodule_update_clone
*suc
= suc_cb
;
2170 const struct cache_entry
*ce
;
2173 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2174 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2175 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2176 int *p
= xmalloc(sizeof(*p
));
2186 * The loop above tried cloning each submodule once, now try the
2187 * stragglers again, which we can imagine as an extension of the
2190 index
= suc
->current
- suc
->update_data
->list
.nr
;
2191 if (index
< suc
->failed_clones_nr
) {
2194 ce
= suc
->failed_clones
[index
];
2195 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2197 strbuf_addstr(err
, "BUG: submodule considered for "
2198 "cloning, doesn't need cloning "
2202 p
= xmalloc(sizeof(*p
));
2212 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2214 void *idx_task_cb UNUSED
)
2216 struct submodule_update_clone
*suc
= suc_cb
;
2222 static int update_clone_task_finished(int result
,
2227 const struct cache_entry
*ce
;
2228 struct submodule_update_clone
*suc
= suc_cb
;
2229 int *idxP
= idx_task_cb
;
2237 if (idx
< suc
->update_data
->list
.nr
) {
2238 ce
= suc
->update_data
->list
.entries
[idx
];
2239 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2241 strbuf_addch(err
, '\n');
2242 ALLOC_GROW(suc
->failed_clones
,
2243 suc
->failed_clones_nr
+ 1,
2244 suc
->failed_clones_alloc
);
2245 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2248 idx
-= suc
->update_data
->list
.nr
;
2249 ce
= suc
->failed_clones
[idx
];
2250 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2252 strbuf_addch(err
, '\n');
2260 static int git_update_clone_config(const char *var
, const char *value
,
2261 const struct config_context
*ctx
,
2266 if (!strcmp(var
, "submodule.fetchjobs"))
2267 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
2271 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2273 struct child_process cp
= CHILD_PROCESS_INIT
;
2274 struct strbuf rev
= STRBUF_INIT
;
2275 char *hex
= oid_to_hex(oid
);
2280 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2282 prepare_submodule_repo_env(&cp
.env
);
2284 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2290 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2291 const struct object_id
*oid
)
2293 struct child_process cp
= CHILD_PROCESS_INIT
;
2295 prepare_submodule_repo_env(&cp
.env
);
2297 cp
.dir
= module_path
;
2299 strvec_push(&cp
.args
, "fetch");
2301 strvec_push(&cp
.args
, "--quiet");
2303 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2305 char *hex
= oid_to_hex(oid
);
2306 char *remote
= get_default_remote();
2308 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2312 return run_command(&cp
);
2315 static int run_update_command(const struct update_data
*ud
, int subforce
)
2317 struct child_process cp
= CHILD_PROCESS_INIT
;
2318 char *oid
= oid_to_hex(&ud
->oid
);
2321 switch (ud
->update_strategy
.type
) {
2322 case SM_UPDATE_CHECKOUT
:
2324 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2326 strvec_push(&cp
.args
, "-f");
2328 case SM_UPDATE_REBASE
:
2330 strvec_push(&cp
.args
, "rebase");
2332 strvec_push(&cp
.args
, "--quiet");
2334 case SM_UPDATE_MERGE
:
2336 strvec_push(&cp
.args
, "merge");
2338 strvec_push(&cp
.args
, "--quiet");
2340 case SM_UPDATE_COMMAND
:
2342 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2345 BUG("unexpected update strategy type: %d",
2346 ud
->update_strategy
.type
);
2348 strvec_push(&cp
.args
, oid
);
2350 cp
.dir
= ud
->sm_path
;
2351 prepare_submodule_repo_env(&cp
.env
);
2352 if ((ret
= run_command(&cp
))) {
2353 switch (ud
->update_strategy
.type
) {
2354 case SM_UPDATE_CHECKOUT
:
2355 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2356 oid
, ud
->displaypath
);
2357 /* No "ret" assignment, use "git checkout"'s */
2359 case SM_UPDATE_REBASE
:
2360 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2361 oid
, ud
->displaypath
);
2363 case SM_UPDATE_MERGE
:
2364 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2365 oid
, ud
->displaypath
);
2367 case SM_UPDATE_COMMAND
:
2368 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2369 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2372 BUG("unexpected update strategy type: %d",
2373 ud
->update_strategy
.type
);
2382 switch (ud
->update_strategy
.type
) {
2383 case SM_UPDATE_CHECKOUT
:
2384 printf(_("Submodule path '%s': checked out '%s'\n"),
2385 ud
->displaypath
, oid
);
2387 case SM_UPDATE_REBASE
:
2388 printf(_("Submodule path '%s': rebased into '%s'\n"),
2389 ud
->displaypath
, oid
);
2391 case SM_UPDATE_MERGE
:
2392 printf(_("Submodule path '%s': merged in '%s'\n"),
2393 ud
->displaypath
, oid
);
2395 case SM_UPDATE_COMMAND
:
2396 printf(_("Submodule path '%s': '%s %s'\n"),
2397 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2400 BUG("unexpected update strategy type: %d",
2401 ud
->update_strategy
.type
);
2407 static int run_update_procedure(const struct update_data
*ud
)
2409 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2413 * Run fetch only if `oid` isn't present or it
2414 * is not reachable from a ref.
2416 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2417 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2420 _("Unable to fetch in submodule path '%s'; "
2421 "trying to directly fetch %s:"),
2422 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2424 * Now we tried the usual fetch, but `oid` may
2425 * not be reachable from any of the refs.
2427 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2428 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2429 return die_message(_("Fetched in submodule path '%s', but it did not "
2430 "contain %s. Direct fetching of that commit failed."),
2431 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2434 return run_update_command(ud
, subforce
);
2437 static int remote_submodule_branch(const char *path
, const char **branch
)
2439 const struct submodule
*sub
;
2443 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2445 return die_message(_("could not initialize submodule at path '%s'"),
2448 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2449 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2450 *branch
= sub
->branch
;
2458 if (!strcmp(*branch
, ".")) {
2459 const char *refname
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
2464 return die_message(_("No such ref: %s"), "HEAD");
2467 if (!strcmp(refname
, "HEAD"))
2468 return die_message(_("Submodule (%s) branch configured to inherit "
2469 "branch from superproject, but the superproject "
2470 "is not on any branch"), sub
->name
);
2472 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2473 return die_message(_("Expecting a full ref name, got %s"),
2480 /* Our "branch" is coming from repo_config_get_string_tmp() */
2484 static int ensure_core_worktree(const char *path
)
2487 struct repository subrepo
;
2489 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2490 return die_message(_("could not get a repository handle for submodule '%s'"),
2493 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2494 char *cfg_file
, *abs_path
;
2495 const char *rel_path
;
2496 struct strbuf sb
= STRBUF_INIT
;
2498 cfg_file
= repo_git_path(&subrepo
, "config");
2500 abs_path
= absolute_pathdup(path
);
2501 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2503 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2507 strbuf_release(&sb
);
2510 repo_clear(&subrepo
);
2514 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2517 case SM_UPDATE_CHECKOUT
:
2519 case SM_UPDATE_MERGE
:
2521 case SM_UPDATE_REBASE
:
2523 case SM_UPDATE_UNSPECIFIED
:
2524 case SM_UPDATE_NONE
:
2525 case SM_UPDATE_COMMAND
:
2528 BUG("unreachable with type %d", type
);
2531 static void update_data_to_args(const struct update_data
*update_data
,
2532 struct strvec
*args
)
2534 enum submodule_update_type update_type
= update_data
->update_default
;
2536 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2537 if (update_data
->displaypath
) {
2538 strvec_push(args
, "--super-prefix");
2539 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2541 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2542 if (update_data
->quiet
)
2543 strvec_push(args
, "--quiet");
2544 if (update_data
->force
)
2545 strvec_push(args
, "--force");
2546 if (update_data
->init
)
2547 strvec_push(args
, "--init");
2548 if (update_data
->remote
)
2549 strvec_push(args
, "--remote");
2550 if (update_data
->nofetch
)
2551 strvec_push(args
, "--no-fetch");
2552 if (update_data
->dissociate
)
2553 strvec_push(args
, "--dissociate");
2554 if (update_data
->progress
)
2555 strvec_push(args
, "--progress");
2556 if (update_data
->require_init
)
2557 strvec_push(args
, "--require-init");
2558 if (update_data
->depth
)
2559 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2560 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2561 strvec_pushf(args
, "--%s",
2562 submodule_update_type_to_label(update_type
));
2564 if (update_data
->references
.nr
) {
2565 struct string_list_item
*item
;
2567 for_each_string_list_item(item
, &update_data
->references
)
2568 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2570 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2571 strvec_pushf(args
, "--filter=%s",
2572 expand_list_objects_filter_spec(
2573 update_data
->filter_options
));
2574 if (update_data
->recommend_shallow
== 0)
2575 strvec_push(args
, "--no-recommend-shallow");
2576 else if (update_data
->recommend_shallow
== 1)
2577 strvec_push(args
, "--recommend-shallow");
2578 if (update_data
->single_branch
>= 0)
2579 strvec_push(args
, update_data
->single_branch
?
2581 "--no-single-branch");
2584 static int update_submodule(struct update_data
*update_data
)
2588 if (validate_submodule_path(update_data
->sm_path
) < 0)
2591 ret
= determine_submodule_update_strategy(the_repository
,
2592 update_data
->just_cloned
,
2593 update_data
->sm_path
,
2594 update_data
->update_default
,
2595 &update_data
->update_strategy
);
2599 if (update_data
->just_cloned
)
2600 oidcpy(&update_data
->suboid
, null_oid());
2601 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2602 return die_message(_("Unable to find current revision in submodule path '%s'"),
2603 update_data
->displaypath
);
2605 if (update_data
->remote
) {
2611 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2614 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2617 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2621 if (!update_data
->nofetch
) {
2622 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2624 return die_message(_("Unable to fetch in submodule path '%s'"),
2625 update_data
->sm_path
);
2628 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2629 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2630 remote_ref
, update_data
->sm_path
);
2635 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2636 ret
= run_update_procedure(update_data
);
2641 if (update_data
->recursive
) {
2642 struct child_process cp
= CHILD_PROCESS_INIT
;
2643 struct update_data next
= *update_data
;
2646 oidcpy(&next
.oid
, null_oid());
2647 oidcpy(&next
.suboid
, null_oid());
2649 cp
.dir
= update_data
->sm_path
;
2651 prepare_submodule_repo_env(&cp
.env
);
2652 update_data_to_args(&next
, &cp
.args
);
2654 ret
= run_command(&cp
);
2656 die_message(_("Failed to recurse into submodule path '%s'"),
2657 update_data
->displaypath
);
2664 static int update_submodules(struct update_data
*update_data
)
2667 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2668 const struct run_process_parallel_opts opts
= {
2669 .tr2_category
= "submodule",
2670 .tr2_label
= "parallel/update",
2672 .processes
= update_data
->max_jobs
,
2674 .get_next_task
= update_clone_get_next_task
,
2675 .start_failure
= update_clone_start_failure
,
2676 .task_finished
= update_clone_task_finished
,
2680 suc
.update_data
= update_data
;
2681 run_processes_parallel(&opts
);
2684 * We saved the output and put it out all at once now.
2686 * - the listener does not have to interleave their (checkout)
2687 * work with our fetching. The writes involved in a
2688 * checkout involve more straightforward sequential I/O.
2689 * - the listener can avoid doing any work if fetching failed.
2691 if (suc
.quickstop
) {
2696 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2697 struct update_clone_data ucd
= suc
.update_clone
[i
];
2700 oidcpy(&update_data
->oid
, &ucd
.oid
);
2701 update_data
->just_cloned
= ucd
.just_cloned
;
2702 update_data
->sm_path
= ucd
.sub
->path
;
2705 * Verify that the submodule path does not contain any
2706 * symlinks; if it does, it might have been tampered with.
2707 * TODO: allow exempting it via
2708 * `safe.submodule.path` or something
2710 if (validate_submodule_path(update_data
->sm_path
) < 0)
2713 code
= ensure_core_worktree(update_data
->sm_path
);
2717 update_data
->displaypath
= get_submodule_displaypath(
2718 update_data
->sm_path
, update_data
->prefix
,
2719 update_data
->super_prefix
);
2720 code
= update_submodule(update_data
);
2721 FREE_AND_NULL(update_data
->displaypath
);
2731 submodule_update_clone_release(&suc
);
2732 string_list_clear(&update_data
->references
, 0);
2736 static int module_update(int argc
, const char **argv
, const char *prefix
)
2738 struct pathspec pathspec
= { 0 };
2739 struct pathspec pathspec2
= { 0 };
2740 struct update_data opt
= UPDATE_DATA_INIT
;
2741 struct list_objects_filter_options filter_options
=
2742 LIST_OBJECTS_FILTER_INIT
;
2744 struct option module_update_options
[] = {
2745 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2746 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2747 OPT_BOOL(0, "init", &opt
.init
,
2748 N_("initialize uninitialized submodules before update")),
2749 OPT_BOOL(0, "remote", &opt
.remote
,
2750 N_("use SHA-1 of submodule's remote tracking branch")),
2751 OPT_BOOL(0, "recursive", &opt
.recursive
,
2752 N_("traverse submodules recursively")),
2753 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2754 N_("don't fetch new objects from the remote site")),
2755 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2756 N_("use the 'checkout' update strategy (default)"),
2757 SM_UPDATE_CHECKOUT
),
2758 OPT_SET_INT('m', "merge", &opt
.update_default
,
2759 N_("use the 'merge' update strategy"),
2761 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2762 N_("use the 'rebase' update strategy"),
2764 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2765 N_("reference repository")),
2766 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2767 N_("use --reference only while cloning")),
2768 OPT_INTEGER(0, "depth", &opt
.depth
,
2769 N_("create a shallow clone truncated to the "
2770 "specified number of revisions")),
2771 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2772 N_("parallel jobs")),
2773 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2774 N_("whether the initial clone should follow the shallow recommendation")),
2775 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2776 OPT_BOOL(0, "progress", &opt
.progress
,
2777 N_("force cloning progress")),
2778 OPT_BOOL(0, "require-init", &opt
.require_init
,
2779 N_("disallow cloning into non-empty directory, implies --init")),
2780 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2781 N_("clone only one branch, HEAD or --branch")),
2782 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2785 const char *const git_submodule_helper_usage
[] = {
2786 N_("git submodule [--quiet] update"
2787 " [--init [--filter=<filter-spec>]] [--remote]"
2788 " [-N|--no-fetch] [-f|--force]"
2789 " [--checkout|--merge|--rebase]"
2790 " [--[no-]recommend-shallow] [--reference <repository>]"
2791 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2795 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2796 git_config(git_update_clone_config
, &opt
.max_jobs
);
2798 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2799 git_submodule_helper_usage
, 0);
2801 if (opt
.require_init
)
2804 if (filter_options
.choice
&& !opt
.init
) {
2805 usage_with_options(git_submodule_helper_usage
,
2806 module_update_options
);
2809 opt
.filter_options
= &filter_options
;
2810 opt
.prefix
= prefix
;
2812 if (opt
.update_default
)
2813 opt
.update_strategy
.type
= opt
.update_default
;
2815 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2821 opt
.warn_if_uninitialized
= 1;
2824 struct module_list list
= MODULE_LIST_INIT
;
2825 struct init_cb info
= INIT_CB_INIT
;
2827 if (module_list_compute(argv
, opt
.prefix
,
2828 &pathspec2
, &list
) < 0) {
2829 module_list_release(&list
);
2835 * If there are no path args and submodule.active is set then,
2836 * by default, only initialize 'active' modules.
2838 if (!argc
&& !git_config_get("submodule.active"))
2839 module_list_active(&list
);
2841 info
.prefix
= opt
.prefix
;
2842 info
.super_prefix
= opt
.super_prefix
;
2844 info
.flags
|= OPT_QUIET
;
2846 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2847 module_list_release(&list
);
2850 ret
= update_submodules(&opt
);
2852 update_data_release(&opt
);
2853 list_objects_filter_release(&filter_options
);
2854 clear_pathspec(&pathspec
);
2855 clear_pathspec(&pathspec2
);
2859 static int push_check(int argc
, const char **argv
, const char *prefix UNUSED
)
2861 struct remote
*remote
;
2862 const char *superproject_head
;
2864 int detached_head
= 0;
2865 struct object_id head_oid
;
2868 die("submodule--helper push-check requires at least 2 arguments");
2871 * superproject's resolved head ref.
2872 * if HEAD then the superproject is in a detached head state, otherwise
2873 * it will be the resolved head ref.
2875 superproject_head
= argv
[1];
2878 /* Get the submodule's head ref and determine if it is detached */
2879 head
= refs_resolve_refdup(get_main_ref_store(the_repository
), "HEAD",
2880 0, &head_oid
, NULL
);
2882 die(_("Failed to resolve HEAD as a valid ref."));
2883 if (!strcmp(head
, "HEAD"))
2887 * The remote must be configured.
2888 * This is to avoid pushing to the exact same URL as the parent.
2890 remote
= pushremote_get(argv
[1]);
2891 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2892 die("remote '%s' not configured", argv
[1]);
2894 /* Check the refspec */
2897 struct ref
*local_refs
= get_local_heads();
2898 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2900 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2902 for (i
= 0; i
< refspec
.nr
; i
++) {
2903 const struct refspec_item
*rs
= &refspec
.items
[i
];
2905 if (rs
->pattern
|| rs
->matching
)
2908 /* LHS must match a single ref */
2909 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2914 * If LHS matches 'HEAD' then we need to ensure
2915 * that it matches the same named branch
2916 * checked out in the superproject.
2918 if (!strcmp(rs
->src
, "HEAD")) {
2919 if (!detached_head
&&
2920 !strcmp(head
, superproject_head
))
2922 die("HEAD does not match the named branch in the superproject");
2926 die("src refspec '%s' must name a ref",
2930 refspec_clear(&refspec
);
2937 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2940 struct pathspec pathspec
= { 0 };
2941 struct module_list list
= MODULE_LIST_INIT
;
2942 const char *super_prefix
= NULL
;
2943 struct option embed_gitdir_options
[] = {
2944 OPT__SUPER_PREFIX(&super_prefix
),
2947 const char *const git_submodule_helper_usage
[] = {
2948 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2953 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2954 git_submodule_helper_usage
, 0);
2956 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
2959 for (i
= 0; i
< list
.nr
; i
++)
2960 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
2965 clear_pathspec(&pathspec
);
2966 module_list_release(&list
);
2970 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2976 struct option options
[] = {
2977 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2980 const char *const usage
[] = {
2981 N_("git submodule set-url [--quiet] <path> <newurl>"),
2984 const struct submodule
*sub
;
2986 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2988 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2989 usage_with_options(usage
, options
);
2991 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2994 die(_("no submodule mapping found in .gitmodules for path '%s'"),
2997 config_name
= xstrfmt("submodule.%s.url", sub
->name
);
2998 ret
= config_set_in_gitmodules_file_gently(config_name
, newurl
);
3001 repo_read_gitmodules(the_repository
, 0);
3002 sync_submodule(sub
->path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
3009 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
3011 int opt_default
= 0, ret
;
3012 const char *opt_branch
= NULL
;
3015 struct option options
[] = {
3017 * We accept the `quiet` option for uniformity across subcommands,
3018 * though there is nothing to make less verbose in this subcommand.
3020 OPT_NOOP_NOARG('q', "quiet"),
3022 OPT_BOOL('d', "default", &opt_default
,
3023 N_("set the default tracking branch to master")),
3024 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
3025 N_("set the default tracking branch")),
3028 const char *const usage
[] = {
3029 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3030 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3033 const struct submodule
*sub
;
3035 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3037 if (!opt_branch
&& !opt_default
)
3038 die(_("--branch or --default required"));
3040 if (opt_branch
&& opt_default
)
3041 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3043 if (argc
!= 1 || !(path
= argv
[0]))
3044 usage_with_options(usage
, options
);
3046 sub
= submodule_from_path(the_repository
, null_oid(), path
);
3049 die(_("no submodule mapping found in .gitmodules for path '%s'"),
3052 config_name
= xstrfmt("submodule.%s.branch", sub
->name
);
3053 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
3059 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
3061 enum branch_track track
;
3062 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
3063 struct option options
[] = {
3064 OPT__QUIET(&quiet
, N_("print only error messages")),
3065 OPT__FORCE(&force
, N_("force creation"), 0),
3066 OPT_BOOL(0, "create-reflog", &reflog
,
3067 N_("create the branch's reflog")),
3068 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
3069 N_("set branch tracking configuration"),
3071 parse_opt_tracking_mode
),
3072 OPT__DRY_RUN(&dry_run
,
3073 N_("show whether the branch would be created")),
3076 const char *const usage
[] = {
3077 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3081 git_config(git_default_config
, NULL
);
3082 track
= git_branch_track
;
3083 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3086 usage_with_options(usage
, options
);
3088 if (!quiet
&& !dry_run
)
3089 printf_ln(_("creating branch '%s'"), argv
[0]);
3091 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
3092 force
, reflog
, quiet
, track
, dry_run
);
3099 const char *reference_path
;
3101 const char *sm_name
;
3103 const char *realrepo
;
3105 unsigned int force
: 1;
3106 unsigned int quiet
: 1;
3107 unsigned int progress
: 1;
3108 unsigned int dissociate
: 1;
3110 #define ADD_DATA_INIT { .depth = -1 }
3112 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3114 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3115 struct strbuf sb_remote_out
= STRBUF_INIT
;
3117 cp_remote
.git_cmd
= 1;
3118 strvec_pushf(&cp_remote
.env
,
3119 "GIT_DIR=%s", git_dir_path
);
3120 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3121 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3122 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3124 char *line
= sb_remote_out
.buf
;
3126 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3127 size_t len
= next_line
- line
;
3129 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3130 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3131 line
= next_line
+ 1;
3135 strbuf_release(&sb_remote_out
);
3138 static int add_submodule(const struct add_data
*add_data
)
3140 char *submod_gitdir_path
;
3141 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3142 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3145 /* perhaps the path already exists and is already a git repo, else clone it */
3146 if (is_directory(add_data
->sm_path
)) {
3147 struct strbuf sm_path
= STRBUF_INIT
;
3148 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3149 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3150 if (is_nonbare_repository_dir(&sm_path
))
3151 printf(_("Adding existing repo at '%s' to the index\n"),
3154 die(_("'%s' already exists and is not a valid git repo"),
3156 strbuf_release(&sm_path
);
3157 free(submod_gitdir_path
);
3159 struct child_process cp
= CHILD_PROCESS_INIT
;
3161 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3163 if (is_directory(submod_gitdir_path
)) {
3164 if (!add_data
->force
) {
3165 struct strbuf msg
= STRBUF_INIT
;
3168 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3169 "locally with remote(s):\n"),
3172 append_fetch_remotes(&msg
, submod_gitdir_path
);
3173 free(submod_gitdir_path
);
3175 strbuf_addf(&msg
, _("If you want to reuse this local git "
3176 "directory instead of cloning again from\n"
3178 "use the '--force' option. If the local git "
3179 "directory is not the correct repo\n"
3180 "or you are unsure what this means choose "
3181 "another name with the '--name' option."),
3182 add_data
->realrepo
);
3184 die_msg
= strbuf_detach(&msg
, NULL
);
3187 printf(_("Reactivating local git directory for "
3188 "submodule '%s'\n"), add_data
->sm_name
);
3191 free(submod_gitdir_path
);
3193 clone_data
.prefix
= add_data
->prefix
;
3194 clone_data
.path
= add_data
->sm_path
;
3195 clone_data
.name
= add_data
->sm_name
;
3196 clone_data
.url
= add_data
->realrepo
;
3197 clone_data
.quiet
= add_data
->quiet
;
3198 clone_data
.progress
= add_data
->progress
;
3199 if (add_data
->reference_path
) {
3200 char *p
= xstrdup(add_data
->reference_path
);
3202 string_list_append(&reference
, p
)->util
= p
;
3204 clone_data
.dissociate
= add_data
->dissociate
;
3205 if (add_data
->depth
>= 0)
3206 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3208 if (clone_submodule(&clone_data
, &reference
))
3211 prepare_submodule_repo_env(&cp
.env
);
3213 cp
.dir
= add_data
->sm_path
;
3215 * NOTE: we only get here if add_data->force is true, so
3216 * passing --force to checkout is reasonable.
3218 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3220 if (add_data
->branch
) {
3221 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3222 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3225 if (run_command(&cp
))
3226 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3230 string_list_clear(&reference
, 1);
3234 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3239 if (!is_writing_gitmodules_ok())
3240 die(_("please make sure that the .gitmodules file is in the working tree"));
3242 key
= xstrfmt("submodule.%s.%s", name
, var
);
3243 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3249 static void configure_added_submodule(struct add_data
*add_data
)
3252 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3253 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3255 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3256 git_config_set_gently(key
, add_data
->realrepo
);
3259 add_submod
.git_cmd
= 1;
3260 strvec_pushl(&add_submod
.args
, "add",
3261 "--no-warn-embedded-repo", NULL
);
3262 if (add_data
->force
)
3263 strvec_push(&add_submod
.args
, "--force");
3264 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3266 if (run_command(&add_submod
))
3267 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3269 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3270 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3271 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3273 if (add_data
->branch
) {
3274 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3275 "branch", add_data
->branch
))
3276 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3279 add_gitmodules
.git_cmd
= 1;
3280 strvec_pushl(&add_gitmodules
.args
,
3281 "add", "--force", "--", ".gitmodules", NULL
);
3283 if (run_command(&add_gitmodules
))
3284 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3287 * NEEDSWORK: In a multi-working-tree world this needs to be
3288 * set in the per-worktree config.
3291 * NEEDSWORK: In the longer run, we need to get rid of this
3292 * pattern of querying "submodule.active" before calling
3293 * is_submodule_active(), since that function needs to find
3294 * out the value of "submodule.active" again anyway.
3296 if (!git_config_get("submodule.active")) {
3298 * If the submodule being added isn't already covered by the
3299 * current configured pathspec, set the submodule's active flag
3301 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3302 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3303 git_config_set_gently(key
, "true");
3307 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3308 git_config_set_gently(key
, "true");
3313 static void die_on_index_match(const char *path
, int force
)
3316 const char *args
[] = { path
, NULL
};
3317 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3319 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3320 die(_("index file corrupt"));
3324 char *ps_matched
= xcalloc(ps
.nr
, 1);
3326 /* TODO: audit for interaction with sparse-index. */
3327 ensure_full_index(the_repository
->index
);
3330 * Since there is only one pathspec, we just need to
3331 * check ps_matched[0] to know if a cache entry matched.
3333 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
3334 ce_path_match(the_repository
->index
, the_repository
->index
->cache
[i
], &ps
,
3337 if (ps_matched
[0]) {
3339 die(_("'%s' already exists in the index"),
3341 if (!S_ISGITLINK(the_repository
->index
->cache
[i
]->ce_mode
))
3342 die(_("'%s' already exists in the index "
3343 "and is not a submodule"), path
);
3349 clear_pathspec(&ps
);
3352 static void die_on_repo_without_commits(const char *path
)
3354 struct strbuf sb
= STRBUF_INIT
;
3355 strbuf_addstr(&sb
, path
);
3356 if (is_nonbare_repository_dir(&sb
)) {
3357 struct object_id oid
;
3358 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3359 die(_("'%s' does not have a commit checked out"), path
);
3361 strbuf_release(&sb
);
3364 static int module_add(int argc
, const char **argv
, const char *prefix
)
3366 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3367 struct add_data add_data
= ADD_DATA_INIT
;
3368 char *to_free
= NULL
;
3369 struct option options
[] = {
3370 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3371 N_("branch of repository to add as submodule")),
3372 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3373 PARSE_OPT_NOCOMPLETE
),
3374 OPT__QUIET(&quiet
, N_("print only error messages")),
3375 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3376 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3377 N_("reference repository")),
3378 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3379 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3380 N_("sets the submodule's name to the given string "
3381 "instead of defaulting to its path")),
3382 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3385 const char *const usage
[] = {
3386 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3389 struct strbuf sb
= STRBUF_INIT
;
3392 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3394 if (!is_writing_gitmodules_ok())
3395 die(_("please make sure that the .gitmodules file is in the working tree"));
3397 if (prefix
&& *prefix
&&
3398 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3399 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3401 if (argc
== 0 || argc
> 2)
3402 usage_with_options(usage
, options
);
3404 add_data
.repo
= argv
[0];
3406 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3408 add_data
.sm_path
= xstrdup(argv
[1]);
3410 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3411 char *sm_path
= add_data
.sm_path
;
3413 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3417 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3418 starts_with_dot_slash(add_data
.repo
)) {
3420 die(_("Relative path can only be used from the toplevel "
3421 "of the working tree"));
3423 /* dereference source url relative to parent's url */
3424 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3425 add_data
.realrepo
= to_free
;
3426 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3427 add_data
.realrepo
= add_data
.repo
;
3429 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3435 * multiple //; leading ./; /./; /../;
3437 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3438 strip_dir_trailing_slashes(add_data
.sm_path
);
3440 if (validate_submodule_path(add_data
.sm_path
) < 0)
3443 die_on_index_match(add_data
.sm_path
, force
);
3444 die_on_repo_without_commits(add_data
.sm_path
);
3447 struct child_process cp
= CHILD_PROCESS_INIT
;
3451 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3452 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3453 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3454 strbuf_complete_line(&sb
);
3455 fputs(sb
.buf
, stderr
);
3460 if(!add_data
.sm_name
)
3461 add_data
.sm_name
= add_data
.sm_path
;
3463 if (check_submodule_name(add_data
.sm_name
))
3464 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3466 add_data
.prefix
= prefix
;
3467 add_data
.force
= !!force
;
3468 add_data
.quiet
= !!quiet
;
3469 add_data
.progress
= !!progress
;
3470 add_data
.dissociate
= !!dissociate
;
3472 if (add_submodule(&add_data
))
3474 configure_added_submodule(&add_data
);
3478 free(add_data
.sm_path
);
3480 strbuf_release(&sb
);
3485 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3487 parse_opt_subcommand_fn
*fn
= NULL
;
3488 const char *const usage
[] = {
3489 N_("git submodule--helper <command>"),
3492 struct option options
[] = {
3493 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3494 OPT_SUBCOMMAND("add", &fn
, module_add
),
3495 OPT_SUBCOMMAND("update", &fn
, module_update
),
3496 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3497 OPT_SUBCOMMAND("init", &fn
, module_init
),
3498 OPT_SUBCOMMAND("status", &fn
, module_status
),
3499 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3500 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3501 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3502 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3503 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3504 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3505 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3506 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3509 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3511 return fn(argc
, argv
, prefix
);