From ffefd7f9814316e75344d98880f663927aee7c5c Mon Sep 17 00:00:00 2001 From: mrbeanc Date: Thu, 21 Dec 2023 17:12:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0Index=E6=9E=90=E6=9E=84?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=BF=9D=E5=AD=98=EF=BC=9ADrop=20for=20Index?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/add.rs | 2 -- src/commands/status.rs | 4 ++++ src/models/index.rs | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index a43f1d6..bf920ab 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -42,8 +42,6 @@ pub fn add(files: Vec, 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) { diff --git a/src/commands/status.rs b/src/commands/status.rs index 02f200e..5aab088 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -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)比较,即上次的暂存区 diff --git a/src/models/index.rs b/src/models/index.rs index 5ada61a..203375b 100644 --- a/src/models/index.rs +++ b/src/models/index.rs @@ -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::*;