From 59d43f4d5de77bdb87ccb648ea1f5b777a9c02a7 Mon Sep 17 00:00:00 2001 From: Dan Anderson Date: Sat, 27 Jun 2020 18:03:54 +0000 Subject: [PATCH] allow undefined user.name on commit --- asyncgit/src/sync/utils.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index e68636f9..896b39c5 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -66,6 +66,25 @@ pub fn get_head_repo(repo: &Repository) -> Result { } } +/// Wrap Repository::signature to allow unknown user.name. +/// +/// See . +pub fn signature_allow_undefined_name( + repo: &Repository, +) -> std::result::Result, git2::Error> { + match repo.signature() { + Err(e) if e.code() == git2::ErrorCode::NotFound => { + let config = repo.config()?; + git2::Signature::now( + config.get_str("user.name").unwrap_or("unknown"), + config.get_str("user.email")?, + ) + } + + v => v, + } +} + /// ditto pub fn commit_new(repo_path: &str, msg: &str) -> Result { commit(repo_path, msg).map(CommitId::new) @@ -77,7 +96,7 @@ pub fn commit(repo_path: &str, msg: &str) -> Result { let repo = repo(repo_path)?; - let signature = repo.signature()?; + let signature = signature_allow_undefined_name(&repo)?; let mut index = repo.index()?; let tree_id = index.write_tree()?; let tree = repo.find_tree(tree_id)?;