1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
3 #include "repository.h"
6 #include "parse-options.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
21 #include "object-store.h"
24 #include "list-objects-filter-options.h"
26 #define OPT_QUIET (1 << 0)
27 #define OPT_CACHED (1 << 1)
28 #define OPT_RECURSIVE (1 << 2)
29 #define OPT_FORCE (1 << 3)
31 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
34 static char *repo_get_default_remote(struct repository
*repo
)
36 char *dest
= NULL
, *ret
;
37 struct strbuf sb
= STRBUF_INIT
;
38 struct ref_store
*store
= get_main_ref_store(repo
);
39 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
43 die(_("No such ref: %s"), "HEAD");
46 if (!strcmp(refname
, "HEAD"))
47 return xstrdup("origin");
49 if (!skip_prefix(refname
, "refs/heads/", &refname
))
50 die(_("Expecting a full ref name, got %s"), refname
);
52 strbuf_addf(&sb
, "branch.%s.remote", refname
);
53 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
54 ret
= xstrdup("origin");
62 static char *get_default_remote_submodule(const char *module_path
)
64 struct repository subrepo
;
66 repo_submodule_init(&subrepo
, the_repository
, module_path
, null_oid());
67 return repo_get_default_remote(&subrepo
);
70 static char *get_default_remote(void)
72 return repo_get_default_remote(the_repository
);
75 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
77 char *remoteurl
, *resolved_url
;
78 char *remote
= get_default_remote();
79 struct strbuf remotesb
= STRBUF_INIT
;
81 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
82 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
84 warning(_("could not look up configuration '%s'. "
85 "Assuming this repository is its own "
86 "authoritative upstream."),
88 remoteurl
= xgetcwd();
90 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
94 strbuf_release(&remotesb
);
99 static int resolve_relative_url_test(int argc
, const char **argv
, const char *prefix
)
101 char *remoteurl
, *res
;
102 const char *up_path
, *url
;
105 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
108 remoteurl
= xstrdup(argv
[2]);
111 if (!strcmp(up_path
, "(null)"))
114 res
= relative_url(remoteurl
, url
, up_path
);
121 /* the result should be freed by the caller. */
122 static char *get_submodule_displaypath(const char *path
, const char *prefix
)
124 const char *super_prefix
= get_super_prefix();
126 if (prefix
&& super_prefix
) {
127 BUG("cannot have prefix '%s' and superprefix '%s'",
128 prefix
, super_prefix
);
130 struct strbuf sb
= STRBUF_INIT
;
131 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
134 } else if (super_prefix
) {
135 return xstrfmt("%s%s", super_prefix
, path
);
137 return xstrdup(path
);
141 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
143 struct strbuf sb
= STRBUF_INIT
;
146 static const char *describe_bare
[] = { NULL
};
148 static const char *describe_tags
[] = { "--tags", NULL
};
150 static const char *describe_contains
[] = { "--contains", NULL
};
152 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
154 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
156 describe_all_always
, NULL
};
158 for (d
= describe_argv
; *d
; d
++) {
159 struct child_process cp
= CHILD_PROCESS_INIT
;
160 prepare_submodule_repo_env(&cp
.env
);
165 strvec_push(&cp
.args
, "describe");
166 strvec_pushv(&cp
.args
, *d
);
167 strvec_push(&cp
.args
, object_id
);
169 if (!capture_command(&cp
, &sb
, 0)) {
170 strbuf_strip_suffix(&sb
, "\n");
171 return strbuf_detach(&sb
, NULL
);
180 const struct cache_entry
**entries
;
183 #define MODULE_LIST_INIT { 0 }
185 static int module_list_compute(int argc
, const char **argv
,
187 struct pathspec
*pathspec
,
188 struct module_list
*list
)
191 char *ps_matched
= NULL
;
192 parse_pathspec(pathspec
, 0,
193 PATHSPEC_PREFER_FULL
,
197 ps_matched
= xcalloc(pathspec
->nr
, 1);
199 if (read_cache() < 0)
200 die(_("index file corrupt"));
202 for (i
= 0; i
< active_nr
; i
++) {
203 const struct cache_entry
*ce
= active_cache
[i
];
205 if (!match_pathspec(&the_index
, pathspec
, ce
->name
, ce_namelen(ce
),
207 !S_ISGITLINK(ce
->ce_mode
))
210 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
211 list
->entries
[list
->nr
++] = ce
;
212 while (i
+ 1 < active_nr
&&
213 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
215 * Skip entries with the same name in different stages
216 * to make sure an entry is returned only once.
221 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
229 static void module_list_active(struct module_list
*list
)
232 struct module_list active_modules
= MODULE_LIST_INIT
;
234 for (i
= 0; i
< list
->nr
; i
++) {
235 const struct cache_entry
*ce
= list
->entries
[i
];
237 if (!is_submodule_active(the_repository
, ce
->name
))
240 ALLOC_GROW(active_modules
.entries
,
241 active_modules
.nr
+ 1,
242 active_modules
.alloc
);
243 active_modules
.entries
[active_modules
.nr
++] = ce
;
247 *list
= active_modules
;
250 static char *get_up_path(const char *path
)
253 struct strbuf sb
= STRBUF_INIT
;
255 for (i
= count_slashes(path
); i
; i
--)
256 strbuf_addstr(&sb
, "../");
259 * Check if 'path' ends with slash or not
260 * for having the same output for dir/sub_dir
263 if (!is_dir_sep(path
[strlen(path
) - 1]))
264 strbuf_addstr(&sb
, "../");
266 return strbuf_detach(&sb
, NULL
);
269 static int module_list(int argc
, const char **argv
, const char *prefix
)
272 struct pathspec pathspec
;
273 struct module_list list
= MODULE_LIST_INIT
;
275 struct option module_list_options
[] = {
276 OPT_STRING(0, "prefix", &prefix
,
278 N_("alternative anchor for relative paths")),
282 const char *const git_submodule_helper_usage
[] = {
283 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
287 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
288 git_submodule_helper_usage
, 0);
290 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
293 for (i
= 0; i
< list
.nr
; i
++) {
294 const struct cache_entry
*ce
= list
.entries
[i
];
297 printf("%06o %s U\t", ce
->ce_mode
,
298 oid_to_hex(null_oid()));
300 printf("%06o %s %d\t", ce
->ce_mode
,
301 oid_to_hex(&ce
->oid
), ce_stage(ce
));
303 fprintf(stdout
, "%s\n", ce
->name
);
308 static void for_each_listed_submodule(const struct module_list
*list
,
309 each_submodule_fn fn
, void *cb_data
)
312 for (i
= 0; i
< list
->nr
; i
++)
313 fn(list
->entries
[i
], cb_data
);
323 #define FOREACH_CB_INIT { 0 }
325 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
328 struct foreach_cb
*info
= cb_data
;
329 const char *path
= list_item
->name
;
330 const struct object_id
*ce_oid
= &list_item
->oid
;
332 const struct submodule
*sub
;
333 struct child_process cp
= CHILD_PROCESS_INIT
;
336 displaypath
= get_submodule_displaypath(path
, info
->prefix
);
338 sub
= submodule_from_path(the_repository
, null_oid(), path
);
341 die(_("No url found for submodule path '%s' in .gitmodules"),
344 if (!is_submodule_populated_gently(path
, NULL
))
347 prepare_submodule_repo_env(&cp
.env
);
350 * For the purpose of executing <command> in the submodule,
351 * separate shell is used for the purpose of running the
358 * NEEDSWORK: the command currently has access to the variables $name,
359 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
360 * contains a single argument. This is done for maintaining a faithful
361 * translation from shell script.
363 if (info
->argc
== 1) {
364 char *toplevel
= xgetcwd();
365 struct strbuf sb
= STRBUF_INIT
;
367 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
368 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
369 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
370 strvec_pushf(&cp
.env
, "sha1=%s",
372 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
375 * Since the path variable was accessible from the script
376 * before porting, it is also made available after porting.
377 * The environment variable "PATH" has a very special purpose
378 * on windows. And since environment variables are
379 * case-insensitive in windows, it interferes with the
380 * existing PATH variable. Hence, to avoid that, we expose
381 * path via the args strvec and not via env.
383 sq_quote_buf(&sb
, path
);
384 strvec_pushf(&cp
.args
, "path=%s; %s",
385 sb
.buf
, info
->argv
[0]);
389 strvec_pushv(&cp
.args
, info
->argv
);
393 printf(_("Entering '%s'\n"), displaypath
);
395 if (info
->argv
[0] && run_command(&cp
))
396 die(_("run_command returned non-zero status for %s\n."),
399 if (info
->recursive
) {
400 struct child_process cpr
= CHILD_PROCESS_INIT
;
404 prepare_submodule_repo_env(&cpr
.env
);
406 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
407 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
408 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
412 strvec_push(&cpr
.args
, "--quiet");
414 strvec_push(&cpr
.args
, "--");
415 strvec_pushv(&cpr
.args
, info
->argv
);
417 if (run_command(&cpr
))
418 die(_("run_command returned non-zero status while "
419 "recursing in the nested submodules of %s\n."),
427 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
429 struct foreach_cb info
= FOREACH_CB_INIT
;
430 struct pathspec pathspec
;
431 struct module_list list
= MODULE_LIST_INIT
;
433 struct option module_foreach_options
[] = {
434 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
435 OPT_BOOL(0, "recursive", &info
.recursive
,
436 N_("recurse into nested submodules")),
440 const char *const git_submodule_helper_usage
[] = {
441 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
445 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
446 git_submodule_helper_usage
, 0);
448 if (module_list_compute(0, NULL
, prefix
, &pathspec
, &list
) < 0)
453 info
.prefix
= prefix
;
455 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
460 static int starts_with_dot_slash(const char *const path
)
462 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
463 PATH_MATCH_XPLATFORM
);
466 static int starts_with_dot_dot_slash(const char *const path
)
468 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
469 PATH_MATCH_XPLATFORM
);
476 #define INIT_CB_INIT { 0 }
478 static void init_submodule(const char *path
, const char *prefix
,
481 const struct submodule
*sub
;
482 struct strbuf sb
= STRBUF_INIT
;
483 char *upd
= NULL
, *url
= NULL
, *displaypath
;
485 displaypath
= get_submodule_displaypath(path
, prefix
);
487 sub
= submodule_from_path(the_repository
, null_oid(), path
);
490 die(_("No url found for submodule path '%s' in .gitmodules"),
494 * NEEDSWORK: In a multi-working-tree world, this needs to be
495 * set in the per-worktree config.
497 * Set active flag for the submodule being initialized
499 if (!is_submodule_active(the_repository
, path
)) {
500 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
501 git_config_set_gently(sb
.buf
, "true");
506 * Copy url setting when it is not set yet.
507 * To look up the url in .git/config, we must not fall back to
508 * .gitmodules, so look it up directly.
510 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
511 if (git_config_get_string(sb
.buf
, &url
)) {
513 die(_("No url found for submodule path '%s' in .gitmodules"),
516 url
= xstrdup(sub
->url
);
518 /* Possibly a url relative to parent */
519 if (starts_with_dot_dot_slash(url
) ||
520 starts_with_dot_slash(url
)) {
522 url
= resolve_relative_url(oldurl
, NULL
, 0);
526 if (git_config_set_gently(sb
.buf
, url
))
527 die(_("Failed to register url for submodule path '%s'"),
529 if (!(flags
& OPT_QUIET
))
531 _("Submodule '%s' (%s) registered for path '%s'\n"),
532 sub
->name
, url
, displaypath
);
536 /* Copy "update" setting when it is not set yet */
537 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
538 if (git_config_get_string(sb
.buf
, &upd
) &&
539 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
540 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
541 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
543 upd
= xstrdup("none");
545 upd
= xstrdup(submodule_strategy_to_string(&sub
->update_strategy
));
547 if (git_config_set_gently(sb
.buf
, upd
))
548 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
556 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
558 struct init_cb
*info
= cb_data
;
559 init_submodule(list_item
->name
, info
->prefix
, info
->flags
);
562 static int module_init(int argc
, const char **argv
, const char *prefix
)
564 struct init_cb info
= INIT_CB_INIT
;
565 struct pathspec pathspec
;
566 struct module_list list
= MODULE_LIST_INIT
;
569 struct option module_init_options
[] = {
570 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
574 const char *const git_submodule_helper_usage
[] = {
575 N_("git submodule init [<options>] [<path>]"),
579 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
580 git_submodule_helper_usage
, 0);
582 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
586 * If there are no path args and submodule.active is set then,
587 * by default, only initialize 'active' modules.
589 if (!argc
&& git_config_get_value_multi("submodule.active"))
590 module_list_active(&list
);
592 info
.prefix
= prefix
;
594 info
.flags
|= OPT_QUIET
;
596 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
605 #define STATUS_CB_INIT { 0 }
607 static void print_status(unsigned int flags
, char state
, const char *path
,
608 const struct object_id
*oid
, const char *displaypath
)
610 if (flags
& OPT_QUIET
)
613 printf("%c%s %s", state
, oid_to_hex(oid
), displaypath
);
615 if (state
== ' ' || state
== '+') {
616 const char *name
= compute_rev_name(path
, oid_to_hex(oid
));
619 printf(" (%s)", name
);
625 static int handle_submodule_head_ref(const char *refname
,
626 const struct object_id
*oid
, int flags
,
629 struct object_id
*output
= cb_data
;
636 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
637 unsigned int ce_flags
, const char *prefix
,
641 struct strvec diff_files_args
= STRVEC_INIT
;
642 struct rev_info rev
= REV_INFO_INIT
;
643 int diff_files_result
;
644 struct strbuf buf
= STRBUF_INIT
;
647 if (!submodule_from_path(the_repository
, null_oid(), path
))
648 die(_("no submodule mapping found in .gitmodules for path '%s'"),
651 displaypath
= get_submodule_displaypath(path
, prefix
);
653 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
654 print_status(flags
, 'U', path
, null_oid(), displaypath
);
658 strbuf_addf(&buf
, "%s/.git", path
);
659 git_dir
= read_gitfile(buf
.buf
);
663 if (!is_submodule_active(the_repository
, path
) ||
664 !is_git_directory(git_dir
)) {
665 print_status(flags
, '-', path
, ce_oid
, displaypath
);
666 strbuf_release(&buf
);
669 strbuf_release(&buf
);
671 strvec_pushl(&diff_files_args
, "diff-files",
672 "--ignore-submodules=dirty", "--quiet", "--",
675 git_config(git_diff_basic_config
, NULL
);
677 repo_init_revisions(the_repository
, &rev
, NULL
);
679 diff_files_args
.nr
= setup_revisions(diff_files_args
.nr
,
682 diff_files_result
= run_diff_files(&rev
, 0);
684 if (!diff_result_code(&rev
.diffopt
, diff_files_result
)) {
685 print_status(flags
, ' ', path
, ce_oid
,
687 } else if (!(flags
& OPT_CACHED
)) {
688 struct object_id oid
;
689 struct ref_store
*refs
= get_submodule_ref_store(path
);
692 print_status(flags
, '-', path
, ce_oid
, displaypath
);
695 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
696 die(_("could not resolve HEAD ref inside the "
697 "submodule '%s'"), path
);
699 print_status(flags
, '+', path
, &oid
, displaypath
);
701 print_status(flags
, '+', path
, ce_oid
, displaypath
);
704 if (flags
& OPT_RECURSIVE
) {
705 struct child_process cpr
= CHILD_PROCESS_INIT
;
709 prepare_submodule_repo_env(&cpr
.env
);
711 strvec_push(&cpr
.args
, "--super-prefix");
712 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
713 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
714 "--recursive", NULL
);
716 if (flags
& OPT_CACHED
)
717 strvec_push(&cpr
.args
, "--cached");
719 if (flags
& OPT_QUIET
)
720 strvec_push(&cpr
.args
, "--quiet");
722 if (run_command(&cpr
))
723 die(_("failed to recurse into submodule '%s'"), path
);
727 strvec_clear(&diff_files_args
);
729 release_revisions(&rev
);
732 static void status_submodule_cb(const struct cache_entry
*list_item
,
735 struct status_cb
*info
= cb_data
;
736 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
737 info
->prefix
, info
->flags
);
740 static int module_status(int argc
, const char **argv
, const char *prefix
)
742 struct status_cb info
= STATUS_CB_INIT
;
743 struct pathspec pathspec
;
744 struct module_list list
= MODULE_LIST_INIT
;
747 struct option module_status_options
[] = {
748 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
749 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
750 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
754 const char *const git_submodule_helper_usage
[] = {
755 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
759 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
760 git_submodule_helper_usage
, 0);
762 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
765 info
.prefix
= prefix
;
767 info
.flags
|= OPT_QUIET
;
769 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
774 static int module_name(int argc
, const char **argv
, const char *prefix
)
776 const struct submodule
*sub
;
779 usage(_("git submodule--helper name <path>"));
781 sub
= submodule_from_path(the_repository
, null_oid(), argv
[1]);
784 die(_("no submodule mapping found in .gitmodules for path '%s'"),
787 printf("%s\n", sub
->name
);
793 unsigned int mod_src
;
794 unsigned int mod_dst
;
795 struct object_id oid_src
;
796 struct object_id oid_dst
;
800 #define MODULE_CB_INIT { 0 }
802 struct module_cb_list
{
803 struct module_cb
**entries
;
806 #define MODULE_CB_LIST_INIT { 0 }
812 unsigned int cached
: 1;
813 unsigned int for_status
: 1;
814 unsigned int files
: 1;
817 #define SUMMARY_CB_INIT { 0 }
824 static char *verify_submodule_committish(const char *sm_path
,
825 const char *committish
)
827 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
828 struct strbuf result
= STRBUF_INIT
;
830 cp_rev_parse
.git_cmd
= 1;
831 cp_rev_parse
.dir
= sm_path
;
832 prepare_submodule_repo_env(&cp_rev_parse
.env
);
833 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
834 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
835 strvec_push(&cp_rev_parse
.args
, "--");
837 if (capture_command(&cp_rev_parse
, &result
, 0))
840 strbuf_trim_trailing_newline(&result
);
841 return strbuf_detach(&result
, NULL
);
844 static void print_submodule_summary(struct summary_cb
*info
, char *errmsg
,
845 int total_commits
, const char *displaypath
,
846 const char *src_abbrev
, const char *dst_abbrev
,
849 if (p
->status
== 'T') {
850 if (S_ISGITLINK(p
->mod_dst
))
851 printf(_("* %s %s(blob)->%s(submodule)"),
852 displaypath
, src_abbrev
, dst_abbrev
);
854 printf(_("* %s %s(submodule)->%s(blob)"),
855 displaypath
, src_abbrev
, dst_abbrev
);
857 printf("* %s %s...%s",
858 displaypath
, src_abbrev
, dst_abbrev
);
861 if (total_commits
< 0)
864 printf(" (%d):\n", total_commits
);
867 printf(_("%s"), errmsg
);
868 } else if (total_commits
> 0) {
869 struct child_process cp_log
= CHILD_PROCESS_INIT
;
872 cp_log
.dir
= p
->sm_path
;
873 prepare_submodule_repo_env(&cp_log
.env
);
874 strvec_pushl(&cp_log
.args
, "log", NULL
);
876 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
877 if (info
->summary_limit
> 0)
878 strvec_pushf(&cp_log
.args
, "-%d",
879 info
->summary_limit
);
881 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
882 "--first-parent", NULL
);
883 strvec_pushf(&cp_log
.args
, "%s...%s",
884 src_abbrev
, dst_abbrev
);
885 } else if (S_ISGITLINK(p
->mod_dst
)) {
886 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
887 "-1", dst_abbrev
, NULL
);
889 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
890 "-1", src_abbrev
, NULL
);
892 run_command(&cp_log
);
897 static void generate_submodule_summary(struct summary_cb
*info
,
900 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
901 int missing_src
= 0, missing_dst
= 0;
903 int total_commits
= -1;
905 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
906 if (S_ISGITLINK(p
->mod_dst
)) {
907 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
909 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
910 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
912 int fd
= open(p
->sm_path
, O_RDONLY
);
914 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
915 index_fd(&the_index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
917 error(_("couldn't hash object from '%s'"), p
->sm_path
);
919 /* for a submodule removal (mode:0000000), don't warn */
921 warning(_("unexpected mode %o\n"), p
->mod_dst
);
925 if (S_ISGITLINK(p
->mod_src
)) {
926 if (p
->status
!= 'D')
927 src_abbrev
= verify_submodule_committish(p
->sm_path
,
928 oid_to_hex(&p
->oid_src
));
932 * As `rev-parse` failed, we fallback to getting
933 * the abbreviated hash using oid_src. We do
934 * this as we might still need the abbreviated
935 * hash in cases like a submodule type change, etc.
937 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
941 * The source does not point to a submodule.
942 * So, we fallback to getting the abbreviation using
943 * oid_src as we might still need the abbreviated
944 * hash in cases like submodule add, etc.
946 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
949 if (S_ISGITLINK(p
->mod_dst
)) {
950 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
951 oid_to_hex(&p
->oid_dst
));
955 * As `rev-parse` failed, we fallback to getting
956 * the abbreviated hash using oid_dst. We do
957 * this as we might still need the abbreviated
958 * hash in cases like a submodule type change, etc.
960 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
964 * The destination does not point to a submodule.
965 * So, we fallback to getting the abbreviation using
966 * oid_dst as we might still need the abbreviated
967 * hash in cases like a submodule removal, etc.
969 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
972 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
);
974 if (!missing_src
&& !missing_dst
) {
975 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
976 struct strbuf sb_rev_list
= STRBUF_INIT
;
978 strvec_pushl(&cp_rev_list
.args
, "rev-list",
979 "--first-parent", "--count", NULL
);
980 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
981 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
982 src_abbrev
, dst_abbrev
);
984 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
985 src_abbrev
: dst_abbrev
);
986 strvec_push(&cp_rev_list
.args
, "--");
988 cp_rev_list
.git_cmd
= 1;
989 cp_rev_list
.dir
= p
->sm_path
;
990 prepare_submodule_repo_env(&cp_rev_list
.env
);
992 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
993 total_commits
= atoi(sb_rev_list
.buf
);
995 strbuf_release(&sb_rev_list
);
998 * Don't give error msg for modification whose dst is not
999 * submodule, i.e., deleted or changed to blob
1001 if (S_ISGITLINK(p
->mod_dst
)) {
1002 struct strbuf errmsg_str
= STRBUF_INIT
;
1003 if (missing_src
&& missing_dst
) {
1004 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commits %s and %s\n",
1005 displaypath
, oid_to_hex(&p
->oid_src
),
1006 oid_to_hex(&p
->oid_dst
));
1008 strbuf_addf(&errmsg_str
, " Warn: %s doesn't contain commit %s\n",
1009 displaypath
, missing_src
?
1010 oid_to_hex(&p
->oid_src
) :
1011 oid_to_hex(&p
->oid_dst
));
1013 errmsg
= strbuf_detach(&errmsg_str
, NULL
);
1017 print_submodule_summary(info
, errmsg
, total_commits
,
1018 displaypath
, src_abbrev
,
1026 static void prepare_submodule_summary(struct summary_cb
*info
,
1027 struct module_cb_list
*list
)
1030 for (i
= 0; i
< list
->nr
; i
++) {
1031 const struct submodule
*sub
;
1032 struct module_cb
*p
= list
->entries
[i
];
1033 struct strbuf sm_gitdir
= STRBUF_INIT
;
1035 if (p
->status
== 'D' || p
->status
== 'T') {
1036 generate_submodule_summary(info
, p
);
1040 if (info
->for_status
&& p
->status
!= 'A' &&
1041 (sub
= submodule_from_path(the_repository
,
1042 null_oid(), p
->sm_path
))) {
1043 char *config_key
= NULL
;
1047 config_key
= xstrfmt("submodule.%s.ignore",
1049 if (!git_config_get_string_tmp(config_key
, &value
))
1050 ignore_all
= !strcmp(value
, "all");
1051 else if (sub
->ignore
)
1052 ignore_all
= !strcmp(sub
->ignore
, "all");
1059 /* Also show added or modified modules which are checked out */
1060 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1061 if (is_nonbare_repository_dir(&sm_gitdir
))
1062 generate_submodule_summary(info
, p
);
1063 strbuf_release(&sm_gitdir
);
1067 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1068 struct diff_options
*options
,
1072 struct module_cb_list
*list
= data
;
1073 for (i
= 0; i
< q
->nr
; i
++) {
1074 struct diff_filepair
*p
= q
->queue
[i
];
1075 struct module_cb
*temp
;
1077 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1079 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1080 temp
->mod_src
= p
->one
->mode
;
1081 temp
->mod_dst
= p
->two
->mode
;
1082 temp
->oid_src
= p
->one
->oid
;
1083 temp
->oid_dst
= p
->two
->oid
;
1084 temp
->status
= p
->status
;
1085 temp
->sm_path
= xstrdup(p
->one
->path
);
1087 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1088 list
->entries
[list
->nr
++] = temp
;
1092 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1095 case DIFF_INDEX
: return "diff-index";
1096 case DIFF_FILES
: return "diff-files";
1097 default: BUG("bad diff_cmd value %d", diff_cmd
);
1101 static int compute_summary_module_list(struct object_id
*head_oid
,
1102 struct summary_cb
*info
,
1103 enum diff_cmd diff_cmd
)
1105 struct strvec diff_args
= STRVEC_INIT
;
1106 struct rev_info rev
;
1107 struct setup_revision_opt opt
= {
1108 .free_removed_argv_elements
= 1,
1110 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1113 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1115 strvec_push(&diff_args
, "--cached");
1116 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1118 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1119 strvec_push(&diff_args
, "--");
1121 strvec_pushv(&diff_args
, info
->argv
);
1123 git_config(git_diff_basic_config
, NULL
);
1124 init_revisions(&rev
, info
->prefix
);
1126 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1127 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1128 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1129 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1130 rev
.diffopt
.format_callback_data
= &list
;
1132 if (!info
->cached
) {
1133 if (diff_cmd
== DIFF_INDEX
)
1135 if (read_cache_preload(&rev
.diffopt
.pathspec
) < 0) {
1136 perror("read_cache_preload");
1140 } else if (read_cache() < 0) {
1141 perror("read_cache");
1146 if (diff_cmd
== DIFF_INDEX
)
1147 run_diff_index(&rev
, info
->cached
);
1149 run_diff_files(&rev
, 0);
1150 prepare_submodule_summary(info
, &list
);
1152 strvec_clear(&diff_args
);
1153 release_revisions(&rev
);
1157 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1159 struct summary_cb info
= SUMMARY_CB_INIT
;
1163 int summary_limit
= -1;
1164 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1165 struct object_id head_oid
;
1168 struct option module_summary_options
[] = {
1169 OPT_BOOL(0, "cached", &cached
,
1170 N_("use the commit stored in the index instead of the submodule HEAD")),
1171 OPT_BOOL(0, "files", &files
,
1172 N_("compare the commit in the index with that in the submodule HEAD")),
1173 OPT_BOOL(0, "for-status", &for_status
,
1174 N_("skip submodules with 'ignore_config' value set to 'all'")),
1175 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1176 N_("limit the summary size")),
1180 const char *const git_submodule_helper_usage
[] = {
1181 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1185 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1186 git_submodule_helper_usage
, 0);
1191 if (!get_oid(argc
? argv
[0] : "HEAD", &head_oid
)) {
1196 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1197 /* before the first commit: compare with an empty tree */
1198 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1204 if (get_oid("HEAD", &head_oid
))
1205 die(_("could not fetch a revision for HEAD"));
1210 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1211 diff_cmd
= DIFF_FILES
;
1216 info
.prefix
= prefix
;
1217 info
.cached
= !!cached
;
1218 info
.files
= !!files
;
1219 info
.for_status
= !!for_status
;
1220 info
.summary_limit
= summary_limit
;
1222 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1231 #define SYNC_CB_INIT { 0 }
1233 static void sync_submodule(const char *path
, const char *prefix
,
1236 const struct submodule
*sub
;
1237 char *remote_key
= NULL
;
1238 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1239 struct strbuf sb
= STRBUF_INIT
;
1240 char *sub_config_path
= NULL
;
1242 if (!is_submodule_active(the_repository
, path
))
1245 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1247 if (sub
&& sub
->url
) {
1248 if (starts_with_dot_dot_slash(sub
->url
) ||
1249 starts_with_dot_slash(sub
->url
)) {
1250 char *up_path
= get_up_path(path
);
1251 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1252 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1255 sub_origin_url
= xstrdup(sub
->url
);
1256 super_config_url
= xstrdup(sub
->url
);
1259 sub_origin_url
= xstrdup("");
1260 super_config_url
= xstrdup("");
1263 displaypath
= get_submodule_displaypath(path
, prefix
);
1265 if (!(flags
& OPT_QUIET
))
1266 printf(_("Synchronizing submodule url for '%s'\n"),
1270 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1271 if (git_config_set_gently(sb
.buf
, super_config_url
))
1272 die(_("failed to register url for submodule path '%s'"),
1275 if (!is_submodule_populated_gently(path
, NULL
))
1279 default_remote
= get_default_remote_submodule(path
);
1280 if (!default_remote
)
1281 die(_("failed to get the default remote for submodule '%s'"),
1284 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1285 free(default_remote
);
1287 submodule_to_gitdir(&sb
, path
);
1288 strbuf_addstr(&sb
, "/config");
1290 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, sub_origin_url
))
1291 die(_("failed to update remote for submodule '%s'"),
1294 if (flags
& OPT_RECURSIVE
) {
1295 struct child_process cpr
= CHILD_PROCESS_INIT
;
1299 prepare_submodule_repo_env(&cpr
.env
);
1301 strvec_push(&cpr
.args
, "--super-prefix");
1302 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1303 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1304 "--recursive", NULL
);
1306 if (flags
& OPT_QUIET
)
1307 strvec_push(&cpr
.args
, "--quiet");
1309 if (run_command(&cpr
))
1310 die(_("failed to recurse into submodule '%s'"),
1315 free(super_config_url
);
1316 free(sub_origin_url
);
1317 strbuf_release(&sb
);
1320 free(sub_config_path
);
1323 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1325 struct sync_cb
*info
= cb_data
;
1326 sync_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1329 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1331 struct sync_cb info
= SYNC_CB_INIT
;
1332 struct pathspec pathspec
;
1333 struct module_list list
= MODULE_LIST_INIT
;
1337 struct option module_sync_options
[] = {
1338 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1339 OPT_BOOL(0, "recursive", &recursive
,
1340 N_("recurse into nested submodules")),
1344 const char *const git_submodule_helper_usage
[] = {
1345 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1349 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1350 git_submodule_helper_usage
, 0);
1352 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1355 info
.prefix
= prefix
;
1357 info
.flags
|= OPT_QUIET
;
1359 info
.flags
|= OPT_RECURSIVE
;
1361 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1370 #define DEINIT_CB_INIT { 0 }
1372 static void deinit_submodule(const char *path
, const char *prefix
,
1375 const struct submodule
*sub
;
1376 char *displaypath
= NULL
;
1377 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1378 struct strbuf sb_config
= STRBUF_INIT
;
1379 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1381 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1383 if (!sub
|| !sub
->name
)
1386 displaypath
= get_submodule_displaypath(path
, prefix
);
1388 /* remove the submodule work tree (unless the user already did it) */
1389 if (is_directory(path
)) {
1390 struct strbuf sb_rm
= STRBUF_INIT
;
1393 if (is_directory(sub_git_dir
)) {
1394 if (!(flags
& OPT_QUIET
))
1395 warning(_("Submodule work tree '%s' contains a .git "
1396 "directory. This will be replaced with a "
1397 ".git file by using absorbgitdirs."),
1400 absorb_git_dir_into_superproject(path
,
1401 ABSORB_GITDIR_RECURSE_SUBMODULES
);
1405 if (!(flags
& OPT_FORCE
)) {
1406 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1408 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1411 if (run_command(&cp_rm
))
1412 die(_("Submodule work tree '%s' contains local "
1413 "modifications; use '-f' to discard them"),
1417 strbuf_addstr(&sb_rm
, path
);
1419 if (!remove_dir_recursively(&sb_rm
, 0))
1420 format
= _("Cleared directory '%s'\n");
1422 format
= _("Could not remove submodule work tree '%s'\n");
1424 if (!(flags
& OPT_QUIET
))
1425 printf(format
, displaypath
);
1427 submodule_unset_core_worktree(sub
);
1429 strbuf_release(&sb_rm
);
1432 if (mkdir(path
, 0777))
1433 printf(_("could not create empty submodule directory %s"),
1436 cp_config
.git_cmd
= 1;
1437 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1438 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1440 /* remove the .git/config entries (unless the user already did it) */
1441 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1442 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1444 * remove the whole section so we have a clean state when
1445 * the user later decides to init this submodule again
1447 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1448 if (!(flags
& OPT_QUIET
))
1449 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1450 sub
->name
, sub
->url
, displaypath
);
1457 strbuf_release(&sb_config
);
1460 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1463 struct deinit_cb
*info
= cb_data
;
1464 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1467 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1469 struct deinit_cb info
= DEINIT_CB_INIT
;
1470 struct pathspec pathspec
;
1471 struct module_list list
= MODULE_LIST_INIT
;
1476 struct option module_deinit_options
[] = {
1477 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1478 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1479 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1483 const char *const git_submodule_helper_usage
[] = {
1484 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1488 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1489 git_submodule_helper_usage
, 0);
1492 error("pathspec and --all are incompatible");
1493 usage_with_options(git_submodule_helper_usage
,
1494 module_deinit_options
);
1498 die(_("Use '--all' if you really want to deinitialize all submodules"));
1500 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
1503 info
.prefix
= prefix
;
1505 info
.flags
|= OPT_QUIET
;
1507 info
.flags
|= OPT_FORCE
;
1509 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1514 struct module_clone_data
{
1520 struct list_objects_filter_options
*filter_options
;
1521 struct string_list reference
;
1522 unsigned int quiet
: 1;
1523 unsigned int progress
: 1;
1524 unsigned int dissociate
: 1;
1525 unsigned int require_init
: 1;
1528 #define MODULE_CLONE_DATA_INIT { \
1529 .reference = STRING_LIST_INIT_NODUP, \
1530 .single_branch = -1, \
1533 struct submodule_alternate_setup
{
1534 const char *submodule_name
;
1535 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1536 SUBMODULE_ALTERNATE_ERROR_DIE
,
1537 SUBMODULE_ALTERNATE_ERROR_INFO
,
1538 SUBMODULE_ALTERNATE_ERROR_IGNORE
1540 struct string_list
*reference
;
1542 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1543 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1546 static const char alternate_error_advice
[] = N_(
1547 "An alternate computed from a superproject's alternate is invalid.\n"
1548 "To allow Git to clone without an alternate in such a case, set\n"
1549 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1550 "'--reference-if-able' instead of '--reference'."
1553 static int add_possible_reference_from_superproject(
1554 struct object_directory
*odb
, void *sas_cb
)
1556 struct submodule_alternate_setup
*sas
= sas_cb
;
1560 * If the alternate object store is another repository, try the
1561 * standard layout with .git/(modules/<name>)+/objects
1563 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1564 struct repository alternate
;
1566 struct strbuf sb
= STRBUF_INIT
;
1567 struct strbuf err
= STRBUF_INIT
;
1568 strbuf_add(&sb
, odb
->path
, len
);
1570 repo_init(&alternate
, sb
.buf
, NULL
);
1573 * We need to end the new path with '/' to mark it as a dir,
1574 * otherwise a submodule name containing '/' will be broken
1575 * as the last part of a missing submodule reference would
1576 * be taken as a file name.
1579 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1580 strbuf_addch(&sb
, '/');
1581 repo_clear(&alternate
);
1583 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1585 string_list_append(sas
->reference
, xstrdup(sb
.buf
));
1588 switch (sas
->error_mode
) {
1589 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1590 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1591 advise(_(alternate_error_advice
));
1592 die(_("submodule '%s' cannot add alternate: %s"),
1593 sas
->submodule_name
, err
.buf
);
1594 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1595 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1596 sas
->submodule_name
, err
.buf
);
1597 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1601 strbuf_release(&sb
);
1607 static void prepare_possible_alternates(const char *sm_name
,
1608 struct string_list
*reference
)
1610 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1611 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1613 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1617 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1619 if (!error_strategy
)
1620 error_strategy
= xstrdup("die");
1622 sas
.submodule_name
= sm_name
;
1623 sas
.reference
= reference
;
1624 if (!strcmp(error_strategy
, "die"))
1625 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1626 else if (!strcmp(error_strategy
, "info"))
1627 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1628 else if (!strcmp(error_strategy
, "ignore"))
1629 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1631 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1633 if (!strcmp(sm_alternate
, "superproject"))
1634 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1635 else if (!strcmp(sm_alternate
, "no"))
1638 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1641 free(error_strategy
);
1644 static int clone_submodule(struct module_clone_data
*clone_data
)
1646 char *p
, *sm_gitdir
;
1647 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1648 struct strbuf sb
= STRBUF_INIT
;
1649 struct child_process cp
= CHILD_PROCESS_INIT
;
1651 submodule_name_to_gitdir(&sb
, the_repository
, clone_data
->name
);
1652 sm_gitdir
= absolute_pathdup(sb
.buf
);
1655 if (!is_absolute_path(clone_data
->path
)) {
1656 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), clone_data
->path
);
1657 clone_data
->path
= strbuf_detach(&sb
, NULL
);
1659 clone_data
->path
= xstrdup(clone_data
->path
);
1662 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1663 die(_("refusing to create/use '%s' in another submodule's "
1664 "git dir"), sm_gitdir
);
1666 if (!file_exists(sm_gitdir
)) {
1667 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1668 die(_("could not create directory '%s'"), sm_gitdir
);
1670 prepare_possible_alternates(clone_data
->name
, &clone_data
->reference
);
1672 strvec_push(&cp
.args
, "clone");
1673 strvec_push(&cp
.args
, "--no-checkout");
1674 if (clone_data
->quiet
)
1675 strvec_push(&cp
.args
, "--quiet");
1676 if (clone_data
->progress
)
1677 strvec_push(&cp
.args
, "--progress");
1678 if (clone_data
->depth
&& *(clone_data
->depth
))
1679 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1680 if (clone_data
->reference
.nr
) {
1681 struct string_list_item
*item
;
1682 for_each_string_list_item(item
, &clone_data
->reference
)
1683 strvec_pushl(&cp
.args
, "--reference",
1684 item
->string
, NULL
);
1686 if (clone_data
->dissociate
)
1687 strvec_push(&cp
.args
, "--dissociate");
1688 if (sm_gitdir
&& *sm_gitdir
)
1689 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1690 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1691 strvec_pushf(&cp
.args
, "--filter=%s",
1692 expand_list_objects_filter_spec(
1693 clone_data
->filter_options
));
1694 if (clone_data
->single_branch
>= 0)
1695 strvec_push(&cp
.args
, clone_data
->single_branch
?
1697 "--no-single-branch");
1699 strvec_push(&cp
.args
, "--");
1700 strvec_push(&cp
.args
, clone_data
->url
);
1701 strvec_push(&cp
.args
, clone_data
->path
);
1704 prepare_submodule_repo_env(&cp
.env
);
1707 if(run_command(&cp
))
1708 die(_("clone of '%s' into submodule path '%s' failed"),
1709 clone_data
->url
, clone_data
->path
);
1711 if (clone_data
->require_init
&& !access(clone_data
->path
, X_OK
) &&
1712 !is_empty_dir(clone_data
->path
))
1713 die(_("directory not empty: '%s'"), clone_data
->path
);
1714 if (safe_create_leading_directories_const(clone_data
->path
) < 0)
1715 die(_("could not create directory '%s'"), clone_data
->path
);
1716 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
1717 unlink_or_warn(sb
.buf
);
1721 connect_work_tree_and_git_dir(clone_data
->path
, sm_gitdir
, 0);
1723 p
= git_pathdup_submodule(clone_data
->path
, "config");
1725 die(_("could not get submodule directory for '%s'"), clone_data
->path
);
1727 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1728 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1730 git_config_set_in_file(p
, "submodule.alternateLocation",
1732 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1734 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1738 free(error_strategy
);
1740 strbuf_release(&sb
);
1746 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1748 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1749 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1750 struct list_objects_filter_options filter_options
;
1752 struct option module_clone_options
[] = {
1753 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1755 N_("alternative anchor for relative paths")),
1756 OPT_STRING(0, "path", &clone_data
.path
,
1758 N_("where the new submodule will be cloned to")),
1759 OPT_STRING(0, "name", &clone_data
.name
,
1761 N_("name of the new submodule")),
1762 OPT_STRING(0, "url", &clone_data
.url
,
1764 N_("url where to clone the submodule from")),
1765 OPT_STRING_LIST(0, "reference", &clone_data
.reference
,
1767 N_("reference repository")),
1768 OPT_BOOL(0, "dissociate", &dissociate
,
1769 N_("use --reference only while cloning")),
1770 OPT_STRING(0, "depth", &clone_data
.depth
,
1772 N_("depth for shallow clones")),
1773 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1774 OPT_BOOL(0, "progress", &progress
,
1775 N_("force cloning progress")),
1776 OPT_BOOL(0, "require-init", &require_init
,
1777 N_("disallow cloning into non-empty directory")),
1778 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1779 N_("clone only one branch, HEAD or --branch")),
1780 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1784 const char *const git_submodule_helper_usage
[] = {
1785 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1786 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1787 "[--single-branch] [--filter <filter-spec>] "
1788 "--url <url> --path <path>"),
1792 memset(&filter_options
, 0, sizeof(filter_options
));
1793 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1794 git_submodule_helper_usage
, 0);
1796 clone_data
.dissociate
= !!dissociate
;
1797 clone_data
.quiet
= !!quiet
;
1798 clone_data
.progress
= !!progress
;
1799 clone_data
.require_init
= !!require_init
;
1800 clone_data
.filter_options
= &filter_options
;
1802 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1803 usage_with_options(git_submodule_helper_usage
,
1804 module_clone_options
);
1806 clone_submodule(&clone_data
);
1807 list_objects_filter_release(&filter_options
);
1811 static void determine_submodule_update_strategy(struct repository
*r
,
1814 enum submodule_update_type update
,
1815 struct submodule_update_strategy
*out
)
1817 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1821 key
= xstrfmt("submodule.%s.update", sub
->name
);
1825 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1826 if (parse_submodule_update_strategy(val
, out
) < 0)
1827 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1829 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1830 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1831 BUG("how did we read update = !command from .gitmodules?");
1832 out
->type
= sub
->update_strategy
.type
;
1833 out
->command
= sub
->update_strategy
.command
;
1835 out
->type
= SM_UPDATE_CHECKOUT
;
1838 (out
->type
== SM_UPDATE_MERGE
||
1839 out
->type
== SM_UPDATE_REBASE
||
1840 out
->type
== SM_UPDATE_NONE
))
1841 out
->type
= SM_UPDATE_CHECKOUT
;
1846 struct update_clone_data
{
1847 const struct submodule
*sub
;
1848 struct object_id oid
;
1849 unsigned just_cloned
;
1852 struct submodule_update_clone
{
1853 /* index into 'update_data.list', the list of submodules to look into for cloning */
1856 /* configuration parameters which are passed on to the children */
1857 struct update_data
*update_data
;
1859 /* to be consumed by update_submodule() */
1860 struct update_clone_data
*update_clone
;
1861 int update_clone_nr
; int update_clone_alloc
;
1863 /* If we want to stop as fast as possible and return an error */
1864 unsigned quickstop
: 1;
1866 /* failed clones to be retried again */
1867 const struct cache_entry
**failed_clones
;
1868 int failed_clones_nr
, failed_clones_alloc
;
1870 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1872 struct update_data
{
1874 const char *displaypath
;
1875 enum submodule_update_type update_default
;
1876 struct object_id suboid
;
1877 struct string_list references
;
1878 struct submodule_update_strategy update_strategy
;
1879 struct list_objects_filter_options
*filter_options
;
1880 struct module_list list
;
1884 int recommend_shallow
;
1885 unsigned int require_init
;
1888 unsigned int nofetch
;
1889 unsigned int remote
;
1890 unsigned int progress
;
1891 unsigned int dissociate
;
1893 unsigned int warn_if_uninitialized
;
1894 unsigned int recursive
;
1896 /* copied over from update_clone_data */
1897 struct object_id oid
;
1898 unsigned int just_cloned
;
1899 const char *sm_path
;
1901 #define UPDATE_DATA_INIT { \
1902 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1903 .list = MODULE_LIST_INIT, \
1904 .recommend_shallow = -1, \
1905 .references = STRING_LIST_INIT_DUP, \
1906 .single_branch = -1, \
1910 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1911 struct strbuf
*out
, const char *displaypath
)
1914 * Only mention uninitialized submodules when their
1915 * paths have been specified.
1917 if (suc
->update_data
->warn_if_uninitialized
) {
1919 _("Submodule path '%s' not initialized"),
1921 strbuf_addch(out
, '\n');
1923 _("Maybe you want to use 'update --init'?"));
1924 strbuf_addch(out
, '\n');
1929 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1930 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1932 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1933 struct child_process
*child
,
1934 struct submodule_update_clone
*suc
,
1937 const struct submodule
*sub
= NULL
;
1938 const char *url
= NULL
;
1939 const char *update_string
;
1940 enum submodule_update_type update_type
;
1942 struct update_data
*ud
= suc
->update_data
;
1943 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
);
1944 struct strbuf sb
= STRBUF_INIT
;
1945 int needs_cloning
= 0;
1946 int need_free_url
= 0;
1949 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
1950 strbuf_addch(out
, '\n');
1954 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
1957 next_submodule_warn_missing(suc
, out
, displaypath
);
1961 key
= xstrfmt("submodule.%s.update", sub
->name
);
1962 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
1963 update_type
= parse_submodule_update_type(update_string
);
1965 update_type
= sub
->update_strategy
.type
;
1969 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
1970 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
1971 && update_type
== SM_UPDATE_NONE
)) {
1972 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
1973 strbuf_addch(out
, '\n');
1977 /* Check if the submodule has been initialized. */
1978 if (!is_submodule_active(the_repository
, ce
->name
)) {
1979 next_submodule_warn_missing(suc
, out
, displaypath
);
1984 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1985 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
1986 if (starts_with_dot_slash(sub
->url
) ||
1987 starts_with_dot_dot_slash(sub
->url
)) {
1988 url
= resolve_relative_url(sub
->url
, NULL
, 0);
1995 strbuf_addf(&sb
, "%s/.git", ce
->name
);
1996 needs_cloning
= !file_exists(sb
.buf
);
1998 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
1999 suc
->update_clone_alloc
);
2000 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2001 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2002 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2003 suc
->update_clone_nr
++;
2009 child
->no_stdin
= 1;
2010 child
->stdout_to_stderr
= 1;
2012 strvec_push(&child
->args
, "submodule--helper");
2013 strvec_push(&child
->args
, "clone");
2014 if (suc
->update_data
->progress
)
2015 strvec_push(&child
->args
, "--progress");
2016 if (suc
->update_data
->quiet
)
2017 strvec_push(&child
->args
, "--quiet");
2018 if (suc
->update_data
->prefix
)
2019 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2020 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2021 strvec_push(&child
->args
, "--depth=1");
2022 else if (suc
->update_data
->depth
)
2023 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2024 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2025 strvec_pushf(&child
->args
, "--filter=%s",
2026 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2027 if (suc
->update_data
->require_init
)
2028 strvec_push(&child
->args
, "--require-init");
2029 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2030 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2031 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2032 if (suc
->update_data
->references
.nr
) {
2033 struct string_list_item
*item
;
2034 for_each_string_list_item(item
, &suc
->update_data
->references
)
2035 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2037 if (suc
->update_data
->dissociate
)
2038 strvec_push(&child
->args
, "--dissociate");
2039 if (suc
->update_data
->single_branch
>= 0)
2040 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2042 "--no-single-branch");
2046 strbuf_release(&sb
);
2050 return needs_cloning
;
2053 static int update_clone_get_next_task(struct child_process
*child
,
2058 struct submodule_update_clone
*suc
= suc_cb
;
2059 const struct cache_entry
*ce
;
2062 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2063 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2064 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2065 int *p
= xmalloc(sizeof(*p
));
2074 * The loop above tried cloning each submodule once, now try the
2075 * stragglers again, which we can imagine as an extension of the
2078 index
= suc
->current
- suc
->update_data
->list
.nr
;
2079 if (index
< suc
->failed_clones_nr
) {
2081 ce
= suc
->failed_clones
[index
];
2082 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2084 strbuf_addstr(err
, "BUG: submodule considered for "
2085 "cloning, doesn't need cloning "
2089 p
= xmalloc(sizeof(*p
));
2099 static int update_clone_start_failure(struct strbuf
*err
,
2103 struct submodule_update_clone
*suc
= suc_cb
;
2108 static int update_clone_task_finished(int result
,
2113 const struct cache_entry
*ce
;
2114 struct submodule_update_clone
*suc
= suc_cb
;
2116 int *idxP
= idx_task_cb
;
2123 if (idx
< suc
->update_data
->list
.nr
) {
2124 ce
= suc
->update_data
->list
.entries
[idx
];
2125 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2127 strbuf_addch(err
, '\n');
2128 ALLOC_GROW(suc
->failed_clones
,
2129 suc
->failed_clones_nr
+ 1,
2130 suc
->failed_clones_alloc
);
2131 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2134 idx
-= suc
->update_data
->list
.nr
;
2135 ce
= suc
->failed_clones
[idx
];
2136 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2138 strbuf_addch(err
, '\n');
2146 static int git_update_clone_config(const char *var
, const char *value
,
2150 if (!strcmp(var
, "submodule.fetchjobs"))
2151 *max_jobs
= parse_submodule_fetchjobs(var
, value
);
2155 static int is_tip_reachable(const char *path
, struct object_id
*oid
)
2157 struct child_process cp
= CHILD_PROCESS_INIT
;
2158 struct strbuf rev
= STRBUF_INIT
;
2159 char *hex
= oid_to_hex(oid
);
2162 cp
.dir
= xstrdup(path
);
2164 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2166 prepare_submodule_repo_env(&cp
.env
);
2168 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2174 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
, struct object_id
*oid
)
2176 struct child_process cp
= CHILD_PROCESS_INIT
;
2178 prepare_submodule_repo_env(&cp
.env
);
2180 cp
.dir
= xstrdup(module_path
);
2182 strvec_push(&cp
.args
, "fetch");
2184 strvec_push(&cp
.args
, "--quiet");
2186 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2188 char *hex
= oid_to_hex(oid
);
2189 char *remote
= get_default_remote();
2190 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2194 return run_command(&cp
);
2197 static int run_update_command(struct update_data
*ud
, int subforce
)
2199 struct child_process cp
= CHILD_PROCESS_INIT
;
2200 char *oid
= oid_to_hex(&ud
->oid
);
2201 int must_die_on_failure
= 0;
2203 switch (ud
->update_strategy
.type
) {
2204 case SM_UPDATE_CHECKOUT
:
2206 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2208 strvec_push(&cp
.args
, "-f");
2210 case SM_UPDATE_REBASE
:
2212 strvec_push(&cp
.args
, "rebase");
2214 strvec_push(&cp
.args
, "--quiet");
2215 must_die_on_failure
= 1;
2217 case SM_UPDATE_MERGE
:
2219 strvec_push(&cp
.args
, "merge");
2221 strvec_push(&cp
.args
, "--quiet");
2222 must_die_on_failure
= 1;
2224 case SM_UPDATE_COMMAND
:
2226 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2227 must_die_on_failure
= 1;
2230 BUG("unexpected update strategy type: %s",
2231 submodule_strategy_to_string(&ud
->update_strategy
));
2233 strvec_push(&cp
.args
, oid
);
2235 cp
.dir
= xstrdup(ud
->sm_path
);
2236 prepare_submodule_repo_env(&cp
.env
);
2237 if (run_command(&cp
)) {
2238 switch (ud
->update_strategy
.type
) {
2239 case SM_UPDATE_CHECKOUT
:
2240 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2241 oid
, ud
->displaypath
);
2243 case SM_UPDATE_REBASE
:
2244 die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2245 oid
, ud
->displaypath
);
2247 case SM_UPDATE_MERGE
:
2248 die_message(_("Unable to merge '%s' in submodule path '%s'"),
2249 oid
, ud
->displaypath
);
2251 case SM_UPDATE_COMMAND
:
2252 die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2253 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2256 BUG("unexpected update strategy type: %s",
2257 submodule_strategy_to_string(&ud
->update_strategy
));
2259 if (must_die_on_failure
)
2262 /* the command failed, but update must continue */
2269 switch (ud
->update_strategy
.type
) {
2270 case SM_UPDATE_CHECKOUT
:
2271 printf(_("Submodule path '%s': checked out '%s'\n"),
2272 ud
->displaypath
, oid
);
2274 case SM_UPDATE_REBASE
:
2275 printf(_("Submodule path '%s': rebased into '%s'\n"),
2276 ud
->displaypath
, oid
);
2278 case SM_UPDATE_MERGE
:
2279 printf(_("Submodule path '%s': merged in '%s'\n"),
2280 ud
->displaypath
, oid
);
2282 case SM_UPDATE_COMMAND
:
2283 printf(_("Submodule path '%s': '%s %s'\n"),
2284 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2287 BUG("unexpected update strategy type: %s",
2288 submodule_strategy_to_string(&ud
->update_strategy
));
2294 static int run_update_procedure(struct update_data
*ud
)
2296 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2300 * Run fetch only if `oid` isn't present or it
2301 * is not reachable from a ref.
2303 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2304 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2307 _("Unable to fetch in submodule path '%s'; "
2308 "trying to directly fetch %s:"),
2309 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2311 * Now we tried the usual fetch, but `oid` may
2312 * not be reachable from any of the refs.
2314 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2315 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2316 die(_("Fetched in submodule path '%s', but it did not "
2317 "contain %s. Direct fetching of that commit failed."),
2318 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2321 return run_update_command(ud
, subforce
);
2324 static const char *remote_submodule_branch(const char *path
)
2326 const struct submodule
*sub
;
2327 const char *branch
= NULL
;
2330 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2334 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2335 if (repo_config_get_string_tmp(the_repository
, key
, &branch
))
2336 branch
= sub
->branch
;
2342 if (!strcmp(branch
, ".")) {
2343 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2346 die(_("No such ref: %s"), "HEAD");
2349 if (!strcmp(refname
, "HEAD"))
2350 die(_("Submodule (%s) branch configured to inherit "
2351 "branch from superproject, but the superproject "
2352 "is not on any branch"), sub
->name
);
2354 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2355 die(_("Expecting a full ref name, got %s"), refname
);
2362 static void ensure_core_worktree(const char *path
)
2365 struct repository subrepo
;
2367 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2368 die(_("could not get a repository handle for submodule '%s'"), path
);
2370 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2371 char *cfg_file
, *abs_path
;
2372 const char *rel_path
;
2373 struct strbuf sb
= STRBUF_INIT
;
2375 cfg_file
= repo_git_path(&subrepo
, "config");
2377 abs_path
= absolute_pathdup(path
);
2378 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2380 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2384 strbuf_release(&sb
);
2388 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2391 case SM_UPDATE_CHECKOUT
:
2393 case SM_UPDATE_MERGE
:
2395 case SM_UPDATE_REBASE
:
2397 case SM_UPDATE_UNSPECIFIED
:
2398 case SM_UPDATE_NONE
:
2399 case SM_UPDATE_COMMAND
:
2402 BUG("unreachable with type %d", type
);
2405 static void update_data_to_args(struct update_data
*update_data
, struct strvec
*args
)
2407 enum submodule_update_type update_type
= update_data
->update_default
;
2409 if (update_data
->displaypath
) {
2410 strvec_push(args
, "--super-prefix");
2411 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2413 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2414 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2415 if (update_data
->quiet
)
2416 strvec_push(args
, "--quiet");
2417 if (update_data
->force
)
2418 strvec_push(args
, "--force");
2419 if (update_data
->init
)
2420 strvec_push(args
, "--init");
2421 if (update_data
->remote
)
2422 strvec_push(args
, "--remote");
2423 if (update_data
->nofetch
)
2424 strvec_push(args
, "--no-fetch");
2425 if (update_data
->dissociate
)
2426 strvec_push(args
, "--dissociate");
2427 if (update_data
->progress
)
2428 strvec_push(args
, "--progress");
2429 if (update_data
->require_init
)
2430 strvec_push(args
, "--require-init");
2431 if (update_data
->depth
)
2432 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2433 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2434 strvec_pushf(args
, "--%s",
2435 submodule_update_type_to_label(update_type
));
2437 if (update_data
->references
.nr
) {
2438 struct string_list_item
*item
;
2439 for_each_string_list_item(item
, &update_data
->references
)
2440 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2442 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2443 strvec_pushf(args
, "--filter=%s",
2444 expand_list_objects_filter_spec(
2445 update_data
->filter_options
));
2446 if (update_data
->recommend_shallow
== 0)
2447 strvec_push(args
, "--no-recommend-shallow");
2448 else if (update_data
->recommend_shallow
== 1)
2449 strvec_push(args
, "--recommend-shallow");
2450 if (update_data
->single_branch
>= 0)
2451 strvec_push(args
, update_data
->single_branch
?
2453 "--no-single-branch");
2456 static int update_submodule(struct update_data
*update_data
)
2458 ensure_core_worktree(update_data
->sm_path
);
2460 update_data
->displaypath
= get_submodule_displaypath(
2461 update_data
->sm_path
, update_data
->prefix
);
2463 determine_submodule_update_strategy(the_repository
, update_data
->just_cloned
,
2464 update_data
->sm_path
, update_data
->update_default
,
2465 &update_data
->update_strategy
);
2467 if (update_data
->just_cloned
)
2468 oidcpy(&update_data
->suboid
, null_oid());
2469 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2470 die(_("Unable to find current revision in submodule path '%s'"),
2471 update_data
->displaypath
);
2473 if (update_data
->remote
) {
2474 char *remote_name
= get_default_remote_submodule(update_data
->sm_path
);
2475 const char *branch
= remote_submodule_branch(update_data
->sm_path
);
2476 char *remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2478 if (!update_data
->nofetch
) {
2479 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2481 die(_("Unable to fetch in submodule path '%s'"),
2482 update_data
->sm_path
);
2485 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2486 die(_("Unable to find %s revision in submodule path '%s'"),
2487 remote_ref
, update_data
->sm_path
);
2492 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
)
2493 if (run_update_procedure(update_data
))
2496 if (update_data
->recursive
) {
2497 struct child_process cp
= CHILD_PROCESS_INIT
;
2498 struct update_data next
= *update_data
;
2502 oidcpy(&next
.oid
, null_oid());
2503 oidcpy(&next
.suboid
, null_oid());
2505 cp
.dir
= update_data
->sm_path
;
2507 prepare_submodule_repo_env(&cp
.env
);
2508 update_data_to_args(&next
, &cp
.args
);
2510 /* die() if child process die()'d */
2511 res
= run_command(&cp
);
2514 die_message(_("Failed to recurse into submodule path '%s'"),
2515 update_data
->displaypath
);
2525 static int update_submodules(struct update_data
*update_data
)
2528 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2530 suc
.update_data
= update_data
;
2531 run_processes_parallel_tr2(suc
.update_data
->max_jobs
, update_clone_get_next_task
,
2532 update_clone_start_failure
,
2533 update_clone_task_finished
, &suc
, "submodule",
2537 * We saved the output and put it out all at once now.
2539 * - the listener does not have to interleave their (checkout)
2540 * work with our fetching. The writes involved in a
2541 * checkout involve more straightforward sequential I/O.
2542 * - the listener can avoid doing any work if fetching failed.
2544 if (suc
.quickstop
) {
2549 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2550 struct update_clone_data ucd
= suc
.update_clone
[i
];
2552 oidcpy(&update_data
->oid
, &ucd
.oid
);
2553 update_data
->just_cloned
= ucd
.just_cloned
;
2554 update_data
->sm_path
= ucd
.sub
->path
;
2556 if (update_submodule(update_data
))
2561 string_list_clear(&update_data
->references
, 0);
2565 static int module_update(int argc
, const char **argv
, const char *prefix
)
2567 struct pathspec pathspec
;
2568 struct update_data opt
= UPDATE_DATA_INIT
;
2569 struct list_objects_filter_options filter_options
;
2572 struct option module_update_options
[] = {
2573 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2574 OPT_BOOL(0, "init", &opt
.init
,
2575 N_("initialize uninitialized submodules before update")),
2576 OPT_BOOL(0, "remote", &opt
.remote
,
2577 N_("use SHA-1 of submodule's remote tracking branch")),
2578 OPT_BOOL(0, "recursive", &opt
.recursive
,
2579 N_("traverse submodules recursively")),
2580 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2581 N_("don't fetch new objects from the remote site")),
2582 OPT_STRING(0, "prefix", &opt
.prefix
,
2584 N_("path into the working tree")),
2585 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2586 N_("use the 'checkout' update strategy (default)"),
2587 SM_UPDATE_CHECKOUT
),
2588 OPT_SET_INT('m', "merge", &opt
.update_default
,
2589 N_("use the 'merge' update strategy"),
2591 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2592 N_("use the 'rebase' update strategy"),
2594 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2595 N_("reference repository")),
2596 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2597 N_("use --reference only while cloning")),
2598 OPT_INTEGER(0, "depth", &opt
.depth
,
2599 N_("create a shallow clone truncated to the "
2600 "specified number of revisions")),
2601 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2602 N_("parallel jobs")),
2603 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2604 N_("whether the initial clone should follow the shallow recommendation")),
2605 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2606 OPT_BOOL(0, "progress", &opt
.progress
,
2607 N_("force cloning progress")),
2608 OPT_BOOL(0, "require-init", &opt
.require_init
,
2609 N_("disallow cloning into non-empty directory, implies --init")),
2610 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2611 N_("clone only one branch, HEAD or --branch")),
2612 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2616 const char *const git_submodule_helper_usage
[] = {
2617 N_("git submodule [--quiet] update"
2618 " [--init [--filter=<filter-spec>]] [--remote]"
2619 " [-N|--no-fetch] [-f|--force]"
2620 " [--checkout|--merge|--rebase]"
2621 " [--[no-]recommend-shallow] [--reference <repository>]"
2622 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2626 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2627 git_config(git_update_clone_config
, &opt
.max_jobs
);
2629 memset(&filter_options
, 0, sizeof(filter_options
));
2630 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2631 git_submodule_helper_usage
, 0);
2633 if (opt
.require_init
)
2636 if (filter_options
.choice
&& !opt
.init
) {
2637 usage_with_options(git_submodule_helper_usage
,
2638 module_update_options
);
2641 opt
.filter_options
= &filter_options
;
2643 if (opt
.update_default
)
2644 opt
.update_strategy
.type
= opt
.update_default
;
2646 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2647 list_objects_filter_release(&filter_options
);
2652 opt
.warn_if_uninitialized
= 1;
2655 struct module_list list
= MODULE_LIST_INIT
;
2656 struct init_cb info
= INIT_CB_INIT
;
2658 if (module_list_compute(argc
, argv
, opt
.prefix
,
2659 &pathspec
, &list
) < 0)
2663 * If there are no path args and submodule.active is set then,
2664 * by default, only initialize 'active' modules.
2666 if (!argc
&& git_config_get_value_multi("submodule.active"))
2667 module_list_active(&list
);
2669 info
.prefix
= opt
.prefix
;
2671 info
.flags
|= OPT_QUIET
;
2673 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2676 ret
= update_submodules(&opt
);
2677 list_objects_filter_release(&filter_options
);
2681 static int push_check(int argc
, const char **argv
, const char *prefix
)
2683 struct remote
*remote
;
2684 const char *superproject_head
;
2686 int detached_head
= 0;
2687 struct object_id head_oid
;
2690 die("submodule--helper push-check requires at least 2 arguments");
2693 * superproject's resolved head ref.
2694 * if HEAD then the superproject is in a detached head state, otherwise
2695 * it will be the resolved head ref.
2697 superproject_head
= argv
[1];
2700 /* Get the submodule's head ref and determine if it is detached */
2701 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2703 die(_("Failed to resolve HEAD as a valid ref."));
2704 if (!strcmp(head
, "HEAD"))
2708 * The remote must be configured.
2709 * This is to avoid pushing to the exact same URL as the parent.
2711 remote
= pushremote_get(argv
[1]);
2712 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2713 die("remote '%s' not configured", argv
[1]);
2715 /* Check the refspec */
2718 struct ref
*local_refs
= get_local_heads();
2719 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2721 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2723 for (i
= 0; i
< refspec
.nr
; i
++) {
2724 const struct refspec_item
*rs
= &refspec
.items
[i
];
2726 if (rs
->pattern
|| rs
->matching
)
2729 /* LHS must match a single ref */
2730 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2735 * If LHS matches 'HEAD' then we need to ensure
2736 * that it matches the same named branch
2737 * checked out in the superproject.
2739 if (!strcmp(rs
->src
, "HEAD")) {
2740 if (!detached_head
&&
2741 !strcmp(head
, superproject_head
))
2743 die("HEAD does not match the named branch in the superproject");
2747 die("src refspec '%s' must name a ref",
2751 refspec_clear(&refspec
);
2758 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2761 struct pathspec pathspec
;
2762 struct module_list list
= MODULE_LIST_INIT
;
2763 unsigned flags
= ABSORB_GITDIR_RECURSE_SUBMODULES
;
2765 struct option embed_gitdir_options
[] = {
2766 OPT_STRING(0, "prefix", &prefix
,
2768 N_("path into the working tree")),
2769 OPT_BIT(0, "--recursive", &flags
, N_("recurse into submodules"),
2770 ABSORB_GITDIR_RECURSE_SUBMODULES
),
2774 const char *const git_submodule_helper_usage
[] = {
2775 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2779 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2780 git_submodule_helper_usage
, 0);
2782 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0)
2785 for (i
= 0; i
< list
.nr
; i
++)
2786 absorb_git_dir_into_superproject(list
.entries
[i
]->name
, flags
);
2791 static int is_active(int argc
, const char **argv
, const char *prefix
)
2794 die("submodule--helper is-active takes exactly 1 argument");
2796 return !is_submodule_active(the_repository
, argv
[1]);
2800 * Exit non-zero if any of the submodule names given on the command line is
2801 * invalid. If no names are given, filter stdin to print only valid names
2802 * (which is primarily intended for testing).
2804 static int check_name(int argc
, const char **argv
, const char *prefix
)
2808 if (check_submodule_name(*argv
) < 0)
2812 struct strbuf buf
= STRBUF_INIT
;
2813 while (strbuf_getline(&buf
, stdin
) != EOF
) {
2814 if (!check_submodule_name(buf
.buf
))
2815 printf("%s\n", buf
.buf
);
2817 strbuf_release(&buf
);
2822 static int module_config(int argc
, const char **argv
, const char *prefix
)
2825 CHECK_WRITEABLE
= 1,
2829 struct option module_config_options
[] = {
2830 OPT_CMDMODE(0, "check-writeable", &command
,
2831 N_("check if it is safe to write to the .gitmodules file"),
2833 OPT_CMDMODE(0, "unset", &command
,
2834 N_("unset the config in the .gitmodules file"),
2838 const char *const git_submodule_helper_usage
[] = {
2839 N_("git submodule--helper config <name> [<value>]"),
2840 N_("git submodule--helper config --unset <name>"),
2841 "git submodule--helper config --check-writeable",
2845 argc
= parse_options(argc
, argv
, prefix
, module_config_options
,
2846 git_submodule_helper_usage
, PARSE_OPT_KEEP_ARGV0
);
2848 if (argc
== 1 && command
== CHECK_WRITEABLE
)
2849 return is_writing_gitmodules_ok() ? 0 : -1;
2851 /* Equivalent to ACTION_GET in builtin/config.c */
2852 if (argc
== 2 && command
!= DO_UNSET
)
2853 return print_config_from_gitmodules(the_repository
, argv
[1]);
2855 /* Equivalent to ACTION_SET in builtin/config.c */
2856 if (argc
== 3 || (argc
== 2 && command
== DO_UNSET
)) {
2857 const char *value
= (argc
== 3) ? argv
[2] : NULL
;
2859 if (!is_writing_gitmodules_ok())
2860 die(_("please make sure that the .gitmodules file is in the working tree"));
2862 return config_set_in_gitmodules_file_gently(argv
[1], value
);
2865 usage_with_options(git_submodule_helper_usage
, module_config_options
);
2868 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2875 struct option options
[] = {
2876 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2879 const char *const usage
[] = {
2880 N_("git submodule set-url [--quiet] <path> <newurl>"),
2884 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2886 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2887 usage_with_options(usage
, options
);
2889 config_name
= xstrfmt("submodule.%s.url", path
);
2891 config_set_in_gitmodules_file_gently(config_name
, newurl
);
2892 sync_submodule(path
, prefix
, quiet
? OPT_QUIET
: 0);
2899 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2901 int opt_default
= 0, ret
;
2902 const char *opt_branch
= NULL
;
2907 * We accept the `quiet` option for uniformity across subcommands,
2908 * though there is nothing to make less verbose in this subcommand.
2910 struct option options
[] = {
2911 OPT_NOOP_NOARG('q', "quiet"),
2912 OPT_BOOL('d', "default", &opt_default
,
2913 N_("set the default tracking branch to master")),
2914 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2915 N_("set the default tracking branch")),
2918 const char *const usage
[] = {
2919 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2920 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2924 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2926 if (!opt_branch
&& !opt_default
)
2927 die(_("--branch or --default required"));
2929 if (opt_branch
&& opt_default
)
2930 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2932 if (argc
!= 1 || !(path
= argv
[0]))
2933 usage_with_options(usage
, options
);
2935 config_name
= xstrfmt("submodule.%s.branch", path
);
2936 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2942 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
2944 enum branch_track track
;
2945 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
2947 struct option options
[] = {
2948 OPT__QUIET(&quiet
, N_("print only error messages")),
2949 OPT__FORCE(&force
, N_("force creation"), 0),
2950 OPT_BOOL(0, "create-reflog", &reflog
,
2951 N_("create the branch's reflog")),
2952 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
2953 N_("set branch tracking configuration"),
2955 parse_opt_tracking_mode
),
2956 OPT__DRY_RUN(&dry_run
,
2957 N_("show whether the branch would be created")),
2960 const char *const usage
[] = {
2961 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2965 git_config(git_default_config
, NULL
);
2966 track
= git_branch_track
;
2967 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2970 usage_with_options(usage
, options
);
2972 if (!quiet
&& !dry_run
)
2973 printf_ln(_("creating branch '%s'"), argv
[0]);
2975 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
2976 force
, reflog
, quiet
, track
, dry_run
);
2983 const char *reference_path
;
2985 const char *sm_name
;
2987 const char *realrepo
;
2989 unsigned int force
: 1;
2990 unsigned int quiet
: 1;
2991 unsigned int progress
: 1;
2992 unsigned int dissociate
: 1;
2994 #define ADD_DATA_INIT { .depth = -1 }
2996 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
2998 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
2999 struct strbuf sb_remote_out
= STRBUF_INIT
;
3001 cp_remote
.git_cmd
= 1;
3002 strvec_pushf(&cp_remote
.env
,
3003 "GIT_DIR=%s", git_dir_path
);
3004 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3005 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3006 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3008 char *line
= sb_remote_out
.buf
;
3009 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3010 size_t len
= next_line
- line
;
3011 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3012 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3013 line
= next_line
+ 1;
3017 strbuf_release(&sb_remote_out
);
3020 static int add_submodule(const struct add_data
*add_data
)
3022 char *submod_gitdir_path
;
3023 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3025 /* perhaps the path already exists and is already a git repo, else clone it */
3026 if (is_directory(add_data
->sm_path
)) {
3027 struct strbuf sm_path
= STRBUF_INIT
;
3028 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3029 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3030 if (is_nonbare_repository_dir(&sm_path
))
3031 printf(_("Adding existing repo at '%s' to the index\n"),
3034 die(_("'%s' already exists and is not a valid git repo"),
3036 strbuf_release(&sm_path
);
3037 free(submod_gitdir_path
);
3039 struct child_process cp
= CHILD_PROCESS_INIT
;
3040 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3042 if (is_directory(submod_gitdir_path
)) {
3043 if (!add_data
->force
) {
3044 struct strbuf msg
= STRBUF_INIT
;
3047 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3048 "locally with remote(s):\n"),
3051 append_fetch_remotes(&msg
, submod_gitdir_path
);
3052 free(submod_gitdir_path
);
3054 strbuf_addf(&msg
, _("If you want to reuse this local git "
3055 "directory instead of cloning again from\n"
3057 "use the '--force' option. If the local git "
3058 "directory is not the correct repo\n"
3059 "or you are unsure what this means choose "
3060 "another name with the '--name' option."),
3061 add_data
->realrepo
);
3063 die_msg
= strbuf_detach(&msg
, NULL
);
3066 printf(_("Reactivating local git directory for "
3067 "submodule '%s'\n"), add_data
->sm_name
);
3070 free(submod_gitdir_path
);
3072 clone_data
.prefix
= add_data
->prefix
;
3073 clone_data
.path
= add_data
->sm_path
;
3074 clone_data
.name
= add_data
->sm_name
;
3075 clone_data
.url
= add_data
->realrepo
;
3076 clone_data
.quiet
= add_data
->quiet
;
3077 clone_data
.progress
= add_data
->progress
;
3078 if (add_data
->reference_path
)
3079 string_list_append(&clone_data
.reference
,
3080 xstrdup(add_data
->reference_path
));
3081 clone_data
.dissociate
= add_data
->dissociate
;
3082 if (add_data
->depth
>= 0)
3083 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3085 if (clone_submodule(&clone_data
))
3088 prepare_submodule_repo_env(&cp
.env
);
3090 cp
.dir
= add_data
->sm_path
;
3092 * NOTE: we only get here if add_data->force is true, so
3093 * passing --force to checkout is reasonable.
3095 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3097 if (add_data
->branch
) {
3098 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3099 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3102 if (run_command(&cp
))
3103 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3108 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3113 if (!is_writing_gitmodules_ok())
3114 die(_("please make sure that the .gitmodules file is in the working tree"));
3116 key
= xstrfmt("submodule.%s.%s", name
, var
);
3117 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3123 static void configure_added_submodule(struct add_data
*add_data
)
3127 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3128 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3130 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3131 git_config_set_gently(key
, add_data
->realrepo
);
3134 add_submod
.git_cmd
= 1;
3135 strvec_pushl(&add_submod
.args
, "add",
3136 "--no-warn-embedded-repo", NULL
);
3137 if (add_data
->force
)
3138 strvec_push(&add_submod
.args
, "--force");
3139 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3141 if (run_command(&add_submod
))
3142 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3144 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3145 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3146 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3148 if (add_data
->branch
) {
3149 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3150 "branch", add_data
->branch
))
3151 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3154 add_gitmodules
.git_cmd
= 1;
3155 strvec_pushl(&add_gitmodules
.args
,
3156 "add", "--force", "--", ".gitmodules", NULL
);
3158 if (run_command(&add_gitmodules
))
3159 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3162 * NEEDSWORK: In a multi-working-tree world this needs to be
3163 * set in the per-worktree config.
3166 * NEEDSWORK: In the longer run, we need to get rid of this
3167 * pattern of querying "submodule.active" before calling
3168 * is_submodule_active(), since that function needs to find
3169 * out the value of "submodule.active" again anyway.
3171 if (!git_config_get_string("submodule.active", &val
) && val
) {
3173 * If the submodule being added isn't already covered by the
3174 * current configured pathspec, set the submodule's active flag
3176 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3177 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3178 git_config_set_gently(key
, "true");
3182 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3183 git_config_set_gently(key
, "true");
3188 static void die_on_index_match(const char *path
, int force
)
3191 const char *args
[] = { path
, NULL
};
3192 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3194 if (read_cache_preload(NULL
) < 0)
3195 die(_("index file corrupt"));
3199 char *ps_matched
= xcalloc(ps
.nr
, 1);
3201 /* TODO: audit for interaction with sparse-index. */
3202 ensure_full_index(&the_index
);
3205 * Since there is only one pathspec, we just need
3206 * need to check ps_matched[0] to know if a cache
3209 for (i
= 0; i
< active_nr
; i
++) {
3210 ce_path_match(&the_index
, active_cache
[i
], &ps
,
3213 if (ps_matched
[0]) {
3215 die(_("'%s' already exists in the index"),
3217 if (!S_ISGITLINK(active_cache
[i
]->ce_mode
))
3218 die(_("'%s' already exists in the index "
3219 "and is not a submodule"), path
);
3225 clear_pathspec(&ps
);
3228 static void die_on_repo_without_commits(const char *path
)
3230 struct strbuf sb
= STRBUF_INIT
;
3231 strbuf_addstr(&sb
, path
);
3232 if (is_nonbare_repository_dir(&sb
)) {
3233 struct object_id oid
;
3234 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3235 die(_("'%s' does not have a commit checked out"), path
);
3237 strbuf_release(&sb
);
3240 static int module_add(int argc
, const char **argv
, const char *prefix
)
3242 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3243 struct add_data add_data
= ADD_DATA_INIT
;
3244 char *to_free
= NULL
;
3246 struct option options
[] = {
3247 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3248 N_("branch of repository to add as submodule")),
3249 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3250 PARSE_OPT_NOCOMPLETE
),
3251 OPT__QUIET(&quiet
, N_("print only error messages")),
3252 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3253 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3254 N_("reference repository")),
3255 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3256 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3257 N_("sets the submodule's name to the given string "
3258 "instead of defaulting to its path")),
3259 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3263 const char *const usage
[] = {
3264 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3268 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3270 if (!is_writing_gitmodules_ok())
3271 die(_("please make sure that the .gitmodules file is in the working tree"));
3273 if (prefix
&& *prefix
&&
3274 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3275 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3277 if (argc
== 0 || argc
> 2)
3278 usage_with_options(usage
, options
);
3280 add_data
.repo
= argv
[0];
3282 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3284 add_data
.sm_path
= xstrdup(argv
[1]);
3286 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
))
3287 add_data
.sm_path
= xstrfmt("%s%s", prefix
, add_data
.sm_path
);
3289 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3290 starts_with_dot_slash(add_data
.repo
)) {
3292 die(_("Relative path can only be used from the toplevel "
3293 "of the working tree"));
3295 /* dereference source url relative to parent's url */
3296 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3297 add_data
.realrepo
= to_free
;
3298 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3299 add_data
.realrepo
= add_data
.repo
;
3301 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3307 * multiple //; leading ./; /./; /../;
3309 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3310 strip_dir_trailing_slashes(add_data
.sm_path
);
3312 die_on_index_match(add_data
.sm_path
, force
);
3313 die_on_repo_without_commits(add_data
.sm_path
);
3317 struct strbuf sb
= STRBUF_INIT
;
3318 struct child_process cp
= CHILD_PROCESS_INIT
;
3321 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3322 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3323 if ((exit_code
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3324 strbuf_complete_line(&sb
);
3325 fputs(sb
.buf
, stderr
);
3326 free(add_data
.sm_path
);
3329 strbuf_release(&sb
);
3332 if(!add_data
.sm_name
)
3333 add_data
.sm_name
= add_data
.sm_path
;
3335 if (check_submodule_name(add_data
.sm_name
))
3336 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3338 add_data
.prefix
= prefix
;
3339 add_data
.force
= !!force
;
3340 add_data
.quiet
= !!quiet
;
3341 add_data
.progress
= !!progress
;
3342 add_data
.dissociate
= !!dissociate
;
3344 if (add_submodule(&add_data
)) {
3345 free(add_data
.sm_path
);
3348 configure_added_submodule(&add_data
);
3349 free(add_data
.sm_path
);
3355 #define SUPPORT_SUPER_PREFIX (1<<0)
3359 int (*fn
)(int, const char **, const char *);
3363 static struct cmd_struct commands
[] = {
3364 {"list", module_list
, 0},
3365 {"name", module_name
, 0},
3366 {"clone", module_clone
, SUPPORT_SUPER_PREFIX
},
3367 {"add", module_add
, 0},
3368 {"update", module_update
, SUPPORT_SUPER_PREFIX
},
3369 {"resolve-relative-url-test", resolve_relative_url_test
, 0},
3370 {"foreach", module_foreach
, SUPPORT_SUPER_PREFIX
},
3371 {"init", module_init
, 0},
3372 {"status", module_status
, SUPPORT_SUPER_PREFIX
},
3373 {"sync", module_sync
, SUPPORT_SUPER_PREFIX
},
3374 {"deinit", module_deinit
, 0},
3375 {"summary", module_summary
, 0},
3376 {"push-check", push_check
, 0},
3377 {"absorbgitdirs", absorb_git_dirs
, SUPPORT_SUPER_PREFIX
},
3378 {"is-active", is_active
, 0},
3379 {"check-name", check_name
, 0},
3380 {"config", module_config
, 0},
3381 {"set-url", module_set_url
, 0},
3382 {"set-branch", module_set_branch
, 0},
3383 {"create-branch", module_create_branch
, 0},
3386 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3389 if (argc
< 2 || !strcmp(argv
[1], "-h"))
3390 usage("git submodule--helper <command>");
3392 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
3393 if (!strcmp(argv
[1], commands
[i
].cmd
)) {
3394 if (get_super_prefix() &&
3395 !(commands
[i
].option
& SUPPORT_SUPER_PREFIX
))
3396 die(_("%s doesn't support --super-prefix"),
3398 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
3402 die(_("'%s' is not a valid submodule--helper "
3403 "subcommand"), argv
[1]);