2 #include "repository.h"
5 #include "parse-options.h"
10 #include "submodule-config.h"
11 #include "string-list.h"
12 #include "run-command.h"
20 #include "object-store.h"
22 #define OPT_QUIET (1 << 0)
23 #define OPT_CACHED (1 << 1)
24 #define OPT_RECURSIVE (1 << 2)
25 #define OPT_FORCE (1 << 3)
27 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
30 static char *get_default_remote(void)
32 char *dest
= NULL
, *ret
;
33 struct strbuf sb
= STRBUF_INIT
;
34 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
37 die(_("No such ref: %s"), "HEAD");
40 if (!strcmp(refname
, "HEAD"))
41 return xstrdup("origin");
43 if (!skip_prefix(refname
, "refs/heads/", &refname
))
44 die(_("Expecting a full ref name, got %s"), refname
);
46 strbuf_addf(&sb
, "branch.%s.remote", refname
);
47 if (git_config_get_string(sb
.buf
, &dest
))
48 ret
= xstrdup("origin");
56 static int print_default_remote(int argc
, const char **argv
, const char *prefix
)
61 die(_("submodule--helper print-default-remote takes no arguments"));
63 remote
= get_default_remote();
65 printf("%s\n", remote
);
70 static int starts_with_dot_slash(const char *str
)
72 return str
[0] == '.' && is_dir_sep(str
[1]);
75 static int starts_with_dot_dot_slash(const char *str
)
77 return str
[0] == '.' && str
[1] == '.' && is_dir_sep(str
[2]);
81 * Returns 1 if it was the last chop before ':'.
83 static int chop_last_dir(char **remoteurl
, int is_relative
)
85 char *rfind
= find_last_dir_sep(*remoteurl
);
91 rfind
= strrchr(*remoteurl
, ':');
97 if (is_relative
|| !strcmp(".", *remoteurl
))
98 die(_("cannot strip one component off url '%s'"),
102 *remoteurl
= xstrdup(".");
107 * The `url` argument is the URL that navigates to the submodule origin
108 * repo. When relative, this URL is relative to the superproject origin
109 * URL repo. The `up_path` argument, if specified, is the relative
110 * path that navigates from the submodule working tree to the superproject
111 * working tree. Returns the origin URL of the submodule.
113 * Return either an absolute URL or filesystem path (if the superproject
114 * origin URL is an absolute URL or filesystem path, respectively) or a
115 * relative file system path (if the superproject origin URL is a relative
118 * When the output is a relative file system path, the path is either
119 * relative to the submodule working tree, if up_path is specified, or to
120 * the superproject working tree otherwise.
122 * NEEDSWORK: This works incorrectly on the domain and protocol part.
123 * remote_url url outcome expectation
124 * http://a.com/b ../c http://a.com/c as is
125 * http://a.com/b/ ../c http://a.com/c same as previous line, but
126 * ignore trailing slash in url
127 * http://a.com/b ../../c http://c error out
128 * http://a.com/b ../../../c http:/c error out
129 * http://a.com/b ../../../../c http:c error out
130 * http://a.com/b ../../../../../c .:c error out
131 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
132 * when a local part has a colon in its path component, too.
134 static char *relative_url(const char *remote_url
,
141 char *remoteurl
= xstrdup(remote_url
);
142 struct strbuf sb
= STRBUF_INIT
;
143 size_t len
= strlen(remoteurl
);
145 if (is_dir_sep(remoteurl
[len
-1]))
146 remoteurl
[len
-1] = '\0';
148 if (!url_is_local_not_ssh(remoteurl
) || is_absolute_path(remoteurl
))
153 * Prepend a './' to ensure all relative
154 * remoteurls start with './' or '../'
156 if (!starts_with_dot_slash(remoteurl
) &&
157 !starts_with_dot_dot_slash(remoteurl
)) {
159 strbuf_addf(&sb
, "./%s", remoteurl
);
161 remoteurl
= strbuf_detach(&sb
, NULL
);
165 * When the url starts with '../', remove that and the
166 * last directory in remoteurl.
169 if (starts_with_dot_dot_slash(url
)) {
171 colonsep
|= chop_last_dir(&remoteurl
, is_relative
);
172 } else if (starts_with_dot_slash(url
))
178 strbuf_addf(&sb
, "%s%s%s", remoteurl
, colonsep
? ":" : "/", url
);
179 if (ends_with(url
, "/"))
180 strbuf_setlen(&sb
, sb
.len
- 1);
183 if (starts_with_dot_slash(sb
.buf
))
184 out
= xstrdup(sb
.buf
+ 2);
186 out
= xstrdup(sb
.buf
);
189 if (!up_path
|| !is_relative
)
192 strbuf_addf(&sb
, "%s%s", up_path
, out
);
194 return strbuf_detach(&sb
, NULL
);
197 static int resolve_relative_url(int argc
, const char **argv
, const char *prefix
)
199 char *remoteurl
= NULL
;
200 char *remote
= get_default_remote();
201 const char *up_path
= NULL
;
204 struct strbuf sb
= STRBUF_INIT
;
206 if (argc
!= 2 && argc
!= 3)
207 die("resolve-relative-url only accepts one or two arguments");
210 strbuf_addf(&sb
, "remote.%s.url", remote
);
213 if (git_config_get_string(sb
.buf
, &remoteurl
))
214 /* the repository is its own authoritative upstream */
215 remoteurl
= xgetcwd();
220 res
= relative_url(remoteurl
, url
, up_path
);
227 static int resolve_relative_url_test(int argc
, const char **argv
, const char *prefix
)
229 char *remoteurl
, *res
;
230 const char *up_path
, *url
;
233 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
236 remoteurl
= xstrdup(argv
[2]);
239 if (!strcmp(up_path
, "(null)"))
242 res
= relative_url(remoteurl
, url
, up_path
);
249 /* the result should be freed by the caller. */
250 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
252 const char *super_prefix
= get_super_prefix();
254 if (prefix
&& super_prefix
) {
255 BUG("cannot have prefix '%s' and superprefix '%s'",
256 prefix
, super_prefix
);
258 struct strbuf sb
= STRBUF_INIT
;
259 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
262 } else if (super_prefix
) {
263 return xstrfmt("%s%s", super_prefix
, path
);
265 return xstrdup(path
);
269 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
271 struct strbuf sb
= STRBUF_INIT
;
274 static const char *describe_bare
[] = { NULL
};
276 static const char *describe_tags
[] = { "--tags", NULL
};
278 static const char *describe_contains
[] = { "--contains", NULL
};
280 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
282 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
284 describe_all_always
, NULL
};
286 for (d
= describe_argv
; *d
; d
++) {
287 struct child_process cp
= CHILD_PROCESS_INIT
;
288 prepare_submodule_repo_env(&cp
.env_array
);
293 argv_array_push(&cp
.args
, "describe");
294 argv_array_pushv(&cp
.args
, *d
);
295 argv_array_push(&cp
.args
, object_id
);
297 if (!capture_command(&cp
, &sb
, 0)) {
298 strbuf_strip_suffix(&sb
, "\n");
299 return strbuf_detach(&sb
, NULL
);
308 const struct cache_entry
**entries
;
311 #define MODULE_LIST_INIT { NULL, 0, 0 }
313 static int module_list_compute(int argc
, const char **argv
,
315 struct pathspec
*pathspec
,
316 struct module_list
*list
)
319 char *ps_matched
= NULL
;
320 parse_pathspec(pathspec
, 0,
321 PATHSPEC_PREFER_FULL
,
325 ps_matched
= xcalloc(pathspec
->nr
, 1);
327 if (read_cache() < 0)
328 die(_("index file corrupt"));
330 for (i
= 0; i
< active_nr
; i
++) {
331 const struct cache_entry
*ce
= active_cache
[i
];
333 if (!match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
),
335 !S_ISGITLINK(ce
->ce_mode
))
338 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
339 list
->entries
[list
->nr
++] = ce
;
340 while (i
+ 1 < active_nr
&&
341 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
343 * Skip entries with the same name in different stages
344 * to make sure an entry is returned only once.
349 if (ps_matched
&& report_path_error(ps_matched
, pathspec
, prefix
))
357 static void module_list_active(struct module_list
*list
)
360 struct module_list active_modules
= MODULE_LIST_INIT
;
362 for (i
= 0; i
< list
->nr
; i
++) {
363 const struct cache_entry
*ce
= list
->entries
[i
];
365 if (!is_submodule_active(the_repository
, ce
->name
))
368 ALLOC_GROW(active_modules
.entries
,
369 active_modules
.nr
+ 1,
370 active_modules
.alloc
);
371 active_modules
.entries
[active_modules
.nr
++] = ce
;
375 *list
= active_modules
;
378 static char *get_up_path(const char *path
)
381 struct strbuf sb
= STRBUF_INIT
;
383 for (i
= count_slashes(path
); i
; i
--)
384 strbuf_addstr(&sb
, "../");
387 * Check if 'path' ends with slash or not
388 * for having the same output for dir/sub_dir
391 if (!is_dir_sep(path
[strlen(path
) - 1]))
392 strbuf_addstr(&sb
, "../");
394 return strbuf_detach(&sb
, NULL
);
397 static int module_list(int argc
, const char **argv
, const char *prefix
)
400 struct pathspec pathspec
;
401 struct module_list list
= MODULE_LIST_INIT
;
403 struct option module_list_options
[] = {
404 OPT_STRING(0, "prefix", &prefix
,
406 N_("alternative anchor for relative paths")),
410 const char *const git_submodule_helper_usage
[] = {
411 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
415 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
416 git_submodule_helper_usage
, 0);
418 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
421 for (i
= 0; i
< list
.nr
; i
++) {
422 const struct cache_entry
*ce
= list
.entries
[i
];
425 printf("%06o %s U\t", ce
->ce_mode
, sha1_to_hex(null_sha1
));
427 printf("%06o %s %d\t", ce
->ce_mode
,
428 oid_to_hex(&ce
->oid
), ce_stage(ce
));
430 fprintf(stdout
, "%s\n", ce
->name
);
435 static void for_each_listed_submodule(const struct module_list
*list
,
436 each_submodule_fn fn
, void *cb_data
)
439 for (i
= 0; i
< list
->nr
; i
++)
440 fn(list
->entries
[i
], cb_data
);
448 #define INIT_CB_INIT { NULL, 0 }
450 static void init_submodule(const char *path
, const char *prefix
,
453 const struct submodule
*sub
;
454 struct strbuf sb
= STRBUF_INIT
;
455 char *upd
= NULL
, *url
= NULL
, *displaypath
;
457 displaypath
= get_submodule_displaypath(path
, prefix
);
459 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
462 die(_("No url found for submodule path '%s' in .gitmodules"),
466 * NEEDSWORK: In a multi-working-tree world, this needs to be
467 * set in the per-worktree config.
469 * Set active flag for the submodule being initialized
471 if (!is_submodule_active(the_repository
, path
)) {
472 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
473 git_config_set_gently(sb
.buf
, "true");
478 * Copy url setting when it is not set yet.
479 * To look up the url in .git/config, we must not fall back to
480 * .gitmodules, so look it up directly.
482 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
483 if (git_config_get_string(sb
.buf
, &url
)) {
485 die(_("No url found for submodule path '%s' in .gitmodules"),
488 url
= xstrdup(sub
->url
);
490 /* Possibly a url relative to parent */
491 if (starts_with_dot_dot_slash(url
) ||
492 starts_with_dot_slash(url
)) {
493 char *remoteurl
, *relurl
;
494 char *remote
= get_default_remote();
495 struct strbuf remotesb
= STRBUF_INIT
;
496 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
499 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
500 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb
.buf
);
501 remoteurl
= xgetcwd();
503 relurl
= relative_url(remoteurl
, url
, NULL
);
504 strbuf_release(&remotesb
);
510 if (git_config_set_gently(sb
.buf
, url
))
511 die(_("Failed to register url for submodule path '%s'"),
513 if (!(flags
& OPT_QUIET
))
515 _("Submodule '%s' (%s) registered for path '%s'\n"),
516 sub
->name
, url
, displaypath
);
520 /* Copy "update" setting when it is not set yet */
521 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
522 if (git_config_get_string(sb
.buf
, &upd
) &&
523 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
524 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
525 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
527 upd
= xstrdup("none");
529 upd
= xstrdup(submodule_strategy_to_string(&sub
->update_strategy
));
531 if (git_config_set_gently(sb
.buf
, upd
))
532 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
540 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
542 struct init_cb
*info
= cb_data
;
543 init_submodule(list_item
->name
, info
->prefix
, info
->flags
);
546 static int module_init(int argc
, const char **argv
, const char *prefix
)
548 struct init_cb info
= INIT_CB_INIT
;
549 struct pathspec pathspec
;
550 struct module_list list
= MODULE_LIST_INIT
;
553 struct option module_init_options
[] = {
554 OPT__QUIET(&quiet
, N_("Suppress output for initializing a submodule")),
558 const char *const git_submodule_helper_usage
[] = {
559 N_("git submodule--helper init [<path>]"),
563 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
564 git_submodule_helper_usage
, 0);
566 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
570 * If there are no path args and submodule.active is set then,
571 * by default, only initialize 'active' modules.
573 if (!argc
&& git_config_get_value_multi("submodule.active"))
574 module_list_active(&list
);
576 info
.prefix
= prefix
;
578 info
.flags
|= OPT_QUIET
;
580 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
590 #define STATUS_CB_INIT { NULL, 0 }
592 static void print_status(unsigned int flags
, char state
, const char *path
,
593 const struct object_id
*oid
, const char *displaypath
)
595 if (flags
& OPT_QUIET
)
598 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
600 if (state
== ' ' || state
== '+') {
601 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
604 printf(" (%s)", name
);
610 static int handle_submodule_head_ref(const char *refname
,
611 const struct object_id
*oid
, int flags
,
614 struct object_id
*output
= cb_data
;
621 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
622 unsigned int ce_flags
, const char *prefix
,
626 struct argv_array diff_files_args
= ARGV_ARRAY_INIT
;
628 int diff_files_result
;
630 if (!submodule_from_path(the_repository
, &null_oid
, path
))
631 die(_("no submodule mapping found in .gitmodules for path '%s'"),
634 displaypath
= get_submodule_displaypath(path
, prefix
);
636 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
637 print_status(flags
, 'U', path
, &null_oid
, displaypath
);
641 if (!is_submodule_active(the_repository
, path
)) {
642 print_status(flags
, '-', path
, ce_oid
, displaypath
);
646 argv_array_pushl(&diff_files_args
, "diff-files",
647 "--ignore-submodules=dirty", "--quiet", "--",
650 git_config(git_diff_basic_config
, NULL
);
651 init_revisions(&rev
, prefix
);
653 diff_files_args
.argc
= setup_revisions(diff_files_args
.argc
,
654 diff_files_args
.argv
,
656 diff_files_result
= run_diff_files(&rev
, 0);
658 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
659 print_status(flags
, ' ', path
, ce_oid
,
661 } else if (!(flags
& OPT_CACHED
)) {
662 struct object_id oid
;
663 struct ref_store
*refs
= get_submodule_ref_store(path
);
666 print_status(flags
, '-', path
, ce_oid
, displaypath
);
669 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
670 die(_("could not resolve HEAD ref inside the "
671 "submodule '%s'"), path
);
673 print_status(flags
, '+', path
, &oid
, displaypath
);
675 print_status(flags
, '+', path
, ce_oid
, displaypath
);
678 if (flags
& OPT_RECURSIVE
) {
679 struct child_process cpr
= CHILD_PROCESS_INIT
;
683 prepare_submodule_repo_env(&cpr
.env_array
);
685 argv_array_push(&cpr
.args
, "--super-prefix");
686 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
687 argv_array_pushl(&cpr
.args
, "submodule--helper", "status",
688 "--recursive", NULL
);
690 if (flags
& OPT_CACHED
)
691 argv_array_push(&cpr
.args
, "--cached");
693 if (flags
& OPT_QUIET
)
694 argv_array_push(&cpr
.args
, "--quiet");
696 if (run_command(&cpr
))
697 die(_("failed to recurse into submodule '%s'"), path
);
701 argv_array_clear(&diff_files_args
);
705 static void status_submodule_cb(const struct cache_entry
*list_item
,
708 struct status_cb
*info
= cb_data
;
709 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
710 info
->prefix
, info
->flags
);
713 static int module_status(int argc
, const char **argv
, const char *prefix
)
715 struct status_cb info
= STATUS_CB_INIT
;
716 struct pathspec pathspec
;
717 struct module_list list
= MODULE_LIST_INIT
;
720 struct option module_status_options
[] = {
721 OPT__QUIET(&quiet
, N_("Suppress submodule status output")),
722 OPT_BIT(0, "cached", &info
.flags
, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
723 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
727 const char *const git_submodule_helper_usage
[] = {
728 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
732 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
733 git_submodule_helper_usage
, 0);
735 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
738 info
.prefix
= prefix
;
740 info
.flags
|= OPT_QUIET
;
742 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
747 static int module_name(int argc
, const char **argv
, const char *prefix
)
749 const struct submodule
*sub
;
752 usage(_("git submodule--helper name <path>"));
754 sub
= submodule_from_path(the_repository
, &null_oid
, argv
[1]);
757 die(_("no submodule mapping found in .gitmodules for path '%s'"),
760 printf("%s\n", sub
->name
);
770 #define SYNC_CB_INIT { NULL, 0 }
772 static void sync_submodule(const char *path
, const char *prefix
,
775 const struct submodule
*sub
;
776 char *remote_key
= NULL
;
777 char *sub_origin_url
, *super_config_url
, *displaypath
;
778 struct strbuf sb
= STRBUF_INIT
;
779 struct child_process cp
= CHILD_PROCESS_INIT
;
780 char *sub_config_path
= NULL
;
782 if (!is_submodule_active(the_repository
, path
))
785 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
787 if (sub
&& sub
->url
) {
788 if (starts_with_dot_dot_slash(sub
->url
) ||
789 starts_with_dot_slash(sub
->url
)) {
790 char *remote_url
, *up_path
;
791 char *remote
= get_default_remote();
792 strbuf_addf(&sb
, "remote.%s.url", remote
);
794 if (git_config_get_string(sb
.buf
, &remote_url
))
795 remote_url
= xgetcwd();
797 up_path
= get_up_path(path
);
798 sub_origin_url
= relative_url(remote_url
, sub
->url
, up_path
);
799 super_config_url
= relative_url(remote_url
, sub
->url
, NULL
);
805 sub_origin_url
= xstrdup(sub
->url
);
806 super_config_url
= xstrdup(sub
->url
);
809 sub_origin_url
= xstrdup("");
810 super_config_url
= xstrdup("");
813 displaypath
= get_submodule_displaypath(path
, prefix
);
815 if (!(flags
& OPT_QUIET
))
816 printf(_("Synchronizing submodule url for '%s'\n"),
820 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
821 if (git_config_set_gently(sb
.buf
, super_config_url
))
822 die(_("failed to register url for submodule path '%s'"),
825 if (!is_submodule_populated_gently(path
, NULL
))
828 prepare_submodule_repo_env(&cp
.env_array
);
831 argv_array_pushl(&cp
.args
, "submodule--helper",
832 "print-default-remote", NULL
);
835 if (capture_command(&cp
, &sb
, 0))
836 die(_("failed to get the default remote for submodule '%s'"),
839 strbuf_strip_suffix(&sb
, "\n");
840 remote_key
= xstrfmt("remote.%s.url", sb
.buf
);
843 submodule_to_gitdir(&sb
, path
);
844 strbuf_addstr(&sb
, "/config");
846 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
847 die(_("failed to update remote for submodule '%s'"),
850 if (flags
& OPT_RECURSIVE
) {
851 struct child_process cpr
= CHILD_PROCESS_INIT
;
855 prepare_submodule_repo_env(&cpr
.env_array
);
857 argv_array_push(&cpr
.args
, "--super-prefix");
858 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
859 argv_array_pushl(&cpr
.args
, "submodule--helper", "sync",
860 "--recursive", NULL
);
862 if (flags
& OPT_QUIET
)
863 argv_array_push(&cpr
.args
, "--quiet");
865 if (run_command(&cpr
))
866 die(_("failed to recurse into submodule '%s'"),
871 free(super_config_url
);
872 free(sub_origin_url
);
876 free(sub_config_path
);
879 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
881 struct sync_cb
*info
= cb_data
;
882 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
886 static int module_sync(int argc
, const char **argv
, const char *prefix
)
888 struct sync_cb info
= SYNC_CB_INIT
;
889 struct pathspec pathspec
;
890 struct module_list list
= MODULE_LIST_INIT
;
894 struct option module_sync_options
[] = {
895 OPT__QUIET(&quiet
, N_("Suppress output of synchronizing submodule url")),
896 OPT_BOOL(0, "recursive", &recursive
,
897 N_("Recurse into nested submodules")),
901 const char *const git_submodule_helper_usage
[] = {
902 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
906 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
907 git_submodule_helper_usage
, 0);
909 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
912 info
.prefix
= prefix
;
914 info
.flags
|= OPT_QUIET
;
916 info
.flags
|= OPT_RECURSIVE
;
918 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
927 #define DEINIT_CB_INIT { NULL, 0 }
929 static void deinit_submodule(const char *path
, const char *prefix
,
932 const struct submodule
*sub
;
933 char *displaypath
= NULL
;
934 struct child_process cp_config
= CHILD_PROCESS_INIT
;
935 struct strbuf sb_config
= STRBUF_INIT
;
936 char *sub_git_dir
= xstrfmt("%s/.git", path
);
938 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
940 if (!sub
|| !sub
->name
)
943 displaypath
= get_submodule_displaypath(path
, prefix
);
945 /* remove the submodule work tree (unless the user already did it) */
946 if (is_directory(path
)) {
947 struct strbuf sb_rm
= STRBUF_INIT
;
951 * protect submodules containing a .git directory
952 * NEEDSWORK: instead of dying, automatically call
953 * absorbgitdirs and (possibly) warn.
955 if (is_directory(sub_git_dir
))
956 die(_("Submodule work tree '%s' contains a .git "
957 "directory (use 'rm -rf' if you really want "
958 "to remove it including all of its history)"),
961 if (!(flags
& OPT_FORCE
)) {
962 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
964 argv_array_pushl(&cp_rm
.args
, "rm", "-qn",
967 if (run_command(&cp_rm
))
968 die(_("Submodule work tree '%s' contains local "
969 "modifications; use '-f' to discard them"),
973 strbuf_addstr(&sb_rm
, path
);
975 if (!remove_dir_recursively(&sb_rm
, 0))
976 format
= _("Cleared directory '%s'\n");
978 format
= _("Could not remove submodule work tree '%s'\n");
980 if (!(flags
& OPT_QUIET
))
981 printf(format
, displaypath
);
983 strbuf_release(&sb_rm
);
986 if (mkdir(path
, 0777))
987 printf(_("could not create empty submodule directory %s"),
990 cp_config
.git_cmd
= 1;
991 argv_array_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
992 argv_array_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
994 /* remove the .git/config entries (unless the user already did it) */
995 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
996 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
998 * remove the whole section so we have a clean state when
999 * the user later decides to init this submodule again
1001 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1002 if (!(flags
& OPT_QUIET
))
1003 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1004 sub
->name
, sub
->url
, displaypath
);
1011 strbuf_release(&sb_config
);
1014 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1017 struct deinit_cb
*info
= cb_data
;
1018 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1021 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1023 struct deinit_cb info
= DEINIT_CB_INIT
;
1024 struct pathspec pathspec
;
1025 struct module_list list
= MODULE_LIST_INIT
;
1030 struct option module_deinit_options
[] = {
1031 OPT__QUIET(&quiet
, N_("Suppress submodule status output")),
1032 OPT__FORCE(&force
, N_("Remove submodule working trees even if they contain local changes"), 0),
1033 OPT_BOOL(0, "all", &all
, N_("Unregister all submodules")),
1037 const char *const git_submodule_helper_usage
[] = {
1038 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1042 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1043 git_submodule_helper_usage
, 0);
1046 error("pathspec and --all are incompatible");
1047 usage_with_options(git_submodule_helper_usage
,
1048 module_deinit_options
);
1052 die(_("Use '--all' if you really want to deinitialize all submodules"));
1054 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1057 info
.prefix
= prefix
;
1059 info
.flags
|= OPT_QUIET
;
1061 info
.flags
|= OPT_FORCE
;
1063 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1068 static int clone_submodule(const char *path
, const char *gitdir
, const char *url
,
1069 const char *depth
, struct string_list
*reference
, int dissociate
,
1070 int quiet
, int progress
)
1072 struct child_process cp
= CHILD_PROCESS_INIT
;
1074 argv_array_push(&cp
.args
, "clone");
1075 argv_array_push(&cp
.args
, "--no-checkout");
1077 argv_array_push(&cp
.args
, "--quiet");
1079 argv_array_push(&cp
.args
, "--progress");
1080 if (depth
&& *depth
)
1081 argv_array_pushl(&cp
.args
, "--depth", depth
, NULL
);
1082 if (reference
->nr
) {
1083 struct string_list_item
*item
;
1084 for_each_string_list_item(item
, reference
)
1085 argv_array_pushl(&cp
.args
, "--reference",
1086 item
->string
, NULL
);
1089 argv_array_push(&cp
.args
, "--dissociate");
1090 if (gitdir
&& *gitdir
)
1091 argv_array_pushl(&cp
.args
, "--separate-git-dir", gitdir
, NULL
);
1093 argv_array_push(&cp
.args
, url
);
1094 argv_array_push(&cp
.args
, path
);
1097 prepare_submodule_repo_env(&cp
.env_array
);
1100 return run_command(&cp
);
1103 struct submodule_alternate_setup
{
1104 const char *submodule_name
;
1105 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1106 SUBMODULE_ALTERNATE_ERROR_DIE
,
1107 SUBMODULE_ALTERNATE_ERROR_INFO
,
1108 SUBMODULE_ALTERNATE_ERROR_IGNORE
1110 struct string_list
*reference
;
1112 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1113 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1115 static int add_possible_reference_from_superproject(
1116 struct alternate_object_database
*alt
, void *sas_cb
)
1118 struct submodule_alternate_setup
*sas
= sas_cb
;
1121 * If the alternate object store is another repository, try the
1122 * standard layout with .git/(modules/<name>)+/objects
1124 if (ends_with(alt
->path
, "/objects")) {
1126 struct strbuf sb
= STRBUF_INIT
;
1127 struct strbuf err
= STRBUF_INIT
;
1128 strbuf_add(&sb
, alt
->path
, strlen(alt
->path
) - strlen("objects"));
1131 * We need to end the new path with '/' to mark it as a dir,
1132 * otherwise a submodule name containing '/' will be broken
1133 * as the last part of a missing submodule reference would
1134 * be taken as a file name.
1136 strbuf_addf(&sb
, "modules/%s/", sas
->submodule_name
);
1138 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1140 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1143 switch (sas
->error_mode
) {
1144 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1145 die(_("submodule '%s' cannot add alternate: %s"),
1146 sas
->submodule_name
, err
.buf
);
1147 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1148 fprintf(stderr
, _("submodule '%s' cannot add alternate: %s"),
1149 sas
->submodule_name
, err
.buf
);
1150 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1154 strbuf_release(&sb
);
1160 static void prepare_possible_alternates(const char *sm_name
,
1161 struct string_list
*reference
)
1163 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1164 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1166 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1170 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1172 if (!error_strategy
)
1173 error_strategy
= xstrdup("die");
1175 sas
.submodule_name
= sm_name
;
1176 sas
.reference
= reference
;
1177 if (!strcmp(error_strategy
, "die"))
1178 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1179 else if (!strcmp(error_strategy
, "info"))
1180 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1181 else if (!strcmp(error_strategy
, "ignore"))
1182 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1184 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1186 if (!strcmp(sm_alternate
, "superproject"))
1187 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1188 else if (!strcmp(sm_alternate
, "no"))
1191 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1194 free(error_strategy
);
1197 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1199 const char *name
= NULL
, *url
= NULL
, *depth
= NULL
;
1202 char *p
, *path
= NULL
, *sm_gitdir
;
1203 struct strbuf sb
= STRBUF_INIT
;
1204 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1206 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1208 struct option module_clone_options
[] = {
1209 OPT_STRING(0, "prefix", &prefix
,
1211 N_("alternative anchor for relative paths")),
1212 OPT_STRING(0, "path", &path
,
1214 N_("where the new submodule will be cloned to")),
1215 OPT_STRING(0, "name", &name
,
1217 N_("name of the new submodule")),
1218 OPT_STRING(0, "url", &url
,
1220 N_("url where to clone the submodule from")),
1221 OPT_STRING_LIST(0, "reference", &reference
,
1223 N_("reference repository")),
1224 OPT_BOOL(0, "dissociate", &dissociate
,
1225 N_("use --reference only while cloning")),
1226 OPT_STRING(0, "depth", &depth
,
1228 N_("depth for shallow clones")),
1229 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
1230 OPT_BOOL(0, "progress", &progress
,
1231 N_("force cloning progress")),
1235 const char *const git_submodule_helper_usage
[] = {
1236 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1237 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1238 "--url <url> --path <path>"),
1242 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1243 git_submodule_helper_usage
, 0);
1245 if (argc
|| !url
|| !path
|| !*path
)
1246 usage_with_options(git_submodule_helper_usage
,
1247 module_clone_options
);
1249 strbuf_addf(&sb
, "%s/modules/%s", get_git_dir(), name
);
1250 sm_gitdir
= absolute_pathdup(sb
.buf
);
1253 if (!is_absolute_path(path
)) {
1254 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), path
);
1255 path
= strbuf_detach(&sb
, NULL
);
1257 path
= xstrdup(path
);
1259 if (!file_exists(sm_gitdir
)) {
1260 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1261 die(_("could not create directory '%s'"), sm_gitdir
);
1263 prepare_possible_alternates(name
, &reference
);
1265 if (clone_submodule(path
, sm_gitdir
, url
, depth
, &reference
, dissociate
,
1267 die(_("clone of '%s' into submodule path '%s' failed"),
1270 if (safe_create_leading_directories_const(path
) < 0)
1271 die(_("could not create directory '%s'"), path
);
1272 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1273 unlink_or_warn(sb
.buf
);
1277 connect_work_tree_and_git_dir(path
, sm_gitdir
, 0);
1279 p
= git_pathdup_submodule(path
, "config");
1281 die(_("could not get submodule directory for '%s'"), path
);
1283 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1284 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1286 git_config_set_in_file(p
, "submodule.alternateLocation",
1288 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1290 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1294 free(error_strategy
);
1296 strbuf_release(&sb
);
1303 struct submodule_update_clone
{
1304 /* index into 'list', the list of submodules to look into for cloning */
1306 struct module_list list
;
1307 unsigned warn_if_uninitialized
: 1;
1309 /* update parameter passed via commandline */
1310 struct submodule_update_strategy update
;
1312 /* configuration parameters which are passed on to the children */
1315 int recommend_shallow
;
1316 struct string_list references
;
1319 const char *recursive_prefix
;
1322 /* Machine-readable status lines to be consumed by git-submodule.sh */
1323 struct string_list projectlines
;
1325 /* If we want to stop as fast as possible and return an error */
1326 unsigned quickstop
: 1;
1328 /* failed clones to be retried again */
1329 const struct cache_entry
**failed_clones
;
1330 int failed_clones_nr
, failed_clones_alloc
;
1332 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1333 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
1335 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
1338 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1339 struct strbuf
*out
, const char *displaypath
)
1342 * Only mention uninitialized submodules when their
1343 * paths have been specified.
1345 if (suc
->warn_if_uninitialized
) {
1347 _("Submodule path '%s' not initialized"),
1349 strbuf_addch(out
, '\n');
1351 _("Maybe you want to use 'update --init'?"));
1352 strbuf_addch(out
, '\n');
1357 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1358 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1360 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1361 struct child_process
*child
,
1362 struct submodule_update_clone
*suc
,
1365 const struct submodule
*sub
= NULL
;
1366 const char *url
= NULL
;
1367 const char *update_string
;
1368 enum submodule_update_type update_type
;
1370 struct strbuf displaypath_sb
= STRBUF_INIT
;
1371 struct strbuf sb
= STRBUF_INIT
;
1372 const char *displaypath
= NULL
;
1373 int needs_cloning
= 0;
1376 if (suc
->recursive_prefix
)
1377 strbuf_addf(&sb
, "%s/%s", suc
->recursive_prefix
, ce
->name
);
1379 strbuf_addstr(&sb
, ce
->name
);
1380 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
1381 strbuf_addch(out
, '\n');
1385 sub
= submodule_from_path(the_repository
, &null_oid
, ce
->name
);
1387 if (suc
->recursive_prefix
)
1388 displaypath
= relative_path(suc
->recursive_prefix
,
1389 ce
->name
, &displaypath_sb
);
1391 displaypath
= ce
->name
;
1394 next_submodule_warn_missing(suc
, out
, displaypath
);
1398 key
= xstrfmt("submodule.%s.update", sub
->name
);
1399 if (!repo_config_get_string_const(the_repository
, key
, &update_string
)) {
1400 update_type
= parse_submodule_update_type(update_string
);
1402 update_type
= sub
->update_strategy
.type
;
1406 if (suc
->update
.type
== SM_UPDATE_NONE
1407 || (suc
->update
.type
== SM_UPDATE_UNSPECIFIED
1408 && update_type
== SM_UPDATE_NONE
)) {
1409 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
1410 strbuf_addch(out
, '\n');
1414 /* Check if the submodule has been initialized. */
1415 if (!is_submodule_active(the_repository
, ce
->name
)) {
1416 next_submodule_warn_missing(suc
, out
, displaypath
);
1421 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1422 if (repo_config_get_string_const(the_repository
, sb
.buf
, &url
))
1426 strbuf_addf(&sb
, "%s/.git", ce
->name
);
1427 needs_cloning
= !file_exists(sb
.buf
);
1430 strbuf_addf(&sb
, "%06o %s %d %d\t%s\n", ce
->ce_mode
,
1431 oid_to_hex(&ce
->oid
), ce_stage(ce
),
1432 needs_cloning
, ce
->name
);
1433 string_list_append(&suc
->projectlines
, sb
.buf
);
1439 child
->no_stdin
= 1;
1440 child
->stdout_to_stderr
= 1;
1442 argv_array_push(&child
->args
, "submodule--helper");
1443 argv_array_push(&child
->args
, "clone");
1445 argv_array_push(&child
->args
, "--progress");
1447 argv_array_push(&child
->args
, "--quiet");
1449 argv_array_pushl(&child
->args
, "--prefix", suc
->prefix
, NULL
);
1450 if (suc
->recommend_shallow
&& sub
->recommend_shallow
== 1)
1451 argv_array_push(&child
->args
, "--depth=1");
1452 argv_array_pushl(&child
->args
, "--path", sub
->path
, NULL
);
1453 argv_array_pushl(&child
->args
, "--name", sub
->name
, NULL
);
1454 argv_array_pushl(&child
->args
, "--url", url
, NULL
);
1455 if (suc
->references
.nr
) {
1456 struct string_list_item
*item
;
1457 for_each_string_list_item(item
, &suc
->references
)
1458 argv_array_pushl(&child
->args
, "--reference", item
->string
, NULL
);
1460 if (suc
->dissociate
)
1461 argv_array_push(&child
->args
, "--dissociate");
1463 argv_array_push(&child
->args
, suc
->depth
);
1466 strbuf_reset(&displaypath_sb
);
1469 return needs_cloning
;
1472 static int update_clone_get_next_task(struct child_process
*child
,
1477 struct submodule_update_clone
*suc
= suc_cb
;
1478 const struct cache_entry
*ce
;
1481 for (; suc
->current
< suc
->list
.nr
; suc
->current
++) {
1482 ce
= suc
->list
.entries
[suc
->current
];
1483 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
1484 int *p
= xmalloc(sizeof(*p
));
1493 * The loop above tried cloning each submodule once, now try the
1494 * stragglers again, which we can imagine as an extension of the
1497 index
= suc
->current
- suc
->list
.nr
;
1498 if (index
< suc
->failed_clones_nr
) {
1500 ce
= suc
->failed_clones
[index
];
1501 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
1503 strbuf_addstr(err
, "BUG: submodule considered for "
1504 "cloning, doesn't need cloning "
1508 p
= xmalloc(sizeof(*p
));
1518 static int update_clone_start_failure(struct strbuf
*err
,
1522 struct submodule_update_clone
*suc
= suc_cb
;
1527 static int update_clone_task_finished(int result
,
1532 const struct cache_entry
*ce
;
1533 struct submodule_update_clone
*suc
= suc_cb
;
1535 int *idxP
= idx_task_cb
;
1542 if (idx
< suc
->list
.nr
) {
1543 ce
= suc
->list
.entries
[idx
];
1544 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
1546 strbuf_addch(err
, '\n');
1547 ALLOC_GROW(suc
->failed_clones
,
1548 suc
->failed_clones_nr
+ 1,
1549 suc
->failed_clones_alloc
);
1550 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
1553 idx
-= suc
->list
.nr
;
1554 ce
= suc
->failed_clones
[idx
];
1555 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
1557 strbuf_addch(err
, '\n');
1565 static int gitmodules_update_clone_config(const char *var
, const char *value
,
1569 if (!strcmp(var
, "submodule.fetchjobs"))
1570 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
1574 static int update_clone(int argc
, const char **argv
, const char *prefix
)
1576 const char *update
= NULL
;
1578 struct string_list_item
*item
;
1579 struct pathspec pathspec
;
1580 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
1582 struct option module_update_clone_options
[] = {
1583 OPT_STRING(0, "prefix", &prefix
,
1585 N_("path into the working tree")),
1586 OPT_STRING(0, "recursive-prefix", &suc
.recursive_prefix
,
1588 N_("path into the working tree, across nested "
1589 "submodule boundaries")),
1590 OPT_STRING(0, "update", &update
,
1592 N_("rebase, merge, checkout or none")),
1593 OPT_STRING_LIST(0, "reference", &suc
.references
, N_("repo"),
1594 N_("reference repository")),
1595 OPT_BOOL(0, "dissociate", &suc
.dissociate
,
1596 N_("use --reference only while cloning")),
1597 OPT_STRING(0, "depth", &suc
.depth
, "<depth>",
1598 N_("Create a shallow clone truncated to the "
1599 "specified number of revisions")),
1600 OPT_INTEGER('j', "jobs", &max_jobs
,
1601 N_("parallel jobs")),
1602 OPT_BOOL(0, "recommend-shallow", &suc
.recommend_shallow
,
1603 N_("whether the initial clone should follow the shallow recommendation")),
1604 OPT__QUIET(&suc
.quiet
, N_("don't print cloning progress")),
1605 OPT_BOOL(0, "progress", &suc
.progress
,
1606 N_("force cloning progress")),
1610 const char *const git_submodule_helper_usage
[] = {
1611 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1614 suc
.prefix
= prefix
;
1616 config_from_gitmodules(gitmodules_update_clone_config
, &max_jobs
);
1617 git_config(gitmodules_update_clone_config
, &max_jobs
);
1619 argc
= parse_options(argc
, argv
, prefix
, module_update_clone_options
,
1620 git_submodule_helper_usage
, 0);
1623 if (parse_submodule_update_strategy(update
, &suc
.update
) < 0)
1624 die(_("bad value for update parameter"));
1626 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &suc
.list
) < 0)
1630 suc
.warn_if_uninitialized
= 1;
1632 run_processes_parallel(max_jobs
,
1633 update_clone_get_next_task
,
1634 update_clone_start_failure
,
1635 update_clone_task_finished
,
1639 * We saved the output and put it out all at once now.
1641 * - the listener does not have to interleave their (checkout)
1642 * work with our fetching. The writes involved in a
1643 * checkout involve more straightforward sequential I/O.
1644 * - the listener can avoid doing any work if fetching failed.
1649 for_each_string_list_item(item
, &suc
.projectlines
)
1650 fprintf(stdout
, "%s", item
->string
);
1655 static int resolve_relative_path(int argc
, const char **argv
, const char *prefix
)
1657 struct strbuf sb
= STRBUF_INIT
;
1659 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc
);
1661 printf("%s", relative_path(argv
[1], argv
[2], &sb
));
1662 strbuf_release(&sb
);
1666 static const char *remote_submodule_branch(const char *path
)
1668 const struct submodule
*sub
;
1669 const char *branch
= NULL
;
1672 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
1676 key
= xstrfmt("submodule.%s.branch", sub
->name
);
1677 if (repo_config_get_string_const(the_repository
, key
, &branch
))
1678 branch
= sub
->branch
;
1684 if (!strcmp(branch
, ".")) {
1685 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1688 die(_("No such ref: %s"), "HEAD");
1691 if (!strcmp(refname
, "HEAD"))
1692 die(_("Submodule (%s) branch configured to inherit "
1693 "branch from superproject, but the superproject "
1694 "is not on any branch"), sub
->name
);
1696 if (!skip_prefix(refname
, "refs/heads/", &refname
))
1697 die(_("Expecting a full ref name, got %s"), refname
);
1704 static int resolve_remote_submodule_branch(int argc
, const char **argv
,
1708 struct strbuf sb
= STRBUF_INIT
;
1710 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc
);
1712 ret
= remote_submodule_branch(argv
[1]);
1714 die("submodule %s doesn't exist", argv
[1]);
1717 strbuf_release(&sb
);
1721 static int push_check(int argc
, const char **argv
, const char *prefix
)
1723 struct remote
*remote
;
1724 const char *superproject_head
;
1726 int detached_head
= 0;
1727 struct object_id head_oid
;
1730 die("submodule--helper push-check requires at least 2 arguments");
1733 * superproject's resolved head ref.
1734 * if HEAD then the superproject is in a detached head state, otherwise
1735 * it will be the resolved head ref.
1737 superproject_head
= argv
[1];
1740 /* Get the submodule's head ref and determine if it is detached */
1741 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1743 die(_("Failed to resolve HEAD as a valid ref."));
1744 if (!strcmp(head
, "HEAD"))
1748 * The remote must be configured.
1749 * This is to avoid pushing to the exact same URL as the parent.
1751 remote
= pushremote_get(argv
[1]);
1752 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
1753 die("remote '%s' not configured", argv
[1]);
1755 /* Check the refspec */
1758 struct ref
*local_refs
= get_local_heads();
1759 struct refspec refspec
= REFSPEC_INIT_PUSH
;
1761 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
1763 for (i
= 0; i
< refspec
.nr
; i
++) {
1764 const struct refspec_item
*rs
= &refspec
.items
[i
];
1766 if (rs
->pattern
|| rs
->matching
)
1769 /* LHS must match a single ref */
1770 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
1775 * If LHS matches 'HEAD' then we need to ensure
1776 * that it matches the same named branch
1777 * checked out in the superproject.
1779 if (!strcmp(rs
->src
, "HEAD")) {
1780 if (!detached_head
&&
1781 !strcmp(head
, superproject_head
))
1783 die("HEAD does not match the named branch in the superproject");
1787 die("src refspec '%s' must name a ref",
1791 refspec_clear(&refspec
);
1798 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
1801 struct pathspec pathspec
;
1802 struct module_list list
= MODULE_LIST_INIT
;
1803 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
1805 struct option embed_gitdir_options
[] = {
1806 OPT_STRING(0, "prefix", &prefix
,
1808 N_("path into the working tree")),
1809 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
1810 ABSORB_GITDIR_RECURSE_SUBMODULES
),
1814 const char *const git_submodule_helper_usage
[] = {
1815 N_("git submodule--helper embed-git-dir [<path>...]"),
1819 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
1820 git_submodule_helper_usage
, 0);
1822 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1825 for (i
= 0; i
< list
.nr
; i
++)
1826 absorb_git_dir_into_superproject(prefix
,
1827 list
.entries
[i
]->name
, flags
);
1832 static int is_active(int argc
, const char **argv
, const char *prefix
)
1835 die("submodule--helper is-active takes exactly 1 argument");
1837 return !is_submodule_active(the_repository
, argv
[1]);
1841 * Exit non-zero if any of the submodule names given on the command line is
1842 * invalid. If no names are given, filter stdin to print only valid names
1843 * (which is primarily intended for testing).
1845 static int check_name(int argc
, const char **argv
, const char *prefix
)
1849 if (check_submodule_name(*argv
) < 0)
1853 struct strbuf buf
= STRBUF_INIT
;
1854 while (strbuf_getline(&buf
, stdin
) != EOF
) {
1855 if (!check_submodule_name(buf
.buf
))
1856 printf("%s\n", buf
.buf
);
1858 strbuf_release(&buf
);
1863 #define SUPPORT_SUPER_PREFIX (1<<0)
1867 int (*fn
)(int, const char **, const char *);
1871 static struct cmd_struct commands
[] = {
1872 {"list", module_list
, 0},
1873 {"name", module_name
, 0},
1874 {"clone", module_clone
, 0},
1875 {"update-clone", update_clone
, 0},
1876 {"relative-path", resolve_relative_path
, 0},
1877 {"resolve-relative-url", resolve_relative_url
, 0},
1878 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
1879 {"init", module_init
, SUPPORT_SUPER_PREFIX
},
1880 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
1881 {"print-default-remote", print_default_remote
, 0},
1882 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
1883 {"deinit", module_deinit
, 0},
1884 {"remote-branch", resolve_remote_submodule_branch
, 0},
1885 {"push-check", push_check
, 0},
1886 {"absorb-git-dirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
1887 {"is-active", is_active
, 0},
1888 {"check-name", check_name
, 0},
1891 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
1894 if (argc
< 2 || !strcmp(argv
[1], "-h"))
1895 usage("git submodule--helper <command>");
1897 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
1898 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
1899 if (get_super_prefix() &&
1900 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
1901 die(_("%s doesn't support --super-prefix"),
1903 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
1907 die(_("'%s' is not a valid submodule--helper "
1908 "subcommand"), argv
[1]);