forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

churn.rs85 lines, 3.1 KiBHistoryRawDownload
1//! Lines changed per commit, for the history strip on the repo page and the CI page: the newest
2//! commits reachable from a commit, each counted against its first parent. One `git log` run with
3//! `--shortstat`, so the output is a few bytes per commit however big the commits are.
4
5use std::path::Path;
6use std::time::Duration;
7
8use crate::command::GitEnv;
9use crate::error::{Error, Result};
10use crate::history::{git, read_capped, validate_oid};
11
12/// Most commits one call returns.
13pub const MAX_COMMITS: usize = 48;
14/// Longest a single walk may take. A huge commit is one diff for git to count, so this bounds the
15/// page's wait; the child is killed when the timeout drops the read.
16const WALK_TIMEOUT: Duration = Duration::from_secs(5);
17/// Record start in the `log` format, so a commit's header can be told from its stat line.
18const REC: u8 = 0x1e;
19
20/// One commit and the lines it changed: additions plus deletions against its first parent.
21/// A root commit counts against the empty tree. Binary files add nothing to the count.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct CommitChurn {
24 pub oid: String,
25 pub lines: u64,
26}
27
28/// The newest `limit` commits reachable from `head` (a full object id), newest first, capped at
29/// [`MAX_COMMITS`]. Merge commits count their diff against the first parent only.
30pub async fn recent_churn(
31 env: &GitEnv,
32 repo: &Path,
33 head: &str,
34 limit: usize,
35) -> Result<Vec<CommitChurn>> {
36 validate_oid(head)?;
37 let limit = limit.clamp(1, MAX_COMMITS);
38 let cmd = git(env, "log")
39 .git_dir(repo)
40 .arg("--no-color")
41 .arg("--no-ext-diff")
42 .arg("--no-textconv")
43 .arg("--no-renames")
44 .arg("--root")
45 .arg("--shortstat")
46 .arg("--diff-merges=first-parent")
47 .arg("--format=%x1e%H")
48 .user_opt("--max-count", &limit.to_string())?
49 .user_revs(&[head])?;
50 let read = match tokio::time::timeout(WALK_TIMEOUT, read_capped(cmd)).await {
51 Ok(read) => read?,
52 Err(_) => return Err(Error::Failed("commit walk timed out".into())),
53 };
54 Ok(parse(&read.0))
55}
56
57/// Splits `log` output into records. Each record is one commit id, then its stat line when the
58/// commit changed anything.
59fn parse(buf: &[u8]) -> Vec<CommitChurn> {
60 buf.split(|&b| b == REC)
61 .filter_map(|rec| {
62 let text = String::from_utf8_lossy(rec);
63 let mut lines = text.lines();
64 let oid = lines.next()?.trim();
65 validate_oid(oid).ok()?;
66 let lines_changed = lines.map(stat_line_total).sum();
67 Some(CommitChurn {
68 oid: oid.to_owned(),
69 lines: lines_changed,
70 })
71 })
72 .collect()
73}
74
75/// The insertions plus deletions in one `--shortstat` line, such as
76/// " 3 files changed, 10 insertions(+), 2 deletions(-)". Zero for any other line.
77fn stat_line_total(line: &str) -> u64 {
78 if !line.contains("changed") {
79 return 0;
80 }
81 line.split(',')
82 .filter(|part| part.contains("insertion") || part.contains("deletion"))
83 .filter_map(|part| part.split_whitespace().next()?.parse::<u64>().ok())
84 .sum()
85}