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
117
118
119
120
121
122
123
124
125
126
127
|
use std::ops::Deref;
use std::path::Path;
use async_std::sync::{Arc, Mutex};
use eyre::Result;
use tide::prelude::*;
mod state;
mod templates;
use state::State;
#[async_std::main]
async fn main() -> Result<()> {
tide::log::start();
let matches = clap::App::new("gityeet")
.arg(clap::Arg::with_name("root")
.help("sets the directory from which repositories will be found")
.required(true))
.arg(clap::Arg::with_name("port")
.help("sets the port number")
.required(true))
.get_matches();
let root = matches.value_of("root").expect("root is required");
let port = matches.value_of("port").expect("port is required");
let port: u16 = port.parse().expect("port was not valid");
let state = State::discover(root).await?;
// unfortunately, the State has to be both Send and Sync, but git2::Repository is not Sync
// and even an Arc<RwLock<State>> isn't Sync if State isn't Sync, it has to be a Mutex to get Sync
// TODO do something smarter for that like maybe just a vec of repo paths
let mut app = tide::with_state(Arc::new(Mutex::new(state)));
app.at("/").get(index);
{
let mut repo = app.at("/*repo/");
repo.get(repo_index);
repo.at("tree/*path").get(repo_tree_path);
}
app.listen(("127.0.0.1", port)).await?;
Ok(())
}
type Request = tide::Request<Arc<Mutex<State>>>;
async fn index(req: Request) -> tide::Result {
let state = req.state();
let state = state.lock_arc().await;
Ok(templates::Index { state: state.deref() }.into())
}
async fn repo_index(req: Request) -> tide::Result {
let state = req.state();
let repo_rel_path = req.param("repo").expect("no repo in repo-based URL?");
view_tree_path(state, repo_rel_path, "").await
}
async fn repo_tree_path(req: Request) -> tide::Result {
let state = req.state();
let repo_rel_path = req.param("repo").expect("no repo in repo-based URL?");
let tree_rel_path = req.param("path").expect("no path in path-based URL?");
view_tree_path(state, repo_rel_path, tree_rel_path).await
}
async fn view_tree_path(state: &Arc<Mutex<State>>, repo_rel_path: &str, tree_rel_path: &str) -> tide::Result {
let state = state.lock_arc().await;
let repo = state.data.iter()
.find(|repo| state.relative_path(repo.path()) == Path::new(repo_rel_path));
let repo = match repo {
Some(x) => x,
None => return Ok("bruh that's not a real repo".into()),
};
let head = repo.head();
let head = match head {
Ok(x) => x,
Err(err) => return Ok(format!("bruh that repo has no HEAD: {}", err).into()),
};
let head_tree = head.peel_to_tree();
let head_tree = match head_tree {
Ok(x) => x,
Err(err) => return Ok(format!("bruh that repo's HEAD has no tree: {}", err).into()),
};
let selected_object = if tree_rel_path.is_empty() {
head_tree.into_object()
} else {
let selected_tree_entry = head_tree.get_path(Path::new(&tree_rel_path));
let selected_tree_entry = match selected_tree_entry {
Ok(x) => x,
Err(err) => return Ok(format!("bruh that path doesn't exist: {}", err).into()),
};
let selected_object = selected_tree_entry.to_object(repo);
let selected_object = match selected_object {
Ok(x) => x,
Err(err) => return Ok(format!("bruh that path doesn't exist: {}", err).into()),
};
selected_object
};
let tree_rel_path = format!("/{}", tree_rel_path);
if let Some(selected_tree) = selected_object.as_tree() {
let template = templates::RepoFolder {
repo,
repo_path: repo_rel_path,
title: repo_rel_path,
rel_path: &tree_rel_path,
tree: selected_tree.clone(),
};
Ok(template.into())
} else if let Some(selected_blob) = selected_object.as_blob() {
let template = templates::RepoFile {
repo_path: repo_rel_path,
title: repo_rel_path,
rel_path: &tree_rel_path,
blob: selected_blob.clone(),
};
Ok(template.into())
} else {
panic!()
}
}
|