mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
fix(context): find the Gitea remote among all remotes of a clone
Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
6 files changed
+205
-27
No files matched your search
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user