From f8294dcb7b032a6bfc920694f731216b4dc16e95 Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Fri, 15 May 2020 18:23:07 +0200 Subject: [PATCH] cleanup some result return types --- asyncgit/src/sync/diff.rs | 9 ++---- asyncgit/src/sync/reset.rs | 7 ++-- asyncgit/src/sync/utils.rs | 65 +++++++++++--------------------------- src/components/changes.rs | 6 ++-- 4 files changed, 26 insertions(+), 61 deletions(-) diff --git a/asyncgit/src/sync/diff.rs b/asyncgit/src/sync/diff.rs index 7b65bfeb..9d9c497c 100644 --- a/asyncgit/src/sync/diff.rs +++ b/asyncgit/src/sync/diff.rs @@ -313,10 +313,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 0)); - assert_eq!( - stage_add_file(repo_path, file_path).unwrap(), - true - ); + stage_add_file(repo_path, file_path).unwrap(); assert_eq!(get_statuses(repo_path), (0, 1)); @@ -378,9 +375,7 @@ mod tests { assert_eq!(res.len(), 1); assert_eq!(res[0].path, "bar.txt"); - let res = - stage_add_file(repo_path, Path::new("bar.txt")).unwrap(); - assert_eq!(res, true); + stage_add_file(repo_path, Path::new("bar.txt")).unwrap(); assert_eq!(get_statuses(repo_path), (0, 1)); // overwrite with next content diff --git a/asyncgit/src/sync/reset.rs b/asyncgit/src/sync/reset.rs index be8865e1..870dbeca 100644 --- a/asyncgit/src/sync/reset.rs +++ b/asyncgit/src/sync/reset.rs @@ -213,7 +213,7 @@ mod tests { .write_all(b"file3")?; } - assert!(stage_add_all(repo_path, "*").unwrap()); + stage_add_all(repo_path, "*").unwrap(); commit(repo_path, "msg").unwrap(); { @@ -295,10 +295,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 0)); - assert_eq!( - stage_add_file(repo_path, file_path).unwrap(), - true - ); + stage_add_file(repo_path, file_path).unwrap(); assert_eq!(get_statuses(repo_path), (0, 1)); diff --git a/asyncgit/src/sync/utils.rs b/asyncgit/src/sync/utils.rs index e891e967..c99577f1 100644 --- a/asyncgit/src/sync/utils.rs +++ b/asyncgit/src/sync/utils.rs @@ -68,57 +68,45 @@ pub fn commit(repo_path: &str, msg: &str) -> Result { } /// add a file diff from workingdir to stage (will not add removed files see `stage_addremoved`) -pub fn stage_add_file(repo_path: &str, path: &Path) -> Result { +pub fn stage_add_file(repo_path: &str, path: &Path) -> Result<()> { scope_time!("stage_add_file"); let repo = repo(repo_path)?; let mut index = repo.index()?; - if index.add_path(path).is_ok() { - index.write()?; - return Ok(true); - } + index.add_path(path)?; + index.write()?; - Ok(false) + Ok(()) } /// like `stage_add_file` but uses a pattern to match/glob multiple files/folders -pub fn stage_add_all(repo_path: &str, pattern: &str) -> Result { +pub fn stage_add_all(repo_path: &str, pattern: &str) -> Result<()> { scope_time!("stage_add_all"); let repo = repo(repo_path)?; let mut index = repo.index()?; - if index - .add_all(vec![pattern], IndexAddOption::DEFAULT, None) - .is_ok() - { - index.write()?; - return Ok(true); - } + index.add_all(vec![pattern], IndexAddOption::DEFAULT, None)?; + index.write()?; - Ok(false) + Ok(()) } /// stage a removed file -pub fn stage_addremoved( - repo_path: &str, - path: &Path, -) -> Result { +pub fn stage_addremoved(repo_path: &str, path: &Path) -> Result<()> { scope_time!("stage_addremoved"); let repo = repo(repo_path)?; let mut index = repo.index()?; - if index.remove_path(path).is_ok() { - index.write()?; - return Ok(true); - } + index.remove_path(path)?; + index.write()?; - Ok(false) + Ok(()) } #[cfg(test)] @@ -148,10 +136,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 0)); - assert_eq!( - stage_add_file(repo_path, file_path).unwrap(), - true - ); + stage_add_file(repo_path, file_path).unwrap(); assert_eq!(get_statuses(repo_path), (0, 1)); @@ -176,10 +161,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 0)); - assert_eq!( - stage_add_file(repo_path, file_path).unwrap(), - true - ); + stage_add_file(repo_path, file_path).unwrap(); assert_eq!(get_statuses(repo_path), (0, 1)); @@ -196,7 +178,7 @@ mod tests { let repo_path = root.as_os_str().to_str().unwrap(); assert_eq!( - stage_add_file(repo_path, file_path).unwrap(), + stage_add_file(repo_path, file_path).is_ok(), false ); } @@ -220,10 +202,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (2, 0)); - assert_eq!( - stage_add_file(repo_path, file_path).unwrap(), - true - ); + stage_add_file(repo_path, file_path).unwrap(); assert_eq!(get_statuses(repo_path), (1, 1)); } @@ -248,7 +227,7 @@ mod tests { assert_eq!(status_count(StatusType::WorkingDir), 3); - assert_eq!(stage_add_all(repo_path, "a/d").unwrap(), true); + stage_add_all(repo_path, "a/d").unwrap(); assert_eq!(status_count(StatusType::WorkingDir), 1); assert_eq!(status_count(StatusType::Stage), 2); @@ -274,10 +253,7 @@ mod tests { .write_all(b"test file1 content") .unwrap(); - assert_eq!( - stage_add_file(repo_path, file_path).unwrap(), - true - ); + stage_add_file(repo_path, file_path).unwrap(); commit(repo_path, "commit msg").unwrap(); @@ -287,10 +263,7 @@ mod tests { // deleted file in diff now assert_eq!(status_count(StatusType::WorkingDir), 1); - assert_eq!( - stage_addremoved(repo_path, file_path).unwrap(), - true - ); + stage_addremoved(repo_path, file_path).unwrap(); assert_eq!(status_count(StatusType::WorkingDir), 0); assert_eq!(status_count(StatusType::Stage), 1); diff --git a/src/components/changes.rs b/src/components/changes.rs index a692ddd4..66418a03 100644 --- a/src/components/changes.rs +++ b/src/components/changes.rs @@ -109,10 +109,10 @@ impl ChangesComponent { return match status { StatusItemType::Deleted => { sync::stage_addremoved(CWD, path) - .unwrap() + .is_ok() } _ => sync::stage_add_file(CWD, path) - .unwrap(), + .is_ok(), }; } } else { @@ -121,7 +121,7 @@ impl ChangesComponent { CWD, tree_item.info.full_path.as_str(), ) - .unwrap(); + .is_ok(); } } else { let path =