git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 1/4] prefix_path: support prefixes not ending in trailing slash

From
Jacob Keller <jacob.e.keller@intel.com>
Date
May 20, 2025, 00:01 UTC
Message-ID
<20250520000125.2162144-2-jacob.e.keller@intel.com>
In-Reply-To
<20250520000125.2162144-1-jacob.e.keller@intel.com>
From: Jacob Keller <jacob.keller@gmail.com>

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@gmail.com>
---
 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
Previous: Jacob KellerNext: Junio C Hamano
Message 2 of 15 in “diff: add pathspec support to --no-index”
  1. 0/4 diff: add pathspec support to --no-indexJacob Keller, May 20, 2025
  2. 1/4 prefix_path: support prefixes not ending in trailing slashJacob Keller, May 20, 2025
  3. Junio C HamanoMay 20, 2025
  4. Jacob KellerMay 20, 2025
  5. 2/4 pathspec: expose match_pathspec_with_flagsJacob Keller, May 20, 2025
  6. Junio C HamanoMay 20, 2025
  7. Jacob KellerMay 20, 2025
  8. 3/4 pathspec: add flag to indicate operation without repositoryJacob Keller, May 20, 2025
  9. Junio C HamanoMay 20, 2025
  10. Jacob KellerMay 20, 2025
  11. Jacob KellerMay 21, 2025
  12. 4/4 diff --no-index: support limiting by pathspecJacob Keller, May 20, 2025
  13. Junio C HamanoMay 20, 2025
  14. Jacob KellerMay 20, 2025
  15. Jacob KellerMay 20, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.