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