From ca2315aa5972d680efaabfeae0ccc94e5ced158a Mon Sep 17 00:00:00 2001 From: Austin Gill Date: Tue, 26 Mar 2024 20:05:00 -0500 Subject: [PATCH] rev-walk --- src/git/mod.rs | 13 ++++++++++++- src/main.rs | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/git/mod.rs b/src/git/mod.rs index 323a754..1f64513 100644 --- a/src/git/mod.rs +++ b/src/git/mod.rs @@ -1,7 +1,7 @@ use std::path::Path; use eyre::WrapErr; -use git2::{ObjectType, Oid, Repository}; +use git2::{ObjectType, Oid, Repository, Sort}; /// Fetch or find the specified repository /// @@ -39,3 +39,14 @@ pub fn rev_parse(reference: &str, repo: &Repository) -> eyre::Result { ); Ok(oid) } + +pub fn rev_walk( + oid: Oid, + repo: &Repository, +) -> eyre::Result> + '_> { + let mut revwalk = repo.revwalk().wrap_err("Could not walk repository")?; + revwalk.set_sorting(Sort::TIME | Sort::TOPOLOGICAL)?; + revwalk.push(oid)?; + + Ok(revwalk.map(|r| r.wrap_err("Failed to yield next rev"))) +} diff --git a/src/main.rs b/src/main.rs index df9c619..f1c9e01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,16 @@ fn main() -> eyre::Result<()> { let oid = git::rev_parse(&args.reference, &repo) .wrap_err(format!("Failed to resolve reference {:?}", args.reference))?; + let oids = git::rev_walk(oid, &repo).wrap_err(format!("Failed to walk OID {oid:?}"))?; + for oid in oids { + // I'm not sure why this would happen, nor why the iterator wouldn't just return None. + // Maybe it's because returning None gives no context? + let oid = oid.wrap_err("Failed to get next OID")?; + let commit = repo + .find_commit(oid) + .wrap_err(format!("Failed to find commit with OID {oid:?}"))?; + tracing::debug!("Found commit: {:?}", commit.summary().unwrap_or("??")); + } Ok(()) }