diff --git a/asyncgit/src/sync/branch.rs b/asyncgit/src/sync/branch.rs index 81263855..ec46cf79 100644 --- a/asyncgit/src/sync/branch.rs +++ b/asyncgit/src/sync/branch.rs @@ -8,6 +8,8 @@ use git2::BranchType; use scopetime::scope_time; use utils::get_head_repo; +use super::utils::bytes2string; + /// returns the branch-name head is currently pointing to /// this might be expensive, see `cached::BranchName` pub(crate) fn get_branch_name(repo_path: &str) -> Result { @@ -58,15 +60,11 @@ pub fn get_branches_to_display( let top_commit = branch.get().peel_to_commit()?; Ok(BranchForDisplay { - name: String::from_utf8(Vec::from( - branch.name_bytes()?, - ))?, - reference: String::from_utf8(Vec::from( - branch.get().name_bytes(), - ))?, - top_commit_message: String::from_utf8(Vec::from( + name: bytes2string(branch.name_bytes()?)?, + reference: bytes2string(branch.get().name_bytes())?, + top_commit_message: bytes2string( top_commit.summary_bytes().unwrap_or_default(), - ))?, + )?, top_commit: top_commit.id().into(), is_head: branch.is_head(), }) @@ -132,7 +130,9 @@ pub fn checkout_branch( git2::build::CheckoutBuilder::new().force(), )) { // This is safe beacuse cur_ref was just found - repo.set_head(cur_ref.name().expect("utf8 error"))?; + repo.set_head( + bytes2string(cur_ref.name_bytes())?.as_str(), + )?; return Err(Error::Git(e)); } Ok(()) @@ -188,8 +188,7 @@ pub fn create_branch(repo_path: &str, name: &str) -> Result<()> { let branch = repo.branch(name, &head_commit, false)?; let branch_ref = branch.into_reference(); - let branch_ref_name = - String::from_utf8(branch_ref.name_bytes().to_vec())?; + let branch_ref_name = bytes2string(branch_ref.name_bytes())?; repo.set_head(branch_ref_name.as_str())?; Ok(()) diff --git a/asyncgit/src/sync/commit_files.rs b/asyncgit/src/sync/commit_files.rs index 6d0e9aa6..f988a3d9 100644 --- a/asyncgit/src/sync/commit_files.rs +++ b/asyncgit/src/sync/commit_files.rs @@ -1,5 +1,7 @@ use super::{stash::is_stash_commit, utils::repo, CommitId}; -use crate::{error::Result, StatusItem, StatusItemType}; +use crate::{ + error::Error, error::Result, StatusItem, StatusItemType, +}; use git2::{Diff, DiffDelta, DiffOptions, Repository}; use scopetime::scope_time; @@ -66,7 +68,10 @@ pub(crate) fn get_commit_diff( )?; if is_stash_commit( - repo.path().to_str().expect("repo path utf8 err"), + repo.path().to_str().map_or_else( + || Err(Error::Generic("repo path utf8 err".to_owned())), + Ok, + )?, &id, )? { if let Ok(untracked_commit) = commit.parent_id(2) { diff --git a/asyncgit/src/sync/tags.rs b/asyncgit/src/sync/tags.rs index 4c98dc02..ebe7a274 100644 --- a/asyncgit/src/sync/tags.rs +++ b/asyncgit/src/sync/tags.rs @@ -25,6 +25,7 @@ pub fn get_tags(repo_path: &str) -> Result { repo.tag_foreach(|id, name| { if let Ok(name) = + // skip the `refs/tags/` part String::from_utf8(name[10..name.len()].into()) { //NOTE: find_tag (git_tag_lookup) only works on annotated tags diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index 639830ba..ecb45813 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -84,8 +84,7 @@ pub fn get_head_tuple(repo_path: &str) -> Result { /// pub fn get_head_refname(repo: &Repository) -> Result { let head = repo.head()?; - let name_bytes = head.name_bytes(); - let ref_name = String::from_utf8(name_bytes.to_vec())?; + let ref_name = bytes2string(head.name_bytes())?; Ok(ref_name) } @@ -145,6 +144,11 @@ pub fn stage_addremoved(repo_path: &str, path: &Path) -> Result<()> { Ok(()) } +/// helper function +pub(crate) fn bytes2string(bytes: &[u8]) -> Result { + Ok(String::from_utf8(bytes.to_vec())?) +} + #[cfg(test)] mod tests { use super::*;