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>
This commit is contained in:
bircni committed 2026-10-11 23:55:11 +02:00
1 parent 2c5e005bcf
commit 4895aa089d
28 files changed
+1169 -504

No files matched your search

+40 -15
View File
@@ -11,6 +11,7 @@ import (
"testing"
"gitea.dev/tea/internal/git"
"gitea.dev/tea/internal/services"
"gitea.dev/tea/internal/settings"
"github.com/stretchr/testify/require"
)
@@ -66,15 +67,14 @@ func TestCheckoutMatchesRemoteThroughEnvironmentSSHHost(t *testing.T) {
deps.Getenv = serverEnv(server.URL, map[string]string{"GITEA_INSTANCE_SSH_HOST": "ssh.example.org"})
deps.Git = &fakeGit{results: map[string]string{
"remote": "origin", "remote get-url origin": "git@ssh.example.org:alice/project.git",
"rev-parse --is-inside-work-tree": "true", "check-ref-format --branch pr/7": "",
"check-ref-format --branch feature": "",
"rev-parse --is-inside-work-tree": "true", "check-ref-format --branch feature": "",
"status --porcelain=v1 --untracked-files=normal": "", "for-each-ref --format=%(refname) refs/heads/": "",
"fetch --no-tags -- origin refs/heads/feature": "", "rev-parse --verify FETCH_HEAD^{commit}": "abc123", "switch --create pr/7 abc123": "",
"config branch.pr/7.remote origin": "", "config branch.pr/7.merge refs/heads/feature": "",
"config branch.pr/7.tea-pr 7": "", "config branch.pr/7.tea-base-remote origin": "",
"fetch --quiet --no-tags -- origin refs/heads/feature": "", "rev-parse --verify FETCH_HEAD^{commit}": "abc123", "switch --quiet --create feature abc123": "",
"config branch.feature.remote origin": "", "config branch.feature.merge refs/heads/feature": "",
"config branch.feature.tea-pr 7": "", "config branch.feature.tea-base-remote origin": "",
}}
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "checkout", "7", "--repo", "alice/project", "--remote", "origin", "--no-input", "--json"}))
require.JSONEq(t, `{"branch":"pr/7","commit":"abc123","detached":false,"remote":"origin","tracking":"origin/feature"}`, out.String())
require.JSONEq(t, `{"branch":"feature","commit":"abc123","detached":false,"remote":"origin","tracking":"origin/feature"}`, out.String())
}
func TestSSHCheckoutUsesAccountKeyOnlyForFetch(t *testing.T) {
@@ -90,21 +90,20 @@ func TestSSHCheckoutUsesAccountKeyOnlyForFetch(t *testing.T) {
}})
sshArgs, err := git.SSHArguments("/keys/alice key", false)
require.NoError(t, err)
fetch := strings.Join(append(sshArgs, "fetch", "--no-tags", "--", "origin", "refs/heads/feature"), " ")
fetch := strings.Join(append(sshArgs, "fetch", "--quiet", "--no-tags", "--", "origin", "refs/heads/feature"), " ")
runner := &fakeGit{results: map[string]string{
"remote": "origin", "remote get-url origin": "ssh://git@git.example.org:2222/alice/project.git",
"rev-parse --is-inside-work-tree": "true", "check-ref-format --branch pr/7": "",
"check-ref-format --branch feature": "",
"rev-parse --is-inside-work-tree": "true", "check-ref-format --branch feature": "",
"status --porcelain=v1 --untracked-files=normal": "", "for-each-ref --format=%(refname) refs/heads/": "",
fetch: "", "rev-parse --verify FETCH_HEAD^{commit}": "abc123", "switch --create pr/7 abc123": "",
"config branch.pr/7.remote origin": "", "config branch.pr/7.merge refs/heads/feature": "",
"config branch.pr/7.tea-pr 7": "", "config branch.pr/7.tea-base-remote origin": "",
fetch: "", "rev-parse --verify FETCH_HEAD^{commit}": "abc123", "switch --quiet --create feature abc123": "",
"config branch.feature.remote origin": "", "config branch.feature.merge refs/heads/feature": "",
"config branch.feature.tea-pr 7": "", "config branch.feature.tea-base-remote origin": "",
}}
deps.Git = runner
require.NoError(t, Execute(context.Background(), deps, []string{"pr", "checkout", "7", "--repo", "alice/project", "--remote", "origin", "--no-input", "--json"}))
require.Contains(t, runner.calls, fetch)
require.Contains(t, runner.calls, "config branch.pr/7.remote origin")
require.Contains(t, runner.calls, "config branch.pr/7.merge refs/heads/feature")
require.Contains(t, runner.calls, "config branch.feature.remote origin")
require.Contains(t, runner.calls, "config branch.feature.merge refs/heads/feature")
configured := 0
for _, call := range runner.calls {
if strings.HasPrefix(call, "-c ") {
@@ -114,5 +113,31 @@ func TestSSHCheckoutUsesAccountKeyOnlyForFetch(t *testing.T) {
}
require.Equal(t, 1, configured)
require.False(t, runner.prompt)
require.JSONEq(t, `{"branch":"pr/7","commit":"abc123","detached":false,"remote":"origin","tracking":"origin/feature"}`, out.String())
require.JSONEq(t, `{"branch":"feature","commit":"abc123","detached":false,"remote":"origin","tracking":"origin/feature"}`, out.String())
}
func TestCheckoutNamesTheBranchAfterTheHead(t *testing.T) {
same := services.Pull{Number: 7, Head: "feature", HeadSHA: "abc123", HeadRepo: "alice/project"}
fork := services.Pull{Number: 7, Head: "feature", HeadSHA: "abc123", HeadRepo: "bob/project"}
for _, tc := range []struct {
name string
pull services.Pull
branches map[string]string
want string
}{
{"free head name", same, nil, "feature"},
{"head name at the head", same, map[string]string{"rev-parse --verify refs/heads/feature^{commit}": "abc123"}, "feature"},
{"head name of an earlier checkout", same, map[string]string{"rev-parse --verify refs/heads/feature^{commit}": "old", "config --get branch.feature.tea-pr": "7"}, "feature"},
{"head name taken", same, map[string]string{"rev-parse --verify refs/heads/feature^{commit}": "other"}, "pr/7"},
{"fork head name taken", fork, map[string]string{"rev-parse --verify refs/heads/feature^{commit}": "other"}, "bob/feature"},
{"fork names taken", fork, map[string]string{"rev-parse --verify refs/heads/feature^{commit}": "other", "rev-parse --verify refs/heads/bob/feature^{commit}": "other"}, "pr/7"},
{"deleted head", services.Pull{Number: 7, Head: "feature", HeadRemoteDeleted: true}, nil, "pr/7"},
} {
t.Run(tc.name, func(t *testing.T) {
deps, _, _ := testDeps(t)
deps.Git = &fakeGit{results: tc.branches}
a := &application{deps: deps}
require.Equal(t, tc.want, a.checkoutBranchName(context.Background(), target{Repo: "alice/project"}, tc.pull))
})
}
}