From d31afcd16ff419963d68b76aa85898c4e9b03909 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sat, 16 Mar 2013 13:24:39 -0500 Subject: [PATCH 1/2] Remove orphan hack for Commits.QueryBy() --- LibGit2Sharp.Tests/RepositoryFixture.cs | 18 ++++++++++----- LibGit2Sharp/CommitLog.cs | 30 ++++++++++++++++--------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/LibGit2Sharp.Tests/RepositoryFixture.cs b/LibGit2Sharp.Tests/RepositoryFixture.cs index 9f7536025..11115b507 100644 --- a/LibGit2Sharp.Tests/RepositoryFixture.cs +++ b/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(() => 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(() => repo.Commits.QueryBy(new Filter { Since = expectedHeadTargetIdentifier }).Count()); Assert.Null(repo.Head["subdir/I-do-not-exist"]); diff --git a/LibGit2Sharp/CommitLog.cs b/LibGit2Sharp/CommitLog.cs index 9abfc6988..5655cccb3 100644 --- a/LibGit2Sharp/CommitLog.cs +++ b/LibGit2Sharp/CommitLog.cs @@ -59,11 +59,6 @@ public virtual GitSortOptions SortedBy /// An object that can be used to iterate through the log. public virtual IEnumerator GetEnumerator() { - if ((repo.Info.IsEmpty) && queryFilter.SinceList.Any(o => PointsAtTheHead(o.ToString()))) // TODO: ToString() == fragile - { - return Enumerable.Empty().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)); - } - /// /// Find the best possible common ancestor given two s. /// @@ -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 RetrieveCommitOids(object identifier) { if (identifier is string) @@ -307,6 +309,12 @@ private IEnumerable 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; From e6dbc3a24761ca4660b9759e3d9184d9292ec784 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sat, 16 Mar 2013 17:05:09 -0500 Subject: [PATCH 2/2] Restore support to filter since current orphan branch --- LibGit2Sharp.Tests/RepositoryFixture.cs | 2 +- LibGit2Sharp/CommitLog.cs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/LibGit2Sharp.Tests/RepositoryFixture.cs b/LibGit2Sharp.Tests/RepositoryFixture.cs index 11115b507..22b93558d 100644 --- a/LibGit2Sharp.Tests/RepositoryFixture.cs +++ b/LibGit2Sharp.Tests/RepositoryFixture.cs @@ -174,7 +174,7 @@ private static void AssertInitializedRepository(Repository repo, string expected 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.Throws(() => repo.Commits.QueryBy(new Filter { Since = expectedHeadTargetIdentifier }).Count()); + Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = expectedHeadTargetIdentifier }).Count()); Assert.Null(repo.Head["subdir/I-do-not-exist"]); diff --git a/LibGit2Sharp/CommitLog.cs b/LibGit2Sharp/CommitLog.cs index 5655cccb3..149b4b278 100644 --- a/LibGit2Sharp/CommitLog.cs +++ b/LibGit2Sharp/CommitLog.cs @@ -271,7 +271,8 @@ private ObjectId DereferenceToCommit(string identifier) private bool AllowOrphanReference(string identifier) { - return string.Equals(identifier, "HEAD", StringComparison.Ordinal); + return string.Equals(identifier, "HEAD", StringComparison.Ordinal) + || string.Equals(identifier, repo.Head.CanonicalName, StringComparison.Ordinal); } private IEnumerable RetrieveCommitOids(object identifier)