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
);
191 if (!up_path
|| !is_relative
) {
197 strbuf_addf(&sb
, "%s%s", up_path
, out
);
199 return strbuf_detach(&sb
, NULL
);
202 static int resolve_relative_url(int argc
, const char **argv
, const char *prefix
)
204 char *remoteurl
= NULL
;
205 char *remote
= get_default_remote();
206 const char *up_path
= NULL
;
209 struct strbuf sb
= STRBUF_INIT
;
211 if (argc
!= 2 && argc
!= 3)
212 die("resolve-relative-url only accepts one or two arguments");
215 strbuf_addf(&sb
, "remote.%s.url", remote
);
218 if (git_config_get_string(sb
.buf
, &remoteurl
))
219 /* the repository is its own authoritative upstream */
220 remoteurl
= xgetcwd();
225 res
= relative_url(remoteurl
, url
, up_path
);
232 static int resolve_relative_url_test(int argc
, const char **argv
, const char *prefix
)
234 char *remoteurl
, *res
;
235 const char *up_path
, *url
;
238 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
241 remoteurl
= xstrdup(argv
[2]);
244 if (!strcmp(up_path
, "(null)"))
247 res
= relative_url(remoteurl
, url
, up_path
);
254 /* the result should be freed by the caller. */
255 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
257 const char *super_prefix
= get_super_prefix();
259 if (prefix
&& super_prefix
) {
260 BUG("cannot have prefix '%s' and superprefix '%s'",
261 prefix
, super_prefix
);
263 struct strbuf sb
= STRBUF_INIT
;
264 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
267 } else if (super_prefix
) {
268 return xstrfmt("%s%s", super_prefix
, path
);
270 return xstrdup(path
);
274 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
276 struct strbuf sb
= STRBUF_INIT
;
279 static const char *describe_bare
[] = { NULL
};
281 static const char *describe_tags
[] = { "--tags", NULL
};
283 static const char *describe_contains
[] = { "--contains", NULL
};
285 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
287 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
289 describe_all_always
, NULL
};
291 for (d
= describe_argv
; *d
; d
++) {
292 struct child_process cp
= CHILD_PROCESS_INIT
;
293 prepare_submodule_repo_env(&cp
.env_array
);
298 strvec_push(&cp
.args
, "describe");
299 strvec_pushv(&cp
.args
, *d
);
300 strvec_push(&cp
.args
, object_id
);
302 if (!capture_command(&cp
, &sb
, 0)) {
303 strbuf_strip_suffix(&sb
, "\n");
304 return strbuf_detach(&sb
, NULL
);
313 const struct cache_entry
**entries
;
316 #define MODULE_LIST_INIT { NULL, 0, 0 }
318 static int module_list_compute(int argc
, const char **argv
,
320 struct pathspec
*pathspec
,
321 struct module_list
*list
)
324 char *ps_matched
= NULL
;
325 parse_pathspec(pathspec
, 0,
326 PATHSPEC_PREFER_FULL
,
330 ps_matched
= xcalloc(pathspec
->nr
, 1);
332 if (read_cache() < 0)
333 die(_("index file corrupt"));
335 for (i
= 0; i
< active_nr
; i
++) {
336 const struct cache_entry
*ce
= active_cache
[i
];
338 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
340 !S_ISGITLINK(ce
->ce_mode
))
343 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
344 list
->entries
[list
->nr
++] = ce
;
345 while (i
+ 1 < active_nr
&&
346 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
348 * Skip entries with the same name in different stages
349 * to make sure an entry is returned only once.
354 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
362 static void module_list_active(struct module_list
*list
)
365 struct module_list active_modules
= MODULE_LIST_INIT
;
367 for (i
= 0; i
< list
->nr
; i
++) {
368 const struct cache_entry
*ce
= list
->entries
[i
];
370 if (!is_submodule_active(the_repository
, ce
->name
))
373 ALLOC_GROW(active_modules
.entries
,
374 active_modules
.nr
+ 1,
375 active_modules
.alloc
);
376 active_modules
.entries
[active_modules
.nr
++] = ce
;
380 *list
= active_modules
;
383 static char *get_up_path(const char *path
)
386 struct strbuf sb
= STRBUF_INIT
;
388 for (i
= count_slashes(path
); i
; i
--)
389 strbuf_addstr(&sb
, "../");
392 * Check if 'path' ends with slash or not
393 * for having the same output for dir/sub_dir
396 if (!is_dir_sep(path
[strlen(path
) - 1]))
397 strbuf_addstr(&sb
, "../");
399 return strbuf_detach(&sb
, NULL
);
402 static int module_list(int argc
, const char **argv
, const char *prefix
)
405 struct pathspec pathspec
;
406 struct module_list list
= MODULE_LIST_INIT
;
408 struct option module_list_options
[] = {
409 OPT_STRING(0, "prefix", &prefix
,
411 N_("alternative anchor for relative paths")),
415 const char *const git_submodule_helper_usage
[] = {
416 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
420 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
421 git_submodule_helper_usage
, 0);
423 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
426 for (i
= 0; i
< list
.nr
; i
++) {
427 const struct cache_entry
*ce
= list
.entries
[i
];
430 printf("%06o %s U\t", ce
->ce_mode
,
431 oid_to_hex(null_oid()));
433 printf("%06o %s %d\t", ce
->ce_mode
,
434 oid_to_hex(&ce
->oid
), ce_stage(ce
));
436 fprintf(stdout
, "%s\n", ce
->name
);
441 static void for_each_listed_submodule(const struct module_list
*list
,
442 each_submodule_fn fn
, void *cb_data
)
445 for (i
= 0; i
< list
->nr
; i
++)
446 fn(list
->entries
[i
], cb_data
);
456 #define FOREACH_CB_INIT { 0 }
458 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
461 struct foreach_cb
*info
= cb_data
;
462 const char *path
= list_item
->name
;
463 const struct object_id
*ce_oid
= &list_item
->oid
;
465 const struct submodule
*sub
;
466 struct child_process cp
= CHILD_PROCESS_INIT
;
469 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
471 sub
= submodule_from_path(the_repository
, null_oid(), path
);
474 die(_("No url found for submodule path '%s' in .gitmodules"),
477 if (!is_submodule_populated_gently(path
, NULL
))
480 prepare_submodule_repo_env(&cp
.env_array
);
483 * For the purpose of executing <command> in the submodule,
484 * separate shell is used for the purpose of running the
491 * NEEDSWORK: the command currently has access to the variables $name,
492 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
493 * contains a single argument. This is done for maintaining a faithful
494 * translation from shell script.
496 if (info
->argc
== 1) {
497 char *toplevel
= xgetcwd();
498 struct strbuf sb
= STRBUF_INIT
;
500 strvec_pushf(&cp
.env_array
, "name=%s", sub
->name
);
501 strvec_pushf(&cp
.env_array
, "sm_path=%s", path
);
502 strvec_pushf(&cp
.env_array
, "displaypath=%s", displaypath
);
503 strvec_pushf(&cp
.env_array
, "sha1=%s",
505 strvec_pushf(&cp
.env_array
, "toplevel=%s", toplevel
);
508 * Since the path variable was accessible from the script
509 * before porting, it is also made available after porting.
510 * The environment variable "PATH" has a very special purpose
511 * on windows. And since environment variables are
512 * case-insensitive in windows, it interferes with the
513 * existing PATH variable. Hence, to avoid that, we expose
514 * path via the args strvec and not via env_array.
516 sq_quote_buf(&sb
, path
);
517 strvec_pushf(&cp
.args
, "path=%s; %s",
518 sb
.buf
, info
->argv
[0]);
522 strvec_pushv(&cp
.args
, info
->argv
);
526 printf(_("Entering '%s'\n"), displaypath
);
528 if (info
->argv
[0] && run_command(&cp
))
529 die(_("run_command returned non-zero status for %s\n."),
532 if (info
->recursive
) {
533 struct child_process cpr
= CHILD_PROCESS_INIT
;
537 prepare_submodule_repo_env(&cpr
.env_array
);
539 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
540 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
541 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
545 strvec_push(&cpr
.args
, "--quiet");
547 strvec_push(&cpr
.args
, "--");
548 strvec_pushv(&cpr
.args
, info
->argv
);
550 if (run_command(&cpr
))
551 die(_("run_command returned non-zero status while "
552 "recursing in the nested submodules of %s\n."),
560 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
562 struct foreach_cb info
= FOREACH_CB_INIT
;
563 struct pathspec pathspec
;
564 struct module_list list
= MODULE_LIST_INIT
;
566 struct option module_foreach_options
[] = {
567 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
568 OPT_BOOL(0, "recursive", &info
.recursive
,
569 N_("recurse into nested submodules")),
573 const char *const git_submodule_helper_usage
[] = {
574 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
578 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
579 git_submodule_helper_usage
, 0);
581 if (module_list_compute(0, NULL
, prefix
, &pathspec
, &list
) < 0)
586 info
.prefix
= prefix
;
588 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
593 static char *compute_submodule_clone_url(const char *rel_url
)
595 char *remoteurl
, *relurl
;
596 char *remote
= get_default_remote();
597 struct strbuf remotesb
= STRBUF_INIT
;
599 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
600 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
601 warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb
.buf
);
602 remoteurl
= xgetcwd();
604 relurl
= relative_url(remoteurl
, rel_url
, NULL
);
608 strbuf_release(&remotesb
);
617 #define INIT_CB_INIT { NULL, 0 }
619 static void init_submodule(const char *path
, const char *prefix
,
622 const struct submodule
*sub
;
623 struct strbuf sb
= STRBUF_INIT
;
624 char *upd
= NULL
, *url
= NULL
, *displaypath
;
626 displaypath
= get_submodule_displaypath(path
, prefix
);
628 sub
= submodule_from_path(the_repository
, null_oid(), path
);
631 die(_("No url found for submodule path '%s' in .gitmodules"),
635 * NEEDSWORK: In a multi-working-tree world, this needs to be
636 * set in the per-worktree config.
638 * Set active flag for the submodule being initialized
640 if (!is_submodule_active(the_repository
, path
)) {
641 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
642 git_config_set_gently(sb
.buf
, "true");
647 * Copy url setting when it is not set yet.
648 * To look up the url in .git/config, we must not fall back to
649 * .gitmodules, so look it up directly.
651 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
652 if (git_config_get_string(sb
.buf
, &url
)) {
654 die(_("No url found for submodule path '%s' in .gitmodules"),
657 url
= xstrdup(sub
->url
);
659 /* Possibly a url relative to parent */
660 if (starts_with_dot_dot_slash(url
) ||
661 starts_with_dot_slash(url
)) {
663 url
= compute_submodule_clone_url(oldurl
);
667 if (git_config_set_gently(sb
.buf
, url
))
668 die(_("Failed to register url for submodule path '%s'"),
670 if (!(flags
& OPT_QUIET
))
672 _("Submodule '%s' (%s) registered for path '%s'\n"),
673 sub
->name
, url
, displaypath
);
677 /* Copy "update" setting when it is not set yet */
678 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
679 if (git_config_get_string(sb
.buf
, &upd
) &&
680 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
681 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
682 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
684 upd
= xstrdup("none");
686 upd
= xstrdup(submodule_strategy_to_string(&sub
->update_strategy
));
688 if (git_config_set_gently(sb
.buf
, upd
))
689 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
697 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
699 struct init_cb
*info
= cb_data
;
700 init_submodule(list_item
->name
, info
->prefix
, info
->flags
);
703 static int module_init(int argc
, const char **argv
, const char *prefix
)
705 struct init_cb info
= INIT_CB_INIT
;
706 struct pathspec pathspec
;
707 struct module_list list
= MODULE_LIST_INIT
;
710 struct option module_init_options
[] = {
711 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
715 const char *const git_submodule_helper_usage
[] = {
716 N_("git submodule--helper init [<options>] [<path>]"),
720 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
721 git_submodule_helper_usage
, 0);
723 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
727 * If there are no path args and submodule.active is set then,
728 * by default, only initialize 'active' modules.
730 if (!argc
&& git_config_get_value_multi("submodule.active"))
731 module_list_active(&list
);
733 info
.prefix
= prefix
;
735 info
.flags
|= OPT_QUIET
;
737 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
746 #define STATUS_CB_INIT { NULL, 0 }
748 static void print_status(unsigned int flags
, char state
, const char *path
,
749 const struct object_id
*oid
, const char *displaypath
)
751 if (flags
& OPT_QUIET
)
754 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
756 if (state
== ' ' || state
== '+') {
757 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
760 printf(" (%s)", name
);
766 static int handle_submodule_head_ref(const char *refname
,
767 const struct object_id
*oid
, int flags
,
770 struct object_id
*output
= cb_data
;
777 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
778 unsigned int ce_flags
, const char *prefix
,
782 struct strvec diff_files_args
= STRVEC_INIT
;
784 int diff_files_result
;
785 struct strbuf buf
= STRBUF_INIT
;
788 if (!submodule_from_path(the_repository
, null_oid(), path
))
789 die(_("no submodule mapping found in .gitmodules for path '%s'"),
792 displaypath
= get_submodule_displaypath(path
, prefix
);
794 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
795 print_status(flags
, 'U', path
, null_oid(), displaypath
);
799 strbuf_addf(&buf
, "%s/.git", path
);
800 git_dir
= read_gitfile(buf
.buf
);
804 if (!is_submodule_active(the_repository
, path
) ||
805 !is_git_directory(git_dir
)) {
806 print_status(flags
, '-', path
, ce_oid
, displaypath
);
807 strbuf_release(&buf
);
810 strbuf_release(&buf
);
812 strvec_pushl(&diff_files_args
, "diff-files",
813 "--ignore-submodules=dirty", "--quiet", "--",
816 git_config(git_diff_basic_config
, NULL
);
818 repo_init_revisions(the_repository
, &rev
, NULL
);
820 diff_files_args
.nr
= setup_revisions(diff_files_args
.nr
,
823 diff_files_result
= run_diff_files(&rev
, 0);
825 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
826 print_status(flags
, ' ', path
, ce_oid
,
828 } else if (!(flags
& OPT_CACHED
)) {
829 struct object_id oid
;
830 struct ref_store
*refs
= get_submodule_ref_store(path
);
833 print_status(flags
, '-', path
, ce_oid
, displaypath
);
836 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
837 die(_("could not resolve HEAD ref inside the "
838 "submodule '%s'"), path
);
840 print_status(flags
, '+', path
, &oid
, displaypath
);
842 print_status(flags
, '+', path
, ce_oid
, displaypath
);
845 if (flags
& OPT_RECURSIVE
) {
846 struct child_process cpr
= CHILD_PROCESS_INIT
;
850 prepare_submodule_repo_env(&cpr
.env_array
);
852 strvec_push(&cpr
.args
, "--super-prefix");
853 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
854 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
855 "--recursive", NULL
);
857 if (flags
& OPT_CACHED
)
858 strvec_push(&cpr
.args
, "--cached");
860 if (flags
& OPT_QUIET
)
861 strvec_push(&cpr
.args
, "--quiet");
863 if (run_command(&cpr
))
864 die(_("failed to recurse into submodule '%s'"), path
);
868 strvec_clear(&diff_files_args
);
872 static void status_submodule_cb(const struct cache_entry
*list_item
,
875 struct status_cb
*info
= cb_data
;
876 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
877 info
->prefix
, info
->flags
);
880 static int module_status(int argc
, const char **argv
, const char *prefix
)
882 struct status_cb info
= STATUS_CB_INIT
;
883 struct pathspec pathspec
;
884 struct module_list list
= MODULE_LIST_INIT
;
887 struct option module_status_options
[] = {
888 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
889 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
890 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
894 const char *const git_submodule_helper_usage
[] = {
895 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
899 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
900 git_submodule_helper_usage
, 0);
902 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
905 info
.prefix
= prefix
;
907 info
.flags
|= OPT_QUIET
;
909 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
914 static int module_name(int argc
, const char **argv
, const char *prefix
)
916 const struct submodule
*sub
;
919 usage(_("git submodule--helper name <path>"));
921 sub
= submodule_from_path(the_repository
, null_oid(), argv
[1]);
924 die(_("no submodule mapping found in .gitmodules for path '%s'"),
927 printf("%s\n", sub
->name
);
933 unsigned int mod_src
;
934 unsigned int mod_dst
;
935 struct object_id oid_src
;
936 struct object_id oid_dst
;
940 #define MODULE_CB_INIT { 0, 0, NULL, NULL, '\0', NULL }
942 struct module_cb_list
{
943 struct module_cb
**entries
;
946 #define MODULE_CB_LIST_INIT { NULL, 0, 0 }
952 unsigned int cached
: 1;
953 unsigned int for_status
: 1;
954 unsigned int files
: 1;
957 #define SUMMARY_CB_INIT { 0, NULL, NULL, 0, 0, 0, 0 }
964 static char *verify_submodule_committish(const char *sm_path
,
965 const char *committish
)
967 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
968 struct strbuf result
= STRBUF_INIT
;
970 cp_rev_parse
.git_cmd
= 1;
971 cp_rev_parse
.dir
= sm_path
;
972 prepare_submodule_repo_env(&cp_rev_parse
.env_array
);
973 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
974 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
975 strvec_push(&cp_rev_parse
.args
, "--");
977 if (capture_command(&cp_rev_parse
, &result
, 0))
980 strbuf_trim_trailing_newline(&result
);
981 return strbuf_detach(&result
, NULL
);
984 static void print_submodule_summary(struct summary_cb
*info
, char *errmsg
,
985 int total_commits
, const char *displaypath
,
986 const char *src_abbrev
, const char *dst_abbrev
,
989 if (p
->status
== 'T') {
990 if (S_ISGITLINK(p
->mod_dst
))
991 printf(_("* %s %s(blob)->%s(submodule)"),
992 displaypath
, src_abbrev
, dst_abbrev
);
994 printf(_("* %s %s(submodule)->%s(blob)"),
995 displaypath
, src_abbrev
, dst_abbrev
);
997 printf("* %s %s...%s",
998 displaypath
, src_abbrev
, dst_abbrev
);
1001 if (total_commits
< 0)
1004 printf(" (%d):\n", total_commits
);
1007 printf(_("%s"), errmsg
);
1008 } else if (total_commits
> 0) {
1009 struct child_process cp_log
= CHILD_PROCESS_INIT
;
1012 cp_log
.dir
= p
->sm_path
;
1013 prepare_submodule_repo_env(&cp_log
.env_array
);
1014 strvec_pushl(&cp_log
.args
, "log", NULL
);
1016 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
1017 if (info
->summary_limit
> 0)
1018 strvec_pushf(&cp_log
.args
, "-%d",
1019 info
->summary_limit
);
1021 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
1022 "--first-parent", NULL
);
1023 strvec_pushf(&cp_log
.args
, "%s...%s",
1024 src_abbrev
, dst_abbrev
);
1025 } else if (S_ISGITLINK(p
->mod_dst
)) {
1026 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
1027 "-1", dst_abbrev
, NULL
);
1029 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
1030 "-1", src_abbrev
, NULL
);
1032 run_command(&cp_log
);
1037 static void generate_submodule_summary(struct summary_cb
*info
,
1038 struct module_cb
*p
)
1040 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
1041 int missing_src
= 0, missing_dst
= 0;
1042 char *errmsg
= NULL
;
1043 int total_commits
= -1;
1045 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
1046 if (S_ISGITLINK(p
->mod_dst
)) {
1047 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
1049 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
1050 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
1052 int fd
= open(p
->sm_path
, O_RDONLY
);
1054 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
1055 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
1057 error(_("couldn't hash object from '%s'"), p
->sm_path
);
1059 /* for a submodule removal (mode:0000000), don't warn */
1061 warning(_("unexpected mode %o\n"), p
->mod_dst
);
1065 if (S_ISGITLINK(p
->mod_src
)) {
1066 if (p
->status
!= 'D')
1067 src_abbrev
= verify_submodule_committish(p
->sm_path
,
1068 oid_to_hex(&p
->oid_src
));
1072 * As `rev-parse` failed, we fallback to getting
1073 * the abbreviated hash using oid_src. We do
1074 * this as we might still need the abbreviated
1075 * hash in cases like a submodule type change, etc.
1077 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
1081 * The source does not point to a submodule.
1082 * So, we fallback to getting the abbreviation using
1083 * oid_src as we might still need the abbreviated
1084 * hash in cases like submodule add, etc.
1086 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
1089 if (S_ISGITLINK(p
->mod_dst
)) {
1090 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
1091 oid_to_hex(&p
->oid_dst
));
1095 * As `rev-parse` failed, we fallback to getting
1096 * the abbreviated hash using oid_dst. We do
1097 * this as we might still need the abbreviated
1098 * hash in cases like a submodule type change, etc.
1100 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
1104 * The destination does not point to a submodule.
1105 * So, we fallback to getting the abbreviation using
1106 * oid_dst as we might still need the abbreviated
1107 * hash in cases like a submodule removal, etc.
1109 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
1112 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
);
1114 if (!missing_src
&& !missing_dst
) {
1115 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
1116 struct strbuf sb_rev_list
= STRBUF_INIT
;
1118 strvec_pushl(&cp_rev_list
.args
, "rev-list",
1119 "--first-parent", "--count", NULL
);
1120 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
1121 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
1122 src_abbrev
, dst_abbrev
);
1124 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
1125 src_abbrev
: dst_abbrev
);
1126 strvec_push(&cp_rev_list
.args
, "--");
1128 cp_rev_list
.git_cmd
= 1;
1129 cp_rev_list
.dir
= p
->sm_path
;
1130 prepare_submodule_repo_env(&cp_rev_list
.env_array
);
1132 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
1133 total_commits
= atoi(sb_rev_list
.buf
);
1135 strbuf_release(&sb_rev_list
);
1138 * Don't give error msg for modification whose dst is not
1139 * submodule, i.e., deleted or changed to blob
1141 if (S_ISGITLINK(p
->mod_dst
)) {
1142 struct strbuf errmsg_str
= STRBUF_INIT
;
1143 if (missing_src
&& missing_dst
) {
1144 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commits %s and %s\n",
1145 displaypath
, oid_to_hex(&p
->oid_src
),
1146 oid_to_hex(&p
->oid_dst
));
1148 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commit %s\n",
1149 displaypath
, missing_src
?
1150 oid_to_hex(&p
->oid_src
) :
1151 oid_to_hex(&p
->oid_dst
));
1153 errmsg
= strbuf_detach(&errmsg_str
, NULL
);
1157 print_submodule_summary(info
, errmsg
, total_commits
,
1158 displaypath
, src_abbrev
,
1166 static void prepare_submodule_summary(struct summary_cb
*info
,
1167 struct module_cb_list
*list
)
1170 for (i
= 0; i
< list
->nr
; i
++) {
1171 const struct submodule
*sub
;
1172 struct module_cb
*p
= list
->entries
[i
];
1173 struct strbuf sm_gitdir
= STRBUF_INIT
;
1175 if (p
->status
== 'D' || p
->status
== 'T') {
1176 generate_submodule_summary(info
, p
);
1180 if (info
->for_status
&& p
->status
!= 'A' &&
1181 (sub
= submodule_from_path(the_repository
,
1182 null_oid(), p
->sm_path
))) {
1183 char *config_key
= NULL
;
1187 config_key
= xstrfmt("submodule.%s.ignore",
1189 if (!git_config_get_string_tmp(config_key
, &value
))
1190 ignore_all
= !strcmp(value
, "all");
1191 else if (sub
->ignore
)
1192 ignore_all
= !strcmp(sub
->ignore
, "all");
1199 /* Also show added or modified modules which are checked out */
1200 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1201 if (is_nonbare_repository_dir(&sm_gitdir
))
1202 generate_submodule_summary(info
, p
);
1203 strbuf_release(&sm_gitdir
);
1207 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1208 struct diff_options
*options
,
1212 struct module_cb_list
*list
= data
;
1213 for (i
= 0; i
< q
->nr
; i
++) {
1214 struct diff_filepair
*p
= q
->queue
[i
];
1215 struct module_cb
*temp
;
1217 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1219 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1220 temp
->mod_src
= p
->one
->mode
;
1221 temp
->mod_dst
= p
->two
->mode
;
1222 temp
->oid_src
= p
->one
->oid
;
1223 temp
->oid_dst
= p
->two
->oid
;
1224 temp
->status
= p
->status
;
1225 temp
->sm_path
= xstrdup(p
->one
->path
);
1227 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1228 list
->entries
[list
->nr
++] = temp
;
1232 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1235 case DIFF_INDEX
: return "diff-index";
1236 case DIFF_FILES
: return "diff-files";
1237 default: BUG("bad diff_cmd value %d", diff_cmd
);
1241 static int compute_summary_module_list(struct object_id
*head_oid
,
1242 struct summary_cb
*info
,
1243 enum diff_cmd diff_cmd
)
1245 struct strvec diff_args
= STRVEC_INIT
;
1246 struct rev_info rev
;
1247 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1249 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1251 strvec_push(&diff_args
, "--cached");
1252 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1254 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1255 strvec_push(&diff_args
, "--");
1257 strvec_pushv(&diff_args
, info
->argv
);
1259 git_config(git_diff_basic_config
, NULL
);
1260 init_revisions(&rev
, info
->prefix
);
1262 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1263 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, NULL
);
1264 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1265 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1266 rev
.diffopt
.format_callback_data
= &list
;
1268 if (!info
->cached
) {
1269 if (diff_cmd
== DIFF_INDEX
)
1271 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1272 perror("read_cache_preload");
1275 } else if (read_cache() < 0) {
1276 perror("read_cache");
1280 if (diff_cmd
== DIFF_INDEX
)
1281 run_diff_index(&rev
, info
->cached
);
1283 run_diff_files(&rev
, 0);
1284 prepare_submodule_summary(info
, &list
);
1285 strvec_clear(&diff_args
);
1289 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1291 struct summary_cb info
= SUMMARY_CB_INIT
;
1295 int summary_limit
= -1;
1296 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1297 struct object_id head_oid
;
1300 struct option module_summary_options
[] = {
1301 OPT_BOOL(0, "cached", &cached
,
1302 N_("use the commit stored in the index instead of the submodule HEAD")),
1303 OPT_BOOL(0, "files", &files
,
1304 N_("compare the commit in the index with that in the submodule HEAD")),
1305 OPT_BOOL(0, "for-status", &for_status
,
1306 N_("skip submodules with 'ignore_config' value set to 'all'")),
1307 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1308 N_("limit the summary size")),
1312 const char *const git_submodule_helper_usage
[] = {
1313 N_("git submodule--helper summary [<options>] [<commit>] [--] [<path>]"),
1317 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1318 git_submodule_helper_usage
, 0);
1323 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1328 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1329 /* before the first commit: compare with an empty tree */
1330 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1336 if (get_oid("HEAD", &head_oid
))
1337 die(_("could not fetch a revision for HEAD"));
1342 die(_("--cached and --files are mutually exclusive"));
1343 diff_cmd
= DIFF_FILES
;
1348 info
.prefix
= prefix
;
1349 info
.cached
= !!cached
;
1350 info
.files
= !!files
;
1351 info
.for_status
= !!for_status
;
1352 info
.summary_limit
= summary_limit
;
1354 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1363 #define SYNC_CB_INIT { NULL, 0 }
1365 static void sync_submodule(const char *path
, const char *prefix
,
1368 const struct submodule
*sub
;
1369 char *remote_key
= NULL
;
1370 char *sub_origin_url
, *super_config_url
, *displaypath
;
1371 struct strbuf sb
= STRBUF_INIT
;
1372 struct child_process cp
= CHILD_PROCESS_INIT
;
1373 char *sub_config_path
= NULL
;
1375 if (!is_submodule_active(the_repository
, path
))
1378 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1380 if (sub
&& sub
->url
) {
1381 if (starts_with_dot_dot_slash(sub
->url
) ||
1382 starts_with_dot_slash(sub
->url
)) {
1383 char *remote_url
, *up_path
;
1384 char *remote
= get_default_remote();
1385 strbuf_addf(&sb
, "remote.%s.url", remote
);
1387 if (git_config_get_string(sb
.buf
, &remote_url
))
1388 remote_url
= xgetcwd();
1390 up_path
= get_up_path(path
);
1391 sub_origin_url
= relative_url(remote_url
, sub
->url
, up_path
);
1392 super_config_url
= relative_url(remote_url
, sub
->url
, NULL
);
1398 sub_origin_url
= xstrdup(sub
->url
);
1399 super_config_url
= xstrdup(sub
->url
);
1402 sub_origin_url
= xstrdup("");
1403 super_config_url
= xstrdup("");
1406 displaypath
= get_submodule_displaypath(path
, prefix
);
1408 if (!(flags
& OPT_QUIET
))
1409 printf(_("Synchronizing submodule url for '%s'\n"),
1413 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1414 if (git_config_set_gently(sb
.buf
, super_config_url
))
1415 die(_("failed to register url for submodule path '%s'"),
1418 if (!is_submodule_populated_gently(path
, NULL
))
1421 prepare_submodule_repo_env(&cp
.env_array
);
1424 strvec_pushl(&cp
.args
, "submodule--helper",
1425 "print-default-remote", NULL
);
1428 if (capture_command(&cp
, &sb
, 0))
1429 die(_("failed to get the default remote for submodule '%s'"),
1432 strbuf_strip_suffix(&sb
, "\n");
1433 remote_key
= xstrfmt("remote.%s.url", sb
.buf
);
1436 submodule_to_gitdir(&sb
, path
);
1437 strbuf_addstr(&sb
, "/config");
1439 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1440 die(_("failed to update remote for submodule '%s'"),
1443 if (flags
& OPT_RECURSIVE
) {
1444 struct child_process cpr
= CHILD_PROCESS_INIT
;
1448 prepare_submodule_repo_env(&cpr
.env_array
);
1450 strvec_push(&cpr
.args
, "--super-prefix");
1451 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1452 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1453 "--recursive", NULL
);
1455 if (flags
& OPT_QUIET
)
1456 strvec_push(&cpr
.args
, "--quiet");
1458 if (run_command(&cpr
))
1459 die(_("failed to recurse into submodule '%s'"),
1464 free(super_config_url
);
1465 free(sub_origin_url
);
1466 strbuf_release(&sb
);
1469 free(sub_config_path
);
1472 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1474 struct sync_cb
*info
= cb_data
;
1475 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1478 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1480 struct sync_cb info
= SYNC_CB_INIT
;
1481 struct pathspec pathspec
;
1482 struct module_list list
= MODULE_LIST_INIT
;
1486 struct option module_sync_options
[] = {
1487 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1488 OPT_BOOL(0, "recursive", &recursive
,
1489 N_("recurse into nested submodules")),
1493 const char *const git_submodule_helper_usage
[] = {
1494 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1498 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1499 git_submodule_helper_usage
, 0);
1501 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1504 info
.prefix
= prefix
;
1506 info
.flags
|= OPT_QUIET
;
1508 info
.flags
|= OPT_RECURSIVE
;
1510 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1519 #define DEINIT_CB_INIT { NULL, 0 }
1521 static void deinit_submodule(const char *path
, const char *prefix
,
1524 const struct submodule
*sub
;
1525 char *displaypath
= NULL
;
1526 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1527 struct strbuf sb_config
= STRBUF_INIT
;
1528 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1530 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1532 if (!sub
|| !sub
->name
)
1535 displaypath
= get_submodule_displaypath(path
, prefix
);
1537 /* remove the submodule work tree (unless the user already did it) */
1538 if (is_directory(path
)) {
1539 struct strbuf sb_rm
= STRBUF_INIT
;
1543 * protect submodules containing a .git directory
1544 * NEEDSWORK: instead of dying, automatically call
1545 * absorbgitdirs and (possibly) warn.
1547 if (is_directory(sub_git_dir
))
1548 die(_("Submodule work tree '%s' contains a .git "
1549 "directory (use 'rm -rf' if you really want "
1550 "to remove it including all of its history)"),
1553 if (!(flags
& OPT_FORCE
)) {
1554 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1556 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1559 if (run_command(&cp_rm
))
1560 die(_("Submodule work tree '%s' contains local "
1561 "modifications; use '-f' to discard them"),
1565 strbuf_addstr(&sb_rm
, path
);
1567 if (!remove_dir_recursively(&sb_rm
, 0))
1568 format
= _("Cleared directory '%s'\n");
1570 format
= _("Could not remove submodule work tree '%s'\n");
1572 if (!(flags
& OPT_QUIET
))
1573 printf(format
, displaypath
);
1575 submodule_unset_core_worktree(sub
);
1577 strbuf_release(&sb_rm
);
1580 if (mkdir(path
, 0777))
1581 printf(_("could not create empty submodule directory %s"),
1584 cp_config
.git_cmd
= 1;
1585 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1586 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1588 /* remove the .git/config entries (unless the user already did it) */
1589 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1590 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1592 * remove the whole section so we have a clean state when
1593 * the user later decides to init this submodule again
1595 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1596 if (!(flags
& OPT_QUIET
))
1597 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1598 sub
->name
, sub
->url
, displaypath
);
1605 strbuf_release(&sb_config
);
1608 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1611 struct deinit_cb
*info
= cb_data
;
1612 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1615 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1617 struct deinit_cb info
= DEINIT_CB_INIT
;
1618 struct pathspec pathspec
;
1619 struct module_list list
= MODULE_LIST_INIT
;
1624 struct option module_deinit_options
[] = {
1625 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1626 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1627 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1631 const char *const git_submodule_helper_usage
[] = {
1632 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1636 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1637 git_submodule_helper_usage
, 0);
1640 error("pathspec and --all are incompatible");
1641 usage_with_options(git_submodule_helper_usage
,
1642 module_deinit_options
);
1646 die(_("Use '--all' if you really want to deinitialize all submodules"));
1648 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1651 info
.prefix
= prefix
;
1653 info
.flags
|= OPT_QUIET
;
1655 info
.flags
|= OPT_FORCE
;
1657 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1662 struct module_clone_data
{
1668 struct string_list reference
;
1669 unsigned int quiet
: 1;
1670 unsigned int progress
: 1;
1671 unsigned int dissociate
: 1;
1672 unsigned int require_init
: 1;
1675 #define MODULE_CLONE_DATA_INIT { .reference = STRING_LIST_INIT_NODUP, .single_branch = -1 }
1677 struct submodule_alternate_setup
{
1678 const char *submodule_name
;
1679 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1680 SUBMODULE_ALTERNATE_ERROR_DIE
,
1681 SUBMODULE_ALTERNATE_ERROR_INFO
,
1682 SUBMODULE_ALTERNATE_ERROR_IGNORE
1684 struct string_list
*reference
;
1686 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1687 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1689 static const char alternate_error_advice
[] = N_(
1690 "An alternate computed from a superproject's alternate is invalid.\n"
1691 "To allow Git to clone without an alternate in such a case, set\n"
1692 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1693 "'--reference-if-able' instead of '--reference'."
1696 static int add_possible_reference_from_superproject(
1697 struct object_directory
*odb
, void *sas_cb
)
1699 struct submodule_alternate_setup
*sas
= sas_cb
;
1703 * If the alternate object store is another repository, try the
1704 * standard layout with .git/(modules/<name>)+/objects
1706 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1708 struct strbuf sb
= STRBUF_INIT
;
1709 struct strbuf err
= STRBUF_INIT
;
1710 strbuf_add(&sb
, odb
->path
, len
);
1713 * We need to end the new path with '/' to mark it as a dir,
1714 * otherwise a submodule name containing '/' will be broken
1715 * as the last part of a missing submodule reference would
1716 * be taken as a file name.
1718 strbuf_addf(&sb
, "/modules/%s/", sas
->submodule_name
);
1720 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1722 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1725 switch (sas
->error_mode
) {
1726 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1727 if (advice_submodule_alternate_error_strategy_die
)
1728 advise(_(alternate_error_advice
));
1729 die(_("submodule '%s' cannot add alternate: %s"),
1730 sas
->submodule_name
, err
.buf
);
1731 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1732 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1733 sas
->submodule_name
, err
.buf
);
1734 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1738 strbuf_release(&sb
);
1744 static void prepare_possible_alternates(const char *sm_name
,
1745 struct string_list
*reference
)
1747 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1748 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1750 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1754 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1756 if (!error_strategy
)
1757 error_strategy
= xstrdup("die");
1759 sas
.submodule_name
= sm_name
;
1760 sas
.reference
= reference
;
1761 if (!strcmp(error_strategy
, "die"))
1762 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1763 else if (!strcmp(error_strategy
, "info"))
1764 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1765 else if (!strcmp(error_strategy
, "ignore"))
1766 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1768 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1770 if (!strcmp(sm_alternate
, "superproject"))
1771 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1772 else if (!strcmp(sm_alternate
, "no"))
1775 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1778 free(error_strategy
);
1781 static int clone_submodule(struct module_clone_data
*clone_data
)
1783 char *p
, *sm_gitdir
;
1784 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1785 struct strbuf sb
= STRBUF_INIT
;
1786 struct child_process cp
= CHILD_PROCESS_INIT
;
1788 strbuf_addf(&sb
, "%s/modules/%s", get_git_dir(), clone_data
->name
);
1789 sm_gitdir
= absolute_pathdup(sb
.buf
);
1792 if (!is_absolute_path(clone_data
->path
)) {
1793 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), clone_data
->path
);
1794 clone_data
->path
= strbuf_detach(&sb
, NULL
);
1796 clone_data
->path
= xstrdup(clone_data
->path
);
1799 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1800 die(_("refusing to create/use '%s' in another submodule's "
1801 "git dir"), sm_gitdir
);
1803 if (!file_exists(sm_gitdir
)) {
1804 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1805 die(_("could not create directory '%s'"), sm_gitdir
);
1807 prepare_possible_alternates(clone_data
->name
, &clone_data
->reference
);
1809 strvec_push(&cp
.args
, "clone");
1810 strvec_push(&cp
.args
, "--no-checkout");
1811 if (clone_data
->quiet
)
1812 strvec_push(&cp
.args
, "--quiet");
1813 if (clone_data
->progress
)
1814 strvec_push(&cp
.args
, "--progress");
1815 if (clone_data
->depth
&& *(clone_data
->depth
))
1816 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1817 if (clone_data
->reference
.nr
) {
1818 struct string_list_item
*item
;
1819 for_each_string_list_item(item
, &clone_data
->reference
)
1820 strvec_pushl(&cp
.args
, "--reference",
1821 item
->string
, NULL
);
1823 if (clone_data
->dissociate
)
1824 strvec_push(&cp
.args
, "--dissociate");
1825 if (sm_gitdir
&& *sm_gitdir
)
1826 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1827 if (clone_data
->single_branch
>= 0)
1828 strvec_push(&cp
.args
, clone_data
->single_branch
?
1830 "--no-single-branch");
1832 strvec_push(&cp
.args
, "--");
1833 strvec_push(&cp
.args
, clone_data
->url
);
1834 strvec_push(&cp
.args
, clone_data
->path
);
1837 prepare_submodule_repo_env(&cp
.env_array
);
1840 if(run_command(&cp
))
1841 die(_("clone of '%s' into submodule path '%s' failed"),
1842 clone_data
->url
, clone_data
->path
);
1844 if (clone_data
->require_init
&& !access(clone_data
->path
, X_OK
) &&
1845 !is_empty_dir(clone_data
->path
))
1846 die(_("directory not empty: '%s'"), clone_data
->path
);
1847 if (safe_create_leading_directories_const(clone_data
->path
) < 0)
1848 die(_("could not create directory '%s'"), clone_data
->path
);
1849 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1850 unlink_or_warn(sb
.buf
);
1854 connect_work_tree_and_git_dir(clone_data
->path
, sm_gitdir
, 0);
1856 p
= git_pathdup_submodule(clone_data
->path
, "config");
1858 die(_("could not get submodule directory for '%s'"), clone_data
->path
);
1860 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1861 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1863 git_config_set_in_file(p
, "submodule.alternateLocation",
1865 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1867 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1871 free(error_strategy
);
1873 strbuf_release(&sb
);
1879 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1881 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1882 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1884 struct option module_clone_options
[] = {
1885 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1887 N_("alternative anchor for relative paths")),
1888 OPT_STRING(0, "path", &clone_data
.path
,
1890 N_("where the new submodule will be cloned to")),
1891 OPT_STRING(0, "name", &clone_data
.name
,
1893 N_("name of the new submodule")),
1894 OPT_STRING(0, "url", &clone_data
.url
,
1896 N_("url where to clone the submodule from")),
1897 OPT_STRING_LIST(0, "reference", &clone_data
.reference
,
1899 N_("reference repository")),
1900 OPT_BOOL(0, "dissociate", &dissociate
,
1901 N_("use --reference only while cloning")),
1902 OPT_STRING(0, "depth", &clone_data
.depth
,
1904 N_("depth for shallow clones")),
1905 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
1906 OPT_BOOL(0, "progress", &progress
,
1907 N_("force cloning progress")),
1908 OPT_BOOL(0, "require-init", &require_init
,
1909 N_("disallow cloning into non-empty directory")),
1910 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1911 N_("clone only one branch, HEAD or --branch")),
1915 const char *const git_submodule_helper_usage
[] = {
1916 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1917 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1918 "[--single-branch] "
1919 "--url <url> --path <path>"),
1923 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1924 git_submodule_helper_usage
, 0);
1926 clone_data
.dissociate
= !!dissociate
;
1927 clone_data
.quiet
= !!quiet
;
1928 clone_data
.progress
= !!progress
;
1929 clone_data
.require_init
= !!require_init
;
1931 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1932 usage_with_options(git_submodule_helper_usage
,
1933 module_clone_options
);
1935 clone_submodule(&clone_data
);
1939 static void determine_submodule_update_strategy(struct repository
*r
,
1943 struct submodule_update_strategy
*out
)
1945 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1949 key
= xstrfmt("submodule.%s.update", sub
->name
);
1952 if (parse_submodule_update_strategy(update
, out
) < 0)
1953 die(_("Invalid update mode '%s' for submodule path '%s'"),
1955 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1956 if (parse_submodule_update_strategy(val
, out
) < 0)
1957 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1959 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1960 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1961 BUG("how did we read update = !command from .gitmodules?");
1962 out
->type
= sub
->update_strategy
.type
;
1963 out
->command
= sub
->update_strategy
.command
;
1965 out
->type
= SM_UPDATE_CHECKOUT
;
1968 (out
->type
== SM_UPDATE_MERGE
||
1969 out
->type
== SM_UPDATE_REBASE
||
1970 out
->type
== SM_UPDATE_NONE
))
1971 out
->type
= SM_UPDATE_CHECKOUT
;
1976 static int module_update_module_mode(int argc
, const char **argv
, const char *prefix
)
1978 const char *path
, *update
= NULL
;
1980 struct submodule_update_strategy update_strategy
= { .type
= SM_UPDATE_CHECKOUT
};
1982 if (argc
< 3 || argc
> 4)
1983 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1985 just_cloned
= git_config_int("just_cloned", argv
[1]);
1991 determine_submodule_update_strategy(the_repository
,
1992 just_cloned
, path
, update
,
1994 fputs(submodule_strategy_to_string(&update_strategy
), stdout
);
1999 struct update_clone_data
{
2000 const struct submodule
*sub
;
2001 struct object_id oid
;
2002 unsigned just_cloned
;
2005 struct submodule_update_clone
{
2006 /* index into 'list', the list of submodules to look into for cloning */
2008 struct module_list list
;
2009 unsigned warn_if_uninitialized
: 1;
2011 /* update parameter passed via commandline */
2012 struct submodule_update_strategy update
;
2014 /* configuration parameters which are passed on to the children */
2017 int recommend_shallow
;
2018 struct string_list references
;
2020 unsigned require_init
;
2022 const char *recursive_prefix
;
2026 /* to be consumed by git-submodule.sh */
2027 struct update_clone_data
*update_clone
;
2028 int update_clone_nr
; int update_clone_alloc
;
2030 /* If we want to stop as fast as possible and return an error */
2031 unsigned quickstop
: 1;
2033 /* failed clones to be retried again */
2034 const struct cache_entry
**failed_clones
;
2035 int failed_clones_nr
, failed_clones_alloc
;
2039 #define SUBMODULE_UPDATE_CLONE_INIT { \
2040 .list = MODULE_LIST_INIT, \
2041 .update = SUBMODULE_UPDATE_STRATEGY_INIT, \
2042 .recommend_shallow = -1, \
2043 .references = STRING_LIST_INIT_DUP, \
2044 .single_branch = -1, \
2049 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
2050 struct strbuf
*out
, const char *displaypath
)
2053 * Only mention uninitialized submodules when their
2054 * paths have been specified.
2056 if (suc
->warn_if_uninitialized
) {
2058 _("Submodule path '%s' not initialized"),
2060 strbuf_addch(out
, '\n');
2062 _("Maybe you want to use 'update --init'?"));
2063 strbuf_addch(out
, '\n');
2068 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2069 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2071 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
2072 struct child_process
*child
,
2073 struct submodule_update_clone
*suc
,
2076 const struct submodule
*sub
= NULL
;
2077 const char *url
= NULL
;
2078 const char *update_string
;
2079 enum submodule_update_type update_type
;
2081 struct strbuf displaypath_sb
= STRBUF_INIT
;
2082 struct strbuf sb
= STRBUF_INIT
;
2083 const char *displaypath
= NULL
;
2084 int needs_cloning
= 0;
2085 int need_free_url
= 0;
2088 if (suc
->recursive_prefix
)
2089 strbuf_addf(&sb
, "%s/%s", suc
->recursive_prefix
, ce
->name
);
2091 strbuf_addstr(&sb
, ce
->name
);
2092 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
2093 strbuf_addch(out
, '\n');
2097 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
2099 if (suc
->recursive_prefix
)
2100 displaypath
= relative_path(suc
->recursive_prefix
,
2101 ce
->name
, &displaypath_sb
);
2103 displaypath
= ce
->name
;
2106 next_submodule_warn_missing(suc
, out
, displaypath
);
2110 key
= xstrfmt("submodule.%s.update", sub
->name
);
2111 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2112 update_type
= parse_submodule_update_type(update_string
);
2114 update_type
= sub
->update_strategy
.type
;
2118 if (suc
->update
.type
== SM_UPDATE_NONE
2119 || (suc
->update
.type
== SM_UPDATE_UNSPECIFIED
2120 && update_type
== SM_UPDATE_NONE
)) {
2121 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2122 strbuf_addch(out
, '\n');
2126 /* Check if the submodule has been initialized. */
2127 if (!is_submodule_active(the_repository
, ce
->name
)) {
2128 next_submodule_warn_missing(suc
, out
, displaypath
);
2133 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2134 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2135 if (starts_with_dot_slash(sub
->url
) ||
2136 starts_with_dot_dot_slash(sub
->url
)) {
2137 url
= compute_submodule_clone_url(sub
->url
);
2144 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2145 needs_cloning
= !file_exists(sb
.buf
);
2147 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2148 suc
->update_clone_alloc
);
2149 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2150 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2151 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2152 suc
->update_clone_nr
++;
2158 child
->no_stdin
= 1;
2159 child
->stdout_to_stderr
= 1;
2161 strvec_push(&child
->args
, "submodule--helper");
2162 strvec_push(&child
->args
, "clone");
2164 strvec_push(&child
->args
, "--progress");
2166 strvec_push(&child
->args
, "--quiet");
2168 strvec_pushl(&child
->args
, "--prefix", suc
->prefix
, NULL
);
2169 if (suc
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2170 strvec_push(&child
->args
, "--depth=1");
2171 if (suc
->require_init
)
2172 strvec_push(&child
->args
, "--require-init");
2173 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2174 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2175 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2176 if (suc
->references
.nr
) {
2177 struct string_list_item
*item
;
2178 for_each_string_list_item(item
, &suc
->references
)
2179 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2181 if (suc
->dissociate
)
2182 strvec_push(&child
->args
, "--dissociate");
2184 strvec_push(&child
->args
, suc
->depth
);
2185 if (suc
->single_branch
>= 0)
2186 strvec_push(&child
->args
, suc
->single_branch
?
2188 "--no-single-branch");
2191 strbuf_release(&displaypath_sb
);
2192 strbuf_release(&sb
);
2196 return needs_cloning
;
2199 static int update_clone_get_next_task(struct child_process
*child
,
2204 struct submodule_update_clone
*suc
= suc_cb
;
2205 const struct cache_entry
*ce
;
2208 for (; suc
->current
< suc
->list
.nr
; suc
->current
++) {
2209 ce
= suc
->list
.entries
[suc
->current
];
2210 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2211 int *p
= xmalloc(sizeof(*p
));
2220 * The loop above tried cloning each submodule once, now try the
2221 * stragglers again, which we can imagine as an extension of the
2224 index
= suc
->current
- suc
->list
.nr
;
2225 if (index
< suc
->failed_clones_nr
) {
2227 ce
= suc
->failed_clones
[index
];
2228 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2230 strbuf_addstr(err
, "BUG: submodule considered for "
2231 "cloning, doesn't need cloning "
2235 p
= xmalloc(sizeof(*p
));
2245 static int update_clone_start_failure(struct strbuf
*err
,
2249 struct submodule_update_clone
*suc
= suc_cb
;
2254 static int update_clone_task_finished(int result
,
2259 const struct cache_entry
*ce
;
2260 struct submodule_update_clone
*suc
= suc_cb
;
2262 int *idxP
= idx_task_cb
;
2269 if (idx
< suc
->list
.nr
) {
2270 ce
= suc
->list
.entries
[idx
];
2271 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2273 strbuf_addch(err
, '\n');
2274 ALLOC_GROW(suc
->failed_clones
,
2275 suc
->failed_clones_nr
+ 1,
2276 suc
->failed_clones_alloc
);
2277 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2280 idx
-= suc
->list
.nr
;
2281 ce
= suc
->failed_clones
[idx
];
2282 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2284 strbuf_addch(err
, '\n');
2292 static int git_update_clone_config(const char *var
, const char *value
,
2296 if (!strcmp(var
, "submodule.fetchjobs"))
2297 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2301 static void update_submodule(struct update_clone_data
*ucd
)
2303 fprintf(stdout
, "dummy %s %d\t%s\n",
2304 oid_to_hex(&ucd
->oid
),
2309 static int update_submodules(struct submodule_update_clone
*suc
)
2313 run_processes_parallel_tr2(suc
->max_jobs
, update_clone_get_next_task
,
2314 update_clone_start_failure
,
2315 update_clone_task_finished
, suc
, "submodule",
2319 * We saved the output and put it out all at once now.
2321 * - the listener does not have to interleave their (checkout)
2322 * work with our fetching. The writes involved in a
2323 * checkout involve more straightforward sequential I/O.
2324 * - the listener can avoid doing any work if fetching failed.
2329 for (i
= 0; i
< suc
->update_clone_nr
; i
++)
2330 update_submodule(&suc
->update_clone
[i
]);
2335 static int update_clone(int argc
, const char **argv
, const char *prefix
)
2337 const char *update
= NULL
;
2338 struct pathspec pathspec
;
2339 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2341 struct option module_update_clone_options
[] = {
2342 OPT_STRING(0, "prefix", &prefix
,
2344 N_("path into the working tree")),
2345 OPT_STRING(0, "recursive-prefix", &suc
.recursive_prefix
,
2347 N_("path into the working tree, across nested "
2348 "submodule boundaries")),
2349 OPT_STRING(0, "update", &update
,
2351 N_("rebase, merge, checkout or none")),
2352 OPT_STRING_LIST(0, "reference", &suc
.references
, N_("repo"),
2353 N_("reference repository")),
2354 OPT_BOOL(0, "dissociate", &suc
.dissociate
,
2355 N_("use --reference only while cloning")),
2356 OPT_STRING(0, "depth", &suc
.depth
, "<depth>",
2357 N_("create a shallow clone truncated to the "
2358 "specified number of revisions")),
2359 OPT_INTEGER('j', "jobs", &suc
.max_jobs
,
2360 N_("parallel jobs")),
2361 OPT_BOOL(0, "recommend-shallow", &suc
.recommend_shallow
,
2362 N_("whether the initial clone should follow the shallow recommendation")),
2363 OPT__QUIET(&suc
.quiet
, N_("don't print cloning progress")),
2364 OPT_BOOL(0, "progress", &suc
.progress
,
2365 N_("force cloning progress")),
2366 OPT_BOOL(0, "require-init", &suc
.require_init
,
2367 N_("disallow cloning into non-empty directory")),
2368 OPT_BOOL(0, "single-branch", &suc
.single_branch
,
2369 N_("clone only one branch, HEAD or --branch")),
2373 const char *const git_submodule_helper_usage
[] = {
2374 N_("git submodule--helper update-clone [--prefix=<path>] [<path>...]"),
2377 suc
.prefix
= prefix
;
2379 update_clone_config_from_gitmodules(&suc
.max_jobs
);
2380 git_config(git_update_clone_config
, &suc
.max_jobs
);
2382 argc
= parse_options(argc
, argv
, prefix
, module_update_clone_options
,
2383 git_submodule_helper_usage
, 0);
2386 if (parse_submodule_update_strategy(update
, &suc
.update
) < 0)
2387 die(_("bad value for update parameter"));
2389 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &suc
.list
) < 0)
2393 suc
.warn_if_uninitialized
= 1;
2395 return update_submodules(&suc
);
2398 static int resolve_relative_path(int argc
, const char **argv
, const char *prefix
)
2400 struct strbuf sb
= STRBUF_INIT
;
2402 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc
);
2404 printf("%s", relative_path(argv
[1], argv
[2], &sb
));
2405 strbuf_release(&sb
);
2409 static const char *remote_submodule_branch(const char *path
)
2411 const struct submodule
*sub
;
2412 const char *branch
= NULL
;
2415 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2419 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2420 if (repo_config_get_string_tmp(the_repository
, key
, &branch
))
2421 branch
= sub
->branch
;
2427 if (!strcmp(branch
, ".")) {
2428 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2431 die(_("No such ref: %s"), "HEAD");
2434 if (!strcmp(refname
, "HEAD"))
2435 die(_("Submodule (%s) branch configured to inherit "
2436 "branch from superproject, but the superproject "
2437 "is not on any branch"), sub
->name
);
2439 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2440 die(_("Expecting a full ref name, got %s"), refname
);
2447 static int resolve_remote_submodule_branch(int argc
, const char **argv
,
2451 struct strbuf sb
= STRBUF_INIT
;
2453 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc
);
2455 ret
= remote_submodule_branch(argv
[1]);
2457 die("submodule %s doesn't exist", argv
[1]);
2460 strbuf_release(&sb
);
2464 static int push_check(int argc
, const char **argv
, const char *prefix
)
2466 struct remote
*remote
;
2467 const char *superproject_head
;
2469 int detached_head
= 0;
2470 struct object_id head_oid
;
2473 die("submodule--helper push-check requires at least 2 arguments");
2476 * superproject's resolved head ref.
2477 * if HEAD then the superproject is in a detached head state, otherwise
2478 * it will be the resolved head ref.
2480 superproject_head
= argv
[1];
2483 /* Get the submodule's head ref and determine if it is detached */
2484 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2486 die(_("Failed to resolve HEAD as a valid ref."));
2487 if (!strcmp(head
, "HEAD"))
2491 * The remote must be configured.
2492 * This is to avoid pushing to the exact same URL as the parent.
2494 remote
= pushremote_get(argv
[1]);
2495 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2496 die("remote '%s' not configured", argv
[1]);
2498 /* Check the refspec */
2501 struct ref
*local_refs
= get_local_heads();
2502 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2504 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2506 for (i
= 0; i
< refspec
.nr
; i
++) {
2507 const struct refspec_item
*rs
= &refspec
.items
[i
];
2509 if (rs
->pattern
|| rs
->matching
)
2512 /* LHS must match a single ref */
2513 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2518 * If LHS matches 'HEAD' then we need to ensure
2519 * that it matches the same named branch
2520 * checked out in the superproject.
2522 if (!strcmp(rs
->src
, "HEAD")) {
2523 if (!detached_head
&&
2524 !strcmp(head
, superproject_head
))
2526 die("HEAD does not match the named branch in the superproject");
2530 die("src refspec '%s' must name a ref",
2534 refspec_clear(&refspec
);
2541 static int ensure_core_worktree(int argc
, const char **argv
, const char *prefix
)
2543 const struct submodule
*sub
;
2546 struct repository subrepo
;
2549 BUG("submodule--helper ensure-core-worktree <path>");
2553 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2555 BUG("We could get the submodule handle before?");
2557 if (repo_submodule_init(&subrepo
, the_repository
, sub
))
2558 die(_("could not get a repository handle for submodule '%s'"), path
);
2560 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2561 char *cfg_file
, *abs_path
;
2562 const char *rel_path
;
2563 struct strbuf sb
= STRBUF_INIT
;
2565 cfg_file
= repo_git_path(&subrepo
, "config");
2567 abs_path
= absolute_pathdup(path
);
2568 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2570 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2574 strbuf_release(&sb
);
2580 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2583 struct pathspec pathspec
;
2584 struct module_list list
= MODULE_LIST_INIT
;
2585 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
2587 struct option embed_gitdir_options
[] = {
2588 OPT_STRING(0, "prefix", &prefix
,
2590 N_("path into the working tree")),
2591 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
2592 ABSORB_GITDIR_RECURSE_SUBMODULES
),
2596 const char *const git_submodule_helper_usage
[] = {
2597 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2601 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2602 git_submodule_helper_usage
, 0);
2604 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
2607 for (i
= 0; i
< list
.nr
; i
++)
2608 absorb_git_dir_into_superproject(list
.entries
[i
]->name
, flags
);
2613 static int is_active(int argc
, const char **argv
, const char *prefix
)
2616 die("submodule--helper is-active takes exactly 1 argument");
2618 return !is_submodule_active(the_repository
, argv
[1]);
2622 * Exit non-zero if any of the submodule names given on the command line is
2623 * invalid. If no names are given, filter stdin to print only valid names
2624 * (which is primarily intended for testing).
2626 static int check_name(int argc
, const char **argv
, const char *prefix
)
2630 if (check_submodule_name(*argv
) < 0)
2634 struct strbuf buf
= STRBUF_INIT
;
2635 while (strbuf_getline(&buf
, stdin
) != EOF
) {
2636 if (!check_submodule_name(buf
.buf
))
2637 printf("%s\n", buf
.buf
);
2639 strbuf_release(&buf
);
2644 static int module_config(int argc
, const char **argv
, const char *prefix
)
2647 CHECK_WRITEABLE
= 1,
2651 struct option module_config_options
[] = {
2652 OPT_CMDMODE(0, "check-writeable", &command
,
2653 N_("check if it is safe to write to the .gitmodules file"),
2655 OPT_CMDMODE(0, "unset", &command
,
2656 N_("unset the config in the .gitmodules file"),
2660 const char *const git_submodule_helper_usage
[] = {
2661 N_("git submodule--helper config <name> [<value>]"),
2662 N_("git submodule--helper config --unset <name>"),
2663 N_("git submodule--helper config --check-writeable"),
2667 argc
= parse_options(argc
, argv
, prefix
, module_config_options
,
2668 git_submodule_helper_usage
, PARSE_OPT_KEEP_ARGV0
);
2670 if (argc
== 1 && command
== CHECK_WRITEABLE
)
2671 return is_writing_gitmodules_ok() ? 0 : -1;
2673 /* Equivalent to ACTION_GET in builtin/config.c */
2674 if (argc
== 2 && command
!= DO_UNSET
)
2675 return print_config_from_gitmodules(the_repository
, argv
[1]);
2677 /* Equivalent to ACTION_SET in builtin/config.c */
2678 if (argc
== 3 || (argc
== 2 && command
== DO_UNSET
)) {
2679 const char *value
= (argc
== 3) ? argv
[2] : NULL
;
2681 if (!is_writing_gitmodules_ok())
2682 die(_("please make sure that the .gitmodules file is in the working tree"));
2684 return config_set_in_gitmodules_file_gently(argv
[1], value
);
2687 usage_with_options(git_submodule_helper_usage
, module_config_options
);
2690 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2697 struct option options
[] = {
2698 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2701 const char *const usage
[] = {
2702 N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
2706 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2708 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2709 usage_with_options(usage
, options
);
2711 config_name
= xstrfmt("submodule.%s.url", path
);
2713 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2714 sync_submodule(path
, prefix
, quiet
? OPT_QUIET
: 0);
2721 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2723 int opt_default
= 0, ret
;
2724 const char *opt_branch
= NULL
;
2729 * We accept the `quiet` option for uniformity across subcommands,
2730 * though there is nothing to make less verbose in this subcommand.
2732 struct option options
[] = {
2733 OPT_NOOP_NOARG('q', "quiet"),
2734 OPT_BOOL('d', "default", &opt_default
,
2735 N_("set the default tracking branch to master")),
2736 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2737 N_("set the default tracking branch")),
2740 const char *const usage
[] = {
2741 N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
2742 N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2746 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2748 if (!opt_branch
&& !opt_default
)
2749 die(_("--branch or --default required"));
2751 if (opt_branch
&& opt_default
)
2752 die(_("--branch and --default are mutually exclusive"));
2754 if (argc
!= 1 || !(path
= argv
[0]))
2755 usage_with_options(usage
, options
);
2757 config_name
= xstrfmt("submodule.%s.branch", path
);
2758 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2767 const char *reference_path
;
2768 const char *sm_path
;
2769 const char *sm_name
;
2771 const char *realrepo
;
2773 unsigned int force
: 1;
2774 unsigned int quiet
: 1;
2775 unsigned int progress
: 1;
2776 unsigned int dissociate
: 1;
2778 #define ADD_DATA_INIT { .depth = -1 }
2780 static void show_fetch_remotes(FILE *output
, const char *git_dir_path
)
2782 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
2783 struct strbuf sb_remote_out
= STRBUF_INIT
;
2785 cp_remote
.git_cmd
= 1;
2786 strvec_pushf(&cp_remote
.env_array
,
2787 "GIT_DIR=%s", git_dir_path
);
2788 strvec_push(&cp_remote
.env_array
, "GIT_WORK_TREE=.");
2789 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
2790 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
2792 char *line
= sb_remote_out
.buf
;
2793 while ((next_line
= strchr(line
, '\n')) != NULL
) {
2794 size_t len
= next_line
- line
;
2795 if (strip_suffix_mem(line
, &len
, " (fetch)"))
2796 fprintf(output
, " %.*s\n", (int)len
, line
);
2797 line
= next_line
+ 1;
2801 strbuf_release(&sb_remote_out
);
2804 static int add_submodule(const struct add_data
*add_data
)
2806 char *submod_gitdir_path
;
2807 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
2809 /* perhaps the path already exists and is already a git repo, else clone it */
2810 if (is_directory(add_data
->sm_path
)) {
2811 struct strbuf sm_path
= STRBUF_INIT
;
2812 strbuf_addstr(&sm_path
, add_data
->sm_path
);
2813 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
2814 if (is_nonbare_repository_dir(&sm_path
))
2815 printf(_("Adding existing repo at '%s' to the index\n"),
2818 die(_("'%s' already exists and is not a valid git repo"),
2820 strbuf_release(&sm_path
);
2821 free(submod_gitdir_path
);
2823 struct child_process cp
= CHILD_PROCESS_INIT
;
2824 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
2826 if (is_directory(submod_gitdir_path
)) {
2827 if (!add_data
->force
) {
2828 fprintf(stderr
, _("A git directory for '%s' is found "
2829 "locally with remote(s):"),
2831 show_fetch_remotes(stderr
, submod_gitdir_path
);
2832 free(submod_gitdir_path
);
2833 die(_("If you want to reuse this local git "
2834 "directory instead of cloning again from\n"
2836 "use the '--force' option. If the local git "
2837 "directory is not the correct repo\n"
2838 "or if you are unsure what this means, choose "
2839 "another name with the '--name' option.\n"),
2840 add_data
->realrepo
);
2842 printf(_("Reactivating local git directory for "
2843 "submodule '%s'\n"), add_data
->sm_name
);
2846 free(submod_gitdir_path
);
2848 clone_data
.prefix
= add_data
->prefix
;
2849 clone_data
.path
= add_data
->sm_path
;
2850 clone_data
.name
= add_data
->sm_name
;
2851 clone_data
.url
= add_data
->realrepo
;
2852 clone_data
.quiet
= add_data
->quiet
;
2853 clone_data
.progress
= add_data
->progress
;
2854 if (add_data
->reference_path
)
2855 string_list_append(&clone_data
.reference
,
2856 xstrdup(add_data
->reference_path
));
2857 clone_data
.dissociate
= add_data
->dissociate
;
2858 if (add_data
->depth
>= 0)
2859 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
2861 if (clone_submodule(&clone_data
))
2864 prepare_submodule_repo_env(&cp
.env_array
);
2866 cp
.dir
= add_data
->sm_path
;
2867 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
2869 if (add_data
->branch
) {
2870 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
2871 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
2874 if (run_command(&cp
))
2875 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
2880 static int add_clone(int argc
, const char **argv
, const char *prefix
)
2882 int force
= 0, quiet
= 0, dissociate
= 0, progress
= 0;
2883 struct add_data add_data
= ADD_DATA_INIT
;
2885 struct option options
[] = {
2886 OPT_STRING('b', "branch", &add_data
.branch
,
2888 N_("branch of repository to checkout on cloning")),
2889 OPT_STRING(0, "prefix", &prefix
,
2891 N_("alternative anchor for relative paths")),
2892 OPT_STRING(0, "path", &add_data
.sm_path
,
2894 N_("where the new submodule will be cloned to")),
2895 OPT_STRING(0, "name", &add_data
.sm_name
,
2897 N_("name of the new submodule")),
2898 OPT_STRING(0, "url", &add_data
.realrepo
,
2900 N_("url where to clone the submodule from")),
2901 OPT_STRING(0, "reference", &add_data
.reference_path
,
2903 N_("reference repository")),
2904 OPT_BOOL(0, "dissociate", &dissociate
,
2905 N_("use --reference only while cloning")),
2906 OPT_INTEGER(0, "depth", &add_data
.depth
,
2907 N_("depth for shallow clones")),
2908 OPT_BOOL(0, "progress", &progress
,
2909 N_("force cloning progress")),
2910 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
2911 PARSE_OPT_NOCOMPLETE
),
2912 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
2916 const char *const usage
[] = {
2917 N_("git submodule--helper add-clone [<options>...] "
2918 "--url <url> --path <path> --name <name>"),
2922 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2925 usage_with_options(usage
, options
);
2927 add_data
.prefix
= prefix
;
2928 add_data
.progress
= !!progress
;
2929 add_data
.dissociate
= !!dissociate
;
2930 add_data
.force
= !!force
;
2931 add_data
.quiet
= !!quiet
;
2933 if (add_submodule(&add_data
))
2939 #define SUPPORT_SUPER_PREFIX (1<<0)
2943 int (*fn
)(int, const char **, const char *);
2947 static struct cmd_struct commands
[] = {
2948 {"list", module_list
, 0},
2949 {"name", module_name
, 0},
2950 {"clone", module_clone
, 0},
2951 {"add-clone", add_clone
, 0},
2952 {"update-module-mode", module_update_module_mode
, 0},
2953 {"update-clone", update_clone
, 0},
2954 {"ensure-core-worktree", ensure_core_worktree
, 0},
2955 {"relative-path", resolve_relative_path
, 0},
2956 {"resolve-relative-url", resolve_relative_url
, 0},
2957 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
2958 {"foreach", module_foreach
, SUPPORT_SUPER_PREFIX
},
2959 {"init", module_init
, SUPPORT_SUPER_PREFIX
},
2960 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
2961 {"print-default-remote", print_default_remote
, 0},
2962 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
2963 {"deinit", module_deinit
, 0},
2964 {"summary", module_summary
, SUPPORT_SUPER_PREFIX
},
2965 {"remote-branch", resolve_remote_submodule_branch
, 0},
2966 {"push-check", push_check
, 0},
2967 {"absorb-git-dirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
2968 {"is-active", is_active
, 0},
2969 {"check-name", check_name
, 0},
2970 {"config", module_config
, 0},
2971 {"set-url", module_set_url
, 0},
2972 {"set-branch", module_set_branch
, 0},
2975 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
2978 if (argc
< 2 || !strcmp(argv
[1], "-h"))
2979 usage("git submodule--helper <command>");
2981 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
2982 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
2983 if (get_super_prefix() &&
2984 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
2985 die(_("%s doesn't support --super-prefix"),
2987 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
2991 die(_("'%s' is not a valid submodule--helper "
2992 "subcommand"), argv
[1]);