Skip to main content

drep/diff/
uncommitted.rs

1//! What a staged-mode linter would read that the commit does not hold.
2
3use std::path::{Path, PathBuf};
4
5use super::git::{GitEnv, spawn_git_bytes, spawn_git_bytes_with_input};
6use super::prefix::{from_prefix, paths_from};
7use super::quoting::path_from_bytes;
8use super::{GitError, StagedView};
9
10/// What the working tree does not hold of the committing index, each path
11/// named from the directory drep runs in.
12#[derive(Debug, Default, PartialEq, Eq)]
13pub struct Uncommitted {
14    /// Files whose working-tree state the committing index does not hold:
15    /// tracked files whose working-tree content differs from that index,
16    /// untracked files git does not ignore, and flagged files edited behind
17    /// their flags. This is what a linter reading the working tree would see
18    /// that the commit does not hold.
19    pub differing: Vec<PathBuf>,
20    /// Files marked `skip-worktree` and absent from the working tree, as a
21    /// sparse checkout leaves them. No tool can read one, so none refuses a
22    /// tool that reads beyond its files; but one can be the configuration
23    /// the commit gives a file whose working tree shows none.
24    pub index_only: Vec<PathBuf>,
25}
26
27/// The committing index's paths the working tree does not hold.
28///
29/// The deterministic layer's tools read the working tree, so after a partial
30/// `git add -p`, an edit made after staging, or a source file never added at
31/// all they would lint content the commit does not contain - and a
32/// lint-failing staged blob could pass. This listing is what lets the caller
33/// refuse those runs. The index read is the committing one
34/// (`StagedView::env`), so a hook's alternate `GIT_INDEX_FILE` is honoured
35/// the same way it is for the staged diff itself.
36///
37/// The diff argv is deliberately not `DIFF`: that set carries hunk-only pins
38/// and `--diff-filter=ACMRT`, which would drop the deletions this query
39/// exists to see (a staged file deleted from the working tree differs
40/// exactly as an edited one does). What stays is the subset a name-only
41/// listing needs: naming every file from the top level and ignoring external
42/// diff drivers and textconv. Both listings are NUL-separated and read as raw
43/// bytes, never through `spawn_git`, which trims what it reads, so each name
44/// is taken byte for byte. The untracked listing takes `-- :/` for the same reason the diff takes `--no-relative`:
45/// bare `ls-files` limits its answer to the working directory, so an
46/// untracked helper above it would go unlisted.
47pub async fn uncommitted_paths(root: &Path) -> Result<Uncommitted, GitError> {
48    let view = StagedView::of(root).await?;
49    // `-z`: a name is listed unquoted, with a leading space or a newline of
50    // its own intact.
51    let (tracked, untracked, listing) = tokio::join!(
52        spawn_git_bytes(
53            root,
54            &[
55                // A read under a hook must not write: porcelain diff otherwise
56                // rewrites the index it compared to refresh its stat data, and
57                // that index can be the commit's own lock file.
58                "--no-optional-locks",
59                "diff",
60                "--name-only",
61                "-z",
62                // A dirty submodule a staged file builds against differs even
63                // where configuration says to ignore submodules.
64                "--ignore-submodules=none",
65                "--no-relative",
66                "--no-ext-diff",
67                "--no-textconv",
68            ],
69            view.env(),
70        ),
71        spawn_git_bytes(
72            root,
73            &[
74                "--no-optional-locks",
75                "ls-files",
76                "-z",
77                "--others",
78                "--exclude-standard",
79                "--full-name",
80                "--",
81                ":/",
82            ],
83            view.env(),
84        ),
85        spawn_git_bytes(
86            root,
87            &[
88                "--no-optional-locks",
89                "ls-files",
90                "-z",
91                "--stage",
92                "-v",
93                "--full-name",
94                "--",
95                ":/",
96            ],
97            view.env(),
98        )
99    );
100    let (tracked, untracked) = (tracked?, untracked?);
101    let mut paths = paths_from(
102        listed(&tracked).chain(listed(&untracked)).collect(),
103        &view.prefix,
104    );
105    let listing = listing?;
106    let (flagged, filtered) = tokio::join!(
107        flagged_differences(root, &view, &listing),
108        filtered_differences(root, &view, &listing)
109    );
110    let flagged = flagged?;
111    paths.extend(filtered?);
112    paths.extend(flagged.differing);
113    paths.sort();
114    paths.dedup();
115    Ok(Uncommitted {
116        differing: paths,
117        index_only: flagged.index_only,
118    })
119}
120
121/// Tracked files whose index entries tell git not to compare them
122/// (`assume-unchanged`, `skip-worktree`) and whose working-tree state differs
123/// from the committing index all the same. `git diff` trusts the flags and
124/// reports neither, so each flagged regular file on disk is hashed and
125/// compared with its index blob, a flagged link's text is compared with its
126/// blob, a flagged submodule's checkout with its recorded commit
127/// (`submodule_differs`), and anything else in its place differs. One missing from the working
128/// tree differs when assumed unchanged; a `skip-worktree` one missing is a
129/// sparse checkout's ordinary state and is index-only.
130async fn flagged_differences(
131    root: &Path,
132    view: &StagedView,
133    listing: &[u8],
134) -> Result<Uncommitted, GitError> {
135    const LINK: &[u8] = b"120000";
136    const GITLINK: &[u8] = b"160000";
137    let mut flagged = Uncommitted::default();
138    let mut files: Vec<(PathBuf, &[u8])> = Vec::new();
139    for entry in index_entries(listing).filter(IndexEntry::flagged) {
140        let named = from_prefix(&path_from_bytes(entry.name.to_vec()), &view.prefix);
141        if entry.mode == GITLINK {
142            if submodule_differs(&root.join(&named), entry.blob).await? {
143                flagged.differing.push(named);
144            }
145            continue;
146        }
147        match std::fs::symlink_metadata(root.join(&named)) {
148            Ok(meta) if meta.is_file() && entry.mode != LINK => files.push((named, entry.blob)),
149            Ok(meta) if meta.file_type().is_symlink() && entry.mode == LINK => {
150                // A link's blob is its target text.
151                let oid = String::from_utf8_lossy(entry.blob);
152                let text = spawn_git_bytes(root, &["cat-file", "blob", &oid], view.env()).await?;
153                if !std::fs::read_link(root.join(&named))
154                    .is_ok_and(|target| target == path_from_bytes(text))
155                {
156                    flagged.differing.push(named);
157                }
158            }
159            // Anything else in a flagged file's place is not the blob the
160            // index holds, and hash-object would refuse a directory.
161            Ok(_) => flagged.differing.push(named),
162            Err(_) if entry.skip_worktree() => flagged.index_only.push(named),
163            Err(_) => flagged.differing.push(named),
164        }
165    }
166    // A long list is hashed a batch at a time, within any system's argument limit.
167    for batch in files.chunks(256) {
168        let mut args: Vec<std::ffi::OsString> = vec!["hash-object".into(), "--".into()];
169        args.extend(
170            batch
171                .iter()
172                .map(|(named, _)| root.join(named).into_os_string()),
173        );
174        let hashes = spawn_git_bytes(root, &args, view.env()).await?;
175        let mut hashes = hashes.split(|&byte| byte == b'\n');
176        for (named, blob) in batch {
177            if hashes.next() != Some(*blob) {
178                flagged.differing.push(named.clone());
179            }
180        }
181    }
182    Ok(flagged)
183}
184
185/// Whether a flagged submodule's checkout differs from the commit its index
186/// entry records: another commit checked out, changes of its own, or
187/// something other than a directory in its place. A submodule not checked
188/// out, or never initialized, holds nothing a tool can read.
189async fn submodule_differs(path: &Path, commit: &[u8]) -> Result<bool, GitError> {
190    match std::fs::symlink_metadata(path) {
191        Err(_) => return Ok(false),
192        Ok(meta) if !meta.is_dir() => return Ok(true),
193        Ok(_) => {}
194    }
195    if !path.join(".git").exists() {
196        return Ok(false);
197    }
198    let head = spawn_git_bytes(path, &["rev-parse", "HEAD"], GitEnv::Scrubbed).await?;
199    if head.trim_ascii() != commit {
200        return Ok(true);
201    }
202    let changes = spawn_git_bytes(
203        path,
204        &[
205            "--no-optional-locks",
206            "status",
207            "--porcelain",
208            "-z",
209            "--ignore-submodules=none",
210        ],
211        GitEnv::Scrubbed,
212    )
213    .await?;
214    Ok(!changes.is_empty())
215}
216
217/// Tracked regular files a clean filter (a `filter` attribute) rewrites whose
218/// working-tree bytes differ from what checking out their index blob writes.
219/// `git diff` compares the filtered content and reports none of them, while a
220/// linter reads the file as it is: a filter that strips a suppression comment
221/// commits a file the linter never saw. Git LFS is left alone: it round-trips
222/// losslessly and is content-addressed, so an LFS file `git diff` reports
223/// unchanged already holds its blob's content, and smudging every asset again
224/// would stream them all on each commit.
225async fn filtered_differences(
226    root: &Path,
227    view: &StagedView,
228    listing: &[u8],
229) -> Result<Vec<PathBuf>, GitError> {
230    let regular: Vec<IndexEntry<'_>> = index_entries(listing)
231        .filter(|entry| entry.mode == b"100644" || entry.mode == b"100755")
232        .collect();
233    if regular.is_empty() {
234        return Ok(Vec::new());
235    }
236    let named: Vec<PathBuf> = regular
237        .iter()
238        .map(|entry| from_prefix(&path_from_bytes(entry.name.to_vec()), &view.prefix))
239        .collect();
240    let mut input = Vec::new();
241    for path in &named {
242        input.extend_from_slice(path.as_os_str().as_encoded_bytes());
243        input.push(0);
244    }
245    let attributes = spawn_git_bytes_with_input(
246        root,
247        &["check-attr", "--stdin", "-z", "filter"],
248        view.env(),
249        &input,
250    )
251    .await?;
252    // `path NUL filter NUL value NUL`, one record per path in the order given.
253    let values = attributes.split(|&byte| byte == 0).skip(2).step_by(3);
254    let mut differing = Vec::new();
255    for ((entry, path), value) in regular.iter().zip(named).zip(values) {
256        if matches!(value, b"unspecified" | b"unset" | b"set" | b"lfs") {
257            continue;
258        }
259        // A file absent from the working tree is `git diff`'s to report.
260        let Ok(raw) = std::fs::read(root.join(&path)) else {
261            continue;
262        };
263        let mut at = std::ffi::OsString::from("--path=");
264        at.push(&path);
265        let oid = std::ffi::OsString::from(String::from_utf8_lossy(entry.blob).as_ref());
266        let checkout = spawn_git_bytes(
267            root,
268            &[
269                std::ffi::OsString::from("cat-file"),
270                "--filters".into(),
271                at,
272                oid,
273            ],
274            view.env(),
275        )
276        .await;
277        if checkout.is_ok_and(|content| content == raw) {
278            continue;
279        }
280        differing.push(path);
281    }
282    Ok(differing)
283}
284
285/// One `ls-files --stage -v` record.
286struct IndexEntry<'a> {
287    /// A lowercase tag is assumed unchanged, and `S` or `s` is skip-worktree.
288    tag: u8,
289    mode: &'a [u8],
290    blob: &'a [u8],
291    name: &'a [u8],
292}
293
294impl IndexEntry<'_> {
295    /// Whether git is told not to compare this entry with the working tree.
296    fn flagged(&self) -> bool {
297        self.tag.is_ascii_lowercase() || self.tag == b'S'
298    }
299
300    fn skip_worktree(&self) -> bool {
301        self.tag.eq_ignore_ascii_case(&b'S')
302    }
303}
304
305/// The records of an `ls-files --stage -v -z` listing.
306fn index_entries(listing: &[u8]) -> impl Iterator<Item = IndexEntry<'_>> {
307    listing.split(|&byte| byte == 0).filter_map(|record| {
308        let tab = record.iter().position(|&byte| byte == b'\t')?;
309        let mut fields = record[..tab].split(|&byte| byte == b' ');
310        Some(IndexEntry {
311            tag: *fields.next()?.first()?,
312            mode: fields.next()?,
313            blob: fields.next()?,
314            name: &record[tab + 1..],
315        })
316    })
317}
318
319/// The names in a NUL-separated git listing.
320fn listed(output: &[u8]) -> impl Iterator<Item = PathBuf> + '_ {
321    output
322        .split(|&byte| byte == 0)
323        .filter(|name| !name.is_empty())
324        .map(|name| path_from_bytes(name.to_vec()))
325}