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"
20 #include "thread-utils.h"
24 #include "parse-options.h"
25 #include "object-file.h"
26 #include "object-name.h"
27 #include "object-store-ll.h"
28 #include "commit-reach.h"
29 #include "read-cache-ll.h"
33 static int config_update_recurse_submodules
= RECURSE_SUBMODULES_OFF
;
34 static int initialized_fetch_ref_tips
;
35 static struct oid_array ref_tips_before_fetch
;
36 static struct oid_array ref_tips_after_fetch
;
39 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
40 * will be disabled because we can't guess what might be configured in
41 * .gitmodules unless the user resolves the conflict.
43 int is_gitmodules_unmerged(struct index_state
*istate
)
45 int pos
= index_name_pos(istate
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
46 if (pos
< 0) { /* .gitmodules not found or isn't merged */
48 if (istate
->cache_nr
> pos
) { /* there is a .gitmodules */
49 const struct cache_entry
*ce
= istate
->cache
[pos
];
50 if (ce_namelen(ce
) == strlen(GITMODULES_FILE
) &&
51 !strcmp(ce
->name
, GITMODULES_FILE
))
60 * Check if the .gitmodules file is safe to write.
62 * Writing to the .gitmodules file requires that the file exists in the
63 * working tree or, if it doesn't, that a brand new .gitmodules file is going
64 * to be created (i.e. it's neither in the index nor in the current branch).
66 * It is not safe to write to .gitmodules if it's not in the working tree but
67 * it is in the index or in the current branch, because writing new values
68 * (and staging them) would blindly overwrite ALL the old content.
70 int is_writing_gitmodules_ok(void)
73 return file_exists(GITMODULES_FILE
) ||
74 (repo_get_oid(the_repository
, GITMODULES_INDEX
, &oid
) < 0 && repo_get_oid(the_repository
, GITMODULES_HEAD
, &oid
) < 0);
78 * Check if the .gitmodules file has unstaged modifications. This must be
79 * checked before allowing modifications to the .gitmodules file with the
80 * intention to stage them later, because when continuing we would stage the
81 * modifications the user didn't stage herself too. That might change in a
82 * future version when we learn to stage the changes we do ourselves without
83 * staging any previous modifications.
85 int is_staging_gitmodules_ok(struct index_state
*istate
)
87 int pos
= index_name_pos(istate
, GITMODULES_FILE
, strlen(GITMODULES_FILE
));
89 if ((pos
>= 0) && (pos
< istate
->cache_nr
)) {
91 if (lstat(GITMODULES_FILE
, &st
) == 0 &&
92 ie_modified(istate
, istate
->cache
[pos
], &st
, 0) & DATA_CHANGED
)
99 static int for_each_remote_ref_submodule(const char *submodule
,
100 each_ref_fn fn
, void *cb_data
)
102 return refs_for_each_remote_ref(get_submodule_ref_store(submodule
),
107 * Try to update the "path" entry in the "submodule.<name>" section of the
108 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
109 * with the correct path=<oldpath> setting was found and we could update it.
111 int update_path_in_gitmodules(const char *oldpath
, const char *newpath
)
113 struct strbuf entry
= STRBUF_INIT
;
114 const struct submodule
*submodule
;
117 if (!file_exists(GITMODULES_FILE
)) /* Do nothing without .gitmodules */
120 if (is_gitmodules_unmerged(the_repository
->index
))
121 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
123 submodule
= submodule_from_path(the_repository
, null_oid(), oldpath
);
124 if (!submodule
|| !submodule
->name
) {
125 warning(_("Could not find section in .gitmodules where path=%s"), oldpath
);
128 strbuf_addstr(&entry
, "submodule.");
129 strbuf_addstr(&entry
, submodule
->name
);
130 strbuf_addstr(&entry
, ".path");
131 ret
= config_set_in_gitmodules_file_gently(entry
.buf
, newpath
);
132 strbuf_release(&entry
);
137 * Try to remove the "submodule.<name>" section from .gitmodules where the given
138 * path is configured. Return 0 only if a .gitmodules file was found, a section
139 * with the correct path=<path> setting was found and we could remove it.
141 int remove_path_from_gitmodules(const char *path
)
143 struct strbuf sect
= STRBUF_INIT
;
144 const struct submodule
*submodule
;
146 if (!file_exists(GITMODULES_FILE
)) /* Do nothing without .gitmodules */
149 if (is_gitmodules_unmerged(the_repository
->index
))
150 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
152 submodule
= submodule_from_path(the_repository
, null_oid(), path
);
153 if (!submodule
|| !submodule
->name
) {
154 warning(_("Could not find section in .gitmodules where path=%s"), path
);
157 strbuf_addstr(§
, "submodule.");
158 strbuf_addstr(§
, submodule
->name
);
159 if (git_config_rename_section_in_file(GITMODULES_FILE
, sect
.buf
, NULL
) < 0) {
160 /* Maybe the user already did that, don't error out here */
161 warning(_("Could not remove .gitmodules entry for %s"), path
);
162 strbuf_release(§
);
165 strbuf_release(§
);
169 void stage_updated_gitmodules(struct index_state
*istate
)
171 if (add_file_to_index(istate
, GITMODULES_FILE
, 0))
172 die(_("staging updated .gitmodules failed"));
175 static struct string_list added_submodule_odb_paths
= STRING_LIST_INIT_NODUP
;
177 void add_submodule_odb_by_path(const char *path
)
179 string_list_insert(&added_submodule_odb_paths
, xstrdup(path
));
182 int register_all_submodule_odb_as_alternates(void)
185 int ret
= added_submodule_odb_paths
.nr
;
187 for (i
= 0; i
< added_submodule_odb_paths
.nr
; i
++)
188 add_to_alternates_memory(added_submodule_odb_paths
.items
[i
].string
);
190 string_list_clear(&added_submodule_odb_paths
, 0);
191 trace2_data_intmax("submodule", the_repository
,
192 "register_all_submodule_odb_as_alternates/registered", ret
);
193 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
194 BUG("register_all_submodule_odb_as_alternates() called");
199 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
202 const struct submodule
*submodule
= submodule_from_path(the_repository
,
209 key
= xstrfmt("submodule.%s.ignore", submodule
->name
);
210 if (repo_config_get_string_tmp(the_repository
, key
, &ignore
))
211 ignore
= submodule
->ignore
;
215 handle_ignore_submodules_arg(diffopt
, ignore
);
216 else if (is_gitmodules_unmerged(the_repository
->index
))
217 diffopt
->flags
.ignore_submodules
= 1;
221 /* Cheap function that only determines if we're interested in submodules at all */
222 int git_default_submodule_config(const char *var
, const char *value
,
225 if (!strcmp(var
, "submodule.recurse")) {
226 int v
= git_config_bool(var
, value
) ?
227 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
228 config_update_recurse_submodules
= v
;
233 int option_parse_recurse_submodules_worktree_updater(const struct option
*opt
,
234 const char *arg
, int unset
)
237 config_update_recurse_submodules
= RECURSE_SUBMODULES_OFF
;
241 config_update_recurse_submodules
=
242 parse_update_recurse_submodules_arg(opt
->long_name
,
245 config_update_recurse_submodules
= RECURSE_SUBMODULES_ON
;
251 * Determine if a submodule has been initialized at a given 'path'
254 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
255 * ie, the config looks like: "[submodule] active\n".
256 * Since that is an invalid pathspec, we should inform the user.
258 int is_tree_submodule_active(struct repository
*repo
,
259 const struct object_id
*treeish_name
,
265 const struct string_list
*sl
;
266 const struct submodule
*module
;
268 module
= submodule_from_path(repo
, treeish_name
, path
);
270 /* early return if there isn't a path->module mapping */
274 /* submodule.<name>.active is set */
275 key
= xstrfmt("submodule.%s.active", module
->name
);
276 if (!repo_config_get_bool(repo
, key
, &ret
)) {
282 /* submodule.active is set */
283 if (!repo_config_get_string_multi(repo
, "submodule.active", &sl
)) {
285 struct strvec args
= STRVEC_INIT
;
286 const struct string_list_item
*item
;
288 for_each_string_list_item(item
, sl
) {
289 strvec_push(&args
, item
->string
);
292 parse_pathspec(&ps
, 0, 0, NULL
, args
.v
);
293 ret
= match_pathspec(repo
->index
, &ps
, path
, strlen(path
), 0, NULL
, 1);
300 /* fallback to checking if the URL is set */
301 key
= xstrfmt("submodule.%s.url", module
->name
);
302 ret
= !repo_config_get_string(repo
, key
, &value
);
309 int is_submodule_active(struct repository
*repo
, const char *path
)
311 return is_tree_submodule_active(repo
, null_oid(), path
);
314 int is_submodule_populated_gently(const char *path
, int *return_error_code
)
317 char *gitdir
= xstrfmt("%s/.git", path
);
319 if (resolve_gitdir_gently(gitdir
, return_error_code
))
327 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
329 void die_in_unpopulated_submodule(struct index_state
*istate
,
337 prefixlen
= strlen(prefix
);
339 for (i
= 0; i
< istate
->cache_nr
; i
++) {
340 struct cache_entry
*ce
= istate
->cache
[i
];
341 int ce_len
= ce_namelen(ce
);
343 if (!S_ISGITLINK(ce
->ce_mode
))
345 if (prefixlen
<= ce_len
)
347 if (strncmp(ce
->name
, prefix
, ce_len
))
349 if (prefix
[ce_len
] != '/')
352 die(_("in unpopulated submodule '%s'"), ce
->name
);
357 * Dies if any paths in the provided pathspec descends into a submodule
359 void die_path_inside_submodule(struct index_state
*istate
,
360 const struct pathspec
*ps
)
364 for (i
= 0; i
< istate
->cache_nr
; i
++) {
365 struct cache_entry
*ce
= istate
->cache
[i
];
366 int ce_len
= ce_namelen(ce
);
368 if (!S_ISGITLINK(ce
->ce_mode
))
371 for (j
= 0; j
< ps
->nr
; j
++) {
372 const struct pathspec_item
*item
= &ps
->items
[j
];
374 if (item
->len
<= ce_len
)
376 if (item
->match
[ce_len
] != '/')
378 if (strncmp(ce
->name
, item
->match
, ce_len
))
380 if (item
->len
== ce_len
+ 1)
383 die(_("Pathspec '%s' is in submodule '%.*s'"),
384 item
->original
, ce_len
, ce
->name
);
389 enum submodule_update_type
parse_submodule_update_type(const char *value
)
391 if (!strcmp(value
, "none"))
392 return SM_UPDATE_NONE
;
393 else if (!strcmp(value
, "checkout"))
394 return SM_UPDATE_CHECKOUT
;
395 else if (!strcmp(value
, "rebase"))
396 return SM_UPDATE_REBASE
;
397 else if (!strcmp(value
, "merge"))
398 return SM_UPDATE_MERGE
;
399 else if (*value
== '!')
400 return SM_UPDATE_COMMAND
;
402 return SM_UPDATE_UNSPECIFIED
;
405 int parse_submodule_update_strategy(const char *value
,
406 struct submodule_update_strategy
*dst
)
408 enum submodule_update_type type
;
410 free((void*)dst
->command
);
413 type
= parse_submodule_update_type(value
);
414 if (type
== SM_UPDATE_UNSPECIFIED
)
418 if (type
== SM_UPDATE_COMMAND
)
419 dst
->command
= xstrdup(value
+ 1);
424 const char *submodule_update_type_to_string(enum submodule_update_type type
)
427 case SM_UPDATE_CHECKOUT
:
429 case SM_UPDATE_MERGE
:
431 case SM_UPDATE_REBASE
:
435 case SM_UPDATE_UNSPECIFIED
:
436 case SM_UPDATE_COMMAND
:
437 BUG("init_submodule() should handle type %d", type
);
439 BUG("unexpected update strategy type: %d", type
);
443 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
446 diffopt
->flags
.ignore_submodule_set
= 1;
447 diffopt
->flags
.ignore_submodules
= 0;
448 diffopt
->flags
.ignore_untracked_in_submodules
= 0;
449 diffopt
->flags
.ignore_dirty_submodules
= 0;
451 if (!strcmp(arg
, "all"))
452 diffopt
->flags
.ignore_submodules
= 1;
453 else if (!strcmp(arg
, "untracked"))
454 diffopt
->flags
.ignore_untracked_in_submodules
= 1;
455 else if (!strcmp(arg
, "dirty"))
456 diffopt
->flags
.ignore_dirty_submodules
= 1;
457 else if (strcmp(arg
, "none"))
458 die(_("bad --ignore-submodules argument: %s"), arg
);
460 * Please update _git_status() in git-completion.bash when you
465 static int prepare_submodule_diff_summary(struct repository
*r
, struct rev_info
*rev
,
467 struct commit
*left
, struct commit
*right
,
468 struct commit_list
*merge_bases
)
470 struct commit_list
*list
;
472 repo_init_revisions(r
, rev
, NULL
);
473 setup_revisions(0, NULL
, rev
, NULL
);
475 rev
->first_parent_only
= 1;
476 left
->object
.flags
|= SYMMETRIC_LEFT
;
477 add_pending_object(rev
, &left
->object
, path
);
478 add_pending_object(rev
, &right
->object
, path
);
479 for (list
= merge_bases
; list
; list
= list
->next
) {
480 list
->item
->object
.flags
|= UNINTERESTING
;
481 add_pending_object(rev
, &list
->item
->object
,
482 oid_to_hex(&list
->item
->object
.oid
));
484 return prepare_revision_walk(rev
);
487 static void print_submodule_diff_summary(struct repository
*r
, struct rev_info
*rev
, struct diff_options
*o
)
489 static const char format
[] = " %m %s";
490 struct strbuf sb
= STRBUF_INIT
;
491 struct commit
*commit
;
493 while ((commit
= get_revision(rev
))) {
494 struct pretty_print_context ctx
= {0};
495 ctx
.date_mode
= rev
->date_mode
;
496 ctx
.output_encoding
= get_log_output_encoding();
497 strbuf_setlen(&sb
, 0);
498 repo_format_commit_message(r
, commit
, format
, &sb
,
500 strbuf_addch(&sb
, '\n');
501 if (commit
->object
.flags
& SYMMETRIC_LEFT
)
502 diff_emit_submodule_del(o
, sb
.buf
);
504 diff_emit_submodule_add(o
, sb
.buf
);
509 void prepare_submodule_repo_env(struct strvec
*out
)
511 prepare_other_repo_env(out
, DEFAULT_GIT_DIR_ENVIRONMENT
);
514 static void prepare_submodule_repo_env_in_gitdir(struct strvec
*out
)
516 prepare_other_repo_env(out
, ".");
520 * Initialize a repository struct for a submodule based on the provided 'path'.
522 * Returns the repository struct on success,
523 * NULL when the submodule is not present.
525 static struct repository
*open_submodule(const char *path
)
527 struct strbuf sb
= STRBUF_INIT
;
528 struct repository
*out
= xmalloc(sizeof(*out
));
530 if (submodule_to_gitdir(&sb
, path
) || repo_init(out
, sb
.buf
, NULL
)) {
536 /* Mark it as a submodule */
537 out
->submodule_prefix
= xstrdup(path
);
544 * Helper function to display the submodule header line prior to the full
547 * If it can locate the submodule git directory it will create a repository
548 * handle for the submodule and lookup both the left and right commits and
549 * put them into the left and right pointers.
551 static void show_submodule_header(struct diff_options
*o
,
553 struct object_id
*one
, struct object_id
*two
,
554 unsigned dirty_submodule
,
555 struct repository
*sub
,
556 struct commit
**left
, struct commit
**right
,
557 struct commit_list
**merge_bases
)
559 const char *message
= NULL
;
560 struct strbuf sb
= STRBUF_INIT
;
561 int fast_forward
= 0, fast_backward
= 0;
563 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
564 diff_emit_submodule_untracked(o
, path
);
566 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
567 diff_emit_submodule_modified(o
, path
);
569 if (is_null_oid(one
))
570 message
= "(new submodule)";
571 else if (is_null_oid(two
))
572 message
= "(submodule deleted)";
576 message
= "(commits not present)";
581 * Attempt to lookup the commit references, and determine if this is
582 * a fast forward or fast backwards update.
584 *left
= lookup_commit_reference(sub
, one
);
585 *right
= lookup_commit_reference(sub
, two
);
588 * Warn about missing commits in the submodule project, but only if
591 if ((!is_null_oid(one
) && !*left
) ||
592 (!is_null_oid(two
) && !*right
))
593 message
= "(commits not present)";
596 if (repo_get_merge_bases(sub
, *left
, *right
, merge_bases
) < 0) {
597 message
= "(corrupt repository)";
602 if ((*merge_bases
)->item
== *left
)
604 else if ((*merge_bases
)->item
== *right
)
608 if (oideq(one
, two
)) {
614 strbuf_addf(&sb
, "Submodule %s ", path
);
615 strbuf_add_unique_abbrev(&sb
, one
, DEFAULT_ABBREV
);
616 strbuf_addstr(&sb
, (fast_backward
|| fast_forward
) ? ".." : "...");
617 strbuf_add_unique_abbrev(&sb
, two
, DEFAULT_ABBREV
);
619 strbuf_addf(&sb
, " %s\n", message
);
621 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
622 diff_emit_submodule_header(o
, sb
.buf
);
627 void show_submodule_diff_summary(struct diff_options
*o
, const char *path
,
628 struct object_id
*one
, struct object_id
*two
,
629 unsigned dirty_submodule
)
631 struct rev_info rev
= REV_INFO_INIT
;
632 struct commit
*left
= NULL
, *right
= NULL
;
633 struct commit_list
*merge_bases
= NULL
;
634 struct repository
*sub
;
636 sub
= open_submodule(path
);
637 show_submodule_header(o
, path
, one
, two
, dirty_submodule
,
638 sub
, &left
, &right
, &merge_bases
);
641 * If we don't have both a left and a right pointer, there is no
642 * reason to try and display a summary. The header line should contain
643 * all the information the user needs.
645 if (!left
|| !right
|| !sub
)
648 /* Treat revision walker failure the same as missing commits */
649 if (prepare_submodule_diff_summary(sub
, &rev
, path
, left
, right
, merge_bases
)) {
650 diff_emit_submodule_error(o
, "(revision walker failed)\n");
654 print_submodule_diff_summary(sub
, &rev
, o
);
657 free_commit_list(merge_bases
);
658 release_revisions(&rev
);
659 clear_commit_marks(left
, ~0);
660 clear_commit_marks(right
, ~0);
667 void show_submodule_inline_diff(struct diff_options
*o
, const char *path
,
668 struct object_id
*one
, struct object_id
*two
,
669 unsigned dirty_submodule
)
671 const struct object_id
*old_oid
= the_hash_algo
->empty_tree
, *new_oid
= the_hash_algo
->empty_tree
;
672 struct commit
*left
= NULL
, *right
= NULL
;
673 struct commit_list
*merge_bases
= NULL
;
674 struct child_process cp
= CHILD_PROCESS_INIT
;
675 struct strbuf sb
= STRBUF_INIT
;
676 struct repository
*sub
;
678 sub
= open_submodule(path
);
679 show_submodule_header(o
, path
, one
, two
, dirty_submodule
,
680 sub
, &left
, &right
, &merge_bases
);
682 /* We need a valid left and right commit to display a difference */
683 if (!(left
|| is_null_oid(one
)) ||
684 !(right
|| is_null_oid(two
)))
697 /* TODO: other options may need to be passed here. */
698 strvec_pushl(&cp
.args
, "diff", "--submodule=diff", NULL
);
699 strvec_pushf(&cp
.args
, "--color=%s", want_color(o
->use_color
) ?
702 if (o
->flags
.reverse_diff
) {
703 strvec_pushf(&cp
.args
, "--src-prefix=%s%s/",
705 strvec_pushf(&cp
.args
, "--dst-prefix=%s%s/",
708 strvec_pushf(&cp
.args
, "--src-prefix=%s%s/",
710 strvec_pushf(&cp
.args
, "--dst-prefix=%s%s/",
713 strvec_push(&cp
.args
, oid_to_hex(old_oid
));
715 * If the submodule has modified content, we will diff against the
716 * work tree, under the assumption that the user has asked for the
717 * diff format and wishes to actually see all differences even if they
718 * haven't yet been committed to the submodule yet.
720 if (!(dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
))
721 strvec_push(&cp
.args
, oid_to_hex(new_oid
));
723 prepare_submodule_repo_env(&cp
.env
);
725 if (!is_directory(path
)) {
726 /* fall back to absorbed git dir, if any */
729 cp
.dir
= sub
->gitdir
;
730 strvec_push(&cp
.env
, GIT_DIR_ENVIRONMENT
"=.");
731 strvec_push(&cp
.env
, GIT_WORK_TREE_ENVIRONMENT
"=.");
734 if (start_command(&cp
)) {
735 diff_emit_submodule_error(o
, "(diff failed)\n");
739 while (strbuf_getwholeline_fd(&sb
, cp
.out
, '\n') != EOF
)
740 diff_emit_submodule_pipethrough(o
, sb
.buf
, sb
.len
);
742 if (finish_command(&cp
))
743 diff_emit_submodule_error(o
, "(diff failed)\n");
747 free_commit_list(merge_bases
);
749 clear_commit_marks(left
, ~0);
751 clear_commit_marks(right
, ~0);
758 int should_update_submodules(void)
760 return config_update_recurse_submodules
== RECURSE_SUBMODULES_ON
;
763 const struct submodule
*submodule_from_ce(const struct cache_entry
*ce
)
765 if (!S_ISGITLINK(ce
->ce_mode
))
768 if (!should_update_submodules())
771 return submodule_from_path(the_repository
, null_oid(), ce
->name
);
775 struct collect_changed_submodules_cb_data
{
776 struct repository
*repo
;
777 struct string_list
*changed
;
778 const struct object_id
*commit_oid
;
782 * this would normally be two functions: default_name_from_path() and
783 * path_from_default_name(). Since the default name is the same as
784 * the submodule path we can get away with just one function which only
785 * checks whether there is a submodule in the working directory at that
788 static const char *default_name_or_path(const char *path_or_name
)
792 if (!is_submodule_populated_gently(path_or_name
, &error_code
))
799 * Holds relevant information for a changed submodule. Used as the .util
800 * member of the changed submodule name string_list_item.
802 * (super_oid, path) allows the submodule config to be read from _some_
803 * .gitmodules file. We store this information the first time we find a
804 * superproject commit that points to the submodule, but this is
805 * arbitrary - we can choose any (super_oid, path) that matches the
808 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
809 * guarantee that we're reading the commit that the user would expect. A better
810 * scheme would be to just fetch a submodule by its name. This requires two
812 * - Create a function that behaves like repo_submodule_init(), but accepts a
813 * submodule name instead of treeish_name and path. This should be easy
814 * because repo_submodule_init() internally uses the submodule's name.
816 * - Replace most instances of 'struct submodule' (which is the .gitmodules
817 * config) with just the submodule name. This is OK because we expect
818 * submodule settings to be stored in .git/config (via "git submodule init"),
819 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
820 * which constructs a bogus 'struct submodule' for the sake of giving a
821 * placeholder name to a gitlink.
823 struct changed_submodule_data
{
825 * The first superproject commit in the rev walk that points to
828 const struct object_id
*super_oid
;
830 * Path to the submodule in the superproject commit referenced
834 /* The submodule commits that have changed in the rev walk. */
835 struct oid_array new_commits
;
838 static void changed_submodule_data_clear(struct changed_submodule_data
*cs_data
)
840 oid_array_clear(&cs_data
->new_commits
);
844 static void collect_changed_submodules_cb(struct diff_queue_struct
*q
,
845 struct diff_options
*options UNUSED
,
848 struct collect_changed_submodules_cb_data
*me
= data
;
849 struct string_list
*changed
= me
->changed
;
850 const struct object_id
*commit_oid
= me
->commit_oid
;
853 for (i
= 0; i
< q
->nr
; i
++) {
854 struct diff_filepair
*p
= q
->queue
[i
];
855 const struct submodule
*submodule
;
857 struct string_list_item
*item
;
858 struct changed_submodule_data
*cs_data
;
860 if (!S_ISGITLINK(p
->two
->mode
))
863 submodule
= submodule_from_path(me
->repo
,
864 commit_oid
, p
->two
->path
);
866 name
= submodule
->name
;
868 name
= default_name_or_path(p
->two
->path
);
869 /* make sure name does not collide with existing one */
871 submodule
= submodule_from_name(me
->repo
,
874 warning(_("Submodule in commit %s at path: "
875 "'%s' collides with a submodule named "
876 "the same. Skipping it."),
877 oid_to_hex(commit_oid
), p
->two
->path
);
885 item
= string_list_insert(changed
, name
);
887 cs_data
= item
->util
;
889 item
->util
= xcalloc(1, sizeof(struct changed_submodule_data
));
890 cs_data
= item
->util
;
891 cs_data
->super_oid
= commit_oid
;
892 cs_data
->path
= xstrdup(p
->two
->path
);
894 oid_array_append(&cs_data
->new_commits
, &p
->two
->oid
);
899 * Collect the paths of submodules in 'changed' which have changed based on
900 * the revisions as specified in 'argv'. Each entry in 'changed' will also
901 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
902 * what the submodule pointers were updated to during the change.
904 static void collect_changed_submodules(struct repository
*r
,
905 struct string_list
*changed
,
909 const struct commit
*commit
;
911 struct setup_revision_opt s_r_opt
= {
912 .assume_dashdash
= 1,
915 save_warning
= warn_on_object_refname_ambiguity
;
916 warn_on_object_refname_ambiguity
= 0;
917 repo_init_revisions(r
, &rev
, NULL
);
918 setup_revisions(argv
->nr
, argv
->v
, &rev
, &s_r_opt
);
919 warn_on_object_refname_ambiguity
= save_warning
;
920 if (prepare_revision_walk(&rev
))
921 die(_("revision walk setup failed"));
923 while ((commit
= get_revision(&rev
))) {
924 struct rev_info diff_rev
;
925 struct collect_changed_submodules_cb_data data
;
927 data
.changed
= changed
;
928 data
.commit_oid
= &commit
->object
.oid
;
930 repo_init_revisions(r
, &diff_rev
, NULL
);
931 diff_rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
932 diff_rev
.diffopt
.format_callback
= collect_changed_submodules_cb
;
933 diff_rev
.diffopt
.format_callback_data
= &data
;
934 diff_rev
.dense_combined_merges
= 1;
935 diff_tree_combined_merge(commit
, &diff_rev
);
936 release_revisions(&diff_rev
);
939 reset_revision_walk();
940 release_revisions(&rev
);
943 static void free_submodules_data(struct string_list
*submodules
)
945 struct string_list_item
*item
;
946 for_each_string_list_item(item
, submodules
)
947 changed_submodule_data_clear(item
->util
);
949 string_list_clear(submodules
, 1);
952 static int has_remote(const char *refname UNUSED
,
953 const struct object_id
*oid UNUSED
,
954 int flags UNUSED
, void *cb_data UNUSED
)
959 static int append_oid_to_argv(const struct object_id
*oid
, void *data
)
961 struct strvec
*argv
= data
;
962 strvec_push(argv
, oid_to_hex(oid
));
966 struct has_commit_data
{
967 struct repository
*repo
;
970 const struct object_id
*super_oid
;
973 static int check_has_commit(const struct object_id
*oid
, void *data
)
975 struct has_commit_data
*cb
= data
;
976 struct repository subrepo
;
977 enum object_type type
;
979 if (repo_submodule_init(&subrepo
, cb
->repo
, cb
->path
, cb
->super_oid
)) {
981 /* subrepo failed to init, so don't clean it up. */
985 type
= oid_object_info(&subrepo
, oid
, NULL
);
992 * Object is missing or invalid. If invalid, an error message
993 * has already been printed.
998 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
999 cb
->path
, oid_to_hex(oid
), type_name(type
));
1002 repo_clear(&subrepo
);
1006 static int submodule_has_commits(struct repository
*r
,
1008 const struct object_id
*super_oid
,
1009 struct oid_array
*commits
)
1011 struct has_commit_data has_commit
= {
1015 .super_oid
= super_oid
1018 oid_array_for_each_unique(commits
, check_has_commit
, &has_commit
);
1020 if (has_commit
.result
) {
1022 * Even if the submodule is checked out and the commit is
1023 * present, make sure it exists in the submodule's object store
1024 * and that it is reachable from a ref.
1026 struct child_process cp
= CHILD_PROCESS_INIT
;
1027 struct strbuf out
= STRBUF_INIT
;
1029 strvec_pushl(&cp
.args
, "rev-list", "-n", "1", NULL
);
1030 oid_array_for_each_unique(commits
, append_oid_to_argv
, &cp
.args
);
1031 strvec_pushl(&cp
.args
, "--not", "--all", NULL
);
1033 prepare_submodule_repo_env(&cp
.env
);
1038 if (capture_command(&cp
, &out
, GIT_MAX_HEXSZ
+ 1) || out
.len
)
1039 has_commit
.result
= 0;
1041 strbuf_release(&out
);
1044 return has_commit
.result
;
1047 static int submodule_needs_pushing(struct repository
*r
,
1049 struct oid_array
*commits
)
1051 if (!submodule_has_commits(r
, path
, null_oid(), commits
))
1053 * NOTE: We do consider it safe to return "no" here. The
1054 * correct answer would be "We do not know" instead of
1055 * "No push needed", but it is quite hard to change
1056 * the submodule pointer without having the submodule
1057 * around. If a user did however change the submodules
1058 * without having the submodule around, this indicates
1059 * an expert who knows what they are doing or a
1060 * maintainer integrating work from other people. In
1061 * both cases it should be safe to skip this check.
1065 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
1066 struct child_process cp
= CHILD_PROCESS_INIT
;
1067 struct strbuf buf
= STRBUF_INIT
;
1068 int needs_pushing
= 0;
1070 strvec_push(&cp
.args
, "rev-list");
1071 oid_array_for_each_unique(commits
, append_oid_to_argv
, &cp
.args
);
1072 strvec_pushl(&cp
.args
, "--not", "--remotes", "-n", "1" , NULL
);
1074 prepare_submodule_repo_env(&cp
.env
);
1079 if (start_command(&cp
))
1080 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
1082 if (strbuf_read(&buf
, cp
.out
, the_hash_algo
->hexsz
+ 1))
1084 finish_command(&cp
);
1086 strbuf_release(&buf
);
1087 return needs_pushing
;
1093 int find_unpushed_submodules(struct repository
*r
,
1094 struct oid_array
*commits
,
1095 const char *remotes_name
,
1096 struct string_list
*needs_pushing
)
1098 struct string_list submodules
= STRING_LIST_INIT_DUP
;
1099 struct string_list_item
*name
;
1100 struct strvec argv
= STRVEC_INIT
;
1102 /* argv.v[0] will be ignored by setup_revisions */
1103 strvec_push(&argv
, "find_unpushed_submodules");
1104 oid_array_for_each_unique(commits
, append_oid_to_argv
, &argv
);
1105 strvec_push(&argv
, "--not");
1106 strvec_pushf(&argv
, "--remotes=%s", remotes_name
);
1108 collect_changed_submodules(r
, &submodules
, &argv
);
1110 for_each_string_list_item(name
, &submodules
) {
1111 struct changed_submodule_data
*cs_data
= name
->util
;
1112 const struct submodule
*submodule
;
1113 const char *path
= NULL
;
1115 submodule
= submodule_from_name(r
, null_oid(), name
->string
);
1117 path
= submodule
->path
;
1119 path
= default_name_or_path(name
->string
);
1124 if (submodule_needs_pushing(r
, path
, &cs_data
->new_commits
))
1125 string_list_insert(needs_pushing
, path
);
1128 free_submodules_data(&submodules
);
1129 strvec_clear(&argv
);
1131 return needs_pushing
->nr
;
1134 static int push_submodule(const char *path
,
1135 const struct remote
*remote
,
1136 const struct refspec
*rs
,
1137 const struct string_list
*push_options
,
1140 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
1141 struct child_process cp
= CHILD_PROCESS_INIT
;
1142 strvec_push(&cp
.args
, "push");
1144 * When recursing into a submodule, treat any "only" configurations as "on-
1145 * demand", since "only" would not work (we need all submodules to be pushed
1146 * in order to be able to push the superproject).
1148 strvec_push(&cp
.args
, "--recurse-submodules=only-is-on-demand");
1150 strvec_push(&cp
.args
, "--dry-run");
1152 if (push_options
&& push_options
->nr
) {
1153 const struct string_list_item
*item
;
1154 for_each_string_list_item(item
, push_options
)
1155 strvec_pushf(&cp
.args
, "--push-option=%s",
1159 if (remote
->origin
!= REMOTE_UNCONFIGURED
) {
1161 strvec_push(&cp
.args
, remote
->name
);
1162 for (i
= 0; i
< rs
->raw_nr
; i
++)
1163 strvec_push(&cp
.args
, rs
->raw
[i
]);
1166 prepare_submodule_repo_env(&cp
.env
);
1170 if (run_command(&cp
))
1179 * Perform a check in the submodule to see if the remote and refspec work.
1180 * Die if the submodule can't be pushed.
1182 static void submodule_push_check(const char *path
, const char *head
,
1183 const struct remote
*remote
,
1184 const struct refspec
*rs
)
1186 struct child_process cp
= CHILD_PROCESS_INIT
;
1189 strvec_push(&cp
.args
, "submodule--helper");
1190 strvec_push(&cp
.args
, "push-check");
1191 strvec_push(&cp
.args
, head
);
1192 strvec_push(&cp
.args
, remote
->name
);
1194 for (i
= 0; i
< rs
->raw_nr
; i
++)
1195 strvec_push(&cp
.args
, rs
->raw
[i
]);
1197 prepare_submodule_repo_env(&cp
.env
);
1204 * Simply indicate if 'submodule--helper push-check' failed.
1205 * More detailed error information will be provided by the
1208 if (run_command(&cp
))
1209 die(_("process for submodule '%s' failed"), path
);
1212 int push_unpushed_submodules(struct repository
*r
,
1213 struct oid_array
*commits
,
1214 const struct remote
*remote
,
1215 const struct refspec
*rs
,
1216 const struct string_list
*push_options
,
1220 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1222 if (!find_unpushed_submodules(r
, commits
,
1223 remote
->name
, &needs_pushing
))
1227 * Verify that the remote and refspec can be propagated to all
1228 * submodules. This check can be skipped if the remote and refspec
1229 * won't be propagated due to the remote being unconfigured (e.g. a URL
1230 * instead of a remote name).
1232 if (remote
->origin
!= REMOTE_UNCONFIGURED
) {
1234 struct object_id head_oid
;
1236 head
= resolve_refdup("HEAD", 0, &head_oid
, NULL
);
1238 die(_("Failed to resolve HEAD as a valid ref."));
1240 for (i
= 0; i
< needs_pushing
.nr
; i
++)
1241 submodule_push_check(needs_pushing
.items
[i
].string
,
1246 /* Actually push the submodules */
1247 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
1248 const char *path
= needs_pushing
.items
[i
].string
;
1249 fprintf(stderr
, _("Pushing submodule '%s'\n"), path
);
1250 if (!push_submodule(path
, remote
, rs
,
1251 push_options
, dry_run
)) {
1252 fprintf(stderr
, _("Unable to push submodule '%s'\n"), path
);
1257 string_list_clear(&needs_pushing
, 0);
1262 static int append_oid_to_array(const char *ref UNUSED
,
1263 const struct object_id
*oid
,
1264 int flags UNUSED
, void *data
)
1266 struct oid_array
*array
= data
;
1267 oid_array_append(array
, oid
);
1271 void check_for_new_submodule_commits(struct object_id
*oid
)
1273 if (!initialized_fetch_ref_tips
) {
1274 for_each_ref(append_oid_to_array
, &ref_tips_before_fetch
);
1275 initialized_fetch_ref_tips
= 1;
1278 oid_array_append(&ref_tips_after_fetch
, oid
);
1282 * Returns 1 if there is at least one submodule gitdir in
1283 * $GIT_DIR/modules and 0 otherwise. This follows
1284 * submodule_name_to_gitdir(), which looks for submodules in
1285 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1287 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1288 * submodule absorbgitdirs", or it may be initialized there by "git
1289 * submodule update".
1291 static int repo_has_absorbed_submodules(struct repository
*r
)
1294 struct strbuf buf
= STRBUF_INIT
;
1296 strbuf_repo_git_path(&buf
, r
, "modules/");
1297 ret
= file_exists(buf
.buf
) && !is_empty_dir(buf
.buf
);
1298 strbuf_release(&buf
);
1302 static void calculate_changed_submodule_paths(struct repository
*r
,
1303 struct string_list
*changed_submodule_names
)
1305 struct strvec argv
= STRVEC_INIT
;
1306 struct string_list_item
*name
;
1308 /* No need to check if no submodules would be fetched */
1309 if (!submodule_from_path(r
, NULL
, NULL
) &&
1310 !repo_has_absorbed_submodules(r
))
1313 strvec_push(&argv
, "--"); /* argv[0] program name */
1314 oid_array_for_each_unique(&ref_tips_after_fetch
,
1315 append_oid_to_argv
, &argv
);
1316 strvec_push(&argv
, "--not");
1317 oid_array_for_each_unique(&ref_tips_before_fetch
,
1318 append_oid_to_argv
, &argv
);
1321 * Collect all submodules (whether checked out or not) for which new
1322 * commits have been recorded upstream in "changed_submodule_names".
1324 collect_changed_submodules(r
, changed_submodule_names
, &argv
);
1326 for_each_string_list_item(name
, changed_submodule_names
) {
1327 struct changed_submodule_data
*cs_data
= name
->util
;
1328 const struct submodule
*submodule
;
1329 const char *path
= NULL
;
1331 submodule
= submodule_from_name(r
, null_oid(), name
->string
);
1333 path
= submodule
->path
;
1335 path
= default_name_or_path(name
->string
);
1340 if (submodule_has_commits(r
, path
, null_oid(), &cs_data
->new_commits
)) {
1341 changed_submodule_data_clear(cs_data
);
1342 *name
->string
= '\0';
1346 string_list_remove_empty_items(changed_submodule_names
, 1);
1348 strvec_clear(&argv
);
1349 oid_array_clear(&ref_tips_before_fetch
);
1350 oid_array_clear(&ref_tips_after_fetch
);
1351 initialized_fetch_ref_tips
= 0;
1354 int submodule_touches_in_range(struct repository
*r
,
1355 struct object_id
*excl_oid
,
1356 struct object_id
*incl_oid
)
1358 struct string_list subs
= STRING_LIST_INIT_DUP
;
1359 struct strvec args
= STRVEC_INIT
;
1362 /* No need to check if there are no submodules configured */
1363 if (!submodule_from_path(r
, NULL
, NULL
))
1366 strvec_push(&args
, "--"); /* args[0] program name */
1367 strvec_push(&args
, oid_to_hex(incl_oid
));
1368 if (!is_null_oid(excl_oid
)) {
1369 strvec_push(&args
, "--not");
1370 strvec_push(&args
, oid_to_hex(excl_oid
));
1373 collect_changed_submodules(r
, &subs
, &args
);
1376 strvec_clear(&args
);
1378 free_submodules_data(&subs
);
1382 struct submodule_parallel_fetch
{
1384 * The index of the last index entry processed by
1385 * get_fetch_task_from_index().
1389 * The index of the last string_list entry processed by
1390 * get_fetch_task_from_changed().
1394 struct repository
*r
;
1396 int command_line_option
;
1402 * Names of submodules that have new commits. Generated by
1403 * walking the newly fetched superproject commits.
1405 struct string_list changed_submodule_names
;
1407 * Names of submodules that have already been processed. Lets us
1408 * avoid fetching the same submodule more than once.
1410 struct string_list seen_submodule_names
;
1412 /* Pending fetches by OIDs */
1413 struct fetch_task
**oid_fetch_tasks
;
1414 int oid_fetch_tasks_nr
, oid_fetch_tasks_alloc
;
1416 struct strbuf submodules_with_errors
;
1418 #define SPF_INIT { \
1419 .args = STRVEC_INIT, \
1420 .changed_submodule_names = STRING_LIST_INIT_DUP, \
1421 .seen_submodule_names = STRING_LIST_INIT_DUP, \
1422 .submodules_with_errors = STRBUF_INIT, \
1425 static int get_fetch_recurse_config(const struct submodule
*submodule
,
1426 struct submodule_parallel_fetch
*spf
)
1428 if (spf
->command_line_option
!= RECURSE_SUBMODULES_DEFAULT
)
1429 return spf
->command_line_option
;
1435 int fetch_recurse
= submodule
->fetch_recurse
;
1436 key
= xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule
->name
);
1437 if (!repo_config_get_string_tmp(spf
->r
, key
, &value
)) {
1438 fetch_recurse
= parse_fetch_recurse_submodules_arg(key
, value
);
1442 if (fetch_recurse
!= RECURSE_SUBMODULES_NONE
)
1443 /* local config overrules everything except commandline */
1444 return fetch_recurse
;
1447 return spf
->default_option
;
1451 * Fetch in progress (if callback data) or
1452 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1455 struct repository
*repo
;
1456 const struct submodule
*sub
;
1457 unsigned free_sub
: 1; /* Do we need to free the submodule? */
1458 const char *default_argv
; /* The default fetch mode. */
1459 struct strvec git_args
; /* Args for the child git process. */
1461 struct oid_array
*commits
; /* Ensure these commits are fetched */
1465 * When a submodule is not defined in .gitmodules, we cannot access it
1466 * via the regular submodule-config. Create a fake submodule, which we can
1469 static const struct submodule
*get_non_gitmodules_submodule(const char *path
)
1471 struct submodule
*ret
= NULL
;
1472 const char *name
= default_name_or_path(path
);
1477 ret
= xmalloc(sizeof(*ret
));
1478 memset(ret
, 0, sizeof(*ret
));
1482 return (const struct submodule
*) ret
;
1485 static void fetch_task_release(struct fetch_task
*p
)
1488 free((void*)p
->sub
);
1493 repo_clear(p
->repo
);
1494 FREE_AND_NULL(p
->repo
);
1496 strvec_clear(&p
->git_args
);
1499 static struct repository
*get_submodule_repo_for(struct repository
*r
,
1501 const struct object_id
*treeish_name
)
1503 struct repository
*ret
= xmalloc(sizeof(*ret
));
1505 if (repo_submodule_init(ret
, r
, path
, treeish_name
)) {
1513 static struct fetch_task
*fetch_task_create(struct submodule_parallel_fetch
*spf
,
1515 const struct object_id
*treeish_name
)
1517 struct fetch_task
*task
= xmalloc(sizeof(*task
));
1518 memset(task
, 0, sizeof(*task
));
1520 task
->sub
= submodule_from_path(spf
->r
, treeish_name
, path
);
1524 * No entry in .gitmodules? Technically not a submodule,
1525 * but historically we supported repositories that happen to be
1526 * in-place where a gitlink is. Keep supporting them.
1528 task
->sub
= get_non_gitmodules_submodule(path
);
1535 if (string_list_lookup(&spf
->seen_submodule_names
, task
->sub
->name
))
1538 switch (get_fetch_recurse_config(task
->sub
, spf
))
1541 case RECURSE_SUBMODULES_DEFAULT
:
1542 case RECURSE_SUBMODULES_ON_DEMAND
:
1544 !string_list_lookup(
1545 &spf
->changed_submodule_names
,
1548 task
->default_argv
= "on-demand";
1550 case RECURSE_SUBMODULES_ON
:
1551 task
->default_argv
= "yes";
1553 case RECURSE_SUBMODULES_OFF
:
1557 task
->repo
= get_submodule_repo_for(spf
->r
, path
, treeish_name
);
1562 fetch_task_release(task
);
1567 static struct fetch_task
*
1568 get_fetch_task_from_index(struct submodule_parallel_fetch
*spf
,
1571 for (; spf
->index_count
< spf
->r
->index
->cache_nr
; spf
->index_count
++) {
1572 const struct cache_entry
*ce
=
1573 spf
->r
->index
->cache
[spf
->index_count
];
1574 struct fetch_task
*task
;
1576 if (!S_ISGITLINK(ce
->ce_mode
))
1579 task
= fetch_task_create(spf
, ce
->name
, null_oid());
1585 strbuf_addf(err
, _("Fetching submodule %s%s\n"),
1586 spf
->prefix
, ce
->name
);
1591 struct strbuf empty_submodule_path
= STRBUF_INIT
;
1593 fetch_task_release(task
);
1597 * An empty directory is normal,
1598 * the submodule is not initialized
1600 strbuf_addf(&empty_submodule_path
, "%s/%s/",
1603 if (S_ISGITLINK(ce
->ce_mode
) &&
1604 !is_empty_dir(empty_submodule_path
.buf
)) {
1607 _("Could not access submodule '%s'\n"),
1610 strbuf_release(&empty_submodule_path
);
1616 static struct fetch_task
*
1617 get_fetch_task_from_changed(struct submodule_parallel_fetch
*spf
,
1620 for (; spf
->changed_count
< spf
->changed_submodule_names
.nr
;
1621 spf
->changed_count
++) {
1622 struct string_list_item item
=
1623 spf
->changed_submodule_names
.items
[spf
->changed_count
];
1624 struct changed_submodule_data
*cs_data
= item
.util
;
1625 struct fetch_task
*task
;
1627 if (!is_tree_submodule_active(spf
->r
, cs_data
->super_oid
,cs_data
->path
))
1630 task
= fetch_task_create(spf
, cs_data
->path
,
1631 cs_data
->super_oid
);
1636 strbuf_addf(err
, _("Could not access submodule '%s' at commit %s\n"),
1638 repo_find_unique_abbrev(the_repository
, cs_data
->super_oid
, DEFAULT_ABBREV
));
1640 fetch_task_release(task
);
1647 _("Fetching submodule %s%s at commit %s\n"),
1648 spf
->prefix
, task
->sub
->path
,
1649 repo_find_unique_abbrev(the_repository
, cs_data
->super_oid
,
1652 spf
->changed_count
++;
1654 * NEEDSWORK: Submodules set/unset a value for
1655 * core.worktree when they are populated/unpopulated by
1656 * "git checkout" (and similar commands, see
1657 * submodule_move_head() and
1658 * connect_work_tree_and_git_dir()), but if the
1659 * submodule is unpopulated in another way (e.g. "git
1660 * rm", "rm -r"), core.worktree will still be set even
1661 * though the directory doesn't exist, and the child
1662 * process will crash while trying to chdir into the
1663 * nonexistent directory.
1665 * In this case, we know that the submodule has no
1666 * working tree, so we can work around this by
1667 * setting "--work-tree=." (--bare does not work because
1668 * worktree settings take precedence over bare-ness).
1669 * However, this is not necessarily true in other cases,
1670 * so a generalized solution is still necessary.
1672 * Possible solutions:
1673 * - teach "git [add|rm]" to unset core.worktree and
1674 * discourage users from removing submodules without
1675 * using a Git command.
1676 * - teach submodule child processes to ignore stale
1677 * core.worktree values.
1679 strvec_push(&task
->git_args
, "--work-tree=.");
1685 static int get_next_submodule(struct child_process
*cp
, struct strbuf
*err
,
1686 void *data
, void **task_cb
)
1688 struct submodule_parallel_fetch
*spf
= data
;
1689 struct fetch_task
*task
=
1690 get_fetch_task_from_index(spf
, err
);
1692 task
= get_fetch_task_from_changed(spf
, err
);
1695 child_process_init(cp
);
1696 cp
->dir
= task
->repo
->gitdir
;
1697 prepare_submodule_repo_env_in_gitdir(&cp
->env
);
1699 strvec_init(&cp
->args
);
1700 if (task
->git_args
.nr
)
1701 strvec_pushv(&cp
->args
, task
->git_args
.v
);
1702 strvec_pushv(&cp
->args
, spf
->args
.v
);
1703 strvec_push(&cp
->args
, task
->default_argv
);
1704 strvec_pushf(&cp
->args
, "--submodule-prefix=%s%s/",
1705 spf
->prefix
, task
->sub
->path
);
1709 string_list_insert(&spf
->seen_submodule_names
, task
->sub
->name
);
1713 if (spf
->oid_fetch_tasks_nr
) {
1714 struct fetch_task
*task
=
1715 spf
->oid_fetch_tasks
[spf
->oid_fetch_tasks_nr
- 1];
1716 spf
->oid_fetch_tasks_nr
--;
1718 child_process_init(cp
);
1719 prepare_submodule_repo_env_in_gitdir(&cp
->env
);
1721 cp
->dir
= task
->repo
->gitdir
;
1723 strvec_init(&cp
->args
);
1724 strvec_pushv(&cp
->args
, spf
->args
.v
);
1725 strvec_push(&cp
->args
, "on-demand");
1726 strvec_pushf(&cp
->args
, "--submodule-prefix=%s%s/",
1727 spf
->prefix
, task
->sub
->path
);
1729 /* NEEDSWORK: have get_default_remote from submodule--helper */
1730 strvec_push(&cp
->args
, "origin");
1731 oid_array_for_each_unique(task
->commits
,
1732 append_oid_to_argv
, &cp
->args
);
1741 static int fetch_start_failure(struct strbuf
*err UNUSED
,
1742 void *cb
, void *task_cb
)
1744 struct submodule_parallel_fetch
*spf
= cb
;
1745 struct fetch_task
*task
= task_cb
;
1749 fetch_task_release(task
);
1753 static int commit_missing_in_sub(const struct object_id
*oid
, void *data
)
1755 struct repository
*subrepo
= data
;
1757 enum object_type type
= oid_object_info(subrepo
, oid
, NULL
);
1759 return type
!= OBJ_COMMIT
;
1762 static int fetch_finish(int retvalue
, struct strbuf
*err UNUSED
,
1763 void *cb
, void *task_cb
)
1765 struct submodule_parallel_fetch
*spf
= cb
;
1766 struct fetch_task
*task
= task_cb
;
1768 struct string_list_item
*it
;
1769 struct changed_submodule_data
*cs_data
;
1771 if (!task
|| !task
->sub
)
1772 BUG("callback cookie bogus");
1776 * NEEDSWORK: This indicates that the overall fetch
1777 * failed, even though there may be a subsequent fetch
1778 * by commit hash that might work. It may be a good
1779 * idea to not indicate failure in this case, and only
1780 * indicate failure if the subsequent fetch fails.
1784 strbuf_addf(&spf
->submodules_with_errors
, "\t%s\n",
1788 /* Is this the second time we process this submodule? */
1792 it
= string_list_lookup(&spf
->changed_submodule_names
, task
->sub
->name
);
1794 /* Could be an unchanged submodule, not contained in the list */
1798 oid_array_filter(&cs_data
->new_commits
,
1799 commit_missing_in_sub
,
1802 /* Are there commits we want, but do not exist? */
1803 if (cs_data
->new_commits
.nr
) {
1804 task
->commits
= &cs_data
->new_commits
;
1805 ALLOC_GROW(spf
->oid_fetch_tasks
,
1806 spf
->oid_fetch_tasks_nr
+ 1,
1807 spf
->oid_fetch_tasks_alloc
);
1808 spf
->oid_fetch_tasks
[spf
->oid_fetch_tasks_nr
] = task
;
1809 spf
->oid_fetch_tasks_nr
++;
1814 fetch_task_release(task
);
1819 int fetch_submodules(struct repository
*r
,
1820 const struct strvec
*options
,
1821 const char *prefix
, int command_line_option
,
1823 int quiet
, int max_parallel_jobs
)
1826 struct submodule_parallel_fetch spf
= SPF_INIT
;
1827 const struct run_process_parallel_opts opts
= {
1828 .tr2_category
= "submodule",
1829 .tr2_label
= "parallel/fetch",
1831 .processes
= max_parallel_jobs
,
1833 .get_next_task
= get_next_submodule
,
1834 .start_failure
= fetch_start_failure
,
1835 .task_finished
= fetch_finish
,
1840 spf
.command_line_option
= command_line_option
;
1841 spf
.default_option
= default_option
;
1843 spf
.prefix
= prefix
;
1848 if (repo_read_index(r
) < 0)
1849 die(_("index file corrupt"));
1851 strvec_push(&spf
.args
, "fetch");
1852 for (i
= 0; i
< options
->nr
; i
++)
1853 strvec_push(&spf
.args
, options
->v
[i
]);
1854 strvec_push(&spf
.args
, "--recurse-submodules-default");
1855 /* default value, "--submodule-prefix" and its value are added later */
1857 calculate_changed_submodule_paths(r
, &spf
.changed_submodule_names
);
1858 string_list_sort(&spf
.changed_submodule_names
);
1859 run_processes_parallel(&opts
);
1861 if (spf
.submodules_with_errors
.len
> 0)
1862 fprintf(stderr
, _("Errors during submodule fetch:\n%s"),
1863 spf
.submodules_with_errors
.buf
);
1866 strvec_clear(&spf
.args
);
1868 free_submodules_data(&spf
.changed_submodule_names
);
1872 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
1874 struct child_process cp
= CHILD_PROCESS_INIT
;
1875 struct strbuf buf
= STRBUF_INIT
;
1877 unsigned dirty_submodule
= 0;
1878 const char *git_dir
;
1879 int ignore_cp_exit_code
= 0;
1881 strbuf_addf(&buf
, "%s/.git", path
);
1882 git_dir
= read_gitfile(buf
.buf
);
1885 if (!is_git_directory(git_dir
)) {
1886 if (is_directory(git_dir
))
1887 die(_("'%s' not recognized as a git repository"), git_dir
);
1888 strbuf_release(&buf
);
1889 /* The submodule is not checked out, so it is not modified */
1894 strvec_pushl(&cp
.args
, "status", "--porcelain=2", NULL
);
1895 if (ignore_untracked
)
1896 strvec_push(&cp
.args
, "-uno");
1898 prepare_submodule_repo_env(&cp
.env
);
1903 if (start_command(&cp
))
1904 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path
);
1906 fp
= xfdopen(cp
.out
, "r");
1907 while (strbuf_getwholeline(&buf
, fp
, '\n') != EOF
) {
1908 /* regular untracked files */
1909 if (buf
.buf
[0] == '?')
1910 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
1912 if (buf
.buf
[0] == 'u' ||
1913 buf
.buf
[0] == '1' ||
1914 buf
.buf
[0] == '2') {
1915 /* T = line type, XY = status, SSSS = submodule state */
1916 if (buf
.len
< strlen("T XY SSSS"))
1917 BUG("invalid status --porcelain=2 line %s",
1920 if (buf
.buf
[5] == 'S' && buf
.buf
[8] == 'U')
1921 /* nested untracked file */
1922 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
1924 if (buf
.buf
[0] == 'u' ||
1925 buf
.buf
[0] == '2' ||
1926 memcmp(buf
.buf
+ 5, "S..U", 4))
1928 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
1931 if ((dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) &&
1932 ((dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ||
1933 ignore_untracked
)) {
1935 * We're not interested in any further information from
1936 * the child any more, neither output nor its exit code.
1938 ignore_cp_exit_code
= 1;
1944 if (finish_command(&cp
) && !ignore_cp_exit_code
)
1945 die(_("'git status --porcelain=2' failed in submodule %s"), path
);
1947 strbuf_release(&buf
);
1948 return dirty_submodule
;
1951 int submodule_uses_gitfile(const char *path
)
1953 struct child_process cp
= CHILD_PROCESS_INIT
;
1954 struct strbuf buf
= STRBUF_INIT
;
1955 const char *git_dir
;
1957 strbuf_addf(&buf
, "%s/.git", path
);
1958 git_dir
= read_gitfile(buf
.buf
);
1960 strbuf_release(&buf
);
1963 strbuf_release(&buf
);
1965 /* Now test that all nested submodules use a gitfile too */
1966 strvec_pushl(&cp
.args
,
1967 "submodule", "foreach", "--quiet", "--recursive",
1968 "test -f .git", NULL
);
1970 prepare_submodule_repo_env(&cp
.env
);
1976 if (run_command(&cp
))
1983 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1986 * Return 1 if we'd lose data, return 0 if the removal is fine,
1987 * and negative values for errors.
1989 int bad_to_remove_submodule(const char *path
, unsigned flags
)
1992 struct child_process cp
= CHILD_PROCESS_INIT
;
1993 struct strbuf buf
= STRBUF_INIT
;
1996 if (!file_exists(path
) || is_empty_dir(path
))
1999 if (!submodule_uses_gitfile(path
))
2002 strvec_pushl(&cp
.args
, "status", "--porcelain",
2003 "--ignore-submodules=none", NULL
);
2005 if (flags
& SUBMODULE_REMOVAL_IGNORE_UNTRACKED
)
2006 strvec_push(&cp
.args
, "-uno");
2008 strvec_push(&cp
.args
, "-uall");
2010 if (!(flags
& SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
))
2011 strvec_push(&cp
.args
, "--ignored");
2013 prepare_submodule_repo_env(&cp
.env
);
2018 if (start_command(&cp
)) {
2019 if (flags
& SUBMODULE_REMOVAL_DIE_ON_ERROR
)
2020 die(_("could not start 'git status' in submodule '%s'"),
2026 len
= strbuf_read(&buf
, cp
.out
, 1024);
2031 if (finish_command(&cp
)) {
2032 if (flags
& SUBMODULE_REMOVAL_DIE_ON_ERROR
)
2033 die(_("could not run 'git status' in submodule '%s'"),
2038 strbuf_release(&buf
);
2042 void submodule_unset_core_worktree(const struct submodule
*sub
)
2044 struct strbuf config_path
= STRBUF_INIT
;
2046 submodule_name_to_gitdir(&config_path
, the_repository
, sub
->name
);
2047 strbuf_addstr(&config_path
, "/config");
2049 if (git_config_set_in_file_gently(config_path
.buf
, "core.worktree", NULL
, NULL
))
2050 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2053 strbuf_release(&config_path
);
2056 static int submodule_has_dirty_index(const struct submodule
*sub
)
2058 struct child_process cp
= CHILD_PROCESS_INIT
;
2060 prepare_submodule_repo_env(&cp
.env
);
2063 strvec_pushl(&cp
.args
, "diff-index", "--quiet",
2064 "--cached", "HEAD", NULL
);
2068 if (start_command(&cp
))
2069 die(_("could not recurse into submodule '%s'"), sub
->path
);
2071 return finish_command(&cp
);
2074 static void submodule_reset_index(const char *path
, const char *super_prefix
)
2076 struct child_process cp
= CHILD_PROCESS_INIT
;
2077 prepare_submodule_repo_env(&cp
.env
);
2083 /* TODO: determine if this might overwright untracked files */
2084 strvec_pushl(&cp
.args
, "read-tree", "-u", "--reset", NULL
);
2085 strvec_pushf(&cp
.args
, "--super-prefix=%s%s/",
2086 (super_prefix
? super_prefix
: ""), path
);
2088 strvec_push(&cp
.args
, empty_tree_oid_hex());
2090 if (run_command(&cp
))
2091 die(_("could not reset submodule index"));
2095 * Moves a submodule at a given path from a given head to another new head.
2096 * For edge cases (a submodule coming into existence or removing a submodule)
2097 * pass NULL for old or new respectively.
2099 int submodule_move_head(const char *path
, const char *super_prefix
,
2100 const char *old_head
, const char *new_head
,
2104 struct child_process cp
= CHILD_PROCESS_INIT
;
2105 const struct submodule
*sub
;
2106 int *error_code_ptr
, error_code
;
2108 if (!is_submodule_active(the_repository
, path
))
2111 if (flags
& SUBMODULE_MOVE_HEAD_FORCE
)
2113 * Pass non NULL pointer to is_submodule_populated_gently
2114 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2115 * to fixup the submodule in the force case later.
2117 error_code_ptr
= &error_code
;
2119 error_code_ptr
= NULL
;
2121 if (old_head
&& !is_submodule_populated_gently(path
, error_code_ptr
))
2124 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2127 BUG("could not get submodule information for '%s'", path
);
2129 if (old_head
&& !(flags
& SUBMODULE_MOVE_HEAD_FORCE
)) {
2130 /* Check if the submodule has a dirty index. */
2131 if (submodule_has_dirty_index(sub
))
2132 return error(_("submodule '%s' has dirty index"), path
);
2135 if (!(flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)) {
2137 if (!submodule_uses_gitfile(path
))
2138 absorb_git_dir_into_superproject(path
,
2141 struct strbuf gitdir
= STRBUF_INIT
;
2142 submodule_name_to_gitdir(&gitdir
, the_repository
,
2144 connect_work_tree_and_git_dir(path
, gitdir
.buf
, 0);
2145 strbuf_release(&gitdir
);
2147 /* make sure the index is clean as well */
2148 submodule_reset_index(path
, super_prefix
);
2151 if (old_head
&& (flags
& SUBMODULE_MOVE_HEAD_FORCE
)) {
2152 struct strbuf gitdir
= STRBUF_INIT
;
2153 submodule_name_to_gitdir(&gitdir
, the_repository
,
2155 connect_work_tree_and_git_dir(path
, gitdir
.buf
, 1);
2156 strbuf_release(&gitdir
);
2160 prepare_submodule_repo_env(&cp
.env
);
2166 strvec_pushl(&cp
.args
, "read-tree", "--recurse-submodules", NULL
);
2167 strvec_pushf(&cp
.args
, "--super-prefix=%s%s/",
2168 (super_prefix
? super_prefix
: ""), path
);
2170 if (flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)
2171 strvec_push(&cp
.args
, "-n");
2173 strvec_push(&cp
.args
, "-u");
2175 if (flags
& SUBMODULE_MOVE_HEAD_FORCE
)
2176 strvec_push(&cp
.args
, "--reset");
2178 strvec_push(&cp
.args
, "-m");
2180 if (!(flags
& SUBMODULE_MOVE_HEAD_FORCE
))
2181 strvec_push(&cp
.args
, old_head
? old_head
: empty_tree_oid_hex());
2183 strvec_push(&cp
.args
, new_head
? new_head
: empty_tree_oid_hex());
2185 if (run_command(&cp
)) {
2186 ret
= error(_("Submodule '%s' could not be updated."), path
);
2190 if (!(flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)) {
2192 child_process_init(&cp
);
2193 /* also set the HEAD accordingly */
2198 prepare_submodule_repo_env(&cp
.env
);
2199 strvec_pushl(&cp
.args
, "update-ref", "HEAD",
2200 "--no-deref", new_head
, NULL
);
2202 if (run_command(&cp
)) {
2207 struct strbuf sb
= STRBUF_INIT
;
2209 strbuf_addf(&sb
, "%s/.git", path
);
2210 unlink_or_warn(sb
.buf
);
2211 strbuf_release(&sb
);
2213 if (is_empty_dir(path
))
2214 rmdir_or_warn(path
);
2216 submodule_unset_core_worktree(sub
);
2223 int validate_submodule_git_dir(char *git_dir
, const char *submodule_name
)
2225 size_t len
= strlen(git_dir
), suffix_len
= strlen(submodule_name
);
2229 if (len
<= suffix_len
|| (p
= git_dir
+ len
- suffix_len
)[-1] != '/' ||
2230 strcmp(p
, submodule_name
))
2231 BUG("submodule name '%s' not a suffix of git dir '%s'",
2232 submodule_name
, git_dir
);
2235 * We prevent the contents of sibling submodules' git directories to
2238 * Example: having a submodule named `hippo` and another one named
2239 * `hippo/hooks` would result in the git directories
2240 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2241 * but the latter directory is already designated to contain the hooks
2245 if (is_dir_sep(*p
)) {
2249 if (is_git_directory(git_dir
))
2254 return error(_("submodule git dir '%s' is "
2255 "inside git dir '%.*s'"),
2257 (int)(p
- git_dir
), git_dir
);
2265 * Embeds a single submodules git directory into the superprojects git dir,
2268 static void relocate_single_git_dir_into_superproject(const char *path
,
2269 const char *super_prefix
)
2271 char *old_git_dir
= NULL
, *real_old_git_dir
= NULL
, *real_new_git_dir
= NULL
;
2272 struct strbuf new_gitdir
= STRBUF_INIT
;
2273 const struct submodule
*sub
;
2275 if (submodule_uses_worktrees(path
))
2276 die(_("relocate_gitdir for submodule '%s' with "
2277 "more than one worktree not supported"), path
);
2279 old_git_dir
= xstrfmt("%s/.git", path
);
2280 if (read_gitfile(old_git_dir
))
2281 /* If it is an actual gitfile, it doesn't need migration. */
2284 real_old_git_dir
= real_pathdup(old_git_dir
, 1);
2286 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2288 die(_("could not lookup name for submodule '%s'"), path
);
2290 submodule_name_to_gitdir(&new_gitdir
, the_repository
, sub
->name
);
2291 if (validate_submodule_git_dir(new_gitdir
.buf
, sub
->name
) < 0)
2292 die(_("refusing to move '%s' into an existing git dir"),
2294 if (safe_create_leading_directories_const(new_gitdir
.buf
) < 0)
2295 die(_("could not create directory '%s'"), new_gitdir
.buf
);
2296 real_new_git_dir
= real_pathdup(new_gitdir
.buf
, 1);
2298 fprintf(stderr
, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
2299 super_prefix
? super_prefix
: "", path
,
2300 real_old_git_dir
, real_new_git_dir
);
2302 relocate_gitdir(path
, real_old_git_dir
, real_new_git_dir
);
2305 free(real_old_git_dir
);
2306 free(real_new_git_dir
);
2307 strbuf_release(&new_gitdir
);
2310 static void absorb_git_dir_into_superproject_recurse(const char *path
,
2311 const char *super_prefix
)
2314 struct child_process cp
= CHILD_PROCESS_INIT
;
2319 strvec_pushl(&cp
.args
, "submodule--helper",
2320 "absorbgitdirs", NULL
);
2321 strvec_pushf(&cp
.args
, "--super-prefix=%s%s/", super_prefix
?
2322 super_prefix
: "", path
);
2324 prepare_submodule_repo_env(&cp
.env
);
2325 if (run_command(&cp
))
2326 die(_("could not recurse into submodule '%s'"), path
);
2330 * Migrate the git directory of the submodule given by path from
2331 * having its git directory within the working tree to the git dir nested
2332 * in its superprojects git dir under modules/.
2334 void absorb_git_dir_into_superproject(const char *path
,
2335 const char *super_prefix
)
2338 const char *sub_git_dir
;
2339 struct strbuf gitdir
= STRBUF_INIT
;
2340 strbuf_addf(&gitdir
, "%s/.git", path
);
2341 sub_git_dir
= resolve_gitdir_gently(gitdir
.buf
, &err_code
);
2343 /* Not populated? */
2345 const struct submodule
*sub
;
2346 struct strbuf sub_gitdir
= STRBUF_INIT
;
2348 if (err_code
== READ_GITFILE_ERR_STAT_FAILED
) {
2349 /* unpopulated as expected */
2350 strbuf_release(&gitdir
);
2354 if (err_code
!= READ_GITFILE_ERR_NOT_A_REPO
)
2355 /* We don't know what broke here. */
2356 read_gitfile_error_die(err_code
, path
, NULL
);
2359 * Maybe populated, but no git directory was found?
2360 * This can happen if the superproject is a submodule
2361 * itself and was just absorbed. The absorption of the
2362 * superproject did not rewrite the git file links yet,
2365 sub
= submodule_from_path(the_repository
, null_oid(), path
);
2367 die(_("could not lookup name for submodule '%s'"), path
);
2368 submodule_name_to_gitdir(&sub_gitdir
, the_repository
, sub
->name
);
2369 connect_work_tree_and_git_dir(path
, sub_gitdir
.buf
, 0);
2370 strbuf_release(&sub_gitdir
);
2372 /* Is it already absorbed into the superprojects git dir? */
2373 char *real_sub_git_dir
= real_pathdup(sub_git_dir
, 1);
2374 char *real_common_git_dir
= real_pathdup(get_git_common_dir(), 1);
2376 if (!starts_with(real_sub_git_dir
, real_common_git_dir
))
2377 relocate_single_git_dir_into_superproject(path
, super_prefix
);
2379 free(real_sub_git_dir
);
2380 free(real_common_git_dir
);
2382 strbuf_release(&gitdir
);
2384 absorb_git_dir_into_superproject_recurse(path
, super_prefix
);
2387 int get_superproject_working_tree(struct strbuf
*buf
)
2389 struct child_process cp
= CHILD_PROCESS_INIT
;
2390 struct strbuf sb
= STRBUF_INIT
;
2391 struct strbuf one_up
= STRBUF_INIT
;
2392 char *cwd
= xgetcwd();
2394 const char *subpath
;
2398 if (!is_inside_work_tree())
2401 * We might have a superproject, but it is harder
2406 if (!strbuf_realpath(&one_up
, "../", 0))
2409 subpath
= relative_path(cwd
, one_up
.buf
, &sb
);
2410 strbuf_release(&one_up
);
2412 prepare_submodule_repo_env(&cp
.env
);
2413 strvec_pop(&cp
.env
);
2415 strvec_pushl(&cp
.args
, "--literal-pathspecs", "-C", "..",
2416 "ls-files", "-z", "--stage", "--full-name", "--",
2425 if (start_command(&cp
))
2426 die(_("could not start ls-files in .."));
2428 len
= strbuf_read(&sb
, cp
.out
, PATH_MAX
);
2431 if (starts_with(sb
.buf
, "160000")) {
2433 int cwd_len
= strlen(cwd
);
2434 char *super_sub
, *super_wt
;
2437 * There is a superproject having this repo as a submodule.
2438 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2439 * We're only interested in the name after the tab.
2441 super_sub
= strchr(sb
.buf
, '\t') + 1;
2442 super_sub_len
= strlen(super_sub
);
2444 if (super_sub_len
> cwd_len
||
2445 strcmp(&cwd
[cwd_len
- super_sub_len
], super_sub
))
2446 BUG("returned path string doesn't match cwd?");
2448 super_wt
= xstrdup(cwd
);
2449 super_wt
[cwd_len
- super_sub_len
] = '\0';
2451 strbuf_realpath(buf
, super_wt
, 1);
2456 strbuf_release(&sb
);
2458 code
= finish_command(&cp
);
2461 /* '../' is not a git repository */
2463 if (code
== 0 && len
== 0)
2464 /* There is an unrelated git repository at '../' */
2467 die(_("ls-tree returned unexpected return code %d"), code
);
2473 * Put the gitdir for a submodule (given relative to the main
2474 * repository worktree) into `buf`, or return -1 on error.
2476 int submodule_to_gitdir(struct strbuf
*buf
, const char *submodule
)
2478 const struct submodule
*sub
;
2479 const char *git_dir
;
2483 strbuf_addstr(buf
, submodule
);
2484 strbuf_complete(buf
, '/');
2485 strbuf_addstr(buf
, ".git");
2487 git_dir
= read_gitfile(buf
->buf
);
2490 strbuf_addstr(buf
, git_dir
);
2492 if (!is_git_directory(buf
->buf
)) {
2493 sub
= submodule_from_path(the_repository
, null_oid(),
2500 submodule_name_to_gitdir(buf
, the_repository
, sub
->name
);
2507 void submodule_name_to_gitdir(struct strbuf
*buf
, struct repository
*r
,
2508 const char *submodule_name
)
2511 * NEEDSWORK: The current way of mapping a submodule's name to
2512 * its location in .git/modules/ has problems with some naming
2513 * schemes. For example, if a submodule is named "foo" and
2514 * another is named "foo/bar" (whether present in the same
2515 * superproject commit or not - the problem will arise if both
2516 * superproject commits have been checked out at any point in
2517 * time), or if two submodule names only have different cases in
2518 * a case-insensitive filesystem.
2520 * There are several solutions, including encoding the path in
2521 * some way, introducing a submodule.<name>.gitdir config in
2522 * .git/config (not .gitmodules) that allows overriding what the
2523 * gitdir of a submodule would be (and teach Git, upon noticing
2524 * a clash, to automatically determine a non-clashing name and
2525 * to write such a config), or introducing a
2526 * submodule.<name>.gitdir config in .gitmodules that repo
2527 * administrators can explicitly set. Nothing has been decided,
2528 * so for now, just append the name at the end of the path.
2530 strbuf_repo_git_path(buf
, r
, "modules/");
2531 strbuf_addstr(buf
, submodule_name
);