git log实现与测试

This commit is contained in:
HouXiaoxuan
2023-12-21 23:01:05 +08:00
parent a8707149a9
commit f9e7ef493a
4 changed files with 112 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ use clap::{Parser, Subcommand};
use mit::commands::add::add;
use mit::commands::commit::commit;
use mit::commands::init::init;
use mit::commands::log::log;
use mit::commands::remove::remove;
/// Rust实现的简易版本的Git用于学习Rust语言
@@ -50,6 +51,14 @@ enum Command {
#[clap(long, action)]
allow_empty: bool,
},
/// log 现实提交历史
Log {
#[clap(short = 'A', long)]
all: bool,
#[clap(short, long)]
number: Option<usize>,
},
}
pub fn handle_command() {
let cli = Cli::parse();
@@ -66,5 +75,8 @@ pub fn handle_command() {
Command::Commit { message, allow_empty } => {
commit(message, allow_empty);
}
Command::Log { all, number } => {
log(all, number);
}
}
}

97
src/commands/log.rs Normal file
View File

@@ -0,0 +1,97 @@
use crate::{head, models::commit::Commit};
use colored::Colorize;
use core::num;
use std::option;
const DEFAULT_LOG_NUMBER: usize = 10;
pub fn log(all: bool, number: Option<usize>) {
println!("log all: {:?}, number: {:?}", all, number);
let _ = __log(all, number);
}
pub fn __log(all: bool, number: Option<usize>) -> usize {
let mut log_count = 0usize;
let head = head::current_head();
let mut branch_name: Option<String> = None;
let mut head_commit = match head {
head::Head::Branch(_branch_name) => {
let commit = head::get_branch_head(&_branch_name);
branch_name = Some(_branch_name.clone());
if commit.is_empty() {
println!("当前分支{:?}没有任何提交", _branch_name);
return 0;
}
commit
}
head::Head::Detached(commit_hash) => commit_hash,
};
let mut number = match number {
Some(number) => number,
None => DEFAULT_LOG_NUMBER,
};
let mut first = true;
loop {
log_count += 1;
let commit = Commit::load(&head_commit);
if first {
first = false;
print!(
"{}{}{}{}",
"commit ".yellow(),
commit.get_hash().yellow(),
"(".yellow(),
"HEAD".blue()
);
if let Some(ref branch_name) = branch_name {
print!("{}", format!(" -> {}", branch_name).blue());
}
println!("{}", ")".yellow());
} else {
println!(
"{}{}{}{}{}",
"commit ".yellow(),
head_commit.yellow(),
"(".yellow(),
"HEAD".blue(),
")".yellow()
);
}
println!("Author: {}", commit.get_author());
println!("Date: {}", commit.get_date());
println!();
println!(" {}", commit.get_message());
println!();
if all == false {
if number > 1 {
number -= 1;
} else {
break;
}
}
if commit.get_parent_hash().len() == 0 {
break;
}
head_commit = commit.get_parent_hash().first().unwrap().clone();
}
log_count
}
#[cfg(test)]
mod test {
use super::super::super::commands;
use crate::utils::util;
#[test]
fn test_log() {
util::setup_test_with_clean_mit();
assert_eq!(super::__log(false, None), 0);
commands::commit::commit("test commit 2".into(), true);
assert_eq!(super::__log(false, Some(1)), 1);
commands::commit::commit("test commit 3".into(), true);
assert_eq!(super::__log(false, None), 2);
}
}

View File

@@ -3,3 +3,4 @@ pub mod commit;
pub mod init;
pub mod remove;
pub mod status;
pub mod log;

View File

@@ -19,7 +19,7 @@ pub fn current_head() -> Head {
Head::Detached(head_content)
}
}
fn update_branch(branch_name: &String, commit_hash: &String) {
pub fn update_branch(branch_name: &String, commit_hash: &String) {
// 更新分支head
let mut branch = util::get_storage_path().unwrap();
branch.push("refs");
@@ -28,7 +28,7 @@ fn update_branch(branch_name: &String, commit_hash: &String) {
std::fs::write(&branch, commit_hash).expect(&format!("无法写入branch in {:?} with {}", branch, commit_hash));
}
fn get_branch_head(branch_name: &String) -> String {
pub fn get_branch_head(branch_name: &String) -> String {
// 返回当前分支的commit hash
let mut branch = util::get_storage_path().unwrap();
branch.push("refs");