//! Two revisions compared: the commits one has that the other lacks, and what changed. use super::Repo; use super::commit::render_diff_between; use super::log::commit_rows; use crate::cache::cached; use crate::html::{icon, plural}; use crate::repository::Head; use crate::urls::{Q, lossy}; use crate::{View, templates}; /// The most commits a comparison lists; the diff still covers all of them. const COMPARE_COMMITS: usize = 250; pub(crate) fn view_compare(repo: &Repo, q: &Q) -> Option { let given = |key| q.get(key).filter(|v| !v.is_empty()).cloned(); let from = given("from"); // Only `from` is needed: comparing against the default branch is the common case. let to = given("to").or_else(|| from.as_ref().map(|_| repo.reader.default_ref())); // A branch and a tag may share a name; suggest it once. let mut seen = std::collections::HashSet::new(); let refs: Vec = repo .reader .heads() .iter() .chain(repo.reader.tags()) .map(|name| lossy(name).into_owned()) .filter(|name| seen.insert(name.clone())) .collect(); let mut page = templates::Compare { title: "Compare revisions".into(), signed_in: repo.signed_in, masthead: String::new(), action: repo.url("compare", &[]), from: from.as_deref().map(lossy).unwrap_or_default().into_owned(), to: to.as_deref().map(lossy).unwrap_or_default().into_owned(), refs, message: None, summary: None, swap: None, rows: Vec::new(), more: None, diff: None, tag_icon: icon("tag"), }; let (Some(from), Some(to)) = (from, to) else { page.masthead = repo.masthead("log", &repo.reader.default_ref(), None); return Some(View::Html(page.to_string())); }; let resolved = (repo.reader.resolve(&from), repo.reader.resolve(&to)); let (Some(old), Some(new)) = resolved else { let missing = if resolved.0.is_none() { &from } else { &to }; page.masthead = repo.masthead("log", &repo.reader.default_ref(), None); page.message = Some(format!("There’s no branch, tag or commit named {}.", lossy(missing))); return Some(View::Html(page.to_string())); }; let (from_name, to_name) = (page.from.clone(), page.to.clone()); page.title = format!("Comparing {from_name}...{to_name}"); page.masthead = repo.masthead("log", &to, Some(&new)); page.swap = Some(repo.compare_url(&to, &from)); // Both ids are commits, so these results never change and are cached by them. let span = format!("{old}...{new}"); let counts = cached(repo.key("ahead", &span, ""), || { repo.reader.git(&["rev-list", "--left-right", "--count", &span, "--"]).map(|out| out.trim().to_string()) })?; let (behind, ahead) = counts.split_once('\t')?; let (behind, ahead): (usize, usize) = (behind.parse().ok()?, ahead.parse().ok()?); // Unrelated histories have no merge base; their trees are then compared directly. let base = cached(repo.key("base", &span, ""), || repo.reader.git(&["merge-base", &old, &new]).map(|o| o.trim().into())); let commits = plural(ahead, "commit", "commits"); page.summary = Some(match (&base, ahead, behind) { _ if old == new => format!("{from_name} and {to_name} are the same commit."), (None, ..) => format!( "{to_name} and {from_name} share no history. The {commits} below are all of {to_name}’s, \ and the changes compare the two directly." ), (_, 0, _) => format!("{to_name} has no commits that {from_name} lacks."), (_, _, 0) => format!("{to_name} is {commits} ahead of {from_name}."), (_, _, behind) => format!( "{to_name} is {commits} ahead of {from_name} and {} behind it. The changes are {to_name}’s since \ the two diverged.", plural(behind, "commit", "commits") ), }); if ahead == 0 { return Some(View::Html(page.to_string())); } let range = format!("{old}..{new}"); let count = format!("-n{COMPARE_COMMITS}"); let log = repo.reader.git(&["log", &count, "--date=format:%-d %b %Y", Head::FORMAT, &range, "--"])?; page.rows = commit_rows(repo, &log, COMPARE_COMMITS); page.more = (ahead > COMPARE_COMMITS).then(|| format!("Showing the newest {COMPARE_COMMITS} of {ahead} commits.")); let start = base.as_deref().map_or(old.as_str(), |b| b); let diff = cached(repo.key("compare", &format!("{start}..{new}"), ""), || render_diff_between(repo, start, &new)) .map_or_else(|| r#"

These changes couldn’t be read.

"#.to_string(), |d| d.to_string()); page.diff = Some(diff); Some(View::Html(page.to_string())) }