Count lines of code for each ancestor

This commit is contained in:
Joscha 2023-01-23 12:26:07 +01:00
parent fc7c76aaf3
commit 35cda2cbb8

View file

@ -1,7 +1,7 @@
use std::path::PathBuf; use std::path::PathBuf;
use clap::Parser; use clap::Parser;
use git_repository::{traverse::tree::Recorder, Commit}; use git_repository::{objs::tree::EntryMode, traverse::tree::Recorder, Commit, Repository};
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Args { struct Args {
@ -10,50 +10,19 @@ struct Args {
repo: PathBuf, repo: PathBuf,
} }
struct TreeVisitor; fn count_lines(repo: &Repository, commit: &Commit) -> anyhow::Result<usize> {
let mut lines = 0;
impl git_repository::traverse::tree::Visit for TreeVisitor {
fn pop_front_tracked_path_and_set_current(&mut self) {
println!("pop_front_tracked_path_and_set_current");
}
fn push_back_tracked_path_component(&mut self, component: &git_repository::bstr::BStr) {
println!("push_back_tracked_path_component {component:?}");
}
fn push_path_component(&mut self, component: &git_repository::bstr::BStr) {
println!("push_path_component {component:?}");
}
fn pop_path_component(&mut self) {
println!("pop_path_component");
}
fn visit_tree(
&mut self,
entry: &git_repository::objs::tree::EntryRef<'_>,
) -> git_repository::traverse::tree::visit::Action {
println!("visit_tree {entry:?}");
git_repository::traverse::tree::visit::Action::Continue
}
fn visit_nontree(
&mut self,
entry: &git_repository::objs::tree::EntryRef<'_>,
) -> git_repository::traverse::tree::visit::Action {
println!("visit_nontree {entry:?}");
git_repository::traverse::tree::visit::Action::Continue
}
}
fn print_commit(commit: &Commit) -> anyhow::Result<()> {
println!("{commit:?}");
println!("{:?}", commit.message()?);
let mut recorder = Recorder::default(); let mut recorder = Recorder::default();
commit.tree()?.traverse().breadthfirst(&mut recorder)?; commit.tree()?.traverse().breadthfirst(&mut recorder)?;
println!("{recorder:#?}"); for entry in recorder.records {
println!(); if matches!(entry.mode, EntryMode::Blob | EntryMode::BlobExecutable) {
Ok(()) let object = repo.find_object(entry.oid)?;
let data = object.detach().data;
let text = String::from_utf8(data)?;
lines += text.lines().count();
}
}
Ok(lines)
} }
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
@ -62,7 +31,8 @@ fn main() -> anyhow::Result<()> {
let commit = repo.head_commit()?; let commit = repo.head_commit()?;
for ancestor in commit.ancestors().all()? { for ancestor in commit.ancestors().all()? {
let ancestor = repo.find_object(ancestor.unwrap())?.try_into_commit()?; let ancestor = repo.find_object(ancestor.unwrap())?.try_into_commit()?;
print_commit(&ancestor)?; let lines = count_lines(&repo, &ancestor)?;
println!("{} {lines}", ancestor.id);
} }
Ok(()) Ok(())
} }