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

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;