Skip to main content

greengate_runtime/hooks/
large_files.rs

1//! The file that makes every future clone slower — caught while it is
2//! still one `git rm --cached` away.
3//!
4//! Git history never forgets a megabyte: an accidentally committed
5//! dataset, bundle, or core dump is paid for by every clone, forever,
6//! even after it is deleted — deletion adds a commit, it does not remove
7//! the bytes. And the hosting side has hard opinions: GitHub warns at
8//! 50 MB and refuses files over 100 MB outright, so the block threshold
9//! here is not policy invented by a hook, it is the push failing later
10//! with a worse error.
11//!
12//! Two thresholds, both in megabytes and both configurable:
13//!
14//! - `greengate.largeFileWarn` (default 10) — named, never blocking: a large
15//!   asset can be deliberate, and the warning is the moment to decide;
16//! - `greengate.largeFileBlock` (default 100, GitHub's refusal line) — blocks,
17//!   with the remedy named (git-lfs, or keep it out of history).
18//!
19//! Under the staged-only hold the working tree IS the commit's content,
20//! so file sizes on disk are the sizes being committed.
21
22use crate::check::{Outcome, Severity};
23use crate::finding::Finding;
24
25use super::common;
26
27const MB: u64 = 1024 * 1024;
28
29/// The check's own short name, and the `check` field of every finding it makes.
30pub const NAME: &str = "large-files";
31
32/// The two thresholds, in bytes, as configured here.
33pub fn thresholds(settings: &crate::config::Settings) -> (u64, u64) {
34    let warn =
35        crate::config::integer_or(settings, "greengate.largeFileWarn", 10, 1..=1_000_000) as u64;
36    let block =
37        crate::config::integer_or(settings, "greengate.largeFileBlock", 100, 1..=1_000_000) as u64;
38    (warn * MB, block * MB)
39}
40
41/// A finding for one file's SIZE, or none if it is within both thresholds.
42///
43/// Deliberately without a position: the problem is the file, not a place in
44/// it. `Finding::line` being `None` is how that is said, and every renderer
45/// degrades to naming the file — which is all this check could ever say.
46pub fn scan(settings: &crate::config::Settings, file: &str, len: u64) -> Option<Finding> {
47    let (warn_bytes, block_bytes) = thresholds(settings);
48    let mb = len / MB;
49    if len >= block_bytes {
50        Some(Finding::new(
51            NAME,
52            crate::ui::sanitize(file),
53            Severity::Block,
54            format!(
55                "{mb} MB — over the {} MB limit (GitHub refuses these at push). \
56                 Use git-lfs, or keep it out of history: deletion later does \
57                 not remove the bytes",
58                block_bytes / MB
59            ),
60        ))
61    } else if len >= warn_bytes {
62        Some(Finding::new(
63            NAME,
64            crate::ui::sanitize(file),
65            Severity::Warn,
66            format!(
67                "{mb} MB — every future clone pays for this forever \
68                 (git config greengate.largeFileWarn to tune)"
69            ),
70        ))
71    } else {
72        None
73    }
74}
75
76pub fn staged(settings: &crate::config::Settings) -> Outcome {
77    let root = common::repo_root();
78    let mut blocked = false;
79    let mut warned = false;
80    for f in common::staged_files(&[]) {
81        let path = std::path::Path::new(&root).join(&f);
82        let Ok(meta) = std::fs::metadata(&path) else {
83            continue; // deleted or unreadable: nothing this size to commit
84        };
85        if !meta.is_file() {
86            continue;
87        }
88        let Some(finding) = scan(settings, &f, meta.len()) else {
89            continue;
90        };
91        let line = format!("large-files: {} is {}", finding.file, finding.message);
92        match finding.severity {
93            Severity::Block => {
94                blocked = true;
95                common::fail(&line);
96            }
97            Severity::Warn => {
98                warned = true;
99                common::warn(&line);
100            }
101        }
102    }
103    if blocked {
104        return Outcome::Failed;
105    }
106    if warned {
107        return Outcome::Warned;
108    }
109    common::ok(settings, "No oversized files staged");
110    Outcome::Passed
111}