🐛 fix(spx): 同一 host 有多条 tea login 时优先取 default,不再固定取第一条

This commit is contained in:
cruldra committed 2026-10-19 08:40:47 +11:00
1 parent 3e3de0ae07
commit e6d086d821
2 files changed
+31 -4

No files matched your search

+12 -4
View File
@@ -58,8 +58,9 @@ func load(path string) (*TeaConfig, error) {
// LoginForHost returns the login in the tea config at `path` whose URL points at `host`, or `nil` when none does.
//
// Hosts are compared by hostname only, ignoring case, scheme, port and trailing slashes, so the host read from a git
// SSH remote still finds the login saved with the forge's web URL. A missing config file is not an error either,
// because the caller can still fall back to `GITEA_TOKEN`.
// SSH remote still finds the login saved with the forge's web URL. When several logins point at `host`, the one marked
// `default` wins, because that is the identity the user picked; otherwise the first. A missing config file is not an
// error either, because the caller can still fall back to `GITEA_TOKEN`.
func LoginForHost(path, host string) (*TeaLogin, error) {
cfg, err := load(path)
if errors.Is(err, fs.ErrNotExist) {
@@ -69,11 +70,18 @@ func LoginForHost(path, host string) (*TeaLogin, error) {
return nil, err
}
want := strings.ToLower(host)
var first *TeaLogin
for i := range cfg.Logins {
u, perr := url.Parse(strings.TrimSpace(cfg.Logins[i].URL))
if perr == nil && strings.ToLower(u.Hostname()) == want {
if perr != nil || strings.ToLower(u.Hostname()) != want {
continue
}
if cfg.Logins[i].Default {
return &cfg.Logins[i], nil
}
if first == nil {
first = &cfg.Logins[i]
}
}
return nil, nil
return first, nil
}