| 1 |
package object |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sort" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v5/plumbing" |
| 8 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 9 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 10 |
|
| 11 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 12 |
. "gopkg.in/check.v1" |
| 13 |
) |
| 14 |
|
| 15 |
func alphabeticSortCommits(commits []*Commit) { |
| 16 |
sort.Slice(commits, func(i, j int) bool { |
| 17 |
return commits[i].Hash.String() > commits[j].Hash.String() |
| 18 |
}) |
| 19 |
} |
| 20 |
|
| 21 |
/* |
| 22 |
|
| 23 |
The following tests consider this history having two root commits: V and W |
| 24 |
|
| 25 |
V---o---M----AB----A---CD1--P---C--------S-------------------Q < master |
| 26 |
\ \ / / / |
| 27 |
\ X GQ1---G < feature / |
| 28 |
\ / \ / / / |
| 29 |
W---o---N----o----B---CD2---o---D----o----GQ2------------o < dev |
| 30 |
|
| 31 |
MergeBase |
| 32 |
---------------------------- |
| 33 |
passed merge-base |
| 34 |
M, N Commits with unrelated history, have no merge-base |
| 35 |
A, B AB Regular merge-base between two commits |
| 36 |
A, A A The merge-commit between equal commits, is the same |
| 37 |
Q, N N The merge-commit between a commit an its ancestor, is the ancestor |
| 38 |
C, D CD1, CD2 Cross merges causes more than one merge-base |
| 39 |
G, Q GQ1, GQ2 Feature branches including merges, causes more than one merge-base |
| 40 |
|
| 41 |
Independents |
| 42 |
---------------------------- |
| 43 |
candidates result |
| 44 |
A A Only one commit returns it |
| 45 |
A, A, A A Repeated commits are ignored |
| 46 |
A, A, M, M, N A, N M is reachable from A, so it is not independent |
| 47 |
S, G, P S, G P is reachable from S, so it is not independent |
| 48 |
CD1, CD2, M, N CD1, CD2 M and N are reachable from CD2, so they're not |
| 49 |
C, G, dev, M, N C, G, dev M and N are reachable from G, so they're not |
| 50 |
C, D, M, N C, D M and N are reachable from C, so they're not |
| 51 |
A, A^, A, N, N^ A, N A^ and N^ are reachable from A and N |
| 52 |
A^^^, A^, A^^, A, N A, N A^^^, A^^ and A^ are reachable from A, so they're not |
| 53 |
|
| 54 |
IsAncestor |
| 55 |
---------------------------- |
| 56 |
passed result |
| 57 |
A^^, A true Will be true if first is ancestor of the second |
| 58 |
M, G true True because it will also reach G from M crossing merge commits |
| 59 |
A, A true True if first and second are the same |
| 60 |
M, N false Commits with unrelated history, will return false |
| 61 |
*/ |
| 62 |
|
| 63 |
var _ = Suite(&mergeBaseSuite{}) |
| 64 |
|
| 65 |
type mergeBaseSuite struct { |
| 66 |
BaseObjectsSuite |
| 67 |
} |
| 68 |
|
| 69 |
func (s *mergeBaseSuite) SetUpSuite(c *C) { |
| 70 |
s.Fixture = fixtures.ByTag("merge-base").One() |
| 71 |
s.Storer = filesystem.NewStorage(s.Fixture.DotGit(), cache.NewObjectLRUDefault()) |
| 72 |
} |
| 73 |
|
| 74 |
var revisionIndex = map[string]plumbing.Hash{ |
| 75 |
"master": plumbing.NewHash("dce0e0c20d701c3d260146e443d6b3b079505191"), |
| 76 |
"feature": plumbing.NewHash("d1b0093698e398d596ef94d646c4db37e8d1e970"), |
| 77 |
"dev": plumbing.NewHash("25ca6c810c08482d61113fbcaaada38bb59093a8"), |
| 78 |
"M": plumbing.NewHash("bb355b64e18386dbc3af63dfd09c015c44cbd9b6"), |
| 79 |
"N": plumbing.NewHash("d64b894762ab5f09e2b155221b90c18bd0637236"), |
| 80 |
"A": plumbing.NewHash("29740cfaf0c2ee4bb532dba9e80040ca738f367c"), |
| 81 |
"B": plumbing.NewHash("2c84807970299ba98951c65fe81ebbaac01030f0"), |
| 82 |
"AB": plumbing.NewHash("31a7e081a28f149ee98ffd13ba1a6d841a5f46fd"), |
| 83 |
"P": plumbing.NewHash("ff84393134864cf9d3a9853a81bde81778bd5805"), |
| 84 |
"C": plumbing.NewHash("8b72fabdc4222c3ff965bc310ded788c601c50ed"), |
| 85 |
"D": plumbing.NewHash("14777cf3e209334592fbfd0b878f6868394db836"), |
| 86 |
"CD1": plumbing.NewHash("4709e13a3cbb300c2b8a917effda776e1b8955c7"), |
| 87 |
"CD2": plumbing.NewHash("38468e274e91e50ffb637b88a1954ab6193fe974"), |
| 88 |
"S": plumbing.NewHash("628f1a42b70380ed05734bf01b468b46206ef1ea"), |
| 89 |
"G": plumbing.NewHash("d1b0093698e398d596ef94d646c4db37e8d1e970"), |
| 90 |
"Q": plumbing.NewHash("dce0e0c20d701c3d260146e443d6b3b079505191"), |
| 91 |
"GQ1": plumbing.NewHash("ccaaa99c21dad7e9f392c36ae8cb72dc63bed458"), |
| 92 |
"GQ2": plumbing.NewHash("806824d4778e94fe7c3244e92a9cd07090c9ab54"), |
| 93 |
"A^": plumbing.NewHash("31a7e081a28f149ee98ffd13ba1a6d841a5f46fd"), |
| 94 |
"A^^": plumbing.NewHash("bb355b64e18386dbc3af63dfd09c015c44cbd9b6"), |
| 95 |
"A^^^": plumbing.NewHash("8d08dd1388b82dd354cb43918d83da86c76b0978"), |
| 96 |
"N^": plumbing.NewHash("b6e1fc8dad4f1068fb42774ec5fc65c065b2c312"), |
| 97 |
} |
| 98 |
|
| 99 |
func (s *mergeBaseSuite) commitsFromRevs(c *C, revs []string) ([]*Commit, error) { |
| 100 |
var commits []*Commit |
| 101 |
for _, rev := range revs { |
| 102 |
hash, ok := revisionIndex[rev] |
| 103 |
if !ok { |
| 104 |
return nil, fmt.Errorf("Revision not found '%s'", rev) |
| 105 |
} |
| 106 |
|
| 107 |
commits = append(commits, s.commit(c, hash)) |
| 108 |
} |
| 109 |
|
| 110 |
return commits, nil |
| 111 |
} |
| 112 |
|
| 113 |
// AssertMergeBase validates that the merge-base of the passed revs, |
| 114 |
// matches the expected result |
| 115 |
func (s *mergeBaseSuite) AssertMergeBase(c *C, revs, expectedRevs []string) { |
| 116 |
c.Assert(revs, HasLen, 2) |
| 117 |
|
| 118 |
commits, err := s.commitsFromRevs(c, revs) |
| 119 |
c.Assert(err, IsNil) |
| 120 |
|
| 121 |
results, err := commits[0].MergeBase(commits[1]) |
| 122 |
c.Assert(err, IsNil) |
| 123 |
|
| 124 |
expected, err := s.commitsFromRevs(c, expectedRevs) |
| 125 |
c.Assert(err, IsNil) |
| 126 |
|
| 127 |
c.Assert(results, HasLen, len(expected)) |
| 128 |
|
| 129 |
alphabeticSortCommits(results) |
| 130 |
alphabeticSortCommits(expected) |
| 131 |
for i, commit := range results { |
| 132 |
c.Assert(commit.Hash.String(), Equals, expected[i].Hash.String()) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
// AssertIndependents validates the independent commits of the passed list |
| 137 |
func (s *mergeBaseSuite) AssertIndependents(c *C, revs, expectedRevs []string) { |
| 138 |
commits, err := s.commitsFromRevs(c, revs) |
| 139 |
c.Assert(err, IsNil) |
| 140 |
|
| 141 |
results, err := Independents(commits) |
| 142 |
c.Assert(err, IsNil) |
| 143 |
|
| 144 |
expected, err := s.commitsFromRevs(c, expectedRevs) |
| 145 |
c.Assert(err, IsNil) |
| 146 |
|
| 147 |
c.Assert(results, HasLen, len(expected)) |
| 148 |
|
| 149 |
alphabeticSortCommits(results) |
| 150 |
alphabeticSortCommits(expected) |
| 151 |
for i, commit := range results { |
| 152 |
c.Assert(commit.Hash.String(), Equals, expected[i].Hash.String()) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// AssertAncestor validates if the first rev is ancestor of the second one |
| 157 |
func (s *mergeBaseSuite) AssertAncestor(c *C, revs []string, shouldBeAncestor bool) { |
| 158 |
c.Assert(revs, HasLen, 2) |
| 159 |
|
| 160 |
commits, err := s.commitsFromRevs(c, revs) |
| 161 |
c.Assert(err, IsNil) |
| 162 |
|
| 163 |
isAncestor, err := commits[0].IsAncestor(commits[1]) |
| 164 |
c.Assert(err, IsNil) |
| 165 |
c.Assert(isAncestor, Equals, shouldBeAncestor) |
| 166 |
} |
| 167 |
|
| 168 |
// TestNoAncestorsWhenNoCommonHistory validates that merge-base returns no commits |
| 169 |
// when there is no common history (M, N -> none) |
| 170 |
func (s *mergeBaseSuite) TestNoAncestorsWhenNoCommonHistory(c *C) { |
| 171 |
revs := []string{"M", "N"} |
| 172 |
nothing := []string{} |
| 173 |
s.AssertMergeBase(c, revs, nothing) |
| 174 |
} |
| 175 |
|
| 176 |
// TestCommonAncestorInMergedOrphans validates that merge-base returns a common |
| 177 |
// ancestor in orphan branches when they where merged (A, B -> AB) |
| 178 |
func (s *mergeBaseSuite) TestCommonAncestorInMergedOrphans(c *C) { |
| 179 |
revs := []string{"A", "B"} |
| 180 |
expectedRevs := []string{"AB"} |
| 181 |
s.AssertMergeBase(c, revs, expectedRevs) |
| 182 |
} |
| 183 |
|
| 184 |
// TestMergeBaseWithSelf validates that merge-base between equal commits, returns |
| 185 |
// the same commit (A, A -> A) |
| 186 |
func (s *mergeBaseSuite) TestMergeBaseWithSelf(c *C) { |
| 187 |
revs := []string{"A", "A"} |
| 188 |
expectedRevs := []string{"A"} |
| 189 |
s.AssertMergeBase(c, revs, expectedRevs) |
| 190 |
} |
| 191 |
|
| 192 |
// TestMergeBaseWithAncestor validates that merge-base between a commit an its |
| 193 |
// ancestor returns the ancestor (Q, N -> N) |
| 194 |
func (s *mergeBaseSuite) TestMergeBaseWithAncestor(c *C) { |
| 195 |
revs := []string{"Q", "N"} |
| 196 |
expectedRevs := []string{"N"} |
| 197 |
s.AssertMergeBase(c, revs, expectedRevs) |
| 198 |
} |
| 199 |
|
| 200 |
// TestDoubleCommonAncestorInCrossMerge validates that merge-base returns two |
| 201 |
// common ancestors when there are cross merges (C, D -> CD1, CD2) |
| 202 |
func (s *mergeBaseSuite) TestDoubleCommonAncestorInCrossMerge(c *C) { |
| 203 |
revs := []string{"C", "D"} |
| 204 |
expectedRevs := []string{"CD1", "CD2"} |
| 205 |
s.AssertMergeBase(c, revs, expectedRevs) |
| 206 |
} |
| 207 |
|
| 208 |
// TestDoubleCommonInSubFeatureBranches validates that merge-base returns two |
| 209 |
// common ancestors when two branches where partially merged (G, Q -> GQ1, GQ2) |
| 210 |
func (s *mergeBaseSuite) TestDoubleCommonInSubFeatureBranches(c *C) { |
| 211 |
revs := []string{"G", "Q"} |
| 212 |
expectedRevs := []string{"GQ1", "GQ2"} |
| 213 |
s.AssertMergeBase(c, revs, expectedRevs) |
| 214 |
} |
| 215 |
|
| 216 |
// TestIndependentOnlyOne validates that Independents for one commit returns |
| 217 |
// that same commit (A -> A) |
| 218 |
func (s *mergeBaseSuite) TestIndependentOnlyOne(c *C) { |
| 219 |
revs := []string{"A"} |
| 220 |
expectedRevs := []string{"A"} |
| 221 |
s.AssertIndependents(c, revs, expectedRevs) |
| 222 |
} |
| 223 |
|
| 224 |
// TestIndependentOnlyRepeated validates that Independents for one repeated commit |
| 225 |
// returns that same commit (A, A, A -> A) |
| 226 |
func (s *mergeBaseSuite) TestIndependentOnlyRepeated(c *C) { |
| 227 |
revs := []string{"A", "A", "A"} |
| 228 |
expectedRevs := []string{"A"} |
| 229 |
s.AssertIndependents(c, revs, expectedRevs) |
| 230 |
} |
| 231 |
|
| 232 |
// TestIndependentWithRepeatedAncestors validates that Independents works well |
| 233 |
// when there are repeated ancestors (A, A, M, M, N -> A, N) |
| 234 |
func (s *mergeBaseSuite) TestIndependentWithRepeatedAncestors(c *C) { |
| 235 |
revs := []string{"A", "A", "M", "M", "N"} |
| 236 |
expectedRevs := []string{"A", "N"} |
| 237 |
s.AssertIndependents(c, revs, expectedRevs) |
| 238 |
} |
| 239 |
|
| 240 |
// TestIndependentBeyondShortcut validates that Independents does not stop walking |
| 241 |
// in all paths when one of them is known (S, G, P -> S, G) |
| 242 |
func (s *mergeBaseSuite) TestIndependentBeyondShortcut(c *C) { |
| 243 |
revs := []string{"S", "G", "P"} |
| 244 |
expectedRevs := []string{"S", "G"} |
| 245 |
s.AssertIndependents(c, revs, expectedRevs) |
| 246 |
} |
| 247 |
|
| 248 |
// TestIndependentBeyondShortcutBis validates that Independents does not stop walking |
| 249 |
// in all paths when one of them is known (CD1, CD2, M, N -> CD1, CD2) |
| 250 |
func (s *mergeBaseSuite) TestIndependentBeyondShortcutBis(c *C) { |
| 251 |
revs := []string{"CD1", "CD2", "M", "N"} |
| 252 |
expectedRevs := []string{"CD1", "CD2"} |
| 253 |
s.AssertIndependents(c, revs, expectedRevs) |
| 254 |
} |
| 255 |
|
| 256 |
// TestIndependentWithPairOfAncestors validates that Independents excluded all |
| 257 |
// the ancestors (C, D, M, N -> C, D) |
| 258 |
func (s *mergeBaseSuite) TestIndependentWithPairOfAncestors(c *C) { |
| 259 |
revs := []string{"C", "D", "M", "N"} |
| 260 |
expectedRevs := []string{"C", "D"} |
| 261 |
s.AssertIndependents(c, revs, expectedRevs) |
| 262 |
} |
| 263 |
|
| 264 |
// TestIndependentAcrossCrossMerges validates that Independents works well |
| 265 |
// along cross merges (C, G, dev, M -> C, G, dev) |
| 266 |
func (s *mergeBaseSuite) TestIndependentAcrossCrossMerges(c *C) { |
| 267 |
revs := []string{"C", "G", "dev", "M", "N"} |
| 268 |
expectedRevs := []string{"C", "G", "dev"} |
| 269 |
s.AssertIndependents(c, revs, expectedRevs) |
| 270 |
} |
| 271 |
|
| 272 |
// TestIndependentChangingOrderRepetition validates that Independents works well |
| 273 |
// when the order and repetition is tricky (A, A^, A, N, N^ -> A, N) |
| 274 |
func (s *mergeBaseSuite) TestIndependentChangingOrderRepetition(c *C) { |
| 275 |
revs := []string{"A", "A^", "A", "N", "N^"} |
| 276 |
expectedRevs := []string{"A", "N"} |
| 277 |
s.AssertIndependents(c, revs, expectedRevs) |
| 278 |
} |
| 279 |
|
| 280 |
// TestIndependentChangingOrder validates that Independents works well |
| 281 |
// when the order is tricky (A^^^, A^, A^^, A, N -> A, N) |
| 282 |
func (s *mergeBaseSuite) TestIndependentChangingOrder(c *C) { |
| 283 |
revs := []string{"A^^^", "A^", "A^^", "A", "N"} |
| 284 |
expectedRevs := []string{"A", "N"} |
| 285 |
s.AssertIndependents(c, revs, expectedRevs) |
| 286 |
} |
| 287 |
|
| 288 |
// TestAncestor validates that IsAncestor returns true if walking from first |
| 289 |
// commit, through its parents, it can be reached the second ( A^^, A -> true ) |
| 290 |
func (s *mergeBaseSuite) TestAncestor(c *C) { |
| 291 |
revs := []string{"A^^", "A"} |
| 292 |
s.AssertAncestor(c, revs, true) |
| 293 |
|
| 294 |
revs = []string{"A", "A^^"} |
| 295 |
s.AssertAncestor(c, revs, false) |
| 296 |
} |
| 297 |
|
| 298 |
// TestAncestorBeyondMerges validates that IsAncestor returns true also if first can be |
| 299 |
// be reached from first one even crossing merge commits in between ( M, G -> true ) |
| 300 |
func (s *mergeBaseSuite) TestAncestorBeyondMerges(c *C) { |
| 301 |
revs := []string{"M", "G"} |
| 302 |
s.AssertAncestor(c, revs, true) |
| 303 |
|
| 304 |
revs = []string{"G", "M"} |
| 305 |
s.AssertAncestor(c, revs, false) |
| 306 |
} |
| 307 |
|
| 308 |
// TestAncestorSame validates that IsAncestor returns both are the same ( A, A -> true ) |
| 309 |
func (s *mergeBaseSuite) TestAncestorSame(c *C) { |
| 310 |
revs := []string{"A", "A"} |
| 311 |
s.AssertAncestor(c, revs, true) |
| 312 |
} |
| 313 |
|
| 314 |
// TestAncestorUnrelated validates that IsAncestor returns false when the passed commits |
| 315 |
// does not share any history, no matter the order used ( M, N -> false ) |
| 316 |
func (s *mergeBaseSuite) TestAncestorUnrelated(c *C) { |
| 317 |
revs := []string{"M", "N"} |
| 318 |
s.AssertAncestor(c, revs, false) |
| 319 |
|
| 320 |
revs = []string{"N", "M"} |
| 321 |
s.AssertAncestor(c, revs, false) |
| 322 |
} |