From ba7797ece1d7a465edcf4e52d2ba4460d4ea113f Mon Sep 17 00:00:00 2001 From: HouXiaoxuan Date: Thu, 21 Dec 2023 02:14:59 +0800 Subject: [PATCH] =?UTF-8?q?commit=E6=93=8D=E4=BD=9C=E5=88=9D=E7=89=88?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli.rs | 5 +++-- src/commands/commit.rs | 18 +++++++++++++++--- src/head.rs | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index db95778..e5bd748 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,5 +1,6 @@ use clap::{Parser, Subcommand}; use mit::commands::add::add; +use mit::commands::commit::commit; use mit::commands::init::init; /// Rust实现的简易版本的Git,用于学习Rust语言 @@ -52,7 +53,7 @@ pub fn handle_command() { Command::Init => { let _ = init(); } - Command::Add { files , all, update} => { + Command::Add { files, all, update } => { add(files, all, update); } Command::Rm { files, cached } => { @@ -62,7 +63,7 @@ pub fn handle_command() { message, allow_empty, } => { - println!("commit: {:?}, allow empty={:?}", message, allow_empty); + commit(message, allow_empty); } } } diff --git a/src/commands/commit.rs b/src/commands/commit.rs index 106a71c..302e9cc 100644 --- a/src/commands/commit.rs +++ b/src/commands/commit.rs @@ -1,16 +1,28 @@ use crate::head; use crate::models::{commit, index}; -// XXX NOT TESTED pub fn commit(message: String, allow_enpty: bool) { let index = index::Index::new(); // XXX true 需要替换为 index.is_empty() - if true && !allow_enpty { + if false && !allow_enpty { println!("工作区没有任何改动,不需要提交"); } + let current_head = head::current_head(); let current_commit_hash = head::current_head_commit(); - let mut commit = commit::Commit::new(&index, vec![current_commit_hash], message); + let mut commit = + commit::Commit::new(&index, vec![current_commit_hash.clone()], message.clone()); let commit_hash = commit.save(); head::update_head_commit(&commit_hash); + + { + let commit_target = { + match current_head { + head::Head::Branch(branch_name) => branch_name, + head::Head::Detached(commit_hash) => commit_hash[..6].to_string(), + } + }; + println!("commit to [{:?}] message{:?}", commit_target, message); + println!("commit hash: {:?}", commit_hash); + } } diff --git a/src/head.rs b/src/head.rs index 2de4fcc..0e6806e 100644 --- a/src/head.rs +++ b/src/head.rs @@ -5,7 +5,7 @@ pub enum Head { Branch(String), } -fn current_head() -> Head { +pub fn current_head() -> Head { let mut head = util::get_storage_path().unwrap(); head.push("HEAD"); let head_content = std::fs::read_to_string(head).expect("HEAD文件损坏");