-
Notifications
You must be signed in to change notification settings - Fork 25
/
git_rebase.go
501 lines (429 loc) · 12.8 KB
/
git_rebase.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
package vcs
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"time"
git "github.com/libgit2/git2go/v33"
)
type SturdyRebaseStatus int
const (
RebaseInProgress SturdyRebaseStatus = iota // Used during initialization
RebaseCanContinue // The rebase can continue
RebaseHaveConflicts // There are conflicts that must be resolved before continuing
RebaseCompleted // The rebase is completed
)
var commonRebaseOptions = &git.RebaseOptions{
InMemory: 0,
MergeOptions: git.MergeOptions{
// Defaults from https://github.com/libgit2/libgit2sharp/blob/master/LibGit2Sharp/MergeOptionsBase.cs
RenameThreshold: 50,
TargetLimit: 200,
},
CheckoutOptions: git.CheckoutOptions{
Strategy: git.CheckoutSafe,
},
}
type SturdyRebase struct {
repo *repository
gitRebase *git.Rebase
completed bool
lastCompletedCommit string
}
var NoRebaseInProgress = errors.New("no rebasing in progress")
// OpenRebase resumes a rebasing operation started by InitRebase
func (r *repository) OpenRebase() (*SturdyRebase, error) {
defer getMeterFunc("OpenRebase")()
reb, err := r.r.OpenRebase(commonRebaseOptions)
if err != nil {
if gitErr, ok := err.(*git.GitError); ok && gitErr.Class == git.ErrClassRebase && gitErr.Code == git.ErrNotFound {
return nil, NoRebaseInProgress
}
return nil, fmt.Errorf("could not open rebase: %w", err)
}
return &SturdyRebase{
repo: r,
gitRebase: reb,
}, nil
}
type SturdyRebaseResolve struct {
Path string
Version string
}
func (rebase *SturdyRebase) ResolveFiles(resolves []SturdyRebaseResolve) error {
idx, err := rebase.repo.r.Index()
if err != nil {
return fmt.Errorf("failed to get index in rebase: %w", err)
}
defer idx.Free()
for _, resolve := range resolves {
err := rebase.resolveFile(idx, resolve.Path, resolve.Version)
if err != nil {
return err
}
}
err = idx.Write()
if err != nil {
return fmt.Errorf("failed to write new index: %w", err)
}
return nil
}
func (rebase *SturdyRebase) resolveFile(idx *git.Index, filePath string, version string) error {
conflict, err := idx.Conflict(filePath)
if err != nil {
return fmt.Errorf("failed to get conflict: %w", err)
}
// The perspective when rebasing is from the new bases pov
var use *git.IndexEntry
switch version {
case "custom":
// Add the file as is
err = idx.RemoveConflict(filePath)
if err != nil {
return fmt.Errorf("failed to remove conflict from index: %w", err)
}
err = idx.AddByPath(filePath)
if err != nil {
return fmt.Errorf("failed to add resolved file: %w", err)
}
return nil
case "workspace":
use = conflict.Their
case "trunk":
use = conflict.Our
default:
return fmt.Errorf("unknown version: %s", version)
}
fullFilePath := path.Join(rebase.repo.path, filePath)
// use is nil when the file that we're picking is deleted
if use != nil {
blb, err := rebase.repo.r.LookupBlob(use.Id)
if err != nil {
return fmt.Errorf("failed to get blob: %w", err)
}
defer blb.Free()
err = ioutil.WriteFile(fullFilePath, blb.Contents(), os.FileMode(use.Mode))
if err != nil {
return fmt.Errorf("failed to write resolution: %w", err)
}
} else {
// Make sure to delete the file
if err := os.Remove(fullFilePath); err != nil {
return fmt.Errorf("failed to remove file during conflict resolution: %w", err)
}
}
if err := idx.RemoveConflict(filePath); err != nil {
return fmt.Errorf("failed to remove conflict from index: %w", err)
}
if use != nil {
if err := idx.AddByPath(filePath); err != nil {
return fmt.Errorf("failed to add resolved file: %w", err)
}
} else {
if err := idx.RemoveByPath(filePath); err != nil {
return fmt.Errorf("failed to remove resolved file: %w", err)
}
}
return nil
}
func (rebase *SturdyRebase) ConflictingFiles() ([]string, error) {
idx, err := rebase.repo.r.Index()
if err != nil {
return nil, fmt.Errorf("failed to get index in rebase: %w", err)
}
defer idx.Free()
return ConflictingFilesInIndex(idx)
}
func ConflictingFilesInIndex(idx *git.Index) ([]string, error) {
conflicts, err := idx.ConflictIterator()
if err != nil {
return nil, fmt.Errorf("failed to parse conflicts: %w", err)
}
var paths []string
for {
c, err := conflicts.Next()
if err != nil {
if gitErr, ok := err.(*git.GitError); ok && gitErr.Code == git.ErrIterOver {
break
}
return nil, fmt.Errorf("failed to get next conflict: %w", err)
}
if c.Their != nil {
paths = append(paths, c.Their.Path)
} else if c.Our != nil {
paths = append(paths, c.Our.Path)
} else if c.Ancestor != nil {
paths = append(paths, c.Ancestor.Path)
}
}
return paths, nil
}
type RebasedCommit struct {
OldCommitID string
NewCommitID string
Noop bool
}
// Continue the rebase until the next conflict, or the rebase is completed
func (rebase *SturdyRebase) Continue() (conflicts bool, rebasedCommits []RebasedCommit, err error) {
idx, err := rebase.repo.r.Index()
if err != nil {
return false, nil, fmt.Errorf("failed to get index: %w", err)
}
defer idx.Free()
for {
var operation *git.RebaseOperation
rebaseOpIdx, err := rebase.gitRebase.CurrentOperationIndex()
if err != nil {
if errors.Is(err, git.ErrRebaseNoOperation) {
// Continue with the next operation
operation, err = rebase.gitRebase.Next()
if err != nil {
if gitErr, ok := err.(*git.GitError); ok && gitErr.Code == git.ErrIterOver {
break
}
return false, nil, fmt.Errorf("failed to run next operation: %w", err)
}
} else {
return false, nil, fmt.Errorf("failed to get operation: %w", err)
}
} else {
operation = rebase.gitRebase.OperationAt(rebaseOpIdx)
}
if idx.HasConflicts() {
return true, rebasedCommits, nil
}
// Load commit data so that we can re-use it
commit, err := rebase.repo.r.LookupCommit(operation.Id)
if err != nil {
return false, nil, fmt.Errorf("failed to find commit in rebase: %w", err)
}
defer commit.Free()
// Add all unstaged changes to the index (to track new updates, deletes, and added files _during_ the sync)
if _, err := rebase.repo.AddFilesToIndex([]string{"."}); err != nil {
return false, nil, fmt.Errorf("failed to add unstaged to index: %w", err)
}
var noop bool
err = rebase.gitRebase.Commit(operation.Id, commit.Author(), commit.Committer(), commit.Message())
if err != nil {
if gitErr, ok := err.(*git.GitError); ok && gitErr.Class == git.ErrClassRebase && gitErr.Code == git.ErrApplied {
noop = true
} else {
return false, nil, fmt.Errorf("failed to commit: %w", err)
}
}
if noop {
rebasedCommits = append(rebasedCommits, RebasedCommit{
OldCommitID: commit.Id().String(),
Noop: noop,
})
} else {
headCommitAfter, err := rebase.repo.HeadCommit()
if err != nil {
return false, nil, fmt.Errorf("failed to get HEAD after commit: %w", err)
}
// Record which commit resulted in what new commit
rebasedCommits = append(rebasedCommits, RebasedCommit{
OldCommitID: commit.Id().String(),
NewCommitID: headCommitAfter.Id().String(),
})
headCommitAfter.Free()
}
rebase.lastCompletedCommit = commit.Id().String()
// Advance to the next operation
_, err = rebase.gitRebase.Next()
if err != nil {
if gitErr, ok := err.(*git.GitError); ok && gitErr.Code == git.ErrIterOver {
break
}
return false, nil, fmt.Errorf("failed to run next operation: %w", err)
}
}
err = rebase.gitRebase.Finish()
if err != nil {
return false, nil, fmt.Errorf("failed to complete rebase: %w", err)
}
// Stash unsaved changes before attempting rebase
err = rebase.repo.stashPopFromRebase()
if err != nil {
return false, nil, fmt.Errorf("failed to pop stash: %w", err)
}
rebase.completed = true
return false, rebasedCommits, nil
}
func (rebase *SturdyRebase) LastCompletedCommit() string {
return rebase.lastCompletedCommit
}
func (rebase *SturdyRebase) Status() (SturdyRebaseStatus, error) {
if rebase.completed {
return RebaseCompleted, nil
}
// This is not a nice solution
// TODO: Create an interface with "Status() (SturdyRebaseStatus, error)" so that
// this case can be handled nicely when SturdyRebase is "empty" (happens in a rebase with dropped commits)
if rebase.repo == nil {
return RebaseCompleted, nil
}
idx, err := rebase.repo.r.Index()
if err != nil {
return 0, fmt.Errorf("failed to get index: %w", err)
}
defer idx.Free()
if idx.HasConflicts() {
return RebaseHaveConflicts, nil
}
return RebaseCanContinue, nil
}
func (rebase *SturdyRebase) Progress() (current, total uint, err error) {
current, err = rebase.gitRebase.CurrentOperationIndex()
if err != nil {
return 0, 0, fmt.Errorf("failed to get current operation index: %w", err)
}
total = rebase.gitRebase.OperationCount()
return
}
type ConflictDiffs struct {
WorkspacePatch string
TrunkPatch string
}
func (rebase *SturdyRebase) ConflictDiff(filePath string) (ConflictDiffs, error) {
idx, err := rebase.repo.r.Index()
if err != nil {
return ConflictDiffs{}, fmt.Errorf("failed to get index: %w", err)
}
defer idx.Free()
conflict, err := idx.Conflict(filePath)
if err != nil {
return ConflictDiffs{}, fmt.Errorf("failed to get conflict: %w", err)
}
var diffs ConflictDiffs
diffs.WorkspacePatch, err = rebase.patchBetweenIndexEntries(conflict.Ancestor, conflict.Their)
if err != nil {
return ConflictDiffs{}, fmt.Errorf("failed to create workspace patch: %w", err)
}
diffs.TrunkPatch, err = rebase.patchBetweenIndexEntries(conflict.Ancestor, conflict.Our)
if err != nil {
return ConflictDiffs{}, fmt.Errorf("failed to create trunk patch: %w", err)
}
return diffs, nil
}
func (rebase *SturdyRebase) patchBetweenIndexEntries(entryA, entryB *git.IndexEntry) (string, error) {
var blobA, blobB *git.Blob
var blobApath, blobBpath string
var err error
// It's ok for one of entryA or entryB to be nil
if entryA != nil {
blobApath = entryA.Path
blobA, err = rebase.repo.r.LookupBlob(entryA.Id)
if err != nil {
return "", fmt.Errorf("failed to get blob: %w", err)
}
defer blobA.Free()
}
if entryB != nil {
blobBpath = entryB.Path
blobB, err = rebase.repo.r.LookupBlob(entryB.Id)
if err != nil {
return "", fmt.Errorf("failed to get blob: %w", err)
}
defer blobB.Free()
}
var patch string
err = git.DiffBlobs(
blobA,
blobApath,
blobB,
blobBpath,
nil,
func(delta git.DiffDelta, progress float64) (git.DiffForEachHunkCallback, error) {
// An example diff:
// diff --git b/abc.txt a/abc.txt
// index 0000000..a3bb749 100644
// --- b/abc.txt
// +++ a/abc.txt
// @@ -1,6 +1,7 @@
// a
// b
// d
// +e
// f
// g
// h
nameOrDevNull := func(prefix, name string) string {
if len(name) == 0 {
return "/dev/null"
}
return prefix + name
}
oldName := nameOrDevNull("a/", delta.OldFile.Path)
newName := nameOrDevNull("b/", delta.NewFile.Path)
patch = fmt.Sprintf("diff --git %s %s\n", oldName, newName)
patch += fmt.Sprintf("index 0000000..0000000 %x\n", delta.NewFile.Mode)
patch += fmt.Sprintf("--- %s\n", oldName)
patch += fmt.Sprintf("+++ %s\n", newName)
return func(hunk git.DiffHunk) (git.DiffForEachLineCallback, error) {
patch += fmt.Sprintf("%s", hunk.Header)
return func(line git.DiffLine) error {
var prefix rune
switch line.Origin {
case git.DiffLineAddition, git.DiffLineAddEOFNL:
prefix = '+'
case git.DiffLineDeletion, git.DiffLineDelEOFNL:
prefix = '-'
case git.DiffLineContext, git.DiffLineContextEOFNL:
prefix = ' '
default:
return fmt.Errorf("unknown line.Origin: %+v", line)
}
patch += fmt.Sprintf("%c%s", prefix, line.Content)
return nil
}, nil
}, nil
},
git.DiffDetailLines,
)
if err != nil {
return "", fmt.Errorf("patch creation failed: %w", err)
}
return patch, nil
}
func (r *repository) stashUnsavedForRebase() error {
defer getMeterFunc("stashUnsavedForRebase")()
_, err := r.r.Stashes.Save(&git.Signature{
Name: "Stasher",
Email: "stash@getsturdy.com",
When: time.Now(),
}, "Stashing to perform rebase", git.StashDefault)
if err != nil {
// Nothing to stash
if gitErr := err.(*git.GitError); gitErr.Class == git.ErrClassStash && gitErr.Code == git.ErrNotFound {
return nil
}
return err
}
return nil
}
var errStopLoop = errors.New("stop loop")
func (r *repository) stashPopFromRebase() error {
defer getMeterFunc("stashPopFromRebase")()
err := r.r.Stashes.Foreach(func(index int, message string, id *git.Oid) error {
if strings.Contains(message, "Stashing to perform rebase") {
err := r.r.Stashes.Apply(index, git.StashApplyOptions{
CheckoutOptions: git.CheckoutOpts{
Strategy: git.CheckoutSafe,
},
})
if err != nil {
return fmt.Errorf("failed to apply stash: %w", err)
}
return errStopLoop
}
return nil
})
if err != nil && !errors.Is(err, errStopLoop) {
return err
}
return nil
}