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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
| use git2::{Blob, Object, Repository, Commit, Tree, ObjectType, TreeEntry};
use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
use std::env;
use std::error::Error;
use std::fs::{File, create_dir_all};
use chrono::Local;
const DOMAIN: &str = "https://git.hdm.sh";
struct Config<'a> {
output_path: &'a Path,
files_path: &'a Path,
repo_name: &'a str,
description: &'a str,
repo: &'a Repository
}
const HEADER: &'static str = "
<html lang='en'>\n
<head>\n
<meta charset='UTF-8'>\n
<meta name='viewport' content='width=device-width, initial-scale=1.0'>\n
<meta name='author' content='Herman Malik' />\n
<title>git.mcksp.com</title>\n
<link type='text/css' rel='stylesheet' href='/assets/style.css'>\n
<style>.line-num{text-decoration:none;color:#555;}.icon{width:16px;height:16px;image-rendering: pixelated;padding-right:4px;}.listing > div{margin-bottom:4px;}a.link{text-decoration:none;}.repo-name{padding-right:32px;}</style>\n
</head>\n
<body>\n
<header>\n
<a id='title' href='/'>git.hdm.sh</a></span>\n
<div id='navbar'>\n
<a href='https://hdm.sh/'>home</a>\n
</div>\n
</header>\n
";
fn intro(file: &mut File, repo_name: &str, desc: &str) -> Result<(), Box<dyn Error>> {
write!(file, "<table style='padding-bottom:16px'><tbody><tr><td><img src='/icons/image.png' style='width:32px;height:32px;image-rendering: pixelated'></td>")?;
write!(file, "<td><div><b>{}</b> - {}</div><div>git clone {}/{}.git</div></td></tr></tbody></table>\n", repo_name, desc, DOMAIN, repo_name)?;
Ok(())
}
fn footer(file: &mut File) -> Result<(), Box<dyn Error>> {
write!(file, "
<footer>\n
generated on {} with <a href='https://git.hdm.sh/gituwa-rs'>gituwa-rs</a>, a port of <a href='https://git.mcksp.com/gituwa'>gituwa</a>\n
</footer>\n
</body>\n
</html>\n
", Local::now().format("%Y-%m-%d %H:%M:%S"))?;
Ok(())
}
/// Turn string into escaped HTML.
fn html_encode(s: &str) -> String {
let mut out: String = String::with_capacity(s.len());
for c in s.chars() {
match c {
'<' => out.push_str("<" ),
'>' => out.push_str(">" ),
'\'' => out.push_str("'" ),
'&' => out.push_str("&" ),
'"' => out.push_str("""),
_ => out.push(c),
}
}
out
}
fn print_file(file: &mut File, blob: Blob, lines_number: bool) -> Result<(), Box<dyn Error>> {
let content = blob.content();
write!(file, "<div style='overflow-x:scroll;'><table><tbody><tr>")?;
if lines_number {
write!(file, "<td><pre>")?;
for (i, _line) in content.lines().enumerate() {
write!(file, "<a href='#L{}' class='line-num' id='L{}'>{:5}</a>\n", i + 1, i + 1, i + 1)?;
}
write!(file, "</pre></td>")?;
}
write!(file, "<td><pre style='padding-left:8px'>")?;
write!(file, "{}", html_encode(&String::from_utf8_lossy(content)))?;
write!(file, "</pre></td></tr></tbody></table></div>")?;
Ok(())
}
fn root_readme(file: &mut File, tree: &Tree, repo: &Repository) -> Result<(), Box<dyn Error>> {
write!(file, "<hr><div style='text-align:center;'><img src='img.png'/></div>\n")?;
let readme = tree.get_name("README")
.or_else(|| tree.get_name("README.md"));
if let Some(entry) = readme {
let blob = repo.find_blob(entry.id())?;
print_file(file, blob, false)?;
}
Ok(())
}
fn process_file(tree: &TreeEntry, root: &Path, name: &str, conf: &Config) -> Result<(), Box<dyn Error>> {
let blob = conf.repo.find_blob(tree.id())?;
let path = conf.files_path.join(root).join(format!("{}.html", name));
let mut file = File::create(path)?;
write!(file, "{}", HEADER)?;
if blob.is_binary() {
write!(file, "<p>binary file</p>")?;
} else {
print_file(&mut file, blob, true)?;
}
footer(&mut file)?;
Ok(())
}
fn process_tree(tree: &Tree, tree_path: &Path, conf: &Config, is_root: bool) -> Result<(), Box<dyn Error>> {
let dir_path = conf.files_path.join(tree_path);
create_dir_all(dir_path)?; // TODO 755 perms
let index_path: PathBuf;
if is_root {
index_path = conf.output_path.join("index.html")
} else {
index_path = conf.files_path.join(tree_path).with_extension("html")
}
let mut index_file = File::create(index_path)?;
write!(index_file, "{}", HEADER)?;
intro(&mut index_file, conf.repo_name, conf.description)?;
write!(index_file, "<div class='listing'>")?;
for child in tree {
let child_name = child.name().unwrap_or("<unnamed>");
let child_type = child.kind()
.ok_or_else(|| {format!("Could not determine type of {}", child_name)})?;
let child_path = tree_path.join(child_name);
match child_type {
ObjectType::Blob => {
write!(index_file,
"<div><img src='/icons/text.png' class='icon'><a class='link' href='/{}/{}.html'>{}</a></div>",
conf.files_path.to_string_lossy(),
child_path.to_string_lossy(),
child_name
)?;
process_file(&child, tree_path, child_name, conf)?;
},
ObjectType::Tree => {
write!(index_file,
"<div><img src='/icons/dir.png' class='icon'><a class='link' href='/{}/{}.html'>{}</a></div>",
conf.files_path.to_string_lossy(),
child_path.to_string_lossy(),
child_name
)?;
let child_tree = conf.repo.find_tree(child.id())?;
process_tree(&child_tree, &child_path, conf, false)?;
},
_ => () // Includes Commit and Tag
}
}
if is_root {
root_readme(&mut index_file, tree, conf.repo)?;
}
write!(index_file, "</div>")?;
footer(&mut index_file)?;
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
return Err("usage: gituwa <repo_path> <output_path> [<description>]".into());
}
let repo_path = Path::new(&args[1]);
let output_path = Path::new(&args[2]);
let repo: Repository = Repository::open(&repo_path)?;
let head: Object<'_>= repo.revparse_single("HEAD")?;
let commit: Commit<'_> = match head.into_commit() {
Ok(commit) => commit,
Err(_e) => return Err("failed to rev-parse HEAD".into())
};
let tree: Tree<'_> = commit.tree()?;
let conf= Config {
output_path: output_path,
files_path: &output_path.join(Path::new("files")),
repo_name: &repo_path
.file_stem()
.ok_or("Could not determine repo name from path")?
.to_string_lossy(),
description: &args[3..].join(" "),
repo: &repo
};
// TODO these need 755 perms
create_dir_all(conf.output_path)?;
create_dir_all(conf.files_path)?;
process_tree(&tree, &Path::new(""), &conf, true)?;
Ok(())
}
|