Permalink
Browse files

Allow access to config outside the context of a repo

  • Loading branch information...
1 parent 61af9fc commit 2ec29cecc83c510705077695445c05a487f41841 @tclem tclem committed with nulltoken Nov 16, 2011
Showing with 76 additions and 4 deletions.
  1. +42 −0 LibGit2Sharp.Tests/ConfigurationFixture.cs
  2. +34 −4 LibGit2Sharp/Configuration.cs
@@ -80,6 +80,16 @@ public void CanGetGlobalStringValue()
}
[Test]
+ public void CanGetGlobalStringValueWithoutRepo()
+ {
+ using (var config = new Configuration())
+ {
+ InconclusiveIf(() => !config.HasGlobalConfig, "No Git global configuration available");
+ config.Get<string>("user.name", null).ShouldNotBeNull();
+ }
+ }
+
+ [Test]
public void CanReadBooleanValue()
{
using (var repo = new Repository(Constants.StandardTestRepoPath))
@@ -155,6 +165,38 @@ public void CanSetGlobalStringValue()
}
[Test]
+ public void CanSetGlobalStringValueWithoutRepo()
+ {
+ using(var config = new Configuration())
+ {
+ InconclusiveIf(() => !config.HasGlobalConfig, "No Git global configuration available");
+
+ var existing = config.Get<string>("user.name", null);
+ existing.ShouldNotBeNull();
+
+ try
+ {
+ config.Set("user.name", "Unit Test", ConfigurationLevel.Global);
+
+ AssertValueInGlobalConfigFile("name = Unit Test$");
+ }
+ finally
+ {
+ config.Set("user.name", existing, ConfigurationLevel.Global);
+ }
+ }
+ }
+
+ [Test]
+ public void SettingLocalConfigurationOutsideAReposThrows()
+ {
+ using (var config = new Configuration())
+ {
+ Assert.Throws<LibGit2Exception>(() => config.Set("unittests.intsetting", 3));
+ }
+ }
+
+ [Test]
public void CanSetIntValue()
{
var path = BuildTemporaryCloneOfTestRepo(Constants.StandardTestRepoPath);
@@ -30,6 +30,22 @@ internal Configuration(Repository repository)
}
/// <summary>
+ /// Access configuration values without a repository. Generally you want to access configuration via an instance of <see cref = "Repository" /> instead.
+ /// </summary>
+ public Configuration()
+ : this(null)
+ {
+ }
+
+ /// <summary>
+ /// Determines if there is a local repository level Git configuration file.
+ /// </summary>
+ private bool HasLocalConfig
+ {
+ get { return localHandle != null; }
+ }
+
+ /// <summary>
/// Determines if a Git configuration file specific to the current interactive user has been found.
/// </summary>
public bool HasGlobalConfig
@@ -183,7 +199,13 @@ public T Get<T>(string key, T defaultValue)
throw new ArgumentException(string.Format("Generic Argument of type '{0}' is not supported.", typeof(T).FullName));
}
- return (T)configurationTypedRetriever[typeof(T)](key, defaultValue, LocalHandle);
+ ConfigurationSafeHandle handle = (LocalHandle ?? globalHandle) ?? systemHandle;
+ if (handle == null)
+ {
+ throw new LibGit2Exception("Could not find a local, global or system level configuration.");
+ }
+
+ return (T)configurationTypedRetriever[typeof(T)](key, defaultValue, handle);
}
/// <summary>
@@ -223,7 +245,7 @@ public T Get<T>(string firstKeyPart, string secondKeyPart, T defaultValue)
///
/// <code>
/// [difftool "kdiff3"]
- /// path = c:/Program Files/KDiff3/kdiff3.exe
+ /// path = c:/Program Files/KDiff3/kdiff3.exe
/// </code>
///
/// You would call:
@@ -245,7 +267,7 @@ public T Get<T>(string firstKeyPart, string secondKeyPart, string thirdKeyPart,
Ensure.ArgumentNotNull(secondKeyPart, "secondKeyPart");
Ensure.ArgumentNotNull(thirdKeyPart, "secondKeyPart");
- return Get(new [] { firstKeyPart, secondKeyPart, thirdKeyPart }, defaultValue);
+ return Get(new[] { firstKeyPart, secondKeyPart, thirdKeyPart }, defaultValue);
}
/// <summary>
@@ -278,7 +300,10 @@ public T Get<T>(string[] keyParts, T defaultValue)
private void Init()
{
- Ensure.Success(NativeMethods.git_repository_config(out localHandle, repository.Handle, globalConfigPath, systemConfigPath));
+ if (repository != null)
+ {
+ Ensure.Success(NativeMethods.git_repository_config(out localHandle, repository.Handle, globalConfigPath, systemConfigPath));
+ }
if (globalConfigPath != null)
{
@@ -332,6 +357,11 @@ public void Set<T>(string key, T value, ConfigurationLevel level = Configuration
{
Ensure.ArgumentNotNullOrEmptyString(key, "key");
+ if (level == ConfigurationLevel.Local && !HasLocalConfig)
+ {
+ throw new LibGit2Exception("No local configuration file has been found. You must use ConfigurationLevel.Global when accessing configuration outside of repository.");
+ }
+
if (level == ConfigurationLevel.Global && !HasGlobalConfig)
{
throw new LibGit2Exception("No global configuration file has been found.");

0 comments on commit 2ec29ce

Please sign in to comment.