forked from abhinav/git-spice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
downstack_edit.go
91 lines (76 loc) · 2.45 KB
/
downstack_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
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
package main
import (
"context"
"errors"
"fmt"
"slices"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/must"
"go.abhg.dev/gs/internal/spice"
"go.abhg.dev/gs/internal/text"
)
type downstackEditCmd struct {
Editor string `help:"Editor to use for editing the downstack. Defaults to Git's default editor."`
Branch string `placeholder:"NAME" help:"Branch to edit from. Defaults to current branch." predictor:"trackedBranches"`
}
func (*downstackEditCmd) Help() string {
return text.Dedent(`
An editor opens with a list of branches in-order,
starting from the current branch until trunk.
The current branch is at the top of the list.
Use --branch to start at a different branch.
Modifications to the list will be reflected in the stack
when the editor is closed, and the topmost branch will be checked out.
If the file is cleared, no changes will be made.
Branches that are deleted from the list will be ignored.
Branches that are upstack of the current branch will not be modified.
`)
}
func (cmd *downstackEditCmd) Run(ctx context.Context, log *log.Logger, opts *globalOptions) error {
repo, store, svc, err := openRepo(ctx, log, opts)
if err != nil {
return err
}
if cmd.Editor == "" {
cmd.Editor = gitEditor(ctx, repo)
}
if cmd.Branch == "" {
currentBranch, err := repo.CurrentBranch(ctx)
if err != nil {
return fmt.Errorf("get current branch: %w", err)
}
cmd.Branch = currentBranch
}
if cmd.Branch == store.Trunk() {
return errors.New("cannot edit below trunk")
}
downstacks, err := svc.ListDownstack(ctx, cmd.Branch)
if err != nil {
return fmt.Errorf("list downstack: %w", err)
}
must.NotBeEmptyf(downstacks, "downstack cannot be empty")
must.BeEqualf(downstacks[0], cmd.Branch,
"downstack must start with the original branch")
if len(downstacks) == 1 {
log.Infof("nothing to edit below %s", cmd.Branch)
return nil
}
slices.Reverse(downstacks) // branch closest to trunk first
res, err := svc.StackEdit(ctx, &spice.StackEditRequest{
Stack: downstacks,
Editor: cmd.Editor,
})
if err != nil {
if errors.Is(err, spice.ErrStackEditAborted) {
log.Infof("downstack edit aborted")
return nil
}
// TODO: we can probably recover from the rebase operation
// by saving the branch list somewhere,
// and allowing it to be provided as input to the command.
return fmt.Errorf("edit downstack: %w", err)
}
return (&branchCheckoutCmd{
Branch: res.Stack[len(res.Stack)-1],
}).Run(ctx, log, opts)
}