优化git提交用户名处理逻辑

This commit is contained in:
itpkcn@gmail.com
2026-03-21 13:29:50 +08:00
parent aaad19adb5
commit dac99fdccf
2 changed files with 44 additions and 3 deletions

View File

@@ -156,6 +156,33 @@ pub async fn load_github_accounts(
serde_json::from_str::<GitHubAccountsSettings>(&raw).ok()
}
/// Resolve the commit author (name + email) from the matching account for a repo.
///
/// Returns `Some((name, email))` if a matching account is found.
/// Uses GitHub's noreply email format: `username@users.noreply.github.com`.
pub async fn resolve_commit_author(
repo_path: &str,
conn: &DatabaseConnection,
) -> Option<(String, String)> {
let settings = load_github_accounts(conn).await?;
if settings.accounts.is_empty() {
return None;
}
let remote_url = get_remote_url(repo_path).await?;
let account = find_matching_account(&settings.accounts, &remote_url)?;
let host = extract_host(&remote_url).unwrap_or_default();
let email = if host == "github.com" {
format!("{}@users.noreply.github.com", account.username)
} else {
// For non-GitHub hosts, use username@host as a reasonable fallback
format!("{}@{}", account.username, host)
};
Some((account.username.clone(), email))
}
/// Resolve credentials for a git repository and inject them into the command.
///
/// This is the main entry point: given a repo path and a git command,