mirror of
https://github.com/MrBeanCpp/MIT.git
synced 2026-04-29 21:29:46 +08:00
git log实现与测试
This commit is contained in:
97
src/commands/log.rs
Normal file
97
src/commands/log.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -3,3 +3,4 @@ pub mod commit;
|
||||
pub mod init;
|
||||
pub mod remove;
|
||||
pub mod status;
|
||||
pub mod log;
|
||||
Reference in New Issue
Block a user