forked from abhinav/git-spice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch_split.go
397 lines (343 loc) · 11.4 KB
/
branch_split.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
import (
"cmp"
"context"
"errors"
"fmt"
"strings"
"github.com/alecthomas/kong"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/forge"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/spice/state"
"go.abhg.dev/gs/internal/text"
"go.abhg.dev/gs/internal/ui"
"go.abhg.dev/gs/internal/ui/widget"
)
// branchSplitCmd splits a branch into two or more branches
// along commit boundaries.
type branchSplitCmd struct {
At []branchSplit `placeholder:"COMMIT:NAME" help:"Commits to split the branch at."`
Branch string `placeholder:"NAME" help:"Branch to split commits of."`
}
func (*branchSplitCmd) Help() string {
return text.Dedent(`
Splits the current branch into two or more branches at specific
commits, inserting the new branches into the stack
at the positions of the commits.
Use the --branch flag to specify a different branch to split.
By default, the command will prompt for commits to introduce
splits at.
Supply the --at flag one or more times to split a branch
without a prompt.
--at COMMIT:NAME
Where COMMIT resolves to a commit per gitrevisions(7),
and NAME is the name of the new branch.
For example:
# split at a specific commit
gs branch split --at 1234567:newbranch
# split at the previous commit
gs branch split --at HEAD^:newbranch
`)
}
func (cmd *branchSplitCmd) Run(ctx context.Context, log *log.Logger, opts *globalOptions) (err error) {
repo, store, svc, err := openRepo(ctx, log, opts)
if err != nil {
return err
}
if cmd.Branch == "" {
cmd.Branch, err = repo.CurrentBranch(ctx)
if err != nil {
return fmt.Errorf("get current branch: %w", err)
}
}
if cmd.Branch == store.Trunk() {
return fmt.Errorf("cannot split trunk")
}
branch, err := svc.LookupBranch(ctx, cmd.Branch)
if err != nil {
return fmt.Errorf("lookup branch %q: %w", cmd.Branch, err)
}
// Commits are in oldest-to-newest order,
// with last commit being branch head.
branchCommits, err := repo.ListCommitsDetails(ctx,
git.CommitRangeFrom(branch.Head).
ExcludeFrom(branch.BaseHash).
Reverse())
if err != nil {
return fmt.Errorf("list commits: %w", err)
}
branchCommitHashes := make(map[git.Hash]struct{}, len(branchCommits))
for _, commit := range branchCommits {
branchCommitHashes[commit.Hash] = struct{}{}
}
// If len(cmd.At) == 0, run in interactive mode to build up cmd.At.
if len(cmd.At) == 0 {
if !opts.Prompt {
return fmt.Errorf("use --at to split non-interactively: %w", errNoPrompt)
}
if len(branchCommits) < 2 {
return errors.New("cannot split a branch with fewer than 2 commits")
}
commits := make([]widget.CommitSummary, len(branchCommits))
for i, commit := range branchCommits {
commits[i] = widget.CommitSummary{
ShortHash: commit.ShortHash,
Subject: commit.Subject,
AuthorDate: commit.AuthorDate,
}
}
selectWidget := widget.NewBranchSplit().
WithTitle("Select commits").
WithHEAD(cmd.Branch).
WithDescription("Select commits to split the branch at").
WithCommits(commits...)
if err := ui.Run(selectWidget); err != nil {
return fmt.Errorf("prompt: %w", err)
}
selectedIdxes := selectWidget.Selected()
if len(selectedIdxes) < 1 {
return errors.New("no commits selected")
}
selected := make([]git.CommitDetail, len(selectedIdxes))
for i, idx := range selectedIdxes {
selected[i] = branchCommits[idx]
}
fields := make([]ui.Field, len(selected))
branchNames := make([]string, len(selected))
for i, commit := range selected {
var desc strings.Builder
desc.WriteString(" □ ")
(&widget.CommitSummary{
ShortHash: commit.ShortHash,
Subject: commit.Subject,
AuthorDate: commit.AuthorDate,
}).Render(&desc, widget.DefaultCommitSummaryStyle)
input := ui.NewInput().
WithTitle("Branch name").
WithDescription(desc.String()).
WithValidate(func(value string) error {
if strings.TrimSpace(value) == "" {
return errors.New("branch name cannot be empty")
}
if repo.BranchExists(ctx, value) {
return fmt.Errorf("branch name already taken: %v", value)
}
return nil
}).
WithValue(&branchNames[i])
fields[i] = input
}
if err := ui.NewForm(fields...).Run(); err != nil {
return fmt.Errorf("prompt: %w", err)
}
for i, split := range selected {
cmd.At = append(cmd.At, branchSplit{
Commit: split.Hash.String(),
Name: branchNames[i],
})
}
}
// Turn each commitish into a commit.
commitHashes := make([]git.Hash, len(cmd.At))
newTakenNames := make(map[string]int, len(cmd.At)) // index into cmd.At
for i, split := range cmd.At {
// Interactive prompt verifies if the branch name is taken,
// but we have to check again here.
if repo.BranchExists(ctx, split.Name) {
return fmt.Errorf("--at[%d]: branch already exists: %v", i, split.Name)
}
// Also prevent duplicate branch names speciifed as input.
if otherIdx, ok := newTakenNames[split.Name]; ok {
return fmt.Errorf("--at[%d]: branch name already taken by --at[%d]: %v", i, otherIdx, split.Name)
}
newTakenNames[split.Name] = i
commitHash, err := repo.PeelToCommit(ctx, split.Commit)
if err != nil {
return fmt.Errorf("--at[%d]: resolve commit %q: %w", i, split.Commit, err)
}
// All commits must in base..head.
// So you can't do 'split --at HEAD~10:newbranch'.
if _, ok := branchCommitHashes[commitHash]; !ok {
return fmt.Errorf("--at[%d]: %v (%v) is not in range %v..%v", i,
split.Commit, commitHash, branch.Base, cmd.Branch)
}
commitHashes[i] = commitHash
}
// First we'll stage the state changes.
// The commits are in oldest-to-newst order,
// so we can just go through them in order.
branchTx := store.BeginBranchTx()
for idx, split := range cmd.At {
base, baseHash := branch.Base, branch.BaseHash
if idx > 0 {
base, baseHash = cmd.At[idx-1].Name, commitHashes[idx-1]
}
if err := branchTx.Upsert(ctx, state.UpsertRequest{
Name: split.Name,
Base: base,
BaseHash: baseHash,
}); err != nil {
return fmt.Errorf("add branch %v with base %v: %w", split.Name, base, err)
}
}
finalBase, finalBaseHash := branch.Base, branch.BaseHash
if len(cmd.At) > 0 {
finalBase, finalBaseHash = cmd.At[len(cmd.At)-1].Name, commitHashes[len(cmd.At)-1]
}
if err := branchTx.Upsert(ctx, state.UpsertRequest{
Name: cmd.Branch,
Base: finalBase,
BaseHash: finalBaseHash,
}); err != nil {
return fmt.Errorf("update branch %v with base %v: %w", cmd.Branch, finalBase, err)
}
// If the branch being split had a Change associated with it,
// ask the user which branch to associate the Change with.
if branch.Change != nil && !opts.Prompt {
log.Info("Branch has an associated CR. Leaving it assigned to the original branch.",
"cr", branch.Change.ChangeID())
} else if branch.Change != nil {
branchNames := make([]string, len(cmd.At)+1)
for i, split := range cmd.At {
branchNames[i] = split.Name
}
branchNames[len(branchNames)-1] = cmd.Branch
// TODO:
// use ll branch-style widget instead
// showing the commits for each branch.
var changeBranch string
prompt := ui.NewSelect[string]().
WithTitle(fmt.Sprintf("Assign CR %v to branch", branch.Change.ChangeID())).
WithDescription("Branch being split has an open CR assigned to it.\n" +
"Select which branch should take over the CR.").
WithValue(&changeBranch).
With(ui.ComparableOptions(cmd.Branch, branchNames...))
if err := ui.Run(prompt); err != nil {
return fmt.Errorf("prompt: %w", err)
}
// The user selected a branch that is not the original branch
// so update the Change metadata to reflect the new branch.
if changeBranch != cmd.Branch {
transfer, err := prepareChangeMetadataTransfer(
ctx,
log,
repo,
store,
cmd.Branch,
changeBranch,
branch.Change,
branch.UpstreamBranch,
branchTx,
)
if err != nil {
return fmt.Errorf("transfer CR %v to %v: %w", branch.Change.ChangeID(), changeBranch, err)
}
// Perform the actual transfer only if the transaction succeeds.
defer func() {
if err == nil {
transfer()
}
}()
}
}
// State updates will probably succeed if we got here,
// so make the branch changes in the repo.
for idx, split := range cmd.At {
if err := repo.CreateBranch(ctx, git.CreateBranchRequest{
Name: split.Name,
Head: commitHashes[idx].String(),
}); err != nil {
return fmt.Errorf("create branch %q: %w", split.Name, err)
}
}
if err := branchTx.Commit(ctx, fmt.Sprintf("%v: split %d new branches", cmd.Branch, len(cmd.At))); err != nil {
return fmt.Errorf("update store: %w", err)
}
return nil
}
func prepareChangeMetadataTransfer(
ctx context.Context,
log *log.Logger,
repo *git.Repository,
store *state.Store,
fromBranch, toBranch string,
meta forge.ChangeMetadata,
upstreamBranch string,
tx *state.BranchTx,
) (transfer func(), _ error) {
forgeID := meta.ForgeID()
f, ok := forge.Lookup(forgeID)
if !ok {
return nil, fmt.Errorf("unknown forge: %v", forgeID)
}
remote, err := store.Remote()
if err != nil {
return nil, fmt.Errorf("get remote: %w", err)
}
metaJSON, err := f.MarshalChangeMetadata(meta)
if err != nil {
return nil, fmt.Errorf("marshal change metadata: %w", err)
}
// The original CR was pushed to this upstream branch name.
// The new branch will inherit this upstream branch name.
//
// However, if this name matches the original branch name (which it usually does),
// we'll want to warn the user that they should use a different name
// when they push it upstream.
toUpstreamBranch := cmp.Or(upstreamBranch, fromBranch)
var empty string
if err := tx.Upsert(ctx, state.UpsertRequest{
Name: fromBranch,
ChangeMetadata: state.Null,
UpstreamBranch: &empty,
}); err != nil {
return nil, fmt.Errorf("clear change metadata from %v: %w", fromBranch, err)
}
if err := tx.Upsert(ctx, state.UpsertRequest{
Name: toBranch,
ChangeMetadata: metaJSON,
ChangeForge: forgeID,
UpstreamBranch: &toUpstreamBranch,
}); err != nil {
return nil, fmt.Errorf("set change metadata on %v: %w", toBranch, err)
}
return func() {
if err := repo.SetBranchUpstream(ctx, toBranch, remote+"/"+toUpstreamBranch); err != nil {
log.Warnf("%v: Failed to set upstream branch %v: %v", toBranch, toUpstreamBranch, err)
}
if _, err := repo.BranchUpstream(ctx, fromBranch); err == nil {
if err := repo.SetBranchUpstream(ctx, fromBranch, ""); err != nil {
log.Warnf("%v: Failed to unset upstream branch %v: %v", fromBranch, upstreamBranch, err)
}
}
log.Infof("%v: Upstream branch '%v' transferred to '%v'", fromBranch, toUpstreamBranch, toBranch)
if toUpstreamBranch == fromBranch {
pushCmd := fmt.Sprintf("git push -u %v %v:<new name>", remote, fromBranch)
log.Warnf("%v: If you push this branch with 'git push' instead of 'gs branch submit',", fromBranch)
log.Warnf("%v: remember to use a different upstream branch name with the command:\n\t%s", fromBranch, _highlightStyle.Render(pushCmd))
}
}, nil
}
type branchSplit struct {
Commit string
Name string
}
func (b *branchSplit) Decode(ctx *kong.DecodeContext) error {
var spec string
if err := ctx.Scan.PopValueInto("at", &spec); err != nil {
return err
}
idx := strings.LastIndex(spec, ":")
switch {
case idx == -1:
return fmt.Errorf("expected COMMIT:NAME, got %q", spec)
case len(spec[:idx]) == 0:
return fmt.Errorf("part before : cannot be empty: %q", spec)
case len(spec[idx+1:]) == 0:
return fmt.Errorf("part after : cannot be empty: %q", spec)
}
b.Commit = spec[:idx]
b.Name = spec[idx+1:]
return nil
}