From 6ebe717fb14e1fa71d55130a0036ad382a7f54ba Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Fri, 12 Jun 2020 14:41:37 +0200 Subject: [PATCH] remove some duplication in reset --- asyncgit/src/sync/mod.rs | 4 +-- asyncgit/src/sync/reset.rs | 60 +++++++------------------------------- src/tabs/status.rs | 14 ++++----- 3 files changed, 17 insertions(+), 61 deletions(-) diff --git a/asyncgit/src/sync/mod.rs b/asyncgit/src/sync/mod.rs index 2a80470c..4237672e 100644 --- a/asyncgit/src/sync/mod.rs +++ b/asyncgit/src/sync/mod.rs @@ -22,9 +22,7 @@ pub use hooks::{hooks_commit_msg, hooks_post_commit, HookResult}; pub use hunks::{stage_hunk, unstage_hunk}; pub use ignore::add_to_ignore; pub use logwalker::LogWalker; -pub use reset::{ - reset_stage, reset_workdir_file, reset_workdir_folder, -}; +pub use reset::{reset_stage, reset_workdir}; pub use stash::{get_stashes, stash_apply, stash_drop, stash_save}; pub use tags::{get_tags, Tags}; pub use utils::{ diff --git a/asyncgit/src/sync/reset.rs b/asyncgit/src/sync/reset.rs index 02aff69b..30a2961c 100644 --- a/asyncgit/src/sync/reset.rs +++ b/asyncgit/src/sync/reset.rs @@ -1,8 +1,7 @@ -use super::utils::{repo, work_dir}; +use super::utils::repo; use crate::error::{Error, Result}; -use git2::{build::CheckoutBuilder, ObjectType, Status}; +use git2::{build::CheckoutBuilder, ObjectType}; use scopetime::scope_time; -use std::{fs, path::Path}; /// pub fn reset_stage(repo_path: &str, path: &str) -> Result<()> { @@ -32,43 +31,8 @@ pub fn reset_stage(repo_path: &str, path: &str) -> Result<()> { } /// -pub fn reset_workdir_file(repo_path: &str, path: &str) -> Result<()> { - scope_time!("reset_workdir_file"); - - let repo = repo(repo_path)?; - - let workdir = work_dir(&repo); - - // Note: early out for removing untracked files, due to bug in checkout_head code: - // see https://github.com/libgit2/libgit2/issues/5089 - let status = repo.status_file(Path::new(path))?; - - if status == Status::WT_NEW - || (status == Status::WT_MODIFIED | Status::INDEX_NEW) - { - fs::remove_file(Path::new(workdir).join(path))?; - }; - - if status == Status::WT_NEW { - return Ok(()); - } - - let mut checkout_opts = CheckoutBuilder::new(); - checkout_opts - .update_index(true) // windows: needs this to be true WTF?! - .force() - .path(path); - - repo.checkout_index(None, Some(&mut checkout_opts))?; - Ok(()) -} - -/// -pub fn reset_workdir_folder( - repo_path: &str, - path: &str, -) -> Result<()> { - scope_time!("reset_workdir_folder"); +pub fn reset_workdir(repo_path: &str, path: &str) -> Result<()> { + scope_time!("reset_workdir"); let repo = repo(repo_path)?; @@ -85,9 +49,7 @@ pub fn reset_workdir_folder( #[cfg(test)] mod tests { - use super::{ - reset_stage, reset_workdir_file, reset_workdir_folder, - }; + use super::{reset_stage, reset_workdir}; use crate::error::Result; use crate::sync::{ status::{get_status, StatusType}, @@ -165,7 +127,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 1)); - reset_workdir_file(repo_path, "bar.txt").unwrap(); + reset_workdir(repo_path, "bar.txt").unwrap(); debug_cmd_print(repo_path, "git status"); @@ -190,7 +152,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 0)); - reset_workdir_file(repo_path, "foo/bar.txt").unwrap(); + reset_workdir(repo_path, "foo/bar.txt").unwrap(); debug_cmd_print(repo_path, "git status"); @@ -235,7 +197,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (4, 1)); - reset_workdir_folder(repo_path, "foo").unwrap(); + reset_workdir(repo_path, "foo").unwrap(); assert_eq!(get_statuses(repo_path), (1, 1)); @@ -274,7 +236,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 1)); - reset_workdir_file(repo_path, file).unwrap(); + reset_workdir(repo_path, file).unwrap(); debug_cmd_print(repo_path, "git status"); @@ -322,7 +284,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 0)); - reset_workdir_file( + reset_workdir( &root.join("foo").as_os_str().to_str().unwrap(), "foo/bar.txt", ) @@ -351,7 +313,7 @@ mod tests { assert_eq!(get_statuses(repo_path), (1, 0)); - reset_workdir_folder(repo_path, "foo/bar").unwrap(); + reset_workdir(repo_path, "foo/bar").unwrap(); debug_cmd_print(repo_path, "git status"); diff --git a/src/tabs/status.rs b/src/tabs/status.rs index a8c3c890..e1a7e289 100644 --- a/src/tabs/status.rs +++ b/src/tabs/status.rs @@ -283,22 +283,18 @@ impl Status { /// called after confirmation pub fn reset(&mut self, item: &ResetItem) -> bool { - let res = if item.is_folder { - sync::reset_workdir_folder(CWD, item.path.as_str()) - } else { - sync::reset_workdir_file(CWD, item.path.as_str()) - }; - - if let Err(e) = &res { + if let Err(e) = sync::reset_workdir(CWD, item.path.as_str()) { self.queue.borrow_mut().push_back( InternalEvent::ShowErrorMsg(format!( "reset failed:\n{}", e )), ); - } - res.is_ok() + false + } else { + true + } } }