Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 57 additions & 24 deletions LibGit2Sharp/Core/Ensure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,11 @@ public static void ArgumentNotNullOrEmptyString(string argumentValue, string arg
}
}

/// <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 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)
private static void HandleError(int result)
{
if (result == (int)GitErrorCode.Ok)
{
return;
}

if (allowPositiveResult && result > (int)GitErrorCode.Ok)
{
return;
}

string errorMessage;
GitError error = NativeMethods.giterr_last().MarshalAsGitError();


if (error == null)
{
error = new GitError { Category = GitErrorCategory.Unknown, Message = IntPtr.Zero };
Expand Down Expand Up @@ -98,6 +75,62 @@ public static void Success(int result, bool allowPositiveResult = false)
}
}

/// <summary>
/// Check that the result of a C call was successful
/// <para>
/// The native function is expected to return strictly 0 for
/// success or a negative value in the case of failure.
/// </para>
/// </summary>
/// <param name = "result">The result to examine.</param>
public static void ZeroResult(int result)
{
if (result == (int)GitErrorCode.Ok)
{
return;
}

HandleError(result);
}

/// <summary>
/// Check that the result of a C call that returns a boolean value
/// was successful
/// <para>
/// The native function is expected to return strictly 0 for
/// success or a negative value in the case of failure.
/// </para>
/// </summary>
/// <param name = "result">The result to examine.</param>
public static void BooleanResult(int result)
{
if (result == (int)GitErrorCode.Ok || result == 1)
{
return;
}

HandleError(result);
}

/// <summary>
/// Check that the result of a C call that returns an integer value
/// was successful
/// <para>
/// The native function is expected to return strictly 0 for
/// success or a negative value in the case of failure.
/// </para>
/// </summary>
/// <param name = "result">The result to examine.</param>
public static void Int32Result(int result)
{
if (result >= (int)GitErrorCode.Ok)
{
return;
}

HandleError(result);
}

/// <summary>
/// Checks an argument by applying provided checker.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private sealed class LibraryLifetimeObject : CriticalFinalizerObject
// Ensure mono can JIT the .cctor and adjust the PATH before trying to load the native library.
// See https://github.com/libgit2/libgit2sharp/pull/190
[MethodImpl(MethodImplOptions.NoInlining)]
public LibraryLifetimeObject() { Ensure.Success(NativeMethods.git_threads_init()); }
public LibraryLifetimeObject() { Ensure.ZeroResult(NativeMethods.git_threads_init()); }
~LibraryLifetimeObject() { NativeMethods.git_threads_shutdown(); }
}

Expand Down Expand Up @@ -988,7 +988,7 @@ internal static extern int git_treebuilder_insert(
internal static extern void git_treebuilder_free(IntPtr bld);

[DllImport(libgit2)]
internal static extern bool git_blob_is_binary(GitObjectSafeHandle blob);
internal static extern int git_blob_is_binary(GitObjectSafeHandle blob);
}
}
// ReSharper restore InconsistentNaming
Loading