tangled knot that runs entirely on cloudflare
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111use std::cell::RefCell;use std::collections::{HashMap, HashSet};use std::rc::Rc;
use futures::future::LocalBoxFuture;use gix_hash::ObjectId;
use crate::error::KnotError;use crate::git::RefSnapshot;use crate::odb::{Fetched, ObjectSource, Objects};use crate::remote::Remote;
#[derive(Default)]struct Synced { objects: Objects, tips: HashSet<ObjectId>,}
thread_local! { static CACHE: RefCell<HashMap<String, Rc<RefCell<Synced>>>> = RefCell::new(HashMap::new());}
pub fn cached_objects(key: &str) -> Objects { synced(key).borrow().objects.clone()}
fn synced(key: &str) -> Rc<RefCell<Synced>> { CACHE.with(|cache| { cache .borrow_mut() .entry(key.to_string()) .or_default() .clone() })}
pub fn forget(key: &str) { CACHE.with(|cache| { cache.borrow_mut().remove(key); });}
pub struct PackSource { key: String, remote: Remote, tips: Vec<ObjectId>, hash: gix_hash::Kind,}
impl PackSource { pub fn new( key: impl Into<String>, remote: Remote, refs: &RefSnapshot, hash: gix_hash::Kind, ) -> Self { let mut tips: Vec<ObjectId> = refs .refs .iter() .map(|(_, target)| target.object_id()) .chain(refs.head_target.map(|target| target.object_id())) .collect(); tips.sort(); tips.dedup(); Self { key: key.into(), remote, tips, hash, } }
async fn sync(&self) -> Result<Vec<Fetched>, KnotError> { let state = synced(&self.key); let (wants, haves): (Vec<ObjectId>, Vec<ObjectId>) = { let state = state.borrow(); let wants = self .tips .iter() .filter(|tip| !state.tips.contains(*tip) && !state.objects.has(tip)) .copied() .collect(); (wants, state.tips.iter().copied().collect()) }; if wants.is_empty() { return Ok(Vec::new()); } let pack = self.remote.fetch_pack(&wants, &haves).await?; let objects = state.borrow().objects.clone(); let fetched = crate::pack::unpack(&pack, self.hash, |id| objects.get(id))?; state.borrow_mut().tips.extend(self.tips.iter().copied()); Ok(fetched) }}
impl ObjectSource for PackSource { fn fetch<'a>( &'a self, _ids: Vec<ObjectId>, ) -> LocalBoxFuture<'a, Result<Vec<Fetched>, KnotError>> { Box::pin(self.sync()) }
fn blob_size<'a>( &'a self, _id: ObjectId, ) -> LocalBoxFuture<'a, Result<Option<u64>, KnotError>> { Box::pin(async { Ok(None) }) }}