-
Notifications
You must be signed in to change notification settings - Fork 18
/
branch_edit.go
63 lines (55 loc) · 1.44 KB
/
branch_edit.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
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 branchEditCmd struct{}
func (*branchEditCmd) Help() string {
return text.Dedent(`
Starts an interactive rebase with only the commits
in this branch.
Following the rebase, branches upstack from this branch
will be restacked.
`)
}
func (*branchEditCmd) Run(
ctx context.Context,
log *log.Logger,
repo *git.Repository,
store *state.Store,
svc *spice.Service,
) error {
currentBranch, err := repo.CurrentBranch(ctx)
if err != nil {
return fmt.Errorf("get current branch: %w", err)
}
b, err := svc.LookupBranch(ctx, currentBranch)
if err != nil {
if errors.Is(err, state.ErrNotExist) {
return fmt.Errorf("branch not tracked: %s", currentBranch)
}
return fmt.Errorf("get branch: %w", err)
}
req := git.RebaseRequest{
Interactive: true,
Branch: currentBranch,
Upstream: b.Base,
}
if err := repo.Rebase(ctx, req); err != nil {
// if the rebase is interrupted,
// recover with an 'upstack restack' later.
return svc.RebaseRescue(ctx, spice.RebaseRescueRequest{
Err: err,
Command: []string{"upstack", "restack"},
Branch: currentBranch,
Message: fmt.Sprintf("interrupted: edit branch %s", currentBranch),
})
}
return (&upstackRestackCmd{}).Run(ctx, log, repo, store, svc)
}