From 3b6da2b4774d6fa926bfc870bc9717ee47dec499 Mon Sep 17 00:00:00 2001 From: "itpkcn@gmail.com" Date: Sat, 21 Mar 2026 14:52:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dgit=E5=87=AD=E6=8D=AE?= =?UTF-8?q?=E5=9B=9E=E9=80=80=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/git_credential.rs | 37 +++++++++++---------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/src-tauri/src/git_credential.rs b/src-tauri/src/git_credential.rs index cbcf869..6eb6c87 100644 --- a/src-tauri/src/git_credential.rs +++ b/src-tauri/src/git_credential.rs @@ -110,12 +110,11 @@ fn extract_host(remote_url: &str) -> Option { None } -/// Find the best matching GitHub account for a given remote URL. +/// Find the best matching account for a given remote URL. /// -/// Matching logic: -/// 1. Match by server_url hostname against the remote URL host -/// 2. Fall back to the default account -/// 3. Fall back to the first account +/// Only returns an account whose server_url hostname matches the remote URL host. +/// Does NOT fall back to unrelated accounts — if no hostname matches, returns None +/// so the caller can fall back to git config defaults. pub fn find_matching_account<'a>( accounts: &'a [GitHubAccount], remote_url: &str, @@ -124,25 +123,13 @@ pub fn find_matching_account<'a>( return None; } - let remote_host = extract_host(remote_url); + let remote_host = extract_host(remote_url)?; - // Try to match by hostname - if let Some(ref host) = remote_host { - let matched = accounts.iter().find(|a| { - let account_host = extract_host(&a.server_url) - .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()) + accounts.iter().find(|a| { + let account_host = extract_host(&a.server_url) + .unwrap_or_else(|| a.server_url.trim().trim_end_matches('/').to_lowercase()); + account_host == remote_host + }) } /// Load GitHub accounts from the database. @@ -313,8 +300,8 @@ mod tests { let matched = find_matching_account(&accounts, "https://gitlab.example.com/org/repo"); 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"); - assert_eq!(matched.unwrap().username, "user2"); + assert!(matched.is_none()); } }