Skip to content

Commit 433339c

Browse files
authored
Merge pull request #30 from epage/source
fix(ops): Expose separate committer/author signatures
2 parents e2394d6 + bccb2e5 commit 433339c

File tree

1 file changed

+43
-50
lines changed

1 file changed

+43
-50
lines changed

src/ops.rs

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub fn cherry_pick(
117117
));
118118
}
119119

120-
let mut sig = signature(repo)?;
120+
let mut sig = commit_signature(repo)?;
121121
if let (Some(name), Some(email)) = (sig.name(), sig.email()) {
122122
// For simple rebases, preserve the original commit time
123123
sig = git2::Signature::new(name, email, &cherry_commit.time())?.to_owned();
@@ -309,7 +309,7 @@ impl UserSign {
309309

310310
let signing_key = config.get_string("user.signingkey").or_else(
311311
|_| -> Result<_, git2::Error> {
312-
let sig = signature(repo)?;
312+
let sig = commit_signature(repo)?;
313313
Ok(sig.to_string())
314314
},
315315
)?;
@@ -326,7 +326,7 @@ impl UserSign {
326326

327327
let signing_key = config.get_string("user.signingkey").or_else(
328328
|_| -> Result<_, git2::Error> {
329-
let sig = signature(repo)?;
329+
let sig = commit_signature(repo)?;
330330
Ok(sig.to_string())
331331
},
332332
)?;
@@ -347,7 +347,7 @@ impl UserSign {
347347
.unwrap_or_else(|_| -> Result<_, git2::Error> {
348348
get_default_ssh_signing_key(config)?.map(Ok).unwrap_or_else(
349349
|| -> Result<_, git2::Error> {
350-
let sig = signature(repo)?;
350+
let sig = commit_signature(repo)?;
351351
Ok(sig.to_string())
352352
},
353353
)
@@ -669,61 +669,54 @@ fn get_default_ssh_signing_key(config: &git2::Config) -> Result<Option<String>,
669669
Ok(Some(default_key.to_owned()))
670670
}
671671

672-
/// Replacement for [`git2::Repository::signature`] that respects env variables
672+
#[doc(hidden)]
673+
#[deprecated(
674+
since = "0.4.3",
675+
note = "Replaced with `commit_signature`, `author_signature`"
676+
)]
673677
pub fn signature(repo: &git2::Repository) -> Result<git2::Signature<'_>, git2::Error> {
674-
let config_sig = repo.signature()?;
678+
commit_signature(repo)
679+
}
675680

676-
let name = std::env::var_os("GIT_COMMITTER_NAME")
677-
.map(|os| {
678-
os.into_string().map_err(|os| {
679-
git2::Error::new(
680-
git2::ErrorCode::Unmerged,
681-
git2::ErrorClass::Invalid,
682-
format!(
683-
"`GIT_COMMITTER_NAME` is not valid UTF-8: {}",
684-
os.to_string_lossy()
685-
),
686-
)
687-
})
688-
})
689-
.unwrap_or_else(|| {
690-
config_sig.name().map(ToOwned::to_owned).ok_or_else(|| {
691-
git2::Error::new(
692-
git2::ErrorCode::Unmerged,
693-
git2::ErrorClass::Invalid,
694-
format!(
695-
"name is not valid UTF-8: {}",
696-
String::from_utf8_lossy(config_sig.name_bytes())
697-
),
698-
)
699-
})
700-
})?;
681+
/// Lookup the configured committer's signature
682+
pub fn commit_signature(repo: &git2::Repository) -> Result<git2::Signature<'_>, git2::Error> {
683+
let config = repo.config()?;
684+
let name = read_signature_field(&config, "GIT_COMMITTER_NAME", "committer.name", "user.name")?;
685+
let email = read_signature_field(
686+
&config,
687+
"GIT_COMMITTER_EMAIL",
688+
"committer.email",
689+
"user.email",
690+
)?;
691+
692+
git2::Signature::now(&name, &email)
693+
}
694+
695+
/// Lookup the configured author's signature
696+
pub fn author_signature(repo: &git2::Repository) -> Result<git2::Signature<'_>, git2::Error> {
697+
let config = repo.config()?;
698+
let name = read_signature_field(&config, "GIT_AUTHOR_NAME", "author.name", "user.name")?;
699+
let email = read_signature_field(&config, "GIT_AUTHOR_EMAIL", "author.email", "user.email")?;
701700

702-
let email = std::env::var_os("GIT_COMMITTER_EMAIL")
701+
git2::Signature::now(&name, &email)
702+
}
703+
704+
fn read_signature_field(
705+
config: &git2::Config,
706+
env_var: &str,
707+
specialized_key: &str,
708+
general_key: &str,
709+
) -> Result<String, git2::Error> {
710+
std::env::var_os(env_var)
703711
.map(|os| {
704712
os.into_string().map_err(|os| {
705713
git2::Error::new(
706714
git2::ErrorCode::Unmerged,
707715
git2::ErrorClass::Invalid,
708-
format!(
709-
"`GIT_COMMITTER_EMAIL` is not valid UTF-8: {}",
710-
os.to_string_lossy()
711-
),
716+
format!("`{}` is not valid UTF-8: {}", env_var, os.to_string_lossy()),
712717
)
713718
})
714719
})
715-
.unwrap_or_else(|| {
716-
config_sig.email().map(ToOwned::to_owned).ok_or_else(|| {
717-
git2::Error::new(
718-
git2::ErrorCode::Unmerged,
719-
git2::ErrorClass::Invalid,
720-
format!(
721-
"email is not valid UTF-8: {}",
722-
String::from_utf8_lossy(config_sig.email_bytes())
723-
),
724-
)
725-
})
726-
})?;
727-
728-
git2::Signature::new(&name, &email, &config_sig.when())
720+
.or_else(|| config.get_string(specialized_key).ok().map(Ok))
721+
.unwrap_or_else(|| config.get_string(general_key))
729722
}

0 commit comments

Comments
 (0)