git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jacob Keller <jacob.e.keller@intel.com>
To: <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>,
	Jacob Keller <jacob.keller@gmail.com>
Subject: [PATCH v3 1/4] prefix_path: support prefixes not ending in trailing slash
Date: Mon, 19 May 2025 17:01:22 -0700	[thread overview]
Message-ID: <20250520000125.2162144-2-jacob.e.keller@intel.com> (raw)
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


  reply	other threads:[~2025-05-20  0:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-20  0:01 [PATCH v3 0/4] diff: add pathspec support to --no-index Jacob Keller
2025-05-20  0:01 ` Jacob Keller [this message]
2025-05-20 14:35   ` [PATCH v3 1/4] prefix_path: support prefixes not ending in trailing slash Junio C Hamano
2025-05-20 22:34     ` Jacob Keller
2025-05-20  0:01 ` [PATCH v3 2/4] pathspec: expose match_pathspec_with_flags Jacob Keller
2025-05-20 14:39   ` Junio C Hamano
2025-05-20 22:38     ` Jacob Keller
2025-05-20  0:01 ` [PATCH v3 3/4] pathspec: add flag to indicate operation without repository Jacob Keller
2025-05-20 15:13   ` Junio C Hamano
2025-05-20 22:42     ` Jacob Keller
2025-05-21 23:05     ` Jacob Keller
2025-05-20  0:01 ` [PATCH v3 4/4] diff --no-index: support limiting by pathspec Jacob Keller
2025-05-20 16:30   ` Junio C Hamano
2025-05-20 22:45     ` Jacob Keller
2025-05-20 22:47     ` Jacob Keller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250520000125.2162144-2-jacob.e.keller@intel.com \
    --to=jacob.e.keller@intel.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jacob.keller@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).