Created
June 1, 2012 09:42
-
-
Save nulltoken/2850800 to your computer and use it in GitHub Desktop.
[LibGit2Sharp] Proposal for #161 fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LibGit2Sharp.Tests/ConfigurationFixture.cs | 38 ++++++++++++++++++++++++++- | |
LibGit2Sharp/Configuration.cs | 36 +++++++++++++++++++------- | |
LibGit2Sharp/Core/Ensure.cs | 22 +++++++++++---- | |
3 files changed, 78 insertions(+), 18 deletions(-) | |
diff --git a/LibGit2Sharp.Tests/ConfigurationFixture.cs b/LibGit2Sharp.Tests/ConfigurationFixture.cs | |
index 7e1ebd9..634ed21 100644 | |
--- a/LibGit2Sharp.Tests/ConfigurationFixture.cs | |
+++ b/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,40 @@ 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"); | |
+ | |
+ 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, | |
+ }; | |
+ | |
+ using (var repo = new Repository(BareTestRepoPath, options)) | |
+ { | |
+ Assert.True(repo.Config.HasGlobalConfig); | |
+ Assert.Equal(42, repo.Config.Get<int>("Wow.Man-I-am-totally-global", 1337)); | |
+ | |
+ repo.Config.Delete("Wow.Man-I-am-totally-global"); | |
+ Assert.Equal(42, repo.Config.Get<int>("Wow.Man-I-am-totally-global", 1337)); | |
+ | |
+ repo.Config.Delete("Wow.Man-I-am-totally-global", ConfigurationLevel.Global); | |
+ Assert.Equal(1337, repo.Config.Get<int>("Wow.Man-I-am-totally-global", 1337)); | |
+ } | |
+ } | |
+ | |
[SkippableFact] | |
public void CanGetGlobalStringValue() | |
{ | |
diff --git a/LibGit2Sharp/Configuration.cs b/LibGit2Sharp/Configuration.cs | |
index 8784b43..fa2d978 100644 | |
--- a/LibGit2Sharp/Configuration.cs | |
+++ b/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); | |
diff --git a/LibGit2Sharp/Core/Ensure.cs b/LibGit2Sharp/Core/Ensure.cs | |
index 60566d3..f80651e 100644 | |
--- a/LibGit2Sharp/Core/Ensure.cs | |
+++ b/LibGit2Sharp/Core/Ensure.cs | |
@@ -64,16 +64,26 @@ 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}", | |
- Enum.GetName(typeof(GitErrorType), error.Klass), | |
- result, | |
- Environment.NewLine, | |
- errorMessage)); | |
+ String.Format(CultureInfo.InvariantCulture, "An error was raised by libgit2. Class = {0} ({1}).{2}{3}", | |
+ Enum.GetName(typeof(GitErrorType), error.Klass), | |
+ result, | |
+ Environment.NewLine, | |
+ errorMessage)); | |
} | |
/// <summary> | |
-- | |
1.7.8.msysgit.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment