Count lines of code for each ancestor
This commit is contained in:
parent
fc7c76aaf3
commit
35cda2cbb8
1 changed files with 14 additions and 44 deletions
58
src/main.rs
58
src/main.rs
|
|
@ -1,7 +1,7 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use clap::Parser;
|
||||
use git_repository::{traverse::tree::Recorder, Commit};
|
||||
use git_repository::{objs::tree::EntryMode, traverse::tree::Recorder, Commit, Repository};
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct Args {
|
||||
|
|
@ -10,50 +10,19 @@ struct Args {
|
|||
repo: PathBuf,
|
||||
}
|
||||
|
||||
struct TreeVisitor;
|
||||
|
||||
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()?);
|
||||
fn count_lines(repo: &Repository, commit: &Commit) -> anyhow::Result<usize> {
|
||||
let mut lines = 0;
|
||||
let mut recorder = Recorder::default();
|
||||
commit.tree()?.traverse().breadthfirst(&mut recorder)?;
|
||||
println!("{recorder:#?}");
|
||||
println!();
|
||||
Ok(())
|
||||
for entry in recorder.records {
|
||||
if matches!(entry.mode, EntryMode::Blob | EntryMode::BlobExecutable) {
|
||||
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<()> {
|
||||
|
|
@ -62,7 +31,8 @@ fn main() -> anyhow::Result<()> {
|
|||
let commit = repo.head_commit()?;
|
||||
for ancestor in commit.ancestors().all()? {
|
||||
let ancestor = repo.find_object(ancestor.unwrap())?.try_into_commit()?;
|
||||
print_commit(&ancestor)?;
|
||||
let lines = count_lines(&repo, &ancestor)?;
|
||||
println!("{} {lines}", ancestor.id);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue