mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-03-25 14:30:32 +08:00
实现Index析构自动保存:Drop for Index
This commit is contained in:
@@ -42,8 +42,6 @@ pub fn add(files: Vec<String>, all: bool, mut update: bool) {
|
||||
for file in &files {
|
||||
add_a_file(file, &mut index);
|
||||
}
|
||||
|
||||
index.save();
|
||||
}
|
||||
|
||||
fn add_a_file(file: &Path, index: &mut Index) {
|
||||
|
||||
@@ -65,6 +65,10 @@ pub fn changes_to_be_committed() -> Changes {
|
||||
change
|
||||
}
|
||||
|
||||
pub fn changes_to_be_staged() {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
/** 分为两个部分
|
||||
1. unstaged: 暂存区与工作区比较
|
||||
2. staged to be committed: 暂存区与HEAD(最后一次Commit::Tree)比较,即上次的暂存区
|
||||
|
||||
@@ -7,6 +7,7 @@ use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::SystemTime;
|
||||
use colored::Colorize;
|
||||
|
||||
// 文件元数据结构
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
@@ -65,7 +66,8 @@ impl Index {
|
||||
|
||||
// 删除文件
|
||||
pub fn remove(&mut self, path: &Path) {
|
||||
self.entries.remove(path);
|
||||
let path = Index::preprocess_path(&path);
|
||||
self.entries.remove(&path);
|
||||
}
|
||||
|
||||
// 获取文件元数据
|
||||
@@ -172,6 +174,14 @@ impl Index {
|
||||
}
|
||||
}
|
||||
|
||||
/// 析构自动保存
|
||||
impl Drop for Index {
|
||||
fn drop(&mut self) {
|
||||
self.save();
|
||||
println!("{}", "Index auto saved".bright_green());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user