feat: adding log
This commit is contained in:
parent
62af81b28f
commit
f047bb5181
29
src/log.rs
Normal file
29
src/log.rs
Normal 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(())
|
||||
}
|
||||
}
|
@ -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(())
|
||||
|
Loading…
x
Reference in New Issue
Block a user