From 6745f1aeb86f891ab2ef46d763d8e42299c2e34a Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Fri, 13 Jan 2017 06:31:50 -0500 Subject: [PATCH] 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`. --- LibGit2Sharp/GlobalSettings.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp/GlobalSettings.cs b/LibGit2Sharp/GlobalSettings.cs index a514caf04..c456df928 100644 --- a/LibGit2Sharp/GlobalSettings.cs +++ b/LibGit2Sharp/GlobalSettings.cs @@ -23,7 +23,26 @@ static GlobalSettings() { if (Platform.OperatingSystem == OperatingSystemType.Windows) { - string managedPath = new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath; + /* Assembly.CodeBase is not actually a correctly formatted + * URI. It's merely prefixed with `file:///` and has its + * backslashes flipped. This is superior to EscapedCodeBase, + * which does not correctly escape things, and ambiguates a + * space (%20) with a literal `%20` in the path. Sigh. + */ + var managedPath = Assembly.GetExecutingAssembly().CodeBase; + if (managedPath == null) + { + managedPath = Assembly.GetExecutingAssembly().Location; + } + else if (managedPath.StartsWith("file:///")) + { + managedPath = managedPath.Substring(8).Replace('/', '\\'); + } + else if (managedPath.StartsWith("file://")) + { + managedPath = @"\\" + managedPath.Substring(7).Replace('/', '\\'); + } + nativeLibraryPath = Path.Combine(Path.Combine(Path.GetDirectoryName(managedPath), "lib"), "win32"); }