From 1502b9332525a8047f1340c67cd0e85e1485083c Mon Sep 17 00:00:00 2001 From: Julien Richard Date: Mon, 3 Jan 2022 16:15:59 +0100 Subject: [PATCH 1/3] Throw NotFoundException if trees are missing when computing diff --- LibGit2Sharp/Core/Proxy.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 6c6d04670..8991430bd 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -797,8 +797,8 @@ public static unsafe DiffHandle git_diff_tree_to_tree( ObjectId newTree, GitDiffOptions options) { - using (var osw1 = new ObjectSafeWrapper(oldTree, repo, true)) - using (var osw2 = new ObjectSafeWrapper(newTree, repo, true)) + using (var osw1 = new ObjectSafeWrapper(oldTree, repo, true, throwIfMissing: true)) + using (var osw2 = new ObjectSafeWrapper(newTree, repo, true, throwIfMissing: true)) { git_diff* diff; int res = NativeMethods.git_diff_tree_to_tree(out diff, repo, osw1.ObjectPtr, osw2.ObjectPtr, options); From 93d8e453c7f8698cdbdd126da981e64c51689c96 Mon Sep 17 00:00:00 2001 From: Julien Richard Date: Mon, 3 Jan 2022 18:10:54 +0100 Subject: [PATCH 2/3] Add test --- LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index d03b26e4e..e2760757b 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -1256,5 +1256,40 @@ public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff() Assert.Equal(diffPatience, changes); } } + + [Fact] + public void DiffThrowsANotFoundExceptionIfATreeIsMissing() + { + string repoPath = SandboxBareTestRepo(); + + // Manually delete the objects directory to simulate a partial clone + File.Delete(Path.Combine(repoPath, "objects", "58", "1f9824ecaf824221bd36edf5430f2739a7c4f5")); + + using (var repo = new Repository(repoPath)) + { + // The commit is there but its tree is missing + var commit = repo.Lookup("4c062a6361ae6959e06292c1fa5e2822d9c96345"); + Assert.NotNull(commit); + Assert.Equal("581f9824ecaf824221bd36edf5430f2739a7c4f5", commit.Tree.Sha); + Assert.True(commit.Tree.IsMissing); + + var tree = repo.Lookup("581f9824ecaf824221bd36edf5430f2739a7c4f5"); + Assert.Null(tree); + + var otherCommit = repo.Lookup("be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); + Assert.NotNull(otherCommit); + Assert.False(otherCommit.Tree.IsMissing); + + Assert.Throws(() => + { + using (repo.Diff.Compare(commit.Tree, otherCommit.Tree)) {} + }); + + Assert.Throws(() => + { + using (repo.Diff.Compare(otherCommit.Tree, commit.Tree)) {} + }); + } + } } } From 9780ddf9450ef6c740bce21a20d8e0765116fcbe Mon Sep 17 00:00:00 2001 From: Julien Richard Date: Mon, 3 Jan 2022 19:40:27 +0100 Subject: [PATCH 3/3] Fix comment --- LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index e2760757b..0fe7adee1 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -1262,7 +1262,7 @@ public void DiffThrowsANotFoundExceptionIfATreeIsMissing() { string repoPath = SandboxBareTestRepo(); - // Manually delete the objects directory to simulate a partial clone + // Manually delete the tree object to simulate a partial clone File.Delete(Path.Combine(repoPath, "objects", "58", "1f9824ecaf824221bd36edf5430f2739a7c4f5")); using (var repo = new Repository(repoPath))