From 6e6832da2175e70f9a75a3ad65e859c7d0bed1cd Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Sun, 1 Nov 2020 12:37:07 +0100 Subject: [PATCH] cleanup one more expect --- asyncgit/src/sync/diff.rs | 58 ++++++++++++++++++++------------------ asyncgit/src/sync/hunks.rs | 9 ++++-- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index cdc55473..88c43ca7 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -219,40 +219,39 @@ fn raw_diff_to_file_diff<'a>( }; let new_file_diff = if diff.deltas().len() == 1 { - let delta: DiffDelta = diff - .deltas() - .next() - .expect("it's safe to unwrap here because we check first that diff.deltas has a single element"); + if let Some(delta) = diff.deltas().next() { + if delta.status() == Delta::Untracked { + let relative_path = + delta.new_file().path().ok_or_else(|| { + Error::Generic( + "new file path is unspecified." + .to_string(), + ) + })?; - if delta.status() == Delta::Untracked { - let relative_path = - delta.new_file().path().ok_or_else(|| { - Error::Generic( - "new file path is unspecified." - .to_string(), - ) - })?; + let newfile_path = work_dir.join(relative_path); - let newfile_path = work_dir.join(relative_path); + if let Some(newfile_content) = + new_file_content(&newfile_path) + { + let mut patch = Patch::from_buffers( + &[], + None, + newfile_content.as_slice(), + Some(&newfile_path), + None, + )?; - if let Some(newfile_content) = - new_file_content(&newfile_path) - { - let mut patch = Patch::from_buffers( - &[], - None, - newfile_content.as_slice(), - Some(&newfile_path), - None, - )?; - - patch + patch .print(&mut |delta, hunk:Option, line: git2::DiffLine| { put(delta,hunk,line); true })?; - true + true + } else { + false + } } else { false } @@ -275,7 +274,10 @@ fn raw_diff_to_file_diff<'a>( if !current_lines.is_empty() { adder( - ¤t_hunk.expect("invalid hunk"), + ¤t_hunk.map_or_else( + || Err(Error::Generic("invalid hunk".to_owned())), + Ok, + )?, ¤t_lines, ); } @@ -285,7 +287,7 @@ fn raw_diff_to_file_diff<'a>( } } let res = Rc::try_unwrap(res) - .map_err(|_| Error::Generic("".to_owned()))?; + .map_err(|_| Error::Generic("rc unwrap error".to_owned()))?; Ok(res.into_inner()) } diff --git a/asyncgit/src/sync/hunks.rs b/asyncgit/src/sync/hunks.rs index a37d7c28..19cda65b 100644 --- a/asyncgit/src/sync/hunks.rs +++ b/asyncgit/src/sync/hunks.rs @@ -23,9 +23,12 @@ pub fn stage_hunk( let mut opt = ApplyOptions::new(); opt.hunk_callback(|hunk| { - let header = - HunkHeader::from(hunk.expect("hunk unavailable")); - hash(&header) == hunk_hash + if let Some(hunk) = hunk { + let header = HunkHeader::from(hunk); + hash(&header) == hunk_hash + } else { + false + } }); repo.apply(&diff, ApplyLocation::Index, Some(&mut opt))?;