Skip to content
Merged
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
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/BlobFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void CanTellIfTheBlobContentLooksLikeBinary()

private static void SkipIfNotSupported(string autocrlf)
{
InconclusiveIf(() => autocrlf == "true" && IsRunningOnLinux(), "Non-Windows does not support core.autocrlf = true");
InconclusiveIf(() => autocrlf == "true" && IsRunningOnUnix(), "Non-Windows does not support core.autocrlf = true");
}
}
}
9 changes: 9 additions & 0 deletions LibGit2Sharp.Tests/GlobalSettingsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,14 @@ public void CanRetrieveValidVersionString()
Assert.True(matchGroups[i].Success);
}
}

[Fact]
public void TryingToResetNativeLibraryPathAfterLoadedThrows()
{
// Do something that loads the native library
Assert.NotNull(GlobalSettings.Version.Features);

Assert.Throws<LibGit2SharpException>(() => { GlobalSettings.NativeLibraryPath = "C:/Foo"; });
}
}
}
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/ShadowCopyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void CanProbeForNativeBinariesFromAShadowCopiedAssembly()
string cachedAssembliesPath = Path.Combine(setup.CachePath, setup.ApplicationName);
Assert.True(cachedAssemblyLocation.StartsWith(cachedAssembliesPath));

if (!IsRunningOnLinux())
if (!IsRunningOnUnix())
{
// ...that this cache doesn't contain the `NativeBinaries` folder
string cachedAssemblyParentPath = Path.GetDirectoryName(cachedAssemblyLocation);
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ private static bool IsFileSystemCaseSensitiveInternal()
return !isInsensitive;
}

// Should match LibGit2Sharp.Core.NativeMethods.IsRunningOnLinux()
protected static bool IsRunningOnLinux()
// Should match LibGit2Sharp.Core.NativeMethods.IsRunningOnUnix()
protected static bool IsRunningOnUnix()
{
// see http://mono-project.com/FAQ%3a_Technical#Mono_Platforms
var p = (int)Environment.OSVersion.Platform;
Expand Down
31 changes: 4 additions & 27 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,21 @@ internal static void RemoveHandle()

static NativeMethods()
{
if (!IsRunningOnLinux())
if (Platform.OperatingSystem == OperatingSystemType.Windows)
{
string originalAssemblypath = new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;
string nativeLibraryPath = GlobalSettings.GetAndLockNativeLibraryPath();

string currentArchSubPath = "NativeBinaries/" + ProcessorArchitecture;

string path = Path.Combine(Path.GetDirectoryName(originalAssemblypath), currentArchSubPath);
string path = Path.Combine(nativeLibraryPath, Platform.ProcessorArchitecture);

const string pathEnvVariable = "PATH";
Environment.SetEnvironmentVariable(pathEnvVariable,
String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", path, Path.PathSeparator, Environment.GetEnvironmentVariable(pathEnvVariable)));
String.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", path, Path.PathSeparator, Environment.GetEnvironmentVariable(pathEnvVariable)));
}

// See LibraryLifetimeObject description.
lifetimeObject = new LibraryLifetimeObject();
}

public static string ProcessorArchitecture
{
get
{
if (Environment.Is64BitProcess)
{
return "amd64";
}

return "x86";
}
}

// Should match LibGit2Sharp.Tests.TestHelpers.BaseFixture.IsRunningOnLinux()
private static bool IsRunningOnLinux()
{
// see http://mono-project.com/FAQ%3a_Technical#Mono_Platforms
var p = (int)Environment.OSVersion.Platform;
return (p == 4) || (p == 6) || (p == 128);
}

[DllImport(libgit2)]
internal static extern GitErrorSafeHandle giterr_last();

Expand Down
50 changes: 50 additions & 0 deletions LibGit2Sharp/Core/Platform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibGit2Sharp.Core
{
internal enum OperatingSystemType
{
Windows,
Unix,
MacOSX
}

internal class Platform
{
public static string ProcessorArchitecture
{
get
{
if (Environment.Is64BitProcess)
{
return "amd64";
}

return "x86";
}
}

public static OperatingSystemType OperatingSystem
{
get
{
// See http://www.mono-project.com/docs/faq/technical/#how-to-detect-the-execution-platform
var platformId = (int)Environment.OSVersion.Platform;

switch ((int)Environment.OSVersion.Platform)
{
case 4:
case 128:
return OperatingSystemType.Unix;
case 6:
return OperatingSystemType.MacOSX;
default:
return OperatingSystemType.Windows;
}
}
}
}
}
61 changes: 61 additions & 0 deletions LibGit2Sharp/GlobalSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.IO;
using System.Reflection;
using LibGit2Sharp.Core;

namespace LibGit2Sharp
Expand All @@ -12,6 +14,18 @@ public static class GlobalSettings

private static LogConfiguration logConfiguration = LogConfiguration.None;

private static string nativeLibraryPath;
private static bool nativeLibraryPathLocked;

static GlobalSettings()
{
if (Platform.OperatingSystem == OperatingSystemType.Windows)
{
string managedPath = new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath;
nativeLibraryPath = Path.Combine(Path.GetDirectoryName(managedPath), "NativeBinaries");
}
}

/// <summary>
/// Returns information related to the current LibGit2Sharp
/// library.
Expand Down Expand Up @@ -108,5 +122,52 @@ public static LogConfiguration LogConfiguration
return logConfiguration;
}
}

/// <summary>
/// Sets a hint path for searching for native binaries: when
/// specified, native binaries will first be searched in a
/// subdirectory of the given path corresponding to the architecture
/// (eg, "x86" or "amd64") before falling back to the default
/// path ("NativeBinaries\x86" or "NativeBinaries\amd64" next
/// to the application).
/// <para>
/// This must be set before any other calls to the library,
/// and is not available on Unix platforms: see your dynamic
/// library loader's documentation for details.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what I'm being asked for. :P

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha. I think he wants you to confirm that my documentation matches reality.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Therzok Sorry for having pinged you without any context.

@ethomson ❤️

/// </para>
/// </summary>
public static string NativeLibraryPath
{
get
{
if (Platform.OperatingSystem != OperatingSystemType.Windows)
{
throw new LibGit2SharpException("Querying the native hint path is only supported on Windows platforms");
}

return nativeLibraryPath;
}

set
{
if (Platform.OperatingSystem != OperatingSystemType.Windows)
{
throw new LibGit2SharpException("Setting the native hint path is only supported on Windows platforms");
}

if (nativeLibraryPathLocked)
{
throw new LibGit2SharpException("You cannot set the native library path after it has been loaded");
}

nativeLibraryPath = value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be a way to throw as well if any call to the native library has already been made?

}
}

internal static string GetAndLockNativeLibraryPath()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
nativeLibraryPathLocked = true;
return nativeLibraryPath;
}
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Compile Include="CommitOptions.cs" />
<Compile Include="CommitSortStrategies.cs" />
<Compile Include="CompareOptions.cs" />
<Compile Include="Core\Platform.cs" />
<Compile Include="DescribeOptions.cs" />
<Compile Include="DescribeStrategy.cs" />
<Compile Include="Core\GitDescribeFormatOptions.cs" />
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private string RetrieveVersion()
InformationalVersion,
LibGit2SharpCommitSha,
LibGit2CommitSha,
NativeMethods.ProcessorArchitecture,
Platform.ProcessorArchitecture,
features);
}

Expand Down