Permalink
Browse files

Can get basic settings from config file

  • Loading branch information...
tclem authored and nulltoken committed Sep 27, 2011
1 parent 02dbc31 commit 87248e06d8fe95208fab10feb7318335a638e4ba
@@ -0,0 +1,45 @@
+using LibGit2Sharp.Tests.TestHelpers;
+using NUnit.Framework;
+
+namespace LibGit2Sharp.Tests
+{
+ [TestFixture]
+ public class ConfigurationFixture : BaseFixture
+ {
+ [Test]
+ public void CanReadBooleanValue()
+ {
+ using (var repo = new Repository(Constants.StandardTestRepoPath))
+ {
+ Assert.IsFalse(repo.Config.Get<bool>("core.bare"));
+ }
+ }
+
+ [Test]
+ public void CanReadIntValue()
+ {
+ using (var repo = new Repository(Constants.StandardTestRepoPath))
+ {
+ Assert.AreEqual(2, repo.Config.Get<int>("unittests.intsetting"));
+ }
+ }
+
+ [Test]
+ public void CanReadLongValue()
+ {
+ using (var repo = new Repository(Constants.StandardTestRepoPath))
+ {
+ Assert.AreEqual(15234, repo.Config.Get<long>("unittests.longsetting"));
+ }
+ }
+
+ [Test]
+ public void CanReadStringValue()
+ {
+ using (var repo = new Repository(Constants.StandardTestRepoPath))
+ {
+ Assert.AreEqual("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remotes.origin.fetch"));
+ }
+ }
+ }
+}
@@ -42,6 +42,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="ConfigurationFixture.cs" />
<Compile Include="LazyFixture.cs" />
<Compile Include="TestHelpers\BaseFixture.cs" />
<Compile Include="BlobFixture.cs" />
@@ -12,3 +12,6 @@
[branch "master"]
remote = origin
merge = refs/heads/master
+[unittests]
+ longsetting = 15234
+ intsetting = 2
@@ -0,0 +1,111 @@
+using System;
+using LibGit2Sharp.Core;
+
+namespace LibGit2Sharp
+{
+ /// <summary>
+ /// Provides access to the '.gitconfig' configuration for a repository.
+ /// </summary>
+ public class Configuration : IDisposable
+ {
+ private readonly ConfigurationSafeHandle handle;
+ private readonly Repository repository;
+
+ public Configuration(Repository repository)
+ {
+ this.repository = repository;
+ Ensure.Success(NativeMethods.git_repository_config(out handle, this.repository.Handle, null, null));
+ }
+
+ #region IDisposable Members
+
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ public void Dispose()
+ {
+ Dispose(true);
+ GC.SuppressFinalize(this);
+ }
+
+ #endregion
+
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
+ protected virtual void Dispose(bool disposing)
+ {
+ if (handle != null && !handle.IsInvalid)
+ {
+ handle.Dispose();
+ }
+ }
+
+ /// <summary>
+ /// Get a configuration value for a key. Keys are in the form 'section.name'. For example in
+ /// order to get the value for this in a .gitconfig file:
+ ///
+ /// [core]
+ /// bare = true
+ ///
+ /// You would call:
+ ///
+ /// bool isBare = repo.Config.Get<bool>("core.bare");
+ /// </summary>
+ /// <typeparam name = "T"></typeparam>
+ /// <param name = "key"></param>
+ /// <returns></returns>
+ public T Get<T>(string key)
+ {
+ if (typeof(T) == typeof(string))
+ {
+ return (T)(object)GetString(key);
+ }
+
+ if (typeof(T) == typeof(bool))
+ {
+ return (T)(object)GetBool(key);
+ }
+
+ if (typeof(T) == typeof(int))
+ {
+ return (T)(object)GetInt(key);
+ }
+
+ if (typeof(T) == typeof(long))
+ {
+ return (T)(object)GetLong(key);
+ }
+
+ return default(T);
+ }
+
+ private bool GetBool(string key)
+ {
+ bool value;
+ Ensure.Success(NativeMethods.git_config_get_bool(handle, key, out value));
+ return value;
+ }
+
+ private int GetInt(string key)
+ {
+ int value;
+ Ensure.Success(NativeMethods.git_config_get_int(handle, key, out value));
+ return value;
+ }
+
+ private long GetLong(string key)
+ {
+ long value;
+ Ensure.Success(NativeMethods.git_config_get_long(handle, key, out value));
+ return value;
+ }
+
+ private string GetString(string key)
+ {
+ IntPtr value;
+ Ensure.Success(NativeMethods.git_config_get_string(handle, key, out value));
+ return value.MarshallAsString();
+ }
+ }
+}
@@ -0,0 +1,11 @@
+namespace LibGit2Sharp.Core
+{
+ internal class ConfigurationSafeHandle : SafeHandleBase
+ {
+ protected override bool ReleaseHandle()
+ {
+ NativeMethods.git_config_free(handle);
+ return true;
+ }
+ }
+}
@@ -50,6 +50,21 @@ internal static class NativeMethods
[DllImport(libgit2)]
public static extern IntPtr git_commit_tree_oid(IntPtr commit);
+ [DllImport(libgit2)]
+ public static extern void git_config_free(IntPtr cfg);
+
+ [DllImport(libgit2)]
+ public static extern int git_config_get_bool(ConfigurationSafeHandle cfg, string name, out bool value);
+
+ [DllImport(libgit2)]
+ public static extern int git_config_get_int(ConfigurationSafeHandle cfg, string name, out int value);
+
+ [DllImport(libgit2)]
+ public static extern int git_config_get_long(ConfigurationSafeHandle cfg, string name, out long value);
+
+ [DllImport(libgit2)]
+ public static extern int git_config_get_string(ConfigurationSafeHandle cfg, string name, out IntPtr value);
+
[DllImport(libgit2)]
public static extern int git_index_add(
IndexSafeHandle index,
@@ -158,6 +173,9 @@ internal static class NativeMethods
[DllImport(libgit2)]
public static extern GitReferenceType git_reference_type(IntPtr reference);
+ [DllImport(libgit2)]
+ public static extern int git_repository_config(out ConfigurationSafeHandle cfg, RepositorySafeHandle repo, string userConfigPath, string systemConfigPath);
+
[DllImport(libgit2)]
public static extern IntPtr git_repository_database(RepositorySafeHandle repository);
@@ -50,7 +50,9 @@
<Compile Include="BranchCollection.cs" />
<Compile Include="Commit.cs" />
<Compile Include="CommitCollection.cs" />
+ <Compile Include="Configuration.cs" />
<Compile Include="LibGit2Exception.cs" />
+ <Compile Include="Core\ConfigurationSafeHandle.cs" />
<Compile Include="Core\Ensure.cs" />
<Compile Include="Core\Epoch.cs" />
<Compile Include="Core\GitErrorCode.cs" />
View
@@ -12,6 +12,7 @@ public class Repository : IDisposable
{
private readonly BranchCollection branches;
private readonly CommitCollection commits;
+ private readonly Lazy<Configuration> config;
private readonly RepositorySafeHandle handle;
private readonly Index index;
private readonly ReferenceCollection refs;
@@ -43,6 +44,7 @@ public Repository(string path)
branches = new BranchCollection(this);
tags = new TagCollection(this);
info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare));
+ config = new Lazy<Configuration>(() => new Configuration(this));
}
internal RepositorySafeHandle Handle
@@ -69,6 +71,14 @@ public Branch Head
}
}
+ /// <summary>
+ /// Provides access to the configuration settings for this repository.
+ /// </summary>
+ public Configuration Config
+ {
+ get { return config.Value; }
+ }
+
/// <summary>
/// Gets the index.
/// </summary>

0 comments on commit 87248e0

Please sign in to comment.