-
Notifications
You must be signed in to change notification settings - Fork 315
/
rebase_test.go
602 lines (491 loc) · 14 KB
/
rebase_test.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package git
import (
"bytes"
"errors"
"strconv"
"strings"
"testing"
"time"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
)
// Tests
func TestRebaseInMemoryWithConflict(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
// Create two branches with common history, where both modify "common-file"
// in a conflicting way.
_, err := commitSomething(repo, "common-file", "a\nb\nc\n", commitOptions{})
checkFatal(t, err)
checkFatal(t, createBranch(repo, "branch-a"))
checkFatal(t, createBranch(repo, "branch-b"))
checkFatal(t, repo.SetHead("refs/heads/branch-a"))
_, err = commitSomething(repo, "common-file", "1\nb\nc\n", commitOptions{})
checkFatal(t, err)
checkFatal(t, repo.SetHead("refs/heads/branch-b"))
_, err = commitSomething(repo, "common-file", "x\nb\nc\n", commitOptions{})
checkFatal(t, err)
branchA, err := repo.LookupBranch("branch-a", BranchLocal)
checkFatal(t, err)
onto, err := repo.AnnotatedCommitFromRef(branchA.Reference)
checkFatal(t, err)
// We then rebase "branch-b" onto "branch-a" in-memory, which should result
// in a conflict.
rebase, err := repo.InitRebase(nil, nil, onto, &RebaseOptions{InMemory: 1})
checkFatal(t, err)
_, err = rebase.Next()
checkFatal(t, err)
index, err := rebase.InmemoryIndex()
checkFatal(t, err)
// We simply resolve the conflict and commit the rebase.
if !index.HasConflicts() {
t.Fatal("expected index to have conflicts")
}
conflict, err := index.Conflict("common-file")
checkFatal(t, err)
resolvedBlobID, err := repo.CreateBlobFromBuffer([]byte("resolved contents"))
checkFatal(t, err)
resolvedEntry := *conflict.Our
resolvedEntry.Id = resolvedBlobID
checkFatal(t, index.Add(&resolvedEntry))
checkFatal(t, index.RemoveConflict("common-file"))
var commitID Oid
checkFatal(t, rebase.Commit(&commitID, signature(), signature(), "rebased message"))
checkFatal(t, rebase.Finish())
// And then assert that we can look up the new merge commit, and that the
// "common-file" has the expected contents.
commit, err := repo.LookupCommit(&commitID)
checkFatal(t, err)
if commit.Message() != "rebased message" {
t.Fatalf("unexpected commit message %q", commit.Message())
}
tree, err := commit.Tree()
checkFatal(t, err)
blob, err := repo.LookupBlob(tree.EntryByName("common-file").Id)
checkFatal(t, err)
if string(blob.Contents()) != "resolved contents" {
t.Fatalf("unexpected resolved blob contents %q", string(blob.Contents()))
}
}
func TestRebaseAbort(t *testing.T) {
// TEST DATA
// Inputs
branchName := "emile"
masterCommit := "something"
emileCommits := []string{
"fou",
"barre",
}
// Outputs
expectedHistory := []string{
"Test rebase, Baby! " + emileCommits[1],
"Test rebase, Baby! " + emileCommits[0],
"This is a commit\n",
}
// TEST
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
// Setup a repo with 2 branches and a different tree
err := setupRepoForRebase(repo, masterCommit, branchName, commitOptions{})
checkFatal(t, err)
// Create several commits in emile
for _, commit := range emileCommits {
_, err = commitSomething(repo, commit, commit, commitOptions{})
checkFatal(t, err)
}
// Check history
actualHistory, err := commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
// Rebase onto master
rebase, err := performRebaseOnto(repo, "master", nil)
checkFatal(t, err)
defer rebase.Free()
// Abort rebase
rebase.Abort()
// Check history is still the same
actualHistory, err = commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
}
func TestRebaseNoConflicts(t *testing.T) {
// TEST DATA
// Inputs
branchName := "emile"
masterCommit := "something"
emileCommits := []string{
"fou",
"barre",
"ouich",
}
// Outputs
expectedHistory := []string{
"Test rebase, Baby! " + emileCommits[2],
"Test rebase, Baby! " + emileCommits[1],
"Test rebase, Baby! " + emileCommits[0],
"Test rebase, Baby! " + masterCommit,
"This is a commit\n",
}
// TEST
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepo(t, repo)
// Try to open existing rebase
oRebase, err := repo.OpenRebase(nil)
if err == nil {
t.Fatal("Did not expect to find a rebase in progress")
}
// Setup a repo with 2 branches and a different tree
err = setupRepoForRebase(repo, masterCommit, branchName, commitOptions{})
checkFatal(t, err)
// Create several commits in emile
for _, commit := range emileCommits {
_, err = commitSomething(repo, commit, commit, commitOptions{})
checkFatal(t, err)
}
// Rebase onto master
rebase, err := performRebaseOnto(repo, "master", nil)
checkFatal(t, err)
defer rebase.Free()
// Open existing rebase
oRebase, err = repo.OpenRebase(nil)
checkFatal(t, err)
defer oRebase.Free()
if oRebase == nil {
t.Fatal("Expected to find an existing rebase in progress")
}
// Finish the rebase properly
err = rebase.Finish()
checkFatal(t, err)
// Check no more rebase is in progress
oRebase, err = repo.OpenRebase(nil)
if err == nil {
t.Fatal("Did not expect to find a rebase in progress")
}
// Check history is in correct order
actualHistory, err := commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
}
func TestRebaseGpgSigned(t *testing.T) {
// TEST DATA
entity, err := openpgp.NewEntity("Namey mcnameface", "test comment", "[email protected]", nil)
checkFatal(t, err)
rebaseOpts, err := DefaultRebaseOptions()
checkFatal(t, err)
signCommitContent := func(commitContent string) (string, string, error) {
cipherText := new(bytes.Buffer)
err := openpgp.ArmoredDetachSignText(cipherText, entity, strings.NewReader(commitContent), &packet.Config{})
if err != nil {
return "", "", errors.New("error signing payload")
}
return cipherText.String(), "", nil
}
rebaseOpts.CommitSigningCallback = signCommitContent
commitOpts := commitOptions{
CommitSigningCallback: signCommitContent,
}
// Inputs
branchName := "emile"
masterCommit := "something"
emileCommits := []string{
"fou",
"barre",
"ouich",
}
// Outputs
expectedHistory := []string{
"Test rebase, Baby! " + emileCommits[2],
"Test rebase, Baby! " + emileCommits[1],
"Test rebase, Baby! " + emileCommits[0],
"Test rebase, Baby! " + masterCommit,
"This is a commit\n",
}
// TEST
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
seedTestRepoOpt(t, repo, commitOpts)
// Try to open existing rebase
_, err = repo.OpenRebase(nil)
if err == nil {
t.Fatal("Did not expect to find a rebase in progress")
}
// Setup a repo with 2 branches and a different tree
err = setupRepoForRebase(repo, masterCommit, branchName, commitOpts)
checkFatal(t, err)
// Create several commits in emile
for _, commit := range emileCommits {
_, err = commitSomething(repo, commit, commit, commitOpts)
checkFatal(t, err)
}
// Rebase onto master
rebase, err := performRebaseOnto(repo, "master", &rebaseOpts)
checkFatal(t, err)
defer rebase.Free()
// Finish the rebase properly
err = rebase.Finish()
checkFatal(t, err)
// Check history is in correct order
actualHistory, err := commitMsgsList(repo)
checkFatal(t, err)
assertStringList(t, expectedHistory, actualHistory)
checkAllCommitsSigned(t, entity, repo)
}
func checkAllCommitsSigned(t *testing.T, entity *openpgp.Entity, repo *Repository) {
head, err := headCommit(repo)
checkFatal(t, err)
defer head.Free()
parent := head
err = checkCommitSigned(t, entity, parent)
checkFatal(t, err)
for parent.ParentCount() != 0 {
parent = parent.Parent(0)
defer parent.Free()
err = checkCommitSigned(t, entity, parent)
checkFatal(t, err)
}
}
func checkCommitSigned(t *testing.T, entity *openpgp.Entity, commit *Commit) error {
t.Helper()
signature, signedData, err := commit.ExtractSignature()
if err != nil {
t.Logf("No signature on commit\n%s", commit.ContentToSign())
return err
}
_, err = openpgp.CheckArmoredDetachedSignature(openpgp.EntityList{entity}, strings.NewReader(signedData), bytes.NewBufferString(signature))
if err != nil {
t.Logf("Commit is not signed correctly\n%s", commit.ContentToSign())
return err
}
return nil
}
// Utils
func setupRepoForRebase(repo *Repository, masterCommit, branchName string, commitOpts commitOptions) error {
// Create a new branch from master
err := createBranch(repo, branchName)
if err != nil {
return err
}
// Create a commit in master
_, err = commitSomething(repo, masterCommit, masterCommit, commitOpts)
if err != nil {
return err
}
// Switch to emile
err = repo.SetHead("refs/heads/" + branchName)
if err != nil {
return err
}
// Check master commit is not in emile branch
if entryExists(repo, masterCommit) {
return errors.New(masterCommit + " entry should not exist in " + branchName + " branch.")
}
return nil
}
func performRebaseOnto(repo *Repository, branch string, rebaseOpts *RebaseOptions) (*Rebase, error) {
master, err := repo.LookupBranch(branch, BranchLocal)
if err != nil {
return nil, err
}
defer master.Free()
onto, err := repo.AnnotatedCommitFromRef(master.Reference)
if err != nil {
return nil, err
}
defer onto.Free()
// Init rebase
rebase, err := repo.InitRebase(nil, nil, onto, rebaseOpts)
if err != nil {
return nil, err
}
// Check no operation has been started yet
rebaseOperationIndex, err := rebase.CurrentOperationIndex()
if rebaseOperationIndex != RebaseNoOperation && err != ErrRebaseNoOperation {
return nil, errors.New("No operation should have been started yet")
}
// Iterate in rebase operations regarding operation count
opCount := int(rebase.OperationCount())
for op := 0; op < opCount; op++ {
operation, err := rebase.Next()
if err != nil {
return nil, err
}
// Check operation index is correct
rebaseOperationIndex, err = rebase.CurrentOperationIndex()
if int(rebaseOperationIndex) != op {
return nil, errors.New("Bad operation index")
}
if !operationsAreEqual(rebase.OperationAt(uint(op)), operation) {
return nil, errors.New("Rebase operations should be equal")
}
// Get current rebase operation created commit
commit, err := repo.LookupCommit(operation.Id)
if err != nil {
return nil, err
}
defer commit.Free()
// Apply commit
err = rebase.Commit(operation.Id, signature(), signature(), commit.Message())
if err != nil {
return nil, err
}
}
return rebase, nil
}
func operationsAreEqual(l, r *RebaseOperation) bool {
return l.Exec == r.Exec && l.Type == r.Type && l.Id.String() == r.Id.String()
}
func createBranch(repo *Repository, branch string) error {
commit, err := headCommit(repo)
if err != nil {
return err
}
defer commit.Free()
_, err = repo.CreateBranch(branch, commit, false)
if err != nil {
return err
}
return nil
}
func signature() *Signature {
return &Signature{
Name: "Emile",
Email: "[email protected]",
When: time.Now(),
}
}
func headCommit(repo *Repository) (*Commit, error) {
head, err := repo.Head()
if err != nil {
return nil, err
}
defer head.Free()
commit, err := repo.LookupCommit(head.Target())
if err != nil {
return nil, err
}
return commit, nil
}
func headTree(repo *Repository) (*Tree, error) {
headCommit, err := headCommit(repo)
if err != nil {
return nil, err
}
defer headCommit.Free()
tree, err := headCommit.Tree()
if err != nil {
return nil, err
}
return tree, nil
}
func commitSomething(repo *Repository, something, content string, commitOpts commitOptions) (*Oid, error) {
headCommit, err := headCommit(repo)
if err != nil {
return nil, err
}
defer headCommit.Free()
index, err := NewIndex()
if err != nil {
return nil, err
}
defer index.Free()
blobOID, err := repo.CreateBlobFromBuffer([]byte(content))
if err != nil {
return nil, err
}
entry := &IndexEntry{
Mode: FilemodeBlob,
Id: blobOID,
Path: something,
}
if err := index.Add(entry); err != nil {
return nil, err
}
newTreeOID, err := index.WriteTreeTo(repo)
if err != nil {
return nil, err
}
newTree, err := repo.LookupTree(newTreeOID)
if err != nil {
return nil, err
}
defer newTree.Free()
commit, err := repo.CreateCommit("HEAD", signature(), signature(), "Test rebase, Baby! "+something, newTree, headCommit)
if err != nil {
return nil, err
}
if commitOpts.CommitSigningCallback != nil {
commit, err := repo.LookupCommit(commit)
if err != nil {
return nil, err
}
oid, err := commit.WithSignatureUsing(commitOpts.CommitSigningCallback)
if err != nil {
return nil, err
}
newCommit, err := repo.LookupCommit(oid)
if err != nil {
return nil, err
}
head, err := repo.Head()
if err != nil {
return nil, err
}
_, err = repo.References.Create(
head.Name(),
newCommit.Id(),
true,
"repoint to signed commit",
)
if err != nil {
return nil, err
}
}
checkoutOpts := &CheckoutOptions{
Strategy: CheckoutRemoveUntracked | CheckoutForce,
}
err = repo.CheckoutIndex(index, checkoutOpts)
if err != nil {
return nil, err
}
return commit, nil
}
func entryExists(repo *Repository, file string) bool {
headTree, err := headTree(repo)
if err != nil {
return false
}
defer headTree.Free()
_, err = headTree.EntryByPath(file)
return err == nil
}
func commitMsgsList(repo *Repository) ([]string, error) {
head, err := headCommit(repo)
if err != nil {
return nil, err
}
defer head.Free()
var commits []string
parent := head.Parent(0)
defer parent.Free()
commits = append(commits, head.Message(), parent.Message())
for parent.ParentCount() != 0 {
parent = parent.Parent(0)
defer parent.Free()
commits = append(commits, parent.Message())
}
return commits, nil
}
func assertStringList(t *testing.T, expected, actual []string) {
if len(expected) != len(actual) {
t.Fatal("Lists are not the same size, expected " + strconv.Itoa(len(expected)) +
", got " + strconv.Itoa(len(actual)))
}
for index, element := range expected {
if element != actual[index] {
t.Error("Expected element " + strconv.Itoa(index) + " to be " + element + ", got " + actual[index])
}
}
}