From 632f2555487efb52ee3543de0be03f16d49e9a46 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 1 Nov 2020 02:11:44 +0100 Subject: [PATCH] cleanup some more expects --- asyncgit/src/error.rs | 3 +++ asyncgit/src/lib.rs | 2 ++ asyncgit/src/sync/diff.rs | 7 ++++--- asyncgit/src/sync/hooks.rs | 9 ++++----- asyncgit/src/sync/hunks.rs | 10 +++++----- asyncgit/src/sync/ignore.rs | 2 +- asyncgit/src/sync/utils.rs | 6 +++--- 7 files changed, 22 insertions(+), 17 deletions(-) diff --git a/asyncgit/src/error.rs b/asyncgit/src/error.rs index 1b7c8adc..989a2416 100644 --- a/asyncgit/src/error.rs +++ b/asyncgit/src/error.rs @@ -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), diff --git a/asyncgit/src/lib.rs b/asyncgit/src/lib.rs index 70ebf658..3ca6d2a3 100644 --- a/asyncgit/src/lib.rs +++ b/asyncgit/src/lib.rs @@ -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; diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index cbeb0567..cdc55473 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -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()) } diff --git a/asyncgit/src/sync/hooks.rs b/asyncgit/src/sync/hooks.rs index 021fd257..18819ed7 100644 --- a/asyncgit/src/sync/hooks.rs +++ b/asyncgit/src/sync/hooks.rs @@ -77,14 +77,13 @@ pub fn hooks_post_commit(repo_path: &str) -> Result { fn work_dir_as_string(repo_path: &str) -> Result { 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 { diff --git a/asyncgit/src/sync/hunks.rs b/asyncgit/src/sync/hunks.rs index 99a72de3..a37d7c28 100644 --- a/asyncgit/src/sync/hunks.rs +++ b/asyncgit/src/sync/hunks.rs @@ -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 { diff --git a/asyncgit/src/sync/ignore.rs b/asyncgit/src/sync/ignore.rs index c71ad462..62e383ea 100644 --- a/asyncgit/src/sync/ignore.rs +++ b/asyncgit/src/sync/ignore.rs @@ -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)?; diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index 1cf090d2..639830ba 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -52,14 +52,14 @@ pub(crate) fn repo(repo_path: &str) -> Result { } /// -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 { 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()))