status 实现一半

This commit is contained in:
HouXiaoxuan
2023-12-21 03:20:04 +08:00
parent ea9592d152
commit 0c154163ed
4 changed files with 65 additions and 2 deletions

View File

@@ -3,7 +3,7 @@ use crate::models::{commit, index};
pub fn commit(message: String, allow_enpty: bool) {
let index = index::Index::new();
// XXX true 需要替换为 index.is_empty()
// XXX true 需要替换为 status::changes_to_be_committed()
if false && !allow_enpty {
println!("工作区没有任何改动,不需要提交");
}

View File

@@ -1,3 +1,4 @@
pub mod init;
pub mod add;
pub mod commit;
pub mod commit;
pub mod status;

61
src/commands/status.rs Normal file
View File

@@ -0,0 +1,61 @@
use std::path::PathBuf;
use crate::{head, models::index, utils::util};
/** 获取需要commit的更改 */
pub struct Changes {
pub new: Vec<String>,
pub modified: Vec<String>,
pub deleted: Vec<String>,
}
fn __file_string(path: &PathBuf) -> String {
util::to_root_relative_path(&path)
.as_os_str()
.to_str()
.unwrap()
.to_string()
}
pub fn changes_to_be_committed() -> Changes {
let mut change = Changes {
new: vec![],
modified: vec![],
deleted: vec![],
};
let index = index::Index::new();
let head_hash = head::current_head_commit();
if head_hash == "".to_string() {
// 初始提交
change.new = index
.get_tracked_files()
.iter()
.map(|f| __file_string(f))
.collect();
return change;
}
let commit = crate::models::commit::Commit::load(&head_hash);
let tree = commit.get_tree();
let mut tree_files = tree.get_recursive_blobs();
let mut index_files: Vec<PathBuf> = index
.get_tracked_files()
.iter()
.map(|f| util::to_root_relative_path(f))
.collect();
for tree_item in tree_files.iter() {
if index_files.contains(&tree_item.0) {
// 比较文件内容
// XXX @mrbeanc 我看到Blob的new被改成调用save了。这里的实现希望比较Blob内容不然就得读取文件内容。
} else {
change.deleted.push(__file_string(&tree_item.0));
}
}
change
}
pub fn status() {
unimplemented!()
}

View File

@@ -154,6 +154,7 @@ pub fn list_files(path: &Path) -> io::Result<Vec<PathBuf>> {
}
/// 获取相对于dir的相对路径
/// XXX 是否只能用在windows是否检查dir是否是path的父目录
pub fn get_relative_path(path: &Path, dir: &Path) -> PathBuf {
let path = if path.is_relative() {
get_absolute_path(path)