submodule: remove trailing slashes from submodule paths We allow looking up a submodule by path, but we lost the path normalisation during the recent changes. Bring it back.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
diff --git a/src/submodule.c b/src/submodule.c
index f710f8d..0cd6bb9 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -168,11 +168,18 @@ int git_submodule_lookup(
return error;
}
- /* If it's not configured, we need to check for the path */
+ /* If it's not configured or we're looking by path */
if (location == 0 || location == GIT_SUBMODULE_STATUS_IN_WD) {
git_config_backend *mods;
const char *pattern = "submodule\\..*\\.path";
- fbp_data data = { name, NULL };
+ git_buf path = GIT_BUF_INIT;
+ fbp_data data = { NULL, NULL };
+
+ git_buf_puts(&path, name);
+ while (path.ptr[path.size-1] == '/') {
+ path.ptr[--path.size] = '\0';
+ }
+ data.path = path.ptr;
mods = open_gitmodules(repo, GITMODULES_EXISTING);
@@ -189,8 +196,7 @@ int git_submodule_lookup(
if (data.name) {
git__free(sm->name);
sm->name = data.name;
- sm->path = git__strdup(name);
- GITERR_CHECK_ALLOC(sm->path);
+ sm->path = git_buf_detach(&path);
/* Try to load again with the right name */
if ((error = git_submodule_reload(sm, false)) < 0) {
@@ -198,6 +204,8 @@ int git_submodule_lookup(
return error;
}
}
+
+ git_buf_free(&path);
}
if ((error = git_submodule_location(&location, sm)) < 0) {
diff --git a/tests/submodule/lookup.c b/tests/submodule/lookup.c
index 666f56e..cddbdcf 100644
--- a/tests/submodule/lookup.c
+++ b/tests/submodule/lookup.c
@@ -31,6 +31,9 @@ void test_submodule_lookup__simple_lookup(void)
/* lookup non-existent item */
refute_submodule_exists(g_repo, "no_such_file", GIT_ENOTFOUND);
+
+ /* lookup a submodule by path with a trailing slash */
+ assert_submodule_exists(g_repo, "sm_added_and_uncommited/");
}
void test_submodule_lookup__accessors(void)