fix(pr): keep targeting the base repository after a fork PR checkout

Repository discovery preferred the current branch's upstream remote,
which for a fork pull request is the pulls/<owner>/<repo> remote that
pr checkout adds, so every later command targeted the fork. pr checkout
now records the pull request number and base remote in
branch.<name>.tea-pr and branch.<name>.tea-base-remote, discovery
honors the recorded base remote, and it skips pulls/* head remotes.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-11 21:47:06 +02:00
1 parent 59bae60926
commit 299428bf10
5 files changed
+50 -7

No files matched your search

+24 -2
View File
@@ -14,12 +14,17 @@ import (
var ErrAmbiguousRemote = errors.New("ambiguous repository: select --remote or --repo")
// DiscoverRemote names the remote used for repository discovery: preferred when set, otherwise
// the current branch's upstream remote, then upstream, origin, or the only remote.
// the base remote pr checkout recorded for the current branch, the current branch's upstream
// remote unless it is a pulls/<owner>/<repo> head remote, then upstream, origin, or the only remote.
func DiscoverRemote(ctx context.Context, runner Runner, dir, preferred string) (string, error) {
if preferred != "" {
return preferred, nil
}
if _, remote := CurrentUpstreamRemote(ctx, runner, dir); remote != "" {
branch, remote := CurrentUpstreamRemote(ctx, runner, dir)
if base := pullBaseRemote(ctx, runner, dir, branch); base != "" {
return base, nil
}
if remote != "" && !strings.HasPrefix(remote, "pulls/") {
return remote, nil
}
names, err := runner.Output(ctx, dir, false, "remote")
@@ -61,3 +66,20 @@ func UpstreamRemote(ctx context.Context, runner Runner, dir, branch string) stri
}
return remote
}
// pullBaseRemote returns the base remote pr checkout recorded for branch, or "" when there is
// none or that remote no longer exists.
func pullBaseRemote(ctx context.Context, runner Runner, dir, branch string) string {
if branch == "" {
return ""
}
base, err := runner.Output(ctx, dir, false, "config", "--get", "branch."+branch+"."+pullBaseRemoteKey)
if err != nil || base == "" {
return ""
}
names, err := runner.Output(ctx, dir, false, "remote")
if err != nil || !slices.Contains(strings.Fields(names), base) {
return ""
}
return base
}