-
Notifications
You must be signed in to change notification settings - Fork 18
/
stack_edit.go
101 lines (86 loc) · 2.72 KB
/
stack_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
92
93
94
95
96
97
98
99
100
101
package main
import (
"context"
"errors"
"fmt"
"strings"
"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"
"go.abhg.dev/gs/internal/ui"
)
type stackEditCmd struct {
Editor string `help:"Editor to use for editing the downstack. Defaults to Git's default editor."`
Branch string `placeholder:"NAME" help:"Branch whose stack we're editing. Defaults to current branch." predictor:"trackedBranches"`
}
func (*stackEditCmd) Help() string {
return text.Dedent(`
This operation requires a linear stack:
no branch can have multiple branches above it.
An editor opens with a list of branches in the current stack in-order,
with the topmost branch at the top of the file,
and the branch closest to the trunk at the bottom.
Modifications to the list will be reflected in the stack
when the editor is closed.
If the file is cleared, no changes will be made.
Branches that are deleted from the list will be ignored.
`)
}
func (cmd *stackEditCmd) Run(
ctx context.Context,
log *log.Logger,
view ui.View,
repo *git.Repository,
store *state.Store,
svc *spice.Service,
) error {
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
}
stack, err := svc.ListStackLinear(ctx, cmd.Branch)
if err != nil {
var nonLinearErr *spice.NonLinearStackError
if errors.As(err, &nonLinearErr) {
// TODO: We could provide a prompt here to select a linear stack to edit from.
log.Errorf("%v is part of a stack with a divergent upstack.", cmd.Branch)
log.Errorf("%v has multiple branches above it: %s", nonLinearErr.Branch, strings.Join(nonLinearErr.Aboves, ", "))
log.Errorf("Check out one of those branches and try again.")
return errors.New("current branch has ambiguous upstack")
}
return fmt.Errorf("list stack: %w", err)
}
// If current branch was trunk, it'll be at the bottom of the stack.
if stack[0] == store.Trunk() {
stack = stack[1:]
}
if len(stack) == 1 {
log.Info("nothing to edit")
return nil
}
_, err = svc.StackEdit(ctx, &spice.StackEditRequest{
Editor: cmd.Editor,
Stack: stack,
})
if err != nil {
if errors.Is(err, spice.ErrStackEditAborted) {
log.Infof("stack 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: cmd.Branch,
}).Run(ctx, log, view, repo, store, svc)
}