2 #include "repository.h"
5 #include "parse-options.h"
10 #include "submodule-config.h"
11 #include "string-list.h"
12 #include "run-command.h"
19 #include "object-store.h"
21 #define OPT_QUIET (1 << 0)
22 #define OPT_CACHED (1 << 1)
23 #define OPT_RECURSIVE (1 << 2)
24 #define OPT_FORCE (1 << 3)
26 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
29 static char *get_default_remote(void)
31 char *dest
= NULL
, *ret
;
32 struct strbuf sb
= STRBUF_INIT
;
33 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
36 die(_("No such ref: %s"), "HEAD");
39 if (!strcmp(refname
, "HEAD"))
40 return xstrdup("origin");
42 if (!skip_prefix(refname
, "refs/heads/", &refname
))
43 die(_("Expecting a full ref name, got %s"), refname
);
45 strbuf_addf(&sb
, "branch.%s.remote", refname
);
46 if (git_config_get_string(sb
.buf
, &dest
))
47 ret
= xstrdup("origin");
55 static int print_default_remote(int argc
, const char **argv
, const char *prefix
)
60 die(_("submodule--helper print-default-remote takes no arguments"));
62 remote
= get_default_remote();
64 printf("%s\n", remote
);
69 static int starts_with_dot_slash(const char *str
)
71 return str
[0] == '.' && is_dir_sep(str
[1]);
74 static int starts_with_dot_dot_slash(const char *str
)
76 return str
[0] == '.' && str
[1] == '.' && is_dir_sep(str
[2]);
80 * Returns 1 if it was the last chop before ':'.
82 static int chop_last_dir(char **remoteurl
, int is_relative
)
84 char *rfind
= find_last_dir_sep(*remoteurl
);
90 rfind
= strrchr(*remoteurl
, ':');
96 if (is_relative
|| !strcmp(".", *remoteurl
))
97 die(_("cannot strip one component off url '%s'"),
101 *remoteurl
= xstrdup(".");
106 * The `url` argument is the URL that navigates to the submodule origin
107 * repo. When relative, this URL is relative to the superproject origin
108 * URL repo. The `up_path` argument, if specified, is the relative
109 * path that navigates from the submodule working tree to the superproject
110 * working tree. Returns the origin URL of the submodule.
112 * Return either an absolute URL or filesystem path (if the superproject
113 * origin URL is an absolute URL or filesystem path, respectively) or a
114 * relative file system path (if the superproject origin URL is a relative
117 * When the output is a relative file system path, the path is either
118 * relative to the submodule working tree, if up_path is specified, or to
119 * the superproject working tree otherwise.
121 * NEEDSWORK: This works incorrectly on the domain and protocol part.
122 * remote_url url outcome expectation
123 * http://a.com/b ../c http://a.com/c as is
124 * http://a.com/b/ ../c http://a.com/c same as previous line, but
125 * ignore trailing slash in url
126 * http://a.com/b ../../c http://c error out
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 .:c error out
130 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
131 * when a local part has a colon in its path component, too.
133 static char *relative_url(const char *remote_url
,
140 char *remoteurl
= xstrdup(remote_url
);
141 struct strbuf sb
= STRBUF_INIT
;
142 size_t len
= strlen(remoteurl
);
144 if (is_dir_sep(remoteurl
[len
-1]))
145 remoteurl
[len
-1] = '\0';
147 if (!url_is_local_not_ssh(remoteurl
) || is_absolute_path(remoteurl
))
152 * Prepend a './' to ensure all relative
153 * remoteurls start with './' or '../'
155 if (!starts_with_dot_slash(remoteurl
) &&
156 !starts_with_dot_dot_slash(remoteurl
)) {
158 strbuf_addf(&sb
, "./%s", remoteurl
);
160 remoteurl
= strbuf_detach(&sb
, NULL
);
164 * When the url starts with '../', remove that and the
165 * last directory in remoteurl.
168 if (starts_with_dot_dot_slash(url
)) {
170 colonsep
|= chop_last_dir(&remoteurl
, is_relative
);
171 } else if (starts_with_dot_slash(url
))
177 strbuf_addf(&sb
, "%s%s%s", remoteurl
, colonsep
? ":" : "/", url
);
178 if (ends_with(url
, "/"))
179 strbuf_setlen(&sb
, sb
.len
- 1);
182 if (starts_with_dot_slash(sb
.buf
))
183 out
= xstrdup(sb
.buf
+ 2);
185 out
= xstrdup(sb
.buf
);
188 if (!up_path
|| !is_relative
)
191 strbuf_addf(&sb
, "%s%s", up_path
, out
);
193 return strbuf_detach(&sb
, NULL
);
196 static int resolve_relative_url(int argc
, const char **argv
, const char *prefix
)
198 char *remoteurl
= NULL
;
199 char *remote
= get_default_remote();
200 const char *up_path
= NULL
;
203 struct strbuf sb
= STRBUF_INIT
;
205 if (argc
!= 2 && argc
!= 3)
206 die("resolve-relative-url only accepts one or two arguments");
209 strbuf_addf(&sb
, "remote.%s.url", remote
);
212 if (git_config_get_string(sb
.buf
, &remoteurl
))
213 /* the repository is its own authoritative upstream */
214 remoteurl
= xgetcwd();
219 res
= relative_url(remoteurl
, url
, up_path
);
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 argv_array_push(&cp
.args
, "describe");
293 argv_array_pushv(&cp
.args
, *d
);
294 argv_array_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 { NULL, 0, 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(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
, prefix
))
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
, sha1_to_hex(null_sha1
));
426 printf("%06o %s %d\t", ce
->ce_mode
,
427 oid_to_hex(&ce
->oid
), ce_stage(ce
));
429 fprintf(stdout
, "%s\n", ce
->name
);
434 static void for_each_listed_submodule(const struct module_list
*list
,
435 each_submodule_fn fn
, void *cb_data
)
438 for (i
= 0; i
< list
->nr
; i
++)
439 fn(list
->entries
[i
], cb_data
);
447 #define INIT_CB_INIT { NULL, 0 }
449 static void init_submodule(const char *path
, const char *prefix
,
452 const struct submodule
*sub
;
453 struct strbuf sb
= STRBUF_INIT
;
454 char *upd
= NULL
, *url
= NULL
, *displaypath
;
456 displaypath
= get_submodule_displaypath(path
, prefix
);
458 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
461 die(_("No url found for submodule path '%s' in .gitmodules"),
465 * NEEDSWORK: In a multi-working-tree world, this needs to be
466 * set in the per-worktree config.
468 * Set active flag for the submodule being initialized
470 if (!is_submodule_active(the_repository
, path
)) {
471 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
472 git_config_set_gently(sb
.buf
, "true");
477 * Copy url setting when it is not set yet.
478 * To look up the url in .git/config, we must not fall back to
479 * .gitmodules, so look it up directly.
481 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
482 if (git_config_get_string(sb
.buf
, &url
)) {
484 die(_("No url found for submodule path '%s' in .gitmodules"),
487 url
= xstrdup(sub
->url
);
489 /* Possibly a url relative to parent */
490 if (starts_with_dot_dot_slash(url
) ||
491 starts_with_dot_slash(url
)) {
492 char *remoteurl
, *relurl
;
493 char *remote
= get_default_remote();
494 struct strbuf remotesb
= STRBUF_INIT
;
495 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
498 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
499 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb
.buf
);
500 remoteurl
= xgetcwd();
502 relurl
= relative_url(remoteurl
, url
, NULL
);
503 strbuf_release(&remotesb
);
509 if (git_config_set_gently(sb
.buf
, url
))
510 die(_("Failed to register url for submodule path '%s'"),
512 if (!(flags
& OPT_QUIET
))
514 _("Submodule '%s' (%s) registered for path '%s'\n"),
515 sub
->name
, url
, displaypath
);
519 /* Copy "update" setting when it is not set yet */
520 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
521 if (git_config_get_string(sb
.buf
, &upd
) &&
522 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
523 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
524 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
526 upd
= xstrdup("none");
528 upd
= xstrdup(submodule_strategy_to_string(&sub
->update_strategy
));
530 if (git_config_set_gently(sb
.buf
, upd
))
531 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
539 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
541 struct init_cb
*info
= cb_data
;
542 init_submodule(list_item
->name
, info
->prefix
, info
->flags
);
545 static int module_init(int argc
, const char **argv
, const char *prefix
)
547 struct init_cb info
= INIT_CB_INIT
;
548 struct pathspec pathspec
;
549 struct module_list list
= MODULE_LIST_INIT
;
552 struct option module_init_options
[] = {
553 OPT__QUIET(&quiet
, N_("Suppress output for initializing a submodule")),
557 const char *const git_submodule_helper_usage
[] = {
558 N_("git submodule--helper init [<path>]"),
562 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
563 git_submodule_helper_usage
, 0);
565 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
569 * If there are no path args and submodule.active is set then,
570 * by default, only initialize 'active' modules.
572 if (!argc
&& git_config_get_value_multi("submodule.active"))
573 module_list_active(&list
);
575 info
.prefix
= prefix
;
577 info
.flags
|= OPT_QUIET
;
579 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
589 #define STATUS_CB_INIT { NULL, 0 }
591 static void print_status(unsigned int flags
, char state
, const char *path
,
592 const struct object_id
*oid
, const char *displaypath
)
594 if (flags
& OPT_QUIET
)
597 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
599 if (state
== ' ' || state
== '+')
600 printf(" (%s)", compute_rev_name(path
, oid_to_hex(oid
)));
605 static int handle_submodule_head_ref(const char *refname
,
606 const struct object_id
*oid
, int flags
,
609 struct object_id
*output
= cb_data
;
616 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
617 unsigned int ce_flags
, const char *prefix
,
621 struct argv_array diff_files_args
= ARGV_ARRAY_INIT
;
623 int diff_files_result
;
625 if (!submodule_from_path(the_repository
, &null_oid
, path
))
626 die(_("no submodule mapping found in .gitmodules for path '%s'"),
629 displaypath
= get_submodule_displaypath(path
, prefix
);
631 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
632 print_status(flags
, 'U', path
, &null_oid
, displaypath
);
636 if (!is_submodule_active(the_repository
, path
)) {
637 print_status(flags
, '-', path
, ce_oid
, displaypath
);
641 argv_array_pushl(&diff_files_args
, "diff-files",
642 "--ignore-submodules=dirty", "--quiet", "--",
645 git_config(git_diff_basic_config
, NULL
);
646 init_revisions(&rev
, prefix
);
648 diff_files_args
.argc
= setup_revisions(diff_files_args
.argc
,
649 diff_files_args
.argv
,
651 diff_files_result
= run_diff_files(&rev
, 0);
653 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
654 print_status(flags
, ' ', path
, ce_oid
,
656 } else if (!(flags
& OPT_CACHED
)) {
657 struct object_id oid
;
658 struct ref_store
*refs
= get_submodule_ref_store(path
);
661 print_status(flags
, '-', path
, ce_oid
, displaypath
);
664 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
665 die(_("could not resolve HEAD ref inside the "
666 "submodule '%s'"), path
);
668 print_status(flags
, '+', path
, &oid
, displaypath
);
670 print_status(flags
, '+', path
, ce_oid
, displaypath
);
673 if (flags
& OPT_RECURSIVE
) {
674 struct child_process cpr
= CHILD_PROCESS_INIT
;
678 prepare_submodule_repo_env(&cpr
.env_array
);
680 argv_array_push(&cpr
.args
, "--super-prefix");
681 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
682 argv_array_pushl(&cpr
.args
, "submodule--helper", "status",
683 "--recursive", NULL
);
685 if (flags
& OPT_CACHED
)
686 argv_array_push(&cpr
.args
, "--cached");
688 if (flags
& OPT_QUIET
)
689 argv_array_push(&cpr
.args
, "--quiet");
691 if (run_command(&cpr
))
692 die(_("failed to recurse into submodule '%s'"), path
);
696 argv_array_clear(&diff_files_args
);
700 static void status_submodule_cb(const struct cache_entry
*list_item
,
703 struct status_cb
*info
= cb_data
;
704 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
705 info
->prefix
, info
->flags
);
708 static int module_status(int argc
, const char **argv
, const char *prefix
)
710 struct status_cb info
= STATUS_CB_INIT
;
711 struct pathspec pathspec
;
712 struct module_list list
= MODULE_LIST_INIT
;
715 struct option module_status_options
[] = {
716 OPT__QUIET(&quiet
, N_("Suppress submodule status output")),
717 OPT_BIT(0, "cached", &info
.flags
, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
718 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
722 const char *const git_submodule_helper_usage
[] = {
723 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
727 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
728 git_submodule_helper_usage
, 0);
730 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
733 info
.prefix
= prefix
;
735 info
.flags
|= OPT_QUIET
;
737 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
742 static int module_name(int argc
, const char **argv
, const char *prefix
)
744 const struct submodule
*sub
;
747 usage(_("git submodule--helper name <path>"));
749 sub
= submodule_from_path(the_repository
, &null_oid
, argv
[1]);
752 die(_("no submodule mapping found in .gitmodules for path '%s'"),
755 printf("%s\n", sub
->name
);
765 #define SYNC_CB_INIT { NULL, 0 }
767 static void sync_submodule(const char *path
, const char *prefix
,
770 const struct submodule
*sub
;
771 char *remote_key
= NULL
;
772 char *sub_origin_url
, *super_config_url
, *displaypath
;
773 struct strbuf sb
= STRBUF_INIT
;
774 struct child_process cp
= CHILD_PROCESS_INIT
;
775 char *sub_config_path
= NULL
;
777 if (!is_submodule_active(the_repository
, path
))
780 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
782 if (sub
&& sub
->url
) {
783 if (starts_with_dot_dot_slash(sub
->url
) ||
784 starts_with_dot_slash(sub
->url
)) {
785 char *remote_url
, *up_path
;
786 char *remote
= get_default_remote();
787 strbuf_addf(&sb
, "remote.%s.url", remote
);
789 if (git_config_get_string(sb
.buf
, &remote_url
))
790 remote_url
= xgetcwd();
792 up_path
= get_up_path(path
);
793 sub_origin_url
= relative_url(remote_url
, sub
->url
, up_path
);
794 super_config_url
= relative_url(remote_url
, sub
->url
, NULL
);
800 sub_origin_url
= xstrdup(sub
->url
);
801 super_config_url
= xstrdup(sub
->url
);
804 sub_origin_url
= xstrdup("");
805 super_config_url
= xstrdup("");
808 displaypath
= get_submodule_displaypath(path
, prefix
);
810 if (!(flags
& OPT_QUIET
))
811 printf(_("Synchronizing submodule url for '%s'\n"),
815 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
816 if (git_config_set_gently(sb
.buf
, super_config_url
))
817 die(_("failed to register url for submodule path '%s'"),
820 if (!is_submodule_populated_gently(path
, NULL
))
823 prepare_submodule_repo_env(&cp
.env_array
);
826 argv_array_pushl(&cp
.args
, "submodule--helper",
827 "print-default-remote", NULL
);
830 if (capture_command(&cp
, &sb
, 0))
831 die(_("failed to get the default remote for submodule '%s'"),
834 strbuf_strip_suffix(&sb
, "\n");
835 remote_key
= xstrfmt("remote.%s.url", sb
.buf
);
838 submodule_to_gitdir(&sb
, path
);
839 strbuf_addstr(&sb
, "/config");
841 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
842 die(_("failed to update remote for submodule '%s'"),
845 if (flags
& OPT_RECURSIVE
) {
846 struct child_process cpr
= CHILD_PROCESS_INIT
;
850 prepare_submodule_repo_env(&cpr
.env_array
);
852 argv_array_push(&cpr
.args
, "--super-prefix");
853 argv_array_pushf(&cpr
.args
, "%s/", displaypath
);
854 argv_array_pushl(&cpr
.args
, "submodule--helper", "sync",
855 "--recursive", NULL
);
857 if (flags
& OPT_QUIET
)
858 argv_array_push(&cpr
.args
, "--quiet");
860 if (run_command(&cpr
))
861 die(_("failed to recurse into submodule '%s'"),
866 free(super_config_url
);
867 free(sub_origin_url
);
871 free(sub_config_path
);
874 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
876 struct sync_cb
*info
= cb_data
;
877 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
881 static int module_sync(int argc
, const char **argv
, const char *prefix
)
883 struct sync_cb info
= SYNC_CB_INIT
;
884 struct pathspec pathspec
;
885 struct module_list list
= MODULE_LIST_INIT
;
889 struct option module_sync_options
[] = {
890 OPT__QUIET(&quiet
, N_("Suppress output of synchronizing submodule url")),
891 OPT_BOOL(0, "recursive", &recursive
,
892 N_("Recurse into nested submodules")),
896 const char *const git_submodule_helper_usage
[] = {
897 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
901 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
902 git_submodule_helper_usage
, 0);
904 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
907 info
.prefix
= prefix
;
909 info
.flags
|= OPT_QUIET
;
911 info
.flags
|= OPT_RECURSIVE
;
913 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
922 #define DEINIT_CB_INIT { NULL, 0 }
924 static void deinit_submodule(const char *path
, const char *prefix
,
927 const struct submodule
*sub
;
928 char *displaypath
= NULL
;
929 struct child_process cp_config
= CHILD_PROCESS_INIT
;
930 struct strbuf sb_config
= STRBUF_INIT
;
931 char *sub_git_dir
= xstrfmt("%s/.git", path
);
933 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
935 if (!sub
|| !sub
->name
)
938 displaypath
= get_submodule_displaypath(path
, prefix
);
940 /* remove the submodule work tree (unless the user already did it) */
941 if (is_directory(path
)) {
942 struct strbuf sb_rm
= STRBUF_INIT
;
946 * protect submodules containing a .git directory
947 * NEEDSWORK: instead of dying, automatically call
948 * absorbgitdirs and (possibly) warn.
950 if (is_directory(sub_git_dir
))
951 die(_("Submodule work tree '%s' contains a .git "
952 "directory (use 'rm -rf' if you really want "
953 "to remove it including all of its history)"),
956 if (!(flags
& OPT_FORCE
)) {
957 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
959 argv_array_pushl(&cp_rm
.args
, "rm", "-qn",
962 if (run_command(&cp_rm
))
963 die(_("Submodule work tree '%s' contains local "
964 "modifications; use '-f' to discard them"),
968 strbuf_addstr(&sb_rm
, path
);
970 if (!remove_dir_recursively(&sb_rm
, 0))
971 format
= _("Cleared directory '%s'\n");
973 format
= _("Could not remove submodule work tree '%s'\n");
975 if (!(flags
& OPT_QUIET
))
976 printf(format
, displaypath
);
978 strbuf_release(&sb_rm
);
981 if (mkdir(path
, 0777))
982 printf(_("could not create empty submodule directory %s"),
985 cp_config
.git_cmd
= 1;
986 argv_array_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
987 argv_array_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
989 /* remove the .git/config entries (unless the user already did it) */
990 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
991 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
993 * remove the whole section so we have a clean state when
994 * the user later decides to init this submodule again
996 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
997 if (!(flags
& OPT_QUIET
))
998 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
999 sub
->name
, sub
->url
, displaypath
);
1006 strbuf_release(&sb_config
);
1009 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1012 struct deinit_cb
*info
= cb_data
;
1013 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1016 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1018 struct deinit_cb info
= DEINIT_CB_INIT
;
1019 struct pathspec pathspec
;
1020 struct module_list list
= MODULE_LIST_INIT
;
1025 struct option module_deinit_options
[] = {
1026 OPT__QUIET(&quiet
, N_("Suppress submodule status output")),
1027 OPT__FORCE(&force
, N_("Remove submodule working trees even if they contain local changes"), 0),
1028 OPT_BOOL(0, "all", &all
, N_("Unregister all submodules")),
1032 const char *const git_submodule_helper_usage
[] = {
1033 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1037 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1038 git_submodule_helper_usage
, 0);
1041 error("pathspec and --all are incompatible");
1042 usage_with_options(git_submodule_helper_usage
,
1043 module_deinit_options
);
1047 die(_("Use '--all' if you really want to deinitialize all submodules"));
1049 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1052 info
.prefix
= prefix
;
1054 info
.flags
|= OPT_QUIET
;
1056 info
.flags
|= OPT_FORCE
;
1058 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1063 static int clone_submodule(const char *path
, const char *gitdir
, const char *url
,
1064 const char *depth
, struct string_list
*reference
,
1065 int quiet
, int progress
)
1067 struct child_process cp
= CHILD_PROCESS_INIT
;
1069 argv_array_push(&cp
.args
, "clone");
1070 argv_array_push(&cp
.args
, "--no-checkout");
1072 argv_array_push(&cp
.args
, "--quiet");
1074 argv_array_push(&cp
.args
, "--progress");
1075 if (depth
&& *depth
)
1076 argv_array_pushl(&cp
.args
, "--depth", depth
, NULL
);
1077 if (reference
->nr
) {
1078 struct string_list_item
*item
;
1079 for_each_string_list_item(item
, reference
)
1080 argv_array_pushl(&cp
.args
, "--reference",
1081 item
->string
, NULL
);
1083 if (gitdir
&& *gitdir
)
1084 argv_array_pushl(&cp
.args
, "--separate-git-dir", gitdir
, NULL
);
1086 argv_array_push(&cp
.args
, url
);
1087 argv_array_push(&cp
.args
, path
);
1090 prepare_submodule_repo_env(&cp
.env_array
);
1093 return run_command(&cp
);
1096 struct submodule_alternate_setup
{
1097 const char *submodule_name
;
1098 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1099 SUBMODULE_ALTERNATE_ERROR_DIE
,
1100 SUBMODULE_ALTERNATE_ERROR_INFO
,
1101 SUBMODULE_ALTERNATE_ERROR_IGNORE
1103 struct string_list
*reference
;
1105 #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1106 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1108 static int add_possible_reference_from_superproject(
1109 struct alternate_object_database
*alt
, void *sas_cb
)
1111 struct submodule_alternate_setup
*sas
= sas_cb
;
1114 * If the alternate object store is another repository, try the
1115 * standard layout with .git/(modules/<name>)+/objects
1117 if (ends_with(alt
->path
, "/objects")) {
1119 struct strbuf sb
= STRBUF_INIT
;
1120 struct strbuf err
= STRBUF_INIT
;
1121 strbuf_add(&sb
, alt
->path
, strlen(alt
->path
) - strlen("objects"));
1124 * We need to end the new path with '/' to mark it as a dir,
1125 * otherwise a submodule name containing '/' will be broken
1126 * as the last part of a missing submodule reference would
1127 * be taken as a file name.
1129 strbuf_addf(&sb
, "modules/%s/", sas
->submodule_name
);
1131 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1133 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1136 switch (sas
->error_mode
) {
1137 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1138 die(_("submodule '%s' cannot add alternate: %s"),
1139 sas
->submodule_name
, err
.buf
);
1140 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1141 fprintf(stderr
, _("submodule '%s' cannot add alternate: %s"),
1142 sas
->submodule_name
, err
.buf
);
1143 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1147 strbuf_release(&sb
);
1153 static void prepare_possible_alternates(const char *sm_name
,
1154 struct string_list
*reference
)
1156 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1157 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1159 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1163 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1165 if (!error_strategy
)
1166 error_strategy
= xstrdup("die");
1168 sas
.submodule_name
= sm_name
;
1169 sas
.reference
= reference
;
1170 if (!strcmp(error_strategy
, "die"))
1171 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1172 else if (!strcmp(error_strategy
, "info"))
1173 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1174 else if (!strcmp(error_strategy
, "ignore"))
1175 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1177 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1179 if (!strcmp(sm_alternate
, "superproject"))
1180 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1181 else if (!strcmp(sm_alternate
, "no"))
1184 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1187 free(error_strategy
);
1190 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1192 const char *name
= NULL
, *url
= NULL
, *depth
= NULL
;
1195 char *p
, *path
= NULL
, *sm_gitdir
;
1196 struct strbuf sb
= STRBUF_INIT
;
1197 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1198 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1200 struct option module_clone_options
[] = {
1201 OPT_STRING(0, "prefix", &prefix
,
1203 N_("alternative anchor for relative paths")),
1204 OPT_STRING(0, "path", &path
,
1206 N_("where the new submodule will be cloned to")),
1207 OPT_STRING(0, "name", &name
,
1209 N_("name of the new submodule")),
1210 OPT_STRING(0, "url", &url
,
1212 N_("url where to clone the submodule from")),
1213 OPT_STRING_LIST(0, "reference", &reference
,
1215 N_("reference repository")),
1216 OPT_STRING(0, "depth", &depth
,
1218 N_("depth for shallow clones")),
1219 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
1220 OPT_BOOL(0, "progress", &progress
,
1221 N_("force cloning progress")),
1225 const char *const git_submodule_helper_usage
[] = {
1226 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1227 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1228 "--url <url> --path <path>"),
1232 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1233 git_submodule_helper_usage
, 0);
1235 if (argc
|| !url
|| !path
|| !*path
)
1236 usage_with_options(git_submodule_helper_usage
,
1237 module_clone_options
);
1239 strbuf_addf(&sb
, "%s/modules/%s", get_git_dir(), name
);
1240 sm_gitdir
= absolute_pathdup(sb
.buf
);
1243 if (!is_absolute_path(path
)) {
1244 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), path
);
1245 path
= strbuf_detach(&sb
, NULL
);
1247 path
= xstrdup(path
);
1249 if (!file_exists(sm_gitdir
)) {
1250 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1251 die(_("could not create directory '%s'"), sm_gitdir
);
1253 prepare_possible_alternates(name
, &reference
);
1255 if (clone_submodule(path
, sm_gitdir
, url
, depth
, &reference
,
1257 die(_("clone of '%s' into submodule path '%s' failed"),
1260 if (safe_create_leading_directories_const(path
) < 0)
1261 die(_("could not create directory '%s'"), path
);
1262 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1263 unlink_or_warn(sb
.buf
);
1267 connect_work_tree_and_git_dir(path
, sm_gitdir
, 0);
1269 p
= git_pathdup_submodule(path
, "config");
1271 die(_("could not get submodule directory for '%s'"), path
);
1273 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1274 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1276 git_config_set_in_file(p
, "submodule.alternateLocation",
1278 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1280 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1284 free(error_strategy
);
1286 strbuf_release(&sb
);
1293 struct submodule_update_clone
{
1294 /* index into 'list', the list of submodules to look into for cloning */
1296 struct module_list list
;
1297 unsigned warn_if_uninitialized
: 1;
1299 /* update parameter passed via commandline */
1300 struct submodule_update_strategy update
;
1302 /* configuration parameters which are passed on to the children */
1305 int recommend_shallow
;
1306 struct string_list references
;
1308 const char *recursive_prefix
;
1311 /* Machine-readable status lines to be consumed by git-submodule.sh */
1312 struct string_list projectlines
;
1314 /* If we want to stop as fast as possible and return an error */
1315 unsigned quickstop
: 1;
1317 /* failed clones to be retried again */
1318 const struct cache_entry
**failed_clones
;
1319 int failed_clones_nr
, failed_clones_alloc
;
1321 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1322 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \
1324 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
1327 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1328 struct strbuf
*out
, const char *displaypath
)
1331 * Only mention uninitialized submodules when their
1332 * paths have been specified.
1334 if (suc
->warn_if_uninitialized
) {
1336 _("Submodule path '%s' not initialized"),
1338 strbuf_addch(out
, '\n');
1340 _("Maybe you want to use 'update --init'?"));
1341 strbuf_addch(out
, '\n');
1346 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1347 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1349 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1350 struct child_process
*child
,
1351 struct submodule_update_clone
*suc
,
1354 const struct submodule
*sub
= NULL
;
1355 const char *url
= NULL
;
1356 const char *update_string
;
1357 enum submodule_update_type update_type
;
1359 struct strbuf displaypath_sb
= STRBUF_INIT
;
1360 struct strbuf sb
= STRBUF_INIT
;
1361 const char *displaypath
= NULL
;
1362 int needs_cloning
= 0;
1365 if (suc
->recursive_prefix
)
1366 strbuf_addf(&sb
, "%s/%s", suc
->recursive_prefix
, ce
->name
);
1368 strbuf_addstr(&sb
, ce
->name
);
1369 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
1370 strbuf_addch(out
, '\n');
1374 sub
= submodule_from_path(the_repository
, &null_oid
, ce
->name
);
1376 if (suc
->recursive_prefix
)
1377 displaypath
= relative_path(suc
->recursive_prefix
,
1378 ce
->name
, &displaypath_sb
);
1380 displaypath
= ce
->name
;
1383 next_submodule_warn_missing(suc
, out
, displaypath
);
1387 key
= xstrfmt("submodule.%s.update", sub
->name
);
1388 if (!repo_config_get_string_const(the_repository
, key
, &update_string
)) {
1389 update_type
= parse_submodule_update_type(update_string
);
1391 update_type
= sub
->update_strategy
.type
;
1395 if (suc
->update
.type
== SM_UPDATE_NONE
1396 || (suc
->update
.type
== SM_UPDATE_UNSPECIFIED
1397 && update_type
== SM_UPDATE_NONE
)) {
1398 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
1399 strbuf_addch(out
, '\n');
1403 /* Check if the submodule has been initialized. */
1404 if (!is_submodule_active(the_repository
, ce
->name
)) {
1405 next_submodule_warn_missing(suc
, out
, displaypath
);
1410 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1411 if (repo_config_get_string_const(the_repository
, sb
.buf
, &url
))
1415 strbuf_addf(&sb
, "%s/.git", ce
->name
);
1416 needs_cloning
= !file_exists(sb
.buf
);
1419 strbuf_addf(&sb
, "%06o %s %d %d\t%s\n", ce
->ce_mode
,
1420 oid_to_hex(&ce
->oid
), ce_stage(ce
),
1421 needs_cloning
, ce
->name
);
1422 string_list_append(&suc
->projectlines
, sb
.buf
);
1428 child
->no_stdin
= 1;
1429 child
->stdout_to_stderr
= 1;
1431 argv_array_push(&child
->args
, "submodule--helper");
1432 argv_array_push(&child
->args
, "clone");
1434 argv_array_push(&child
->args
, "--progress");
1436 argv_array_push(&child
->args
, "--quiet");
1438 argv_array_pushl(&child
->args
, "--prefix", suc
->prefix
, NULL
);
1439 if (suc
->recommend_shallow
&& sub
->recommend_shallow
== 1)
1440 argv_array_push(&child
->args
, "--depth=1");
1441 argv_array_pushl(&child
->args
, "--path", sub
->path
, NULL
);
1442 argv_array_pushl(&child
->args
, "--name", sub
->name
, NULL
);
1443 argv_array_pushl(&child
->args
, "--url", url
, NULL
);
1444 if (suc
->references
.nr
) {
1445 struct string_list_item
*item
;
1446 for_each_string_list_item(item
, &suc
->references
)
1447 argv_array_pushl(&child
->args
, "--reference", item
->string
, NULL
);
1450 argv_array_push(&child
->args
, suc
->depth
);
1453 strbuf_reset(&displaypath_sb
);
1456 return needs_cloning
;
1459 static int update_clone_get_next_task(struct child_process
*child
,
1464 struct submodule_update_clone
*suc
= suc_cb
;
1465 const struct cache_entry
*ce
;
1468 for (; suc
->current
< suc
->list
.nr
; suc
->current
++) {
1469 ce
= suc
->list
.entries
[suc
->current
];
1470 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
1471 int *p
= xmalloc(sizeof(*p
));
1480 * The loop above tried cloning each submodule once, now try the
1481 * stragglers again, which we can imagine as an extension of the
1484 index
= suc
->current
- suc
->list
.nr
;
1485 if (index
< suc
->failed_clones_nr
) {
1487 ce
= suc
->failed_clones
[index
];
1488 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
1490 strbuf_addstr(err
, "BUG: submodule considered for "
1491 "cloning, doesn't need cloning "
1495 p
= xmalloc(sizeof(*p
));
1505 static int update_clone_start_failure(struct strbuf
*err
,
1509 struct submodule_update_clone
*suc
= suc_cb
;
1514 static int update_clone_task_finished(int result
,
1519 const struct cache_entry
*ce
;
1520 struct submodule_update_clone
*suc
= suc_cb
;
1522 int *idxP
= idx_task_cb
;
1529 if (idx
< suc
->list
.nr
) {
1530 ce
= suc
->list
.entries
[idx
];
1531 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
1533 strbuf_addch(err
, '\n');
1534 ALLOC_GROW(suc
->failed_clones
,
1535 suc
->failed_clones_nr
+ 1,
1536 suc
->failed_clones_alloc
);
1537 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
1540 idx
-= suc
->list
.nr
;
1541 ce
= suc
->failed_clones
[idx
];
1542 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
1544 strbuf_addch(err
, '\n');
1552 static int gitmodules_update_clone_config(const char *var
, const char *value
,
1556 if (!strcmp(var
, "submodule.fetchjobs"))
1557 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
1561 static int update_clone(int argc
, const char **argv
, const char *prefix
)
1563 const char *update
= NULL
;
1565 struct string_list_item
*item
;
1566 struct pathspec pathspec
;
1567 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
1569 struct option module_update_clone_options
[] = {
1570 OPT_STRING(0, "prefix", &prefix
,
1572 N_("path into the working tree")),
1573 OPT_STRING(0, "recursive-prefix", &suc
.recursive_prefix
,
1575 N_("path into the working tree, across nested "
1576 "submodule boundaries")),
1577 OPT_STRING(0, "update", &update
,
1579 N_("rebase, merge, checkout or none")),
1580 OPT_STRING_LIST(0, "reference", &suc
.references
, N_("repo"),
1581 N_("reference repository")),
1582 OPT_STRING(0, "depth", &suc
.depth
, "<depth>",
1583 N_("Create a shallow clone truncated to the "
1584 "specified number of revisions")),
1585 OPT_INTEGER('j', "jobs", &max_jobs
,
1586 N_("parallel jobs")),
1587 OPT_BOOL(0, "recommend-shallow", &suc
.recommend_shallow
,
1588 N_("whether the initial clone should follow the shallow recommendation")),
1589 OPT__QUIET(&suc
.quiet
, N_("don't print cloning progress")),
1590 OPT_BOOL(0, "progress", &suc
.progress
,
1591 N_("force cloning progress")),
1595 const char *const git_submodule_helper_usage
[] = {
1596 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1599 suc
.prefix
= prefix
;
1601 config_from_gitmodules(gitmodules_update_clone_config
, &max_jobs
);
1602 git_config(gitmodules_update_clone_config
, &max_jobs
);
1604 argc
= parse_options(argc
, argv
, prefix
, module_update_clone_options
,
1605 git_submodule_helper_usage
, 0);
1608 if (parse_submodule_update_strategy(update
, &suc
.update
) < 0)
1609 die(_("bad value for update parameter"));
1611 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &suc
.list
) < 0)
1615 suc
.warn_if_uninitialized
= 1;
1617 run_processes_parallel(max_jobs
,
1618 update_clone_get_next_task
,
1619 update_clone_start_failure
,
1620 update_clone_task_finished
,
1624 * We saved the output and put it out all at once now.
1626 * - the listener does not have to interleave their (checkout)
1627 * work with our fetching. The writes involved in a
1628 * checkout involve more straightforward sequential I/O.
1629 * - the listener can avoid doing any work if fetching failed.
1634 for_each_string_list_item(item
, &suc
.projectlines
)
1635 fprintf(stdout
, "%s", item
->string
);
1640 static int resolve_relative_path(int argc
, const char **argv
, const char *prefix
)
1642 struct strbuf sb
= STRBUF_INIT
;
1644 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc
);
1646 printf("%s", relative_path(argv
[1], argv
[2], &sb
));
1647 strbuf_release(&sb
);
1651 static const char *remote_submodule_branch(const char *path
)
1653 const struct submodule
*sub
;
1654 const char *branch
= NULL
;
1657 sub
= submodule_from_path(the_repository
, &null_oid
, path
);
1661 key
= xstrfmt("submodule.%s.branch", sub
->name
);
1662 if (repo_config_get_string_const(the_repository
, key
, &branch
))
1663 branch
= sub
->branch
;
1669 if (!strcmp(branch
, ".")) {
1670 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
1673 die(_("No such ref: %s"), "HEAD");
1676 if (!strcmp(refname
, "HEAD"))
1677 die(_("Submodule (%s) branch configured to inherit "
1678 "branch from superproject, but the superproject "
1679 "is not on any branch"), sub
->name
);
1681 if (!skip_prefix(refname
, "refs/heads/", &refname
))
1682 die(_("Expecting a full ref name, got %s"), refname
);
1689 static int resolve_remote_submodule_branch(int argc
, const char **argv
,
1693 struct strbuf sb
= STRBUF_INIT
;
1695 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc
);
1697 ret
= remote_submodule_branch(argv
[1]);
1699 die("submodule %s doesn't exist", argv
[1]);
1702 strbuf_release(&sb
);
1706 static int push_check(int argc
, const char **argv
, const char *prefix
)
1708 struct remote
*remote
;
1709 const char *superproject_head
;
1711 int detached_head
= 0;
1712 struct object_id head_oid
;
1715 die("submodule--helper push-check requires at least 2 arguments");
1718 * superproject's resolved head ref.
1719 * if HEAD then the superproject is in a detached head state, otherwise
1720 * it will be the resolved head ref.
1722 superproject_head
= argv
[1];
1725 /* Get the submodule's head ref and determine if it is detached */
1726 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1728 die(_("Failed to resolve HEAD as a valid ref."));
1729 if (!strcmp(head
, "HEAD"))
1733 * The remote must be configured.
1734 * This is to avoid pushing to the exact same URL as the parent.
1736 remote
= pushremote_get(argv
[1]);
1737 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
1738 die("remote '%s' not configured", argv
[1]);
1740 /* Check the refspec */
1742 int i
, refspec_nr
= argc
- 2;
1743 struct ref
*local_refs
= get_local_heads();
1744 struct refspec
*refspec
= parse_push_refspec(refspec_nr
,
1747 for (i
= 0; i
< refspec_nr
; i
++) {
1748 struct refspec
*rs
= refspec
+ i
;
1750 if (rs
->pattern
|| rs
->matching
)
1753 /* LHS must match a single ref */
1754 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
1759 * If LHS matches 'HEAD' then we need to ensure
1760 * that it matches the same named branch
1761 * checked out in the superproject.
1763 if (!strcmp(rs
->src
, "HEAD")) {
1764 if (!detached_head
&&
1765 !strcmp(head
, superproject_head
))
1767 die("HEAD does not match the named branch in the superproject");
1771 die("src refspec '%s' must name a ref",
1775 free_refspec(refspec_nr
, refspec
);
1782 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
1785 struct pathspec pathspec
;
1786 struct module_list list
= MODULE_LIST_INIT
;
1787 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
1789 struct option embed_gitdir_options
[] = {
1790 OPT_STRING(0, "prefix", &prefix
,
1792 N_("path into the working tree")),
1793 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
1794 ABSORB_GITDIR_RECURSE_SUBMODULES
),
1798 const char *const git_submodule_helper_usage
[] = {
1799 N_("git submodule--helper embed-git-dir [<path>...]"),
1803 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
1804 git_submodule_helper_usage
, 0);
1806 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1809 for (i
= 0; i
< list
.nr
; i
++)
1810 absorb_git_dir_into_superproject(prefix
,
1811 list
.entries
[i
]->name
, flags
);
1816 static int is_active(int argc
, const char **argv
, const char *prefix
)
1819 die("submodule--helper is-active takes exactly 1 argument");
1821 return !is_submodule_active(the_repository
, argv
[1]);
1824 #define SUPPORT_SUPER_PREFIX (1<<0)
1828 int (*fn
)(int, const char **, const char *);
1832 static struct cmd_struct commands
[] = {
1833 {"list", module_list
, 0},
1834 {"name", module_name
, 0},
1835 {"clone", module_clone
, 0},
1836 {"update-clone", update_clone
, 0},
1837 {"relative-path", resolve_relative_path
, 0},
1838 {"resolve-relative-url", resolve_relative_url
, 0},
1839 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
1840 {"init", module_init
, SUPPORT_SUPER_PREFIX
},
1841 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
1842 {"print-default-remote", print_default_remote
, 0},
1843 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
1844 {"deinit", module_deinit
, 0},
1845 {"remote-branch", resolve_remote_submodule_branch
, 0},
1846 {"push-check", push_check
, 0},
1847 {"absorb-git-dirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
1848 {"is-active", is_active
, 0},
1851 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
1854 if (argc
< 2 || !strcmp(argv
[1], "-h"))
1855 usage("git submodule--helper <command>");
1857 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
1858 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
1859 if (get_super_prefix() &&
1860 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
1861 die(_("%s doesn't support --super-prefix"),
1863 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
1867 die(_("'%s' is not a valid submodule--helper "
1868 "subcommand"), argv
[1]);