git2-rs icon indicating copy to clipboard operation
git2-rs copied to clipboard

[Documentation] Improve revwalk examples. Finding last change to file?

Open kallisti5 opened this issue 4 years ago • 5 comments

The revwalk documentation is unclear about how to find the last commit changing a file (a pretty basic usage).

Example:

    fn recipe_modified(path: PathBuf) -> Result<i32, Box<dyn Error>> {
        let repo = git2::Repository::discover(path)?;
        let mut revwalk = repo.revwalk()?;
        revwalk.push_head()?;
        revwalk.set_sorting(git2::Sort::TIME)?;
        for rev in revwalk {
            let commit = repo.find_commit(rev?)?;
            let message = commit.summary_bytes().unwrap_or_else(|| commit.message_bytes());
            println!("{}\t{}", commit.id(), String::from_utf8_lossy(message));
        }
        Ok(0)
    }

Here, provided a git repo, we pull all of the commits for it.. but I don't see a clear way to:

  • Check the files included in a commit.
  • Revwalk only on commits impacting a file.

This seems like a really common usage scenario, but little documentation on it,

kallisti5 avatar Mar 29 '20 02:03 kallisti5