From e8e848a8dad92b6542b16449d29f4a5535fa7c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 1 Jul 2015 21:10:40 +0200 Subject: [PATCH 1/2] submodule: add failing test for loading the wrong submodule When two submodules are fairly similar, we may end up loading the wrong one. --- tests/submodule/lookup.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/submodule/lookup.c b/tests/submodule/lookup.c index cddbdcfc2..4d40e2279 100644 --- a/tests/submodule/lookup.c +++ b/tests/submodule/lookup.c @@ -269,3 +269,26 @@ void test_submodule_lookup__just_added(void) refute_submodule_exists(g_repo, "sm_just_added_head", GIT_EEXISTS); } +/* Test_App and Test_App2 are fairly similar names, make sure we load the right one */ +void test_submodule_lookup__prefix_name(void) +{ + git_submodule *sm; + + cl_git_rewritefile("submod2/.gitmodules", + "[submodule \"Test_App\"]\n" + " path = Test_App\n" + " url = ../Test_App\n" + "[submodule \"Test_App2\"]\n" + " path = Test_App2\n" + " url = ../Test_App\n"); + + cl_git_pass(git_submodule_lookup(&sm, g_repo, "Test_App")); + cl_assert_equal_s("Test_App", git_submodule_name(sm)); + + git_submodule_free(sm); + + cl_git_pass(git_submodule_lookup(&sm, g_repo, "Test_App2")); + cl_assert_equal_s("Test_App2", git_submodule_name(sm)); + + git_submodule_free(sm); +} From e0af3cb30c10f65f6db802f64a3389f73c0292b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 1 Jul 2015 21:15:06 +0200 Subject: [PATCH 2/2] submodule: correctly delimit the keys to use for lookup The regex we use to look at the gitmodules file does not correctly delimit the name of submodule which we want to look up and puts '.*' straight after the name, maching on any submodule which has the seeked submodule as a prefix of its name. Add the missing '\.' in the regex so we want a full stop to exist both before and after the submodule name. --- src/submodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/submodule.c b/src/submodule.c index 17e1a3561..fb3d4bf1e 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -1385,7 +1385,7 @@ int git_submodule_reload(git_submodule *sm, int force) git_buf_sets(&path, "submodule\\."); git_buf_text_puts_escape_regex(&path, sm->name); - git_buf_puts(&path, ".*"); + git_buf_puts(&path, "\\..*"); if (git_buf_oom(&path)) { error = -1;