diff options
author | nulltoken <emeric.fermas@gmail.com> | 2011-06-01 15:53:16 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2011-06-01 23:28:54 +0400 |
commit | 381521c83e1c501dbb92ee0fb5b1f6416312f1e3 (patch) | |
tree | d3652cbf678ab94e2f22c34e19f6c6c1b0cbaf53 | |
parent | d42f31560cd254b4281924df693b145e6c77af23 (diff) |
Enhance error handling when native methods return 0 or positive values as success signals
Usually libgit2 API returns negative values for errors and 0 (zero) when the methods executed successfully. In some rare cases, some native methods may additionally return positive values. This fix cope with this.
-rw-r--r-- | LibGit2Sharp/Core/Ensure.cs | 21 | ||||
-rw-r--r-- | LibGit2Sharp/Index.cs | 5 |
2 files changed, 21 insertions, 5 deletions
diff --git a/LibGit2Sharp/Core/Ensure.cs b/LibGit2Sharp/Core/Ensure.cs index bbd4c31a..8d943cfd 100644 --- a/LibGit2Sharp/Core/Ensure.cs +++ b/LibGit2Sharp/Core/Ensure.cs @@ -38,11 +38,24 @@ namespace LibGit2Sharp.Core /// <summary> /// Check that the result of a C call was successful + /// <para> + /// This usually means that the method is expected to return 0. + /// In some rare cases, some methods may return negative values for errors and + /// positive values carrying information. Those positive values should be interpreted + /// as successful calls as well. + /// </para> /// </summary> - /// <param name = "result">The result.</param> - public static void Success(int result) + /// <param name = "result">The result to examine.</param> + /// <param name="allowPositiveResult">False to only allow success when comparing against 0, + /// True when positive values are allowed as well.</param> + public static void Success(int result, bool allowPositiveResult = false) { - if (result == (int) GitErrorCode.GIT_SUCCESS) + if (result == (int)GitErrorCode.GIT_SUCCESS) + { + return; + } + + if (allowPositiveResult && result > (int)GitErrorCode.GIT_SUCCESS) { return; } @@ -65,7 +78,7 @@ namespace LibGit2Sharp.Core { return; } - + throw new ArgumentException(argumentName); } } diff --git a/LibGit2Sharp/Index.cs b/LibGit2Sharp/Index.cs index cf1c811b..de822896 100644 --- a/LibGit2Sharp/Index.cs +++ b/LibGit2Sharp/Index.cs @@ -30,7 +30,10 @@ namespace LibGit2Sharp { Ensure.ArgumentNotNullOrEmptyString(path, "path"); - return this[NativeMethods.git_index_find(handle, path)]; + int res = NativeMethods.git_index_find(handle, path); + Ensure.Success(res, true); + + return this[res]; } } |