Skip to content
Merged
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
12 changes: 12 additions & 0 deletions josh-core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ lazy_static! {
static ref DB: std::sync::Mutex<Option<sled::Db>> = std::sync::Mutex::new(None);
}

pub(crate) fn josh_commit_signature<'a>() -> JoshResult<git2::Signature<'a>> {
Ok(if let Ok(time) = std::env::var("JOSH_COMMIT_TIME") {
git2::Signature::new(
"JOSH",
"josh@josh-project.dev",
&git2::Time::new(time.parse()?, 0),
)?
} else {
git2::Signature::now("JOSH", "josh@josh-project.dev")?
})
}

static REF_CACHE: LazyLock<RwLock<HashMap<git2::Oid, HashMap<git2::Oid, git2::Oid>>>> =
LazyLock::new(Default::default);

Expand Down
66 changes: 66 additions & 0 deletions josh-core/src/cache_notes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::JoshResult;
use crate::cache::{CACHE_VERSION, CacheBackend};
use crate::filter::Filter;

pub struct NotesCacheBackend {
repo: std::sync::Mutex<git2::Repository>,
}

impl NotesCacheBackend {
pub fn new(repo_path: impl AsRef<std::path::Path>) -> JoshResult<Self> {
let repo = git2::Repository::open(repo_path.as_ref())?;
Ok(Self {
repo: std::sync::Mutex::new(repo),
})
}
}

fn is_note_eligible(oid: git2::Oid) -> bool {
oid.as_bytes()[0] == 0
}

fn note_path(key: git2::Oid) -> String {
format!("refs/josh/{}/{}", CACHE_VERSION, key)
}

impl CacheBackend for NotesCacheBackend {
fn read(&self, filter: Filter, from: git2::Oid) -> JoshResult<Option<git2::Oid>> {
let repo = self.repo.lock()?;
let key = crate::filter::as_tree(&repo, filter)?;

if !is_note_eligible(from) {
return Ok(None);
}

if let Ok(note) = repo.find_note(Some(&note_path(key)), from) {
let message = note.message().unwrap_or("");
let result = git2::Oid::from_str(message)?;

Ok(Some(result))
} else {
Ok(None)
}
}

fn write(&self, filter: Filter, from: git2::Oid, to: git2::Oid) -> JoshResult<()> {
let repo = self.repo.lock()?;
let key = crate::filter::as_tree(&repo, filter)?;

if !is_note_eligible(from) {
return Ok(());
}

let signature = crate::cache::josh_commit_signature()?;

repo.note(
&signature,
&signature,
Some(&note_path(key)),
from,
&to.to_string(),
true,
)?;

Ok(())
}
}
1 change: 1 addition & 0 deletions josh-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ macro_rules! ok_or {
extern crate rs_tracing;

pub mod cache;
pub mod cache_notes;
pub mod cache_sled;
pub mod cache_stack;
pub mod filter;
Expand Down
15 changes: 12 additions & 3 deletions josh-filter/src/bin/josh-filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,21 @@ fn run_filter(args: Vec<String>) -> josh::JoshResult<i32> {

let mut filterobj = josh::filter::parse(&specstr)?;

if !args.get_flag("no-cache") {
let repo_path = {
let repo = git2::Repository::open_from_env()?;
josh::cache_sled::sled_load(repo.path())?;
repo.path().to_path_buf()
};

if !args.get_flag("no-cache") {
josh::cache_sled::sled_load(&repo_path)?;
}

let cache = std::sync::Arc::new(josh::cache_stack::CacheStack::default());
let cache = std::sync::Arc::new(
josh::cache_stack::CacheStack::new()
.with_backend(josh::cache_sled::SledCacheBackend::default())
.with_backend(josh::cache_notes::NotesCacheBackend::new(&repo_path)?),
);

let mut transaction = josh::cache::TransactionContext::from_env(cache.clone())?.open(None)?;

let repo_for_hook = git2::Repository::open_ext(
Expand Down