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