Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions LibGit2Sharp.Tests/RepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -147,15 +153,15 @@ 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);
Assert.True(repo.Info.IsHeadOrphaned);

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);
Expand All @@ -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"]);

Expand Down
31 changes: 20 additions & 11 deletions LibGit2Sharp/CommitLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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<ObjectId> RetrieveCommitOids(object identifier)
{
if (identifier is string)
Expand Down Expand Up @@ -307,6 +310,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;
Expand Down