-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcommit_pick.go
184 lines (162 loc) · 4.94 KB
/
commit_pick.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
package main
import (
"cmp"
"context"
"fmt"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/git"
"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"
"go.abhg.dev/gs/internal/ui/widget"
)
type commitPickCmd struct {
Commit string `arg:"" optional:"" help:"Commit to cherry-pick"`
// TODO: Support multiple commits similarly to git cherry-pick.
Edit bool `default:"false" negatable:"" config:"commitPick.edit" help:"Whether to open an editor to edit the commit message."`
From string `placeholder:"NAME" predictor:"trackedBranches" help:"Branch whose upstack commits will be considered."`
}
func (*commitPickCmd) Help() string {
return text.Dedent(`
Apply the changes introduced by a commit to the current branch
and restack the upstack branches.
If a commit is not specified, a prompt will allow picking
from commits of upstack branches of the current branch.
Use the --from option to pick a commit from a different branch
or its upstack.
By default, commit messages for cherry-picked commits will be used verbatim.
Supply --edit to open an editor and change the commit message,
or set the spice.commitPick.edit configuration option to true
to always open an editor for cherry picks.
`)
}
func (cmd *commitPickCmd) Run(
ctx context.Context,
log *log.Logger,
view ui.View,
repo *git.Repository,
store *state.Store,
svc *spice.Service,
) (err error) {
var commit git.Hash
if cmd.Commit == "" {
if !ui.Interactive(view) {
return fmt.Errorf("no commit specified: %w", errNoPrompt)
}
commit, err = cmd.commitPrompt(ctx, log, view, repo, store, svc)
if err != nil {
return fmt.Errorf("prompt for commit: %w", err)
}
} else {
commit, err = repo.PeelToCommit(ctx, cmd.Commit)
if err != nil {
return fmt.Errorf("peel to commit: %w", err)
}
}
log.Debugf("Cherry-picking: %v", commit)
err = repo.CherryPick(ctx, git.CherryPickRequest{
Commits: []git.Hash{commit},
Edit: cmd.Edit,
// If you selected an empty commit,
// you probably want to retain that.
// This still won't allow for no-op cherry-picks.
AllowEmpty: true,
})
if err != nil {
return fmt.Errorf("cherry-pick: %w", err)
}
// TODO: cherry-pick the commit
// TODO: handle --continue/--abort
// TODO: upstack restack
return nil
}
func (cmd *commitPickCmd) commitPrompt(
ctx context.Context,
log *log.Logger,
view ui.View,
repo *git.Repository,
store *state.Store,
svc *spice.Service,
) (git.Hash, error) {
currentBranch, err := repo.CurrentBranch(ctx)
if err != nil {
// TODO: allow for cherry-pick onto non-branch HEAD.
return "", fmt.Errorf("determine current branch: %w", err)
}
cmd.From = cmp.Or(cmd.From, currentBranch)
upstack, err := svc.ListUpstack(ctx, cmd.From)
if err != nil {
return "", fmt.Errorf("list upstack branches: %w", err)
}
var totalCommits int
branches := make([]widget.CommitPickBranch, 0, len(upstack))
shortToLongHash := make(map[git.Hash]git.Hash)
for _, name := range upstack {
if name == store.Trunk() {
continue
}
// TODO: build commit list for each branch concurrently
b, err := svc.LookupBranch(ctx, name)
if err != nil {
log.Warn("Could not look up branch. Skipping.",
"branch", name, "error", err)
continue
}
// If doing a --from=$other,
// where $other is downstack from current,
// we don't want to list commits for current branch,
// so add an empty entry for it.
if name == currentBranch {
// Don't list the current branch's commits.
branches = append(branches, widget.CommitPickBranch{
Branch: name,
Base: b.Base,
})
continue
}
commits, err := 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)
}
commitSummaries := make([]widget.CommitSummary, len(commits))
for i, c := range commits {
commitSummaries[i] = widget.CommitSummary{
ShortHash: c.ShortHash,
Subject: c.Subject,
AuthorDate: c.AuthorDate,
}
shortToLongHash[c.ShortHash] = c.Hash
}
branches = append(branches, widget.CommitPickBranch{
Branch: name,
Base: b.Base,
Commits: commitSummaries,
})
totalCommits += len(commitSummaries)
}
if totalCommits == 0 {
log.Warn("Please provide a commit hash to cherry pick from.")
return "", fmt.Errorf("upstack of %v does not have any commits to cherry-pick", cmd.From)
}
msg := fmt.Sprintf("Selected commit will be cherry-picked into %v", currentBranch)
var selected git.Hash
prompt := widget.NewCommitPick().
WithTitle("Pick a commit").
WithDescription(msg).
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
}
return selected, nil
}