Files
gitea-tea/internal/terminal/merge_form.go
T
bircni 4895aa089d feat(pr): merge with the repository's defaults, ready, gh flags and ✓ lines for pr mutations
pr merge takes the method from the repository's default merge style
instead of always merging (D22), accepts gh's --merge, --squash and
--rebase next to -s/--method, refuses methods the repository disables,
and adds -d/--delete-branch (defaulting to the repository setting),
--auto, -t, -b, -F and -e. On a terminal it asks for the method and
branch deletion with the defaults preselected, without a picker when
the current branch has a pull request. It reports "✓ Squashed and
merged pull request alice/x#3 (title)", the deleted remote branch, and
switches off and deletes the local head branch; --json returns the
merged pull request. Merged, closed and draft pull requests are
reported before anything is asked. -s without a value points at
--squash.

pr checkout names the local branch after the head branch when that
name is free or already at the head, falling back to OWNER/BRANCH for
forks and pr/NUMBER (D13), fetches and switches quietly, reports
"✓ Checked out #1 into feat/one", and has the alias co. pr clean
reports what it deleted and that a remote branch was already gone.

pr create asks for the title and body, then "What's next?": submit,
submit as a draft, add metadata or change the branches, without the
AGit question up front or a final confirmation. It pushes an unpushed
head with --set-upstream --quiet ("✓ Pushed feat/one to origin"),
prints "✓ Created pull request alice/x#9 (title)" and the URL, and
adds --fill, --web, the alias new, the gh shorthands, and milestones
by title.

New tea pr ready [--undo]. pr close and reopen take -c/--comment,
close takes -d/--delete-branch, and both say when the pull request
already is in that state. pr edit reports "✓ Edited", takes the
singular gh reviewer flags (the plurals still work), milestones by
title and --remove-milestone. pr diff is colored and paged on a
terminal and byte-exact when piped, with --name-only, --color and
--web. Every pull request argument accepts #N, URLs and branch names
and defaults to the current branch's pull request.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-11 23:55:11 +02:00

89 lines
3.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package terminal
import (
"context"
"errors"
"fmt"
"charm.land/huh/v2"
)
// MergePullChoice is one selectable open pull request for interactive merge.
type MergePullChoice struct {
Number int64
Title, Head string
}
// MergeMethodLabels describe Gitea's merge styles in the merge form.
var MergeMethodLabels = map[string]string{
"merge": "Create a merge commit", "rebase": "Rebase and merge", "rebase-merge": "Rebase and create a merge commit",
"squash": "Squash and merge", "fast-forward-only": "Fast-forward only",
}
// MergeFormChoices describes the pull request to merge and what the form asks about it.
type MergeFormChoices struct {
// Header introduces the form, such as `Merge #3 "Merge me" (feat/merge-me → main) in alice/x`.
Header string
// Methods are the merge styles the repository allows; Method preselects one. Without Methods,
// the form does not ask for one.
Methods []string
Method string
// DeleteBranch preselects whether to delete the head branch; Branch, when set, asks it.
DeleteBranch bool
Branch string
}
// MergeFormValues are the chosen merge style and whether to delete the head branch.
type MergeFormValues struct {
Method string
DeleteBranch bool
}
// MergeForm collects which pull request to merge, and how.
type MergeForm interface {
SelectPull(context.Context, []MergePullChoice) (int64, error)
MergeOptions(context.Context, MergeFormChoices) (MergeFormValues, error)
}
// SelectPull prompts for a pull request on stderr.
func (p *FormPrompter) SelectPull(ctx context.Context, choices []MergePullChoice) (int64, error) {
if len(choices) == 0 {
return 0, errors.New("no open pull requests match the current branch")
}
options := make([]huh.Option[int64], 0, len(choices))
for _, choice := range choices {
options = append(options, huh.NewOption(SafeLine(fmt.Sprintf("#%d %s (%s)", choice.Number, choice.Title, choice.Head)), choice.Number))
}
selected := choices[0].Number
err := p.runForm(ctx, huh.NewForm(huh.NewGroup(
huh.NewSelect[int64]().Title("Pull request to merge:").Options(options...).Value(&selected),
)))
return selected, err
}
// MergeOptions asks for the merge style and whether to delete the head branch, on stderr.
func (p *FormPrompter) MergeOptions(ctx context.Context, choices MergeFormChoices) (MergeFormValues, error) {
values := MergeFormValues{Method: choices.Method, DeleteBranch: choices.DeleteBranch}
var fields []huh.Field
if len(choices.Methods) > 0 {
options := make([]huh.Option[string], len(choices.Methods))
for i, method := range choices.Methods {
options[i] = huh.NewOption(MergeMethodLabels[method], method)
}
fields = append(fields, huh.NewSelect[string]().Title("Merge method:").Options(options...).Value(&values.Method))
}
if choices.Branch != "" {
fields = append(fields, huh.NewConfirm().Title(SafeLine(fmt.Sprintf("Delete branch %s locally and on Gitea?", choices.Branch))).Value(&values.DeleteBranch))
}
if choices.Header != "" {
fmt.Fprintln(p.Streams.ErrOut, SafeLine(choices.Header))
}
if len(fields) == 0 {
return values, nil
}
return values, p.runForm(ctx, huh.NewForm(huh.NewGroup(fields...)))
}