-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathcommit_fixup.go
More file actions
298 lines (263 loc) · 7.83 KB
/
commit_fixup.go
File metadata and controls
298 lines (263 loc) · 7.83 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
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
package main
import (
"context"
"errors"
"fmt"
"runtime"
"sync"
"github.com/alecthomas/kong"
"go.abhg.dev/gs/internal/git"
"go.abhg.dev/gs/internal/handler/autostash"
"go.abhg.dev/gs/internal/handler/fixup"
"go.abhg.dev/gs/internal/must"
"go.abhg.dev/gs/internal/silog"
"go.abhg.dev/gs/internal/sliceutil"
"go.abhg.dev/gs/internal/spice"
"go.abhg.dev/gs/internal/text"
"go.abhg.dev/gs/internal/ui"
"go.abhg.dev/gs/internal/ui/commit"
"go.abhg.dev/gs/internal/ui/widget"
)
type commitFixupCmd struct {
fixup.Options
Commit string `arg:"" optional:"" help:"The commit to fixup. Must be reachable from the HEAD commit."`
}
func (cmd *commitFixupCmd) Help() string {
return text.Dedent(`
Apply staged uncommited changes to another commit
down the stack, and restack the rest of the stack on top of it.
If a commit is not specified, a prompt is shown to select one.
If the commit is specified,
it must be reachable from the current commit,
(i.e. it must be down the stack).
If it's not possible to apply the staged changes to the commit
without causing a conflict, the command will fail.
This command requires at least Git 2.45.
`)
}
type FixupHandler interface {
FixupCommit(ctx context.Context, req *fixup.Request) error
}
func (cmd *commitFixupCmd) AfterApply(kctx *kong.Context) error {
return kctx.BindToProvider(func(
log *silog.Logger,
repo *git.Repository,
wt *git.Worktree,
svc *spice.Service,
restackHandler RestackHandler,
) (FixupHandler, error) {
return &fixup.Handler{
Log: log,
Worktree: wt,
Repository: repo,
Service: svc,
Restack: restackHandler,
}, nil
})
}
func (cmd *commitFixupCmd) Run(
ctx context.Context,
log *silog.Logger,
view ui.View,
repo *git.Repository,
svc *spice.Service,
wt *git.Worktree,
handler FixupHandler,
autostashHandler AutostashHandler,
) (retErr error) {
// TODO: Should we do a Git version check here?
// git --version output is relatively stable.
currentBranch, err := wt.CurrentBranch(ctx)
if err != nil {
if errors.Is(err, git.ErrDetachedHead) {
// TODO: To support fixup from detached HEAD,
// we'll need to know what the rebased commit HEAD is.
return errors.New("HEAD is detached; cannot fixup commit")
}
return fmt.Errorf("determine current branch: %w", err)
}
var (
commitHash git.Hash
commitBranch string
)
if cmd.Commit != "" {
var err error
commitHash, err = wt.PeelToCommit(ctx, cmd.Commit)
if err != nil {
return fmt.Errorf("not a commit: %q: %w", cmd.Commit, err)
}
if string(commitHash) != cmd.Commit {
log.Debugf("Commit resolved: %v", commitHash)
}
} else {
if !ui.Interactive(view) {
return fmt.Errorf("no commit specified: %w", errNoPrompt)
}
var err error
commitBranch, commitHash, err = cmd.commitPrompt(ctx, log, view, repo, svc, currentBranch)
if err != nil {
return fmt.Errorf("prompt for commit: %w", err)
}
}
must.NotBeBlankf(commitHash, "commit hash not specified, nor set in prompt")
head, err := wt.Head(ctx)
if err != nil {
return fmt.Errorf("determine HEAD: %w", err)
}
// Target commit must be an ancestor of HEAD.
if !repo.IsAncestor(ctx, commitHash, head) {
log.Errorf("Target commit (%v) is not reachable from HEAD (%v)", commitHash, head)
return errors.New("fixup commit must be an ancestor of HEAD")
}
// But it must be more recent than trunk.
//
// TODO:
// Non-restack version of this command that works for detached HEAD
// would also support fixing up commits that are already in trunk.
if trunkHash, err := repo.PeelToCommit(ctx, svc.Trunk()); err == nil {
if repo.IsAncestor(ctx, commitHash, trunkHash) {
log.Errorf("Target commit (%v) is already in trunk (%v)", commitHash, trunkHash)
return errors.New("cannot fixup a commit that has been merged into trunk")
}
}
// There must be something to commit.
if diff, err := wt.DiffIndex(ctx, head.String()); err != nil {
return fmt.Errorf("diff index: %w", err)
} else if len(diff) == 0 {
return errors.New("no changes staged for commit")
}
// Delay autostash until validation is complete so that
// rejections leave unstaged worktree changes untouched.
cleanup, err := autostashHandler.BeginAutostash(ctx, &autostash.Options{
Message: "git-spice: autostash before commit fixup",
ResetMode: autostash.ResetWorktree,
Branch: currentBranch,
})
if err != nil {
return err
}
defer func() {
if retErr == nil {
if err := wt.CheckoutBranch(ctx, currentBranch); err != nil {
retErr = fmt.Errorf("restore original branch %q: %w", currentBranch, err)
}
}
cleanup(&retErr, nil)
}()
req := &fixup.Request{
Options: &cmd.Options,
TargetHash: commitHash,
TargetBranch: commitBranch,
HeadBranch: currentBranch,
}
if err := handler.FixupCommit(ctx, req); err != nil {
// If the fixup fails because of a rebase conflict,
// after the conflict is resolved and other operations done
// (e.g. restack), we want to return to the original branch.
return svc.RebaseRescue(ctx, spice.RebaseRescueRequest{
Err: err,
Branch: currentBranch,
Command: []string{"branch", "checkout", currentBranch},
Message: fmt.Sprintf("fixup commit %s", commitHash),
})
}
return nil
}
func (cmd *commitFixupCmd) commitPrompt(
ctx context.Context,
log *silog.Logger,
view ui.View,
repo *git.Repository,
svc *spice.Service,
currentBranch string,
) (string, git.Hash, error) {
graph, err := svc.BranchGraph(ctx, nil)
if err != nil {
return "", "", fmt.Errorf("load branch graph: %w", err)
}
var (
mu sync.Mutex
wg sync.WaitGroup
totalCommits int
)
var branches []widget.CommitPickBranch
shortToLongHash := make(map[git.Hash]git.Hash)
longHashToBranch := make(map[git.Hash]string)
branchc := make(chan string)
for range runtime.GOMAXPROCS(0) {
wg.Go(func() {
for name := range branchc {
// TODO:
// Awful lot of duplication here
// with how list.Handler works.
// Might want to re-use that here somehow.
//
// Or push commit range listing into graph.
b, ok := graph.Lookup(name)
if !ok {
log.Warn("Could not look up branch. Skipping.",
"branch", name, "error", err)
continue
}
commits, err := sliceutil.CollectErr(repo.ListCommitsDetails(ctx,
git.CommitRangeFrom(b.Head).
ExcludeFrom(b.BaseHash).
FirstParent()))
if err != nil {
log.Warn("Could not list commits for branch. Skipping.",
"branch", name, "error", err)
continue
}
if len(commits) == 0 {
continue
}
commitSummaries := make([]commit.Summary, len(commits))
mu.Lock()
for i, c := range commits {
commitSummaries[i] = commit.Summary{
ShortHash: c.ShortHash,
Subject: c.Subject,
AuthorDate: c.AuthorDate,
}
shortToLongHash[c.ShortHash] = c.Hash
longHashToBranch[c.Hash] = name
}
branches = append(branches, widget.CommitPickBranch{
Branch: name,
Base: b.Base,
Commits: commitSummaries,
})
totalCommits += len(commitSummaries)
mu.Unlock()
}
})
}
for name := range graph.Downstack(currentBranch) {
if name == graph.Trunk() {
continue
}
branchc <- name
}
close(branchc)
wg.Wait()
if totalCommits == 0 {
return "", "", fmt.Errorf("downstack of %v does not have any commits to cherry-pick", currentBranch)
}
var selected git.Hash
prompt := widget.NewCommitPick().
WithTitle("Pick a commit").
WithDescription("Staged changes will be applied to this commit.").
WithBranches(branches...).
WithValue(&selected)
if err := ui.Run(view, prompt); err != nil {
return "", "", err
}
if long, ok := shortToLongHash[selected]; ok {
// This will always be true but it doesn't hurt
// to be defensive here.
selected = long
}
branch, ok := longHashToBranch[selected]
must.Bef(ok, "selected commit %s has no branch associated with it", selected)
return branch, selected, nil
}