You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2026/08/03 21:40:13 UTC
(maven-shared-utils) branch master updated: PathTool.getRelativeFilePath: fix broken Windows drive-letter regex (#384)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git
The following commit(s) were added to refs/heads/master by this push:
new cfc9c12 PathTool.getRelativeFilePath: fix broken Windows drive-letter regex (#384)
cfc9c12 is described below
commit cfc9c12b7f8f827f79e02f82a247d93c6da655ee
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Mon Aug 3 21:40:08 2026 +0000
PathTool.getRelativeFilePath: fix broken Windows drive-letter regex (#384)
* Add failing test: PathTool Windows drive letter regex
* PathTool.getRelativeFilePath: fix broken Windows drive-letter regex
* spotless:apply formatting
* Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <17...@users.noreply.github.com>
* Fix misleading comment: code strips backslash before drive letter, not 'any leading slashes'
---------
Co-authored-by: Copilot Autofix powered by AI <17...@users.noreply.github.com>
Co-authored-by: opencode <op...@example.com>
---
src/main/java/org/apache/maven/shared/utils/PathTool.java | 12 +++++++++---
.../java/org/apache/maven/shared/utils/PathToolTest.java | 8 ++++++++
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/src/main/java/org/apache/maven/shared/utils/PathTool.java b/src/main/java/org/apache/maven/shared/utils/PathTool.java
index 59cef00..cff7614 100644
--- a/src/main/java/org/apache/maven/shared/utils/PathTool.java
+++ b/src/main/java/org/apache/maven/shared/utils/PathTool.java
@@ -142,11 +142,17 @@ public class PathTool {
String fromPath = new File(oldPath).getPath();
String toPath = new File(newPath).getPath();
- // strip any leading slashes if its a windows path
- if (toPath.matches("^\\[a-zA-Z]:")) {
+ // strip any leading backslash before a Windows drive letter
+ if (toPath.length() > 2
+ && toPath.charAt(0) == '\\'
+ && Character.isLetter(toPath.charAt(1))
+ && toPath.charAt(2) == ':') {
toPath = toPath.substring(1);
}
- if (fromPath.matches("^\\[a-zA-Z]:")) {
+ if (fromPath.length() > 2
+ && fromPath.charAt(0) == '\\'
+ && Character.isLetter(fromPath.charAt(1))
+ && fromPath.charAt(2) == ':') {
fromPath = fromPath.substring(1);
}
diff --git a/src/test/java/org/apache/maven/shared/utils/PathToolTest.java b/src/test/java/org/apache/maven/shared/utils/PathToolTest.java
index e1c98cb..cc7e3cc 100644
--- a/src/test/java/org/apache/maven/shared/utils/PathToolTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/PathToolTest.java
@@ -106,6 +106,14 @@ public class PathToolTest {
assertEquals("", PathTool.getRelativePath("/usr/local/java/bin/java.sh", "/usr/local/"));
}
+ @Test
+ public void testGetRelativeFilePathWithDifferentWindowsDrives() {
+ // Verifies that a leading backslash before a Windows drive letter (e.g. "\C:\\...")
+ // is stripped so the drive-letter comparison logic runs.
+ // Different drives should return null.
+ assertNull(PathTool.getRelativeFilePath("\\C:\\usr\\local", "\\D:\\usr\\local\\java\\bin"));
+ }
+
@Test
public void testUppercaseDrive() {
assertNull(PathTool.uppercaseDrive(null));