-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathRepositoryExtensions.cs
474 lines (413 loc) · 21.7 KB
/
RepositoryExtensions.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// Provides helper overloads to a <see cref="Repository"/>.
/// </summary>
public static class RepositoryExtensions
{
/// <summary>
/// Try to lookup an object by its sha or a reference name.
/// </summary>
/// <typeparam name="T">The kind of <see cref="GitObject"/> to lookup.</typeparam>
/// <param name="repository">The <see cref="Repository"/> being looked up.</param>
/// <param name="objectish">The revparse spec for the object to lookup.</param>
/// <returns>The retrieved <see cref="GitObject"/>, or <c>null</c> if none was found.</returns>
public static T Lookup<T>(this IRepository repository, string objectish) where T : GitObject
{
EnsureNoGitLink<T>();
if (typeof(T) == typeof(GitObject))
{
return (T)repository.Lookup(objectish);
}
return (T)repository.Lookup(objectish, GitObject.TypeToKindMap[typeof(T)]);
}
/// <summary>
/// Try to lookup an object by its <see cref="ObjectId"/>.
/// </summary>
/// <typeparam name="T">The kind of <see cref="GitObject"/> to lookup.</typeparam>
/// <param name="repository">The <see cref="Repository"/> being looked up.</param>
/// <param name="id">The id.</param>
/// <returns>The retrieved <see cref="GitObject"/>, or <c>null</c> if none was found.</returns>
public static T Lookup<T>(this IRepository repository, ObjectId id) where T : GitObject
{
EnsureNoGitLink<T>();
if (typeof(T) == typeof(GitObject))
{
return (T)repository.Lookup(id);
}
return (T)repository.Lookup(id, GitObject.TypeToKindMap[typeof(T)]);
}
private static void EnsureNoGitLink<T>() where T : GitObject
{
if (typeof(T) != typeof(GitLink))
{
return;
}
throw new ArgumentException("A GitObject of type 'GitLink' cannot be looked up.");
}
/// <summary>
/// Creates a lightweight tag with the specified name. This tag will point at the commit pointed at by the <see cref="Repository.Head"/>.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="tagName">The name of the tag to create.</param>
public static Tag ApplyTag(this IRepository repository, string tagName)
{
return repository.Tags.Add(tagName, RetrieveHeadCommit(repository));
}
/// <summary>
/// Creates a lightweight tag with the specified name. This tag will point at the <paramref name="objectish"/>.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="tagName">The name of the tag to create.</param>
/// <param name="objectish">The revparse spec for the target object.</param>
public static Tag ApplyTag(this IRepository repository, string tagName, string objectish)
{
return repository.Tags.Add(tagName, objectish);
}
/// <summary>
/// Creates an annotated tag with the specified name. This tag will point at the commit pointed at by the <see cref="Repository.Head"/>.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="tagName">The name of the tag to create.</param>
/// <param name="tagger">The identity of the creator of this tag.</param>
/// <param name="message">The annotation message.</param>
public static Tag ApplyTag(this IRepository repository, string tagName, Signature tagger, string message)
{
return repository.Tags.Add(tagName, RetrieveHeadCommit(repository), tagger, message);
}
private static Commit RetrieveHeadCommit(IRepository repository)
{
Commit commit = repository.Head.Tip;
Ensure.GitObjectIsNotNull(commit, "HEAD");
return commit;
}
/// <summary>
/// Creates an annotated tag with the specified name. This tag will point at the <paramref name="objectish"/>.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="tagName">The name of the tag to create.</param>
/// <param name="objectish">The revparse spec for the target object.</param>
/// <param name="tagger">The identity of the creator of this tag.</param>
/// <param name="message">The annotation message.</param>
public static Tag ApplyTag(this IRepository repository, string tagName, string objectish, Signature tagger, string message)
{
return repository.Tags.Add(tagName, objectish, tagger, message);
}
/// <summary>
/// Creates a branch with the specified name. This branch will point at the commit pointed at by the <see cref="Repository.Head"/>.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="branchName">The name of the branch to create.</param>
public static Branch CreateBranch(this IRepository repository, string branchName)
{
var head = repository.Head;
var reflogName = head is DetachedHead ? head.Tip.Sha : head.FriendlyName;
return CreateBranch(repository, branchName, reflogName);
}
/// <summary>
/// Creates a branch with the specified name. This branch will point at <paramref name="target"/>.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="branchName">The name of the branch to create.</param>
/// <param name="target">The commit which should be pointed at by the Branch.</param>
public static Branch CreateBranch(this IRepository repository, string branchName, Commit target)
{
return repository.Branches.Add(branchName, target);
}
/// <summary>
/// Creates a branch with the specified name. This branch will point at the commit pointed at by the <see cref="Repository.Head"/>.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="branchName">The name of the branch to create.</param>
/// <param name="committish">The revparse spec for the target commit.</param>
public static Branch CreateBranch(this IRepository repository, string branchName, string committish)
{
return repository.Branches.Add(branchName, committish);
}
/// <summary>
/// Sets the current <see cref="Repository.Head"/> and resets the <see cref="Index"/> and
/// the content of the working tree to match.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="resetMode">Flavor of reset operation to perform.</param>
public static void Reset(this IRepository repository, ResetMode resetMode)
{
repository.Reset(resetMode, "HEAD");
}
/// <summary>
/// Sets the current <see cref="Repository.Head"/> to the specified commitish and optionally resets the <see cref="Index"/> and
/// the content of the working tree to match.
/// </summary>
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
/// <param name="resetMode">Flavor of reset operation to perform.</param>
/// <param name="committish">A revparse spec for the target commit object.</param>
public static void Reset(this IRepository repository, ResetMode resetMode, string committish)
{
Ensure.ArgumentNotNullOrEmptyString(committish, "committish");
Commit commit = LookUpCommit(repository, committish);
repository.Reset(resetMode, commit);
}
private static Commit LookUpCommit(IRepository repository, string committish)
{
GitObject obj = repository.Lookup(committish);
Ensure.GitObjectIsNotNull(obj, committish);
return obj.Peel<Commit>(true);
}
/// <summary>
/// Stores the content of the <see cref="Repository.Index"/> as a new <see cref="LibGit2Sharp.Commit"/> into the repository.
/// The tip of the <see cref="Repository.Head"/> will be used as the parent of this new Commit.
/// Once the commit is created, the <see cref="Repository.Head"/> will move forward to point at it.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="message">The description of why a change was made to the repository.</param>
/// <param name="author">The <see cref="Signature"/> of who made the change.</param>
/// <param name="committer">The <see cref="Signature"/> of who added the change to the repository.</param>
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
public static Commit Commit(this IRepository repository, string message, Signature author, Signature committer)
{
return repository.Commit(message, author, committer, default(CommitOptions));
}
internal static string BuildRelativePathFrom(this IRepository repo, string path)
{
//TODO: To be removed when libgit2 natively implements this
if (!Path.IsPathRooted(path))
{
return path;
}
string normalizedPath = Path.GetFullPath(path);
if (!PathStartsWith(repo, normalizedPath, repo.Info.WorkingDirectory))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,
"Unable to process file '{0}'. This file is not located under the working directory of the repository ('{1}').",
normalizedPath,
repo.Info.WorkingDirectory));
}
return normalizedPath.Substring(repo.Info.WorkingDirectory.Length);
}
internal static bool PathStartsWith(IRepository repository, string path, string value)
{
var pathCase = new PathCase(repository);
return pathCase.StartsWith(path, value);
}
private static ObjectId DereferenceToCommit(Repository repo, string identifier)
{
var options = LookUpOptions.DereferenceResultToCommit;
if (!AllowOrphanReference(repo, identifier))
{
options |= LookUpOptions.ThrowWhenNoGitObjectHasBeenFound;
}
// TODO: Should we check the type? Git-log allows TagAnnotation oid as parameter. But what about Blobs and Trees?
GitObject commit = repo.Lookup(identifier, GitObjectType.Any, options);
return commit != null ? commit.Id : null;
}
private static bool AllowOrphanReference(IRepository repo, string identifier)
{
return string.Equals(identifier, "HEAD", StringComparison.Ordinal)
|| string.Equals(identifier, repo.Head.CanonicalName, StringComparison.Ordinal);
}
private static ObjectId SingleCommittish(this Repository repo, object identifier)
{
if (ReferenceEquals(identifier, null))
{
return null;
}
if (identifier is string)
{
return DereferenceToCommit(repo, (string)identifier);
}
if (identifier is ObjectId)
{
return DereferenceToCommit(repo, ((ObjectId)identifier).Sha);
}
if (identifier is Commit)
{
return ((Commit)identifier).Id;
}
if (identifier is TagAnnotation)
{
return DereferenceToCommit(repo, ((TagAnnotation)identifier).Target.Id.Sha);
}
if (identifier is Tag)
{
return DereferenceToCommit(repo, ((Tag)identifier).Target.Id.Sha);
}
var branch = identifier as Branch;
if (branch != null)
{
if (branch.Tip != null || !branch.IsCurrentRepositoryHead)
{
Ensure.GitObjectIsNotNull(branch.Tip, branch.CanonicalName);
return branch.Tip.Id;
}
}
if (identifier is Reference)
{
return DereferenceToCommit(repo, ((Reference)identifier).CanonicalName);
}
return null;
}
/// <summary>
/// Dereferences the passed identifier to a commit. If the identifier is enumerable, all items are dereferenced.
/// </summary>
/// <param name="repo">Repository to search</param>
/// <param name="identifier">Committish to dereference</param>
/// <param name="throwIfNotFound">If true, allow thrown exceptions to propagate. If false, exceptions will be swallowed and null returned.</param>
/// <returns>A series of commit <see cref="ObjectId"/>s which identify commit objects.</returns>
internal static IEnumerable<ObjectId> Committishes(this Repository repo, object identifier, bool throwIfNotFound = false)
{
var singleReturnValue = repo.SingleCommittish(identifier);
if (singleReturnValue != null)
{
yield return singleReturnValue;
yield break;
}
if (identifier is IEnumerable)
{
foreach (object entry in (IEnumerable)identifier)
{
foreach (ObjectId oid in Committishes(repo, entry))
{
yield return oid;
}
}
yield break;
}
if (throwIfNotFound)
{
throw new LibGit2SharpException("Unexpected kind of identifier '{0}'.", identifier);
}
yield return null;
}
/// <summary>
/// Dereference the identifier to a commit. If the identifier is enumerable, dereference the first element.
/// </summary>
/// <param name="repo">The <see cref="Repository"/> to search</param>
/// <param name="identifier">Committish to dereference</param>
/// <returns>An <see cref="ObjectId"/> for a commit object.</returns>
internal static ObjectId Committish(this Repository repo, object identifier)
{
return repo.Committishes(identifier, true).First();
}
/// <summary>
/// Merges changes from branch into the branch pointed at by HEAD.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="branch">The branch to merge into the branch pointed at by HEAD.</param>
/// <param name="merger">The <see cref="Signature"/> of who is performing the merge.</param>
/// <returns>The <see cref="MergeResult"/> of the merge.</returns>
public static MergeResult Merge(this IRepository repository, Branch branch, Signature merger)
{
return repository.Merge(branch, merger, null);
}
/// <summary>
/// Merges changes from the commit into the branch pointed at by HEAD.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="committish">The commit to merge into the branch pointed at by HEAD.</param>
/// <param name="merger">The <see cref="Signature"/> of who is performing the merge.</param>
/// <returns>The <see cref="MergeResult"/> of the merge.</returns>
public static MergeResult Merge(this IRepository repository, string committish, Signature merger)
{
return repository.Merge(committish, merger, null);
}
/// <summary>
/// Updates specifed paths in the index and working directory with the versions from the specified branch, reference, or SHA.
/// <para>
/// This method does not switch branches or update the current repository HEAD.
/// </para>
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name = "committishOrBranchSpec">A revparse spec for the commit or branch to checkout paths from.</param>
/// <param name="paths">The paths to checkout. Will throw if null is passed in. Passing an empty enumeration results in nothing being checked out.</param>
public static void CheckoutPaths(this IRepository repository, string committishOrBranchSpec, IEnumerable<string> paths)
{
repository.CheckoutPaths(committishOrBranchSpec, paths, null);
}
/// <summary>
/// Sets the current <see cref="IRepository.Head"/> to the specified commit and optionally resets the <see cref="Index"/> and
/// the content of the working tree to match.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="resetMode">Flavor of reset operation to perform.</param>
/// <param name="commit">The target commit object.</param>
public static void Reset(this IRepository repository, ResetMode resetMode, Commit commit)
{
repository.Reset(resetMode, commit);
}
/// <summary>
/// Find where each line of a file originated.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="path">Path of the file to blame.</param>
/// <returns>The blame for the file.</returns>
public static BlameHunkCollection Blame(this IRepository repository, string path)
{
return repository.Blame(path, null);
}
/// <summary>
/// Cherry-picks the specified commit.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="commit">The <see cref="LibGit2Sharp.Commit"/> to cherry-pick.</param>
/// <param name="committer">The <see cref="Signature"/> of who is performing the cherry pick.</param>
/// <returns>The result of the cherry pick.</returns>
public static CherryPickResult CherryPick(this IRepository repository, Commit commit, Signature committer)
{
return repository.CherryPick(commit, committer, null);
}
/// <summary>
/// Merges changes from commit into the branch pointed at by HEAD.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="commit">The commit to merge into the branch pointed at by HEAD.</param>
/// <param name="merger">The <see cref="Signature"/> of who is performing the merge.</param>
/// <returns>The <see cref="MergeResult"/> of the merge.</returns>
public static MergeResult Merge(this IRepository repository, Commit commit, Signature merger)
{
return repository.Merge(commit, merger, null);
}
/// <summary>
/// Revert the specified commit.
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="commit">The <see cref="LibGit2Sharp.Commit"/> to revert.</param>
/// <param name="reverter">The <see cref="Signature"/> of who is performing the revert.</param>
/// <returns>The result of the revert.</returns>
public static RevertResult Revert(this IRepository repository, Commit commit, Signature reverter)
{
return repository.Revert(commit, reverter, null);
}
/// <summary>
/// Retrieves the state of all files in the working directory, comparing them against the staging area and the latest commit.
/// </summary>
/// <returns>A <see cref="RepositoryStatus"/> holding the state of all the files.</returns>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
public static RepositoryStatus RetrieveStatus(this IRepository repository)
{
Proxy.git_index_read(repository.Index.Handle);
return new RepositoryStatus((Repository)repository, null);
}
/// <summary>
/// Finds the most recent annotated tag that is reachable from a commit.
/// <para>
/// If the tag points to the commit, then only the tag is shown. Otherwise,
/// it suffixes the tag name with the number of additional commits on top
/// of the tagged object and the abbreviated object name of the most recent commit.
/// </para>
/// </summary>
/// <param name="repository">The <see cref="IRepository"/> being worked with.</param>
/// <param name="commit">The commit to be described.</param>
/// <returns>A descriptive identifier for the commit based on the nearest annotated tag.</returns>
public static string Describe(this IRepository repository, Commit commit)
{
return repository.Describe(commit, new DescribeOptions());
}
}
}