1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
3 #include "repository.h"
6 #include "parse-options.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
21 #include "object-store.h"
24 #include "list-objects-filter-options.h"
26 #define OPT_QUIET (1 << 0)
27 #define OPT_CACHED (1 << 1)
28 #define OPT_RECURSIVE (1 << 2)
29 #define OPT_FORCE (1 << 3)
31 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
34 static char *repo_get_default_remote(struct repository
*repo
)
36 char *dest
= NULL
, *ret
;
37 struct strbuf sb
= STRBUF_INIT
;
38 struct ref_store
*store
= get_main_ref_store(repo
);
39 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
43 die(_("No such ref: %s"), "HEAD");
46 if (!strcmp(refname
, "HEAD"))
47 return xstrdup("origin");
49 if (!skip_prefix(refname
, "refs/heads/", &refname
))
50 die(_("Expecting a full ref name, got %s"), refname
);
52 strbuf_addf(&sb
, "branch.%s.remote", refname
);
53 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
54 ret
= xstrdup("origin");
62 static char *get_default_remote_submodule(const char *module_path
)
64 struct repository subrepo
;
66 repo_submodule_init(&subrepo
, the_repository
, module_path
, null_oid());
67 return repo_get_default_remote(&subrepo
);
70 static char *get_default_remote(void)
72 return repo_get_default_remote(the_repository
);
75 static int starts_with_dot_slash(const char *str
)
77 return str
[0] == '.' && is_dir_sep(str
[1]);
80 static int starts_with_dot_dot_slash(const char *str
)
82 return str
[0] == '.' && str
[1] == '.' && is_dir_sep(str
[2]);
86 * Returns 1 if it was the last chop before ':'.
88 static int chop_last_dir(char **remoteurl
, int is_relative
)
90 char *rfind
= find_last_dir_sep(*remoteurl
);
96 rfind
= strrchr(*remoteurl
, ':');
102 if (is_relative
|| !strcmp(".", *remoteurl
))
103 die(_("cannot strip one component off url '%s'"),
107 *remoteurl
= xstrdup(".");
112 * The `url` argument is the URL that navigates to the submodule origin
113 * repo. When relative, this URL is relative to the superproject origin
114 * URL repo. The `up_path` argument, if specified, is the relative
115 * path that navigates from the submodule working tree to the superproject
116 * working tree. Returns the origin URL of the submodule.
118 * Return either an absolute URL or filesystem path (if the superproject
119 * origin URL is an absolute URL or filesystem path, respectively) or a
120 * relative file system path (if the superproject origin URL is a relative
123 * When the output is a relative file system path, the path is either
124 * relative to the submodule working tree, if up_path is specified, or to
125 * the superproject working tree otherwise.
127 * NEEDSWORK: This works incorrectly on the domain and protocol part.
128 * remote_url url outcome expectation
129 * http://a.com/b ../c http://a.com/c as is
130 * http://a.com/b/ ../c http://a.com/c same as previous line, but
131 * ignore trailing slash in url
132 * http://a.com/b ../../c http://c error out
133 * http://a.com/b ../../../c http:/c error out
134 * http://a.com/b ../../../../c http:c error out
135 * http://a.com/b ../../../../../c .:c error out
136 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
137 * when a local part has a colon in its path component, too.
139 static char *relative_url(const char *remote_url
,
146 char *remoteurl
= xstrdup(remote_url
);
147 struct strbuf sb
= STRBUF_INIT
;
148 size_t len
= strlen(remoteurl
);
150 if (is_dir_sep(remoteurl
[len
-1]))
151 remoteurl
[len
-1] = '\0';
153 if (!url_is_local_not_ssh(remoteurl
) || is_absolute_path(remoteurl
))
158 * Prepend a './' to ensure all relative
159 * remoteurls start with './' or '../'
161 if (!starts_with_dot_slash(remoteurl
) &&
162 !starts_with_dot_dot_slash(remoteurl
)) {
164 strbuf_addf(&sb
, "./%s", remoteurl
);
166 remoteurl
= strbuf_detach(&sb
, NULL
);
170 * When the url starts with '../', remove that and the
171 * last directory in remoteurl.
174 if (starts_with_dot_dot_slash(url
)) {
176 colonsep
|= chop_last_dir(&remoteurl
, is_relative
);
177 } else if (starts_with_dot_slash(url
))
183 strbuf_addf(&sb
, "%s%s%s", remoteurl
, colonsep
? ":" : "/", url
);
184 if (ends_with(url
, "/"))
185 strbuf_setlen(&sb
, sb
.len
- 1);
188 if (starts_with_dot_slash(sb
.buf
))
189 out
= xstrdup(sb
.buf
+ 2);
191 out
= xstrdup(sb
.buf
);
193 if (!up_path
|| !is_relative
) {
199 strbuf_addf(&sb
, "%s%s", up_path
, out
);
201 return strbuf_detach(&sb
, NULL
);
204 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
206 char *remoteurl
, *resolved_url
;
207 char *remote
= get_default_remote();
208 struct strbuf remotesb
= STRBUF_INIT
;
210 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
211 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
213 warning(_("could not look up configuration '%s'. "
214 "Assuming this repository is its own "
215 "authoritative upstream."),
217 remoteurl
= xgetcwd();
219 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
223 strbuf_release(&remotesb
);
228 static int resolve_relative_url_test(int argc
, const char **argv
, const char *prefix
)
230 char *remoteurl
, *res
;
231 const char *up_path
, *url
;
234 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
237 remoteurl
= xstrdup(argv
[2]);
240 if (!strcmp(up_path
, "(null)"))
243 res
= relative_url(remoteurl
, url
, up_path
);
250 static char *do_get_submodule_displaypath(const char *path
,
252 const char *super_prefix
)
254 if (prefix
&& super_prefix
) {
255 BUG("cannot have prefix '%s' and superprefix '%s'",
256 prefix
, super_prefix
);
258 struct strbuf sb
= STRBUF_INIT
;
259 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
262 } else if (super_prefix
) {
263 return xstrfmt("%s%s", super_prefix
, path
);
265 return xstrdup(path
);
269 /* the result should be freed by the caller. */
270 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
272 const char *super_prefix
= get_super_prefix();
273 return do_get_submodule_displaypath(path
, prefix
, super_prefix
);
276 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
278 struct strbuf sb
= STRBUF_INIT
;
281 static const char *describe_bare
[] = { NULL
};
283 static const char *describe_tags
[] = { "--tags", NULL
};
285 static const char *describe_contains
[] = { "--contains", NULL
};
287 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
289 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
291 describe_all_always
, NULL
};
293 for (d
= describe_argv
; *d
; d
++) {
294 struct child_process cp
= CHILD_PROCESS_INIT
;
295 prepare_submodule_repo_env(&cp
.env_array
);
300 strvec_push(&cp
.args
, "describe");
301 strvec_pushv(&cp
.args
, *d
);
302 strvec_push(&cp
.args
, object_id
);
304 if (!capture_command(&cp
, &sb
, 0)) {
305 strbuf_strip_suffix(&sb
, "\n");
306 return strbuf_detach(&sb
, NULL
);
315 const struct cache_entry
**entries
;
318 #define MODULE_LIST_INIT { 0 }
320 static int module_list_compute(int argc
, const char **argv
,
322 struct pathspec
*pathspec
,
323 struct module_list
*list
)
326 char *ps_matched
= NULL
;
327 parse_pathspec(pathspec
, 0,
328 PATHSPEC_PREFER_FULL
,
332 ps_matched
= xcalloc(pathspec
->nr
, 1);
334 if (read_cache() < 0)
335 die(_("index file corrupt"));
337 for (i
= 0; i
< active_nr
; i
++) {
338 const struct cache_entry
*ce
= active_cache
[i
];
340 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
342 !S_ISGITLINK(ce
->ce_mode
))
345 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
346 list
->entries
[list
->nr
++] = ce
;
347 while (i
+ 1 < active_nr
&&
348 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
350 * Skip entries with the same name in different stages
351 * to make sure an entry is returned only once.
356 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
364 static void module_list_active(struct module_list
*list
)
367 struct module_list active_modules
= MODULE_LIST_INIT
;
369 for (i
= 0; i
< list
->nr
; i
++) {
370 const struct cache_entry
*ce
= list
->entries
[i
];
372 if (!is_submodule_active(the_repository
, ce
->name
))
375 ALLOC_GROW(active_modules
.entries
,
376 active_modules
.nr
+ 1,
377 active_modules
.alloc
);
378 active_modules
.entries
[active_modules
.nr
++] = ce
;
382 *list
= active_modules
;
385 static char *get_up_path(const char *path
)
388 struct strbuf sb
= STRBUF_INIT
;
390 for (i
= count_slashes(path
); i
; i
--)
391 strbuf_addstr(&sb
, "../");
394 * Check if 'path' ends with slash or not
395 * for having the same output for dir/sub_dir
398 if (!is_dir_sep(path
[strlen(path
) - 1]))
399 strbuf_addstr(&sb
, "../");
401 return strbuf_detach(&sb
, NULL
);
404 static int module_list(int argc
, const char **argv
, const char *prefix
)
407 struct pathspec pathspec
;
408 struct module_list list
= MODULE_LIST_INIT
;
410 struct option module_list_options
[] = {
411 OPT_STRING(0, "prefix", &prefix
,
413 N_("alternative anchor for relative paths")),
417 const char *const git_submodule_helper_usage
[] = {
418 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
422 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
423 git_submodule_helper_usage
, 0);
425 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
428 for (i
= 0; i
< list
.nr
; i
++) {
429 const struct cache_entry
*ce
= list
.entries
[i
];
432 printf("%06o %s U\t", ce
->ce_mode
,
433 oid_to_hex(null_oid()));
435 printf("%06o %s %d\t", ce
->ce_mode
,
436 oid_to_hex(&ce
->oid
), ce_stage(ce
));
438 fprintf(stdout
, "%s\n", ce
->name
);
443 static void for_each_listed_submodule(const struct module_list
*list
,
444 each_submodule_fn fn
, void *cb_data
)
447 for (i
= 0; i
< list
->nr
; i
++)
448 fn(list
->entries
[i
], cb_data
);
458 #define FOREACH_CB_INIT { 0 }
460 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
463 struct foreach_cb
*info
= cb_data
;
464 const char *path
= list_item
->name
;
465 const struct object_id
*ce_oid
= &list_item
->oid
;
467 const struct submodule
*sub
;
468 struct child_process cp
= CHILD_PROCESS_INIT
;
471 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
473 sub
= submodule_from_path(the_repository
, null_oid(), path
);
476 die(_("No url found for submodule path '%s' in .gitmodules"),
479 if (!is_submodule_populated_gently(path
, NULL
))
482 prepare_submodule_repo_env(&cp
.env_array
);
485 * For the purpose of executing <command> in the submodule,
486 * separate shell is used for the purpose of running the
493 * NEEDSWORK: the command currently has access to the variables $name,
494 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
495 * contains a single argument. This is done for maintaining a faithful
496 * translation from shell script.
498 if (info
->argc
== 1) {
499 char *toplevel
= xgetcwd();
500 struct strbuf sb
= STRBUF_INIT
;
502 strvec_pushf(&cp
.env_array
, "name=%s", sub
->name
);
503 strvec_pushf(&cp
.env_array
, "sm_path=%s", path
);
504 strvec_pushf(&cp
.env_array
, "displaypath=%s", displaypath
);
505 strvec_pushf(&cp
.env_array
, "sha1=%s",
507 strvec_pushf(&cp
.env_array
, "toplevel=%s", toplevel
);
510 * Since the path variable was accessible from the script
511 * before porting, it is also made available after porting.
512 * The environment variable "PATH" has a very special purpose
513 * on windows. And since environment variables are
514 * case-insensitive in windows, it interferes with the
515 * existing PATH variable. Hence, to avoid that, we expose
516 * path via the args strvec and not via env_array.
518 sq_quote_buf(&sb
, path
);
519 strvec_pushf(&cp
.args
, "path=%s; %s",
520 sb
.buf
, info
->argv
[0]);
524 strvec_pushv(&cp
.args
, info
->argv
);
528 printf(_("Entering '%s'\n"), displaypath
);
530 if (info
->argv
[0] && run_command(&cp
))
531 die(_("run_command returned non-zero status for %s\n."),
534 if (info
->recursive
) {
535 struct child_process cpr
= CHILD_PROCESS_INIT
;
539 prepare_submodule_repo_env(&cpr
.env_array
);
541 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
542 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
543 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
547 strvec_push(&cpr
.args
, "--quiet");
549 strvec_push(&cpr
.args
, "--");
550 strvec_pushv(&cpr
.args
, info
->argv
);
552 if (run_command(&cpr
))
553 die(_("run_command returned non-zero status while "
554 "recursing in the nested submodules of %s\n."),
562 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
564 struct foreach_cb info
= FOREACH_CB_INIT
;
565 struct pathspec pathspec
;
566 struct module_list list
= MODULE_LIST_INIT
;
568 struct option module_foreach_options
[] = {
569 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
570 OPT_BOOL(0, "recursive", &info
.recursive
,
571 N_("recurse into nested submodules")),
575 const char *const git_submodule_helper_usage
[] = {
576 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
580 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
581 git_submodule_helper_usage
, 0);
583 if (module_list_compute(0, NULL
, prefix
, &pathspec
, &list
) < 0)
588 info
.prefix
= prefix
;
590 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
597 const char *superprefix
;
600 #define INIT_CB_INIT { 0 }
602 static void init_submodule(const char *path
, const char *prefix
,
603 const char *superprefix
, unsigned int flags
)
605 const struct submodule
*sub
;
606 struct strbuf sb
= STRBUF_INIT
;
607 char *upd
= NULL
, *url
= NULL
, *displaypath
;
609 /* try superprefix from the environment, if it is not passed explicitly */
611 superprefix
= get_super_prefix();
612 displaypath
= do_get_submodule_displaypath(path
, prefix
, superprefix
);
614 sub
= submodule_from_path(the_repository
, null_oid(), path
);
617 die(_("No url found for submodule path '%s' in .gitmodules"),
621 * NEEDSWORK: In a multi-working-tree world, this needs to be
622 * set in the per-worktree config.
624 * Set active flag for the submodule being initialized
626 if (!is_submodule_active(the_repository
, path
)) {
627 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
628 git_config_set_gently(sb
.buf
, "true");
633 * Copy url setting when it is not set yet.
634 * To look up the url in .git/config, we must not fall back to
635 * .gitmodules, so look it up directly.
637 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
638 if (git_config_get_string(sb
.buf
, &url
)) {
640 die(_("No url found for submodule path '%s' in .gitmodules"),
643 url
= xstrdup(sub
->url
);
645 /* Possibly a url relative to parent */
646 if (starts_with_dot_dot_slash(url
) ||
647 starts_with_dot_slash(url
)) {
649 url
= resolve_relative_url(oldurl
, NULL
, 0);
653 if (git_config_set_gently(sb
.buf
, url
))
654 die(_("Failed to register url for submodule path '%s'"),
656 if (!(flags
& OPT_QUIET
))
658 _("Submodule '%s' (%s) registered for path '%s'\n"),
659 sub
->name
, url
, displaypath
);
663 /* Copy "update" setting when it is not set yet */
664 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
665 if (git_config_get_string(sb
.buf
, &upd
) &&
666 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
667 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
668 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
670 upd
= xstrdup("none");
672 upd
= xstrdup(submodule_strategy_to_string(&sub
->update_strategy
));
674 if (git_config_set_gently(sb
.buf
, upd
))
675 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
683 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
685 struct init_cb
*info
= cb_data
;
686 init_submodule(list_item
->name
, info
->prefix
, info
->superprefix
, info
->flags
);
689 static int module_init(int argc
, const char **argv
, const char *prefix
)
691 struct init_cb info
= INIT_CB_INIT
;
692 struct pathspec pathspec
;
693 struct module_list list
= MODULE_LIST_INIT
;
696 struct option module_init_options
[] = {
697 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
701 const char *const git_submodule_helper_usage
[] = {
702 N_("git submodule--helper init [<options>] [<path>]"),
706 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
707 git_submodule_helper_usage
, 0);
709 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
713 * If there are no path args and submodule.active is set then,
714 * by default, only initialize 'active' modules.
716 if (!argc
&& git_config_get_value_multi("submodule.active"))
717 module_list_active(&list
);
719 info
.prefix
= prefix
;
721 info
.flags
|= OPT_QUIET
;
723 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
732 #define STATUS_CB_INIT { 0 }
734 static void print_status(unsigned int flags
, char state
, const char *path
,
735 const struct object_id
*oid
, const char *displaypath
)
737 if (flags
& OPT_QUIET
)
740 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
742 if (state
== ' ' || state
== '+') {
743 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
746 printf(" (%s)", name
);
752 static int handle_submodule_head_ref(const char *refname
,
753 const struct object_id
*oid
, int flags
,
756 struct object_id
*output
= cb_data
;
763 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
764 unsigned int ce_flags
, const char *prefix
,
768 struct strvec diff_files_args
= STRVEC_INIT
;
770 int diff_files_result
;
771 struct strbuf buf
= STRBUF_INIT
;
774 if (!submodule_from_path(the_repository
, null_oid(), path
))
775 die(_("no submodule mapping found in .gitmodules for path '%s'"),
778 displaypath
= get_submodule_displaypath(path
, prefix
);
780 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
781 print_status(flags
, 'U', path
, null_oid(), displaypath
);
785 strbuf_addf(&buf
, "%s/.git", path
);
786 git_dir
= read_gitfile(buf
.buf
);
790 if (!is_submodule_active(the_repository
, path
) ||
791 !is_git_directory(git_dir
)) {
792 print_status(flags
, '-', path
, ce_oid
, displaypath
);
793 strbuf_release(&buf
);
796 strbuf_release(&buf
);
798 strvec_pushl(&diff_files_args
, "diff-files",
799 "--ignore-submodules=dirty", "--quiet", "--",
802 git_config(git_diff_basic_config
, NULL
);
804 repo_init_revisions(the_repository
, &rev
, NULL
);
806 diff_files_args
.nr
= setup_revisions(diff_files_args
.nr
,
809 diff_files_result
= run_diff_files(&rev
, 0);
811 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
812 print_status(flags
, ' ', path
, ce_oid
,
814 } else if (!(flags
& OPT_CACHED
)) {
815 struct object_id oid
;
816 struct ref_store
*refs
= get_submodule_ref_store(path
);
819 print_status(flags
, '-', path
, ce_oid
, displaypath
);
822 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
823 die(_("could not resolve HEAD ref inside the "
824 "submodule '%s'"), path
);
826 print_status(flags
, '+', path
, &oid
, displaypath
);
828 print_status(flags
, '+', path
, ce_oid
, displaypath
);
831 if (flags
& OPT_RECURSIVE
) {
832 struct child_process cpr
= CHILD_PROCESS_INIT
;
836 prepare_submodule_repo_env(&cpr
.env_array
);
838 strvec_push(&cpr
.args
, "--super-prefix");
839 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
840 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
841 "--recursive", NULL
);
843 if (flags
& OPT_CACHED
)
844 strvec_push(&cpr
.args
, "--cached");
846 if (flags
& OPT_QUIET
)
847 strvec_push(&cpr
.args
, "--quiet");
849 if (run_command(&cpr
))
850 die(_("failed to recurse into submodule '%s'"), path
);
854 strvec_clear(&diff_files_args
);
858 static void status_submodule_cb(const struct cache_entry
*list_item
,
861 struct status_cb
*info
= cb_data
;
862 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
863 info
->prefix
, info
->flags
);
866 static int module_status(int argc
, const char **argv
, const char *prefix
)
868 struct status_cb info
= STATUS_CB_INIT
;
869 struct pathspec pathspec
;
870 struct module_list list
= MODULE_LIST_INIT
;
873 struct option module_status_options
[] = {
874 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
875 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
876 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
880 const char *const git_submodule_helper_usage
[] = {
881 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
885 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
886 git_submodule_helper_usage
, 0);
888 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
891 info
.prefix
= prefix
;
893 info
.flags
|= OPT_QUIET
;
895 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
900 static int module_name(int argc
, const char **argv
, const char *prefix
)
902 const struct submodule
*sub
;
905 usage(_("git submodule--helper name <path>"));
907 sub
= submodule_from_path(the_repository
, null_oid(), argv
[1]);
910 die(_("no submodule mapping found in .gitmodules for path '%s'"),
913 printf("%s\n", sub
->name
);
919 unsigned int mod_src
;
920 unsigned int mod_dst
;
921 struct object_id oid_src
;
922 struct object_id oid_dst
;
926 #define MODULE_CB_INIT { 0 }
928 struct module_cb_list
{
929 struct module_cb
**entries
;
932 #define MODULE_CB_LIST_INIT { 0 }
938 unsigned int cached
: 1;
939 unsigned int for_status
: 1;
940 unsigned int files
: 1;
943 #define SUMMARY_CB_INIT { 0 }
950 static char *verify_submodule_committish(const char *sm_path
,
951 const char *committish
)
953 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
954 struct strbuf result
= STRBUF_INIT
;
956 cp_rev_parse
.git_cmd
= 1;
957 cp_rev_parse
.dir
= sm_path
;
958 prepare_submodule_repo_env(&cp_rev_parse
.env_array
);
959 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
960 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
961 strvec_push(&cp_rev_parse
.args
, "--");
963 if (capture_command(&cp_rev_parse
, &result
, 0))
966 strbuf_trim_trailing_newline(&result
);
967 return strbuf_detach(&result
, NULL
);
970 static void print_submodule_summary(struct summary_cb
*info
, char *errmsg
,
971 int total_commits
, const char *displaypath
,
972 const char *src_abbrev
, const char *dst_abbrev
,
975 if (p
->status
== 'T') {
976 if (S_ISGITLINK(p
->mod_dst
))
977 printf(_("* %s %s(blob)->%s(submodule)"),
978 displaypath
, src_abbrev
, dst_abbrev
);
980 printf(_("* %s %s(submodule)->%s(blob)"),
981 displaypath
, src_abbrev
, dst_abbrev
);
983 printf("* %s %s...%s",
984 displaypath
, src_abbrev
, dst_abbrev
);
987 if (total_commits
< 0)
990 printf(" (%d):\n", total_commits
);
993 printf(_("%s"), errmsg
);
994 } else if (total_commits
> 0) {
995 struct child_process cp_log
= CHILD_PROCESS_INIT
;
998 cp_log
.dir
= p
->sm_path
;
999 prepare_submodule_repo_env(&cp_log
.env_array
);
1000 strvec_pushl(&cp_log
.args
, "log", NULL
);
1002 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
1003 if (info
->summary_limit
> 0)
1004 strvec_pushf(&cp_log
.args
, "-%d",
1005 info
->summary_limit
);
1007 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
1008 "--first-parent", NULL
);
1009 strvec_pushf(&cp_log
.args
, "%s...%s",
1010 src_abbrev
, dst_abbrev
);
1011 } else if (S_ISGITLINK(p
->mod_dst
)) {
1012 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
1013 "-1", dst_abbrev
, NULL
);
1015 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
1016 "-1", src_abbrev
, NULL
);
1018 run_command(&cp_log
);
1023 static void generate_submodule_summary(struct summary_cb
*info
,
1024 struct module_cb
*p
)
1026 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
1027 int missing_src
= 0, missing_dst
= 0;
1028 char *errmsg
= NULL
;
1029 int total_commits
= -1;
1031 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
1032 if (S_ISGITLINK(p
->mod_dst
)) {
1033 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
1035 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
1036 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
1038 int fd
= open(p
->sm_path
, O_RDONLY
);
1040 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
1041 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
1043 error(_("couldn't hash object from '%s'"), p
->sm_path
);
1045 /* for a submodule removal (mode:0000000), don't warn */
1047 warning(_("unexpected mode %o\n"), p
->mod_dst
);
1051 if (S_ISGITLINK(p
->mod_src
)) {
1052 if (p
->status
!= 'D')
1053 src_abbrev
= verify_submodule_committish(p
->sm_path
,
1054 oid_to_hex(&p
->oid_src
));
1058 * As `rev-parse` failed, we fallback to getting
1059 * the abbreviated hash using oid_src. We do
1060 * this as we might still need the abbreviated
1061 * hash in cases like a submodule type change, etc.
1063 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
1067 * The source does not point to a submodule.
1068 * So, we fallback to getting the abbreviation using
1069 * oid_src as we might still need the abbreviated
1070 * hash in cases like submodule add, etc.
1072 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
1075 if (S_ISGITLINK(p
->mod_dst
)) {
1076 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
1077 oid_to_hex(&p
->oid_dst
));
1081 * As `rev-parse` failed, we fallback to getting
1082 * the abbreviated hash using oid_dst. We do
1083 * this as we might still need the abbreviated
1084 * hash in cases like a submodule type change, etc.
1086 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
1090 * The destination does not point to a submodule.
1091 * So, we fallback to getting the abbreviation using
1092 * oid_dst as we might still need the abbreviated
1093 * hash in cases like a submodule removal, etc.
1095 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
1098 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
);
1100 if (!missing_src
&& !missing_dst
) {
1101 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
1102 struct strbuf sb_rev_list
= STRBUF_INIT
;
1104 strvec_pushl(&cp_rev_list
.args
, "rev-list",
1105 "--first-parent", "--count", NULL
);
1106 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
1107 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
1108 src_abbrev
, dst_abbrev
);
1110 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
1111 src_abbrev
: dst_abbrev
);
1112 strvec_push(&cp_rev_list
.args
, "--");
1114 cp_rev_list
.git_cmd
= 1;
1115 cp_rev_list
.dir
= p
->sm_path
;
1116 prepare_submodule_repo_env(&cp_rev_list
.env_array
);
1118 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
1119 total_commits
= atoi(sb_rev_list
.buf
);
1121 strbuf_release(&sb_rev_list
);
1124 * Don't give error msg for modification whose dst is not
1125 * submodule, i.e., deleted or changed to blob
1127 if (S_ISGITLINK(p
->mod_dst
)) {
1128 struct strbuf errmsg_str
= STRBUF_INIT
;
1129 if (missing_src
&& missing_dst
) {
1130 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commits %s and %s\n",
1131 displaypath
, oid_to_hex(&p
->oid_src
),
1132 oid_to_hex(&p
->oid_dst
));
1134 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commit %s\n",
1135 displaypath
, missing_src
?
1136 oid_to_hex(&p
->oid_src
) :
1137 oid_to_hex(&p
->oid_dst
));
1139 errmsg
= strbuf_detach(&errmsg_str
, NULL
);
1143 print_submodule_summary(info
, errmsg
, total_commits
,
1144 displaypath
, src_abbrev
,
1152 static void prepare_submodule_summary(struct summary_cb
*info
,
1153 struct module_cb_list
*list
)
1156 for (i
= 0; i
< list
->nr
; i
++) {
1157 const struct submodule
*sub
;
1158 struct module_cb
*p
= list
->entries
[i
];
1159 struct strbuf sm_gitdir
= STRBUF_INIT
;
1161 if (p
->status
== 'D' || p
->status
== 'T') {
1162 generate_submodule_summary(info
, p
);
1166 if (info
->for_status
&& p
->status
!= 'A' &&
1167 (sub
= submodule_from_path(the_repository
,
1168 null_oid(), p
->sm_path
))) {
1169 char *config_key
= NULL
;
1173 config_key
= xstrfmt("submodule.%s.ignore",
1175 if (!git_config_get_string_tmp(config_key
, &value
))
1176 ignore_all
= !strcmp(value
, "all");
1177 else if (sub
->ignore
)
1178 ignore_all
= !strcmp(sub
->ignore
, "all");
1185 /* Also show added or modified modules which are checked out */
1186 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1187 if (is_nonbare_repository_dir(&sm_gitdir
))
1188 generate_submodule_summary(info
, p
);
1189 strbuf_release(&sm_gitdir
);
1193 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1194 struct diff_options
*options
,
1198 struct module_cb_list
*list
= data
;
1199 for (i
= 0; i
< q
->nr
; i
++) {
1200 struct diff_filepair
*p
= q
->queue
[i
];
1201 struct module_cb
*temp
;
1203 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1205 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1206 temp
->mod_src
= p
->one
->mode
;
1207 temp
->mod_dst
= p
->two
->mode
;
1208 temp
->oid_src
= p
->one
->oid
;
1209 temp
->oid_dst
= p
->two
->oid
;
1210 temp
->status
= p
->status
;
1211 temp
->sm_path
= xstrdup(p
->one
->path
);
1213 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1214 list
->entries
[list
->nr
++] = temp
;
1218 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1221 case DIFF_INDEX
: return "diff-index";
1222 case DIFF_FILES
: return "diff-files";
1223 default: BUG("bad diff_cmd value %d", diff_cmd
);
1227 static int compute_summary_module_list(struct object_id
*head_oid
,
1228 struct summary_cb
*info
,
1229 enum diff_cmd diff_cmd
)
1231 struct strvec diff_args
= STRVEC_INIT
;
1232 struct rev_info rev
;
1233 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1235 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1237 strvec_push(&diff_args
, "--cached");
1238 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1240 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1241 strvec_push(&diff_args
, "--");
1243 strvec_pushv(&diff_args
, info
->argv
);
1245 git_config(git_diff_basic_config
, NULL
);
1246 init_revisions(&rev
, info
->prefix
);
1248 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1249 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, NULL
);
1250 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1251 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1252 rev
.diffopt
.format_callback_data
= &list
;
1254 if (!info
->cached
) {
1255 if (diff_cmd
== DIFF_INDEX
)
1257 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1258 perror("read_cache_preload");
1261 } else if (read_cache() < 0) {
1262 perror("read_cache");
1266 if (diff_cmd
== DIFF_INDEX
)
1267 run_diff_index(&rev
, info
->cached
);
1269 run_diff_files(&rev
, 0);
1270 prepare_submodule_summary(info
, &list
);
1271 strvec_clear(&diff_args
);
1275 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1277 struct summary_cb info
= SUMMARY_CB_INIT
;
1281 int summary_limit
= -1;
1282 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1283 struct object_id head_oid
;
1286 struct option module_summary_options
[] = {
1287 OPT_BOOL(0, "cached", &cached
,
1288 N_("use the commit stored in the index instead of the submodule HEAD")),
1289 OPT_BOOL(0, "files", &files
,
1290 N_("compare the commit in the index with that in the submodule HEAD")),
1291 OPT_BOOL(0, "for-status", &for_status
,
1292 N_("skip submodules with 'ignore_config' value set to 'all'")),
1293 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1294 N_("limit the summary size")),
1298 const char *const git_submodule_helper_usage
[] = {
1299 N_("git submodule--helper summary [<options>] [<commit>] [--] [<path>]"),
1303 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1304 git_submodule_helper_usage
, 0);
1309 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1314 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1315 /* before the first commit: compare with an empty tree */
1316 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1322 if (get_oid("HEAD", &head_oid
))
1323 die(_("could not fetch a revision for HEAD"));
1328 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1329 diff_cmd
= DIFF_FILES
;
1334 info
.prefix
= prefix
;
1335 info
.cached
= !!cached
;
1336 info
.files
= !!files
;
1337 info
.for_status
= !!for_status
;
1338 info
.summary_limit
= summary_limit
;
1340 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1349 #define SYNC_CB_INIT { 0 }
1351 static void sync_submodule(const char *path
, const char *prefix
,
1354 const struct submodule
*sub
;
1355 char *remote_key
= NULL
;
1356 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1357 struct strbuf sb
= STRBUF_INIT
;
1358 char *sub_config_path
= NULL
;
1360 if (!is_submodule_active(the_repository
, path
))
1363 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1365 if (sub
&& sub
->url
) {
1366 if (starts_with_dot_dot_slash(sub
->url
) ||
1367 starts_with_dot_slash(sub
->url
)) {
1368 char *up_path
= get_up_path(path
);
1369 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1370 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1373 sub_origin_url
= xstrdup(sub
->url
);
1374 super_config_url
= xstrdup(sub
->url
);
1377 sub_origin_url
= xstrdup("");
1378 super_config_url
= xstrdup("");
1381 displaypath
= get_submodule_displaypath(path
, prefix
);
1383 if (!(flags
& OPT_QUIET
))
1384 printf(_("Synchronizing submodule url for '%s'\n"),
1388 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1389 if (git_config_set_gently(sb
.buf
, super_config_url
))
1390 die(_("failed to register url for submodule path '%s'"),
1393 if (!is_submodule_populated_gently(path
, NULL
))
1397 default_remote
= get_default_remote_submodule(path
);
1398 if (!default_remote
)
1399 die(_("failed to get the default remote for submodule '%s'"),
1402 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1403 free(default_remote
);
1405 submodule_to_gitdir(&sb
, path
);
1406 strbuf_addstr(&sb
, "/config");
1408 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1409 die(_("failed to update remote for submodule '%s'"),
1412 if (flags
& OPT_RECURSIVE
) {
1413 struct child_process cpr
= CHILD_PROCESS_INIT
;
1417 prepare_submodule_repo_env(&cpr
.env_array
);
1419 strvec_push(&cpr
.args
, "--super-prefix");
1420 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1421 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1422 "--recursive", NULL
);
1424 if (flags
& OPT_QUIET
)
1425 strvec_push(&cpr
.args
, "--quiet");
1427 if (run_command(&cpr
))
1428 die(_("failed to recurse into submodule '%s'"),
1433 free(super_config_url
);
1434 free(sub_origin_url
);
1435 strbuf_release(&sb
);
1438 free(sub_config_path
);
1441 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1443 struct sync_cb
*info
= cb_data
;
1444 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1447 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1449 struct sync_cb info
= SYNC_CB_INIT
;
1450 struct pathspec pathspec
;
1451 struct module_list list
= MODULE_LIST_INIT
;
1455 struct option module_sync_options
[] = {
1456 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1457 OPT_BOOL(0, "recursive", &recursive
,
1458 N_("recurse into nested submodules")),
1462 const char *const git_submodule_helper_usage
[] = {
1463 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1467 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1468 git_submodule_helper_usage
, 0);
1470 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1473 info
.prefix
= prefix
;
1475 info
.flags
|= OPT_QUIET
;
1477 info
.flags
|= OPT_RECURSIVE
;
1479 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1488 #define DEINIT_CB_INIT { 0 }
1490 static void deinit_submodule(const char *path
, const char *prefix
,
1493 const struct submodule
*sub
;
1494 char *displaypath
= NULL
;
1495 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1496 struct strbuf sb_config
= STRBUF_INIT
;
1497 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1499 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1501 if (!sub
|| !sub
->name
)
1504 displaypath
= get_submodule_displaypath(path
, prefix
);
1506 /* remove the submodule work tree (unless the user already did it) */
1507 if (is_directory(path
)) {
1508 struct strbuf sb_rm
= STRBUF_INIT
;
1511 if (is_directory(sub_git_dir
)) {
1512 if (!(flags
& OPT_QUIET
))
1513 warning(_("Submodule work tree '%s' contains a .git "
1514 "directory. This will be replaced with a "
1515 ".git file by using absorbgitdirs."),
1518 absorb_git_dir_into_superproject(path
,
1519 ABSORB_GITDIR_RECURSE_SUBMODULES
);
1523 if (!(flags
& OPT_FORCE
)) {
1524 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1526 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1529 if (run_command(&cp_rm
))
1530 die(_("Submodule work tree '%s' contains local "
1531 "modifications; use '-f' to discard them"),
1535 strbuf_addstr(&sb_rm
, path
);
1537 if (!remove_dir_recursively(&sb_rm
, 0))
1538 format
= _("Cleared directory '%s'\n");
1540 format
= _("Could not remove submodule work tree '%s'\n");
1542 if (!(flags
& OPT_QUIET
))
1543 printf(format
, displaypath
);
1545 submodule_unset_core_worktree(sub
);
1547 strbuf_release(&sb_rm
);
1550 if (mkdir(path
, 0777))
1551 printf(_("could not create empty submodule directory %s"),
1554 cp_config
.git_cmd
= 1;
1555 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1556 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1558 /* remove the .git/config entries (unless the user already did it) */
1559 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1560 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1562 * remove the whole section so we have a clean state when
1563 * the user later decides to init this submodule again
1565 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1566 if (!(flags
& OPT_QUIET
))
1567 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1568 sub
->name
, sub
->url
, displaypath
);
1575 strbuf_release(&sb_config
);
1578 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1581 struct deinit_cb
*info
= cb_data
;
1582 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1585 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1587 struct deinit_cb info
= DEINIT_CB_INIT
;
1588 struct pathspec pathspec
;
1589 struct module_list list
= MODULE_LIST_INIT
;
1594 struct option module_deinit_options
[] = {
1595 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1596 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1597 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1601 const char *const git_submodule_helper_usage
[] = {
1602 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1606 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1607 git_submodule_helper_usage
, 0);
1610 error("pathspec and --all are incompatible");
1611 usage_with_options(git_submodule_helper_usage
,
1612 module_deinit_options
);
1616 die(_("Use '--all' if you really want to deinitialize all submodules"));
1618 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1621 info
.prefix
= prefix
;
1623 info
.flags
|= OPT_QUIET
;
1625 info
.flags
|= OPT_FORCE
;
1627 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1632 struct module_clone_data
{
1638 struct list_objects_filter_options
*filter_options
;
1639 struct string_list reference
;
1640 unsigned int quiet
: 1;
1641 unsigned int progress
: 1;
1642 unsigned int dissociate
: 1;
1643 unsigned int require_init
: 1;
1646 #define MODULE_CLONE_DATA_INIT { \
1647 .reference = STRING_LIST_INIT_NODUP, \
1648 .single_branch = -1, \
1651 struct submodule_alternate_setup
{
1652 const char *submodule_name
;
1653 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1654 SUBMODULE_ALTERNATE_ERROR_DIE
,
1655 SUBMODULE_ALTERNATE_ERROR_INFO
,
1656 SUBMODULE_ALTERNATE_ERROR_IGNORE
1658 struct string_list
*reference
;
1660 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1661 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1664 static const char alternate_error_advice
[] = N_(
1665 "An alternate computed from a superproject's alternate is invalid.\n"
1666 "To allow Git to clone without an alternate in such a case, set\n"
1667 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1668 "'--reference-if-able' instead of '--reference'."
1671 static int add_possible_reference_from_superproject(
1672 struct object_directory
*odb
, void *sas_cb
)
1674 struct submodule_alternate_setup
*sas
= sas_cb
;
1678 * If the alternate object store is another repository, try the
1679 * standard layout with .git/(modules/<name>)+/objects
1681 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1682 struct repository alternate
;
1684 struct strbuf sb
= STRBUF_INIT
;
1685 struct strbuf err
= STRBUF_INIT
;
1686 strbuf_add(&sb
, odb
->path
, len
);
1688 repo_init(&alternate
, sb
.buf
, NULL
);
1691 * We need to end the new path with '/' to mark it as a dir,
1692 * otherwise a submodule name containing '/' will be broken
1693 * as the last part of a missing submodule reference would
1694 * be taken as a file name.
1697 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1698 strbuf_addch(&sb
, '/');
1699 repo_clear(&alternate
);
1701 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1703 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1706 switch (sas
->error_mode
) {
1707 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1708 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1709 advise(_(alternate_error_advice
));
1710 die(_("submodule '%s' cannot add alternate: %s"),
1711 sas
->submodule_name
, err
.buf
);
1712 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1713 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1714 sas
->submodule_name
, err
.buf
);
1715 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1719 strbuf_release(&sb
);
1725 static void prepare_possible_alternates(const char *sm_name
,
1726 struct string_list
*reference
)
1728 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1729 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1731 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1735 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1737 if (!error_strategy
)
1738 error_strategy
= xstrdup("die");
1740 sas
.submodule_name
= sm_name
;
1741 sas
.reference
= reference
;
1742 if (!strcmp(error_strategy
, "die"))
1743 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1744 else if (!strcmp(error_strategy
, "info"))
1745 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1746 else if (!strcmp(error_strategy
, "ignore"))
1747 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1749 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1751 if (!strcmp(sm_alternate
, "superproject"))
1752 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1753 else if (!strcmp(sm_alternate
, "no"))
1756 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1759 free(error_strategy
);
1762 static int clone_submodule(struct module_clone_data
*clone_data
)
1764 char *p
, *sm_gitdir
;
1765 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1766 struct strbuf sb
= STRBUF_INIT
;
1767 struct child_process cp
= CHILD_PROCESS_INIT
;
1769 submodule_name_to_gitdir(&sb
, the_repository
, clone_data
->name
);
1770 sm_gitdir
= absolute_pathdup(sb
.buf
);
1773 if (!is_absolute_path(clone_data
->path
)) {
1774 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), clone_data
->path
);
1775 clone_data
->path
= strbuf_detach(&sb
, NULL
);
1777 clone_data
->path
= xstrdup(clone_data
->path
);
1780 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1781 die(_("refusing to create/use '%s' in another submodule's "
1782 "git dir"), sm_gitdir
);
1784 if (!file_exists(sm_gitdir
)) {
1785 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1786 die(_("could not create directory '%s'"), sm_gitdir
);
1788 prepare_possible_alternates(clone_data
->name
, &clone_data
->reference
);
1790 strvec_push(&cp
.args
, "clone");
1791 strvec_push(&cp
.args
, "--no-checkout");
1792 if (clone_data
->quiet
)
1793 strvec_push(&cp
.args
, "--quiet");
1794 if (clone_data
->progress
)
1795 strvec_push(&cp
.args
, "--progress");
1796 if (clone_data
->depth
&& *(clone_data
->depth
))
1797 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1798 if (clone_data
->reference
.nr
) {
1799 struct string_list_item
*item
;
1800 for_each_string_list_item(item
, &clone_data
->reference
)
1801 strvec_pushl(&cp
.args
, "--reference",
1802 item
->string
, NULL
);
1804 if (clone_data
->dissociate
)
1805 strvec_push(&cp
.args
, "--dissociate");
1806 if (sm_gitdir
&& *sm_gitdir
)
1807 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1808 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1809 strvec_pushf(&cp
.args
, "--filter=%s",
1810 expand_list_objects_filter_spec(
1811 clone_data
->filter_options
));
1812 if (clone_data
->single_branch
>= 0)
1813 strvec_push(&cp
.args
, clone_data
->single_branch
?
1815 "--no-single-branch");
1817 strvec_push(&cp
.args
, "--");
1818 strvec_push(&cp
.args
, clone_data
->url
);
1819 strvec_push(&cp
.args
, clone_data
->path
);
1822 prepare_submodule_repo_env(&cp
.env_array
);
1825 if(run_command(&cp
))
1826 die(_("clone of '%s' into submodule path '%s' failed"),
1827 clone_data
->url
, clone_data
->path
);
1829 if (clone_data
->require_init
&& !access(clone_data
->path
, X_OK
) &&
1830 !is_empty_dir(clone_data
->path
))
1831 die(_("directory not empty: '%s'"), clone_data
->path
);
1832 if (safe_create_leading_directories_const(clone_data
->path
) < 0)
1833 die(_("could not create directory '%s'"), clone_data
->path
);
1834 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1835 unlink_or_warn(sb
.buf
);
1839 connect_work_tree_and_git_dir(clone_data
->path
, sm_gitdir
, 0);
1841 p
= git_pathdup_submodule(clone_data
->path
, "config");
1843 die(_("could not get submodule directory for '%s'"), clone_data
->path
);
1845 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1846 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1848 git_config_set_in_file(p
, "submodule.alternateLocation",
1850 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1852 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1856 free(error_strategy
);
1858 strbuf_release(&sb
);
1864 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1866 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1867 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1868 struct list_objects_filter_options filter_options
;
1870 struct option module_clone_options
[] = {
1871 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1873 N_("alternative anchor for relative paths")),
1874 OPT_STRING(0, "path", &clone_data
.path
,
1876 N_("where the new submodule will be cloned to")),
1877 OPT_STRING(0, "name", &clone_data
.name
,
1879 N_("name of the new submodule")),
1880 OPT_STRING(0, "url", &clone_data
.url
,
1882 N_("url where to clone the submodule from")),
1883 OPT_STRING_LIST(0, "reference", &clone_data
.reference
,
1885 N_("reference repository")),
1886 OPT_BOOL(0, "dissociate", &dissociate
,
1887 N_("use --reference only while cloning")),
1888 OPT_STRING(0, "depth", &clone_data
.depth
,
1890 N_("depth for shallow clones")),
1891 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1892 OPT_BOOL(0, "progress", &progress
,
1893 N_("force cloning progress")),
1894 OPT_BOOL(0, "require-init", &require_init
,
1895 N_("disallow cloning into non-empty directory")),
1896 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1897 N_("clone only one branch, HEAD or --branch")),
1898 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1902 const char *const git_submodule_helper_usage
[] = {
1903 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1904 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1905 "[--single-branch] [--filter <filter-spec>] "
1906 "--url <url> --path <path>"),
1910 memset(&filter_options
, 0, sizeof(filter_options
));
1911 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1912 git_submodule_helper_usage
, 0);
1914 clone_data
.dissociate
= !!dissociate
;
1915 clone_data
.quiet
= !!quiet
;
1916 clone_data
.progress
= !!progress
;
1917 clone_data
.require_init
= !!require_init
;
1918 clone_data
.filter_options
= &filter_options
;
1920 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1921 usage_with_options(git_submodule_helper_usage
,
1922 module_clone_options
);
1924 clone_submodule(&clone_data
);
1925 list_objects_filter_release(&filter_options
);
1929 static void determine_submodule_update_strategy(struct repository
*r
,
1933 struct submodule_update_strategy
*out
)
1935 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1939 key
= xstrfmt("submodule.%s.update", sub
->name
);
1942 if (parse_submodule_update_strategy(update
, out
) < 0)
1943 die(_("Invalid update mode '%s' for submodule path '%s'"),
1945 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1946 if (parse_submodule_update_strategy(val
, out
) < 0)
1947 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1949 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1950 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1951 BUG("how did we read update = !command from .gitmodules?");
1952 out
->type
= sub
->update_strategy
.type
;
1953 out
->command
= sub
->update_strategy
.command
;
1955 out
->type
= SM_UPDATE_CHECKOUT
;
1958 (out
->type
== SM_UPDATE_MERGE
||
1959 out
->type
== SM_UPDATE_REBASE
||
1960 out
->type
== SM_UPDATE_NONE
))
1961 out
->type
= SM_UPDATE_CHECKOUT
;
1966 struct update_clone_data
{
1967 const struct submodule
*sub
;
1968 struct object_id oid
;
1969 unsigned just_cloned
;
1972 struct submodule_update_clone
{
1973 /* index into 'update_data.list', the list of submodules to look into for cloning */
1976 /* configuration parameters which are passed on to the children */
1977 struct update_data
*update_data
;
1979 /* to be consumed by update_submodule() */
1980 struct update_clone_data
*update_clone
;
1981 int update_clone_nr
; int update_clone_alloc
;
1983 /* If we want to stop as fast as possible and return an error */
1984 unsigned quickstop
: 1;
1986 /* failed clones to be retried again */
1987 const struct cache_entry
**failed_clones
;
1988 int failed_clones_nr
, failed_clones_alloc
;
1990 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1992 struct update_data
{
1994 const char *recursive_prefix
;
1995 const char *displaypath
;
1996 const char *update_default
;
1997 struct object_id suboid
;
1998 struct string_list references
;
1999 struct submodule_update_strategy update_strategy
;
2000 struct list_objects_filter_options
*filter_options
;
2001 struct module_list list
;
2005 int recommend_shallow
;
2006 unsigned int require_init
;
2009 unsigned int nofetch
;
2010 unsigned int remote
;
2011 unsigned int progress
;
2012 unsigned int dissociate
;
2014 unsigned int warn_if_uninitialized
;
2015 unsigned int recursive
;
2017 /* copied over from update_clone_data */
2018 struct object_id oid
;
2019 unsigned int just_cloned
;
2020 const char *sm_path
;
2022 #define UPDATE_DATA_INIT { \
2023 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2024 .list = MODULE_LIST_INIT, \
2025 .recommend_shallow = -1, \
2026 .references = STRING_LIST_INIT_DUP, \
2027 .single_branch = -1, \
2031 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
2032 struct strbuf
*out
, const char *displaypath
)
2035 * Only mention uninitialized submodules when their
2036 * paths have been specified.
2038 if (suc
->update_data
->warn_if_uninitialized
) {
2040 _("Submodule path '%s' not initialized"),
2042 strbuf_addch(out
, '\n');
2044 _("Maybe you want to use 'update --init'?"));
2045 strbuf_addch(out
, '\n');
2050 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2051 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2053 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
2054 struct child_process
*child
,
2055 struct submodule_update_clone
*suc
,
2058 const struct submodule
*sub
= NULL
;
2059 const char *url
= NULL
;
2060 const char *update_string
;
2061 enum submodule_update_type update_type
;
2063 struct strbuf displaypath_sb
= STRBUF_INIT
;
2064 struct strbuf sb
= STRBUF_INIT
;
2065 const char *displaypath
= NULL
;
2066 int needs_cloning
= 0;
2067 int need_free_url
= 0;
2070 if (suc
->update_data
->recursive_prefix
)
2071 strbuf_addf(&sb
, "%s/%s", suc
->update_data
->recursive_prefix
, ce
->name
);
2073 strbuf_addstr(&sb
, ce
->name
);
2074 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
2075 strbuf_addch(out
, '\n');
2079 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
2081 if (suc
->update_data
->recursive_prefix
)
2082 displaypath
= relative_path(suc
->update_data
->recursive_prefix
,
2083 ce
->name
, &displaypath_sb
);
2085 displaypath
= ce
->name
;
2088 next_submodule_warn_missing(suc
, out
, displaypath
);
2092 key
= xstrfmt("submodule.%s.update", sub
->name
);
2093 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2094 update_type
= parse_submodule_update_type(update_string
);
2096 update_type
= sub
->update_strategy
.type
;
2100 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2101 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2102 && update_type
== SM_UPDATE_NONE
)) {
2103 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2104 strbuf_addch(out
, '\n');
2108 /* Check if the submodule has been initialized. */
2109 if (!is_submodule_active(the_repository
, ce
->name
)) {
2110 next_submodule_warn_missing(suc
, out
, displaypath
);
2115 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2116 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2117 if (starts_with_dot_slash(sub
->url
) ||
2118 starts_with_dot_dot_slash(sub
->url
)) {
2119 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2126 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2127 needs_cloning
= !file_exists(sb
.buf
);
2129 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2130 suc
->update_clone_alloc
);
2131 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2132 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2133 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2134 suc
->update_clone_nr
++;
2140 child
->no_stdin
= 1;
2141 child
->stdout_to_stderr
= 1;
2143 strvec_push(&child
->args
, "submodule--helper");
2144 strvec_push(&child
->args
, "clone");
2145 if (suc
->update_data
->progress
)
2146 strvec_push(&child
->args
, "--progress");
2147 if (suc
->update_data
->quiet
)
2148 strvec_push(&child
->args
, "--quiet");
2149 if (suc
->update_data
->prefix
)
2150 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2151 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2152 strvec_push(&child
->args
, "--depth=1");
2153 else if (suc
->update_data
->depth
)
2154 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2155 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2156 strvec_pushf(&child
->args
, "--filter=%s",
2157 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2158 if (suc
->update_data
->require_init
)
2159 strvec_push(&child
->args
, "--require-init");
2160 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2161 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2162 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2163 if (suc
->update_data
->references
.nr
) {
2164 struct string_list_item
*item
;
2165 for_each_string_list_item(item
, &suc
->update_data
->references
)
2166 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2168 if (suc
->update_data
->dissociate
)
2169 strvec_push(&child
->args
, "--dissociate");
2170 if (suc
->update_data
->single_branch
>= 0)
2171 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2173 "--no-single-branch");
2176 strbuf_release(&displaypath_sb
);
2177 strbuf_release(&sb
);
2181 return needs_cloning
;
2184 static int update_clone_get_next_task(struct child_process
*child
,
2189 struct submodule_update_clone
*suc
= suc_cb
;
2190 const struct cache_entry
*ce
;
2193 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2194 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2195 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2196 int *p
= xmalloc(sizeof(*p
));
2205 * The loop above tried cloning each submodule once, now try the
2206 * stragglers again, which we can imagine as an extension of the
2209 index
= suc
->current
- suc
->update_data
->list
.nr
;
2210 if (index
< suc
->failed_clones_nr
) {
2212 ce
= suc
->failed_clones
[index
];
2213 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2215 strbuf_addstr(err
, "BUG: submodule considered for "
2216 "cloning, doesn't need cloning "
2220 p
= xmalloc(sizeof(*p
));
2230 static int update_clone_start_failure(struct strbuf
*err
,
2234 struct submodule_update_clone
*suc
= suc_cb
;
2239 static int update_clone_task_finished(int result
,
2244 const struct cache_entry
*ce
;
2245 struct submodule_update_clone
*suc
= suc_cb
;
2247 int *idxP
= idx_task_cb
;
2254 if (idx
< suc
->update_data
->list
.nr
) {
2255 ce
= suc
->update_data
->list
.entries
[idx
];
2256 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2258 strbuf_addch(err
, '\n');
2259 ALLOC_GROW(suc
->failed_clones
,
2260 suc
->failed_clones_nr
+ 1,
2261 suc
->failed_clones_alloc
);
2262 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2265 idx
-= suc
->update_data
->list
.nr
;
2266 ce
= suc
->failed_clones
[idx
];
2267 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2269 strbuf_addch(err
, '\n');
2277 static int git_update_clone_config(const char *var
, const char *value
,
2281 if (!strcmp(var
, "submodule.fetchjobs"))
2282 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2286 static int is_tip_reachable(const char *path
, struct object_id
*oid
)
2288 struct child_process cp
= CHILD_PROCESS_INIT
;
2289 struct strbuf rev
= STRBUF_INIT
;
2290 char *hex
= oid_to_hex(oid
);
2293 cp
.dir
= xstrdup(path
);
2295 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2297 prepare_submodule_repo_env(&cp
.env_array
);
2299 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2305 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
, struct object_id
*oid
)
2307 struct child_process cp
= CHILD_PROCESS_INIT
;
2309 prepare_submodule_repo_env(&cp
.env_array
);
2311 cp
.dir
= xstrdup(module_path
);
2313 strvec_push(&cp
.args
, "fetch");
2315 strvec_push(&cp
.args
, "--quiet");
2317 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2319 char *hex
= oid_to_hex(oid
);
2320 char *remote
= get_default_remote();
2321 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2324 return run_command(&cp
);
2327 static int run_update_command(struct update_data
*ud
, int subforce
)
2329 struct child_process cp
= CHILD_PROCESS_INIT
;
2330 char *oid
= oid_to_hex(&ud
->oid
);
2331 int must_die_on_failure
= 0;
2333 switch (ud
->update_strategy
.type
) {
2334 case SM_UPDATE_CHECKOUT
:
2336 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2338 strvec_push(&cp
.args
, "-f");
2340 case SM_UPDATE_REBASE
:
2342 strvec_push(&cp
.args
, "rebase");
2344 strvec_push(&cp
.args
, "--quiet");
2345 must_die_on_failure
= 1;
2347 case SM_UPDATE_MERGE
:
2349 strvec_push(&cp
.args
, "merge");
2351 strvec_push(&cp
.args
, "--quiet");
2352 must_die_on_failure
= 1;
2354 case SM_UPDATE_COMMAND
:
2356 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2357 must_die_on_failure
= 1;
2360 BUG("unexpected update strategy type: %s",
2361 submodule_strategy_to_string(&ud
->update_strategy
));
2363 strvec_push(&cp
.args
, oid
);
2365 cp
.dir
= xstrdup(ud
->sm_path
);
2366 prepare_submodule_repo_env(&cp
.env_array
);
2367 if (run_command(&cp
)) {
2368 switch (ud
->update_strategy
.type
) {
2369 case SM_UPDATE_CHECKOUT
:
2370 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2371 oid
, ud
->displaypath
);
2373 case SM_UPDATE_REBASE
:
2374 die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2375 oid
, ud
->displaypath
);
2377 case SM_UPDATE_MERGE
:
2378 die_message(_("Unable to merge '%s' in submodule path '%s'"),
2379 oid
, ud
->displaypath
);
2381 case SM_UPDATE_COMMAND
:
2382 die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2383 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2386 BUG("unexpected update strategy type: %s",
2387 submodule_strategy_to_string(&ud
->update_strategy
));
2389 if (must_die_on_failure
)
2392 /* the command failed, but update must continue */
2399 switch (ud
->update_strategy
.type
) {
2400 case SM_UPDATE_CHECKOUT
:
2401 printf(_("Submodule path '%s': checked out '%s'\n"),
2402 ud
->displaypath
, oid
);
2404 case SM_UPDATE_REBASE
:
2405 printf(_("Submodule path '%s': rebased into '%s'\n"),
2406 ud
->displaypath
, oid
);
2408 case SM_UPDATE_MERGE
:
2409 printf(_("Submodule path '%s': merged in '%s'\n"),
2410 ud
->displaypath
, oid
);
2412 case SM_UPDATE_COMMAND
:
2413 printf(_("Submodule path '%s': '%s %s'\n"),
2414 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2417 BUG("unexpected update strategy type: %s",
2418 submodule_strategy_to_string(&ud
->update_strategy
));
2424 static int run_update_procedure(struct update_data
*ud
)
2426 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2430 * Run fetch only if `oid` isn't present or it
2431 * is not reachable from a ref.
2433 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2434 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2437 _("Unable to fetch in submodule path '%s'; "
2438 "trying to directly fetch %s:"),
2439 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2441 * Now we tried the usual fetch, but `oid` may
2442 * not be reachable from any of the refs.
2444 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2445 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2446 die(_("Fetched in submodule path '%s', but it did not "
2447 "contain %s. Direct fetching of that commit failed."),
2448 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2451 return run_update_command(ud
, subforce
);
2454 static const char *remote_submodule_branch(const char *path
)
2456 const struct submodule
*sub
;
2457 const char *branch
= NULL
;
2460 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2464 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2465 if (repo_config_get_string_tmp(the_repository
, key
, &branch
))
2466 branch
= sub
->branch
;
2472 if (!strcmp(branch
, ".")) {
2473 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2476 die(_("No such ref: %s"), "HEAD");
2479 if (!strcmp(refname
, "HEAD"))
2480 die(_("Submodule (%s) branch configured to inherit "
2481 "branch from superproject, but the superproject "
2482 "is not on any branch"), sub
->name
);
2484 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2485 die(_("Expecting a full ref name, got %s"), refname
);
2492 static void ensure_core_worktree(const char *path
)
2495 struct repository subrepo
;
2497 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2498 die(_("could not get a repository handle for submodule '%s'"), path
);
2500 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2501 char *cfg_file
, *abs_path
;
2502 const char *rel_path
;
2503 struct strbuf sb
= STRBUF_INIT
;
2505 cfg_file
= repo_git_path(&subrepo
, "config");
2507 abs_path
= absolute_pathdup(path
);
2508 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2510 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2514 strbuf_release(&sb
);
2518 static void update_data_to_args(struct update_data
*update_data
, struct strvec
*args
)
2520 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2521 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2522 if (update_data
->recursive_prefix
)
2523 strvec_pushl(args
, "--recursive-prefix",
2524 update_data
->recursive_prefix
, NULL
);
2525 if (update_data
->quiet
)
2526 strvec_push(args
, "--quiet");
2527 if (update_data
->force
)
2528 strvec_push(args
, "--force");
2529 if (update_data
->init
)
2530 strvec_push(args
, "--init");
2531 if (update_data
->remote
)
2532 strvec_push(args
, "--remote");
2533 if (update_data
->nofetch
)
2534 strvec_push(args
, "--no-fetch");
2535 if (update_data
->dissociate
)
2536 strvec_push(args
, "--dissociate");
2537 if (update_data
->progress
)
2538 strvec_push(args
, "--progress");
2539 if (update_data
->require_init
)
2540 strvec_push(args
, "--require-init");
2541 if (update_data
->depth
)
2542 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2543 if (update_data
->update_default
)
2544 strvec_pushl(args
, "--update", update_data
->update_default
, NULL
);
2545 if (update_data
->references
.nr
) {
2546 struct string_list_item
*item
;
2547 for_each_string_list_item(item
, &update_data
->references
)
2548 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2550 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2551 strvec_pushf(args
, "--filter=%s",
2552 expand_list_objects_filter_spec(
2553 update_data
->filter_options
));
2554 if (update_data
->recommend_shallow
== 0)
2555 strvec_push(args
, "--no-recommend-shallow");
2556 else if (update_data
->recommend_shallow
== 1)
2557 strvec_push(args
, "--recommend-shallow");
2558 if (update_data
->single_branch
>= 0)
2559 strvec_push(args
, update_data
->single_branch
?
2561 "--no-single-branch");
2564 static int update_submodule(struct update_data
*update_data
)
2566 char *prefixed_path
;
2568 ensure_core_worktree(update_data
->sm_path
);
2570 if (update_data
->recursive_prefix
)
2571 prefixed_path
= xstrfmt("%s%s", update_data
->recursive_prefix
,
2572 update_data
->sm_path
);
2574 prefixed_path
= xstrdup(update_data
->sm_path
);
2576 update_data
->displaypath
= get_submodule_displaypath(prefixed_path
,
2577 update_data
->prefix
);
2578 free(prefixed_path
);
2580 determine_submodule_update_strategy(the_repository
, update_data
->just_cloned
,
2581 update_data
->sm_path
, update_data
->update_default
,
2582 &update_data
->update_strategy
);
2584 if (update_data
->just_cloned
)
2585 oidcpy(&update_data
->suboid
, null_oid());
2586 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2587 die(_("Unable to find current revision in submodule path '%s'"),
2588 update_data
->displaypath
);
2590 if (update_data
->remote
) {
2591 char *remote_name
= get_default_remote_submodule(update_data
->sm_path
);
2592 const char *branch
= remote_submodule_branch(update_data
->sm_path
);
2593 char *remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2595 if (!update_data
->nofetch
) {
2596 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2598 die(_("Unable to fetch in submodule path '%s'"),
2599 update_data
->sm_path
);
2602 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2603 die(_("Unable to find %s revision in submodule path '%s'"),
2604 remote_ref
, update_data
->sm_path
);
2609 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
)
2610 if (run_update_procedure(update_data
))
2613 if (update_data
->recursive
) {
2614 struct child_process cp
= CHILD_PROCESS_INIT
;
2615 struct update_data next
= *update_data
;
2618 if (update_data
->recursive_prefix
)
2619 prefixed_path
= xstrfmt("%s%s/", update_data
->recursive_prefix
,
2620 update_data
->sm_path
);
2622 prefixed_path
= xstrfmt("%s/", update_data
->sm_path
);
2624 next
.recursive_prefix
= get_submodule_displaypath(prefixed_path
,
2625 update_data
->prefix
);
2627 oidcpy(&next
.oid
, null_oid());
2628 oidcpy(&next
.suboid
, null_oid());
2630 cp
.dir
= update_data
->sm_path
;
2632 prepare_submodule_repo_env(&cp
.env_array
);
2633 update_data_to_args(&next
, &cp
.args
);
2635 /* die() if child process die()'d */
2636 res
= run_command(&cp
);
2639 die_message(_("Failed to recurse into submodule path '%s'"),
2640 update_data
->displaypath
);
2650 static int update_submodules(struct update_data
*update_data
)
2653 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2655 suc
.update_data
= update_data
;
2656 run_processes_parallel_tr2(suc
.update_data
->max_jobs
, update_clone_get_next_task
,
2657 update_clone_start_failure
,
2658 update_clone_task_finished
, &suc
, "submodule",
2662 * We saved the output and put it out all at once now.
2664 * - the listener does not have to interleave their (checkout)
2665 * work with our fetching. The writes involved in a
2666 * checkout involve more straightforward sequential I/O.
2667 * - the listener can avoid doing any work if fetching failed.
2669 if (suc
.quickstop
) {
2674 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2675 struct update_clone_data ucd
= suc
.update_clone
[i
];
2677 oidcpy(&update_data
->oid
, &ucd
.oid
);
2678 update_data
->just_cloned
= ucd
.just_cloned
;
2679 update_data
->sm_path
= ucd
.sub
->path
;
2681 if (update_submodule(update_data
))
2686 string_list_clear(&update_data
->references
, 0);
2690 static int module_update(int argc
, const char **argv
, const char *prefix
)
2692 struct pathspec pathspec
;
2693 struct update_data opt
= UPDATE_DATA_INIT
;
2694 struct list_objects_filter_options filter_options
;
2697 struct option module_update_options
[] = {
2698 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2699 OPT_BOOL(0, "init", &opt
.init
,
2700 N_("initialize uninitialized submodules before update")),
2701 OPT_BOOL(0, "remote", &opt
.remote
,
2702 N_("use SHA-1 of submodule's remote tracking branch")),
2703 OPT_BOOL(0, "recursive", &opt
.recursive
,
2704 N_("traverse submodules recursively")),
2705 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2706 N_("don't fetch new objects from the remote site")),
2707 OPT_STRING(0, "prefix", &opt
.prefix
,
2709 N_("path into the working tree")),
2710 OPT_STRING(0, "recursive-prefix", &opt
.recursive_prefix
,
2712 N_("path into the working tree, across nested "
2713 "submodule boundaries")),
2714 OPT_STRING(0, "update", &opt
.update_default
,
2716 N_("rebase, merge, checkout or none")),
2717 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2718 N_("reference repository")),
2719 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2720 N_("use --reference only while cloning")),
2721 OPT_INTEGER(0, "depth", &opt
.depth
,
2722 N_("create a shallow clone truncated to the "
2723 "specified number of revisions")),
2724 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2725 N_("parallel jobs")),
2726 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2727 N_("whether the initial clone should follow the shallow recommendation")),
2728 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2729 OPT_BOOL(0, "progress", &opt
.progress
,
2730 N_("force cloning progress")),
2731 OPT_BOOL(0, "require-init", &opt
.require_init
,
2732 N_("disallow cloning into non-empty directory")),
2733 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2734 N_("clone only one branch, HEAD or --branch")),
2735 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2739 const char *const git_submodule_helper_usage
[] = {
2740 N_("git submodule [--quiet] update"
2741 " [--init [--filter=<filter-spec>]] [--remote]"
2742 " [-N|--no-fetch] [-f|--force]"
2743 " [--checkout|--merge|--rebase]"
2744 " [--[no-]recommend-shallow] [--reference <repository>]"
2745 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2749 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2750 git_config(git_update_clone_config
, &opt
.max_jobs
);
2752 memset(&filter_options
, 0, sizeof(filter_options
));
2753 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2754 git_submodule_helper_usage
, 0);
2756 if (filter_options
.choice
&& !opt
.init
) {
2757 usage_with_options(git_submodule_helper_usage
,
2758 module_update_options
);
2761 opt
.filter_options
= &filter_options
;
2763 if (opt
.update_default
)
2764 if (parse_submodule_update_strategy(opt
.update_default
,
2765 &opt
.update_strategy
) < 0)
2766 die(_("bad value for update parameter"));
2768 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2769 list_objects_filter_release(&filter_options
);
2774 opt
.warn_if_uninitialized
= 1;
2777 struct module_list list
= MODULE_LIST_INIT
;
2778 struct init_cb info
= INIT_CB_INIT
;
2780 if (module_list_compute(argc
, argv
, opt
.prefix
,
2781 &pathspec
, &list
) < 0)
2785 * If there are no path args and submodule.active is set then,
2786 * by default, only initialize 'active' modules.
2788 if (!argc
&& git_config_get_value_multi("submodule.active"))
2789 module_list_active(&list
);
2791 info
.prefix
= opt
.prefix
;
2792 info
.superprefix
= opt
.recursive_prefix
;
2794 info
.flags
|= OPT_QUIET
;
2796 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2799 ret
= update_submodules(&opt
);
2800 list_objects_filter_release(&filter_options
);
2804 static int push_check(int argc
, const char **argv
, const char *prefix
)
2806 struct remote
*remote
;
2807 const char *superproject_head
;
2809 int detached_head
= 0;
2810 struct object_id head_oid
;
2813 die("submodule--helper push-check requires at least 2 arguments");
2816 * superproject's resolved head ref.
2817 * if HEAD then the superproject is in a detached head state, otherwise
2818 * it will be the resolved head ref.
2820 superproject_head
= argv
[1];
2823 /* Get the submodule's head ref and determine if it is detached */
2824 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2826 die(_("Failed to resolve HEAD as a valid ref."));
2827 if (!strcmp(head
, "HEAD"))
2831 * The remote must be configured.
2832 * This is to avoid pushing to the exact same URL as the parent.
2834 remote
= pushremote_get(argv
[1]);
2835 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2836 die("remote '%s' not configured", argv
[1]);
2838 /* Check the refspec */
2841 struct ref
*local_refs
= get_local_heads();
2842 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2844 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2846 for (i
= 0; i
< refspec
.nr
; i
++) {
2847 const struct refspec_item
*rs
= &refspec
.items
[i
];
2849 if (rs
->pattern
|| rs
->matching
)
2852 /* LHS must match a single ref */
2853 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2858 * If LHS matches 'HEAD' then we need to ensure
2859 * that it matches the same named branch
2860 * checked out in the superproject.
2862 if (!strcmp(rs
->src
, "HEAD")) {
2863 if (!detached_head
&&
2864 !strcmp(head
, superproject_head
))
2866 die("HEAD does not match the named branch in the superproject");
2870 die("src refspec '%s' must name a ref",
2874 refspec_clear(&refspec
);
2881 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2884 struct pathspec pathspec
;
2885 struct module_list list
= MODULE_LIST_INIT
;
2886 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
2888 struct option embed_gitdir_options
[] = {
2889 OPT_STRING(0, "prefix", &prefix
,
2891 N_("path into the working tree")),
2892 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
2893 ABSORB_GITDIR_RECURSE_SUBMODULES
),
2897 const char *const git_submodule_helper_usage
[] = {
2898 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2902 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2903 git_submodule_helper_usage
, 0);
2905 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
2908 for (i
= 0; i
< list
.nr
; i
++)
2909 absorb_git_dir_into_superproject(list
.entries
[i
]->name
, flags
);
2914 static int is_active(int argc
, const char **argv
, const char *prefix
)
2917 die("submodule--helper is-active takes exactly 1 argument");
2919 return !is_submodule_active(the_repository
, argv
[1]);
2923 * Exit non-zero if any of the submodule names given on the command line is
2924 * invalid. If no names are given, filter stdin to print only valid names
2925 * (which is primarily intended for testing).
2927 static int check_name(int argc
, const char **argv
, const char *prefix
)
2931 if (check_submodule_name(*argv
) < 0)
2935 struct strbuf buf
= STRBUF_INIT
;
2936 while (strbuf_getline(&buf
, stdin
) != EOF
) {
2937 if (!check_submodule_name(buf
.buf
))
2938 printf("%s\n", buf
.buf
);
2940 strbuf_release(&buf
);
2945 static int module_config(int argc
, const char **argv
, const char *prefix
)
2948 CHECK_WRITEABLE
= 1,
2952 struct option module_config_options
[] = {
2953 OPT_CMDMODE(0, "check-writeable", &command
,
2954 N_("check if it is safe to write to the .gitmodules file"),
2956 OPT_CMDMODE(0, "unset", &command
,
2957 N_("unset the config in the .gitmodules file"),
2961 const char *const git_submodule_helper_usage
[] = {
2962 N_("git submodule--helper config <name> [<value>]"),
2963 N_("git submodule--helper config --unset <name>"),
2964 "git submodule--helper config --check-writeable",
2968 argc
= parse_options(argc
, argv
, prefix
, module_config_options
,
2969 git_submodule_helper_usage
, PARSE_OPT_KEEP_ARGV0
);
2971 if (argc
== 1 && command
== CHECK_WRITEABLE
)
2972 return is_writing_gitmodules_ok() ? 0 : -1;
2974 /* Equivalent to ACTION_GET in builtin/config.c */
2975 if (argc
== 2 && command
!= DO_UNSET
)
2976 return print_config_from_gitmodules(the_repository
, argv
[1]);
2978 /* Equivalent to ACTION_SET in builtin/config.c */
2979 if (argc
== 3 || (argc
== 2 && command
== DO_UNSET
)) {
2980 const char *value
= (argc
== 3) ? argv
[2] : NULL
;
2982 if (!is_writing_gitmodules_ok())
2983 die(_("please make sure that the .gitmodules file is in the working tree"));
2985 return config_set_in_gitmodules_file_gently(argv
[1], value
);
2988 usage_with_options(git_submodule_helper_usage
, module_config_options
);
2991 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2998 struct option options
[] = {
2999 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
3002 const char *const usage
[] = {
3003 N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
3007 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3009 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
3010 usage_with_options(usage
, options
);
3012 config_name
= xstrfmt("submodule.%s.url", path
);
3014 config_set_in_gitmodules_file_gently(config_name
, newurl
);
3015 sync_submodule(path
, prefix
, quiet
? OPT_QUIET
: 0);
3022 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
3024 int opt_default
= 0, ret
;
3025 const char *opt_branch
= NULL
;
3030 * We accept the `quiet` option for uniformity across subcommands,
3031 * though there is nothing to make less verbose in this subcommand.
3033 struct option options
[] = {
3034 OPT_NOOP_NOARG('q', "quiet"),
3035 OPT_BOOL('d', "default", &opt_default
,
3036 N_("set the default tracking branch to master")),
3037 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
3038 N_("set the default tracking branch")),
3041 const char *const usage
[] = {
3042 N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
3043 N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3047 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3049 if (!opt_branch
&& !opt_default
)
3050 die(_("--branch or --default required"));
3052 if (opt_branch
&& opt_default
)
3053 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3055 if (argc
!= 1 || !(path
= argv
[0]))
3056 usage_with_options(usage
, options
);
3058 config_name
= xstrfmt("submodule.%s.branch", path
);
3059 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
3065 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
3067 enum branch_track track
;
3068 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
3070 struct option options
[] = {
3071 OPT__QUIET(&quiet
, N_("print only error messages")),
3072 OPT__FORCE(&force
, N_("force creation"), 0),
3073 OPT_BOOL(0, "create-reflog", &reflog
,
3074 N_("create the branch's reflog")),
3075 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
3076 N_("set branch tracking configuration"),
3078 parse_opt_tracking_mode
),
3079 OPT__DRY_RUN(&dry_run
,
3080 N_("show whether the branch would be created")),
3083 const char *const usage
[] = {
3084 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3088 git_config(git_default_config
, NULL
);
3089 track
= git_branch_track
;
3090 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3093 usage_with_options(usage
, options
);
3095 if (!quiet
&& !dry_run
)
3096 printf_ln(_("creating branch '%s'"), argv
[0]);
3098 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
3099 force
, reflog
, quiet
, track
, dry_run
);
3106 const char *reference_path
;
3108 const char *sm_name
;
3110 const char *realrepo
;
3112 unsigned int force
: 1;
3113 unsigned int quiet
: 1;
3114 unsigned int progress
: 1;
3115 unsigned int dissociate
: 1;
3117 #define ADD_DATA_INIT { .depth = -1 }
3119 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3121 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3122 struct strbuf sb_remote_out
= STRBUF_INIT
;
3124 cp_remote
.git_cmd
= 1;
3125 strvec_pushf(&cp_remote
.env_array
,
3126 "GIT_DIR=%s", git_dir_path
);
3127 strvec_push(&cp_remote
.env_array
, "GIT_WORK_TREE=.");
3128 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3129 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3131 char *line
= sb_remote_out
.buf
;
3132 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3133 size_t len
= next_line
- line
;
3134 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3135 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3136 line
= next_line
+ 1;
3140 strbuf_release(&sb_remote_out
);
3143 static int add_submodule(const struct add_data
*add_data
)
3145 char *submod_gitdir_path
;
3146 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3148 /* perhaps the path already exists and is already a git repo, else clone it */
3149 if (is_directory(add_data
->sm_path
)) {
3150 struct strbuf sm_path
= STRBUF_INIT
;
3151 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3152 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3153 if (is_nonbare_repository_dir(&sm_path
))
3154 printf(_("Adding existing repo at '%s' to the index\n"),
3157 die(_("'%s' already exists and is not a valid git repo"),
3159 strbuf_release(&sm_path
);
3160 free(submod_gitdir_path
);
3162 struct child_process cp
= CHILD_PROCESS_INIT
;
3163 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3165 if (is_directory(submod_gitdir_path
)) {
3166 if (!add_data
->force
) {
3167 struct strbuf msg
= STRBUF_INIT
;
3170 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3171 "locally with remote(s):\n"),
3174 append_fetch_remotes(&msg
, submod_gitdir_path
);
3175 free(submod_gitdir_path
);
3177 strbuf_addf(&msg
, _("If you want to reuse this local git "
3178 "directory instead of cloning again from\n"
3180 "use the '--force' option. If the local git "
3181 "directory is not the correct repo\n"
3182 "or you are unsure what this means choose "
3183 "another name with the '--name' option."),
3184 add_data
->realrepo
);
3186 die_msg
= strbuf_detach(&msg
, NULL
);
3189 printf(_("Reactivating local git directory for "
3190 "submodule '%s'\n"), add_data
->sm_name
);
3193 free(submod_gitdir_path
);
3195 clone_data
.prefix
= add_data
->prefix
;
3196 clone_data
.path
= add_data
->sm_path
;
3197 clone_data
.name
= add_data
->sm_name
;
3198 clone_data
.url
= add_data
->realrepo
;
3199 clone_data
.quiet
= add_data
->quiet
;
3200 clone_data
.progress
= add_data
->progress
;
3201 if (add_data
->reference_path
)
3202 string_list_append(&clone_data
.reference
,
3203 xstrdup(add_data
->reference_path
));
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
))
3211 prepare_submodule_repo_env(&cp
.env_array
);
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
);
3231 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3236 if (!is_writing_gitmodules_ok())
3237 die(_("please make sure that the .gitmodules file is in the working tree"));
3239 key
= xstrfmt("submodule.%s.%s", name
, var
);
3240 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3246 static void configure_added_submodule(struct add_data
*add_data
)
3250 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3251 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3253 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3254 git_config_set_gently(key
, add_data
->realrepo
);
3257 add_submod
.git_cmd
= 1;
3258 strvec_pushl(&add_submod
.args
, "add",
3259 "--no-warn-embedded-repo", NULL
);
3260 if (add_data
->force
)
3261 strvec_push(&add_submod
.args
, "--force");
3262 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3264 if (run_command(&add_submod
))
3265 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3267 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3268 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3269 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3271 if (add_data
->branch
) {
3272 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3273 "branch", add_data
->branch
))
3274 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3277 add_gitmodules
.git_cmd
= 1;
3278 strvec_pushl(&add_gitmodules
.args
,
3279 "add", "--force", "--", ".gitmodules", NULL
);
3281 if (run_command(&add_gitmodules
))
3282 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3285 * NEEDSWORK: In a multi-working-tree world this needs to be
3286 * set in the per-worktree config.
3289 * NEEDSWORK: In the longer run, we need to get rid of this
3290 * pattern of querying "submodule.active" before calling
3291 * is_submodule_active(), since that function needs to find
3292 * out the value of "submodule.active" again anyway.
3294 if (!git_config_get_string("submodule.active", &val
) && val
) {
3296 * If the submodule being added isn't already covered by the
3297 * current configured pathspec, set the submodule's active flag
3299 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3300 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3301 git_config_set_gently(key
, "true");
3305 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3306 git_config_set_gently(key
, "true");
3311 static void die_on_index_match(const char *path
, int force
)
3314 const char *args
[] = { path
, NULL
};
3315 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3317 if (read_cache_preload(NULL
) < 0)
3318 die(_("index file corrupt"));
3322 char *ps_matched
= xcalloc(ps
.nr
, 1);
3324 /* TODO: audit for interaction with sparse-index. */
3325 ensure_full_index(&the_index
);
3328 * Since there is only one pathspec, we just need
3329 * need to check ps_matched[0] to know if a cache
3332 for (i
= 0; i
< active_nr
; i
++) {
3333 ce_path_match(&the_index
, active_cache
[i
], &ps
,
3336 if (ps_matched
[0]) {
3338 die(_("'%s' already exists in the index"),
3340 if (!S_ISGITLINK(active_cache
[i
]->ce_mode
))
3341 die(_("'%s' already exists in the index "
3342 "and is not a submodule"), path
);
3348 clear_pathspec(&ps
);
3351 static void die_on_repo_without_commits(const char *path
)
3353 struct strbuf sb
= STRBUF_INIT
;
3354 strbuf_addstr(&sb
, path
);
3355 if (is_nonbare_repository_dir(&sb
)) {
3356 struct object_id oid
;
3357 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3358 die(_("'%s' does not have a commit checked out"), path
);
3360 strbuf_release(&sb
);
3363 static int module_add(int argc
, const char **argv
, const char *prefix
)
3365 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3366 struct add_data add_data
= ADD_DATA_INIT
;
3367 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")),
3386 const char *const usage
[] = {
3387 N_("git submodule--helper add [<options>] [--] <repository> [<path>]"),
3391 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3393 if (!is_writing_gitmodules_ok())
3394 die(_("please make sure that the .gitmodules file is in the working tree"));
3396 if (prefix
&& *prefix
&&
3397 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3398 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3400 if (argc
== 0 || argc
> 2)
3401 usage_with_options(usage
, options
);
3403 add_data
.repo
= argv
[0];
3405 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3407 add_data
.sm_path
= xstrdup(argv
[1]);
3409 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
))
3410 add_data
.sm_path
= xstrfmt("%s%s", prefix
, add_data
.sm_path
);
3412 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3413 starts_with_dot_slash(add_data
.repo
)) {
3415 die(_("Relative path can only be used from the toplevel "
3416 "of the working tree"));
3418 /* dereference source url relative to parent's url */
3419 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3420 add_data
.realrepo
= to_free
;
3421 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3422 add_data
.realrepo
= add_data
.repo
;
3424 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3430 * multiple //; leading ./; /./; /../;
3432 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3433 strip_dir_trailing_slashes(add_data
.sm_path
);
3435 die_on_index_match(add_data
.sm_path
, force
);
3436 die_on_repo_without_commits(add_data
.sm_path
);
3440 struct strbuf sb
= STRBUF_INIT
;
3441 struct child_process cp
= CHILD_PROCESS_INIT
;
3444 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3445 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3446 if ((exit_code
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3447 strbuf_complete_line(&sb
);
3448 fputs(sb
.buf
, stderr
);
3449 free(add_data
.sm_path
);
3452 strbuf_release(&sb
);
3455 if(!add_data
.sm_name
)
3456 add_data
.sm_name
= add_data
.sm_path
;
3458 if (check_submodule_name(add_data
.sm_name
))
3459 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3461 add_data
.prefix
= prefix
;
3462 add_data
.force
= !!force
;
3463 add_data
.quiet
= !!quiet
;
3464 add_data
.progress
= !!progress
;
3465 add_data
.dissociate
= !!dissociate
;
3467 if (add_submodule(&add_data
)) {
3468 free(add_data
.sm_path
);
3471 configure_added_submodule(&add_data
);
3472 free(add_data
.sm_path
);
3478 #define SUPPORT_SUPER_PREFIX (1<<0)
3482 int (*fn
)(int, const char **, const char *);
3486 static struct cmd_struct commands
[] = {
3487 {"list", module_list
, 0},
3488 {"name", module_name
, 0},
3489 {"clone", module_clone
, 0},
3490 {"add", module_add
, SUPPORT_SUPER_PREFIX
},
3491 {"update", module_update
, 0},
3492 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
3493 {"foreach", module_foreach
, SUPPORT_SUPER_PREFIX
},
3494 {"init", module_init
, SUPPORT_SUPER_PREFIX
},
3495 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
3496 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
3497 {"deinit", module_deinit
, 0},
3498 {"summary", module_summary
, SUPPORT_SUPER_PREFIX
},
3499 {"push-check", push_check
, 0},
3500 {"absorb-git-dirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
3501 {"is-active", is_active
, 0},
3502 {"check-name", check_name
, 0},
3503 {"config", module_config
, 0},
3504 {"set-url", module_set_url
, 0},
3505 {"set-branch", module_set_branch
, 0},
3506 {"create-branch", module_create_branch
, 0},
3509 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3512 if (argc
< 2 || !strcmp(argv
[1], "-h"))
3513 usage("git submodule--helper <command>");
3515 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
3516 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
3517 if (get_super_prefix() &&
3518 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
3519 die(_("%s doesn't support --super-prefix"),
3521 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
3525 die(_("'%s' is not a valid submodule--helper "
3526 "subcommand"), argv
[1]);