|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "cmp" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "go.abhg.dev/gs/internal/git" |
| 9 | + "go.abhg.dev/gs/internal/silog" |
| 10 | + "go.abhg.dev/gs/internal/sliceutil" |
| 11 | + "go.abhg.dev/gs/internal/spice" |
| 12 | + "go.abhg.dev/gs/internal/spice/state" |
| 13 | + "go.abhg.dev/gs/internal/text" |
| 14 | + "go.abhg.dev/gs/internal/ui" |
| 15 | + "go.abhg.dev/gs/internal/ui/widget" |
| 16 | +) |
| 17 | + |
| 18 | +type commitPickCmd struct { |
| 19 | + Commit string `arg:"" optional:"" help:"Commit to cherry-pick"` |
| 20 | + // TODO: Support multiple commits similarly to git cherry-pick. |
| 21 | + |
| 22 | + Edit bool `default:"false" negatable:"" config:"commitPick.edit" help:"Whether to open an editor to edit the commit message."` |
| 23 | + From string `placeholder:"NAME" predictor:"trackedBranches" help:"Branch whose upstack commits will be considered."` |
| 24 | +} |
| 25 | + |
| 26 | +func (*commitPickCmd) Help() string { |
| 27 | + return text.Dedent(` |
| 28 | + Apply the changes introduced by a commit to the current branch |
| 29 | + and restack the upstack branches. |
| 30 | +
|
| 31 | + If a commit is not specified, a prompt will allow picking |
| 32 | + from commits of upstack branches of the current branch. |
| 33 | + Use the --from option to pick a commit from a different branch |
| 34 | + or its upstack. |
| 35 | +
|
| 36 | + By default, commit messages for cherry-picked commits will be used verbatim. |
| 37 | + Supply --edit to open an editor and change the commit message, |
| 38 | + or set the spice.commitPick.edit configuration option to true |
| 39 | + to always open an editor for cherry picks. |
| 40 | + `) |
| 41 | +} |
| 42 | + |
| 43 | +func (cmd *commitPickCmd) Run( |
| 44 | + ctx context.Context, |
| 45 | + log *silog.Logger, |
| 46 | + view ui.View, |
| 47 | + repo *git.Repository, |
| 48 | + store *state.Store, |
| 49 | + svc *spice.Service, |
| 50 | +) (err error) { |
| 51 | + var commit git.Hash |
| 52 | + if cmd.Commit == "" { |
| 53 | + if !ui.Interactive(view) { |
| 54 | + return fmt.Errorf("no commit specified: %w", errNoPrompt) |
| 55 | + } |
| 56 | + |
| 57 | + commit, err = cmd.commitPrompt(ctx, log, view, repo, store, svc) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("prompt for commit: %w", err) |
| 60 | + } |
| 61 | + } else { |
| 62 | + commit, err = repo.PeelToCommit(ctx, cmd.Commit) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("peel to commit: %w", err) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + log.Debugf("Cherry-picking: %v", commit) |
| 69 | + err = repo.CherryPick(ctx, git.CherryPickRequest{ |
| 70 | + Commits: []git.Hash{commit}, |
| 71 | + Edit: cmd.Edit, |
| 72 | + // If you selected an empty commit, |
| 73 | + // you probably want to retain that. |
| 74 | + // This still won't allow for no-op cherry-picks. |
| 75 | + AllowEmpty: true, |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + return fmt.Errorf("cherry-pick: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + // TODO: cherry-pick the commit |
| 82 | + // TODO: handle --continue/--abort |
| 83 | + // TODO: upstack restack |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func (cmd *commitPickCmd) commitPrompt( |
| 88 | + ctx context.Context, |
| 89 | + log *silog.Logger, |
| 90 | + view ui.View, |
| 91 | + repo *git.Repository, |
| 92 | + store *state.Store, |
| 93 | + svc *spice.Service, |
| 94 | +) (git.Hash, error) { |
| 95 | + currentBranch, err := repo.CurrentBranch(ctx) |
| 96 | + if err != nil { |
| 97 | + // TODO: allow for cherry-pick onto non-branch HEAD. |
| 98 | + return "", fmt.Errorf("determine current branch: %w", err) |
| 99 | + } |
| 100 | + cmd.From = cmp.Or(cmd.From, currentBranch) |
| 101 | + |
| 102 | + upstack, err := svc.ListUpstack(ctx, cmd.From) |
| 103 | + if err != nil { |
| 104 | + return "", fmt.Errorf("list upstack branches: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + var totalCommits int |
| 108 | + branches := make([]widget.CommitPickBranch, 0, len(upstack)) |
| 109 | + shortToLongHash := make(map[git.Hash]git.Hash) |
| 110 | + for _, name := range upstack { |
| 111 | + if name == store.Trunk() { |
| 112 | + continue |
| 113 | + } |
| 114 | + |
| 115 | + // TODO: build commit list for each branch concurrently |
| 116 | + b, err := svc.LookupBranch(ctx, name) |
| 117 | + if err != nil { |
| 118 | + log.Warn("Could not look up branch. Skipping.", |
| 119 | + "branch", name, "error", err) |
| 120 | + continue |
| 121 | + } |
| 122 | + |
| 123 | + // If doing a --from=$other, |
| 124 | + // where $other is downstack from current, |
| 125 | + // we don't want to list commits for current branch, |
| 126 | + // so add an empty entry for it. |
| 127 | + if name == currentBranch { |
| 128 | + // Don't list the current branch's commits. |
| 129 | + branches = append(branches, widget.CommitPickBranch{ |
| 130 | + Branch: name, |
| 131 | + Base: b.Base, |
| 132 | + }) |
| 133 | + continue |
| 134 | + } |
| 135 | + |
| 136 | + commits, err := sliceutil.CollectErr(repo.ListCommitsDetails(ctx, |
| 137 | + git.CommitRangeFrom(b.Head). |
| 138 | + ExcludeFrom(b.BaseHash). |
| 139 | + FirstParent())) |
| 140 | + if err != nil { |
| 141 | + log.Warn("Could not list commits for branch. Skipping.", |
| 142 | + "branch", name, "error", err) |
| 143 | + continue |
| 144 | + } |
| 145 | + |
| 146 | + commitSummaries := make([]widget.CommitSummary, len(commits)) |
| 147 | + for i, c := range commits { |
| 148 | + commitSummaries[i] = widget.CommitSummary{ |
| 149 | + ShortHash: c.ShortHash, |
| 150 | + Subject: c.Subject, |
| 151 | + AuthorDate: c.AuthorDate, |
| 152 | + } |
| 153 | + shortToLongHash[c.ShortHash] = c.Hash |
| 154 | + } |
| 155 | + |
| 156 | + branches = append(branches, widget.CommitPickBranch{ |
| 157 | + Branch: name, |
| 158 | + Base: b.Base, |
| 159 | + Commits: commitSummaries, |
| 160 | + }) |
| 161 | + totalCommits += len(commitSummaries) |
| 162 | + } |
| 163 | + |
| 164 | + if totalCommits == 0 { |
| 165 | + log.Warn("Please provide a commit hash to cherry pick from.") |
| 166 | + return "", fmt.Errorf("upstack of %v does not have any commits to cherry-pick", cmd.From) |
| 167 | + } |
| 168 | + |
| 169 | + msg := fmt.Sprintf("Selected commit will be cherry-picked into %v", currentBranch) |
| 170 | + var selected git.Hash |
| 171 | + prompt := widget.NewCommitPick(). |
| 172 | + WithTitle("Pick a commit"). |
| 173 | + WithDescription(msg). |
| 174 | + WithBranches(branches...). |
| 175 | + WithValue(&selected) |
| 176 | + if err := ui.Run(view, prompt); err != nil { |
| 177 | + return "", err |
| 178 | + } |
| 179 | + |
| 180 | + if long, ok := shortToLongHash[selected]; ok { |
| 181 | + // This will always be true but it doesn't hurt |
| 182 | + // to be defensive here. |
| 183 | + selected = long |
| 184 | + } |
| 185 | + return selected, nil |
| 186 | +} |
0 commit comments