From 67cf7e4d98716bf3e383dafe454d970147483795 Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Tue, 2 Jan 2024 15:20:05 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9Astatus=E6=98=BE=E7=A4=BA=E6=89=80?= =?UTF-8?q?=E6=9C=89=E6=9B=B4=E6=94=B9=EF=BC=8C=E8=80=8C=E9=9D=9E=E4=BB=85?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E7=9B=AE=E5=BD=95=20(cherry=20picked=20from?= =?UTF-8?q?=20commit=202ec4b394920caa27661c2493fbc554c5b8459c98)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 2 +- src/commands/status.rs | 14 ++++++++++---- src/utils/util.rs | 16 ++++++++++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 14a7e17..188253a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mit" # mini_git -version = "0.2.0" +version = "1.0.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/commands/status.rs b/src/commands/status.rs index f3d705d..e75a352 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -32,8 +32,9 @@ impl Changes { } /// 使用paths过滤,返回相对路径(to cur_dir) + ///
注意,如果paths为空,则返回空 pub fn filter_relative(&self, paths: &Vec) -> Changes { - self.filter_abs(paths).to_relative() + self.filter_abs(paths).to_relative_from_abs() } /// 转换为绝对路径(from workdir相对路径) @@ -52,7 +53,7 @@ impl Changes { } /// 转换为相对路径(to cur_dir)注意:要先转换为绝对路径 - fn to_relative(&self) -> Changes { + fn to_relative_from_abs(&self) -> Changes { let mut change = self.clone(); [&mut change.new, &mut change.modified, &mut change.deleted] .iter_mut() @@ -61,6 +62,11 @@ impl Changes { }); change } + + ///转换为相对路径(to cur_dir) + pub fn to_relative(&self) -> Changes { + self.to_absolute().to_relative_from_abs() + } } /** 比较暂存区与HEAD(最后一次Commit::Tree)的差异 @@ -146,8 +152,8 @@ pub fn status() { } // 对当前目录进行过滤 & 转换为相对路径 - let staged = changes_to_be_committed().filter_relative(&vec![util::cur_dir()]); - let unstaged = changes_to_be_staged().filter_relative(&vec![util::cur_dir()]); + let staged = changes_to_be_committed().to_relative(); + let unstaged = changes_to_be_staged().to_relative(); if staged.is_empty() && unstaged.is_empty() { println!("nothing to commit, working tree clean"); return; diff --git a/src/utils/util.rs b/src/utils/util.rs index e5dc13b..393bd35 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -96,6 +96,7 @@ pub fn is_sub_path(path: &Path, parent: &Path) -> bool { } /// 判断是否在paths中(包括子目录),不检查存在性 +///
注意,如果paths为空,则返回false pub fn include_in_paths(path: &Path, paths: U) -> bool where T: AsRef, @@ -345,8 +346,6 @@ pub fn get_absolute_path_to_dir(path: &Path, dir: &Path) -> PathBuf { path.to_path_buf() } else { //相对路径 - /*let abs_path = path.canonicalize().unwrap(); //这一步会统一路径分隔符 //canonicalize()不能处理不存在的文件 - clean_win_abs_path_pre(abs_path)*/ // 所以决定手动解析相对路径中的../ ./ let mut abs_path = dir.to_path_buf(); // 这里会拆分所有组件,所以会自动统一路径分隔符 @@ -410,6 +409,19 @@ pub fn is_typeof_commit(hash: Hash) -> bool { check_object_type(hash) == ObjectType::Commit } +/// 将内容对应的文件内容(主要是blob)还原到file +pub fn write_workfile(content: String, file: &PathBuf) { + let mut parent = file.clone(); + parent.pop(); + std::fs::create_dir_all(parent).unwrap(); + std::fs::write(file, content).unwrap(); +} + +/// 从工作区读取文件内容 +pub fn read_workfile(file: &Path) -> String { + std::fs::read_to_string(file).unwrap() +} + #[cfg(test)] mod tests { use crate::{