//! A branch or tag's history, or the history of one file or folder on it. use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; use super::{PAGE, Repo}; use crate::html::{ago, icon, subject_or_placeholder}; use crate::repository::Head; use crate::urls::{Q, clean_path, lossy, param}; use crate::{View, templates}; pub(crate) fn view_log(repo: &Repo, q: &Q) -> Option { let reference = repo.reference(q); let number: usize = param(q, "page").and_then(|p| p.parse().ok()).unwrap_or(0); let skip = number.checked_mul(PAGE)?; let path = clean_path(q.get("path").map_or(&[][..], Vec::as_slice))?; let commit = repo.reader.resolve(&reference)?; let (count, skip) = (format!("-n{}", PAGE + 1), format!("--skip={skip}")); // The path reaches git as bytes after `--`, and git's pathspecs are literal here, so a // name like `star*.txt` means only that file. let mut args: Vec<&OsStr> = [OsStr::new("log"), count.as_ref(), skip.as_ref(), "--date=format:%-d %b %Y".as_ref(), Head::FORMAT.as_ref()] .to_vec(); args.extend([OsStr::new(&commit), OsStr::new("--")]); if !path.is_empty() { args.push(OsStr::from_bytes(&path)); } let out = repo.reader.git(&args)?; let lines: Vec<&str> = out.lines().collect(); let rows = commit_rows(repo, &out, PAGE); let page_url = |n: usize| repo.url_at("log", &reference, &path, &format!("page={n}")); let default = repo.reader.default_ref(); let compare = (path.is_empty() && repo.reader.resolve(&default).is_some_and(|d| d != commit)).then(|| { let shown = default.strip_prefix(b"refs/heads/").unwrap_or(&default); (repo.compare_url(&default, &reference), format!("Compare with {}", lossy(shown))) }); let title = if path.is_empty() { format!("Commits on {}", lossy(&reference)) } else { format!("History of {} on {}", lossy(&path), lossy(&reference)) }; Some(View::Html( templates::History { title, signed_in: repo.signed_in, masthead: repo.masthead("log", &reference, Some(&commit)), locator: repo.locator("log", &reference, &path), rows, compare, newer: number.checked_sub(1).map(page_url), older: (lines.len() > PAGE).then(|| page_url(number + 1)), tag_icon: icon("tag"), } .to_string(), )) } /// Rows for up to `limit` commits of `git log` output in [`Head::FORMAT`], each labelled with /// the tags that point at it. pub(crate) fn commit_rows<'a>(repo: &'a Repo, out: &str, limit: usize) -> Vec> { let tags = repo.reader.tags_by_commit(); let mut rows = Vec::new(); for head in out.lines().take(limit).filter_map(Head::parse) { let labels = tags .get(&head.oid) .into_iter() .flatten() .map(|name| templates::TagLink { name: lossy(name).into_owned().into(), url: repo.url_at("tree", &repo.reader.tag_ref(name), b"", ""), }) .collect(); rows.push(templates::CommitRow { url: repo.url("commit", &[("id", &head.oid)]), subject: subject_or_placeholder(&head.subject).to_owned(), short_id: head.oid.chars().take(7).collect(), author: head.author, when: ago(&head.stamp, &head.date), tags: labels, }); } rows }