-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcommit_amend.go
More file actions
259 lines (235 loc) · 7.93 KB
/
Copy pathcommit_amend.go
File metadata and controls
259 lines (235 loc) · 7.93 KB
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
package main
import (
"context"
"errors"
"fmt"
"go.abhg.dev/gs/internal/cli"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/handler/restack"
"go.abhg.dev/gs/internal/silog"
"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 commitAmendCmd struct {
branchCreateConfig // TODO: find a way to avoid this
All bool `short:"a" help:"Stage all changes before committing."`
AllowEmpty bool `help:"Create a commit even if it contains no changes."`
Message []string `short:"m" xor:"commit-message-source" sep:"none" placeholder:"MSG" help:"Use the given message as the commit message. May be repeated to add multiple paragraphs."`
MessageFile string `short:"F" xor:"commit-message-source" placeholder:"FILE" help:"Read the commit message from the given file."`
NoEdit bool `help:"Don't edit the commit message"`
NoVerify bool `help:"Bypass pre-commit and commit-msg hooks."`
Signoff bool `config:"commit.signoff" help:"Add Signed-off-by trailer to the commit message"`
Restack spice.AutoRestackMode `negatable:"" default:"upstack" config:"commitAmend.restack" enum:"none,upstack" help:"Whether to restack upstack branches."`
}
func (*commitAmendCmd) Help() string {
name := cli.Name()
return text.Dedent(fmt.Sprintf(`
Staged changes are amended into the topmost commit.
Branches upstack are restacked if necessary.
Use --no-restack to disable automatic restacking,
or the 'spice.commitAmend.restack' configuration option
to change the default.
This is a shortcut for 'git commit --amend'
followed by '%[1]s upstack restack'.
Use '%[1]s commit fixup' to amend commits
that are further downstack.
An editor is opened to edit the commit message
unless the --no-edit flag is given.
Use the -m/--message or -F/--file option
to specify the message without opening an editor.
Git hooks are run unless the --no-verify flag is given.
Use the -a/--all flag to stage all changes before committing.
To prevent accidental amends on the trunk branch,
a prompt will require confirmation when amending on trunk.
The --no-prompt flag can be used to skip this prompt in scripts.
`, name))
}
func (cmd *commitAmendCmd) Run(
ctx context.Context,
log *silog.Logger,
view ui.View,
repo *git.Repository,
wt *git.Worktree,
store *state.Store,
svc *spice.Service,
restackHandler RestackHandler,
) error {
var detachedHead bool
currentBranch, err := wt.CurrentBranch(ctx)
if err != nil {
if !errors.Is(err, git.ErrDetachedHead) {
return fmt.Errorf("get current branch: %w", err)
}
detachedHead = true
currentBranch = ""
}
if currentBranch == store.Trunk() {
if !ui.Interactive(view) {
log.Warnf("You are about to amend a commit on the trunk branch (%v).", store.Trunk())
} else {
var (
amendOnTrunk bool
branchName string
)
fields := []ui.Field{
ui.NewList[bool]().
WithTitle("Do you want to amend a commit on trunk?").
WithDescription(fmt.Sprintf("You are about to amend a commit on the trunk branch (%v). "+
"This is usually not what you want to do.", store.Trunk())).
WithItems(
ui.ListItem[bool]{
Title: "Yes",
Description: func(ui.Theme, bool) string {
return "Amend the commit on trunk"
},
Value: true,
},
ui.ListItem[bool]{
Title: "No",
Description: func(ui.Theme, bool) string {
return "Create a branch and commit there instead"
},
Value: false,
},
).
WithValue(&amendOnTrunk),
ui.Defer(func() ui.Field {
if amendOnTrunk {
return nil
}
return ui.NewInput().
WithTitle("Branch name").
WithDescription("What do you want to call the new branch?").
WithValue(&branchName)
}),
}
if err := ui.Run(view, fields...); err != nil {
return fmt.Errorf("run prompt: %w", err)
}
if !amendOnTrunk {
// TODO: shared commitOptions struct?
return (&branchCreateCmd{
branchCreateConfig: cmd.branchCreateConfig,
Name: branchName,
All: cmd.All,
NoVerify: cmd.NoVerify,
Message: cmd.Message,
MessageFile: cmd.MessageFile,
Signoff: cmd.Signoff,
Commit: true,
}).Run(ctx, log, repo, wt, store, svc, restackHandler)
}
}
}
// rebaseAmendWarning carries the text for one warning before amend.
type rebaseAmendWarning struct {
logTitle string
logSuggestion string
promptDescription string
}
var rebaseWarning *rebaseAmendWarning
var rebasing bool
if _, err := wt.RebaseState(ctx); err == nil {
rebasing = true
// If we're in the middle of a rebase and Git still has
// unmerged paths, the user likely wants to resolve conflicts,
// stage them, and run 'git-spice rebase continue'.
var numUnmerged int
for _, err := range wt.ListFilesPaths(ctx, &git.ListFilesOptions{Unmerged: true}) {
if err == nil {
numUnmerged++
}
}
if numUnmerged > 0 {
rebaseWarning = &rebaseAmendWarning{
logTitle: "You are in the middle of a rebase with unmerged paths.",
logSuggestion: fmt.Sprintf(`You probably want resolve the conflicts and run "git add", then "%s rebase continue" instead.`, cli.Name()),
promptDescription: fmt.Sprintf("You are in the middle of a rebase with unmerged paths.\n"+
"You might want to resolve the conflicts and run 'git add', then '%s rebase continue' instead.", cli.Name()),
}
} else {
// Once conflicts have been staged,
// unmerged paths are gone but the rebase is still waiting
// for 'git-spice rebase continue'.
//
// Deliberate stops like edit, reword, break, or exec are
// allowed to amend here; RebaseStopReason separates those
// from conflict-resolution stops.
stopReason := git.RebaseInterruptConflict
if r, err := wt.RebaseStopReason(ctx); err == nil {
stopReason = r
}
if stopReason == git.RebaseInterruptConflict {
rebaseWarning = &rebaseAmendWarning{
logTitle: "You appear to have resolved a rebase conflict.",
logSuggestion: fmt.Sprintf(`You probably want "%s rebase continue" instead of amending.`, cli.Name()),
promptDescription: fmt.Sprintf("You appear to have resolved a rebase conflict.\n"+
"You might want to run '%s rebase continue' instead of amending.", cli.Name()),
}
}
}
}
if rebaseWarning != nil {
if !ui.Interactive(view) {
log.Warnf("%s", rebaseWarning.logTitle)
log.Warnf("%s", rebaseWarning.logSuggestion)
} else {
var continueAmend bool
fields := []ui.Field{
ui.NewList[bool]().
WithTitle("Do you want to amend the commit?").
WithDescription(rebaseWarning.promptDescription).
WithItems(
ui.ListItem[bool]{
Title: "Yes",
Description: func(ui.Theme, bool) string { return "Continue with commit amend" },
Value: true,
},
ui.ListItem[bool]{
Title: "No",
Description: func(ui.Theme, bool) string { return "Abort the operation" },
Value: false,
},
).
WithValue(&continueAmend),
}
if err := ui.Run(view, fields...); err != nil {
return fmt.Errorf("run prompt: %w", err)
}
if !continueAmend {
return errors.New("operation aborted")
}
}
}
if err := wt.Commit(ctx, git.CommitRequest{
Messages: cmd.Message,
MessageFile: cmd.MessageFile,
AllowEmpty: cmd.AllowEmpty,
Amend: true,
NoEdit: cmd.NoEdit,
NoVerify: cmd.NoVerify,
All: cmd.All,
Signoff: cmd.Signoff,
}); err != nil {
return fmt.Errorf("commit: %w", err)
}
if rebasing {
log.Debug("A rebase is in progress, skipping restack")
return nil
}
if detachedHead {
log.Debug("HEAD is detached, skipping restack")
return nil
}
if cmd.Restack.None() {
return nil
}
return restackHandler.RestackUpstack(ctx, &restack.UpstackRequest{
Branch: currentBranch,
Options: &restack.UpstackOptions{
SkipStart: true,
},
})
}