Skip to content

Commit 6883cd6

Browse files
committed
Make Unbricked into a submodule
Pulling specific versions of its files via `git2` Fixes #110 (by keeping the variable even after outliving its usefulness)
1 parent 0c2600f commit 6883cd6

35 files changed

+579
-5405
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "unbricked"]
2+
path = unbricked
3+
url = https://github.com/ISSOtm/unbricked

Cargo.lock

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

preproc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ pulldown-cmark-to-cmark = "6.0.0"
1717
regex = "1.5.4"
1818
termcolor = "1.1.2"
1919
serde_json = "1.0.59"
20+
git2 = "0.20.2"

preproc/src/git.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)