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
|
use std::fs::{metadata, read_dir};
use std::io;
use std::path::{Path, PathBuf};
use eyre::Result;
use git2::Repository;
pub struct State {
pub root: PathBuf,
pub data: Vec<Repository>,
}
// TODO figure out how to make this return impl Stream without living in actual hell
// Heartbreaking: E0733
fn find_repos_in(dir: impl AsRef<Path>) -> Result<Vec<Repository>> {
let dir = dir.as_ref();
let dir = read_dir(dir);
let dir = match dir {
Ok(x) => x,
Err(err) if err.kind() == io::ErrorKind::PermissionDenied => return Ok(vec![]),
Err(err) => Err(err)?,
};
let mut result = vec![];
for subdir in dir {
if let Ok(subdir) = subdir {
match Repository::open_bare(subdir.path()) {
Ok(repo) => result.push(repo),
Err(err) if err.class() == git2::ErrorClass::Repository && err.code() == git2::ErrorCode::NotFound => {
let metadata = metadata(subdir.path())?;
if metadata.is_dir() {
result.extend(find_repos_in(subdir.path())?)
}
}
// TODO handle in a non-god-awful way
Err(err) => panic!("{}", err),
}
}
}
Ok(result)
}
impl State {
pub async fn discover(root: impl AsRef<Path>) -> Result<Self> {
let root = root.as_ref();
let mut data = find_repos_in(root)?;
data.sort_by_key(|repo| {
let mut last_update = git2::Time::new(0, 0);
repo.odb().expect("no object database").foreach(|oid| {
if let Ok(commit) = repo.find_commit(oid.clone()) {
last_update = last_update.max(commit.time());
}
true
}).expect("foreach failed");
last_update
});
data.reverse();
Ok(Self { root: root.to_owned(), data })
}
pub fn relative_path<'a>(&'a self, subdir: &'a Path) -> &'a Path {
subdir.strip_prefix(&self.root).unwrap_or(subdir)
}
pub fn open(&self, path: impl AsRef<Path>) -> Result<Repository> {
let path = self.root.join(path);
Ok(Repository::open_bare(path)?)
}
}
|