10000 Introduce repo.Committish(es) · apfunk/libgit2sharp@a3f95fc · GitHub
[go: up one dir, main page]

Skip to content

Commit a3f95fc

Browse files
committed
Introduce repo.Committish(es)
1 parent af3bc89 commit a3f95fc

File tree

2 files changed

+112
-92
lines changed

2 files changed

+112
-92
lines changed

LibGit2Sharp/CommitLog.cs

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private void Dispose(bool disposing)
204204

205205
private void InternalHidePush(IList<object> identifier, HidePushSignature hidePush)
206206
{
207-
IEnumerable<ObjectId> oids = RetrieveCommitOids(identifier).TakeWhile(o => o != null);
207+
IEnumerable<ObjectId> oids = repo.Committishes(identifier).TakeWhile(o => o != null);
208208

209209
foreach (ObjectId actedOn in oids)
210210
{
@@ -232,97 +232,6 @@ private void Sort(CommitSortStrategies options)
232232
Proxy.git_revwalk_sorting(handle, options);
233233
}
234234

235-
private ObjectId DereferenceToCommit(string identifier)
236-
{
237-
var options = LookUpOptions.DereferenceResultToCommit;
238-
239-
if (!AllowOrphanReference(identifier))
240-
{
241-
options |= LookUpOptions.ThrowWhenNoGitObjectHasBeenFound;
242-
}
243-
244-
// TODO: Should we check the type? Git-log allows TagAnnotation oid as parameter. But what about Blobs and Trees?
245-
GitObject commit = repo.Lookup(identifier, GitObjectType.Any, options);
246-
247-
return commit != null ? commit.Id : null;
248-
}
249-
250-
private bool AllowOrphanReference(string identifier)
251-
{
252-
return string.Equals(identifier, "HEAD", StringComparison.Ordinal)
253-
|| string.Equals(identifier, repo.Head.CanonicalName, StringComparison.Ordinal);
254-
}
255-
256-
private IEnumerable<ObjectId> RetrieveCommitOids(object identifier)
257-
{
258-
if (identifier is string)
259-
{
260-
yield return DereferenceToCommit(identifier as string);
261-
yield break;
262-
}
263-
264-
if (identifier is ObjectId)
265-
{
266-
yield return DereferenceToCommit(((ObjectId)identifier).Sha);
267-
yield break;
268-
}
269-
270-
if (identifier is Commit)
271-
{
272-
yield return ((Commit)identifier).Id;
273-
yield break;
274-
}
275-
276-
if (identifier is TagAnnotation)
277-
{
278-
yield return DereferenceToCommit(((TagAnnotation)identifier).Target.Id.Sha);
279-
yield break;
280-
}
281-
282-
if (identifier is Tag)
283-
{
284-
yield return DereferenceToCommit(((Tag)identifier).Target.Id.Sha);
285-
yield break;
286-
}
287-
288-
if (identifier is Branch)
289-
{
290-
var branch = (Branch)identifier;
291-
if (branch.Tip == null && branch.IsCurrentRepositoryHead)
292-
{
293-
yield return null;
294-
yield break;
295-
}
296-
297-
Ensure.GitObjectIsNotNull(branch.Tip, branch.CanonicalName);
298-
299-
yield return branch.Tip.Id;
300-
yield break;
301-
}
302-
303-
if (identifier is Reference)
304-
{
305-
yield return DereferenceToCommit(((Reference)identifier).CanonicalName);
306-
yield break;
307-
}
308-
309-
if (identifier is IEnumerable)
310-
{
311-
var enumerable = (IEnumerable)identifier;
312-
313-
foreach (object entry in enumerable)
314-
{
315-
F438 foreach (ObjectId oid in RetrieveCommitOids(entry))
316-
{
317-
yield return oid;
318-
}
319-
}
320-
321-
yield break;
322-
}
323-
324-
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected kind of identifier '{0}'.", identifier));
325-
}
326235
}
327236
}
328237
}

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Globalization;
45
using System.IO;
6+
using System.Linq;
57
using LibGit2Sharp.Core;
68
using LibGit2Sharp.Handlers;
79

@@ -318,5 +320,114 @@ internal static string BuildRelativePathFrom(this Repository repo, string path)
318320

319321
return normalizedPath.Substring(repo.Info.WorkingDirectory.Length);
320322
}
323+
324+
private static ObjectId DereferenceToCommit(Repository repo, string identifier)
325+
{
326+
var options = LookUpOptions.DereferenceResultToCommit;
327+
328+
if (!AllowOrphanReference(repo, identifier))
329+
{
330+
options |= LookUpOptions.ThrowWhenNoGitObjectHasBeenFound;
331+
}
332+
333+
// TODO: Should we check the type? Git-log allows TagAnnotation oid as parameter. But what about Blobs and Trees?
334+
GitObject commit = repo.Lookup(identifier, GitObjectType.Any, options);
335+
336+
return commit != null ? commit.Id : null;
337+
}
338+
339+
private static bool AllowOrphanReference(IRepository repo, string identifier)
340+
{
341+
return string.Equals(identifier, "HEAD", StringComparison.Ordinal)
342+
|| string.Equals(identifier, repo.Head.CanonicalName, StringComparison.Ordinal);
343+
}
344+
345+
/// <summary>
346+
/// Dereferences the passed identifier to a commit. If the identifier is enumerable, all items are dereferenced.
347+
/// </summary>
348+
/// <param name="repo">Repository to search</param>
349+
/// <param name="identifier">Committish to dereference</param>
350+
/// <param name="throwIfNotFound">If true, allow thrown exceptions to propagate. If false, exceptions will be swallowed and null returned.</param>
351+
/// <returns>A series of commit <see cref="ObjectId"/>s which identify commit objects.</returns>
352+
internal static IEnumerable<ObjectId> Committishes(this Repository repo, object identifier, bool throwIfNotFound = false)
353+
{
354+
ObjectId singleReturnValue = null;
355+
356+
if (identifier is string)
357+
{
358+
singleReturnValue = DereferenceToCommit(repo, identifier as string);
359+
}
360+
361+
if (identifier is ObjectId)
362+
{
363+
singleReturnValue = DereferenceToCommit(repo, ((ObjectId) identifier).Sha);
364+
}
365+
366+
if (identifier is Commit)
367+
{
368+
singleReturnValue = ((Commit) identifier).Id;
369+
}
370+
371+
if (identifier is TagAnnotation)
372+
{
373+
singleReturnValue = DereferenceToCommit(repo, ((TagAnnotation) identifier).Target.Id.Sha);
374+
}
375+
376+
if (identifier is Tag)
377+
{
378+
singleReturnValue = DereferenceToCommit(repo, ((Tag) identifier).Target.Id.Sha);
379+
}
380+
381+
if (identifier is Branch)
382+
{
383+
var branch = (Branch) identifier;
384+
if (branch.Tip != null || !branch.IsCurrentRepositoryHead)
385+
{
386+
Ensure.GitObjectIsNotNull(branch.Tip, branch.CanonicalName);
387+
singleReturnValue = branch.Tip.Id;
388+
}
389+
}
390+
391+
if (identifier is Reference)
392+
{
393+
singleReturnValue = DereferenceToCommit(repo, ((Reference) identifier).CanonicalName);
394+
}
395+
396+
if (singleReturnValue != null)
397+
{
398+
yield return singleReturnValue;
399+
yield break;
400+
}
401+
402+
if (identifier is IEnumerable)
403+
{
404+
foreach (object entry in (IEnumerable)identifier)
405+
{
406+
foreach (ObjectId oid in Committishes(repo, entry))
407+
{
408+
yield return oid;
409+
}
410+
}
411+
412+
yield break;
413+
}
414+
415+
if (throwIfNotFound)
416+
{
417+
throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected kind of identifier '{0}'.", identifier));
418+
}
419+
yield return null;
420+
}
421+
422+
/// <summary>
423+
/// Dereference the identifier to a commit. If the identifier is enumerable, dereference the first element.
424+
/// </summary>
425+
/// <param name="repo">The <see cref="Repository"/> to search</param>
426+
/// <param name="identifier">Committish to dereference</param>
427+
/// <returns>An <see cref="ObjectId"/> for a commit object.</returns>
428+
internal static ObjectId Committish(this Repository repo, object identifier)
429+
{
430+
return repo.Committishes(identifier, true).First();
431+
}
321432
}
322433
}

0 commit comments

Comments
 (0)
0