Skip to content

Commit 5a1fa41

Browse files
committed
Short-circuit Committishes for simple identifiers
1 parent bd85a5b commit 5a1fa41

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -365,57 +365,67 @@ private static bool AllowOrphanReference(IRepository repo, string identifier)
365365
|| string.Equals(identifier, repo.Head.CanonicalName, StringComparison.Ordinal);
366366
}
367367

368-
/// <summary>
369-
/// Dereferences the passed identifier to a commit. If the identifier is enumerable, all items are dereferenced.
370-
/// </summary>
371-
/// <param name="repo">Repository to search</param>
372-
/// <param name="identifier">Committish to dereference</param>
373-
/// <param name="throwIfNotFound">If true, allow thrown exceptions to propagate. If false, exceptions will be swallowed and null returned.</param>
374-
/// <returns>A series of commit <see cref="ObjectId"/>s which identify commit objects.</returns>
375-
internal static IEnumerable<ObjectId> Committishes(this Repository repo, object identifier, bool throwIfNotFound = false)
368+
private static ObjectId SingleCommittish(this Repository repo, object identifier)
376369
{
377-
ObjectId singleReturnValue = null;
370+
if (ReferenceEquals(identifier, null))
371+
{
372+
return null;
373+
}
378374

379375
if (identifier is string)
380376
{
381-
singleReturnValue = DereferenceToCommit(repo, identifier as string);
377+
return DereferenceToCommit(repo, (string)identifier);
382378
}
383379

384380
if (identifier is ObjectId)
385381
{
386-
singleReturnValue = DereferenceToCommit(repo, ((ObjectId) identifier).Sha);
382+
return DereferenceToCommit(repo, ((ObjectId)identifier).Sha);
387383
}
388384

389385
if (identifier is Commit)
390386
{
391-
singleReturnValue = ((Commit) identifier).Id;
387+
return ((Commit)identifier).Id;
392388
}
393389

394390
if (identifier is TagAnnotation)
395391
{
396-
singleReturnValue = DereferenceToCommit(repo, ((TagAnnotation) identifier).Target.Id.Sha);
392+
return DereferenceToCommit(repo, ((TagAnnotation)identifier).Target.Id.Sha);
397393
}
398394

399395
if (identifier is Tag)
400396
{
401-
singleReturnValue = DereferenceToCommit(repo, ((Tag) identifier).Target.Id.Sha);
397+
return DereferenceToCommit(repo, ((Tag)identifier).Target.Id.Sha);
402398
}
403399

404-
if (identifier is Branch)
400+
var branch = identifier as Branch;
401+
if (branch != null)
405402
{
406-
var branch = (Branch) identifier;
407403
if (branch.Tip != null || !branch.IsCurrentRepositoryHead)
408404
{
409405
Ensure.GitObjectIsNotNull(branch.Tip, branch.CanonicalName);
410-
singleReturnValue = branch.Tip.Id;
406+
return branch.Tip.Id;
411407
}
412408
}
413409

414410
if (identifier is Reference)
415411
{
416-
singleReturnValue = DereferenceToCommit(repo, ((Reference) identifier).CanonicalName);
412+
return DereferenceToCommit(repo, ((Reference)identifier).CanonicalName);
417413
}
418414

415+
return null;
416+
}
417+
418+
/// <summary>
419+
/// Dereferences the passed identifier to a commit. If the identifier is enumerable, all items are dereferenced.
420+
/// </summary>
421+
/// <param name="repo">Repository to search</param>
422+
/// <param name="identifier">Committish to dereference</param>
423+
/// <param name="throwIfNotFound">If true, allow thrown exceptions to propagate. If false, exceptions will be swallowed and null returned.</param>
424+
/// <returns>A series of commit <see cref="ObjectId"/>s which identify commit objects.</returns>
425+
internal static IEnumerable<ObjectId> Committishes(this Repository repo, object identifier, bool throwIfNotFound = false)
426+
{
427+
var singleReturnValue = repo.SingleCommittish(identifier);
428+
419429
if (singleReturnValue != null)
420430
{
421431
yield return singleReturnValue;

0 commit comments

Comments
 (0)