|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using LibGit2Sharp.Tests.TestHelpers; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace LibGit2Sharp.Tests |
| 7 | +{ |
| 8 | + public class CommitAncestorFixture : BaseFixture |
| 9 | + { |
| 10 | + /* |
| 11 | + * BareTestRepoPath structure |
| 12 | + * |
| 13 | + * * commit 4c062a6361ae6959e06292c1fa5e2822d9c96345 |
| 14 | + * | |
| 15 | + * * commit be3563ae3f795b2b4353bcce3a527ad0a4f7f644 |
| 16 | + * |\ |
| 17 | + * | | |
| 18 | + * | * commit c47800c7266a2be04c571c04d5a6614691ea99bd |
| 19 | + * | | |
| 20 | + * * | commit 9fd738e8f7967c078dceed8190330fc8648ee56a |
| 21 | + * | | |
| 22 | + * * | commit 4a202b346bb0fb0db7eff3cffeb3c70babbd2045 |
| 23 | + * |/ |
| 24 | + * | |
| 25 | + * * commit 5b5b025afb0b4c913b4c338a42934a3863bf3644 |
| 26 | + * | |
| 27 | + * * commit 8496071c1b46c854b31185ea97743be6a877447 |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void CanFindCommonAncestorForTwoCommits() |
| 33 | + { |
| 34 | + using (var repo = new Repository(BareTestRepoPath)) |
| 35 | + { |
| 36 | + var first = repo.Lookup<Commit>("c47800c7266a2be04c571c04d5a6614691ea99bd"); |
| 37 | + var second = repo.Lookup<Commit>("9fd738e8f7967c078dceed8190330fc8648ee56a"); |
| 38 | + |
| 39 | + Commit ancestor = repo.Commits.FindCommonAncestor(first, second); |
| 40 | + |
| 41 | + Assert.NotNull(ancestor); |
| 42 | + ancestor.Id.Sha.ShouldEqual("5b5b025afb0b4c913b4c338a42934a3863bf3644"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void CanFindCommonAncestorForTwoCommitsAsEnumerable() |
| 48 | + { |
| 49 | + using (var repo = new Repository(BareTestRepoPath)) |
| 50 | + { |
| 51 | + var first = repo.Lookup<Commit>("c47800c7266a2be04c571c04d5a6614691ea99bd"); |
| 52 | + var second = repo.Lookup<Commit>("9fd738e8f7967c078dceed8190330fc8648ee56a"); |
| 53 | + |
| 54 | + Commit ancestor = repo.Commits.FindCommonAncestor(new[] { first, second }); |
| 55 | + |
| 56 | + Assert.NotNull(ancestor); |
| 57 | + ancestor.Id.Sha.ShouldEqual("5b5b025afb0b4c913b4c338a42934a3863bf3644"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void CanFindCommonAncestorForSeveralCommits() |
| 63 | + { |
| 64 | + using (var repo = new Repository(BareTestRepoPath)) |
| 65 | + { |
| 66 | + var first = repo.Lookup<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345"); |
| 67 | + var second = repo.Lookup<Commit>("be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); |
| 68 | + var third = repo.Lookup<Commit>("c47800c7266a2be04c571c04d5a6614691ea99bd"); |
| 69 | + var fourth = repo.Lookup<Commit>("5b5b025afb0b4c913b4c338a42934a3863bf3644"); |
| 70 | + |
| 71 | + Commit ancestor = repo.Commits.FindCommonAncestor(new[] { first, second, third, fourth }); |
| 72 | + |
| 73 | + Assert.NotNull(ancestor); |
| 74 | + ancestor.Id.Sha.ShouldEqual("5b5b025afb0b4c913b4c338a42934a3863bf3644"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void CannotFindAncestorForTwoCommmitsWithoutCommonAncestor() |
| 80 | + { |
| 81 | + var scd = BuildTemporaryCloneOfTestRepo(); |
| 82 | + |
| 83 | + using (var repo = new Repository(scd.RepositoryPath)) |
| 84 | + { |
| 85 | + var first = repo.Lookup<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345"); |
| 86 | + var second = repo.Lookup<Commit>("be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); |
| 87 | + var third = repo.Lookup<Commit>("c47800c7266a2be04c571c04d5a6614691ea99bd"); |
| 88 | + var fourth = repo.Lookup<Commit>("5b5b025afb0b4c913b4c338a42934a3863bf3644"); |
| 89 | + |
| 90 | + Commit orphanedCommit = CreateOrphanedCommit(repo); |
| 91 | + |
| 92 | + Commit ancestor = repo.Commits.FindCommonAncestor(new[] { first, second, orphanedCommit, third, fourth }); |
| 93 | + Assert.Null(ancestor); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void CannotFindCommonAncestorForSeveralCommmitsWithoutCommonAncestor() |
| 99 | + { |
| 100 | + var scd = BuildTemporaryCloneOfTestRepo(); |
| 101 | + |
| 102 | + using (var repo = new Repository(scd.RepositoryPath)) |
| 103 | + { |
| 104 | + var first = repo.Lookup<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345"); |
| 105 | + |
| 106 | + var orphanedCommit = CreateOrphanedCommit(repo); |
| 107 | + |
| 108 | + Commit ancestor = repo.Commits.FindCommonAncestor(first, orphanedCommit); |
| 109 | + Assert.Null(ancestor); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static Commit CreateOrphanedCommit(Repository repo) |
| 114 | + { |
| 115 | + Commit random = repo.Head.Tip; |
| 116 | + |
| 117 | + Commit orphanedCommit = repo.ObjectDatabase.CreateCommit( |
| 118 | + "This is a test commit created by 'CommitFixture.CannotFindCommonAncestorForCommmitsWithoutCommonAncestor'", |
| 119 | + random.Author, |
| 120 | + random.Committer, |
| 121 | + random.Tree, |
| 122 | + Enumerable.Empty<Commit>()); |
| 123 | + |
| 124 | + return orphanedCommit; |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + public void FindCommonAncestorForSingleCommitThrows() |
| 129 | + { |
| 130 | + using (var repo = new Repository(BareTestRepoPath)) |
| 131 | + { |
| 132 | + var first = repo.Lookup<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345"); |
| 133 | + |
| 134 | + Assert.Throws<ArgumentException>(() => repo.Commits.FindCommonAncestor(new[] { first })); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void FindCommonAncestorForEnumerableWithNullCommitThrows() |
| 140 | + { |
| 141 | + using (var repo = new Repository(BareTestRepoPath)) |
| 142 | + { |
| 143 | + var first = repo.Lookup<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345"); |
| 144 | + var second = repo.Lookup<Commit>("be3563ae3f795b2b4353bcce3a527ad0a4f7f644"); |
| 145 | + |
| 146 | + Assert.Throws<ArgumentException>(() => repo.Commits.FindCommonAncestor(new[] { first, second, null })); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + [Fact] |
| 151 | + public void FindCommonAncestorForWithNullCommitThrows() |
| 152 | + { |
| 153 | + using (var repo = new Repository(BareTestRepoPath)) |
| 154 | + { |
| 155 | + var first = repo.Lookup<Commit>("4c062a6361ae6959e06292c1fa5e2822d9c96345"); |
| 156 | + |
| 157 | + Assert.Throws<ArgumentNullException>(() => repo.Commits.FindCommonAncestor(first, null)); |
| 158 | + Assert.Throws<ArgumentNullException>(() => repo.Commits.FindCommonAncestor(null, first)); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments