Skip to content

Commit

Permalink
Remove dependency on nom, once_cell & parking_lot
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Sep 28, 2024
1 parent 3a3f23a commit 37f9347
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 28 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ humantime = "2.1"
itertools = "0.13.0"
md5 = "0.7"
moka = { version = "0.12.0", features = ["future"] }
nom = "7.1"
once_cell = "1.18"
parking_lot = "0.12"
path-clean = "1.0.1"
rand = "0.8.5"
rocksdb = { version = "0.22", default-features = false, features = ["snappy"] }
Expand Down
31 changes: 16 additions & 15 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ use gix::{
objs::tree::EntryRef,
prelude::TreeEntryRefExt,
traverse::tree::visit::Action,
ObjectId,
ObjectId, ThreadSafeRepository,
};
use moka::future::Cache;
use parking_lot::Mutex;
use syntect::{
parsing::{BasicScopeStackOp, ParseState, Scope, ScopeStack, SyntaxSet, SCOPE_REPO},
util::LinesWithEndings,
Expand Down Expand Up @@ -71,9 +70,13 @@ impl Git {
repo_path: PathBuf,
branch: Option<Arc<str>>,
) -> Result<Arc<OpenRepository>> {
let mut repo = tokio::task::spawn_blocking({
let repo = tokio::task::spawn_blocking({
let repo_path = repo_path.clone();
move || gix::open(repo_path)
move || {
gix::open::Options::isolated()
.open_path_as_is(true)
.open(&repo_path)
}
})
.await
.context("Failed to join Tokio task")?
Expand All @@ -82,12 +85,10 @@ impl Git {
anyhow!("Failed to open repository")
})?;

repo.object_cache_size(10 * 1024 * 1024);

Ok(Arc::new(OpenRepository {
git: self,
cache_key: repo_path,
repo: Mutex::new(repo),
repo,
branch,
}))
}
Expand All @@ -96,7 +97,7 @@ impl Git {
pub struct OpenRepository {
git: Arc<Git>,
cache_key: PathBuf,
repo: Mutex<gix::Repository>,
repo: ThreadSafeRepository,
branch: Option<Arc<str>>,
}

Expand All @@ -113,7 +114,7 @@ impl OpenRepository {
.context("Failed to parse tree hash")?;

tokio::task::spawn_blocking(move || {
let repo = self.repo.lock();
let repo = self.repo.to_thread_local();

let mut tree = if let Some(tree_id) = tree_id {
repo.find_tree(tree_id)
Expand Down Expand Up @@ -213,7 +214,7 @@ impl OpenRepository {
pub async fn tag_info(self: Arc<Self>) -> Result<DetailedTag> {
tokio::task::spawn_blocking(move || {
let tag_name = self.branch.clone().context("no tag given")?;
let repo = self.repo.lock();
let repo = self.repo.to_thread_local();

let tag = repo
.find_reference(&format!("refs/tags/{tag_name}"))
Expand Down Expand Up @@ -255,7 +256,7 @@ impl OpenRepository {
git.readme_cache
.try_get_with((self.cache_key.clone(), self.branch.clone()), async move {
tokio::task::spawn_blocking(move || {
let repo = self.repo.lock();
let repo = self.repo.to_thread_local();

let mut head = if let Some(reference) = &self.branch {
repo.find_reference(reference.as_ref())?
Expand Down Expand Up @@ -306,7 +307,7 @@ impl OpenRepository {

pub async fn default_branch(self: Arc<Self>) -> Result<Option<String>> {
tokio::task::spawn_blocking(move || {
let repo = self.repo.lock();
let repo = self.repo.to_thread_local();
let head = repo.head().context("Couldn't find HEAD of repository")?;
Ok(head.referent_name().map(|v| v.shorten().to_string()))
})
Expand All @@ -317,7 +318,7 @@ impl OpenRepository {
#[instrument(skip(self))]
pub async fn latest_commit(self: Arc<Self>, highlighted: bool) -> Result<Commit> {
tokio::task::spawn_blocking(move || {
let repo = self.repo.lock();
let repo = self.repo.to_thread_local();

let mut head = if let Some(reference) = &self.branch {
repo.find_reference(reference.as_ref())?
Expand Down Expand Up @@ -354,7 +355,7 @@ impl OpenRepository {
.context("failed to build oid")?;

tokio::task::spawn_blocking(move || {
let repo = self.repo.lock();
let repo = self.repo.to_thread_local();

let tree = if let Some(commit) = commit {
repo.find_commit(commit)?.tree()?
Expand Down Expand Up @@ -411,7 +412,7 @@ impl OpenRepository {
git.commits
.try_get_with((commit, highlighted), async move {
tokio::task::spawn_blocking(move || {
let repo = self.repo.lock();
let repo = self.repo.to_thread_local();

let commit = repo.find_commit(commit)?;

Expand Down
12 changes: 5 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
net::SocketAddr,
path::PathBuf,
str::FromStr,
sync::Arc,
sync::{Arc, LazyLock, OnceLock},
time::Duration,
};

Expand All @@ -24,8 +24,6 @@ use axum::{
use bat::assets::HighlightingAssets;
use clap::Parser;
use database::schema::SCHEMA_VERSION;
use nom::AsBytes;
use once_cell::sync::{Lazy, OnceCell};
use rocksdb::{Options, SliceTransform};
use sha2::{digest::FixedOutput, Digest};
use syntect::html::ClassStyle;
Expand Down Expand Up @@ -57,10 +55,10 @@ mod unified_diff_builder;
const CRATE_VERSION: &str = clap::crate_version!();

static GLOBAL_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/statics/css/style.css",));
static GLOBAL_CSS_HASH: Lazy<Box<str>> = Lazy::new(|| build_asset_hash(GLOBAL_CSS));
static GLOBAL_CSS_HASH: LazyLock<Box<str>> = LazyLock::new(|| build_asset_hash(GLOBAL_CSS));

static HIGHLIGHT_CSS_HASH: OnceCell<Box<str>> = OnceCell::new();
static DARK_HIGHLIGHT_CSS_HASH: OnceCell<Box<str>> = OnceCell::new();
static HIGHLIGHT_CSS_HASH: OnceLock<Box<str>> = OnceLock::new();
static DARK_HIGHLIGHT_CSS_HASH: OnceLock<Box<str>> = OnceLock::new();

#[derive(Parser, Debug)]
#[clap(author, version, about)]
Expand Down Expand Up @@ -260,7 +258,7 @@ fn open_db(args: &Args) -> Result<Arc<rocksdb::DB>, anyhow::Error> {
)?;

let needs_schema_regen = match db.get("schema_version")? {
Some(v) if v.as_bytes() != SCHEMA_VERSION.as_bytes() => Some(Some(v)),
Some(v) if v.as_slice() != SCHEMA_VERSION.as_bytes() => Some(Some(v)),
Some(_) => None,
None => {
db.put("schema_version", SCHEMA_VERSION)?;
Expand Down

0 comments on commit 37f9347

Please sign in to comment.