Skip to content

Commit

Permalink
rev-walk
Browse files Browse the repository at this point in the history
  • Loading branch information
Notgnoshi committed Mar 27, 2024
1 parent 428c7d7 commit ca2315a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/git/mod.rs
Original file line number Diff line number Diff line change
@@ -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
///
Expand Down Expand Up @@ -39,3 +39,14 @@ pub fn rev_parse(reference: &str, repo: &Repository) -> eyre::Result<Oid> {
);
Ok(oid)
}

pub fn rev_walk(
oid: Oid,
repo: &Repository,
) -> eyre::Result<impl Iterator<Item = eyre::Result<Oid>> + '_> {
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")))
}
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}

0 comments on commit ca2315a

Please sign in to comment.