Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove orphan hack for Commits.QueryBy()

  • Loading branch information...
commit d31afcd16ff419963d68b76aa85898c4e9b03909 1 parent 7745cc2
@dahlbyk dahlbyk authored
Showing with 32 additions and 16 deletions.
  1. +13 −5 LibGit2Sharp.Tests/RepositoryFixture.cs
  2. +19 −11 LibGit2Sharp/CommitLog.cs
View
18 LibGit2Sharp.Tests/RepositoryFixture.cs
@@ -26,7 +26,10 @@ public void CanCreateBareRepo()
Assert.Equal(scd.RootedDirectoryPath + Path.DirectorySeparatorChar, repo.Info.Path);
Assert.True(repo.Info.IsBare);
- AssertInitializedRepository(repo);
+ AssertInitializedRepository(repo, "refs/heads/master");
+
+ repo.Refs.Add("HEAD", "refs/heads/orphan", true);
+ AssertInitializedRepository(repo, "refs/heads/orphan");
}
}
@@ -57,7 +60,10 @@ public void CanCreateStandardRepo()
AssertIsHidden(repo.Info.Path);
- AssertInitializedRepository(repo);
+ AssertInitializedRepository(repo, "refs/heads/master");
+
+ repo.Refs.Add("HEAD", "refs/heads/orphan", true);
+ AssertInitializedRepository(repo, "refs/heads/orphan");
}
}
@@ -147,7 +153,7 @@ public void CreatingRepoWithBadParamsThrows()
Assert.Throws<ArgumentNullException>(() => Repository.Init(null));
}
- private static void AssertInitializedRepository(Repository repo)
+ private static void AssertInitializedRepository(Repository repo, string expectedHeadTargetIdentifier)
{
Assert.NotNull(repo.Info.Path);
Assert.False(repo.Info.IsHeadDetached);
@@ -155,7 +161,7 @@ private static void AssertInitializedRepository(Repository repo)
Reference headRef = repo.Refs.Head;
Assert.NotNull(headRef);
- Assert.Equal("refs/heads/master", headRef.TargetIdentifier);
+ Assert.Equal(expectedHeadTargetIdentifier, headRef.TargetIdentifier);
Assert.Null(headRef.ResolveToDirectReference());
Assert.NotNull(repo.Head);
@@ -164,9 +170,11 @@ private static void AssertInitializedRepository(Repository repo)
Assert.Null(repo.Head.Tip);
Assert.Equal(0, repo.Commits.Count());
+ Assert.Equal(0, repo.Commits.QueryBy(new Filter()).Count());
+ Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = repo.Refs.Head }).Count());
Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = repo.Head }).Count());
Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = "HEAD" }).Count());
- Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = "refs/heads/master" }).Count());
+ Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new Filter { Since = expectedHeadTargetIdentifier }).Count());
Assert.Null(repo.Head["subdir/I-do-not-exist"]);
View
30 LibGit2Sharp/CommitLog.cs
@@ -59,11 +59,6 @@ public virtual GitSortOptions SortedBy
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the log.</returns>
public virtual IEnumerator<Commit> GetEnumerator()
{
- if ((repo.Info.IsEmpty) && queryFilter.SinceList.Any(o => PointsAtTheHead(o.ToString()))) // TODO: ToString() == fragile
- {
- return Enumerable.Empty<Commit>().GetEnumerator();
- }
-
return new CommitEnumerator(repo, queryFilter);
}
@@ -92,11 +87,6 @@ public virtual ICommitLog QueryBy(Filter filter)
return new CommitLog(repo, filter);
}
- private static bool PointsAtTheHead(string shaOrRefName)
- {
- return ("HEAD".Equals(shaOrRefName, StringComparison.Ordinal) || "refs/heads/master".Equals(shaOrRefName, StringComparison.Ordinal));
- }
-
/// <summary>
/// Find the best possible common ancestor given two <see cref = "Commit"/>s.
/// </summary>
@@ -266,12 +256,24 @@ private void Sort(GitSortOptions options)
private ObjectId DereferenceToCommit(string identifier)
{
+ var options = LookUpOptions.DereferenceResultToCommit;
+
+ if (!AllowOrphanReference(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, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound | LookUpOptions.DereferenceResultToCommit);
+ GitObject commit = repo.Lookup(identifier, GitObjectType.Any, options);
return commit != null ? commit.Id : null;
}
+ private bool AllowOrphanReference(string identifier)
+ {
+ return string.Equals(identifier, "HEAD", StringComparison.Ordinal);
+ }
+
private IEnumerable<ObjectId> RetrieveCommitOids(object identifier)
{
if (identifier is string)
@@ -307,6 +309,12 @@ private IEnumerable<ObjectId> RetrieveCommitOids(object identifier)
if (identifier is Branch)
{
var branch = (Branch)identifier;
+ if (branch.Tip == null && branch.IsCurrentRepositoryHead)
+ {
+ yield return null;
+ yield break;
+ }
+
Ensure.GitObjectIsNotNull(branch.Tip, branch.CanonicalName);
yield return branch.Tip.Id;
Please sign in to comment.
Something went wrong with that request. Please try again.