Allow requesting badges for branches != master
This changed the cache structure to allow caching data for multiple branches. The service still assumes a default branch named `master` but using the `branch` get parameter, the default branch can be changed. The 404 page for the missing `master` branch was changed to explain the new parameter.
This commit is contained in:
100
src/cache.rs
100
src/cache.rs
@@ -1,6 +1,7 @@
|
||||
use crate::error::{Error, Result};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
fs::{create_dir_all, File, OpenOptions},
|
||||
io::BufReader,
|
||||
path::Path,
|
||||
@@ -9,49 +10,106 @@ use std::{
|
||||
/// Enum to indicate the state of the cache
|
||||
pub(crate) enum CacheState<'a> {
|
||||
/// Current head and cached head are the same
|
||||
Current { count: u64, commits: u64 },
|
||||
Current {
|
||||
count: u64,
|
||||
commits: u64,
|
||||
cache: Cache<'a>,
|
||||
},
|
||||
/// Cached head is older than current head
|
||||
Old(Cache<'a>),
|
||||
Old {
|
||||
head: String,
|
||||
cache: Cache<'a>,
|
||||
},
|
||||
NoneForBranch(Cache<'a>),
|
||||
/// No cache was found
|
||||
No,
|
||||
}
|
||||
|
||||
impl<'a> CacheState<'a> {
|
||||
pub(crate) fn read_from_file(path: impl AsRef<Path>, head: &str) -> Result<CacheState> {
|
||||
pub(crate) fn read_from_file(
|
||||
path: impl AsRef<Path>,
|
||||
branch: &str,
|
||||
head: &str,
|
||||
) -> Result<CacheState<'a>> {
|
||||
if path.as_ref().exists() {
|
||||
let cache: Cache = serde_json::from_reader(BufReader::new(File::open(path)?))?;
|
||||
if cache.head == head {
|
||||
Ok(CacheState::Current {
|
||||
count: cache.count,
|
||||
commits: cache.commits,
|
||||
Ok(cache
|
||||
.entries
|
||||
.get(branch)
|
||||
.map(|c| {
|
||||
if c.head == head {
|
||||
CacheState::Current {
|
||||
count: c.count,
|
||||
commits: c.commits,
|
||||
// TODO: get rid of clone
|
||||
cache: cache.clone(),
|
||||
}
|
||||
} else {
|
||||
CacheState::Old {
|
||||
head: c.head.to_string(),
|
||||
// TODO: get rid of clone
|
||||
cache: cache.clone(),
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Ok(CacheState::Old(cache))
|
||||
}
|
||||
// TODO: get rid of clone
|
||||
.unwrap_or_else(|| CacheState::NoneForBranch(cache.clone())))
|
||||
} else {
|
||||
Ok(CacheState::No)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn calculate_new_cache(self, count: u64, commits: u64, head: Cow<'a, str>) -> Cache {
|
||||
pub(crate) fn calculate_new_cache(
|
||||
self,
|
||||
count: u64,
|
||||
commits: u64,
|
||||
head: Cow<'a, str>,
|
||||
branch: &'a str,
|
||||
) -> Cache<'a> {
|
||||
match self {
|
||||
CacheState::Old(mut cache) => {
|
||||
cache.head = head;
|
||||
cache.count += count;
|
||||
cache.commits += commits;
|
||||
CacheState::Old { mut cache, .. } => {
|
||||
if let Some(mut cache) = cache.entries.get_mut(branch) {
|
||||
cache.head = head;
|
||||
cache.count += count;
|
||||
cache.commits += commits;
|
||||
}
|
||||
cache
|
||||
}
|
||||
CacheState::No | CacheState::Current { .. } => Cache {
|
||||
head,
|
||||
count,
|
||||
commits,
|
||||
},
|
||||
CacheState::Current { cache, .. } => cache,
|
||||
CacheState::NoneForBranch(mut cache) => {
|
||||
cache.entries.insert(
|
||||
branch.into(),
|
||||
CacheEntry {
|
||||
head,
|
||||
count,
|
||||
commits,
|
||||
},
|
||||
);
|
||||
cache
|
||||
}
|
||||
CacheState::No => {
|
||||
let mut entries = HashMap::with_capacity(1);
|
||||
entries.insert(
|
||||
branch.into(),
|
||||
CacheEntry {
|
||||
commits,
|
||||
head,
|
||||
count,
|
||||
},
|
||||
);
|
||||
Cache { entries }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub(crate) struct Cache<'a> {
|
||||
pub entries: HashMap<Cow<'a, str>, CacheEntry<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
pub(crate) struct CacheEntry<'a> {
|
||||
/// HEAD commit ref
|
||||
pub head: Cow<'a, str>,
|
||||
/// HoC value
|
||||
|
Reference in New Issue
Block a user