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
165 lines (143 loc) · 5.82 KB
/
Copy pathEnsure.cs
File metadata and controls
165 lines (143 loc) · 5.82 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Diagnostics;
using System.Globalization;
namespace LibGit2Sharp.Core
{
/// <summary>
/// Ensure input parameters
/// </summary>
[DebuggerStepThrough]
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);
}
}
private static void HandleError(int result)
{
string errorMessage;
GitError error = NativeMethods.giterr_last().MarshalAsGitError();
if (error == null)
{
error = new GitError { Category = GitErrorCategory.Unknown, Message = IntPtr.Zero };
errorMessage = "No error message has been provided by the native library";
}
else
{
errorMessage = Utf8Marshaler.FromNative(error.Message);
}
switch (result)
{
case (int)GitErrorCode.BareRepo:
throw new BareRepositoryException(errorMessage, (GitErrorCode)result, error.Category);
case (int)GitErrorCode.Exists:
throw new NameConflictException(errorMessage, (GitErrorCode)result, error.Category);
case (int)GitErrorCode.InvalidSpecification:
throw new InvalidSpecificationException(errorMessage, (GitErrorCode)result, error.Category);
case (int)GitErrorCode.UnmergedEntries:
throw new UnmergedIndexEntriesException(errorMessage, (GitErrorCode)result, error.Category);
case (int)GitErrorCode.NonFastForward:
throw new NonFastForwardException(errorMessage, (GitErrorCode)result, error.Category);
case (int)GitErrorCode.MergeConflict:
throw new MergeConflictException(errorMessage, (GitErrorCode)result, error.Category);
default:
throw new LibGit2SharpException(errorMessage, (GitErrorCode)result, error.Category);
}
}
/// <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>
/// <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 LibGit2SharpException(string.Format(CultureInfo.InvariantCulture,
"No valid git object identified by '{0}' exists in the repository.",
identifier));
}
}
}