aboutsummaryrefslogtreecommitdiffstats
path: root/submodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'submodule.c')
-rw-r--r--submodule.c155
1 files changed, 128 insertions, 27 deletions
diff --git a/submodule.c b/submodule.c
index 40a5c6fb9d..ff2f45457d 100644
--- a/submodule.c
+++ b/submodule.c
@@ -31,6 +31,7 @@
#include "commit-reach.h"
#include "read-cache-ll.h"
#include "setup.h"
+#include "url.h"
static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
static int initialized_fetch_ref_tips;
@@ -2250,16 +2251,30 @@ out:
return ret;
}
+/*
+ * Find the last submodule name in the gitdir path (modules can be nested).
+ * Returns a pointer into `path` to the beginning of the name or NULL if not found.
+ */
+static char *find_last_submodule_name(char *git_dir_path)
+{
+ const char *modules_marker = "/modules/";
+ char *p = git_dir_path;
+ char *last = NULL;
+
+ while ((p = strstr(p, modules_marker))) {
+ last = p + strlen(modules_marker);
+ p++;
+ }
+
+ return last;
+}
+
int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
{
size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
- char *p;
- int ret = 0;
-
- if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
- strcmp(p, submodule_name))
- BUG("submodule name '%s' not a suffix of git dir '%s'",
- submodule_name, git_dir);
+ char *p = git_dir + len - suffix_len;
+ bool suffixes_match = !strcmp(p, submodule_name);
+ int ret = 0, config_ignorecase = 0;
/*
* We prevent the contents of sibling submodules' git directories to
@@ -2271,7 +2286,7 @@ int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
* but the latter directory is already designated to contain the hooks
* of the former.
*/
- for (; *p; p++) {
+ for (; *p && suffixes_match; p++) {
if (is_dir_sep(*p)) {
char c = *p;
@@ -2288,6 +2303,51 @@ int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
}
}
+ /* tests after this check are only for encoded names, when the extension is enabled */
+ if (!the_repository->repository_format_submodule_encoding)
+ return 0;
+
+ /* Prevent the use of '/' in names */
+ p = find_last_submodule_name(git_dir);
+ if (p && strchr(p, '/') != NULL)
+ return error("submodule gitdir name '%s' contains unexpected '/'", p);
+
+ /* Prevent conflicts on case-folding filesystems */
+ repo_config_get_bool(the_repository, "core.ignorecase", &config_ignorecase);
+ if (ignore_case || config_ignorecase) {
+ char *lower_gitdir = xstrdup(git_dir);
+ char *module_name = find_last_submodule_name(lower_gitdir);
+
+ if (module_name) {
+ for (p = module_name; *p; p++)
+ *p = tolower(*p);
+
+ /*
+ * If lower path is different and already exists, check for collision.
+ * Intentionally double-check to eliminate false-positives.
+ */
+ if (strcmp(lower_gitdir, git_dir) && is_git_directory(lower_gitdir)) {
+ char *canonical = real_pathdup(git_dir, 0);
+ if (canonical) {
+ struct strbuf norm_git_dir = STRBUF_INIT;
+ strbuf_addstr(&norm_git_dir, git_dir);
+ strbuf_normalize_path(&norm_git_dir);
+
+ if (strcmp(canonical, norm_git_dir.buf))
+ ret = error(_("submodule git dir '%s' "
+ "collides with '%s'"),
+ canonical, norm_git_dir.buf);
+
+ strbuf_release(&norm_git_dir);
+ FREE_AND_NULL(canonical);
+ }
+ }
+ }
+
+ FREE_AND_NULL(lower_gitdir);
+ return ret;
+ }
+
return 0;
}
@@ -2575,29 +2635,70 @@ cleanup:
return ret;
}
+static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,
+ const char *submodule_name)
+{
+ char *key;
+
+ if (validate_submodule_git_dir(gitdir_path->buf, submodule_name))
+ return -1;
+
+ key = xstrfmt("submodule.%s.gitdir", submodule_name);
+ repo_config_set_gently(the_repository, key, gitdir_path->buf);
+ FREE_AND_NULL(key);
+
+ return 0;
+
+}
+
void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
const char *submodule_name)
{
+ const char *gitdir;
+ char *key;
+
+ repo_git_path_append(r, buf, "modules/");
+ strbuf_addstr(buf, submodule_name);
+
+ /* If extensions.submoduleEncoding is disabled, use the plain path set above */
+ if (!r->repository_format_submodule_encoding)
+ return;
+
+ /* Extension is enabled: use the gitdir config if it exists */
+ key = xstrfmt("submodule.%s.gitdir", submodule_name);
+ if (!repo_config_get_string_tmp(r, key, &gitdir)) {
+ strbuf_reset(buf);
+ strbuf_addstr(buf, gitdir);
+ FREE_AND_NULL(key);
+ return;
+ }
+ FREE_AND_NULL(key);
+
/*
- * NEEDSWORK: The current way of mapping a submodule's name to
- * its location in .git/modules/ has problems with some naming
- * schemes. For example, if a submodule is named "foo" and
- * another is named "foo/bar" (whether present in the same
- * superproject commit or not - the problem will arise if both
- * superproject commits have been checked out at any point in
- * time), or if two submodule names only have different cases in
- * a case-insensitive filesystem.
- *
- * There are several solutions, including encoding the path in
- * some way, introducing a submodule.<name>.gitdir config in
- * .git/config (not .gitmodules) that allows overriding what the
- * gitdir of a submodule would be (and teach Git, upon noticing
- * a clash, to automatically determine a non-clashing name and
- * to write such a config), or introducing a
- * submodule.<name>.gitdir config in .gitmodules that repo
- * administrators can explicitly set. Nothing has been decided,
- * so for now, just append the name at the end of the path.
+ * The gitdir config does not exist, even though the extension is enabled.
+ * Therefore we are in one of the following cases:
*/
+
+ /* Case 1: legacy migration of valid plain submodule names */
+ if (!validate_and_set_submodule_gitdir(buf, submodule_name))
+ return;
+
+ /* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */
+ strbuf_reset(buf);
repo_git_path_append(r, buf, "modules/");
- strbuf_addstr(buf, submodule_name);
+ strbuf_addstr_urlencode(buf, submodule_name, is_rfc3986_unreserved);
+ if (!validate_and_set_submodule_gitdir(buf, submodule_name))
+ return;
+
+ /* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */
+ strbuf_reset(buf);
+ repo_git_path_append(r, buf, "modules/");
+ strbuf_addstr_urlencode(buf, submodule_name, is_casefolding_rfc3986_unreserved);
+ if (!validate_and_set_submodule_gitdir(buf, submodule_name))
+ return;
+
+ /* Case 3: error out */
+ die(_("Cannot construct a valid gitdir path for submodule '%s': "
+ "please set a unique git config for 'submodule.%s.gitdir'."),
+ submodule_name, submodule_name);
}