3 #include "environment.h"
6 #include "repository.h"
8 #include "parse-options.h"
12 #include "preload-index.h"
14 #include "read-cache.h"
16 #include "sparse-index.h"
17 #include "submodule.h"
18 #include "submodule-config.h"
19 #include "string-list.h"
20 #include "run-command.h"
27 #include "object-file.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
32 #include "list-objects-filter-options.h"
34 #define OPT_QUIET (1 << 0)
35 #define OPT_CACHED (1 << 1)
36 #define OPT_RECURSIVE (1 << 2)
37 #define OPT_FORCE (1 << 3)
39 typedef void (*each_submodule_fn
)(const struct cache_entry
*list_item
,
42 static int repo_get_default_remote(struct repository
*repo
, char **default_remote
)
45 struct strbuf sb
= STRBUF_INIT
;
46 struct ref_store
*store
= get_main_ref_store(repo
);
47 const char *refname
= refs_resolve_ref_unsafe(store
, "HEAD", 0, NULL
,
51 return die_message(_("No such ref: %s"), "HEAD");
54 if (!strcmp(refname
, "HEAD")) {
55 *default_remote
= xstrdup("origin");
59 if (!skip_prefix(refname
, "refs/heads/", &refname
))
60 return die_message(_("Expecting a full ref name, got %s"),
63 strbuf_addf(&sb
, "branch.%s.remote", refname
);
64 if (repo_config_get_string(repo
, sb
.buf
, &dest
))
65 *default_remote
= xstrdup("origin");
67 *default_remote
= dest
;
73 static int get_default_remote_submodule(const char *module_path
, char **default_remote
)
75 struct repository subrepo
;
78 if (repo_submodule_init(&subrepo
, the_repository
, module_path
,
80 return die_message(_("could not get a repository handle for submodule '%s'"),
82 ret
= repo_get_default_remote(&subrepo
, default_remote
);
88 static char *get_default_remote(void)
91 int code
= repo_get_default_remote(the_repository
, &default_remote
);
96 return default_remote
;
99 static char *resolve_relative_url(const char *rel_url
, const char *up_path
, int quiet
)
101 char *remoteurl
, *resolved_url
;
102 char *remote
= get_default_remote();
103 struct strbuf remotesb
= STRBUF_INIT
;
105 strbuf_addf(&remotesb
, "remote.%s.url", remote
);
106 if (git_config_get_string(remotesb
.buf
, &remoteurl
)) {
108 warning(_("could not look up configuration '%s'. "
109 "Assuming this repository is its own "
110 "authoritative upstream."),
112 remoteurl
= xgetcwd();
114 resolved_url
= relative_url(remoteurl
, rel_url
, up_path
);
118 strbuf_release(&remotesb
);
123 /* the result should be freed by the caller. */
124 static char *get_submodule_displaypath(const char *path
, const char *prefix
,
125 const char *super_prefix
)
127 if (prefix
&& super_prefix
) {
128 BUG("cannot have prefix '%s' and superprefix '%s'",
129 prefix
, super_prefix
);
131 struct strbuf sb
= STRBUF_INIT
;
132 char *displaypath
= xstrdup(relative_path(path
, prefix
, &sb
));
135 } else if (super_prefix
) {
136 return xstrfmt("%s%s", super_prefix
, path
);
138 return xstrdup(path
);
142 static char *compute_rev_name(const char *sub_path
, const char* object_id
)
144 struct strbuf sb
= STRBUF_INIT
;
147 static const char *describe_bare
[] = { NULL
};
149 static const char *describe_tags
[] = { "--tags", NULL
};
151 static const char *describe_contains
[] = { "--contains", NULL
};
153 static const char *describe_all_always
[] = { "--all", "--always", NULL
};
155 static const char **describe_argv
[] = { describe_bare
, describe_tags
,
157 describe_all_always
, NULL
};
159 for (d
= describe_argv
; *d
; d
++) {
160 struct child_process cp
= CHILD_PROCESS_INIT
;
161 prepare_submodule_repo_env(&cp
.env
);
166 strvec_push(&cp
.args
, "describe");
167 strvec_pushv(&cp
.args
, *d
);
168 strvec_push(&cp
.args
, object_id
);
170 if (!capture_command(&cp
, &sb
, 0)) {
171 strbuf_strip_suffix(&sb
, "\n");
172 return strbuf_detach(&sb
, NULL
);
181 const struct cache_entry
**entries
;
184 #define MODULE_LIST_INIT { 0 }
186 static void module_list_release(struct module_list
*ml
)
191 static int module_list_compute(const char **argv
,
193 struct pathspec
*pathspec
,
194 struct module_list
*list
)
197 char *ps_matched
= NULL
;
199 parse_pathspec(pathspec
, 0,
200 PATHSPEC_PREFER_FULL
,
204 ps_matched
= xcalloc(pathspec
->nr
, 1);
206 if (repo_read_index(the_repository
) < 0)
207 die(_("index file corrupt"));
209 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
210 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
212 if (!match_pathspec(the_repository
->index
, pathspec
, ce
->name
, ce_namelen(ce
),
214 !S_ISGITLINK(ce
->ce_mode
))
217 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
218 list
->entries
[list
->nr
++] = ce
;
219 while (i
+ 1 < the_repository
->index
->cache_nr
&&
220 !strcmp(ce
->name
, the_repository
->index
->cache
[i
+ 1]->name
))
222 * Skip entries with the same name in different stages
223 * to make sure an entry is returned only once.
228 if (ps_matched
&& report_path_error(ps_matched
, pathspec
))
236 static void module_list_active(struct module_list
*list
)
239 struct module_list active_modules
= MODULE_LIST_INIT
;
241 for (i
= 0; i
< list
->nr
; i
++) {
242 const struct cache_entry
*ce
= list
->entries
[i
];
244 if (!is_submodule_active(the_repository
, ce
->name
))
247 ALLOC_GROW(active_modules
.entries
,
248 active_modules
.nr
+ 1,
249 active_modules
.alloc
);
250 active_modules
.entries
[active_modules
.nr
++] = ce
;
253 module_list_release(list
);
254 *list
= active_modules
;
257 static char *get_up_path(const char *path
)
260 struct strbuf sb
= STRBUF_INIT
;
262 for (i
= count_slashes(path
); i
; i
--)
263 strbuf_addstr(&sb
, "../");
266 * Check if 'path' ends with slash or not
267 * for having the same output for dir/sub_dir
270 if (!is_dir_sep(path
[strlen(path
) - 1]))
271 strbuf_addstr(&sb
, "../");
273 return strbuf_detach(&sb
, NULL
);
276 static void for_each_listed_submodule(const struct module_list
*list
,
277 each_submodule_fn fn
, void *cb_data
)
281 for (i
= 0; i
< list
->nr
; i
++)
282 fn(list
->entries
[i
], cb_data
);
289 const char *super_prefix
;
293 #define FOREACH_CB_INIT { 0 }
295 static void runcommand_in_submodule_cb(const struct cache_entry
*list_item
,
298 struct foreach_cb
*info
= cb_data
;
299 const char *path
= list_item
->name
;
300 const struct object_id
*ce_oid
= &list_item
->oid
;
301 const struct submodule
*sub
;
302 struct child_process cp
= CHILD_PROCESS_INIT
;
305 displaypath
= get_submodule_displaypath(path
, info
->prefix
,
308 sub
= submodule_from_path(the_repository
, null_oid(), path
);
311 die(_("No url found for submodule path '%s' in .gitmodules"),
314 if (!is_submodule_populated_gently(path
, NULL
))
317 prepare_submodule_repo_env(&cp
.env
);
320 * For the purpose of executing <command> in the submodule,
321 * separate shell is used for the purpose of running the
328 * NEEDSWORK: the command currently has access to the variables $name,
329 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
330 * contains a single argument. This is done for maintaining a faithful
331 * translation from shell script.
333 if (info
->argc
== 1) {
334 char *toplevel
= xgetcwd();
335 struct strbuf sb
= STRBUF_INIT
;
337 strvec_pushf(&cp
.env
, "name=%s", sub
->name
);
338 strvec_pushf(&cp
.env
, "sm_path=%s", path
);
339 strvec_pushf(&cp
.env
, "displaypath=%s", displaypath
);
340 strvec_pushf(&cp
.env
, "sha1=%s",
342 strvec_pushf(&cp
.env
, "toplevel=%s", toplevel
);
345 * Since the path variable was accessible from the script
346 * before porting, it is also made available after porting.
347 * The environment variable "PATH" has a very special purpose
348 * on windows. And since environment variables are
349 * case-insensitive in windows, it interferes with the
350 * existing PATH variable. Hence, to avoid that, we expose
351 * path via the args strvec and not via env.
353 sq_quote_buf(&sb
, path
);
354 strvec_pushf(&cp
.args
, "path=%s; %s",
355 sb
.buf
, info
->argv
[0]);
359 strvec_pushv(&cp
.args
, info
->argv
);
363 printf(_("Entering '%s'\n"), displaypath
);
365 if (info
->argv
[0] && run_command(&cp
))
366 die(_("run_command returned non-zero status for %s\n."),
369 if (info
->recursive
) {
370 struct child_process cpr
= CHILD_PROCESS_INIT
;
374 prepare_submodule_repo_env(&cpr
.env
);
376 strvec_pushl(&cpr
.args
, "submodule--helper", "foreach", "--recursive",
378 strvec_pushl(&cpr
.args
, "--super-prefix", NULL
);
379 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
382 strvec_push(&cpr
.args
, "--quiet");
384 strvec_push(&cpr
.args
, "--");
385 strvec_pushv(&cpr
.args
, info
->argv
);
387 if (run_command(&cpr
))
388 die(_("run_command returned non-zero status while "
389 "recursing in the nested submodules of %s\n."),
397 static int module_foreach(int argc
, const char **argv
, const char *prefix
)
399 struct foreach_cb info
= FOREACH_CB_INIT
;
400 struct pathspec pathspec
= { 0 };
401 struct module_list list
= MODULE_LIST_INIT
;
402 struct option module_foreach_options
[] = {
403 OPT__SUPER_PREFIX(&info
.super_prefix
),
404 OPT__QUIET(&info
.quiet
, N_("suppress output of entering each submodule command")),
405 OPT_BOOL(0, "recursive", &info
.recursive
,
406 N_("recurse into nested submodules")),
409 const char *const git_submodule_helper_usage
[] = {
410 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
415 argc
= parse_options(argc
, argv
, prefix
, module_foreach_options
,
416 git_submodule_helper_usage
, 0);
418 if (module_list_compute(NULL
, prefix
, &pathspec
, &list
) < 0)
423 info
.prefix
= prefix
;
425 for_each_listed_submodule(&list
, runcommand_in_submodule_cb
, &info
);
429 module_list_release(&list
);
430 clear_pathspec(&pathspec
);
434 static int starts_with_dot_slash(const char *const path
)
436 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_SLASH
|
437 PATH_MATCH_XPLATFORM
);
440 static int starts_with_dot_dot_slash(const char *const path
)
442 return path_match_flags(path
, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH
|
443 PATH_MATCH_XPLATFORM
);
448 const char *super_prefix
;
451 #define INIT_CB_INIT { 0 }
453 static void init_submodule(const char *path
, const char *prefix
,
454 const char *super_prefix
,
457 const struct submodule
*sub
;
458 struct strbuf sb
= STRBUF_INIT
;
460 char *url
= NULL
, *displaypath
;
462 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
464 sub
= submodule_from_path(the_repository
, null_oid(), path
);
467 die(_("No url found for submodule path '%s' in .gitmodules"),
471 * NEEDSWORK: In a multi-working-tree world, this needs to be
472 * set in the per-worktree config.
474 * Set active flag for the submodule being initialized
476 if (!is_submodule_active(the_repository
, path
)) {
477 strbuf_addf(&sb
, "submodule.%s.active", sub
->name
);
478 git_config_set_gently(sb
.buf
, "true");
483 * Copy url setting when it is not set yet.
484 * To look up the url in .git/config, we must not fall back to
485 * .gitmodules, so look it up directly.
487 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
488 if (git_config_get_string(sb
.buf
, &url
)) {
490 die(_("No url found for submodule path '%s' in .gitmodules"),
493 url
= xstrdup(sub
->url
);
495 /* Possibly a url relative to parent */
496 if (starts_with_dot_dot_slash(url
) ||
497 starts_with_dot_slash(url
)) {
500 url
= resolve_relative_url(oldurl
, NULL
, 0);
504 if (git_config_set_gently(sb
.buf
, url
))
505 die(_("Failed to register url for submodule path '%s'"),
507 if (!(flags
& OPT_QUIET
))
509 _("Submodule '%s' (%s) registered for path '%s'\n"),
510 sub
->name
, url
, displaypath
);
514 /* Copy "update" setting when it is not set yet */
515 strbuf_addf(&sb
, "submodule.%s.update", sub
->name
);
516 if (git_config_get_string_tmp(sb
.buf
, &upd
) &&
517 sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
518 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
) {
519 fprintf(stderr
, _("warning: command update mode suggested for submodule '%s'\n"),
523 upd
= submodule_update_type_to_string(sub
->update_strategy
.type
);
526 if (git_config_set_gently(sb
.buf
, upd
))
527 die(_("Failed to register update mode for submodule path '%s'"), displaypath
);
534 static void init_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
536 struct init_cb
*info
= cb_data
;
538 init_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
542 static int module_init(int argc
, const char **argv
, const char *prefix
)
544 struct init_cb info
= INIT_CB_INIT
;
545 struct pathspec pathspec
= { 0 };
546 struct module_list list
= MODULE_LIST_INIT
;
548 struct option module_init_options
[] = {
549 OPT__QUIET(&quiet
, N_("suppress output for initializing a submodule")),
552 const char *const git_submodule_helper_usage
[] = {
553 N_("git submodule init [<options>] [<path>]"),
558 argc
= parse_options(argc
, argv
, prefix
, module_init_options
,
559 git_submodule_helper_usage
, 0);
561 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
565 * If there are no path args and submodule.active is set then,
566 * by default, only initialize 'active' modules.
568 if (!argc
&& !git_config_get("submodule.active"))
569 module_list_active(&list
);
571 info
.prefix
= prefix
;
573 info
.flags
|= OPT_QUIET
;
575 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
579 module_list_release(&list
);
580 clear_pathspec(&pathspec
);
586 const char *super_prefix
;
589 #define STATUS_CB_INIT { 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 char *name
= compute_rev_name(path
, oid_to_hex(oid
));
603 printf(" (%s)", name
);
610 static int handle_submodule_head_ref(const char *refname UNUSED
,
611 const struct object_id
*oid
,
615 struct object_id
*output
= cb_data
;
623 static void status_submodule(const char *path
, const struct object_id
*ce_oid
,
624 unsigned int ce_flags
, const char *prefix
,
625 const char *super_prefix
, unsigned int flags
)
628 struct strvec diff_files_args
= STRVEC_INIT
;
629 struct rev_info rev
= REV_INFO_INIT
;
630 struct strbuf buf
= STRBUF_INIT
;
632 struct setup_revision_opt opt
= {
633 .free_removed_argv_elements
= 1,
636 if (!submodule_from_path(the_repository
, null_oid(), path
))
637 die(_("no submodule mapping found in .gitmodules for path '%s'"),
640 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
642 if ((CE_STAGEMASK
& ce_flags
) >> CE_STAGESHIFT
) {
643 print_status(flags
, 'U', path
, null_oid(), displaypath
);
647 strbuf_addf(&buf
, "%s/.git", path
);
648 git_dir
= read_gitfile(buf
.buf
);
652 if (!is_submodule_active(the_repository
, path
) ||
653 !is_git_directory(git_dir
)) {
654 print_status(flags
, '-', path
, ce_oid
, displaypath
);
655 strbuf_release(&buf
);
658 strbuf_release(&buf
);
660 strvec_pushl(&diff_files_args
, "diff-files",
661 "--ignore-submodules=dirty", "--quiet", "--",
664 git_config(git_diff_basic_config
, NULL
);
666 repo_init_revisions(the_repository
, &rev
, NULL
);
668 setup_revisions(diff_files_args
.nr
, diff_files_args
.v
, &rev
, &opt
);
669 run_diff_files(&rev
, 0);
671 if (!diff_result_code(&rev
.diffopt
)) {
672 print_status(flags
, ' ', path
, ce_oid
,
674 } else if (!(flags
& OPT_CACHED
)) {
675 struct object_id oid
;
676 struct ref_store
*refs
= get_submodule_ref_store(path
);
679 print_status(flags
, '-', path
, ce_oid
, displaypath
);
682 if (refs_head_ref(refs
, handle_submodule_head_ref
, &oid
))
683 die(_("could not resolve HEAD ref inside the "
684 "submodule '%s'"), path
);
686 print_status(flags
, '+', path
, &oid
, displaypath
);
688 print_status(flags
, '+', path
, ce_oid
, displaypath
);
691 if (flags
& OPT_RECURSIVE
) {
692 struct child_process cpr
= CHILD_PROCESS_INIT
;
696 prepare_submodule_repo_env(&cpr
.env
);
698 strvec_pushl(&cpr
.args
, "submodule--helper", "status",
699 "--recursive", NULL
);
700 strvec_push(&cpr
.args
, "--super-prefix");
701 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
703 if (flags
& OPT_CACHED
)
704 strvec_push(&cpr
.args
, "--cached");
706 if (flags
& OPT_QUIET
)
707 strvec_push(&cpr
.args
, "--quiet");
709 if (run_command(&cpr
))
710 die(_("failed to recurse into submodule '%s'"), path
);
714 strvec_clear(&diff_files_args
);
716 release_revisions(&rev
);
719 static void status_submodule_cb(const struct cache_entry
*list_item
,
722 struct status_cb
*info
= cb_data
;
724 status_submodule(list_item
->name
, &list_item
->oid
, list_item
->ce_flags
,
725 info
->prefix
, info
->super_prefix
, info
->flags
);
728 static int module_status(int argc
, const char **argv
, const char *prefix
)
730 struct status_cb info
= STATUS_CB_INIT
;
731 struct pathspec pathspec
= { 0 };
732 struct module_list list
= MODULE_LIST_INIT
;
734 struct option module_status_options
[] = {
735 OPT__SUPER_PREFIX(&info
.super_prefix
),
736 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
737 OPT_BIT(0, "cached", &info
.flags
, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED
),
738 OPT_BIT(0, "recursive", &info
.flags
, N_("recurse into nested submodules"), OPT_RECURSIVE
),
741 const char *const git_submodule_helper_usage
[] = {
742 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
747 argc
= parse_options(argc
, argv
, prefix
, module_status_options
,
748 git_submodule_helper_usage
, 0);
750 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
753 info
.prefix
= prefix
;
755 info
.flags
|= OPT_QUIET
;
757 for_each_listed_submodule(&list
, status_submodule_cb
, &info
);
761 module_list_release(&list
);
762 clear_pathspec(&pathspec
);
767 unsigned int mod_src
;
768 unsigned int mod_dst
;
769 struct object_id oid_src
;
770 struct object_id oid_dst
;
774 #define MODULE_CB_INIT { 0 }
776 static void module_cb_release(struct module_cb
*mcb
)
781 struct module_cb_list
{
782 struct module_cb
**entries
;
785 #define MODULE_CB_LIST_INIT { 0 }
787 static void module_cb_list_release(struct module_cb_list
*mcbl
)
791 for (i
= 0; i
< mcbl
->nr
; i
++) {
792 struct module_cb
*mcb
= mcbl
->entries
[i
];
794 module_cb_release(mcb
);
804 const char *super_prefix
;
805 unsigned int cached
: 1;
806 unsigned int for_status
: 1;
807 unsigned int files
: 1;
810 #define SUMMARY_CB_INIT { 0 }
817 static char *verify_submodule_committish(const char *sm_path
,
818 const char *committish
)
820 struct child_process cp_rev_parse
= CHILD_PROCESS_INIT
;
821 struct strbuf result
= STRBUF_INIT
;
823 cp_rev_parse
.git_cmd
= 1;
824 cp_rev_parse
.dir
= sm_path
;
825 prepare_submodule_repo_env(&cp_rev_parse
.env
);
826 strvec_pushl(&cp_rev_parse
.args
, "rev-parse", "-q", "--short", NULL
);
827 strvec_pushf(&cp_rev_parse
.args
, "%s^0", committish
);
828 strvec_push(&cp_rev_parse
.args
, "--");
830 if (capture_command(&cp_rev_parse
, &result
, 0))
833 strbuf_trim_trailing_newline(&result
);
834 return strbuf_detach(&result
, NULL
);
837 static void print_submodule_summary(struct summary_cb
*info
, const char *errmsg
,
838 int total_commits
, const char *displaypath
,
839 const char *src_abbrev
, const char *dst_abbrev
,
842 if (p
->status
== 'T') {
843 if (S_ISGITLINK(p
->mod_dst
))
844 printf(_("* %s %s(blob)->%s(submodule)"),
845 displaypath
, src_abbrev
, dst_abbrev
);
847 printf(_("* %s %s(submodule)->%s(blob)"),
848 displaypath
, src_abbrev
, dst_abbrev
);
850 printf("* %s %s...%s",
851 displaypath
, src_abbrev
, dst_abbrev
);
854 if (total_commits
< 0)
857 printf(" (%d):\n", total_commits
);
860 printf(_("%s"), errmsg
);
861 } else if (total_commits
> 0) {
862 struct child_process cp_log
= CHILD_PROCESS_INIT
;
865 cp_log
.dir
= p
->sm_path
;
866 prepare_submodule_repo_env(&cp_log
.env
);
867 strvec_pushl(&cp_log
.args
, "log", NULL
);
869 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
)) {
870 if (info
->summary_limit
> 0)
871 strvec_pushf(&cp_log
.args
, "-%d",
872 info
->summary_limit
);
874 strvec_pushl(&cp_log
.args
, "--pretty= %m %s",
875 "--first-parent", NULL
);
876 strvec_pushf(&cp_log
.args
, "%s...%s",
877 src_abbrev
, dst_abbrev
);
878 } else if (S_ISGITLINK(p
->mod_dst
)) {
879 strvec_pushl(&cp_log
.args
, "--pretty= > %s",
880 "-1", dst_abbrev
, NULL
);
882 strvec_pushl(&cp_log
.args
, "--pretty= < %s",
883 "-1", src_abbrev
, NULL
);
885 run_command(&cp_log
);
890 static void generate_submodule_summary(struct summary_cb
*info
,
893 char *displaypath
, *src_abbrev
= NULL
, *dst_abbrev
;
894 int missing_src
= 0, missing_dst
= 0;
895 struct strbuf errmsg
= STRBUF_INIT
;
896 int total_commits
= -1;
898 if (!info
->cached
&& oideq(&p
->oid_dst
, null_oid())) {
899 if (S_ISGITLINK(p
->mod_dst
)) {
900 struct ref_store
*refs
= get_submodule_ref_store(p
->sm_path
);
903 refs_head_ref(refs
, handle_submodule_head_ref
, &p
->oid_dst
);
904 } else if (S_ISLNK(p
->mod_dst
) || S_ISREG(p
->mod_dst
)) {
906 int fd
= open(p
->sm_path
, O_RDONLY
);
908 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
909 index_fd(the_repository
->index
, &p
->oid_dst
, fd
, &st
, OBJ_BLOB
,
911 error(_("couldn't hash object from '%s'"), p
->sm_path
);
913 /* for a submodule removal (mode:0000000), don't warn */
915 warning(_("unexpected mode %o\n"), p
->mod_dst
);
919 if (S_ISGITLINK(p
->mod_src
)) {
920 if (p
->status
!= 'D')
921 src_abbrev
= verify_submodule_committish(p
->sm_path
,
922 oid_to_hex(&p
->oid_src
));
926 * As `rev-parse` failed, we fallback to getting
927 * the abbreviated hash using oid_src. We do
928 * this as we might still need the abbreviated
929 * hash in cases like a submodule type change, etc.
931 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
935 * The source does not point to a submodule.
936 * So, we fallback to getting the abbreviation using
937 * oid_src as we might still need the abbreviated
938 * hash in cases like submodule add, etc.
940 src_abbrev
= xstrndup(oid_to_hex(&p
->oid_src
), 7);
943 if (S_ISGITLINK(p
->mod_dst
)) {
944 dst_abbrev
= verify_submodule_committish(p
->sm_path
,
945 oid_to_hex(&p
->oid_dst
));
949 * As `rev-parse` failed, we fallback to getting
950 * the abbreviated hash using oid_dst. We do
951 * this as we might still need the abbreviated
952 * hash in cases like a submodule type change, etc.
954 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
958 * The destination does not point to a submodule.
959 * So, we fallback to getting the abbreviation using
960 * oid_dst as we might still need the abbreviated
961 * hash in cases like a submodule removal, etc.
963 dst_abbrev
= xstrndup(oid_to_hex(&p
->oid_dst
), 7);
966 displaypath
= get_submodule_displaypath(p
->sm_path
, info
->prefix
,
969 if (!missing_src
&& !missing_dst
) {
970 struct child_process cp_rev_list
= CHILD_PROCESS_INIT
;
971 struct strbuf sb_rev_list
= STRBUF_INIT
;
973 strvec_pushl(&cp_rev_list
.args
, "rev-list",
974 "--first-parent", "--count", NULL
);
975 if (S_ISGITLINK(p
->mod_src
) && S_ISGITLINK(p
->mod_dst
))
976 strvec_pushf(&cp_rev_list
.args
, "%s...%s",
977 src_abbrev
, dst_abbrev
);
979 strvec_push(&cp_rev_list
.args
, S_ISGITLINK(p
->mod_src
) ?
980 src_abbrev
: dst_abbrev
);
981 strvec_push(&cp_rev_list
.args
, "--");
983 cp_rev_list
.git_cmd
= 1;
984 cp_rev_list
.dir
= p
->sm_path
;
985 prepare_submodule_repo_env(&cp_rev_list
.env
);
987 if (!capture_command(&cp_rev_list
, &sb_rev_list
, 0))
988 total_commits
= atoi(sb_rev_list
.buf
);
990 strbuf_release(&sb_rev_list
);
993 * Don't give error msg for modification whose dst is not
994 * submodule, i.e., deleted or changed to blob
996 if (S_ISGITLINK(p
->mod_dst
)) {
997 if (missing_src
&& missing_dst
) {
998 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commits %s and %s\n",
999 displaypath
, oid_to_hex(&p
->oid_src
),
1000 oid_to_hex(&p
->oid_dst
));
1002 strbuf_addf(&errmsg
, " Warn: %s doesn't contain commit %s\n",
1003 displaypath
, missing_src
?
1004 oid_to_hex(&p
->oid_src
) :
1005 oid_to_hex(&p
->oid_dst
));
1010 print_submodule_summary(info
, errmsg
.len
? errmsg
.buf
: NULL
,
1011 total_commits
, displaypath
, src_abbrev
,
1017 strbuf_release(&errmsg
);
1020 static void prepare_submodule_summary(struct summary_cb
*info
,
1021 struct module_cb_list
*list
)
1024 for (i
= 0; i
< list
->nr
; i
++) {
1025 const struct submodule
*sub
;
1026 struct module_cb
*p
= list
->entries
[i
];
1027 struct strbuf sm_gitdir
= STRBUF_INIT
;
1029 if (p
->status
== 'D' || p
->status
== 'T') {
1030 generate_submodule_summary(info
, p
);
1034 if (info
->for_status
&& p
->status
!= 'A' &&
1035 (sub
= submodule_from_path(the_repository
,
1036 null_oid(), p
->sm_path
))) {
1037 char *config_key
= NULL
;
1041 config_key
= xstrfmt("submodule.%s.ignore",
1043 if (!git_config_get_string_tmp(config_key
, &value
))
1044 ignore_all
= !strcmp(value
, "all");
1045 else if (sub
->ignore
)
1046 ignore_all
= !strcmp(sub
->ignore
, "all");
1053 /* Also show added or modified modules which are checked out */
1054 strbuf_addstr(&sm_gitdir
, p
->sm_path
);
1055 if (is_nonbare_repository_dir(&sm_gitdir
))
1056 generate_submodule_summary(info
, p
);
1057 strbuf_release(&sm_gitdir
);
1061 static void submodule_summary_callback(struct diff_queue_struct
*q
,
1062 struct diff_options
*options UNUSED
,
1066 struct module_cb_list
*list
= data
;
1067 for (i
= 0; i
< q
->nr
; i
++) {
1068 struct diff_filepair
*p
= q
->queue
[i
];
1069 struct module_cb
*temp
;
1071 if (!S_ISGITLINK(p
->one
->mode
) && !S_ISGITLINK(p
->two
->mode
))
1073 temp
= (struct module_cb
*)malloc(sizeof(struct module_cb
));
1074 temp
->mod_src
= p
->one
->mode
;
1075 temp
->mod_dst
= p
->two
->mode
;
1076 temp
->oid_src
= p
->one
->oid
;
1077 temp
->oid_dst
= p
->two
->oid
;
1078 temp
->status
= p
->status
;
1079 temp
->sm_path
= xstrdup(p
->one
->path
);
1081 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
1082 list
->entries
[list
->nr
++] = temp
;
1086 static const char *get_diff_cmd(enum diff_cmd diff_cmd
)
1089 case DIFF_INDEX
: return "diff-index";
1090 case DIFF_FILES
: return "diff-files";
1091 default: BUG("bad diff_cmd value %d", diff_cmd
);
1095 static int compute_summary_module_list(struct object_id
*head_oid
,
1096 struct summary_cb
*info
,
1097 enum diff_cmd diff_cmd
)
1099 struct strvec diff_args
= STRVEC_INIT
;
1100 struct rev_info rev
;
1101 struct setup_revision_opt opt
= {
1102 .free_removed_argv_elements
= 1,
1104 struct module_cb_list list
= MODULE_CB_LIST_INIT
;
1107 strvec_push(&diff_args
, get_diff_cmd(diff_cmd
));
1109 strvec_push(&diff_args
, "--cached");
1110 strvec_pushl(&diff_args
, "--ignore-submodules=dirty", "--raw", NULL
);
1112 strvec_push(&diff_args
, oid_to_hex(head_oid
));
1113 strvec_push(&diff_args
, "--");
1115 strvec_pushv(&diff_args
, info
->argv
);
1117 git_config(git_diff_basic_config
, NULL
);
1118 repo_init_revisions(the_repository
, &rev
, info
->prefix
);
1120 precompose_argv_prefix(diff_args
.nr
, diff_args
.v
, NULL
);
1121 setup_revisions(diff_args
.nr
, diff_args
.v
, &rev
, &opt
);
1122 rev
.diffopt
.output_format
= DIFF_FORMAT_NO_OUTPUT
| DIFF_FORMAT_CALLBACK
;
1123 rev
.diffopt
.format_callback
= submodule_summary_callback
;
1124 rev
.diffopt
.format_callback_data
= &list
;
1126 if (!info
->cached
) {
1127 if (diff_cmd
== DIFF_INDEX
)
1129 if (repo_read_index_preload(the_repository
, &rev
.diffopt
.pathspec
, 0) < 0) {
1130 perror("repo_read_index_preload");
1134 } else if (repo_read_index(the_repository
) < 0) {
1135 perror("repo_read_cache");
1140 if (diff_cmd
== DIFF_INDEX
)
1141 run_diff_index(&rev
, info
->cached
? DIFF_INDEX_CACHED
: 0);
1143 run_diff_files(&rev
, 0);
1144 prepare_submodule_summary(info
, &list
);
1146 strvec_clear(&diff_args
);
1147 release_revisions(&rev
);
1148 module_cb_list_release(&list
);
1152 static int module_summary(int argc
, const char **argv
, const char *prefix
)
1154 struct summary_cb info
= SUMMARY_CB_INIT
;
1158 int summary_limit
= -1;
1159 enum diff_cmd diff_cmd
= DIFF_INDEX
;
1160 struct object_id head_oid
;
1162 struct option module_summary_options
[] = {
1163 OPT_BOOL(0, "cached", &cached
,
1164 N_("use the commit stored in the index instead of the submodule HEAD")),
1165 OPT_BOOL(0, "files", &files
,
1166 N_("compare the commit in the index with that in the submodule HEAD")),
1167 OPT_BOOL(0, "for-status", &for_status
,
1168 N_("skip submodules with 'ignore_config' value set to 'all'")),
1169 OPT_INTEGER('n', "summary-limit", &summary_limit
,
1170 N_("limit the summary size")),
1173 const char *const git_submodule_helper_usage
[] = {
1174 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1178 argc
= parse_options(argc
, argv
, prefix
, module_summary_options
,
1179 git_submodule_helper_usage
, 0);
1184 if (!repo_get_oid(the_repository
, argc
? argv
[0] : "HEAD", &head_oid
)) {
1189 } else if (!argc
|| !strcmp(argv
[0], "HEAD")) {
1190 /* before the first commit: compare with an empty tree */
1191 oidcpy(&head_oid
, the_hash_algo
->empty_tree
);
1197 if (repo_get_oid(the_repository
, "HEAD", &head_oid
))
1198 die(_("could not fetch a revision for HEAD"));
1203 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1204 diff_cmd
= DIFF_FILES
;
1209 info
.prefix
= prefix
;
1210 info
.cached
= !!cached
;
1211 info
.files
= !!files
;
1212 info
.for_status
= !!for_status
;
1213 info
.summary_limit
= summary_limit
;
1215 ret
= compute_summary_module_list((diff_cmd
== DIFF_INDEX
) ? &head_oid
: NULL
,
1222 const char *super_prefix
;
1225 #define SYNC_CB_INIT { 0 }
1227 static void sync_submodule(const char *path
, const char *prefix
,
1228 const char *super_prefix
, unsigned int flags
)
1230 const struct submodule
*sub
;
1231 char *remote_key
= NULL
;
1232 char *sub_origin_url
, *super_config_url
, *displaypath
, *default_remote
;
1233 struct strbuf sb
= STRBUF_INIT
;
1234 char *sub_config_path
= NULL
;
1237 if (!is_submodule_active(the_repository
, path
))
1240 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1242 if (sub
&& sub
->url
) {
1243 if (starts_with_dot_dot_slash(sub
->url
) ||
1244 starts_with_dot_slash(sub
->url
)) {
1245 char *up_path
= get_up_path(path
);
1247 sub_origin_url
= resolve_relative_url(sub
->url
, up_path
, 1);
1248 super_config_url
= resolve_relative_url(sub
->url
, NULL
, 1);
1251 sub_origin_url
= xstrdup(sub
->url
);
1252 super_config_url
= xstrdup(sub
->url
);
1255 sub_origin_url
= xstrdup("");
1256 super_config_url
= xstrdup("");
1259 displaypath
= get_submodule_displaypath(path
, prefix
, super_prefix
);
1261 if (!(flags
& OPT_QUIET
))
1262 printf(_("Synchronizing submodule url for '%s'\n"),
1266 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
1267 if (git_config_set_gently(sb
.buf
, super_config_url
))
1268 die(_("failed to register url for submodule path '%s'"),
1271 if (!is_submodule_populated_gently(path
, NULL
))
1275 code
= get_default_remote_submodule(path
, &default_remote
);
1279 remote_key
= xstrfmt("remote.%s.url", default_remote
);
1280 free(default_remote
);
1282 submodule_to_gitdir(&sb
, path
);
1283 strbuf_addstr(&sb
, "/config");
1285 if (git_config_set_in_file_gently(sb
.buf
, remote_key
, NULL
, sub_origin_url
))
1286 die(_("failed to update remote for submodule '%s'"),
1289 if (flags
& OPT_RECURSIVE
) {
1290 struct child_process cpr
= CHILD_PROCESS_INIT
;
1294 prepare_submodule_repo_env(&cpr
.env
);
1296 strvec_pushl(&cpr
.args
, "submodule--helper", "sync",
1297 "--recursive", NULL
);
1298 strvec_push(&cpr
.args
, "--super-prefix");
1299 strvec_pushf(&cpr
.args
, "%s/", displaypath
);
1302 if (flags
& OPT_QUIET
)
1303 strvec_push(&cpr
.args
, "--quiet");
1305 if (run_command(&cpr
))
1306 die(_("failed to recurse into submodule '%s'"),
1311 free(super_config_url
);
1312 free(sub_origin_url
);
1313 strbuf_release(&sb
);
1316 free(sub_config_path
);
1319 static void sync_submodule_cb(const struct cache_entry
*list_item
, void *cb_data
)
1321 struct sync_cb
*info
= cb_data
;
1323 sync_submodule(list_item
->name
, info
->prefix
, info
->super_prefix
,
1327 static int module_sync(int argc
, const char **argv
, const char *prefix
)
1329 struct sync_cb info
= SYNC_CB_INIT
;
1330 struct pathspec pathspec
= { 0 };
1331 struct module_list list
= MODULE_LIST_INIT
;
1334 struct option module_sync_options
[] = {
1335 OPT__SUPER_PREFIX(&info
.super_prefix
),
1336 OPT__QUIET(&quiet
, N_("suppress output of synchronizing submodule url")),
1337 OPT_BOOL(0, "recursive", &recursive
,
1338 N_("recurse into nested submodules")),
1341 const char *const git_submodule_helper_usage
[] = {
1342 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1347 argc
= parse_options(argc
, argv
, prefix
, module_sync_options
,
1348 git_submodule_helper_usage
, 0);
1350 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1353 info
.prefix
= prefix
;
1355 info
.flags
|= OPT_QUIET
;
1357 info
.flags
|= OPT_RECURSIVE
;
1359 for_each_listed_submodule(&list
, sync_submodule_cb
, &info
);
1363 module_list_release(&list
);
1364 clear_pathspec(&pathspec
);
1372 #define DEINIT_CB_INIT { 0 }
1374 static void deinit_submodule(const char *path
, const char *prefix
,
1377 const struct submodule
*sub
;
1378 char *displaypath
= NULL
;
1379 struct child_process cp_config
= CHILD_PROCESS_INIT
;
1380 struct strbuf sb_config
= STRBUF_INIT
;
1381 char *sub_git_dir
= xstrfmt("%s/.git", path
);
1383 sub
= submodule_from_path(the_repository
, null_oid(), path
);
1385 if (!sub
|| !sub
->name
)
1388 displaypath
= get_submodule_displaypath(path
, prefix
, NULL
);
1390 /* remove the submodule work tree (unless the user already did it) */
1391 if (is_directory(path
)) {
1392 struct strbuf sb_rm
= STRBUF_INIT
;
1395 if (is_directory(sub_git_dir
)) {
1396 if (!(flags
& OPT_QUIET
))
1397 warning(_("Submodule work tree '%s' contains a .git "
1398 "directory. This will be replaced with a "
1399 ".git file by using absorbgitdirs."),
1402 absorb_git_dir_into_superproject(path
, NULL
);
1406 if (!(flags
& OPT_FORCE
)) {
1407 struct child_process cp_rm
= CHILD_PROCESS_INIT
;
1410 strvec_pushl(&cp_rm
.args
, "rm", "-qn",
1413 if (run_command(&cp_rm
))
1414 die(_("Submodule work tree '%s' contains local "
1415 "modifications; use '-f' to discard them"),
1419 strbuf_addstr(&sb_rm
, path
);
1421 if (!remove_dir_recursively(&sb_rm
, 0))
1422 format
= _("Cleared directory '%s'\n");
1424 format
= _("Could not remove submodule work tree '%s'\n");
1426 if (!(flags
& OPT_QUIET
))
1427 printf(format
, displaypath
);
1429 submodule_unset_core_worktree(sub
);
1431 strbuf_release(&sb_rm
);
1434 if (mkdir(path
, 0777))
1435 printf(_("could not create empty submodule directory %s"),
1438 cp_config
.git_cmd
= 1;
1439 strvec_pushl(&cp_config
.args
, "config", "--get-regexp", NULL
);
1440 strvec_pushf(&cp_config
.args
, "submodule.%s\\.", sub
->name
);
1442 /* remove the .git/config entries (unless the user already did it) */
1443 if (!capture_command(&cp_config
, &sb_config
, 0) && sb_config
.len
) {
1444 char *sub_key
= xstrfmt("submodule.%s", sub
->name
);
1447 * remove the whole section so we have a clean state when
1448 * the user later decides to init this submodule again
1450 git_config_rename_section_in_file(NULL
, sub_key
, NULL
);
1451 if (!(flags
& OPT_QUIET
))
1452 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1453 sub
->name
, sub
->url
, displaypath
);
1460 strbuf_release(&sb_config
);
1463 static void deinit_submodule_cb(const struct cache_entry
*list_item
,
1466 struct deinit_cb
*info
= cb_data
;
1467 deinit_submodule(list_item
->name
, info
->prefix
, info
->flags
);
1470 static int module_deinit(int argc
, const char **argv
, const char *prefix
)
1472 struct deinit_cb info
= DEINIT_CB_INIT
;
1473 struct pathspec pathspec
= { 0 };
1474 struct module_list list
= MODULE_LIST_INIT
;
1478 struct option module_deinit_options
[] = {
1479 OPT__QUIET(&quiet
, N_("suppress submodule status output")),
1480 OPT__FORCE(&force
, N_("remove submodule working trees even if they contain local changes"), 0),
1481 OPT_BOOL(0, "all", &all
, N_("unregister all submodules")),
1484 const char *const git_submodule_helper_usage
[] = {
1485 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1490 argc
= parse_options(argc
, argv
, prefix
, module_deinit_options
,
1491 git_submodule_helper_usage
, 0);
1494 error("pathspec and --all are incompatible");
1495 usage_with_options(git_submodule_helper_usage
,
1496 module_deinit_options
);
1500 die(_("Use '--all' if you really want to deinitialize all submodules"));
1502 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
1505 info
.prefix
= prefix
;
1507 info
.flags
|= OPT_QUIET
;
1509 info
.flags
|= OPT_FORCE
;
1511 for_each_listed_submodule(&list
, deinit_submodule_cb
, &info
);
1515 module_list_release(&list
);
1516 clear_pathspec(&pathspec
);
1520 struct module_clone_data
{
1526 struct list_objects_filter_options
*filter_options
;
1527 unsigned int quiet
: 1;
1528 unsigned int progress
: 1;
1529 unsigned int dissociate
: 1;
1530 unsigned int require_init
: 1;
1533 #define MODULE_CLONE_DATA_INIT { \
1534 .single_branch = -1, \
1537 struct submodule_alternate_setup
{
1538 const char *submodule_name
;
1539 enum SUBMODULE_ALTERNATE_ERROR_MODE
{
1540 SUBMODULE_ALTERNATE_ERROR_DIE
,
1541 SUBMODULE_ALTERNATE_ERROR_INFO
,
1542 SUBMODULE_ALTERNATE_ERROR_IGNORE
1544 struct string_list
*reference
;
1546 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1547 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1550 static const char alternate_error_advice
[] = N_(
1551 "An alternate computed from a superproject's alternate is invalid.\n"
1552 "To allow Git to clone without an alternate in such a case, set\n"
1553 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1554 "'--reference-if-able' instead of '--reference'."
1557 static int add_possible_reference_from_superproject(
1558 struct object_directory
*odb
, void *sas_cb
)
1560 struct submodule_alternate_setup
*sas
= sas_cb
;
1564 * If the alternate object store is another repository, try the
1565 * standard layout with .git/(modules/<name>)+/objects
1567 if (strip_suffix(odb
->path
, "/objects", &len
)) {
1568 struct repository alternate
;
1570 struct strbuf sb
= STRBUF_INIT
;
1571 struct strbuf err
= STRBUF_INIT
;
1572 strbuf_add(&sb
, odb
->path
, len
);
1574 if (repo_init(&alternate
, sb
.buf
, NULL
) < 0)
1575 die(_("could not get a repository handle for gitdir '%s'"),
1579 * We need to end the new path with '/' to mark it as a dir,
1580 * otherwise a submodule name containing '/' will be broken
1581 * as the last part of a missing submodule reference would
1582 * be taken as a file name.
1585 submodule_name_to_gitdir(&sb
, &alternate
, sas
->submodule_name
);
1586 strbuf_addch(&sb
, '/');
1587 repo_clear(&alternate
);
1589 sm_alternate
= compute_alternate_path(sb
.buf
, &err
);
1591 char *p
= strbuf_detach(&sb
, NULL
);
1593 string_list_append(sas
->reference
, p
)->util
= p
;
1596 switch (sas
->error_mode
) {
1597 case SUBMODULE_ALTERNATE_ERROR_DIE
:
1598 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE
))
1599 advise(_(alternate_error_advice
));
1600 die(_("submodule '%s' cannot add alternate: %s"),
1601 sas
->submodule_name
, err
.buf
);
1602 case SUBMODULE_ALTERNATE_ERROR_INFO
:
1603 fprintf_ln(stderr
, _("submodule '%s' cannot add alternate: %s"),
1604 sas
->submodule_name
, err
.buf
);
1605 case SUBMODULE_ALTERNATE_ERROR_IGNORE
:
1609 strbuf_release(&sb
);
1615 static void prepare_possible_alternates(const char *sm_name
,
1616 struct string_list
*reference
)
1618 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1619 struct submodule_alternate_setup sas
= SUBMODULE_ALTERNATE_SETUP_INIT
;
1621 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1625 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1627 if (!error_strategy
)
1628 error_strategy
= xstrdup("die");
1630 sas
.submodule_name
= sm_name
;
1631 sas
.reference
= reference
;
1632 if (!strcmp(error_strategy
, "die"))
1633 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_DIE
;
1634 else if (!strcmp(error_strategy
, "info"))
1635 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_INFO
;
1636 else if (!strcmp(error_strategy
, "ignore"))
1637 sas
.error_mode
= SUBMODULE_ALTERNATE_ERROR_IGNORE
;
1639 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy
);
1641 if (!strcmp(sm_alternate
, "superproject"))
1642 foreach_alt_odb(add_possible_reference_from_superproject
, &sas
);
1643 else if (!strcmp(sm_alternate
, "no"))
1646 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate
);
1649 free(error_strategy
);
1652 static char *clone_submodule_sm_gitdir(const char *name
)
1654 struct strbuf sb
= STRBUF_INIT
;
1657 submodule_name_to_gitdir(&sb
, the_repository
, name
);
1658 sm_gitdir
= absolute_pathdup(sb
.buf
);
1659 strbuf_release(&sb
);
1664 static int clone_submodule(const struct module_clone_data
*clone_data
,
1665 struct string_list
*reference
)
1668 char *sm_gitdir
= clone_submodule_sm_gitdir(clone_data
->name
);
1669 char *sm_alternate
= NULL
, *error_strategy
= NULL
;
1670 struct child_process cp
= CHILD_PROCESS_INIT
;
1671 const char *clone_data_path
= clone_data
->path
;
1672 char *to_free
= NULL
;
1674 if (!is_absolute_path(clone_data
->path
))
1675 clone_data_path
= to_free
= xstrfmt("%s/%s", get_git_work_tree(),
1678 if (validate_submodule_git_dir(sm_gitdir
, clone_data
->name
) < 0)
1679 die(_("refusing to create/use '%s' in another submodule's "
1680 "git dir"), sm_gitdir
);
1682 if (!file_exists(sm_gitdir
)) {
1683 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
1684 die(_("could not create directory '%s'"), sm_gitdir
);
1686 prepare_possible_alternates(clone_data
->name
, reference
);
1688 strvec_push(&cp
.args
, "clone");
1689 strvec_push(&cp
.args
, "--no-checkout");
1690 if (clone_data
->quiet
)
1691 strvec_push(&cp
.args
, "--quiet");
1692 if (clone_data
->progress
)
1693 strvec_push(&cp
.args
, "--progress");
1694 if (clone_data
->depth
&& *(clone_data
->depth
))
1695 strvec_pushl(&cp
.args
, "--depth", clone_data
->depth
, NULL
);
1696 if (reference
->nr
) {
1697 struct string_list_item
*item
;
1699 for_each_string_list_item(item
, reference
)
1700 strvec_pushl(&cp
.args
, "--reference",
1701 item
->string
, NULL
);
1703 if (clone_data
->dissociate
)
1704 strvec_push(&cp
.args
, "--dissociate");
1705 if (sm_gitdir
&& *sm_gitdir
)
1706 strvec_pushl(&cp
.args
, "--separate-git-dir", sm_gitdir
, NULL
);
1707 if (clone_data
->filter_options
&& clone_data
->filter_options
->choice
)
1708 strvec_pushf(&cp
.args
, "--filter=%s",
1709 expand_list_objects_filter_spec(
1710 clone_data
->filter_options
));
1711 if (clone_data
->single_branch
>= 0)
1712 strvec_push(&cp
.args
, clone_data
->single_branch
?
1714 "--no-single-branch");
1716 strvec_push(&cp
.args
, "--");
1717 strvec_push(&cp
.args
, clone_data
->url
);
1718 strvec_push(&cp
.args
, clone_data_path
);
1721 prepare_submodule_repo_env(&cp
.env
);
1724 if(run_command(&cp
))
1725 die(_("clone of '%s' into submodule path '%s' failed"),
1726 clone_data
->url
, clone_data_path
);
1730 if (clone_data
->require_init
&& !access(clone_data_path
, X_OK
) &&
1731 !is_empty_dir(clone_data_path
))
1732 die(_("directory not empty: '%s'"), clone_data_path
);
1733 if (safe_create_leading_directories_const(clone_data_path
) < 0)
1734 die(_("could not create directory '%s'"), clone_data_path
);
1735 path
= xstrfmt("%s/index", sm_gitdir
);
1736 unlink_or_warn(path
);
1740 connect_work_tree_and_git_dir(clone_data_path
, sm_gitdir
, 0);
1742 p
= git_pathdup_submodule(clone_data_path
, "config");
1744 die(_("could not get submodule directory for '%s'"), clone_data_path
);
1746 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1747 git_config_get_string("submodule.alternateLocation", &sm_alternate
);
1749 git_config_set_in_file(p
, "submodule.alternateLocation",
1751 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy
);
1753 git_config_set_in_file(p
, "submodule.alternateErrorStrategy",
1757 free(error_strategy
);
1765 static int module_clone(int argc
, const char **argv
, const char *prefix
)
1767 int dissociate
= 0, quiet
= 0, progress
= 0, require_init
= 0;
1768 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
1769 struct string_list reference
= STRING_LIST_INIT_NODUP
;
1770 struct list_objects_filter_options filter_options
=
1771 LIST_OBJECTS_FILTER_INIT
;
1773 struct option module_clone_options
[] = {
1774 OPT_STRING(0, "prefix", &clone_data
.prefix
,
1776 N_("alternative anchor for relative paths")),
1777 OPT_STRING(0, "path", &clone_data
.path
,
1779 N_("where the new submodule will be cloned to")),
1780 OPT_STRING(0, "name", &clone_data
.name
,
1782 N_("name of the new submodule")),
1783 OPT_STRING(0, "url", &clone_data
.url
,
1785 N_("url where to clone the submodule from")),
1786 OPT_STRING_LIST(0, "reference", &reference
,
1788 N_("reference repository")),
1789 OPT_BOOL(0, "dissociate", &dissociate
,
1790 N_("use --reference only while cloning")),
1791 OPT_STRING(0, "depth", &clone_data
.depth
,
1793 N_("depth for shallow clones")),
1794 OPT__QUIET(&quiet
, "suppress output for cloning a submodule"),
1795 OPT_BOOL(0, "progress", &progress
,
1796 N_("force cloning progress")),
1797 OPT_BOOL(0, "require-init", &require_init
,
1798 N_("disallow cloning into non-empty directory")),
1799 OPT_BOOL(0, "single-branch", &clone_data
.single_branch
,
1800 N_("clone only one branch, HEAD or --branch")),
1801 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
1804 const char *const git_submodule_helper_usage
[] = {
1805 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1806 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1807 "[--single-branch] [--filter <filter-spec>] "
1808 "--url <url> --path <path>"),
1812 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
1813 git_submodule_helper_usage
, 0);
1815 clone_data
.dissociate
= !!dissociate
;
1816 clone_data
.quiet
= !!quiet
;
1817 clone_data
.progress
= !!progress
;
1818 clone_data
.require_init
= !!require_init
;
1819 clone_data
.filter_options
= &filter_options
;
1821 if (argc
|| !clone_data
.url
|| !clone_data
.path
|| !*(clone_data
.path
))
1822 usage_with_options(git_submodule_helper_usage
,
1823 module_clone_options
);
1825 clone_submodule(&clone_data
, &reference
);
1826 list_objects_filter_release(&filter_options
);
1827 string_list_clear(&reference
, 1);
1831 static int determine_submodule_update_strategy(struct repository
*r
,
1834 enum submodule_update_type update
,
1835 struct submodule_update_strategy
*out
)
1837 const struct submodule
*sub
= submodule_from_path(r
, null_oid(), path
);
1842 key
= xstrfmt("submodule.%s.update", sub
->name
);
1846 } else if (!repo_config_get_string_tmp(r
, key
, &val
)) {
1847 if (parse_submodule_update_strategy(val
, out
) < 0) {
1848 ret
= die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1852 } else if (sub
->update_strategy
.type
!= SM_UPDATE_UNSPECIFIED
) {
1853 if (sub
->update_strategy
.type
== SM_UPDATE_COMMAND
)
1854 BUG("how did we read update = !command from .gitmodules?");
1855 out
->type
= sub
->update_strategy
.type
;
1856 out
->command
= sub
->update_strategy
.command
;
1858 out
->type
= SM_UPDATE_CHECKOUT
;
1861 (out
->type
== SM_UPDATE_MERGE
||
1862 out
->type
== SM_UPDATE_REBASE
||
1863 out
->type
== SM_UPDATE_NONE
))
1864 out
->type
= SM_UPDATE_CHECKOUT
;
1872 struct update_clone_data
{
1873 const struct submodule
*sub
;
1874 struct object_id oid
;
1875 unsigned just_cloned
;
1878 struct submodule_update_clone
{
1879 /* index into 'update_data.list', the list of submodules to look into for cloning */
1882 /* configuration parameters which are passed on to the children */
1883 const struct update_data
*update_data
;
1885 /* to be consumed by update_submodule() */
1886 struct update_clone_data
*update_clone
;
1887 int update_clone_nr
; int update_clone_alloc
;
1889 /* If we want to stop as fast as possible and return an error */
1890 unsigned quickstop
: 1;
1892 /* failed clones to be retried again */
1893 const struct cache_entry
**failed_clones
;
1894 int failed_clones_nr
, failed_clones_alloc
;
1896 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1898 static void submodule_update_clone_release(struct submodule_update_clone
*suc
)
1900 free(suc
->update_clone
);
1901 free(suc
->failed_clones
);
1904 struct update_data
{
1906 const char *super_prefix
;
1908 enum submodule_update_type update_default
;
1909 struct object_id suboid
;
1910 struct string_list references
;
1911 struct submodule_update_strategy update_strategy
;
1912 struct list_objects_filter_options
*filter_options
;
1913 struct module_list list
;
1917 int recommend_shallow
;
1918 unsigned int require_init
;
1921 unsigned int nofetch
;
1922 unsigned int remote
;
1923 unsigned int progress
;
1924 unsigned int dissociate
;
1926 unsigned int warn_if_uninitialized
;
1927 unsigned int recursive
;
1929 /* copied over from update_clone_data */
1930 struct object_id oid
;
1931 unsigned int just_cloned
;
1932 const char *sm_path
;
1934 #define UPDATE_DATA_INIT { \
1935 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1936 .list = MODULE_LIST_INIT, \
1937 .recommend_shallow = -1, \
1938 .references = STRING_LIST_INIT_DUP, \
1939 .single_branch = -1, \
1943 static void update_data_release(struct update_data
*ud
)
1945 free(ud
->displaypath
);
1946 module_list_release(&ud
->list
);
1949 static void next_submodule_warn_missing(struct submodule_update_clone
*suc
,
1950 struct strbuf
*out
, const char *displaypath
)
1953 * Only mention uninitialized submodules when their
1954 * paths have been specified.
1956 if (suc
->update_data
->warn_if_uninitialized
) {
1958 _("Submodule path '%s' not initialized"),
1960 strbuf_addch(out
, '\n');
1962 _("Maybe you want to use 'update --init'?"));
1963 strbuf_addch(out
, '\n');
1968 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1969 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1971 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
1972 struct child_process
*child
,
1973 struct submodule_update_clone
*suc
,
1976 const struct submodule
*sub
= NULL
;
1977 const char *url
= NULL
;
1978 const char *update_string
;
1979 enum submodule_update_type update_type
;
1981 const struct update_data
*ud
= suc
->update_data
;
1982 char *displaypath
= get_submodule_displaypath(ce
->name
, ud
->prefix
,
1984 struct strbuf sb
= STRBUF_INIT
;
1985 int needs_cloning
= 0;
1986 int need_free_url
= 0;
1989 strbuf_addf(out
, _("Skipping unmerged submodule %s"), displaypath
);
1990 strbuf_addch(out
, '\n');
1994 sub
= submodule_from_path(the_repository
, null_oid(), ce
->name
);
1997 next_submodule_warn_missing(suc
, out
, displaypath
);
2001 key
= xstrfmt("submodule.%s.update", sub
->name
);
2002 if (!repo_config_get_string_tmp(the_repository
, key
, &update_string
)) {
2003 update_type
= parse_submodule_update_type(update_string
);
2005 update_type
= sub
->update_strategy
.type
;
2009 if (suc
->update_data
->update_strategy
.type
== SM_UPDATE_NONE
2010 || (suc
->update_data
->update_strategy
.type
== SM_UPDATE_UNSPECIFIED
2011 && update_type
== SM_UPDATE_NONE
)) {
2012 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
2013 strbuf_addch(out
, '\n');
2017 /* Check if the submodule has been initialized. */
2018 if (!is_submodule_active(the_repository
, ce
->name
)) {
2019 next_submodule_warn_missing(suc
, out
, displaypath
);
2024 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
2025 if (repo_config_get_string_tmp(the_repository
, sb
.buf
, &url
)) {
2026 if (sub
->url
&& (starts_with_dot_slash(sub
->url
) ||
2027 starts_with_dot_dot_slash(sub
->url
))) {
2028 url
= resolve_relative_url(sub
->url
, NULL
, 0);
2035 die(_("cannot clone submodule '%s' without a URL"), sub
->name
);
2038 strbuf_addf(&sb
, "%s/.git", ce
->name
);
2039 needs_cloning
= !file_exists(sb
.buf
);
2041 ALLOC_GROW(suc
->update_clone
, suc
->update_clone_nr
+ 1,
2042 suc
->update_clone_alloc
);
2043 oidcpy(&suc
->update_clone
[suc
->update_clone_nr
].oid
, &ce
->oid
);
2044 suc
->update_clone
[suc
->update_clone_nr
].just_cloned
= needs_cloning
;
2045 suc
->update_clone
[suc
->update_clone_nr
].sub
= sub
;
2046 suc
->update_clone_nr
++;
2052 child
->no_stdin
= 1;
2053 child
->stdout_to_stderr
= 1;
2055 strvec_push(&child
->args
, "submodule--helper");
2056 strvec_push(&child
->args
, "clone");
2057 if (suc
->update_data
->progress
)
2058 strvec_push(&child
->args
, "--progress");
2059 if (suc
->update_data
->quiet
)
2060 strvec_push(&child
->args
, "--quiet");
2061 if (suc
->update_data
->prefix
)
2062 strvec_pushl(&child
->args
, "--prefix", suc
->update_data
->prefix
, NULL
);
2063 if (suc
->update_data
->recommend_shallow
&& sub
->recommend_shallow
== 1)
2064 strvec_push(&child
->args
, "--depth=1");
2065 else if (suc
->update_data
->depth
)
2066 strvec_pushf(&child
->args
, "--depth=%d", suc
->update_data
->depth
);
2067 if (suc
->update_data
->filter_options
&& suc
->update_data
->filter_options
->choice
)
2068 strvec_pushf(&child
->args
, "--filter=%s",
2069 expand_list_objects_filter_spec(suc
->update_data
->filter_options
));
2070 if (suc
->update_data
->require_init
)
2071 strvec_push(&child
->args
, "--require-init");
2072 strvec_pushl(&child
->args
, "--path", sub
->path
, NULL
);
2073 strvec_pushl(&child
->args
, "--name", sub
->name
, NULL
);
2074 strvec_pushl(&child
->args
, "--url", url
, NULL
);
2075 if (suc
->update_data
->references
.nr
) {
2076 struct string_list_item
*item
;
2078 for_each_string_list_item(item
, &suc
->update_data
->references
)
2079 strvec_pushl(&child
->args
, "--reference", item
->string
, NULL
);
2081 if (suc
->update_data
->dissociate
)
2082 strvec_push(&child
->args
, "--dissociate");
2083 if (suc
->update_data
->single_branch
>= 0)
2084 strvec_push(&child
->args
, suc
->update_data
->single_branch
?
2086 "--no-single-branch");
2090 strbuf_release(&sb
);
2094 return needs_cloning
;
2097 static int update_clone_get_next_task(struct child_process
*child
,
2102 struct submodule_update_clone
*suc
= suc_cb
;
2103 const struct cache_entry
*ce
;
2106 for (; suc
->current
< suc
->update_data
->list
.nr
; suc
->current
++) {
2107 ce
= suc
->update_data
->list
.entries
[suc
->current
];
2108 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2109 int *p
= xmalloc(sizeof(*p
));
2119 * The loop above tried cloning each submodule once, now try the
2120 * stragglers again, which we can imagine as an extension of the
2123 index
= suc
->current
- suc
->update_data
->list
.nr
;
2124 if (index
< suc
->failed_clones_nr
) {
2127 ce
= suc
->failed_clones
[index
];
2128 if (!prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
2130 strbuf_addstr(err
, "BUG: submodule considered for "
2131 "cloning, doesn't need cloning "
2135 p
= xmalloc(sizeof(*p
));
2145 static int update_clone_start_failure(struct strbuf
*err UNUSED
,
2147 void *idx_task_cb UNUSED
)
2149 struct submodule_update_clone
*suc
= suc_cb
;
2155 static int update_clone_task_finished(int result
,
2160 const struct cache_entry
*ce
;
2161 struct submodule_update_clone
*suc
= suc_cb
;
2162 int *idxP
= idx_task_cb
;
2170 if (idx
< suc
->update_data
->list
.nr
) {
2171 ce
= suc
->update_data
->list
.entries
[idx
];
2172 strbuf_addf(err
, _("Failed to clone '%s'. Retry scheduled"),
2174 strbuf_addch(err
, '\n');
2175 ALLOC_GROW(suc
->failed_clones
,
2176 suc
->failed_clones_nr
+ 1,
2177 suc
->failed_clones_alloc
);
2178 suc
->failed_clones
[suc
->failed_clones_nr
++] = ce
;
2181 idx
-= suc
->update_data
->list
.nr
;
2182 ce
= suc
->failed_clones
[idx
];
2183 strbuf_addf(err
, _("Failed to clone '%s' a second time, aborting"),
2185 strbuf_addch(err
, '\n');
2193 static int git_update_clone_config(const char *var
, const char *value
,
2194 const struct config_context
*ctx
,
2199 if (!strcmp(var
, "submodule.fetchjobs"))
2200 *max_jobs
= parse_submodule_fetchjobs(var
, value
, ctx
->kvi
);
2204 static int is_tip_reachable(const char *path
, const struct object_id
*oid
)
2206 struct child_process cp
= CHILD_PROCESS_INIT
;
2207 struct strbuf rev
= STRBUF_INIT
;
2208 char *hex
= oid_to_hex(oid
);
2213 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", hex
, "--not", "--all", NULL
);
2215 prepare_submodule_repo_env(&cp
.env
);
2217 if (capture_command(&cp
, &rev
, GIT_MAX_HEXSZ
+ 1) || rev
.len
)
2223 static int fetch_in_submodule(const char *module_path
, int depth
, int quiet
,
2224 const struct object_id
*oid
)
2226 struct child_process cp
= CHILD_PROCESS_INIT
;
2228 prepare_submodule_repo_env(&cp
.env
);
2230 cp
.dir
= module_path
;
2232 strvec_push(&cp
.args
, "fetch");
2234 strvec_push(&cp
.args
, "--quiet");
2236 strvec_pushf(&cp
.args
, "--depth=%d", depth
);
2238 char *hex
= oid_to_hex(oid
);
2239 char *remote
= get_default_remote();
2241 strvec_pushl(&cp
.args
, remote
, hex
, NULL
);
2245 return run_command(&cp
);
2248 static int run_update_command(const struct update_data
*ud
, int subforce
)
2250 struct child_process cp
= CHILD_PROCESS_INIT
;
2251 char *oid
= oid_to_hex(&ud
->oid
);
2254 switch (ud
->update_strategy
.type
) {
2255 case SM_UPDATE_CHECKOUT
:
2257 strvec_pushl(&cp
.args
, "checkout", "-q", NULL
);
2259 strvec_push(&cp
.args
, "-f");
2261 case SM_UPDATE_REBASE
:
2263 strvec_push(&cp
.args
, "rebase");
2265 strvec_push(&cp
.args
, "--quiet");
2267 case SM_UPDATE_MERGE
:
2269 strvec_push(&cp
.args
, "merge");
2271 strvec_push(&cp
.args
, "--quiet");
2273 case SM_UPDATE_COMMAND
:
2275 strvec_push(&cp
.args
, ud
->update_strategy
.command
);
2278 BUG("unexpected update strategy type: %d",
2279 ud
->update_strategy
.type
);
2281 strvec_push(&cp
.args
, oid
);
2283 cp
.dir
= ud
->sm_path
;
2284 prepare_submodule_repo_env(&cp
.env
);
2285 if ((ret
= run_command(&cp
))) {
2286 switch (ud
->update_strategy
.type
) {
2287 case SM_UPDATE_CHECKOUT
:
2288 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2289 oid
, ud
->displaypath
);
2290 /* No "ret" assignment, use "git checkout"'s */
2292 case SM_UPDATE_REBASE
:
2293 ret
= die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2294 oid
, ud
->displaypath
);
2296 case SM_UPDATE_MERGE
:
2297 ret
= die_message(_("Unable to merge '%s' in submodule path '%s'"),
2298 oid
, ud
->displaypath
);
2300 case SM_UPDATE_COMMAND
:
2301 ret
= die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2302 ud
->update_strategy
.command
, oid
, ud
->displaypath
);
2305 BUG("unexpected update strategy type: %d",
2306 ud
->update_strategy
.type
);
2315 switch (ud
->update_strategy
.type
) {
2316 case SM_UPDATE_CHECKOUT
:
2317 printf(_("Submodule path '%s': checked out '%s'\n"),
2318 ud
->displaypath
, oid
);
2320 case SM_UPDATE_REBASE
:
2321 printf(_("Submodule path '%s': rebased into '%s'\n"),
2322 ud
->displaypath
, oid
);
2324 case SM_UPDATE_MERGE
:
2325 printf(_("Submodule path '%s': merged in '%s'\n"),
2326 ud
->displaypath
, oid
);
2328 case SM_UPDATE_COMMAND
:
2329 printf(_("Submodule path '%s': '%s %s'\n"),
2330 ud
->displaypath
, ud
->update_strategy
.command
, oid
);
2333 BUG("unexpected update strategy type: %d",
2334 ud
->update_strategy
.type
);
2340 static int run_update_procedure(const struct update_data
*ud
)
2342 int subforce
= is_null_oid(&ud
->suboid
) || ud
->force
;
2346 * Run fetch only if `oid` isn't present or it
2347 * is not reachable from a ref.
2349 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2350 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, NULL
) &&
2353 _("Unable to fetch in submodule path '%s'; "
2354 "trying to directly fetch %s:"),
2355 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2357 * Now we tried the usual fetch, but `oid` may
2358 * not be reachable from any of the refs.
2360 if (!is_tip_reachable(ud
->sm_path
, &ud
->oid
) &&
2361 fetch_in_submodule(ud
->sm_path
, ud
->depth
, ud
->quiet
, &ud
->oid
))
2362 return die_message(_("Fetched in submodule path '%s', but it did not "
2363 "contain %s. Direct fetching of that commit failed."),
2364 ud
->displaypath
, oid_to_hex(&ud
->oid
));
2367 return run_update_command(ud
, subforce
);
2370 static int remote_submodule_branch(const char *path
, const char **branch
)
2372 const struct submodule
*sub
;
2376 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2378 return die_message(_("could not initialize submodule at path '%s'"),
2381 key
= xstrfmt("submodule.%s.branch", sub
->name
);
2382 if (repo_config_get_string_tmp(the_repository
, key
, branch
))
2383 *branch
= sub
->branch
;
2391 if (!strcmp(*branch
, ".")) {
2392 const char *refname
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
2395 return die_message(_("No such ref: %s"), "HEAD");
2398 if (!strcmp(refname
, "HEAD"))
2399 return die_message(_("Submodule (%s) branch configured to inherit "
2400 "branch from superproject, but the superproject "
2401 "is not on any branch"), sub
->name
);
2403 if (!skip_prefix(refname
, "refs/heads/", &refname
))
2404 return die_message(_("Expecting a full ref name, got %s"),
2411 /* Our "branch" is coming from repo_config_get_string_tmp() */
2415 static int ensure_core_worktree(const char *path
)
2418 struct repository subrepo
;
2420 if (repo_submodule_init(&subrepo
, the_repository
, path
, null_oid()))
2421 return die_message(_("could not get a repository handle for submodule '%s'"),
2424 if (!repo_config_get_string_tmp(&subrepo
, "core.worktree", &cw
)) {
2425 char *cfg_file
, *abs_path
;
2426 const char *rel_path
;
2427 struct strbuf sb
= STRBUF_INIT
;
2429 cfg_file
= repo_git_path(&subrepo
, "config");
2431 abs_path
= absolute_pathdup(path
);
2432 rel_path
= relative_path(abs_path
, subrepo
.gitdir
, &sb
);
2434 git_config_set_in_file(cfg_file
, "core.worktree", rel_path
);
2438 strbuf_release(&sb
);
2441 repo_clear(&subrepo
);
2445 static const char *submodule_update_type_to_label(enum submodule_update_type type
)
2448 case SM_UPDATE_CHECKOUT
:
2450 case SM_UPDATE_MERGE
:
2452 case SM_UPDATE_REBASE
:
2454 case SM_UPDATE_UNSPECIFIED
:
2455 case SM_UPDATE_NONE
:
2456 case SM_UPDATE_COMMAND
:
2459 BUG("unreachable with type %d", type
);
2462 static void update_data_to_args(const struct update_data
*update_data
,
2463 struct strvec
*args
)
2465 enum submodule_update_type update_type
= update_data
->update_default
;
2467 strvec_pushl(args
, "submodule--helper", "update", "--recursive", NULL
);
2468 if (update_data
->displaypath
) {
2469 strvec_push(args
, "--super-prefix");
2470 strvec_pushf(args
, "%s/", update_data
->displaypath
);
2472 strvec_pushf(args
, "--jobs=%d", update_data
->max_jobs
);
2473 if (update_data
->quiet
)
2474 strvec_push(args
, "--quiet");
2475 if (update_data
->force
)
2476 strvec_push(args
, "--force");
2477 if (update_data
->init
)
2478 strvec_push(args
, "--init");
2479 if (update_data
->remote
)
2480 strvec_push(args
, "--remote");
2481 if (update_data
->nofetch
)
2482 strvec_push(args
, "--no-fetch");
2483 if (update_data
->dissociate
)
2484 strvec_push(args
, "--dissociate");
2485 if (update_data
->progress
)
2486 strvec_push(args
, "--progress");
2487 if (update_data
->require_init
)
2488 strvec_push(args
, "--require-init");
2489 if (update_data
->depth
)
2490 strvec_pushf(args
, "--depth=%d", update_data
->depth
);
2491 if (update_type
!= SM_UPDATE_UNSPECIFIED
)
2492 strvec_pushf(args
, "--%s",
2493 submodule_update_type_to_label(update_type
));
2495 if (update_data
->references
.nr
) {
2496 struct string_list_item
*item
;
2498 for_each_string_list_item(item
, &update_data
->references
)
2499 strvec_pushl(args
, "--reference", item
->string
, NULL
);
2501 if (update_data
->filter_options
&& update_data
->filter_options
->choice
)
2502 strvec_pushf(args
, "--filter=%s",
2503 expand_list_objects_filter_spec(
2504 update_data
->filter_options
));
2505 if (update_data
->recommend_shallow
== 0)
2506 strvec_push(args
, "--no-recommend-shallow");
2507 else if (update_data
->recommend_shallow
== 1)
2508 strvec_push(args
, "--recommend-shallow");
2509 if (update_data
->single_branch
>= 0)
2510 strvec_push(args
, update_data
->single_branch
?
2512 "--no-single-branch");
2515 static int update_submodule(struct update_data
*update_data
)
2519 ret
= determine_submodule_update_strategy(the_repository
,
2520 update_data
->just_cloned
,
2521 update_data
->sm_path
,
2522 update_data
->update_default
,
2523 &update_data
->update_strategy
);
2527 if (update_data
->just_cloned
)
2528 oidcpy(&update_data
->suboid
, null_oid());
2529 else if (resolve_gitlink_ref(update_data
->sm_path
, "HEAD", &update_data
->suboid
))
2530 return die_message(_("Unable to find current revision in submodule path '%s'"),
2531 update_data
->displaypath
);
2533 if (update_data
->remote
) {
2539 code
= get_default_remote_submodule(update_data
->sm_path
, &remote_name
);
2542 code
= remote_submodule_branch(update_data
->sm_path
, &branch
);
2545 remote_ref
= xstrfmt("refs/remotes/%s/%s", remote_name
, branch
);
2549 if (!update_data
->nofetch
) {
2550 if (fetch_in_submodule(update_data
->sm_path
, update_data
->depth
,
2552 return die_message(_("Unable to fetch in submodule path '%s'"),
2553 update_data
->sm_path
);
2556 if (resolve_gitlink_ref(update_data
->sm_path
, remote_ref
, &update_data
->oid
))
2557 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2558 remote_ref
, update_data
->sm_path
);
2563 if (!oideq(&update_data
->oid
, &update_data
->suboid
) || update_data
->force
) {
2564 ret
= run_update_procedure(update_data
);
2569 if (update_data
->recursive
) {
2570 struct child_process cp
= CHILD_PROCESS_INIT
;
2571 struct update_data next
= *update_data
;
2574 oidcpy(&next
.oid
, null_oid());
2575 oidcpy(&next
.suboid
, null_oid());
2577 cp
.dir
= update_data
->sm_path
;
2579 prepare_submodule_repo_env(&cp
.env
);
2580 update_data_to_args(&next
, &cp
.args
);
2582 ret
= run_command(&cp
);
2584 die_message(_("Failed to recurse into submodule path '%s'"),
2585 update_data
->displaypath
);
2592 static int update_submodules(struct update_data
*update_data
)
2595 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
2596 const struct run_process_parallel_opts opts
= {
2597 .tr2_category
= "submodule",
2598 .tr2_label
= "parallel/update",
2600 .processes
= update_data
->max_jobs
,
2602 .get_next_task
= update_clone_get_next_task
,
2603 .start_failure
= update_clone_start_failure
,
2604 .task_finished
= update_clone_task_finished
,
2608 suc
.update_data
= update_data
;
2609 run_processes_parallel(&opts
);
2612 * We saved the output and put it out all at once now.
2614 * - the listener does not have to interleave their (checkout)
2615 * work with our fetching. The writes involved in a
2616 * checkout involve more straightforward sequential I/O.
2617 * - the listener can avoid doing any work if fetching failed.
2619 if (suc
.quickstop
) {
2624 for (i
= 0; i
< suc
.update_clone_nr
; i
++) {
2625 struct update_clone_data ucd
= suc
.update_clone
[i
];
2628 oidcpy(&update_data
->oid
, &ucd
.oid
);
2629 update_data
->just_cloned
= ucd
.just_cloned
;
2630 update_data
->sm_path
= ucd
.sub
->path
;
2632 code
= ensure_core_worktree(update_data
->sm_path
);
2636 update_data
->displaypath
= get_submodule_displaypath(
2637 update_data
->sm_path
, update_data
->prefix
,
2638 update_data
->super_prefix
);
2639 code
= update_submodule(update_data
);
2640 FREE_AND_NULL(update_data
->displaypath
);
2650 submodule_update_clone_release(&suc
);
2651 string_list_clear(&update_data
->references
, 0);
2655 static int module_update(int argc
, const char **argv
, const char *prefix
)
2657 struct pathspec pathspec
= { 0 };
2658 struct pathspec pathspec2
= { 0 };
2659 struct update_data opt
= UPDATE_DATA_INIT
;
2660 struct list_objects_filter_options filter_options
=
2661 LIST_OBJECTS_FILTER_INIT
;
2663 struct option module_update_options
[] = {
2664 OPT__SUPER_PREFIX(&opt
.super_prefix
),
2665 OPT__FORCE(&opt
.force
, N_("force checkout updates"), 0),
2666 OPT_BOOL(0, "init", &opt
.init
,
2667 N_("initialize uninitialized submodules before update")),
2668 OPT_BOOL(0, "remote", &opt
.remote
,
2669 N_("use SHA-1 of submodule's remote tracking branch")),
2670 OPT_BOOL(0, "recursive", &opt
.recursive
,
2671 N_("traverse submodules recursively")),
2672 OPT_BOOL('N', "no-fetch", &opt
.nofetch
,
2673 N_("don't fetch new objects from the remote site")),
2674 OPT_SET_INT(0, "checkout", &opt
.update_default
,
2675 N_("use the 'checkout' update strategy (default)"),
2676 SM_UPDATE_CHECKOUT
),
2677 OPT_SET_INT('m', "merge", &opt
.update_default
,
2678 N_("use the 'merge' update strategy"),
2680 OPT_SET_INT('r', "rebase", &opt
.update_default
,
2681 N_("use the 'rebase' update strategy"),
2683 OPT_STRING_LIST(0, "reference", &opt
.references
, N_("repo"),
2684 N_("reference repository")),
2685 OPT_BOOL(0, "dissociate", &opt
.dissociate
,
2686 N_("use --reference only while cloning")),
2687 OPT_INTEGER(0, "depth", &opt
.depth
,
2688 N_("create a shallow clone truncated to the "
2689 "specified number of revisions")),
2690 OPT_INTEGER('j', "jobs", &opt
.max_jobs
,
2691 N_("parallel jobs")),
2692 OPT_BOOL(0, "recommend-shallow", &opt
.recommend_shallow
,
2693 N_("whether the initial clone should follow the shallow recommendation")),
2694 OPT__QUIET(&opt
.quiet
, N_("don't print cloning progress")),
2695 OPT_BOOL(0, "progress", &opt
.progress
,
2696 N_("force cloning progress")),
2697 OPT_BOOL(0, "require-init", &opt
.require_init
,
2698 N_("disallow cloning into non-empty directory, implies --init")),
2699 OPT_BOOL(0, "single-branch", &opt
.single_branch
,
2700 N_("clone only one branch, HEAD or --branch")),
2701 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
2704 const char *const git_submodule_helper_usage
[] = {
2705 N_("git submodule [--quiet] update"
2706 " [--init [--filter=<filter-spec>]] [--remote]"
2707 " [-N|--no-fetch] [-f|--force]"
2708 " [--checkout|--merge|--rebase]"
2709 " [--[no-]recommend-shallow] [--reference <repository>]"
2710 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2714 update_clone_config_from_gitmodules(&opt
.max_jobs
);
2715 git_config(git_update_clone_config
, &opt
.max_jobs
);
2717 argc
= parse_options(argc
, argv
, prefix
, module_update_options
,
2718 git_submodule_helper_usage
, 0);
2720 if (opt
.require_init
)
2723 if (filter_options
.choice
&& !opt
.init
) {
2724 usage_with_options(git_submodule_helper_usage
,
2725 module_update_options
);
2728 opt
.filter_options
= &filter_options
;
2729 opt
.prefix
= prefix
;
2731 if (opt
.update_default
)
2732 opt
.update_strategy
.type
= opt
.update_default
;
2734 if (module_list_compute(argv
, prefix
, &pathspec
, &opt
.list
) < 0) {
2740 opt
.warn_if_uninitialized
= 1;
2743 struct module_list list
= MODULE_LIST_INIT
;
2744 struct init_cb info
= INIT_CB_INIT
;
2746 if (module_list_compute(argv
, opt
.prefix
,
2747 &pathspec2
, &list
) < 0) {
2748 module_list_release(&list
);
2754 * If there are no path args and submodule.active is set then,
2755 * by default, only initialize 'active' modules.
2757 if (!argc
&& !git_config_get("submodule.active"))
2758 module_list_active(&list
);
2760 info
.prefix
= opt
.prefix
;
2761 info
.super_prefix
= opt
.super_prefix
;
2763 info
.flags
|= OPT_QUIET
;
2765 for_each_listed_submodule(&list
, init_submodule_cb
, &info
);
2766 module_list_release(&list
);
2769 ret
= update_submodules(&opt
);
2771 update_data_release(&opt
);
2772 list_objects_filter_release(&filter_options
);
2773 clear_pathspec(&pathspec
);
2774 clear_pathspec(&pathspec2
);
2778 static int push_check(int argc
, const char **argv
, const char *prefix UNUSED
)
2780 struct remote
*remote
;
2781 const char *superproject_head
;
2783 int detached_head
= 0;
2784 struct object_id head_oid
;
2787 die("submodule--helper push-check requires at least 2 arguments");
2790 * superproject's resolved head ref.
2791 * if HEAD then the superproject is in a detached head state, otherwise
2792 * it will be the resolved head ref.
2794 superproject_head
= argv
[1];
2797 /* Get the submodule's head ref and determine if it is detached */
2798 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
2800 die(_("Failed to resolve HEAD as a valid ref."));
2801 if (!strcmp(head
, "HEAD"))
2805 * The remote must be configured.
2806 * This is to avoid pushing to the exact same URL as the parent.
2808 remote
= pushremote_get(argv
[1]);
2809 if (!remote
|| remote
->origin
== REMOTE_UNCONFIGURED
)
2810 die("remote '%s' not configured", argv
[1]);
2812 /* Check the refspec */
2815 struct ref
*local_refs
= get_local_heads();
2816 struct refspec refspec
= REFSPEC_INIT_PUSH
;
2818 refspec_appendn(&refspec
, argv
+ 2, argc
- 2);
2820 for (i
= 0; i
< refspec
.nr
; i
++) {
2821 const struct refspec_item
*rs
= &refspec
.items
[i
];
2823 if (rs
->pattern
|| rs
->matching
)
2826 /* LHS must match a single ref */
2827 switch (count_refspec_match(rs
->src
, local_refs
, NULL
)) {
2832 * If LHS matches 'HEAD' then we need to ensure
2833 * that it matches the same named branch
2834 * checked out in the superproject.
2836 if (!strcmp(rs
->src
, "HEAD")) {
2837 if (!detached_head
&&
2838 !strcmp(head
, superproject_head
))
2840 die("HEAD does not match the named branch in the superproject");
2844 die("src refspec '%s' must name a ref",
2848 refspec_clear(&refspec
);
2855 static int absorb_git_dirs(int argc
, const char **argv
, const char *prefix
)
2858 struct pathspec pathspec
= { 0 };
2859 struct module_list list
= MODULE_LIST_INIT
;
2860 const char *super_prefix
= NULL
;
2861 struct option embed_gitdir_options
[] = {
2862 OPT__SUPER_PREFIX(&super_prefix
),
2865 const char *const git_submodule_helper_usage
[] = {
2866 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2871 argc
= parse_options(argc
, argv
, prefix
, embed_gitdir_options
,
2872 git_submodule_helper_usage
, 0);
2874 if (module_list_compute(argv
, prefix
, &pathspec
, &list
) < 0)
2877 for (i
= 0; i
< list
.nr
; i
++)
2878 absorb_git_dir_into_superproject(list
.entries
[i
]->name
,
2883 clear_pathspec(&pathspec
);
2884 module_list_release(&list
);
2888 static int module_set_url(int argc
, const char **argv
, const char *prefix
)
2894 struct option options
[] = {
2895 OPT__QUIET(&quiet
, N_("suppress output for setting url of a submodule")),
2898 const char *const usage
[] = {
2899 N_("git submodule set-url [--quiet] <path> <newurl>"),
2902 const struct submodule
*sub
;
2904 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2906 if (argc
!= 2 || !(path
= argv
[0]) || !(newurl
= argv
[1]))
2907 usage_with_options(usage
, options
);
2909 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2912 die(_("no submodule mapping found in .gitmodules for path '%s'"),
2915 config_name
= xstrfmt("submodule.%s.url", sub
->name
);
2916 ret
= config_set_in_gitmodules_file_gently(config_name
, newurl
);
2919 repo_read_gitmodules(the_repository
, 0);
2920 sync_submodule(sub
->path
, prefix
, NULL
, quiet
? OPT_QUIET
: 0);
2927 static int module_set_branch(int argc
, const char **argv
, const char *prefix
)
2929 int opt_default
= 0, ret
;
2930 const char *opt_branch
= NULL
;
2933 struct option options
[] = {
2935 * We accept the `quiet` option for uniformity across subcommands,
2936 * though there is nothing to make less verbose in this subcommand.
2938 OPT_NOOP_NOARG('q', "quiet"),
2940 OPT_BOOL('d', "default", &opt_default
,
2941 N_("set the default tracking branch to master")),
2942 OPT_STRING('b', "branch", &opt_branch
, N_("branch"),
2943 N_("set the default tracking branch")),
2946 const char *const usage
[] = {
2947 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2948 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2951 const struct submodule
*sub
;
2953 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2955 if (!opt_branch
&& !opt_default
)
2956 die(_("--branch or --default required"));
2958 if (opt_branch
&& opt_default
)
2959 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2961 if (argc
!= 1 || !(path
= argv
[0]))
2962 usage_with_options(usage
, options
);
2964 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2967 die(_("no submodule mapping found in .gitmodules for path '%s'"),
2970 config_name
= xstrfmt("submodule.%s.branch", sub
->name
);
2971 ret
= config_set_in_gitmodules_file_gently(config_name
, opt_branch
);
2977 static int module_create_branch(int argc
, const char **argv
, const char *prefix
)
2979 enum branch_track track
;
2980 int quiet
= 0, force
= 0, reflog
= 0, dry_run
= 0;
2981 struct option options
[] = {
2982 OPT__QUIET(&quiet
, N_("print only error messages")),
2983 OPT__FORCE(&force
, N_("force creation"), 0),
2984 OPT_BOOL(0, "create-reflog", &reflog
,
2985 N_("create the branch's reflog")),
2986 OPT_CALLBACK_F('t', "track", &track
, "(direct|inherit)",
2987 N_("set branch tracking configuration"),
2989 parse_opt_tracking_mode
),
2990 OPT__DRY_RUN(&dry_run
,
2991 N_("show whether the branch would be created")),
2994 const char *const usage
[] = {
2995 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2999 git_config(git_default_config
, NULL
);
3000 track
= git_branch_track
;
3001 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3004 usage_with_options(usage
, options
);
3006 if (!quiet
&& !dry_run
)
3007 printf_ln(_("creating branch '%s'"), argv
[0]);
3009 create_branches_recursively(the_repository
, argv
[0], argv
[1], argv
[2],
3010 force
, reflog
, quiet
, track
, dry_run
);
3017 const char *reference_path
;
3019 const char *sm_name
;
3021 const char *realrepo
;
3023 unsigned int force
: 1;
3024 unsigned int quiet
: 1;
3025 unsigned int progress
: 1;
3026 unsigned int dissociate
: 1;
3028 #define ADD_DATA_INIT { .depth = -1 }
3030 static void append_fetch_remotes(struct strbuf
*msg
, const char *git_dir_path
)
3032 struct child_process cp_remote
= CHILD_PROCESS_INIT
;
3033 struct strbuf sb_remote_out
= STRBUF_INIT
;
3035 cp_remote
.git_cmd
= 1;
3036 strvec_pushf(&cp_remote
.env
,
3037 "GIT_DIR=%s", git_dir_path
);
3038 strvec_push(&cp_remote
.env
, "GIT_WORK_TREE=.");
3039 strvec_pushl(&cp_remote
.args
, "remote", "-v", NULL
);
3040 if (!capture_command(&cp_remote
, &sb_remote_out
, 0)) {
3042 char *line
= sb_remote_out
.buf
;
3044 while ((next_line
= strchr(line
, '\n')) != NULL
) {
3045 size_t len
= next_line
- line
;
3047 if (strip_suffix_mem(line
, &len
, " (fetch)"))
3048 strbuf_addf(msg
, " %.*s\n", (int)len
, line
);
3049 line
= next_line
+ 1;
3053 strbuf_release(&sb_remote_out
);
3056 static int add_submodule(const struct add_data
*add_data
)
3058 char *submod_gitdir_path
;
3059 struct module_clone_data clone_data
= MODULE_CLONE_DATA_INIT
;
3060 struct string_list reference
= STRING_LIST_INIT_NODUP
;
3063 /* perhaps the path already exists and is already a git repo, else clone it */
3064 if (is_directory(add_data
->sm_path
)) {
3065 struct strbuf sm_path
= STRBUF_INIT
;
3066 strbuf_addstr(&sm_path
, add_data
->sm_path
);
3067 submod_gitdir_path
= xstrfmt("%s/.git", add_data
->sm_path
);
3068 if (is_nonbare_repository_dir(&sm_path
))
3069 printf(_("Adding existing repo at '%s' to the index\n"),
3072 die(_("'%s' already exists and is not a valid git repo"),
3074 strbuf_release(&sm_path
);
3075 free(submod_gitdir_path
);
3077 struct child_process cp
= CHILD_PROCESS_INIT
;
3079 submod_gitdir_path
= xstrfmt(".git/modules/%s", add_data
->sm_name
);
3081 if (is_directory(submod_gitdir_path
)) {
3082 if (!add_data
->force
) {
3083 struct strbuf msg
= STRBUF_INIT
;
3086 strbuf_addf(&msg
, _("A git directory for '%s' is found "
3087 "locally with remote(s):\n"),
3090 append_fetch_remotes(&msg
, submod_gitdir_path
);
3091 free(submod_gitdir_path
);
3093 strbuf_addf(&msg
, _("If you want to reuse this local git "
3094 "directory instead of cloning again from\n"
3096 "use the '--force' option. If the local git "
3097 "directory is not the correct repo\n"
3098 "or you are unsure what this means choose "
3099 "another name with the '--name' option."),
3100 add_data
->realrepo
);
3102 die_msg
= strbuf_detach(&msg
, NULL
);
3105 printf(_("Reactivating local git directory for "
3106 "submodule '%s'\n"), add_data
->sm_name
);
3109 free(submod_gitdir_path
);
3111 clone_data
.prefix
= add_data
->prefix
;
3112 clone_data
.path
= add_data
->sm_path
;
3113 clone_data
.name
= add_data
->sm_name
;
3114 clone_data
.url
= add_data
->realrepo
;
3115 clone_data
.quiet
= add_data
->quiet
;
3116 clone_data
.progress
= add_data
->progress
;
3117 if (add_data
->reference_path
) {
3118 char *p
= xstrdup(add_data
->reference_path
);
3120 string_list_append(&reference
, p
)->util
= p
;
3122 clone_data
.dissociate
= add_data
->dissociate
;
3123 if (add_data
->depth
>= 0)
3124 clone_data
.depth
= xstrfmt("%d", add_data
->depth
);
3126 if (clone_submodule(&clone_data
, &reference
))
3129 prepare_submodule_repo_env(&cp
.env
);
3131 cp
.dir
= add_data
->sm_path
;
3133 * NOTE: we only get here if add_data->force is true, so
3134 * passing --force to checkout is reasonable.
3136 strvec_pushl(&cp
.args
, "checkout", "-f", "-q", NULL
);
3138 if (add_data
->branch
) {
3139 strvec_pushl(&cp
.args
, "-B", add_data
->branch
, NULL
);
3140 strvec_pushf(&cp
.args
, "origin/%s", add_data
->branch
);
3143 if (run_command(&cp
))
3144 die(_("unable to checkout submodule '%s'"), add_data
->sm_path
);
3148 string_list_clear(&reference
, 1);
3152 static int config_submodule_in_gitmodules(const char *name
, const char *var
, const char *value
)
3157 if (!is_writing_gitmodules_ok())
3158 die(_("please make sure that the .gitmodules file is in the working tree"));
3160 key
= xstrfmt("submodule.%s.%s", name
, var
);
3161 ret
= config_set_in_gitmodules_file_gently(key
, value
);
3167 static void configure_added_submodule(struct add_data
*add_data
)
3170 struct child_process add_submod
= CHILD_PROCESS_INIT
;
3171 struct child_process add_gitmodules
= CHILD_PROCESS_INIT
;
3173 key
= xstrfmt("submodule.%s.url", add_data
->sm_name
);
3174 git_config_set_gently(key
, add_data
->realrepo
);
3177 add_submod
.git_cmd
= 1;
3178 strvec_pushl(&add_submod
.args
, "add",
3179 "--no-warn-embedded-repo", NULL
);
3180 if (add_data
->force
)
3181 strvec_push(&add_submod
.args
, "--force");
3182 strvec_pushl(&add_submod
.args
, "--", add_data
->sm_path
, NULL
);
3184 if (run_command(&add_submod
))
3185 die(_("Failed to add submodule '%s'"), add_data
->sm_path
);
3187 if (config_submodule_in_gitmodules(add_data
->sm_name
, "path", add_data
->sm_path
) ||
3188 config_submodule_in_gitmodules(add_data
->sm_name
, "url", add_data
->repo
))
3189 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3191 if (add_data
->branch
) {
3192 if (config_submodule_in_gitmodules(add_data
->sm_name
,
3193 "branch", add_data
->branch
))
3194 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3197 add_gitmodules
.git_cmd
= 1;
3198 strvec_pushl(&add_gitmodules
.args
,
3199 "add", "--force", "--", ".gitmodules", NULL
);
3201 if (run_command(&add_gitmodules
))
3202 die(_("Failed to register submodule '%s'"), add_data
->sm_path
);
3205 * NEEDSWORK: In a multi-working-tree world this needs to be
3206 * set in the per-worktree config.
3209 * NEEDSWORK: In the longer run, we need to get rid of this
3210 * pattern of querying "submodule.active" before calling
3211 * is_submodule_active(), since that function needs to find
3212 * out the value of "submodule.active" again anyway.
3214 if (!git_config_get("submodule.active")) {
3216 * If the submodule being added isn't already covered by the
3217 * current configured pathspec, set the submodule's active flag
3219 if (!is_submodule_active(the_repository
, add_data
->sm_path
)) {
3220 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3221 git_config_set_gently(key
, "true");
3225 key
= xstrfmt("submodule.%s.active", add_data
->sm_name
);
3226 git_config_set_gently(key
, "true");
3231 static void die_on_index_match(const char *path
, int force
)
3234 const char *args
[] = { path
, NULL
};
3235 parse_pathspec(&ps
, 0, PATHSPEC_PREFER_CWD
, NULL
, args
);
3237 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
3238 die(_("index file corrupt"));
3242 char *ps_matched
= xcalloc(ps
.nr
, 1);
3244 /* TODO: audit for interaction with sparse-index. */
3245 ensure_full_index(the_repository
->index
);
3248 * Since there is only one pathspec, we just need to
3249 * check ps_matched[0] to know if a cache entry matched.
3251 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
3252 ce_path_match(the_repository
->index
, the_repository
->index
->cache
[i
], &ps
,
3255 if (ps_matched
[0]) {
3257 die(_("'%s' already exists in the index"),
3259 if (!S_ISGITLINK(the_repository
->index
->cache
[i
]->ce_mode
))
3260 die(_("'%s' already exists in the index "
3261 "and is not a submodule"), path
);
3267 clear_pathspec(&ps
);
3270 static void die_on_repo_without_commits(const char *path
)
3272 struct strbuf sb
= STRBUF_INIT
;
3273 strbuf_addstr(&sb
, path
);
3274 if (is_nonbare_repository_dir(&sb
)) {
3275 struct object_id oid
;
3276 if (resolve_gitlink_ref(path
, "HEAD", &oid
) < 0)
3277 die(_("'%s' does not have a commit checked out"), path
);
3279 strbuf_release(&sb
);
3282 static int module_add(int argc
, const char **argv
, const char *prefix
)
3284 int force
= 0, quiet
= 0, progress
= 0, dissociate
= 0;
3285 struct add_data add_data
= ADD_DATA_INIT
;
3286 char *to_free
= NULL
;
3287 struct option options
[] = {
3288 OPT_STRING('b', "branch", &add_data
.branch
, N_("branch"),
3289 N_("branch of repository to add as submodule")),
3290 OPT__FORCE(&force
, N_("allow adding an otherwise ignored submodule path"),
3291 PARSE_OPT_NOCOMPLETE
),
3292 OPT__QUIET(&quiet
, N_("print only error messages")),
3293 OPT_BOOL(0, "progress", &progress
, N_("force cloning progress")),
3294 OPT_STRING(0, "reference", &add_data
.reference_path
, N_("repository"),
3295 N_("reference repository")),
3296 OPT_BOOL(0, "dissociate", &dissociate
, N_("borrow the objects from reference repositories")),
3297 OPT_STRING(0, "name", &add_data
.sm_name
, N_("name"),
3298 N_("sets the submodule's name to the given string "
3299 "instead of defaulting to its path")),
3300 OPT_INTEGER(0, "depth", &add_data
.depth
, N_("depth for shallow clones")),
3303 const char *const usage
[] = {
3304 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3307 struct strbuf sb
= STRBUF_INIT
;
3310 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3312 if (!is_writing_gitmodules_ok())
3313 die(_("please make sure that the .gitmodules file is in the working tree"));
3315 if (prefix
&& *prefix
&&
3316 add_data
.reference_path
&& !is_absolute_path(add_data
.reference_path
))
3317 add_data
.reference_path
= xstrfmt("%s%s", prefix
, add_data
.reference_path
);
3319 if (argc
== 0 || argc
> 2)
3320 usage_with_options(usage
, options
);
3322 add_data
.repo
= argv
[0];
3324 add_data
.sm_path
= git_url_basename(add_data
.repo
, 0, 0);
3326 add_data
.sm_path
= xstrdup(argv
[1]);
3328 if (prefix
&& *prefix
&& !is_absolute_path(add_data
.sm_path
)) {
3329 char *sm_path
= add_data
.sm_path
;
3331 add_data
.sm_path
= xstrfmt("%s%s", prefix
, sm_path
);
3335 if (starts_with_dot_dot_slash(add_data
.repo
) ||
3336 starts_with_dot_slash(add_data
.repo
)) {
3338 die(_("Relative path can only be used from the toplevel "
3339 "of the working tree"));
3341 /* dereference source url relative to parent's url */
3342 to_free
= resolve_relative_url(add_data
.repo
, NULL
, 1);
3343 add_data
.realrepo
= to_free
;
3344 } else if (is_dir_sep(add_data
.repo
[0]) || strchr(add_data
.repo
, ':')) {
3345 add_data
.realrepo
= add_data
.repo
;
3347 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3353 * multiple //; leading ./; /./; /../;
3355 normalize_path_copy(add_data
.sm_path
, add_data
.sm_path
);
3356 strip_dir_trailing_slashes(add_data
.sm_path
);
3358 die_on_index_match(add_data
.sm_path
, force
);
3359 die_on_repo_without_commits(add_data
.sm_path
);
3362 struct child_process cp
= CHILD_PROCESS_INIT
;
3366 strvec_pushl(&cp
.args
, "add", "--dry-run", "--ignore-missing",
3367 "--no-warn-embedded-repo", add_data
.sm_path
, NULL
);
3368 if ((ret
= pipe_command(&cp
, NULL
, 0, NULL
, 0, &sb
, 0))) {
3369 strbuf_complete_line(&sb
);
3370 fputs(sb
.buf
, stderr
);
3375 if(!add_data
.sm_name
)
3376 add_data
.sm_name
= add_data
.sm_path
;
3378 if (check_submodule_name(add_data
.sm_name
))
3379 die(_("'%s' is not a valid submodule name"), add_data
.sm_name
);
3381 add_data
.prefix
= prefix
;
3382 add_data
.force
= !!force
;
3383 add_data
.quiet
= !!quiet
;
3384 add_data
.progress
= !!progress
;
3385 add_data
.dissociate
= !!dissociate
;
3387 if (add_submodule(&add_data
))
3389 configure_added_submodule(&add_data
);
3393 free(add_data
.sm_path
);
3395 strbuf_release(&sb
);
3400 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
3402 parse_opt_subcommand_fn
*fn
= NULL
;
3403 const char *const usage
[] = {
3404 N_("git submodule--helper <command>"),
3407 struct option options
[] = {
3408 OPT_SUBCOMMAND("clone", &fn
, module_clone
),
3409 OPT_SUBCOMMAND("add", &fn
, module_add
),
3410 OPT_SUBCOMMAND("update", &fn
, module_update
),
3411 OPT_SUBCOMMAND("foreach", &fn
, module_foreach
),
3412 OPT_SUBCOMMAND("init", &fn
, module_init
),
3413 OPT_SUBCOMMAND("status", &fn
, module_status
),
3414 OPT_SUBCOMMAND("sync", &fn
, module_sync
),
3415 OPT_SUBCOMMAND("deinit", &fn
, module_deinit
),
3416 OPT_SUBCOMMAND("summary", &fn
, module_summary
),
3417 OPT_SUBCOMMAND("push-check", &fn
, push_check
),
3418 OPT_SUBCOMMAND("absorbgitdirs", &fn
, absorb_git_dirs
),
3419 OPT_SUBCOMMAND("set-url", &fn
, module_set_url
),
3420 OPT_SUBCOMMAND("set-branch", &fn
, module_set_branch
),
3421 OPT_SUBCOMMAND("create-branch", &fn
, module_create_branch
),
3424 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
3426 return fn(argc
, argv
, prefix
);