aboutsummaryrefslogtreecommitdiff
path: root/submodule.c
diff options
context:
space:
mode:
authorAdrian Ratiu <adrian.ratiu@collabora.com>2026-01-12 20:46:32 +0200
committerJunio C Hamano <gitster@pobox.com>2026-01-12 11:56:57 -0800
commite897c9b7f31cf83e93cfefe1f82eb4a18337c9b1 (patch)
tree2f4df5ed1d0fb4172b00e9770a86a186d87fc71e /submodule.c
parent82c36fa0a987c9c8617f5ded41834f7487e616e2 (diff)
downloadgit-e897c9b7f31cf83e93cfefe1f82eb4a18337c9b1.tar.xz
submodule: detect conflicts with existing gitdir configs
Credit goes to Emily and Josh for testing and noticing a corner-case which caused conflicts with existing gitdir configs to silently pass validation, then fail later in add_submodule() with a cryptic error: fatal: A git directory for 'nested%2fsub' is found locally with remote(s): origin /.../trash directory.t7425-submodule-gitdir-path-extension/sub This change ensures the validation step checks existing gitdirs for conflicts. We only have to do this for submodules having gitdirs, because those without submodule.%s.gitdir need to be migrated and will throw an error earlier in the submodule codepath. Quoting Josh: My testing setup has been as follows: * Using our locally-built Git with our downstream patch of [1] included: * create a repo "sub" * create a repo "super" * In "super": * mkdir nested * git submodule add ../sub nested/sub * Verify that the submodule's gitdir is .git/modules/nested%2fsub * Using a build of git from upstream `next` plus this series: * git config set --global extensions.submodulepathconfig true * git clone --recurse-submodules super super2 * create a repo "nested%2fsub" * In "super2": * git submodule add ../nested%2fsub At this point I'd expect the collision detection / encoding to take effect, but instead I get the error listed above. End quote Suggested-by: Josh Steadmon <steadmon@google.com> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'submodule.c')
-rw-r--r--submodule.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/submodule.c b/submodule.c
index a5a47359c7..4ab3fcb598 100644
--- a/submodule.c
+++ b/submodule.c
@@ -2296,6 +2296,62 @@ cleanup:
return ret;
}
+struct submodule_from_gitdir_cb {
+ const char *gitdir;
+ const char *submodule_name;
+ bool conflict_found;
+};
+
+static int find_conflict_by_gitdir_cb(const char *var, const char *value,
+ const struct config_context *ctx UNUSED, void *data)
+{
+ struct submodule_from_gitdir_cb *cb = data;
+ const char *submodule_name_start;
+ size_t submodule_name_len;
+ const char *suffix = ".gitdir";
+ size_t suffix_len = strlen(suffix);
+
+ if (!skip_prefix(var, "submodule.", &submodule_name_start))
+ return 0;
+
+ /* Check if submodule_name_start ends with ".gitdir" */
+ submodule_name_len = strlen(submodule_name_start);
+ if (submodule_name_len < suffix_len ||
+ strcmp(submodule_name_start + submodule_name_len - suffix_len, suffix) != 0)
+ return 0; /* Does not end with ".gitdir" */
+
+ submodule_name_len -= suffix_len;
+
+ /*
+ * A conflict happens if:
+ * 1. The submodule names are different and
+ * 2. The gitdir paths resolve to the same absolute path
+ */
+ if (value && strncmp(cb->submodule_name, submodule_name_start, submodule_name_len)) {
+ char *abs_path_cb = absolute_pathdup(cb->gitdir);
+ char *abs_path_value = absolute_pathdup(value);
+
+ cb->conflict_found = !strcmp(abs_path_cb, abs_path_value);
+
+ free(abs_path_cb);
+ free(abs_path_value);
+ }
+
+ return cb->conflict_found;
+}
+
+static bool submodule_conflicts_with_existing(const char *gitdir, const char *submodule_name)
+{
+ struct submodule_from_gitdir_cb cb = { 0 };
+ cb.submodule_name = submodule_name;
+ cb.gitdir = gitdir;
+
+ /* Find conflicts with existing repo gitdir configs */
+ repo_config(the_repository, find_conflict_by_gitdir_cb, &cb);
+
+ return cb.conflict_found;
+}
+
/*
* Encoded gitdir validation, only used when extensions.submodulePathConfig is enabled.
* This does not print errors like the non-encoded version, because encoding is supposed
@@ -2321,6 +2377,11 @@ static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodu
if (!last_submodule_name || strchr(last_submodule_name, '/'))
return -1;
+ /* Prevent conflicts with existing submodule gitdirs */
+ if (is_git_directory(git_dir) &&
+ submodule_conflicts_with_existing(git_dir, submodule_name))
+ return -1;
+
/* Prevent conflicts on case-folding filesystems */
repo_config_get_bool(the_repository, "core.ignorecase", &config_ignorecase);
if (ignore_case || config_ignorecase) {