forked from abhinav/git-spice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upstack_submit.go
103 lines (88 loc) · 2.34 KB
/
upstack_submit.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
102
103
package main
import (
"context"
"errors"
"fmt"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/secret"
"go.abhg.dev/gs/internal/text"
)
type upstackSubmitCmd struct {
submitOptions
Branch string `placeholder:"NAME" help:"Branch to start at" predictor:"trackedBranches"`
}
func (*upstackSubmitCmd) Help() string {
return text.Dedent(`
Change Requests are created or updated
for the current branch and all branches upstack from it.
If the base of the current branch is not trunk,
it must have already been submitted by a prior command.
Use --branch to start at a different branch.
`) + "\n" + _submitHelp
}
func (cmd *upstackSubmitCmd) Run(
ctx context.Context,
secretStash secret.Stash,
log *log.Logger,
opts *globalOptions,
) error {
repo, store, svc, err := openRepo(ctx, log, opts)
if err != nil {
return err
}
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() {
b, err := svc.LookupBranch(ctx, cmd.Branch)
if err != nil {
return fmt.Errorf("lookup branch %v: %w", cmd.Branch, err)
}
if b.Base != store.Trunk() {
base, err := svc.LookupBranch(ctx, b.Base)
if err != nil {
return fmt.Errorf("lookup base %v: %w", b.Base, err)
}
if base.Change == nil && cmd.Publish {
log.Errorf("%v: base (%v) has not been submitted", cmd.Branch, b.Base)
return errors.New("submit the base branch first")
}
}
}
upstacks, err := svc.ListUpstack(ctx, cmd.Branch)
if err != nil {
return fmt.Errorf("list upstack: %w", err)
}
// If running from trunk, exclude trunk from the list.
// Trunk cannot be submitted but everything upstack can.
if cmd.Branch == store.Trunk() {
upstacks = upstacks[1:]
}
// TODO: generalize into a service-level method
// TODO: separate preparation of the stack from submission
session := newSubmitSession(repo, store, secretStash, opts, log)
for _, b := range upstacks {
err := (&branchSubmitCmd{
submitOptions: cmd.submitOptions,
Branch: b,
}).run(ctx, session, repo, store, svc, log, opts)
if err != nil {
return fmt.Errorf("submit %v: %w", b, err)
}
}
if cmd.DryRun {
return nil
}
return updateNavigationComments(
ctx,
store,
svc,
log,
cmd.NavigationComment,
session,
)
}