mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 19:11:25 +01:00
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>
171 lines
6.7 KiB
Go
171 lines
6.7 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Branch configuration keys that record the pull request and base remote of a checked-out PR branch.
|
|
const (
|
|
pullNumberKey = "tea-pr"
|
|
pullBaseRemoteKey = "tea-base-remote"
|
|
)
|
|
|
|
// CheckoutOptions identifies the base remote and how to check out the PR commit.
|
|
type CheckoutOptions struct {
|
|
// CredentialArgs contains per-operation Git configuration for the fetch only.
|
|
CredentialArgs []string
|
|
Directory string
|
|
Remote string
|
|
Number int64
|
|
Branch string
|
|
Detached bool
|
|
Update bool
|
|
Prompt bool
|
|
ExpectedCommit string
|
|
// HeadRemote and HeadBranch select fork tracking when the head branch still exists.
|
|
// When HeadBranch is empty, checkout fetches the retained pull ref from Remote.
|
|
HeadRemote string
|
|
HeadBranch string
|
|
}
|
|
|
|
// CheckoutResult describes the selected branch or detached PR commit.
|
|
type CheckoutResult struct {
|
|
Detached bool `json:"detached"`
|
|
Branch string `json:"branch"`
|
|
Commit string `json:"commit"`
|
|
Remote string `json:"remote,omitempty"`
|
|
Tracking string `json:"tracking,omitempty"`
|
|
}
|
|
|
|
// CheckoutPull fetches a PR head and checks out its commit.
|
|
// When HeadRemote and HeadBranch are set, it fetches that head branch and configures tracking.
|
|
// Otherwise it fetches Gitea's retained pull ref from the base remote.
|
|
func CheckoutPull(ctx context.Context, runner Runner, options CheckoutOptions) (CheckoutResult, error) {
|
|
if options.Number < 1 || options.Remote == "" || (!options.Detached && options.Branch == "") {
|
|
return CheckoutResult{}, errors.New("checkout requires a pull number, base remote, and either a branch or detached mode")
|
|
}
|
|
if options.Detached && (options.Branch != "" || options.Update) {
|
|
return CheckoutResult{}, errors.New("detached checkout cannot select a branch or update an existing branch")
|
|
}
|
|
if options.HeadBranch != "" && options.HeadRemote == "" {
|
|
return CheckoutResult{}, errors.New("tracked checkout requires a head remote")
|
|
}
|
|
run := func(args ...string) (string, error) {
|
|
return runner.Output(ctx, options.Directory, options.Prompt, args...)
|
|
}
|
|
if err := requireWorkTree(run, "PR checkout"); err != nil {
|
|
return CheckoutResult{}, err
|
|
}
|
|
if !options.Detached {
|
|
if _, err := run("check-ref-format", "--branch", options.Branch); err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("invalid checkout branch: %w", err)
|
|
}
|
|
}
|
|
status, err := run("status", "--porcelain=v1", "--untracked-files=normal")
|
|
if err != nil {
|
|
return CheckoutResult{}, err
|
|
}
|
|
if status != "" {
|
|
return CheckoutResult{}, errors.New("working tree has changes; commit or stash them before PR checkout")
|
|
}
|
|
existing := false
|
|
previousCommit := ""
|
|
if !options.Detached {
|
|
branches, err := run("for-each-ref", "--format=%(refname)", "refs/heads/")
|
|
if err != nil {
|
|
return CheckoutResult{}, err
|
|
}
|
|
for branch := range strings.SplitSeq(branches, "\n") {
|
|
if branch == "refs/heads/"+options.Branch {
|
|
if options.ExpectedCommit == "" {
|
|
return CheckoutResult{}, fmt.Errorf("branch %q already exists; choose another checkout branch", options.Branch)
|
|
}
|
|
commit, err := run("rev-parse", "--verify", branch+"^{commit}")
|
|
if err != nil {
|
|
return CheckoutResult{}, err
|
|
}
|
|
if commit != options.ExpectedCommit && !options.Update {
|
|
return CheckoutResult{}, fmt.Errorf("branch %q already exists with a different commit; choose another checkout branch", options.Branch)
|
|
}
|
|
previousCommit = commit
|
|
existing = true
|
|
}
|
|
}
|
|
}
|
|
fetchRemote := options.Remote
|
|
fetchRef := fmt.Sprintf("refs/pull/%d/head", options.Number)
|
|
tracking := ""
|
|
if options.HeadBranch != "" {
|
|
if _, err := run("check-ref-format", "--branch", options.HeadBranch); err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("invalid head branch: %w", err)
|
|
}
|
|
fetchRemote = options.HeadRemote
|
|
fetchRef = "refs/heads/" + options.HeadBranch
|
|
tracking = options.HeadRemote + "/" + options.HeadBranch
|
|
}
|
|
fetchArgs := append([]string{}, options.CredentialArgs...)
|
|
fetchArgs = append(fetchArgs, "fetch", "--quiet", "--no-tags", "--", fetchRemote, fetchRef)
|
|
if _, err := run(fetchArgs...); err != nil {
|
|
return CheckoutResult{}, err
|
|
}
|
|
commit, err := run("rev-parse", "--verify", "FETCH_HEAD^{commit}")
|
|
if err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("PR was fetched, but its commit could not be resolved: %w", err)
|
|
}
|
|
if options.ExpectedCommit != "" && commit != options.ExpectedCommit {
|
|
return CheckoutResult{}, errors.New("PR head changed while fetching; retry checkout")
|
|
}
|
|
if existing {
|
|
localCommit, err := run("rev-parse", "--verify", "refs/heads/"+options.Branch+"^{commit}")
|
|
if err != nil {
|
|
return CheckoutResult{}, err
|
|
}
|
|
if localCommit != previousCommit {
|
|
return CheckoutResult{}, errors.New("local branch changed while fetching; retry checkout")
|
|
}
|
|
if localCommit != commit {
|
|
if _, err := run("merge-base", "--is-ancestor", localCommit, commit); err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("existing branch cannot be fast-forwarded; choose another checkout branch: %w", err)
|
|
}
|
|
}
|
|
}
|
|
switchArgs := []string{"switch", "--quiet", "--detach", commit}
|
|
if !options.Detached {
|
|
switchArgs = []string{"switch", "--quiet", "--create", options.Branch, commit}
|
|
if existing {
|
|
switchArgs = []string{"switch", "--quiet", options.Branch}
|
|
}
|
|
}
|
|
if _, err := run(switchArgs...); err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("PR was fetched, but checkout failed: %w", err)
|
|
}
|
|
if existing && previousCommit != commit {
|
|
if _, err := run("merge", "--ff-only", "--no-edit", commit); err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("checked out the existing branch, but its fast-forward failed: %w", err)
|
|
}
|
|
}
|
|
if tracking != "" && !options.Detached {
|
|
if _, err := run("config", "branch."+options.Branch+".remote", options.HeadRemote); err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("checked out the branch, but could not configure tracking: %w", err)
|
|
}
|
|
if _, err := run("config", "branch."+options.Branch+".merge", "refs/heads/"+options.HeadBranch); err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("checked out the branch, but could not configure tracking: %w", err)
|
|
}
|
|
}
|
|
if !options.Detached {
|
|
for _, setting := range [][2]string{{pullNumberKey, strconv.FormatInt(options.Number, 10)}, {pullBaseRemoteKey, options.Remote}} {
|
|
if _, err := run("config", "branch."+options.Branch+"."+setting[0], setting[1]); err != nil {
|
|
return CheckoutResult{}, fmt.Errorf("checked out the branch, but could not record its pull request: %w", err)
|
|
}
|
|
}
|
|
}
|
|
return CheckoutResult{Branch: options.Branch, Commit: commit, Detached: options.Detached, Remote: fetchRemote, Tracking: tracking}, nil
|
|
}
|