1 #include "git-compat-util.h"
3 #include "repository.h"
5 #include "submodule-config.h"
10 #include "environment.h"
14 #include "run-command.h"
17 #include "string-list.h"
18 #include "oid-array.h"
21 #include "thread-utils.h"
26 #include "parse-options.h"
27 #include "object-file.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
30 #include "commit-reach.h"
31 #include "read-cache-ll.h"
36 static int config_update_recurse_submodules
= RECURSE_SUBMODULES_OFF
;
37 static int initialized_fetch_ref_tips
;
38 static struct oid_array ref_tips_before_fetch
;
39 static struct oid_array ref_tips_after_fetch
;
42 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
43 * will be disabled because we can't guess what might be configured in
44 * .gitmodules unless the user resolves the conflict.
46 int is_gitmodules_unmerged(struct index_state
*istate
)
48 int pos
= index_name_pos(istate
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
49 if (pos
< 0) { /* .gitmodules not found or isn't merged */
51 if (istate
->cache_nr
> pos
) { /* there is a .gitmodules */
52 const struct cache_entry
*ce
= istate
->cache
[pos
];
53 if (ce_namelen(ce
) == strlen(GITMODULES_FILE
) &&
54 !strcmp(ce
->name
, GITMODULES_FILE
))
63 * Check if the .gitmodules file is safe to write.
65 * Writing to the .gitmodules file requires that the file exists in the
66 * working tree or, if it doesn't, that a brand new .gitmodules file is going
67 * to be created (i.e. it's neither in the index nor in the current branch).
69 * It is not safe to write to .gitmodules if it's not in the working tree but
70 * it is in the index or in the current branch, because writing new values
71 * (and staging them) would blindly overwrite ALL the old content.
73 int is_writing_gitmodules_ok(void)
76 return file_exists(GITMODULES_FILE
) ||
77 (repo_get_oid(the_repository
, GITMODULES_INDEX
, &oid
) < 0 && repo_get_oid(the_repository
, GITMODULES_HEAD
, &oid
) < 0);
81 * Check if the .gitmodules file has unstaged modifications. This must be
82 * checked before allowing modifications to the .gitmodules file with the
83 * intention to stage them later, because when continuing we would stage the
84 * modifications the user didn't stage herself too. That might change in a
85 * future version when we learn to stage the changes we do ourselves without
86 * staging any previous modifications.
88 int is_staging_gitmodules_ok(struct index_state
*istate
)
90 int pos
= index_name_pos(istate
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
92 if ((pos
>= 0) && (pos
< istate
->cache_nr
)) {
94 if (lstat(GITMODULES_FILE
, &st
) == 0 &&
95 ie_modified(istate
, istate
->cache
[pos
], &st
, 0) & DATA_CHANGED
)
102 static int for_each_remote_ref_submodule(const char *submodule
,
103 each_ref_fn fn
, void *cb_data
)
105 return refs_for_each_remote_ref(get_submodule_ref_store(submodule
),
110 * Try to update the "path" entry in the "submodule.<name>" section of the
111 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
112 * with the correct path=<oldpath> setting was found and we could update it.
114 int update_path_in_gitmodules(const char *oldpath
, const char *newpath
)
116 struct strbuf entry
= STRBUF_INIT
;
117 const struct submodule
*submodule
;
120 if (!file_exists(GITMODULES_FILE
)) /* Do nothing without .gitmodules */
123 if (is_gitmodules_unmerged(the_repository
->index
))
124 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
126 submodule
= submodule_from_path(the_repository
, null_oid(), oldpath
);
127 if (!submodule
|| !submodule
->name
) {
128 warning(_("Could not find section in .gitmodules where path=%s"), oldpath
);
131 strbuf_addstr(&entry
, "submodule.");
132 strbuf_addstr(&entry
, submodule
->name
);
133 strbuf_addstr(&entry
, ".path");
134 ret
= config_set_in_gitmodules_file_gently(entry
.buf
, newpath
);
135 strbuf_release(&entry
);
140 * Try to remove the "submodule.<name>" section from .gitmodules where the given
141 * path is configured. Return 0 only if a .gitmodules file was found, a section
142 * with the correct path=<path> setting was found and we could remove it.
144 int remove_path_from_gitmodules(const char *path
)
146 struct strbuf sect
= STRBUF_INIT
;
147 const struct submodule
*submodule
;
149 if (!file_exists(GITMODULES_FILE
)) /* Do nothing without .gitmodules */
152 if (is_gitmodules_unmerged(the_repository
->index
))
153 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
155 submodule
= submodule_from_path(the_repository
, null_oid(), path
);
156 if (!submodule
|| !submodule
->name
) {
157 warning(_("Could not find section in .gitmodules where path=%s"), path
);
160 strbuf_addstr(§
, "submodule.");
161 strbuf_addstr(§
, submodule
->name
);
162 if (git_config_rename_section_in_file(GITMODULES_FILE
, sect
.buf
, NULL
) < 0) {
163 /* Maybe the user already did that, don't error out here */
164 warning(_("Could not remove .gitmodules entry for %s"), path
);
165 strbuf_release(§
);
168 strbuf_release(§
);
172 void stage_updated_gitmodules(struct index_state
*istate
)
174 if (add_file_to_index(istate
, GITMODULES_FILE
, 0))
175 die(_("staging updated .gitmodules failed"));
178 static struct string_list added_submodule_odb_paths
= STRING_LIST_INIT_NODUP
;
180 void add_submodule_odb_by_path(const char *path
)
182 string_list_insert(&added_submodule_odb_paths
, xstrdup(path
));
185 int register_all_submodule_odb_as_alternates(void)
188 int ret
= added_submodule_odb_paths
.nr
;
190 for (i
= 0; i
< added_submodule_odb_paths
.nr
; i
++)
191 add_to_alternates_memory(added_submodule_odb_paths
.items
[i
].string
);
193 string_list_clear(&added_submodule_odb_paths
, 0);
194 trace2_data_intmax("submodule", the_repository
,
195 "register_all_submodule_odb_as_alternates/registered", ret
);
196 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
197 BUG("register_all_submodule_odb_as_alternates() called");
202 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
205 const struct submodule
*submodule
= submodule_from_path(the_repository
,
212 key
= xstrfmt("submodule.%s.ignore", submodule
->name
);
213 if (repo_config_get_string_tmp(the_repository
, key
, &ignore
))
214 ignore
= submodule
->ignore
;
218 handle_ignore_submodules_arg(diffopt
, ignore
);
219 else if (is_gitmodules_unmerged(the_repository
->index
))
220 diffopt
->flags
.ignore_submodules
= 1;
224 /* Cheap function that only determines if we're interested in submodules at all */
225 int git_default_submodule_config(const char *var
, const char *value
,
228 if (!strcmp(var
, "submodule.recurse")) {
229 int v
= git_config_bool(var
, value
) ?
230 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
231 config_update_recurse_submodules
= v
;
236 int option_parse_recurse_submodules_worktree_updater(const struct option
*opt
,
237 const char *arg
, int unset
)
240 config_update_recurse_submodules
= RECURSE_SUBMODULES_OFF
;
244 config_update_recurse_submodules
=
245 parse_update_recurse_submodules_arg(opt
->long_name
,
248 config_update_recurse_submodules
= RECURSE_SUBMODULES_ON
;
254 * Determine if a submodule has been initialized at a given 'path'
257 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
258 * ie, the config looks like: "[submodule] active\n".
259 * Since that is an invalid pathspec, we should inform the user.
261 int is_tree_submodule_active(struct repository
*repo
,
262 const struct object_id
*treeish_name
,
268 const struct string_list
*sl
;
269 const struct submodule
*module
;
271 module
= submodule_from_path(repo
, treeish_name
, path
);
273 /* early return if there isn't a path->module mapping */
277 /* submodule.<name>.active is set */
278 key
= xstrfmt("submodule.%s.active", module
->name
);
279 if (!repo_config_get_bool(repo
, key
, &ret
)) {
285 /* submodule.active is set */
286 if (!repo_config_get_string_multi(repo
, "submodule.active", &sl
)) {
288 struct strvec args
= STRVEC_INIT
;
289 const struct string_list_item
*item
;
291 for_each_string_list_item(item
, sl
) {
292 strvec_push(&args
, item
->string
);
295 parse_pathspec(&ps
, 0, 0, NULL
, args
.v
);
296 ret
= match_pathspec(repo
->index
, &ps
, path
, strlen(path
), 0, NULL
, 1);
303 /* fallback to checking if the URL is set */
304 key
= xstrfmt("submodule.%s.url", module
->name
);
305 ret
= !repo_config_get_string(repo
, key
, &value
);
312 int is_submodule_active(struct repository
*repo
, const char *path
)
314 return is_tree_submodule_active(repo
, null_oid(), path
);
317 int is_submodule_populated_gently(const char *path
, int *return_error_code
)
320 char *gitdir
= xstrfmt("%s/.git", path
);
322 if (resolve_gitdir_gently(gitdir
, return_error_code
))
330 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
332 void die_in_unpopulated_submodule(struct index_state
*istate
,
340 prefixlen
= strlen(prefix
);
342 for (i
= 0; i
< istate
->cache_nr
; i
++) {
343 struct cache_entry
*ce
= istate
->cache
[i
];
344 int ce_len
= ce_namelen(ce
);
346 if (!S_ISGITLINK(ce
->ce_mode
))
348 if (prefixlen
<= ce_len
)
350 if (strncmp(ce
->name
, prefix
, ce_len
))
352 if (prefix
[ce_len
] != '/')
355 die(_("in unpopulated submodule '%s'"), ce
->name
);
360 * Dies if any paths in the provided pathspec descends into a submodule
362 void die_path_inside_submodule(struct index_state
*istate
,
363 const struct pathspec
*ps
)
367 for (i
= 0; i
< istate
->cache_nr
; i
++) {
368 struct cache_entry
*ce
= istate
->cache
[i
];
369 int ce_len
= ce_namelen(ce
);
371 if (!S_ISGITLINK(ce
->ce_mode
))
374 for (j
= 0; j
< ps
->nr
; j
++) {
375 const struct pathspec_item
*item
= &ps
->items
[j
];
377 if (item
->len
<= ce_len
)
379 if (item
->match
[ce_len
] != '/')
381 if (strncmp(ce
->name
, item
->match
, ce_len
))
383 if (item
->len
== ce_len
+ 1)
386 die(_("Pathspec '%s' is in submodule '%.*s'"),
387 item
->original
, ce_len
, ce
->name
);
392 enum submodule_update_type
parse_submodule_update_type(const char *value
)
394 if (!strcmp(value
, "none"))
395 return SM_UPDATE_NONE
;
396 else if (!strcmp(value
, "checkout"))
397 return SM_UPDATE_CHECKOUT
;
398 else if (!strcmp(value
, "rebase"))
399 return SM_UPDATE_REBASE
;
400 else if (!strcmp(value
, "merge"))
401 return SM_UPDATE_MERGE
;
402 else if (*value
== '!')
403 return SM_UPDATE_COMMAND
;
405 return SM_UPDATE_UNSPECIFIED
;
408 int parse_submodule_update_strategy(const char *value
,
409 struct submodule_update_strategy
*dst
)
411 enum submodule_update_type type
;
413 free((void*)dst
->command
);
416 type
= parse_submodule_update_type(value
);
417 if (type
== SM_UPDATE_UNSPECIFIED
)
421 if (type
== SM_UPDATE_COMMAND
)
422 dst
->command
= xstrdup(value
+ 1);
427 const char *submodule_update_type_to_string(enum submodule_update_type type
)
430 case SM_UPDATE_CHECKOUT
:
432 case SM_UPDATE_MERGE
:
434 case SM_UPDATE_REBASE
:
438 case SM_UPDATE_UNSPECIFIED
:
439 case SM_UPDATE_COMMAND
:
440 BUG("init_submodule() should handle type %d", type
);
442 BUG("unexpected update strategy type: %d", type
);
446 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
449 diffopt
->flags
.ignore_submodule_set
= 1;
450 diffopt
->flags
.ignore_submodules
= 0;
451 diffopt
->flags
.ignore_untracked_in_submodules
= 0;
452 diffopt
->flags
.ignore_dirty_submodules
= 0;
454 if (!strcmp(arg
, "all"))
455 diffopt
->flags
.ignore_submodules
= 1;
456 else if (!strcmp(arg
, "untracked"))
457 diffopt
->flags
.ignore_untracked_in_submodules
= 1;
458 else if (!strcmp(arg
, "dirty"))
459 diffopt
->flags
.ignore_dirty_submodules
= 1;
460 else if (strcmp(arg
, "none"))
461 die(_("bad --ignore-submodules argument: %s"), arg
);
463 * Please update _git_status() in git-completion.bash when you
468 static int prepare_submodule_diff_summary(struct repository
*r
, struct rev_info
*rev
,
470 struct commit
*left
, struct commit
*right
,
471 struct commit_list
*merge_bases
)
473 struct commit_list
*list
;
475 repo_init_revisions(r
, rev
, NULL
);
476 setup_revisions(0, NULL
, rev
, NULL
);
478 rev
->first_parent_only
= 1;
479 left
->object
.flags
|= SYMMETRIC_LEFT
;
480 add_pending_object(rev
, &left
->object
, path
);
481 add_pending_object(rev
, &right
->object
, path
);
482 for (list
= merge_bases
; list
; list
= list
->next
) {
483 list
->item
->object
.flags
|= UNINTERESTING
;
484 add_pending_object(rev
, &list
->item
->object
,
485 oid_to_hex(&list
->item
->object
.oid
));
487 return prepare_revision_walk(rev
);
490 static void print_submodule_diff_summary(struct repository
*r
, struct rev_info
*rev
, struct diff_options
*o
)
492 static const char format
[] = " %m %s";
493 struct strbuf sb
= STRBUF_INIT
;
494 struct commit
*commit
;
496 while ((commit
= get_revision(rev
))) {
497 struct pretty_print_context ctx
= {0};
498 ctx
.date_mode
= rev
->date_mode
;
499 ctx
.output_encoding
= get_log_output_encoding();
500 strbuf_setlen(&sb
, 0);
501 repo_format_commit_message(r
, commit
, format
, &sb
,
503 strbuf_addch(&sb
, '\n');
504 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
505 diff_emit_submodule_del(o
, sb
.buf
);
507 diff_emit_submodule_add(o
, sb
.buf
);
512 void prepare_submodule_repo_env(struct strvec
*out
)
514 prepare_other_repo_env(out
, DEFAULT_GIT_DIR_ENVIRONMENT
);
517 static void prepare_submodule_repo_env_in_gitdir(struct strvec
*out
)
519 prepare_other_repo_env(out
, ".");
523 * Initialize a repository struct for a submodule based on the provided 'path'.
525 * Returns the repository struct on success,
526 * NULL when the submodule is not present.
528 static struct repository
*open_submodule(const char *path
)
530 struct strbuf sb
= STRBUF_INIT
;
531 struct repository
*out
= xmalloc(sizeof(*out
));
533 if (submodule_to_gitdir(&sb
, path
) || repo_init(out
, sb
.buf
, NULL
)) {
539 /* Mark it as a submodule */
540 out
->submodule_prefix
= xstrdup(path
);
547 * Helper function to display the submodule header line prior to the full
550 * If it can locate the submodule git directory it will create a repository
551 * handle for the submodule and lookup both the left and right commits and
552 * put them into the left and right pointers.
554 static void show_submodule_header(struct diff_options
*o
,
556 struct object_id
*one
, struct object_id
*two
,
557 unsigned dirty_submodule
,
558 struct repository
*sub
,
559 struct commit
**left
, struct commit
**right
,
560 struct commit_list
**merge_bases
)
562 const char *message
= NULL
;
563 struct strbuf sb
= STRBUF_INIT
;
564 int fast_forward
= 0, fast_backward
= 0;
566 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
567 diff_emit_submodule_untracked(o
, path
);
569 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
570 diff_emit_submodule_modified(o
, path
);
572 if (is_null_oid(one
))
573 message
= "(new submodule)";
574 else if (is_null_oid(two
))
575 message
= "(submodule deleted)";
579 message
= "(commits not present)";
584 * Attempt to lookup the commit references, and determine if this is
585 * a fast forward or fast backwards update.
587 *left
= lookup_commit_reference(sub
, one
);
588 *right
= lookup_commit_reference(sub
, two
);
591 * Warn about missing commits in the submodule project, but only if
594 if ((!is_null_oid(one
) && !*left
) ||
595 (!is_null_oid(two
) && !*right
))
596 message
= "(commits not present)";
598 *merge_bases
= repo_get_merge_bases(sub
, *left
, *right
);
600 if ((*merge_bases
)->item
== *left
)
602 else if ((*merge_bases
)->item
== *right
)
606 if (oideq(one
, two
)) {
612 strbuf_addf(&sb
, "Submodule %s ", path
);
613 strbuf_add_unique_abbrev(&sb
, one
, DEFAULT_ABBREV
);
614 strbuf_addstr(&sb
, (fast_backward
|| fast_forward
) ? ".." : "...");
615 strbuf_add_unique_abbrev(&sb
, two
, DEFAULT_ABBREV
);
617 strbuf_addf(&sb
, " %s\n", message
);
619 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
620 diff_emit_submodule_header(o
, sb
.buf
);
625 void show_submodule_diff_summary(struct diff_options
*o
, const char *path
,
626 struct object_id
*one
, struct object_id
*two
,
627 unsigned dirty_submodule
)
629 struct rev_info rev
= REV_INFO_INIT
;
630 struct commit
*left
= NULL
, *right
= NULL
;
631 struct commit_list
*merge_bases
= NULL
;
632 struct repository
*sub
;
634 sub
= open_submodule(path
);
635 show_submodule_header(o
, path
, one
, two
, dirty_submodule
,
636 sub
, &left
, &right
, &merge_bases
);
639 * If we don't have both a left and a right pointer, there is no
640 * reason to try and display a summary. The header line should contain
641 * all the information the user needs.
643 if (!left
|| !right
|| !sub
)
646 /* Treat revision walker failure the same as missing commits */
647 if (prepare_submodule_diff_summary(sub
, &rev
, path
, left
, right
, merge_bases
)) {
648 diff_emit_submodule_error(o
, "(revision walker failed)\n");
652 print_submodule_diff_summary(sub
, &rev
, o
);
655 free_commit_list(merge_bases
);
656 release_revisions(&rev
);
657 clear_commit_marks(left
, ~0);
658 clear_commit_marks(right
, ~0);
665 void show_submodule_inline_diff(struct diff_options
*o
, const char *path
,
666 struct object_id
*one
, struct object_id
*two
,
667 unsigned dirty_submodule
)
669 const struct object_id
*old_oid
= the_hash_algo
->empty_tree
, *new_oid
= the_hash_algo
->empty_tree
;
670 struct commit
*left
= NULL
, *right
= NULL
;
671 struct commit_list
*merge_bases
= NULL
;
672 struct child_process cp
= CHILD_PROCESS_INIT
;
673 struct strbuf sb
= STRBUF_INIT
;
674 struct repository
*sub
;
676 sub
= open_submodule(path
);
677 show_submodule_header(o
, path
, one
, two
, dirty_submodule
,
678 sub
, &left
, &right
, &merge_bases
);
680 /* We need a valid left and right commit to display a difference */
681 if (!(left
|| is_null_oid(one
)) ||
682 !(right
|| is_null_oid(two
)))
695 /* TODO: other options may need to be passed here. */
696 strvec_pushl(&cp
.args
, "diff", "--submodule=diff", NULL
);
697 strvec_pushf(&cp
.args
, "--color=%s", want_color(o
->use_color
) ?
700 if (o
->flags
.reverse_diff
) {
701 strvec_pushf(&cp
.args
, "--src-prefix=%s%s/",
703 strvec_pushf(&cp
.args
, "--dst-prefix=%s%s/",
706 strvec_pushf(&cp
.args
, "--src-prefix=%s%s/",
708 strvec_pushf(&cp
.args
, "--dst-prefix=%s%s/",
711 strvec_push(&cp
.args
, oid_to_hex(old_oid
));
713 * If the submodule has modified content, we will diff against the
714 * work tree, under the assumption that the user has asked for the
715 * diff format and wishes to actually see all differences even if they
716 * haven't yet been committed to the submodule yet.
718 if (!(dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
))
719 strvec_push(&cp
.args
, oid_to_hex(new_oid
));
721 prepare_submodule_repo_env(&cp
.env
);
723 if (!is_directory(path
)) {
724 /* fall back to absorbed git dir, if any */
727 cp
.dir
= sub
->gitdir
;
728 strvec_push(&cp
.env
, GIT_DIR_ENVIRONMENT
"=.");
729 strvec_push(&cp
.env
, GIT_WORK_TREE_ENVIRONMENT
"=.");
732 if (start_command(&cp
)) {
733 diff_emit_submodule_error(o
, "(diff failed)\n");
737 while (strbuf_getwholeline_fd(&sb
, cp
.out
, '\n') != EOF
)
738 diff_emit_submodule_pipethrough(o
, sb
.buf
, sb
.len
);
740 if (finish_command(&cp
))
741 diff_emit_submodule_error(o
, "(diff failed)\n");
745 free_commit_list(merge_bases
);
747 clear_commit_marks(left
, ~0);
749 clear_commit_marks(right
, ~0);
756 int should_update_submodules(void)
758 return config_update_recurse_submodules
== RECURSE_SUBMODULES_ON
;
761 const struct submodule
*submodule_from_ce(const struct cache_entry
*ce
)
763 if (!S_ISGITLINK(ce
->ce_mode
))
766 if (!should_update_submodules())
769 return submodule_from_path(the_repository
, null_oid(), ce
->name
);
773 struct collect_changed_submodules_cb_data
{
774 struct repository
*repo
;
775 struct string_list
*changed
;
776 const struct object_id
*commit_oid
;
780 * this would normally be two functions: default_name_from_path() and
781 * path_from_default_name(). Since the default name is the same as
782 * the submodule path we can get away with just one function which only
783 * checks whether there is a submodule in the working directory at that
786 static const char *default_name_or_path(const char *path_or_name
)
790 if (!is_submodule_populated_gently(path_or_name
, &error_code
))
797 * Holds relevant information for a changed submodule. Used as the .util
798 * member of the changed submodule name string_list_item.
800 * (super_oid, path) allows the submodule config to be read from _some_
801 * .gitmodules file. We store this information the first time we find a
802 * superproject commit that points to the submodule, but this is
803 * arbitrary - we can choose any (super_oid, path) that matches the
806 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
807 * guarantee that we're reading the commit that the user would expect. A better
808 * scheme would be to just fetch a submodule by its name. This requires two
810 * - Create a function that behaves like repo_submodule_init(), but accepts a
811 * submodule name instead of treeish_name and path. This should be easy
812 * because repo_submodule_init() internally uses the submodule's name.
814 * - Replace most instances of 'struct submodule' (which is the .gitmodules
815 * config) with just the submodule name. This is OK because we expect
816 * submodule settings to be stored in .git/config (via "git submodule init"),
817 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
818 * which constructs a bogus 'struct submodule' for the sake of giving a
819 * placeholder name to a gitlink.
821 struct changed_submodule_data
{
823 * The first superproject commit in the rev walk that points to
826 const struct object_id
*super_oid
;
828 * Path to the submodule in the superproject commit referenced
832 /* The submodule commits that have changed in the rev walk. */
833 struct oid_array new_commits
;
836 static void changed_submodule_data_clear(struct changed_submodule_data
*cs_data
)
838 oid_array_clear(&cs_data
->new_commits
);
842 static void collect_changed_submodules_cb(struct diff_queue_struct
*q
,
843 struct diff_options
*options UNUSED
,
846 struct collect_changed_submodules_cb_data
*me
= data
;
847 struct string_list
*changed
= me
->changed
;
848 const struct object_id
*commit_oid
= me
->commit_oid
;
851 for (i
= 0; i
< q
->nr
; i
++) {
852 struct diff_filepair
*p
= q
->queue
[i
];
853 const struct submodule
*submodule
;
855 struct string_list_item
*item
;
856 struct changed_submodule_data
*cs_data
;
858 if (!S_ISGITLINK(p
->two
->mode
))
861 submodule
= submodule_from_path(me
->repo
,
862 commit_oid
, p
->two
->path
);
864 name
= submodule
->name
;
866 name
= default_name_or_path(p
->two
->path
);
867 /* make sure name does not collide with existing one */
869 submodule
= submodule_from_name(me
->repo
,
872 warning(_("Submodule in commit %s at path: "
873 "'%s' collides with a submodule named "
874 "the same. Skipping it."),
875 oid_to_hex(commit_oid
), p
->two
->path
);
883 item
= string_list_insert(changed
, name
);
885 cs_data
= item
->util
;
887 item
->util
= xcalloc(1, sizeof(struct changed_submodule_data
));
888 cs_data
= item
->util
;
889 cs_data
->super_oid
= commit_oid
;
890 cs_data
->path
= xstrdup(p
->two
->path
);
892 oid_array_append(&cs_data
->new_commits
, &p
->two
->oid
);
897 * Collect the paths of submodules in 'changed' which have changed based on
898 * the revisions as specified in 'argv'. Each entry in 'changed' will also
899 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
900 * what the submodule pointers were updated to during the change.
902 static void collect_changed_submodules(struct repository
*r
,
903 struct string_list
*changed
,
907 const struct commit
*commit
;
909 struct setup_revision_opt s_r_opt
= {
910 .assume_dashdash
= 1,
913 save_warning
= warn_on_object_refname_ambiguity
;
914 warn_on_object_refname_ambiguity
= 0;
915 repo_init_revisions(r
, &rev
, NULL
);
916 setup_revisions(argv
->nr
, argv
->v
, &rev
, &s_r_opt
);
917 warn_on_object_refname_ambiguity
= save_warning
;
918 if (prepare_revision_walk(&rev
))
919 die(_("revision walk setup failed"));
921 while ((commit
= get_revision(&rev
))) {
922 struct rev_info diff_rev
;
923 struct collect_changed_submodules_cb_data data
;
925 data
.changed
= changed
;
926 data
.commit_oid
= &commit
->object
.oid
;
928 repo_init_revisions(r
, &diff_rev
, NULL
);
929 diff_rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
930 diff_rev
.diffopt
.format_callback
= collect_changed_submodules_cb
;
931 diff_rev
.diffopt
.format_callback_data
= &data
;
932 diff_rev
.dense_combined_merges
= 1;
933 diff_tree_combined_merge(commit
, &diff_rev
);
934 release_revisions(&diff_rev
);
937 reset_revision_walk();
938 release_revisions(&rev
);
941 static void free_submodules_data(struct string_list
*submodules
)
943 struct string_list_item
*item
;
944 for_each_string_list_item(item
, submodules
)
945 changed_submodule_data_clear(item
->util
);
947 string_list_clear(submodules
, 1);
950 static int has_remote(const char *refname UNUSED
,
951 const struct object_id
*oid UNUSED
,
952 int flags UNUSED
, void *cb_data UNUSED
)
957 static int append_oid_to_argv(const struct object_id
*oid
, void *data
)
959 struct strvec
*argv
= data
;
960 strvec_push(argv
, oid_to_hex(oid
));
964 struct has_commit_data
{
965 struct repository
*repo
;
968 const struct object_id
*super_oid
;
971 static int check_has_commit(const struct object_id
*oid
, void *data
)
973 struct has_commit_data
*cb
= data
;
974 struct repository subrepo
;
975 enum object_type type
;
977 if (repo_submodule_init(&subrepo
, cb
->repo
, cb
->path
, cb
->super_oid
)) {
979 /* subrepo failed to init, so don't clean it up. */
983 type
= oid_object_info(&subrepo
, oid
, NULL
);
990 * Object is missing or invalid. If invalid, an error message
991 * has already been printed.
996 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
997 cb
->path
, oid_to_hex(oid
), type_name(type
));
1000 repo_clear(&subrepo
);
1004 static int submodule_has_commits(struct repository
*r
,
1006 const struct object_id
*super_oid
,
1007 struct oid_array
*commits
)
1009 struct has_commit_data has_commit
= {
1013 .super_oid
= super_oid
1016 if (validate_submodule_path(path
) < 0)
1019 oid_array_for_each_unique(commits
, check_has_commit
, &has_commit
);
1021 if (has_commit
.result
) {
1023 * Even if the submodule is checked out and the commit is
1024 * present, make sure it exists in the submodule's object store
1025 * and that it is reachable from a ref.
1027 struct child_process cp
= CHILD_PROCESS_INIT
;
1028 struct strbuf out
= STRBUF_INIT
;
1030 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", NULL
);
1031 oid_array_for_each_unique(commits
, append_oid_to_argv
, &cp
.args
);
1032 strvec_pushl(&cp
.args
, "--not", "--all", NULL
);
1034 prepare_submodule_repo_env(&cp
.env
);
1039 if (capture_command(&cp
, &out
, GIT_MAX_HEXSZ
+ 1) || out
.len
)
1040 has_commit
.result
= 0;
1042 strbuf_release(&out
);
1045 return has_commit
.result
;
1048 static int submodule_needs_pushing(struct repository
*r
,
1050 struct oid_array
*commits
)
1052 if (!submodule_has_commits(r
, path
, null_oid(), commits
))
1054 * NOTE: We do consider it safe to return "no" here. The
1055 * correct answer would be "We do not know" instead of
1056 * "No push needed", but it is quite hard to change
1057 * the submodule pointer without having the submodule
1058 * around. If a user did however change the submodules
1059 * without having the submodule around, this indicates
1060 * an expert who knows what they are doing or a
1061 * maintainer integrating work from other people. In
1062 * both cases it should be safe to skip this check.
1066 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
1067 struct child_process cp
= CHILD_PROCESS_INIT
;
1068 struct strbuf buf
= STRBUF_INIT
;
1069 int needs_pushing
= 0;
1071 strvec_push(&cp
.args
, "rev-list");
1072 oid_array_for_each_unique(commits
, append_oid_to_argv
, &cp
.args
);
1073 strvec_pushl(&cp
.args
, "--not", "--remotes", "-n", "1" , NULL
);
1075 prepare_submodule_repo_env(&cp
.env
);
1080 if (start_command(&cp
))
1081 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
1083 if (strbuf_read(&buf
, cp
.out
, the_hash_algo
->hexsz
+ 1))
1085 finish_command(&cp
);
1087 strbuf_release(&buf
);
1088 return needs_pushing
;
1094 int find_unpushed_submodules(struct repository
*r
,
1095 struct oid_array
*commits
,
1096 const char *remotes_name
,
1097 struct string_list
*needs_pushing
)
1099 struct string_list submodules
= STRING_LIST_INIT_DUP
;
1100 struct string_list_item
*name
;
1101 struct strvec argv
= STRVEC_INIT
;
1103 /* argv.v[0] will be ignored by setup_revisions */
1104 strvec_push(&argv
, "find_unpushed_submodules");
1105 oid_array_for_each_unique(commits
, append_oid_to_argv
, &argv
);
1106 strvec_push(&argv
, "--not");
1107 strvec_pushf(&argv
, "--remotes=%s", remotes_name
);
1109 collect_changed_submodules(r
, &submodules
, &argv
);
1111 for_each_string_list_item(name
, &submodules
) {
1112 struct changed_submodule_data
*cs_data
= name
->util
;
1113 const struct submodule
*submodule
;
1114 const char *path
= NULL
;
1116 submodule
= submodule_from_name(r
, null_oid(), name
->string
);
1118 path
= submodule
->path
;
1120 path
= default_name_or_path(name
->string
);
1125 if (submodule_needs_pushing(r
, path
, &cs_data
->new_commits
))
1126 string_list_insert(needs_pushing
, path
);
1129 free_submodules_data(&submodules
);
1130 strvec_clear(&argv
);
1132 return needs_pushing
->nr
;
1135 static int push_submodule(const char *path
,
1136 const struct remote
*remote
,
1137 const struct refspec
*rs
,
1138 const struct string_list
*push_options
,
1141 if (validate_submodule_path(path
) < 0)
1144 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
1145 struct child_process cp
= CHILD_PROCESS_INIT
;
1146 strvec_push(&cp
.args
, "push");
1148 * When recursing into a submodule, treat any "only" configurations as "on-
1149 * demand", since "only" would not work (we need all submodules to be pushed
1150 * in order to be able to push the superproject).
1152 strvec_push(&cp
.args
, "--recurse-submodules=only-is-on-demand");
1154 strvec_push(&cp
.args
, "--dry-run");
1156 if (push_options
&& push_options
->nr
) {
1157 const struct string_list_item
*item
;
1158 for_each_string_list_item(item
, push_options
)
1159 strvec_pushf(&cp
.args
, "--push-option=%s",
1163 if (remote
->origin
!= REMOTE_UNCONFIGURED
) {
1165 strvec_push(&cp
.args
, remote
->name
);
1166 for (i
= 0; i
< rs
->raw_nr
; i
++)
1167 strvec_push(&cp
.args
, rs
->raw
[i
]);
1170 prepare_submodule_repo_env(&cp
.env
);
1174 if (run_command(&cp
))
1183 * Perform a check in the submodule to see if the remote and refspec work.
1184 * Die if the submodule can't be pushed.
1186 static void submodule_push_check(const char *path
, const char *head
,
1187 const struct remote
*remote
,
1188 const struct refspec
*rs
)
1190 struct child_process cp
= CHILD_PROCESS_INIT
;
1193 if (validate_submodule_path(path
) < 0)
1196 strvec_push(&cp
.args
, "submodule--helper");
1197 strvec_push(&cp
.args
, "push-check");
1198 strvec_push(&cp
.args
, head
);
1199 strvec_push(&cp
.args
, remote
->name
);
1201 for (i
= 0; i
< rs
->raw_nr
; i
++)
1202 strvec_push(&cp
.args
, rs
->raw
[i
]);
1204 prepare_submodule_repo_env(&cp
.env
);
1211 * Simply indicate if 'submodule--helper push-check' failed.
1212 * More detailed error information will be provided by the
1215 if (run_command(&cp
))
1216 die(_("process for submodule '%s' failed"), path
);
1219 int push_unpushed_submodules(struct repository
*r
,
1220 struct oid_array
*commits
,
1221 const struct remote
*remote
,
1222 const struct refspec
*rs
,
1223 const struct string_list
*push_options
,
1227 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1229 if (!find_unpushed_submodules(r
, commits
,
1230 remote
->name
, &needs_pushing
))
1234 * Verify that the remote and refspec can be propagated to all
1235 * submodules. This check can be skipped if the remote and refspec
1236 * won't be propagated due to the remote being unconfigured (e.g. a URL
1237 * instead of a remote name).
1239 if (remote
->origin
!= REMOTE_UNCONFIGURED
) {
1241 struct object_id head_oid
;
1243 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1245 die(_("Failed to resolve HEAD as a valid ref."));
1247 for (i
= 0; i
< needs_pushing
.nr
; i
++)
1248 submodule_push_check(needs_pushing
.items
[i
].string
,
1253 /* Actually push the submodules */
1254 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
1255 const char *path
= needs_pushing
.items
[i
].string
;
1256 fprintf(stderr
, _("Pushing submodule '%s'\n"), path
);
1257 if (!push_submodule(path
, remote
, rs
,
1258 push_options
, dry_run
)) {
1259 fprintf(stderr
, _("Unable to push submodule '%s'\n"), path
);
1264 string_list_clear(&needs_pushing
, 0);
1269 static int append_oid_to_array(const char *ref UNUSED
,
1270 const struct object_id
*oid
,
1271 int flags UNUSED
, void *data
)
1273 struct oid_array
*array
= data
;
1274 oid_array_append(array
, oid
);
1278 void check_for_new_submodule_commits(struct object_id
*oid
)
1280 if (!initialized_fetch_ref_tips
) {
1281 for_each_ref(append_oid_to_array
, &ref_tips_before_fetch
);
1282 initialized_fetch_ref_tips
= 1;
1285 oid_array_append(&ref_tips_after_fetch
, oid
);
1289 * Returns 1 if there is at least one submodule gitdir in
1290 * $GIT_DIR/modules and 0 otherwise. This follows
1291 * submodule_name_to_gitdir(), which looks for submodules in
1292 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1294 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1295 * submodule absorbgitdirs", or it may be initialized there by "git
1296 * submodule update".
1298 static int repo_has_absorbed_submodules(struct repository
*r
)
1301 struct strbuf buf
= STRBUF_INIT
;
1303 strbuf_repo_git_path(&buf
, r
, "modules/");
1304 ret
= file_exists(buf
.buf
) && !is_empty_dir(buf
.buf
);
1305 strbuf_release(&buf
);
1309 static void calculate_changed_submodule_paths(struct repository
*r
,
1310 struct string_list
*changed_submodule_names
)
1312 struct strvec argv
= STRVEC_INIT
;
1313 struct string_list_item
*name
;
1315 /* No need to check if no submodules would be fetched */
1316 if (!submodule_from_path(r
, NULL
, NULL
) &&
1317 !repo_has_absorbed_submodules(r
))
1320 strvec_push(&argv
, "--"); /* argv[0] program name */
1321 oid_array_for_each_unique(&ref_tips_after_fetch
,
1322 append_oid_to_argv
, &argv
);
1323 strvec_push(&argv
, "--not");
1324 oid_array_for_each_unique(&ref_tips_before_fetch
,
1325 append_oid_to_argv
, &argv
);
1328 * Collect all submodules (whether checked out or not) for which new
1329 * commits have been recorded upstream in "changed_submodule_names".
1331 collect_changed_submodules(r
, changed_submodule_names
, &argv
);
1333 for_each_string_list_item(name
, changed_submodule_names
) {
1334 struct changed_submodule_data
*cs_data
= name
->util
;
1335 const struct submodule
*submodule
;
1336 const char *path
= NULL
;
1338 submodule
= submodule_from_name(r
, null_oid(), name
->string
);
1340 path
= submodule
->path
;
1342 path
= default_name_or_path(name
->string
);
1347 if (submodule_has_commits(r
, path
, null_oid(), &cs_data
->new_commits
)) {
1348 changed_submodule_data_clear(cs_data
);
1349 *name
->string
= '\0';
1353 string_list_remove_empty_items(changed_submodule_names
, 1);
1355 strvec_clear(&argv
);
1356 oid_array_clear(&ref_tips_before_fetch
);
1357 oid_array_clear(&ref_tips_after_fetch
);
1358 initialized_fetch_ref_tips
= 0;
1361 int submodule_touches_in_range(struct repository
*r
,
1362 struct object_id
*excl_oid
,
1363 struct object_id
*incl_oid
)
1365 struct string_list subs
= STRING_LIST_INIT_DUP
;
1366 struct strvec args
= STRVEC_INIT
;
1369 /* No need to check if there are no submodules configured */
1370 if (!submodule_from_path(r
, NULL
, NULL
))
1373 strvec_push(&args
, "--"); /* args[0] program name */
1374 strvec_push(&args
, oid_to_hex(incl_oid
));
1375 if (!is_null_oid(excl_oid
)) {
1376 strvec_push(&args
, "--not");
1377 strvec_push(&args
, oid_to_hex(excl_oid
));
1380 collect_changed_submodules(r
, &subs
, &args
);
1383 strvec_clear(&args
);
1385 free_submodules_data(&subs
);
1389 struct submodule_parallel_fetch
{
1391 * The index of the last index entry processed by
1392 * get_fetch_task_from_index().
1396 * The index of the last string_list entry processed by
1397 * get_fetch_task_from_changed().
1401 struct repository
*r
;
1403 int command_line_option
;
1409 * Names of submodules that have new commits. Generated by
1410 * walking the newly fetched superproject commits.
1412 struct string_list changed_submodule_names
;
1414 * Names of submodules that have already been processed. Lets us
1415 * avoid fetching the same submodule more than once.
1417 struct string_list seen_submodule_names
;
1419 /* Pending fetches by OIDs */
1420 struct fetch_task
**oid_fetch_tasks
;
1421 int oid_fetch_tasks_nr
, oid_fetch_tasks_alloc
;
1423 struct strbuf submodules_with_errors
;
1425 #define SPF_INIT { \
1426 .args = STRVEC_INIT, \
1427 .changed_submodule_names = STRING_LIST_INIT_DUP, \
1428 .seen_submodule_names = STRING_LIST_INIT_DUP, \
1429 .submodules_with_errors = STRBUF_INIT, \
1432 static int get_fetch_recurse_config(const struct submodule
*submodule
,
1433 struct submodule_parallel_fetch
*spf
)
1435 if (spf
->command_line_option
!= RECURSE_SUBMODULES_DEFAULT
)
1436 return spf
->command_line_option
;
1442 int fetch_recurse
= submodule
->fetch_recurse
;
1443 key
= xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule
->name
);
1444 if (!repo_config_get_string_tmp(spf
->r
, key
, &value
)) {
1445 fetch_recurse
= parse_fetch_recurse_submodules_arg(key
, value
);
1449 if (fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
1450 /* local config overrules everything except commandline */
1451 return fetch_recurse
;
1454 return spf
->default_option
;
1458 * Fetch in progress (if callback data) or
1459 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1462 struct repository
*repo
;
1463 const struct submodule
*sub
;
1464 unsigned free_sub
: 1; /* Do we need to free the submodule? */
1465 const char *default_argv
; /* The default fetch mode. */
1466 struct strvec git_args
; /* Args for the child git process. */
1468 struct oid_array
*commits
; /* Ensure these commits are fetched */
1472 * When a submodule is not defined in .gitmodules, we cannot access it
1473 * via the regular submodule-config. Create a fake submodule, which we can
1476 static const struct submodule
*get_non_gitmodules_submodule(const char *path
)
1478 struct submodule
*ret
= NULL
;
1479 const char *name
= default_name_or_path(path
);
1484 ret
= xmalloc(sizeof(*ret
));
1485 memset(ret
, 0, sizeof(*ret
));
1489 return (const struct submodule
*) ret
;
1492 static void fetch_task_release(struct fetch_task
*p
)
1495 free((void*)p
->sub
);
1500 repo_clear(p
->repo
);
1501 FREE_AND_NULL(p
->repo
);
1503 strvec_clear(&p
->git_args
);
1506 static struct repository
*get_submodule_repo_for(struct repository
*r
,
1508 const struct object_id
*treeish_name
)
1510 struct repository
*ret
= xmalloc(sizeof(*ret
));
1512 if (repo_submodule_init(ret
, r
, path
, treeish_name
)) {
1520 static struct fetch_task
*fetch_task_create(struct submodule_parallel_fetch
*spf
,
1522 const struct object_id
*treeish_name
)
1524 struct fetch_task
*task
= xmalloc(sizeof(*task
));
1525 memset(task
, 0, sizeof(*task
));
1527 if (validate_submodule_path(path
) < 0)
1530 task
->sub
= submodule_from_path(spf
->r
, treeish_name
, path
);
1534 * No entry in .gitmodules? Technically not a submodule,
1535 * but historically we supported repositories that happen to be
1536 * in-place where a gitlink is. Keep supporting them.
1538 task
->sub
= get_non_gitmodules_submodule(path
);
1545 if (string_list_lookup(&spf
->seen_submodule_names
, task
->sub
->name
))
1548 switch (get_fetch_recurse_config(task
->sub
, spf
))
1551 case RECURSE_SUBMODULES_DEFAULT
:
1552 case RECURSE_SUBMODULES_ON_DEMAND
:
1554 !string_list_lookup(
1555 &spf
->changed_submodule_names
,
1558 task
->default_argv
= "on-demand";
1560 case RECURSE_SUBMODULES_ON
:
1561 task
->default_argv
= "yes";
1563 case RECURSE_SUBMODULES_OFF
:
1567 task
->repo
= get_submodule_repo_for(spf
->r
, path
, treeish_name
);
1572 fetch_task_release(task
);
1577 static struct fetch_task
*
1578 get_fetch_task_from_index(struct submodule_parallel_fetch
*spf
,
1581 for (; spf
->index_count
< spf
->r
->index
->cache_nr
; spf
->index_count
++) {
1582 const struct cache_entry
*ce
=
1583 spf
->r
->index
->cache
[spf
->index_count
];
1584 struct fetch_task
*task
;
1586 if (!S_ISGITLINK(ce
->ce_mode
))
1589 task
= fetch_task_create(spf
, ce
->name
, null_oid());
1595 strbuf_addf(err
, _("Fetching submodule %s%s\n"),
1596 spf
->prefix
, ce
->name
);
1601 struct strbuf empty_submodule_path
= STRBUF_INIT
;
1603 fetch_task_release(task
);
1607 * An empty directory is normal,
1608 * the submodule is not initialized
1610 strbuf_addf(&empty_submodule_path
, "%s/%s/",
1613 if (S_ISGITLINK(ce
->ce_mode
) &&
1614 !is_empty_dir(empty_submodule_path
.buf
)) {
1617 _("Could not access submodule '%s'\n"),
1620 strbuf_release(&empty_submodule_path
);
1626 static struct fetch_task
*
1627 get_fetch_task_from_changed(struct submodule_parallel_fetch
*spf
,
1630 for (; spf
->changed_count
< spf
->changed_submodule_names
.nr
;
1631 spf
->changed_count
++) {
1632 struct string_list_item item
=
1633 spf
->changed_submodule_names
.items
[spf
->changed_count
];
1634 struct changed_submodule_data
*cs_data
= item
.util
;
1635 struct fetch_task
*task
;
1637 if (!is_tree_submodule_active(spf
->r
, cs_data
->super_oid
,cs_data
->path
))
1640 task
= fetch_task_create(spf
, cs_data
->path
,
1641 cs_data
->super_oid
);
1646 strbuf_addf(err
, _("Could not access submodule '%s' at commit %s\n"),
1648 repo_find_unique_abbrev(the_repository
, cs_data
->super_oid
, DEFAULT_ABBREV
));
1650 fetch_task_release(task
);
1657 _("Fetching submodule %s%s at commit %s\n"),
1658 spf
->prefix
, task
->sub
->path
,
1659 repo_find_unique_abbrev(the_repository
, cs_data
->super_oid
,
1662 spf
->changed_count
++;
1664 * NEEDSWORK: Submodules set/unset a value for
1665 * core.worktree when they are populated/unpopulated by
1666 * "git checkout" (and similar commands, see
1667 * submodule_move_head() and
1668 * connect_work_tree_and_git_dir()), but if the
1669 * submodule is unpopulated in another way (e.g. "git
1670 * rm", "rm -r"), core.worktree will still be set even
1671 * though the directory doesn't exist, and the child
1672 * process will crash while trying to chdir into the
1673 * nonexistent directory.
1675 * In this case, we know that the submodule has no
1676 * working tree, so we can work around this by
1677 * setting "--work-tree=." (--bare does not work because
1678 * worktree settings take precedence over bare-ness).
1679 * However, this is not necessarily true in other cases,
1680 * so a generalized solution is still necessary.
1682 * Possible solutions:
1683 * - teach "git [add|rm]" to unset core.worktree and
1684 * discourage users from removing submodules without
1685 * using a Git command.
1686 * - teach submodule child processes to ignore stale
1687 * core.worktree values.
1689 strvec_push(&task
->git_args
, "--work-tree=.");
1695 static int get_next_submodule(struct child_process
*cp
, struct strbuf
*err
,
1696 void *data
, void **task_cb
)
1698 struct submodule_parallel_fetch
*spf
= data
;
1699 struct fetch_task
*task
=
1700 get_fetch_task_from_index(spf
, err
);
1702 task
= get_fetch_task_from_changed(spf
, err
);
1705 struct strbuf submodule_prefix
= STRBUF_INIT
;
1707 child_process_init(cp
);
1708 cp
->dir
= task
->repo
->gitdir
;
1709 prepare_submodule_repo_env_in_gitdir(&cp
->env
);
1711 strvec_init(&cp
->args
);
1712 if (task
->git_args
.nr
)
1713 strvec_pushv(&cp
->args
, task
->git_args
.v
);
1714 strvec_pushv(&cp
->args
, spf
->args
.v
);
1715 strvec_push(&cp
->args
, task
->default_argv
);
1716 strvec_push(&cp
->args
, "--submodule-prefix");
1718 strbuf_addf(&submodule_prefix
, "%s%s/",
1721 strvec_push(&cp
->args
, submodule_prefix
.buf
);
1724 strbuf_release(&submodule_prefix
);
1725 string_list_insert(&spf
->seen_submodule_names
, task
->sub
->name
);
1729 if (spf
->oid_fetch_tasks_nr
) {
1730 struct fetch_task
*task
=
1731 spf
->oid_fetch_tasks
[spf
->oid_fetch_tasks_nr
- 1];
1732 struct strbuf submodule_prefix
= STRBUF_INIT
;
1733 spf
->oid_fetch_tasks_nr
--;
1735 strbuf_addf(&submodule_prefix
, "%s%s/",
1736 spf
->prefix
, task
->sub
->path
);
1738 child_process_init(cp
);
1739 prepare_submodule_repo_env_in_gitdir(&cp
->env
);
1741 cp
->dir
= task
->repo
->gitdir
;
1743 strvec_init(&cp
->args
);
1744 strvec_pushv(&cp
->args
, spf
->args
.v
);
1745 strvec_push(&cp
->args
, "on-demand");
1746 strvec_push(&cp
->args
, "--submodule-prefix");
1747 strvec_push(&cp
->args
, submodule_prefix
.buf
);
1749 /* NEEDSWORK: have get_default_remote from submodule--helper */
1750 strvec_push(&cp
->args
, "origin");
1751 oid_array_for_each_unique(task
->commits
,
1752 append_oid_to_argv
, &cp
->args
);
1755 strbuf_release(&submodule_prefix
);
1762 static int fetch_start_failure(struct strbuf
*err UNUSED
,
1763 void *cb
, void *task_cb
)
1765 struct submodule_parallel_fetch
*spf
= cb
;
1766 struct fetch_task
*task
= task_cb
;
1770 fetch_task_release(task
);
1774 static int commit_missing_in_sub(const struct object_id
*oid
, void *data
)
1776 struct repository
*subrepo
= data
;
1778 enum object_type type
= oid_object_info(subrepo
, oid
, NULL
);
1780 return type
!= OBJ_COMMIT
;
1783 static int fetch_finish(int retvalue
, struct strbuf
*err UNUSED
,
1784 void *cb
, void *task_cb
)
1786 struct submodule_parallel_fetch
*spf
= cb
;
1787 struct fetch_task
*task
= task_cb
;
1789 struct string_list_item
*it
;
1790 struct changed_submodule_data
*cs_data
;
1792 if (!task
|| !task
->sub
)
1793 BUG("callback cookie bogus");
1797 * NEEDSWORK: This indicates that the overall fetch
1798 * failed, even though there may be a subsequent fetch
1799 * by commit hash that might work. It may be a good
1800 * idea to not indicate failure in this case, and only
1801 * indicate failure if the subsequent fetch fails.
1805 strbuf_addf(&spf
->submodules_with_errors
, "\t%s\n",
1809 /* Is this the second time we process this submodule? */
1813 it
= string_list_lookup(&spf
->changed_submodule_names
, task
->sub
->name
);
1815 /* Could be an unchanged submodule, not contained in the list */
1819 oid_array_filter(&cs_data
->new_commits
,
1820 commit_missing_in_sub
,
1823 /* Are there commits we want, but do not exist? */
1824 if (cs_data
->new_commits
.nr
) {
1825 task
->commits
= &cs_data
->new_commits
;
1826 ALLOC_GROW(spf
->oid_fetch_tasks
,
1827 spf
->oid_fetch_tasks_nr
+ 1,
1828 spf
->oid_fetch_tasks_alloc
);
1829 spf
->oid_fetch_tasks
[spf
->oid_fetch_tasks_nr
] = task
;
1830 spf
->oid_fetch_tasks_nr
++;
1835 fetch_task_release(task
);
1840 int fetch_submodules(struct repository
*r
,
1841 const struct strvec
*options
,
1842 const char *prefix
, int command_line_option
,
1844 int quiet
, int max_parallel_jobs
)
1847 struct submodule_parallel_fetch spf
= SPF_INIT
;
1848 const struct run_process_parallel_opts opts
= {
1849 .tr2_category
= "submodule",
1850 .tr2_label
= "parallel/fetch",
1852 .processes
= max_parallel_jobs
,
1854 .get_next_task
= get_next_submodule
,
1855 .start_failure
= fetch_start_failure
,
1856 .task_finished
= fetch_finish
,
1861 spf
.command_line_option
= command_line_option
;
1862 spf
.default_option
= default_option
;
1864 spf
.prefix
= prefix
;
1869 if (repo_read_index(r
) < 0)
1870 die(_("index file corrupt"));
1872 strvec_push(&spf
.args
, "fetch");
1873 for (i
= 0; i
< options
->nr
; i
++)
1874 strvec_push(&spf
.args
, options
->v
[i
]);
1875 strvec_push(&spf
.args
, "--recurse-submodules-default");
1876 /* default value, "--submodule-prefix" and its value are added later */
1878 calculate_changed_submodule_paths(r
, &spf
.changed_submodule_names
);
1879 string_list_sort(&spf
.changed_submodule_names
);
1880 run_processes_parallel(&opts
);
1882 if (spf
.submodules_with_errors
.len
> 0)
1883 fprintf(stderr
, _("Errors during submodule fetch:\n%s"),
1884 spf
.submodules_with_errors
.buf
);
1887 strvec_clear(&spf
.args
);
1889 free_submodules_data(&spf
.changed_submodule_names
);
1893 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
1895 struct child_process cp
= CHILD_PROCESS_INIT
;
1896 struct strbuf buf
= STRBUF_INIT
;
1898 unsigned dirty_submodule
= 0;
1899 const char *git_dir
;
1900 int ignore_cp_exit_code
= 0;
1902 if (validate_submodule_path(path
) < 0)
1905 strbuf_addf(&buf
, "%s/.git", path
);
1906 git_dir
= read_gitfile(buf
.buf
);
1909 if (!is_git_directory(git_dir
)) {
1910 if (is_directory(git_dir
))
1911 die(_("'%s' not recognized as a git repository"), git_dir
);
1912 strbuf_release(&buf
);
1913 /* The submodule is not checked out, so it is not modified */
1918 strvec_pushl(&cp
.args
, "status", "--porcelain=2", NULL
);
1919 if (ignore_untracked
)
1920 strvec_push(&cp
.args
, "-uno");
1922 prepare_submodule_repo_env(&cp
.env
);
1927 if (start_command(&cp
))
1928 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path
);
1930 fp
= xfdopen(cp
.out
, "r");
1931 while (strbuf_getwholeline(&buf
, fp
, '\n') != EOF
) {
1932 /* regular untracked files */
1933 if (buf
.buf
[0] == '?')
1934 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
1936 if (buf
.buf
[0] == 'u' ||
1937 buf
.buf
[0] == '1' ||
1938 buf
.buf
[0] == '2') {
1939 /* T = line type, XY = status, SSSS = submodule state */
1940 if (buf
.len
< strlen("T XY SSSS"))
1941 BUG("invalid status --porcelain=2 line %s",
1944 if (buf
.buf
[5] == 'S' && buf
.buf
[8] == 'U')
1945 /* nested untracked file */
1946 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
1948 if (buf
.buf
[0] == 'u' ||
1949 buf
.buf
[0] == '2' ||
1950 memcmp(buf
.buf
+ 5, "S..U", 4))
1952 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
1955 if ((dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) &&
1956 ((dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ||
1957 ignore_untracked
)) {
1959 * We're not interested in any further information from
1960 * the child any more, neither output nor its exit code.
1962 ignore_cp_exit_code
= 1;
1968 if (finish_command(&cp
) && !ignore_cp_exit_code
)
1969 die(_("'git status --porcelain=2' failed in submodule %s"), path
);
1971 strbuf_release(&buf
);
1972 return dirty_submodule
;
1975 int submodule_uses_gitfile(const char *path
)
1977 struct child_process cp
= CHILD_PROCESS_INIT
;
1978 struct strbuf buf
= STRBUF_INIT
;
1979 const char *git_dir
;
1981 if (validate_submodule_path(path
) < 0)
1984 strbuf_addf(&buf
, "%s/.git", path
);
1985 git_dir
= read_gitfile(buf
.buf
);
1987 strbuf_release(&buf
);
1990 strbuf_release(&buf
);
1992 /* Now test that all nested submodules use a gitfile too */
1993 strvec_pushl(&cp
.args
,
1994 "submodule", "foreach", "--quiet", "--recursive",
1995 "test -f .git", NULL
);
1997 prepare_submodule_repo_env(&cp
.env
);
2003 if (run_command(&cp
))
2010 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
2013 * Return 1 if we'd lose data, return 0 if the removal is fine,
2014 * and negative values for errors.
2016 int bad_to_remove_submodule(const char *path
, unsigned flags
)
2019 struct child_process cp
= CHILD_PROCESS_INIT
;
2020 struct strbuf buf
= STRBUF_INIT
;
2023 if (validate_submodule_path(path
) < 0)
2026 if (!file_exists(path
) || is_empty_dir(path
))
2029 if (!submodule_uses_gitfile(path
))
2032 strvec_pushl(&cp
.args
, "status", "--porcelain",
2033 "--ignore-submodules=none", NULL
);
2035 if (flags
& SUBMODULE_REMOVAL_IGNORE_UNTRACKED
)
2036 strvec_push(&cp
.args
, "-uno");
2038 strvec_push(&cp
.args
, "-uall");
2040 if (!(flags
& SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
))
2041 strvec_push(&cp
.args
, "--ignored");
2043 prepare_submodule_repo_env(&cp
.env
);
2048 if (start_command(&cp
)) {
2049 if (flags
& SUBMODULE_REMOVAL_DIE_ON_ERROR
)
2050 die(_("could not start 'git status' in submodule '%s'"),
2056 len
= strbuf_read(&buf
, cp
.out
, 1024);
2061 if (finish_command(&cp
)) {
2062 if (flags
& SUBMODULE_REMOVAL_DIE_ON_ERROR
)
2063 die(_("could not run 'git status' in submodule '%s'"),
2068 strbuf_release(&buf
);
2072 void submodule_unset_core_worktree(const struct submodule
*sub
)
2074 struct strbuf config_path
= STRBUF_INIT
;
2076 if (validate_submodule_path(sub
->path
) < 0)
2079 submodule_name_to_gitdir(&config_path
, the_repository
, sub
->name
);
2080 strbuf_addstr(&config_path
, "/config");
2082 if (git_config_set_in_file_gently(config_path
.buf
, "core.worktree", NULL
))
2083 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2086 strbuf_release(&config_path
);
2089 static int submodule_has_dirty_index(const struct submodule
*sub
)
2091 struct child_process cp
= CHILD_PROCESS_INIT
;
2093 if (validate_submodule_path(sub
->path
) < 0)
2096 prepare_submodule_repo_env(&cp
.env
);
2099 strvec_pushl(&cp
.args
, "diff-index", "--quiet",
2100 "--cached", "HEAD", NULL
);
2104 if (start_command(&cp
))
2105 die(_("could not recurse into submodule '%s'"), sub
->path
);
2107 return finish_command(&cp
);
2110 static void submodule_reset_index(const char *path
, const char *super_prefix
)
2112 struct child_process cp
= CHILD_PROCESS_INIT
;
2114 if (validate_submodule_path(path
) < 0)
2117 prepare_submodule_repo_env(&cp
.env
);
2123 /* TODO: determine if this might overwright untracked files */
2124 strvec_pushl(&cp
.args
, "read-tree", "-u", "--reset", NULL
);
2125 strvec_pushf(&cp
.args
, "--super-prefix=%s%s/",
2126 (super_prefix
? super_prefix
: ""), path
);
2128 strvec_push(&cp
.args
, empty_tree_oid_hex());
2130 if (run_command(&cp
))
2131 die(_("could not reset submodule index"));
2135 * Moves a submodule at a given path from a given head to another new head.
2136 * For edge cases (a submodule coming into existence or removing a submodule)
2137 * pass NULL for old or new respectively.
2139 int submodule_move_head(const char *path
, const char *super_prefix
,
2140 const char *old_head
, const char *new_head
,
2144 struct child_process cp
= CHILD_PROCESS_INIT
;
2145 const struct submodule
*sub
;
2146 int *error_code_ptr
, error_code
;
2148 if (!is_submodule_active(the_repository
, path
))
2151 if (flags
& SUBMODULE_MOVE_HEAD_FORCE
)
2153 * Pass non NULL pointer to is_submodule_populated_gently
2154 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2155 * to fixup the submodule in the force case later.
2157 error_code_ptr
= &error_code
;
2159 error_code_ptr
= NULL
;
2161 if (old_head
&& !is_submodule_populated_gently(path
, error_code_ptr
))
2164 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2167 BUG("could not get submodule information for '%s'", path
);
2169 if (old_head
&& !(flags
& SUBMODULE_MOVE_HEAD_FORCE
)) {
2170 /* Check if the submodule has a dirty index. */
2171 if (submodule_has_dirty_index(sub
))
2172 return error(_("submodule '%s' has dirty index"), path
);
2175 if (!(flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)) {
2177 if (!submodule_uses_gitfile(path
))
2178 absorb_git_dir_into_superproject(path
,
2181 char *dotgit
= xstrfmt("%s/.git", path
);
2182 char *git_dir
= xstrdup(read_gitfile(dotgit
));
2185 if (validate_submodule_git_dir(git_dir
,
2187 die(_("refusing to create/use '%s' in "
2188 "another submodule's git dir"),
2193 struct strbuf gitdir
= STRBUF_INIT
;
2194 submodule_name_to_gitdir(&gitdir
, the_repository
,
2196 if (validate_submodule_git_dir(gitdir
.buf
,
2198 die(_("refusing to create/use '%s' in another "
2199 "submodule's git dir"),
2201 connect_work_tree_and_git_dir(path
, gitdir
.buf
, 0);
2202 strbuf_release(&gitdir
);
2204 /* make sure the index is clean as well */
2205 submodule_reset_index(path
, super_prefix
);
2208 if (old_head
&& (flags
& SUBMODULE_MOVE_HEAD_FORCE
)) {
2209 struct strbuf gitdir
= STRBUF_INIT
;
2210 submodule_name_to_gitdir(&gitdir
, the_repository
,
2212 connect_work_tree_and_git_dir(path
, gitdir
.buf
, 1);
2213 strbuf_release(&gitdir
);
2217 prepare_submodule_repo_env(&cp
.env
);
2223 strvec_pushl(&cp
.args
, "read-tree", "--recurse-submodules", NULL
);
2224 strvec_pushf(&cp
.args
, "--super-prefix=%s%s/",
2225 (super_prefix
? super_prefix
: ""), path
);
2227 if (flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)
2228 strvec_push(&cp
.args
, "-n");
2230 strvec_push(&cp
.args
, "-u");
2232 if (flags
& SUBMODULE_MOVE_HEAD_FORCE
)
2233 strvec_push(&cp
.args
, "--reset");
2235 strvec_push(&cp
.args
, "-m");
2237 if (!(flags
& SUBMODULE_MOVE_HEAD_FORCE
))
2238 strvec_push(&cp
.args
, old_head
? old_head
: empty_tree_oid_hex());
2240 strvec_push(&cp
.args
, new_head
? new_head
: empty_tree_oid_hex());
2242 if (run_command(&cp
)) {
2243 ret
= error(_("Submodule '%s' could not be updated."), path
);
2247 if (!(flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)) {
2249 child_process_init(&cp
);
2250 /* also set the HEAD accordingly */
2255 prepare_submodule_repo_env(&cp
.env
);
2256 strvec_pushl(&cp
.args
, "update-ref", "HEAD",
2257 "--no-deref", new_head
, NULL
);
2259 if (run_command(&cp
)) {
2264 struct strbuf sb
= STRBUF_INIT
;
2266 strbuf_addf(&sb
, "%s/.git", path
);
2267 unlink_or_warn(sb
.buf
);
2268 strbuf_release(&sb
);
2270 if (is_empty_dir(path
))
2271 rmdir_or_warn(path
);
2273 submodule_unset_core_worktree(sub
);
2280 int validate_submodule_git_dir(char *git_dir
, const char *submodule_name
)
2282 size_t len
= strlen(git_dir
), suffix_len
= strlen(submodule_name
);
2286 if (len
<= suffix_len
|| (p
= git_dir
+ len
- suffix_len
)[-1] != '/' ||
2287 strcmp(p
, submodule_name
))
2288 BUG("submodule name '%s' not a suffix of git dir '%s'",
2289 submodule_name
, git_dir
);
2292 * We prevent the contents of sibling submodules' git directories to
2295 * Example: having a submodule named `hippo` and another one named
2296 * `hippo/hooks` would result in the git directories
2297 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2298 * but the latter directory is already designated to contain the hooks
2302 if (is_dir_sep(*p
)) {
2306 if (is_git_directory(git_dir
))
2311 return error(_("submodule git dir '%s' is "
2312 "inside git dir '%.*s'"),
2314 (int)(p
- git_dir
), git_dir
);
2321 int validate_submodule_path(const char *path
)
2323 char *p
= xstrdup(path
);
2328 for (i
= 0; !ret
&& p
[i
]; i
++) {
2329 if (!is_dir_sep(p
[i
]))
2334 /* allow missing components, but no symlinks */
2335 ret
= lstat(p
, &st
) || !S_ISLNK(st
.st_mode
) ? 0 : -1;
2338 error(_("expected '%.*s' in submodule path '%s' not to "
2339 "be a symbolic link"), i
, p
, p
);
2341 if (!lstat(p
, &st
) && S_ISLNK(st
.st_mode
))
2342 ret
= error(_("expected submodule path '%s' not to be a "
2343 "symbolic link"), p
);
2350 * Embeds a single submodules git directory into the superprojects git dir,
2353 static void relocate_single_git_dir_into_superproject(const char *path
,
2354 const char *super_prefix
)
2356 char *old_git_dir
= NULL
, *real_old_git_dir
= NULL
, *real_new_git_dir
= NULL
;
2357 struct strbuf new_gitdir
= STRBUF_INIT
;
2358 const struct submodule
*sub
;
2360 if (validate_submodule_path(path
) < 0)
2363 if (submodule_uses_worktrees(path
))
2364 die(_("relocate_gitdir for submodule '%s' with "
2365 "more than one worktree not supported"), path
);
2367 old_git_dir
= xstrfmt("%s/.git", path
);
2368 if (read_gitfile(old_git_dir
))
2369 /* If it is an actual gitfile, it doesn't need migration. */
2372 real_old_git_dir
= real_pathdup(old_git_dir
, 1);
2374 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2376 die(_("could not lookup name for submodule '%s'"), path
);
2378 submodule_name_to_gitdir(&new_gitdir
, the_repository
, sub
->name
);
2379 if (validate_submodule_git_dir(new_gitdir
.buf
, sub
->name
) < 0)
2380 die(_("refusing to move '%s' into an existing git dir"),
2382 if (safe_create_leading_directories_const(new_gitdir
.buf
) < 0)
2383 die(_("could not create directory '%s'"), new_gitdir
.buf
);
2384 real_new_git_dir
= real_pathdup(new_gitdir
.buf
, 1);
2386 fprintf(stderr
, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
2387 super_prefix
? super_prefix
: "", path
,
2388 real_old_git_dir
, real_new_git_dir
);
2390 relocate_gitdir(path
, real_old_git_dir
, real_new_git_dir
);
2393 free(real_old_git_dir
);
2394 free(real_new_git_dir
);
2395 strbuf_release(&new_gitdir
);
2398 static void absorb_git_dir_into_superproject_recurse(const char *path
,
2399 const char *super_prefix
)
2402 struct child_process cp
= CHILD_PROCESS_INIT
;
2404 if (validate_submodule_path(path
) < 0)
2410 strvec_pushl(&cp
.args
, "submodule--helper",
2411 "absorbgitdirs", NULL
);
2412 strvec_pushf(&cp
.args
, "--super-prefix=%s%s/", super_prefix
?
2413 super_prefix
: "", path
);
2415 prepare_submodule_repo_env(&cp
.env
);
2416 if (run_command(&cp
))
2417 die(_("could not recurse into submodule '%s'"), path
);
2421 * Migrate the git directory of the submodule given by path from
2422 * having its git directory within the working tree to the git dir nested
2423 * in its superprojects git dir under modules/.
2425 void absorb_git_dir_into_superproject(const char *path
,
2426 const char *super_prefix
)
2429 const char *sub_git_dir
;
2430 struct strbuf gitdir
= STRBUF_INIT
;
2432 if (validate_submodule_path(path
) < 0)
2435 strbuf_addf(&gitdir
, "%s/.git", path
);
2436 sub_git_dir
= resolve_gitdir_gently(gitdir
.buf
, &err_code
);
2438 /* Not populated? */
2440 const struct submodule
*sub
;
2441 struct strbuf sub_gitdir
= STRBUF_INIT
;
2443 if (err_code
== READ_GITFILE_ERR_STAT_FAILED
) {
2444 /* unpopulated as expected */
2445 strbuf_release(&gitdir
);
2449 if (err_code
!= READ_GITFILE_ERR_NOT_A_REPO
)
2450 /* We don't know what broke here. */
2451 read_gitfile_error_die(err_code
, path
, NULL
);
2454 * Maybe populated, but no git directory was found?
2455 * This can happen if the superproject is a submodule
2456 * itself and was just absorbed. The absorption of the
2457 * superproject did not rewrite the git file links yet,
2460 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2462 die(_("could not lookup name for submodule '%s'"), path
);
2463 submodule_name_to_gitdir(&sub_gitdir
, the_repository
, sub
->name
);
2464 connect_work_tree_and_git_dir(path
, sub_gitdir
.buf
, 0);
2465 strbuf_release(&sub_gitdir
);
2467 /* Is it already absorbed into the superprojects git dir? */
2468 char *real_sub_git_dir
= real_pathdup(sub_git_dir
, 1);
2469 char *real_common_git_dir
= real_pathdup(get_git_common_dir(), 1);
2471 if (!starts_with(real_sub_git_dir
, real_common_git_dir
))
2472 relocate_single_git_dir_into_superproject(path
, super_prefix
);
2474 free(real_sub_git_dir
);
2475 free(real_common_git_dir
);
2477 strbuf_release(&gitdir
);
2479 absorb_git_dir_into_superproject_recurse(path
, super_prefix
);
2482 int get_superproject_working_tree(struct strbuf
*buf
)
2484 struct child_process cp
= CHILD_PROCESS_INIT
;
2485 struct strbuf sb
= STRBUF_INIT
;
2486 struct strbuf one_up
= STRBUF_INIT
;
2487 char *cwd
= xgetcwd();
2489 const char *subpath
;
2493 if (!is_inside_work_tree())
2496 * We might have a superproject, but it is harder
2501 if (!strbuf_realpath(&one_up
, "../", 0))
2504 subpath
= relative_path(cwd
, one_up
.buf
, &sb
);
2505 strbuf_release(&one_up
);
2507 prepare_submodule_repo_env(&cp
.env
);
2508 strvec_pop(&cp
.env
);
2510 strvec_pushl(&cp
.args
, "--literal-pathspecs", "-C", "..",
2511 "ls-files", "-z", "--stage", "--full-name", "--",
2520 if (start_command(&cp
))
2521 die(_("could not start ls-files in .."));
2523 len
= strbuf_read(&sb
, cp
.out
, PATH_MAX
);
2526 if (starts_with(sb
.buf
, "160000")) {
2528 int cwd_len
= strlen(cwd
);
2529 char *super_sub
, *super_wt
;
2532 * There is a superproject having this repo as a submodule.
2533 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2534 * We're only interested in the name after the tab.
2536 super_sub
= strchr(sb
.buf
, '\t') + 1;
2537 super_sub_len
= strlen(super_sub
);
2539 if (super_sub_len
> cwd_len
||
2540 strcmp(&cwd
[cwd_len
- super_sub_len
], super_sub
))
2541 BUG("returned path string doesn't match cwd?");
2543 super_wt
= xstrdup(cwd
);
2544 super_wt
[cwd_len
- super_sub_len
] = '\0';
2546 strbuf_realpath(buf
, super_wt
, 1);
2551 strbuf_release(&sb
);
2553 code
= finish_command(&cp
);
2556 /* '../' is not a git repository */
2558 if (code
== 0 && len
== 0)
2559 /* There is an unrelated git repository at '../' */
2562 die(_("ls-tree returned unexpected return code %d"), code
);
2568 * Put the gitdir for a submodule (given relative to the main
2569 * repository worktree) into `buf`, or return -1 on error.
2571 int submodule_to_gitdir(struct strbuf
*buf
, const char *submodule
)
2573 const struct submodule
*sub
;
2574 const char *git_dir
;
2577 if (validate_submodule_path(submodule
) < 0)
2581 strbuf_addstr(buf
, submodule
);
2582 strbuf_complete(buf
, '/');
2583 strbuf_addstr(buf
, ".git");
2585 git_dir
= read_gitfile(buf
->buf
);
2588 strbuf_addstr(buf
, git_dir
);
2590 if (!is_git_directory(buf
->buf
)) {
2591 sub
= submodule_from_path(the_repository
, null_oid(),
2598 submodule_name_to_gitdir(buf
, the_repository
, sub
->name
);
2605 void submodule_name_to_gitdir(struct strbuf
*buf
, struct repository
*r
,
2606 const char *submodule_name
)
2609 * NEEDSWORK: The current way of mapping a submodule's name to
2610 * its location in .git/modules/ has problems with some naming
2611 * schemes. For example, if a submodule is named "foo" and
2612 * another is named "foo/bar" (whether present in the same
2613 * superproject commit or not - the problem will arise if both
2614 * superproject commits have been checked out at any point in
2615 * time), or if two submodule names only have different cases in
2616 * a case-insensitive filesystem.
2618 * There are several solutions, including encoding the path in
2619 * some way, introducing a submodule.<name>.gitdir config in
2620 * .git/config (not .gitmodules) that allows overriding what the
2621 * gitdir of a submodule would be (and teach Git, upon noticing
2622 * a clash, to automatically determine a non-clashing name and
2623 * to write such a config), or introducing a
2624 * submodule.<name>.gitdir config in .gitmodules that repo
2625 * administrators can explicitly set. Nothing has been decided,
2626 * so for now, just append the name at the end of the path.
2628 strbuf_repo_git_path(buf
, r
, "modules/");
2629 strbuf_addstr(buf
, submodule_name
);