| 1 |
|
| 2 |
|
| 3 |
use super::{filters, lookup, Error, Page, RepoShell, Shell}; |
| 4 |
use crate::auth::Viewer; |
| 5 |
use crate::render::{self, diff, graph}; |
| 6 |
use crate::repos::view; |
| 7 |
use crate::Shared; |
| 8 |
use askama::Template; |
| 9 |
use axum::extract::{Path, Query, State}; |
| 10 |
use axum::response::{IntoResponse, Response}; |
| 11 |
use oxy_core::repo::FileDiff; |
| 12 |
use serde::Deserialize; |
| 13 |
use std::collections::BTreeMap; |
| 14 |
|
| 15 |
const PAGE: usize = 50; |
| 16 |
|
| 17 |
#[derive(Deserialize)] |
| 18 |
pub struct HistoryQuery { |
| 19 |
path: Option<String>, |
| 20 |
offset: Option<usize>, |
| 21 |
} |
| 22 |
|
| 23 |
#[derive(Template)] |
| 24 |
#[template(path = "history.html")] |
| 25 |
pub struct HistoryPage { |
| 26 |
shell: Shell, |
| 27 |
repo: RepoShell, |
| 28 |
h: view::History, |
| 29 |
rows: Vec<graph::Row>, |
| 30 |
lanes: usize, |
| 31 |
} |
| 32 |
|
| 33 |
pub async fn history( |
| 34 |
State(app): State<Shared>, |
| 35 |
viewer: Viewer, |
| 36 |
Path((owner, name, r)): Path<(String, String, String)>, |
| 37 |
Query(q): Query<HistoryQuery>, |
| 38 |
) -> Result<Page<HistoryPage>, Error> { |
| 39 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/changes/{r}"), &viewer); |
| 40 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 41 |
let spec = r.clone(); |
| 42 |
let path = q.path.clone().filter(|p| !p.is_empty()); |
| 43 |
let offset = q.offset.unwrap_or(0); |
| 44 |
let h = app |
| 45 |
.repos |
| 46 |
.read(&repo, move |repo, cache| { |
| 47 |
let target = view::resolve(repo, cache, &spec)?; |
| 48 |
view::history(repo, cache, target, path.as_deref(), offset, PAGE) |
| 49 |
}) |
| 50 |
.await |
| 51 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 52 |
let mut h = h; |
| 53 |
super::attribute(&app, &mut h.entries.iter_mut().collect::<Vec<_>>()).await; |
| 54 |
let graph_input: Vec<(String, Vec<String>)> = h.entries.iter().map(|c| (c.hash.clone(), c.deps.clone())).collect(); |
| 55 |
let rows = graph::layout(&graph_input); |
| 56 |
let lanes = graph::width(&rows); |
| 57 |
Ok(Page(HistoryPage { shell, repo: RepoShell::new(&app, &row, "changes").with_role(role), h, rows, lanes })) |
| 58 |
} |
| 59 |
|
| 60 |
#[derive(Deserialize)] |
| 61 |
pub struct FunctionQuery { |
| 62 |
start: usize, |
| 63 |
end: usize, |
| 64 |
#[serde(default)] |
| 65 |
name: String, |
| 66 |
} |
| 67 |
|
| 68 |
#[derive(Template)] |
| 69 |
#[template(path = "function.html")] |
| 70 |
pub struct FunctionPage { |
| 71 |
shell: Shell, |
| 72 |
repo: RepoShell, |
| 73 |
r: String, |
| 74 |
path: String, |
| 75 |
name: String, |
| 76 |
start: usize, |
| 77 |
end: usize, |
| 78 |
entries: Vec<view::ChangeView>, |
| 79 |
rows: Vec<graph::Row>, |
| 80 |
lanes: usize, |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub async fn function( |
| 88 |
State(app): State<Shared>, |
| 89 |
viewer: Viewer, |
| 90 |
Path((owner, name, r, path)): Path<(String, String, String, String)>, |
| 91 |
Query(q): Query<FunctionQuery>, |
| 92 |
) -> Result<Page<FunctionPage>, Error> { |
| 93 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/function/{r}/{path}"), &viewer); |
| 94 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 95 |
let (spec, p, start, end) = (r.clone(), path.clone(), q.start, q.end); |
| 96 |
let mut entries = app |
| 97 |
.repos |
| 98 |
.read(&repo, move |repo, cache| { |
| 99 |
let target = view::resolve(repo, cache, &spec)?; |
| 100 |
let history = repo.line_history(&target.heads, &p, start, end)?; |
| 101 |
let labels = view::head_labels(repo)?; |
| 102 |
Ok(history.iter().map(|e| view::change_view_pub(repo, &e.hash, &e.change, e.is_head, &labels)).collect::<Vec<_>>()) |
| 103 |
}) |
| 104 |
.await |
| 105 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 106 |
|
| 107 |
let listed: std::collections::HashSet<String> = entries.iter().map(|c| c.hash.clone()).collect(); |
| 108 |
for c in &mut entries { |
| 109 |
c.deps.retain(|d| listed.contains(d)); |
| 110 |
} |
| 111 |
super::attribute(&app, &mut entries.iter_mut().collect::<Vec<_>>()).await; |
| 112 |
let graph_input: Vec<(String, Vec<String>)> = entries.iter().map(|c| (c.hash.clone(), c.deps.clone())).collect(); |
| 113 |
let rows = graph::layout(&graph_input); |
| 114 |
let lanes = graph::width(&rows); |
| 115 |
Ok(Page(FunctionPage { |
| 116 |
shell, |
| 117 |
repo: RepoShell::new(&app, &row, "code").with_role(role), |
| 118 |
r, |
| 119 |
path, |
| 120 |
name: q.name, |
| 121 |
start, |
| 122 |
end, |
| 123 |
entries, |
| 124 |
rows, |
| 125 |
lanes, |
| 126 |
})) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
pub struct FileRows { |
| 131 |
pub path: String, |
| 132 |
pub from: Option<String>, |
| 133 |
pub kind: String, |
| 134 |
pub lines: Vec<diff::Line>, |
| 135 |
pub added: usize, |
| 136 |
pub removed: usize, |
| 137 |
pub anchor: String, |
| 138 |
} |
| 139 |
|
| 140 |
pub fn render_files(files: &[FileDiff], bases: &BTreeMap<String, Vec<u8>>) -> Vec<FileRows> { |
| 141 |
render_files_with(files, bases, diff::Options::default()) |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
pub fn render_files_with(files: &[FileDiff], bases: &BTreeMap<String, Vec<u8>>, opts: diff::Options) -> Vec<FileRows> { |
| 146 |
files |
| 147 |
.iter() |
| 148 |
.enumerate() |
| 149 |
.map(|(i, f)| { |
| 150 |
let base = bases.get(&f.path).map(Vec::as_slice).unwrap_or(b""); |
| 151 |
let lines = diff::unified_with(base, &f.hunks, 3, opts); |
| 152 |
FileRows { |
| 153 |
path: f.path.clone(), |
| 154 |
from: f.from.clone(), |
| 155 |
kind: f.kind.clone(), |
| 156 |
added: lines.iter().filter(|l| l.kind == diff::Kind::Add).count(), |
| 157 |
removed: lines.iter().filter(|l| l.kind == diff::Kind::Del).count(), |
| 158 |
lines, |
| 159 |
anchor: format!("diff-{i}"), |
| 160 |
} |
| 161 |
}) |
| 162 |
.collect() |
| 163 |
} |
| 164 |
|
| 165 |
#[derive(Template)] |
| 166 |
#[template(path = "change.html")] |
| 167 |
pub struct ChangePage { |
| 168 |
shell: Shell, |
| 169 |
repo: RepoShell, |
| 170 |
c: view::ChangeDetail, |
| 171 |
files: Vec<FileRows>, |
| 172 |
body_html: String, |
| 173 |
ignore_ws: bool, |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
#[derive(Deserialize, Default)] |
| 178 |
pub struct DiffQuery { |
| 179 |
#[serde(default)] |
| 180 |
w: Option<String>, |
| 181 |
} |
| 182 |
|
| 183 |
impl DiffQuery { |
| 184 |
pub fn options(&self) -> diff::Options { |
| 185 |
diff::Options { ignore_whitespace: self.w.as_deref() == Some("1") } |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
pub async fn change( |
| 190 |
State(app): State<Shared>, |
| 191 |
viewer: Viewer, |
| 192 |
Path((owner, name, hash)): Path<(String, String, String)>, |
| 193 |
Query(q): Query<DiffQuery>, |
| 194 |
) -> Result<Page<ChangePage>, Error> { |
| 195 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/change/{hash}"), &viewer); |
| 196 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 197 |
let spec = hash.clone(); |
| 198 |
let c = app |
| 199 |
.repos |
| 200 |
.read(&repo, move |repo, cache| { |
| 201 |
let h = view::resolve_change(repo, cache, &spec)?; |
| 202 |
view::change(repo, cache, &h) |
| 203 |
}) |
| 204 |
.await |
| 205 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 206 |
let mut c = c; |
| 207 |
{ |
| 208 |
let mut all: Vec<&mut view::ChangeView> = vec![&mut c.view]; |
| 209 |
all.extend(c.deps.iter_mut()); |
| 210 |
all.extend(c.dependents.iter_mut()); |
| 211 |
super::attribute(&app, &mut all).await; |
| 212 |
} |
| 213 |
let files = render_files_with(&c.files, &c.bases, q.options()); |
| 214 |
let ignore_ws = q.options().ignore_whitespace; |
| 215 |
let body_html = |
| 216 |
if c.view.body.is_empty() { String::new() } else { format!("<pre class=\"msg\">{}</pre>", render::escape(&c.view.body)) }; |
| 217 |
Ok(Page(ChangePage { shell, repo: RepoShell::new(&app, &row, "changes").with_role(role), c, files, body_html, ignore_ws })) |
| 218 |
} |
| 219 |
|
| 220 |
#[derive(Template)] |
| 221 |
#[template(path = "compare.html")] |
| 222 |
pub struct ComparePage { |
| 223 |
shell: Shell, |
| 224 |
repo: RepoShell, |
| 225 |
cmp: view::Compare, |
| 226 |
files: Vec<FileRows>, |
| 227 |
ignore_ws: bool, |
| 228 |
|
| 229 |
base_spec: String, |
| 230 |
head_spec: String, |
| 231 |
|
| 232 |
bookmarks: Vec<String>, |
| 233 |
tags: Vec<String>, |
| 234 |
propose: Option<Propose>, |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
pub struct Propose { |
| 239 |
pub url: String, |
| 240 |
|
| 241 |
pub existing: Option<i64>, |
| 242 |
} |
| 243 |
|
| 244 |
#[derive(Deserialize)] |
| 245 |
pub struct CompareForm { |
| 246 |
base: Option<String>, |
| 247 |
head: Option<String>, |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
pub async fn compare_index( |
| 253 |
State(app): State<Shared>, |
| 254 |
viewer: Viewer, |
| 255 |
Path((owner, name)): Path<(String, String)>, |
| 256 |
Query(q): Query<CompareForm>, |
| 257 |
) -> Result<Response, Error> { |
| 258 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/compare"), &viewer); |
| 259 |
let (repo, row, _role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 260 |
let clean = |s: Option<String>| s.map(|x| x.trim().to_string()).filter(|x| !x.is_empty() && !x.contains("...")); |
| 261 |
let (base, head) = match (clean(q.base), clean(q.head)) { |
| 262 |
(Some(b), Some(h)) => (b, h), |
| 263 |
(b, h) => { |
| 264 |
let default = row.default_bookmark.clone(); |
| 265 |
let d = default.clone(); |
| 266 |
let newest = app |
| 267 |
.repos |
| 268 |
.read(&repo, move |r, cache| { |
| 269 |
Ok(view::bookmarks(r, cache, &d)?.into_iter().filter(|b| !b.default).max_by_key(|b| b.latest_ms).map(|b| b.name)) |
| 270 |
}) |
| 271 |
.await |
| 272 |
.ok() |
| 273 |
.flatten(); |
| 274 |
(b.unwrap_or_else(|| default.clone()), h.unwrap_or_else(|| newest.unwrap_or(default))) |
| 275 |
} |
| 276 |
}; |
| 277 |
let enc = |s: &str| s.replace('%', "%25").replace(' ', "%20").replace('?', "%3F").replace('#', "%23"); |
| 278 |
Ok(axum::response::Redirect::to(&format!("/{owner}/{name}/compare/{}...{}", enc(&base), enc(&head))).into_response()) |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
pub async fn compare( |
| 283 |
State(app): State<Shared>, |
| 284 |
viewer: Viewer, |
| 285 |
Path((owner, name, spec)): Path<(String, String, String)>, |
| 286 |
Query(q): Query<DiffQuery>, |
| 287 |
) -> Result<Page<ComparePage>, Error> { |
| 288 |
let shell = Shell::new(&app, &format!("/{owner}/{name}/compare/{spec}"), &viewer); |
| 289 |
let (repo, row, role) = lookup(&app, &shell, &viewer, &owner, &name).await?; |
| 290 |
let Some((a, b)) = spec.split_once("...") else { |
| 291 |
return Err(Error::bad(shell, "compare takes base...head")); |
| 292 |
}; |
| 293 |
let (a, b) = (a.to_string(), b.to_string()); |
| 294 |
let cmp = app |
| 295 |
.repos |
| 296 |
.read(&repo, move |repo, cache| { |
| 297 |
let base = view::resolve(repo, cache, &a)?; |
| 298 |
let head = view::resolve(repo, cache, &b)?; |
| 299 |
view::compare(repo, cache, base, head) |
| 300 |
}) |
| 301 |
.await |
| 302 |
.map_err(|e| Error::from_repo(shell.clone(), e))?; |
| 303 |
let mut cmp = cmp; |
| 304 |
super::attribute(&app, &mut cmp.added.iter_mut().collect::<Vec<_>>()).await; |
| 305 |
let files = render_files_with(&cmp.files, &cmp.bases, q.options()); |
| 306 |
let ignore_ws = q.options().ignore_whitespace; |
| 307 |
let rs = RepoShell::new(&app, &row, "changes").with_role(role); |
| 308 |
let (bookmarks, tags) = app |
| 309 |
.repos |
| 310 |
.read(&repo, |r, _| { |
| 311 |
let b: Vec<String> = r.bookmarks()?.into_iter().map(|b| b.name).collect(); |
| 312 |
let t: Vec<String> = view::tags(r)?.into_iter().map(|t| t.name).collect(); |
| 313 |
Ok((b, t)) |
| 314 |
}) |
| 315 |
.await |
| 316 |
.unwrap_or_default(); |
| 317 |
let both_bookmarks = cmp.base.kind == view::RefKind::Bookmark && cmp.head.kind == view::RefKind::Bookmark; |
| 318 |
let propose = if both_bookmarks && cmp.base.label != cmp.head.label && !cmp.added.is_empty() && rs.can(crate::auth::Access::Write) { |
| 319 |
let open = crate::db::proposals::list(&app.db, row.id, &["open"]).await.unwrap_or_default(); |
| 320 |
let existing = open |
| 321 |
.iter() |
| 322 |
.find(|p| p.source_kind == "bookmark" && p.source_bookmark == cmp.head.label && p.target == cmp.base.label) |
| 323 |
.map(|p| p.number); |
| 324 |
Some(Propose { url: format!("{}/proposals/new?source={}&target={}", rs.url, cmp.head.label, cmp.base.label), existing }) |
| 325 |
} else { |
| 326 |
None |
| 327 |
}; |
| 328 |
let (base_spec, head_spec) = spec.split_once("...").map(|(a, b)| (a.to_string(), b.to_string())).unwrap_or_default(); |
| 329 |
Ok(Page(ComparePage { shell, repo: rs, cmp, files, ignore_ws, base_spec, head_spec, bookmarks, tags, propose })) |
| 330 |
} |