2
2
using System . Linq ;
3
3
using LibGit2Sharp . Tests . TestHelpers ;
4
4
using Xunit ;
5
+ using Xunit . Extensions ;
5
6
6
7
namespace LibGit2Sharp . Tests
7
8
{
@@ -28,83 +29,86 @@ public class CommitAncestorFixture : BaseFixture
28
29
*
29
30
*/
30
31
31
- [ Fact ]
32
- public void CanFindCommonAncestorForTwoCommits ( )
32
+ [ Theory ]
33
+ [ InlineData ( "5b5b025afb0b4c913b4c338a42934a3863bf3644" , "c47800c" , "9fd738e" ) ]
34
+ [ InlineData ( "9fd738e8f7967c078dceed8190330fc8648ee56a" , "be3563a" , "9fd738e" ) ]
35
+ [ InlineData ( null , "be3563a" , "-" ) ]
36
+ public void FindCommonAncestorForTwoCommits ( string result , string sha1 , string sha2 )
33
37
{
34
38
using ( var repo = new Repository ( BareTestRepoPath ) )
35
39
{
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
- Assert . Equal ( "5b5b025afb0b4c913b4c338a42934a3863bf3644" , ancestor . Id . Sha ) ;
40
+ var first = sha1 == "-" ? CreateOrphanedCommit ( repo ) : repo . Lookup < Commit > ( sha1 ) ;
41
+ var second = sha2 == "-" ? CreateOrphanedCommit ( repo ) : repo . Lookup < Commit > ( sha2 ) ;
42
+
43
+ Commit ancestor = repo . Commits . FindMergeBase ( first , second ) ;
44
+
45
+ if ( result == null )
46
+ {
47
+ Assert . Null ( ancestor ) ;
48
+ }
49
+ else
50
+ {
51
+ Assert . NotNull ( ancestor ) ;
52
+ Assert . Equal ( result , ancestor . Id . Sha ) ;
53
+ }
43
54
}
44
55
}
45
56
46
- [ Fact ]
47
- public void CanFindCommonAncestorForTwoCommitsAsEnumerable ( )
57
+ [ Theory ]
58
+ [ InlineData ( "5b5b025afb0b4c913b4c338a42934a3863bf3644" , new [ ] { "c47800c" , "9fd738e" } , MergeBaseFindingStrategy . Octopus ) ]
59
+ [ InlineData ( "5b5b025afb0b4c913b4c338a42934a3863bf3644" , new [ ] { "c47800c" , "9fd738e" } , MergeBaseFindingStrategy . Standard ) ]
60
+ [ InlineData ( "5b5b025afb0b4c913b4c338a42934a3863bf3644" , new [ ] { "4c062a6" , "be3563a" , "c47800c" , "5b5b025" } , MergeBaseFindingStrategy . Octopus ) ]
61
+ [ InlineData ( "be3563ae3f795b2b4353bcce3a527ad0a4f7f644" , new [ ] { "4c062a6" , "be3563a" , "c47800c" , "5b5b025" } , MergeBaseFindingStrategy . Standard ) ]
62
+ [ InlineData ( null , new [ ] { "4c062a6" , "be3563a" , "-" , "c47800c" , "5b5b025" } , MergeBaseFindingStrategy . Octopus ) ]
63
+ [ InlineData ( "be3563ae3f795b2b4353bcce3a527ad0a4f7f644" , new [ ] { "4c062a6" , "be3563a" , "-" , "c47800c" , "5b5b025" } , MergeBaseFindingStrategy . Standard ) ]
64
+ [ InlineData ( null , new [ ] { "4c062a6" , "-" } , MergeBaseFindingStrategy . Octopus ) ]
65
+ [ InlineData ( null , new [ ] { "4c062a6" , "-" } , MergeBaseFindingStrategy . Standard ) ]
66
+ public void FindCommonAncestorForCommitsAsEnumerable ( string result , string [ ] shas , MergeBaseFindingStrategy strategy )
48
67
{
49
68
using ( var repo = new Repository ( BareTestRepoPath ) )
50
69
{
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
- Assert . Equal ( "5b5b025afb0b4c913b4c338a42934a3863bf3644" , ancestor . Id . Sha ) ;
70
+ var commits = shas . Select ( sha => sha == "-" ? CreateOrphanedCommit ( repo ) : repo . Lookup < Commit > ( sha ) ) . ToArray ( ) ;
71
+
72
+ Commit ancestor = repo . Commits . FindMergeBase ( commits , strategy ) ;
73
+
74
+ if ( result == null )
75
+ {
76
+ Assert . Null ( ancestor ) ;
77
+ }
78
+ else
79
+ {
80
+ Assert . NotNull ( ancestor ) ;
81
+ Assert . Equal ( result , ancestor . Id . Sha ) ;
82
+ }
58
83
}
59
84
}
60
85
61
- [ Fact ]
62
- public void CanFindCommonAncestorForSeveralCommits ( )
86
+ [ Theory ]
87
+ [ InlineData ( "4c062a6" , "0000000" ) ]
88
+ [ InlineData ( "0000000" , "4c062a6" ) ]
89
+ public void FindCommonAncestorForTwoCommitsThrows ( string sha1 , string sha2 )
63
90
{
64
91
using ( var repo = new Repository ( BareTestRepoPath ) )
65
92
{
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
- Assert . Equal ( "5b5b025afb0b4c913b4c338a42934a3863bf3644" , ancestor . Id . Sha ) ;
75
- }
76
- }
77
-
78
- [ Fact ]
79
- public void CannotFindAncestorForTwoCommmitsWithoutCommonAncestor ( )
80
- {
81
- string path = CloneBareTestRepo ( ) ;
82
- using ( var repo = new Repository ( path ) )
83
- {
84
- var first = repo . Lookup < Commit > ( "4c062a6361ae6959e06292c1fa5e2822d9c96345" ) ;
85
- var second = repo . Lookup < Commit > ( "be3563ae3f795b2b4353bcce3a527ad0a4f7f644" ) ;
86
- var third = repo . Lookup < Commit > ( "c47800c7266a2be04c571c04d5a6614691ea99bd" ) ;
87
- var fourth = repo . Lookup < Commit > ( "5b5b025afb0b4c913b4c338a42934a3863bf3644" ) ;
88
-
89
- Commit orphanedCommit = CreateOrphanedCommit ( repo ) ;
93
+ var first = repo . Lookup < Commit > ( sha1 ) ;
94
+ var second = repo . Lookup < Commit > ( sha2 ) ;
90
95
91
- Commit ancestor = repo . Commits . FindCommonAncestor ( new [ ] { first , second , orphanedCommit , third , fourth } ) ;
92
- Assert . Null ( ancestor ) ;
96
+ Assert . Throws < ArgumentNullException > ( ( ) => repo . Commits . FindMergeBase ( first , second ) ) ;
93
97
}
94
98
}
95
99
96
- [ Fact ]
97
- public void CannotFindCommonAncestorForSeveralCommmitsWithoutCommonAncestor ( )
100
+ [ Theory ]
101
+ [ InlineData ( new [ ] { "4c062a6" } , MergeBaseFindingStrategy . Octopus ) ]
102
+ [ InlineData ( new [ ] { "4c062a6" } , MergeBaseFindingStrategy . Standard ) ]
103
+ [ InlineData ( new [ ] { "4c062a6" , "be3563a" , "000000" } , MergeBaseFindingStrategy . Octopus ) ]
104
+ [ InlineData ( new [ ] { "4c062a6" , "be3563a" , "000000" } , MergeBaseFindingStrategy . Standard ) ]
105
+ public void FindCommonAncestorForCommitsAsEnumerableThrows ( string [ ] shas , MergeBaseFindingStrategy strategy )
98
106
{
99
- string path = CloneBareTestRepo ( ) ;
100
- using ( var repo = new Repository ( path ) )
107
+ using ( var repo = new Repository ( BareTestRepoPath ) )
101
108
{
102
- var first = repo . Lookup < Commit > ( "4c062a6361ae6959e06292c1fa5e2822d9c96345" ) ;
103
-
104
- var orphanedCommit = CreateOrphanedCommit ( repo ) ;
109
+ var commits = shas . Select ( sha => sha == "-" ? CreateOrphanedCommit ( repo ) : repo . Lookup < Commit > ( sha ) ) . ToArray ( ) ;
105
110
106
- Commit ancestor = repo . Commits . FindCommonAncestor ( first , orphanedCommit ) ;
107
- Assert . Null ( ancestor ) ;
111
+ Assert . Throws < ArgumentException > ( ( ) => repo . Commits . FindMergeBase ( commits , strategy ) ) ;
108
112
}
109
113
}
110
114
@@ -122,40 +126,5 @@ private static Commit CreateOrphanedCommit(IRepository repo)
122
126
123
127
return orphanedCommit ;
124
128
}
125
-
126
- [ Fact ]
127
- public void FindCommonAncestorForSingleCommitThrows ( )
128
- {
129
- using ( var repo = new Repository ( BareTestRepoPath ) )
130
- {
131
- var first = repo . Lookup < Commit > ( "4c062a6361ae6959e06292c1fa5e2822d9c96345" ) ;
132
-
133
- Assert . Throws < ArgumentException > ( ( ) => repo . Commits . FindCommonAncestor ( new [ ] { first } ) ) ;
134
- }
135
- }
136
-
137
- [ Fact ]
138
- public void FindCommonAncestorForEnumerableWithNullCommitThrows ( )
139
- {
140
- using ( var repo = new Repository ( BareTestRepoPath ) )
141
- {
142
- var first = repo . Lookup < Commit > ( "4c062a6361ae6959e06292c1fa5e2822d9c96345" ) ;
143
- var second = repo . Lookup < Commit > ( "be3563ae3f795b2b4353bcce3a527ad0a4f7f644" ) ;
144
-
145
- Assert . Throws < ArgumentException > ( ( ) => repo . Commits . FindCommonAncestor ( new [ ] { first , second , null } ) ) ;
146
- }
147
- }
148
-
149
- [ Fact ]
150
- public void FindCommonAncestorForWithNullCommitThrows ( )
151
- {
152
- using ( var repo = new Repository ( BareTestRepoPath ) )
153
- {
154
- var first = repo . Lookup < Commit > ( "4c062a6361ae6959e06292c1fa5e2822d9c96345" ) ;
155
-
156
- Assert . Throws < ArgumentNullException > ( ( ) => repo . Commits . FindCommonAncestor ( first , null ) ) ;
157
- Assert . Throws < ArgumentNullException > ( ( ) => repo . Commits . FindCommonAncestor ( null , first ) ) ;
158
- }
159
- }
160
129
}
161
130
}
0 commit comments