修复git凭据回退逻辑

This commit is contained in:
itpkcn@gmail.com
2026-03-21 14:52:03 +08:00
parent 107ee21a48
commit 3b6da2b477

View File

@@ -110,12 +110,11 @@ fn extract_host(remote_url: &str) -> Option<String> {
None None
} }
/// Find the best matching GitHub account for a given remote URL. /// Find the best matching account for a given remote URL.
/// ///
/// Matching logic: /// Only returns an account whose server_url hostname matches the remote URL host.
/// 1. Match by server_url hostname against the remote URL host /// Does NOT fall back to unrelated accounts — if no hostname matches, returns None
/// 2. Fall back to the default account /// so the caller can fall back to git config defaults.
/// 3. Fall back to the first account
pub fn find_matching_account<'a>( pub fn find_matching_account<'a>(
accounts: &'a [GitHubAccount], accounts: &'a [GitHubAccount],
remote_url: &str, remote_url: &str,
@@ -124,25 +123,13 @@ pub fn find_matching_account<'a>(
return None; return None;
} }
let remote_host = extract_host(remote_url); let remote_host = extract_host(remote_url)?;
// Try to match by hostname accounts.iter().find(|a| {
if let Some(ref host) = remote_host { let account_host = extract_host(&a.server_url)
let matched = accounts.iter().find(|a| { .unwrap_or_else(|| a.server_url.trim().trim_end_matches('/').to_lowercase());
let account_host = extract_host(&a.server_url) account_host == remote_host
.unwrap_or_else(|| a.server_url.trim().trim_end_matches('/').to_lowercase()); })
account_host == *host
});
if matched.is_some() {
return matched;
}
}
// Fall back to default account
accounts
.iter()
.find(|a| a.is_default)
.or_else(|| accounts.first())
} }
/// Load GitHub accounts from the database. /// Load GitHub accounts from the database.
@@ -313,8 +300,8 @@ mod tests {
let matched = find_matching_account(&accounts, "https://gitlab.example.com/org/repo"); let matched = find_matching_account(&accounts, "https://gitlab.example.com/org/repo");
assert_eq!(matched.unwrap().username, "user2"); assert_eq!(matched.unwrap().username, "user2");
// Unknown host falls back to default // Unknown host returns None — no fallback to unrelated accounts
let matched = find_matching_account(&accounts, "https://unknown.com/repo"); let matched = find_matching_account(&accounts, "https://unknown.com/repo");
assert_eq!(matched.unwrap().username, "user2"); assert!(matched.is_none());
} }
} }