gnodet commented on code in PR #387:
URL: https://github.com/apache/maven-filtering/pull/387#discussion_r4093615421


##########
src/main/java/org/apache/maven/shared/filtering/FilteringUtils.java:
##########
@@ -176,15 +176,16 @@ public static String getRelativeFilePath(final String 
oldPath, final String newP
             return "";
         }
 
-        // normalise the path delimiters
-        String fromPath = new File(oldPath).getPath();
-        String toPath = new File(newPath).getPath();
+        // normalise the path delimiters to forward slashes for cross-platform 
consistency
+        String fromPath = new File(oldPath).getPath().replace('\\', '/');
+        String toPath = new File(newPath).getPath().replace('\\', '/');
 
-        // strip any leading slashes if its a windows path
-        if (toPath.matches("^\\[a-zA-Z]:")) {
+        // strip any leading slashes if its a windows path (require separator 
after colon to avoid
+        // false positives on unusual Unix paths like /a:/something)
+        if (toPath.matches("^[/\\\\][a-zA-Z]:[/\\\\].*")) {

Review Comment:
   Fixed in debb3f6 — simplified to `^/[a-zA-Z]:/.*` since backslashes are 
already normalized to forward slashes two lines above.



##########
src/main/java/org/apache/maven/shared/filtering/FilteringUtils.java:
##########
@@ -176,15 +176,16 @@ public static String getRelativeFilePath(final String 
oldPath, final String newP
             return "";
         }
 
-        // normalise the path delimiters
-        String fromPath = new File(oldPath).getPath();
-        String toPath = new File(newPath).getPath();
+        // normalise the path delimiters to forward slashes for cross-platform 
consistency
+        String fromPath = new File(oldPath).getPath().replace('\\', '/');
+        String toPath = new File(newPath).getPath().replace('\\', '/');
 
-        // strip any leading slashes if its a windows path
-        if (toPath.matches("^\\[a-zA-Z]:")) {
+        // strip any leading slashes if its a windows path (require separator 
after colon to avoid
+        // false positives on unusual Unix paths like /a:/something)
+        if (toPath.matches("^[/\\\\][a-zA-Z]:[/\\\\].*")) {
             toPath = toPath.substring(1);
         }
-        if (fromPath.matches("^\\[a-zA-Z]:")) {
+        if (fromPath.matches("^[/\\\\][a-zA-Z]:[/\\\\].*")) {

Review Comment:
   Fixed in debb3f6 — same simplification applied to `fromPath`.



##########
src/test/java/org/apache/maven/shared/filtering/FilteringUtilsTest.java:
##########
@@ -146,4 +146,10 @@ void escapeWindowsPathNotAtBeginning() {
                 "jdbc:derby:C:\\\\Users\\\\Administrator/test;create=true",
                 
FilteringUtils.escapeWindowsPath("jdbc:derby:C:\\Users\\Administrator/test;create=true"));
     }
+
+    @Test
+    void relativeFilePathStripsLeadingSeparatorFromWindowsDrivePath() {
+        assertEquals("file.txt", FilteringUtils.getRelativeFilePath("C:/base", 
"/C:/base/file.txt"));
+        assertEquals("../other/file.txt", 
FilteringUtils.getRelativeFilePath("/C:/base/dir", "C:/base/other/file.txt"));
+    }

Review Comment:
   Fixed in debb3f6 — added `relativeFilePathUnixStylePaths` test covering the 
three Javadoc-documented cases. Note: the trailing-slash case returns 
`../usr/local/` (the method preserves the trailing slash from `newPath`), so 
the assertion was adjusted accordingly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to