解决TODO&创建TODO

This commit is contained in:
HouXiaoxuan
2023-12-21 19:15:43 +08:00
parent c54f7896f4
commit 526210b1db
4 changed files with 25 additions and 21 deletions

View File

@@ -1,11 +1,11 @@
use std::env;
use std::path::{Path, PathBuf};
use colored::Colorize;
use sha1::Digest;
use crate::models::blob::Blob;
use crate::models::index::{FileMetaData, Index};
use crate::utils::util::{check_repo_exist, get_relative_path, get_working_dir, list_files};
use colored::Colorize;
use std::env;
use std::path::{Path, PathBuf};
// TODO: fatal: ../moj/app.py: '../moj/app.py' is outside repository at 'Git-Rust'
pub fn add(files: Vec<String>, all: bool, mut update: bool) {
check_repo_exist();
let mut index = Index::new();
@@ -13,8 +13,9 @@ pub fn add(files: Vec<String>, all: bool, mut update: bool) {
let mut files: Vec<PathBuf> = files.into_iter().map(PathBuf::from).collect();
if dot || all || update{
if all { // all 优先级最高
if dot || all || update {
if all {
// all 优先级最高
dot = false;
update = false;
} else if update {
@@ -31,9 +32,7 @@ pub fn add(files: Vec<String>, all: bool, mut update: bool) {
println!("Working on [{}]\n", path.to_str().unwrap().bright_blue());
files = list_files(&path).unwrap();
if update {
files.retain(|file|{
index.contains(file)
});
files.retain(|file| index.contains(file));
}
files.extend(index.get_deleted_files()); //包含已删除的文件
@@ -47,22 +46,28 @@ pub fn add(files: Vec<String>, all: bool, mut update: bool) {
fn add_a_file(file: &Path, index: &mut Index) {
let relative_path = get_relative_path(file, &get_working_dir().unwrap());
if !file.exists() { //文件被删除
if !file.exists() {
//文件被删除
index.remove(file);
println!("removed: {}", relative_path.display());
} else { //文件存在
if !index.contains(file) { //文件未被跟踪
} else {
//文件存在
if !index.contains(file) {
//文件未被跟踪
let blob = Blob::new(file);
index.add(file.to_path_buf(), FileMetaData::new(&blob, file));
println!("add(stage): {}", relative_path.display());
} else { //文件已被跟踪,可能被修改
if index.is_modified(file) { //文件被修改,但不一定内容更
} else {
//文件已被跟踪,可能被修
if index.is_modified(file) {
//文件被修改,但不一定内容更改
let blob = Blob::new(file); //到这一步才创建blob是为了优化
if !index.verify_hash(file, &blob.get_hash()) { //比较hash 确认内容更改
if !index.verify_hash(file, &blob.get_hash()) {
//比较hash 确认内容更改
index.update(file.to_path_buf(), FileMetaData::new(&blob, file));
println!("add(modified): {}", relative_path.display());
}
}
}
}
}
}

View File

@@ -22,7 +22,7 @@ pub fn changes_to_be_committed() -> Changes {
let tracked_files = index
.get_tracked_files()
.iter()
.map(|f| util::to_root_relative_path(f))
.map(|f| util::to_workdir_relative_path(f))
.collect::<Vec<PathBuf>>();
if head_hash == "" {
// 初始提交
@@ -43,7 +43,7 @@ pub fn changes_to_be_committed() -> Changes {
change.modified.push(tree_file.clone());
}
} else {
change.deleted.push(tree_file.clone()); //todo: abs_path?
change.deleted.push(tree_file.clone());
}
}
for index_file in index_files.iter() {