fix: set git author before app commits
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use super::git_command;
|
||||
use super::{ensure_author_config, git_command};
|
||||
use std::path::Path;
|
||||
|
||||
struct CommitFailure {
|
||||
@@ -22,6 +22,8 @@ pub fn git_commit(vault_path: &str, message: &str) -> Result<String, String> {
|
||||
return Err(format!("git add failed: {}", stderr));
|
||||
}
|
||||
|
||||
ensure_author_config(vault)?;
|
||||
|
||||
match run_commit(vault, message, false) {
|
||||
Ok(stdout) => Ok(stdout),
|
||||
Err(failure) if is_commit_signing_failure(&failure.detail()) => {
|
||||
@@ -89,6 +91,30 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::git::tests::setup_git_repo;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
fn unset_local_author_config(vault: &Path) {
|
||||
for key in ["user.name", "user.email"] {
|
||||
let status = git_command()
|
||||
.args(["config", "--local", "--unset-all", key])
|
||||
.current_dir(vault)
|
||||
.status()
|
||||
.unwrap();
|
||||
assert!(status.success(), "failed to unset {key}");
|
||||
}
|
||||
}
|
||||
|
||||
fn local_config_value(vault: &Path, key: &str) -> Option<String> {
|
||||
let output = git_command()
|
||||
.args(["config", "--local", key])
|
||||
.current_dir(vault)
|
||||
.output()
|
||||
.unwrap();
|
||||
output
|
||||
.status
|
||||
.success()
|
||||
.then(|| String::from_utf8_lossy(&output.stdout).trim().to_string())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_git_commit() {
|
||||
@@ -110,6 +136,40 @@ mod tests {
|
||||
assert!(log_str.contains("Test commit"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_git_commit_sets_missing_local_author_identity() {
|
||||
let dir = setup_git_repo();
|
||||
let vault = dir.path();
|
||||
unset_local_author_config(vault);
|
||||
|
||||
fs::write(vault.join("identity-fallback.md"), "# Identity fallback\n").unwrap();
|
||||
|
||||
let result = git_commit(vault.to_str().unwrap(), "Commit without local identity");
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"commit should set local fallback identity: {result:?}"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
local_config_value(vault, "user.name").as_deref(),
|
||||
Some("Tolaria")
|
||||
);
|
||||
assert_eq!(
|
||||
local_config_value(vault, "user.email").as_deref(),
|
||||
Some("vault@tolaria.md")
|
||||
);
|
||||
|
||||
let author = git_command()
|
||||
.args(["log", "-1", "--format=%an <%ae>"])
|
||||
.current_dir(vault)
|
||||
.output()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
String::from_utf8_lossy(&author.stdout).trim(),
|
||||
"Tolaria <vault@tolaria.md>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_commit_nothing_to_commit_returns_error() {
|
||||
let dir = setup_git_repo();
|
||||
|
||||
Reference in New Issue
Block a user