mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-02-04 02:43:54 +08:00
fix: is_dir依赖文件存在性,在restore中删除使用
This commit is contained in:
@@ -16,8 +16,8 @@ use crate::{
|
||||
};
|
||||
|
||||
/// 统计[工作区]中的dirs文件夹中,相对于target_blobs已删除的文件
|
||||
fn get_worktree_deleted_files_in_dirs(
|
||||
dirs: &HashSet<PathBuf>,
|
||||
fn get_worktree_deleted_files_in_paths(
|
||||
filters: &Vec<PathBuf>,
|
||||
target_blobs: &HashMap<PathBuf, Hash>,
|
||||
) -> HashSet<PathBuf> {
|
||||
target_blobs //统计所有目录中(包括None & '.'),删除的文件
|
||||
@@ -25,8 +25,8 @@ fn get_worktree_deleted_files_in_dirs(
|
||||
.filter(|(path, _)| {
|
||||
assert!(path.is_absolute()); //
|
||||
if !path.exists() {
|
||||
for dir in dirs {
|
||||
if util::is_parent_dir(path, dir) {
|
||||
for filter in filters {
|
||||
if util::is_parent_dir(path, filter) {
|
||||
//需要包含在指定dir内
|
||||
return true;
|
||||
}
|
||||
@@ -39,9 +39,9 @@ fn get_worktree_deleted_files_in_dirs(
|
||||
}
|
||||
|
||||
/// 统计[暂存区index]中相对于target_blobs已删除的文件,且包含在指定dirs内
|
||||
fn get_index_deleted_files_in_dirs(
|
||||
fn get_index_deleted_files_in_paths(
|
||||
index: &Index,
|
||||
dirs: &HashSet<PathBuf>,
|
||||
filters: &Vec<PathBuf>,
|
||||
target_blobs: &HashMap<PathBuf, Hash>,
|
||||
) -> HashSet<PathBuf> {
|
||||
target_blobs //统计index中相对target已删除的文件,且包含在指定dir内
|
||||
@@ -50,8 +50,8 @@ fn get_index_deleted_files_in_dirs(
|
||||
assert!(path.is_absolute()); //
|
||||
if !index.contains(path) {
|
||||
//index中不存在
|
||||
for dir in dirs {
|
||||
if util::is_parent_dir(path, dir) {
|
||||
for filter in filters {
|
||||
if util::is_parent_dir(path, filter) {
|
||||
//需要包含在指定dir内
|
||||
return true;
|
||||
}
|
||||
@@ -85,8 +85,7 @@ pub fn restore_worktree(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathB
|
||||
let input_paths = preprocess_filters(filter); //预处理filter 将None转化为workdir
|
||||
let target_blobs = preprocess_blobs(target_blobs); //预处理target_blobs 转化为绝对路径HashMap
|
||||
|
||||
let dirs = util::filter_dirs(&input_paths); //统计所有目录
|
||||
let deleted_files = get_worktree_deleted_files_in_dirs(&dirs, &target_blobs); //统计所有目录中已删除的文件
|
||||
let deleted_files = get_worktree_deleted_files_in_paths(&input_paths, &target_blobs); //统计所有目录中已删除的文件
|
||||
|
||||
let mut file_paths = util::integrate_paths(&input_paths); //整合存在的文件(绝对路径)
|
||||
file_paths.extend(deleted_files); //已删除的文件
|
||||
@@ -95,7 +94,7 @@ pub fn restore_worktree(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathB
|
||||
let store = Store::new();
|
||||
|
||||
for path in &file_paths {
|
||||
assert!(path.is_absolute() && !path.is_dir()); // 绝对路径且不是目录
|
||||
assert!(path.is_absolute()); // 绝对路径且不是目录
|
||||
if !path.exists() {
|
||||
//文件不存在于workdir
|
||||
if target_blobs.contains_key(path) {
|
||||
@@ -130,29 +129,23 @@ pub fn restore_index(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathBuf,
|
||||
let target_blobs = preprocess_blobs(target_blobs); //预处理target_blobs 转化为绝对路径HashMap
|
||||
|
||||
let mut index = Index::new();
|
||||
let deleted_files_index = get_index_deleted_files_in_paths(&index, &input_paths, &target_blobs); //统计所有目录中已删除的文件
|
||||
|
||||
let dirs = util::filter_dirs(&input_paths); //统计所有目录
|
||||
let deleted_files_index = get_index_deleted_files_in_dirs(&index, &dirs, &target_blobs); //统计所有目录中已删除的文件
|
||||
|
||||
// 1. 获取输入中的[文件路径]
|
||||
let mut file_paths: HashSet<PathBuf> = util::filter_files(&input_paths)
|
||||
.iter()
|
||||
.map(|path| util::get_absolute_path(path))
|
||||
.collect();
|
||||
// 2.获取index中包含于dirs的文件(使用dirs进行筛选)
|
||||
let mut file_paths = HashSet::new();
|
||||
// 1.获取index中包含于input_path的文件
|
||||
for index_file in index.get_tracked_files() {
|
||||
for dir in &dirs {
|
||||
if util::is_parent_dir(&index_file, dir) {
|
||||
for path in &input_paths {
|
||||
if util::is_parent_dir(&index_file, path) {
|
||||
//需要包含在指定dir内
|
||||
file_paths.insert(index_file.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3.补充index中已删除的文件(相较于target_blobs)
|
||||
// 2.补充index中已删除的文件(相较于target_blobs)
|
||||
file_paths.extend(deleted_files_index); //已删除的文件
|
||||
|
||||
for path in &file_paths {
|
||||
assert!(path.is_absolute() && !path.is_dir()); // 绝对路径且不是目录
|
||||
assert!(path.is_absolute()); // 绝对路径且不是目录
|
||||
if !index.contains(path) {
|
||||
//文件不存在于index
|
||||
if target_blobs.contains_key(path) {
|
||||
|
||||
@@ -147,9 +147,8 @@ pub fn is_inside_dir(file: &Path, dir: &Path) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// 检测dir是否是file的父目录 (不论文件是否存在)
|
||||
/// 检测dir是否是file的父目录 (不论文件是否存在) dir可以是一个文件
|
||||
pub fn is_parent_dir(file: &Path, dir: &Path) -> bool {
|
||||
assert!(dir.is_dir());
|
||||
let file = get_absolute_path(file);
|
||||
let dir = get_absolute_path(dir);
|
||||
file.starts_with(dir)
|
||||
@@ -170,15 +169,6 @@ pub fn format_time(time: &std::time::SystemTime) -> String {
|
||||
datetime.format("%Y-%m-%d %H:%M:%S.%3f").to_string()
|
||||
}
|
||||
|
||||
/// 过滤出路径数组中的目录
|
||||
pub fn filter_dirs(paths: &Vec<PathBuf>) -> HashSet<PathBuf> {
|
||||
paths.iter().filter(|path| path.is_dir()).cloned().collect()
|
||||
}
|
||||
|
||||
pub fn filter_files(paths: &Vec<PathBuf>) -> HashSet<PathBuf> {
|
||||
paths.iter().filter(|path| path.is_file()).cloned().collect()
|
||||
}
|
||||
|
||||
/// 将路径中的分隔符统一为当前系统的分隔符
|
||||
fn unify_path_separator(path: &Path) -> PathBuf {
|
||||
#[cfg(windows)]
|
||||
|
||||
Reference in New Issue
Block a user