Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw NotFoundException if trees are missing when computing diff #1936

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,5 +1256,40 @@ public void UsingPatienceAlgorithmCompareOptionProducesPatienceDiff()
Assert.Equal(diffPatience, changes);
}
}

[Fact]
public void DiffThrowsANotFoundExceptionIfATreeIsMissing()
{
string repoPath = SandboxBareTestRepo();

// Manually delete the tree object 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<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345");
Assert.NotNull(commit);
Assert.Equal("581f9824ecaf824221bd36edf5430f2739a7c4f5", commit.Tree.Sha);
Assert.True(commit.Tree.IsMissing);

var tree = repo.Lookup<Tree>("581f9824ecaf824221bd36edf5430f2739a7c4f5");
Assert.Null(tree);

var otherCommit = repo.Lookup<Commit>("be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
Assert.NotNull(otherCommit);
Assert.False(otherCommit.Tree.IsMissing);

Assert.Throws<NotFoundException>(() =>
{
using (repo.Diff.Compare<TreeChanges>(commit.Tree, otherCommit.Tree)) {}
});

Assert.Throws<NotFoundException>(() =>
{
using (repo.Diff.Compare<TreeChanges>(otherCommit.Tree, commit.Tree)) {}
});
}
}
}
}
4 changes: 2 additions & 2 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,8 @@ public static unsafe void git_diff_merge(DiffHandle onto, DiffHandle from)
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);
Expand Down