Skip to content

Make Unbricked into a submodule #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout gb-asm-tutorial
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
path: gb-asm-tutorial
submodules: recursive

- name: Install mdbook
uses: peaceiris/actions-mdbook@v1
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "unbricked"]
path = unbricked
url = https://github.com/ISSOtm/unbricked
92 changes: 92 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions preproc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ pulldown-cmark-to-cmark = "6.0.0"
regex = "1.5.4"
termcolor = "1.1.2"
serde_json = "1.0.59"
git2 = "0.20.2"
61 changes: 61 additions & 0 deletions preproc/src/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::{
collections::{hash_map::Entry, HashMap},
path::{Path, PathBuf},
};

use anyhow::{anyhow, Context};
use git2::{Blob, Oid, Repository};

#[derive(Default)]
pub struct Repos(HashMap<PathBuf, Repository>);

impl Repos {
pub fn open(&mut self, path: PathBuf) -> mdbook::errors::Result<&Repository> {
match self.0.entry(path) {
Entry::Occupied(occupied_entry) => Ok(occupied_entry.into_mut()),
Entry::Vacant(vacant_entry) => {
let repo = Repository::open(vacant_entry.key())?;
Ok(vacant_entry.insert(repo))
}
}
}
}

pub fn find_commit_by_msg(repo: &Repository, msg: &str) -> mdbook::errors::Result<Oid> {
let head = repo.head().context("Failed to look up repo HEAD")?;
let mut commit = head
.peel_to_commit()
.context("Failed to get the commit pointed to by HEAD")?;
while !commit
.message()
.ok_or(anyhow!("Non-UTF-8 commit message!?"))?
.strip_prefix(msg)
.is_some_and(|rest| rest.is_empty() || rest.starts_with('\n'))
{
commit = commit
.parent(0)
.context("Failed to find a commit with specified message")?;
}
Ok(commit.id())
}

pub fn get_file<'repos>(
repos: &'repos Repos,
(repo_path, commit_id): &(PathBuf, Oid),
path: &Path,
) -> mdbook::errors::Result<Blob<'repos>> {
let repo = &repos.0[repo_path];
let commit = repo.find_commit(*commit_id).unwrap();
let tree = commit
.tree()
.context("Unable to obtain the commit's tree")?;
let entry = tree
.get_path(path)
.context("Unable to find the specified file")?;
let object = entry
.to_object(repo)
.context("Unable to obtain the file's object in the repo")?;
object
.peel_to_blob()
.context("The specified path is not a file")
}
Loading