From a459a0922e7df91a36f14151f9c19726c5110af9 Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Thu, 21 Dec 2023 14:25:40 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E5=8E=BB=E9=99=A4current=5Fhead()?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=80=BC=E4=B8=AD=E7=9A=84\n=20\n=E4=BC=9A?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=96=87=E4=BB=B6=E5=86=99=E5=85=A5=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli.rs | 5 +---- src/commands/commit.rs | 6 +++--- src/commands/status.rs | 21 +++++++++++++-------- src/head.rs | 4 ++-- src/models/index.rs | 2 +- src/models/tree.rs | 4 ++-- src/utils/util.rs | 3 ++- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index e5bd748..e03ffd3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -59,10 +59,7 @@ pub fn handle_command() { Command::Rm { files, cached } => { println!("rm: {:?}, cached= {}", files, cached); } - Command::Commit { - message, - allow_empty, - } => { + Command::Commit { message, allow_empty, } => { commit(message, allow_empty); } } diff --git a/src/commands/commit.rs b/src/commands/commit.rs index 325ad10..02a8686 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -3,13 +3,13 @@ use crate::models::{commit, index}; use super::status; -fn no_change() -> bool { +fn no_change() -> bool { //todo: move to status.rs let change = status::changes_to_be_committed(); change.new.len() == 0 && change.modified.len() == 0 && change.deleted.len() == 0 } -pub fn commit(message: String, allow_enpty: bool) { +pub fn commit(message: String, allow_empty: bool) { let index = index::Index::new(); - if no_change() && !allow_enpty { + if no_change() && !allow_empty { panic!("工作区没有任何改动,不需要提交"); } diff --git a/src/commands/status.rs b/src/commands/status.rs index 8aa91be..605cc60 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -6,7 +6,8 @@ use crate::{ utils::util, }; -/** 获取需要commit的更改 */ +/** 获取需要commit的更改(staged) */ +#[derive(Debug)] pub struct Changes { pub new: Vec, pub modified: Vec, @@ -14,7 +15,7 @@ pub struct Changes { } fn __file_string(path: &PathBuf) -> String { - util::to_root_relative_path(&path) + util::to_root_relative_path(&path) //todo: to_string_lossy() .as_os_str() .to_str() .unwrap() @@ -22,14 +23,14 @@ fn __file_string(path: &PathBuf) -> String { } pub fn changes_to_be_committed() -> Changes { - let mut change = Changes { + let mut change = Changes { //todo: Changes::default() new: vec![], modified: vec![], deleted: vec![], }; let index = index::Index::new(); let head_hash = head::current_head_commit(); - if head_hash == "".to_string() { + if head_hash == "".to_string() { //todo: head_hash.is_empty() or head_hash == "" // 初始提交 change.new = index .get_tracked_files() @@ -51,10 +52,10 @@ pub fn changes_to_be_committed() -> Changes { for tree_item in tree_files.iter() { let index_file = index_files.iter().find(|f| **f == tree_item.0); if index_file.is_none() { - change.deleted.push(__file_string(&tree_item.0)); + change.deleted.push(__file_string(&tree_item.0)); //todo: abs_path? } else { - let index_blob = blob::Blob::new( - util::get_working_dir() + let index_blob = blob::Blob::new( //todo: index有函数可以获取blob_hash 不需要new + util::get_working_dir() //todo: 优化:提取为变量 .unwrap() .join(index_file.unwrap()) .as_path(), @@ -74,6 +75,10 @@ pub fn changes_to_be_committed() -> Changes { change } +/** 分为两个部分 +1. unstaged: 暂存区与工作区比较 +2. staged to be committed: 暂存区与HEAD(最后一次Commit::Tree)比较,即上次的暂存区 + */ pub fn status() { unimplemented!() } @@ -92,7 +97,7 @@ mod tests { commit::commit("test commit".to_string(), true); let mut index = index::Index::new(); - index.add( + index.add( //todo 可以直接调用add函数 PathBuf::from(test_file), index::FileMetaData::new(&blob::Blob::new(Path::new(test_file)), Path::new(test_file)), ); diff --git a/src/head.rs b/src/head.rs index 0e6806e..026425c 100644 --- a/src/head.rs +++ b/src/head.rs @@ -8,7 +8,7 @@ pub enum Head { pub fn current_head() -> Head { let mut head = util::get_storage_path().unwrap(); head.push("HEAD"); - let head_content = std::fs::read_to_string(head).expect("HEAD文件损坏"); + let head_content = std::fs::read_to_string(head).expect("HEAD文件损坏").trim_end().to_string(); //去除末尾\n if head_content.starts_with("ref: refs/heads/") { let branch_name = head_content.trim_start_matches("ref: refs/heads/"); Head::Branch(branch_name.to_string()) @@ -22,7 +22,7 @@ fn update_branch_head(branch_name: &String, commit_hash: &String) { branch.push("refs"); branch.push("heads"); branch.push(branch_name); - std::fs::write(branch, commit_hash).expect("无法写入branch"); + std::fs::write(&branch, commit_hash).expect(&format!("无法写入branch in {:?} with {}", branch, commit_hash)); } fn get_branch_head(branch_name: &String) -> String { diff --git a/src/models/index.rs b/src/models/index.rs index 8ed5bb9..8209d32 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -170,7 +170,7 @@ impl Index { pub fn get_tracked_files(&self) -> Vec { let mut files = Vec::new(); self.entries.keys().for_each(|file| { - if file.exists() { + if file.exists() { //todo: cancel this check files.push(file.clone()); } }); diff --git a/src/models/tree.rs b/src/models/tree.rs index 485212e..e0ddf88 100644 --- a/src/models/tree.rs +++ b/src/models/tree.rs @@ -141,7 +141,7 @@ impl Tree { let mut blobs = Vec::new(); for entry in self.entries.iter() { if entry.filemode.0 == "blob" { - let blob = super::blob::Blob::load(&entry.object_hash); + let blob = super::blob::Blob::load(&entry.object_hash); //todo: hash only blobs.push((PathBuf::from(entry.name.clone()), blob)); } else { let sub_tree = Tree::load(&entry.object_hash); @@ -151,7 +151,7 @@ impl Tree { sub_blobs .iter() .map(|(path, blob)| { - (PathBuf::from(entry.name.clone()).join(path), blob.clone()) + (PathBuf::from(entry.name.clone()).join(path), blob.clone()) //todo: why join? }) .collect::>() .as_mut(), diff --git a/src/utils/util.rs b/src/utils/util.rs index 8d83ddd..f8c3a07 100644 --- a/src/utils/util.rs +++ b/src/utils/util.rs @@ -165,7 +165,8 @@ pub fn get_relative_path(path: &Path, dir: &Path) -> PathBuf { relative_path.to_path_buf() } -pub fn to_root_relative_path(path: &Path) -> PathBuf { +/// 获取相较于工作区(Working Dir)的相对路径 +pub fn to_root_relative_path(path: &Path) -> PathBuf { //todo: rename get_relative_path(path, &get_working_dir().unwrap()) }