1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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};
const COMPARE_COMMITS: usize = 250;
pub(crate) fn view_compare(repo: &Repo, q: &Q) -> Option<View> {
let given = |key| q.get(key).filter(|v| !v.is_empty()).cloned();
let from = given("from");
let to = given("to").or_else(|| from.as_ref().map(|_| repo.reader.default_ref()));
let mut seen = std::collections::HashSet::new();
let refs: Vec<String> = 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));
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()?);
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#"<p class="note">These changes couldn’t be read.</p>"#.to_string(), |d| d.to_string());
page.diff = Some(diff);
Some(View::Html(page.to_string()))
}