8000 Remove orphan hack for Commits.QueryBy() · gitextensions/libgit2sharp@d31afcd · GitHub
[go: up one dir, main page]

Skip to content

Commit d31afcd

Browse files
committed
Remove orphan hack for Commits.QueryBy()
1 parent 7745cc2 commit d31afcd

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public void CanCreateBareRepo()
2626
Assert.Equal(scd.RootedDirectoryPath + Path.DirectorySeparatorChar, repo.Info.Path);
2727
Assert.True(repo.Info.IsBare);
2828

29-
AssertInitializedRepository(repo);
29+
AssertInitializedRepository(repo, "refs/heads/master");
30+
31+
repo.Refs.Add("HEAD", "refs/heads/orphan", true);
32+
AssertInitializedRepository(repo, "refs/heads/orphan");
3033
}
3134
}
3235

@@ -57,7 +60,10 @@ public void CanCreateStandardRepo()
5760

5861
AssertIsHidden(repo.Info.Path);
5962

60-
AssertInitializedRepository(repo);
63+
AssertInitializedRepository(repo, "refs/heads/master");
64+
65+
repo.Refs.Add("HEAD", "refs/heads/orphan", true);
66+
AssertInitializedRepository(repo, "refs/heads/orphan");
6167
}
6268
}
6369

@@ -147,15 +153,15 @@ public void CreatingRepoWithBadParamsThrows()
147153
Assert.Throws<ArgumentNullException>(() => Repository.Init(null));
148154
}
149155

150-
private static void AssertInitializedRepository(Repository repo)
156+
private static void AssertInitializedRepository(Repository repo, string expectedHeadTargetIdentifier)
151157
{
152158
Assert.NotNull(repo.Info.Path);
153159
Assert.False(repo.Info.IsHeadDetached);
154160
Assert.True(repo.Info.IsHeadOrphaned);
155161

156162
Reference headRef = repo.Refs.Head;
157163
Assert.NotNull(headRef);
158-
Assert.Equal("refs/heads/master", headRef.TargetIdentifier);
164+
Assert.Equal(expectedHeadTargetIdentifier, headRef.TargetIdentifier);
159165
Assert.Null(headRef.ResolveToDirectReference());
160166

161167
Assert.NotNull(repo.Head);
@@ -164,9 +170,11 @@ private static void AssertInitializedRepository(Repository repo)
164170
Assert.Null(repo.Head.Tip);
165171

166172
Assert.Equal(0, repo.Commits.Count());
173+
Assert.Equal(0, repo.Commits.QueryBy(new Filter()).Count());
174+
Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = repo.Refs.Head }).Count());
167175
Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = repo.Head }).Count());
168176
Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = "HEAD" }).Count());
169-
Assert.Equal(0, repo.Commits.QueryBy(new Filter { Since = "refs/heads/master" }).Count());
177+
Assert.Throws<LibGit2SharpException>(() => repo.Commits.QueryBy(new Filter { Since = expectedHeadTargetIdentifier }).Count());
170178

171179
Assert.Null(repo.Head["subdir/I-do-not-exist"]);
172180

LibGit2Sharp/CommitLog.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ public virtual GitSortOptions SortedBy
5959
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the log.</returns>
6060
public virtual IEnumerator<Commit> GetEnumerator()
6161
{
62-
if ((repo.Info.IsEmpty) && queryFilter.SinceList.Any(o => PointsAtTheHead(o.ToString()))) // TODO: ToString() == fragile
63-
{
64-
return Enumerable.Empty<Commit>().GetEnumerator();
65-
}
66-
6762
return new CommitEnumerator(repo, queryFilter);
6863
}
6964

@@ -92,11 +87,6 @@ public virtual ICommitLog QueryBy(Filter filter)
9287
return new CommitLog(repo, filter);
9388
}
9489

95-
private static bool PointsAtTheHead(string shaOrRefName)
96-
{
97-
return ("HEAD".Equals(shaOrRefName, StringComparison.Ordinal) || "refs/heads/master".Equals(shaOrRefName, StringComparison.Ordinal));
98-
}
99-
10090
/// <summary>
10191
/// Find the best possible common ancestor given two <see cref = "Commit"/>s.
10292
/// </summary>
@@ -266,12 +256,24 @@ private void Sort(GitSortOptions options)
266256

267257
private ObjectId DereferenceToCommit(string identifier)
268258
{
259+
var options = LookUpOptions.DereferenceResultToCommit;
260+
261+
if (!AllowOrphanReference(identifier))
262+
{
263+
options |= LookUpOptions.ThrowWhenNoGitObjectHasBeenFound;
264+
}
265+
269266
// TODO: Should we check the type? Git-log allows TagAnnotation oid as parameter. But what about Blobs and Trees?
270-
GitObject commit = repo.Lookup(identifier, GitObjectType.Any, LookUpOptions.ThrowWhenNoGitObjectHasBeenFound | LookUpOptions.DereferenceResultToCommit);
267+
GitObject commit = repo.Lookup(identifier, GitObjectType.Any, options);
271268

272269
return commit != null ? commit.Id : null;
273270
}
274271

272+
private bool AllowOrphanReference(string identifier)
273+
{
274+
return string.Equals(identifier, "HEAD", StringComparison.Ordinal);
275+
}
276+
275277
private IEnumerable<ObjectId> RetrieveCommitOids(object identifier)
276278
{
277279
if (identifier is string)
@@ -307,6 +309,12 @@ private IEnumerable<ObjectId> RetrieveCommitOids(object identifier)
307309
if (identifier is Branch)
308310
{
309311
var branch = (Branch)identifier;
312+
if (branch.Tip == null && branch.IsCurrentRepositoryHead)
313+
{
314+
yield return null;
315+
yield break;
316+
}
317+
310318
Ensure.GitObjectIsNotNull(branch.Tip, branch.CanonicalName);
311319

312320
yield return branch.Tip.Id;

0 commit comments

Comments
 (0)
0