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 #define OPT_QUIET (1 << 0)
25 #define OPT_CACHED (1 << 1)
26 #define OPT_RECURSIVE (1 << 2)
27 #define OPT_FORCE (1 << 3)
29 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
32 static char *get_default_remote(void)
34 char *dest
= NULL
, *ret
;
35 struct strbuf sb
= STRBUF_INIT
;
36 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
39 die(_("No such ref: %s"), "HEAD");
42 if (!strcmp(refname
, "HEAD"))
43 return xstrdup("origin");
45 if (!skip_prefix(refname
, "refs/heads/", &refname
))
46 die(_("Expecting a full ref name, got %s"), refname
);
48 strbuf_addf(&sb
, "branch.%s.remote", refname
);
49 if (git_config_get_string(sb
.buf
, &dest
))
50 ret
= xstrdup("origin");
58 static int print_default_remote(int argc
, const char **argv
, const char *prefix
)
63 die(_("submodule--helper print-default-remote takes no arguments"));
65 remote
= get_default_remote();
67 printf("%s\n", remote
);
73 static int starts_with_dot_slash(const char *str
)
75 return str
[0] == '.' && is_dir_sep(str
[1]);
78 static int starts_with_dot_dot_slash(const char *str
)
80 return str
[0] == '.' && str
[1] == '.' && is_dir_sep(str
[2]);
84 * Returns 1 if it was the last chop before ':'.
86 static int chop_last_dir(char **remoteurl
, int is_relative
)
88 char *rfind
= find_last_dir_sep(*remoteurl
);
94 rfind
= strrchr(*remoteurl
, ':');
100 if (is_relative
|| !strcmp(".", *remoteurl
))
101 die(_("cannot strip one component off url '%s'"),
105 *remoteurl
= xstrdup(".");
110 * The `url` argument is the URL that navigates to the submodule origin
111 * repo. When relative, this URL is relative to the superproject origin
112 * URL repo. The `up_path` argument, if specified, is the relative
113 * path that navigates from the submodule working tree to the superproject
114 * working tree. Returns the origin URL of the submodule.
116 * Return either an absolute URL or filesystem path (if the superproject
117 * origin URL is an absolute URL or filesystem path, respectively) or a
118 * relative file system path (if the superproject origin URL is a relative
121 * When the output is a relative file system path, the path is either
122 * relative to the submodule working tree, if up_path is specified, or to
123 * the superproject working tree otherwise.
125 * NEEDSWORK: This works incorrectly on the domain and protocol part.
126 * remote_url url outcome expectation
127 * http://a.com/b ../c http://a.com/c as is
128 * http://a.com/b/ ../c http://a.com/c same as previous line, but
129 * ignore trailing slash in url
130 * http://a.com/b ../../c http://c error out
131 * http://a.com/b ../../../c http:/c error out
132 * http://a.com/b ../../../../c http:c error out
133 * http://a.com/b ../../../../../c .:c error out
134 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
135 * when a local part has a colon in its path component, too.
137 static char *relative_url(const char *remote_url
,
144 char *remoteurl
= xstrdup(remote_url
);
145 struct strbuf sb
= STRBUF_INIT
;
146 size_t len
= strlen(remoteurl
);
148 if (is_dir_sep(remoteurl
[len
-1]))
149 remoteurl
[len
-1] = '\0';
151 if (!url_is_local_not_ssh(remoteurl
) || is_absolute_path(remoteurl
))
156 * Prepend a './' to ensure all relative
157 * remoteurls start with './' or '../'
159 if (!starts_with_dot_slash(remoteurl
) &&
160 !starts_with_dot_dot_slash(remoteurl
)) {
162 strbuf_addf(&sb
, "./%s", remoteurl
);
164 remoteurl
= strbuf_detach(&sb
, NULL
);
168 * When the url starts with '../', remove that and the
169 * last directory in remoteurl.
172 if (starts_with_dot_dot_slash(url
)) {
174 colonsep
|= chop_last_dir(&remoteurl
, is_relative
);
175 } else if (starts_with_dot_slash(url
))
181 strbuf_addf(&sb
, "%s%s%s", remoteurl
, colonsep
? ":" : "/", url
);
182 if (ends_with(url
, "/"))
183 strbuf_setlen(&sb
, sb
.len
- 1);
186 if (starts_with_dot_slash(sb
.buf
))
187 out
= xstrdup(sb
.buf
+ 2);
189 out
= xstrdup(sb
.buf
);
192 if (!up_path
|| !is_relative
)
195 strbuf_addf(&sb
, "%s%s", up_path
, out
);
197 return strbuf_detach(&sb
, NULL
);
200 static int resolve_relative_url(int argc
, const char **argv
, const char *prefix
)
202 char *remoteurl
= NULL
;
203 char *remote
= get_default_remote();
204 const char *up_path
= NULL
;
207 struct strbuf sb
= STRBUF_INIT
;
209 if (argc
!= 2 && argc
!= 3)
210 die("resolve-relative-url only accepts one or two arguments");
213 strbuf_addf(&sb
, "remote.%s.url", remote
);
216 if (git_config_get_string(sb
.buf
, &remoteurl
))
217 /* the repository is its own authoritative upstream */
218 remoteurl
= xgetcwd();
223 res
= relative_url(remoteurl
, url
, up_path
);
230 static int resolve_relative_url_test(int argc
, const char **argv
, const char *prefix
)
232 char *remoteurl
, *res
;
233 const char *up_path
, *url
;
236 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
239 remoteurl
= xstrdup(argv
[2]);
242 if (!strcmp(up_path
, "(null)"))
245 res
= relative_url(remoteurl
, url
, up_path
);
252 /* the result should be freed by the caller. */
253 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
255 const char *super_prefix
= get_super_prefix();
257 if (prefix
&& super_prefix
) {
258 BUG("cannot have prefix '%s' and superprefix '%s'",
259 prefix
, super_prefix
);
261 struct strbuf sb
= STRBUF_INIT
;
262 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
265 } else if (super_prefix
) {
266 return xstrfmt("%s%s", super_prefix
, path
);
268 return xstrdup(path
);
272 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
274 struct strbuf sb
= STRBUF_INIT
;
277 static const char *describe_bare
[] = { NULL
};
279 static const char *describe_tags
[] = { "--tags", NULL
};
281 static const char *describe_contains
[] = { "--contains", NULL
};
283 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
285 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
287 describe_all_always
, NULL
};
289 for (d
= describe_argv
; *d
; d
++) {
290 struct child_process cp
= CHILD_PROCESS_INIT
;
291 prepare_submodule_repo_env(&cp
.env_array
);
296 argv_array_push(&cp
.args
, "describe");
297 argv_array_pushv(&cp
.args
, *d
);
298 argv_array_push(&cp
.args
, object_id
);
300 if (!capture_command(&cp
, &sb
, 0)) {
301 strbuf_strip_suffix(&sb
, "\n");
302 return strbuf_detach(&sb
, NULL
);
311 const struct cache_entry
**entries
;
314 #define MODULE_LIST_INIT { NULL, 0, 0 }
316 static int module_list_compute(int argc
, const char **argv
,
318 struct pathspec
*pathspec
,
319 struct module_list
*list
)
322 char *ps_matched
= NULL
;
323 parse_pathspec(pathspec
, 0,
324 PATHSPEC_PREFER_FULL
,
328 ps_matched
= xcalloc(pathspec
->nr
, 1);
330 if (read_cache() < 0)
331 die(_("index file corrupt"));
333 for (i
= 0; i
< active_nr
; i
++) {
334 const struct cache_entry
*ce
= active_cache
[i
];
336 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
338 !S_ISGITLINK(ce
->ce_mode
))
341 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
342 list
->entries
[list
->nr
++] = ce
;
343 while (i
+ 1 < active_nr
&&
344 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
346 * Skip entries with the same name in different stages
347 * to make sure an entry is returned only once.
352 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
360 static void module_list_active(struct module_list
*list
)
363 struct module_list active_modules
= MODULE_LIST_INIT
;
365 for (i
= 0; i
< list
->nr
; i
++) {
366 const struct cache_entry
*ce
= list
->entries
[i
];
368 if (!is_submodule_active(the_repository
, ce
->name
))
371 ALLOC_GROW(active_modules
.entries
,
372 active_modules
.nr
+ 1,
373 active_modules
.alloc
);
374 active_modules
.entries
[active_modules
.nr
++] = ce
;
378 *list
= active_modules
;
381 static char *get_up_path(const char *path
)
384 struct strbuf sb
= STRBUF_INIT
;
386 for (i
= count_slashes(path
); i
; i
--)
387 strbuf_addstr(&sb
, "../");
390 * Check if 'path' ends with slash or not
391 * for having the same output for dir/sub_dir
394 if (!is_dir_sep(path
[strlen(path
) - 1]))
395 strbuf_addstr(&sb
, "../");
397 return strbuf_detach(&sb
, NULL
);
400 static int module_list(int argc
, const char **argv
, const char *prefix
)
403 struct pathspec pathspec
;
404 struct module_list list
= MODULE_LIST_INIT
;
406 struct option module_list_options
[] = {
407 OPT_STRING(0, "prefix", &prefix
,
409 N_("alternative anchor for relative paths")),
413 const char *const git_submodule_helper_usage
[] = {
414 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
418 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
419 git_submodule_helper_usage
, 0);
421 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
424 for (i
= 0; i
< list
.nr
; i
++) {
425 const struct cache_entry
*ce
= list
.entries
[i
];
428 printf("%06o %s U\t", ce
->ce_mode
, oid_to_hex(&null_oid
));
430 printf("%06o %s %d\t", ce
->ce_mode
,
431 oid_to_hex(&ce
->oid
), ce_stage(ce
));
433 fprintf(stdout
, "%s\n", ce
->name
);
438 static void for_each_listed_submodule(const struct module_list
*list
,
439 each_submodule_fn fn
, void *cb_data
)
442 for (i
= 0; i
< list
->nr
; i
++)
443 fn(list
->entries
[i
], cb_data
);
453 #define CB_FOREACH_INIT { 0 }
455 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
458 struct cb_foreach
*info
= cb_data
;
459 const char *path
= list_item
->name
;
460 const struct object_id
*ce_oid
= &list_item
->oid
;
462 const struct submodule
*sub
;
463 struct child_process cp
= CHILD_PROCESS_INIT
;
466 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
468 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
471 die(_("No url found for submodule path '%s' in .gitmodules"),
474 if (!is_submodule_populated_gently(path
, NULL
))
477 prepare_submodule_repo_env(&cp
.env_array
);
480 * For the purpose of executing <command> in the submodule,
481 * separate shell is used for the purpose of running the
488 * NEEDSWORK: the command currently has access to the variables $name,
489 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
490 * contains a single argument. This is done for maintaining a faithful
491 * translation from shell script.
493 if (info
->argc
== 1) {
494 char *toplevel
= xgetcwd();
495 struct strbuf sb
= STRBUF_INIT
;
497 argv_array_pushf(&cp
.env_array
, "name=%s", sub
->name
);
498 argv_array_pushf(&cp
.env_array
, "sm_path=%s", path
);
499 argv_array_pushf(&cp
.env_array
, "displaypath=%s", displaypath
);
500 argv_array_pushf(&cp
.env_array
, "sha1=%s",
502 argv_array_pushf(&cp
.env_array
, "toplevel=%s", toplevel
);
505 * Since the path variable was accessible from the script
506 * before porting, it is also made available after porting.
507 * The environment variable "PATH" has a very special purpose
508 * on windows. And since environment variables are
509 * case-insensitive in windows, it interferes with the
510 * existing PATH variable. Hence, to avoid that, we expose
511 * path via the args argv_array and not via env_array.
513 sq_quote_buf(&sb
, path
);
514 argv_array_pushf(&cp
.args
, "path=%s; %s",
515 sb
.buf
, info
->argv
[0]);
519 argv_array_pushv(&cp
.args
, info
->argv
);
523 printf(_("Entering '%s'\n"), displaypath
);
525 if (info
->argv
[0] && run_command(&cp
))
526 die(_("run_command returned non-zero status for %s\n."),
529 if (info
->recursive
) {
530 struct child_process cpr
= CHILD_PROCESS_INIT
;
534 prepare_submodule_repo_env(&cpr
.env_array
);
536 argv_array_pushl(&cpr
.args
, "--super-prefix", NULL
);
537 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
538 argv_array_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
542 argv_array_push(&cpr
.args
, "--quiet");
544 argv_array_push(&cpr
.args
, "--");
545 argv_array_pushv(&cpr
.args
, info
->argv
);
547 if (run_command(&cpr
))
548 die(_("run_command returned non-zero status while "
549 "recursing in the nested submodules of %s\n."),
557 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
559 struct cb_foreach info
= CB_FOREACH_INIT
;
560 struct pathspec pathspec
;
561 struct module_list list
= MODULE_LIST_INIT
;
563 struct option module_foreach_options
[] = {
564 OPT__QUIET(&info
.quiet
, N_("Suppress output of entering each submodule command")),
565 OPT_BOOL(0, "recursive", &info
.recursive
,
566 N_("Recurse into nested submodules")),
570 const char *const git_submodule_helper_usage
[] = {
571 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
575 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
576 git_submodule_helper_usage
, 0);
578 if (module_list_compute(0, NULL
, prefix
, &pathspec
, &list
) < 0)
583 info
.prefix
= prefix
;
585 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
590 static char *compute_submodule_clone_url(const char *rel_url
)
592 char *remoteurl
, *relurl
;
593 char *remote
= get_default_remote();
594 struct strbuf remotesb
= STRBUF_INIT
;
596 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
597 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
598 warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb
.buf
);
599 remoteurl
= xgetcwd();
601 relurl
= relative_url(remoteurl
, rel_url
, NULL
);
605 strbuf_release(&remotesb
);
615 #define INIT_CB_INIT { NULL, 0 }
617 static void init_submodule(const char *path
, const char *prefix
,
620 const struct submodule
*sub
;
621 struct strbuf sb
= STRBUF_INIT
;
622 char *upd
= NULL
, *url
= NULL
, *displaypath
;
624 displaypath
= get_submodule_displaypath(path
, prefix
);
626 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
629 die(_("No url found for submodule path '%s' in .gitmodules"),
633 * NEEDSWORK: In a multi-working-tree world, this needs to be
634 * set in the per-worktree config.
636 * Set active flag for the submodule being initialized
638 if (!is_submodule_active(the_repository
, path
)) {
639 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
640 git_config_set_gently(sb
.buf
, "true");
645 * Copy url setting when it is not set yet.
646 * To look up the url in .git/config, we must not fall back to
647 * .gitmodules, so look it up directly.
649 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
650 if (git_config_get_string(sb
.buf
, &url
)) {
652 die(_("No url found for submodule path '%s' in .gitmodules"),
655 url
= xstrdup(sub
->url
);
657 /* Possibly a url relative to parent */
658 if (starts_with_dot_dot_slash(url
) ||
659 starts_with_dot_slash(url
)) {
661 url
= compute_submodule_clone_url(oldurl
);
665 if (git_config_set_gently(sb
.buf
, url
))
666 die(_("Failed to register url for submodule path '%s'"),
668 if (!(flags
& OPT_QUIET
))
670 _("Submodule '%s' (%s) registered for path '%s'\n"),
671 sub
->name
, url
, displaypath
);
675 /* Copy "update" setting when it is not set yet */
676 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
677 if (git_config_get_string(sb
.buf
, &upd
) &&
678 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
679 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
680 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
682 upd
= xstrdup("none");
684 upd
= xstrdup(submodule_strategy_to_string(&sub
->update_strategy
));
686 if (git_config_set_gently(sb
.buf
, upd
))
687 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
695 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
697 struct init_cb
*info
= cb_data
;
698 init_submodule(list_item
->name
, info
->prefix
, info
->flags
);
701 static int module_init(int argc
, const char **argv
, const char *prefix
)
703 struct init_cb info
= INIT_CB_INIT
;
704 struct pathspec pathspec
;
705 struct module_list list
= MODULE_LIST_INIT
;
708 struct option module_init_options
[] = {
709 OPT__QUIET(&quiet
, N_("Suppress output for initializing a submodule")),
713 const char *const git_submodule_helper_usage
[] = {
714 N_("git submodule--helper init [<options>] [<path>]"),
718 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
719 git_submodule_helper_usage
, 0);
721 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
725 * If there are no path args and submodule.active is set then,
726 * by default, only initialize 'active' modules.
728 if (!argc
&& git_config_get_value_multi("submodule.active"))
729 module_list_active(&list
);
731 info
.prefix
= prefix
;
733 info
.flags
|= OPT_QUIET
;
735 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
745 #define STATUS_CB_INIT { NULL, 0 }
747 static void print_status(unsigned int flags
, char state
, const char *path
,
748 const struct object_id
*oid
, const char *displaypath
)
750 if (flags
& OPT_QUIET
)
753 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
755 if (state
== ' ' || state
== '+') {
756 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
759 printf(" (%s)", name
);
765 static int handle_submodule_head_ref(const char *refname
,
766 const struct object_id
*oid
, int flags
,
769 struct object_id
*output
= cb_data
;
776 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
777 unsigned int ce_flags
, const char *prefix
,
781 struct argv_array diff_files_args
= ARGV_ARRAY_INIT
;
783 int diff_files_result
;
785 if (!submodule_from_path(the_repository
, &null_oid
, path
))
786 die(_("no submodule mapping found in .gitmodules for path '%s'"),
789 displaypath
= get_submodule_displaypath(path
, prefix
);
791 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
792 print_status(flags
, 'U', path
, &null_oid
, displaypath
);
796 if (!is_submodule_active(the_repository
, path
)) {
797 print_status(flags
, '-', path
, ce_oid
, displaypath
);
801 argv_array_pushl(&diff_files_args
, "diff-files",
802 "--ignore-submodules=dirty", "--quiet", "--",
805 git_config(git_diff_basic_config
, NULL
);
806 repo_init_revisions(the_repository
, &rev
, prefix
);
808 diff_files_args
.argc
= setup_revisions(diff_files_args
.argc
,
809 diff_files_args
.argv
,
811 diff_files_result
= run_diff_files(&rev
, 0);
813 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
814 print_status(flags
, ' ', path
, ce_oid
,
816 } else if (!(flags
& OPT_CACHED
)) {
817 struct object_id oid
;
818 struct ref_store
*refs
= get_submodule_ref_store(path
);
821 print_status(flags
, '-', path
, ce_oid
, displaypath
);
824 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
825 die(_("could not resolve HEAD ref inside the "
826 "submodule '%s'"), path
);
828 print_status(flags
, '+', path
, &oid
, displaypath
);
830 print_status(flags
, '+', path
, ce_oid
, displaypath
);
833 if (flags
& OPT_RECURSIVE
) {
834 struct child_process cpr
= CHILD_PROCESS_INIT
;
838 prepare_submodule_repo_env(&cpr
.env_array
);
840 argv_array_push(&cpr
.args
, "--super-prefix");
841 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
842 argv_array_pushl(&cpr
.args
, "submodule--helper", "status",
843 "--recursive", NULL
);
845 if (flags
& OPT_CACHED
)
846 argv_array_push(&cpr
.args
, "--cached");
848 if (flags
& OPT_QUIET
)
849 argv_array_push(&cpr
.args
, "--quiet");
851 if (run_command(&cpr
))
852 die(_("failed to recurse into submodule '%s'"), path
);
856 argv_array_clear(&diff_files_args
);
860 static void status_submodule_cb(const struct cache_entry
*list_item
,
863 struct status_cb
*info
= cb_data
;
864 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
865 info
->prefix
, info
->flags
);
868 static int module_status(int argc
, const char **argv
, const char *prefix
)
870 struct status_cb info
= STATUS_CB_INIT
;
871 struct pathspec pathspec
;
872 struct module_list list
= MODULE_LIST_INIT
;
875 struct option module_status_options
[] = {
876 OPT__QUIET(&quiet
, N_("Suppress submodule status output")),
877 OPT_BIT(0, "cached", &info
.flags
, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
878 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
882 const char *const git_submodule_helper_usage
[] = {
883 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
887 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
888 git_submodule_helper_usage
, 0);
890 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
893 info
.prefix
= prefix
;
895 info
.flags
|= OPT_QUIET
;
897 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
902 static int module_name(int argc
, const char **argv
, const char *prefix
)
904 const struct submodule
*sub
;
907 usage(_("git submodule--helper name <path>"));
909 sub
= submodule_from_path(the_repository
, &null_oid
, argv
[1]);
912 die(_("no submodule mapping found in .gitmodules for path '%s'"),
915 printf("%s\n", sub
->name
);
925 #define SYNC_CB_INIT { NULL, 0 }
927 static void sync_submodule(const char *path
, const char *prefix
,
930 const struct submodule
*sub
;
931 char *remote_key
= NULL
;
932 char *sub_origin_url
, *super_config_url
, *displaypath
;
933 struct strbuf sb
= STRBUF_INIT
;
934 struct child_process cp
= CHILD_PROCESS_INIT
;
935 char *sub_config_path
= NULL
;
937 if (!is_submodule_active(the_repository
, path
))
940 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
942 if (sub
&& sub
->url
) {
943 if (starts_with_dot_dot_slash(sub
->url
) ||
944 starts_with_dot_slash(sub
->url
)) {
945 char *remote_url
, *up_path
;
946 char *remote
= get_default_remote();
947 strbuf_addf(&sb
, "remote.%s.url", remote
);
949 if (git_config_get_string(sb
.buf
, &remote_url
))
950 remote_url
= xgetcwd();
952 up_path
= get_up_path(path
);
953 sub_origin_url
= relative_url(remote_url
, sub
->url
, up_path
);
954 super_config_url
= relative_url(remote_url
, sub
->url
, NULL
);
960 sub_origin_url
= xstrdup(sub
->url
);
961 super_config_url
= xstrdup(sub
->url
);
964 sub_origin_url
= xstrdup("");
965 super_config_url
= xstrdup("");
968 displaypath
= get_submodule_displaypath(path
, prefix
);
970 if (!(flags
& OPT_QUIET
))
971 printf(_("Synchronizing submodule url for '%s'\n"),
975 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
976 if (git_config_set_gently(sb
.buf
, super_config_url
))
977 die(_("failed to register url for submodule path '%s'"),
980 if (!is_submodule_populated_gently(path
, NULL
))
983 prepare_submodule_repo_env(&cp
.env_array
);
986 argv_array_pushl(&cp
.args
, "submodule--helper",
987 "print-default-remote", NULL
);
990 if (capture_command(&cp
, &sb
, 0))
991 die(_("failed to get the default remote for submodule '%s'"),
994 strbuf_strip_suffix(&sb
, "\n");
995 remote_key
= xstrfmt("remote.%s.url", sb
.buf
);
998 submodule_to_gitdir(&sb
, path
);
999 strbuf_addstr(&sb
, "/config");
1001 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1002 die(_("failed to update remote for submodule '%s'"),
1005 if (flags
& OPT_RECURSIVE
) {
1006 struct child_process cpr
= CHILD_PROCESS_INIT
;
1010 prepare_submodule_repo_env(&cpr
.env_array
);
1012 argv_array_push(&cpr
.args
, "--super-prefix");
1013 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
1014 argv_array_pushl(&cpr
.args
, "submodule--helper", "sync",
1015 "--recursive", NULL
);
1017 if (flags
& OPT_QUIET
)
1018 argv_array_push(&cpr
.args
, "--quiet");
1020 if (run_command(&cpr
))
1021 die(_("failed to recurse into submodule '%s'"),
1026 free(super_config_url
);
1027 free(sub_origin_url
);
1028 strbuf_release(&sb
);
1031 free(sub_config_path
);
1034 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1036 struct sync_cb
*info
= cb_data
;
1037 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1040 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1042 struct sync_cb info
= SYNC_CB_INIT
;
1043 struct pathspec pathspec
;
1044 struct module_list list
= MODULE_LIST_INIT
;
1048 struct option module_sync_options
[] = {
1049 OPT__QUIET(&quiet
, N_("Suppress output of synchronizing submodule url")),
1050 OPT_BOOL(0, "recursive", &recursive
,
1051 N_("Recurse into nested submodules")),
1055 const char *const git_submodule_helper_usage
[] = {
1056 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1060 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1061 git_submodule_helper_usage
, 0);
1063 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1066 info
.prefix
= prefix
;
1068 info
.flags
|= OPT_QUIET
;
1070 info
.flags
|= OPT_RECURSIVE
;
1072 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1081 #define DEINIT_CB_INIT { NULL, 0 }
1083 static void deinit_submodule(const char *path
, const char *prefix
,
1086 const struct submodule
*sub
;
1087 char *displaypath
= NULL
;
1088 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1089 struct strbuf sb_config
= STRBUF_INIT
;
1090 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1092 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
1094 if (!sub
|| !sub
->name
)
1097 displaypath
= get_submodule_displaypath(path
, prefix
);
1099 /* remove the submodule work tree (unless the user already did it) */
1100 if (is_directory(path
)) {
1101 struct strbuf sb_rm
= STRBUF_INIT
;
1105 * protect submodules containing a .git directory
1106 * NEEDSWORK: instead of dying, automatically call
1107 * absorbgitdirs and (possibly) warn.
1109 if (is_directory(sub_git_dir
))
1110 die(_("Submodule work tree '%s' contains a .git "
1111 "directory (use 'rm -rf' if you really want "
1112 "to remove it including all of its history)"),
1115 if (!(flags
& OPT_FORCE
)) {
1116 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1118 argv_array_pushl(&cp_rm
.args
, "rm", "-qn",
1121 if (run_command(&cp_rm
))
1122 die(_("Submodule work tree '%s' contains local "
1123 "modifications; use '-f' to discard them"),
1127 strbuf_addstr(&sb_rm
, path
);
1129 if (!remove_dir_recursively(&sb_rm
, 0))
1130 format
= _("Cleared directory '%s'\n");
1132 format
= _("Could not remove submodule work tree '%s'\n");
1134 if (!(flags
& OPT_QUIET
))
1135 printf(format
, displaypath
);
1137 submodule_unset_core_worktree(sub
);
1139 strbuf_release(&sb_rm
);
1142 if (mkdir(path
, 0777))
1143 printf(_("could not create empty submodule directory %s"),
1146 cp_config
.git_cmd
= 1;
1147 argv_array_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1148 argv_array_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1150 /* remove the .git/config entries (unless the user already did it) */
1151 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1152 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1154 * remove the whole section so we have a clean state when
1155 * the user later decides to init this submodule again
1157 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1158 if (!(flags
& OPT_QUIET
))
1159 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1160 sub
->name
, sub
->url
, displaypath
);
1167 strbuf_release(&sb_config
);
1170 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1173 struct deinit_cb
*info
= cb_data
;
1174 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1177 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1179 struct deinit_cb info
= DEINIT_CB_INIT
;
1180 struct pathspec pathspec
;
1181 struct module_list list
= MODULE_LIST_INIT
;
1186 struct option module_deinit_options
[] = {
1187 OPT__QUIET(&quiet
, N_("Suppress submodule status output")),
1188 OPT__FORCE(&force
, N_("Remove submodule working trees even if they contain local changes"), 0),
1189 OPT_BOOL(0, "all", &all
, N_("Unregister all submodules")),
1193 const char *const git_submodule_helper_usage
[] = {
1194 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1198 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1199 git_submodule_helper_usage
, 0);
1202 error("pathspec and --all are incompatible");
1203 usage_with_options(git_submodule_helper_usage
,
1204 module_deinit_options
);
1208 die(_("Use '--all' if you really want to deinitialize all submodules"));
1210 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1213 info
.prefix
= prefix
;
1215 info
.flags
|= OPT_QUIET
;
1217 info
.flags
|= OPT_FORCE
;
1219 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1224 static int clone_submodule(const char *path
, const char *gitdir
, const char *url
,
1225 const char *depth
, struct string_list
*reference
, int dissociate
,
1226 int quiet
, int progress
)
1228 struct child_process cp
= CHILD_PROCESS_INIT
;
1230 argv_array_push(&cp
.args
, "clone");
1231 argv_array_push(&cp
.args
, "--no-checkout");
1233 argv_array_push(&cp
.args
, "--quiet");
1235 argv_array_push(&cp
.args
, "--progress");
1236 if (depth
&& *depth
)
1237 argv_array_pushl(&cp
.args
, "--depth", depth
, NULL
);
1238 if (reference
->nr
) {
1239 struct string_list_item
*item
;
1240 for_each_string_list_item(item
, reference
)
1241 argv_array_pushl(&cp
.args
, "--reference",
1242 item
->string
, NULL
);
1245 argv_array_push(&cp
.args
, "--dissociate");
1246 if (gitdir
&& *gitdir
)
1247 argv_array_pushl(&cp
.args
, "--separate-git-dir", gitdir
, NULL
);
1249 argv_array_push(&cp
.args
, "--");
1250 argv_array_push(&cp
.args
, url
);
1251 argv_array_push(&cp
.args
, path
);
1254 prepare_submodule_repo_env(&cp
.env_array
);
1257 return run_command(&cp
);
1260 struct submodule_alternate_setup
{
1261 const char *submodule_name
;
1262 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1263 SUBMODULE_ALTERNATE_ERROR_DIE
,
1264 SUBMODULE_ALTERNATE_ERROR_INFO
,
1265 SUBMODULE_ALTERNATE_ERROR_IGNORE
1267 struct string_list
*reference
;
1269 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1270 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1272 static int add_possible_reference_from_superproject(
1273 struct object_directory
*odb
, void *sas_cb
)
1275 struct submodule_alternate_setup
*sas
= sas_cb
;
1279 * If the alternate object store is another repository, try the
1280 * standard layout with .git/(modules/<name>)+/objects
1282 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1284 struct strbuf sb
= STRBUF_INIT
;
1285 struct strbuf err
= STRBUF_INIT
;
1286 strbuf_add(&sb
, odb
->path
, len
);
1289 * We need to end the new path with '/' to mark it as a dir,
1290 * otherwise a submodule name containing '/' will be broken
1291 * as the last part of a missing submodule reference would
1292 * be taken as a file name.
1294 strbuf_addf(&sb
, "/modules/%s/", sas
->submodule_name
);
1296 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1298 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1301 switch (sas
->error_mode
) {
1302 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1303 die(_("submodule '%s' cannot add alternate: %s"),
1304 sas
->submodule_name
, err
.buf
);
1305 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1306 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1307 sas
->submodule_name
, err
.buf
);
1308 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1312 strbuf_release(&sb
);
1318 static void prepare_possible_alternates(const char *sm_name
,
1319 struct string_list
*reference
)
1321 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1322 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1324 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1328 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1330 if (!error_strategy
)
1331 error_strategy
= xstrdup("die");
1333 sas
.submodule_name
= sm_name
;
1334 sas
.reference
= reference
;
1335 if (!strcmp(error_strategy
, "die"))
1336 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1337 else if (!strcmp(error_strategy
, "info"))
1338 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1339 else if (!strcmp(error_strategy
, "ignore"))
1340 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1342 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1344 if (!strcmp(sm_alternate
, "superproject"))
1345 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1346 else if (!strcmp(sm_alternate
, "no"))
1349 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1352 free(error_strategy
);
1355 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1357 const char *name
= NULL
, *url
= NULL
, *depth
= NULL
;
1360 char *p
, *path
= NULL
, *sm_gitdir
;
1361 struct strbuf sb
= STRBUF_INIT
;
1362 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1363 int dissociate
= 0, require_init
= 0;
1364 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1366 struct option module_clone_options
[] = {
1367 OPT_STRING(0, "prefix", &prefix
,
1369 N_("alternative anchor for relative paths")),
1370 OPT_STRING(0, "path", &path
,
1372 N_("where the new submodule will be cloned to")),
1373 OPT_STRING(0, "name", &name
,
1375 N_("name of the new submodule")),
1376 OPT_STRING(0, "url", &url
,
1378 N_("url where to clone the submodule from")),
1379 OPT_STRING_LIST(0, "reference", &reference
,
1381 N_("reference repository")),
1382 OPT_BOOL(0, "dissociate", &dissociate
,
1383 N_("use --reference only while cloning")),
1384 OPT_STRING(0, "depth", &depth
,
1386 N_("depth for shallow clones")),
1387 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
1388 OPT_BOOL(0, "progress", &progress
,
1389 N_("force cloning progress")),
1390 OPT_BOOL(0, "require-init", &require_init
,
1391 N_("disallow cloning into non-empty directory")),
1395 const char *const git_submodule_helper_usage
[] = {
1396 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1397 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1398 "--url <url> --path <path>"),
1402 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1403 git_submodule_helper_usage
, 0);
1405 if (argc
|| !url
|| !path
|| !*path
)
1406 usage_with_options(git_submodule_helper_usage
,
1407 module_clone_options
);
1409 strbuf_addf(&sb
, "%s/modules/%s", get_git_dir(), name
);
1410 sm_gitdir
= absolute_pathdup(sb
.buf
);
1413 if (!is_absolute_path(path
)) {
1414 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), path
);
1415 path
= strbuf_detach(&sb
, NULL
);
1417 path
= xstrdup(path
);
1419 if (validate_submodule_git_dir(sm_gitdir
, name
) < 0)
1420 die(_("refusing to create/use '%s' in another submodule's "
1421 "git dir"), sm_gitdir
);
1423 if (!file_exists(sm_gitdir
)) {
1424 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1425 die(_("could not create directory '%s'"), sm_gitdir
);
1427 prepare_possible_alternates(name
, &reference
);
1429 if (clone_submodule(path
, sm_gitdir
, url
, depth
, &reference
, dissociate
,
1431 die(_("clone of '%s' into submodule path '%s' failed"),
1434 if (require_init
&& !access(path
, X_OK
) && !is_empty_dir(path
))
1435 die(_("directory not empty: '%s'"), path
);
1436 if (safe_create_leading_directories_const(path
) < 0)
1437 die(_("could not create directory '%s'"), path
);
1438 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1439 unlink_or_warn(sb
.buf
);
1443 connect_work_tree_and_git_dir(path
, sm_gitdir
, 0);
1445 p
= git_pathdup_submodule(path
, "config");
1447 die(_("could not get submodule directory for '%s'"), path
);
1449 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1450 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1452 git_config_set_in_file(p
, "submodule.alternateLocation",
1454 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1456 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1460 free(error_strategy
);
1462 strbuf_release(&sb
);
1469 static void determine_submodule_update_strategy(struct repository
*r
,
1473 struct submodule_update_strategy
*out
)
1475 const struct submodule
*sub
= submodule_from_path(r
, &null_oid
, path
);
1479 key
= xstrfmt("submodule.%s.update", sub
->name
);
1482 if (parse_submodule_update_strategy(update
, out
) < 0)
1483 die(_("Invalid update mode '%s' for submodule path '%s'"),
1485 } else if (!repo_config_get_string_const(r
, key
, &val
)) {
1486 if (parse_submodule_update_strategy(val
, out
) < 0)
1487 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1489 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1490 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1491 BUG("how did we read update = !command from .gitmodules?");
1492 out
->type
= sub
->update_strategy
.type
;
1493 out
->command
= sub
->update_strategy
.command
;
1495 out
->type
= SM_UPDATE_CHECKOUT
;
1498 (out
->type
== SM_UPDATE_MERGE
||
1499 out
->type
== SM_UPDATE_REBASE
||
1500 out
->type
== SM_UPDATE_NONE
))
1501 out
->type
= SM_UPDATE_CHECKOUT
;
1506 static int module_update_module_mode(int argc
, const char **argv
, const char *prefix
)
1508 const char *path
, *update
= NULL
;
1510 struct submodule_update_strategy update_strategy
= { .type
= SM_UPDATE_CHECKOUT
};
1512 if (argc
< 3 || argc
> 4)
1513 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1515 just_cloned
= git_config_int("just_cloned", argv
[1]);
1521 determine_submodule_update_strategy(the_repository
,
1522 just_cloned
, path
, update
,
1524 fputs(submodule_strategy_to_string(&update_strategy
), stdout
);
1529 struct update_clone_data
{
1530 const struct submodule
*sub
;
1531 struct object_id oid
;
1532 unsigned just_cloned
;
1535 struct submodule_update_clone
{
1536 /* index into 'list', the list of submodules to look into for cloning */
1538 struct module_list list
;
1539 unsigned warn_if_uninitialized
: 1;
1541 /* update parameter passed via commandline */
1542 struct submodule_update_strategy update
;
1544 /* configuration parameters which are passed on to the children */
1547 int recommend_shallow
;
1548 struct string_list references
;
1550 unsigned require_init
;
1552 const char *recursive_prefix
;
1555 /* to be consumed by git-submodule.sh */
1556 struct update_clone_data
*update_clone
;
1557 int update_clone_nr
; int update_clone_alloc
;
1559 /* If we want to stop as fast as possible and return an error */
1560 unsigned quickstop
: 1;
1562 /* failed clones to be retried again */
1563 const struct cache_entry
**failed_clones
;
1564 int failed_clones_nr
, failed_clones_alloc
;
1568 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1569 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, 0, \
1571 NULL, 0, 0, 0, NULL, 0, 0, 1}
1574 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1575 struct strbuf
*out
, const char *displaypath
)
1578 * Only mention uninitialized submodules when their
1579 * paths have been specified.
1581 if (suc
->warn_if_uninitialized
) {
1583 _("Submodule path '%s' not initialized"),
1585 strbuf_addch(out
, '\n');
1587 _("Maybe you want to use 'update --init'?"));
1588 strbuf_addch(out
, '\n');
1593 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1594 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1596 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1597 struct child_process
*child
,
1598 struct submodule_update_clone
*suc
,
1601 const struct submodule
*sub
= NULL
;
1602 const char *url
= NULL
;
1603 const char *update_string
;
1604 enum submodule_update_type update_type
;
1606 struct strbuf displaypath_sb
= STRBUF_INIT
;
1607 struct strbuf sb
= STRBUF_INIT
;
1608 const char *displaypath
= NULL
;
1609 int needs_cloning
= 0;
1610 int need_free_url
= 0;
1613 if (suc
->recursive_prefix
)
1614 strbuf_addf(&sb
, "%s/%s", suc
->recursive_prefix
, ce
->name
);
1616 strbuf_addstr(&sb
, ce
->name
);
1617 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
1618 strbuf_addch(out
, '\n');
1622 sub
= submodule_from_path(the_repository
, &null_oid
, ce
->name
);
1624 if (suc
->recursive_prefix
)
1625 displaypath
= relative_path(suc
->recursive_prefix
,
1626 ce
->name
, &displaypath_sb
);
1628 displaypath
= ce
->name
;
1631 next_submodule_warn_missing(suc
, out
, displaypath
);
1635 key
= xstrfmt("submodule.%s.update", sub
->name
);
1636 if (!repo_config_get_string_const(the_repository
, key
, &update_string
)) {
1637 update_type
= parse_submodule_update_type(update_string
);
1639 update_type
= sub
->update_strategy
.type
;
1643 if (suc
->update
.type
== SM_UPDATE_NONE
1644 || (suc
->update
.type
== SM_UPDATE_UNSPECIFIED
1645 && update_type
== SM_UPDATE_NONE
)) {
1646 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
1647 strbuf_addch(out
, '\n');
1651 /* Check if the submodule has been initialized. */
1652 if (!is_submodule_active(the_repository
, ce
->name
)) {
1653 next_submodule_warn_missing(suc
, out
, displaypath
);
1658 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1659 if (repo_config_get_string_const(the_repository
, sb
.buf
, &url
)) {
1660 if (starts_with_dot_slash(sub
->url
) ||
1661 starts_with_dot_dot_slash(sub
->url
)) {
1662 url
= compute_submodule_clone_url(sub
->url
);
1669 strbuf_addf(&sb
, "%s/.git", ce
->name
);
1670 needs_cloning
= !file_exists(sb
.buf
);
1672 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
1673 suc
->update_clone_alloc
);
1674 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
1675 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
1676 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
1677 suc
->update_clone_nr
++;
1683 child
->no_stdin
= 1;
1684 child
->stdout_to_stderr
= 1;
1686 argv_array_push(&child
->args
, "submodule--helper");
1687 argv_array_push(&child
->args
, "clone");
1689 argv_array_push(&child
->args
, "--progress");
1691 argv_array_push(&child
->args
, "--quiet");
1693 argv_array_pushl(&child
->args
, "--prefix", suc
->prefix
, NULL
);
1694 if (suc
->recommend_shallow
&& sub
->recommend_shallow
== 1)
1695 argv_array_push(&child
->args
, "--depth=1");
1696 if (suc
->require_init
)
1697 argv_array_push(&child
->args
, "--require-init");
1698 argv_array_pushl(&child
->args
, "--path", sub
->path
, NULL
);
1699 argv_array_pushl(&child
->args
, "--name", sub
->name
, NULL
);
1700 argv_array_pushl(&child
->args
, "--url", url
, NULL
);
1701 if (suc
->references
.nr
) {
1702 struct string_list_item
*item
;
1703 for_each_string_list_item(item
, &suc
->references
)
1704 argv_array_pushl(&child
->args
, "--reference", item
->string
, NULL
);
1706 if (suc
->dissociate
)
1707 argv_array_push(&child
->args
, "--dissociate");
1709 argv_array_push(&child
->args
, suc
->depth
);
1712 strbuf_reset(&displaypath_sb
);
1717 return needs_cloning
;
1720 static int update_clone_get_next_task(struct child_process
*child
,
1725 struct submodule_update_clone
*suc
= suc_cb
;
1726 const struct cache_entry
*ce
;
1729 for (; suc
->current
< suc
->list
.nr
; suc
->current
++) {
1730 ce
= suc
->list
.entries
[suc
->current
];
1731 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
1732 int *p
= xmalloc(sizeof(*p
));
1741 * The loop above tried cloning each submodule once, now try the
1742 * stragglers again, which we can imagine as an extension of the
1745 index
= suc
->current
- suc
->list
.nr
;
1746 if (index
< suc
->failed_clones_nr
) {
1748 ce
= suc
->failed_clones
[index
];
1749 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
1751 strbuf_addstr(err
, "BUG: submodule considered for "
1752 "cloning, doesn't need cloning "
1756 p
= xmalloc(sizeof(*p
));
1766 static int update_clone_start_failure(struct strbuf
*err
,
1770 struct submodule_update_clone
*suc
= suc_cb
;
1775 static int update_clone_task_finished(int result
,
1780 const struct cache_entry
*ce
;
1781 struct submodule_update_clone
*suc
= suc_cb
;
1783 int *idxP
= idx_task_cb
;
1790 if (idx
< suc
->list
.nr
) {
1791 ce
= suc
->list
.entries
[idx
];
1792 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
1794 strbuf_addch(err
, '\n');
1795 ALLOC_GROW(suc
->failed_clones
,
1796 suc
->failed_clones_nr
+ 1,
1797 suc
->failed_clones_alloc
);
1798 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
1801 idx
-= suc
->list
.nr
;
1802 ce
= suc
->failed_clones
[idx
];
1803 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
1805 strbuf_addch(err
, '\n');
1813 static int git_update_clone_config(const char *var
, const char *value
,
1817 if (!strcmp(var
, "submodule.fetchjobs"))
1818 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
1822 static void update_submodule(struct update_clone_data
*ucd
)
1824 fprintf(stdout
, "dummy %s %d\t%s\n",
1825 oid_to_hex(&ucd
->oid
),
1830 static int update_submodules(struct submodule_update_clone
*suc
)
1834 run_processes_parallel_tr2(suc
->max_jobs
, update_clone_get_next_task
,
1835 update_clone_start_failure
,
1836 update_clone_task_finished
, suc
, "submodule",
1840 * We saved the output and put it out all at once now.
1842 * - the listener does not have to interleave their (checkout)
1843 * work with our fetching. The writes involved in a
1844 * checkout involve more straightforward sequential I/O.
1845 * - the listener can avoid doing any work if fetching failed.
1850 for (i
= 0; i
< suc
->update_clone_nr
; i
++)
1851 update_submodule(&suc
->update_clone
[i
]);
1856 static int update_clone(int argc
, const char **argv
, const char *prefix
)
1858 const char *update
= NULL
;
1859 struct pathspec pathspec
;
1860 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
1862 struct option module_update_clone_options
[] = {
1863 OPT_STRING(0, "prefix", &prefix
,
1865 N_("path into the working tree")),
1866 OPT_STRING(0, "recursive-prefix", &suc
.recursive_prefix
,
1868 N_("path into the working tree, across nested "
1869 "submodule boundaries")),
1870 OPT_STRING(0, "update", &update
,
1872 N_("rebase, merge, checkout or none")),
1873 OPT_STRING_LIST(0, "reference", &suc
.references
, N_("repo"),
1874 N_("reference repository")),
1875 OPT_BOOL(0, "dissociate", &suc
.dissociate
,
1876 N_("use --reference only while cloning")),
1877 OPT_STRING(0, "depth", &suc
.depth
, "<depth>",
1878 N_("Create a shallow clone truncated to the "
1879 "specified number of revisions")),
1880 OPT_INTEGER('j', "jobs", &suc
.max_jobs
,
1881 N_("parallel jobs")),
1882 OPT_BOOL(0, "recommend-shallow", &suc
.recommend_shallow
,
1883 N_("whether the initial clone should follow the shallow recommendation")),
1884 OPT__QUIET(&suc
.quiet
, N_("don't print cloning progress")),
1885 OPT_BOOL(0, "progress", &suc
.progress
,
1886 N_("force cloning progress")),
1887 OPT_BOOL(0, "require-init", &suc
.require_init
,
1888 N_("disallow cloning into non-empty directory")),
1892 const char *const git_submodule_helper_usage
[] = {
1893 N_("git submodule--helper update-clone [--prefix=<path>] [<path>...]"),
1896 suc
.prefix
= prefix
;
1898 update_clone_config_from_gitmodules(&suc
.max_jobs
);
1899 git_config(git_update_clone_config
, &suc
.max_jobs
);
1901 argc
= parse_options(argc
, argv
, prefix
, module_update_clone_options
,
1902 git_submodule_helper_usage
, 0);
1905 if (parse_submodule_update_strategy(update
, &suc
.update
) < 0)
1906 die(_("bad value for update parameter"));
1908 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &suc
.list
) < 0)
1912 suc
.warn_if_uninitialized
= 1;
1914 return update_submodules(&suc
);
1917 static int resolve_relative_path(int argc
, const char **argv
, const char *prefix
)
1919 struct strbuf sb
= STRBUF_INIT
;
1921 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc
);
1923 printf("%s", relative_path(argv
[1], argv
[2], &sb
));
1924 strbuf_release(&sb
);
1928 static const char *remote_submodule_branch(const char *path
)
1930 const struct submodule
*sub
;
1931 const char *branch
= NULL
;
1934 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
1938 key
= xstrfmt("submodule.%s.branch", sub
->name
);
1939 if (repo_config_get_string_const(the_repository
, key
, &branch
))
1940 branch
= sub
->branch
;
1946 if (!strcmp(branch
, ".")) {
1947 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1950 die(_("No such ref: %s"), "HEAD");
1953 if (!strcmp(refname
, "HEAD"))
1954 die(_("Submodule (%s) branch configured to inherit "
1955 "branch from superproject, but the superproject "
1956 "is not on any branch"), sub
->name
);
1958 if (!skip_prefix(refname
, "refs/heads/", &refname
))
1959 die(_("Expecting a full ref name, got %s"), refname
);
1966 static int resolve_remote_submodule_branch(int argc
, const char **argv
,
1970 struct strbuf sb
= STRBUF_INIT
;
1972 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc
);
1974 ret
= remote_submodule_branch(argv
[1]);
1976 die("submodule %s doesn't exist", argv
[1]);
1979 strbuf_release(&sb
);
1983 static int push_check(int argc
, const char **argv
, const char *prefix
)
1985 struct remote
*remote
;
1986 const char *superproject_head
;
1988 int detached_head
= 0;
1989 struct object_id head_oid
;
1992 die("submodule--helper push-check requires at least 2 arguments");
1995 * superproject's resolved head ref.
1996 * if HEAD then the superproject is in a detached head state, otherwise
1997 * it will be the resolved head ref.
1999 superproject_head
= argv
[1];
2002 /* Get the submodule's head ref and determine if it is detached */
2003 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2005 die(_("Failed to resolve HEAD as a valid ref."));
2006 if (!strcmp(head
, "HEAD"))
2010 * The remote must be configured.
2011 * This is to avoid pushing to the exact same URL as the parent.
2013 remote
= pushremote_get(argv
[1]);
2014 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2015 die("remote '%s' not configured", argv
[1]);
2017 /* Check the refspec */
2020 struct ref
*local_refs
= get_local_heads();
2021 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2023 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2025 for (i
= 0; i
< refspec
.nr
; i
++) {
2026 const struct refspec_item
*rs
= &refspec
.items
[i
];
2028 if (rs
->pattern
|| rs
->matching
)
2031 /* LHS must match a single ref */
2032 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2037 * If LHS matches 'HEAD' then we need to ensure
2038 * that it matches the same named branch
2039 * checked out in the superproject.
2041 if (!strcmp(rs
->src
, "HEAD")) {
2042 if (!detached_head
&&
2043 !strcmp(head
, superproject_head
))
2045 die("HEAD does not match the named branch in the superproject");
2049 die("src refspec '%s' must name a ref",
2053 refspec_clear(&refspec
);
2060 static int ensure_core_worktree(int argc
, const char **argv
, const char *prefix
)
2062 const struct submodule
*sub
;
2065 struct repository subrepo
;
2068 BUG("submodule--helper ensure-core-worktree <path>");
2072 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
2074 BUG("We could get the submodule handle before?");
2076 if (repo_submodule_init(&subrepo
, the_repository
, sub
))
2077 die(_("could not get a repository handle for submodule '%s'"), path
);
2079 if (!repo_config_get_string(&subrepo
, "core.worktree", &cw
)) {
2080 char *cfg_file
, *abs_path
;
2081 const char *rel_path
;
2082 struct strbuf sb
= STRBUF_INIT
;
2084 cfg_file
= repo_git_path(&subrepo
, "config");
2086 abs_path
= absolute_pathdup(path
);
2087 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2089 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2093 strbuf_release(&sb
);
2099 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2102 struct pathspec pathspec
;
2103 struct module_list list
= MODULE_LIST_INIT
;
2104 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
2106 struct option embed_gitdir_options
[] = {
2107 OPT_STRING(0, "prefix", &prefix
,
2109 N_("path into the working tree")),
2110 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
2111 ABSORB_GITDIR_RECURSE_SUBMODULES
),
2115 const char *const git_submodule_helper_usage
[] = {
2116 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2120 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2121 git_submodule_helper_usage
, 0);
2123 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
2126 for (i
= 0; i
< list
.nr
; i
++)
2127 absorb_git_dir_into_superproject(list
.entries
[i
]->name
, flags
);
2132 static int is_active(int argc
, const char **argv
, const char *prefix
)
2135 die("submodule--helper is-active takes exactly 1 argument");
2137 return !is_submodule_active(the_repository
, argv
[1]);
2141 * Exit non-zero if any of the submodule names given on the command line is
2142 * invalid. If no names are given, filter stdin to print only valid names
2143 * (which is primarily intended for testing).
2145 static int check_name(int argc
, const char **argv
, const char *prefix
)
2149 if (check_submodule_name(*argv
) < 0)
2153 struct strbuf buf
= STRBUF_INIT
;
2154 while (strbuf_getline(&buf
, stdin
) != EOF
) {
2155 if (!check_submodule_name(buf
.buf
))
2156 printf("%s\n", buf
.buf
);
2158 strbuf_release(&buf
);
2163 static int module_config(int argc
, const char **argv
, const char *prefix
)
2166 CHECK_WRITEABLE
= 1,
2170 struct option module_config_options
[] = {
2171 OPT_CMDMODE(0, "check-writeable", &command
,
2172 N_("check if it is safe to write to the .gitmodules file"),
2174 OPT_CMDMODE(0, "unset", &command
,
2175 N_("unset the config in the .gitmodules file"),
2179 const char *const git_submodule_helper_usage
[] = {
2180 N_("git submodule--helper config <name> [<value>]"),
2181 N_("git submodule--helper config --unset <name>"),
2182 N_("git submodule--helper config --check-writeable"),
2186 argc
= parse_options(argc
, argv
, prefix
, module_config_options
,
2187 git_submodule_helper_usage
, PARSE_OPT_KEEP_ARGV0
);
2189 if (argc
== 1 && command
== CHECK_WRITEABLE
)
2190 return is_writing_gitmodules_ok() ? 0 : -1;
2192 /* Equivalent to ACTION_GET in builtin/config.c */
2193 if (argc
== 2 && command
!= DO_UNSET
)
2194 return print_config_from_gitmodules(the_repository
, argv
[1]);
2196 /* Equivalent to ACTION_SET in builtin/config.c */
2197 if (argc
== 3 || (argc
== 2 && command
== DO_UNSET
)) {
2198 const char *value
= (argc
== 3) ? argv
[2] : NULL
;
2200 if (!is_writing_gitmodules_ok())
2201 die(_("please make sure that the .gitmodules file is in the working tree"));
2203 return config_set_in_gitmodules_file_gently(argv
[1], value
);
2206 usage_with_options(git_submodule_helper_usage
, module_config_options
);
2209 #define SUPPORT_SUPER_PREFIX (1<<0)
2213 int (*fn
)(int, const char **, const char *);
2217 static struct cmd_struct commands
[] = {
2218 {"list", module_list
, 0},
2219 {"name", module_name
, 0},
2220 {"clone", module_clone
, 0},
2221 {"update-module-mode", module_update_module_mode
, 0},
2222 {"update-clone", update_clone
, 0},
2223 {"ensure-core-worktree", ensure_core_worktree
, 0},
2224 {"relative-path", resolve_relative_path
, 0},
2225 {"resolve-relative-url", resolve_relative_url
, 0},
2226 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
2227 {"foreach", module_foreach
, SUPPORT_SUPER_PREFIX
},
2228 {"init", module_init
, SUPPORT_SUPER_PREFIX
},
2229 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
2230 {"print-default-remote", print_default_remote
, 0},
2231 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
2232 {"deinit", module_deinit
, 0},
2233 {"remote-branch", resolve_remote_submodule_branch
, 0},
2234 {"push-check", push_check
, 0},
2235 {"absorb-git-dirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
2236 {"is-active", is_active
, 0},
2237 {"check-name", check_name
, 0},
2238 {"config", module_config
, 0},
2241 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
2244 if (argc
< 2 || !strcmp(argv
[1], "-h"))
2245 usage("git submodule--helper <command>");
2247 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
2248 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
2249 if (get_super_prefix() &&
2250 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
2251 die(_("%s doesn't support --super-prefix"),
2253 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
2257 die(_("'%s' is not a valid submodule--helper "
2258 "subcommand"), argv
[1]);