mirror of
https://github.com/gitui-org/gitui
synced 2026-05-23 17:08:21 +00:00
remove some duplication in reset
This commit is contained in:
parent
fdd84a8692
commit
6ebe717fb1
3 changed files with 17 additions and 61 deletions
|
|
@ -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::{
|
||||
|
|
|
|||
|
|
@ -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");
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue