|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "cmp" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "go.abhg.dev/gs/internal/git" |
| 10 | + "go.abhg.dev/gs/internal/handler/cherrypick" |
| 11 | + "go.abhg.dev/gs/internal/silog" |
| 12 | + "go.abhg.dev/gs/internal/sliceutil" |
| 13 | + "go.abhg.dev/gs/internal/spice" |
| 14 | + "go.abhg.dev/gs/internal/text" |
| 15 | + "go.abhg.dev/gs/internal/ui" |
| 16 | + "go.abhg.dev/gs/internal/ui/widget" |
| 17 | +) |
| 18 | + |
| 19 | +type commitPickCmd struct { |
| 20 | + cherrypick.Options |
| 21 | + |
| 22 | + Commit string `arg:"" optional:"" help:"Commit to cherry-pick"` |
| 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 | + If it's not possible to cherry-pick the requested commit |
| 37 | + without causing a conflict, the command will fail. |
| 38 | + If the requested commit is a merge commit, |
| 39 | + the command will fail. |
| 40 | +
|
| 41 | + This command requires at least Git 2.45. |
| 42 | + `) |
| 43 | +} |
| 44 | + |
| 45 | +type CherryPickHandler interface { |
| 46 | + CherryPickCommit(ctx context.Context, req *cherrypick.Request) error |
| 47 | +} |
| 48 | + |
| 49 | +func (cmd *commitPickCmd) Run( |
| 50 | + ctx context.Context, |
| 51 | + log *silog.Logger, |
| 52 | + view ui.View, |
| 53 | + repo *git.Repository, |
| 54 | + wt *git.Worktree, |
| 55 | + svc *spice.Service, |
| 56 | + cherryPickHandler CherryPickHandler, |
| 57 | +) (err error) { |
| 58 | + branch, err := wt.CurrentBranch(ctx) |
| 59 | + if err != nil { |
| 60 | + if errors.Is(err, git.ErrDetachedHead) { |
| 61 | + return errors.New("cannot cherry-pick onto detached HEAD") |
| 62 | + } |
| 63 | + return fmt.Errorf("determine current branch: %w", err) |
| 64 | + } |
| 65 | + cmd.From = cmp.Or(cmd.From, branch) |
| 66 | + |
| 67 | + var commit git.Hash |
| 68 | + if cmd.Commit == "" { |
| 69 | + if !ui.Interactive(view) { |
| 70 | + return fmt.Errorf("no commit specified: %w", errNoPrompt) |
| 71 | + } |
| 72 | + |
| 73 | + commit, err = cmd.commitPrompt(ctx, log, view, repo, svc, branch) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("prompt for commit: %w", err) |
| 76 | + } |
| 77 | + } else { |
| 78 | + commit, err = repo.PeelToCommit(ctx, cmd.Commit) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("peel to commit: %w", err) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if branch == svc.Trunk() { |
| 85 | + if !ui.Interactive(view) { |
| 86 | + log.Warnf("You are about to cherry-pick commit %v on the trunk branch (%v).", commit.Short(), svc.Trunk()) |
| 87 | + } else { |
| 88 | + var pickOnTrunk bool |
| 89 | + prompt := ui.NewList[bool](). |
| 90 | + WithTitle("Do you want to cherry-pick on trunk?"). |
| 91 | + WithDescription(fmt.Sprintf("You are about to cherry-pick commit %v on the trunk branch (%v). "+ |
| 92 | + "This is usually not what you want to do.", commit.Short(), svc.Trunk())). |
| 93 | + WithItems( |
| 94 | + ui.ListItem[bool]{ |
| 95 | + Title: "Yes", |
| 96 | + Description: func(bool) string { |
| 97 | + return fmt.Sprintf("Cherry-pick commit %v on trunk", commit.Short()) |
| 98 | + }, |
| 99 | + Value: true, |
| 100 | + }, |
| 101 | + ui.ListItem[bool]{ |
| 102 | + Title: "No", |
| 103 | + Description: func(bool) string { |
| 104 | + return "Abort the operation" |
| 105 | + }, |
| 106 | + Value: false, |
| 107 | + }, |
| 108 | + ). |
| 109 | + WithValue(&pickOnTrunk) |
| 110 | + |
| 111 | + if err := ui.Run(view, prompt); err != nil { |
| 112 | + return fmt.Errorf("prompt: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + if !pickOnTrunk { |
| 116 | + return errors.New("operation aborted") |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + log.Debugf("Cherry-picking: %v", commit) |
| 122 | + return cherryPickHandler.CherryPickCommit(ctx, &cherrypick.Request{ |
| 123 | + Commit: commit, |
| 124 | + Branch: branch, |
| 125 | + Options: &cmd.Options, |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func (cmd *commitPickCmd) commitPrompt( |
| 130 | + ctx context.Context, |
| 131 | + log *silog.Logger, |
| 132 | + view ui.View, |
| 133 | + repo *git.Repository, |
| 134 | + svc *spice.Service, |
| 135 | + currentBranch string, |
| 136 | +) (git.Hash, error) { |
| 137 | + graph, err := svc.BranchGraph(ctx, nil) |
| 138 | + if err != nil { |
| 139 | + return "", fmt.Errorf("load branch graph: %w", err) |
| 140 | + } |
| 141 | + |
| 142 | + var totalCommits int |
| 143 | + shortToLongHash := make(map[git.Hash]git.Hash) |
| 144 | + var branches []widget.CommitPickBranch |
| 145 | + for name := range graph.Upstack(cmd.From) { |
| 146 | + if name == graph.Trunk() { |
| 147 | + continue |
| 148 | + } |
| 149 | + |
| 150 | + // TODO: build commit list for each branch concurrently |
| 151 | + b, ok := graph.Lookup(name) |
| 152 | + if !ok { |
| 153 | + continue // not really possible once past trunk |
| 154 | + } |
| 155 | + |
| 156 | + // If doing a --from=$other, |
| 157 | + // where $other is downstack from current, |
| 158 | + // we don't want to list commits for current branch, |
| 159 | + // so add an empty entry for it. |
| 160 | + if name == currentBranch { |
| 161 | + branches = append(branches, widget.CommitPickBranch{ |
| 162 | + Branch: name, |
| 163 | + Base: b.Base, |
| 164 | + }) |
| 165 | + continue |
| 166 | + } |
| 167 | + |
| 168 | + // TODO: parallel fetching? |
| 169 | + commits, err := sliceutil.CollectErr(repo.ListCommitsDetails(ctx, |
| 170 | + git.CommitRangeFrom(b.Head). |
| 171 | + ExcludeFrom(b.BaseHash). |
| 172 | + FirstParent())) |
| 173 | + if err != nil { |
| 174 | + log.Warn("Could not list commits for branch. Skipping.", |
| 175 | + "branch", name, "error", err) |
| 176 | + continue |
| 177 | + } |
| 178 | + |
| 179 | + commitSummaries := make([]widget.CommitSummary, len(commits)) |
| 180 | + for i, c := range commits { |
| 181 | + commitSummaries[i] = widget.CommitSummary{ |
| 182 | + ShortHash: c.ShortHash, |
| 183 | + Subject: c.Subject, |
| 184 | + AuthorDate: c.AuthorDate, |
| 185 | + } |
| 186 | + shortToLongHash[c.ShortHash] = c.Hash |
| 187 | + } |
| 188 | + |
| 189 | + branches = append(branches, widget.CommitPickBranch{ |
| 190 | + Branch: name, |
| 191 | + Base: b.Base, |
| 192 | + Commits: commitSummaries, |
| 193 | + }) |
| 194 | + totalCommits += len(commitSummaries) |
| 195 | + } |
| 196 | + |
| 197 | + if totalCommits == 0 { |
| 198 | + log.Warn("Please provide a commit hash to cherry pick from.") |
| 199 | + return "", fmt.Errorf("upstack of %v does not have any commits to cherry-pick", cmd.From) |
| 200 | + } |
| 201 | + |
| 202 | + msg := fmt.Sprintf("Selected commit will be cherry-picked into %v", currentBranch) |
| 203 | + var selected git.Hash |
| 204 | + prompt := widget.NewCommitPick(). |
| 205 | + WithTitle("Pick a commit"). |
| 206 | + WithDescription(msg). |
| 207 | + WithBranches(branches...). |
| 208 | + WithValue(&selected) |
| 209 | + if err := ui.Run(view, prompt); err != nil { |
| 210 | + return "", err |
| 211 | + } |
| 212 | + |
| 213 | + if long, ok := shortToLongHash[selected]; ok { |
| 214 | + // This will always be true but it doesn't hurt |
| 215 | + // to be defensive here. |
| 216 | + selected = long |
| 217 | + } |
| 218 | + return selected, nil |
| 219 | +} |
0 commit comments