cleanup some more expects

This commit is contained in:
Stephan Dilly 2020-11-01 02:11:44 +01:00
parent cad40d17ee
commit 632f255548
7 changed files with 22 additions and 17 deletions

View file

@ -12,6 +12,9 @@ pub enum Error {
#[error("git: remote url not found")]
UnknownRemote,
#[error("git: work dir error")]
NoWorkDir,
#[error("io error:{0}")]
Io(#[from] std::io::Error),

View file

@ -7,6 +7,8 @@
#![deny(clippy::unwrap_used)]
#![deny(clippy::panic)]
#![deny(clippy::perf)]
//TODO: get this in someday since expect still leads us to crashes sometimes
// #![deny(clippy::expect_used)]
pub mod cached;
mod commit_files;

View file

@ -132,7 +132,7 @@ pub fn get_diff(
scope_time!("get_diff");
let repo = utils::repo(repo_path)?;
let work_dir = work_dir(&repo);
let work_dir = work_dir(&repo)?;
let diff = get_diff_raw(&repo, &p, stage, false)?;
raw_diff_to_file_diff(&diff, work_dir)
@ -148,7 +148,7 @@ pub fn get_diff_commit(
scope_time!("get_diff_commit");
let repo = utils::repo(repo_path)?;
let work_dir = work_dir(&repo);
let work_dir = work_dir(&repo)?;
let diff = get_commit_diff(&repo, id, Some(p))?;
raw_diff_to_file_diff(&diff, work_dir)
@ -284,7 +284,8 @@ fn raw_diff_to_file_diff<'a>(
res.borrow_mut().untracked = true;
}
}
let res = Rc::try_unwrap(res).expect("rc error");
let res = Rc::try_unwrap(res)
.map_err(|_| Error::Generic("".to_owned()))?;
Ok(res.into_inner())
}

View file

@ -77,14 +77,13 @@ pub fn hooks_post_commit(repo_path: &str) -> Result<HookResult> {
fn work_dir_as_string(repo_path: &str) -> Result<String> {
let repo = repo(repo_path)?;
work_dir(&repo)
.to_str()
.map(|s| s.to_string())
.ok_or_else(|| {
work_dir(&repo)?.to_str().map(|s| s.to_string()).ok_or_else(
|| {
Error::Generic(
"workdir contains invalid utf8".to_string(),
)
})
},
)
}
fn hook_runable(path: &str, hook: &str) -> bool {

View file

@ -105,9 +105,10 @@ pub fn unstage_hunk(
let diff_count_positive = diff.deltas().len();
let hunk_index = find_hunk_index(&diff, hunk_hash);
if hunk_index.is_none() {
return Err(Error::Generic("hunk not found".to_string()));
}
let hunk_index = hunk_index.map_or_else(
|| Err(Error::Generic("hunk not found".to_string())),
Ok,
)?;
let diff = get_diff_raw(&repo, &file_path, true, true)?;
@ -124,8 +125,7 @@ pub fn unstage_hunk(
let mut hunk_idx = 0;
let mut opt = ApplyOptions::new();
opt.hunk_callback(|_hunk| {
let res = if hunk_idx == hunk_index.expect("invalid hunk")
{
let res = if hunk_idx == hunk_index {
count += 1;
true
} else {

View file

@ -18,7 +18,7 @@ pub fn add_to_ignore(
let repo = repo(repo_path)?;
let ignore_file = work_dir(&repo).join(GITIGNORE);
let ignore_file = work_dir(&repo)?.join(GITIGNORE);
let optional_newline = ignore_file.exists()
&& !file_ends_with_newline(&ignore_file)?;

View file

@ -52,14 +52,14 @@ pub(crate) fn repo(repo_path: &str) -> Result<Repository> {
}
///
pub(crate) fn work_dir(repo: &Repository) -> &Path {
repo.workdir().expect("unable to query workdir")
pub(crate) fn work_dir(repo: &Repository) -> Result<&Path> {
repo.workdir().map_or(Err(Error::NoWorkDir), |dir| Ok(dir))
}
///
pub fn repo_work_dir(repo_path: &str) -> Result<String> {
let repo = repo(repo_path)?;
if let Some(workdir) = work_dir(&repo).to_str() {
if let Some(workdir) = work_dir(&repo)?.to_str() {
Ok(workdir.to_string())
} else {
Err(Error::Generic("invalid workdir".to_string()))