-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathcommit_amend.go
More file actions
214 lines (194 loc) · 6.28 KB
/
commit_amend.go
File metadata and controls
214 lines (194 loc) · 6.28 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
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" placeholder:"MSG" help:"Use the given message as the commit message."`
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"`
}
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.
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)
}
}
}
// Check if we're in the middle of a rebase with unmerged paths
var rebasing bool
if _, err := wt.RebaseState(ctx); err == nil {
rebasing = true
// If we're in the middle of a rebase,
// and there are unmerged paths,
// what the user likely wants is 'git add' and 'git-spice rebase continue'.
//
// (If there are no unmerged paths, amending is fine.)
var numUnmerged int
for _, err := range wt.ListFilesPaths(ctx, &git.ListFilesOptions{Unmerged: true}) {
if err == nil {
numUnmerged++
}
}
if numUnmerged > 0 {
if !ui.Interactive(view) {
log.Warnf("You are in the middle of a rebase with unmerged paths.")
log.Warnf(`You probably want resolve the conflicts and run "git add", then "%s rebase continue" instead.`, cli.Name())
} else {
var continueAmend bool
fields := []ui.Field{
ui.NewList[bool]().
WithTitle("Do you want to amend the commit?").
WithDescription(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())).
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{
Message: 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
}
return restackHandler.RestackUpstack(ctx, currentBranch, &restack.UpstackOptions{
SkipStart: true,
})
}