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 char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
204 char *remoteurl
, *resolved_url
;
205 char *remote
= get_default_remote();
206 struct strbuf remotesb
= STRBUF_INIT
;
208 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
209 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
211 warning(_("could not look up configuration '%s'. "
212 "Assuming this repository is its own "
213 "authoritative upstream."),
215 remoteurl
= xgetcwd();
217 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
221 strbuf_release(&remotesb
);
226 static int resolve_relative_url_test(int argc
, const char **argv
, const char *prefix
)
228 char *remoteurl
, *res
;
229 const char *up_path
, *url
;
232 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
235 remoteurl
= xstrdup(argv
[2]);
238 if (!strcmp(up_path
, "(null)"))
241 res
= relative_url(remoteurl
, url
, up_path
);
248 /* the result should be freed by the caller. */
249 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
251 const char *super_prefix
= get_super_prefix();
253 if (prefix
&& super_prefix
) {
254 BUG("cannot have prefix '%s' and superprefix '%s'",
255 prefix
, super_prefix
);
257 struct strbuf sb
= STRBUF_INIT
;
258 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
261 } else if (super_prefix
) {
262 return xstrfmt("%s%s", super_prefix
, path
);
264 return xstrdup(path
);
268 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
270 struct strbuf sb
= STRBUF_INIT
;
273 static const char *describe_bare
[] = { NULL
};
275 static const char *describe_tags
[] = { "--tags", NULL
};
277 static const char *describe_contains
[] = { "--contains", NULL
};
279 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
281 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
283 describe_all_always
, NULL
};
285 for (d
= describe_argv
; *d
; d
++) {
286 struct child_process cp
= CHILD_PROCESS_INIT
;
287 prepare_submodule_repo_env(&cp
.env_array
);
292 strvec_push(&cp
.args
, "describe");
293 strvec_pushv(&cp
.args
, *d
);
294 strvec_push(&cp
.args
, object_id
);
296 if (!capture_command(&cp
, &sb
, 0)) {
297 strbuf_strip_suffix(&sb
, "\n");
298 return strbuf_detach(&sb
, NULL
);
307 const struct cache_entry
**entries
;
310 #define MODULE_LIST_INIT { 0 }
312 static int module_list_compute(int argc
, const char **argv
,
314 struct pathspec
*pathspec
,
315 struct module_list
*list
)
318 char *ps_matched
= NULL
;
319 parse_pathspec(pathspec
, 0,
320 PATHSPEC_PREFER_FULL
,
324 ps_matched
= xcalloc(pathspec
->nr
, 1);
326 if (read_cache() < 0)
327 die(_("index file corrupt"));
329 for (i
= 0; i
< active_nr
; i
++) {
330 const struct cache_entry
*ce
= active_cache
[i
];
332 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
334 !S_ISGITLINK(ce
->ce_mode
))
337 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
338 list
->entries
[list
->nr
++] = ce
;
339 while (i
+ 1 < active_nr
&&
340 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
342 * Skip entries with the same name in different stages
343 * to make sure an entry is returned only once.
348 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
356 static void module_list_active(struct module_list
*list
)
359 struct module_list active_modules
= MODULE_LIST_INIT
;
361 for (i
= 0; i
< list
->nr
; i
++) {
362 const struct cache_entry
*ce
= list
->entries
[i
];
364 if (!is_submodule_active(the_repository
, ce
->name
))
367 ALLOC_GROW(active_modules
.entries
,
368 active_modules
.nr
+ 1,
369 active_modules
.alloc
);
370 active_modules
.entries
[active_modules
.nr
++] = ce
;
374 *list
= active_modules
;
377 static char *get_up_path(const char *path
)
380 struct strbuf sb
= STRBUF_INIT
;
382 for (i
= count_slashes(path
); i
; i
--)
383 strbuf_addstr(&sb
, "../");
386 * Check if 'path' ends with slash or not
387 * for having the same output for dir/sub_dir
390 if (!is_dir_sep(path
[strlen(path
) - 1]))
391 strbuf_addstr(&sb
, "../");
393 return strbuf_detach(&sb
, NULL
);
396 static int module_list(int argc
, const char **argv
, const char *prefix
)
399 struct pathspec pathspec
;
400 struct module_list list
= MODULE_LIST_INIT
;
402 struct option module_list_options
[] = {
403 OPT_STRING(0, "prefix", &prefix
,
405 N_("alternative anchor for relative paths")),
409 const char *const git_submodule_helper_usage
[] = {
410 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
414 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
415 git_submodule_helper_usage
, 0);
417 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
420 for (i
= 0; i
< list
.nr
; i
++) {
421 const struct cache_entry
*ce
= list
.entries
[i
];
424 printf("%06o %s U\t", ce
->ce_mode
,
425 oid_to_hex(null_oid()));
427 printf("%06o %s %d\t", ce
->ce_mode
,
428 oid_to_hex(&ce
->oid
), ce_stage(ce
));
430 fprintf(stdout
, "%s\n", ce
->name
);
435 static void for_each_listed_submodule(const struct module_list
*list
,
436 each_submodule_fn fn
, void *cb_data
)
439 for (i
= 0; i
< list
->nr
; i
++)
440 fn(list
->entries
[i
], cb_data
);
450 #define FOREACH_CB_INIT { 0 }
452 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
455 struct foreach_cb
*info
= cb_data
;
456 const char *path
= list_item
->name
;
457 const struct object_id
*ce_oid
= &list_item
->oid
;
459 const struct submodule
*sub
;
460 struct child_process cp
= CHILD_PROCESS_INIT
;
463 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
465 sub
= submodule_from_path(the_repository
, null_oid(), path
);
468 die(_("No url found for submodule path '%s' in .gitmodules"),
471 if (!is_submodule_populated_gently(path
, NULL
))
474 prepare_submodule_repo_env(&cp
.env_array
);
477 * For the purpose of executing <command> in the submodule,
478 * separate shell is used for the purpose of running the
485 * NEEDSWORK: the command currently has access to the variables $name,
486 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
487 * contains a single argument. This is done for maintaining a faithful
488 * translation from shell script.
490 if (info
->argc
== 1) {
491 char *toplevel
= xgetcwd();
492 struct strbuf sb
= STRBUF_INIT
;
494 strvec_pushf(&cp
.env_array
, "name=%s", sub
->name
);
495 strvec_pushf(&cp
.env_array
, "sm_path=%s", path
);
496 strvec_pushf(&cp
.env_array
, "displaypath=%s", displaypath
);
497 strvec_pushf(&cp
.env_array
, "sha1=%s",
499 strvec_pushf(&cp
.env_array
, "toplevel=%s", toplevel
);
502 * Since the path variable was accessible from the script
503 * before porting, it is also made available after porting.
504 * The environment variable "PATH" has a very special purpose
505 * on windows. And since environment variables are
506 * case-insensitive in windows, it interferes with the
507 * existing PATH variable. Hence, to avoid that, we expose
508 * path via the args strvec and not via env_array.
510 sq_quote_buf(&sb
, path
);
511 strvec_pushf(&cp
.args
, "path=%s; %s",
512 sb
.buf
, info
->argv
[0]);
516 strvec_pushv(&cp
.args
, info
->argv
);
520 printf(_("Entering '%s'\n"), displaypath
);
522 if (info
->argv
[0] && run_command(&cp
))
523 die(_("run_command returned non-zero status for %s\n."),
526 if (info
->recursive
) {
527 struct child_process cpr
= CHILD_PROCESS_INIT
;
531 prepare_submodule_repo_env(&cpr
.env_array
);
533 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
534 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
535 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
539 strvec_push(&cpr
.args
, "--quiet");
541 strvec_push(&cpr
.args
, "--");
542 strvec_pushv(&cpr
.args
, info
->argv
);
544 if (run_command(&cpr
))
545 die(_("run_command returned non-zero status while "
546 "recursing in the nested submodules of %s\n."),
554 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
556 struct foreach_cb info
= FOREACH_CB_INIT
;
557 struct pathspec pathspec
;
558 struct module_list list
= MODULE_LIST_INIT
;
560 struct option module_foreach_options
[] = {
561 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
562 OPT_BOOL(0, "recursive", &info
.recursive
,
563 N_("recurse into nested submodules")),
567 const char *const git_submodule_helper_usage
[] = {
568 N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"),
572 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
573 git_submodule_helper_usage
, 0);
575 if (module_list_compute(0, NULL
, prefix
, &pathspec
, &list
) < 0)
580 info
.prefix
= prefix
;
582 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
591 #define INIT_CB_INIT { 0 }
593 static void init_submodule(const char *path
, const char *prefix
,
596 const struct submodule
*sub
;
597 struct strbuf sb
= STRBUF_INIT
;
598 char *upd
= NULL
, *url
= NULL
, *displaypath
;
600 displaypath
= get_submodule_displaypath(path
, prefix
);
602 sub
= submodule_from_path(the_repository
, null_oid(), path
);
605 die(_("No url found for submodule path '%s' in .gitmodules"),
609 * NEEDSWORK: In a multi-working-tree world, this needs to be
610 * set in the per-worktree config.
612 * Set active flag for the submodule being initialized
614 if (!is_submodule_active(the_repository
, path
)) {
615 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
616 git_config_set_gently(sb
.buf
, "true");
621 * Copy url setting when it is not set yet.
622 * To look up the url in .git/config, we must not fall back to
623 * .gitmodules, so look it up directly.
625 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
626 if (git_config_get_string(sb
.buf
, &url
)) {
628 die(_("No url found for submodule path '%s' in .gitmodules"),
631 url
= xstrdup(sub
->url
);
633 /* Possibly a url relative to parent */
634 if (starts_with_dot_dot_slash(url
) ||
635 starts_with_dot_slash(url
)) {
637 url
= resolve_relative_url(oldurl
, NULL
, 0);
641 if (git_config_set_gently(sb
.buf
, url
))
642 die(_("Failed to register url for submodule path '%s'"),
644 if (!(flags
& OPT_QUIET
))
646 _("Submodule '%s' (%s) registered for path '%s'\n"),
647 sub
->name
, url
, displaypath
);
651 /* Copy "update" setting when it is not set yet */
652 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
653 if (git_config_get_string(sb
.buf
, &upd
) &&
654 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
655 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
656 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
658 upd
= xstrdup("none");
660 upd
= xstrdup(submodule_strategy_to_string(&sub
->update_strategy
));
662 if (git_config_set_gently(sb
.buf
, upd
))
663 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
671 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
673 struct init_cb
*info
= cb_data
;
674 init_submodule(list_item
->name
, info
->prefix
, info
->flags
);
677 static int module_init(int argc
, const char **argv
, const char *prefix
)
679 struct init_cb info
= INIT_CB_INIT
;
680 struct pathspec pathspec
;
681 struct module_list list
= MODULE_LIST_INIT
;
684 struct option module_init_options
[] = {
685 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
689 const char *const git_submodule_helper_usage
[] = {
690 N_("git submodule--helper init [<options>] [<path>]"),
694 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
695 git_submodule_helper_usage
, 0);
697 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
701 * If there are no path args and submodule.active is set then,
702 * by default, only initialize 'active' modules.
704 if (!argc
&& git_config_get_value_multi("submodule.active"))
705 module_list_active(&list
);
707 info
.prefix
= prefix
;
709 info
.flags
|= OPT_QUIET
;
711 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
720 #define STATUS_CB_INIT { 0 }
722 static void print_status(unsigned int flags
, char state
, const char *path
,
723 const struct object_id
*oid
, const char *displaypath
)
725 if (flags
& OPT_QUIET
)
728 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
730 if (state
== ' ' || state
== '+') {
731 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
734 printf(" (%s)", name
);
740 static int handle_submodule_head_ref(const char *refname
,
741 const struct object_id
*oid
, int flags
,
744 struct object_id
*output
= cb_data
;
751 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
752 unsigned int ce_flags
, const char *prefix
,
756 struct strvec diff_files_args
= STRVEC_INIT
;
758 int diff_files_result
;
759 struct strbuf buf
= STRBUF_INIT
;
762 if (!submodule_from_path(the_repository
, null_oid(), path
))
763 die(_("no submodule mapping found in .gitmodules for path '%s'"),
766 displaypath
= get_submodule_displaypath(path
, prefix
);
768 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
769 print_status(flags
, 'U', path
, null_oid(), displaypath
);
773 strbuf_addf(&buf
, "%s/.git", path
);
774 git_dir
= read_gitfile(buf
.buf
);
778 if (!is_submodule_active(the_repository
, path
) ||
779 !is_git_directory(git_dir
)) {
780 print_status(flags
, '-', path
, ce_oid
, displaypath
);
781 strbuf_release(&buf
);
784 strbuf_release(&buf
);
786 strvec_pushl(&diff_files_args
, "diff-files",
787 "--ignore-submodules=dirty", "--quiet", "--",
790 git_config(git_diff_basic_config
, NULL
);
792 repo_init_revisions(the_repository
, &rev
, NULL
);
794 diff_files_args
.nr
= setup_revisions(diff_files_args
.nr
,
797 diff_files_result
= run_diff_files(&rev
, 0);
799 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
800 print_status(flags
, ' ', path
, ce_oid
,
802 } else if (!(flags
& OPT_CACHED
)) {
803 struct object_id oid
;
804 struct ref_store
*refs
= get_submodule_ref_store(path
);
807 print_status(flags
, '-', path
, ce_oid
, displaypath
);
810 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
811 die(_("could not resolve HEAD ref inside the "
812 "submodule '%s'"), path
);
814 print_status(flags
, '+', path
, &oid
, displaypath
);
816 print_status(flags
, '+', path
, ce_oid
, displaypath
);
819 if (flags
& OPT_RECURSIVE
) {
820 struct child_process cpr
= CHILD_PROCESS_INIT
;
824 prepare_submodule_repo_env(&cpr
.env_array
);
826 strvec_push(&cpr
.args
, "--super-prefix");
827 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
828 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
829 "--recursive", NULL
);
831 if (flags
& OPT_CACHED
)
832 strvec_push(&cpr
.args
, "--cached");
834 if (flags
& OPT_QUIET
)
835 strvec_push(&cpr
.args
, "--quiet");
837 if (run_command(&cpr
))
838 die(_("failed to recurse into submodule '%s'"), path
);
842 strvec_clear(&diff_files_args
);
846 static void status_submodule_cb(const struct cache_entry
*list_item
,
849 struct status_cb
*info
= cb_data
;
850 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
851 info
->prefix
, info
->flags
);
854 static int module_status(int argc
, const char **argv
, const char *prefix
)
856 struct status_cb info
= STATUS_CB_INIT
;
857 struct pathspec pathspec
;
858 struct module_list list
= MODULE_LIST_INIT
;
861 struct option module_status_options
[] = {
862 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
863 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
864 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
868 const char *const git_submodule_helper_usage
[] = {
869 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
873 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
874 git_submodule_helper_usage
, 0);
876 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
879 info
.prefix
= prefix
;
881 info
.flags
|= OPT_QUIET
;
883 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
888 static int module_name(int argc
, const char **argv
, const char *prefix
)
890 const struct submodule
*sub
;
893 usage(_("git submodule--helper name <path>"));
895 sub
= submodule_from_path(the_repository
, null_oid(), argv
[1]);
898 die(_("no submodule mapping found in .gitmodules for path '%s'"),
901 printf("%s\n", sub
->name
);
907 unsigned int mod_src
;
908 unsigned int mod_dst
;
909 struct object_id oid_src
;
910 struct object_id oid_dst
;
914 #define MODULE_CB_INIT { 0 }
916 struct module_cb_list
{
917 struct module_cb
**entries
;
920 #define MODULE_CB_LIST_INIT { 0 }
926 unsigned int cached
: 1;
927 unsigned int for_status
: 1;
928 unsigned int files
: 1;
931 #define SUMMARY_CB_INIT { 0 }
938 static char *verify_submodule_committish(const char *sm_path
,
939 const char *committish
)
941 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
942 struct strbuf result
= STRBUF_INIT
;
944 cp_rev_parse
.git_cmd
= 1;
945 cp_rev_parse
.dir
= sm_path
;
946 prepare_submodule_repo_env(&cp_rev_parse
.env_array
);
947 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
948 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
949 strvec_push(&cp_rev_parse
.args
, "--");
951 if (capture_command(&cp_rev_parse
, &result
, 0))
954 strbuf_trim_trailing_newline(&result
);
955 return strbuf_detach(&result
, NULL
);
958 static void print_submodule_summary(struct summary_cb
*info
, char *errmsg
,
959 int total_commits
, const char *displaypath
,
960 const char *src_abbrev
, const char *dst_abbrev
,
963 if (p
->status
== 'T') {
964 if (S_ISGITLINK(p
->mod_dst
))
965 printf(_("* %s %s(blob)->%s(submodule)"),
966 displaypath
, src_abbrev
, dst_abbrev
);
968 printf(_("* %s %s(submodule)->%s(blob)"),
969 displaypath
, src_abbrev
, dst_abbrev
);
971 printf("* %s %s...%s",
972 displaypath
, src_abbrev
, dst_abbrev
);
975 if (total_commits
< 0)
978 printf(" (%d):\n", total_commits
);
981 printf(_("%s"), errmsg
);
982 } else if (total_commits
> 0) {
983 struct child_process cp_log
= CHILD_PROCESS_INIT
;
986 cp_log
.dir
= p
->sm_path
;
987 prepare_submodule_repo_env(&cp_log
.env_array
);
988 strvec_pushl(&cp_log
.args
, "log", NULL
);
990 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
991 if (info
->summary_limit
> 0)
992 strvec_pushf(&cp_log
.args
, "-%d",
993 info
->summary_limit
);
995 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
996 "--first-parent", NULL
);
997 strvec_pushf(&cp_log
.args
, "%s...%s",
998 src_abbrev
, dst_abbrev
);
999 } else if (S_ISGITLINK(p
->mod_dst
)) {
1000 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
1001 "-1", dst_abbrev
, NULL
);
1003 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
1004 "-1", src_abbrev
, NULL
);
1006 run_command(&cp_log
);
1011 static void generate_submodule_summary(struct summary_cb
*info
,
1012 struct module_cb
*p
)
1014 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
1015 int missing_src
= 0, missing_dst
= 0;
1016 char *errmsg
= NULL
;
1017 int total_commits
= -1;
1019 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
1020 if (S_ISGITLINK(p
->mod_dst
)) {
1021 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
1023 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
1024 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
1026 int fd
= open(p
->sm_path
, O_RDONLY
);
1028 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
1029 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
1031 error(_("couldn't hash object from '%s'"), p
->sm_path
);
1033 /* for a submodule removal (mode:0000000), don't warn */
1035 warning(_("unexpected mode %o\n"), p
->mod_dst
);
1039 if (S_ISGITLINK(p
->mod_src
)) {
1040 if (p
->status
!= 'D')
1041 src_abbrev
= verify_submodule_committish(p
->sm_path
,
1042 oid_to_hex(&p
->oid_src
));
1046 * As `rev-parse` failed, we fallback to getting
1047 * the abbreviated hash using oid_src. We do
1048 * this as we might still need the abbreviated
1049 * hash in cases like a submodule type change, etc.
1051 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
1055 * The source does not point to a submodule.
1056 * So, we fallback to getting the abbreviation using
1057 * oid_src as we might still need the abbreviated
1058 * hash in cases like submodule add, etc.
1060 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
1063 if (S_ISGITLINK(p
->mod_dst
)) {
1064 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
1065 oid_to_hex(&p
->oid_dst
));
1069 * As `rev-parse` failed, we fallback to getting
1070 * the abbreviated hash using oid_dst. We do
1071 * this as we might still need the abbreviated
1072 * hash in cases like a submodule type change, etc.
1074 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
1078 * The destination does not point to a submodule.
1079 * So, we fallback to getting the abbreviation using
1080 * oid_dst as we might still need the abbreviated
1081 * hash in cases like a submodule removal, etc.
1083 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
1086 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
);
1088 if (!missing_src
&& !missing_dst
) {
1089 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
1090 struct strbuf sb_rev_list
= STRBUF_INIT
;
1092 strvec_pushl(&cp_rev_list
.args
, "rev-list",
1093 "--first-parent", "--count", NULL
);
1094 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
1095 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
1096 src_abbrev
, dst_abbrev
);
1098 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
1099 src_abbrev
: dst_abbrev
);
1100 strvec_push(&cp_rev_list
.args
, "--");
1102 cp_rev_list
.git_cmd
= 1;
1103 cp_rev_list
.dir
= p
->sm_path
;
1104 prepare_submodule_repo_env(&cp_rev_list
.env_array
);
1106 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
1107 total_commits
= atoi(sb_rev_list
.buf
);
1109 strbuf_release(&sb_rev_list
);
1112 * Don't give error msg for modification whose dst is not
1113 * submodule, i.e., deleted or changed to blob
1115 if (S_ISGITLINK(p
->mod_dst
)) {
1116 struct strbuf errmsg_str
= STRBUF_INIT
;
1117 if (missing_src
&& missing_dst
) {
1118 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commits %s and %s\n",
1119 displaypath
, oid_to_hex(&p
->oid_src
),
1120 oid_to_hex(&p
->oid_dst
));
1122 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commit %s\n",
1123 displaypath
, missing_src
?
1124 oid_to_hex(&p
->oid_src
) :
1125 oid_to_hex(&p
->oid_dst
));
1127 errmsg
= strbuf_detach(&errmsg_str
, NULL
);
1131 print_submodule_summary(info
, errmsg
, total_commits
,
1132 displaypath
, src_abbrev
,
1140 static void prepare_submodule_summary(struct summary_cb
*info
,
1141 struct module_cb_list
*list
)
1144 for (i
= 0; i
< list
->nr
; i
++) {
1145 const struct submodule
*sub
;
1146 struct module_cb
*p
= list
->entries
[i
];
1147 struct strbuf sm_gitdir
= STRBUF_INIT
;
1149 if (p
->status
== 'D' || p
->status
== 'T') {
1150 generate_submodule_summary(info
, p
);
1154 if (info
->for_status
&& p
->status
!= 'A' &&
1155 (sub
= submodule_from_path(the_repository
,
1156 null_oid(), p
->sm_path
))) {
1157 char *config_key
= NULL
;
1161 config_key
= xstrfmt("submodule.%s.ignore",
1163 if (!git_config_get_string_tmp(config_key
, &value
))
1164 ignore_all
= !strcmp(value
, "all");
1165 else if (sub
->ignore
)
1166 ignore_all
= !strcmp(sub
->ignore
, "all");
1173 /* Also show added or modified modules which are checked out */
1174 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1175 if (is_nonbare_repository_dir(&sm_gitdir
))
1176 generate_submodule_summary(info
, p
);
1177 strbuf_release(&sm_gitdir
);
1181 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1182 struct diff_options
*options
,
1186 struct module_cb_list
*list
= data
;
1187 for (i
= 0; i
< q
->nr
; i
++) {
1188 struct diff_filepair
*p
= q
->queue
[i
];
1189 struct module_cb
*temp
;
1191 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1193 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1194 temp
->mod_src
= p
->one
->mode
;
1195 temp
->mod_dst
= p
->two
->mode
;
1196 temp
->oid_src
= p
->one
->oid
;
1197 temp
->oid_dst
= p
->two
->oid
;
1198 temp
->status
= p
->status
;
1199 temp
->sm_path
= xstrdup(p
->one
->path
);
1201 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1202 list
->entries
[list
->nr
++] = temp
;
1206 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1209 case DIFF_INDEX
: return "diff-index";
1210 case DIFF_FILES
: return "diff-files";
1211 default: BUG("bad diff_cmd value %d", diff_cmd
);
1215 static int compute_summary_module_list(struct object_id
*head_oid
,
1216 struct summary_cb
*info
,
1217 enum diff_cmd diff_cmd
)
1219 struct strvec diff_args
= STRVEC_INIT
;
1220 struct rev_info rev
;
1221 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1223 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1225 strvec_push(&diff_args
, "--cached");
1226 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1228 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1229 strvec_push(&diff_args
, "--");
1231 strvec_pushv(&diff_args
, info
->argv
);
1233 git_config(git_diff_basic_config
, NULL
);
1234 init_revisions(&rev
, info
->prefix
);
1236 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1237 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, NULL
);
1238 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1239 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1240 rev
.diffopt
.format_callback_data
= &list
;
1242 if (!info
->cached
) {
1243 if (diff_cmd
== DIFF_INDEX
)
1245 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1246 perror("read_cache_preload");
1249 } else if (read_cache() < 0) {
1250 perror("read_cache");
1254 if (diff_cmd
== DIFF_INDEX
)
1255 run_diff_index(&rev
, info
->cached
);
1257 run_diff_files(&rev
, 0);
1258 prepare_submodule_summary(info
, &list
);
1259 strvec_clear(&diff_args
);
1263 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1265 struct summary_cb info
= SUMMARY_CB_INIT
;
1269 int summary_limit
= -1;
1270 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1271 struct object_id head_oid
;
1274 struct option module_summary_options
[] = {
1275 OPT_BOOL(0, "cached", &cached
,
1276 N_("use the commit stored in the index instead of the submodule HEAD")),
1277 OPT_BOOL(0, "files", &files
,
1278 N_("compare the commit in the index with that in the submodule HEAD")),
1279 OPT_BOOL(0, "for-status", &for_status
,
1280 N_("skip submodules with 'ignore_config' value set to 'all'")),
1281 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1282 N_("limit the summary size")),
1286 const char *const git_submodule_helper_usage
[] = {
1287 N_("git submodule--helper summary [<options>] [<commit>] [--] [<path>]"),
1291 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1292 git_submodule_helper_usage
, 0);
1297 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1302 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1303 /* before the first commit: compare with an empty tree */
1304 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1310 if (get_oid("HEAD", &head_oid
))
1311 die(_("could not fetch a revision for HEAD"));
1316 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1317 diff_cmd
= DIFF_FILES
;
1322 info
.prefix
= prefix
;
1323 info
.cached
= !!cached
;
1324 info
.files
= !!files
;
1325 info
.for_status
= !!for_status
;
1326 info
.summary_limit
= summary_limit
;
1328 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1337 #define SYNC_CB_INIT { 0 }
1339 static void sync_submodule(const char *path
, const char *prefix
,
1342 const struct submodule
*sub
;
1343 char *remote_key
= NULL
;
1344 char *sub_origin_url
, *super_config_url
, *displaypath
;
1345 struct strbuf sb
= STRBUF_INIT
;
1346 struct child_process cp
= CHILD_PROCESS_INIT
;
1347 char *sub_config_path
= NULL
;
1349 if (!is_submodule_active(the_repository
, path
))
1352 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1354 if (sub
&& sub
->url
) {
1355 if (starts_with_dot_dot_slash(sub
->url
) ||
1356 starts_with_dot_slash(sub
->url
)) {
1357 char *up_path
= get_up_path(path
);
1358 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1359 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1362 sub_origin_url
= xstrdup(sub
->url
);
1363 super_config_url
= xstrdup(sub
->url
);
1366 sub_origin_url
= xstrdup("");
1367 super_config_url
= xstrdup("");
1370 displaypath
= get_submodule_displaypath(path
, prefix
);
1372 if (!(flags
& OPT_QUIET
))
1373 printf(_("Synchronizing submodule url for '%s'\n"),
1377 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1378 if (git_config_set_gently(sb
.buf
, super_config_url
))
1379 die(_("failed to register url for submodule path '%s'"),
1382 if (!is_submodule_populated_gently(path
, NULL
))
1385 prepare_submodule_repo_env(&cp
.env_array
);
1388 strvec_pushl(&cp
.args
, "submodule--helper",
1389 "print-default-remote", NULL
);
1392 if (capture_command(&cp
, &sb
, 0))
1393 die(_("failed to get the default remote for submodule '%s'"),
1396 strbuf_strip_suffix(&sb
, "\n");
1397 remote_key
= xstrfmt("remote.%s.url", sb
.buf
);
1400 submodule_to_gitdir(&sb
, path
);
1401 strbuf_addstr(&sb
, "/config");
1403 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1404 die(_("failed to update remote for submodule '%s'"),
1407 if (flags
& OPT_RECURSIVE
) {
1408 struct child_process cpr
= CHILD_PROCESS_INIT
;
1412 prepare_submodule_repo_env(&cpr
.env_array
);
1414 strvec_push(&cpr
.args
, "--super-prefix");
1415 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1416 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1417 "--recursive", NULL
);
1419 if (flags
& OPT_QUIET
)
1420 strvec_push(&cpr
.args
, "--quiet");
1422 if (run_command(&cpr
))
1423 die(_("failed to recurse into submodule '%s'"),
1428 free(super_config_url
);
1429 free(sub_origin_url
);
1430 strbuf_release(&sb
);
1433 free(sub_config_path
);
1436 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1438 struct sync_cb
*info
= cb_data
;
1439 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1442 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1444 struct sync_cb info
= SYNC_CB_INIT
;
1445 struct pathspec pathspec
;
1446 struct module_list list
= MODULE_LIST_INIT
;
1450 struct option module_sync_options
[] = {
1451 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1452 OPT_BOOL(0, "recursive", &recursive
,
1453 N_("recurse into nested submodules")),
1457 const char *const git_submodule_helper_usage
[] = {
1458 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1462 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1463 git_submodule_helper_usage
, 0);
1465 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1468 info
.prefix
= prefix
;
1470 info
.flags
|= OPT_QUIET
;
1472 info
.flags
|= OPT_RECURSIVE
;
1474 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1483 #define DEINIT_CB_INIT { 0 }
1485 static void deinit_submodule(const char *path
, const char *prefix
,
1488 const struct submodule
*sub
;
1489 char *displaypath
= NULL
;
1490 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1491 struct strbuf sb_config
= STRBUF_INIT
;
1492 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1494 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1496 if (!sub
|| !sub
->name
)
1499 displaypath
= get_submodule_displaypath(path
, prefix
);
1501 /* remove the submodule work tree (unless the user already did it) */
1502 if (is_directory(path
)) {
1503 struct strbuf sb_rm
= STRBUF_INIT
;
1506 if (is_directory(sub_git_dir
)) {
1507 if (!(flags
& OPT_QUIET
))
1508 warning(_("Submodule work tree '%s' contains a .git "
1509 "directory. This will be replaced with a "
1510 ".git file by using absorbgitdirs."),
1513 absorb_git_dir_into_superproject(path
,
1514 ABSORB_GITDIR_RECURSE_SUBMODULES
);
1518 if (!(flags
& OPT_FORCE
)) {
1519 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1521 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1524 if (run_command(&cp_rm
))
1525 die(_("Submodule work tree '%s' contains local "
1526 "modifications; use '-f' to discard them"),
1530 strbuf_addstr(&sb_rm
, path
);
1532 if (!remove_dir_recursively(&sb_rm
, 0))
1533 format
= _("Cleared directory '%s'\n");
1535 format
= _("Could not remove submodule work tree '%s'\n");
1537 if (!(flags
& OPT_QUIET
))
1538 printf(format
, displaypath
);
1540 submodule_unset_core_worktree(sub
);
1542 strbuf_release(&sb_rm
);
1545 if (mkdir(path
, 0777))
1546 printf(_("could not create empty submodule directory %s"),
1549 cp_config
.git_cmd
= 1;
1550 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1551 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1553 /* remove the .git/config entries (unless the user already did it) */
1554 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1555 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1557 * remove the whole section so we have a clean state when
1558 * the user later decides to init this submodule again
1560 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1561 if (!(flags
& OPT_QUIET
))
1562 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1563 sub
->name
, sub
->url
, displaypath
);
1570 strbuf_release(&sb_config
);
1573 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1576 struct deinit_cb
*info
= cb_data
;
1577 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1580 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1582 struct deinit_cb info
= DEINIT_CB_INIT
;
1583 struct pathspec pathspec
;
1584 struct module_list list
= MODULE_LIST_INIT
;
1589 struct option module_deinit_options
[] = {
1590 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1591 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1592 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1596 const char *const git_submodule_helper_usage
[] = {
1597 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1601 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1602 git_submodule_helper_usage
, 0);
1605 error("pathspec and --all are incompatible");
1606 usage_with_options(git_submodule_helper_usage
,
1607 module_deinit_options
);
1611 die(_("Use '--all' if you really want to deinitialize all submodules"));
1613 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1616 info
.prefix
= prefix
;
1618 info
.flags
|= OPT_QUIET
;
1620 info
.flags
|= OPT_FORCE
;
1622 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1627 struct module_clone_data
{
1633 struct string_list reference
;
1634 unsigned int quiet
: 1;
1635 unsigned int progress
: 1;
1636 unsigned int dissociate
: 1;
1637 unsigned int require_init
: 1;
1640 #define MODULE_CLONE_DATA_INIT { .reference = STRING_LIST_INIT_NODUP, .single_branch = -1 }
1642 struct submodule_alternate_setup
{
1643 const char *submodule_name
;
1644 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1645 SUBMODULE_ALTERNATE_ERROR_DIE
,
1646 SUBMODULE_ALTERNATE_ERROR_INFO
,
1647 SUBMODULE_ALTERNATE_ERROR_IGNORE
1649 struct string_list
*reference
;
1651 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1652 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1655 static const char alternate_error_advice
[] = N_(
1656 "An alternate computed from a superproject's alternate is invalid.\n"
1657 "To allow Git to clone without an alternate in such a case, set\n"
1658 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1659 "'--reference-if-able' instead of '--reference'."
1662 static int add_possible_reference_from_superproject(
1663 struct object_directory
*odb
, void *sas_cb
)
1665 struct submodule_alternate_setup
*sas
= sas_cb
;
1669 * If the alternate object store is another repository, try the
1670 * standard layout with .git/(modules/<name>)+/objects
1672 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1673 struct repository alternate
;
1675 struct strbuf sb
= STRBUF_INIT
;
1676 struct strbuf err
= STRBUF_INIT
;
1677 strbuf_add(&sb
, odb
->path
, len
);
1679 repo_init(&alternate
, sb
.buf
, NULL
);
1682 * We need to end the new path with '/' to mark it as a dir,
1683 * otherwise a submodule name containing '/' will be broken
1684 * as the last part of a missing submodule reference would
1685 * be taken as a file name.
1688 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1689 strbuf_addch(&sb
, '/');
1690 repo_clear(&alternate
);
1692 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1694 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1697 switch (sas
->error_mode
) {
1698 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1699 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1700 advise(_(alternate_error_advice
));
1701 die(_("submodule '%s' cannot add alternate: %s"),
1702 sas
->submodule_name
, err
.buf
);
1703 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1704 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1705 sas
->submodule_name
, err
.buf
);
1706 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1710 strbuf_release(&sb
);
1716 static void prepare_possible_alternates(const char *sm_name
,
1717 struct string_list
*reference
)
1719 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1720 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1722 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1726 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1728 if (!error_strategy
)
1729 error_strategy
= xstrdup("die");
1731 sas
.submodule_name
= sm_name
;
1732 sas
.reference
= reference
;
1733 if (!strcmp(error_strategy
, "die"))
1734 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1735 else if (!strcmp(error_strategy
, "info"))
1736 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1737 else if (!strcmp(error_strategy
, "ignore"))
1738 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1740 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1742 if (!strcmp(sm_alternate
, "superproject"))
1743 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1744 else if (!strcmp(sm_alternate
, "no"))
1747 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1750 free(error_strategy
);
1753 static int clone_submodule(struct module_clone_data
*clone_data
)
1755 char *p
, *sm_gitdir
;
1756 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1757 struct strbuf sb
= STRBUF_INIT
;
1758 struct child_process cp
= CHILD_PROCESS_INIT
;
1760 submodule_name_to_gitdir(&sb
, the_repository
, clone_data
->name
);
1761 sm_gitdir
= absolute_pathdup(sb
.buf
);
1764 if (!is_absolute_path(clone_data
->path
)) {
1765 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), clone_data
->path
);
1766 clone_data
->path
= strbuf_detach(&sb
, NULL
);
1768 clone_data
->path
= xstrdup(clone_data
->path
);
1771 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1772 die(_("refusing to create/use '%s' in another submodule's "
1773 "git dir"), sm_gitdir
);
1775 if (!file_exists(sm_gitdir
)) {
1776 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1777 die(_("could not create directory '%s'"), sm_gitdir
);
1779 prepare_possible_alternates(clone_data
->name
, &clone_data
->reference
);
1781 strvec_push(&cp
.args
, "clone");
1782 strvec_push(&cp
.args
, "--no-checkout");
1783 if (clone_data
->quiet
)
1784 strvec_push(&cp
.args
, "--quiet");
1785 if (clone_data
->progress
)
1786 strvec_push(&cp
.args
, "--progress");
1787 if (clone_data
->depth
&& *(clone_data
->depth
))
1788 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1789 if (clone_data
->reference
.nr
) {
1790 struct string_list_item
*item
;
1791 for_each_string_list_item(item
, &clone_data
->reference
)
1792 strvec_pushl(&cp
.args
, "--reference",
1793 item
->string
, NULL
);
1795 if (clone_data
->dissociate
)
1796 strvec_push(&cp
.args
, "--dissociate");
1797 if (sm_gitdir
&& *sm_gitdir
)
1798 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1799 if (clone_data
->single_branch
>= 0)
1800 strvec_push(&cp
.args
, clone_data
->single_branch
?
1802 "--no-single-branch");
1804 strvec_push(&cp
.args
, "--");
1805 strvec_push(&cp
.args
, clone_data
->url
);
1806 strvec_push(&cp
.args
, clone_data
->path
);
1809 prepare_submodule_repo_env(&cp
.env_array
);
1812 if(run_command(&cp
))
1813 die(_("clone of '%s' into submodule path '%s' failed"),
1814 clone_data
->url
, clone_data
->path
);
1816 if (clone_data
->require_init
&& !access(clone_data
->path
, X_OK
) &&
1817 !is_empty_dir(clone_data
->path
))
1818 die(_("directory not empty: '%s'"), clone_data
->path
);
1819 if (safe_create_leading_directories_const(clone_data
->path
) < 0)
1820 die(_("could not create directory '%s'"), clone_data
->path
);
1821 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1822 unlink_or_warn(sb
.buf
);
1826 connect_work_tree_and_git_dir(clone_data
->path
, sm_gitdir
, 0);
1828 p
= git_pathdup_submodule(clone_data
->path
, "config");
1830 die(_("could not get submodule directory for '%s'"), clone_data
->path
);
1832 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1833 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1835 git_config_set_in_file(p
, "submodule.alternateLocation",
1837 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1839 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1843 free(error_strategy
);
1845 strbuf_release(&sb
);
1851 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1853 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1854 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1856 struct option module_clone_options
[] = {
1857 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1859 N_("alternative anchor for relative paths")),
1860 OPT_STRING(0, "path", &clone_data
.path
,
1862 N_("where the new submodule will be cloned to")),
1863 OPT_STRING(0, "name", &clone_data
.name
,
1865 N_("name of the new submodule")),
1866 OPT_STRING(0, "url", &clone_data
.url
,
1868 N_("url where to clone the submodule from")),
1869 OPT_STRING_LIST(0, "reference", &clone_data
.reference
,
1871 N_("reference repository")),
1872 OPT_BOOL(0, "dissociate", &dissociate
,
1873 N_("use --reference only while cloning")),
1874 OPT_STRING(0, "depth", &clone_data
.depth
,
1876 N_("depth for shallow clones")),
1877 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
1878 OPT_BOOL(0, "progress", &progress
,
1879 N_("force cloning progress")),
1880 OPT_BOOL(0, "require-init", &require_init
,
1881 N_("disallow cloning into non-empty directory")),
1882 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1883 N_("clone only one branch, HEAD or --branch")),
1887 const char *const git_submodule_helper_usage
[] = {
1888 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1889 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1890 "[--single-branch] "
1891 "--url <url> --path <path>"),
1895 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1896 git_submodule_helper_usage
, 0);
1898 clone_data
.dissociate
= !!dissociate
;
1899 clone_data
.quiet
= !!quiet
;
1900 clone_data
.progress
= !!progress
;
1901 clone_data
.require_init
= !!require_init
;
1903 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1904 usage_with_options(git_submodule_helper_usage
,
1905 module_clone_options
);
1907 clone_submodule(&clone_data
);
1911 static void determine_submodule_update_strategy(struct repository
*r
,
1915 struct submodule_update_strategy
*out
)
1917 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1921 key
= xstrfmt("submodule.%s.update", sub
->name
);
1924 if (parse_submodule_update_strategy(update
, out
) < 0)
1925 die(_("Invalid update mode '%s' for submodule path '%s'"),
1927 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1928 if (parse_submodule_update_strategy(val
, out
) < 0)
1929 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1931 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1932 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1933 BUG("how did we read update = !command from .gitmodules?");
1934 out
->type
= sub
->update_strategy
.type
;
1935 out
->command
= sub
->update_strategy
.command
;
1937 out
->type
= SM_UPDATE_CHECKOUT
;
1940 (out
->type
== SM_UPDATE_MERGE
||
1941 out
->type
== SM_UPDATE_REBASE
||
1942 out
->type
== SM_UPDATE_NONE
))
1943 out
->type
= SM_UPDATE_CHECKOUT
;
1948 static int module_update_module_mode(int argc
, const char **argv
, const char *prefix
)
1950 const char *path
, *update
= NULL
;
1952 struct submodule_update_strategy update_strategy
= { .type
= SM_UPDATE_CHECKOUT
};
1954 if (argc
< 3 || argc
> 4)
1955 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1957 just_cloned
= git_config_int("just_cloned", argv
[1]);
1963 determine_submodule_update_strategy(the_repository
,
1964 just_cloned
, path
, update
,
1966 fputs(submodule_strategy_to_string(&update_strategy
), stdout
);
1971 struct update_clone_data
{
1972 const struct submodule
*sub
;
1973 struct object_id oid
;
1974 unsigned just_cloned
;
1977 struct submodule_update_clone
{
1978 /* index into 'list', the list of submodules to look into for cloning */
1980 struct module_list list
;
1981 unsigned warn_if_uninitialized
: 1;
1983 /* update parameter passed via commandline */
1984 struct submodule_update_strategy update
;
1986 /* configuration parameters which are passed on to the children */
1989 int recommend_shallow
;
1990 struct string_list references
;
1992 unsigned require_init
;
1994 const char *recursive_prefix
;
1998 /* to be consumed by git-submodule.sh */
1999 struct update_clone_data
*update_clone
;
2000 int update_clone_nr
; int update_clone_alloc
;
2002 /* If we want to stop as fast as possible and return an error */
2003 unsigned quickstop
: 1;
2005 /* failed clones to be retried again */
2006 const struct cache_entry
**failed_clones
;
2007 int failed_clones_nr
, failed_clones_alloc
;
2011 #define SUBMODULE_UPDATE_CLONE_INIT { \
2012 .list = MODULE_LIST_INIT, \
2013 .update = SUBMODULE_UPDATE_STRATEGY_INIT, \
2014 .recommend_shallow = -1, \
2015 .references = STRING_LIST_INIT_DUP, \
2016 .single_branch = -1, \
2020 struct update_data
{
2021 const char *recursive_prefix
;
2022 const char *sm_path
;
2023 const char *displaypath
;
2024 struct object_id oid
;
2025 struct object_id suboid
;
2026 struct submodule_update_strategy update_strategy
;
2028 unsigned int force
: 1;
2029 unsigned int quiet
: 1;
2030 unsigned int nofetch
: 1;
2031 unsigned int just_cloned
: 1;
2033 #define UPDATE_DATA_INIT { .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT }
2035 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
2036 struct strbuf
*out
, const char *displaypath
)
2039 * Only mention uninitialized submodules when their
2040 * paths have been specified.
2042 if (suc
->warn_if_uninitialized
) {
2044 _("Submodule path '%s' not initialized"),
2046 strbuf_addch(out
, '\n');
2048 _("Maybe you want to use 'update --init'?"));
2049 strbuf_addch(out
, '\n');
2054 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2055 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2057 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
2058 struct child_process
*child
,
2059 struct submodule_update_clone
*suc
,
2062 const struct submodule
*sub
= NULL
;
2063 const char *url
= NULL
;
2064 const char *update_string
;
2065 enum submodule_update_type update_type
;
2067 struct strbuf displaypath_sb
= STRBUF_INIT
;
2068 struct strbuf sb
= STRBUF_INIT
;
2069 const char *displaypath
= NULL
;
2070 int needs_cloning
= 0;
2071 int need_free_url
= 0;
2074 if (suc
->recursive_prefix
)
2075 strbuf_addf(&sb
, "%s/%s", suc
->recursive_prefix
, ce
->name
);
2077 strbuf_addstr(&sb
, ce
->name
);
2078 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
2079 strbuf_addch(out
, '\n');
2083 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
2085 if (suc
->recursive_prefix
)
2086 displaypath
= relative_path(suc
->recursive_prefix
,
2087 ce
->name
, &displaypath_sb
);
2089 displaypath
= ce
->name
;
2092 next_submodule_warn_missing(suc
, out
, displaypath
);
2096 key
= xstrfmt("submodule.%s.update", sub
->name
);
2097 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2098 update_type
= parse_submodule_update_type(update_string
);
2100 update_type
= sub
->update_strategy
.type
;
2104 if (suc
->update
.type
== SM_UPDATE_NONE
2105 || (suc
->update
.type
== SM_UPDATE_UNSPECIFIED
2106 && update_type
== SM_UPDATE_NONE
)) {
2107 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2108 strbuf_addch(out
, '\n');
2112 /* Check if the submodule has been initialized. */
2113 if (!is_submodule_active(the_repository
, ce
->name
)) {
2114 next_submodule_warn_missing(suc
, out
, displaypath
);
2119 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2120 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2121 if (starts_with_dot_slash(sub
->url
) ||
2122 starts_with_dot_dot_slash(sub
->url
)) {
2123 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2130 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2131 needs_cloning
= !file_exists(sb
.buf
);
2133 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2134 suc
->update_clone_alloc
);
2135 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2136 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2137 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2138 suc
->update_clone_nr
++;
2144 child
->no_stdin
= 1;
2145 child
->stdout_to_stderr
= 1;
2147 strvec_push(&child
->args
, "submodule--helper");
2148 strvec_push(&child
->args
, "clone");
2150 strvec_push(&child
->args
, "--progress");
2152 strvec_push(&child
->args
, "--quiet");
2154 strvec_pushl(&child
->args
, "--prefix", suc
->prefix
, NULL
);
2155 if (suc
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2156 strvec_push(&child
->args
, "--depth=1");
2157 if (suc
->require_init
)
2158 strvec_push(&child
->args
, "--require-init");
2159 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2160 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2161 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2162 if (suc
->references
.nr
) {
2163 struct string_list_item
*item
;
2164 for_each_string_list_item(item
, &suc
->references
)
2165 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2167 if (suc
->dissociate
)
2168 strvec_push(&child
->args
, "--dissociate");
2170 strvec_push(&child
->args
, suc
->depth
);
2171 if (suc
->single_branch
>= 0)
2172 strvec_push(&child
->args
, suc
->single_branch
?
2174 "--no-single-branch");
2177 strbuf_release(&displaypath_sb
);
2178 strbuf_release(&sb
);
2182 return needs_cloning
;
2185 static int update_clone_get_next_task(struct child_process
*child
,
2190 struct submodule_update_clone
*suc
= suc_cb
;
2191 const struct cache_entry
*ce
;
2194 for (; suc
->current
< suc
->list
.nr
; suc
->current
++) {
2195 ce
= suc
->list
.entries
[suc
->current
];
2196 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2197 int *p
= xmalloc(sizeof(*p
));
2206 * The loop above tried cloning each submodule once, now try the
2207 * stragglers again, which we can imagine as an extension of the
2210 index
= suc
->current
- suc
->list
.nr
;
2211 if (index
< suc
->failed_clones_nr
) {
2213 ce
= suc
->failed_clones
[index
];
2214 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2216 strbuf_addstr(err
, "BUG: submodule considered for "
2217 "cloning, doesn't need cloning "
2221 p
= xmalloc(sizeof(*p
));
2231 static int update_clone_start_failure(struct strbuf
*err
,
2235 struct submodule_update_clone
*suc
= suc_cb
;
2240 static int update_clone_task_finished(int result
,
2245 const struct cache_entry
*ce
;
2246 struct submodule_update_clone
*suc
= suc_cb
;
2248 int *idxP
= idx_task_cb
;
2255 if (idx
< suc
->list
.nr
) {
2256 ce
= suc
->list
.entries
[idx
];
2257 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2259 strbuf_addch(err
, '\n');
2260 ALLOC_GROW(suc
->failed_clones
,
2261 suc
->failed_clones_nr
+ 1,
2262 suc
->failed_clones_alloc
);
2263 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2266 idx
-= suc
->list
.nr
;
2267 ce
= suc
->failed_clones
[idx
];
2268 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2270 strbuf_addch(err
, '\n');
2278 static int git_update_clone_config(const char *var
, const char *value
,
2282 if (!strcmp(var
, "submodule.fetchjobs"))
2283 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2287 static int is_tip_reachable(const char *path
, struct object_id
*oid
)
2289 struct child_process cp
= CHILD_PROCESS_INIT
;
2290 struct strbuf rev
= STRBUF_INIT
;
2291 char *hex
= oid_to_hex(oid
);
2294 cp
.dir
= xstrdup(path
);
2296 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2298 prepare_submodule_repo_env(&cp
.env_array
);
2300 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2306 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
, struct object_id
*oid
)
2308 struct child_process cp
= CHILD_PROCESS_INIT
;
2310 prepare_submodule_repo_env(&cp
.env_array
);
2312 cp
.dir
= xstrdup(module_path
);
2314 strvec_push(&cp
.args
, "fetch");
2316 strvec_push(&cp
.args
, "--quiet");
2318 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2320 char *hex
= oid_to_hex(oid
);
2321 char *remote
= get_default_remote();
2322 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2325 return run_command(&cp
);
2328 static int run_update_command(struct update_data
*ud
, int subforce
)
2330 struct strvec args
= STRVEC_INIT
;
2331 struct strvec child_env
= STRVEC_INIT
;
2332 char *oid
= oid_to_hex(&ud
->oid
);
2333 int must_die_on_failure
= 0;
2336 switch (ud
->update_strategy
.type
) {
2337 case SM_UPDATE_CHECKOUT
:
2339 strvec_pushl(&args
, "checkout", "-q", NULL
);
2341 strvec_push(&args
, "-f");
2343 case SM_UPDATE_REBASE
:
2345 strvec_push(&args
, "rebase");
2347 strvec_push(&args
, "--quiet");
2348 must_die_on_failure
= 1;
2350 case SM_UPDATE_MERGE
:
2352 strvec_push(&args
, "merge");
2354 strvec_push(&args
, "--quiet");
2355 must_die_on_failure
= 1;
2357 case SM_UPDATE_COMMAND
:
2359 strvec_push(&args
, ud
->update_strategy
.command
);
2360 must_die_on_failure
= 1;
2363 BUG("unexpected update strategy type: %s",
2364 submodule_strategy_to_string(&ud
->update_strategy
));
2366 strvec_push(&args
, oid
);
2368 prepare_submodule_repo_env(&child_env
);
2369 if (run_command_v_opt_cd_env(args
.v
, git_cmd
? RUN_GIT_CMD
: RUN_USING_SHELL
,
2370 ud
->sm_path
, child_env
.v
)) {
2371 switch (ud
->update_strategy
.type
) {
2372 case SM_UPDATE_CHECKOUT
:
2373 printf(_("Unable to checkout '%s' in submodule path '%s'"),
2374 oid
, ud
->displaypath
);
2376 case SM_UPDATE_REBASE
:
2377 printf(_("Unable to rebase '%s' in submodule path '%s'"),
2378 oid
, ud
->displaypath
);
2380 case SM_UPDATE_MERGE
:
2381 printf(_("Unable to merge '%s' in submodule path '%s'"),
2382 oid
, ud
->displaypath
);
2384 case SM_UPDATE_COMMAND
:
2385 printf(_("Execution of '%s %s' failed in submodule path '%s'"),
2386 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2389 BUG("unexpected update strategy type: %s",
2390 submodule_strategy_to_string(&ud
->update_strategy
));
2393 * NEEDSWORK: We are currently printing to stdout with error
2394 * return so that the shell caller handles the error output
2395 * properly. Once we start handling the error messages within
2396 * C, we should use die() instead.
2398 if (must_die_on_failure
)
2401 * This signifies to the caller in shell that the command
2402 * failed without dying
2407 switch (ud
->update_strategy
.type
) {
2408 case SM_UPDATE_CHECKOUT
:
2409 printf(_("Submodule path '%s': checked out '%s'\n"),
2410 ud
->displaypath
, oid
);
2412 case SM_UPDATE_REBASE
:
2413 printf(_("Submodule path '%s': rebased into '%s'\n"),
2414 ud
->displaypath
, oid
);
2416 case SM_UPDATE_MERGE
:
2417 printf(_("Submodule path '%s': merged in '%s'\n"),
2418 ud
->displaypath
, oid
);
2420 case SM_UPDATE_COMMAND
:
2421 printf(_("Submodule path '%s': '%s %s'\n"),
2422 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2425 BUG("unexpected update strategy type: %s",
2426 submodule_strategy_to_string(&ud
->update_strategy
));
2432 static int do_run_update_procedure(struct update_data
*ud
)
2434 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2438 * Run fetch only if `oid` isn't present or it
2439 * is not reachable from a ref.
2441 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2442 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2445 _("Unable to fetch in submodule path '%s'; "
2446 "trying to directly fetch %s:"),
2447 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2449 * Now we tried the usual fetch, but `oid` may
2450 * not be reachable from any of the refs.
2452 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2453 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2454 die(_("Fetched in submodule path '%s', but it did not "
2455 "contain %s. Direct fetching of that commit failed."),
2456 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2459 return run_update_command(ud
, subforce
);
2462 static void update_submodule(struct update_clone_data
*ucd
)
2464 fprintf(stdout
, "dummy %s %d\t%s\n",
2465 oid_to_hex(&ucd
->oid
),
2470 static int update_submodules(struct submodule_update_clone
*suc
)
2474 run_processes_parallel_tr2(suc
->max_jobs
, update_clone_get_next_task
,
2475 update_clone_start_failure
,
2476 update_clone_task_finished
, suc
, "submodule",
2480 * We saved the output and put it out all at once now.
2482 * - the listener does not have to interleave their (checkout)
2483 * work with our fetching. The writes involved in a
2484 * checkout involve more straightforward sequential I/O.
2485 * - the listener can avoid doing any work if fetching failed.
2490 for (i
= 0; i
< suc
->update_clone_nr
; i
++)
2491 update_submodule(&suc
->update_clone
[i
]);
2496 static int update_clone(int argc
, const char **argv
, const char *prefix
)
2498 const char *update
= NULL
;
2499 struct pathspec pathspec
;
2500 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2502 struct option module_update_clone_options
[] = {
2503 OPT_STRING(0, "prefix", &prefix
,
2505 N_("path into the working tree")),
2506 OPT_STRING(0, "recursive-prefix", &suc
.recursive_prefix
,
2508 N_("path into the working tree, across nested "
2509 "submodule boundaries")),
2510 OPT_STRING(0, "update", &update
,
2512 N_("rebase, merge, checkout or none")),
2513 OPT_STRING_LIST(0, "reference", &suc
.references
, N_("repo"),
2514 N_("reference repository")),
2515 OPT_BOOL(0, "dissociate", &suc
.dissociate
,
2516 N_("use --reference only while cloning")),
2517 OPT_STRING(0, "depth", &suc
.depth
, "<depth>",
2518 N_("create a shallow clone truncated to the "
2519 "specified number of revisions")),
2520 OPT_INTEGER('j', "jobs", &suc
.max_jobs
,
2521 N_("parallel jobs")),
2522 OPT_BOOL(0, "recommend-shallow", &suc
.recommend_shallow
,
2523 N_("whether the initial clone should follow the shallow recommendation")),
2524 OPT__QUIET(&suc
.quiet
, N_("don't print cloning progress")),
2525 OPT_BOOL(0, "progress", &suc
.progress
,
2526 N_("force cloning progress")),
2527 OPT_BOOL(0, "require-init", &suc
.require_init
,
2528 N_("disallow cloning into non-empty directory")),
2529 OPT_BOOL(0, "single-branch", &suc
.single_branch
,
2530 N_("clone only one branch, HEAD or --branch")),
2534 const char *const git_submodule_helper_usage
[] = {
2535 N_("git submodule--helper update-clone [--prefix=<path>] [<path>...]"),
2538 suc
.prefix
= prefix
;
2540 update_clone_config_from_gitmodules(&suc
.max_jobs
);
2541 git_config(git_update_clone_config
, &suc
.max_jobs
);
2543 argc
= parse_options(argc
, argv
, prefix
, module_update_clone_options
,
2544 git_submodule_helper_usage
, 0);
2547 if (parse_submodule_update_strategy(update
, &suc
.update
) < 0)
2548 die(_("bad value for update parameter"));
2550 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &suc
.list
) < 0)
2554 suc
.warn_if_uninitialized
= 1;
2556 return update_submodules(&suc
);
2559 static int run_update_procedure(int argc
, const char **argv
, const char *prefix
)
2561 int force
= 0, quiet
= 0, nofetch
= 0, just_cloned
= 0;
2562 char *prefixed_path
, *update
= NULL
;
2563 struct update_data update_data
= UPDATE_DATA_INIT
;
2565 struct option options
[] = {
2566 OPT__QUIET(&quiet
, N_("suppress output for update by rebase or merge")),
2567 OPT__FORCE(&force
, N_("force checkout updates"), 0),
2568 OPT_BOOL('N', "no-fetch", &nofetch
,
2569 N_("don't fetch new objects from the remote site")),
2570 OPT_BOOL(0, "just-cloned", &just_cloned
,
2571 N_("overrides update mode in case the repository is a fresh clone")),
2572 OPT_INTEGER(0, "depth", &update_data
.depth
, N_("depth for shallow fetch")),
2573 OPT_STRING(0, "prefix", &prefix
,
2575 N_("path into the working tree")),
2576 OPT_STRING(0, "update", &update
,
2578 N_("rebase, merge, checkout or none")),
2579 OPT_STRING(0, "recursive-prefix", &update_data
.recursive_prefix
, N_("path"),
2580 N_("path into the working tree, across nested "
2581 "submodule boundaries")),
2582 OPT_CALLBACK_F(0, "oid", &update_data
.oid
, N_("sha1"),
2583 N_("SHA1 expected by superproject"), PARSE_OPT_NONEG
,
2584 parse_opt_object_id
),
2585 OPT_CALLBACK_F(0, "suboid", &update_data
.suboid
, N_("subsha1"),
2586 N_("SHA1 of submodule's HEAD"), PARSE_OPT_NONEG
,
2587 parse_opt_object_id
),
2591 const char *const usage
[] = {
2592 N_("git submodule--helper run-update-procedure [<options>] <path>"),
2596 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2599 usage_with_options(usage
, options
);
2601 update_data
.force
= !!force
;
2602 update_data
.quiet
= !!quiet
;
2603 update_data
.nofetch
= !!nofetch
;
2604 update_data
.just_cloned
= !!just_cloned
;
2605 update_data
.sm_path
= argv
[0];
2607 if (update_data
.recursive_prefix
)
2608 prefixed_path
= xstrfmt("%s%s", update_data
.recursive_prefix
, update_data
.sm_path
);
2610 prefixed_path
= xstrdup(update_data
.sm_path
);
2612 update_data
.displaypath
= get_submodule_displaypath(prefixed_path
, prefix
);
2614 determine_submodule_update_strategy(the_repository
, update_data
.just_cloned
,
2615 update_data
.sm_path
, update
,
2616 &update_data
.update_strategy
);
2618 free(prefixed_path
);
2620 if (!oideq(&update_data
.oid
, &update_data
.suboid
) || update_data
.force
)
2621 return do_run_update_procedure(&update_data
);
2626 static int resolve_relative_path(int argc
, const char **argv
, const char *prefix
)
2628 struct strbuf sb
= STRBUF_INIT
;
2630 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc
);
2632 printf("%s", relative_path(argv
[1], argv
[2], &sb
));
2633 strbuf_release(&sb
);
2637 static const char *remote_submodule_branch(const char *path
)
2639 const struct submodule
*sub
;
2640 const char *branch
= NULL
;
2643 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2647 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2648 if (repo_config_get_string_tmp(the_repository
, key
, &branch
))
2649 branch
= sub
->branch
;
2655 if (!strcmp(branch
, ".")) {
2656 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2659 die(_("No such ref: %s"), "HEAD");
2662 if (!strcmp(refname
, "HEAD"))
2663 die(_("Submodule (%s) branch configured to inherit "
2664 "branch from superproject, but the superproject "
2665 "is not on any branch"), sub
->name
);
2667 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2668 die(_("Expecting a full ref name, got %s"), refname
);
2675 static int resolve_remote_submodule_branch(int argc
, const char **argv
,
2679 struct strbuf sb
= STRBUF_INIT
;
2681 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc
);
2683 ret
= remote_submodule_branch(argv
[1]);
2685 die("submodule %s doesn't exist", argv
[1]);
2688 strbuf_release(&sb
);
2692 static int push_check(int argc
, const char **argv
, const char *prefix
)
2694 struct remote
*remote
;
2695 const char *superproject_head
;
2697 int detached_head
= 0;
2698 struct object_id head_oid
;
2701 die("submodule--helper push-check requires at least 2 arguments");
2704 * superproject's resolved head ref.
2705 * if HEAD then the superproject is in a detached head state, otherwise
2706 * it will be the resolved head ref.
2708 superproject_head
= argv
[1];
2711 /* Get the submodule's head ref and determine if it is detached */
2712 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2714 die(_("Failed to resolve HEAD as a valid ref."));
2715 if (!strcmp(head
, "HEAD"))
2719 * The remote must be configured.
2720 * This is to avoid pushing to the exact same URL as the parent.
2722 remote
= pushremote_get(argv
[1]);
2723 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2724 die("remote '%s' not configured", argv
[1]);
2726 /* Check the refspec */
2729 struct ref
*local_refs
= get_local_heads();
2730 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2732 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2734 for (i
= 0; i
< refspec
.nr
; i
++) {
2735 const struct refspec_item
*rs
= &refspec
.items
[i
];
2737 if (rs
->pattern
|| rs
->matching
)
2740 /* LHS must match a single ref */
2741 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2746 * If LHS matches 'HEAD' then we need to ensure
2747 * that it matches the same named branch
2748 * checked out in the superproject.
2750 if (!strcmp(rs
->src
, "HEAD")) {
2751 if (!detached_head
&&
2752 !strcmp(head
, superproject_head
))
2754 die("HEAD does not match the named branch in the superproject");
2758 die("src refspec '%s' must name a ref",
2762 refspec_clear(&refspec
);
2769 static int ensure_core_worktree(int argc
, const char **argv
, const char *prefix
)
2773 struct repository subrepo
;
2776 BUG("submodule--helper ensure-core-worktree <path>");
2780 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2781 die(_("could not get a repository handle for submodule '%s'"), path
);
2783 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2784 char *cfg_file
, *abs_path
;
2785 const char *rel_path
;
2786 struct strbuf sb
= STRBUF_INIT
;
2788 cfg_file
= repo_git_path(&subrepo
, "config");
2790 abs_path
= absolute_pathdup(path
);
2791 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2793 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2797 strbuf_release(&sb
);
2803 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2806 struct pathspec pathspec
;
2807 struct module_list list
= MODULE_LIST_INIT
;
2808 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
2810 struct option embed_gitdir_options
[] = {
2811 OPT_STRING(0, "prefix", &prefix
,
2813 N_("path into the working tree")),
2814 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
2815 ABSORB_GITDIR_RECURSE_SUBMODULES
),
2819 const char *const git_submodule_helper_usage
[] = {
2820 N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"),
2824 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2825 git_submodule_helper_usage
, 0);
2827 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
2830 for (i
= 0; i
< list
.nr
; i
++)
2831 absorb_git_dir_into_superproject(list
.entries
[i
]->name
, flags
);
2836 static int is_active(int argc
, const char **argv
, const char *prefix
)
2839 die("submodule--helper is-active takes exactly 1 argument");
2841 return !is_submodule_active(the_repository
, argv
[1]);
2845 * Exit non-zero if any of the submodule names given on the command line is
2846 * invalid. If no names are given, filter stdin to print only valid names
2847 * (which is primarily intended for testing).
2849 static int check_name(int argc
, const char **argv
, const char *prefix
)
2853 if (check_submodule_name(*argv
) < 0)
2857 struct strbuf buf
= STRBUF_INIT
;
2858 while (strbuf_getline(&buf
, stdin
) != EOF
) {
2859 if (!check_submodule_name(buf
.buf
))
2860 printf("%s\n", buf
.buf
);
2862 strbuf_release(&buf
);
2867 static int module_config(int argc
, const char **argv
, const char *prefix
)
2870 CHECK_WRITEABLE
= 1,
2874 struct option module_config_options
[] = {
2875 OPT_CMDMODE(0, "check-writeable", &command
,
2876 N_("check if it is safe to write to the .gitmodules file"),
2878 OPT_CMDMODE(0, "unset", &command
,
2879 N_("unset the config in the .gitmodules file"),
2883 const char *const git_submodule_helper_usage
[] = {
2884 N_("git submodule--helper config <name> [<value>]"),
2885 N_("git submodule--helper config --unset <name>"),
2886 N_("git submodule--helper config --check-writeable"),
2890 argc
= parse_options(argc
, argv
, prefix
, module_config_options
,
2891 git_submodule_helper_usage
, PARSE_OPT_KEEP_ARGV0
);
2893 if (argc
== 1 && command
== CHECK_WRITEABLE
)
2894 return is_writing_gitmodules_ok() ? 0 : -1;
2896 /* Equivalent to ACTION_GET in builtin/config.c */
2897 if (argc
== 2 && command
!= DO_UNSET
)
2898 return print_config_from_gitmodules(the_repository
, argv
[1]);
2900 /* Equivalent to ACTION_SET in builtin/config.c */
2901 if (argc
== 3 || (argc
== 2 && command
== DO_UNSET
)) {
2902 const char *value
= (argc
== 3) ? argv
[2] : NULL
;
2904 if (!is_writing_gitmodules_ok())
2905 die(_("please make sure that the .gitmodules file is in the working tree"));
2907 return config_set_in_gitmodules_file_gently(argv
[1], value
);
2910 usage_with_options(git_submodule_helper_usage
, module_config_options
);
2913 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2920 struct option options
[] = {
2921 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2924 const char *const usage
[] = {
2925 N_("git submodule--helper set-url [--quiet] <path> <newurl>"),
2929 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2931 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2932 usage_with_options(usage
, options
);
2934 config_name
= xstrfmt("submodule.%s.url", path
);
2936 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2937 sync_submodule(path
, prefix
, quiet
? OPT_QUIET
: 0);
2944 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2946 int opt_default
= 0, ret
;
2947 const char *opt_branch
= NULL
;
2952 * We accept the `quiet` option for uniformity across subcommands,
2953 * though there is nothing to make less verbose in this subcommand.
2955 struct option options
[] = {
2956 OPT_NOOP_NOARG('q', "quiet"),
2957 OPT_BOOL('d', "default", &opt_default
,
2958 N_("set the default tracking branch to master")),
2959 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2960 N_("set the default tracking branch")),
2963 const char *const usage
[] = {
2964 N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"),
2965 N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2969 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2971 if (!opt_branch
&& !opt_default
)
2972 die(_("--branch or --default required"));
2974 if (opt_branch
&& opt_default
)
2975 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2977 if (argc
!= 1 || !(path
= argv
[0]))
2978 usage_with_options(usage
, options
);
2980 config_name
= xstrfmt("submodule.%s.branch", path
);
2981 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2990 const char *reference_path
;
2992 const char *sm_name
;
2994 const char *realrepo
;
2996 unsigned int force
: 1;
2997 unsigned int quiet
: 1;
2998 unsigned int progress
: 1;
2999 unsigned int dissociate
: 1;
3001 #define ADD_DATA_INIT { .depth = -1 }
3003 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3005 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3006 struct strbuf sb_remote_out
= STRBUF_INIT
;
3008 cp_remote
.git_cmd
= 1;
3009 strvec_pushf(&cp_remote
.env_array
,
3010 "GIT_DIR=%s", git_dir_path
);
3011 strvec_push(&cp_remote
.env_array
, "GIT_WORK_TREE=.");
3012 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3013 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3015 char *line
= sb_remote_out
.buf
;
3016 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3017 size_t len
= next_line
- line
;
3018 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3019 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3020 line
= next_line
+ 1;
3024 strbuf_release(&sb_remote_out
);
3027 static int add_submodule(const struct add_data
*add_data
)
3029 char *submod_gitdir_path
;
3030 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3032 /* perhaps the path already exists and is already a git repo, else clone it */
3033 if (is_directory(add_data
->sm_path
)) {
3034 struct strbuf sm_path
= STRBUF_INIT
;
3035 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3036 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3037 if (is_nonbare_repository_dir(&sm_path
))
3038 printf(_("Adding existing repo at '%s' to the index\n"),
3041 die(_("'%s' already exists and is not a valid git repo"),
3043 strbuf_release(&sm_path
);
3044 free(submod_gitdir_path
);
3046 struct child_process cp
= CHILD_PROCESS_INIT
;
3047 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3049 if (is_directory(submod_gitdir_path
)) {
3050 if (!add_data
->force
) {
3051 struct strbuf msg
= STRBUF_INIT
;
3054 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3055 "locally with remote(s):\n"),
3058 append_fetch_remotes(&msg
, submod_gitdir_path
);
3059 free(submod_gitdir_path
);
3061 strbuf_addf(&msg
, _("If you want to reuse this local git "
3062 "directory instead of cloning again from\n"
3064 "use the '--force' option. If the local git "
3065 "directory is not the correct repo\n"
3066 "or you are unsure what this means choose "
3067 "another name with the '--name' option."),
3068 add_data
->realrepo
);
3070 die_msg
= strbuf_detach(&msg
, NULL
);
3073 printf(_("Reactivating local git directory for "
3074 "submodule '%s'\n"), add_data
->sm_name
);
3077 free(submod_gitdir_path
);
3079 clone_data
.prefix
= add_data
->prefix
;
3080 clone_data
.path
= add_data
->sm_path
;
3081 clone_data
.name
= add_data
->sm_name
;
3082 clone_data
.url
= add_data
->realrepo
;
3083 clone_data
.quiet
= add_data
->quiet
;
3084 clone_data
.progress
= add_data
->progress
;
3085 if (add_data
->reference_path
)
3086 string_list_append(&clone_data
.reference
,
3087 xstrdup(add_data
->reference_path
));
3088 clone_data
.dissociate
= add_data
->dissociate
;
3089 if (add_data
->depth
>= 0)
3090 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3092 if (clone_submodule(&clone_data
))
3095 prepare_submodule_repo_env(&cp
.env_array
);
3097 cp
.dir
= add_data
->sm_path
;
3099 * NOTE: we only get here if add_data->force is true, so
3100 * passing --force to checkout is reasonable.
3102 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3104 if (add_data
->branch
) {
3105 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3106 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3109 if (run_command(&cp
))
3110 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3115 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3120 if (!is_writing_gitmodules_ok())
3121 die(_("please make sure that the .gitmodules file is in the working tree"));
3123 key
= xstrfmt("submodule.%s.%s", name
, var
);
3124 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3130 static void configure_added_submodule(struct add_data
*add_data
)
3134 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3135 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3137 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3138 git_config_set_gently(key
, add_data
->realrepo
);
3141 add_submod
.git_cmd
= 1;
3142 strvec_pushl(&add_submod
.args
, "add",
3143 "--no-warn-embedded-repo", NULL
);
3144 if (add_data
->force
)
3145 strvec_push(&add_submod
.args
, "--force");
3146 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3148 if (run_command(&add_submod
))
3149 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3151 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3152 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3153 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3155 if (add_data
->branch
) {
3156 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3157 "branch", add_data
->branch
))
3158 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3161 add_gitmodules
.git_cmd
= 1;
3162 strvec_pushl(&add_gitmodules
.args
,
3163 "add", "--force", "--", ".gitmodules", NULL
);
3165 if (run_command(&add_gitmodules
))
3166 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3169 * NEEDSWORK: In a multi-working-tree world this needs to be
3170 * set in the per-worktree config.
3173 * NEEDSWORK: In the longer run, we need to get rid of this
3174 * pattern of querying "submodule.active" before calling
3175 * is_submodule_active(), since that function needs to find
3176 * out the value of "submodule.active" again anyway.
3178 if (!git_config_get_string("submodule.active", &val
) && val
) {
3180 * If the submodule being added isn't already covered by the
3181 * current configured pathspec, set the submodule's active flag
3183 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3184 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3185 git_config_set_gently(key
, "true");
3189 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3190 git_config_set_gently(key
, "true");
3195 static void die_on_index_match(const char *path
, int force
)
3198 const char *args
[] = { path
, NULL
};
3199 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3201 if (read_cache_preload(NULL
) < 0)
3202 die(_("index file corrupt"));
3206 char *ps_matched
= xcalloc(ps
.nr
, 1);
3208 /* TODO: audit for interaction with sparse-index. */
3209 ensure_full_index(&the_index
);
3212 * Since there is only one pathspec, we just need
3213 * need to check ps_matched[0] to know if a cache
3216 for (i
= 0; i
< active_nr
; i
++) {
3217 ce_path_match(&the_index
, active_cache
[i
], &ps
,
3220 if (ps_matched
[0]) {
3222 die(_("'%s' already exists in the index"),
3224 if (!S_ISGITLINK(active_cache
[i
]->ce_mode
))
3225 die(_("'%s' already exists in the index "
3226 "and is not a submodule"), path
);
3232 clear_pathspec(&ps
);
3235 static void die_on_repo_without_commits(const char *path
)
3237 struct strbuf sb
= STRBUF_INIT
;
3238 strbuf_addstr(&sb
, path
);
3239 if (is_nonbare_repository_dir(&sb
)) {
3240 struct object_id oid
;
3241 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3242 die(_("'%s' does not have a commit checked out"), path
);
3244 strbuf_release(&sb
);
3247 static int module_add(int argc
, const char **argv
, const char *prefix
)
3249 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3250 struct add_data add_data
= ADD_DATA_INIT
;
3252 struct option options
[] = {
3253 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3254 N_("branch of repository to add as submodule")),
3255 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3256 PARSE_OPT_NOCOMPLETE
),
3257 OPT__QUIET(&quiet
, N_("print only error messages")),
3258 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3259 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3260 N_("reference repository")),
3261 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3262 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3263 N_("sets the submodule’s name to the given string "
3264 "instead of defaulting to its path")),
3265 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3269 const char *const usage
[] = {
3270 N_("git submodule--helper add [<options>] [--] <repository> [<path>]"),
3274 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3276 if (!is_writing_gitmodules_ok())
3277 die(_("please make sure that the .gitmodules file is in the working tree"));
3279 if (prefix
&& *prefix
&&
3280 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3281 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3283 if (argc
== 0 || argc
> 2)
3284 usage_with_options(usage
, options
);
3286 add_data
.repo
= argv
[0];
3288 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3290 add_data
.sm_path
= xstrdup(argv
[1]);
3292 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
))
3293 add_data
.sm_path
= xstrfmt("%s%s", prefix
, add_data
.sm_path
);
3295 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3296 starts_with_dot_slash(add_data
.repo
)) {
3298 die(_("Relative path can only be used from the toplevel "
3299 "of the working tree"));
3301 /* dereference source url relative to parent's url */
3302 add_data
.realrepo
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3303 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3304 add_data
.realrepo
= add_data
.repo
;
3306 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3312 * multiple //; leading ./; /./; /../;
3314 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3315 strip_dir_trailing_slashes(add_data
.sm_path
);
3317 die_on_index_match(add_data
.sm_path
, force
);
3318 die_on_repo_without_commits(add_data
.sm_path
);
3322 struct strbuf sb
= STRBUF_INIT
;
3323 struct child_process cp
= CHILD_PROCESS_INIT
;
3326 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3327 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3328 if ((exit_code
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3329 strbuf_complete_line(&sb
);
3330 fputs(sb
.buf
, stderr
);
3331 free(add_data
.sm_path
);
3334 strbuf_release(&sb
);
3337 if(!add_data
.sm_name
)
3338 add_data
.sm_name
= add_data
.sm_path
;
3340 if (check_submodule_name(add_data
.sm_name
))
3341 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3343 add_data
.prefix
= prefix
;
3344 add_data
.force
= !!force
;
3345 add_data
.quiet
= !!quiet
;
3346 add_data
.progress
= !!progress
;
3347 add_data
.dissociate
= !!dissociate
;
3349 if (add_submodule(&add_data
)) {
3350 free(add_data
.sm_path
);
3353 configure_added_submodule(&add_data
);
3354 free(add_data
.sm_path
);
3359 #define SUPPORT_SUPER_PREFIX (1<<0)
3363 int (*fn
)(int, const char **, const char *);
3367 static struct cmd_struct commands
[] = {
3368 {"list", module_list
, 0},
3369 {"name", module_name
, 0},
3370 {"clone", module_clone
, 0},
3371 {"add", module_add
, SUPPORT_SUPER_PREFIX
},
3372 {"update-module-mode", module_update_module_mode
, 0},
3373 {"update-clone", update_clone
, 0},
3374 {"run-update-procedure", run_update_procedure
, 0},
3375 {"ensure-core-worktree", ensure_core_worktree
, 0},
3376 {"relative-path", resolve_relative_path
, 0},
3377 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
3378 {"foreach", module_foreach
, SUPPORT_SUPER_PREFIX
},
3379 {"init", module_init
, SUPPORT_SUPER_PREFIX
},
3380 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
3381 {"print-default-remote", print_default_remote
, 0},
3382 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
3383 {"deinit", module_deinit
, 0},
3384 {"summary", module_summary
, SUPPORT_SUPER_PREFIX
},
3385 {"remote-branch", resolve_remote_submodule_branch
, 0},
3386 {"push-check", push_check
, 0},
3387 {"absorb-git-dirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
3388 {"is-active", is_active
, 0},
3389 {"check-name", check_name
, 0},
3390 {"config", module_config
, 0},
3391 {"set-url", module_set_url
, 0},
3392 {"set-branch", module_set_branch
, 0},
3395 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3398 if (argc
< 2 || !strcmp(argv
[1], "-h"))
3399 usage("git submodule--helper <command>");
3401 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
3402 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
3403 if (get_super_prefix() &&
3404 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
3405 die(_("%s doesn't support --super-prefix"),
3407 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
3411 die(_("'%s' is not a valid submodule--helper "
3412 "subcommand"), argv
[1]);