2 #include "repository.h"
5 #include "parse-options.h"
10 #include "submodule-config.h"
11 #include "string-list.h"
12 #include "run-command.h"
20 #include "object-store.h"
22 #define OPT_QUIET (1 << 0)
23 #define OPT_CACHED (1 << 1)
24 #define OPT_RECURSIVE (1 << 2)
25 #define OPT_FORCE (1 << 3)
27 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
30 static char *get_default_remote(void)
32 char *dest
= NULL
, *ret
;
33 struct strbuf sb
= STRBUF_INIT
;
34 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
37 die(_("No such ref: %s"), "HEAD");
40 if (!strcmp(refname
, "HEAD"))
41 return xstrdup("origin");
43 if (!skip_prefix(refname
, "refs/heads/", &refname
))
44 die(_("Expecting a full ref name, got %s"), refname
);
46 strbuf_addf(&sb
, "branch.%s.remote", refname
);
47 if (git_config_get_string(sb
.buf
, &dest
))
48 ret
= xstrdup("origin");
56 static int print_default_remote(int argc
, const char **argv
, const char *prefix
)
61 die(_("submodule--helper print-default-remote takes no arguments"));
63 remote
= get_default_remote();
65 printf("%s\n", remote
);
70 static int starts_with_dot_slash(const char *str
)
72 return str
[0] == '.' && is_dir_sep(str
[1]);
75 static int starts_with_dot_dot_slash(const char *str
)
77 return str
[0] == '.' && str
[1] == '.' && is_dir_sep(str
[2]);
81 * Returns 1 if it was the last chop before ':'.
83 static int chop_last_dir(char **remoteurl
, int is_relative
)
85 char *rfind
= find_last_dir_sep(*remoteurl
);
91 rfind
= strrchr(*remoteurl
, ':');
97 if (is_relative
|| !strcmp(".", *remoteurl
))
98 die(_("cannot strip one component off url '%s'"),
102 *remoteurl
= xstrdup(".");
107 * The `url` argument is the URL that navigates to the submodule origin
108 * repo. When relative, this URL is relative to the superproject origin
109 * URL repo. The `up_path` argument, if specified, is the relative
110 * path that navigates from the submodule working tree to the superproject
111 * working tree. Returns the origin URL of the submodule.
113 * Return either an absolute URL or filesystem path (if the superproject
114 * origin URL is an absolute URL or filesystem path, respectively) or a
115 * relative file system path (if the superproject origin URL is a relative
118 * When the output is a relative file system path, the path is either
119 * relative to the submodule working tree, if up_path is specified, or to
120 * the superproject working tree otherwise.
122 * NEEDSWORK: This works incorrectly on the domain and protocol part.
123 * remote_url url outcome expectation
124 * http://a.com/b ../c http://a.com/c as is
125 * http://a.com/b/ ../c http://a.com/c same as previous line, but
126 * ignore trailing slash in url
127 * http://a.com/b ../../c http://c error out
128 * http://a.com/b ../../../c http:/c error out
129 * http://a.com/b ../../../../c http:c error out
130 * http://a.com/b ../../../../../c .:c error out
131 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
132 * when a local part has a colon in its path component, too.
134 static char *relative_url(const char *remote_url
,
141 char *remoteurl
= xstrdup(remote_url
);
142 struct strbuf sb
= STRBUF_INIT
;
143 size_t len
= strlen(remoteurl
);
145 if (is_dir_sep(remoteurl
[len
-1]))
146 remoteurl
[len
-1] = '\0';
148 if (!url_is_local_not_ssh(remoteurl
) || is_absolute_path(remoteurl
))
153 * Prepend a './' to ensure all relative
154 * remoteurls start with './' or '../'
156 if (!starts_with_dot_slash(remoteurl
) &&
157 !starts_with_dot_dot_slash(remoteurl
)) {
159 strbuf_addf(&sb
, "./%s", remoteurl
);
161 remoteurl
= strbuf_detach(&sb
, NULL
);
165 * When the url starts with '../', remove that and the
166 * last directory in remoteurl.
169 if (starts_with_dot_dot_slash(url
)) {
171 colonsep
|= chop_last_dir(&remoteurl
, is_relative
);
172 } else if (starts_with_dot_slash(url
))
178 strbuf_addf(&sb
, "%s%s%s", remoteurl
, colonsep
? ":" : "/", url
);
179 if (ends_with(url
, "/"))
180 strbuf_setlen(&sb
, sb
.len
- 1);
183 if (starts_with_dot_slash(sb
.buf
))
184 out
= xstrdup(sb
.buf
+ 2);
186 out
= xstrdup(sb
.buf
);
189 if (!up_path
|| !is_relative
)
192 strbuf_addf(&sb
, "%s%s", up_path
, out
);
194 return strbuf_detach(&sb
, NULL
);
197 static int resolve_relative_url(int argc
, const char **argv
, const char *prefix
)
199 char *remoteurl
= NULL
;
200 char *remote
= get_default_remote();
201 const char *up_path
= NULL
;
204 struct strbuf sb
= STRBUF_INIT
;
206 if (argc
!= 2 && argc
!= 3)
207 die("resolve-relative-url only accepts one or two arguments");
210 strbuf_addf(&sb
, "remote.%s.url", remote
);
213 if (git_config_get_string(sb
.buf
, &remoteurl
))
214 /* the repository is its own authoritative upstream */
215 remoteurl
= xgetcwd();
220 res
= relative_url(remoteurl
, url
, up_path
);
227 static int resolve_relative_url_test(int argc
, const char **argv
, const char *prefix
)
229 char *remoteurl
, *res
;
230 const char *up_path
, *url
;
233 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
236 remoteurl
= xstrdup(argv
[2]);
239 if (!strcmp(up_path
, "(null)"))
242 res
= relative_url(remoteurl
, url
, up_path
);
249 /* the result should be freed by the caller. */
250 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
252 const char *super_prefix
= get_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 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
271 struct strbuf sb
= STRBUF_INIT
;
274 static const char *describe_bare
[] = { NULL
};
276 static const char *describe_tags
[] = { "--tags", NULL
};
278 static const char *describe_contains
[] = { "--contains", NULL
};
280 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
282 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
284 describe_all_always
, NULL
};
286 for (d
= describe_argv
; *d
; d
++) {
287 struct child_process cp
= CHILD_PROCESS_INIT
;
288 prepare_submodule_repo_env(&cp
.env_array
);
293 argv_array_push(&cp
.args
, "describe");
294 argv_array_pushv(&cp
.args
, *d
);
295 argv_array_push(&cp
.args
, object_id
);
297 if (!capture_command(&cp
, &sb
, 0)) {
298 strbuf_strip_suffix(&sb
, "\n");
299 return strbuf_detach(&sb
, NULL
);
308 const struct cache_entry
**entries
;
311 #define MODULE_LIST_INIT { NULL, 0, 0 }
313 static int module_list_compute(int argc
, const char **argv
,
315 struct pathspec
*pathspec
,
316 struct module_list
*list
)
319 char *ps_matched
= NULL
;
320 parse_pathspec(pathspec
, 0,
321 PATHSPEC_PREFER_FULL
,
325 ps_matched
= xcalloc(pathspec
->nr
, 1);
327 if (read_cache() < 0)
328 die(_("index file corrupt"));
330 for (i
= 0; i
< active_nr
; i
++) {
331 const struct cache_entry
*ce
= active_cache
[i
];
333 if (!match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
),
335 !S_ISGITLINK(ce
->ce_mode
))
338 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
339 list
->entries
[list
->nr
++] = ce
;
340 while (i
+ 1 < active_nr
&&
341 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
343 * Skip entries with the same name in different stages
344 * to make sure an entry is returned only once.
349 if (ps_matched
&& report_path_error(ps_matched
, pathspec
, prefix
))
357 static void module_list_active(struct module_list
*list
)
360 struct module_list active_modules
= MODULE_LIST_INIT
;
362 for (i
= 0; i
< list
->nr
; i
++) {
363 const struct cache_entry
*ce
= list
->entries
[i
];
365 if (!is_submodule_active(the_repository
, ce
->name
))
368 ALLOC_GROW(active_modules
.entries
,
369 active_modules
.nr
+ 1,
370 active_modules
.alloc
);
371 active_modules
.entries
[active_modules
.nr
++] = ce
;
375 *list
= active_modules
;
378 static char *get_up_path(const char *path
)
381 struct strbuf sb
= STRBUF_INIT
;
383 for (i
= count_slashes(path
); i
; i
--)
384 strbuf_addstr(&sb
, "../");
387 * Check if 'path' ends with slash or not
388 * for having the same output for dir/sub_dir
391 if (!is_dir_sep(path
[strlen(path
) - 1]))
392 strbuf_addstr(&sb
, "../");
394 return strbuf_detach(&sb
, NULL
);
397 static int module_list(int argc
, const char **argv
, const char *prefix
)
400 struct pathspec pathspec
;
401 struct module_list list
= MODULE_LIST_INIT
;
403 struct option module_list_options
[] = {
404 OPT_STRING(0, "prefix", &prefix
,
406 N_("alternative anchor for relative paths")),
410 const char *const git_submodule_helper_usage
[] = {
411 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
415 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
416 git_submodule_helper_usage
, 0);
418 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
421 for (i
= 0; i
< list
.nr
; i
++) {
422 const struct cache_entry
*ce
= list
.entries
[i
];
425 printf("%06o %s U\t", ce
->ce_mode
, sha1_to_hex(null_sha1
));
427 printf("%06o %s %d\t", ce
->ce_mode
,
428 oid_to_hex(&ce
->oid
), ce_stage(ce
));
430 fprintf(stdout
, "%s\n", ce
->name
);
435 static void for_each_listed_submodule(const struct module_list
*list
,
436 each_submodule_fn fn
, void *cb_data
)
439 for (i
= 0; i
< list
->nr
; i
++)
440 fn(list
->entries
[i
], cb_data
);
450 #define CB_FOREACH_INIT { 0 }
452 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
455 struct cb_foreach
*info
= cb_data
;
456 const char *path
= list_item
->name
;
457 const struct object_id
*ce_oid
= &list_item
->oid
;
459 const struct submodule
*sub
;
460 struct child_process cp
= CHILD_PROCESS_INIT
;
463 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
465 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
468 die(_("No url found for submodule path '%s' in .gitmodules"),
471 if (!is_submodule_populated_gently(path
, NULL
))
474 prepare_submodule_repo_env(&cp
.env_array
);
477 * For the purpose of executing <command> in the submodule,
478 * separate shell is used for the purpose of running the
485 * NEEDSWORK: the command currently has access to the variables $name,
486 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
487 * contains a single argument. This is done for maintaining a faithful
488 * translation from shell script.
490 if (info
->argc
== 1) {
491 char *toplevel
= xgetcwd();
492 struct strbuf sb
= STRBUF_INIT
;
494 argv_array_pushf(&cp
.env_array
, "name=%s", sub
->name
);
495 argv_array_pushf(&cp
.env_array
, "sm_path=%s", path
);
496 argv_array_pushf(&cp
.env_array
, "displaypath=%s", displaypath
);
497 argv_array_pushf(&cp
.env_array
, "sha1=%s",
499 argv_array_pushf(&cp
.env_array
, "toplevel=%s", toplevel
);
502 * Since the path variable was accessible from the script
503 * before porting, it is also made available after porting.
504 * The environment variable "PATH" has a very special purpose
505 * on windows. And since environment variables are
506 * case-insensitive in windows, it interferes with the
507 * existing PATH variable. Hence, to avoid that, we expose
508 * path via the args argv_array and not via env_array.
510 sq_quote_buf(&sb
, path
);
511 argv_array_pushf(&cp
.args
, "path=%s; %s",
512 sb
.buf
, info
->argv
[0]);
516 argv_array_pushv(&cp
.args
, info
->argv
);
520 printf(_("Entering '%s'\n"), displaypath
);
522 if (info
->argv
[0] && run_command(&cp
))
523 die(_("run_command returned non-zero status for %s\n."),
526 if (info
->recursive
) {
527 struct child_process cpr
= CHILD_PROCESS_INIT
;
531 prepare_submodule_repo_env(&cpr
.env_array
);
533 argv_array_pushl(&cpr
.args
, "--super-prefix", NULL
);
534 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
535 argv_array_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
539 argv_array_push(&cpr
.args
, "--quiet");
541 argv_array_pushv(&cpr
.args
, info
->argv
);
543 if (run_command(&cpr
))
544 die(_("run_command returned non-zero status while"
545 "recursing in the nested submodules of %s\n."),
553 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
555 struct cb_foreach info
= CB_FOREACH_INIT
;
556 struct pathspec pathspec
;
557 struct module_list list
= MODULE_LIST_INIT
;
559 struct option module_foreach_options
[] = {
560 OPT__QUIET(&info
.quiet
, N_("Suppress output of entering each submodule command")),
561 OPT_BOOL(0, "recursive", &info
.recursive
,
562 N_("Recurse into nested submodules")),
566 const char *const git_submodule_helper_usage
[] = {
567 N_("git submodule--helper foreach [--quiet] [--recursive] <command>"),
571 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
572 git_submodule_helper_usage
, PARSE_OPT_KEEP_UNKNOWN
);
574 if (module_list_compute(0, NULL
, prefix
, &pathspec
, &list
) < 0)
579 info
.prefix
= prefix
;
581 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
591 #define INIT_CB_INIT { NULL, 0 }
593 static void init_submodule(const char *path
, const char *prefix
,
596 const struct submodule
*sub
;
597 struct strbuf sb
= STRBUF_INIT
;
598 char *upd
= NULL
, *url
= NULL
, *displaypath
;
600 displaypath
= get_submodule_displaypath(path
, prefix
);
602 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
605 die(_("No url found for submodule path '%s' in .gitmodules"),
609 * NEEDSWORK: In a multi-working-tree world, this needs to be
610 * set in the per-worktree config.
612 * Set active flag for the submodule being initialized
614 if (!is_submodule_active(the_repository
, path
)) {
615 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
616 git_config_set_gently(sb
.buf
, "true");
621 * Copy url setting when it is not set yet.
622 * To look up the url in .git/config, we must not fall back to
623 * .gitmodules, so look it up directly.
625 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
626 if (git_config_get_string(sb
.buf
, &url
)) {
628 die(_("No url found for submodule path '%s' in .gitmodules"),
631 url
= xstrdup(sub
->url
);
633 /* Possibly a url relative to parent */
634 if (starts_with_dot_dot_slash(url
) ||
635 starts_with_dot_slash(url
)) {
636 char *remoteurl
, *relurl
;
637 char *remote
= get_default_remote();
638 struct strbuf remotesb
= STRBUF_INIT
;
639 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
642 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
643 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb
.buf
);
644 remoteurl
= xgetcwd();
646 relurl
= relative_url(remoteurl
, url
, NULL
);
647 strbuf_release(&remotesb
);
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
->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 [<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
);
733 #define STATUS_CB_INIT { NULL, 0 }
735 static void print_status(unsigned int flags
, char state
, const char *path
,
736 const struct object_id
*oid
, const char *displaypath
)
738 if (flags
& OPT_QUIET
)
741 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
743 if (state
== ' ' || state
== '+') {
744 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
747 printf(" (%s)", name
);
753 static int handle_submodule_head_ref(const char *refname
,
754 const struct object_id
*oid
, int flags
,
757 struct object_id
*output
= cb_data
;
764 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
765 unsigned int ce_flags
, const char *prefix
,
769 struct argv_array diff_files_args
= ARGV_ARRAY_INIT
;
771 int diff_files_result
;
773 if (!submodule_from_path(the_repository
, &null_oid
, path
))
774 die(_("no submodule mapping found in .gitmodules for path '%s'"),
777 displaypath
= get_submodule_displaypath(path
, prefix
);
779 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
780 print_status(flags
, 'U', path
, &null_oid
, displaypath
);
784 if (!is_submodule_active(the_repository
, path
)) {
785 print_status(flags
, '-', path
, ce_oid
, displaypath
);
789 argv_array_pushl(&diff_files_args
, "diff-files",
790 "--ignore-submodules=dirty", "--quiet", "--",
793 git_config(git_diff_basic_config
, NULL
);
794 init_revisions(&rev
, prefix
);
796 diff_files_args
.argc
= setup_revisions(diff_files_args
.argc
,
797 diff_files_args
.argv
,
799 diff_files_result
= run_diff_files(&rev
, 0);
801 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
802 print_status(flags
, ' ', path
, ce_oid
,
804 } else if (!(flags
& OPT_CACHED
)) {
805 struct object_id oid
;
806 struct ref_store
*refs
= get_submodule_ref_store(path
);
809 print_status(flags
, '-', path
, ce_oid
, displaypath
);
812 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
813 die(_("could not resolve HEAD ref inside the "
814 "submodule '%s'"), path
);
816 print_status(flags
, '+', path
, &oid
, displaypath
);
818 print_status(flags
, '+', path
, ce_oid
, displaypath
);
821 if (flags
& OPT_RECURSIVE
) {
822 struct child_process cpr
= CHILD_PROCESS_INIT
;
826 prepare_submodule_repo_env(&cpr
.env_array
);
828 argv_array_push(&cpr
.args
, "--super-prefix");
829 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
830 argv_array_pushl(&cpr
.args
, "submodule--helper", "status",
831 "--recursive", NULL
);
833 if (flags
& OPT_CACHED
)
834 argv_array_push(&cpr
.args
, "--cached");
836 if (flags
& OPT_QUIET
)
837 argv_array_push(&cpr
.args
, "--quiet");
839 if (run_command(&cpr
))
840 die(_("failed to recurse into submodule '%s'"), path
);
844 argv_array_clear(&diff_files_args
);
848 static void status_submodule_cb(const struct cache_entry
*list_item
,
851 struct status_cb
*info
= cb_data
;
852 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
853 info
->prefix
, info
->flags
);
856 static int module_status(int argc
, const char **argv
, const char *prefix
)
858 struct status_cb info
= STATUS_CB_INIT
;
859 struct pathspec pathspec
;
860 struct module_list list
= MODULE_LIST_INIT
;
863 struct option module_status_options
[] = {
864 OPT__QUIET(&quiet
, N_("Suppress submodule status output")),
865 OPT_BIT(0, "cached", &info
.flags
, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
866 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
870 const char *const git_submodule_helper_usage
[] = {
871 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
875 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
876 git_submodule_helper_usage
, 0);
878 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
881 info
.prefix
= prefix
;
883 info
.flags
|= OPT_QUIET
;
885 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
890 static int module_name(int argc
, const char **argv
, const char *prefix
)
892 const struct submodule
*sub
;
895 usage(_("git submodule--helper name <path>"));
897 sub
= submodule_from_path(the_repository
, &null_oid
, argv
[1]);
900 die(_("no submodule mapping found in .gitmodules for path '%s'"),
903 printf("%s\n", sub
->name
);
913 #define SYNC_CB_INIT { NULL, 0 }
915 static void sync_submodule(const char *path
, const char *prefix
,
918 const struct submodule
*sub
;
919 char *remote_key
= NULL
;
920 char *sub_origin_url
, *super_config_url
, *displaypath
;
921 struct strbuf sb
= STRBUF_INIT
;
922 struct child_process cp
= CHILD_PROCESS_INIT
;
923 char *sub_config_path
= NULL
;
925 if (!is_submodule_active(the_repository
, path
))
928 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
930 if (sub
&& sub
->url
) {
931 if (starts_with_dot_dot_slash(sub
->url
) ||
932 starts_with_dot_slash(sub
->url
)) {
933 char *remote_url
, *up_path
;
934 char *remote
= get_default_remote();
935 strbuf_addf(&sb
, "remote.%s.url", remote
);
937 if (git_config_get_string(sb
.buf
, &remote_url
))
938 remote_url
= xgetcwd();
940 up_path
= get_up_path(path
);
941 sub_origin_url
= relative_url(remote_url
, sub
->url
, up_path
);
942 super_config_url
= relative_url(remote_url
, sub
->url
, NULL
);
948 sub_origin_url
= xstrdup(sub
->url
);
949 super_config_url
= xstrdup(sub
->url
);
952 sub_origin_url
= xstrdup("");
953 super_config_url
= xstrdup("");
956 displaypath
= get_submodule_displaypath(path
, prefix
);
958 if (!(flags
& OPT_QUIET
))
959 printf(_("Synchronizing submodule url for '%s'\n"),
963 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
964 if (git_config_set_gently(sb
.buf
, super_config_url
))
965 die(_("failed to register url for submodule path '%s'"),
968 if (!is_submodule_populated_gently(path
, NULL
))
971 prepare_submodule_repo_env(&cp
.env_array
);
974 argv_array_pushl(&cp
.args
, "submodule--helper",
975 "print-default-remote", NULL
);
978 if (capture_command(&cp
, &sb
, 0))
979 die(_("failed to get the default remote for submodule '%s'"),
982 strbuf_strip_suffix(&sb
, "\n");
983 remote_key
= xstrfmt("remote.%s.url", sb
.buf
);
986 submodule_to_gitdir(&sb
, path
);
987 strbuf_addstr(&sb
, "/config");
989 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
990 die(_("failed to update remote for submodule '%s'"),
993 if (flags
& OPT_RECURSIVE
) {
994 struct child_process cpr
= CHILD_PROCESS_INIT
;
998 prepare_submodule_repo_env(&cpr
.env_array
);
1000 argv_array_push(&cpr
.args
, "--super-prefix");
1001 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
1002 argv_array_pushl(&cpr
.args
, "submodule--helper", "sync",
1003 "--recursive", NULL
);
1005 if (flags
& OPT_QUIET
)
1006 argv_array_push(&cpr
.args
, "--quiet");
1008 if (run_command(&cpr
))
1009 die(_("failed to recurse into submodule '%s'"),
1014 free(super_config_url
);
1015 free(sub_origin_url
);
1016 strbuf_release(&sb
);
1019 free(sub_config_path
);
1022 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1024 struct sync_cb
*info
= cb_data
;
1025 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1029 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1031 struct sync_cb info
= SYNC_CB_INIT
;
1032 struct pathspec pathspec
;
1033 struct module_list list
= MODULE_LIST_INIT
;
1037 struct option module_sync_options
[] = {
1038 OPT__QUIET(&quiet
, N_("Suppress output of synchronizing submodule url")),
1039 OPT_BOOL(0, "recursive", &recursive
,
1040 N_("Recurse into nested submodules")),
1044 const char *const git_submodule_helper_usage
[] = {
1045 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1049 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1050 git_submodule_helper_usage
, 0);
1052 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1055 info
.prefix
= prefix
;
1057 info
.flags
|= OPT_QUIET
;
1059 info
.flags
|= OPT_RECURSIVE
;
1061 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1070 #define DEINIT_CB_INIT { NULL, 0 }
1072 static void deinit_submodule(const char *path
, const char *prefix
,
1075 const struct submodule
*sub
;
1076 char *displaypath
= NULL
;
1077 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1078 struct strbuf sb_config
= STRBUF_INIT
;
1079 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1081 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
1083 if (!sub
|| !sub
->name
)
1086 displaypath
= get_submodule_displaypath(path
, prefix
);
1088 /* remove the submodule work tree (unless the user already did it) */
1089 if (is_directory(path
)) {
1090 struct strbuf sb_rm
= STRBUF_INIT
;
1094 * protect submodules containing a .git directory
1095 * NEEDSWORK: instead of dying, automatically call
1096 * absorbgitdirs and (possibly) warn.
1098 if (is_directory(sub_git_dir
))
1099 die(_("Submodule work tree '%s' contains a .git "
1100 "directory (use 'rm -rf' if you really want "
1101 "to remove it including all of its history)"),
1104 if (!(flags
& OPT_FORCE
)) {
1105 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1107 argv_array_pushl(&cp_rm
.args
, "rm", "-qn",
1110 if (run_command(&cp_rm
))
1111 die(_("Submodule work tree '%s' contains local "
1112 "modifications; use '-f' to discard them"),
1116 strbuf_addstr(&sb_rm
, path
);
1118 if (!remove_dir_recursively(&sb_rm
, 0))
1119 format
= _("Cleared directory '%s'\n");
1121 format
= _("Could not remove submodule work tree '%s'\n");
1123 if (!(flags
& OPT_QUIET
))
1124 printf(format
, displaypath
);
1126 strbuf_release(&sb_rm
);
1129 if (mkdir(path
, 0777))
1130 printf(_("could not create empty submodule directory %s"),
1133 cp_config
.git_cmd
= 1;
1134 argv_array_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1135 argv_array_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1137 /* remove the .git/config entries (unless the user already did it) */
1138 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1139 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1141 * remove the whole section so we have a clean state when
1142 * the user later decides to init this submodule again
1144 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1145 if (!(flags
& OPT_QUIET
))
1146 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1147 sub
->name
, sub
->url
, displaypath
);
1154 strbuf_release(&sb_config
);
1157 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1160 struct deinit_cb
*info
= cb_data
;
1161 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1164 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1166 struct deinit_cb info
= DEINIT_CB_INIT
;
1167 struct pathspec pathspec
;
1168 struct module_list list
= MODULE_LIST_INIT
;
1173 struct option module_deinit_options
[] = {
1174 OPT__QUIET(&quiet
, N_("Suppress submodule status output")),
1175 OPT__FORCE(&force
, N_("Remove submodule working trees even if they contain local changes"), 0),
1176 OPT_BOOL(0, "all", &all
, N_("Unregister all submodules")),
1180 const char *const git_submodule_helper_usage
[] = {
1181 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1185 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1186 git_submodule_helper_usage
, 0);
1189 error("pathspec and --all are incompatible");
1190 usage_with_options(git_submodule_helper_usage
,
1191 module_deinit_options
);
1195 die(_("Use '--all' if you really want to deinitialize all submodules"));
1197 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1200 info
.prefix
= prefix
;
1202 info
.flags
|= OPT_QUIET
;
1204 info
.flags
|= OPT_FORCE
;
1206 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1211 static int clone_submodule(const char *path
, const char *gitdir
, const char *url
,
1212 const char *depth
, struct string_list
*reference
, int dissociate
,
1213 int quiet
, int progress
)
1215 struct child_process cp
= CHILD_PROCESS_INIT
;
1217 argv_array_push(&cp
.args
, "clone");
1218 argv_array_push(&cp
.args
, "--no-checkout");
1220 argv_array_push(&cp
.args
, "--quiet");
1222 argv_array_push(&cp
.args
, "--progress");
1223 if (depth
&& *depth
)
1224 argv_array_pushl(&cp
.args
, "--depth", depth
, NULL
);
1225 if (reference
->nr
) {
1226 struct string_list_item
*item
;
1227 for_each_string_list_item(item
, reference
)
1228 argv_array_pushl(&cp
.args
, "--reference",
1229 item
->string
, NULL
);
1232 argv_array_push(&cp
.args
, "--dissociate");
1233 if (gitdir
&& *gitdir
)
1234 argv_array_pushl(&cp
.args
, "--separate-git-dir", gitdir
, NULL
);
1236 argv_array_push(&cp
.args
, url
);
1237 argv_array_push(&cp
.args
, path
);
1240 prepare_submodule_repo_env(&cp
.env_array
);
1243 return run_command(&cp
);
1246 struct submodule_alternate_setup
{
1247 const char *submodule_name
;
1248 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1249 SUBMODULE_ALTERNATE_ERROR_DIE
,
1250 SUBMODULE_ALTERNATE_ERROR_INFO
,
1251 SUBMODULE_ALTERNATE_ERROR_IGNORE
1253 struct string_list
*reference
;
1255 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1256 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1258 static int add_possible_reference_from_superproject(
1259 struct alternate_object_database
*alt
, void *sas_cb
)
1261 struct submodule_alternate_setup
*sas
= sas_cb
;
1264 * If the alternate object store is another repository, try the
1265 * standard layout with .git/(modules/<name>)+/objects
1267 if (ends_with(alt
->path
, "/objects")) {
1269 struct strbuf sb
= STRBUF_INIT
;
1270 struct strbuf err
= STRBUF_INIT
;
1271 strbuf_add(&sb
, alt
->path
, strlen(alt
->path
) - strlen("objects"));
1274 * We need to end the new path with '/' to mark it as a dir,
1275 * otherwise a submodule name containing '/' will be broken
1276 * as the last part of a missing submodule reference would
1277 * be taken as a file name.
1279 strbuf_addf(&sb
, "modules/%s/", sas
->submodule_name
);
1281 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1283 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1286 switch (sas
->error_mode
) {
1287 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1288 die(_("submodule '%s' cannot add alternate: %s"),
1289 sas
->submodule_name
, err
.buf
);
1290 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1291 fprintf(stderr
, _("submodule '%s' cannot add alternate: %s"),
1292 sas
->submodule_name
, err
.buf
);
1293 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1297 strbuf_release(&sb
);
1303 static void prepare_possible_alternates(const char *sm_name
,
1304 struct string_list
*reference
)
1306 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1307 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1309 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1313 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1315 if (!error_strategy
)
1316 error_strategy
= xstrdup("die");
1318 sas
.submodule_name
= sm_name
;
1319 sas
.reference
= reference
;
1320 if (!strcmp(error_strategy
, "die"))
1321 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1322 else if (!strcmp(error_strategy
, "info"))
1323 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1324 else if (!strcmp(error_strategy
, "ignore"))
1325 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1327 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1329 if (!strcmp(sm_alternate
, "superproject"))
1330 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1331 else if (!strcmp(sm_alternate
, "no"))
1334 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1337 free(error_strategy
);
1340 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1342 const char *name
= NULL
, *url
= NULL
, *depth
= NULL
;
1345 char *p
, *path
= NULL
, *sm_gitdir
;
1346 struct strbuf sb
= STRBUF_INIT
;
1347 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1349 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1351 struct option module_clone_options
[] = {
1352 OPT_STRING(0, "prefix", &prefix
,
1354 N_("alternative anchor for relative paths")),
1355 OPT_STRING(0, "path", &path
,
1357 N_("where the new submodule will be cloned to")),
1358 OPT_STRING(0, "name", &name
,
1360 N_("name of the new submodule")),
1361 OPT_STRING(0, "url", &url
,
1363 N_("url where to clone the submodule from")),
1364 OPT_STRING_LIST(0, "reference", &reference
,
1366 N_("reference repository")),
1367 OPT_BOOL(0, "dissociate", &dissociate
,
1368 N_("use --reference only while cloning")),
1369 OPT_STRING(0, "depth", &depth
,
1371 N_("depth for shallow clones")),
1372 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
1373 OPT_BOOL(0, "progress", &progress
,
1374 N_("force cloning progress")),
1378 const char *const git_submodule_helper_usage
[] = {
1379 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1380 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1381 "--url <url> --path <path>"),
1385 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1386 git_submodule_helper_usage
, 0);
1388 if (argc
|| !url
|| !path
|| !*path
)
1389 usage_with_options(git_submodule_helper_usage
,
1390 module_clone_options
);
1392 strbuf_addf(&sb
, "%s/modules/%s", get_git_dir(), name
);
1393 sm_gitdir
= absolute_pathdup(sb
.buf
);
1396 if (!is_absolute_path(path
)) {
1397 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), path
);
1398 path
= strbuf_detach(&sb
, NULL
);
1400 path
= xstrdup(path
);
1402 if (!file_exists(sm_gitdir
)) {
1403 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1404 die(_("could not create directory '%s'"), sm_gitdir
);
1406 prepare_possible_alternates(name
, &reference
);
1408 if (clone_submodule(path
, sm_gitdir
, url
, depth
, &reference
, dissociate
,
1410 die(_("clone of '%s' into submodule path '%s' failed"),
1413 if (safe_create_leading_directories_const(path
) < 0)
1414 die(_("could not create directory '%s'"), path
);
1415 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1416 unlink_or_warn(sb
.buf
);
1420 connect_work_tree_and_git_dir(path
, sm_gitdir
, 0);
1422 p
= git_pathdup_submodule(path
, "config");
1424 die(_("could not get submodule directory for '%s'"), path
);
1426 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1427 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1429 git_config_set_in_file(p
, "submodule.alternateLocation",
1431 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1433 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1437 free(error_strategy
);
1439 strbuf_release(&sb
);
1446 struct submodule_update_clone
{
1447 /* index into 'list', the list of submodules to look into for cloning */
1449 struct module_list list
;
1450 unsigned warn_if_uninitialized
: 1;
1452 /* update parameter passed via commandline */
1453 struct submodule_update_strategy update
;
1455 /* configuration parameters which are passed on to the children */
1458 int recommend_shallow
;
1459 struct string_list references
;
1462 const char *recursive_prefix
;
1465 /* Machine-readable status lines to be consumed by git-submodule.sh */
1466 struct string_list projectlines
;
1468 /* If we want to stop as fast as possible and return an error */
1469 unsigned quickstop
: 1;
1471 /* failed clones to be retried again */
1472 const struct cache_entry
**failed_clones
;
1473 int failed_clones_nr
, failed_clones_alloc
;
1475 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1476 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
1478 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
1481 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1482 struct strbuf
*out
, const char *displaypath
)
1485 * Only mention uninitialized submodules when their
1486 * paths have been specified.
1488 if (suc
->warn_if_uninitialized
) {
1490 _("Submodule path '%s' not initialized"),
1492 strbuf_addch(out
, '\n');
1494 _("Maybe you want to use 'update --init'?"));
1495 strbuf_addch(out
, '\n');
1500 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1501 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1503 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1504 struct child_process
*child
,
1505 struct submodule_update_clone
*suc
,
1508 const struct submodule
*sub
= NULL
;
1509 const char *url
= NULL
;
1510 const char *update_string
;
1511 enum submodule_update_type update_type
;
1513 struct strbuf displaypath_sb
= STRBUF_INIT
;
1514 struct strbuf sb
= STRBUF_INIT
;
1515 const char *displaypath
= NULL
;
1516 int needs_cloning
= 0;
1519 if (suc
->recursive_prefix
)
1520 strbuf_addf(&sb
, "%s/%s", suc
->recursive_prefix
, ce
->name
);
1522 strbuf_addstr(&sb
, ce
->name
);
1523 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
1524 strbuf_addch(out
, '\n');
1528 sub
= submodule_from_path(the_repository
, &null_oid
, ce
->name
);
1530 if (suc
->recursive_prefix
)
1531 displaypath
= relative_path(suc
->recursive_prefix
,
1532 ce
->name
, &displaypath_sb
);
1534 displaypath
= ce
->name
;
1537 next_submodule_warn_missing(suc
, out
, displaypath
);
1541 key
= xstrfmt("submodule.%s.update", sub
->name
);
1542 if (!repo_config_get_string_const(the_repository
, key
, &update_string
)) {
1543 update_type
= parse_submodule_update_type(update_string
);
1545 update_type
= sub
->update_strategy
.type
;
1549 if (suc
->update
.type
== SM_UPDATE_NONE
1550 || (suc
->update
.type
== SM_UPDATE_UNSPECIFIED
1551 && update_type
== SM_UPDATE_NONE
)) {
1552 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
1553 strbuf_addch(out
, '\n');
1557 /* Check if the submodule has been initialized. */
1558 if (!is_submodule_active(the_repository
, ce
->name
)) {
1559 next_submodule_warn_missing(suc
, out
, displaypath
);
1564 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1565 if (repo_config_get_string_const(the_repository
, sb
.buf
, &url
))
1569 strbuf_addf(&sb
, "%s/.git", ce
->name
);
1570 needs_cloning
= !file_exists(sb
.buf
);
1573 strbuf_addf(&sb
, "%06o %s %d %d\t%s\n", ce
->ce_mode
,
1574 oid_to_hex(&ce
->oid
), ce_stage(ce
),
1575 needs_cloning
, ce
->name
);
1576 string_list_append(&suc
->projectlines
, sb
.buf
);
1582 child
->no_stdin
= 1;
1583 child
->stdout_to_stderr
= 1;
1585 argv_array_push(&child
->args
, "submodule--helper");
1586 argv_array_push(&child
->args
, "clone");
1588 argv_array_push(&child
->args
, "--progress");
1590 argv_array_push(&child
->args
, "--quiet");
1592 argv_array_pushl(&child
->args
, "--prefix", suc
->prefix
, NULL
);
1593 if (suc
->recommend_shallow
&& sub
->recommend_shallow
== 1)
1594 argv_array_push(&child
->args
, "--depth=1");
1595 argv_array_pushl(&child
->args
, "--path", sub
->path
, NULL
);
1596 argv_array_pushl(&child
->args
, "--name", sub
->name
, NULL
);
1597 argv_array_pushl(&child
->args
, "--url", url
, NULL
);
1598 if (suc
->references
.nr
) {
1599 struct string_list_item
*item
;
1600 for_each_string_list_item(item
, &suc
->references
)
1601 argv_array_pushl(&child
->args
, "--reference", item
->string
, NULL
);
1603 if (suc
->dissociate
)
1604 argv_array_push(&child
->args
, "--dissociate");
1606 argv_array_push(&child
->args
, suc
->depth
);
1609 strbuf_reset(&displaypath_sb
);
1612 return needs_cloning
;
1615 static int update_clone_get_next_task(struct child_process
*child
,
1620 struct submodule_update_clone
*suc
= suc_cb
;
1621 const struct cache_entry
*ce
;
1624 for (; suc
->current
< suc
->list
.nr
; suc
->current
++) {
1625 ce
= suc
->list
.entries
[suc
->current
];
1626 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
1627 int *p
= xmalloc(sizeof(*p
));
1636 * The loop above tried cloning each submodule once, now try the
1637 * stragglers again, which we can imagine as an extension of the
1640 index
= suc
->current
- suc
->list
.nr
;
1641 if (index
< suc
->failed_clones_nr
) {
1643 ce
= suc
->failed_clones
[index
];
1644 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
1646 strbuf_addstr(err
, "BUG: submodule considered for "
1647 "cloning, doesn't need cloning "
1651 p
= xmalloc(sizeof(*p
));
1661 static int update_clone_start_failure(struct strbuf
*err
,
1665 struct submodule_update_clone
*suc
= suc_cb
;
1670 static int update_clone_task_finished(int result
,
1675 const struct cache_entry
*ce
;
1676 struct submodule_update_clone
*suc
= suc_cb
;
1678 int *idxP
= idx_task_cb
;
1685 if (idx
< suc
->list
.nr
) {
1686 ce
= suc
->list
.entries
[idx
];
1687 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
1689 strbuf_addch(err
, '\n');
1690 ALLOC_GROW(suc
->failed_clones
,
1691 suc
->failed_clones_nr
+ 1,
1692 suc
->failed_clones_alloc
);
1693 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
1696 idx
-= suc
->list
.nr
;
1697 ce
= suc
->failed_clones
[idx
];
1698 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
1700 strbuf_addch(err
, '\n');
1708 static int gitmodules_update_clone_config(const char *var
, const char *value
,
1712 if (!strcmp(var
, "submodule.fetchjobs"))
1713 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
1717 static int update_clone(int argc
, const char **argv
, const char *prefix
)
1719 const char *update
= NULL
;
1721 struct string_list_item
*item
;
1722 struct pathspec pathspec
;
1723 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
1725 struct option module_update_clone_options
[] = {
1726 OPT_STRING(0, "prefix", &prefix
,
1728 N_("path into the working tree")),
1729 OPT_STRING(0, "recursive-prefix", &suc
.recursive_prefix
,
1731 N_("path into the working tree, across nested "
1732 "submodule boundaries")),
1733 OPT_STRING(0, "update", &update
,
1735 N_("rebase, merge, checkout or none")),
1736 OPT_STRING_LIST(0, "reference", &suc
.references
, N_("repo"),
1737 N_("reference repository")),
1738 OPT_BOOL(0, "dissociate", &suc
.dissociate
,
1739 N_("use --reference only while cloning")),
1740 OPT_STRING(0, "depth", &suc
.depth
, "<depth>",
1741 N_("Create a shallow clone truncated to the "
1742 "specified number of revisions")),
1743 OPT_INTEGER('j', "jobs", &max_jobs
,
1744 N_("parallel jobs")),
1745 OPT_BOOL(0, "recommend-shallow", &suc
.recommend_shallow
,
1746 N_("whether the initial clone should follow the shallow recommendation")),
1747 OPT__QUIET(&suc
.quiet
, N_("don't print cloning progress")),
1748 OPT_BOOL(0, "progress", &suc
.progress
,
1749 N_("force cloning progress")),
1753 const char *const git_submodule_helper_usage
[] = {
1754 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1757 suc
.prefix
= prefix
;
1759 config_from_gitmodules(gitmodules_update_clone_config
, &max_jobs
);
1760 git_config(gitmodules_update_clone_config
, &max_jobs
);
1762 argc
= parse_options(argc
, argv
, prefix
, module_update_clone_options
,
1763 git_submodule_helper_usage
, 0);
1766 if (parse_submodule_update_strategy(update
, &suc
.update
) < 0)
1767 die(_("bad value for update parameter"));
1769 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &suc
.list
) < 0)
1773 suc
.warn_if_uninitialized
= 1;
1775 run_processes_parallel(max_jobs
,
1776 update_clone_get_next_task
,
1777 update_clone_start_failure
,
1778 update_clone_task_finished
,
1782 * We saved the output and put it out all at once now.
1784 * - the listener does not have to interleave their (checkout)
1785 * work with our fetching. The writes involved in a
1786 * checkout involve more straightforward sequential I/O.
1787 * - the listener can avoid doing any work if fetching failed.
1792 for_each_string_list_item(item
, &suc
.projectlines
)
1793 fprintf(stdout
, "%s", item
->string
);
1798 static int resolve_relative_path(int argc
, const char **argv
, const char *prefix
)
1800 struct strbuf sb
= STRBUF_INIT
;
1802 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc
);
1804 printf("%s", relative_path(argv
[1], argv
[2], &sb
));
1805 strbuf_release(&sb
);
1809 static const char *remote_submodule_branch(const char *path
)
1811 const struct submodule
*sub
;
1812 const char *branch
= NULL
;
1815 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
1819 key
= xstrfmt("submodule.%s.branch", sub
->name
);
1820 if (repo_config_get_string_const(the_repository
, key
, &branch
))
1821 branch
= sub
->branch
;
1827 if (!strcmp(branch
, ".")) {
1828 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1831 die(_("No such ref: %s"), "HEAD");
1834 if (!strcmp(refname
, "HEAD"))
1835 die(_("Submodule (%s) branch configured to inherit "
1836 "branch from superproject, but the superproject "
1837 "is not on any branch"), sub
->name
);
1839 if (!skip_prefix(refname
, "refs/heads/", &refname
))
1840 die(_("Expecting a full ref name, got %s"), refname
);
1847 static int resolve_remote_submodule_branch(int argc
, const char **argv
,
1851 struct strbuf sb
= STRBUF_INIT
;
1853 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc
);
1855 ret
= remote_submodule_branch(argv
[1]);
1857 die("submodule %s doesn't exist", argv
[1]);
1860 strbuf_release(&sb
);
1864 static int push_check(int argc
, const char **argv
, const char *prefix
)
1866 struct remote
*remote
;
1867 const char *superproject_head
;
1869 int detached_head
= 0;
1870 struct object_id head_oid
;
1873 die("submodule--helper push-check requires at least 2 arguments");
1876 * superproject's resolved head ref.
1877 * if HEAD then the superproject is in a detached head state, otherwise
1878 * it will be the resolved head ref.
1880 superproject_head
= argv
[1];
1883 /* Get the submodule's head ref and determine if it is detached */
1884 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1886 die(_("Failed to resolve HEAD as a valid ref."));
1887 if (!strcmp(head
, "HEAD"))
1891 * The remote must be configured.
1892 * This is to avoid pushing to the exact same URL as the parent.
1894 remote
= pushremote_get(argv
[1]);
1895 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
1896 die("remote '%s' not configured", argv
[1]);
1898 /* Check the refspec */
1901 struct ref
*local_refs
= get_local_heads();
1902 struct refspec refspec
= REFSPEC_INIT_PUSH
;
1904 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
1906 for (i
= 0; i
< refspec
.nr
; i
++) {
1907 const struct refspec_item
*rs
= &refspec
.items
[i
];
1909 if (rs
->pattern
|| rs
->matching
)
1912 /* LHS must match a single ref */
1913 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
1918 * If LHS matches 'HEAD' then we need to ensure
1919 * that it matches the same named branch
1920 * checked out in the superproject.
1922 if (!strcmp(rs
->src
, "HEAD")) {
1923 if (!detached_head
&&
1924 !strcmp(head
, superproject_head
))
1926 die("HEAD does not match the named branch in the superproject");
1930 die("src refspec '%s' must name a ref",
1934 refspec_clear(&refspec
);
1941 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
1944 struct pathspec pathspec
;
1945 struct module_list list
= MODULE_LIST_INIT
;
1946 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
1948 struct option embed_gitdir_options
[] = {
1949 OPT_STRING(0, "prefix", &prefix
,
1951 N_("path into the working tree")),
1952 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
1953 ABSORB_GITDIR_RECURSE_SUBMODULES
),
1957 const char *const git_submodule_helper_usage
[] = {
1958 N_("git submodule--helper embed-git-dir [<path>...]"),
1962 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
1963 git_submodule_helper_usage
, 0);
1965 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1968 for (i
= 0; i
< list
.nr
; i
++)
1969 absorb_git_dir_into_superproject(prefix
,
1970 list
.entries
[i
]->name
, flags
);
1975 static int is_active(int argc
, const char **argv
, const char *prefix
)
1978 die("submodule--helper is-active takes exactly 1 argument");
1980 return !is_submodule_active(the_repository
, argv
[1]);
1984 * Exit non-zero if any of the submodule names given on the command line is
1985 * invalid. If no names are given, filter stdin to print only valid names
1986 * (which is primarily intended for testing).
1988 static int check_name(int argc
, const char **argv
, const char *prefix
)
1992 if (check_submodule_name(*argv
) < 0)
1996 struct strbuf buf
= STRBUF_INIT
;
1997 while (strbuf_getline(&buf
, stdin
) != EOF
) {
1998 if (!check_submodule_name(buf
.buf
))
1999 printf("%s\n", buf
.buf
);
2001 strbuf_release(&buf
);
2006 #define SUPPORT_SUPER_PREFIX (1<<0)
2010 int (*fn
)(int, const char **, const char *);
2014 static struct cmd_struct commands
[] = {
2015 {"list", module_list
, 0},
2016 {"name", module_name
, 0},
2017 {"clone", module_clone
, 0},
2018 {"update-clone", update_clone
, 0},
2019 {"relative-path", resolve_relative_path
, 0},
2020 {"resolve-relative-url", resolve_relative_url
, 0},
2021 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
2022 {"foreach", module_foreach
, SUPPORT_SUPER_PREFIX
},
2023 {"init", module_init
, SUPPORT_SUPER_PREFIX
},
2024 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
2025 {"print-default-remote", print_default_remote
, 0},
2026 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
2027 {"deinit", module_deinit
, 0},
2028 {"remote-branch", resolve_remote_submodule_branch
, 0},
2029 {"push-check", push_check
, 0},
2030 {"absorb-git-dirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
2031 {"is-active", is_active
, 0},
2032 {"check-name", check_name
, 0},
2035 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
2038 if (argc
< 2 || !strcmp(argv
[1], "-h"))
2039 usage("git submodule--helper <command>");
2041 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
2042 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
2043 if (get_super_prefix() &&
2044 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
2045 die(_("%s doesn't support --super-prefix"),
2047 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
2051 die(_("'%s' is not a valid submodule--helper "
2052 "subcommand"), argv
[1]);