From: Jacob Keller <jacob.keller@xxxxxxxxx> The prefix_path_gently() -- and its wrapper prefix_path() -- function normalizes the provided path and optionally adds the provided prefix to relative paths. If the prefix does not end in a trailing slash, the function will combine the last component of the prefix with the first component of the relative path. This is unlikely to produce a desirable result. Teach prefix_path_gently() to check if the prefix ends in a slash. If it does not, then insert a slash between the prefix and the path. Take care to avoid inserting a slash if the prefix is empty. Add test cases to cover the relative path behavior, which was previously untested. Signed-off-by: Jacob Keller <jacob.keller@xxxxxxxxx> --- setup.c | 4 +++- t/t0060-path-utils.sh | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/setup.c b/setup.c index f93bd6a24a5d..bd2888500817 100644 --- a/setup.c +++ b/setup.c @@ -139,7 +139,9 @@ char *prefix_path_gently(const char *prefix, int len, return NULL; } } else { - sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path); + sanitized = xstrfmt("%.*s%s%s", len, len ? prefix : "", + !len || prefix[len - 1] == '/' ? "" : "/", + path); if (remaining_prefix) *remaining_prefix = len; if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) { diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index 8545cdfab559..9274713aea0c 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -307,6 +307,24 @@ test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink test_cmp expect actual ' +test_expect_success 'prefix_path works with relative path' ' + echo "prefix/a" >expect && + test-tool path-utils prefix_path "prefix/" "a" >actual && + test_cmp expect actual +' + +test_expect_success 'prefix_path works with relative path and prefix not ending in /' ' + echo "prefix/a" >expect && + test-tool path-utils prefix_path "prefix" "a" >actual && + test_cmp expect actual +' + +test_expect_success 'prefix_path works with relative path and empty prefix' ' + echo "a" >expect && + test-tool path-utils prefix_path "" "a" >actual && + test_cmp expect actual +' + relative_path /foo/a/b/c/ /foo/a/b/ c/ relative_path /foo/a/b/c/ /foo/a/b c/ relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX -- 2.48.1.397.gec9d649cc640