Skip to content

Commit f83c090

Browse files
Edward Thomsonnulltoken
Edward Thomson
authored andcommitted
Introduce GlobalSettings.NativeLibraryPath
Allow consumers to set the native library path in case they are running in a strange environment that they have little control over where the actual executing assembly lives (but do have some control over the references) and want to point the library to the native location explicitly.
1 parent 005869a commit f83c090

File tree

6 files changed

+113
-29
lines changed

6 files changed

+113
-29
lines changed

LibGit2Sharp.Tests/GlobalSettingsFixture.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,14 @@ public void CanRetrieveValidVersionString()
5555
Assert.True(matchGroups[i].Success);
5656
}
5757
}
58+
59+
[Fact]
60+
public void TryingToResetNativeLibraryPathAfterLoadedThrows()
61+
{
62+
// Do something that loads the native library
63+
Assert.NotNull(GlobalSettings.Version.Features);
64+
65+
Assert.Throws<LibGit2SharpException>(() => { GlobalSettings.NativeLibraryPath = "C:/Foo"; });
66+
}
5867
}
5968
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,44 +64,19 @@ internal static void RemoveHandle()
6464

6565
static NativeMethods()
6666
{
67-
if (!IsRunningOnUnix())
67+
if (Platform.OperatingSystem == OperatingSystemType.Windows)
6868
{
69-
string originalAssemblypath = new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;
70-
71-
string currentArchSubPath = "NativeBinaries/" + ProcessorArchitecture;
72-
73-
string path = Path.Combine(Path.GetDirectoryName(originalAssemblypath), currentArchSubPath);
69+
string path = Path.Combine(GlobalSettings.NativeLibraryPath, Platform.ProcessorArchitecture);
7470

7571
const string pathEnvVariable = "PATH";
7672
Environment.SetEnvironmentVariable(pathEnvVariable,
77-
String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", path, Path.PathSeparator, Environment.GetEnvironmentVariable(pathEnvVariable)));
73+
String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", path, Path.PathSeparator, Environment.GetEnvironmentVariable(pathEnvVariable)));
7874
}
7975

8076
// See LibraryLifetimeObject description.
8177
lifetimeObject = new LibraryLifetimeObject();
8278
}
8379

84-
public static string ProcessorArchitecture
85-
{
86-
get
87-
{
88-
if (Environment.Is64BitProcess)
89-
{
90-
return "amd64";
91-
}
92-
93-
return "x86";
94-
}
95-
}
96-
97-
// Should match LibGit2Sharp.Tests.TestHelpers.BaseFixture.IsRunningOnUnix()
98-
private static bool IsRunningOnUnix()
99-
{
100-
// see http://mono-project.com/FAQ%3a_Technical#Mono_Platforms
101-
var p = (int)Environment.OSVersion.Platform;
102-
return (p == 4) || (p == 6) || (p == 128);
103-
}
104-
10580
[DllImport(libgit2)]
10681
internal static extern GitErrorSafeHandle giterr_last();
10782

LibGit2Sharp/Core/Platform.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace LibGit2Sharp.Core
7+
{
8+
internal enum OperatingSystemType
9+
{
10+
Windows,
11+
Unix,
12+
MacOSX
13+
}
14+
15+
internal class Platform
16+
{
17+
public static string ProcessorArchitecture
18+
{
19+
get
20+
{
21+
if (Environment.Is64BitProcess)
22+
{
23+
return "amd64";
24+
}
25+
26+
return "x86";
27+
}
28+
}
29+
30+
public static OperatingSystemType OperatingSystem
31+
{
32+
get
33+
{
34+
// See http://www.mono-project.com/docs/faq/technical/#how-to-detect-the-execution-platform
35+
var platformId = (int)Environment.OSVersion.Platform;
36+
37+
switch ((int)Environment.OSVersion.Platform)
38+
{
39+
case 4:
40+
case 128:
41+
return OperatingSystemType.Unix;
42+
case 6:
43+
return OperatingSystemType.MacOSX;
44+
default:
45+
return OperatingSystemType.Windows;
46+
}
47+
}
48+
}
49+
}
50+
}

LibGit2Sharp/GlobalSettings.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.IO;
3+
using System.Reflection;
24
using LibGit2Sharp.Core;
35

46
namespace LibGit2Sharp
@@ -12,6 +14,17 @@ public static class GlobalSettings
1214

1315
private static LogConfiguration logConfiguration = LogConfiguration.None;
1416

17+
private static string nativeLibraryPath;
18+
19+
static GlobalSettings()
20+
{
21+
if (Platform.OperatingSystem == OperatingSystemType.Windows)
22+
{
23+
string managedPath = new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;
24+
nativeLibraryPath = Path.Combine(Path.GetDirectoryName(managedPath), "NativeBinaries");
25+
}
26+
}
27+
1528
/// <summary>
1629
/// Returns information related to the current LibGit2Sharp
1730
/// library.
@@ -108,5 +121,41 @@ public static LogConfiguration LogConfiguration
108121
return logConfiguration;
109122
}
110123
}
124+
125+
/// <summary>
126+
/// Sets a hint path for searching for native binaries: when
127+
/// specified, native binaries will first be searched in a
128+
/// subdirectory of the given path corresponding to the architecture
129+
/// (eg, "x86" or "amd64") before falling back to the default
130+
/// path ("NativeBinaries\x86" or "NativeBinaries\amd64" next
131+
/// to the application).
132+
/// <para>
133+
/// This must be set before any other calls to the library,
134+
/// and is not available on Unix platforms: see your dynamic
135+
/// library loader's documentation for details.
136+
/// </para>
137+
/// </summary>
138+
public static string NativeLibraryPath
139+
{
140+
get
141+
{
142+
if (Platform.OperatingSystem != OperatingSystemType.Windows)
143+
{
144+
throw new LibGit2SharpException("Querying the native hint path is only supported on Windows platforms");
145+
}
146+
147+
return nativeLibraryPath;
148+
}
149+
150+
set
151+
{
152+
if (Platform.OperatingSystem != OperatingSystemType.Windows)
153+
{
154+
throw new LibGit2SharpException("Setting the native hint path is only supported on Windows platforms");
155+
}
156+
157+
nativeLibraryPath = value;
158+
}
159+
}
111160
}
112161
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="CommitOptions.cs" />
6969
<Compile Include="CommitSortStrategies.cs" />
7070
<Compile Include="CompareOptions.cs" />
71+
<Compile Include="Core\Platform.cs" />
7172
<Compile Include="DescribeOptions.cs" />
7273
<Compile Include="DescribeStrategy.cs" />
7374
<Compile Include="Core\GitDescribeFormatOptions.cs" />

LibGit2Sharp/Version.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private string RetrieveVersion()
9898
InformationalVersion,
9999
LibGit2SharpCommitSha,
100100
LibGit2CommitSha,
101-
NativeMethods.ProcessorArchitecture,
101+
Platform.ProcessorArchitecture,
102102
features);
103103
}
104104

0 commit comments

Comments
 (0)