mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-04-29 05:09:47 +08:00
将Index改为单例模式,防止状态不一致; 注意:测试为单进程,需要reset防止共享单例
This commit is contained in:
@@ -31,13 +31,12 @@ pub fn add(raw_paths: Vec<String>, all: bool, mut update: bool) {
|
||||
println!("{}", "--update 只对已跟踪文件进行操作 不包含new".bright_green());
|
||||
}
|
||||
|
||||
let mut index = Index::new();
|
||||
for file in &files {
|
||||
add_a_file(file, &mut index);
|
||||
add_a_file(file);
|
||||
}
|
||||
}
|
||||
|
||||
fn add_a_file(file: &Path, index: &mut Index) {
|
||||
fn add_a_file(file: &Path) {
|
||||
let workdir = util::get_working_dir().unwrap();
|
||||
if !util::is_sub_path(file, &workdir) {
|
||||
//文件不在工作区内
|
||||
@@ -50,6 +49,7 @@ fn add_a_file(file: &Path, index: &mut Index) {
|
||||
return;
|
||||
}
|
||||
|
||||
let index = Index::get_instance();
|
||||
let rel_path = util::to_cur_relative_path(file);
|
||||
if !file.exists() {
|
||||
//文件被删除
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::{head, models::*};
|
||||
use super::status;
|
||||
|
||||
pub fn commit(message: String, allow_empty: bool) {
|
||||
let index = Index::new();
|
||||
let index = Index::get_instance();
|
||||
if !allow_empty && status::changes_to_be_committed().is_empty() {
|
||||
panic!("工作区没有任何改动,不需要提交");
|
||||
}
|
||||
@@ -13,9 +13,9 @@ pub fn commit(message: String, allow_empty: bool) {
|
||||
|
||||
let mut commit = {
|
||||
if current_commit_hash.is_empty() {
|
||||
Commit::new(&index, vec![], message.clone())
|
||||
Commit::new(index, vec![], message.clone())
|
||||
} else {
|
||||
Commit::new(&index, vec![current_commit_hash.clone()], message.clone())
|
||||
Commit::new(index, vec![current_commit_hash.clone()], message.clone())
|
||||
}
|
||||
};
|
||||
let commit_hash = commit.save();
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::{fs, io, path::PathBuf};
|
||||
/// 从暂存区&|工作区删除文件
|
||||
pub fn remove(files: Vec<String>, cached: bool, recursive: bool) -> io::Result<()> {
|
||||
util::check_repo_exist();
|
||||
let mut index = Index::new();
|
||||
let index = Index::get_instance();
|
||||
for file in files.iter() {
|
||||
let path = PathBuf::from(file);
|
||||
if !path.exists() {
|
||||
|
||||
@@ -64,7 +64,7 @@ pub fn restore_worktree(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathB
|
||||
let mut file_paths = util::integrate_paths(&input_paths); //根据用户输入整合存在的文件(绝对路径)
|
||||
file_paths.extend(deleted_files); //已删除的文件
|
||||
|
||||
let index = Index::new();
|
||||
let index = Index::get_instance();
|
||||
let store = Store::new();
|
||||
|
||||
for path in &file_paths {
|
||||
@@ -103,7 +103,7 @@ pub fn restore_index(filter: Option<&Vec<PathBuf>>, target_blobs: &Vec<(PathBuf,
|
||||
let input_paths = preprocess_filters(filter); //预处理filter 将None转化为workdir
|
||||
let target_blobs = preprocess_blobs(target_blobs); //预处理target_blobs 转化为绝对路径HashMap
|
||||
|
||||
let mut index = Index::new();
|
||||
let index = Index::get_instance();
|
||||
let deleted_files_index = get_index_deleted_files_in_filters(&index, &input_paths, &target_blobs); //统计已删除的文件
|
||||
|
||||
//1.获取index中包含于input_path的文件(使用paths进行过滤)
|
||||
@@ -181,7 +181,7 @@ pub fn restore(paths: Vec<String>, source: Option<String>, worktree: bool, stage
|
||||
otherwise from the [index].*/
|
||||
if source.is_none() && !staged {
|
||||
// 没有指定source,且没有指定--staged,从[index]中恢复到worktree //只有这种情况是从[index]恢复
|
||||
let entries = Index::new().get_tracked_entries();
|
||||
let entries = Index::get_instance().get_tracked_entries();
|
||||
entries.into_iter().map(|(p, meta)| (p, meta.hash)).collect()
|
||||
} else {
|
||||
//从[target_commit]中恢复
|
||||
@@ -224,7 +224,7 @@ mod test {
|
||||
util::ensure_no_file(&path);
|
||||
cmd::add(vec![], true, false); //add -A
|
||||
cmd::restore(vec![".".to_string()], Some("HEAD".to_string()), false, true);
|
||||
let index = Index::new();
|
||||
let index = Index::get_instance();
|
||||
assert!(index.get_tracked_files().is_empty());
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ impl Changes {
|
||||
*/
|
||||
pub fn changes_to_be_committed() -> Changes {
|
||||
let mut change = Changes::default();
|
||||
let index = Index::new();
|
||||
let index = Index::get_instance();
|
||||
let head_hash = head::current_head_commit();
|
||||
let tracked_files = index
|
||||
.get_tracked_files()
|
||||
@@ -111,7 +111,7 @@ pub fn changes_to_be_committed() -> Changes {
|
||||
/// 比较工作区与暂存区的差异,返回相对路径(to workdir),不筛选
|
||||
pub fn changes_to_be_staged() -> Changes {
|
||||
let mut change = Changes::default();
|
||||
let index = Index::new();
|
||||
let index = Index::get_instance();
|
||||
for file in index.get_tracked_files() {
|
||||
if !file.exists() {
|
||||
change.deleted.push(util::to_workdir_relative_path(&file));
|
||||
|
||||
Reference in New Issue
Block a user