diff --git a/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs b/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs index e4c8b0b7f..cb1ae6262 100644 --- a/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs +++ b/src/NerdBank.GitVersioning/ManagedGit/GitRepository.cs @@ -779,7 +779,8 @@ private bool TryGetObjectByPath(GitObjectId sha, string objectType, [NotNullWhen if (string.CompareOrdinal(objectStream.ObjectType, objectType) != 0) { - throw new GitException($"Got a {objectStream.ObjectType} instead of a {objectType} when opening object {sha}"); + value = null; + return false; } value = objectStream; diff --git a/test/Nerdbank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs b/test/Nerdbank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs index 582dfbdca..ad898ce97 100644 --- a/test/Nerdbank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs +++ b/test/Nerdbank.GitVersioning.Tests/ManagedGit/GitRepositoryTests.cs @@ -271,6 +271,21 @@ public void GetObjectByShaAndWrongTypeTest() } } + [Fact] + public void TryGetObjectByShaAndWrongTypeTest() + { + this.InitializeSourceControl(); + this.AddCommits(2); + + var headObjectId = GitObjectId.Parse(this.LibGit2Repository.Head.Tip.Sha); + + using (var repository = GitRepository.Create(this.RepoPath)) + { + Assert.False(repository.TryGetObjectBySha(headObjectId, "tree", out Stream value)); + Assert.Null(value); + } + } + [Fact] public void GetMissingObjectByShaTest() { diff --git a/test/Nerdbank.GitVersioning.Tests/VersionOracleTests.cs b/test/Nerdbank.GitVersioning.Tests/VersionOracleTests.cs index 946fc5227..7cb7cad6a 100644 --- a/test/Nerdbank.GitVersioning.Tests/VersionOracleTests.cs +++ b/test/Nerdbank.GitVersioning.Tests/VersionOracleTests.cs @@ -1048,7 +1048,7 @@ public void Tags() // Assert that we don't see any tags. Assert.Empty(oracle.Tags); - // Create a tag. + // Create a lightweight tag. this.LibGit2Repository.ApplyTag("mytag"); // Refresh our context before asking again. @@ -1057,6 +1057,16 @@ public void Tags() // Assert that we see the tag. Assert.Equal("refs/tags/mytag", Assert.Single(oracle2.Tags)); + + // Add another commit + this.AddCommits(1); + + // Refresh our context before asking again. + this.Context = this.CreateGitContext(this.RepoPath); + VersionOracle oracle3 = new(this.Context); + + // Assert that HEAD is not pointing to the tag. + Assert.Empty(oracle3.Tags); } [Fact] @@ -1070,7 +1080,7 @@ public void Tags_Annotated() // Assert that we don't see any tags. Assert.Empty(oracle.Tags); - // Create a tag. + // Create an annotated tag. this.LibGit2Repository.ApplyTag("mytag", this.Signer, "my tag"); // Refresh our context before asking again. @@ -1079,6 +1089,16 @@ public void Tags_Annotated() // Assert that we see the tag. Assert.Equal("refs/tags/mytag", Assert.Single(oracle2.Tags)); + + // Add another commit + this.AddCommits(1); + + // Refresh our context before asking again. + this.Context = this.CreateGitContext(this.RepoPath); + VersionOracle oracle3 = new(this.Context); + + // Assert that HEAD is not pointing to the tag. + Assert.Empty(oracle3.Tags); } [Fact]