diff --git a/LibGit2Sharp.Tests/RepositoryFixture.cs b/LibGit2Sharp.Tests/RepositoryFixture.cs index 9f7536025..22b93558d 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.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 9abfc6988..149b4b278 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,25 @@ 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) + || string.Equals(identifier, repo.Head.CanonicalName, StringComparison.Ordinal); + } + private IEnumerable RetrieveCommitOids(object identifier) { if (identifier is string) @@ -307,6 +310,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;