cleanup some more expects

This commit is contained in:
Stephan Dilly 2020-11-01 12:16:40 +01:00
parent 33de4f3f77
commit 2dab9de273
4 changed files with 24 additions and 15 deletions

View file

@ -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<String> {
@ -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(())

View file

@ -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) {

View file

@ -25,6 +25,7 @@ pub fn get_tags(repo_path: &str) -> Result<Tags> {
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

View file

@ -84,8 +84,7 @@ pub fn get_head_tuple(repo_path: &str) -> Result<Head> {
///
pub fn get_head_refname(repo: &Repository) -> Result<String> {
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<String> {
Ok(String::from_utf8(bytes.to_vec())?)
}
#[cfg(test)]
mod tests {
use super::*;