diff --git a/src/commands/commit.rs b/src/commands/commit.rs index 6422286..9080ea6 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -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!("工作区没有任何改动,不需要提交"); } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7219386..0ea1d0c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,3 +1,4 @@ pub mod init; pub mod add; -pub mod commit; \ No newline at end of file +pub mod commit; +pub mod status; \ No newline at end of file diff --git a/src/commands/status.rs b/src/commands/status.rs new file mode 100644 index 0000000..e36b89b --- /dev/null +++ b/src/commands/status.rs @@ -0,0 +1,61 @@ +use std::path::PathBuf; + +use crate::{head, models::index, utils::util}; + +/** 获取需要commit的更改 */ +pub struct Changes { + pub new: Vec, + pub modified: Vec, + pub deleted: Vec, +} + +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 = 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!() +} diff --git a/src/utils/util.rs b/src/utils/util.rs index 46c6771..8d83ddd 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -154,6 +154,7 @@ pub fn list_files(path: &Path) -> io::Result> { } /// 获取相对于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)