Skip to content
Browse files

Make Configuration.Delete() able to target global and system configur…

…ation files

Fix #161
  • Loading branch information...
1 parent 33b486a commit adf32bc0a3c27f42980b85927de78d0e6dcbf481 @nulltoken nulltoken committed Jun 1, 2012
Showing with 75 additions and 13 deletions.
  1. +38 −2 LibGit2Sharp.Tests/ConfigurationFixture.cs
  2. +26 −10 LibGit2Sharp/Configuration.cs
  3. +11 −1 LibGit2Sharp/Core/Ensure.cs
View
40 LibGit2Sharp.Tests/ConfigurationFixture.cs
@@ -1,6 +1,6 @@
using System;
using System.IO;
-using System.Text.RegularExpressions;
+using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
@@ -44,7 +44,7 @@ private static void AssertValueInGlobalConfigFile(string regex)
}
[Fact]
- public void CanDeleteConfiguration()
+ public void CanDeleteAnEntryFromTheLocalConfiguration()
{
var path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
using (var repo = new Repository(path.RepositoryPath))
@@ -60,6 +60,42 @@ public void CanDeleteConfiguration()
}
}
+ [Fact]
+ public void CanDeleteAnEntryFromTheGlobalConfiguration()
+ {
+ SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
+
+ string confs = Path.Combine(scd.DirectoryPath, "confs");
+ Directory.CreateDirectory(confs);
+
+ string globalLocation = Path.Combine(confs, "my-global-config");
+ string systemLocation = Path.Combine(confs, "my-system-config");
+
+ StringBuilder sb = new StringBuilder()
+ .AppendLine("[Wow]")
+ .AppendFormat("Man-I-am-totally-global = 42{0}", Environment.NewLine);
+
+ File.WriteAllText(globalLocation, sb.ToString());
+
+ var options = new RepositoryOptions
+ {
+ GlobalConfigurationLocation = globalLocation,
+ SystemConfigurationLocation = systemLocation,
+ };
+
+ using (var repo = new Repository(BareTestRepoPath, options))
+ {
+ Assert.True(repo.Config.HasGlobalConfig);
+ Assert.Equal(42, repo.Config.Get("Wow.Man-I-am-totally-global", 1337));
+
+ repo.Config.Delete("Wow.Man-I-am-totally-global");
+ Assert.Equal(42, repo.Config.Get("Wow.Man-I-am-totally-global", 1337));
+
+ repo.Config.Delete("Wow.Man-I-am-totally-global", ConfigurationLevel.Global);
+ Assert.Equal(1337, repo.Config.Get("Wow.Man-I-am-totally-global", 1337));
+ }
+ }
+
[SkippableFact]
public void CanGetGlobalStringValue()
{
View
36 LibGit2Sharp/Configuration.cs
@@ -142,9 +142,19 @@ public void Dispose()
/// Delete a configuration variable (key and value).
/// </summary>
/// <param name = "key">The key to delete.</param>
- public void Delete(string key)
+ /// <param name = "level">The configuration file which should be considered as the target of this operation</param>
+ public void Delete(string key, ConfigurationLevel level = ConfigurationLevel.Local)
{
- Ensure.Success(NativeMethods.git_config_delete(localHandle, key));
+ ConfigurationSafeHandle h = RetrieveConfigurationHandle(level);
+
+ int res = NativeMethods.git_config_delete(h, key);
+
+ if (res == (int)GitErrorCode.GIT_ENOTFOUND)
+ {
+ return;
+ }
+
+ Ensure.Success(res);
Save();
}
@@ -314,6 +324,19 @@ public void Set<T>(string key, T value, ConfigurationLevel level = Configuration
{
Ensure.ArgumentNotNullOrEmptyString(key, "key");
+ ConfigurationSafeHandle h = RetrieveConfigurationHandle(level);
+
+ if (!configurationTypedUpdater.ContainsKey(typeof(T)))
+ {
+ throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Generic Argument of type '{0}' is not supported.", typeof(T).FullName));
+ }
+
+ configurationTypedUpdater[typeof(T)](key, value, h);
+ Save();
+ }
+
+ private ConfigurationSafeHandle RetrieveConfigurationHandle(ConfigurationLevel level)
+ {
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.");
@@ -348,14 +371,7 @@ public void Set<T>(string key, T value, ConfigurationLevel level = Configuration
default:
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Configuration level has an unexpected value ('{0}').", Enum.GetName(typeof(ConfigurationLevel), level)), "level");
}
-
- if (!configurationTypedUpdater.ContainsKey(typeof(T)))
- {
- throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Generic Argument of type '{0}' is not supported.", typeof(T).FullName));
- }
-
- configurationTypedUpdater[typeof(T)](key, value, h);
- Save();
+ return h;
}
private delegate int ConfigGetter<T>(out T value, ConfigurationSafeHandle handle, string name);
View
12 LibGit2Sharp/Core/Ensure.cs
@@ -64,9 +64,19 @@ public static void Success(int result, bool allowPositiveResult = false)
return;
}
+ string errorMessage;
GitError error = NativeMethods.giterr_last().MarshalAsGitError();
- var errorMessage = (string)marshaler.MarshalNativeToManaged(error.Message);
+
+ if (error == null)
+ {
+ error = new GitError { Klass = -1, Message = IntPtr.Zero };
+ errorMessage = "No error message has been provided by the native library";
+ }
+ else
+ {
+ errorMessage = (string)marshaler.MarshalNativeToManaged(error.Message);
+ }
throw new LibGit2Exception(
String.Format(CultureInfo.InvariantCulture, "An error was raised by libgit2. Class = {0} ({1}).{2}{3}",

0 comments on commit adf32bc

Please sign in to comment.
Something went wrong with that request. Please try again.