forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnsure.cs
More file actions
97 lines (86 loc) · 3.67 KB
/
Copy pathEnsure.cs
File metadata and controls
97 lines (86 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Globalization;
namespace LibGit2Sharp.Core
{
/// <summary>
/// Ensure input parameters
/// </summary>
internal static class Ensure
{
/// <summary>
/// Checks an argument to ensure it isn't null.
/// </summary>
/// <param name = "argumentValue">The argument value to check.</param>
/// <param name = "argumentName">The name of the argument.</param>
public static void ArgumentNotNull(object argumentValue, string argumentName)
{
if (argumentValue == null)
{
throw new ArgumentNullException(argumentName);
}
}
/// <summary>
/// Checks a string argument to ensure it isn't null or empty.
/// </summary>
/// <param name = "argumentValue">The argument value to check.</param>
/// <param name = "argumentName">The name of the argument.</param>
public static void ArgumentNotNullOrEmptyString(string argumentValue, string argumentName)
{
ArgumentNotNull(argumentValue, argumentName);
if (argumentValue.Trim().Length == 0)
{
throw new ArgumentException("String cannot be empty", argumentName);
}
}
/// <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)
{
if (result == (int)GitErrorCode.GIT_SUCCESS)
{
return;
}
if (allowPositiveResult && result > (int)GitErrorCode.GIT_SUCCESS)
{
return;
}
string errorMessage = NativeMethods.git_lasterror();
throw new LibGit2Exception(
String.Format(CultureInfo.InvariantCulture, "An error was raised by libgit2. Error code = {0} ({1}).{2}{3}", Enum.GetName(typeof(GitErrorCode), result), result, Environment.NewLine, errorMessage));
}
/// <summary>
/// Checks an argument by applying provided checker.
/// </summary>
/// <param name = "argumentValue">The argument value to check.</param>
/// <param name = "checker">The predicate which has to be satisfied</param>
/// <param name = "argumentName">The name of the argument.</param>
public static void ArgumentConformsTo<T>(T argumentValue, Func<T, bool> checker, string argumentName)
{
if (checker(argumentValue))
{
return;
}
throw new ArgumentException(argumentName);
}
public static void GitObjectIsNotNull(GitObject gitObject, string identifier)
{
if (gitObject != null)
{
return;
}
throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture,
"No valid git object identified by '{0}' exists in the repository.",
identifier));
}
}
}