优化git提交用户名处理逻辑
This commit is contained in:
@@ -1372,6 +1372,7 @@ pub async fn git_show_file(
|
||||
pub async fn git_commit(
|
||||
app: tauri::AppHandle,
|
||||
window: tauri::WebviewWindow,
|
||||
db: tauri::State<'_, AppDatabase>,
|
||||
path: String,
|
||||
message: String,
|
||||
files: Vec<String>,
|
||||
@@ -1391,10 +1392,23 @@ pub async fn git_commit(
|
||||
return Err(git_command_error("add", &add_output.stderr));
|
||||
}
|
||||
|
||||
// Resolve commit author from matching account (e.g. GitHub username)
|
||||
let author_override =
|
||||
crate::git_credential::resolve_commit_author(&path, &db.conn).await;
|
||||
|
||||
// Commit
|
||||
let commit_output = crate::process::tokio_command("git")
|
||||
.args(["commit", "-m", &message])
|
||||
.current_dir(&path)
|
||||
let mut commit_cmd = crate::process::tokio_command("git");
|
||||
if let Some((ref name, ref email)) = author_override {
|
||||
commit_cmd.args([
|
||||
"-c",
|
||||
&format!("user.name={name}"),
|
||||
"-c",
|
||||
&format!("user.email={email}"),
|
||||
]);
|
||||
}
|
||||
commit_cmd.args(["commit", "-m", &message]).current_dir(&path);
|
||||
|
||||
let commit_output = commit_cmd
|
||||
.output()
|
||||
.await
|
||||
.map_err(AppCommandError::io)?;
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user