Skip to content

Commit 29afb3b

Browse files
committed
Merge pull request #667 from fdorg/fix/relative-path
Project GetRelativePath improvement
2 parents 139988b + 08c679b commit 29afb3b

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

External/Plugins/ProjectManager/Projects/ProjectPaths.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,20 @@ public static string GetRelativePath(string baseDirectory, string path)
1313
throw new ArgumentException("The path is already relative.");
1414

1515
char slash = Path.DirectorySeparatorChar;
16-
string[] a = baseDirectory.Trim(slash).Split(slash);
17-
string[] b = path.Trim(slash).Split(slash);
16+
path = path.TrimEnd(slash);
17+
baseDirectory = baseDirectory.TrimEnd(slash);
18+
19+
// trivial cases
20+
if (path == baseDirectory)
21+
return "";
22+
if (path[1] == ':' && path[0] != baseDirectory[0]) // drive
23+
return path;
24+
if (path.Length > baseDirectory.Length && path.StartsWith(baseDirectory + slash))
25+
return path.Substring(baseDirectory.Length + 1);
26+
27+
// resolve relative path
28+
string[] a = baseDirectory.Split(slash);
29+
string[] b = path.Split(slash);
1830

1931
ArrayList relPath = new ArrayList();
2032
int i = 0;
@@ -26,6 +38,10 @@ public static string GetRelativePath(string baseDirectory, string path)
2638
break;
2739
}
2840

41+
// only common drive letter, consider not relative
42+
if (i <= 1)
43+
return path;
44+
2945
// at this point, i is the index of the first diverging element of the two paths
3046
int backtracks = a.Length - i;
3147
for (int j = 0; j < backtracks; j++)

0 commit comments

Comments
 (0)