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