OXYGENHUB
# 75d611ab99d1 oxygenhub / crates / hub / src / web / repo.rs
18.3 KiB · Rust · last changed Cache each file's last change and its blame by state and path, and serve ?plain=1 without computing either 1 month ago source blame history raw
1
//! Repository overview, tree, file, raw and blame pages.
2
3
use super::{filters, lookup, Error, Page, RepoShell, Shell};
4
use crate::auth::Viewer;
5
use crate::render::{self, highlight};
6
use crate::repos::view::{self, EntryKind, FileBody};
7
use crate::Shared;
8
use askama::Template;
9
use axum::extract::{Path, Query, State};
10
use axum::response::{IntoResponse, Response};
11
use serde::Deserialize;
12
13
#[derive(Template)]
14
#[template(path = "overview.html")]
15
pub struct OverviewPage {
16
    shell: Shell,
17
    repo: RepoShell,
18
    o: view::Overview,
19
    readme_html: Option<String>,
20
    watching: bool,
21
    latest_release: Option<crate::db::releases::Release>,
22
    /// Set while the repository has no changes (hub spec §6).
23
    quickstart: Option<Quickstart>,
24
    fork: super::forks::ForkInfo,
25
    starred: bool,
26
    stars: i64,
27
    watchers: i64,
28
    topics: Vec<String>,
29
}
30
31
/// A remote name from the hub's host: its first label when that is a
32
/// plain lowercase word (`earth` for `https://earth.example.org:7343`),
33
/// otherwise `hub`.
34
fn remote_name(base_url: &str) -> String {
35
    let host = base_url.split("://").nth(1).unwrap_or("").split(['/', ':']).next().unwrap_or("");
36
    let first = host.split('.').next().unwrap_or("");
37
    let plain = first.len() <= 20
38
        && first.starts_with(|c: char| c.is_ascii_lowercase())
39
        && first.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit());
40
    if plain && first != "www" && first != "localhost" {
41
        first.to_string()
42
    } else {
43
        "hub".to_string()
44
    }
45
}
46
47
/// The commands that get a first push into an empty repository, filled
48
/// in for this hub and this repository.
49
pub struct Quickstart {
50
    /// A remote name taken from the hub's host name (`earth` for
51
    /// `earth.example.org`), `hub` when that is not a plain word.
52
    pub remote: String,
53
    /// Where to download the hub's certificate, when it is self-signed.
54
    pub ca_url: Option<String>,
55
    /// Where the commands save that certificate.
56
    pub ca_file: String,
57
    /// Whether the viewer may push here.
58
    pub can_push: bool,
59
}
60
61
impl Quickstart {
62
    pub fn new(app: &Shared, can_push: bool) -> Quickstart {
63
        let base = app.config.base_url().trim_end_matches('/').to_string();
64
        let remote = remote_name(&base);
65
        let ca_file = format!("~/.config/oxy/{remote}-ca.pem");
66
        let ca_url = app.config.tls.self_signed.then(|| format!("{base}/.well-known/oxyhub-ca.pem"));
67
        Quickstart { remote, ca_url, ca_file, can_push }
68
    }
69
70
    /// Connect a checkout to this repository and push; the token line
71
    /// stays a placeholder so no secret is ever printed.
72
    pub fn connect(&self, clone_url: &str) -> String {
73
        let r = &self.remote;
74
        let mut s = format!("oxy remote add {r} {clone_url}\n");
75
        if let Some(ca) = &self.ca_url {
76
            s += &format!("curl -fsSL {ca} -o {}\n", self.ca_file);
77
            s += &format!("oxy config remote.{r}.ca {}\n", self.ca_file);
78
        }
79
        s += &format!("oxy config remote.{r}.token YOUR-TOKEN\n");
80
        s += &format!("oxy push {r}");
81
        s
82
    }
83
84
    /// Start a repository from nothing.
85
    pub fn fresh(&self, name: &str) -> String {
86
        format!("mkdir {name} && cd {name}\noxy init\necho \"# {name}\" > README.md\noxy commit -m \"First change\"")
87
    }
88
89
    /// Bring a Git branch across.
90
    pub fn git_import(&self, name: &str) -> String {
91
        format!("oxy git import --branch main path/to/git-checkout {name}\ncd {name}")
92
    }
93
}
94
95
pub async fn overview(
96
    State(app): State<Shared>,
97
    viewer: Viewer,
98
    Path((owner, name)): Path<(String, String)>,
99
) -> Result<Page<OverviewPage>, Error> {
100
    let shell = Shell::new(&app, &format!("/{owner}/{name}"), &viewer);
101
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
102
    let default = row.default_bookmark.clone();
103
    let o =
104
        app.repos.read(&repo, move |r, cache| view::overview(r, cache, &default)).await.map_err(|e| Error::from_repo(shell.clone(), e))?;
105
    let rs = RepoShell::new(&app, &row, "code").with_role(role);
106
    let readme_html = o.readme_html(&rs, &o.tree.target.label);
107
    let watching = match viewer.account() {
108
        Some(a) => crate::db::notify::watching(&app.db, a.id, row.id).await.unwrap_or(false),
109
        None => false,
110
    };
111
    let latest_release = crate::db::releases::latest(&app.db, row.id).await.ok().flatten();
112
    let quickstart = (o.changes == 0).then(|| Quickstart::new(&app, rs.can(crate::auth::Access::Write)));
113
    let fork = super::forks::info(&app, &shell, &viewer, &row).await;
114
    let starred = match viewer.account() {
115
        Some(a) => crate::db::repos::starred(&app.db, a.id, row.id).await.unwrap_or(false),
116
        None => false,
117
    };
118
    let (stars, watchers) = crate::db::repos::counts(&app.db, row.id).await.unwrap_or((0, 0));
119
    let topics = row.topic_list().into_iter().map(String::from).collect();
120
    Ok(Page(OverviewPage { shell, repo: rs, o, readme_html, watching, latest_release, quickstart, fork, starred, stars, watchers, topics }))
121
}
122
123
impl view::Overview {
124
    fn readme_html(&self, rs: &RepoShell, r: &str) -> Option<String> {
125
        self.tree.readme.as_ref().map(|md| {
126
            let dir = md.path.rsplit_once('/').map(|(d, _)| format!("{d}/")).unwrap_or_default();
127
            render::markdown::render(&md.text, &format!("{}/file/{r}/{dir}", rs.url), &format!("{}/raw/{r}/{dir}", rs.url))
128
        })
129
    }
130
}
131
132
#[derive(Template)]
133
#[template(path = "tree.html")]
134
pub struct TreePage {
135
    shell: Shell,
136
    repo: RepoShell,
137
    t: view::Tree,
138
    readme_html: Option<String>,
139
}
140
141
pub async fn tree_root(
142
    State(app): State<Shared>,
143
    viewer: Viewer,
144
    Path((owner, name, r)): Path<(String, String, String)>,
145
) -> Result<Page<TreePage>, Error> {
146
    tree(State(app), viewer, Path((owner, name, r, String::new()))).await
147
}
148
149
pub async fn tree(
150
    State(app): State<Shared>,
151
    viewer: Viewer,
152
    Path((owner, name, r, path)): Path<(String, String, String, String)>,
153
) -> Result<Page<TreePage>, Error> {
154
    let shell = Shell::new(&app, &format!("/{owner}/{name}/tree/{r}/{path}"), &viewer);
155
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
156
    let (spec, dir) = (r.clone(), path.clone());
157
    let t = app
158
        .repos
159
        .read(&repo, move |repo, cache| {
160
            let target = view::resolve(repo, cache, &spec)?;
161
            view::tree(repo, cache, target, &dir)
162
        })
163
        .await
164
        .map_err(|e| Error::from_repo(shell.clone(), e))?;
165
    let rs = RepoShell::new(&app, &row, "code").with_role(role);
166
    let readme_html = t.readme.as_ref().map(|md| {
167
        let dir = md.path.rsplit_once('/').map(|(d, _)| format!("{d}/")).unwrap_or_default();
168
        render::markdown::render(&md.text, &format!("{}/file/{r}/{dir}", rs.url), &format!("{}/raw/{r}/{dir}", rs.url))
169
    });
170
    Ok(Page(TreePage { shell, repo: rs, t, readme_html }))
171
}
172
173
#[derive(Deserialize)]
174
pub struct FileQuery {
175
    plain: Option<String>,
176
    blame: Option<String>,
177
    /// `source` shows a Markdown file's text instead of rendering it.
178
    view: Option<String>,
179
}
180
181
#[derive(Template)]
182
#[template(path = "file.html")]
183
pub struct FilePage {
184
    shell: Shell,
185
    repo: RepoShell,
186
    f: view::FileView,
187
    /// Highlighted lines when text.
188
    lines: Vec<String>,
189
    language: Option<&'static str>,
190
    markdown_html: Option<String>,
191
    is_image: bool,
192
    is_markdown: bool,
193
    blame: Option<Vec<BlameRow>>,
194
    /// The `{ref}` of this state for good (hub spec §6.6).
195
    permalink: Option<String>,
196
}
197
198
/// A blame gutter entry: shown only on the first line of a run.
199
pub struct BlameRow {
200
    pub first: bool,
201
    pub hash: String,
202
    pub short: String,
203
    pub author: String,
204
    pub when_ms: u64,
205
    pub run: usize,
206
    /// 0 (the file's oldest line) to 9 (its newest), for shading.
207
    pub age: u8,
208
    /// The state address of the change's dependencies.
209
    pub before: Option<String>,
210
}
211
212
/// Shade steps from `when` within the file's span of times.
213
fn age_step(when: u64, oldest: u64, newest: u64) -> u8 {
214
    if newest <= oldest {
215
        return 9;
216
    }
217
    (((when.saturating_sub(oldest)) as u128 * 9 / (newest - oldest) as u128) as u8).min(9)
218
}
219
220
fn blame_rows(
221
    lines: &[oxy_core::repo::BlameLine],
222
    before: &std::collections::HashMap<oxy_core::hash::ChangeHash, Option<String>>,
223
) -> Vec<BlameRow> {
224
    let oldest = lines.iter().map(|l| l.when_ms).min().unwrap_or(0);
225
    let newest = lines.iter().map(|l| l.when_ms).max().unwrap_or(0);
226
    let mut rows: Vec<BlameRow> = Vec::with_capacity(lines.len());
227
    let mut i = 0;
228
    while i < lines.len() {
229
        let mut run = 1;
230
        while i + run < lines.len() && lines[i + run].hash == lines[i].hash {
231
            run += 1;
232
        }
233
        for k in 0..run {
234
            let l = &lines[i + k];
235
            rows.push(BlameRow {
236
                first: k == 0,
237
                hash: l.hash.to_hex(),
238
                short: l.hash.short(8),
239
                author: l.author.clone(),
240
                when_ms: l.when_ms,
241
                run,
242
                age: age_step(l.when_ms, oldest, newest),
243
                before: before.get(&l.hash).cloned().flatten(),
244
            });
245
        }
246
        i += run;
247
    }
248
    rows
249
}
250
251
pub async fn file(
252
    State(app): State<Shared>,
253
    viewer: Viewer,
254
    Path((owner, name, r, path)): Path<(String, String, String, String)>,
255
    Query(q): Query<FileQuery>,
256
) -> Result<Response, Error> {
257
    let shell = Shell::new(&app, &format!("/{owner}/{name}/file/{r}/{path}"), &viewer);
258
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
259
    if path == super::flow::RUN_LINK && !has_file(&app, &repo, &r, &path).await {
260
        return Ok(axum::response::Redirect::to(&format!("/{owner}/{name}/flow/at/{r}")).into_response());
261
    }
262
    let limit = app.config.limits.render_limit.0;
263
    if q.plain.is_some() {
264
        // Bytes only: no last change, sidebar or highlighting to compute.
265
        let (spec, p) = (r.clone(), path.clone());
266
        let (file_name, bytes) = app
267
            .repos
268
            .read(&repo, move |repo, cache| view::raw(repo, cache, &view::resolve(repo, cache, &spec)?, &p))
269
            .await
270
            .map_err(|e| Error::from_repo(shell.clone(), e))?;
271
        return Ok(super::raw(bytes, &file_name, true));
272
    }
273
    let want_blame = q.blame.is_some();
274
    let (spec, p) = (r.clone(), path.clone());
275
    let (f, blame) = app
276
        .repos
277
        .read(&repo, move |repo, cache| {
278
            let target = view::resolve(repo, cache, &spec)?;
279
            if want_blame {
280
                let b = view::blame(repo, cache, target, &p, limit)?;
281
                let rows = blame_rows(&b.lines, &b.before);
282
                Ok((b.file, Some(rows)))
283
            } else {
284
                Ok((view::file(repo, cache, target, &p, limit)?, None))
285
            }
286
        })
287
        .await
288
        .map_err(|e| Error::from_repo(shell.clone(), e))?;
289
    if q.plain.is_some() {
290
        return Ok(super::raw(f.bytes, &f.name, true));
291
    }
292
    let rs = RepoShell::new(&app, &row, "code").with_role(role);
293
    let is_image = super::mime_of(&f.name).starts_with("image/");
294
    let is_markdown = f.name.to_lowercase().ends_with(".md") || f.name.to_lowercase() == "readme";
295
    let want_source = q.view.as_deref() == Some("source") || blame.is_some();
296
    let (lines, language, markdown_html) = match &f.body {
297
        FileBody::Text(text) if !is_image => {
298
            if is_markdown && !want_source {
299
                let dir = f.path.rsplit_once('/').map(|(d, _)| format!("{d}/")).unwrap_or_default();
300
                let (base, raw) = (format!("{}/file/{r}/{dir}", rs.url), format!("{}/raw/{r}/{dir}", rs.url));
301
                (vec![], highlight::language(&f.path), Some(render::markdown::render(text, &base, &raw)))
302
            } else {
303
                (highlight::lines(&f.path, text), highlight::language(&f.path), None)
304
            }
305
        }
306
        _ => (vec![], None, None),
307
    };
308
    let permalink = f.target.permalink();
309
    Ok(Page(FilePage { shell, repo: rs, f, lines, language, markdown_html, is_image, is_markdown, blame, permalink }).into_response())
310
}
311
312
/// Whether `path` is a file in the state `r` names.
313
async fn has_file(app: &Shared, repo: &crate::repos::RepoName, r: &str, path: &str) -> bool {
314
    let (spec, p) = (r.to_string(), path.to_string());
315
    app.repos
316
        .read(repo, move |repo, cache| {
317
            let target = view::resolve(repo, cache, &spec)?;
318
            Ok(cache.load(repo, &target.heads)?.snapshot.files.get(&p).is_some_and(|f| f.kind != oxy_core::objects::NodeKind::Dir))
319
        })
320
        .await
321
        .unwrap_or(false)
322
}
323
324
/// `GET /{owner}/{name}/raw/{ref}/{path}`: a file's bytes (hub spec §6.3),
325
/// where relative images in rendered Markdown point. `.flow/stamp` is the
326
/// state stamp of `{ref}` unless a real file is there (§13.2).
327
pub async fn raw_file(
328
    State(app): State<Shared>,
329
    viewer: Viewer,
330
    Path((owner, name, r, path)): Path<(String, String, String, String)>,
331
) -> Result<Response, Error> {
332
    let shell = Shell::new(&app, &format!("/{owner}/{name}/raw/{r}/{path}"), &viewer);
333
    let (repo, _row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
334
    if path == super::flow::STAMP_IMAGE && !has_file(&app, &repo, &r, &path).await {
335
        return Ok(axum::response::Redirect::to(&format!("/{owner}/{name}/flow/stamp/{r}")).into_response());
336
    }
337
    let (spec, p) = (r.clone(), path.clone());
338
    let (file_name, bytes) = app
339
        .repos
340
        .read(&repo, move |repo, cache| {
341
            let target = view::resolve(repo, cache, &spec)?;
342
            view::raw(repo, cache, &target, &p)
343
        })
344
        .await
345
        .map_err(|e| Error::from_repo(shell.clone(), e))?;
346
    Ok(super::raw(bytes, &file_name, true))
347
}
348
349
#[derive(Deserialize)]
350
pub struct LastQuery {
351
    /// Comma-separated paths.
352
    paths: String,
353
}
354
355
/// `GET /{owner}/{name}/last/{ref}?paths=a,b` → JSON of last change per
356
/// path, fetched by the tree page after it paints (hub spec §6.5).
357
pub async fn last_changes(
358
    State(app): State<Shared>,
359
    viewer: Viewer,
360
    Path((owner, name, r)): Path<(String, String, String)>,
361
    Query(q): Query<LastQuery>,
362
) -> Result<Response, Error> {
363
    let shell = Shell::new(&app, "", &viewer);
364
    let (repo, _, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
365
    let paths: Vec<String> = q.paths.split(',').filter(|p| !p.is_empty()).take(500).map(String::from).collect();
366
    let spec = r.clone();
367
    let found = app
368
        .repos
369
        .read(&repo, move |repo, cache| {
370
            let target = view::resolve(repo, cache, &spec)?;
371
            view::last_changes(repo, cache, &target.heads, &paths)
372
        })
373
        .await
374
        .map_err(|e| Error::from_repo(shell.clone(), e))?;
375
    let now = crate::db::now_ms() as u64;
376
    let body: Vec<serde_json::Value> = found
377
        .into_iter()
378
        .map(|(p, c)| {
379
            serde_json::json!({ "path": p, "hash": c.hash, "short": c.short, "title": c.title, "ago": render::time::ago(c.when_ms, now), "when": render::time::absolute(c.when_ms) })
380
        })
381
        .collect();
382
    Ok(axum::Json(body).into_response())
383
}
384
385
impl EntryKind {
386
    pub fn glyph(&self) -> &'static str {
387
        match self {
388
            EntryKind::Dir => "▸",
389
            EntryKind::File => "·",
390
            EntryKind::Symlink => "↪",
391
            EntryKind::Subrepo => "◫",
392
        }
393
    }
394
}
395
396
#[derive(Deserialize)]
397
pub struct CodeQuery {
398
    #[serde(default)]
399
    q: String,
400
    #[serde(default, rename = "ref")]
401
    r: Option<String>,
402
    #[serde(default)]
403
    path: String,
404
    #[serde(default)]
405
    re: Option<String>,
406
    #[serde(default)]
407
    case: Option<String>,
408
}
409
410
#[derive(Template)]
411
#[template(path = "code_search.html")]
412
pub struct CodeSearchPage {
413
    shell: Shell,
414
    repo: RepoShell,
415
    q: String,
416
    r: String,
417
    path: String,
418
    regex: bool,
419
    case: bool,
420
    results: crate::repos::search::Results,
421
    error: Option<String>,
422
}
423
424
/// `GET /{owner}/{name}/search?q=…&ref=…&re=1&case=1&path=…` (hub spec §11).
425
pub async fn code_search(
426
    State(app): State<Shared>,
427
    viewer: Viewer,
428
    Path((owner, name)): Path<(String, String)>,
429
    Query(q): Query<CodeQuery>,
430
) -> Result<Page<CodeSearchPage>, Error> {
431
    let shell = Shell::new(&app, &format!("/{owner}/{name}/search"), &viewer);
432
    let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?;
433
    let r = q.r.clone().filter(|x| !x.is_empty()).unwrap_or_else(|| row.default_bookmark.clone());
434
    let (regex, case) = (q.re.is_some(), q.case.is_some());
435
    let mut results = crate::repos::search::Results::default();
436
    let mut error = None;
437
    if !q.q.trim().is_empty() {
438
        let (text, spec, prefix) = (q.q.clone(), r.clone(), q.path.trim_matches('/').to_string());
439
        let app2 = app.clone();
440
        let out = app
441
            .repos
442
            .read(&repo, move |repo, cache| {
443
                let target = view::resolve(repo, cache, &spec)?;
444
                let loaded = cache.load(repo, &target.heads)?;
445
                let state = oxy_core::objects::StateObject::new(loaded.heads.clone()).hash().to_hex();
446
                let key = format!("{state}\0{text}\0{regex}\0{case}\0{prefix}");
447
                if let Some(hit) = app2.repos.searches.get(&key) {
448
                    return Ok(Ok(hit));
449
                }
450
                let res = crate::repos::search::run(
451
                    &loaded,
452
                    &crate::repos::search::Query { text: &text, regex, case_sensitive: case, path_prefix: &prefix },
453
                );
454
                if let Ok(r) = &res {
455
                    app2.repos.searches.put(key, r.clone());
456
                }
457
                Ok(res)
458
            })
459
            .await
460
            .map_err(|e| Error::from_repo(shell.clone(), e))?;
461
        match out {
462
            Ok(r) => results = r,
463
            Err(e) => error = Some(e),
464
        }
465
    }
466
    Ok(Page(CodeSearchPage {
467
        shell,
468
        repo: RepoShell::new(&app, &row, "code").with_role(role),
469
        q: q.q,
470
        r,
471
        path: q.path,
472
        regex,
473
        case,
474
        results,
475
        error,
476
    }))
477
}
478
479
#[cfg(test)]
480
mod tests {
481
    use super::remote_name;
482
483
    #[test]
484
    fn remote_names_come_from_the_host() {
485
        assert_eq!(remote_name("https://earth.andymccall.xyz:7343"), "earth");
486
        assert_eq!(remote_name("https://hub.example.org"), "hub");
487
        assert_eq!(remote_name("http://127.0.0.1:7300"), "hub");
488
        assert_eq!(remote_name("http://localhost:7300"), "hub");
489
        assert_eq!(remote_name("https://www.example.org"), "hub");
490
        assert_eq!(remote_name("https://My-Host.example"), "hub");
491
    }
492
}