feat: adding log
All checks were successful
CI checks / Clippy (push) Successful in 26s
CI checks / Format (push) Successful in 25s

This commit is contained in:
Patrick MARIE 2025-02-05 22:55:27 +01:00
parent 62af81b28f
commit f047bb5181
Signed by: mycroft
GPG Key ID: BB519E5CD8E7BFA7
2 changed files with 36 additions and 0 deletions

29
src/log.rs Normal file
View File

@ -0,0 +1,29 @@
use crate::repository::Repository;
use anyhow::Result;
use hex::FromHex;
impl Repository {
pub fn log(&self) -> Result<()> {
let mut current_commit = self.current_commit()?;
loop {
let mut commit = self.read_object(&hex::encode(current_commit))?;
let commit_desc = commit.string()?;
let lines = commit_desc.lines().collect::<Vec<&str>>();
println!("{} {}", hex::encode(current_commit), lines[lines.len() - 1]);
let parent_commit_id = lines.iter().find(|line| line.starts_with("parent "));
if parent_commit_id.is_none() {
break;
}
let parent_commit_id = parent_commit_id.unwrap();
current_commit = <[u8; 20]>::from_hex(parent_commit_id.split_once(' ').unwrap().1)?;
}
Ok(())
}
}

View File

@ -8,6 +8,7 @@ use clap::Subcommand;
mod commit;
mod error;
mod kind;
mod log;
mod object;
mod repository;
mod tree;
@ -56,6 +57,8 @@ enum Command {
/// The commit to show
hash: Option<String>,
},
/// Show the commit log
Log,
}
fn main() -> Result<(), Error> {
@ -92,6 +95,10 @@ fn main() -> Result<(), Error> {
Ok(_) => (),
Err(e) => eprintln!("Failed to show: {}", e),
},
Command::Log => match repo.log() {
Ok(_) => (),
Err(e) => eprintln!("Failed to show log: {}", e),
},
}
Ok(())