using System;
using System.IO;
using System.Reflection;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
///
/// Global settings for libgit2 and LibGit2Sharp.
///
public static class GlobalSettings
{
private static readonly Lazy version = new Lazy(Version.Build);
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");
}
}
///
/// Returns information related to the current LibGit2Sharp
/// library.
///
public static Version Version
{
get
{
return version.Value;
}
}
///
/// Registers a new as a custom
/// smart-protocol transport with libgit2. Any Git remote with
/// the scheme registered will delegate to the given transport
/// for all communication with the server. use this transport to communicate
/// with the server This is not commonly
/// used: some callers may want to re-use an existing connection to
/// perform fetch / push operations to a remote.
///
/// Note that this configuration is global to an entire process
/// and does not honor application domains.
///
/// The type of SmartSubtransport to register
/// The scheme (eg "http" or "gopher") to register
public static SmartSubtransportRegistration RegisterSmartSubtransport(string scheme)
where T : SmartSubtransport, new()
{
Ensure.ArgumentNotNull(scheme, "scheme");
var registration = new SmartSubtransportRegistration(scheme);
try
{
Proxy.git_transport_register(
registration.Scheme,
registration.FunctionPointer,
registration.RegistrationPointer);
}
catch (Exception)
{
registration.Free();
throw;
}
return registration;
}
///
/// Unregisters a previously registered
/// as a custom smart-protocol transport with libgit2.
///
/// The type of SmartSubtransport to register
/// The previous registration
public static void UnregisterSmartSubtransport(SmartSubtransportRegistration registration)
where T : SmartSubtransport, new()
{
Ensure.ArgumentNotNull(registration, "registration");
Proxy.git_transport_unregister(registration.Scheme);
registration.Free();
}
///
/// Registers a new to receive
/// information logging information from libgit2 and LibGit2Sharp.
///
/// Note that this configuration is global to an entire process
/// and does not honor application domains.
///
public static LogConfiguration LogConfiguration
{
set
{
Ensure.ArgumentNotNull(value, "value");
logConfiguration = value;
if (logConfiguration.Level == LogLevel.None)
{
Proxy.git_trace_set(0, null);
}
else
{
Proxy.git_trace_set(value.Level, value.GitTraceCallback);
Log.Write(LogLevel.Info, "Logging enabled at level {0}", value.Level);
}
}
get
{
return logConfiguration;
}
}
///
/// 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).
///
/// 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.
///
///
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;
}
}
internal static string GetAndLockNativeLibraryPath()
{
nativeLibraryPathLocked = true;
return nativeLibraryPath;
}
}
}