-
Notifications
You must be signed in to change notification settings - Fork 18
/
branch_restack.go
66 lines (59 loc) · 1.7 KB
/
branch_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
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/spice/state"
"go.abhg.dev/gs/internal/text"
)
type branchRestackCmd struct {
Branch string `placeholder:"NAME" help:"Branch to restack" predictor:"trackedBranches"`
}
func (*branchRestackCmd) Help() string {
return text.Dedent(`
The current branch will be rebased onto its base,
ensuring a linear history.
Use --branch to target a different branch.
`)
}
func (cmd *branchRestackCmd) Run(
ctx context.Context,
log *log.Logger,
repo *git.Repository,
svc *spice.Service,
) error {
if cmd.Branch == "" {
currentBranch, err := repo.CurrentBranch(ctx)
if err != nil {
return fmt.Errorf("get current branch: %w", err)
}
cmd.Branch = currentBranch
}
res, err := svc.Restack(ctx, cmd.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{"branch", "restack"},
Branch: cmd.Branch,
Message: fmt.Sprintf("interrupted: restack branch %s", cmd.Branch),
})
case errors.Is(err, state.ErrNotExist):
log.Errorf("%v: branch not tracked: run 'gs branch track'", cmd.Branch)
return errors.New("untracked branch")
case errors.Is(err, spice.ErrAlreadyRestacked):
log.Infof("%v: branch does not need to be restacked.", cmd.Branch)
return nil
}
return fmt.Errorf("restack branch: %w", err)
}
log.Infof("%v: restacked on %v", cmd.Branch, res.Base)
return nil
}