mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-03-25 22:40:52 +08:00
fix:去除current_head()返回值中的\n
\n会导致文件写入失败
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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!("工作区没有任何改动,不需要提交");
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ use crate::{
|
||||
utils::util,
|
||||
};
|
||||
|
||||
/** 获取需要commit的更改 */
|
||||
/** 获取需要commit的更改(staged) */
|
||||
#[derive(Debug)]
|
||||
pub struct Changes {
|
||||
pub new: Vec<String>,
|
||||
pub modified: Vec<String>,
|
||||
@@ -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)),
|
||||
);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -170,7 +170,7 @@ impl Index {
|
||||
pub fn get_tracked_files(&self) -> Vec<PathBuf> {
|
||||
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());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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::<Vec<(PathBuf, super::blob::Blob)>>()
|
||||
.as_mut(),
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user