fix(context): find the Gitea remote among all remotes of a clone

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-18 17:16:57 +02:00
1 parent 51709abdc0
commit bae885a4d2
6 files changed
+205 -27

No files matched your search

+53 -7
View File
@@ -4,6 +4,7 @@
package git
import (
"cmp"
"context"
"errors"
"slices"
@@ -16,27 +17,72 @@ var ErrAmbiguousRemote = errors.New("ambiguous repository: select --remote or --
// 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) {
// When repository is not nil, it names the repository of a remote tea can act on, or returns ""
// for one on another forge. Those steps then skip such remotes, and the other remotes decide when
// they name exactly one repository; with none of them usable the choice is the name-based one.
func DiscoverRemote(ctx context.Context, runner Runner, dir, preferred string, repository func(remote string) 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
usable := func(remote string) bool { return repository == nil || repository(remote) != "" }
// fallback is the remote chosen by name alone, used when no remote is usable.
var fallback string
var checked []string
try := func(remote string) bool {
if remote == "" || slices.Contains(checked, remote) {
return false
}
if usable(remote) {
return true
}
fallback = cmp.Or(fallback, remote)
checked = append(checked, remote)
return false
}
if remote != "" && !strings.HasPrefix(remote, "pulls/") {
return remote, nil
branch, tracking := CurrentUpstreamRemote(ctx, runner, dir)
if strings.HasPrefix(tracking, "pulls/") {
tracking = ""
}
for _, remote := range []string{pullBaseRemote(ctx, runner, dir, branch), tracking} {
if try(remote) {
return remote, nil
}
}
names, err := runner.Output(ctx, dir, false, "remote")
if err != nil {
if fallback != "" {
return fallback, nil
}
return "", err
}
available := strings.Fields(names)
for _, name := range []string{"upstream", "origin"} {
if slices.Contains(available, name) {
if slices.Contains(available, name) && try(name) {
return name, nil
}
}
if repository != nil {
var found, foundRepository string
for _, name := range available {
// pr checkout's head remotes point at forks; they never become the discovered repository.
if slices.Contains(checked, name) || strings.HasPrefix(name, "pulls/") {
continue
}
switch repo := repository(name); {
case repo == "":
case found == "":
found, foundRepository = name, repo
case repo != foundRepository:
return "", ErrAmbiguousRemote
}
}
if found != "" {
return found, nil
}
}
if fallback != "" {
return fallback, nil
}
if len(available) == 1 {
return available[0], nil
}
+45 -2
View File
@@ -45,11 +45,54 @@ func TestDiscoverRemote(t *testing.T) {
{name: "no remotes", runner: discoverRunner{"remote": ""}, wantErr: ErrAmbiguousRemote},
} {
t.Run(test.name, func(t *testing.T) {
remote, err := DiscoverRemote(context.Background(), test.runner, "/work", test.preferred)
remote, err := DiscoverRemote(context.Background(), test.runner, "/work", test.preferred, nil)
require.ErrorIs(t, err, test.wantErr)
require.Equal(t, test.want, remote)
})
}
_, err := DiscoverRemote(context.Background(), discoverRunner{}, "/work", "")
_, err := DiscoverRemote(context.Background(), discoverRunner{}, "/work", "", nil)
require.Error(t, err)
}
func TestDiscoverRemoteSkipsUnusableRemotes(t *testing.T) {
const head, tracking, base = "symbolic-ref --short HEAD", "config --get branch.main.remote", "config --get branch.main.tea-base-remote"
// gitea makes each named remote usable; a name=repository pair names its repository.
gitea := func(names ...string) func(string) string {
return func(remote string) string {
for _, name := range names {
if key, repo, ok := strings.Cut(name, "="); ok && key == remote {
return repo
} else if name == remote {
return "localhost/" + remote
}
}
return ""
}
}
for _, test := range []struct {
name string
runner discoverRunner
usable func(string) string
want string
wantErr error
}{
{name: "origin on another forge", runner: discoverRunner{"remote": "gitea\norigin"}, usable: gitea("gitea"), want: "gitea"},
{name: "no upstream or origin", runner: discoverRunner{"remote": "gitea\ngithub"}, usable: gitea("gitea"), want: "gitea"},
{name: "usable upstream keeps priority", runner: discoverRunner{"remote": "fork\norigin\nupstream"}, usable: gitea("fork", "upstream"), want: "upstream"},
{name: "usable origin after unusable upstream", runner: discoverRunner{"remote": "fork\norigin\nupstream"}, usable: gitea("fork", "origin"), want: "origin"},
{name: "unusable branch upstream", runner: discoverRunner{head: "main", tracking: "github", "remote": "github\norigin"}, usable: gitea("origin"), want: "origin"},
{name: "usable recorded pull base", runner: discoverRunner{head: "main", tracking: "fork", base: "gitea", "remote": "fork\ngitea\norigin"}, usable: gitea("fork", "gitea"), want: "gitea"},
{name: "several usable others", runner: discoverRunner{"remote": "a-gitea\norigin\nz-fork"}, usable: gitea("a-gitea", "z-fork"), wantErr: ErrAmbiguousRemote},
{name: "usable others naming one repository", runner: discoverRunner{"remote": "gitea\ngitea-ssh\norigin"}, usable: gitea("gitea=localhost/alice/project", "gitea-ssh=localhost/alice/project"), want: "gitea"},
{name: "unusable branch upstream named origin", runner: discoverRunner{head: "main", tracking: "origin", "remote": "gitea\norigin"}, usable: gitea("gitea"), want: "gitea"},
{name: "pull head remotes never win", runner: discoverRunner{"remote": "gitea\norigin\npulls/bob/project"}, usable: gitea("gitea", "pulls/bob/project"), want: "gitea"},
{name: "nothing usable keeps the name choice", runner: discoverRunner{"remote": "fork\norigin"}, usable: gitea(), want: "origin"},
{name: "nothing usable stays ambiguous", runner: discoverRunner{"remote": "one\ntwo"}, usable: gitea(), wantErr: ErrAmbiguousRemote},
} {
t.Run(test.name, func(t *testing.T) {
remote, err := DiscoverRemote(context.Background(), test.runner, "/work", "", test.usable)
require.ErrorIs(t, err, test.wantErr)
require.Equal(t, test.want, remote)
})
}
}