Files
gitea-tea/internal/git/discover.go
T
bircni 299428bf10 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>
2026-10-11 21:47:06 +02:00

86 lines
2.8 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"context"
"errors"
"slices"
"strings"
)
// ErrAmbiguousRemote reports that repository discovery cannot choose between several remotes.
var ErrAmbiguousRemote = errors.New("ambiguous repository: select --remote or --repo")
// DiscoverRemote names the remote used for repository discovery: preferred when set, otherwise
// 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
}
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")
if err != nil {
return "", err
}
available := strings.Fields(names)
for _, name := range []string{"upstream", "origin"} {
if slices.Contains(available, name) {
return name, nil
}
}
if len(available) == 1 {
return available[0], nil
}
return "", ErrAmbiguousRemote
}
// CurrentUpstreamRemote names the checked-out branch and the remote it tracks; each is "" when
// HEAD is detached or the branch tracks no remote.
func CurrentUpstreamRemote(ctx context.Context, runner Runner, dir string) (branch, remote string) {
branch, err := runner.Output(ctx, dir, false, "symbolic-ref", "--short", "HEAD")
if err != nil {
return "", ""
}
return branch, UpstreamRemote(ctx, runner, dir, branch)
}
// UpstreamRemote names the remote branch tracks, or "" when it tracks none or a local branch.
// Unlike splitting @{upstream}, it keeps remote names with slashes, such as the
// pulls/<owner>/<repo> remotes pr checkout adds.
func UpstreamRemote(ctx context.Context, runner Runner, dir, branch string) string {
if branch == "" {
return ""
}
remote, err := runner.Output(ctx, dir, false, "config", "--get", "branch."+branch+".remote")
if err != nil || remote == "." {
return ""
}
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
}