mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
- **switch tea to the Cobra command tree:** `internal/app` wires the packages from parts 1–4 into explicit noun/verb commands with `--repo`, `--json` / `--jq`, `--sudo` and `--debug`; `main.go` points at it. Build, release and docs tooling (`tools/cli-docs`, Makefile, goreleaser, snap, CI) and README / CONTRIBUTING are updated. - **remove the urfave/cli command tree:** deletes `cmd/`, `modules/`, the old integration tests, contrib completion scripts and the generated `docs/CLI.md`, and drops their dependencies. This commit only deletes. ## Breaking changes This is a breaking release. It requires Gitea 28 or newer; configuration moves to separate `config.yml` and `hosts.yml` files and requires fresh authentication, and command names, aliases, flags and output formats change. `docs/migration-1.0.md` maps every old command, flag and environment variable to its replacement. ## Issues fixes #1118 fixes #1116 fixes #1115 fixes #1094 fixes #1091 fixes #1073 fixes #1052 fixes #1032 fixes #1023 fixes #966 fixes #908 fixes #833 fixes #831 fixes #817 fixes #804 fixes #779 fixes #770 fixes #737 fixes #697 fixes #726 fixes #677 fixes #672 fixes #633 fixes #632 fixes #560 fixes #556 fixes #523 fixes #516 fixes #510 fixes #504 fixes #500 fixes #482 fixes #406 fixes #403 fixes #373 fixes #351 fixes #344 fixes #326 fixes #325 fixes #323 fixes #246 fixes #240 fixes #232 fixes #197 fixes #172 fixes #141 fixes #69 fixes #1110 fixes #569 fixes #480 fixes #666 fixes #670 fixes #457 fixes #643 fixes #498 fixes #490 fixes #1069 fixes #834 fixes #602 fixes #328 fixes #417 fixes #571 fixes #1029 fixes #489 fixes #1050 fixes #162 fixes #430 fixes #1087 fixes #683 Reviewed-on: https://gitea.com/gitea/tea/pulls/1126 Reviewed-by: TheFox0x7 <95654+thefox0x7@noreply.gitea.com>
208 lines
7.3 KiB
Go
208 lines
7.3 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
|
|
"gitea.dev/tea/internal/clierr"
|
|
"gitea.dev/tea/internal/git"
|
|
"gitea.dev/tea/internal/services"
|
|
"gitea.dev/tea/internal/settings"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func (t target) hosts() map[string]settings.Host {
|
|
return map[string]settings.Host{t.Host: t.host}
|
|
}
|
|
|
|
// ownsRemote reports whether the Git remote URL raw names the target's instance and repository.
|
|
func (t target) ownsRemote(raw string) bool {
|
|
host, repo, err := settings.RepositoryURL(raw, t.hosts())
|
|
return err == nil && host == t.Host && strings.EqualFold(repo, t.Repo)
|
|
}
|
|
|
|
// remoteCredentials returns Git transport arguments for the URL of the named remote.
|
|
func (a *application) remoteCredentials(ctx context.Context, t target, remote string) ([]string, error) {
|
|
raw, err := a.git(ctx, "remote", "get-url", remote)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return a.gitTransportArguments(t, settings.RemoteIsSSH(raw))
|
|
}
|
|
|
|
func (a *application) checkoutRemote(ctx context.Context, target target, snapshot settings.Snapshot) (string, error) {
|
|
explicit := a.options.remote
|
|
if explicit == "" {
|
|
explicit = snapshot.Preferences.Remote
|
|
}
|
|
if explicit != "" {
|
|
raw, err := a.gitProbe().Output(ctx, a.options.cwd, false, "remote", "get-url", explicit)
|
|
if err == nil && target.ownsRemote(raw) {
|
|
return explicit, nil
|
|
}
|
|
return "", errors.New("selected remote does not match the resolved instance and repository")
|
|
}
|
|
matches, err := a.remotesForRepositoryURLs(ctx, target.hosts(), "", "", target.Repo)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
preferredRemotes := []string{"upstream", "origin"}
|
|
if _, tracking := git.CurrentUpstreamRemote(ctx, a.gitProbe(), a.options.cwd); tracking != "" {
|
|
preferredRemotes = append([]string{tracking}, preferredRemotes...)
|
|
}
|
|
for _, preferred := range preferredRemotes {
|
|
if slices.Contains(matches, preferred) {
|
|
return preferred, nil
|
|
}
|
|
}
|
|
if len(matches) == 1 {
|
|
return matches[0], nil
|
|
}
|
|
if len(matches) == 0 {
|
|
return "", errNoMatchingRemote
|
|
}
|
|
return "", errors.New("multiple Git remotes match the selected repository; select --remote")
|
|
}
|
|
|
|
var errNoMatchingRemote = errors.New("no Git remote matches the selected repository; add its base remote before checkout")
|
|
|
|
func (a *application) pullCheckoutCommand() *cobra.Command {
|
|
var branch string
|
|
var detached, update bool
|
|
cmd := &cobra.Command{Use: "checkout <number>", Short: "Check out a pull request in a matching branch or detached HEAD", Args: argsExact(1), RunE: func(cmd *cobra.Command, args []string) error {
|
|
n, err := number(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if detached && (cmd.Flags().Changed("branch") || update) {
|
|
return clierr.Wrap(clierr.Usage, errors.New("--detach and --branch/--update are mutually exclusive"))
|
|
}
|
|
if cmd.Flags().Changed("branch") && strings.TrimSpace(branch) == "" {
|
|
return clierr.Wrap(clierr.Usage, errors.New("--branch cannot be empty"))
|
|
}
|
|
if branch == "" && !detached {
|
|
branch = fmt.Sprintf("pr/%d", n)
|
|
}
|
|
target, err := a.resolve(cmd.Context(), true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
snapshot, err := a.deps.Settings.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
remote, err := a.checkoutRemote(cmd.Context(), target, snapshot)
|
|
if err != nil {
|
|
return clierr.Wrap(clierr.Usage, err)
|
|
}
|
|
pull, err := (services.Pulls{Client: target.client, Repository: target.Repo}).View(cmd.Context(), n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if pull.HeadSHA == "" {
|
|
return errors.New("Gitea did not return the PR head commit")
|
|
}
|
|
rawRemote, err := a.git(cmd.Context(), "remote", "get-url", remote)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !target.ownsRemote(rawRemote) {
|
|
return clierr.Wrap(clierr.Usage, errors.New("selected Git remote changed; resolve the repository again before checkout"))
|
|
}
|
|
credentialRemote := rawRemote
|
|
options := git.CheckoutOptions{
|
|
Detached: detached, Update: update, Directory: a.options.cwd, Remote: remote, Number: n, Branch: branch,
|
|
Prompt: a.policy(snapshot).CanPrompt(a.deps.Streams), ExpectedCommit: pull.HeadSHA,
|
|
}
|
|
undoRemote := func() {}
|
|
if !pull.HeadRemoteDeleted && pull.Head != "" && !detached {
|
|
headRemote, headURL, undo, ensureErr := a.ensureHeadRemote(cmd.Context(), target, pull)
|
|
if ensureErr != nil {
|
|
return ensureErr
|
|
}
|
|
if headRemote != "" {
|
|
options.HeadRemote, options.HeadBranch = headRemote, pull.Head
|
|
credentialRemote, undoRemote = headURL, undo
|
|
}
|
|
}
|
|
credentialArgs, err := a.gitTransportArguments(target, settings.RemoteIsSSH(credentialRemote))
|
|
if err != nil {
|
|
undoRemote()
|
|
return err
|
|
}
|
|
options.CredentialArgs = credentialArgs
|
|
result, err := git.CheckoutPull(cmd.Context(), a.deps.Git, options)
|
|
if err != nil {
|
|
undoRemote()
|
|
return err
|
|
}
|
|
if a.options.json != "" {
|
|
return a.writeJSON(cmd, result)
|
|
}
|
|
if result.Detached {
|
|
_, err := fmt.Fprintf(cmd.OutOrStdout(), "Checked out %s in detached HEAD\n", result.Commit)
|
|
return err
|
|
}
|
|
if result.Tracking != "" {
|
|
_, err = fmt.Fprintf(cmd.OutOrStdout(), "Checked out %s (%s) tracking %s\n", result.Branch, result.Commit, result.Tracking)
|
|
return err
|
|
}
|
|
_, err = fmt.Fprintf(cmd.OutOrStdout(), "Checked out %s (%s)\n", result.Branch, result.Commit)
|
|
return err
|
|
}}
|
|
cmd.Flags().BoolVar(&update, "update", false, "Fast-forward an existing branch to the PR head; reject divergence")
|
|
cmd.Flags().BoolVar(&detached, "detach", false, "Check out the PR commit without creating or changing a branch")
|
|
cmd.Flags().StringVarP(&branch, "branch", "b", "", "Local branch name (default: pr/NUMBER; existing branch must match PR head)")
|
|
return cmd
|
|
}
|
|
|
|
// ensureHeadRemote returns the remote holding the PR head, adding one when none matches.
|
|
// An empty name means the head repository has no usable clone URL and the base remote's PR ref should be used.
|
|
func (a *application) ensureHeadRemote(ctx context.Context, target target, pull services.Pull) (name, remoteURL string, undo func(), err error) {
|
|
matches, err := a.remotesForRepositoryURLs(ctx, target.hosts(), pull.HeadCloneURL, pull.HeadSSHURL, pull.HeadRepo)
|
|
if err != nil {
|
|
return "", "", nil, err
|
|
}
|
|
existing := ""
|
|
switch {
|
|
case a.options.remote != "" && slices.Contains(matches, a.options.remote):
|
|
existing = a.options.remote
|
|
case len(matches) == 1:
|
|
existing = matches[0]
|
|
case len(matches) > 1:
|
|
for _, preferred := range []string{"origin", "upstream"} {
|
|
if slices.Contains(matches, preferred) {
|
|
existing = preferred
|
|
break
|
|
}
|
|
}
|
|
if existing == "" {
|
|
return "", "", nil, clierr.Wrap(clierr.Usage, errors.New("multiple Git remotes match the pull request head repository; select --remote"))
|
|
}
|
|
}
|
|
if existing != "" {
|
|
raw, err := a.git(ctx, "remote", "get-url", existing)
|
|
return existing, raw, func() {}, err
|
|
}
|
|
remoteURL, _, urlErr := cloneURLForProtocol(pull.HeadCloneURL, pull.HeadSSHURL, target.gitProtocol)
|
|
if urlErr != nil {
|
|
return "", "", nil, nil
|
|
}
|
|
name = "pulls/" + pull.HeadRepo
|
|
if pull.HeadRepo == "" {
|
|
name = "pulls/head"
|
|
}
|
|
if _, err := a.git(ctx, "remote", "add", "--", name, remoteURL); err != nil {
|
|
return "", "", nil, fmt.Errorf("could not add head remote %s: %w", name, err)
|
|
}
|
|
// Detached from the command context so an interrupt (Ctrl-C) still removes the half-used remote.
|
|
cleanup := context.WithoutCancel(ctx)
|
|
return name, remoteURL, func() { _, _ = a.git(cleanup, "remote", "remove", "--", name) }, nil
|
|
}
|