1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
use std::collections::HashMap;
use std::io::Read;
use std::path::Path;
use std::sync::Arc;
use std::sync::Mutex;
use anyhow::Error;
use anyhow::Result;
use anyhow::anyhow;
use cached::Return;
use cached::TimedSizedCache;
use cached::proc_macro::cached;
use either::Either;
use git2::Tree;
use handlebars::Handlebars;
#[derive(Clone, getset::Getters, typed_builder::TypedBuilder)]
pub struct AppState<'a> {
#[getset(get = "pub")]
default_branch_name: String,
#[getset(get = "pub")]
repos: HashMap<String, Arc<Mutex<RepoState>>>,
#[getset(get = "pub")]
handlebars: Handlebars<'a>,
#[getset(get = "pub")]
theme: syntect::highlighting::Theme,
}
#[derive(getset::Getters)]
pub struct RepoState {
#[getset(get = "pub")]
name: String,
#[getset(get = "pub")]
repo: git2::Repository,
}
impl RepoState {
pub fn open_repo<P: AsRef<Path>>(path: P) -> Result<Self> {
let name = path.as_ref()
.file_name()
.and_then(std::ffi::OsStr::to_str)
.map(String::from)
.ok_or_else(|| anyhow!("Not valid UTF-8, cannot process: {}", path.as_ref().display()))?;
log::info!("Opening {} = {}", name, path.as_ref().display());
let repo = git2::Repository::open(path)?;
Ok(RepoState { name, repo })
}
pub async fn stat(&self) -> Result<cached::Return<RepositoryStat>> {
let (branches, tags) = futures::try_join!(self.branch_names(), self.tag_names())?;
Ok(cached::Return::new(RepositoryStat {
branches: branches.value,
tags: tags.value,
}))
}
pub async fn branch_names(&self) -> Result<cached::Return<Vec<String>>> {
get_branch_names(&self.name, &self.repo)
}
pub async fn tag_names(&self) -> Result<cached::Return<Vec<String>>> {
get_tag_names(&self.name, &self.repo)
}
}
#[derive(Clone)]
pub struct RepositoryStat {
pub branches: Vec<String>,
pub tags: Vec<String>,
}
#[cached(
type = "TimedSizedCache<String, Return<Vec<String>>>",
create = "{ TimedSizedCache::with_size_and_lifespan(1, 60) }",
convert = r#"{ _repo_name.to_owned() }"#,
with_cached_flag = true,
result = true,
)]
fn get_branch_names(_repo_name: &str, repo: &git2::Repository) -> Result<cached::Return<Vec<String>>> {
repo.branches(None)?
.map(|branch| {
let branch = branch?.0;
branch.name()?
.ok_or_else(|| anyhow!("Branch name is not valid UTF8: {:?}", branch.name()))
.map(String::from)
})
.collect::<Result<Vec<String>>>()
.map(cached::Return::new)
}
#[cached(
type = "TimedSizedCache<String, Return<Vec<String>>>",
create = "{ TimedSizedCache::with_size_and_lifespan(1, 60) }",
convert = r#"{ _repo_name.to_owned() }"#,
with_cached_flag = true,
result = true,
)]
fn get_tag_names(_repo_name: &str, repo: &git2::Repository) -> Result<cached::Return<Vec<String>>> {
repo.tag_names(None)?
.into_iter()
.map(|tag| {
tag.ok_or_else(|| anyhow!("Tag name is not valid UTF8: {:?}", tag))
.map(String::from)
})
.collect::<Result<Vec<String>>>()
.map(cached::Return::new)
}
|