|
| 1 | +use std::{ |
| 2 | + collections::{hash_map::Entry, HashMap}, |
| 3 | + path::{Path, PathBuf}, |
| 4 | +}; |
| 5 | + |
| 6 | +use anyhow::{anyhow, Context}; |
| 7 | +use git2::{Blob, Oid, Repository}; |
| 8 | + |
| 9 | +#[derive(Default)] |
| 10 | +pub struct Repos(HashMap<PathBuf, Repository>); |
| 11 | + |
| 12 | +impl Repos { |
| 13 | + pub fn open(&mut self, path: PathBuf) -> mdbook::errors::Result<&Repository> { |
| 14 | + match self.0.entry(path) { |
| 15 | + Entry::Occupied(occupied_entry) => Ok(occupied_entry.into_mut()), |
| 16 | + Entry::Vacant(vacant_entry) => { |
| 17 | + let repo = Repository::open(vacant_entry.key())?; |
| 18 | + Ok(vacant_entry.insert(repo)) |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +pub fn find_commit_by_msg(repo: &Repository, msg: &str) -> mdbook::errors::Result<Oid> { |
| 25 | + let head = repo.head().context("Failed to look up repo HEAD")?; |
| 26 | + let mut commit = head |
| 27 | + .peel_to_commit() |
| 28 | + .context("Failed to get the commit pointed to by HEAD")?; |
| 29 | + while !commit |
| 30 | + .message() |
| 31 | + .ok_or(anyhow!("Non-UTF-8 commit message!?"))? |
| 32 | + .strip_prefix(msg) |
| 33 | + .is_some_and(|rest| rest.is_empty() || rest.starts_with('\n')) |
| 34 | + { |
| 35 | + commit = commit |
| 36 | + .parent(0) |
| 37 | + .context("Failed to find a commit with specified message")?; |
| 38 | + } |
| 39 | + Ok(commit.id()) |
| 40 | +} |
| 41 | + |
| 42 | +pub fn get_file<'repos>( |
| 43 | + repos: &'repos Repos, |
| 44 | + (repo_path, commit_id): &(PathBuf, Oid), |
| 45 | + path: &Path, |
| 46 | +) -> mdbook::errors::Result<Blob<'repos>> { |
| 47 | + let repo = &repos.0[repo_path]; |
| 48 | + let commit = repo.find_commit(*commit_id).unwrap(); |
| 49 | + let tree = commit |
| 50 | + .tree() |
| 51 | + .context("Unable to obtain the commit's tree")?; |
| 52 | + let entry = tree |
| 53 | + .get_path(path) |
| 54 | + .context("Unable to find the specified file")?; |
| 55 | + let object = entry |
| 56 | + .to_object(repo) |
| 57 | + .context("Unable to obtain the file's object in the repo")?; |
| 58 | + object |
| 59 | + .peel_to_blob() |
| 60 | + .context("The specified path is not a file") |
| 61 | +} |
0 commit comments