From 2dc58245e7f2dccf0ed78af6401fbe7fb5ee73e5 Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 21 Dec 2023 03:48:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=95=B4=E5=AE=9E=E7=8E=B0commit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/commit.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/commands/commit.rs b/src/commands/commit.rs index 9080ea6..325ad10 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -1,11 +1,16 @@ use crate::head; use crate::models::{commit, index}; +use super::status; + +fn no_change() -> bool { + 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) { let index = index::Index::new(); - // XXX true 需要替换为 status::changes_to_be_committed() - if false && !allow_enpty { - println!("工作区没有任何改动,不需要提交"); + if no_change() && !allow_enpty { + panic!("工作区没有任何改动,不需要提交"); } let current_head = head::current_head(); @@ -27,3 +32,16 @@ pub fn commit(message: String, allow_enpty: bool) { println!("commit hash: {:?}", commit_hash); } } + +#[cfg(test)] +mod test { + use crate::utils::util; + + #[test] + #[should_panic] + fn test_commit_empty() { + util::setup_test_with_clean_mit(); + + super::commit("".to_string(), false); + } +}