8000 NativePath: use Assembly.CodeBase · cczinski/libgit2sharp@6745f1a · GitHub
[go: up one dir, main page]

Skip to content

Commit 6745f1a

Browse files
author
Edward Thomson
committed
NativePath: use Assembly.CodeBase
Use `Assembly.CodeBase` instead of `Assembly.EscapedCodeBase` since the latter does not actually properly escape the path in a reversible way. eg, if a path contains a space, it will be encoded as `%20` in the `EscapedCodeBase` however if a path contains a literal `%20` then it will _also_ be encoded as `%20`.
1 parent 3fe02ab commit 6745f1a

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

LibGit2Sharp/GlobalSettings.cs

Lines changed: 20 additions & 1 deletion
6625
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,26 @@ static GlobalSettings()
2323
{
2424
if (Platform.OperatingSystem == OperatingSystemType.Windows)
2525
{
26-
string managedPath = new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;
26+
/* Assembly.CodeBase is not actually a correctly formatted
27+
* URI. It's merely prefixed with `file:///` and has its
28+
* backslashes flipped. This is superior to EscapedCodeBase,
29+
* which does not correctly escape things, and ambiguates a
30+
* space (%20) with a literal `%20` in the path. Sigh.
31+
*/
32+
var managedPath = Assembly.GetExecutingAssembly().CodeBase;
33+
if (managedPath == null)
34+
{
35+
managedPath = Assembly.GetExecutingAssembly().Location;
36+
}
37+
else if (managedPath.StartsWith("file:///"))
38+
{
39+
managedPath = managedPath.Substring(8).Replace('/', '\\');
40+
}
41+
else if (managedPath.StartsWith("file://"))
42+
{
43+
managedPath = @"\\" + managedPath.Substring(7).Replace('/', '\\');
44+
}
45+
2746
nativeLibraryPath = Path.Combine(Path.Combine(Path.GetDirectoryName(managedPath), "lib"), "win32");
2847
}
2948

0 commit comments

Comments
 (0)
0