From aa51fa1e03df9a30c8c37b168758dd34dd75f353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 13 Jul 2015 08:39:35 +0200 Subject: [PATCH 1/3] submodule: add failing test for backslash in url --- tests/submodule/lookup.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/submodule/lookup.c b/tests/submodule/lookup.c index 4d40e2279..38f06abbc 100644 --- a/tests/submodule/lookup.c +++ b/tests/submodule/lookup.c @@ -133,6 +133,29 @@ void test_submodule_lookup__lookup_even_with_missing_index(void) test_submodule_lookup__simple_lookup(); /* baseline should still pass */ } +void test_submodule_lookup__backslashes(void) +{ + git_config *cfg; + git_submodule *sm; + git_repository *subrepo; + git_buf buf = GIT_BUF_INIT; + const char *backslashed_path = "..\\submod2_target"; + + cl_git_pass(git_config_open_ondisk(&cfg, "submod2/.gitmodules")); + cl_git_pass(git_config_set_string(cfg, "submodule.sm_unchanged.url", backslashed_path)); + git_config_free(cfg); + + cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); + cl_assert_equal_s(backslashed_path, git_submodule_url(sm)); + cl_git_pass(git_submodule_open(&subrepo, sm)); + + cl_git_pass(git_submodule_resolve_url(&buf, g_repo, backslashed_path)); + + git_buf_free(&buf); + git_submodule_free(sm); + git_repository_free(subrepo); +} + static void baseline_tests(void) { /* small baseline that should work even if we change the index or make From f00f005bad3d27bb5c23ae5d7187622b940c5d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 13 Jul 2015 09:08:32 +0200 Subject: [PATCH 2/3] submodule: normalize slashes in resolve_url Our path functions expect to work with slashes, so convert a path with backslashes into one with slashes at the top of the function. --- src/submodule.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/submodule.c b/src/submodule.c index fb3d4bf1e..e4469efa3 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -781,11 +781,25 @@ const char *git_submodule_url(git_submodule *submodule) int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *url) { int error = 0; + git_buf normalized = GIT_BUF_INIT; assert(out && repo && url); git_buf_sanitize(out); + if (strchr(url, '\\')) { + char *p; + if ((error = git_buf_puts(&normalized, url)) < 0) + return error; + + for (p = normalized.ptr; *p; p++) { + if (*p == '\\') + *p = '/'; + } + + url = normalized.ptr; + } + if (git_path_is_relative(url)) { if (!(error = get_url_base(out, repo))) error = git_path_apply_relative(out, url); @@ -796,6 +810,7 @@ int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *ur error = -1; } + git_buf_free(&normalized); return error; } From a58854a0311316a66fda363e83665ab942d81ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 13 Jul 2015 17:11:19 +0200 Subject: [PATCH 3/3] submodule, path: extract slash conversion Extract the backslash-to-slash conversion into a helper function. --- src/path.c | 16 ++++++++++++++++ src/path.h | 5 +++++ src/submodule.c | 10 +++------- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/path.c b/src/path.c index 2558058dd..16283cab2 100644 --- a/src/path.c +++ b/src/path.c @@ -1671,3 +1671,19 @@ bool git_path_isvalid( return verify_component(repo, start, (c - start), flags); } + +int git_path_normalize_slashes(git_buf *out, const char *path) +{ + int error; + char *p; + + if ((error = git_buf_puts(out, path)) < 0) + return error; + + for (p = out->ptr; *p; p++) { + if (*p == '\\') + *p = '/'; + } + + return 0; +} diff --git a/src/path.h b/src/path.h index 5927a5381..adb76865e 100644 --- a/src/path.h +++ b/src/path.h @@ -591,4 +591,9 @@ extern bool git_path_isvalid( const char *path, unsigned int flags); +/** + * Convert any backslashes into slashes + */ +int git_path_normalize_slashes(git_buf *out, const char *path); + #endif diff --git a/src/submodule.c b/src/submodule.c index e4469efa3..f16d3c5ab 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -787,19 +787,15 @@ int git_submodule_resolve_url(git_buf *out, git_repository *repo, const char *ur git_buf_sanitize(out); + /* We do this in all platforms in case someone on Windows created the .gitmodules */ if (strchr(url, '\\')) { - char *p; - if ((error = git_buf_puts(&normalized, url)) < 0) + if ((error = git_path_normalize_slashes(&normalized, url)) < 0) return error; - for (p = normalized.ptr; *p; p++) { - if (*p == '\\') - *p = '/'; - } - url = normalized.ptr; } + if (git_path_is_relative(url)) { if (!(error = get_url_base(out, repo))) error = git_path_apply_relative(out, url);