forked from abhinav/git-spice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_restack.go
80 lines (68 loc) · 1.98 KB
/
stack_restack.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
package main
import (
"context"
"errors"
"fmt"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/spice"
"go.abhg.dev/gs/internal/text"
)
type stackRestackCmd struct{}
func (*stackRestackCmd) Help() string {
return text.Dedent(`
All branches in the current stack are rebased on top of their
respective bases, ensuring a linear history.
`)
}
func (*stackRestackCmd) Run(ctx context.Context, log *log.Logger, opts *globalOptions) error {
repo, store, svc, err := openRepo(ctx, log, opts)
if err != nil {
return err
}
currentBranch, err := repo.CurrentBranch(ctx)
if err != nil {
return fmt.Errorf("get current branch: %w", err)
}
stack, err := svc.ListStack(ctx, currentBranch)
if err != nil {
return fmt.Errorf("list stack: %w", err)
}
loop:
for _, branch := range stack {
// Trunk never needs to be restacked.
if branch == store.Trunk() {
continue loop
}
res, err := svc.Restack(ctx, branch)
if err != nil {
var rebaseErr *git.RebaseInterruptError
switch {
case errors.As(err, &rebaseErr):
// If the rebase is interrupted by a conflict,
// we'll resume by re-running this command.
return svc.RebaseRescue(ctx, spice.RebaseRescueRequest{
Err: rebaseErr,
Command: []string{"stack", "restack"},
Branch: currentBranch,
Message: fmt.Sprintf("interrupted: restack stack for %s", branch),
})
case errors.Is(err, spice.ErrAlreadyRestacked):
// Log the "does not need to be restacked" message
// only for branches that are not the current branch.
if branch != currentBranch {
log.Infof("%v: branch does not need to be restacked.", branch)
}
continue loop
default:
return fmt.Errorf("restack branch: %w", err)
}
}
log.Infof("%v: restacked on %v", branch, res.Base)
}
// On success, check out the original branch.
if err := repo.Checkout(ctx, currentBranch); err != nil {
return fmt.Errorf("checkout branch %v: %w", currentBranch, err)
}
return nil
}