Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/main/java/org/apache/maven/shared/utils/PathTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ public static String getRelativeFilePath(final String oldPath, final String newP
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);
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/apache/maven/shared/utils/PathToolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public void testGetRelativePath2Parm() {
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));
Expand Down
Loading