From 1897aa4c604a0d9ab81f84dbff687b7f42bc1f0f Mon Sep 17 00:00:00 2001 From: Mica White Date: Sat, 20 Jun 2026 09:04:33 -0400 Subject: Reduce CPU usage --- src/lib.rs | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 2662948..f44cf57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,12 +12,6 @@ * - allow --no-commit * - display available autosaves if there is more than one * - * git undo-restore-autosave: - * - check for differences between workdir and restored save - * - error if there are unsaved changes - * - apply `refs/autosave/undo` to tree - * - allow force - * * git repair-autosave */ @@ -47,8 +41,8 @@ pub struct Autosave { pub repo_id: String, pub branch_name: String, pub commit_id: Oid, - pub author: Option, - pub email: Option, + pub author: String, + pub email: String, pub host_name: Option, pub time: Time, } @@ -65,11 +59,7 @@ impl TryFrom<&Reference<'_>> for Autosave { type Error = git2::Error; fn try_from(reference: &Reference<'_>) -> Result { - let reference_name = reference.name().ok_or(git2::Error::new( - git2::ErrorCode::Invalid, - git2::ErrorClass::Reference, - "Reference is not valid UTF-8", - ))?; + let reference_name = reference.name()?; let reference_name = reference_name .strip_prefix(AUTOSAVE_ENTRIES_NAMESPACE) .unwrap_or(reference_name); @@ -81,8 +71,8 @@ impl TryFrom<&Reference<'_>> for Autosave { )); }; let commit = reference.peel_to_commit()?; - let message = commit.message(); - let host_name = message.and_then(|m| m.strip_prefix("Autosave: ")); + let message = commit.message()?; + let host_name = message.strip_prefix("Autosave: "); let author = commit.author(); let author_name = author.name(); let author_email = author.email(); @@ -92,8 +82,8 @@ impl TryFrom<&Reference<'_>> for Autosave { repo_id: id.to_string(), branch_name: branch.to_string(), commit_id: commit.id(), - author: author_name.map(|s| s.to_string()), - email: author_email.map(|s| s.to_string()), + author: author_name.map(|s| s.to_string())?, + email: author_email.map(|s| s.to_string())?, host_name: host_name.map(|s| s.to_string()), time, }) @@ -106,11 +96,6 @@ pub fn repository_id(repository: &Repository) -> Result { .get_entry(AUTOSAVE_ID_CONFIG_KEY)? .value() .map(|s| s.to_string()) - .ok_or(git2::Error::new( - git2::ErrorCode::Invalid, - git2::ErrorClass::Config, - "Repository ID is not valid UTF-8", - )) } pub fn init(repository: &Repository, config: Option<&mut Config>) -> Result { @@ -164,7 +149,7 @@ pub fn push_autosaves( .name_bytes() .starts_with(format!("{AUTOSAVE_ENTRIES_NAMESPACE}{id}").as_bytes()) }) - .filter_map(|reference| reference.name().map(|n| n.to_string())) + .filter_map(|reference| reference.name().map(|n| n.to_string()).ok()) .map(|reference| format!("+{reference}:{reference}")) .collect::>(); remote.push(&refs, Some(PushOptions::new().remote_callbacks(callbacks)))?; @@ -224,13 +209,7 @@ pub fn saved_restored_autosave( repository: &Repository, autosave: &Autosave, ) -> Result<(), git2::Error> { - let author = if let Some(author) = &autosave.author - && let Some(email) = &autosave.email - { - Signature::new(author, email, &autosave.time)? - } else { - repository.signature()? - }; + let author = Signature::new(&autosave.author, &autosave.email, &autosave.time)?; let committer = repository.signature()?; let message = format!( "{}:{}/{}", -- cgit v1.2.3