Skip to main content

grit_lib/diff/
core.rs

1//! Diff machinery — compare trees, index entries, and working tree files.
2//!
3//! # Overview
4//!
5//! This module provides the core diffing infrastructure shared by `diff`,
6//! `diff-index`, `status`, `log`, `show`, `commit`, and `merge`.
7//!
8//! ## Levels of comparison
9//!
10//! 1. **Tree-to-tree** — compare two tree objects (e.g. for `log`/`show`).
11//! 2. **Tree-to-index** — compare a tree (usually HEAD) against the index
12//!    (staged changes, used by `diff --cached` and `status`).
13//! 3. **Index-to-worktree** — compare index against the working directory
14//!    (unstaged changes, used by `diff` and `status`).
15//!
16//! ## Content diff
17//!
18//! Line-level diffing uses the `similar` crate (Myers, patience, minimal) and,
19//! for Git's `histogram` algorithm, grit's own histogram engine for output compatible with
20//! upstream Git.
21//! Output formats: unified patch, raw (`:old-mode new-mode ...`), stat,
22//! numstat.
23
24use crate::error::ObjectError;
25use crate::error::RefError;
26use crate::git_path::CanonicalizeExt;
27use crate::worktree::WorktreeError;
28use std::borrow::Cow;
29use std::fs;
30#[cfg(unix)]
31use std::os::unix::fs::MetadataExt;
32use std::path::{Path, PathBuf};
33
34use crate::config::ConfigSet;
35use crate::diff_indent_heuristic;
36use crate::diff_rename_parallel::{
37    diff_rename_parallel_options, inexact_matrix_exceeds_limit, parallel_load_optional_blobs,
38    parallel_map_indices, parse_rename_limit,
39};
40use crate::error::{Error, Result, SubmoduleError};
41use crate::index::{CacheTreeNode, Index, IndexEntry, MODE_SYMLINK, MODE_TREE};
42use crate::index::{IndexCorrupt, IndexCorruptReason, IndexError};
43use crate::objects::{
44    parse_commit, CommitData, FileMode, ObjectId, ObjectKind, TreeEntry, TreeEntryRef, TreeRefIter,
45};
46use crate::odb::{Odb, SessionTree};
47use crate::parallel::ThreadPolicy;
48use crate::repo::Repository;
49use crate::sparse_checkout::SparseCheckout;
50use crate::userdiff::FuncnameMatcher;
51
52use super::rename_lines::RenameLineTable;
53use super::unified_emit::{
54    emit_unified_body_from_edits, line_ending_only_edits, EditSpan, LineSide, UnifiedEmitOptions,
55};
56
57/// Comparison keys for every line of `side`.
58///
59/// Without whitespace normalization a key is the raw line including its `\n` (the same bytes as
60/// `line_diff_token`), borrowed from the side's records. Under `-w`/`-b`/`--ignore-space-at-eol`
61/// the keys are normalized line bodies, kept in `normalized` so the returned slices can borrow
62/// them.
63fn line_compare_keys<'a>(
64    side: &'a LineSide<'a>,
65    ws: super::WhitespaceMode,
66    normalized: &'a mut Vec<Cow<'a, str>>,
67) -> Cow<'a, [&'a str]> {
68    if !ws.normalizes_line_compare() {
69        return Cow::Borrowed(side.records());
70    }
71    // Git -w/-b compare normalized bodies only; EOF newline is whitespace.
72    normalized.extend(
73        side.lines()
74            .map(|line| ws.normalize_line_for_compare(line.body)),
75    );
76    Cow::Owned(normalized.iter().map(Cow::as_ref).collect())
77}
78
79#[path = "../structured_diff.rs"]
80pub mod structured;
81
82pub(crate) fn imara_unified_hunk_slices(body: &str) -> Vec<&str> {
83    let mut starts: Vec<usize> = Vec::new();
84    if body.starts_with("@@") {
85        starts.push(0);
86    }
87    for (idx, _) in body.match_indices("\n@@ ") {
88        starts.push(idx + 1);
89    }
90    starts.push(body.len());
91    starts.windows(2).map(|w| &body[w[0]..w[1]]).collect()
92}
93
94/// Histogram line alignment (Git `xhistogram.c`), then `xdl_change_compact` and indent heuristic.
95pub(crate) fn histogram_compacted_line_ops(
96    old_content: &str,
97    new_content: &str,
98    indent_heuristic: bool,
99    ws: super::WhitespaceMode,
100) -> Vec<similar::DiffOp> {
101    let before = LineSide::from_text(old_content);
102    let after = LineSide::from_text(new_content);
103    let old_lines: Vec<&str> = before.lines().map(|l| l.body).collect();
104    let new_lines: Vec<&str> = after.lines().map(|l| l.body).collect();
105    let old_len = old_lines.len();
106    let new_len = new_lines.len();
107    let (mut old_normalized, mut new_normalized) = (Vec::new(), Vec::new());
108    let old_compare = line_compare_keys(&before, ws, &mut old_normalized);
109    let new_compare = line_compare_keys(&after, ws, &mut new_normalized);
110
111    let (changed1, changed2) = git_xdiff::histogram_changed_flags(&old_compare, &new_compare);
112
113    if !changed1.iter().any(|c| *c) && !changed2.iter().any(|c| *c) {
114        return Vec::new();
115    }
116    let raw = changed_flags_to_ops(&changed1, &changed2, old_len, new_len);
117    diff_indent_heuristic::apply_change_compact_to_ops_with_ws_histogram(
118        &raw,
119        diff_indent_heuristic::CompactSide {
120            lines: &old_lines,
121            keys: &old_compare,
122            compare_ids: None,
123        },
124        diff_indent_heuristic::CompactSide {
125            lines: &new_lines,
126            keys: &new_compare,
127            compare_ids: None,
128        },
129        indent_heuristic,
130        ws,
131        true,
132    )
133}
134
135pub(crate) fn histogram_unified_body_raw(
136    old_content: &str,
137    new_content: &str,
138    context_lines: usize,
139    inter_hunk_context: usize,
140    indent_heuristic: bool,
141    ws: super::WhitespaceMode,
142) -> String {
143    let before_side = LineSide::from_text(old_content);
144    let after_side = LineSide::from_text(new_content);
145    let ops = histogram_compacted_line_ops(old_content, new_content, indent_heuristic, ws);
146    let mut edits = diff_ops_to_edit_spans(&ops);
147    if edits.is_empty() {
148        if old_content != new_content && !ws.normalizes_line_compare() {
149            edits = line_ending_only_edits(&before_side, &after_side);
150        }
151        if edits.is_empty() {
152            return String::new();
153        }
154    }
155    emit_unified_body_from_edits(
156        &before_side,
157        &after_side,
158        &edits,
159        UnifiedEmitOptions {
160            context_lines,
161            inter_hunk_context,
162            function_context: false,
163        },
164        None,
165    )
166}
167
168/// Build the unified-diff body (no `---`/`+++` header) using Git's histogram
169/// algorithm, but applying `--ignore-blank-lines` / `-I` semantics through a
170/// Git-compatible implementation of the xdiff change-record machinery
171/// (`xdl_mark_ignorable_lines` + `xdl_get_hunk` + `xdl_emit_diff`).
172///
173/// `is_ignorable_change` is called with the slices of removed and added lines
174/// for one change record (each item is the line text without its trailing
175/// newline); it returns `true` when the whole change is ignorable (Git's
176/// `xch->ignore`). Ignorable changes that are far from substantive changes are
177/// dropped, and hunk boundaries are computed with Git's `max_ignorable`
178/// distance rules so the line numbers and surviving context match Git exactly.
179///
180/// Returns the body only; an empty string means no substantive change survives.
181/// Optional `--function-context` (`-W`) hunk expansion is applied *after*
182/// ignorable change-record pruning so the boundaries/funcname header match Git.
183#[allow(clippy::too_many_arguments)]
184fn diff_ops_to_ignorable_edit_spans<F>(
185    ops: &[similar::DiffOp],
186    old_lines: &[&str],
187    new_lines: &[&str],
188    is_ignorable_change: F,
189) -> Vec<EditSpan>
190where
191    F: Fn(&[&str], &[&str]) -> bool,
192{
193    use similar::DiffOp;
194    let mut out = Vec::new();
195    for op in ops {
196        match *op {
197            DiffOp::Equal { .. } => {}
198            DiffOp::Delete {
199                old_index,
200                old_len,
201                new_index,
202            } => {
203                let removed = &old_lines[old_index..old_index + old_len];
204                out.push(EditSpan {
205                    before_start: old_index as i64,
206                    before_len: old_len as i64,
207                    after_start: new_index as i64,
208                    after_len: 0,
209                    skip_in_output: is_ignorable_change(removed, &[]),
210                });
211            }
212            DiffOp::Insert {
213                old_index,
214                new_index,
215                new_len,
216            } => {
217                let added = &new_lines[new_index..new_index + new_len];
218                out.push(EditSpan {
219                    before_start: old_index as i64,
220                    before_len: 0,
221                    after_start: new_index as i64,
222                    after_len: new_len as i64,
223                    skip_in_output: is_ignorable_change(&[], added),
224                });
225            }
226            DiffOp::Replace {
227                old_index,
228                old_len,
229                new_index,
230                new_len,
231            } => {
232                let removed = &old_lines[old_index..old_index + old_len];
233                let added = &new_lines[new_index..new_index + new_len];
234                out.push(EditSpan {
235                    before_start: old_index as i64,
236                    before_len: old_len as i64,
237                    after_start: new_index as i64,
238                    after_len: new_len as i64,
239                    skip_in_output: is_ignorable_change(removed, added),
240                });
241            }
242        }
243    }
244    out
245}
246
247#[allow(clippy::too_many_arguments)]
248pub(crate) fn histogram_unified_body_ignore_fc<F>(
249    old_content: &str,
250    new_content: &str,
251    context_lines: usize,
252    inter_hunk_context: usize,
253    function_context: bool,
254    funcname_matcher: Option<&FuncnameMatcher>,
255    indent_heuristic: bool,
256    ws: super::WhitespaceMode,
257    is_ignorable_change: F,
258) -> String
259where
260    F: Fn(&[&str], &[&str]) -> bool,
261{
262    let before = LineSide::from_text(old_content);
263    let after = LineSide::from_text(new_content);
264    let old_lines: Vec<&str> = before.lines().map(|l| l.body).collect();
265    let new_lines: Vec<&str> = after.lines().map(|l| l.body).collect();
266    let ops = histogram_compacted_line_ops(old_content, new_content, indent_heuristic, ws);
267    if ops.is_empty() {
268        return String::new();
269    }
270    let edits = diff_ops_to_ignorable_edit_spans(&ops, &old_lines, &new_lines, is_ignorable_change);
271    emit_unified_body_from_edits(
272        &before,
273        &after,
274        &edits,
275        UnifiedEmitOptions {
276            context_lines,
277            inter_hunk_context,
278            function_context,
279        },
280        funcname_matcher,
281    )
282}
283
284/// Line bodies of `side`, as the change compaction reads them.
285fn line_bodies<'a>(side: &LineSide<'a>) -> Vec<&'a str> {
286    side.lines().map(|l| l.body).collect()
287}
288
289/// Count insertions and deletions from compacted line diff ops (patch/stat alignment).
290#[must_use]
291fn count_diff_ops(ops: &[similar::DiffOp]) -> (usize, usize) {
292    use similar::DiffOp;
293    let mut insertions = 0usize;
294    let mut deletions = 0usize;
295    for op in ops {
296        match *op {
297            DiffOp::Equal { .. } => {}
298            DiffOp::Delete { old_len, .. } => deletions += old_len,
299            DiffOp::Insert { new_len, .. } => insertions += new_len,
300            DiffOp::Replace {
301                old_len, new_len, ..
302            } => {
303                deletions += old_len;
304                insertions += new_len;
305            }
306        }
307    }
308    (insertions, deletions)
309}
310
311/// Line alignment ops shared by unified patches, `--numstat`, and `-G`.
312pub(crate) fn compacted_line_ops_for_options(
313    old_content: &str,
314    new_content: &str,
315    options: &crate::diff::options::DiffOptions,
316) -> Vec<similar::DiffOp> {
317    use crate::diff::options::DiffAlgorithm;
318
319    let ws = options.whitespace;
320    match options.algorithm {
321        DiffAlgorithm::Histogram => {
322            histogram_compacted_line_ops(old_content, new_content, options.indent_heuristic, ws)
323        }
324        DiffAlgorithm::Patience => patience_compacted_line_ops(
325            &LineSide::from_text(old_content),
326            &LineSide::from_text(new_content),
327            options.indent_heuristic,
328            ws,
329            &[],
330        ),
331        DiffAlgorithm::Myers | DiffAlgorithm::Minimal => {
332            let before = LineSide::from_text(old_content);
333            let after = LineSide::from_text(new_content);
334            if before.is_empty() && after.is_empty() {
335                return Vec::new();
336            }
337            let (mut old_normalized, mut new_normalized) = (Vec::new(), Vec::new());
338            let old_compare = line_compare_keys(&before, ws, &mut old_normalized);
339            let new_compare = line_compare_keys(&after, ws, &mut new_normalized);
340            git_xdiff_compacted_line_ops(
341                &old_compare,
342                &new_compare,
343                &before,
344                &after,
345                ws,
346                options.indent_heuristic,
347                options.algorithm == DiffAlgorithm::Minimal,
348            )
349        }
350    }
351}
352
353/// Count changed lines using the same alignment as structured patches and `--numstat`.
354#[must_use]
355pub(crate) fn count_line_changes_with_options(
356    old_content: &str,
357    new_content: &str,
358    options: &crate::diff::options::DiffOptions,
359) -> (usize, usize) {
360    let ws = options.whitespace;
361    let ops = compacted_line_ops_for_options(old_content, new_content, options);
362    let (mut insertions, mut deletions) = count_diff_ops(&ops);
363    if insertions == 0
364        && deletions == 0
365        && old_content != new_content
366        && !ws.normalizes_line_compare()
367    {
368        let before = LineSide::from_text(old_content);
369        let after = LineSide::from_text(new_content);
370        for edit in line_ending_only_edits(&before, &after) {
371            insertions += edit.after_len as usize;
372            deletions += edit.before_len as usize;
373        }
374    }
375    (insertions, deletions)
376}
377
378/// Myers changed flags (via [`git_xdiff::changed_flags_with_shared`]) plus interned compare keys.
379type XdiffTrimmedIds = ((Vec<bool>, Vec<bool>), Vec<u32>, Vec<u32>);
380
381fn xdiff_changed_flags_trimmed_ids(
382    old_compare: &[&str],
383    new_compare: &[&str],
384    need_min: bool,
385) -> XdiffTrimmedIds {
386    use crate::line_interner::LineInterner;
387
388    let nrec1 = old_compare.len();
389    let nrec2 = new_compare.len();
390    if nrec1 == 0 && nrec2 == 0 {
391        return ((Vec::new(), Vec::new()), Vec::new(), Vec::new());
392    }
393    let mut interner = LineInterner::with_capacity(nrec1 + nrec2);
394    let ids1_full = interner.intern_all(old_compare);
395    let ids2_full = interner.intern_all(new_compare);
396    let nclasses = interner.len();
397    // Only the ids are needed from here on; freeing the table before the search keeps it out of
398    // the diff's peak memory.
399    drop(interner);
400
401    let lim = nrec1.min(nrec2);
402    let mut head = 0usize;
403    while head < lim && ids1_full[head] == ids2_full[head] {
404        head += 1;
405    }
406    let mut tail = 0usize;
407    let rem = lim - head;
408    while tail < rem && ids1_full[nrec1 - 1 - tail] == ids2_full[nrec2 - 1 - tail] {
409        tail += 1;
410    }
411    let shared_records = head + tail;
412    let (full1, full2) = if shared_records == 0 {
413        git_xdiff::changed_flags(&ids1_full, &ids2_full, need_min)
414    } else {
415        let cut_ids1 = &ids1_full[head..nrec1 - tail];
416        let cut_ids2 = &ids2_full[head..nrec2 - tail];
417        let mut shared_counts = vec![0u32; nclasses];
418        for &id in &ids1_full[..head] {
419            shared_counts[id as usize] += 1;
420        }
421        for &id in &ids1_full[nrec1 - tail..] {
422            shared_counts[id as usize] += 1;
423        }
424        let (mid1, mid2) = git_xdiff::changed_flags_with_shared(
425            cut_ids1,
426            cut_ids2,
427            &shared_counts,
428            shared_records,
429            need_min,
430        );
431        let mut full1 = vec![false; nrec1];
432        let mut full2 = vec![false; nrec2];
433        if !mid1.is_empty() {
434            full1[head..nrec1 - tail].copy_from_slice(&mid1);
435        }
436        if !mid2.is_empty() {
437            full2[head..nrec2 - tail].copy_from_slice(&mid2);
438        }
439        (full1, full2)
440    };
441    ((full1, full2), ids1_full, ids2_full)
442}
443
444fn xdiff_changed_flags_trimmed_preinterned(
445    ids1_full: &[u32],
446    ids2_full: &[u32],
447    need_min: bool,
448) -> (Vec<bool>, Vec<bool>) {
449    let nrec1 = ids1_full.len();
450    let nrec2 = ids2_full.len();
451    if nrec1 == 0 && nrec2 == 0 {
452        return (Vec::new(), Vec::new());
453    }
454
455    let lim = nrec1.min(nrec2);
456    let mut head = 0usize;
457    while head < lim && ids1_full[head] == ids2_full[head] {
458        head += 1;
459    }
460    let mut tail = 0usize;
461    let rem = lim - head;
462    while tail < rem && ids1_full[nrec1 - 1 - tail] == ids2_full[nrec2 - 1 - tail] {
463        tail += 1;
464    }
465    let shared_records = head + tail;
466    if shared_records == 0 {
467        return git_xdiff::changed_flags(ids1_full, ids2_full, need_min);
468    }
469    let cut_ids1 = &ids1_full[head..nrec1 - tail];
470    let cut_ids2 = &ids2_full[head..nrec2 - tail];
471    let nclasses = ids1_full
472        .iter()
473        .chain(ids2_full)
474        .max()
475        .map_or(0, |&max| max as usize + 1);
476    let mut shared_counts = vec![0u32; nclasses];
477    for &id in &ids1_full[..head] {
478        shared_counts[id as usize] += 1;
479    }
480    for &id in &ids1_full[nrec1 - tail..] {
481        shared_counts[id as usize] += 1;
482    }
483    let (mid1, mid2) = git_xdiff::changed_flags_with_shared(
484        cut_ids1,
485        cut_ids2,
486        &shared_counts,
487        shared_records,
488        need_min,
489    );
490    let mut full1 = vec![false; nrec1];
491    let mut full2 = vec![false; nrec2];
492    if !mid1.is_empty() {
493        full1[head..nrec1 - tail].copy_from_slice(&mid1);
494    }
495    if !mid2.is_empty() {
496        full2[head..nrec2 - tail].copy_from_slice(&mid2);
497    }
498    (full1, full2)
499}
500
501fn xdiff_changed_flags_trimmed_byte_ids(
502    old_compare: &[Vec<u8>],
503    new_compare: &[Vec<u8>],
504    need_min: bool,
505) -> XdiffTrimmedIds {
506    use crate::line_interner::ByteLineInterner;
507
508    let nrec1 = old_compare.len();
509    let nrec2 = new_compare.len();
510    if nrec1 == 0 && nrec2 == 0 {
511        return ((Vec::new(), Vec::new()), Vec::new(), Vec::new());
512    }
513    let mut interner = ByteLineInterner::with_capacity(nrec1 + nrec2);
514    let ids1_full = interner.intern_all_vecs(old_compare);
515    let ids2_full = interner.intern_all_vecs(new_compare);
516    let (full1, full2) = xdiff_changed_flags_trimmed_preinterned(&ids1_full, &ids2_full, need_min);
517    ((full1, full2), ids1_full, ids2_full)
518}
519
520/// Intern compare keys for base and both merge sides in one pass (shared token ids).
521pub(crate) fn intern_merge_compare_three_way(
522    base: &[Vec<u8>],
523    ours: &[Vec<u8>],
524    theirs: &[Vec<u8>],
525) -> (Vec<u32>, Vec<u32>, Vec<u32>) {
526    use crate::line_interner::ByteLineInterner;
527
528    let mut interner = ByteLineInterner::with_capacity(base.len() + ours.len() + theirs.len());
529    (
530        interner.intern_all_vecs(base),
531        interner.intern_all_vecs(ours),
532        interner.intern_all_vecs(theirs),
533    )
534}
535
536pub(crate) fn merge_myops_from_preinterned_ids(
537    old_ids: &[u32],
538    new_ids: &[u32],
539    need_min: bool,
540) -> Vec<similar::DiffOp> {
541    const INDENT_HEURISTIC: bool = false;
542    let (changed1, changed2) = xdiff_changed_flags_trimmed_preinterned(old_ids, new_ids, need_min);
543    let old_lines = merge_dummy_line_refs(old_ids.len());
544    let new_lines = merge_dummy_line_refs(new_ids.len());
545    diff_indent_heuristic::apply_change_compact_to_masks(
546        changed1,
547        changed2,
548        diff_indent_heuristic::CompactSide {
549            lines: &old_lines,
550            keys: &old_lines,
551            compare_ids: Some(old_ids),
552        },
553        diff_indent_heuristic::CompactSide {
554            lines: &new_lines,
555            keys: &new_lines,
556            compare_ids: Some(new_ids),
557        },
558        INDENT_HEURISTIC,
559    )
560}
561
562fn merge_dummy_line_refs(len: usize) -> Vec<&'static str> {
563    vec![""; len]
564}
565
566enum MergeFileCompactAlgorithm {
567    Myers { need_min: bool },
568    Histogram,
569    Patience,
570}
571
572fn parse_merge_file_compact_algorithm(name: Option<&str>) -> MergeFileCompactAlgorithm {
573    match name.map(str::to_lowercase).as_deref() {
574        None | Some("myers") => MergeFileCompactAlgorithm::Myers { need_min: false },
575        Some("minimal") => MergeFileCompactAlgorithm::Myers { need_min: true },
576        Some("histogram") => MergeFileCompactAlgorithm::Histogram,
577        Some("patience") => MergeFileCompactAlgorithm::Patience,
578        Some(_) => MergeFileCompactAlgorithm::Myers { need_min: false },
579    }
580}
581
582/// Compacted line alignment for three-way merge (base↔side diffs and conflict refinement).
583///
584/// Compare keys must already follow merge `xdl_recmatch` rules. Neither merge-file nor merge-ort
585/// enables the indent heuristic; change compaction still slides groups against the other side.
586pub(crate) fn merge_file_compacted_line_ops(
587    old_compare: &[Vec<u8>],
588    new_compare: &[Vec<u8>],
589    diff_algorithm: Option<&str>,
590) -> Vec<similar::DiffOp> {
591    use crate::diff::git_histogram::histogram_changed_flags_bytes;
592    use crate::line_interner::ByteLineInterner;
593
594    const INDENT_HEURISTIC: bool = false;
595    let algo = parse_merge_file_compact_algorithm(diff_algorithm);
596    match algo {
597        MergeFileCompactAlgorithm::Myers { need_min } => {
598            let ((_, _), old_key_ids, new_key_ids) =
599                xdiff_changed_flags_trimmed_byte_ids(old_compare, new_compare, need_min);
600            merge_myops_from_preinterned_ids(&old_key_ids, &new_key_ids, need_min)
601        }
602        MergeFileCompactAlgorithm::Histogram | MergeFileCompactAlgorithm::Patience => {
603            let old_refs: Vec<&[u8]> = old_compare.iter().map(|line| line.as_slice()).collect();
604            let new_refs: Vec<&[u8]> = new_compare.iter().map(|line| line.as_slice()).collect();
605            let mut interner =
606                ByteLineInterner::with_capacity(old_compare.len() + new_compare.len());
607            let old_key_ids = interner.intern_all_vecs(old_compare);
608            let new_key_ids = interner.intern_all_vecs(new_compare);
609            let histogram = matches!(algo, MergeFileCompactAlgorithm::Histogram);
610            let (changed1, changed2) = if histogram {
611                histogram_changed_flags_bytes(&old_refs, &new_refs, |c1, c2, o0, o1, n0, n1| {
612                    git_xdiff::merge_myers_region_bytes(
613                        &old_refs, &new_refs, c1, c2, o0, o1, n0, n1,
614                    );
615                })
616            } else {
617                git_xdiff::patience_changed_flags_bytes(&old_refs, &new_refs)
618            };
619            let raw =
620                changed_flags_to_ops(&changed1, &changed2, old_compare.len(), new_compare.len());
621            let old_lines = merge_dummy_line_refs(old_compare.len());
622            let new_lines = merge_dummy_line_refs(new_compare.len());
623            diff_indent_heuristic::apply_change_compact_to_ops_with_ws_histogram(
624                &raw,
625                diff_indent_heuristic::CompactSide {
626                    lines: &old_lines,
627                    keys: &old_lines,
628                    compare_ids: Some(&old_key_ids),
629                },
630                diff_indent_heuristic::CompactSide {
631                    lines: &new_lines,
632                    keys: &new_lines,
633                    compare_ids: Some(&new_key_ids),
634                },
635                INDENT_HEURISTIC,
636                super::WhitespaceMode::default(),
637                histogram,
638            )
639        }
640    }
641}
642
643/// Myers (or minimal) line alignment with Git's change compaction and indent heuristic.
644///
645/// `old_compare` / `new_compare` must be [`line_compare_keys`] of `before` / `after` under `ws`.
646pub(crate) fn git_xdiff_compacted_line_ops(
647    old_compare: &[&str],
648    new_compare: &[&str],
649    before: &LineSide<'_>,
650    after: &LineSide<'_>,
651    ws: super::WhitespaceMode,
652    indent_heuristic: bool,
653    need_min: bool,
654) -> Vec<similar::DiffOp> {
655    let ((changed1, changed2), old_key_ids, new_key_ids) =
656        xdiff_changed_flags_trimmed_ids(old_compare, new_compare, need_min);
657    // Compaction reads line bodies only to measure indentation. A raw key is the body plus its
658    // newline, which that measure skips as trailing whitespace, so only normalized keys need
659    // the bodies collected separately.
660    let bodies;
661    let (old_lines, new_lines): (&[&str], &[&str]) = if ws.normalizes_line_compare() {
662        bodies = (line_bodies(before), line_bodies(after));
663        (&bodies.0, &bodies.1)
664    } else {
665        (old_compare, new_compare)
666    };
667    diff_indent_heuristic::apply_change_compact_to_masks(
668        changed1,
669        changed2,
670        diff_indent_heuristic::CompactSide {
671            lines: old_lines,
672            keys: old_compare,
673            compare_ids: Some(&old_key_ids),
674        },
675        diff_indent_heuristic::CompactSide {
676            lines: new_lines,
677            keys: new_compare,
678            compare_ids: Some(&new_key_ids),
679        },
680        indent_heuristic,
681    )
682}
683
684fn diff_ops_to_edit_spans(ops: &[similar::DiffOp]) -> Vec<EditSpan> {
685    use similar::DiffOp;
686    let mut out = Vec::new();
687    for op in ops {
688        match *op {
689            DiffOp::Equal { .. } => {}
690            DiffOp::Delete {
691                old_index,
692                old_len,
693                new_index,
694            } => out.push(EditSpan {
695                before_start: old_index as i64,
696                before_len: old_len as i64,
697                after_start: new_index as i64,
698                after_len: 0,
699                skip_in_output: false,
700            }),
701            DiffOp::Insert {
702                old_index,
703                new_index,
704                new_len,
705            } => out.push(EditSpan {
706                before_start: old_index as i64,
707                before_len: 0,
708                after_start: new_index as i64,
709                after_len: new_len as i64,
710                skip_in_output: false,
711            }),
712            DiffOp::Replace {
713                old_index,
714                old_len,
715                new_index,
716                new_len,
717            } => out.push(EditSpan {
718                before_start: old_index as i64,
719                before_len: old_len as i64,
720                after_start: new_index as i64,
721                after_len: new_len as i64,
722                skip_in_output: false,
723            }),
724        }
725    }
726    out
727}
728
729/// Unified hunk body using Git's xdiff engine (no `---` / `+++`).
730fn git_xdiff_unified_body_raw(
731    old_content: &str,
732    new_content: &str,
733    context_lines: usize,
734    inter_hunk_context: usize,
735    indent_heuristic: bool,
736    need_min: bool,
737    ws: super::WhitespaceMode,
738) -> String {
739    git_xdiff_unified_body_raw_with_ignorable(
740        old_content,
741        new_content,
742        context_lines,
743        inter_hunk_context,
744        indent_heuristic,
745        need_min,
746        ws,
747        None::<fn(&[&str], &[&str]) -> bool>,
748    )
749}
750
751#[allow(clippy::too_many_arguments)]
752fn git_xdiff_unified_body_raw_with_ignorable<F>(
753    old_content: &str,
754    new_content: &str,
755    context_lines: usize,
756    inter_hunk_context: usize,
757    indent_heuristic: bool,
758    need_min: bool,
759    ws: super::WhitespaceMode,
760    is_ignorable_change: Option<F>,
761) -> String
762where
763    F: Fn(&[&str], &[&str]) -> bool,
764{
765    let before = LineSide::from_text(old_content);
766    let after = LineSide::from_text(new_content);
767    if before.is_empty() && after.is_empty() {
768        return String::new();
769    }
770    let (mut old_normalized, mut new_normalized) = (Vec::new(), Vec::new());
771    let old_compare = line_compare_keys(&before, ws, &mut old_normalized);
772    let new_compare = line_compare_keys(&after, ws, &mut new_normalized);
773    let ops = git_xdiff_compacted_line_ops(
774        &old_compare,
775        &new_compare,
776        &before,
777        &after,
778        ws,
779        indent_heuristic,
780        need_min,
781    );
782    let mut edits = if let Some(is_ignorable) = is_ignorable_change {
783        diff_ops_to_ignorable_edit_spans(
784            &ops,
785            &line_bodies(&before),
786            &line_bodies(&after),
787            is_ignorable,
788        )
789    } else {
790        diff_ops_to_edit_spans(&ops)
791    };
792    if edits.is_empty() {
793        if old_content != new_content && !ws.normalizes_line_compare() {
794            edits = line_ending_only_edits(&before, &after);
795        }
796        if edits.is_empty() {
797            return String::new();
798        }
799    }
800    emit_unified_body_from_edits(
801        &before,
802        &after,
803        &edits,
804        UnifiedEmitOptions {
805            context_lines,
806            inter_hunk_context,
807            function_context: false,
808        },
809        None,
810    )
811}
812
813#[allow(clippy::too_many_arguments)]
814fn git_patience_unified_body_raw_with_ignorable<F>(
815    old_content: &str,
816    new_content: &str,
817    context_lines: usize,
818    inter_hunk_context: usize,
819    indent_heuristic: bool,
820    ws: super::WhitespaceMode,
821    is_ignorable_change: Option<F>,
822) -> String
823where
824    F: Fn(&[&str], &[&str]) -> bool,
825{
826    let before = LineSide::from_text(old_content);
827    let after = LineSide::from_text(new_content);
828    let ops = patience_compacted_line_ops(&before, &after, indent_heuristic, ws, &[]);
829    unified_body_from_ops(
830        &before,
831        &after,
832        &ops,
833        ws,
834        UnifiedEmitOptions {
835            context_lines,
836            inter_hunk_context,
837            function_context: false,
838        },
839        None,
840        is_ignorable_change,
841    )
842}
843
844/// Patience line alignment (Git `xpatience.c`), then change compaction and the indent heuristic.
845///
846/// `new_anchored[i]` marks new-side line `i` for `--anchored`; pass `&[]` for plain patience.
847pub(crate) fn patience_compacted_line_ops(
848    before: &LineSide<'_>,
849    after: &LineSide<'_>,
850    indent_heuristic: bool,
851    ws: super::WhitespaceMode,
852    new_anchored: &[bool],
853) -> Vec<similar::DiffOp> {
854    if before.is_empty() && after.is_empty() {
855        return Vec::new();
856    }
857    let old_refs = line_bodies(before);
858    let new_refs = line_bodies(after);
859    let (mut old_normalized, mut new_normalized) = (Vec::new(), Vec::new());
860    let old_compare = line_compare_keys(before, ws, &mut old_normalized);
861    let new_compare = line_compare_keys(after, ws, &mut new_normalized);
862    let (changed1, changed2) =
863        git_xdiff::patience_changed_flags(&old_compare, &new_compare, new_anchored);
864    let raw = changed_flags_to_ops(&changed1, &changed2, old_refs.len(), new_refs.len());
865    diff_indent_heuristic::apply_change_compact_to_ops_with_ws_histogram(
866        &raw,
867        diff_indent_heuristic::CompactSide {
868            lines: &old_refs,
869            keys: &old_compare,
870            compare_ids: None,
871        },
872        diff_indent_heuristic::CompactSide {
873            lines: &new_refs,
874            keys: &new_compare,
875            compare_ids: None,
876        },
877        indent_heuristic,
878        ws,
879        false,
880    )
881}
882
883/// Unified hunk body (`@@` … with no-newline markers) for compacted line `ops`.
884///
885/// `is_ignorable_change` marks whole change records as ignorable (`--ignore-blank-lines`); hunks
886/// made only of ignorable records are dropped. When `ops` is empty but the texts still differ in
887/// line endings only, those lines are emitted as replacements, as Git does.
888pub(crate) fn unified_body_from_ops<F>(
889    before: &LineSide<'_>,
890    after: &LineSide<'_>,
891    ops: &[similar::DiffOp],
892    ws: super::WhitespaceMode,
893    emit: UnifiedEmitOptions,
894    funcname_matcher: Option<&FuncnameMatcher>,
895    is_ignorable_change: Option<F>,
896) -> String
897where
898    F: Fn(&[&str], &[&str]) -> bool,
899{
900    let mut edits = match is_ignorable_change {
901        Some(is_ignorable) => diff_ops_to_ignorable_edit_spans(
902            ops,
903            &line_bodies(before),
904            &line_bodies(after),
905            is_ignorable,
906        ),
907        None => diff_ops_to_edit_spans(ops),
908    };
909    if edits.is_empty() && !ws.normalizes_line_compare() && before.records() != after.records() {
910        edits = line_ending_only_edits(before, after);
911    }
912    if edits.is_empty() {
913        return String::new();
914    }
915    emit_unified_body_from_edits(before, after, &edits, emit, funcname_matcher)
916}
917
918pub(crate) fn blank_line_ignorable(
919    ws: super::WhitespaceMode,
920) -> impl Fn(&[&str], &[&str]) -> bool + Copy {
921    move |removed: &[&str], added: &[&str]| {
922        ws.ignore_blank_lines
923            && removed.iter().all(|l| crate::ws::ws_blank_line(l))
924            && added.iter().all(|l| crate::ws::ws_blank_line(l))
925    }
926}
927
928/// Unified hunk bodies (`@@` …) for [`DiffOptions`], shared by structured and log-patch output.
929pub(crate) fn unified_hunk_body_for_diff_options(
930    old_content: &str,
931    new_content: &str,
932    options: &crate::diff::options::DiffOptions,
933) -> String {
934    use crate::diff::options::DiffAlgorithm;
935
936    let ws = options.whitespace;
937    if ws.is_active() {
938        let blank_only = blank_line_ignorable(ws);
939        if ws.normalizes_line_compare() {
940            let ignorable = ws.ignore_blank_lines.then_some(blank_only);
941            return match options.algorithm {
942                DiffAlgorithm::Myers => git_xdiff_unified_body_raw_with_ignorable(
943                    old_content,
944                    new_content,
945                    options.context_lines,
946                    options.inter_hunk_context,
947                    options.indent_heuristic,
948                    false,
949                    ws,
950                    ignorable,
951                ),
952                DiffAlgorithm::Minimal => git_xdiff_unified_body_raw_with_ignorable(
953                    old_content,
954                    new_content,
955                    options.context_lines,
956                    options.inter_hunk_context,
957                    options.indent_heuristic,
958                    true,
959                    ws,
960                    ignorable,
961                ),
962                DiffAlgorithm::Patience => git_patience_unified_body_raw_with_ignorable(
963                    old_content,
964                    new_content,
965                    options.context_lines,
966                    options.inter_hunk_context,
967                    options.indent_heuristic,
968                    ws,
969                    ignorable,
970                ),
971                DiffAlgorithm::Histogram => {
972                    if ws.ignore_blank_lines {
973                        histogram_unified_body_ignore_fc(
974                            old_content,
975                            new_content,
976                            options.context_lines,
977                            options.inter_hunk_context,
978                            false,
979                            None,
980                            options.indent_heuristic,
981                            ws,
982                            blank_only,
983                        )
984                    } else {
985                        histogram_unified_body_raw(
986                            old_content,
987                            new_content,
988                            options.context_lines,
989                            options.inter_hunk_context,
990                            options.indent_heuristic,
991                            ws,
992                        )
993                    }
994                }
995            };
996        }
997        if ws.ignore_blank_lines {
998            return match options.algorithm {
999                DiffAlgorithm::Myers => git_xdiff_unified_body_raw_with_ignorable(
1000                    old_content,
1001                    new_content,
1002                    options.context_lines,
1003                    options.inter_hunk_context,
1004                    options.indent_heuristic,
1005                    false,
1006                    ws,
1007                    Some(blank_only),
1008                ),
1009                DiffAlgorithm::Minimal => git_xdiff_unified_body_raw_with_ignorable(
1010                    old_content,
1011                    new_content,
1012                    options.context_lines,
1013                    options.inter_hunk_context,
1014                    options.indent_heuristic,
1015                    true,
1016                    ws,
1017                    Some(blank_only),
1018                ),
1019                DiffAlgorithm::Patience => git_patience_unified_body_raw_with_ignorable(
1020                    old_content,
1021                    new_content,
1022                    options.context_lines,
1023                    options.inter_hunk_context,
1024                    options.indent_heuristic,
1025                    ws,
1026                    Some(blank_only),
1027                ),
1028                DiffAlgorithm::Histogram => histogram_unified_body_ignore_fc(
1029                    old_content,
1030                    new_content,
1031                    options.context_lines,
1032                    options.inter_hunk_context,
1033                    false,
1034                    None,
1035                    options.indent_heuristic,
1036                    ws,
1037                    blank_only,
1038                ),
1039            };
1040        }
1041    }
1042    match options.algorithm {
1043        DiffAlgorithm::Myers => git_xdiff_unified_body_raw(
1044            old_content,
1045            new_content,
1046            options.context_lines,
1047            options.inter_hunk_context,
1048            options.indent_heuristic,
1049            false,
1050            ws,
1051        ),
1052        DiffAlgorithm::Minimal => git_xdiff_unified_body_raw(
1053            old_content,
1054            new_content,
1055            options.context_lines,
1056            options.inter_hunk_context,
1057            options.indent_heuristic,
1058            true,
1059            ws,
1060        ),
1061        DiffAlgorithm::Patience => git_patience_unified_body_raw(
1062            old_content,
1063            new_content,
1064            options.context_lines,
1065            options.inter_hunk_context,
1066            options.indent_heuristic,
1067        ),
1068        DiffAlgorithm::Histogram => histogram_unified_body_raw(
1069            old_content,
1070            new_content,
1071            options.context_lines,
1072            options.inter_hunk_context,
1073            options.indent_heuristic,
1074            ws,
1075        ),
1076    }
1077}
1078
1079/// Unified hunk body using Git's patience diff (no `---` / `+++`).
1080fn git_patience_unified_body_raw(
1081    old_content: &str,
1082    new_content: &str,
1083    context_lines: usize,
1084    inter_hunk_context: usize,
1085    indent_heuristic: bool,
1086) -> String {
1087    git_patience_unified_body_raw_with_ignorable(
1088        old_content,
1089        new_content,
1090        context_lines,
1091        inter_hunk_context,
1092        indent_heuristic,
1093        super::WhitespaceMode::default(),
1094        None::<fn(&[&str], &[&str]) -> bool>,
1095    )
1096}
1097
1098/// Parse `@@ -old +new @@` into 1-based starts and line counts (Git display form).
1099pub(crate) fn unified_hunk_header_counts(
1100    header_line: &str,
1101) -> Option<(usize, usize, usize, usize)> {
1102    let rest = header_line.strip_prefix("@@ ")?;
1103    let (old_part, rest2) = rest.split_once(" +")?;
1104    let old_tok = old_part.strip_prefix('-')?.split(' ').next()?;
1105    let new_tok = rest2.split(" @@").next()?.split(' ').next()?;
1106
1107    fn parse_side(spec: &str) -> (usize, usize) {
1108        if let Some((start, count)) = spec.split_once(',') {
1109            (start.parse().unwrap_or(0), count.parse().unwrap_or(0))
1110        } else {
1111            (spec.parse().unwrap_or(0), 1)
1112        }
1113    }
1114
1115    let (os, ol) = parse_side(old_tok);
1116    let (ns, nl) = parse_side(new_tok);
1117    Some((os, ol, ns, nl))
1118}
1119
1120/// Write `---` / `+++` and unified hunks using [`unified_hunk_body_for_diff_options`].
1121#[allow(clippy::too_many_arguments)]
1122pub(crate) fn write_unified_diff_from_diff_options<W: std::io::Write>(
1123    out: &mut W,
1124    old_content: &str,
1125    new_content: &str,
1126    old_path: &str,
1127    new_path: &str,
1128    src_prefix: &str,
1129    dst_prefix: &str,
1130    funcname_matcher: Option<&FuncnameMatcher>,
1131    options: &crate::diff::options::DiffOptions,
1132    quote_path_fully: bool,
1133) -> Result<()> {
1134    use crate::quote_path::format_diff_path_with_prefix;
1135
1136    if old_path == "/dev/null" {
1137        out.write_all(b"--- /dev/null\n")?;
1138    } else if src_prefix.is_empty() {
1139        writeln!(out, "--- {old_path}")?;
1140    } else {
1141        out.write_all(b"--- ")?;
1142        out.write_all(
1143            format_diff_path_with_prefix(src_prefix, old_path, quote_path_fully).as_bytes(),
1144        )?;
1145        out.write_all(b"\n")?;
1146    }
1147    if new_path == "/dev/null" {
1148        out.write_all(b"+++ /dev/null\n")?;
1149    } else if dst_prefix.is_empty() {
1150        writeln!(out, "+++ {new_path}")?;
1151    } else {
1152        out.write_all(b"+++ ")?;
1153        out.write_all(
1154            format_diff_path_with_prefix(dst_prefix, new_path, quote_path_fully).as_bytes(),
1155        )?;
1156        out.write_all(b"\n")?;
1157    }
1158
1159    let body = unified_hunk_body_for_diff_options(old_content, new_content, options);
1160    if body.is_empty() {
1161        return Ok(());
1162    }
1163    let old_lines: Vec<&str> = old_content.lines().collect();
1164    for hunk_str in imara_unified_hunk_slices(&body) {
1165        if hunk_str.is_empty() {
1166            continue;
1167        }
1168        if let Some(first_newline) = hunk_str.find('\n') {
1169            let header_line = &hunk_str[..first_newline];
1170            let rest = &hunk_str[first_newline..];
1171            if let Some(func_ctx) =
1172                extract_function_context(header_line, &old_lines, funcname_matcher, Some(old_path))
1173            {
1174                out.write_all(header_line.as_bytes())?;
1175                out.write_all(b" ")?;
1176                out.write_all(func_ctx.as_bytes())?;
1177                out.write_all(rest.as_bytes())?;
1178            } else {
1179                out.write_all(hunk_str.as_bytes())?;
1180            }
1181        } else {
1182            out.write_all(hunk_str.as_bytes())?;
1183        }
1184    }
1185    Ok(())
1186}
1187
1188/// `diff.indentHeuristic` from config (Git defaults to true when unset).
1189#[must_use]
1190pub(crate) fn indent_heuristic_from_config(config: &ConfigSet) -> bool {
1191    config.bool_with_default("diff.indentHeuristic", true)
1192}
1193
1194/// Resolve indent heuristic: `--no-indent-heuristic` and `--indent-heuristic` override config.
1195#[must_use]
1196pub(crate) fn resolve_indent_heuristic(
1197    config: &ConfigSet,
1198    cli_indent_heuristic: bool,
1199    cli_no_indent_heuristic: bool,
1200) -> bool {
1201    if cli_no_indent_heuristic {
1202        false
1203    } else if cli_indent_heuristic {
1204        true
1205    } else {
1206        indent_heuristic_from_config(config)
1207    }
1208}
1209
1210// ---------------------------------------------------------------------------
1211// Git-compatible implementation of the xdiff Myers engine,
1212// restricted to what the word-diff path needs (no rename/ignore-whitespace
1213// flags). Git's word diff runs the *default* xdiff Myers over the per-word
1214// token streams; matching Git's exact change-record selection requires
1215// reproducing its preprocessing (record classification + `xdl_cleanup_records`
1216// + `xdl_trim_ends`) and its divide-and-conquer split, because the histogram
1217// engine (and `similar`) pick different — but equally minimal — alignments for inputs
1218// with many repeated tokens (e.g. the `{`/`}`/`,` in the `bibtex` driver).
1219mod git_xdiff {
1220    use std::cell::RefCell;
1221
1222    const XDL_MAX_COST_MIN: i64 = 256;
1223    const XDL_HEUR_MIN_COST: i64 = 256;
1224    const XDL_SNAKE_CNT: i64 = 20;
1225    const XDL_K_HEUR: i64 = 4;
1226    const XDL_LINE_MAX: i64 = i64::MAX;
1227    const XDL_KPDIS_RUN: i64 = 4;
1228    const XDL_MAX_EQLIMIT: i64 = 1024;
1229    const XDL_SIMSCAN_WINDOW: i64 = 100;
1230
1231    // record-classification actions (xprepare.c)
1232    const DISCARD: u8 = 0;
1233    const KEEP: u8 = 1;
1234    const INVESTIGATE: u8 = 2;
1235
1236    /// Classical integer square root approximation using shifts (`xdl_bogosqrt`).
1237    fn bogosqrt(mut n: i64) -> i64 {
1238        let mut i: i64 = 1;
1239        while n > 0 {
1240            i <<= 1;
1241            n >>= 2;
1242        }
1243        i
1244    }
1245
1246    struct XdAlgoEnv {
1247        mxcost: i64,
1248        snake_cnt: i64,
1249        heur_min: i64,
1250    }
1251
1252    struct XdpSplit {
1253        i1: i64,
1254        i2: i64,
1255        min_lo: bool,
1256        min_hi: bool,
1257    }
1258
1259    /// A single side of the diff: token ids plus the `changed` flags and the
1260    /// `reference_index` mapping from effective record -> original record.
1261    struct XdFile {
1262        changed: Vec<u8>,
1263        reference_index: Vec<usize>,
1264        /// Interned token id per effective record (parallel to `reference_index`).
1265        effective_ids: Vec<u32>,
1266        dstart: i64,
1267        dend: i64,
1268        nreff: i64,
1269    }
1270
1271    #[inline]
1272    fn effective_id(xdf: &XdFile, idx: i64) -> u32 {
1273        xdf.effective_ids[idx as usize]
1274    }
1275
1276    struct XdiffScratch {
1277        count1: Vec<u32>,
1278        count2: Vec<u32>,
1279        action1: Vec<u8>,
1280        action2: Vec<u8>,
1281        kvd: Vec<i64>,
1282    }
1283
1284    impl XdiffScratch {
1285        fn prepare_counts(&mut self, nclasses: usize) {
1286            if self.count1.len() < nclasses {
1287                self.count1.resize(nclasses, 0);
1288                self.count2.resize(nclasses, 0);
1289            }
1290            self.count1[..nclasses].fill(0);
1291            self.count2[..nclasses].fill(0);
1292        }
1293
1294        fn prepare_actions(&mut self, nrec1: usize, nrec2: usize) {
1295            let a1 = nrec1 + 1;
1296            let a2 = nrec2 + 1;
1297            if self.action1.len() < a1 {
1298                self.action1.resize(a1, 0);
1299            } else {
1300                self.action1[..a1].fill(0);
1301            }
1302            if self.action2.len() < a2 {
1303                self.action2.resize(a2, 0);
1304            } else {
1305                self.action2[..a2].fill(0);
1306            }
1307        }
1308
1309        fn prepare_kvd(&mut self, kvd_len: usize) {
1310            // The search reads only diagonals it has written, so the contents never need
1311            // clearing. A fresh zeroed allocation leaves the pages a small diff never reaches
1312            // untouched, where growing in place would write every one of them.
1313            if self.kvd.len() < kvd_len {
1314                self.kvd = vec![0; kvd_len];
1315            }
1316        }
1317    }
1318
1319    thread_local! { // hygiene: per-thread xdiff Myers scratch (class counts, K-vectors) reused across files
1320        static XDIFF_SCRATCH: RefCell<XdiffScratch> = const {
1321            RefCell::new(XdiffScratch {
1322                count1: Vec::new(),
1323                count2: Vec::new(),
1324                action1: Vec::new(),
1325                action2: Vec::new(),
1326                kvd: Vec::new(),
1327            })
1328        };
1329    }
1330
1331    fn bool_changed(changed: Vec<u8>) -> Vec<bool> {
1332        changed.into_iter().map(|b| b != 0).collect()
1333    }
1334
1335    /// Faithful port of `xdl_split` (the middle-snake finder). `kvdf`/`kvdb` are
1336    /// the forward/backward k-vectors, offset so index 0 maps to diagonal 0.
1337    #[allow(clippy::too_many_arguments)]
1338    fn xdl_split(
1339        xdf1: &XdFile,
1340        off1: i64,
1341        lim1: i64,
1342        xdf2: &XdFile,
1343        off2: i64,
1344        lim2: i64,
1345        kvdf: &mut [i64],
1346        kvdb: &mut [i64],
1347        koff: i64,
1348        need_min: bool,
1349        spl: &mut XdpSplit,
1350        xenv: &XdAlgoEnv,
1351    ) -> i64 {
1352        let dmin = off1 - lim2;
1353        let dmax = lim1 - off2;
1354        let fmid = off1 - off2;
1355        let bmid = lim1 - lim2;
1356        let odd = ((fmid - bmid) & 1) != 0;
1357        let mut fmin = fmid;
1358        let mut fmax = fmid;
1359        let mut bmin = bmid;
1360        let mut bmax = bmid;
1361
1362        let kf = |k: i64| -> usize { (k + koff) as usize };
1363
1364        kvdf[kf(fmid)] = off1;
1365        kvdb[kf(bmid)] = lim1;
1366
1367        let mut ec: i64 = 1;
1368        loop {
1369            let mut got_snake = false;
1370
1371            if fmin > dmin {
1372                fmin -= 1;
1373                kvdf[kf(fmin - 1)] = -1;
1374            } else {
1375                fmin += 1;
1376            }
1377            if fmax < dmax {
1378                fmax += 1;
1379                kvdf[kf(fmax + 1)] = -1;
1380            } else {
1381                fmax -= 1;
1382            }
1383
1384            let mut d = fmax;
1385            while d >= fmin {
1386                let mut i1 = if kvdf[kf(d - 1)] >= kvdf[kf(d + 1)] {
1387                    kvdf[kf(d - 1)] + 1
1388                } else {
1389                    kvdf[kf(d + 1)]
1390                };
1391                let prev1 = i1;
1392                let mut i2 = i1 - d;
1393                while i1 < lim1 && i2 < lim2 && effective_id(xdf1, i1) == effective_id(xdf2, i2) {
1394                    i1 += 1;
1395                    i2 += 1;
1396                }
1397                if i1 - prev1 > xenv.snake_cnt {
1398                    got_snake = true;
1399                }
1400                kvdf[kf(d)] = i1;
1401                if odd && bmin <= d && d <= bmax && kvdb[kf(d)] <= i1 {
1402                    spl.i1 = i1;
1403                    spl.i2 = i2;
1404                    spl.min_lo = true;
1405                    spl.min_hi = true;
1406                    return ec;
1407                }
1408                d -= 2;
1409            }
1410
1411            if bmin > dmin {
1412                bmin -= 1;
1413                kvdb[kf(bmin - 1)] = XDL_LINE_MAX;
1414            } else {
1415                bmin += 1;
1416            }
1417            if bmax < dmax {
1418                bmax += 1;
1419                kvdb[kf(bmax + 1)] = XDL_LINE_MAX;
1420            } else {
1421                bmax -= 1;
1422            }
1423
1424            let mut d = bmax;
1425            while d >= bmin {
1426                let mut i1 = if kvdb[kf(d - 1)] < kvdb[kf(d + 1)] {
1427                    kvdb[kf(d - 1)]
1428                } else {
1429                    kvdb[kf(d + 1)] - 1
1430                };
1431                let prev1 = i1;
1432                let mut i2 = i1 - d;
1433                while i1 > off1
1434                    && i2 > off2
1435                    && effective_id(xdf1, i1 - 1) == effective_id(xdf2, i2 - 1)
1436                {
1437                    i1 -= 1;
1438                    i2 -= 1;
1439                }
1440                if prev1 - i1 > xenv.snake_cnt {
1441                    got_snake = true;
1442                }
1443                kvdb[kf(d)] = i1;
1444                if !odd && fmin <= d && d <= fmax && i1 <= kvdf[kf(d)] {
1445                    spl.i1 = i1;
1446                    spl.i2 = i2;
1447                    spl.min_lo = true;
1448                    spl.min_hi = true;
1449                    return ec;
1450                }
1451                d -= 2;
1452            }
1453
1454            if need_min {
1455                ec += 1;
1456                continue;
1457            }
1458
1459            if got_snake && ec > xenv.heur_min {
1460                let mut best = 0i64;
1461                let mut d = fmax;
1462                while d >= fmin {
1463                    let dd = if d > fmid { d - fmid } else { fmid - d };
1464                    let i1 = kvdf[kf(d)];
1465                    let i2 = i1 - d;
1466                    let v = (i1 - off1) + (i2 - off2) - dd;
1467                    if v > XDL_K_HEUR * ec
1468                        && v > best
1469                        && off1 + xenv.snake_cnt <= i1
1470                        && i1 < lim1
1471                        && off2 + xenv.snake_cnt <= i2
1472                        && i2 < lim2
1473                    {
1474                        let mut k = 1i64;
1475                        while effective_id(xdf1, i1 - k) == effective_id(xdf2, i2 - k) {
1476                            if k == xenv.snake_cnt {
1477                                best = v;
1478                                spl.i1 = i1;
1479                                spl.i2 = i2;
1480                                break;
1481                            }
1482                            k += 1;
1483                        }
1484                    }
1485                    d -= 2;
1486                }
1487                if best > 0 {
1488                    spl.min_lo = true;
1489                    spl.min_hi = false;
1490                    return ec;
1491                }
1492
1493                let mut best = 0i64;
1494                let mut d = bmax;
1495                while d >= bmin {
1496                    let dd = if d > bmid { d - bmid } else { bmid - d };
1497                    let i1 = kvdb[kf(d)];
1498                    let i2 = i1 - d;
1499                    let v = (lim1 - i1) + (lim2 - i2) - dd;
1500                    if v > XDL_K_HEUR * ec
1501                        && v > best
1502                        && off1 < i1
1503                        && i1 <= lim1 - xenv.snake_cnt
1504                        && off2 < i2
1505                        && i2 <= lim2 - xenv.snake_cnt
1506                    {
1507                        let mut k = 0i64;
1508                        while effective_id(xdf1, i1 + k) == effective_id(xdf2, i2 + k) {
1509                            if k == xenv.snake_cnt - 1 {
1510                                best = v;
1511                                spl.i1 = i1;
1512                                spl.i2 = i2;
1513                                break;
1514                            }
1515                            k += 1;
1516                        }
1517                    }
1518                    d -= 2;
1519                }
1520                if best > 0 {
1521                    spl.min_lo = false;
1522                    spl.min_hi = true;
1523                    return ec;
1524                }
1525            }
1526
1527            if ec >= xenv.mxcost {
1528                let mut fbest = -1i64;
1529                let mut fbest1 = -1i64;
1530                let mut d = fmax;
1531                while d >= fmin {
1532                    let mut i1 = kvdf[kf(d)].min(lim1);
1533                    let mut i2 = i1 - d;
1534                    if lim2 < i2 {
1535                        i1 = lim2 + d;
1536                        i2 = lim2;
1537                    }
1538                    if fbest < i1 + i2 {
1539                        fbest = i1 + i2;
1540                        fbest1 = i1;
1541                    }
1542                    d -= 2;
1543                }
1544
1545                let mut bbest = XDL_LINE_MAX;
1546                let mut bbest1 = XDL_LINE_MAX;
1547                let mut d = bmax;
1548                while d >= bmin {
1549                    let mut i1 = off1.max(kvdb[kf(d)]);
1550                    let mut i2 = i1 - d;
1551                    if i2 < off2 {
1552                        i1 = off2 + d;
1553                        i2 = off2;
1554                    }
1555                    if i1 + i2 < bbest {
1556                        bbest = i1 + i2;
1557                        bbest1 = i1;
1558                    }
1559                    d -= 2;
1560                }
1561
1562                if (lim1 + lim2) - bbest < fbest - (off1 + off2) {
1563                    spl.i1 = fbest1;
1564                    spl.i2 = fbest - fbest1;
1565                    spl.min_lo = true;
1566                    spl.min_hi = false;
1567                } else {
1568                    spl.i1 = bbest1;
1569                    spl.i2 = bbest - bbest1;
1570                    spl.min_lo = false;
1571                    spl.min_hi = true;
1572                }
1573                return ec;
1574            }
1575
1576            ec += 1;
1577        }
1578    }
1579
1580    /// Faithful port of `xdl_recs_cmp` (divide & conquer). Marks `changed` flags.
1581    #[allow(clippy::too_many_arguments)]
1582    fn xdl_recs_cmp(
1583        xdf1: &mut XdFile,
1584        mut off1: i64,
1585        mut lim1: i64,
1586        xdf2: &mut XdFile,
1587        mut off2: i64,
1588        mut lim2: i64,
1589        kvdf: &mut [i64],
1590        kvdb: &mut [i64],
1591        koff: i64,
1592        need_min: bool,
1593        xenv: &XdAlgoEnv,
1594    ) {
1595        while off1 < lim1 && off2 < lim2 && effective_id(xdf1, off1) == effective_id(xdf2, off2) {
1596            off1 += 1;
1597            off2 += 1;
1598        }
1599        while off1 < lim1
1600            && off2 < lim2
1601            && effective_id(xdf1, lim1 - 1) == effective_id(xdf2, lim2 - 1)
1602        {
1603            lim1 -= 1;
1604            lim2 -= 1;
1605        }
1606
1607        if off1 == lim1 {
1608            while off2 < lim2 {
1609                let r = xdf2.reference_index[off2 as usize];
1610                xdf2.changed[r] = 1;
1611                off2 += 1;
1612            }
1613        } else if off2 == lim2 {
1614            while off1 < lim1 {
1615                let r = xdf1.reference_index[off1 as usize];
1616                xdf1.changed[r] = 1;
1617                off1 += 1;
1618            }
1619        } else {
1620            let mut spl = XdpSplit {
1621                i1: 0,
1622                i2: 0,
1623                min_lo: false,
1624                min_hi: false,
1625            };
1626            xdl_split(
1627                xdf1, off1, lim1, xdf2, off2, lim2, kvdf, kvdb, koff, need_min, &mut spl, xenv,
1628            );
1629            xdl_recs_cmp(
1630                xdf1, off1, spl.i1, xdf2, off2, spl.i2, kvdf, kvdb, koff, spl.min_lo, xenv,
1631            );
1632            xdl_recs_cmp(
1633                xdf1, spl.i1, lim1, xdf2, spl.i2, lim2, kvdf, kvdb, koff, spl.min_hi, xenv,
1634            );
1635        }
1636    }
1637
1638    /// `xdl_clean_mmatch`: decide whether a multimatch record should be discarded.
1639    fn clean_mmatch(action: &[u8], i: i64, mut s: i64, mut e: i64) -> bool {
1640        if i - s > XDL_SIMSCAN_WINDOW {
1641            s = i - XDL_SIMSCAN_WINDOW;
1642        }
1643        if e - i > XDL_SIMSCAN_WINDOW {
1644            e = i + XDL_SIMSCAN_WINDOW;
1645        }
1646
1647        let mut rdis0 = 0i64;
1648        let mut rpdis0 = 1i64;
1649        let mut r = 1i64;
1650        while i - r >= s {
1651            match action[(i - r) as usize] {
1652                DISCARD => rdis0 += 1,
1653                INVESTIGATE => rpdis0 += 1,
1654                _ => break, // KEEP
1655            }
1656            r += 1;
1657        }
1658        if rdis0 == 0 {
1659            return false;
1660        }
1661        let mut rdis1 = 0i64;
1662        let mut rpdis1 = 1i64;
1663        let mut r = 1i64;
1664        while i + r <= e {
1665            match action[(i + r) as usize] {
1666                DISCARD => rdis1 += 1,
1667                INVESTIGATE => rpdis1 += 1,
1668                _ => break, // KEEP
1669            }
1670            r += 1;
1671        }
1672        if rdis1 == 0 {
1673            return false;
1674        }
1675        rdis1 += rdis0;
1676        rpdis1 += rpdis0;
1677        rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1)
1678    }
1679
1680    /// Build the `changed` flags for both token streams using Git's xdiff engine.
1681    /// When `need_min` is true, Git's minimal diff (`--minimal`) is used.
1682    /// `ids1`/`ids2` are dense interned token ids from one [`crate::line_interner::LineInterner`]
1683    /// (equal id <=> equal token); per-id tables are sized by the largest id.
1684    /// Returns `(changed1, changed2)`, one bool per original token.
1685    pub(crate) fn changed_flags(
1686        ids1: &[u32],
1687        ids2: &[u32],
1688        need_min: bool,
1689    ) -> (Vec<bool>, Vec<bool>) {
1690        changed_flags_with_shared(ids1, ids2, &[], 0, need_min)
1691    }
1692
1693    /// Like [`changed_flags`] for slices cut from two larger files whose other records are
1694    /// identical on both sides.
1695    ///
1696    /// Git classifies each record by how often it occurs in the whole other file, so the
1697    /// records left out still count: `shared_counts[id]` is how many of them have token `id`
1698    /// (ids past its end have none), and `shared_records` is how many there are on each side.
1699    pub(crate) fn changed_flags_with_shared(
1700        ids1: &[u32],
1701        ids2: &[u32],
1702        shared_counts: &[u32],
1703        shared_records: usize,
1704        need_min: bool,
1705    ) -> (Vec<bool>, Vec<bool>) {
1706        XDIFF_SCRATCH.with(|cell| {
1707            changed_flags_with_shared_inner(
1708                cell,
1709                ids1,
1710                ids2,
1711                shared_counts,
1712                shared_records,
1713                need_min,
1714            )
1715        })
1716    }
1717
1718    fn changed_flags_with_shared_inner(
1719        scratch: &RefCell<XdiffScratch>,
1720        ids1: &[u32],
1721        ids2: &[u32],
1722        shared_counts: &[u32],
1723        shared_records: usize,
1724        need_min: bool,
1725    ) -> (Vec<bool>, Vec<bool>) {
1726        let nrec1 = ids1.len();
1727        let nrec2 = ids2.len();
1728
1729        // Per-token occurrence counts on each side (class len1/len2), indexed by the dense ids.
1730        let nclasses = ids1
1731            .iter()
1732            .chain(ids2)
1733            .max()
1734            .map_or(0, |&max| max as usize + 1);
1735        let mut work = scratch.borrow_mut();
1736        work.prepare_counts(nclasses);
1737        for &id in ids1 {
1738            work.count1[id as usize] += 1;
1739        }
1740        for &id in ids2 {
1741            work.count2[id as usize] += 1;
1742        }
1743        for (id, &shared) in shared_counts.iter().enumerate().take(nclasses) {
1744            work.count1[id] += shared;
1745            work.count2[id] += shared;
1746        }
1747
1748        let mut xdf1 = XdFile {
1749            changed: vec![0u8; nrec1],
1750            reference_index: Vec::new(),
1751            effective_ids: Vec::new(),
1752            dstart: 0,
1753            dend: nrec1 as i64 - 1,
1754            nreff: 0,
1755        };
1756        let mut xdf2 = XdFile {
1757            changed: vec![0u8; nrec2],
1758            reference_index: Vec::new(),
1759            effective_ids: Vec::new(),
1760            dstart: 0,
1761            dend: nrec2 as i64 - 1,
1762            nreff: 0,
1763        };
1764
1765        // xdl_trim_ends: trim leading/trailing matching records.
1766        let lim = nrec1.min(nrec2) as i64;
1767        let mut i = 0i64;
1768        while i < lim && ids1[i as usize] == ids2[i as usize] {
1769            i += 1;
1770        }
1771        xdf1.dstart = i;
1772        xdf2.dstart = i;
1773        let mut j = 0i64;
1774        let rem = lim - i;
1775        while j < rem && ids1[nrec1 - 1 - j as usize] == ids2[nrec2 - 1 - j as usize] {
1776            j += 1;
1777        }
1778        xdf1.dend = nrec1 as i64 - j - 1;
1779        xdf2.dend = nrec2 as i64 - j - 1;
1780
1781        // xdl_cleanup_records: classify and reduce to effective records.
1782        work.prepare_actions(nrec1, nrec2);
1783        for xdf in [&mut xdf1, &mut xdf2] {
1784            let span = usize::try_from(xdf.dend - xdf.dstart + 1).unwrap_or(0);
1785            xdf.reference_index.reserve_exact(span);
1786            xdf.effective_ids.reserve_exact(span);
1787        }
1788
1789        let mlim1 = if need_min {
1790            i64::MAX
1791        } else {
1792            let mut mlim = bogosqrt((nrec1 + shared_records) as i64);
1793            if mlim > XDL_MAX_EQLIMIT {
1794                mlim = XDL_MAX_EQLIMIT;
1795            }
1796            mlim
1797        };
1798        let mut idx = xdf1.dstart;
1799        while idx <= xdf1.dend {
1800            let id = ids1[idx as usize];
1801            let nm = i64::from(work.count2[id as usize]);
1802            work.action1[idx as usize] = if nm == 0 {
1803                DISCARD
1804            } else if nm >= mlim1 {
1805                INVESTIGATE
1806            } else {
1807                KEEP
1808            };
1809            idx += 1;
1810        }
1811
1812        let mlim2 = if need_min {
1813            i64::MAX
1814        } else {
1815            let mut mlim = bogosqrt((nrec2 + shared_records) as i64);
1816            if mlim > XDL_MAX_EQLIMIT {
1817                mlim = XDL_MAX_EQLIMIT;
1818            }
1819            mlim
1820        };
1821        let mut idx = xdf2.dstart;
1822        while idx <= xdf2.dend {
1823            let id = ids2[idx as usize];
1824            let nm = i64::from(work.count1[id as usize]);
1825            work.action2[idx as usize] = if nm == 0 {
1826                DISCARD
1827            } else if nm >= mlim2 {
1828                INVESTIGATE
1829            } else {
1830                KEEP
1831            };
1832            idx += 1;
1833        }
1834
1835        let mut idx = xdf1.dstart;
1836        while idx <= xdf1.dend {
1837            let id = ids1[idx as usize];
1838            let a = work.action1[idx as usize];
1839            if a == KEEP
1840                || (a == INVESTIGATE
1841                    && !clean_mmatch(&work.action1[..nrec1 + 1], idx, xdf1.dstart, xdf1.dend))
1842            {
1843                xdf1.reference_index.push(idx as usize);
1844                xdf1.effective_ids.push(id);
1845            } else {
1846                xdf1.changed[idx as usize] = 1;
1847            }
1848            idx += 1;
1849        }
1850        xdf1.nreff = xdf1.reference_index.len() as i64;
1851
1852        let mut idx = xdf2.dstart;
1853        while idx <= xdf2.dend {
1854            let id = ids2[idx as usize];
1855            let a = work.action2[idx as usize];
1856            if a == KEEP
1857                || (a == INVESTIGATE
1858                    && !clean_mmatch(&work.action2[..nrec2 + 1], idx, xdf2.dstart, xdf2.dend))
1859            {
1860                xdf2.reference_index.push(idx as usize);
1861                xdf2.effective_ids.push(id);
1862            } else {
1863                xdf2.changed[idx as usize] = 1;
1864            }
1865            idx += 1;
1866        }
1867        xdf2.nreff = xdf2.reference_index.len() as i64;
1868
1869        // Allocate K vectors (xdl_do_diff). koff lets us use negative diagonals.
1870        let ndiags = xdf1.nreff + xdf2.nreff + 3;
1871        let kvd_len = (2 * ndiags + 2) as usize;
1872        work.prepare_kvd(kvd_len);
1873        let kvd = &mut work.kvd[..kvd_len];
1874        // kvdf base offset = nreff2 + 1; kvdb base offset = ndiags + nreff2 + 1.
1875        let koff = xdf2.nreff + 1;
1876        let (kvdf_slice, kvdb_slice) = kvd.split_at_mut(ndiags as usize);
1877        // Both slices are indexed as [k + koff]; their length covers the diagonal range.
1878
1879        let xenv = XdAlgoEnv {
1880            mxcost: bogosqrt(ndiags).max(XDL_MAX_COST_MIN),
1881            snake_cnt: XDL_SNAKE_CNT,
1882            heur_min: XDL_HEUR_MIN_COST,
1883        };
1884
1885        let nreff1 = xdf1.nreff;
1886        let nreff2 = xdf2.nreff;
1887        xdl_recs_cmp(
1888            &mut xdf1, 0, nreff1, &mut xdf2, 0, nreff2, kvdf_slice, kvdb_slice, koff, need_min,
1889            &xenv,
1890        );
1891
1892        (bool_changed(xdf1.changed), bool_changed(xdf2.changed))
1893    }
1894
1895    /// Patience diff: match unique lines, then recurse on gaps with xdiff Myers.
1896    ///
1897    /// `new_anchored[i]` marks new-side line `i` as an `--anchored` line: when it is unique on
1898    /// both sides it is forced into the common sequence. An empty slice anchors nothing.
1899    pub(crate) fn patience_changed_flags(
1900        lines1: &[&str],
1901        lines2: &[&str],
1902        new_anchored: &[bool],
1903    ) -> (Vec<bool>, Vec<bool>) {
1904        let b1: Vec<&[u8]> = lines1.iter().map(|l| l.as_bytes()).collect();
1905        let b2: Vec<&[u8]> = lines2.iter().map(|l| l.as_bytes()).collect();
1906        patience_changed_flags_bytes_anchored(&b1, &b2, new_anchored)
1907    }
1908
1909    pub(crate) fn patience_changed_flags_bytes(
1910        lines1: &[&[u8]],
1911        lines2: &[&[u8]],
1912    ) -> (Vec<bool>, Vec<bool>) {
1913        patience_changed_flags_bytes_anchored(lines1, lines2, &[])
1914    }
1915
1916    fn patience_changed_flags_bytes_anchored(
1917        lines1: &[&[u8]],
1918        lines2: &[&[u8]],
1919        new_anchored: &[bool],
1920    ) -> (Vec<bool>, Vec<bool>) {
1921        let mut changed1 = vec![false; lines1.len()];
1922        let mut changed2 = vec![false; lines2.len()];
1923        patience_recurse_bytes(
1924            lines1,
1925            lines2,
1926            new_anchored,
1927            &mut changed1,
1928            &mut changed2,
1929            0,
1930            lines1.len(),
1931            0,
1932            lines2.len(),
1933        );
1934        (changed1, changed2)
1935    }
1936
1937    #[allow(clippy::too_many_arguments)]
1938    fn patience_recurse_bytes(
1939        lines1: &[&[u8]],
1940        lines2: &[&[u8]],
1941        new_anchored: &[bool],
1942        changed1: &mut [bool],
1943        changed2: &mut [bool],
1944        o_start: usize,
1945        o_end: usize,
1946        n_start: usize,
1947        n_end: usize,
1948    ) {
1949        if o_start >= o_end && n_start >= n_end {
1950            return;
1951        }
1952        if o_start >= o_end {
1953            changed2[n_start..n_end].fill(true);
1954            return;
1955        }
1956        if n_start >= n_end {
1957            changed1[o_start..o_end].fill(true);
1958            return;
1959        }
1960
1961        let matches =
1962            patience_anchors_bytes(lines1, lines2, new_anchored, o_start, o_end, n_start, n_end);
1963        if matches.is_empty() {
1964            merge_myers_region_bytes(
1965                lines1, lines2, changed1, changed2, o_start, o_end, n_start, n_end,
1966            );
1967            return;
1968        }
1969
1970        let mut prev_o = o_start;
1971        let mut prev_n = n_start;
1972        for (ao, an) in matches {
1973            patience_recurse_bytes(
1974                lines1,
1975                lines2,
1976                new_anchored,
1977                changed1,
1978                changed2,
1979                prev_o,
1980                ao,
1981                prev_n,
1982                an,
1983            );
1984            prev_o = ao + 1;
1985            prev_n = an + 1;
1986        }
1987        patience_recurse_bytes(
1988            lines1,
1989            lines2,
1990            new_anchored,
1991            changed1,
1992            changed2,
1993            prev_o,
1994            o_end,
1995            prev_n,
1996            n_end,
1997        );
1998    }
1999
2000    /// Unique-in-both line pairs of one region, reduced to the longest run increasing on both
2001    /// sides (anchored lines are always kept).
2002    #[allow(clippy::too_many_arguments)]
2003    fn patience_anchors_bytes(
2004        lines1: &[&[u8]],
2005        lines2: &[&[u8]],
2006        new_anchored: &[bool],
2007        o_start: usize,
2008        o_end: usize,
2009        n_start: usize,
2010        n_end: usize,
2011    ) -> Vec<(usize, usize)> {
2012        use hashbrown::HashMap;
2013
2014        let old_slice = &lines1[o_start..o_end];
2015        let new_slice = &lines2[n_start..n_end];
2016        let mut old_pos: HashMap<&[u8], Vec<usize>> = HashMap::new();
2017        let mut new_pos: HashMap<&[u8], Vec<usize>> = HashMap::new();
2018        for (i, line) in old_slice.iter().enumerate() {
2019            old_pos.entry(*line).or_default().push(o_start + i);
2020        }
2021        for (i, line) in new_slice.iter().enumerate() {
2022            new_pos.entry(*line).or_default().push(n_start + i);
2023        }
2024
2025        let mut pairs: Vec<(usize, usize)> = Vec::new();
2026        for (line, o_indices) in &old_pos {
2027            if o_indices.len() != 1 {
2028                continue;
2029            }
2030            if let Some(n_indices) = new_pos.get(line) {
2031                if n_indices.len() == 1 {
2032                    pairs.push((o_indices[0], n_indices[0]));
2033                }
2034            }
2035        }
2036        pairs.sort_by_key(|p| p.0);
2037        if new_anchored.is_empty() {
2038            longest_increasing_by_second(&pairs)
2039        } else {
2040            longest_increasing_by_second_anchored(&pairs, new_anchored)
2041        }
2042    }
2043
2044    fn longest_increasing_by_second(pairs: &[(usize, usize)]) -> Vec<(usize, usize)> {
2045        if pairs.is_empty() {
2046            return Vec::new();
2047        }
2048        let n = pairs.len();
2049        let mut tails = vec![0usize; n];
2050        let mut prev = vec![usize::MAX; n];
2051        let mut len = 0usize;
2052        for i in 0..n {
2053            let v = pairs[i].1;
2054            let mut lo = 0usize;
2055            let mut hi = len;
2056            while lo < hi {
2057                let mid = (lo + hi) / 2;
2058                if pairs[tails[mid]].1 < v {
2059                    lo = mid + 1;
2060                } else {
2061                    hi = mid;
2062                }
2063            }
2064            if lo > 0 {
2065                prev[i] = tails[lo - 1];
2066            }
2067            tails[lo] = i;
2068            if lo == len {
2069                len += 1;
2070            }
2071        }
2072        let mut out = Vec::with_capacity(len);
2073        let mut k = tails[len.saturating_sub(1)];
2074        while k != usize::MAX {
2075            out.push(pairs[k]);
2076            k = prev[k];
2077        }
2078        out.reverse();
2079        out
2080    }
2081
2082    /// [`longest_increasing_by_second`] for `--anchored`: a pair whose new line is marked in
2083    /// `new_anchored` pins its slot. The sequence is cut back to end at it, and no later pair may
2084    /// replace it or anything before it, so every anchor that an earlier anchor does not undercut
2085    /// survives into the result.
2086    fn longest_increasing_by_second_anchored(
2087        pairs: &[(usize, usize)],
2088        new_anchored: &[bool],
2089    ) -> Vec<(usize, usize)> {
2090        let n = pairs.len();
2091        let mut tails = vec![0usize; n];
2092        let mut prev = vec![usize::MAX; n];
2093        let mut len = 0usize;
2094        let mut pinned: Option<usize> = None;
2095        for i in 0..n {
2096            let v = pairs[i].1;
2097            let lo = tails[..len].partition_point(|&t| pairs[t].1 < v);
2098            if lo > 0 {
2099                prev[i] = tails[lo - 1];
2100            }
2101            if pinned.is_some_and(|p| lo <= p) {
2102                continue;
2103            }
2104            tails[lo] = i;
2105            if new_anchored.get(v).copied().unwrap_or(false) {
2106                pinned = Some(lo);
2107                len = lo + 1;
2108            } else if lo == len {
2109                len += 1;
2110            }
2111        }
2112        if len == 0 {
2113            return Vec::new();
2114        }
2115        chain_from_tail(pairs, &prev, tails[len - 1], len)
2116    }
2117
2118    /// The pairs on the `prev` chain ending at `tail`, in order.
2119    fn chain_from_tail(
2120        pairs: &[(usize, usize)],
2121        prev: &[usize],
2122        tail: usize,
2123        len: usize,
2124    ) -> Vec<(usize, usize)> {
2125        let mut out = Vec::with_capacity(len);
2126        let mut k = tail;
2127        while k != usize::MAX {
2128            out.push(pairs[k]);
2129            k = prev[k];
2130        }
2131        out.reverse();
2132        out
2133    }
2134
2135    pub(crate) fn histogram_changed_flags(
2136        lines1: &[&str],
2137        lines2: &[&str],
2138    ) -> (Vec<bool>, Vec<bool>) {
2139        crate::diff::git_histogram::histogram_changed_flags(
2140            lines1,
2141            lines2,
2142            |changed1, changed2, o_start, o_end, n_start, n_end| {
2143                merge_myers_region(
2144                    lines1, lines2, changed1, changed2, o_start, o_end, n_start, n_end,
2145                );
2146            },
2147        )
2148    }
2149
2150    #[allow(clippy::too_many_arguments)]
2151    fn merge_myers_region(
2152        lines1: &[&str],
2153        lines2: &[&str],
2154        changed1: &mut [bool],
2155        changed2: &mut [bool],
2156        o_start: usize,
2157        o_end: usize,
2158        n_start: usize,
2159        n_end: usize,
2160    ) {
2161        let b1: Vec<&[u8]> = lines1.iter().map(|l| l.as_bytes()).collect();
2162        let b2: Vec<&[u8]> = lines2.iter().map(|l| l.as_bytes()).collect();
2163        merge_myers_region_bytes(&b1, &b2, changed1, changed2, o_start, o_end, n_start, n_end);
2164    }
2165
2166    #[allow(clippy::too_many_arguments)]
2167    pub(super) fn merge_myers_region_bytes(
2168        lines1: &[&[u8]],
2169        lines2: &[&[u8]],
2170        changed1: &mut [bool],
2171        changed2: &mut [bool],
2172        o_start: usize,
2173        o_end: usize,
2174        n_start: usize,
2175        n_end: usize,
2176    ) {
2177        use crate::line_interner::ByteLineInterner;
2178
2179        let old_slice = &lines1[o_start..o_end];
2180        let new_slice = &lines2[n_start..n_end];
2181        if old_slice.is_empty() && new_slice.is_empty() {
2182            return;
2183        }
2184        let mut interner = ByteLineInterner::with_capacity(old_slice.len() + new_slice.len());
2185        let ids1 = interner.intern_all_bytes(old_slice);
2186        let ids2 = interner.intern_all_bytes(new_slice);
2187        let (sub1, sub2) = changed_flags(&ids1, &ids2, false);
2188        for (i, ch) in sub1.iter().enumerate() {
2189            if *ch {
2190                changed1[o_start + i] = true;
2191            }
2192        }
2193        for (j, ch) in sub2.iter().enumerate() {
2194            if *ch {
2195                changed2[n_start + j] = true;
2196            }
2197        }
2198    }
2199}
2200
2201/// Myers changed-line masks for a line slice, honoring [`super::WhitespaceMode`] like Git `xdl_fall_back_diff`.
2202pub(crate) fn myers_rediff_changed_masks_for_region<'a>(
2203    old_lines: &[&'a str],
2204    new_lines: &[&'a str],
2205    ws: super::WhitespaceMode,
2206) -> (Vec<bool>, Vec<bool>) {
2207    use crate::line_interner::LineInterner;
2208
2209    if old_lines.is_empty() && new_lines.is_empty() {
2210        return (Vec::new(), Vec::new());
2211    }
2212    let normalize = |lines: &[&'a str]| -> Vec<Cow<'a, str>> {
2213        lines
2214            .iter()
2215            .map(|line| ws.normalize_line_for_compare(line))
2216            .collect()
2217    };
2218    let (old_normalized, new_normalized) = (normalize(old_lines), normalize(new_lines));
2219    let old_keys: Vec<&str> = old_normalized.iter().map(Cow::as_ref).collect();
2220    let new_keys: Vec<&str> = new_normalized.iter().map(Cow::as_ref).collect();
2221    let mut interner = LineInterner::with_capacity(old_keys.len() + new_keys.len());
2222    let ids1 = interner.intern_all(&old_keys);
2223    let ids2 = interner.intern_all(&new_keys);
2224    git_xdiff::changed_flags(&ids1, &ids2, false)
2225}
2226
2227/// Myers rediff on pre-interned merge compare-key ids (byte-accurate).
2228pub(crate) fn myers_rediff_changed_masks_for_region_ids(
2229    old_ids: &[u32],
2230    new_ids: &[u32],
2231) -> (Vec<bool>, Vec<bool>) {
2232    if old_ids.is_empty() && new_ids.is_empty() {
2233        return (Vec::new(), Vec::new());
2234    }
2235    git_xdiff::changed_flags(old_ids, new_ids, false)
2236}
2237
2238/// Convert per-token `changed` flags (Git xdiff style) into `similar::DiffOp`s.
2239fn changed_flags_to_ops(
2240    changed1: &[bool],
2241    changed2: &[bool],
2242    old_len: usize,
2243    new_len: usize,
2244) -> Vec<similar::DiffOp> {
2245    use similar::DiffOp;
2246    let mut ops: Vec<DiffOp> = Vec::new();
2247    let mut i = 0usize;
2248    let mut j = 0usize;
2249    while i < old_len || j < new_len {
2250        let del = i < old_len && changed1[i];
2251        let ins = j < new_len && changed2[j];
2252        if !del && !ins {
2253            if i < old_len && j < new_len {
2254                // Equal run on both sides.
2255                let start_i = i;
2256                let start_j = j;
2257                while i < old_len && j < new_len && !changed1[i] && !changed2[j] {
2258                    i += 1;
2259                    j += 1;
2260                }
2261                ops.push(DiffOp::Equal {
2262                    old_index: start_i,
2263                    new_index: start_j,
2264                    len: i - start_i,
2265                });
2266            } else if i < old_len {
2267                let start_i = i;
2268                while i < old_len {
2269                    i += 1;
2270                }
2271                ops.push(DiffOp::Delete {
2272                    old_index: start_i,
2273                    old_len: i - start_i,
2274                    new_index: j,
2275                });
2276            } else if j < new_len {
2277                let start_j = j;
2278                while j < new_len {
2279                    j += 1;
2280                }
2281                ops.push(DiffOp::Insert {
2282                    old_index: i,
2283                    new_index: start_j,
2284                    new_len: j - start_j,
2285                });
2286            }
2287        } else {
2288            // Changed run: consecutive deletions then/and insertions.
2289            let start_i = i;
2290            let start_j = j;
2291            while i < old_len && changed1[i] {
2292                i += 1;
2293            }
2294            while j < new_len && changed2[j] {
2295                j += 1;
2296            }
2297            let dlen = i - start_i;
2298            let ilen = j - start_j;
2299            if dlen > 0 && ilen > 0 {
2300                ops.push(DiffOp::Replace {
2301                    old_index: start_i,
2302                    old_len: dlen,
2303                    new_index: start_j,
2304                    new_len: ilen,
2305                });
2306            } else if dlen > 0 {
2307                ops.push(DiffOp::Delete {
2308                    old_index: start_i,
2309                    old_len: dlen,
2310                    new_index: start_j,
2311                });
2312            } else if ilen > 0 {
2313                ops.push(DiffOp::Insert {
2314                    old_index: start_i,
2315                    new_index: start_j,
2316                    new_len: ilen,
2317                });
2318            }
2319        }
2320    }
2321    ops
2322}
2323
2324/// Line-diff ops for string slices after Git `xdl_change_compact` (and optional indent heuristic).
2325#[must_use]
2326#[allow(dead_code)]
2327fn diff_slice_ops_compacted(
2328    old_lines: &[&str],
2329    new_lines: &[&str],
2330    algorithm: similar::Algorithm,
2331    indent_heuristic: bool,
2332) -> Vec<similar::DiffOp> {
2333    diff_indent_heuristic::diff_slice_ops_compacted(
2334        old_lines,
2335        new_lines,
2336        algorithm,
2337        indent_heuristic,
2338    )
2339}
2340
2341/// Compacted line-diff ops from Git's xdiff Myers engine over precomputed line ids for each side
2342/// (default blame path; faster than `similar` on large files).
2343#[must_use]
2344///
2345/// `shared_counts` and `shared_records` describe identical records of both files left out of
2346/// the slices, as for [`git_xdiff::changed_flags_with_shared`]; pass `&[]` and `0` for whole files.
2347#[allow(clippy::too_many_arguments)]
2348pub(crate) fn line_diff_ops_git_xdiff_compacted_ids(
2349    old_ids: &[u32],
2350    new_ids: &[u32],
2351    shared_counts: &[u32],
2352    shared_records: usize,
2353    old_lines: &[&str],
2354    new_lines: &[&str],
2355    indent_heuristic: bool,
2356    ws: super::WhitespaceMode,
2357) -> Vec<similar::DiffOp> {
2358    let (changed1, changed2) = git_xdiff::changed_flags_with_shared(
2359        old_ids,
2360        new_ids,
2361        shared_counts,
2362        shared_records,
2363        false,
2364    );
2365    let ops = changed_flags_to_ops(&changed1, &changed2, old_lines.len(), new_lines.len());
2366    diff_indent_heuristic::apply_change_compact_to_ops_with_ws(
2367        &ops,
2368        old_lines,
2369        new_lines,
2370        indent_heuristic,
2371        ws,
2372    )
2373}
2374
2375/// Whether each new-side line is marked changed by Git's xdiff engine (after change compaction).
2376#[cfg(test)]
2377#[must_use]
2378pub(crate) fn xdiff_new_line_changed_flags(
2379    old_lines: &[&str],
2380    new_lines: &[&str],
2381    indent_heuristic: bool,
2382) -> Vec<bool> {
2383    let mut interner =
2384        crate::line_interner::LineInterner::with_capacity(old_lines.len() + new_lines.len());
2385    let ids1 = interner.intern_all(old_lines);
2386    let ids2 = interner.intern_all(new_lines);
2387    let (changed1, changed2) = git_xdiff::changed_flags(&ids1, &ids2, false);
2388    let ops = changed_flags_to_ops(&changed1, &changed2, old_lines.len(), new_lines.len());
2389    let compacted = diff_indent_heuristic::apply_change_compact_to_ops(
2390        &ops,
2391        old_lines,
2392        new_lines,
2393        indent_heuristic,
2394    );
2395    let mut flags = vec![false; new_lines.len()];
2396    for op in compacted {
2397        match op.tag() {
2398            similar::DiffTag::Insert | similar::DiffTag::Replace => {
2399                for idx in op.new_range() {
2400                    if idx < flags.len() {
2401                        flags[idx] = true;
2402                    }
2403                }
2404            }
2405            similar::DiffTag::Equal | similar::DiffTag::Delete => {}
2406        }
2407    }
2408    flags
2409}
2410
2411/// The kind of change between two sides of a diff.
2412#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2413#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
2414#[cfg_attr(feature = "serde-models", serde(rename_all = "snake_case"))]
2415pub enum DiffStatus {
2416    /// File was added.
2417    Added,
2418    /// File was deleted.
2419    Deleted,
2420    /// File was modified (content or mode change).
2421    Modified,
2422    /// File was renamed (with optional content change).
2423    Renamed,
2424    /// File was copied.
2425    Copied,
2426    /// File type changed (e.g. regular → symlink).
2427    TypeChanged,
2428    /// Unmerged (conflict).
2429    Unmerged,
2430}
2431
2432/// Unstaged status letter for a worktree diff entry (gitlink `m` vs `M` for submodules).
2433#[must_use]
2434pub(crate) fn gitlink_unstaged_status_letter(entry: &DiffEntry) -> char {
2435    if entry.old_mode.is_gitlink() && entry.status == DiffStatus::Modified {
2436        if entry.new_oid.is_zero() {
2437            return 'm';
2438        }
2439        return 'M';
2440    }
2441    entry.status.letter()
2442}
2443
2444impl DiffStatus {
2445    /// Single-character status letter used in raw diff output.
2446    #[must_use]
2447    pub fn letter(&self) -> char {
2448        match self {
2449            Self::Added => 'A',
2450            Self::Deleted => 'D',
2451            Self::Modified => 'M',
2452            Self::Renamed => 'R',
2453            Self::Copied => 'C',
2454            Self::TypeChanged => 'T',
2455            Self::Unmerged => 'U',
2456        }
2457    }
2458
2459    /// Status for a path present on both sides with differing content or mode.
2460    ///
2461    /// Returns [`DiffStatus::TypeChanged`] when the object type bits of the two
2462    /// modes differ (regular file, symlink, gitlink) and [`DiffStatus::Modified`]
2463    /// otherwise, so a permission-only change stays `Modified`. Every diff mode
2464    /// (tree, index, work tree) shares this rule.
2465    #[must_use]
2466    pub(crate) fn for_content_change(old_mode: u32, new_mode: u32) -> Self {
2467        if old_mode & 0o170000 != new_mode & 0o170000 {
2468            Self::TypeChanged
2469        } else {
2470            Self::Modified
2471        }
2472    }
2473}
2474
2475/// A single diff entry representing one changed path.
2476#[derive(Debug, Clone, PartialEq, Eq)]
2477#[cfg_attr(feature = "serde-models", derive(serde::Serialize))]
2478pub struct DiffEntry {
2479    /// The status of this change.
2480    pub status: DiffStatus,
2481    /// Path in the "old" side (None for Added).
2482    pub old_path: Option<String>,
2483    /// Path in the "new" side (None for Deleted).
2484    pub new_path: Option<String>,
2485    /// Old file mode ([`FileMode::ABSENT`] for Added).
2486    pub old_mode: FileMode,
2487    /// New file mode ([`FileMode::ABSENT`] for Deleted).
2488    pub new_mode: FileMode,
2489    /// Old object ID (zero OID for Added).
2490    pub old_oid: ObjectId,
2491    /// New object ID (zero OID for Deleted, or for Modified index↔worktree / tree↔worktree
2492    /// entries whose content lives only on disk — raw output uses the null OID on that side).
2493    pub new_oid: ObjectId,
2494    /// Similarity score (0–100) for renames/copies.
2495    pub score: Option<u32>,
2496}
2497
2498impl DiffEntry {
2499    /// The primary path for display (new_path for adds, old_path for deletes).
2500    #[must_use]
2501    pub fn path(&self) -> &str {
2502        self.new_path
2503            .as_deref()
2504            .or(self.old_path.as_deref())
2505            .unwrap_or("")
2506    }
2507
2508    /// Return a human-oriented path display for this entry.
2509    ///
2510    /// For renames and copies this returns `old -> new`; for all other entry
2511    /// kinds this returns the primary path.
2512    #[must_use]
2513    pub fn display_path(&self) -> String {
2514        match self.status {
2515            DiffStatus::Renamed | DiffStatus::Copied => {
2516                let old = self.old_path.as_deref().unwrap_or("");
2517                let new = self.new_path.as_deref().unwrap_or("");
2518                if old.is_empty() || new.is_empty() {
2519                    self.path().to_owned()
2520                } else {
2521                    format!("{old} -> {new}")
2522                }
2523            }
2524            _ => self.path().to_owned(),
2525        }
2526    }
2527}
2528
2529/// Return the zero ObjectId.
2530#[must_use]
2531pub fn zero_oid() -> ObjectId {
2532    ObjectId::from_bytes(&[0u8; 20]).unwrap_or_else(|_| {
2533        // This should never fail since we pass exactly 20 bytes
2534        panic!("internal error: failed to create zero OID");
2535    })
2536}
2537
2538/// Return the ObjectId for the empty blob object.
2539#[must_use]
2540pub fn empty_blob_oid() -> ObjectId {
2541    ObjectId::from_hex("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391").unwrap_or_else(|_| {
2542        // This should never fail since the object ID literal is valid.
2543        panic!("internal error: failed to create empty blob OID");
2544    })
2545}
2546
2547// ── Tree-to-tree diff ───────────────────────────────────────────────
2548
2549/// Compare two trees and return the list of changed entries.
2550///
2551/// # Parameters
2552///
2553/// - `odb` — object database to read tree objects from.
2554/// - `old_tree_oid` — OID of the old tree (or `None` for comparison against empty).
2555/// - `new_tree_oid` — OID of the new tree (or `None` for comparison against empty).
2556/// - `prefix` — path prefix for nested tree recursion (empty string for root).
2557///
2558/// # Errors
2559///
2560/// Returns errors from object database reads.
2561pub(crate) fn diff_trees(
2562    odb: &Odb,
2563    old_tree_oid: Option<&ObjectId>,
2564    new_tree_oid: Option<&ObjectId>,
2565    prefix: &str,
2566) -> Result<Vec<DiffEntry>> {
2567    crate::trace::trace_span!("grit.diff.tree");
2568    diff_trees_with_cancel(odb, old_tree_oid, new_tree_oid, prefix, None)
2569}
2570
2571pub(crate) fn diff_trees_with_cancel(
2572    odb: &Odb,
2573    old_tree_oid: Option<&ObjectId>,
2574    new_tree_oid: Option<&ObjectId>,
2575    prefix: &str,
2576    cancel_watch: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
2577) -> Result<Vec<DiffEntry>> {
2578    crate::trace::trace_span!("grit.diff.tree");
2579    let hint = cancel_watch.map(|watch| DiffTreesWalkHint {
2580        cancel_watch: Some(watch),
2581        ..DiffTreesWalkHint::default()
2582    });
2583    diff_trees_opts(
2584        odb,
2585        old_tree_oid,
2586        new_tree_oid,
2587        prefix,
2588        false,
2589        hint.as_ref(),
2590    )
2591}
2592
2593/// Like [`diff_trees`], but when `index` matches `old_tree_oid` deleted subtrees are expanded
2594/// from sorted index entries instead of reading every tree object from the ODB.
2595pub(crate) fn diff_trees_with_index(
2596    odb: &Odb,
2597    index: &Index,
2598    old_tree_oid: Option<&ObjectId>,
2599    new_tree_oid: Option<&ObjectId>,
2600    prefix: &str,
2601    trust_index_for_old_tree: bool,
2602    cancel_watch: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
2603) -> Result<Vec<DiffEntry>> {
2604    let hint = (trust_index_for_old_tree
2605        && old_tree_oid.is_some()
2606        && index.entries_sorted
2607        && !index.entries.iter().any(|e| e.stage() != 0))
2608    .then_some(DiffTreesWalkHint {
2609        index: Some(index),
2610        old_tree_matches: true,
2611        cancel_watch,
2612        ..DiffTreesWalkHint::default()
2613    });
2614    diff_trees_opts(
2615        odb,
2616        old_tree_oid,
2617        new_tree_oid,
2618        prefix,
2619        false,
2620        hint.as_ref(),
2621    )
2622}
2623
2624/// Like [`diff_trees`], but subtrees at directories `index` holds as sparse-directory rows are
2625/// not descended into, so no entry lies inside one. Paths of those directories themselves (a
2626/// blob replaced by a tree, say) are still reported.
2627pub(crate) fn diff_trees_outside_sparse_directories(
2628    odb: &Odb,
2629    index: &Index,
2630    old_tree_oid: Option<&ObjectId>,
2631    new_tree_oid: Option<&ObjectId>,
2632    sparse: Option<&SparseCheckout>,
2633    index_sparse: bool,
2634    cancel_watch: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
2635) -> Result<Vec<DiffEntry>> {
2636    crate::trace::trace_span!("grit.diff.tree");
2637    let hint = index
2638        .has_sparse_directory_placeholders()
2639        .then_some(DiffTreesWalkHint {
2640            index: Some(index),
2641            skip_sparse_directories: true,
2642            sparse,
2643            index_sparse,
2644            cancel_watch,
2645            ..DiffTreesWalkHint::default()
2646        });
2647    diff_trees_opts(odb, old_tree_oid, new_tree_oid, "", false, hint.as_ref())
2648}
2649
2650/// Tree diff for hard reset and similar checkouts: index-backed old tree when clean, and one
2651/// entry per wholly added or removed subtree instead of every file path.
2652pub(crate) fn diff_trees_coalesced_for_checkout(
2653    odb: &Odb,
2654    index: &Index,
2655    old_tree_oid: Option<&ObjectId>,
2656    new_tree_oid: Option<&ObjectId>,
2657    sparse: Option<&SparseCheckout>,
2658    index_sparse: bool,
2659) -> Result<Vec<DiffEntry>> {
2660    crate::trace::trace_span!("grit.diff.tree");
2661    let trust_index_for_old_tree = old_tree_oid.is_some()
2662        && index.entries_sorted
2663        && !index.entries.iter().any(|e| e.stage() != 0);
2664    let hint = DiffTreesWalkHint {
2665        index: trust_index_for_old_tree.then_some(index),
2666        old_tree_matches: trust_index_for_old_tree,
2667        coalesce_subtrees_for_checkout: true,
2668        skip_sparse_directories: index.has_sparse_directory_placeholders(),
2669        sparse,
2670        index_sparse,
2671        ..DiffTreesWalkHint::default()
2672    };
2673    diff_trees_opts(odb, old_tree_oid, new_tree_oid, "", false, Some(&hint))
2674}
2675
2676/// Whether directory `dir` may contain paths selected by `pathspecs` during a tree walk.
2677fn tree_walk_directory_relevant(
2678    dir: &str,
2679    pathspecs: &[String],
2680    pathspec_matcher: Option<&crate::pathspec::PathspecMatcher<'_>>,
2681) -> bool {
2682    if pathspecs.is_empty() {
2683        return true;
2684    }
2685    if let Some(matcher) = pathspec_matcher {
2686        return matcher.matches_entry(dir, 0o040000)
2687            || crate::pathspec::pathspec_wants_descent_for_rev_tree(dir, pathspecs);
2688    }
2689    crate::pathspec::pathspec_entry_is_relevant(dir, true, pathspecs)
2690}
2691
2692fn diff_entry_matches_pathspecs(
2693    entry: &DiffEntry,
2694    pathspecs: &[String],
2695    pathspec_matcher: Option<&crate::pathspec::PathspecMatcher<'_>>,
2696) -> bool {
2697    let path = entry.path();
2698    if path.is_empty() {
2699        return false;
2700    }
2701    if let Some(matcher) = pathspec_matcher {
2702        return matcher.matches_change(path, entry.old_path.as_deref());
2703    }
2704    crate::pathspec::matches_pathspec_list(path, pathspecs)
2705}
2706
2707/// Like [`diff_trees`], but only paths `pathspecs` select (Git's pathspec-limited tree diff).
2708///
2709/// Directories no pathspec can reach are not read. An empty list selects every path.
2710/// When `pathspec_matcher` is set, matching uses the repository attribute stack (required for
2711/// `:(attr:...)` magic).
2712pub(crate) fn diff_trees_for_pathspecs(
2713    odb: &Odb,
2714    old_tree_oid: Option<&ObjectId>,
2715    new_tree_oid: Option<&ObjectId>,
2716    pathspecs: &[String],
2717    pathspec_matcher: Option<&crate::pathspec::PathspecMatcher<'_>>,
2718) -> Result<Vec<DiffEntry>> {
2719    diff_trees_for_pathspecs_at(
2720        odb,
2721        old_tree_oid,
2722        new_tree_oid,
2723        "",
2724        false,
2725        pathspecs,
2726        pathspec_matcher,
2727    )
2728}
2729
2730/// [`diff_trees_for_pathspecs`] for subtrees at `prefix`, optionally reporting changed subtrees
2731/// themselves (`show_trees`, Git `diff-tree -t`).
2732pub(crate) fn diff_trees_for_pathspecs_at(
2733    odb: &Odb,
2734    old_tree_oid: Option<&ObjectId>,
2735    new_tree_oid: Option<&ObjectId>,
2736    prefix: &str,
2737    show_trees: bool,
2738    pathspecs: &[String],
2739    pathspec_matcher: Option<&crate::pathspec::PathspecMatcher<'_>>,
2740) -> Result<Vec<DiffEntry>> {
2741    crate::trace::trace_span!("grit.diff.tree");
2742    if pathspecs.is_empty() {
2743        return diff_trees_opts(odb, old_tree_oid, new_tree_oid, prefix, show_trees, None);
2744    }
2745    let hint = DiffTreesWalkHint {
2746        pathspecs,
2747        pathspec_matcher,
2748        ..DiffTreesWalkHint::default()
2749    };
2750    let mut entries = diff_trees_opts(
2751        odb,
2752        old_tree_oid,
2753        new_tree_oid,
2754        prefix,
2755        show_trees,
2756        Some(&hint),
2757    )?;
2758    entries.retain(|entry| diff_entry_matches_pathspecs(entry, pathspecs, pathspec_matcher));
2759    Ok(entries)
2760}
2761
2762/// Directories a tree diff may skip, and an index that can stand in for the old tree.
2763#[derive(Clone, Default)]
2764struct DiffTreesWalkHint<'a> {
2765    index: Option<&'a Index>,
2766    /// `index` holds exactly the old tree's entries, so deleted subtrees may be listed from it.
2767    old_tree_matches: bool,
2768    /// Emit one tree entry per removed or added subtree instead of every descendant path.
2769    coalesce_subtrees_for_checkout: bool,
2770    /// Do not descend into directories `index` holds as sparse-directory rows.
2771    skip_sparse_directories: bool,
2772    sparse: Option<&'a SparseCheckout>,
2773    index_sparse: bool,
2774    /// When non-empty, do not descend into directories these pathspecs cannot match inside.
2775    pathspecs: &'a [String],
2776    pathspec_matcher: Option<&'a crate::pathspec::PathspecMatcher<'a>>,
2777    /// When set, long walks poll this flag every [`crate::progress::CANCEL_POLL_STRIDE`] entries.
2778    cancel_watch: Option<std::sync::Arc<std::sync::atomic::AtomicBool>>,
2779}
2780
2781impl DiffTreesWalkHint<'_> {
2782    /// Cone sparse checkout keeps `dir` (no trailing slash) as one opaque tree row.
2783    fn sparse_excludes_tree(&self, dir: &str) -> bool {
2784        if !self.index_sparse {
2785            return false;
2786        }
2787        let Some(sparse) = self.sparse else {
2788            return false;
2789        };
2790        let mut path = crate::repo_relpath::storage_bytes(dir).into_owned();
2791        if !path.ends_with(b"/") {
2792            path.push(b'/');
2793        }
2794        sparse.excludes_directory(&path)
2795    }
2796
2797    /// Whether the walk must not enter directory `dir` (stored form, no trailing slash).
2798    fn skips(&self, dir: &str) -> bool {
2799        if dir.is_empty() {
2800            return false;
2801        }
2802        if !self.pathspecs.is_empty()
2803            && !tree_walk_directory_relevant(dir, self.pathspecs, self.pathspec_matcher)
2804        {
2805            return true;
2806        }
2807        let Some(index) = self.index.filter(|_| self.skip_sparse_directories) else {
2808            return false;
2809        };
2810        let mut row = crate::repo_relpath::storage_bytes(dir).into_owned();
2811        row.push(b'/');
2812        index
2813            .find_entry_pos(&row, 0)
2814            .is_some_and(|pos| index.entries[pos].is_sparse_directory_placeholder())
2815    }
2816}
2817
2818/// Count path-level differences between two trees without building a [`DiffEntry`] list.
2819///
2820/// Same semantics as [`diff_trees`], but avoids allocating diff records (issue #1064 commit hot path).
2821///
2822/// # Errors
2823///
2824/// Returns errors from object database reads.
2825pub(crate) fn diff_tree_path_change_count(
2826    odb: &Odb,
2827    old_tree_oid: Option<&ObjectId>,
2828    new_tree_oid: Option<&ObjectId>,
2829    prefix: &str,
2830) -> Result<usize> {
2831    if old_tree_oid == new_tree_oid {
2832        return Ok(0);
2833    }
2834    let old_entries = match old_tree_oid {
2835        Some(oid) => read_tree(odb, oid)?,
2836        None => Vec::new(),
2837    };
2838    let new_entries = match new_tree_oid {
2839        Some(oid) => read_tree(odb, oid)?,
2840        None => Vec::new(),
2841    };
2842    let mut count = 0usize;
2843    diff_tree_entries_count(odb, &old_entries, &new_entries, prefix, false, &mut count)?;
2844    Ok(count)
2845}
2846
2847/// Diff two trees and pair deleted/added paths as renames when content is similar enough.
2848///
2849/// `rename_threshold` is Git's `-M` percentage (`0`–`100`, inclusive). Use
2850/// [`RENAME_DETECTION_DISABLED`](crate::diff::RENAME_DETECTION_DISABLED) to skip rename detection.
2851///
2852/// # Errors
2853///
2854/// Propagates errors from [`diff_trees`].
2855pub(crate) fn diff_trees_with_renames(
2856    odb: &Odb,
2857    old_tree_oid: Option<&ObjectId>,
2858    new_tree_oid: Option<&ObjectId>,
2859    prefix: &str,
2860    rename_threshold: u32,
2861) -> Result<Vec<DiffEntry>> {
2862    let config = ConfigSet::new();
2863    let policy = ThreadPolicy::default();
2864    diff_trees_with_renames_and_policy(
2865        odb,
2866        old_tree_oid,
2867        new_tree_oid,
2868        prefix,
2869        rename_threshold,
2870        &config,
2871        &policy,
2872    )
2873}
2874
2875/// Like [`diff_trees_with_renames`], but honors `index.threads` / embedder thread policy.
2876///
2877/// # Errors
2878///
2879/// Propagates errors from [`diff_trees`].
2880pub(crate) fn diff_trees_with_renames_and_policy(
2881    odb: &Odb,
2882    old_tree_oid: Option<&ObjectId>,
2883    new_tree_oid: Option<&ObjectId>,
2884    prefix: &str,
2885    rename_threshold: u32,
2886    config: &ConfigSet,
2887    thread_policy: &ThreadPolicy,
2888) -> Result<Vec<DiffEntry>> {
2889    Ok(diff_trees_with_renames_and_policy_result(
2890        odb,
2891        old_tree_oid,
2892        new_tree_oid,
2893        prefix,
2894        rename_threshold,
2895        config,
2896        thread_policy,
2897    )?
2898    .entries)
2899}
2900
2901/// Like [`diff_trees_with_renames_and_policy`], but also returns rename-limit skip metadata.
2902///
2903/// # Errors
2904///
2905/// Propagates errors from [`diff_trees`] and rename detection.
2906pub(crate) fn diff_trees_with_renames_and_policy_result(
2907    odb: &Odb,
2908    old_tree_oid: Option<&ObjectId>,
2909    new_tree_oid: Option<&ObjectId>,
2910    prefix: &str,
2911    rename_threshold: u32,
2912    config: &ConfigSet,
2913    thread_policy: &ThreadPolicy,
2914) -> Result<DiffEntriesResult> {
2915    diff_trees_with_renames_for_pathspecs_and_policy_result(
2916        odb,
2917        old_tree_oid,
2918        new_tree_oid,
2919        prefix,
2920        &[],
2921        None,
2922        rename_threshold,
2923        config,
2924        thread_policy,
2925    )
2926}
2927
2928/// Like [`diff_trees_with_renames_and_policy`], but limits the tree walk to `pathspecs` before
2929/// rename detection (Git's pathspec-limited tree diff).
2930///
2931/// An empty `pathspecs` list diffs every path under `prefix`.
2932///
2933/// # Errors
2934///
2935/// Propagates errors from [`diff_trees`] and [`detect_renames_with_policy`].
2936#[allow(clippy::too_many_arguments)]
2937pub(crate) fn diff_trees_with_renames_for_pathspecs_and_policy(
2938    odb: &Odb,
2939    old_tree_oid: Option<&ObjectId>,
2940    new_tree_oid: Option<&ObjectId>,
2941    prefix: &str,
2942    pathspecs: &[String],
2943    pathspec_matcher: Option<&crate::pathspec::PathspecMatcher<'_>>,
2944    rename_threshold: u32,
2945    config: &ConfigSet,
2946    thread_policy: &ThreadPolicy,
2947) -> Result<Vec<DiffEntry>> {
2948    Ok(diff_trees_with_renames_for_pathspecs_and_policy_result(
2949        odb,
2950        old_tree_oid,
2951        new_tree_oid,
2952        prefix,
2953        pathspecs,
2954        pathspec_matcher,
2955        rename_threshold,
2956        config,
2957        thread_policy,
2958    )?
2959    .entries)
2960}
2961
2962/// Pathspec-limited tree diff with rename detection and rename-limit skip metadata.
2963#[allow(clippy::too_many_arguments)]
2964pub(crate) fn diff_trees_with_renames_for_pathspecs_and_policy_result(
2965    odb: &Odb,
2966    old_tree_oid: Option<&ObjectId>,
2967    new_tree_oid: Option<&ObjectId>,
2968    prefix: &str,
2969    pathspecs: &[String],
2970    pathspec_matcher: Option<&crate::pathspec::PathspecMatcher<'_>>,
2971    rename_threshold: u32,
2972    config: &ConfigSet,
2973    thread_policy: &ThreadPolicy,
2974) -> Result<DiffEntriesResult> {
2975    let entries = if pathspecs.is_empty() {
2976        diff_trees(odb, old_tree_oid, new_tree_oid, prefix)?
2977    } else {
2978        diff_trees_for_pathspecs(odb, old_tree_oid, new_tree_oid, pathspecs, pathspec_matcher)?
2979    };
2980    if rename_threshold <= 100 {
2981        let out = detect_renames_with_policy(
2982            odb,
2983            None,
2984            entries,
2985            rename_threshold,
2986            config,
2987            thread_policy,
2988            None,
2989            DetectRenamesOptions::default(),
2990        )?;
2991        return Ok(DiffEntriesResult {
2992            entries: out.entries,
2993            inexact_rename_skipped: out.inexact_skipped,
2994            ..Default::default()
2995        });
2996    }
2997    Ok(DiffEntriesResult {
2998        entries,
2999        inexact_rename_skipped: None,
3000        ..Default::default()
3001    })
3002}
3003
3004/// Return the tree OID stored in a commit object.
3005///
3006/// # Errors
3007///
3008/// Returns [`crate::error::RefError::Invalid`] when `commit_oid` is not a commit, and object read/parse
3009/// errors when it is missing or malformed.
3010pub fn tree_oid_of_commit(odb: &Odb, commit_oid: &ObjectId) -> Result<ObjectId> {
3011    odb.commit_tree(commit_oid).map_err(|err| match err {
3012        Error::Object(ObjectError::UnexpectedKind { .. }) => Error::Ref(RefError::Invalid {
3013            detail: format!("invalid ref: {commit_oid} is not a commit"),
3014        }),
3015        other => other,
3016    })
3017}
3018
3019/// Tree OID to use as one side of a tree diff when the id names a commit or a tree.
3020///
3021/// # Errors
3022///
3023/// Returns [`crate::error::RefError::Invalid`] when `oid` is not a commit or tree, and object read/parse
3024/// errors when it is missing or malformed.
3025pub(crate) fn tree_oid_for_diff_side(odb: &Odb, oid: &ObjectId) -> Result<ObjectId> {
3026    use crate::objects::{CommitRef, ObjectKind};
3027    let object = odb.read(oid)?;
3028    match object.kind {
3029        ObjectKind::Commit => Ok(CommitRef::parse(&object.data)?.tree()),
3030        ObjectKind::Tree => Ok(*oid),
3031        actual => Err(Error::Ref(RefError::Invalid {
3032            detail: format!("invalid ref: {oid} is not a commit or tree ({actual:?})"),
3033        })),
3034    }
3035}
3036
3037/// Like [`tree_oid_of_commit`], but uses the repository commit-graph cache when available.
3038///
3039/// # Errors
3040///
3041/// Returns object read/parse errors when `commit_oid` is missing or not a commit.
3042pub(crate) fn tree_oid_of_commit_in_repo(
3043    repo: &crate::repo::Repository,
3044    commit_oid: &ObjectId,
3045) -> Result<ObjectId> {
3046    if repo.object_id_after_replace(commit_oid) == *commit_oid {
3047        if let Some(chain) = repo.odb().commit_graph_chain() {
3048            if let Some(tree) = chain.graph_commit_tree(commit_oid) {
3049                return Ok(tree);
3050            }
3051        }
3052    }
3053    let effective = repo.object_id_after_replace(commit_oid);
3054    tree_oid_of_commit(repo.odb(), &effective)
3055}
3056
3057/// Diff the trees at two commits and pair deleted/added paths as renames when similar enough.
3058///
3059/// `rename_threshold` is Git's `-M` percentage (`0`–`100`, inclusive). Use
3060/// [`RENAME_DETECTION_DISABLED`](crate::diff::RENAME_DETECTION_DISABLED) to skip rename detection.
3061///
3062/// # Errors
3063///
3064/// Propagates object read errors and tree diff failures.
3065#[cfg(test)]
3066pub(crate) fn diff_commits_with_renames(
3067    odb: &Odb,
3068    base: &ObjectId,
3069    tip: &ObjectId,
3070    rename_threshold: u32,
3071) -> Result<Vec<DiffEntry>> {
3072    let config = ConfigSet::new();
3073    let policy = ThreadPolicy::default();
3074    diff_commits_with_renames_and_policy(odb, base, tip, rename_threshold, &config, &policy)
3075}
3076
3077/// Like [`diff_commits_with_renames`], but honors `index.threads` / embedder thread policy.
3078///
3079/// # Errors
3080///
3081/// Propagates object read errors and tree diff failures.
3082#[cfg(test)]
3083pub(crate) fn diff_commits_with_renames_and_policy(
3084    odb: &Odb,
3085    base: &ObjectId,
3086    tip: &ObjectId,
3087    rename_threshold: u32,
3088    config: &ConfigSet,
3089    thread_policy: &ThreadPolicy,
3090) -> Result<Vec<DiffEntry>> {
3091    let base_tree = tree_oid_of_commit(odb, base)?;
3092    let tip_tree = tree_oid_of_commit(odb, tip)?;
3093    diff_trees_with_renames_and_policy(
3094        odb,
3095        Some(&base_tree),
3096        Some(&tip_tree),
3097        "",
3098        rename_threshold,
3099        config,
3100        thread_policy,
3101    )
3102}
3103
3104/// Like [`diff_trees`], but emit entries for tree objects themselves in addition
3105/// to their recursive contents (Git's `diff-tree -t` behavior).
3106///
3107/// # Errors
3108///
3109/// Returns errors from object database reads.
3110#[cfg(test)]
3111pub(crate) fn diff_trees_show_tree_entries(
3112    odb: &Odb,
3113    old_tree_oid: Option<&ObjectId>,
3114    new_tree_oid: Option<&ObjectId>,
3115    prefix: &str,
3116) -> Result<Vec<DiffEntry>> {
3117    diff_trees_opts(odb, old_tree_oid, new_tree_oid, prefix, true, None)
3118}
3119
3120fn diff_subtrees_in_place(
3121    odb: &Odb,
3122    old_tree_oid: Option<&ObjectId>,
3123    new_tree_oid: Option<&ObjectId>,
3124    prefix: &str,
3125    show_trees: bool,
3126    index_hint: Option<&DiffTreesWalkHint<'_>>,
3127    result: &mut Vec<DiffEntry>,
3128) -> Result<()> {
3129    if index_hint.is_some_and(|hint| hint.skips(prefix)) {
3130        return Ok(());
3131    }
3132    if !show_trees {
3133        if let (Some(a), Some(b)) = (old_tree_oid, new_tree_oid) {
3134            if a == b {
3135                return Ok(());
3136            }
3137        }
3138    }
3139    // The ODB already budgets cached trees. Keep only active recursion frames
3140    // here so completing siblings can release trees evicted from that cache.
3141    let old_tree = old_tree_oid
3142        .map(|oid| odb.cached_tree_for_walk(oid))
3143        .transpose()?;
3144    let new_tree = new_tree_oid
3145        .map(|oid| odb.cached_tree_for_walk(oid))
3146        .transpose()?;
3147    let oid_len = odb.hash_algo().len();
3148    diff_tree_entries_opts(
3149        odb,
3150        WalkEntries::new(old_tree.as_ref(), oid_len),
3151        WalkEntries::new(new_tree.as_ref(), oid_len),
3152        prefix,
3153        show_trees,
3154        index_hint,
3155        result,
3156    )
3157}
3158
3159/// Entries of a tree from [`Odb::cached_tree_for_walk`] in stored order, borrowing names
3160/// from it.
3161enum WalkEntries<'a> {
3162    Parsed(std::slice::Iter<'a, TreeEntry>),
3163    Raw(TreeRefIter<'a>),
3164}
3165
3166impl<'a> WalkEntries<'a> {
3167    /// Iterate `tree`, or nothing when the side is absent.
3168    fn new(tree: Option<&'a SessionTree>, oid_len: usize) -> Self {
3169        match tree {
3170            Some(SessionTree::Parsed(entries)) => Self::Parsed(entries.iter()),
3171            Some(SessionTree::Raw(data)) => Self::Raw(TreeRefIter::with_oid_len(data, oid_len)),
3172            None => Self::Parsed([].iter()),
3173        }
3174    }
3175
3176    fn next_entry(&mut self) -> Result<Option<TreeEntryRef<'a>>> {
3177        match self {
3178            Self::Parsed(entries) => Ok(entries.next().map(|entry| TreeEntryRef {
3179                mode: entry.mode,
3180                name: &entry.name,
3181                oid: entry.oid,
3182            })),
3183            Self::Raw(entries) => entries.next().transpose(),
3184        }
3185    }
3186}
3187
3188fn diff_trees_opts(
3189    odb: &Odb,
3190    old_tree_oid: Option<&ObjectId>,
3191    new_tree_oid: Option<&ObjectId>,
3192    prefix: &str,
3193    show_trees: bool,
3194    index_hint: Option<&DiffTreesWalkHint<'_>>,
3195) -> Result<Vec<DiffEntry>> {
3196    let mut result = Vec::new();
3197    diff_subtrees_in_place(
3198        odb,
3199        old_tree_oid,
3200        new_tree_oid,
3201        prefix,
3202        show_trees,
3203        index_hint,
3204        &mut result,
3205    )?;
3206    Ok(result)
3207}
3208
3209#[cfg(test)]
3210use std::cell::RefCell;
3211#[cfg(test)]
3212use std::rc::Rc;
3213#[cfg(test)]
3214use std::sync::atomic::{AtomicUsize, Ordering};
3215
3216#[cfg(test)]
3217thread_local! {
3218    static DIFF_TREE_READ_HOOK: RefCell<Option<Rc<AtomicUsize>>> = const { RefCell::new(None) };
3219}
3220
3221/// Install a per-thread tree read counter for unit tests (cleared when `hook` is `None`).
3222#[cfg(test)]
3223pub(crate) fn set_diff_tree_read_hook_for_tests(hook: Option<Rc<AtomicUsize>>) {
3224    DIFF_TREE_READ_HOOK.with(|cell| *cell.borrow_mut() = hook);
3225}
3226
3227/// Read and parse a tree object from the ODB (no session cache).
3228fn read_tree_from_odb(odb: &Odb, oid: &ObjectId) -> Result<Vec<TreeEntry>> {
3229    #[cfg(test)]
3230    if let Some(counter) = DIFF_TREE_READ_HOOK.with(|cell| cell.borrow().clone()) {
3231        counter.fetch_add(1, Ordering::Relaxed);
3232    }
3233    Ok((*odb.cached_tree_entries(oid)?).clone())
3234}
3235
3236/// Read and parse a tree object from the ODB.
3237fn read_tree(odb: &Odb, oid: &ObjectId) -> Result<Vec<TreeEntry>> {
3238    read_tree_from_odb(odb, oid)
3239}
3240
3241fn diff_tree_entries_count(
3242    odb: &Odb,
3243    old: &[TreeEntry],
3244    new: &[TreeEntry],
3245    prefix: &str,
3246    show_trees: bool,
3247    count: &mut usize,
3248) -> Result<()> {
3249    let mut oi = 0;
3250    let mut ni = 0;
3251
3252    while oi < old.len() || ni < new.len() {
3253        match (old.get(oi), new.get(ni)) {
3254            (Some(o), Some(n)) => {
3255                let cmp = crate::objects::tree_name_cmp(
3256                    &o.name,
3257                    is_tree_mode(o.mode),
3258                    &n.name,
3259                    is_tree_mode(n.mode),
3260                );
3261                match cmp {
3262                    std::cmp::Ordering::Less => {
3263                        emit_deleted_count(odb, o, prefix, show_trees, count)?;
3264                        oi += 1;
3265                    }
3266                    std::cmp::Ordering::Greater => {
3267                        emit_added_count(odb, n, prefix, show_trees, count)?;
3268                        ni += 1;
3269                    }
3270                    std::cmp::Ordering::Equal => {
3271                        if o.oid != n.oid
3272                            || crate::index::canon_mode(o.mode) != crate::index::canon_mode(n.mode)
3273                        {
3274                            let path = crate::repo_relpath::format_path_stored(prefix, &o.name);
3275                            if is_tree_mode(o.mode) && is_tree_mode(n.mode) {
3276                                if show_trees {
3277                                    *count += 1;
3278                                }
3279                                *count += diff_tree_path_change_count(
3280                                    odb,
3281                                    Some(&o.oid),
3282                                    Some(&n.oid),
3283                                    &path,
3284                                )?;
3285                            } else if is_tree_mode(o.mode) != is_tree_mode(n.mode) {
3286                                emit_deleted_count(odb, o, prefix, show_trees, count)?;
3287                                emit_added_count(odb, n, prefix, show_trees, count)?;
3288                            } else {
3289                                *count += 1;
3290                            }
3291                        }
3292                        oi += 1;
3293                        ni += 1;
3294                    }
3295                }
3296            }
3297            (Some(o), None) => {
3298                emit_deleted_count(odb, o, prefix, show_trees, count)?;
3299                oi += 1;
3300            }
3301            (None, Some(n)) => {
3302                emit_added_count(odb, n, prefix, show_trees, count)?;
3303                ni += 1;
3304            }
3305            (None, None) => break,
3306        }
3307    }
3308
3309    Ok(())
3310}
3311
3312fn emit_deleted_count(
3313    odb: &Odb,
3314    entry: &TreeEntry,
3315    prefix: &str,
3316    show_trees: bool,
3317    count: &mut usize,
3318) -> Result<()> {
3319    let path = crate::repo_relpath::format_path_stored(prefix, &entry.name);
3320    if is_tree_mode(entry.mode) {
3321        if show_trees {
3322            *count += 1;
3323        }
3324        *count += diff_tree_path_change_count(odb, Some(&entry.oid), None, &path)?;
3325    } else {
3326        *count += 1;
3327    }
3328    Ok(())
3329}
3330
3331fn emit_added_count(
3332    odb: &Odb,
3333    entry: &TreeEntry,
3334    prefix: &str,
3335    show_trees: bool,
3336    count: &mut usize,
3337) -> Result<()> {
3338    let path = crate::repo_relpath::format_path_stored(prefix, &entry.name);
3339    if is_tree_mode(entry.mode) {
3340        if show_trees {
3341            *count += 1;
3342        }
3343        *count += diff_tree_path_change_count(odb, None, Some(&entry.oid), &path)?;
3344    } else {
3345        *count += 1;
3346    }
3347    Ok(())
3348}
3349
3350/// Compare two sorted streams of tree entries, recursing into subtrees.
3351fn poll_tree_diff_cancel(
3352    index_hint: Option<&DiffTreesWalkHint<'_>>,
3353    stride: &mut usize,
3354) -> Result<()> {
3355    use crate::progress::{poll_watch, CANCEL_POLL_STRIDE};
3356    *stride = stride.wrapping_add(1);
3357    if stride.is_multiple_of(CANCEL_POLL_STRIDE) {
3358        if let Some(watch) = index_hint.and_then(|hint| hint.cancel_watch.as_ref()) {
3359            poll_watch(watch)?;
3360        }
3361    }
3362    Ok(())
3363}
3364
3365fn diff_tree_entries_opts(
3366    odb: &Odb,
3367    mut old: WalkEntries<'_>,
3368    mut new: WalkEntries<'_>,
3369    prefix: &str,
3370    show_trees: bool,
3371    index_hint: Option<&DiffTreesWalkHint<'_>>,
3372    result: &mut Vec<DiffEntry>,
3373) -> Result<()> {
3374    let mut old_entry = old.next_entry()?;
3375    let mut new_entry = new.next_entry()?;
3376    let mut cancel_stride = 0usize;
3377
3378    loop {
3379        poll_tree_diff_cancel(index_hint, &mut cancel_stride)?;
3380        match (&old_entry, &new_entry) {
3381            (Some(o), Some(n)) => {
3382                let cmp = crate::objects::tree_name_cmp(
3383                    o.name,
3384                    is_tree_mode(o.mode),
3385                    n.name,
3386                    is_tree_mode(n.mode),
3387                );
3388                match cmp {
3389                    std::cmp::Ordering::Less => {
3390                        // Old entry not in new → deleted
3391                        emit_deleted_opts(odb, o, prefix, show_trees, index_hint, result)?;
3392                        old_entry = old.next_entry()?;
3393                    }
3394                    std::cmp::Ordering::Greater => {
3395                        // New entry not in old → added
3396                        emit_added_opts(odb, n, prefix, show_trees, index_hint, result)?;
3397                        new_entry = new.next_entry()?;
3398                    }
3399                    std::cmp::Ordering::Equal => {
3400                        // Both present — check for changes
3401                        if o.oid != n.oid
3402                            || crate::index::canon_mode(o.mode) != crate::index::canon_mode(n.mode)
3403                        {
3404                            let path = crate::repo_relpath::format_path_stored(prefix, o.name);
3405                            if is_tree_mode(o.mode) && is_tree_mode(n.mode) {
3406                                // Both are trees
3407                                if show_trees {
3408                                    result.push(DiffEntry {
3409                                        status: DiffStatus::Modified,
3410                                        old_path: Some(path.clone()),
3411                                        new_path: Some(path.clone()),
3412                                        old_mode: FileMode::new(crate::index::canon_mode(o.mode)),
3413                                        new_mode: FileMode::new(crate::index::canon_mode(n.mode)),
3414                                        old_oid: o.oid,
3415                                        new_oid: n.oid,
3416                                        score: None,
3417                                    });
3418                                }
3419                                diff_subtrees_in_place(
3420                                    odb,
3421                                    Some(&o.oid),
3422                                    Some(&n.oid),
3423                                    &path,
3424                                    show_trees,
3425                                    index_hint,
3426                                    result,
3427                                )?;
3428                            } else if is_tree_mode(o.mode) && !is_tree_mode(n.mode) {
3429                                // Tree → blob: delete tree contents, add blob
3430                                emit_deleted_opts(odb, o, prefix, show_trees, index_hint, result)?;
3431                                emit_added_opts(odb, n, prefix, show_trees, index_hint, result)?;
3432                            } else if !is_tree_mode(o.mode) && is_tree_mode(n.mode) {
3433                                // Blob → tree: delete blob, add tree contents
3434                                emit_deleted_opts(odb, o, prefix, show_trees, index_hint, result)?;
3435                                emit_added_opts(odb, n, prefix, show_trees, index_hint, result)?;
3436                            } else {
3437                                result.push(DiffEntry {
3438                                    status: DiffStatus::for_content_change(o.mode, n.mode),
3439                                    old_path: Some(path.clone()),
3440                                    new_path: Some(path),
3441                                    old_mode: FileMode::new(crate::index::canon_mode(o.mode)),
3442                                    new_mode: FileMode::new(crate::index::canon_mode(n.mode)),
3443                                    old_oid: o.oid,
3444                                    new_oid: n.oid,
3445                                    score: None,
3446                                });
3447                            }
3448                        }
3449                        old_entry = old.next_entry()?;
3450                        new_entry = new.next_entry()?;
3451                    }
3452                }
3453            }
3454            (Some(o), None) => {
3455                emit_deleted_opts(odb, o, prefix, show_trees, index_hint, result)?;
3456                old_entry = old.next_entry()?;
3457            }
3458            (None, Some(n)) => {
3459                emit_added_opts(odb, n, prefix, show_trees, index_hint, result)?;
3460                new_entry = new.next_entry()?;
3461            }
3462            (None, None) => break,
3463        }
3464    }
3465
3466    Ok(())
3467}
3468
3469fn emit_deleted_tree_from_index(
3470    index: &Index,
3471    dir_prefix: &str,
3472    index_hint: Option<&DiffTreesWalkHint<'_>>,
3473    result: &mut Vec<DiffEntry>,
3474) -> Result<()> {
3475    let scan_prefix: Vec<u8> = if dir_prefix.is_empty() {
3476        Vec::new()
3477    } else {
3478        format!("{dir_prefix}/").into_bytes()
3479    };
3480    let mut cancel_stride = 0usize;
3481    if index.entries_sorted {
3482        let start = if scan_prefix.is_empty() {
3483            0
3484        } else {
3485            index
3486                .entries
3487                .partition_point(|e| e.path.as_slice() < scan_prefix.as_slice())
3488        };
3489        for ie in index.entries[start..].iter() {
3490            poll_tree_diff_cancel(index_hint, &mut cancel_stride)?;
3491            if ie.stage() != 0 {
3492                continue;
3493            }
3494            if !scan_prefix.is_empty() && !ie.path.starts_with(&scan_prefix) {
3495                break;
3496            }
3497            if scan_prefix.is_empty() && ie.path.contains(&b'/') {
3498                continue;
3499            }
3500            let path = crate::repo_relpath::storage_string(&ie.path);
3501            result.push(DiffEntry {
3502                status: DiffStatus::Deleted,
3503                old_path: Some(path),
3504                new_path: None,
3505                old_mode: FileMode::new(ie.mode),
3506                new_mode: FileMode::ABSENT,
3507                old_oid: ie.oid,
3508                new_oid: zero_oid(),
3509                score: None,
3510            });
3511        }
3512        return Ok(());
3513    }
3514    for ie in &index.entries {
3515        poll_tree_diff_cancel(index_hint, &mut cancel_stride)?;
3516        if ie.stage() != 0 {
3517            continue;
3518        }
3519        let Ok(path) = std::str::from_utf8(&ie.path) else {
3520            continue;
3521        };
3522        let under = if dir_prefix.is_empty() {
3523            !path.contains('/')
3524        } else if path == dir_prefix {
3525            false
3526        } else {
3527            path.strip_prefix(dir_prefix)
3528                .is_some_and(|rest| rest.starts_with('/'))
3529        };
3530        if !under {
3531            continue;
3532        }
3533        result.push(DiffEntry {
3534            status: DiffStatus::Deleted,
3535            old_path: Some(path.to_owned()),
3536            new_path: None,
3537            old_mode: FileMode::new(ie.mode),
3538            new_mode: FileMode::ABSENT,
3539            old_oid: ie.oid,
3540            new_oid: zero_oid(),
3541            score: None,
3542        });
3543    }
3544    Ok(())
3545}
3546
3547fn emit_deleted_opts(
3548    odb: &Odb,
3549    entry: &TreeEntryRef<'_>,
3550    prefix: &str,
3551    show_trees: bool,
3552    index_hint: Option<&DiffTreesWalkHint<'_>>,
3553    result: &mut Vec<DiffEntry>,
3554) -> Result<()> {
3555    let path = crate::repo_relpath::format_path_stored(prefix, entry.name);
3556    if is_tree_mode(entry.mode) {
3557        if index_hint.is_some_and(|hint| hint.sparse_excludes_tree(&path)) {
3558            return Ok(());
3559        }
3560        if show_trees || index_hint.is_some_and(|hint| hint.coalesce_subtrees_for_checkout) {
3561            result.push(DiffEntry {
3562                status: DiffStatus::Deleted,
3563                old_path: Some(path.clone()),
3564                new_path: None,
3565                old_mode: FileMode::new(entry.mode),
3566                new_mode: FileMode::ABSENT,
3567                old_oid: entry.oid,
3568                new_oid: zero_oid(),
3569                score: None,
3570            });
3571        }
3572        if index_hint.is_some_and(|hint| hint.coalesce_subtrees_for_checkout) {
3573            return Ok(());
3574        }
3575        if let Some(index) = index_hint
3576            .filter(|hint| hint.old_tree_matches)
3577            .and_then(|hint| hint.index)
3578            .filter(|index| !index.has_sparse_directory_placeholders())
3579        {
3580            emit_deleted_tree_from_index(index, &path, index_hint, result)?;
3581            return Ok(());
3582        }
3583        diff_subtrees_in_place(
3584            odb,
3585            Some(&entry.oid),
3586            None,
3587            &path,
3588            show_trees,
3589            index_hint,
3590            result,
3591        )?;
3592    } else {
3593        result.push(DiffEntry {
3594            status: DiffStatus::Deleted,
3595            old_path: Some(path.clone()),
3596            new_path: None,
3597            old_mode: FileMode::new(crate::index::canon_mode(entry.mode)),
3598            new_mode: FileMode::ABSENT,
3599            old_oid: entry.oid,
3600            new_oid: zero_oid(),
3601            score: None,
3602        });
3603    }
3604    Ok(())
3605}
3606
3607fn emit_added_opts(
3608    odb: &Odb,
3609    entry: &TreeEntryRef<'_>,
3610    prefix: &str,
3611    show_trees: bool,
3612    index_hint: Option<&DiffTreesWalkHint<'_>>,
3613    result: &mut Vec<DiffEntry>,
3614) -> Result<()> {
3615    let path = crate::repo_relpath::format_path_stored(prefix, entry.name);
3616    if is_tree_mode(entry.mode) {
3617        if index_hint.is_some_and(|hint| hint.sparse_excludes_tree(&path)) {
3618            result.push(DiffEntry {
3619                status: DiffStatus::Added,
3620                old_path: None,
3621                new_path: Some(crate::repo_relpath::sparse_directory_stored_path(&path)),
3622                old_mode: FileMode::ABSENT,
3623                new_mode: FileMode::new(MODE_TREE),
3624                old_oid: zero_oid(),
3625                new_oid: entry.oid,
3626                score: None,
3627            });
3628            return Ok(());
3629        }
3630        if show_trees || index_hint.is_some_and(|hint| hint.coalesce_subtrees_for_checkout) {
3631            result.push(DiffEntry {
3632                status: DiffStatus::Added,
3633                old_path: None,
3634                new_path: Some(path.clone()),
3635                old_mode: FileMode::ABSENT,
3636                new_mode: FileMode::new(entry.mode),
3637                old_oid: zero_oid(),
3638                new_oid: entry.oid,
3639                score: None,
3640            });
3641        }
3642        if index_hint.is_some_and(|hint| hint.coalesce_subtrees_for_checkout) {
3643            return Ok(());
3644        }
3645        diff_subtrees_in_place(
3646            odb,
3647            None,
3648            Some(&entry.oid),
3649            &path,
3650            show_trees,
3651            index_hint,
3652            result,
3653        )?;
3654    } else {
3655        result.push(DiffEntry {
3656            status: DiffStatus::Added,
3657            old_path: None,
3658            new_path: Some(path),
3659            old_mode: FileMode::ABSENT,
3660            new_mode: FileMode::new(crate::index::canon_mode(entry.mode)),
3661            old_oid: zero_oid(),
3662            new_oid: entry.oid,
3663            score: None,
3664        });
3665    }
3666    Ok(())
3667}
3668
3669// ── Index-to-tree diff (staged changes) ─────────────────────────────
3670
3671/// Compare the index against a tree (usually HEAD's tree).
3672///
3673/// This shows "staged" changes — what would be committed.
3674///
3675/// # Parameters
3676///
3677/// - `odb` — object database.
3678/// - `index` — the current index.
3679/// - `tree_oid` — the tree to compare against (e.g. HEAD's tree), or `None`
3680///   for comparison against an empty tree (initial commit).
3681///
3682/// # Errors
3683///
3684/// Returns errors from ODB reads.
3685///
3686/// When `ignore_submodules` is true, gitlink (`160000`) paths are omitted from the diff, matching
3687/// Git's `require_clean_work_tree(..., ignore_submodules=1)` used by `git rebase` / `git pull`.
3688pub(crate) fn diff_index_to_tree(
3689    odb: &Odb,
3690    index: &Index,
3691    tree_oid: Option<&ObjectId>,
3692    ignore_submodules: bool,
3693) -> Result<Vec<DiffEntry>> {
3694    match index_cache_tree_walk(index, tree_oid) {
3695        CacheTreeWalk::Fast {
3696            head_tree,
3697            cache_root,
3698        } => {
3699            diff_index_to_tree_with_cache_tree(odb, index, head_tree, cache_root, ignore_submodules)
3700        }
3701        CacheTreeWalk::Flatten => {
3702            diff_index_to_tree_flatten(odb, index, tree_oid, ignore_submodules)
3703        }
3704    }
3705}
3706
3707/// Diff the index against a tree and pair deleted/added paths as renames when similar enough.
3708///
3709/// `rename_threshold` is Git's `-M` percentage (50 is the usual default); pass `100` to skip
3710/// rename detection.
3711///
3712/// # Errors
3713///
3714/// Propagates errors from [`diff_index_to_tree`].
3715pub(crate) fn diff_index_to_tree_with_renames(
3716    odb: &Odb,
3717    index: &Index,
3718    tree_oid: Option<&ObjectId>,
3719    ignore_submodules: bool,
3720    rename_threshold: u32,
3721    config: &ConfigSet,
3722    thread_policy: &ThreadPolicy,
3723) -> Result<Vec<DiffEntry>> {
3724    let entries = diff_index_to_tree(odb, index, tree_oid, ignore_submodules)?;
3725    apply_rename_detection_to_diff_entries(
3726        odb,
3727        entries,
3728        None,
3729        rename_threshold,
3730        config,
3731        thread_policy,
3732    )
3733}
3734
3735/// Apply Git `-M` rename detection to a flat diff (index↔tree, tree↔tree, or index↔worktree).
3736///
3737/// Pass `work_root` when added sides may be read from the working tree (index↔worktree diffs).
3738/// Use `rename_threshold` `100` to leave entries unchanged.
3739///
3740/// # Errors
3741///
3742/// Propagates object read failures while scoring renames.
3743pub(crate) fn apply_rename_detection_to_diff_entries(
3744    odb: &Odb,
3745    entries: Vec<DiffEntry>,
3746    work_root: Option<&Path>,
3747    rename_threshold: u32,
3748    config: &ConfigSet,
3749    thread_policy: &ThreadPolicy,
3750) -> Result<Vec<DiffEntry>> {
3751    if entries.is_empty() || rename_threshold >= 100 {
3752        return Ok(entries);
3753    }
3754    if work_root.is_some() {
3755        let opts = DiffPostprocessOptions {
3756            rename_threshold,
3757            ..DiffPostprocessOptions::default()
3758        };
3759        postprocess_diff_entries(
3760            odb,
3761            entries,
3762            None,
3763            work_root,
3764            None,
3765            config,
3766            thread_policy,
3767            &opts,
3768            None,
3769        )
3770    } else {
3771        detect_renames_with_policy(
3772            odb,
3773            None,
3774            entries,
3775            rename_threshold,
3776            config,
3777            thread_policy,
3778            None,
3779            DetectRenamesOptions::default(),
3780        )
3781        .map(|r| r.entries)
3782    }
3783}
3784
3785/// Diff the index against the working tree and pair deleted/added paths as renames when similar.
3786///
3787/// Runs `diff_index_to_worktree_with_options`, then `apply_rename_detection_to_diff_entries`
3788/// with `work_root` set to `work_tree`. Untracked worktree paths are not diff entries (see
3789/// [`crate::porcelain::status`]); they are never fed into rename detection here.
3790///
3791/// # Errors
3792///
3793/// Propagates errors from the index↔worktree diff or rename post-processing.
3794pub(crate) fn diff_index_to_worktree_with_renames(
3795    repo: &Repository,
3796    index: &mut Index,
3797    work_tree: &Path,
3798    options: DiffIndexToWorktreeOptions,
3799    rename_threshold: u32,
3800) -> Result<(Vec<DiffEntry>, bool)> {
3801    let config = repo.config().snapshot()?.as_ref().clone();
3802    let thread_policy = repo.thread_policy();
3803    let odb = repo.odb();
3804    let (entries, changed) = diff_index_to_worktree_with_options(odb, index, work_tree, options)?;
3805    let entries = apply_rename_detection_to_diff_entries(
3806        odb,
3807        entries,
3808        Some(work_tree),
3809        rename_threshold,
3810        &config,
3811        &thread_policy,
3812    )?;
3813    Ok((entries, changed))
3814}
3815
3816enum CacheTreeWalk<'a> {
3817    Fast {
3818        head_tree: &'a ObjectId,
3819        cache_root: &'a CacheTreeNode,
3820    },
3821    Flatten,
3822}
3823
3824/// Whether the index may use the cache-tree walker (possibly with an invalid root and valid
3825/// descendant nodes after partial invalidation).
3826fn index_cache_tree_walk<'a>(
3827    index: &'a Index,
3828    tree_oid: Option<&'a ObjectId>,
3829) -> CacheTreeWalk<'a> {
3830    let Some(head_tree) = tree_oid else {
3831        return CacheTreeWalk::Flatten;
3832    };
3833    let Some(cache_root) = index.cache_tree.as_ref() else {
3834        return CacheTreeWalk::Flatten;
3835    };
3836    if index.entries.iter().any(|e| e.stage() != 0) {
3837        return CacheTreeWalk::Flatten;
3838    }
3839    CacheTreeWalk::Fast {
3840        head_tree,
3841        cache_root,
3842    }
3843}
3844
3845/// Flatten-tree index-vs-tree diff (used when the index has no cache-tree extension).
3846fn diff_index_to_tree_flatten(
3847    odb: &Odb,
3848    index: &Index,
3849    tree_oid: Option<&ObjectId>,
3850    ignore_submodules: bool,
3851) -> Result<Vec<DiffEntry>> {
3852    let sparse_index = index.has_sparse_directory_placeholders();
3853    // Flatten the tree into a sorted list of (path, mode, oid)
3854    let tree_entries = match tree_oid {
3855        Some(oid) if sparse_index => flatten_tree_with_index(odb, oid, "", Some(index))?,
3856        Some(oid) => flatten_tree(odb, oid, "")?,
3857        None => Vec::new(),
3858    };
3859
3860    // Build maps keyed by path
3861    let mut tree_map: std::collections::BTreeMap<&str, &FlatEntry> =
3862        std::collections::BTreeMap::new();
3863    for entry in &tree_entries {
3864        tree_map.insert(&entry.path, entry);
3865    }
3866
3867    let mut result = Vec::new();
3868    let mut stage0_paths = std::collections::BTreeSet::new();
3869    let mut unmerged_modes: std::collections::BTreeMap<String, (u8, u32)> =
3870        std::collections::BTreeMap::new();
3871
3872    // Check index entries against tree
3873    for ie in &index.entries {
3874        let path = String::from_utf8_lossy(&ie.path).to_string();
3875        if ie.stage() == 0 && ie.intent_to_add() {
3876            // Intent-to-add entries are not "staged" for diff-index / status
3877            // (matches Git: `git diff --cached` is empty for `-N` paths).
3878            continue;
3879        }
3880        if ie.stage() == 0 && ie.is_sparse_directory_placeholder() {
3881            let dir = path.strip_suffix('/').unwrap_or(path.as_str());
3882            let index_flat = flatten_tree(odb, &ie.oid, dir)?;
3883            let head_flat = match tree_oid {
3884                None => Vec::new(),
3885                Some(head_root) => match subtree_oid_at_path(odb, head_root, dir)? {
3886                    None => Vec::new(),
3887                    Some(head_sub) if head_sub == ie.oid => {
3888                        continue;
3889                    }
3890                    Some(head_sub) => flatten_tree(odb, &head_sub, dir)?,
3891                },
3892            };
3893            diff_flattened_subtrees(
3894                &head_flat,
3895                &index_flat,
3896                ignore_submodules,
3897                &mut result,
3898                &mut stage0_paths,
3899                &mut tree_map,
3900            );
3901            continue;
3902        }
3903        if ie.stage() != 0 {
3904            let rank = match ie.stage() {
3905                2 => 0u8,
3906                3 => 1u8,
3907                1 => 2u8,
3908                _ => 3u8,
3909            };
3910            match unmerged_modes.get(&path) {
3911                Some((existing_rank, _)) if *existing_rank <= rank => {}
3912                _ => {
3913                    unmerged_modes.insert(path, (rank, ie.mode));
3914                }
3915            }
3916            continue;
3917        }
3918        if ignore_submodules && ie.mode == 0o160000 {
3919            let _ = tree_map.remove(path.as_str());
3920            stage0_paths.insert(path.clone());
3921            continue;
3922        }
3923        stage0_paths.insert(path.clone());
3924        match tree_map.remove(path.as_str()) {
3925            Some(te) => {
3926                // Present in both — check for differences
3927                if te.oid != ie.oid
3928                    || crate::index::canon_mode(te.mode) != crate::index::canon_mode(ie.mode)
3929                {
3930                    let tree_mode = crate::index::canon_mode(te.mode);
3931                    let index_mode = crate::index::canon_mode(ie.mode);
3932                    result.push(DiffEntry {
3933                        status: DiffStatus::for_content_change(tree_mode, index_mode),
3934                        old_path: Some(path.clone()),
3935                        new_path: Some(path),
3936                        old_mode: FileMode::new(tree_mode),
3937                        new_mode: FileMode::new(index_mode),
3938                        old_oid: te.oid,
3939                        new_oid: ie.oid,
3940                        score: None,
3941                    });
3942                }
3943            }
3944            None => {
3945                // In index but not tree → added
3946                result.push(DiffEntry {
3947                    status: DiffStatus::Added,
3948                    old_path: None,
3949                    new_path: Some(path),
3950                    old_mode: FileMode::ABSENT,
3951                    new_mode: FileMode::new(ie.mode),
3952                    old_oid: zero_oid(),
3953                    new_oid: ie.oid,
3954                    score: None,
3955                });
3956            }
3957        }
3958    }
3959
3960    diff_index_to_tree_finish_unmerged_and_deleted(
3961        ignore_submodules,
3962        &mut tree_map,
3963        &unmerged_modes,
3964        &stage0_paths,
3965        &mut result,
3966    );
3967
3968    sort_entries_by_path(&mut result);
3969    Ok(result)
3970}
3971
3972fn diff_index_to_tree_finish_unmerged_and_deleted(
3973    ignore_submodules: bool,
3974    tree_map: &mut std::collections::BTreeMap<&str, &FlatEntry>,
3975    unmerged_modes: &std::collections::BTreeMap<String, (u8, u32)>,
3976    stage0_paths: &std::collections::BTreeSet<String>,
3977    result: &mut Vec<DiffEntry>,
3978) {
3979    for (path, (_, mode)) in unmerged_modes {
3980        if stage0_paths.contains(path) {
3981            continue;
3982        }
3983        tree_map.remove(path.as_str());
3984        result.push(DiffEntry {
3985            status: DiffStatus::Unmerged,
3986            old_path: Some(path.clone()),
3987            new_path: Some(path.clone()),
3988            old_mode: FileMode::ABSENT,
3989            new_mode: FileMode::new(*mode),
3990            old_oid: zero_oid(),
3991            new_oid: zero_oid(),
3992            score: None,
3993        });
3994    }
3995
3996    // Remaining tree entries not in index → deleted
3997    for (path, te) in std::mem::take(tree_map) {
3998        if ignore_submodules && te.mode == 0o160000 {
3999            continue;
4000        }
4001        result.push(DiffEntry {
4002            status: DiffStatus::Deleted,
4003            old_path: Some(path.to_owned()),
4004            new_path: None,
4005            old_mode: FileMode::new(te.mode),
4006            new_mode: FileMode::ABSENT,
4007            old_oid: te.oid,
4008            new_oid: zero_oid(),
4009            score: None,
4010        });
4011    }
4012}
4013
4014/// Cache-tree-aware index-vs-tree diff: walk HEAD tree and index together and skip
4015/// subtrees whose valid cache-tree node OID matches the tree entry (Git unpack-trees).
4016fn diff_index_to_tree_with_cache_tree(
4017    odb: &Odb,
4018    index: &Index,
4019    head_tree: &ObjectId,
4020    cache_root: &CacheTreeNode,
4021    ignore_submodules: bool,
4022) -> Result<Vec<DiffEntry>> {
4023    let mut result = Vec::new();
4024    let mut idx_cursor = 0usize;
4025
4026    let mut walk = DiffIndexTreeWalk {
4027        odb,
4028        index,
4029        idx_cursor: &mut idx_cursor,
4030        ignore_submodules,
4031        result: &mut result,
4032        abort_to_flatten: false,
4033    };
4034    diff_index_tree_at(&mut walk, head_tree, Some(cache_root), b"")?;
4035    if walk.abort_to_flatten {
4036        return diff_index_to_tree_flatten(odb, index, Some(head_tree), ignore_submodules);
4037    }
4038
4039    // Index entries not consumed by the tree walk (added paths).
4040    while idx_cursor < index.entries.len() {
4041        let ie = &index.entries[idx_cursor];
4042        idx_cursor += 1;
4043        let path = String::from_utf8_lossy(&ie.path).to_string();
4044        if ie.stage() == 0 && ie.intent_to_add() {
4045            continue;
4046        }
4047        if ie.stage() != 0 {
4048            continue;
4049        }
4050        if ignore_submodules && ie.mode == 0o160000 {
4051            continue;
4052        }
4053        result.push(DiffEntry {
4054            status: DiffStatus::Added,
4055            old_path: None,
4056            new_path: Some(path),
4057            old_mode: FileMode::ABSENT,
4058            new_mode: FileMode::new(ie.mode),
4059            old_oid: zero_oid(),
4060            new_oid: ie.oid,
4061            score: None,
4062        });
4063    }
4064
4065    sort_entries_by_path(&mut result);
4066    Ok(result)
4067}
4068
4069enum IndexLevelKind<'a> {
4070    Blob {
4071        entry: &'a IndexEntry,
4072        name: &'a [u8],
4073    },
4074    Tree {
4075        name: &'a [u8],
4076    },
4077}
4078
4079/// `path` relative to the directory `dir_prefix` (raw bytes, no trailing slash), or `None` when
4080/// `path` lies outside it. The directory's own path maps to an empty slice.
4081fn index_relative_in_dir<'a>(path: &'a [u8], dir_prefix: &[u8]) -> Option<&'a [u8]> {
4082    if dir_prefix.is_empty() {
4083        return Some(path);
4084    }
4085    let rest = path.strip_prefix(dir_prefix)?;
4086    match rest.split_first() {
4087        None => Some(rest),
4088        Some((b'/', tail)) => Some(tail),
4089        Some(_) => None,
4090    }
4091}
4092
4093fn join_index_path(dir_prefix: &[u8], name: &[u8]) -> Vec<u8> {
4094    if dir_prefix.is_empty() {
4095        return name.to_vec();
4096    }
4097    let mut path = Vec::with_capacity(dir_prefix.len() + 1 + name.len());
4098    path.extend_from_slice(dir_prefix);
4099    path.push(b'/');
4100    path.extend_from_slice(name);
4101    path
4102}
4103
4104fn peek_index_level_entry<'a>(
4105    index: &'a Index,
4106    cursor: &mut usize,
4107    dir_prefix: &[u8],
4108    ignore_submodules: bool,
4109) -> Option<IndexLevelKind<'a>> {
4110    while let Some(ie) = index.entries.get(*cursor) {
4111        let rel = index_relative_in_dir(&ie.path, dir_prefix)?;
4112        if ie.stage() != 0 || ie.intent_to_add() || rel.is_empty() {
4113            *cursor += 1;
4114            continue;
4115        }
4116        if let Some(slash) = rel.iter().position(|&b| b == b'/') {
4117            return Some(IndexLevelKind::Tree {
4118                name: &rel[..slash],
4119            });
4120        }
4121        if ignore_submodules && ie.mode == 0o160000 {
4122            *cursor += 1;
4123            continue;
4124        }
4125        return Some(IndexLevelKind::Blob {
4126            entry: ie,
4127            name: rel,
4128        });
4129    }
4130    None
4131}
4132
4133fn skip_index_subtree(index: &Index, cursor: &mut usize, dir_prefix: &[u8]) {
4134    while index
4135        .entries
4136        .get(*cursor)
4137        .is_some_and(|ie| index_relative_in_dir(&ie.path, dir_prefix).is_some())
4138    {
4139        *cursor += 1;
4140    }
4141}
4142
4143/// Children of a cache-tree node up to which a linear name scan beats sorting.
4144const CACHE_CHILD_LINEAR_SCAN_MAX: usize = 16;
4145
4146/// Name lookup over one cache-tree node's subtrees.
4147///
4148/// The index extension does not promise any child order, and a wide directory is looked up
4149/// once per tree entry, so nodes with many children are sorted once per visit instead of
4150/// being scanned for every entry. The first child with a matching name wins either way.
4151struct CacheChildren<'a> {
4152    children: &'a [CacheTreeNode],
4153    sorted: Vec<&'a CacheTreeNode>,
4154}
4155
4156impl<'a> CacheChildren<'a> {
4157    fn new(node: Option<&'a CacheTreeNode>) -> Self {
4158        let children = node.map_or(&[][..], |n| n.children.as_slice());
4159        let sorted = if children.len() > CACHE_CHILD_LINEAR_SCAN_MAX {
4160            let mut sorted: Vec<&CacheTreeNode> = children.iter().collect();
4161            sorted.sort_by(|a, b| a.name.cmp(&b.name));
4162            sorted
4163        } else {
4164            Vec::new()
4165        };
4166        Self { children, sorted }
4167    }
4168
4169    fn get(&self, name: &[u8]) -> Option<&'a CacheTreeNode> {
4170        if self.sorted.is_empty() {
4171            return self.children.iter().find(|c| c.name == name);
4172        }
4173        let pos = self.sorted.partition_point(|c| c.name.as_slice() < name);
4174        self.sorted.get(pos).copied().filter(|c| c.name == name)
4175    }
4176}
4177
4178struct DiffIndexTreeWalk<'a, 'b> {
4179    odb: &'a Odb,
4180    index: &'a Index,
4181    idx_cursor: &'b mut usize,
4182    ignore_submodules: bool,
4183    result: &'b mut Vec<DiffEntry>,
4184    /// Set when a tree object has duplicate entry names; the caller falls back to flatten.
4185    abort_to_flatten: bool,
4186}
4187
4188fn tree_entries_have_duplicate_names(entries: &[TreeEntry]) -> bool {
4189    let mut seen = std::collections::BTreeSet::<&[u8]>::new();
4190    for entry in entries {
4191        if !seen.insert(entry.name.as_slice()) {
4192            return true;
4193        }
4194    }
4195    false
4196}
4197
4198/// Walk the tree `tree_oid` against the index entries under `dir_prefix` (raw path bytes).
4199fn diff_index_tree_at(
4200    walk: &mut DiffIndexTreeWalk<'_, '_>,
4201    tree_oid: &ObjectId,
4202    cache_node: Option<&CacheTreeNode>,
4203    dir_prefix: &[u8],
4204) -> Result<()> {
4205    if !dir_prefix.is_empty() {
4206        let mut path = dir_prefix.to_vec();
4207        path.push(b'/');
4208        if let Some(entry) = walk
4209            .index
4210            .entries
4211            .get(*walk.idx_cursor)
4212            .filter(|entry| entry.path == path && entry.is_sparse_directory_placeholder())
4213        {
4214            if entry.oid == *tree_oid {
4215                *walk.idx_cursor += 1;
4216                return Ok(());
4217            }
4218            let dir = String::from_utf8_lossy(dir_prefix);
4219            let head_flat = flatten_tree(walk.odb, tree_oid, dir.as_ref())?;
4220            let index_flat = flatten_tree(walk.odb, &entry.oid, dir.as_ref())?;
4221            let mut stage0_paths = std::collections::BTreeSet::new();
4222            let mut tree_map = std::collections::BTreeMap::new();
4223            diff_flattened_subtrees(
4224                &head_flat,
4225                &index_flat,
4226                walk.ignore_submodules,
4227                walk.result,
4228                &mut stage0_paths,
4229                &mut tree_map,
4230            );
4231            *walk.idx_cursor += 1;
4232            return Ok(());
4233        }
4234    }
4235    let tree_entries = read_tree(walk.odb, tree_oid)?;
4236    if tree_entries_have_duplicate_names(&tree_entries) {
4237        walk.abort_to_flatten = true;
4238        return Ok(());
4239    }
4240    let cache_children = CacheChildren::new(cache_node);
4241    let mut ti = 0usize;
4242
4243    loop {
4244        let te = tree_entries.get(ti);
4245        let ie_kind = peek_index_level_entry(
4246            walk.index,
4247            walk.idx_cursor,
4248            dir_prefix,
4249            walk.ignore_submodules,
4250        );
4251
4252        match (te, ie_kind) {
4253            (Some(t_entry), Some(ie)) => {
4254                let t_name = &t_entry.name;
4255                let (i_name, i_is_tree) = match &ie {
4256                    IndexLevelKind::Blob { name, .. } => (*name, false),
4257                    IndexLevelKind::Tree { name } => (*name, true),
4258                };
4259                let t_is_tree = is_tree_mode(t_entry.mode);
4260                let cmp = crate::objects::tree_name_cmp(t_name, t_is_tree, i_name, i_is_tree);
4261                match cmp {
4262                    std::cmp::Ordering::Less => {
4263                        emit_index_tree_deleted(
4264                            walk.odb,
4265                            t_entry,
4266                            dir_prefix,
4267                            walk.ignore_submodules,
4268                            walk.result,
4269                        )?;
4270                        ti += 1;
4271                    }
4272                    std::cmp::Ordering::Greater => {
4273                        emit_index_tree_added(walk, dir_prefix)?;
4274                    }
4275                    std::cmp::Ordering::Equal => {
4276                        if t_is_tree && i_is_tree {
4277                            let full_path = join_index_path(dir_prefix, t_name);
4278                            if diff_index_tree_skip_sparse_placeholder(walk, &full_path, t_entry)? {
4279                                ti += 1;
4280                                continue;
4281                            }
4282                            let child_cache = cache_children.get(t_name);
4283                            if child_cache
4284                                .is_some_and(|c| c.is_valid() && c.oid == Some(t_entry.oid))
4285                            {
4286                                skip_index_subtree(walk.index, walk.idx_cursor, &full_path);
4287                                ti += 1;
4288                                continue;
4289                            }
4290                            diff_index_tree_at(walk, &t_entry.oid, child_cache, &full_path)?;
4291                        } else if t_is_tree != i_is_tree {
4292                            emit_index_tree_deleted(
4293                                walk.odb,
4294                                t_entry,
4295                                dir_prefix,
4296                                walk.ignore_submodules,
4297                                walk.result,
4298                            )?;
4299                            emit_index_tree_added(walk, dir_prefix)?;
4300                        } else {
4301                            let IndexLevelKind::Blob { entry: ie, .. } = ie else {
4302                                ti += 1;
4303                                continue;
4304                            };
4305                            *walk.idx_cursor += 1;
4306                            if t_entry.oid != ie.oid
4307                                || crate::index::canon_mode(t_entry.mode)
4308                                    != crate::index::canon_mode(ie.mode)
4309                            {
4310                                let path = String::from_utf8_lossy(&ie.path).into_owned();
4311                                let tree_mode = crate::index::canon_mode(t_entry.mode);
4312                                let index_mode = crate::index::canon_mode(ie.mode);
4313                                walk.result.push(DiffEntry {
4314                                    status: DiffStatus::for_content_change(tree_mode, index_mode),
4315                                    old_path: Some(path.clone()),
4316                                    new_path: Some(path),
4317                                    old_mode: FileMode::new(tree_mode),
4318                                    new_mode: FileMode::new(index_mode),
4319                                    old_oid: t_entry.oid,
4320                                    new_oid: ie.oid,
4321                                    score: None,
4322                                });
4323                            }
4324                        }
4325                        ti += 1;
4326                    }
4327                }
4328            }
4329            (Some(t_entry), None) => {
4330                emit_index_tree_deleted(
4331                    walk.odb,
4332                    t_entry,
4333                    dir_prefix,
4334                    walk.ignore_submodules,
4335                    walk.result,
4336                )?;
4337                ti += 1;
4338            }
4339            (None, Some(_)) => {
4340                emit_index_tree_added(walk, dir_prefix)?;
4341            }
4342            (None, None) => break,
4343        }
4344    }
4345
4346    Ok(())
4347}
4348
4349fn diff_index_tree_skip_sparse_placeholder(
4350    walk: &mut DiffIndexTreeWalk<'_, '_>,
4351    full_path: &[u8],
4352    t_entry: &TreeEntry,
4353) -> Result<bool> {
4354    let Some(entry) = walk.index.entries.get(*walk.idx_cursor) else {
4355        return Ok(false);
4356    };
4357    if !entry.is_sparse_directory_placeholder() {
4358        return Ok(false);
4359    }
4360    let mut dir = full_path.to_vec();
4361    if !dir.ends_with(b"/") {
4362        dir.push(b'/');
4363    }
4364    if entry.path != dir || entry.oid != t_entry.oid {
4365        return Ok(false);
4366    }
4367    *walk.idx_cursor += 1;
4368    Ok(true)
4369}
4370
4371fn emit_index_tree_deleted(
4372    odb: &Odb,
4373    entry: &TreeEntry,
4374    dir_prefix: &[u8],
4375    ignore_submodules: bool,
4376    result: &mut Vec<DiffEntry>,
4377) -> Result<()> {
4378    if ignore_submodules && entry.mode == 0o160000 {
4379        return Ok(());
4380    }
4381    let path = String::from_utf8_lossy(&join_index_path(dir_prefix, &entry.name)).into_owned();
4382    if is_tree_mode(entry.mode) {
4383        for fe in flatten_tree(odb, &entry.oid, &path)? {
4384            if ignore_submodules && fe.mode == 0o160000 {
4385                continue;
4386            }
4387            result.push(DiffEntry {
4388                status: DiffStatus::Deleted,
4389                old_path: Some(fe.path.clone()),
4390                new_path: None,
4391                old_mode: FileMode::new(fe.mode),
4392                new_mode: FileMode::ABSENT,
4393                old_oid: fe.oid,
4394                new_oid: zero_oid(),
4395                score: None,
4396            });
4397        }
4398    } else {
4399        result.push(DiffEntry {
4400            status: DiffStatus::Deleted,
4401            old_path: Some(path),
4402            new_path: None,
4403            old_mode: FileMode::new(entry.mode),
4404            new_mode: FileMode::ABSENT,
4405            old_oid: entry.oid,
4406            new_oid: zero_oid(),
4407            score: None,
4408        });
4409    }
4410    Ok(())
4411}
4412
4413fn emit_index_tree_added(walk: &mut DiffIndexTreeWalk<'_, '_>, dir_prefix: &[u8]) -> Result<()> {
4414    let ie_kind = peek_index_level_entry(
4415        walk.index,
4416        walk.idx_cursor,
4417        dir_prefix,
4418        walk.ignore_submodules,
4419    )
4420    .ok_or_else(|| {
4421        Error::from(IndexError::corrupt(IndexCorrupt::new(
4422            IndexCorruptReason::MissingIndexEntryDuringTreeWalk,
4423        )))
4424    })?;
4425    match ie_kind {
4426        IndexLevelKind::Blob { entry: ie, .. } => {
4427            let path = String::from_utf8_lossy(&ie.path).into_owned();
4428            *walk.idx_cursor += 1;
4429            walk.result.push(DiffEntry {
4430                status: DiffStatus::Added,
4431                old_path: None,
4432                new_path: Some(path),
4433                old_mode: FileMode::ABSENT,
4434                new_mode: FileMode::new(ie.mode),
4435                old_oid: zero_oid(),
4436                new_oid: ie.oid,
4437                score: None,
4438            });
4439        }
4440        IndexLevelKind::Tree { name } => {
4441            let full_path = join_index_path(dir_prefix, name);
4442            let mut path_slash = full_path.clone();
4443            path_slash.push(b'/');
4444            if let Some(entry) =
4445                walk.index.entries.get(*walk.idx_cursor).filter(|entry| {
4446                    entry.path == path_slash && entry.is_sparse_directory_placeholder()
4447                })
4448            {
4449                let dir = String::from_utf8_lossy(if dir_prefix.is_empty() {
4450                    name
4451                } else {
4452                    &full_path
4453                });
4454                emit_flattened_subtree_as_added(
4455                    walk.odb,
4456                    &entry.oid,
4457                    dir.as_ref(),
4458                    walk.ignore_submodules,
4459                    walk.result,
4460                )?;
4461                *walk.idx_cursor += 1;
4462                return Ok(());
4463            }
4464            let start = *walk.idx_cursor;
4465            skip_index_subtree(walk.index, walk.idx_cursor, &full_path);
4466            for ie in &walk.index.entries[start..*walk.idx_cursor] {
4467                if ie.stage() != 0 || ie.intent_to_add() {
4468                    continue;
4469                }
4470                if walk.ignore_submodules && ie.mode == 0o160000 {
4471                    continue;
4472                }
4473                let path = String::from_utf8_lossy(&ie.path).into_owned();
4474                walk.result.push(DiffEntry {
4475                    status: DiffStatus::Added,
4476                    old_path: None,
4477                    new_path: Some(path),
4478                    old_mode: FileMode::ABSENT,
4479                    new_mode: FileMode::new(ie.mode),
4480                    old_oid: zero_oid(),
4481                    new_oid: ie.oid,
4482                    score: None,
4483                });
4484            }
4485        }
4486    }
4487    Ok(())
4488}
4489
4490/// Paths and rows to reconcile an index with a target tree during hard reset.
4491#[derive(Default)]
4492pub(crate) struct HardResetIndexPlan {
4493    pub remove_paths: Vec<Vec<u8>>,
4494    pub updates: Vec<(Vec<u8>, u32, ObjectId)>,
4495    pub insert_entries: Vec<IndexEntry>,
4496}
4497
4498/// Plan index changes to match `target_tree` using a cache-tree-aware lockstep walk (raw paths).
4499///
4500/// Never builds a flattened tree map; when the cache-tree extension is absent, walks the full tree
4501/// against the index instead of calling [`diff_index_to_tree_flatten`].
4502pub(crate) fn plan_hard_reset_index_align(
4503    odb: &Odb,
4504    index: &Index,
4505    target_tree: &ObjectId,
4506    sparse: Option<&SparseCheckout>,
4507    index_sparse: bool,
4508) -> Result<HardResetIndexPlan> {
4509    let mut plan = HardResetIndexPlan::default();
4510    let mut idx_cursor = 0usize;
4511    let cache_root = index.cache_tree.as_ref();
4512    let use_cache = cache_root.is_some()
4513        && index
4514            .entries
4515            .iter()
4516            .all(|entry| entry.stage() == 0 && !entry.intent_to_add());
4517    let cache_node = if use_cache { cache_root } else { None };
4518    let retry = {
4519        let mut walk = HardResetAlignWalk {
4520            odb,
4521            index,
4522            idx_cursor: &mut idx_cursor,
4523            ignore_submodules: false,
4524            sparse,
4525            index_sparse,
4526            plan: &mut plan,
4527            retry_without_cache: false,
4528        };
4529        hard_reset_align_index_tree_at(&mut walk, target_tree, cache_node, b"")?;
4530        walk.retry_without_cache
4531    };
4532    if retry {
4533        idx_cursor = 0;
4534        plan = HardResetIndexPlan::default();
4535        let mut walk = HardResetAlignWalk {
4536            odb,
4537            index,
4538            idx_cursor: &mut idx_cursor,
4539            ignore_submodules: false,
4540            sparse,
4541            index_sparse,
4542            plan: &mut plan,
4543            retry_without_cache: false,
4544        };
4545        hard_reset_align_index_tree_at(&mut walk, target_tree, None, b"")?;
4546    }
4547    while idx_cursor < index.entries.len() {
4548        let ie = &index.entries[idx_cursor];
4549        idx_cursor += 1;
4550        if ie.stage() != 0 || ie.intent_to_add() {
4551            continue;
4552        }
4553        plan.remove_paths.push(ie.path.clone());
4554    }
4555    Ok(plan)
4556}
4557
4558/// Apply a [`HardResetIndexPlan`] and invalidate cache-tree nodes for touched paths.
4559pub(crate) fn apply_hard_reset_index_plan(index: &mut Index, plan: HardResetIndexPlan) {
4560    let change_count = plan
4561        .remove_paths
4562        .len()
4563        .saturating_add(plan.updates.len())
4564        .saturating_add(plan.insert_entries.len());
4565    const BULK_HARD_RESET_INDEX_MIN: usize = 64;
4566    let bulk = change_count >= BULK_HARD_RESET_INDEX_MIN;
4567    if bulk {
4568        index.drop_derived_index_extensions();
4569    }
4570    for (path, mode, oid) in plan.updates {
4571        if let Some(pos) = index.find_entry_pos(&path, 0) {
4572            let entry = &mut index.entries[pos];
4573            let content_unchanged = entry.oid == oid && entry.mode == mode;
4574            entry.mode = mode;
4575            entry.oid = oid;
4576            if !content_unchanged {
4577                entry.size = 0;
4578            }
4579            if !bulk {
4580                index.invalidate_cache_tree_for_path(&path);
4581            }
4582        }
4583    }
4584    index.defer_untracked_cache_invalidation = bulk;
4585    index.remove_prefixes_paths_and_insert(
4586        &std::collections::BTreeSet::new(),
4587        plan.remove_paths.iter().map(|p| p.as_slice()),
4588        plan.insert_entries,
4589    );
4590    index.defer_untracked_cache_invalidation = false;
4591    if !bulk {
4592        for path in &plan.remove_paths {
4593            index.invalidate_cache_tree_for_path(path);
4594        }
4595    }
4596    index.sort();
4597}
4598
4599/// Rebuild the cache-tree extension after hard-reset index alignment, reusing valid seed
4600/// subtrees from the lockstep tree/index walk.
4601pub(crate) fn rebuild_hard_reset_cache_tree(
4602    odb: &Odb,
4603    index: &mut Index,
4604    target_tree: &ObjectId,
4605) -> Result<()> {
4606    let stage0_count = index
4607        .entries
4608        .iter()
4609        .filter(|entry| entry.stage() == 0 && !entry.intent_to_add())
4610        .count();
4611    let seed_root = index.cache_tree.as_ref();
4612    let mut idx_cursor = 0usize;
4613    let root = rebuild_hard_reset_cache_tree_at(
4614        odb,
4615        index,
4616        &mut idx_cursor,
4617        target_tree,
4618        seed_root,
4619        b"",
4620        Vec::new(),
4621    )?;
4622    if usize::try_from(root.entry_count).ok() == Some(stage0_count) {
4623        index.set_cache_tree(root);
4624        Ok(())
4625    } else {
4626        Err(IndexError::corrupt(IndexCorrupt::new(
4627            IndexCorruptReason::HardResetCacheTreeEntryCountMismatch,
4628        ))
4629        .into())
4630    }
4631}
4632
4633fn rebuild_hard_reset_cache_tree_at(
4634    odb: &Odb,
4635    index: &Index,
4636    idx_cursor: &mut usize,
4637    tree_oid: &ObjectId,
4638    seed_node: Option<&CacheTreeNode>,
4639    dir_prefix: &[u8],
4640    name: Vec<u8>,
4641) -> Result<CacheTreeNode> {
4642    if !dir_prefix.is_empty() {
4643        let mut path = dir_prefix.to_vec();
4644        path.push(b'/');
4645        if let Some(entry) = index.entries.get(*idx_cursor).filter(|entry| {
4646            entry.path == path && entry.is_sparse_directory_placeholder() && entry.oid == *tree_oid
4647        }) {
4648            let _ = entry;
4649            *idx_cursor += 1;
4650            return Ok(CacheTreeNode::valid(name, 1, *tree_oid, Vec::new()));
4651        }
4652    }
4653    let tree_entries = read_tree(odb, tree_oid)?;
4654    let cache_children = CacheChildren::new(seed_node);
4655    let mut entry_count: i32 = 0;
4656    let mut children: Vec<CacheTreeNode> = Vec::new();
4657    let mut ti = 0usize;
4658
4659    loop {
4660        let te = tree_entries.get(ti);
4661        let ie_kind = peek_index_level_entry(index, idx_cursor, dir_prefix, false);
4662        match (te, ie_kind) {
4663            (Some(t_entry), Some(ie)) => {
4664                let t_name = &t_entry.name;
4665                let (i_name, i_is_tree) = match &ie {
4666                    IndexLevelKind::Blob { name, .. } => (*name, false),
4667                    IndexLevelKind::Tree { name } => (*name, true),
4668                };
4669                let t_is_tree = is_tree_mode(t_entry.mode);
4670                let cmp = crate::objects::tree_name_cmp(t_name, t_is_tree, i_name, i_is_tree);
4671                if cmp != std::cmp::Ordering::Equal {
4672                    return Err(IndexError::corrupt(IndexCorrupt::new(
4673                        IndexCorruptReason::HardResetCacheTreeOutOfSync,
4674                    ))
4675                    .into());
4676                }
4677                if t_is_tree && i_is_tree {
4678                    let full_path = join_index_path(dir_prefix, t_name);
4679                    if hard_reset_skip_sparse_cache_rebuild(index, idx_cursor, &full_path, t_entry)?
4680                    {
4681                        entry_count = entry_count.saturating_add(1);
4682                        ti += 1;
4683                        continue;
4684                    }
4685                    let child = if let Some(child_cache) = cache_children
4686                        .get(t_name)
4687                        .filter(|node| node.is_valid() && node.oid == Some(t_entry.oid))
4688                    {
4689                        skip_index_subtree(index, idx_cursor, &full_path);
4690                        let mut reused = (*child_cache).clone();
4691                        reused.name = t_name.clone();
4692                        reused
4693                    } else {
4694                        rebuild_hard_reset_cache_tree_at(
4695                            odb,
4696                            index,
4697                            idx_cursor,
4698                            &t_entry.oid,
4699                            cache_children.get(t_name),
4700                            &full_path,
4701                            t_name.clone(),
4702                        )?
4703                    };
4704                    entry_count = entry_count.saturating_add(child.entry_count.max(0));
4705                    children.push(child);
4706                } else if t_is_tree != i_is_tree {
4707                    return Err(IndexError::corrupt(IndexCorrupt::new(
4708                        IndexCorruptReason::HardResetCacheTreeTypeMismatch,
4709                    ))
4710                    .into());
4711                } else {
4712                    *idx_cursor += 1;
4713                    entry_count = entry_count.saturating_add(1);
4714                }
4715                ti += 1;
4716            }
4717            (None, None) => break,
4718            _ => {
4719                return Err(IndexError::corrupt(IndexCorrupt::new(
4720                    IndexCorruptReason::HardResetCacheTreeOutOfSync,
4721                ))
4722                .into());
4723            }
4724        }
4725    }
4726    children.sort_by(|a, b| a.name.cmp(&b.name));
4727    Ok(CacheTreeNode::valid(name, entry_count, *tree_oid, children))
4728}
4729
4730fn hard_reset_skip_sparse_cache_rebuild(
4731    index: &Index,
4732    idx_cursor: &mut usize,
4733    full_path: &[u8],
4734    t_entry: &TreeEntry,
4735) -> Result<bool> {
4736    let Some(entry) = index.entries.get(*idx_cursor) else {
4737        return Ok(false);
4738    };
4739    if !entry.is_sparse_directory_placeholder() {
4740        return Ok(false);
4741    }
4742    let mut dir = full_path.to_vec();
4743    if !dir.ends_with(b"/") {
4744        dir.push(b'/');
4745    }
4746    if entry.path != dir || entry.oid != t_entry.oid {
4747        return Ok(false);
4748    }
4749    *idx_cursor += 1;
4750    Ok(true)
4751}
4752
4753struct HardResetAlignWalk<'a, 'b> {
4754    odb: &'a Odb,
4755    index: &'a Index,
4756    idx_cursor: &'b mut usize,
4757    ignore_submodules: bool,
4758    sparse: Option<&'a SparseCheckout>,
4759    index_sparse: bool,
4760    plan: &'b mut HardResetIndexPlan,
4761    retry_without_cache: bool,
4762}
4763
4764fn hard_reset_align_index_tree_at(
4765    walk: &mut HardResetAlignWalk<'_, '_>,
4766    tree_oid: &ObjectId,
4767    cache_node: Option<&CacheTreeNode>,
4768    dir_prefix: &[u8],
4769) -> Result<()> {
4770    let tree_entries = read_tree(walk.odb, tree_oid)?;
4771    if tree_entries_have_duplicate_names(&tree_entries) {
4772        walk.retry_without_cache = true;
4773        return Ok(());
4774    }
4775    let cache_children = CacheChildren::new(cache_node);
4776    let mut ti = 0usize;
4777
4778    loop {
4779        let te = tree_entries.get(ti);
4780        let ie_kind = peek_index_level_entry(
4781            walk.index,
4782            walk.idx_cursor,
4783            dir_prefix,
4784            walk.ignore_submodules,
4785        );
4786
4787        match (te, ie_kind) {
4788            (Some(t_entry), Some(ie)) => {
4789                let t_name = &t_entry.name;
4790                let (i_name, i_is_tree) = match &ie {
4791                    IndexLevelKind::Blob { name, .. } => (*name, false),
4792                    IndexLevelKind::Tree { name } => (*name, true),
4793                };
4794                let t_is_tree = is_tree_mode(t_entry.mode);
4795                let cmp = crate::objects::tree_name_cmp(t_name, t_is_tree, i_name, i_is_tree);
4796                match cmp {
4797                    std::cmp::Ordering::Less => {
4798                        hard_reset_plan_tree_only(walk, t_entry, dir_prefix)?;
4799                        ti += 1;
4800                    }
4801                    std::cmp::Ordering::Greater => {
4802                        hard_reset_plan_index_only(walk, dir_prefix)?;
4803                    }
4804                    std::cmp::Ordering::Equal => {
4805                        if t_is_tree && i_is_tree {
4806                            let full_path = join_index_path(dir_prefix, t_name);
4807                            if hard_reset_skip_sparse_placeholder(walk, &full_path, t_entry)? {
4808                                ti += 1;
4809                                continue;
4810                            }
4811                            let child_cache = cache_children.get(t_name);
4812                            if child_cache
4813                                .is_some_and(|c| c.is_valid() && c.oid == Some(t_entry.oid))
4814                            {
4815                                skip_index_subtree(walk.index, walk.idx_cursor, &full_path);
4816                                ti += 1;
4817                                continue;
4818                            }
4819                            hard_reset_align_index_tree_at(
4820                                walk,
4821                                &t_entry.oid,
4822                                child_cache,
4823                                &full_path,
4824                            )?;
4825                        } else if t_is_tree != i_is_tree {
4826                            hard_reset_plan_tree_only(walk, t_entry, dir_prefix)?;
4827                            hard_reset_plan_index_only(walk, dir_prefix)?;
4828                        } else {
4829                            let IndexLevelKind::Blob { entry: ie, .. } = ie else {
4830                                ti += 1;
4831                                continue;
4832                            };
4833                            *walk.idx_cursor += 1;
4834                            if t_entry.oid != ie.oid
4835                                || crate::index::canon_mode(t_entry.mode)
4836                                    != crate::index::canon_mode(ie.mode)
4837                            {
4838                                walk.plan.updates.push((
4839                                    ie.path.clone(),
4840                                    crate::index::canon_mode(t_entry.mode),
4841                                    t_entry.oid,
4842                                ));
4843                            }
4844                        }
4845                        ti += 1;
4846                    }
4847                }
4848            }
4849            (Some(t_entry), None) => {
4850                hard_reset_plan_tree_only(walk, t_entry, dir_prefix)?;
4851                ti += 1;
4852            }
4853            (None, Some(_)) => {
4854                hard_reset_plan_index_only(walk, dir_prefix)?;
4855            }
4856            (None, None) => break,
4857        }
4858    }
4859    Ok(())
4860}
4861
4862fn hard_reset_skip_sparse_placeholder(
4863    walk: &mut HardResetAlignWalk<'_, '_>,
4864    full_path: &[u8],
4865    t_entry: &TreeEntry,
4866) -> Result<bool> {
4867    let Some(ie) = walk.index.entries.get(*walk.idx_cursor) else {
4868        return Ok(false);
4869    };
4870    if !ie.is_sparse_directory_placeholder() {
4871        return Ok(false);
4872    }
4873    let mut dir = full_path.to_vec();
4874    if !dir.ends_with(b"/") {
4875        dir.push(b'/');
4876    }
4877    if ie.path != dir || ie.oid != t_entry.oid {
4878        return Ok(false);
4879    }
4880    *walk.idx_cursor += 1;
4881    Ok(true)
4882}
4883
4884fn hard_reset_plan_index_only(
4885    walk: &mut HardResetAlignWalk<'_, '_>,
4886    dir_prefix: &[u8],
4887) -> Result<()> {
4888    let ie_kind = peek_index_level_entry(
4889        walk.index,
4890        walk.idx_cursor,
4891        dir_prefix,
4892        walk.ignore_submodules,
4893    )
4894    .ok_or_else(|| {
4895        Error::from(IndexError::corrupt(IndexCorrupt::new(
4896            IndexCorruptReason::MissingIndexEntryDuringHardReset,
4897        )))
4898    })?;
4899    match ie_kind {
4900        IndexLevelKind::Blob { entry: ie, .. } => {
4901            walk.plan.remove_paths.push(ie.path.clone());
4902            *walk.idx_cursor += 1;
4903        }
4904        IndexLevelKind::Tree { name } => {
4905            let full_path = join_index_path(dir_prefix, name);
4906            let start = *walk.idx_cursor;
4907            skip_index_subtree(walk.index, walk.idx_cursor, &full_path);
4908            for ie in &walk.index.entries[start..*walk.idx_cursor] {
4909                if ie.stage() != 0 || ie.intent_to_add() {
4910                    continue;
4911                }
4912                walk.plan.remove_paths.push(ie.path.clone());
4913            }
4914        }
4915    }
4916    Ok(())
4917}
4918
4919fn hard_reset_plan_tree_only(
4920    walk: &mut HardResetAlignWalk<'_, '_>,
4921    entry: &TreeEntry,
4922    dir_prefix: &[u8],
4923) -> Result<()> {
4924    if walk.ignore_submodules && entry.mode == 0o160000 {
4925        return Ok(());
4926    }
4927    let path = join_index_path(dir_prefix, &entry.name);
4928    if is_tree_mode(entry.mode) {
4929        if hard_reset_sparse_excludes_tree(walk, &path) {
4930            walk.plan
4931                .insert_entries
4932                .push(hard_reset_sparse_directory_row(path, entry.oid));
4933        } else {
4934            hard_reset_append_entries_from_tree(
4935                walk.odb,
4936                &entry.oid,
4937                &path,
4938                &mut walk.plan.insert_entries,
4939            )?;
4940        }
4941    } else {
4942        walk.plan
4943            .insert_entries
4944            .push(hard_reset_index_tree_row(path, entry.mode, entry.oid));
4945    }
4946    Ok(())
4947}
4948
4949fn hard_reset_sparse_excludes_tree(walk: &HardResetAlignWalk<'_, '_>, path: &[u8]) -> bool {
4950    if !walk.index_sparse {
4951        return false;
4952    }
4953    let Some(sparse) = walk.sparse else {
4954        return false;
4955    };
4956    let mut dir = path.to_vec();
4957    if !dir.ends_with(b"/") {
4958        dir.push(b'/');
4959    }
4960    sparse.excludes_directory(&dir)
4961}
4962
4963fn hard_reset_sparse_directory_row(mut path: Vec<u8>, oid: ObjectId) -> IndexEntry {
4964    if !path.ends_with(b"/") {
4965        path.push(b'/');
4966    }
4967    let mut row = hard_reset_index_tree_row(path, MODE_TREE, oid);
4968    row.set_skip_worktree(true);
4969    row
4970}
4971
4972fn hard_reset_index_tree_row(path: Vec<u8>, mode: u32, oid: ObjectId) -> IndexEntry {
4973    IndexEntry {
4974        ctime_sec: 0,
4975        ctime_nsec: 0,
4976        mtime_sec: 0,
4977        mtime_nsec: 0,
4978        dev: 0,
4979        ino: 0,
4980        mode: crate::index::canon_mode(mode),
4981        uid: 0,
4982        gid: 0,
4983        size: 0,
4984        oid,
4985        flags: path.len().min(0xFFF) as u16,
4986        flags_extended: None,
4987        path,
4988        base_index_pos: 0,
4989    }
4990}
4991
4992fn hard_reset_append_entries_from_tree(
4993    odb: &Odb,
4994    tree_oid: &ObjectId,
4995    prefix: &[u8],
4996    out: &mut Vec<IndexEntry>,
4997) -> Result<()> {
4998    for te in read_tree(odb, tree_oid)? {
4999        let mut path = prefix.to_vec();
5000        if !path.is_empty() {
5001            path.push(b'/');
5002        }
5003        path.extend_from_slice(&te.name);
5004        if is_tree_mode(te.mode) {
5005            hard_reset_append_entries_from_tree(odb, &te.oid, &path, out)?;
5006        } else {
5007            out.push(hard_reset_index_tree_row(path, te.mode, te.oid));
5008        }
5009    }
5010    Ok(())
5011}
5012
5013// ── Index-to-worktree diff (unstaged changes) ───────────────────────
5014
5015/// Compare the index against the working tree.
5016///
5017/// This shows "unstaged" changes — modifications not yet staged.
5018///
5019/// Entries with [`IndexEntry::assume_unchanged`] or [`IndexEntry::skip_worktree`] are treated as
5020/// matching the work tree without examining the filesystem (Git `CE_VALID` / skip-worktree).
5021///
5022/// # Parameters
5023///
5024/// - `odb` — object database (for hashing worktree files).
5025/// - `index` — the current index.
5026/// - `work_tree` — path to the working tree root.
5027/// - `ignore_submodule_untracked` — when true, gitlink entries are not dirty solely from untracked
5028///   files inside the submodule (matches `git status -uno`).
5029/// - `simplify_gitlinks` — when true, nested gitlink entries only compare the submodule checkout
5030///   HEAD to the recorded OID (ignore dirty work trees inside nested submodules). Used when
5031///   computing `submodule_porcelain_flags` so untracked files under a nested submodule do not set
5032///   the parent submodule's `modified` bit (Git `DIRTY_SUBMODULE_MODIFIED`; t7506).
5033///
5034/// # Errors
5035///
5036/// Returns errors from I/O or hashing.
5037pub(crate) fn diff_index_to_worktree(
5038    odb: &Odb,
5039    index: &mut Index,
5040    work_tree: &Path,
5041    ignore_submodule_untracked: bool,
5042    simplify_gitlinks: bool,
5043) -> Result<Vec<DiffEntry>> {
5044    diff_index_to_worktree_with_options(
5045        odb,
5046        index,
5047        work_tree,
5048        DiffIndexToWorktreeOptions {
5049            ignore_submodule_untracked,
5050            simplify_gitlinks,
5051            ..DiffIndexToWorktreeOptions::default()
5052        },
5053    )
5054    .map(|(entries, _)| entries)
5055}
5056
5057/// Predicate over stored index path bytes for [`DiffIndexToWorktreeOptions::skip_index_path`].
5058pub(crate) type IndexPathSkipPredicate = std::sync::Arc<dyn Fn(&[u8]) -> bool + Send + Sync>;
5059
5060/// Additional inputs for `diff_index_to_worktree_with_options`.
5061#[derive(Clone)]
5062pub(crate) struct DiffIndexToWorktreeOptions {
5063    /// Optional index mtime pair `(sec, nsec)` sampled when the index was read.
5064    ///
5065    /// When provided, entries with matching stat data are still considered dirty candidates if
5066    /// their recorded mtime is "racy" (at or after this timestamp), matching Git's
5067    /// `is_racy_timestamp` behavior.
5068    pub index_mtime: Option<(u32, u32)>,
5069    /// When true, gitlink entries are not dirty solely from untracked files inside the submodule.
5070    pub ignore_submodule_untracked: bool,
5071    /// When true, nested gitlink entries only compare the submodule checkout HEAD to the recorded OID.
5072    pub simplify_gitlinks: bool,
5073    /// When true, a populated gitlink checkout whose `.git` indirection cannot resolve to a HEAD
5074    /// is returned as an error instead of a normal modified gitlink.
5075    pub error_on_broken_gitlinks: bool,
5076    /// Repository git directory used to load config (separate git dir / linked worktrees).
5077    ///
5078    /// When unset, defaults to `work_tree/.git`, which is wrong for gitfile layouts.
5079    pub repository_git_dir: Option<std::path::PathBuf>,
5080    /// When true, adopt refreshed stat data for unchanged blobs during the same directory scan
5081    /// as the worktree diff (one lstat per tracked path).
5082    pub refresh_index_stat_in_pass: bool,
5083    /// When set, used instead of loading config from [`Self::repository_git_dir`].
5084    pub config: Option<std::sync::Arc<crate::config::ConfigSet>>,
5085    /// Cap parallel stat workers (`Some(1)` forces serial), also applied to hashing the
5086    /// racily clean entries the stat refresh verifies. When `None`, honors
5087    /// `core.preloadIndex` and `PARALLEL_STAT_MIN_ENTRIES`.
5088    pub stat_parallel_threads: Option<usize>,
5089    /// CLI / embedder thread override applied to stat preload and hashing in this diff.
5090    pub thread_policy: Option<crate::parallel::ThreadPolicy>,
5091    /// Shared per-operation attribute/conversion context (see [`crate::worktree_rules::WorktreeRules`]).
5092    pub worktree_rules:
5093        Option<std::sync::Arc<std::sync::Mutex<crate::worktree_rules::WorktreeRules>>>,
5094    /// Status-only shortcuts (default `false`).
5095    ///
5096    /// When true, a tracked file whose size differs from its index entry is reported as
5097    /// modified without reading it, with a null worktree `new_oid`, and intent-to-add
5098    /// entries are not hashed. Content diffs must leave this off so a content change always
5099    /// carries a null worktree `new_oid` (`old_oid != new_oid`).
5100    pub for_status: bool,
5101    /// Report a tracked file as modified without reading it when its size differs from the
5102    /// index entry and its clean conversion is the identity (default `false`, implied by
5103    /// [`Self::for_status`]).
5104    ///
5105    /// The entry carries a null worktree `new_oid`, so `old_oid != new_oid` still marks a
5106    /// content change. Set this when the caller reads the worktree file afterwards anyway (a
5107    /// patch or diffstat), so hashing it here would only repeat that read.
5108    pub size_change_proves_modified: bool,
5109    /// When true (default), query `core.fsmonitor` before scanning the worktree.
5110    pub refresh_fsmonitor: bool,
5111    /// Injectable subprocess runner for fsmonitor hooks; defaults to the system runner.
5112    pub command_runner: Option<std::sync::Arc<dyn crate::command_runner::CommandRunner>>,
5113    /// When set, used as the hook protocol v1 query boundary; otherwise the wall-clock nanosecond timestamp is sampled once per diff.
5114    pub fsmonitor_query_boundary: Option<crate::fsmonitor::FsmonitorQueryBoundary>,
5115    /// When set, only index paths in this set are compared against the work tree.
5116    ///
5117    /// Used by branch switch to verify dirty paths only where the tree-to-tree diff would
5118    /// update the worktree, avoiding a full tracked scan on large indexes. Callers must
5119    /// refuse the operation when the index has unmerged entries; this filter does not
5120    /// substitute for a full unmerged scan.
5121    pub restrict_to_paths: Option<std::sync::Arc<std::collections::BTreeSet<String>>>,
5122    /// When set, index paths for which this returns true are omitted from the worktree diff.
5123    pub skip_index_path: Option<IndexPathSkipPredicate>,
5124    /// When true with staging materialization, compute blob OIDs without writing loose objects.
5125    pub hash_blobs_without_write: bool,
5126    /// When set, long loops abort with [`crate::error::Error::Cancelled`] once cancelled.
5127    pub cancel: Option<crate::progress::CancelToken>,
5128    /// When set, work-tree access failures during status emit [`crate::diagnostics::Warning`]
5129    /// values here instead of aborting the diff.
5130    pub diagnostics: Option<crate::diagnostics::DiagnosticsHandle>,
5131    /// When true with [`Self::diagnostics`], permission-denied worktree paths warn and are omitted
5132    /// from the diff (not reported as deleted). Used by content diffs and stash; status sets
5133    /// [`Self::for_status`] instead.
5134    pub skip_inaccessible_paths: bool,
5135    /// Shared Windows directory listings for one status pass (issue #2566).
5136    pub listing_cache: Option<std::sync::Arc<crate::windows_dir_listing::WorktreeListingCache>>,
5137}
5138
5139impl Default for DiffIndexToWorktreeOptions {
5140    fn default() -> Self {
5141        Self {
5142            index_mtime: None,
5143            ignore_submodule_untracked: false,
5144            simplify_gitlinks: false,
5145            error_on_broken_gitlinks: false,
5146            repository_git_dir: None,
5147            refresh_index_stat_in_pass: false,
5148            config: None,
5149            stat_parallel_threads: None,
5150            thread_policy: None,
5151            worktree_rules: None,
5152            for_status: false,
5153            size_change_proves_modified: false,
5154            refresh_fsmonitor: true,
5155            command_runner: None,
5156            fsmonitor_query_boundary: None,
5157            restrict_to_paths: None,
5158            skip_index_path: None,
5159            hash_blobs_without_write: false,
5160            cancel: None,
5161            diagnostics: None,
5162            skip_inaccessible_paths: false,
5163            listing_cache: None,
5164        }
5165    }
5166}
5167
5168/// Optional worktree access policy for [`diff_tree_to_worktree_with_git_dir_and_rules`]'s slow path.
5169#[derive(Clone, Default)]
5170pub(crate) struct DiffTreeToWorktreeExtras {
5171    /// When true with [`Self::diagnostics`], skip permission-denied paths with a warning.
5172    pub skip_inaccessible_paths: bool,
5173    /// Warning sink paired with [`Self::skip_inaccessible_paths`].
5174    pub diagnostics: Option<crate::diagnostics::DiagnosticsHandle>,
5175    /// Limit the walk to these index paths (expanded pathspecs).
5176    pub restrict_to_paths: Option<std::sync::Arc<std::collections::BTreeSet<String>>>,
5177}
5178
5179#[inline]
5180fn worktree_diff_skips_inaccessible_paths(
5181    for_status: bool,
5182    skip_inaccessible_paths: bool,
5183    materialize_dirty_blobs: bool,
5184) -> bool {
5185    (for_status || skip_inaccessible_paths) && !materialize_dirty_blobs
5186}
5187
5188#[inline]
5189fn poll_diff_cancel_token(
5190    cancel: Option<&crate::progress::CancelToken>,
5191) -> crate::error::Result<()> {
5192    if let Some(token) = cancel {
5193        token.poll_result()?;
5194    }
5195    Ok(())
5196}
5197
5198#[inline]
5199fn poll_diff_cancel(
5200    options: &DiffIndexToWorktreeOptions,
5201    owner_poll: Option<&std::sync::Arc<crate::progress::StackCancelPoll>>,
5202) -> crate::error::Result<()> {
5203    if let Some(poll) = owner_poll {
5204        poll.poll_owner()?;
5205    } else {
5206        poll_diff_cancel_token(options.cancel.as_ref())?;
5207    }
5208    Ok(())
5209}
5210
5211/// One tracked file the parallel pass must hash, queued once per stat-dirty index entry.
5212///
5213/// A stale index can queue every entry, so the job names its entry by position instead of
5214/// owning copies of it, and the workers rebuild paths from the shared index.
5215struct StagingHashJob {
5216    entry_index: usize,
5217    /// Worktree mode after [`crate::index::WorktreeModePolicy`] against the entry's mode.
5218    worktree_mode: u32,
5219    /// `None` when the path has default attributes, which is the common case.
5220    file_attrs: Option<Box<crate::crlf::FileAttrs>>,
5221}
5222
5223impl StagingHashJob {
5224    fn new(entry_index: usize, worktree_mode: u32, file_attrs: crate::crlf::FileAttrs) -> Self {
5225        let file_attrs =
5226            (file_attrs != crate::crlf::FileAttrs::default()).then(|| Box::new(file_attrs));
5227        Self {
5228            entry_index,
5229            worktree_mode,
5230            file_attrs,
5231        }
5232    }
5233}
5234
5235/// Where the parallel pass finds the index entries and files its jobs name.
5236struct StagingHashSource<'a> {
5237    index: &'a Index,
5238    work_tree: &'a Path,
5239    precompose_unicode: bool,
5240    ignorecase: bool,
5241}
5242
5243#[allow(clippy::too_many_arguments)]
5244fn parallel_process_blob_worktree_jobs(
5245    odb: &Odb,
5246    source: &StagingHashSource<'_>,
5247    jobs: &[StagingHashJob],
5248    thread_policy: &crate::parallel::ThreadPolicy,
5249    config: &crate::config::ConfigSet,
5250    materialize_dirty_blobs: bool,
5251    hash_blobs_without_write: bool,
5252    for_status: bool,
5253    skip_inaccessible_paths: bool,
5254    diagnostics: Option<&crate::diagnostics::DiagnosticsHandle>,
5255    conv: &crate::crlf::ConversionConfig,
5256    filter_process: Option<&crate::filter_process::FilterProcessState>,
5257    _icase_collisions: Option<&crate::path_icase::IcaseCheckoutCollisionMap>,
5258    result: &mut Vec<DiffEntry>,
5259) -> Result<()> {
5260    use crate::parallel::{try_par_map, ParallelHashError, ParallelOp};
5261
5262    if jobs.is_empty() {
5263        return Ok(());
5264    }
5265    let mut par_opts = thread_policy.resolve(ParallelOp::IndexRefresh, config);
5266    par_opts.min_items = 0;
5267    par_opts.min_total_bytes = None;
5268    let est_bytes = jobs.len().saturating_mul(8192);
5269    let store_dirty_blobs = (materialize_dirty_blobs && !hash_blobs_without_write)
5270        .then(|| odb.bulk_stage_write_options());
5271    let stream_from = if store_dirty_blobs.is_some() {
5272        odb.big_file_threshold()?
5273    } else {
5274        hash_stream_threshold(odb)?
5275    };
5276    let default_attrs = crate::crlf::FileAttrs::default();
5277
5278    // Each job yields only the worktree side's oid when the entry changed; most jobs on a
5279    // stale index find clean files, so diff entries are built afterwards for the few changes.
5280    let new_oids = try_par_map::<StagingHashJob, Option<ObjectId>, Error, _>(
5281        jobs,
5282        par_opts,
5283        est_bytes,
5284        |job| {
5285            let entry = &source.index.entries[job.entry_index];
5286            let rel_path = crate::repo_relpath::storage_string(&entry.path);
5287            let modified = |new_oid| Ok(Some(new_oid));
5288            let file_path = index_entry_worktree_abs_bytes(
5289                source.work_tree,
5290                &entry.path,
5291                source.precompose_unicode,
5292                source.ignorecase,
5293            );
5294            let content = match read_regular_worktree_blob(
5295                odb,
5296                &file_path,
5297                conv,
5298                job.file_attrs.as_deref().unwrap_or(&default_attrs),
5299                &rel_path,
5300                Some(entry),
5301                filter_process,
5302                stream_from,
5303            ) {
5304                Ok(content) => content,
5305                Err(err)
5306                    if status_skips_inaccessible_worktree_error(
5307                        &err,
5308                        for_status,
5309                        skip_inaccessible_paths,
5310                        materialize_dirty_blobs,
5311                    ) =>
5312                {
5313                    warn_and_skip_status_inaccessible(diagnostics, rel_path.as_str(), &err);
5314                    return Ok(None);
5315                }
5316                Err(err) if unreadable_worktree_blob_is_modified(&err, materialize_dirty_blobs) => {
5317                    return modified(zero_oid());
5318                }
5319                Err(err) => return Err(err),
5320            };
5321            let eff_oid = content.object_id(odb, store_dirty_blobs)?;
5322            if eff_oid != entry.oid || job.worktree_mode != entry.mode {
5323                let new_oid = if eff_oid != entry.oid && !materialize_dirty_blobs {
5324                    zero_oid()
5325                } else {
5326                    eff_oid
5327                };
5328                modified(new_oid)
5329            } else {
5330                Ok(None)
5331            }
5332        },
5333    )
5334    .map_err(|e| match e {
5335        ParallelHashError::Task(err) => err,
5336    })?;
5337
5338    let mut changed: Vec<(usize, u32, ObjectId)> = jobs
5339        .iter()
5340        .zip(new_oids)
5341        .filter_map(|(job, new_oid)| new_oid.map(|oid| (job.entry_index, job.worktree_mode, oid)))
5342        .collect();
5343    changed.sort_by_key(|(idx, _, _)| *idx);
5344    result.extend(
5345        changed
5346            .into_iter()
5347            .map(|(entry_index, worktree_mode, new_oid)| {
5348                let entry = &source.index.entries[entry_index];
5349                let rel_path = crate::repo_relpath::storage_string(&entry.path);
5350                worktree_content_change_entry(entry, &rel_path, worktree_mode, new_oid)
5351            }),
5352    );
5353    Ok(())
5354}
5355
5356/// Index-vs-worktree entry for a tracked path whose content or mode differs from `ie`.
5357fn worktree_content_change_entry(
5358    ie: &IndexEntry,
5359    path: &str,
5360    worktree_mode: u32,
5361    new_oid: ObjectId,
5362) -> DiffEntry {
5363    DiffEntry {
5364        status: DiffStatus::for_content_change(ie.mode, worktree_mode),
5365        old_path: Some(path.to_owned()),
5366        new_path: Some(path.to_owned()),
5367        old_mode: FileMode::new(ie.mode),
5368        new_mode: FileMode::new(worktree_mode),
5369        old_oid: ie.oid,
5370        new_oid,
5371        score: None,
5372    }
5373}
5374
5375/// Whether a failed read of a tracked file's content reports the path as modified (with an
5376/// unknown worktree oid) instead of failing the whole scan.
5377///
5378/// A file that cannot be opened (no permission, held exclusively by another process) cannot
5379/// be proven to match the index, so it counts as changed, as in Git. Callers that need the
5380/// bytes themselves (`materialize_dirty_blobs`, i.e. staging) still get the error.
5381fn unreadable_worktree_blob_is_modified(err: &Error, materialize_dirty_blobs: bool) -> bool {
5382    !materialize_dirty_blobs
5383        && matches!(
5384            err,
5385            Error::Worktree(crate::worktree::WorktreeError::PathIo(
5386                crate::error::PathIoError {
5387                    operation: crate::error::PathIoOperation::Worktree(
5388                        crate::worktree::WorktreePathOperation::Read
5389                    ),
5390                    ..
5391                }
5392            ))
5393        )
5394}
5395
5396fn warn_worktree_access_failed(
5397    diagnostics: Option<&crate::diagnostics::DiagnosticsHandle>,
5398    path: &str,
5399    err: &std::io::Error,
5400) {
5401    if let Some(handle) = diagnostics {
5402        crate::diagnostics::warn(
5403            handle,
5404            crate::diagnostics::Warning::WorktreeAccessFailed {
5405                path: path.to_owned(),
5406                detail: err.to_string(),
5407            },
5408        );
5409    }
5410}
5411
5412fn blob_disk_lookup_io_result(
5413    diagnostics: Option<&crate::diagnostics::DiagnosticsHandle>,
5414    path: &str,
5415    err: std::io::Error,
5416    for_status: bool,
5417    skip_inaccessible_paths: bool,
5418    materialize_dirty_blobs: bool,
5419) -> Result<()> {
5420    if worktree_diff_skips_inaccessible_paths(
5421        for_status,
5422        skip_inaccessible_paths,
5423        materialize_dirty_blobs,
5424    ) && io_err_is_permission_denied(&err)
5425    {
5426        warn_worktree_access_failed(diagnostics, path, &err);
5427        return Ok(());
5428    }
5429    Err(Error::from(crate::error::PathIoError::worktree(
5430        path.to_owned(),
5431        crate::worktree::WorktreePathOperation::Metadata,
5432        err,
5433    )))
5434}
5435
5436fn io_err_is_permission_denied(err: &std::io::Error) -> bool {
5437    err.kind() == std::io::ErrorKind::PermissionDenied
5438}
5439
5440fn status_skips_inaccessible_worktree_error(
5441    err: &Error,
5442    for_status: bool,
5443    skip_inaccessible_paths: bool,
5444    materialize_dirty_blobs: bool,
5445) -> bool {
5446    if !worktree_diff_skips_inaccessible_paths(
5447        for_status,
5448        skip_inaccessible_paths,
5449        materialize_dirty_blobs,
5450    ) {
5451        return false;
5452    }
5453    match err {
5454        Error::Io(e) => io_err_is_permission_denied(e),
5455        Error::Worktree(crate::worktree::WorktreeError::PathIo(crate::error::PathIoError {
5456            operation:
5457                crate::error::PathIoOperation::Worktree(
5458                    crate::worktree::WorktreePathOperation::Metadata,
5459                ),
5460            source,
5461            ..
5462        })) => io_err_is_permission_denied(source),
5463        _ => false,
5464    }
5465}
5466
5467fn warn_and_skip_status_inaccessible(
5468    diagnostics: Option<&crate::diagnostics::DiagnosticsHandle>,
5469    path: &str,
5470    err: &Error,
5471) -> bool {
5472    let detail = match err {
5473        Error::Io(e) => e.to_string(),
5474        Error::Worktree(crate::worktree::WorktreeError::PathIo(io)) => io.source.to_string(),
5475        _ => return false,
5476    };
5477    if let Some(handle) = diagnostics {
5478        crate::diagnostics::warn(
5479            handle,
5480            crate::diagnostics::Warning::WorktreeAccessFailed {
5481                path: path.to_owned(),
5482                detail,
5483            },
5484        );
5485    }
5486    true
5487}
5488
5489enum WorktreeSymlinkMetadata {
5490    Found(fs::Metadata),
5491    Absent,
5492    Inaccessible,
5493}
5494
5495fn probe_worktree_symlink_metadata(
5496    file_path: &Path,
5497    path_str: &str,
5498    diagnostics: Option<&crate::diagnostics::DiagnosticsHandle>,
5499    for_status: bool,
5500    skip_inaccessible_paths: bool,
5501    materialize_dirty_blobs: bool,
5502) -> Result<WorktreeSymlinkMetadata> {
5503    match fs::symlink_metadata(file_path) {
5504        Ok(meta) => Ok(WorktreeSymlinkMetadata::Found(meta)),
5505        Err(e)
5506            if e.kind() == std::io::ErrorKind::NotFound
5507                || e.raw_os_error() == Some(20) /* ENOTDIR */ =>
5508        {
5509            Ok(WorktreeSymlinkMetadata::Absent)
5510        }
5511        Err(e) => {
5512            if worktree_diff_skips_inaccessible_paths(
5513                for_status,
5514                skip_inaccessible_paths,
5515                materialize_dirty_blobs,
5516            ) && io_err_is_permission_denied(&e)
5517            {
5518                warn_worktree_access_failed(diagnostics, path_str, &e);
5519                Ok(WorktreeSymlinkMetadata::Inaccessible)
5520            } else {
5521                Err(Error::from(crate::error::PathIoError::worktree(
5522                    path_str.to_owned(),
5523                    crate::worktree::WorktreePathOperation::Metadata,
5524                    e,
5525                )))
5526            }
5527        }
5528    }
5529}
5530
5531fn symlink_metadata_worktree_diff(
5532    file_path: &Path,
5533    path_str: &str,
5534    diagnostics: Option<&crate::diagnostics::DiagnosticsHandle>,
5535    for_status: bool,
5536    skip_inaccessible_paths: bool,
5537    materialize_dirty_blobs: bool,
5538) -> Result<Option<fs::Metadata>> {
5539    Ok(
5540        match probe_worktree_symlink_metadata(
5541            file_path,
5542            path_str,
5543            diagnostics,
5544            for_status,
5545            skip_inaccessible_paths,
5546            materialize_dirty_blobs,
5547        )? {
5548            WorktreeSymlinkMetadata::Found(meta) => Some(meta),
5549            WorktreeSymlinkMetadata::Absent | WorktreeSymlinkMetadata::Inaccessible => None,
5550        },
5551    )
5552}
5553
5554/// Whether status may report `ie` modified because the worktree size (already munged to the
5555/// index's 32-bit form) differs from the cached one, without reading the file (Git
5556/// `compare_stat_data`).
5557///
5558/// Only sound when clean conversion is the identity: a line-ending rule, filter, encoding or
5559/// `ident` can turn a file of another size into exactly the indexed blob. The
5560/// `conversion_is_identity` check runs only once the sizes differ, so clean entries never
5561/// resolve attributes here.
5562fn status_size_change_proves_modified(
5563    ie: &IndexEntry,
5564    worktree_size: u32,
5565    for_status: bool,
5566    conversion_is_identity: impl FnOnce() -> bool,
5567) -> bool {
5568    for_status && !ie.stat_cache_unset() && worktree_size != ie.size && conversion_is_identity()
5569}
5570
5571/// Report `ie` modified when [`status_size_change_proves_modified`] holds; returns whether it
5572/// did. The worktree side uses the null OID because the file was not hashed.
5573#[allow(clippy::too_many_arguments)]
5574fn try_push_status_modified_on_size_mismatch(
5575    odb: &Odb,
5576    ie: &IndexEntry,
5577    path_str_ref: &str,
5578    worktree_size: u32,
5579    worktree_mode: u32,
5580    raw_worktree_len: u64,
5581    for_status: bool,
5582    conversion_is_identity: impl FnOnce() -> bool,
5583    result: &mut Vec<DiffEntry>,
5584) -> Result<bool> {
5585    let identity = conversion_is_identity();
5586    let size_differs = worktree_size != ie.size;
5587    if !size_differs || !identity {
5588        return Ok(false);
5589    }
5590    let threshold = odb.big_file_threshold()?;
5591    let big_file = raw_worktree_len > threshold;
5592    let proves_modified = if big_file {
5593        true
5594    } else {
5595        status_size_change_proves_modified(ie, worktree_size, for_status, || identity)
5596    };
5597    if !proves_modified {
5598        return Ok(false);
5599    }
5600    result.push(worktree_content_change_entry(
5601        ie,
5602        path_str_ref,
5603        worktree_mode,
5604        zero_oid(),
5605    ));
5606    Ok(true)
5607}
5608
5609#[allow(clippy::too_many_arguments)]
5610fn push_index_blob_worktree_diff(
5611    odb: &Odb,
5612    ie: &IndexEntry,
5613    path_str_ref: &str,
5614    file_path: &Path,
5615    meta: fs::Metadata,
5616    index_mtime: Option<(u32, u32)>,
5617    mode_policy: crate::index::WorktreeModePolicy,
5618    stat_policy: crate::index::StatMatchPolicy,
5619    file_attrs_for: &dyn Fn(&str) -> crate::crlf::FileAttrs,
5620    conv: &crate::crlf::ConversionConfig,
5621    materialize_dirty_blobs: bool,
5622    hash_blobs_without_write: bool,
5623    for_status: bool,
5624    size_change_proves_modified: bool,
5625    skip_inaccessible_paths: bool,
5626    diagnostics: Option<&crate::diagnostics::DiagnosticsHandle>,
5627    filter_process: Option<&crate::filter_process::FilterProcessState>,
5628    icase_collisions: Option<&crate::path_icase::IcaseCheckoutCollisionMap>,
5629    result: &mut Vec<DiffEntry>,
5630) -> Result<()> {
5631    if meta.is_dir() {
5632        if file_path.join(".git").exists() {
5633            let head = read_submodule_head_oid(file_path).unwrap_or_else(zero_oid);
5634            let path_owned = path_str_ref.to_owned();
5635            result.push(DiffEntry {
5636                status: DiffStatus::TypeChanged,
5637                old_path: Some(path_owned.clone()),
5638                new_path: Some(path_owned),
5639                old_mode: FileMode::new(ie.mode),
5640                new_mode: FileMode::new(0o160000),
5641                old_oid: ie.oid,
5642                new_oid: head,
5643                score: None,
5644            });
5645            return Ok(());
5646        }
5647        result.push(DiffEntry {
5648            status: DiffStatus::Deleted,
5649            old_path: Some(path_str_ref.to_owned()),
5650            new_path: None,
5651            old_mode: FileMode::new(ie.mode),
5652            new_mode: FileMode::ABSENT,
5653            old_oid: ie.oid,
5654            new_oid: zero_oid(),
5655            score: None,
5656        });
5657        return Ok(());
5658    }
5659
5660    let worktree_mode =
5661        mode_policy.index_mode_for_worktree(mode_from_metadata(&meta), Some(ie.mode));
5662    let stat_same = stat_matches_with_policy(ie, &meta, stat_policy);
5663    if stat_same && worktree_mode != ie.mode {
5664        result.push(worktree_content_change_entry(
5665            ie,
5666            path_str_ref,
5667            worktree_mode,
5668            ie.oid,
5669        ));
5670        return Ok(());
5671    }
5672
5673    if stat_same
5674        && index_entry_skips_worktree_content_hash_after_stat_match(ie, index_mtime, stat_policy)
5675    {
5676        return Ok(());
5677    }
5678
5679    let file_attrs = file_attrs_for(path_str_ref);
5680    if !icase_collisions.is_some_and(|map| map.diff_requires_worktree_hash(ie.path.as_slice()))
5681        && try_push_status_modified_on_size_mismatch(
5682            odb,
5683            ie,
5684            path_str_ref,
5685            crate::index::munge_index_size(meta.len()),
5686            worktree_mode,
5687            meta.len(),
5688            size_change_proves_modified,
5689            || crate::crlf::git_conversion_is_identity(conv, &file_attrs),
5690            result,
5691        )?
5692    {
5693        return Ok(());
5694    }
5695
5696    let store = (materialize_dirty_blobs && !hash_blobs_without_write)
5697        .then(|| odb.bulk_stage_write_options());
5698    let stream_from = if store.is_some() {
5699        odb.big_file_threshold()?
5700    } else {
5701        hash_stream_threshold(odb)?
5702    };
5703    let content = match worktree_blob_content_from(
5704        odb,
5705        file_path,
5706        &meta,
5707        conv,
5708        &file_attrs,
5709        path_str_ref,
5710        Some(ie),
5711        filter_process,
5712        stream_from,
5713    ) {
5714        Ok(content) => content,
5715        Err(err)
5716            if status_skips_inaccessible_worktree_error(
5717                &err,
5718                for_status,
5719                skip_inaccessible_paths,
5720                materialize_dirty_blobs,
5721            ) =>
5722        {
5723            warn_and_skip_status_inaccessible(diagnostics, path_str_ref, &err);
5724            return Ok(());
5725        }
5726        Err(err) if unreadable_worktree_blob_is_modified(&err, materialize_dirty_blobs) => {
5727            result.push(worktree_content_change_entry(
5728                ie,
5729                path_str_ref,
5730                worktree_mode,
5731                zero_oid(),
5732            ));
5733            return Ok(());
5734        }
5735        Err(err) => return Err(err),
5736    };
5737    let eff_oid = content.object_id(odb, store)?;
5738
5739    if eff_oid != ie.oid || worktree_mode != ie.mode {
5740        // `git diff-files --raw` uses the null OID for the worktree side when content differs.
5741        // Staging (`diff_index_to_worktree_for_staging`) materializes blobs and needs the real OID.
5742        let new_oid = if eff_oid != ie.oid && !materialize_dirty_blobs {
5743            zero_oid()
5744        } else {
5745            eff_oid
5746        };
5747        result.push(worktree_content_change_entry(
5748            ie,
5749            path_str_ref,
5750            worktree_mode,
5751            new_oid,
5752        ));
5753    }
5754    Ok(())
5755}
5756
5757/// Compare tracked index entries with the worktree, using link-target bytes for symlinks.
5758///
5759/// Symlink targets bypass regular-file filters and line-ending conversion. Parallel and
5760/// serial scans use the same file-type classification, including directory replacements
5761/// and regular-file placeholders checked out with `core.symlinks=false`.
5762///
5763/// # Errors
5764///
5765/// Returns errors from I/O or hashing.
5766pub(crate) fn diff_index_to_worktree_with_options(
5767    odb: &Odb,
5768    index: &mut Index,
5769    work_tree: &Path,
5770    options: DiffIndexToWorktreeOptions,
5771) -> Result<(Vec<DiffEntry>, bool)> {
5772    let default_git_dir = work_tree.join(".git");
5773    let git_dir = options
5774        .repository_git_dir
5775        .clone()
5776        .unwrap_or(default_git_dir);
5777    diff_index_to_worktree_inner(odb, index, work_tree, &git_dir, options, false, None)
5778        .map(|(entries, changed, _)| (entries, changed))
5779}
5780
5781/// Like [`diff_index_to_worktree_with_options`], with an owner-thread progress poll hook for
5782/// long stat scans (used by [`crate::porcelain::status::status`]).
5783pub(crate) fn diff_index_to_worktree_with_owner_poll(
5784    odb: &Odb,
5785    index: &mut Index,
5786    work_tree: &Path,
5787    options: DiffIndexToWorktreeOptions,
5788    owner_poll: Option<std::sync::Arc<crate::progress::StackCancelPoll>>,
5789) -> Result<(Vec<DiffEntry>, bool)> {
5790    let default_git_dir = work_tree.join(".git");
5791    let git_dir = options
5792        .repository_git_dir
5793        .clone()
5794        .unwrap_or(default_git_dir);
5795    diff_index_to_worktree_inner(odb, index, work_tree, &git_dir, options, false, owner_poll)
5796        .map(|(entries, changed, _)| (entries, changed))
5797}
5798
5799/// Like [`diff_index_to_worktree_with_options`], but loads config from `repository_git_dir`
5800/// (the main repository for linked worktrees) and persists each hashed worktree blob into `odb`
5801/// so staging can update the index without re-reading file contents.
5802/// Symlinks store their target bytes. With [`DiffIndexToWorktreeOptions::hash_blobs_without_write`],
5803/// returns the same blob IDs without writing objects, for staging previews.
5804pub(crate) fn diff_index_to_worktree_for_staging(
5805    odb: &Odb,
5806    repository_git_dir: &Path,
5807    index: &mut Index,
5808    work_tree: &Path,
5809    options: DiffIndexToWorktreeOptions,
5810) -> Result<(
5811    Vec<DiffEntry>,
5812    bool,
5813    std::collections::BTreeMap<String, crate::index::IndexStatFields>,
5814)> {
5815    diff_index_to_worktree_inner(
5816        odb,
5817        index,
5818        work_tree,
5819        repository_git_dir,
5820        options,
5821        true,
5822        None,
5823    )
5824}
5825
5826/// First pass of [`diff_index_to_worktree_inner`]: gitlinks and intent-to-add paths only.
5827#[allow(clippy::too_many_arguments)]
5828fn worktree_diff_pass1_index_entry(
5829    odb: &Odb,
5830    work_tree: &Path,
5831    ie: &IndexEntry,
5832    path_str_ref: &str,
5833    config: &ConfigSet,
5834    simplify_gitlinks: bool,
5835    ignore_submodule_untracked: bool,
5836    error_on_broken_gitlinks: bool,
5837    materialize_dirty_blobs: bool,
5838    filter_fp: Option<&crate::filter_process::FilterProcessState>,
5839    conv: &crate::crlf::ConversionConfig,
5840    file_attrs_for: &dyn Fn(&str) -> crate::crlf::FileAttrs,
5841    precompose_unicode: bool,
5842    ignorecase: bool,
5843    result: &mut Vec<DiffEntry>,
5844) -> Result<()> {
5845    if ie.skip_worktree() || ie.assume_unchanged() {
5846        return Ok(());
5847    }
5848    let is_intent_to_add = ie.intent_to_add();
5849
5850    if ie.mode == 0o160000 {
5851        let sub_dir =
5852            index_entry_worktree_abs_bytes(work_tree, &ie.path, precompose_unicode, ignorecase);
5853        match fs::symlink_metadata(&sub_dir) {
5854            Err(e)
5855                if e.kind() == std::io::ErrorKind::NotFound
5856                    || e.raw_os_error() == Some(20) /* ENOTDIR */ =>
5857            {
5858                if !simplify_gitlinks {
5859                    let path_owned = path_str_ref.to_owned();
5860                    result.push(DiffEntry {
5861                        status: DiffStatus::Deleted,
5862                        old_path: Some(path_owned.clone()),
5863                        new_path: Some(path_owned),
5864                        old_mode: FileMode::new(ie.mode),
5865                        new_mode: FileMode::ABSENT,
5866                        old_oid: ie.oid,
5867                        new_oid: zero_oid(),
5868                        score: None,
5869                    });
5870                }
5871                return Ok(());
5872            }
5873            Err(e) => return Err(Error::Io(e)),
5874            Ok(meta) if !meta.is_dir() => {
5875                let worktree_mode = mode_from_metadata(&meta);
5876                let file_attrs = file_attrs_for(path_str_ref);
5877                let worktree_oid = worktree_file_oid(
5878                    odb,
5879                    &sub_dir,
5880                    &meta,
5881                    conv,
5882                    &file_attrs,
5883                    path_str_ref,
5884                    None,
5885                    materialize_dirty_blobs,
5886                    filter_fp,
5887                )?;
5888                let path_owned = path_str_ref.to_owned();
5889                result.push(DiffEntry {
5890                    status: DiffStatus::TypeChanged,
5891                    old_path: Some(path_owned.clone()),
5892                    new_path: Some(path_owned),
5893                    old_mode: FileMode::new(ie.mode),
5894                    new_mode: FileMode::new(worktree_mode),
5895                    old_oid: ie.oid,
5896                    new_oid: worktree_oid,
5897                    score: None,
5898                });
5899                return Ok(());
5900            }
5901            Ok(_) => {}
5902        }
5903        let sub_head_oid = read_submodule_head_oid(&sub_dir);
5904        let ref_matches = if let Some(oid) = sub_head_oid {
5905            oid == ie.oid
5906        } else {
5907            if error_on_broken_gitlinks && submodule_embedded_git_dir(&sub_dir).is_some() {
5908                return Err(Error::Worktree(WorktreeError::Submodule(
5909                    SubmoduleError::HeadUnreadable {
5910                        path: path_str_ref.to_owned(),
5911                    },
5912                )));
5913            }
5914            uninitialized_gitlink_worktree_matches_index(&sub_dir)
5915        };
5916        if simplify_gitlinks {
5917            if !ref_matches {
5918                let path_owned = path_str_ref.to_owned();
5919                let new_oid = sub_head_oid.unwrap_or_else(zero_oid);
5920                result.push(DiffEntry {
5921                    status: DiffStatus::Modified,
5922                    old_path: Some(path_owned.clone()),
5923                    new_path: Some(path_owned),
5924                    old_mode: FileMode::new(ie.mode),
5925                    new_mode: FileMode::new(ie.mode),
5926                    old_oid: ie.oid,
5927                    new_oid,
5928                    score: None,
5929                });
5930            }
5931            return Ok(());
5932        }
5933        if sub_head_oid.is_some() && submodule_head_object_broken(&sub_dir) {
5934            return Err(Error::Worktree(WorktreeError::Submodule(
5935                SubmoduleError::HeadCommitMissing {
5936                    path: path_str_ref.to_owned(),
5937                },
5938            )));
5939        }
5940        let mut flags = submodule_porcelain_flags(work_tree, path_str_ref, ie.oid);
5941        if ignore_submodule_untracked {
5942            flags.untracked = false;
5943        }
5944        let ignore =
5945            crate::submodule_ignore::submodule_ignore_for_path(config, work_tree, path_str_ref);
5946        if crate::submodule_ignore::gitlink_worktree_change_suppressed(ignore, ref_matches, flags) {
5947            return Ok(());
5948        }
5949        let inner_dirty = flags.modified || flags.untracked;
5950        if !ref_matches || inner_dirty {
5951            let path_owned = path_str_ref.to_owned();
5952            let new_oid = if !ref_matches {
5953                sub_head_oid.unwrap_or_else(zero_oid)
5954            } else {
5955                zero_oid()
5956            };
5957            result.push(DiffEntry {
5958                status: DiffStatus::Modified,
5959                old_path: Some(path_owned.clone()),
5960                new_path: Some(path_owned),
5961                old_mode: FileMode::new(ie.mode),
5962                new_mode: FileMode::new(ie.mode),
5963                old_oid: ie.oid,
5964                new_oid,
5965                score: None,
5966            });
5967        }
5968        return Ok(());
5969    }
5970
5971    let file_path =
5972        index_entry_worktree_abs_bytes(work_tree, &ie.path, precompose_unicode, ignorecase);
5973    let path_match = crate::repo_relpath::as_utf8_for_matching(path_str_ref);
5974
5975    if is_intent_to_add {
5976        match fs::symlink_metadata(&file_path) {
5977            Ok(meta) => {
5978                let file_attrs = file_attrs_for(path_match.as_ref());
5979                let worktree_oid = worktree_file_oid(
5980                    odb,
5981                    &file_path,
5982                    &meta,
5983                    conv,
5984                    &file_attrs,
5985                    path_match.as_ref(),
5986                    None,
5987                    materialize_dirty_blobs,
5988                    filter_fp,
5989                )?;
5990                let worktree_mode = mode_from_metadata(&meta);
5991                result.push(DiffEntry {
5992                    status: DiffStatus::Added,
5993                    old_path: None,
5994                    new_path: Some(path_str_ref.to_owned()),
5995                    old_mode: FileMode::ABSENT,
5996                    new_mode: FileMode::new(worktree_mode),
5997                    old_oid: zero_oid(),
5998                    new_oid: worktree_oid,
5999                    score: None,
6000                });
6001            }
6002            Err(e)
6003                if e.kind() == std::io::ErrorKind::NotFound
6004                    || e.raw_os_error() == Some(20) /* ENOTDIR */ =>
6005            {
6006                result.push(DiffEntry {
6007                    status: DiffStatus::Deleted,
6008                    old_path: Some(path_str_ref.to_owned()),
6009                    new_path: None,
6010                    old_mode: FileMode::new(ie.mode),
6011                    new_mode: FileMode::ABSENT,
6012                    old_oid: ie.oid,
6013                    new_oid: zero_oid(),
6014                    score: None,
6015                });
6016            }
6017            Err(e) => return Err(Error::Io(e)),
6018        }
6019    }
6020    Ok(())
6021}
6022
6023#[inline]
6024fn index_entry_needs_worktree_diff_pass1(ie: &IndexEntry) -> bool {
6025    ie.mode == crate::index::MODE_GITLINK || ie.intent_to_add()
6026}
6027
6028fn diff_index_to_worktree_inner(
6029    odb: &Odb,
6030    index: &mut Index,
6031    work_tree: &Path,
6032    repository_git_dir: &Path,
6033    options: DiffIndexToWorktreeOptions,
6034    materialize_dirty_blobs: bool,
6035    owner_poll: Option<std::sync::Arc<crate::progress::StackCancelPoll>>,
6036) -> Result<(
6037    Vec<DiffEntry>,
6038    bool,
6039    std::collections::BTreeMap<String, crate::index::IndexStatFields>,
6040)> {
6041    use crate::config::ConfigSet;
6042    use crate::crlf;
6043    use std::collections::BTreeMap;
6044
6045    let mut staging_stat_cache = BTreeMap::new();
6046    poll_diff_cancel(&options, owner_poll.as_ref())?;
6047    let index_mtime = index_mtime_for_diff(index, options.index_mtime);
6048
6049    let ignore_submodule_untracked = options.ignore_submodule_untracked;
6050    let simplify_gitlinks = options.simplify_gitlinks;
6051    // Staging stores the worktree blob, so it always needs the file's object id.
6052    let size_change_proves_modified =
6053        !materialize_dirty_blobs && (options.for_status || options.size_change_proves_modified);
6054
6055    let rules_lock = options.worktree_rules.as_ref().map(|shared| {
6056        shared
6057            .lock()
6058            .unwrap_or_else(std::sync::PoisonError::into_inner)
6059    });
6060    let config = if let Some(rules) = rules_lock.as_ref() {
6061        rules.config().clone()
6062    } else if let Some(c) = options.config.as_ref() {
6063        c.as_ref().clone()
6064    } else {
6065        ConfigSet::load(
6066            &crate::environment::Environment::empty(),
6067            Some(repository_git_dir),
6068            &crate::config::LoadConfigOptions::standard(
6069                &crate::environment::Environment::empty(),
6070                Some(repository_git_dir),
6071            ),
6072        )
6073        .unwrap_or_else(|_| ConfigSet::new())
6074    };
6075    let conv = rules_lock
6076        .as_ref()
6077        .map(|r| r.conversion().clone())
6078        .unwrap_or_else(|| crlf::ConversionConfig::from_config(&config));
6079    let legacy_attrs = if rules_lock.is_none() {
6080        Some(crlf::load_gitattributes_parsed(work_tree))
6081    } else {
6082        None
6083    };
6084    let file_attrs_for = |path: &str| -> crlf::FileAttrs {
6085        if let Some(rules) = rules_lock.as_ref() {
6086            rules.file_attrs(path, false)
6087        } else if let Some(parsed) = legacy_attrs.as_ref() {
6088            crlf::file_attrs_from_gitattributes(&parsed.rules, &parsed.macros, path, false, &config)
6089        } else {
6090            crlf::FileAttrs::default()
6091        }
6092    };
6093    let filter_fp = rules_lock.as_ref().map(|r| r.filter_process());
6094    let precompose_unicode = config.precompose_unicode();
6095    let mode_policy = crate::index::WorktreeModePolicy::from_config(&config);
6096    let stat_policy = crate::index::StatMatchPolicy::from_config(Some(&config));
6097    let ignorecase = config.ignore_case();
6098    let icase_collisions = ignorecase
6099        .then(|| crate::path_icase::IcaseCheckoutCollisionMap::from_index(work_tree, index, true));
6100    let hash_blobs_without_write = options.hash_blobs_without_write;
6101
6102    let mut result = Vec::new();
6103    let refresh_index_stat_in_pass = options.refresh_index_stat_in_pass;
6104    let mut index_stat_refresh_changed = false;
6105    let mut fsmonitor_valid_updated = false;
6106    let mut honor_fsmonitor_valid = false;
6107    if options.refresh_fsmonitor {
6108        let default_runner = crate::command_runner::system_command_runner();
6109        let runner = options
6110            .command_runner
6111            .as_deref()
6112            .unwrap_or(default_runner.as_ref());
6113        let boundary = options.fsmonitor_query_boundary.unwrap_or_else(|| {
6114            crate::fsmonitor::FsmonitorQueryBoundary(
6115                crate::environment::wall_clock_unix_timestamp_nanos(),
6116            )
6117        });
6118        let refresh_opts = crate::fsmonitor::FsmonitorRefreshOptions::with_query_boundary(boundary);
6119        let refresh = crate::fsmonitor::refresh_index(
6120            index,
6121            repository_git_dir,
6122            work_tree,
6123            &config,
6124            Some(runner),
6125            &refresh_opts,
6126        )?;
6127        if refresh.index_changed {
6128            index_stat_refresh_changed = true;
6129        }
6130        honor_fsmonitor_valid = refresh.honor_valid_bits;
6131    }
6132    let use_fsmonitor_stat_skip = honor_fsmonitor_valid;
6133    let mut unmerged_base: std::collections::BTreeMap<String, (u8, IndexEntry)> =
6134        std::collections::BTreeMap::new();
6135
6136    let error_on_broken_gitlinks = options.error_on_broken_gitlinks;
6137    if options.restrict_to_paths.is_none() {
6138        for ie in &index.entries {
6139            poll_diff_cancel(&options, owner_poll.as_ref())?;
6140            if ie.stage() != 0 {
6141                let path = String::from_utf8_lossy(&ie.path).to_string();
6142                let rank = match ie.stage() {
6143                    2 => 0u8,
6144                    3 => 1u8,
6145                    1 => 2u8,
6146                    _ => 3u8,
6147                };
6148                match unmerged_base.get(&path) {
6149                    Some((existing_rank, _)) if *existing_rank <= rank => {}
6150                    _ => {
6151                        unmerged_base.insert(path, (rank, ie.clone()));
6152                    }
6153                }
6154            }
6155        }
6156    }
6157
6158    if let Some(limit) = options.restrict_to_paths.as_ref() {
6159        let mut cursor = 0;
6160        for path in limit.iter() {
6161            let path_bytes = crate::repo_relpath::storage_bytes(path);
6162            let Some(pos) = index.find_stage0_pos_from(path_bytes.as_ref(), &mut cursor) else {
6163                continue;
6164            };
6165            let ie = &index.entries[pos];
6166            if options
6167                .skip_index_path
6168                .as_ref()
6169                .is_some_and(|skip| skip(&ie.path))
6170            {
6171                continue;
6172            }
6173            if !index_entry_needs_worktree_diff_pass1(ie) {
6174                continue;
6175            }
6176            worktree_diff_pass1_index_entry(
6177                odb,
6178                work_tree,
6179                ie,
6180                path.as_str(),
6181                &config,
6182                simplify_gitlinks,
6183                ignore_submodule_untracked,
6184                error_on_broken_gitlinks,
6185                materialize_dirty_blobs,
6186                filter_fp,
6187                &conv,
6188                &file_attrs_for,
6189                precompose_unicode,
6190                ignorecase,
6191                &mut result,
6192            )?;
6193        }
6194    } else {
6195        for ie in &index.entries {
6196            poll_diff_cancel(&options, owner_poll.as_ref())?;
6197            if ie.stage() != 0 {
6198                continue;
6199            }
6200            if options
6201                .skip_index_path
6202                .as_ref()
6203                .is_some_and(|skip| skip(&ie.path))
6204            {
6205                continue;
6206            }
6207            if !index_entry_needs_worktree_diff_pass1(ie) {
6208                continue;
6209            }
6210            let path_stored = crate::repo_relpath::storage_string(&ie.path);
6211            worktree_diff_pass1_index_entry(
6212                odb,
6213                work_tree,
6214                ie,
6215                &path_stored,
6216                &config,
6217                simplify_gitlinks,
6218                ignore_submodule_untracked,
6219                error_on_broken_gitlinks,
6220                materialize_dirty_blobs,
6221                filter_fp,
6222                &conv,
6223                &file_attrs_for,
6224                precompose_unicode,
6225                ignorecase,
6226                &mut result,
6227            )?;
6228        }
6229    }
6230
6231    use crate::worktree_scan::{
6232        collect_blob_lookup_batches_with_literal_paths, group_blob_entries_by_dir,
6233        stat_scan_thread_count, visit_blob_lookup_batches, BlobDiskLookup, BlobScanSparseMode,
6234        WorktreeBlobScanOptions, PARALLEL_STAT_MIN_ENTRIES,
6235    };
6236    let preload_index = config.preload_index();
6237    let blob_scan = WorktreeBlobScanOptions {
6238        preload_index,
6239        stat_parallel_threads: options.stat_parallel_threads,
6240        listing_cache: options.listing_cache.clone(),
6241        ..WorktreeBlobScanOptions::default()
6242    };
6243    let blob_dirs = if let Some(limit) = options.restrict_to_paths.as_ref() {
6244        if let Some(skip) = options.skip_index_path.as_ref() {
6245            let mut positions = Vec::new();
6246            let mut cursor = 0;
6247            for path in limit.iter() {
6248                let path_bytes = crate::repo_relpath::storage_bytes(path);
6249                let Some(pos) = index.find_stage0_pos_from(path_bytes.as_ref(), &mut cursor) else {
6250                    continue;
6251                };
6252                let ie = &index.entries[pos];
6253                if skip(&ie.path) {
6254                    continue;
6255                }
6256                if !crate::worktree_scan::index_blob_scan_eligible(ie, index, honor_fsmonitor_valid)
6257                {
6258                    continue;
6259                }
6260                positions.push(pos);
6261            }
6262            crate::worktree_scan::plan_blob_entries_by_dir_for_positions(
6263                index,
6264                &positions,
6265                honor_fsmonitor_valid,
6266            )
6267        } else {
6268            crate::worktree_scan::group_blob_entries_by_dir_for_paths(index, limit)
6269        }
6270    } else if let Some(skip) = options.skip_index_path.as_ref() {
6271        let positions: Vec<usize> = index
6272            .entries
6273            .iter()
6274            .enumerate()
6275            .filter(|(_, ie)| {
6276                ie.stage() == 0
6277                    && !skip(&ie.path)
6278                    && crate::worktree_scan::index_blob_scan_eligible(
6279                        ie,
6280                        index,
6281                        honor_fsmonitor_valid,
6282                    )
6283            })
6284            .map(|(pos, _)| pos)
6285            .collect();
6286        crate::worktree_scan::plan_blob_entries_by_dir_for_positions(
6287            index,
6288            &positions,
6289            honor_fsmonitor_valid,
6290        )
6291    } else {
6292        group_blob_entries_by_dir(index, honor_fsmonitor_valid)
6293    };
6294    let work_tree_owned = work_tree.to_path_buf();
6295    let dir_abs = move |dir: &[u8]| {
6296        if dir.is_empty() {
6297            work_tree_owned.clone()
6298        } else {
6299            index_entry_worktree_abs_bytes(&work_tree_owned, dir, precompose_unicode, ignorecase)
6300        }
6301    };
6302    let work_tree_for_files = work_tree.to_path_buf();
6303    let file_abs = move |rel: &[u8]| {
6304        index_entry_worktree_abs_bytes(&work_tree_for_files, rel, precompose_unicode, ignorecase)
6305    };
6306    let blob_count = blob_dirs.len();
6307    let thread_policy = options.thread_policy.unwrap_or_default();
6308    let hash_threads = stat_scan_thread_count(&thread_policy, &config, &blob_scan, blob_count);
6309    let parallel_blob_work = blob_count >= PARALLEL_STAT_MIN_ENTRIES && hash_threads > 1;
6310    let mut staging_hash_jobs: Vec<StagingHashJob> = Vec::new();
6311    // Entries the stat refresh just hashed and found matching their recorded blob.
6312    let mut refresh_content_verified = vec![false; index.entries.len()];
6313    // Parallel preload must lstat ancestor directories without following symlinks. When a
6314    // tracked directory is replaced by a symlink, indexed paths under it are deletions even if
6315    // the link target still holds a file at the same relative path with an unchanged stat.
6316    // This is independent of `core.symlinks` (checkout support); detection must stay enabled.
6317    let sparse = if parallel_blob_work {
6318        BlobScanSparseMode::StatusDiff {
6319            stat_policy,
6320            index_mtime,
6321            mode_policy,
6322            for_status: options.for_status,
6323            refresh_index_stat_in_pass,
6324            parallel_blob_work,
6325            honor_fsmonitor_stat_skip: use_fsmonitor_stat_skip,
6326        }
6327    } else {
6328        BlobScanSparseMode::Dense
6329    };
6330    let blob_stat_batches = collect_blob_lookup_batches_with_literal_paths(
6331        index,
6332        &blob_dirs,
6333        blob_scan,
6334        &thread_policy,
6335        &config,
6336        dir_abs,
6337        file_abs,
6338        !precompose_unicode && !ignorecase,
6339        || {
6340            let mut dir_symlinks = SymlinkDirCache::default();
6341            move |rel_path: &[u8]| {
6342                dir_symlinks.has_symlink_in_path_bytes(
6343                    work_tree,
6344                    rel_path,
6345                    precompose_unicode,
6346                    ignorecase,
6347                )
6348            }
6349        },
6350        sparse,
6351        options.cancel.clone(),
6352        owner_poll.clone(),
6353    )?;
6354    // Size-changed entries ask whether their clean conversion is the identity in both the stat
6355    // refresh and the visit below; resolve each one's attributes once.
6356    let identity_by_entry = std::cell::RefCell::new(std::collections::HashMap::new());
6357    let conversion_is_identity = |entry_index: usize, path: &str| -> bool {
6358        *identity_by_entry
6359            .borrow_mut()
6360            .entry(entry_index)
6361            .or_insert_with(|| crlf::git_conversion_is_identity(&conv, &file_attrs_for(path)))
6362    };
6363    let refresh_checks = if refresh_index_stat_in_pass {
6364        let par_opts =
6365            refresh_hash_parallel_options(&thread_policy, &config, options.stat_parallel_threads);
6366        refresh_content_checks(
6367            odb,
6368            index,
6369            &blob_stat_batches,
6370            index_mtime,
6371            stat_policy,
6372            &|entry_index, ie, stat| {
6373                !(use_fsmonitor_stat_skip && crate::fsmonitor::entry_skips_worktree_stat(ie, index))
6374                    && worktree_diff_refreshes_entry(
6375                        ie,
6376                        stat,
6377                        index_mtime,
6378                        stat_policy,
6379                        options.for_status,
6380                        || {
6381                            conversion_is_identity(
6382                                entry_index,
6383                                &crate::repo_relpath::storage_string(&ie.path),
6384                            )
6385                        },
6386                    )
6387            },
6388            &WorktreeHashContext {
6389                work_tree,
6390                precompose_unicode,
6391                ignorecase,
6392                conv: &conv,
6393                file_attrs_for: &file_attrs_for,
6394                filter_process: filter_fp,
6395            },
6396            par_opts,
6397            options.cancel.as_ref(),
6398        )?
6399    } else {
6400        RefreshChecks::default()
6401    };
6402    poll_diff_cancel(&options, owner_poll.as_ref())?;
6403    visit_blob_lookup_batches(blob_stat_batches, |entry_index, lookup| {
6404        poll_diff_cancel(&options, owner_poll.as_ref())?;
6405        let path_owned = crate::repo_relpath::storage_string(&index.entries[entry_index].path);
6406        let path_str_ref = path_owned.as_str();
6407        if use_fsmonitor_stat_skip {
6408            let ie_skip = &index.entries[entry_index];
6409            if crate::fsmonitor::entry_skips_worktree_stat(ie_skip, index) {
6410                return Ok(());
6411            }
6412        }
6413        match lookup {
6414            BlobDiskLookup::Missing => {
6415                if icase_collisions.as_ref().is_some_and(|map| {
6416                    map.preserve_missing_collision_loser(&index.entries[entry_index].path)
6417                }) {
6418                    return Ok(());
6419                }
6420                let ie = &index.entries[entry_index];
6421                result.push(DiffEntry {
6422                    status: DiffStatus::Deleted,
6423                    old_path: Some(path_str_ref.to_owned()),
6424                    new_path: None,
6425                    old_mode: FileMode::new(ie.mode),
6426                    new_mode: FileMode::ABSENT,
6427                    old_oid: ie.oid,
6428                    new_oid: zero_oid(),
6429                    score: None,
6430                });
6431            }
6432            BlobDiskLookup::Io(e) => {
6433                blob_disk_lookup_io_result(
6434                    options.diagnostics.as_ref(),
6435                    path_str_ref,
6436                    e,
6437                    options.for_status,
6438                    options.skip_inaccessible_paths,
6439                    materialize_dirty_blobs,
6440                )?;
6441            }
6442            BlobDiskLookup::Present(stat) => {
6443                if materialize_dirty_blobs {
6444                    staging_stat_cache.insert(path_str_ref.to_owned(), stat.fields);
6445                }
6446                if refresh_index_stat_in_pass
6447                    && worktree_diff_refreshes_entry(
6448                        &index.entries[entry_index],
6449                        &stat,
6450                        index_mtime,
6451                        stat_policy,
6452                        options.for_status,
6453                        || conversion_is_identity(entry_index, path_str_ref),
6454                    )
6455                {
6456                    let content_matches = RefreshContent::matches(
6457                        refresh_checks.get(entry_index),
6458                        &index.entries[entry_index].oid,
6459                    );
6460                    let refresh = refresh_index_blob_stat_from_scan(
6461                        &mut index.entries[entry_index],
6462                        &stat,
6463                        index_mtime,
6464                        stat_policy,
6465                        content_matches,
6466                    );
6467                    if refresh.index_changed {
6468                        index_stat_refresh_changed = true;
6469                    }
6470                    if refresh.content_verified {
6471                        refresh_content_verified[entry_index] = true;
6472                        let path = index.entries[entry_index].path.clone();
6473                        let oid = index.entries[entry_index].oid;
6474                        index.mark_content_verified(&path, oid);
6475                    }
6476                }
6477                let ie = &index.entries[entry_index];
6478                let worktree_mode =
6479                    mode_policy.index_mode_for_worktree(stat.git_mode, Some(ie.mode));
6480                // The stat refresh already hashed this file and found other content, so do not
6481                // read it again. Staging hashes again because it needs the blob stored.
6482                if !materialize_dirty_blobs
6483                    && !stat.is_dir
6484                    && stat.git_mode != crate::index::MODE_SYMLINK
6485                    && RefreshContent::differs(refresh_checks.get(entry_index), &ie.oid)
6486                {
6487                    result.push(worktree_content_change_entry(
6488                        ie,
6489                        path_str_ref,
6490                        worktree_mode,
6491                        zero_oid(),
6492                    ));
6493                    return Ok(());
6494                }
6495                // Streaming jobs read regular files. Links and directories need the shared
6496                // classifier; use the physical type so core.symlinks=false placeholders
6497                // keep the regular-file fast path even with an effective symlink mode.
6498                if parallel_blob_work && !stat.is_dir && stat.git_mode != crate::index::MODE_SYMLINK
6499                {
6500                    let stat_same = stat_matches_blob_stat(ie, &stat, stat_policy);
6501                    if stat_same
6502                        && worktree_mode == ie.mode
6503                        && index_entry_skips_worktree_content_hash_after_stat_match(
6504                            ie,
6505                            index_mtime,
6506                            stat_policy,
6507                        )
6508                    {
6509                        if crate::fsmonitor::mark_fsmonitor_valid_proven_clean(index, entry_index) {
6510                            fsmonitor_valid_updated = true;
6511                        }
6512                        return Ok(());
6513                    }
6514                    if stat_same && worktree_mode != ie.mode {
6515                        result.push(worktree_content_change_entry(
6516                            ie,
6517                            path_str_ref,
6518                            worktree_mode,
6519                            ie.oid,
6520                        ));
6521                        return Ok(());
6522                    }
6523                    if !icase_collisions.as_ref().is_some_and(|map| {
6524                        map.diff_requires_worktree_hash(&index.entries[entry_index].path)
6525                    }) && try_push_status_modified_on_size_mismatch(
6526                        odb,
6527                        ie,
6528                        path_str_ref,
6529                        stat.worktree_size(),
6530                        worktree_mode,
6531                        stat.raw_size,
6532                        size_change_proves_modified,
6533                        || conversion_is_identity(entry_index, path_str_ref),
6534                        &mut result,
6535                    )? {
6536                        return Ok(());
6537                    }
6538                    if refresh_content_verified[entry_index] {
6539                        if worktree_mode == ie.mode {
6540                            if crate::fsmonitor::mark_fsmonitor_valid_proven_clean(
6541                                index,
6542                                entry_index,
6543                            ) {
6544                                fsmonitor_valid_updated = true;
6545                            }
6546                        } else {
6547                            result.push(worktree_content_change_entry(
6548                                ie,
6549                                path_str_ref,
6550                                worktree_mode,
6551                                ie.oid,
6552                            ));
6553                        }
6554                    } else {
6555                        staging_hash_jobs.push(StagingHashJob::new(
6556                            entry_index,
6557                            worktree_mode,
6558                            file_attrs_for(path_str_ref),
6559                        ));
6560                    }
6561                } else {
6562                    // The scan's lstat already proves an unchanged, non-racy entry clean, and the
6563                    // stat refresh above may have just verified a racy entry by content; only
6564                    // re-stat paths that `push_index_blob_worktree_diff` has to inspect.
6565                    let same_file_type = (worktree_mode == crate::index::MODE_SYMLINK)
6566                        == (ie.mode == crate::index::MODE_SYMLINK);
6567                    if !stat.is_dir
6568                        && worktree_mode == ie.mode
6569                        && ((refresh_content_verified[entry_index] && same_file_type)
6570                            || (stat_matches_blob_stat(ie, &stat, stat_policy)
6571                                && index_entry_skips_worktree_content_hash_after_stat_match(
6572                                    ie,
6573                                    index_mtime,
6574                                    stat_policy,
6575                                )))
6576                    {
6577                        if crate::fsmonitor::mark_fsmonitor_valid_proven_clean(index, entry_index) {
6578                            fsmonitor_valid_updated = true;
6579                        }
6580                        return Ok(());
6581                    }
6582                    let file_path = index_entry_worktree_abs_bytes(
6583                        work_tree,
6584                        &index.entries[entry_index].path,
6585                        precompose_unicode,
6586                        ignorecase,
6587                    );
6588                    let Some(meta) = symlink_metadata_worktree_diff(
6589                        &file_path,
6590                        path_str_ref,
6591                        options.diagnostics.as_ref(),
6592                        options.for_status,
6593                        options.skip_inaccessible_paths,
6594                        materialize_dirty_blobs,
6595                    )?
6596                    else {
6597                        return Ok(());
6598                    };
6599                    push_index_blob_worktree_diff(
6600                        odb,
6601                        ie,
6602                        path_str_ref,
6603                        &file_path,
6604                        meta,
6605                        index_mtime,
6606                        mode_policy,
6607                        stat_policy,
6608                        &file_attrs_for,
6609                        &conv,
6610                        materialize_dirty_blobs,
6611                        hash_blobs_without_write,
6612                        options.for_status,
6613                        size_change_proves_modified,
6614                        options.skip_inaccessible_paths,
6615                        options.diagnostics.as_ref(),
6616                        filter_fp,
6617                        icase_collisions.as_ref(),
6618                        &mut result,
6619                    )?;
6620                }
6621            }
6622        }
6623        Ok(())
6624    })?;
6625    if parallel_blob_work && !staging_hash_jobs.is_empty() {
6626        parallel_process_blob_worktree_jobs(
6627            odb,
6628            &StagingHashSource {
6629                index,
6630                work_tree,
6631                precompose_unicode,
6632                ignorecase,
6633            },
6634            &staging_hash_jobs,
6635            &thread_policy,
6636            &config,
6637            materialize_dirty_blobs,
6638            hash_blobs_without_write,
6639            options.for_status,
6640            options.skip_inaccessible_paths,
6641            options.diagnostics.as_ref(),
6642            &conv,
6643            filter_fp,
6644            icase_collisions.as_ref(),
6645            &mut result,
6646        )?;
6647    }
6648
6649    for (path, (_, base_entry)) in unmerged_base {
6650        let file_path = index_entry_worktree_abs(work_tree, &path, precompose_unicode, ignorecase);
6651        let wt_meta = symlink_metadata_worktree_diff(
6652            &file_path,
6653            &path,
6654            options.diagnostics.as_ref(),
6655            options.for_status,
6656            options.skip_inaccessible_paths,
6657            materialize_dirty_blobs,
6658        )?;
6659
6660        let new_mode = wt_meta.as_ref().map_or_else(
6661            || FileMode::ABSENT,
6662            |meta| FileMode::new(mode_from_metadata(meta)),
6663        );
6664        result.push(DiffEntry {
6665            status: DiffStatus::Unmerged,
6666            old_path: Some(path.clone()),
6667            new_path: Some(path.clone()),
6668            old_mode: FileMode::ABSENT,
6669            new_mode,
6670            old_oid: zero_oid(),
6671            new_oid: zero_oid(),
6672            score: None,
6673        });
6674
6675        if !options.for_status {
6676            if let Some(meta) = wt_meta {
6677                let file_attrs = file_attrs_for(&path);
6678                let wt_oid = worktree_file_oid(
6679                    odb,
6680                    &file_path,
6681                    &meta,
6682                    &conv,
6683                    &file_attrs,
6684                    &path,
6685                    Some(&base_entry),
6686                    materialize_dirty_blobs,
6687                    filter_fp,
6688                )?;
6689                let wt_mode = mode_from_metadata(&meta);
6690                if wt_oid != base_entry.oid || wt_mode != base_entry.mode {
6691                    result.push(DiffEntry {
6692                        status: DiffStatus::for_content_change(base_entry.mode, wt_mode),
6693                        old_path: Some(path.clone()),
6694                        new_path: Some(path),
6695                        old_mode: FileMode::new(base_entry.mode),
6696                        new_mode: FileMode::new(wt_mode),
6697                        old_oid: base_entry.oid,
6698                        new_oid: wt_oid,
6699                        score: None,
6700                    });
6701                }
6702            }
6703        }
6704    }
6705
6706    if fsmonitor_valid_updated {
6707        index.fsmonitor_validity_changed();
6708        index_stat_refresh_changed = true;
6709    }
6710
6711    Ok((result, index_stat_refresh_changed, staging_stat_cache))
6712}
6713
6714/// Per-worker cache answering "is any ancestor directory of this index path a symlink?".
6715///
6716/// It remembers the last chain of ancestors verified to be real directories, plus the first
6717/// ancestor found not to be one. Index paths arrive in sorted order, so consecutive queries
6718/// share most of their leading directories and only the differing components are probed.
6719/// Each scan worker owns its own cache: there is no locking and no per-directory allocation.
6720#[derive(Default)]
6721pub(crate) struct SymlinkDirCache {
6722    /// Leading directory (raw index bytes, no trailing `/`) whose every component is a real
6723    /// directory on disk.
6724    real_dirs: Vec<u8>,
6725    /// First ancestor found not to be a real directory, and whether it is a symlink. Every
6726    /// path under it shares that answer.
6727    blocked: Option<(Vec<u8>, bool)>,
6728    /// Single-walk check for directories several components below `real_dirs`.
6729    #[cfg(all(target_os = "linux", target_pointer_width = "64"))]
6730    chain: Option<crate::worktree_scan::RealDirChain>,
6731    /// Filesystem queries made so far.
6732    #[cfg(test)]
6733    fs_queries: usize,
6734}
6735
6736/// Whether `path` is `dir` itself or lies under it (`dir` has no trailing `/`).
6737fn path_within_dir(path: &[u8], dir: &[u8]) -> bool {
6738    path.starts_with(dir) && path.get(dir.len()).is_none_or(|&b| b == b'/')
6739}
6740
6741/// Length of the longest leading run of whole components shared by two directory paths.
6742fn common_dir_prefix_len(a: &[u8], b: &[u8]) -> usize {
6743    let same = a.iter().zip(b).take_while(|(x, y)| x == y).count();
6744    let ends_component = |p: &[u8]| p.get(same).is_none_or(|&c| c == b'/');
6745    if ends_component(a) && ends_component(b) {
6746        return same;
6747    }
6748    a[..same].iter().rposition(|&c| c == b'/').unwrap_or(0)
6749}
6750
6751pub(crate) fn index_entry_worktree_abs(
6752    work_tree: &Path,
6753    index_relpath: &str,
6754    precompose_unicode: bool,
6755    ignorecase: bool,
6756) -> PathBuf {
6757    index_entry_worktree_abs_bytes(
6758        work_tree,
6759        index_relpath.as_bytes(),
6760        precompose_unicode,
6761        ignorecase,
6762    )
6763}
6764
6765pub(crate) fn index_entry_worktree_abs_bytes(
6766    work_tree: &Path,
6767    index_relpath: &[u8],
6768    precompose_unicode: bool,
6769    ignorecase: bool,
6770) -> PathBuf {
6771    if precompose_unicode || ignorecase {
6772        if let Ok(s) = std::str::from_utf8(index_relpath) {
6773            if precompose_unicode {
6774                return crate::unicode_normalization::resolve_worktree_path_for_staging(
6775                    work_tree, s, true,
6776                )
6777                .abs;
6778            }
6779            return crate::path_icase::worktree_path_for_index_entry(work_tree, s, ignorecase);
6780        }
6781    }
6782    crate::index::worktree_path_from_index_rel(work_tree, index_relpath)
6783}
6784
6785/// Effective index-file mtime for racy-git checks.
6786///
6787/// Uses an explicit override when provided, otherwise [`Index::source_mtime`] from the last
6788/// on-disk read.
6789#[must_use]
6790pub fn index_mtime_for_diff(index: &Index, explicit: Option<(u32, u32)>) -> Option<(u32, u32)> {
6791    explicit.or(index.source_mtime)
6792}
6793
6794impl SymlinkDirCache {
6795    /// Whether any parent directory of `rel_path` (raw index path bytes) is a symlink.
6796    ///
6797    /// Assumes the worktree does not change during the scan this cache serves. A missing
6798    /// ancestor, or one that is a file, is not a symlink and hides everything below it.
6799    pub(crate) fn has_symlink_in_path_bytes(
6800        &mut self,
6801        work_tree: &Path,
6802        rel_path: &[u8],
6803        precompose_unicode: bool,
6804        ignorecase: bool,
6805    ) -> bool {
6806        use crate::worktree_scan::{ancestor_kind, AncestorKind};
6807
6808        let Some(dir_len) = rel_path.iter().rposition(|&b| b == b'/') else {
6809            return false;
6810        };
6811        let dir = &rel_path[..dir_len];
6812        if let Some((blocked, is_symlink)) = &self.blocked {
6813            if path_within_dir(dir, blocked) {
6814                return *is_symlink;
6815            }
6816        }
6817        let verified = common_dir_prefix_len(dir, &self.real_dirs);
6818        self.real_dirs.truncate(verified);
6819        #[cfg(all(target_os = "linux", target_pointer_width = "64"))]
6820        if !precompose_unicode
6821            && !ignorecase
6822            && dir[verified..]
6823                .split(|&b| b == b'/')
6824                .filter(|component| !component.is_empty())
6825                .nth(1)
6826                .is_some()
6827        {
6828            let chain = match &mut self.chain {
6829                Some(chain) if chain.work_tree() == work_tree => chain,
6830                slot => slot.insert(crate::worktree_scan::RealDirChain::open(work_tree)),
6831            };
6832            #[cfg(test)]
6833            {
6834                self.fs_queries += 1;
6835            }
6836            if chain.confirms(dir) {
6837                self.real_dirs.clear();
6838                self.real_dirs.extend_from_slice(dir);
6839                return false;
6840            }
6841        }
6842        let mut start = verified;
6843        while start < dir.len() {
6844            if dir[start] == b'/' {
6845                start += 1;
6846                continue;
6847            }
6848            let end = dir[start..]
6849                .iter()
6850                .position(|&b| b == b'/')
6851                .map_or(dir.len(), |i| start + i);
6852            let prefix = &dir[..end];
6853            let abs =
6854                index_entry_worktree_abs_bytes(work_tree, prefix, precompose_unicode, ignorecase);
6855            #[cfg(test)]
6856            {
6857                self.fs_queries += 1;
6858            }
6859            match ancestor_kind(&abs) {
6860                AncestorKind::Directory => {
6861                    let known = self.real_dirs.len();
6862                    self.real_dirs.extend_from_slice(&prefix[known..]);
6863                }
6864                AncestorKind::Symlink => {
6865                    self.blocked = Some((prefix.to_vec(), true));
6866                    return true;
6867                }
6868                AncestorKind::Other => {
6869                    self.blocked = Some((prefix.to_vec(), false));
6870                    return false;
6871                }
6872            }
6873            start = end;
6874        }
6875        false
6876    }
6877}
6878
6879/// Whether an index entry's cached mtime is at or after the on-disk index file's mtime.
6880///
6881/// Git labels such entries racy: stat data can look clean even though the worktree changed
6882/// within the same second the index was written. Callers pass the index file mtime sampled when
6883/// the index was read (before any rewrite). When `index_mtime` is `None`, returns `false`.
6884#[must_use]
6885pub(crate) fn entry_is_racy(ie: &IndexEntry, index_mtime: Option<(u32, u32)>) -> bool {
6886    entry_is_racy_with_policy(ie, index_mtime, crate::index::StatMatchPolicy::default())
6887}
6888
6889/// Like [`entry_is_racy`], honoring `core.usenanosec` for sub-second racy detection.
6890///
6891/// Sub-second racy detection applies only when stat equality also compares the fractions
6892/// (`core.usenanosec` with `core.checkstat=default`); otherwise a same-second, same-size edit
6893/// would be neither racy nor a stat mismatch.
6894#[must_use]
6895pub(crate) fn entry_is_racy_with_policy(
6896    ie: &IndexEntry,
6897    index_mtime: Option<(u32, u32)>,
6898    policy: crate::index::StatMatchPolicy,
6899) -> bool {
6900    let Some((index_mtime_sec, index_mtime_nsec)) = index_mtime else {
6901        return false;
6902    };
6903    if index_mtime_sec == 0 {
6904        return false;
6905    }
6906    if policy.use_nanosec && policy.check_stat {
6907        index_mtime_sec < ie.mtime_sec
6908            || (index_mtime_sec == ie.mtime_sec && index_mtime_nsec <= ie.mtime_nsec)
6909    } else {
6910        index_mtime_sec <= ie.mtime_sec
6911    }
6912}
6913
6914/// Clear racily-clean cache lines on entries whose blob no longer matches the worktree.
6915///
6916/// Before Git writes the index, it walks stage-0 file and symlink entries and sets the cached
6917/// size to zero when stat data still matches the worktree but content does not match the
6918/// recorded OID and the entry timestamp is racy relative to `index_mtime`. That forces the
6919/// next diff or status pass to re-hash the path instead of trusting stat alone.
6920///
6921/// Skip-worktree, assume-unchanged, and intent-to-add entries are left unchanged, and so are
6922/// entries this process just hashed from the worktree to their current OID (Git skips its
6923/// `CE_UPTODATE` entries the same way). When `index_mtime` is `None` (no prior on-disk index),
6924/// nothing is smudged.
6925///
6926/// Content equality uses `git_dir` config and worktree attributes with [`hash_worktree_file`]
6927/// so the repository hash algorithm and clean (CRLF/filter) conversion match index OIDs.
6928///
6929/// Returns `true` when at least one entry was smudged.
6930pub(crate) fn smudge_racily_clean_entries(
6931    odb: &Odb,
6932    git_dir: &Path,
6933    index: &mut Index,
6934    work_tree: &Path,
6935    index_mtime: Option<(u32, u32)>,
6936    config: Option<&ConfigSet>,
6937    filter_process: Option<&crate::filter_process::FilterProcessState>,
6938) -> bool {
6939    use crate::crlf;
6940    use crate::index::{MODE_EXECUTABLE, MODE_REGULAR, MODE_SYMLINK};
6941
6942    let Some(index_mtime) = index_mtime else {
6943        return false;
6944    };
6945
6946    let config = config.cloned().unwrap_or_else(|| {
6947        ConfigSet::load(
6948            &crate::environment::Environment::empty(),
6949            Some(git_dir),
6950            &crate::config::LoadConfigOptions::standard(
6951                &crate::environment::Environment::empty(),
6952                Some(git_dir),
6953            ),
6954        )
6955        .unwrap_or_default()
6956    });
6957    let conv = crlf::ConversionConfig::from_config(&config);
6958    let attrs = crlf::load_gitattributes(work_tree);
6959    let verified = std::mem::take(&mut index.content_verified);
6960
6961    let mut changed = false;
6962    for ie in &mut index.entries {
6963        let marked_verified = ie.take_content_verified();
6964        if ie.stage() != 0 || ie.skip_worktree() || ie.assume_unchanged() || ie.intent_to_add() {
6965            continue;
6966        }
6967        if ie.mode != MODE_REGULAR && ie.mode != MODE_EXECUTABLE && ie.mode != MODE_SYMLINK {
6968            continue;
6969        }
6970        if ie.size == 0 {
6971            continue;
6972        }
6973        if !entry_is_racy(ie, Some(index_mtime)) {
6974            continue;
6975        }
6976        if marked_verified || verified.get(&ie.path) == Some(&ie.oid) {
6977            continue;
6978        }
6979        let Ok(path) = std::str::from_utf8(&ie.path) else {
6980            continue;
6981        };
6982        let abs = work_tree.join(path);
6983        let Ok(meta) = fs::symlink_metadata(&abs) else {
6984            continue;
6985        };
6986        if !stat_matches(ie, &meta) {
6987            continue;
6988        }
6989        let file_attrs = crlf::get_file_attrs(&attrs, path, false, &config);
6990        let content_matches = hash_worktree_file(
6991            odb,
6992            &abs,
6993            &meta,
6994            &conv,
6995            &file_attrs,
6996            path,
6997            Some(ie),
6998            filter_process,
6999        )
7000        .map(|oid| oid == ie.oid)
7001        .unwrap_or(false);
7002        if content_matches {
7003            continue;
7004        }
7005        ie.size = 0;
7006        changed = true;
7007    }
7008    changed
7009}
7010/// Quick stat check: does the index entry's cached stat data match the file?
7011/// Returns true when the file at `ie`'s path differs from the index entry (mode or blob).
7012///
7013/// Used by commands such as `git mv` to detect "dirty" paths under sparse checkout.
7014/// Symlinks and submodules are compared in a Git-compatible way.
7015///
7016/// `ignore_submodule_untracked` mirrors [`diff_index_to_worktree`]'s same flag for gitlinks.
7017#[allow(dead_code)]
7018fn worktree_differs_from_index_entry(
7019    odb: &Odb,
7020    repository_git_dir: &Path,
7021    work_tree: &Path,
7022    ie: &IndexEntry,
7023    ignore_submodule_untracked: bool,
7024    config: Option<&ConfigSet>,
7025    filter_process: Option<&crate::filter_process::FilterProcessState>,
7026) -> Result<bool> {
7027    WorktreeEntryComparer::new(repository_git_dir, work_tree, config).differs(
7028        odb,
7029        ie,
7030        ignore_submodule_untracked,
7031        filter_process,
7032    )
7033}
7034
7035/// Repeated [`worktree_differs_from_index_entry`] checks against one worktree.
7036///
7037/// Loads config-derived settings and the root `.gitattributes` once instead of per entry.
7038pub(crate) struct WorktreeEntryComparer<'a> {
7039    work_tree: &'a Path,
7040    config: ConfigSet,
7041    mode_policy: crate::index::WorktreeModePolicy,
7042    precompose_unicode: bool,
7043    ignorecase: bool,
7044    conv: std::cell::OnceCell<crate::crlf::ConversionConfig>,
7045    attrs: std::cell::OnceCell<Vec<crate::crlf::AttrRule>>,
7046}
7047
7048impl<'a> WorktreeEntryComparer<'a> {
7049    /// Capture settings for `work_tree`; `config` falls back to the repository config files.
7050    pub fn new(repository_git_dir: &Path, work_tree: &'a Path, config: Option<&ConfigSet>) -> Self {
7051        let config = config.cloned().unwrap_or_else(|| {
7052            ConfigSet::load(
7053                &crate::environment::Environment::empty(),
7054                Some(repository_git_dir),
7055                &crate::config::LoadConfigOptions::standard(
7056                    &crate::environment::Environment::empty(),
7057                    Some(repository_git_dir),
7058                ),
7059            )
7060            .unwrap_or_else(|_| ConfigSet::new())
7061        });
7062        let flag = |key: &str, default: bool| config.bool_with_default(key, default);
7063        Self {
7064            work_tree,
7065            mode_policy: crate::index::WorktreeModePolicy::from_config(&config),
7066            precompose_unicode: flag("core.precomposeunicode", false),
7067            ignorecase: flag("core.ignorecase", false),
7068            config,
7069            conv: std::cell::OnceCell::new(),
7070            attrs: std::cell::OnceCell::new(),
7071        }
7072    }
7073
7074    /// Same contract as [`worktree_differs_from_index_entry`].
7075    pub(crate) fn differs(
7076        &self,
7077        odb: &Odb,
7078        ie: &IndexEntry,
7079        ignore_submodule_untracked: bool,
7080        filter_process: Option<&crate::filter_process::FilterProcessState>,
7081    ) -> Result<bool> {
7082        use crate::crlf;
7083
7084        let work_tree = self.work_tree;
7085        let path_stored = crate::repo_relpath::storage_string(&ie.path);
7086        let path_str_ref = crate::repo_relpath::as_utf8_for_matching(&path_stored);
7087        let file_path = index_entry_worktree_abs_bytes(
7088            work_tree,
7089            &ie.path,
7090            self.precompose_unicode,
7091            self.ignorecase,
7092        );
7093
7094        if ie.mode == 0o160000 {
7095            let sub_head_oid = read_submodule_head(&file_path);
7096            let ref_matches = match sub_head_oid {
7097                Some(oid) => oid == ie.oid,
7098                None => uninitialized_gitlink_worktree_matches_index(&file_path),
7099            };
7100            let mut flags = submodule_porcelain_flags(work_tree, path_str_ref.as_ref(), ie.oid);
7101            if ignore_submodule_untracked {
7102                flags.untracked = false;
7103            }
7104            return Ok(!ref_matches || flags.modified || flags.untracked);
7105        }
7106
7107        let meta = match fs::symlink_metadata(&file_path) {
7108            Ok(m) => m,
7109            Err(e)
7110                if e.kind() == std::io::ErrorKind::NotFound
7111                    || e.raw_os_error() == Some(20) /* ENOTDIR */ =>
7112            {
7113                return Ok(true);
7114            }
7115            Err(e) => return Err(Error::Io(e)),
7116        };
7117
7118        if meta.is_dir() {
7119            return Ok(true);
7120        }
7121
7122        if !self
7123            .mode_policy
7124            .worktree_mode_matches(mode_from_metadata(&meta), ie.mode)
7125        {
7126            return Ok(true);
7127        }
7128
7129        let conv = self
7130            .conv
7131            .get_or_init(|| crlf::ConversionConfig::from_config(&self.config));
7132        let attrs = self
7133            .attrs
7134            .get_or_init(|| crlf::load_gitattributes(work_tree));
7135        let file_attrs = crlf::get_file_attrs(attrs, path_str_ref.as_ref(), false, &self.config);
7136        let worktree_oid = hash_worktree_file(
7137            odb,
7138            &file_path,
7139            &meta,
7140            conv,
7141            &file_attrs,
7142            path_str_ref.as_ref(),
7143            Some(ie),
7144            filter_process,
7145        )?;
7146
7147        let mut eff_oid = worktree_oid;
7148        if eff_oid != ie.oid {
7149            if let Ok(raw) = fs::read(&file_path) {
7150                let raw_oid = odb.hash(ObjectKind::Blob, &raw)?;
7151                if raw_oid == ie.oid {
7152                    eff_oid = ie.oid;
7153                }
7154            }
7155        }
7156
7157        Ok(eff_oid != ie.oid)
7158    }
7159}
7160
7161/// Inputs for [`classify_worktree_entry_for_add`].
7162pub struct WorktreeAddRefreshParams<'a> {
7163    /// Object database used to verify blob OIDs when stat data is inconclusive.
7164    pub odb: &'a Odb,
7165    /// Index entry being compared.
7166    pub ie: &'a IndexEntry,
7167    /// `lstat` metadata for the worktree path.
7168    pub meta: &'a fs::Metadata,
7169    /// Absolute worktree path to the file.
7170    pub abs_path: &'a Path,
7171    /// Repository-relative path string.
7172    pub rel_path: &'a str,
7173    /// CRLF / filter conversion settings.
7174    pub conv: &'a crate::crlf::ConversionConfig,
7175    /// Per-path attribute state.
7176    pub file_attrs: &'a crate::crlf::FileAttrs,
7177    /// On-disk index mtime when the index was loaded; `None` skips racy checks.
7178    pub index_mtime: Option<(u32, u32)>,
7179    /// Mode that would be written, after [`crate::index::WorktreeModePolicy`] against `ie.mode`
7180    /// and any `--chmod` hint.
7181    pub staged_mode: u32,
7182    /// Repository-scoped filter-process registry (when staging through a [`crate::repo::Repository`]).
7183    pub filter_process: Option<&'a crate::filter_process::FilterProcessState>,
7184}
7185
7186/// Outcome of comparing a tracked index entry to its worktree path for staging refresh.
7187#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7188pub enum WorktreeAddRefresh {
7189    /// Index already reflects the worktree; caller may skip staging.
7190    UpToDate,
7191    /// Executable bit differs while blob content is unchanged.
7192    ModeOnly {
7193        /// Mode to store in the index.
7194        mode: u32,
7195    },
7196    /// Blob OID and mode are unchanged; only cached stat fields should be refreshed.
7197    StatOnly,
7198    /// Content, type, or mode/blob mismatch requires a full restage (read, convert, hash).
7199    NeedsRestage,
7200}
7201
7202/// Whether stat-only fast paths may skip re-hashing during staging.
7203#[must_use]
7204pub(crate) fn index_stat_cache_trustworthy(index_mtime: Option<(u32, u32)>) -> bool {
7205    match index_mtime {
7206        None => false,
7207        Some((0, _)) => false,
7208        Some(_) => true,
7209    }
7210}
7211
7212/// When true, after worktree metadata has been read and matches the index entry, blob content
7213/// hashing may be skipped (Git `diff-files` / `update-index --refresh` style).
7214///
7215/// Does not permit skipping worktree observation; fsmonitor-valid entries use a separate path.
7216/// Independent of `core.filemode`: callers compare the executable bit separately.
7217///
7218/// A zero cached size is trusted only for the empty blob; any other blob with size zero was
7219/// smudged by a racy index write and must be re-hashed (Git `ce_match_stat_basic`).
7220#[must_use]
7221pub(crate) fn index_entry_skips_worktree_content_hash_after_stat_match(
7222    ie: &IndexEntry,
7223    index_mtime: Option<(u32, u32)>,
7224    stat_policy: crate::index::StatMatchPolicy,
7225) -> bool {
7226    if ie.stat_cache_unset() || (ie.size == 0 && !ie.oid.is_empty_blob()) {
7227        return false;
7228    }
7229    if !index_stat_cache_trustworthy(index_mtime) {
7230        return false;
7231    }
7232    !entry_is_racy_with_policy(ie, index_mtime, stat_policy)
7233}
7234
7235/// Decide whether staging must re-read and re-hash a tracked path.
7236///
7237/// Mirrors the fast paths in [`diff_index_to_worktree`] and Git's `refresh_index` /
7238/// `add_file_to_index`: trust matching stat when the entry is not racy and
7239/// `index_stat_cache_trustworthy` allows it; re-hash when stat matches but the entry is racy
7240/// or when stat differs; apply CRLF-aware hashing like staging.
7241///
7242/// Returns [`WorktreeAddRefresh::UpToDate`] when the index entry already matches the worktree and
7243/// no restaging is required; other variants describe mode/stat-only updates or a full restage.
7244///
7245/// # Errors
7246///
7247/// Returns [`Error::Io`] when the worktree file cannot be read for content verification.
7248pub(crate) fn classify_worktree_entry_for_add(
7249    params: &WorktreeAddRefreshParams<'_>,
7250) -> Result<WorktreeAddRefresh> {
7251    use crate::index::{MODE_EXECUTABLE, MODE_REGULAR, MODE_SYMLINK};
7252
7253    let ie = params.ie;
7254    let meta = params.meta;
7255    let staged_mode = params.staged_mode;
7256
7257    if ie.intent_to_add() {
7258        return Ok(WorktreeAddRefresh::NeedsRestage);
7259    }
7260
7261    let racy = entry_is_racy(ie, params.index_mtime);
7262    let stat_same = stat_matches(ie, meta);
7263    let trust_stat = index_stat_cache_trustworthy(params.index_mtime);
7264
7265    if ie.mode == MODE_SYMLINK {
7266        // With `core.symlinks=false` the policy keeps a plain file holding the link target as
7267        // a symlink entry, so it is compared by content like a real link.
7268        if !meta.file_type().is_symlink() && staged_mode != MODE_SYMLINK {
7269            return Ok(WorktreeAddRefresh::NeedsRestage);
7270        }
7271        if stat_same && !racy && trust_stat && staged_mode == ie.mode {
7272            return Ok(WorktreeAddRefresh::UpToDate);
7273        }
7274        if stat_same && !racy && trust_stat && staged_mode != ie.mode {
7275            return Ok(WorktreeAddRefresh::ModeOnly { mode: staged_mode });
7276        }
7277        return worktree_effective_oid_matches_index(params);
7278    }
7279
7280    if ie.mode != MODE_REGULAR && ie.mode != MODE_EXECUTABLE {
7281        return Ok(WorktreeAddRefresh::NeedsRestage);
7282    }
7283
7284    if meta.file_type().is_symlink() || meta.is_dir() {
7285        return Ok(WorktreeAddRefresh::NeedsRestage);
7286    }
7287
7288    if stat_same && !racy && trust_stat {
7289        if staged_mode == ie.mode {
7290            return Ok(WorktreeAddRefresh::UpToDate);
7291        }
7292        return Ok(WorktreeAddRefresh::ModeOnly { mode: staged_mode });
7293    }
7294
7295    worktree_effective_oid_matches_index(params)
7296}
7297
7298fn worktree_effective_oid_matches_index(
7299    params: &WorktreeAddRefreshParams<'_>,
7300) -> Result<WorktreeAddRefresh> {
7301    let WorktreeAddRefreshParams {
7302        odb,
7303        ie,
7304        meta,
7305        abs_path,
7306        rel_path,
7307        conv,
7308        file_attrs,
7309        staged_mode,
7310        ..
7311    } = params;
7312    let worktree_oid = hash_worktree_file(
7313        odb,
7314        abs_path,
7315        meta,
7316        conv,
7317        file_attrs,
7318        rel_path,
7319        Some(ie),
7320        params.filter_process,
7321    )?;
7322    let mut eff_oid = worktree_oid;
7323    if eff_oid != ie.oid {
7324        if let Ok(raw) = fs::read(abs_path) {
7325            let raw_oid = odb.hash(ObjectKind::Blob, &raw)?;
7326            if raw_oid == ie.oid {
7327                eff_oid = ie.oid;
7328            }
7329        }
7330    }
7331    if eff_oid != ie.oid {
7332        return Ok(WorktreeAddRefresh::NeedsRestage);
7333    }
7334    if *staged_mode != ie.mode {
7335        return Ok(WorktreeAddRefresh::ModeOnly { mode: *staged_mode });
7336    }
7337    if stat_matches(ie, meta) {
7338        Ok(WorktreeAddRefresh::UpToDate)
7339    } else {
7340        Ok(WorktreeAddRefresh::StatOnly)
7341    }
7342}
7343
7344/// Whether worktree metadata matches cached index stat data (Git `ie_match_stat` / `match_stat_data`).
7345#[must_use]
7346pub fn stat_matches(ie: &IndexEntry, meta: &fs::Metadata) -> bool {
7347    stat_matches_with_policy(ie, meta, crate::index::StatMatchPolicy::default())
7348}
7349
7350/// Whether compact worktree stat from a directory scan matches the index row.
7351pub(crate) fn stat_matches_blob_stat(
7352    ie: &IndexEntry,
7353    stat: &crate::worktree_scan::BlobDiskStat,
7354    policy: crate::index::StatMatchPolicy,
7355) -> bool {
7356    stat.fields.matches_index_entry(ie, policy)
7357}
7358
7359/// Like [`stat_matches`], honoring `core.trustctime`, `core.checkstat`, and `core.usenanosec`.
7360#[must_use]
7361pub(crate) fn stat_matches_with_policy(
7362    ie: &IndexEntry,
7363    meta: &fs::Metadata,
7364    policy: crate::index::StatMatchPolicy,
7365) -> bool {
7366    if crate::index::munge_index_size(meta.len()) != ie.size {
7367        return false;
7368    }
7369    #[cfg(unix)]
7370    {
7371        use std::os::unix::fs::MetadataExt;
7372        if meta.mtime() as u32 != ie.mtime_sec {
7373            return false;
7374        }
7375        if policy.use_nanosec && policy.check_stat && meta.mtime_nsec() as u32 != ie.mtime_nsec {
7376            return false;
7377        }
7378        if policy.trust_ctime && policy.check_stat {
7379            if meta.ctime() as u32 != ie.ctime_sec {
7380                return false;
7381            }
7382            if policy.use_nanosec && meta.ctime_nsec() as u32 != ie.ctime_nsec {
7383                return false;
7384            }
7385        }
7386        if policy.check_stat && meta.ino() as u32 != ie.ino {
7387            return false;
7388        }
7389    }
7390    #[cfg(not(unix))]
7391    {
7392        let ((ctime_sec, ctime_nsec), (mtime_sec, mtime_nsec)) = crate::index::metadata_times(meta);
7393        if mtime_sec != ie.mtime_sec {
7394            return false;
7395        }
7396        if policy.use_nanosec && policy.check_stat && mtime_nsec != ie.mtime_nsec {
7397            return false;
7398        }
7399        if policy.trust_ctime && policy.check_stat {
7400            if ctime_sec != ie.ctime_sec {
7401                return false;
7402            }
7403            if policy.use_nanosec && ctime_nsec != ie.ctime_nsec {
7404                return false;
7405            }
7406        }
7407    }
7408    true
7409}
7410
7411/// Refresh cached stat data for stage-0 file/symlink entries whose worktree content still matches
7412/// the recorded OID but whose on-disk stat went stale.
7413///
7414/// This mirrors Git's `refresh_index` / `refresh_cache_ent`: an entry is only marked clean (stat
7415/// adopted from the worktree) after its content is re-verified against the index OID. A genuinely
7416/// modified entry keeps its stale stat so `diff-files` / `status` continue to report it. Operations
7417/// that rewrite the worktree (`status`, `reset --mixed`, `stash`) call this before writing the
7418/// index so a subsequent `git diff-files` sees refreshed entries as clean.
7419///
7420/// Gitlinks, sparse (`skip_worktree`), `assume_unchanged` and intent-to-add entries are skipped.
7421/// The blob comparison is a raw-content hash, so a CRLF-smudged match is conservatively missed
7422/// (the entry simply stays stat-dirty and is re-hashed next time — never the reverse).
7423///
7424/// `index_mtime` is the on-disk index file's `(mtime_sec, mtime_nsec)` (see
7425/// `entry_is_racy` / Git `is_racy_timestamp`); pass `None` when unknown — racy detection is
7426/// then skipped, which is conservative for tree-built indexes whose zeroed stat never matches.
7427///
7428/// `stat_parallel_threads` overrides parallel stat workers (`Some(1)` forces serial); when `None`,
7429/// honors `core.preloadIndex` and `PARALLEL_STAT_MIN_ENTRIES`.
7430///
7431/// Returns `Ok(true)` when at least one entry was refreshed or invalidated, so callers can write
7432/// the index opportunistically (Git only persists a refresh that changed something).
7433pub fn refresh_index_stat_content_verified(
7434    odb: &Odb,
7435    git_dir: &Path,
7436    index: &mut Index,
7437    work_tree: &Path,
7438    index_mtime: Option<(u32, u32)>,
7439    config: Option<&ConfigSet>,
7440    _stat_parallel_threads: Option<usize>,
7441) -> Result<bool> {
7442    let _ = _stat_parallel_threads;
7443    refresh_index_stat_content_verified_with_rules(
7444        odb,
7445        git_dir,
7446        index,
7447        work_tree,
7448        index_mtime,
7449        config,
7450        _stat_parallel_threads,
7451        None,
7452    )
7453}
7454
7455/// Like [`refresh_index_stat_content_verified`], resolving attributes and conversion through a
7456/// shared [`crate::worktree_rules::WorktreeRules`] when one is supplied (its config snapshot then
7457/// takes precedence over `config`).
7458///
7459/// # Errors
7460///
7461/// Propagates I/O errors from the directory-grouped worktree scan.
7462#[allow(clippy::too_many_arguments)]
7463pub(crate) fn refresh_index_stat_content_verified_with_rules(
7464    odb: &Odb,
7465    git_dir: &Path,
7466    index: &mut Index,
7467    work_tree: &Path,
7468    index_mtime: Option<(u32, u32)>,
7469    config: Option<&ConfigSet>,
7470    stat_parallel_threads: Option<usize>,
7471    worktree_rules: Option<&std::sync::Arc<std::sync::Mutex<crate::worktree_rules::WorktreeRules>>>,
7472) -> Result<bool> {
7473    use crate::config::ConfigSet;
7474    use crate::crlf;
7475    use crate::worktree_scan::{
7476        collect_blob_lookup_batches, group_blob_entries_by_dir, visit_blob_lookup_batches,
7477        BlobDiskLookup, BlobScanSparseMode, WorktreeBlobScanOptions,
7478    };
7479
7480    let index_mtime = index_mtime_for_diff(index, index_mtime);
7481    let rules_lock = worktree_rules.map(|shared| {
7482        shared
7483            .lock()
7484            .unwrap_or_else(std::sync::PoisonError::into_inner)
7485    });
7486    let config = rules_lock
7487        .as_ref()
7488        .map(|r| r.config().clone())
7489        .unwrap_or_else(|| {
7490            config.cloned().unwrap_or_else(|| {
7491                ConfigSet::load(
7492                    &crate::environment::Environment::empty(),
7493                    Some(git_dir),
7494                    &crate::config::LoadConfigOptions::standard(
7495                        &crate::environment::Environment::empty(),
7496                        Some(git_dir),
7497                    ),
7498                )
7499                .unwrap_or_default()
7500            })
7501        });
7502    let preload_index = config.preload_index();
7503    let blob_scan = WorktreeBlobScanOptions {
7504        preload_index,
7505        stat_parallel_threads,
7506        ..WorktreeBlobScanOptions::default()
7507    };
7508    let conv = rules_lock
7509        .as_ref()
7510        .map(|r| r.conversion().clone())
7511        .unwrap_or_else(|| crlf::ConversionConfig::from_config(&config));
7512    let legacy_attrs = if rules_lock.is_none() {
7513        Some(crlf::load_gitattributes_parsed(work_tree))
7514    } else {
7515        None
7516    };
7517    let file_attrs_for = |path: &str| -> crlf::FileAttrs {
7518        if let Some(rules) = rules_lock.as_ref() {
7519            rules.file_attrs(path, false)
7520        } else if let Some(parsed) = legacy_attrs.as_ref() {
7521            crlf::file_attrs_from_gitattributes(&parsed.rules, &parsed.macros, path, false, &config)
7522        } else {
7523            crlf::FileAttrs::default()
7524        }
7525    };
7526    let precompose_unicode = config.precompose_unicode();
7527    let ignorecase = config.ignore_case();
7528    let stat_policy = crate::index::StatMatchPolicy::from_config(Some(&config));
7529
7530    let blob_dirs = group_blob_entries_by_dir(index, false);
7531    let work_tree_owned = work_tree.to_path_buf();
7532    let dir_abs = move |dir: &[u8]| {
7533        if dir.is_empty() {
7534            work_tree_owned.clone()
7535        } else {
7536            index_entry_worktree_abs_bytes(&work_tree_owned, dir, precompose_unicode, ignorecase)
7537        }
7538    };
7539    let work_tree_for_files = work_tree.to_path_buf();
7540    let file_abs = move |rel: &[u8]| {
7541        index_entry_worktree_abs_bytes(&work_tree_for_files, rel, precompose_unicode, ignorecase)
7542    };
7543    let mut changed = false;
7544    let filter_fp = rules_lock.as_ref().map(|r| r.filter_process());
7545    let thread_policy = crate::parallel::ThreadPolicy::default();
7546    // Symlink ancestor detection must run even when core.symlinks is false.
7547    let refresh_batches = collect_blob_lookup_batches(
7548        index,
7549        &blob_dirs,
7550        blob_scan,
7551        &thread_policy,
7552        &config,
7553        dir_abs,
7554        file_abs,
7555        || {
7556            let mut dir_symlinks = SymlinkDirCache::default();
7557            move |rel_path: &[u8]| {
7558                dir_symlinks.has_symlink_in_path_bytes(
7559                    work_tree,
7560                    rel_path,
7561                    precompose_unicode,
7562                    ignorecase,
7563                )
7564            }
7565        },
7566        BlobScanSparseMode::StatRefresh {
7567            stat_policy,
7568            index_mtime,
7569        },
7570    )?;
7571    let par_opts = refresh_hash_parallel_options(&thread_policy, &config, stat_parallel_threads);
7572    let refresh_checks = refresh_content_checks(
7573        odb,
7574        index,
7575        &refresh_batches,
7576        index_mtime,
7577        stat_policy,
7578        &|_, _, _| true,
7579        &WorktreeHashContext {
7580            work_tree,
7581            precompose_unicode,
7582            ignorecase,
7583            conv: &conv,
7584            file_attrs_for: &file_attrs_for,
7585            filter_process: filter_fp,
7586        },
7587        par_opts,
7588        None,
7589    )?;
7590    visit_blob_lookup_batches(refresh_batches, |entry_index, lookup| {
7591        let BlobDiskLookup::Present(stat) = lookup else {
7592            return Ok(());
7593        };
7594        let content_matches = RefreshContent::matches(
7595            refresh_checks.get(entry_index),
7596            &index.entries[entry_index].oid,
7597        );
7598        let refresh = refresh_index_blob_stat_from_scan(
7599            &mut index.entries[entry_index],
7600            &stat,
7601            index_mtime,
7602            stat_policy,
7603            content_matches,
7604        );
7605        if refresh.index_changed {
7606            changed = true;
7607        }
7608        if refresh.content_verified {
7609            let path = index.entries[entry_index].path.clone();
7610            let oid = index.entries[entry_index].oid;
7611            index.mark_content_verified(&path, oid);
7612        }
7613        Ok(())
7614    })?;
7615    Ok(changed)
7616}
7617
7618/// Result of refreshing one index entry's cached stat during a worktree scan.
7619struct BlobStatRefreshOutcome {
7620    /// Whether the in-memory index entry was updated (stat fields or invalidation).
7621    index_changed: bool,
7622    /// Blob content was read and matches the recorded OID (skip a second hash in the same pass).
7623    content_verified: bool,
7624}
7625
7626/// Whether the stat refresh may adopt worktree stat data for `ie` at all.
7627fn refresh_index_entry_eligible(ie: &IndexEntry) -> bool {
7628    use crate::index::{MODE_EXECUTABLE, MODE_REGULAR, MODE_SYMLINK};
7629    ie.stage() == 0
7630        && !ie.skip_worktree()
7631        && !ie.assume_unchanged()
7632        && !ie.intent_to_add()
7633        && matches!(ie.mode, MODE_REGULAR | MODE_EXECUTABLE | MODE_SYMLINK)
7634}
7635
7636/// Whether refreshing `ie` from the scanned `stat` must first prove by content that the
7637/// worktree still holds `ie.oid`: the stat data changed, or it matches but is racy.
7638fn refresh_needs_content_check(
7639    ie: &IndexEntry,
7640    stat: &crate::worktree_scan::BlobDiskStat,
7641    index_mtime: Option<(u32, u32)>,
7642    stat_policy: crate::index::StatMatchPolicy,
7643) -> bool {
7644    refresh_index_entry_eligible(ie)
7645        && !stat.is_dir
7646        && (!stat_matches_blob_stat(ie, stat, stat_policy)
7647            || entry_is_racy_with_policy(ie, index_mtime, stat_policy))
7648}
7649
7650/// Whether a worktree diff with `refresh_index_stat_in_pass` refreshes `ie` from `stat`.
7651///
7652/// Status leaves out entries whose stat data already decides the answer: clean and not racy,
7653/// or a changed size that [`status_size_change_proves_modified`] accepts (reported as modified
7654/// without reading the file). A size change under a non-identity clean conversion is refreshed:
7655/// when the cleaned content still matches, the entry adopts the new stat, so later scans trust it.
7656pub(crate) fn worktree_diff_refreshes_entry(
7657    ie: &IndexEntry,
7658    stat: &crate::worktree_scan::BlobDiskStat,
7659    index_mtime: Option<(u32, u32)>,
7660    stat_policy: crate::index::StatMatchPolicy,
7661    for_status: bool,
7662    conversion_is_identity: impl FnOnce() -> bool,
7663) -> bool {
7664    if !for_status {
7665        return true;
7666    }
7667    if stat_matches_blob_stat(ie, stat, stat_policy) {
7668        return entry_is_racy_with_policy(ie, index_mtime, stat_policy);
7669    }
7670    !status_size_change_proves_modified(
7671        ie,
7672        stat.worktree_size(),
7673        for_status,
7674        conversion_is_identity,
7675    )
7676}
7677
7678/// Pool options for the stat refresh's content checks.
7679///
7680/// `thread_policy` and config pick the worker count; `stat_parallel_threads` caps it the same
7681/// way it caps the stat scan (`Some(0)` or `Some(1)` keeps hashing on the calling thread).
7682/// There is no byte threshold: opening each file, not hashing its bytes, dominates for the
7683/// small files a checkout leaves racy.
7684fn refresh_hash_parallel_options(
7685    thread_policy: &crate::parallel::ThreadPolicy,
7686    config: &ConfigSet,
7687    stat_parallel_threads: Option<usize>,
7688) -> crate::parallel::ParallelOptions {
7689    let policy = crate::parallel::ThreadPolicy {
7690        min_items_override: thread_policy.min_items_override.or(Some(0)),
7691        min_total_bytes_override: thread_policy.min_total_bytes_override.or(Some(0)),
7692        ..*thread_policy
7693    };
7694    let mut par_opts = policy.resolve(crate::parallel::ParallelOp::IndexRefresh, config);
7695    if let Some(cap) = stat_parallel_threads {
7696        par_opts.threads = par_opts
7697            .threads
7698            .min(std::num::NonZeroUsize::new(cap).unwrap_or(std::num::NonZeroUsize::MIN));
7699    }
7700    par_opts.min_items = 0;
7701    par_opts.min_total_bytes = None;
7702    par_opts
7703}
7704
7705/// Worktree layout and attribute context for hashing index entries during a scan.
7706struct WorktreeHashContext<'a> {
7707    work_tree: &'a Path,
7708    precompose_unicode: bool,
7709    ignorecase: bool,
7710    conv: &'a crate::crlf::ConversionConfig,
7711    file_attrs_for: &'a dyn Fn(&str) -> crate::crlf::FileAttrs,
7712    filter_process: Option<&'a crate::filter_process::FilterProcessState>,
7713}
7714
7715/// Run every content check the stat refresh needs before the serial visit of `batches`.
7716///
7717/// Racily clean entries are the common case right after a checkout writes the index: every
7718/// file then shares the index's timestamp second. Hashing them here, in parallel when
7719/// `par_opts` and the total size allow it, keeps that first scan from reading files one by one.
7720///
7721/// Returns a check for each scanned entry that `include` (given the entry index) admits and
7722/// [`refresh_needs_content_check`] selects. Read and filter failures are
7723/// [`RefreshContent::Failed`], so those entries stay unverified.
7724#[allow(clippy::too_many_arguments)]
7725fn refresh_content_checks(
7726    odb: &Odb,
7727    index: &Index,
7728    batches: &[Vec<(usize, crate::worktree_scan::BlobDiskLookup)>],
7729    index_mtime: Option<(u32, u32)>,
7730    stat_policy: crate::index::StatMatchPolicy,
7731    include: &dyn Fn(usize, &IndexEntry, &crate::worktree_scan::BlobDiskStat) -> bool,
7732    ctx: &WorktreeHashContext<'_>,
7733    par_opts: crate::parallel::ParallelOptions,
7734    cancel: Option<&crate::progress::CancelToken>,
7735) -> Result<RefreshChecks> {
7736    use crate::progress::CANCEL_POLL_STRIDE;
7737    use crate::worktree_scan::BlobDiskLookup;
7738
7739    /// Attributes are resolved on the calling thread: the shared rules are not `Sync`.
7740    struct ContentCheck {
7741        entry_index: usize,
7742        worktree_mode: u32,
7743        /// Position of the entry's attributes in `attrs`.
7744        attrs_index: usize,
7745    }
7746
7747    let mut attrs = DistinctFileAttrs::default();
7748    let mut jobs = Vec::new();
7749    let mut total_bytes = 0usize;
7750    let mut cancel_stride = 0usize;
7751    for (entry_index, lookup) in batches.iter().flatten() {
7752        cancel_stride = cancel_stride.wrapping_add(1);
7753        if cancel_stride.is_multiple_of(CANCEL_POLL_STRIDE) {
7754            if let Some(token) = cancel {
7755                token.poll_result()?;
7756            }
7757        }
7758        let BlobDiskLookup::Present(stat) = lookup else {
7759            continue;
7760        };
7761        let ie = &index.entries[*entry_index];
7762        if !refresh_needs_content_check(ie, stat, index_mtime, stat_policy)
7763            || !include(*entry_index, ie, stat)
7764        {
7765            continue;
7766        }
7767        total_bytes = total_bytes.saturating_add(ie.size as usize);
7768        let file_attrs = (ctx.file_attrs_for)(&crate::repo_relpath::storage_string(&ie.path));
7769        jobs.push(ContentCheck {
7770            entry_index: *entry_index,
7771            worktree_mode: stat.git_mode,
7772            attrs_index: attrs.intern(file_attrs),
7773        });
7774    }
7775    if jobs.is_empty() {
7776        return Ok(RefreshChecks::default());
7777    }
7778    let attrs = attrs.0;
7779    let mut checks = vec![None; index.entries.len()];
7780    let (work_tree, precompose_unicode, ignorecase) =
7781        (ctx.work_tree, ctx.precompose_unicode, ctx.ignorecase);
7782    let (conv, filter_process) = (ctx.conv, ctx.filter_process);
7783    let entries = index.entries.as_slice();
7784    let hash_one = |job: &ContentCheck| -> Result<RefreshContent> {
7785        if job.entry_index.is_multiple_of(CANCEL_POLL_STRIDE) {
7786            if let Some(token) = cancel {
7787                token.poll_result()?;
7788            }
7789        }
7790        let ie = &entries[job.entry_index];
7791        let file_path =
7792            index_entry_worktree_abs_bytes(work_tree, &ie.path, precompose_unicode, ignorecase);
7793        Ok(RefreshContent::from(hash_worktree_file_with_mode(
7794            odb,
7795            &file_path,
7796            job.worktree_mode,
7797            conv,
7798            &attrs[job.attrs_index],
7799            &crate::repo_relpath::storage_string(&ie.path),
7800            Some(ie),
7801            filter_process,
7802        )))
7803    };
7804    let matches = if par_opts.threads.get() > 1 && jobs.len() > 1 {
7805        crate::parallel::try_par_map_force(&jobs, par_opts, hash_one).map_err(|e| match e {
7806            crate::parallel::ParallelHashError::Task(err) => err,
7807        })?
7808    } else {
7809        let mut out = Vec::with_capacity(jobs.len());
7810        let mut serial_stride = 0usize;
7811        for job in &jobs {
7812            serial_stride = serial_stride.wrapping_add(1);
7813            if serial_stride.is_multiple_of(CANCEL_POLL_STRIDE) {
7814                if let Some(token) = cancel {
7815                    token.poll_result()?;
7816                }
7817            }
7818            out.push(hash_one(job)?);
7819        }
7820        out
7821    };
7822    for (job, content) in jobs.iter().zip(matches) {
7823        checks[job.entry_index] = Some(content);
7824    }
7825    Ok(RefreshChecks(checks))
7826}
7827
7828/// The distinct attribute sets of the entries a scan hashes, so each entry stores an index
7829/// instead of its own [`FileAttrs`](crate::crlf::FileAttrs) (a few hundred bytes).
7830///
7831/// Nearly every path of a repository resolves to the same few sets. Lookups compare against
7832/// at most [`Self::SEARCH_LIMIT`] sets, the most recent first, so a tree with many distinct
7833/// sets stores some twice instead of scanning a long list for every entry.
7834#[derive(Default)]
7835struct DistinctFileAttrs(Vec<crate::crlf::FileAttrs>);
7836
7837impl DistinctFileAttrs {
7838    const SEARCH_LIMIT: usize = 16;
7839
7840    /// Index of `attrs` in the list, appending it when no recent set is equal.
7841    fn intern(&mut self, attrs: crate::crlf::FileAttrs) -> usize {
7842        let len = self.0.len();
7843        let found = self
7844            .0
7845            .iter()
7846            .rev()
7847            .take(Self::SEARCH_LIMIT)
7848            .position(|seen| *seen == attrs);
7849        if let Some(back) = found {
7850            return len - 1 - back;
7851        }
7852        self.0.push(attrs);
7853        len
7854    }
7855}
7856
7857/// Results of [`refresh_content_checks`], by index entry position.
7858///
7859/// Empty when no entry needed a check (a clean, non-racy work tree), so the common case
7860/// allocates nothing.
7861#[derive(Default)]
7862struct RefreshChecks(Vec<Option<RefreshContent>>);
7863
7864impl RefreshChecks {
7865    /// The check that ran for the entry at `entry_index`, if any.
7866    fn get(&self, entry_index: usize) -> Option<RefreshContent> {
7867        self.0.get(entry_index).copied().flatten()
7868    }
7869}
7870
7871/// What the stat refresh learned by hashing one entry's worktree file.
7872#[derive(Clone, Copy)]
7873enum RefreshContent {
7874    /// The file could not be read or clean-converted.
7875    Failed,
7876    /// The cleaned file hashes to this blob id.
7877    Hashed(ObjectId),
7878}
7879
7880impl From<Result<ObjectId>> for RefreshContent {
7881    fn from(hashed: Result<ObjectId>) -> Self {
7882        hashed.map_or(Self::Failed, Self::Hashed)
7883    }
7884}
7885
7886impl RefreshContent {
7887    /// Whether `check` proved the worktree still holds `oid`.
7888    fn matches(check: Option<Self>, oid: &ObjectId) -> bool {
7889        matches!(check, Some(Self::Hashed(hashed)) if hashed == *oid)
7890    }
7891
7892    /// Whether `check` proved the worktree holds other content than `oid`.
7893    fn differs(check: Option<Self>, oid: &ObjectId) -> bool {
7894        matches!(check, Some(Self::Hashed(hashed)) if hashed != *oid)
7895    }
7896}
7897
7898/// Adopt the scanned `stat` for `ie` where that cannot hide a worktree change.
7899///
7900/// `content_matches` is the [`refresh_content_checks`] result for this entry; it is only
7901/// consulted when [`refresh_needs_content_check`] holds. A racy entry whose content no longer
7902/// matches has its stat cache invalidated so later scans re-hash it.
7903fn refresh_index_blob_stat_from_scan(
7904    ie: &mut IndexEntry,
7905    stat: &crate::worktree_scan::BlobDiskStat,
7906    index_mtime: Option<(u32, u32)>,
7907    stat_policy: crate::index::StatMatchPolicy,
7908    content_matches: bool,
7909) -> BlobStatRefreshOutcome {
7910    const UNCHANGED: BlobStatRefreshOutcome = BlobStatRefreshOutcome {
7911        index_changed: false,
7912        content_verified: false,
7913    };
7914    if !refresh_index_entry_eligible(ie) || stat.is_dir {
7915        return UNCHANGED;
7916    }
7917    let cached = crate::index::IndexStatFields::of_entry(ie);
7918    let stat_same = cached.equivalent_to(&stat.fields, stat_policy);
7919    let stat_matches = stat_matches_blob_stat(ie, stat, stat_policy);
7920    if !stat_matches || entry_is_racy_with_policy(ie, index_mtime, stat_policy) {
7921        if !content_matches {
7922            if stat_matches {
7923                invalidate_index_stat_cache(ie);
7924                return BlobStatRefreshOutcome {
7925                    index_changed: true,
7926                    content_verified: false,
7927                };
7928            }
7929            return UNCHANGED;
7930        }
7931        if !stat_same {
7932            stat.fields.store_in(ie);
7933        }
7934        return BlobStatRefreshOutcome {
7935            index_changed: !stat_same,
7936            content_verified: true,
7937        };
7938    }
7939    if stat_same {
7940        return BlobStatRefreshOutcome {
7941            index_changed: false,
7942            content_verified: true,
7943        };
7944    }
7945    stat.fields.store_in(ie);
7946    BlobStatRefreshOutcome {
7947        index_changed: true,
7948        content_verified: false,
7949    }
7950}
7951
7952/// Whether path-only checkout can skip writing a blob because the worktree is up to date.
7953///
7954/// Matching stat data is trusted unless the entry is racy relative to the on-disk index mtime;
7955/// then the worktree content (after clean/filter conversion) must hash to `expected_oid`
7956/// ([`hash_worktree_file`]). `config` is an optional preloaded configuration.
7957///
7958/// Returns `Ok(true)` when the caller should skip rewriting the path.
7959///
7960/// # Errors
7961///
7962/// Propagates I/O or hashing failures from [`hash_worktree_file`].
7963#[allow(clippy::too_many_arguments)]
7964pub(crate) fn path_checkout_skip_blob_write_when_up_to_date_with_config(
7965    odb: &Odb,
7966    git_dir: &Path,
7967    work_tree: &Path,
7968    index: &Index,
7969    entry: &IndexEntry,
7970    expected_oid: &ObjectId,
7971    expected_mode: u32,
7972    rel_path: &str,
7973    filter_process: Option<&crate::filter_process::FilterProcessState>,
7974    preloaded_config: Option<&crate::config::ConfigSet>,
7975) -> Result<bool> {
7976    use crate::config::ConfigSet;
7977    use crate::crlf;
7978    use crate::index::{MODE_EXECUTABLE, MODE_REGULAR};
7979
7980    if entry.oid != *expected_oid || entry.mode != expected_mode {
7981        return Ok(false);
7982    }
7983    if expected_mode != MODE_REGULAR && expected_mode != MODE_EXECUTABLE {
7984        return Ok(false);
7985    }
7986    let abs_path = crate::repo_relpath::worktree_abs(work_tree, rel_path);
7987    let meta = match fs::symlink_metadata(&abs_path) {
7988        Ok(m) => m,
7989        Err(_) => return Ok(false),
7990    };
7991    if !meta.is_file() || !stat_matches(entry, &meta) {
7992        return Ok(false);
7993    }
7994    let owned_config;
7995    let config = match preloaded_config {
7996        Some(cfg) => cfg,
7997        None => {
7998            owned_config = ConfigSet::load(
7999                &crate::environment::Environment::empty(),
8000                Some(git_dir),
8001                &crate::config::LoadConfigOptions::standard(
8002                    &crate::environment::Environment::empty(),
8003                    Some(git_dir),
8004                ),
8005            )
8006            .unwrap_or_default();
8007            &owned_config
8008        }
8009    };
8010    if !crate::index::WorktreeModePolicy::from_config(config)
8011        .worktree_mode_matches(mode_from_metadata(&meta), expected_mode)
8012    {
8013        return Ok(false);
8014    }
8015    let index_mtime = index_mtime_for_diff(index, None);
8016    if !entry_is_racy(entry, index_mtime) {
8017        return Ok(true);
8018    }
8019    let conv = crlf::ConversionConfig::from_config(config);
8020    let attrs = crlf::load_gitattributes(work_tree);
8021    let file_attrs = crlf::get_file_attrs(&attrs, rel_path, false, config);
8022    let wt_oid = hash_worktree_file(
8023        odb,
8024        &abs_path,
8025        &meta,
8026        &conv,
8027        &file_attrs,
8028        rel_path,
8029        Some(entry),
8030        filter_process,
8031    )?;
8032    Ok(wt_oid == *expected_oid)
8033}
8034
8035/// Clear cached stat fields so the next diff/status pass re-reads the work tree.
8036fn invalidate_index_stat_cache(ie: &mut IndexEntry) {
8037    ie.ctime_sec = 0;
8038    ie.ctime_nsec = 0;
8039    ie.mtime_sec = 0;
8040    ie.mtime_nsec = 0;
8041    ie.dev = 0;
8042    ie.ino = 0;
8043    ie.size = 0;
8044}
8045
8046/// Read worktree content for diffing, honoring Git tree modes.
8047///
8048/// For [`MODE_SYMLINK`] entries backed by a symlink on disk this returns the link target path
8049/// bytes (via [`read_link`](std::fs::read_link)), not the contents of the followed file. A
8050/// symlink entry checked out as a plain file (`core.symlinks=false`) already holds the target,
8051/// so it is read like other modes, with a plain [`read`](std::fs::read) of the path.
8052///
8053/// # Errors
8054///
8055/// Returns [`std::io::Error`] when the path cannot be read.
8056pub(crate) fn read_worktree_bytes_for_mode(path: &Path, mode: u32) -> std::io::Result<Vec<u8>> {
8057    if mode == MODE_SYMLINK && fs::symlink_metadata(path)?.file_type().is_symlink() {
8058        let target = fs::read_link(path)?;
8059        return Ok(symlink_target_bytes(&target));
8060    }
8061    fs::read(path)
8062}
8063
8064/// Read worktree file bytes in Git (repository) form for line diffs against index or tree blobs.
8065///
8066/// Applies the same `.gitattributes`, filter, and line-ending rules as
8067/// [`diff_index_to_worktree`]. Pass the stage-0 [`IndexEntry`] when available so
8068/// `autocrlf` index-guard normalization matches status and raw diff collection.
8069///
8070/// # Errors
8071///
8072/// Returns I/O errors from the worktree path or [`Error`] from filter conversion.
8073pub fn read_worktree_side_bytes_for_diff(
8074    odb: &Odb,
8075    work_tree: &Path,
8076    rel_path: &str,
8077    mode: u32,
8078    config: &crate::config::ConfigSet,
8079    index_entry: Option<&IndexEntry>,
8080    worktree_rules: Option<&crate::worktree_rules::WorktreeRules>,
8081) -> Result<Vec<u8>> {
8082    Ok(load_worktree_side_for_diff(
8083        odb,
8084        work_tree,
8085        rel_path,
8086        mode,
8087        config,
8088        index_entry,
8089        worktree_rules,
8090        false,
8091    )?
8092    .0
8093    .data)
8094}
8095
8096/// Load worktree bytes for diffing, optionally skipping reads above `core.bigFileThreshold`.
8097///
8098/// Returns empty [`super::structured::SideBytes`] with `binary = true` when the file is not read.
8099/// When `force_read_content` is true, large files are read and binary payload bytes are retained
8100/// (for `GIT binary patch` / `git apply`).
8101///
8102/// # Errors
8103///
8104/// Returns I/O errors from the worktree path or [`Error`] from filter conversion.
8105#[allow(clippy::too_many_arguments)]
8106pub(crate) fn load_worktree_side_for_diff(
8107    odb: &Odb,
8108    work_tree: &Path,
8109    rel_path: &str,
8110    mode: u32,
8111    config: &crate::config::ConfigSet,
8112    index_entry: Option<&IndexEntry>,
8113    worktree_rules: Option<&crate::worktree_rules::WorktreeRules>,
8114    force_read_content: bool,
8115) -> Result<(super::structured::SideBytes, bool)> {
8116    use super::tree_content::{side_bytes_after_blob_read, treat_side_as_binary_without_reading};
8117    let file_path = crate::repo_relpath::worktree_abs(work_tree, rel_path);
8118    let rel_match = crate::repo_relpath::as_utf8_for_matching(rel_path);
8119    let meta = fs::symlink_metadata(&file_path).map_err(|source| {
8120        crate::worktree::map_worktree_io(
8121            rel_match.as_ref(),
8122            crate::worktree::WorktreePathOperation::Metadata,
8123            source,
8124        )
8125    })?;
8126    if mode == MODE_SYMLINK && meta.file_type().is_symlink() {
8127        let target = fs::read_link(&file_path).map_err(|source| {
8128            crate::worktree::map_worktree_io(
8129                rel_match.as_ref(),
8130                crate::worktree::WorktreePathOperation::Read,
8131                source,
8132            )
8133        })?;
8134        return Ok((
8135            super::structured::SideBytes::from_bytes(symlink_target_bytes(&target)),
8136            false,
8137        ));
8138    }
8139    if !force_read_content
8140        && treat_side_as_binary_without_reading(odb, meta.len(), Some(rel_match.as_ref()), false)?
8141    {
8142        return Ok((super::structured::SideBytes::unread(meta.len()), true));
8143    }
8144    let data = if let Some(rules) = worktree_rules {
8145        worktree_blob_bytes(
8146            odb,
8147            &file_path,
8148            &meta,
8149            rules.conversion(),
8150            &rules.file_attrs(rel_match.as_ref(), meta.is_dir()),
8151            rel_match.as_ref(),
8152            index_entry,
8153            Some(rules.filter_process()),
8154        )?
8155    } else {
8156        let conv = crate::crlf::ConversionConfig::from_config(config);
8157        let attrs = crate::crlf::load_gitattributes(work_tree);
8158        let file_attrs =
8159            crate::crlf::get_file_attrs(&attrs, rel_match.as_ref(), meta.is_dir(), config);
8160        worktree_blob_bytes(
8161            odb,
8162            &file_path,
8163            &meta,
8164            &conv,
8165            &file_attrs,
8166            rel_match.as_ref(),
8167            index_entry,
8168            None,
8169        )?
8170    };
8171    Ok(side_bytes_after_blob_read(data, force_read_content))
8172}
8173
8174/// Symlink target as the byte string Git hashes for the blob OID.
8175///
8176/// On Unix the raw `OsStr` bytes are used verbatim. On Windows `OsStr` is WTF-8
8177/// and has no stable byte view, so the lossy UTF-8 form is used (symlink targets
8178/// in Git trees are UTF-8 in practice).
8179pub(crate) fn symlink_target_bytes(target: &Path) -> Vec<u8> {
8180    #[cfg(unix)]
8181    {
8182        use std::os::unix::ffi::OsStrExt as _;
8183        target.as_os_str().as_bytes().to_vec()
8184    }
8185    #[cfg(not(unix))]
8186    {
8187        target.as_os_str().to_string_lossy().as_bytes().to_vec()
8188    }
8189}
8190
8191/// A worktree file's blob content: normalized bytes, or an open file that is hashed or
8192/// stored as a stream.
8193///
8194/// Regular files of at least the caller's size threshold whose bytes can be stored without clean
8195/// conversion ([`crate::crlf::worktree_allows_raw_byte_stream`], including binary files under
8196/// `core.autocrlf`) are streamed so memory use does not depend on the file size.
8197pub(crate) enum WorktreeBlobContent {
8198    /// Content after the clean conversion (filters, encoding, line endings).
8199    Bytes(Vec<u8>),
8200    /// Unconverted big file, positioned at its start.
8201    Stream {
8202        /// Open handle on the worktree file.
8203        file: fs::File,
8204        /// File size when it was opened.
8205        size: u64,
8206    },
8207}
8208
8209impl WorktreeBlobContent {
8210    /// Object id of the content, without storing it.
8211    pub fn hash(self, odb: &Odb) -> Result<ObjectId> {
8212        match self {
8213            Self::Bytes(data) => odb.hash(ObjectKind::Blob, &data),
8214            Self::Stream { mut file, size } => odb.hash_stream(ObjectKind::Blob, size, &mut file),
8215        }
8216    }
8217
8218    /// Store the content as a blob and return its id.
8219    pub fn store(self, odb: &Odb, options: crate::odb::WriteOptions) -> Result<ObjectId> {
8220        match self {
8221            Self::Bytes(data) => odb.write_with(ObjectKind::Blob, &data, options),
8222            Self::Stream { mut file, size } => {
8223                odb.write_stream(ObjectKind::Blob, size, &mut file, options)
8224            }
8225        }
8226    }
8227
8228    /// [`Self::store`] with `options` when given, otherwise [`Self::hash`].
8229    fn object_id(self, odb: &Odb, store: Option<crate::odb::WriteOptions>) -> Result<ObjectId> {
8230        match store {
8231            Some(options) => self.store(odb, options),
8232            None => self.hash(odb),
8233        }
8234    }
8235}
8236
8237/// Read and clean-convert an open regular worktree file of `size` bytes.
8238#[allow(clippy::too_many_arguments)]
8239fn read_regular_worktree_bytes(
8240    odb: &Odb,
8241    mut file: fs::File,
8242    size: u64,
8243    conv: &crate::crlf::ConversionConfig,
8244    file_attrs: &crate::crlf::FileAttrs,
8245    rel_path: &str,
8246    index_entry: Option<&IndexEntry>,
8247    filter_process: Option<&crate::filter_process::FilterProcessState>,
8248) -> Result<Vec<u8>> {
8249    let needs_index_blob =
8250        index_entry.is_some_and(|_| crate::crlf::clean_uses_autocrlf_index_guard(file_attrs, conv));
8251    let prior_blob: Option<Vec<u8>> = if needs_index_blob {
8252        index_entry
8253            .filter(|e| e.oid != zero_oid())
8254            .and_then(|e| odb.read(&e.oid).ok().map(|o| o.data))
8255    } else {
8256        None
8257    };
8258    let mut raw = Vec::with_capacity(usize::try_from(size).unwrap_or_default());
8259    std::io::Read::read_to_end(&mut file, &mut raw).map_err(|source| {
8260        crate::worktree::map_worktree_io(
8261            rel_path,
8262            crate::worktree::WorktreePathOperation::Read,
8263            source,
8264        )
8265    })?;
8266    #[cfg(test)]
8267    odb.hot_path_metrics().record_blob_content_read();
8268    let opts = crate::crlf::ConvertToGitOpts {
8269        index_blob: prior_blob.as_deref(),
8270        renormalize: false,
8271        check_safecrlf: false,
8272        filter_process,
8273        command_runner: None,
8274    };
8275    Ok(crate::crlf::convert_to_git_owned(
8276        raw, rel_path, conv, file_attrs, opts,
8277    )?)
8278}
8279
8280fn open_with_size(path: &Path, rel_path: &str) -> Result<(fs::File, u64)> {
8281    let file = fs::File::open(path).map_err(|source| {
8282        crate::worktree::map_worktree_io(
8283            rel_path,
8284            crate::worktree::WorktreePathOperation::Read,
8285            source,
8286        )
8287    })?;
8288    let size = file
8289        .metadata()
8290        .map_err(|source| {
8291            crate::worktree::map_worktree_io(
8292                rel_path,
8293                crate::worktree::WorktreePathOperation::Metadata,
8294                source,
8295            )
8296        })?
8297        .len();
8298    Ok((file, size))
8299}
8300
8301/// Open a regular worktree file and clean-convert it, or hand it out as a stream when it is
8302/// at least `stream_from` bytes and its clean conversion is the identity.
8303#[allow(clippy::too_many_arguments)]
8304fn read_regular_worktree_blob(
8305    odb: &Odb,
8306    path: &Path,
8307    conv: &crate::crlf::ConversionConfig,
8308    file_attrs: &crate::crlf::FileAttrs,
8309    rel_path: &str,
8310    index_entry: Option<&IndexEntry>,
8311    filter_process: Option<&crate::filter_process::FilterProcessState>,
8312    stream_from: u64,
8313) -> Result<WorktreeBlobContent> {
8314    let (mut file, size) = open_with_size(path, rel_path)?;
8315    if size >= stream_from
8316        && crate::crlf::worktree_allows_raw_byte_stream(
8317            conv,
8318            file_attrs,
8319            index_entry,
8320            &mut file,
8321            size,
8322        )?
8323    {
8324        #[cfg(test)]
8325        odb.hot_path_metrics().record_blob_content_read();
8326        return Ok(WorktreeBlobContent::Stream { file, size });
8327    }
8328    read_regular_worktree_bytes(
8329        odb,
8330        file,
8331        size,
8332        conv,
8333        file_attrs,
8334        rel_path,
8335        index_entry,
8336        filter_process,
8337    )
8338    .map(WorktreeBlobContent::Bytes)
8339}
8340
8341/// Blob content for a worktree path: symlink target, empty for a directory, or the
8342/// clean-converted (or streamed, see [`WorktreeBlobContent`]) file content.
8343#[allow(clippy::too_many_arguments)]
8344pub(crate) fn worktree_blob_content(
8345    odb: &Odb,
8346    path: &Path,
8347    meta: &fs::Metadata,
8348    conv: &crate::crlf::ConversionConfig,
8349    file_attrs: &crate::crlf::FileAttrs,
8350    rel_path: &str,
8351    index_entry: Option<&IndexEntry>,
8352    filter_process: Option<&crate::filter_process::FilterProcessState>,
8353) -> Result<WorktreeBlobContent> {
8354    worktree_blob_content_from(
8355        odb,
8356        path,
8357        meta,
8358        conv,
8359        file_attrs,
8360        rel_path,
8361        index_entry,
8362        filter_process,
8363        hash_stream_threshold(odb)?,
8364    )
8365}
8366
8367/// [`worktree_blob_content`], streaming unconverted regular files from `stream_from` bytes.
8368#[allow(clippy::too_many_arguments)]
8369fn worktree_blob_content_from(
8370    odb: &Odb,
8371    path: &Path,
8372    meta: &fs::Metadata,
8373    conv: &crate::crlf::ConversionConfig,
8374    file_attrs: &crate::crlf::FileAttrs,
8375    rel_path: &str,
8376    index_entry: Option<&IndexEntry>,
8377    filter_process: Option<&crate::filter_process::FilterProcessState>,
8378    stream_from: u64,
8379) -> Result<WorktreeBlobContent> {
8380    if meta.file_type().is_symlink() {
8381        let target = fs::read_link(path).map_err(|source| {
8382            crate::worktree::map_worktree_io(
8383                rel_path,
8384                crate::worktree::WorktreePathOperation::Read,
8385                source,
8386            )
8387        })?;
8388        return Ok(WorktreeBlobContent::Bytes(symlink_target_bytes(&target)));
8389    }
8390    if meta.is_dir() {
8391        return Ok(WorktreeBlobContent::Bytes(Vec::new()));
8392    }
8393    read_regular_worktree_blob(
8394        odb,
8395        path,
8396        conv,
8397        file_attrs,
8398        rel_path,
8399        index_entry,
8400        filter_process,
8401        stream_from,
8402    )
8403}
8404
8405/// Like [`worktree_blob_content`], always returning the bytes (for callers that need the
8406/// content itself, such as line diffs).
8407#[allow(clippy::too_many_arguments)]
8408pub(crate) fn worktree_blob_bytes(
8409    odb: &Odb,
8410    path: &Path,
8411    meta: &fs::Metadata,
8412    conv: &crate::crlf::ConversionConfig,
8413    file_attrs: &crate::crlf::FileAttrs,
8414    rel_path: &str,
8415    index_entry: Option<&IndexEntry>,
8416    filter_process: Option<&crate::filter_process::FilterProcessState>,
8417) -> Result<Vec<u8>> {
8418    if meta.file_type().is_symlink() {
8419        let target = fs::read_link(path).map_err(|source| {
8420            crate::worktree::map_worktree_io(
8421                rel_path,
8422                crate::worktree::WorktreePathOperation::Read,
8423                source,
8424            )
8425        })?;
8426        return Ok(symlink_target_bytes(&target));
8427    }
8428    if meta.is_dir() {
8429        return Ok(Vec::new());
8430    }
8431    let (file, size) = open_with_size(path, rel_path)?;
8432    read_regular_worktree_bytes(
8433        odb,
8434        file,
8435        size,
8436        conv,
8437        file_attrs,
8438        rel_path,
8439        index_entry,
8440        filter_process,
8441    )
8442}
8443
8444#[allow(clippy::too_many_arguments)]
8445fn worktree_file_oid(
8446    odb: &Odb,
8447    path: &Path,
8448    meta: &fs::Metadata,
8449    conv: &crate::crlf::ConversionConfig,
8450    file_attrs: &crate::crlf::FileAttrs,
8451    rel_path: &str,
8452    index_entry: Option<&IndexEntry>,
8453    materialize: bool,
8454    filter_process: Option<&crate::filter_process::FilterProcessState>,
8455) -> Result<ObjectId> {
8456    let stream_from = hash_stream_threshold(odb)?;
8457    worktree_blob_content_from(
8458        odb,
8459        path,
8460        meta,
8461        conv,
8462        file_attrs,
8463        rel_path,
8464        index_entry,
8465        filter_process,
8466        stream_from,
8467    )?
8468    .object_id(odb, materialize.then(|| odb.bulk_stage_write_options()))
8469}
8470
8471/// Hash normalized worktree bytes for a path (does not write to the object database).
8472///
8473/// Files that need no clean conversion are hashed as a stream, without reading them into
8474/// memory, from 1 MiB (or `core.bigFileThreshold` when that is smaller).
8475///
8476/// # Errors
8477///
8478/// Returns [`Error::Io`] when the file cannot be read, and [`crate::worktree::WorktreeError::Filter`] when a
8479/// `filter.<driver>.required` clean filter fails or cannot be started; content is never
8480/// stored or hashed unfiltered in that case.
8481#[allow(clippy::too_many_arguments)]
8482pub(crate) fn hash_worktree_file(
8483    odb: &Odb,
8484    path: &Path,
8485    meta: &fs::Metadata,
8486    conv: &crate::crlf::ConversionConfig,
8487    file_attrs: &crate::crlf::FileAttrs,
8488    rel_path: &str,
8489    index_entry: Option<&IndexEntry>,
8490    filter_process: Option<&crate::filter_process::FilterProcessState>,
8491) -> Result<ObjectId> {
8492    worktree_file_oid(
8493        odb,
8494        path,
8495        meta,
8496        conv,
8497        file_attrs,
8498        rel_path,
8499        index_entry,
8500        false,
8501        filter_process,
8502    )
8503}
8504
8505/// Work-tree files at least this large are hashed as a stream (when their content goes into
8506/// Git unchanged) even below `core.bigFileThreshold`, which only governs how blobs are stored.
8507const STREAMED_HASH_MIN_SIZE: u64 = 1024 * 1024;
8508
8509/// Size from which an unconverted work-tree file is hashed as a stream rather than read whole.
8510fn hash_stream_threshold(odb: &Odb) -> Result<u64> {
8511    Ok(odb.big_file_threshold()?.min(STREAMED_HASH_MIN_SIZE))
8512}
8513
8514/// Like [`hash_worktree_file`], for callers that already know the worktree file type from a
8515/// directory scan (`worktree_mode` is the Git mode derived from that stat), so no metadata
8516/// probe is needed.
8517#[allow(clippy::too_many_arguments)]
8518pub(crate) fn hash_worktree_file_with_mode(
8519    odb: &Odb,
8520    path: &Path,
8521    worktree_mode: u32,
8522    conv: &crate::crlf::ConversionConfig,
8523    file_attrs: &crate::crlf::FileAttrs,
8524    rel_path: &str,
8525    index_entry: Option<&IndexEntry>,
8526    filter_process: Option<&crate::filter_process::FilterProcessState>,
8527) -> Result<ObjectId> {
8528    if worktree_mode == MODE_SYMLINK {
8529        let data = read_worktree_bytes_for_mode(path, worktree_mode)?;
8530        return odb.hash(ObjectKind::Blob, &data);
8531    }
8532    read_regular_worktree_blob(
8533        odb,
8534        path,
8535        conv,
8536        file_attrs,
8537        rel_path,
8538        index_entry,
8539        filter_process,
8540        hash_stream_threshold(odb)?,
8541    )?
8542    .hash(odb)
8543}
8544
8545/// Read, normalize (CRLF/attributes), and store a worktree blob once.
8546///
8547/// # Errors
8548///
8549/// Returns [`Error::Io`] when the file cannot be read, and [`crate::worktree::WorktreeError::Filter`] when a
8550/// `filter.<driver>.required` clean filter fails or cannot be started; content is never
8551/// stored or hashed unfiltered in that case.
8552#[allow(clippy::too_many_arguments)]
8553pub(crate) fn materialize_worktree_blob(
8554    odb: &Odb,
8555    path: &Path,
8556    meta: &fs::Metadata,
8557    conv: &crate::crlf::ConversionConfig,
8558    file_attrs: &crate::crlf::FileAttrs,
8559    rel_path: &str,
8560    index_entry: Option<&IndexEntry>,
8561    filter_process: Option<&crate::filter_process::FilterProcessState>,
8562) -> Result<ObjectId> {
8563    worktree_file_oid(
8564        odb,
8565        path,
8566        meta,
8567        conv,
8568        file_attrs,
8569        rel_path,
8570        index_entry,
8571        true,
8572        filter_process,
8573    )
8574}
8575
8576/// Derive a Git file mode from filesystem metadata.
8577pub fn mode_from_metadata(meta: &fs::Metadata) -> u32 {
8578    if meta.file_type().is_symlink() {
8579        0o120000
8580    } else {
8581        #[cfg(unix)]
8582        {
8583            if meta.mode() & 0o111 != 0 {
8584                return 0o100755;
8585            }
8586        }
8587        0o100644
8588    }
8589}
8590
8591/// Compare a tree against the working tree.
8592///
8593/// Shows changes from `tree_oid` to the current working directory state.
8594/// Files tracked in the index but not in the tree are shown as Added.
8595/// Files in the tree but missing from the working tree are shown as Deleted.
8596///
8597/// # Parameters
8598///
8599/// - `odb` — object database.
8600/// - `tree_oid` — the tree to compare against (`None` for empty tree).
8601/// - `work_tree` — path to the working tree root.
8602/// - `index` — current index (used to discover new tracked files not in tree).
8603///
8604/// # Errors
8605///
8606/// Returns errors from ODB reads or I/O.
8607#[cfg(test)]
8608pub(crate) fn diff_tree_to_worktree(
8609    odb: &Odb,
8610    tree_oid: Option<&ObjectId>,
8611    work_tree: &Path,
8612    index: &Index,
8613) -> Result<Vec<DiffEntry>> {
8614    diff_tree_to_worktree_with_git_dir(
8615        odb,
8616        tree_oid,
8617        work_tree,
8618        &work_tree.join(".git"),
8619        index,
8620        None,
8621    )
8622}
8623
8624/// Like [`diff_tree_to_worktree`], but loads config from `repository_git_dir` (linked worktrees / gitfiles).
8625///
8626/// Pass `config` when the caller already holds a repository config snapshot.
8627#[cfg(test)]
8628pub(crate) fn diff_tree_to_worktree_with_git_dir(
8629    odb: &Odb,
8630    tree_oid: Option<&ObjectId>,
8631    work_tree: &Path,
8632    repository_git_dir: &Path,
8633    index: &Index,
8634    config: Option<&ConfigSet>,
8635) -> Result<Vec<DiffEntry>> {
8636    diff_tree_to_worktree_with_git_dir_and_rules(
8637        odb,
8638        tree_oid,
8639        work_tree,
8640        repository_git_dir,
8641        index,
8642        config,
8643        None,
8644    )
8645}
8646
8647/// Like [`diff_tree_to_worktree_with_git_dir`], with optional shared [`crate::worktree_rules::WorktreeRules`].
8648pub(crate) fn diff_tree_to_worktree_with_git_dir_and_rules(
8649    odb: &Odb,
8650    tree_oid: Option<&ObjectId>,
8651    work_tree: &Path,
8652    repository_git_dir: &Path,
8653    index: &Index,
8654    config: Option<&ConfigSet>,
8655    worktree_rules: Option<&crate::worktree_rules::WorktreeRules>,
8656) -> Result<Vec<DiffEntry>> {
8657    diff_tree_to_worktree_with_git_dir_and_rules_extras(
8658        odb,
8659        tree_oid,
8660        work_tree,
8661        repository_git_dir,
8662        index,
8663        config,
8664        worktree_rules,
8665        DiffTreeToWorktreeExtras::default(),
8666    )
8667}
8668
8669/// Like [`diff_tree_to_worktree_with_git_dir_and_rules`], with worktree access policy for the slow path.
8670#[allow(clippy::too_many_arguments)]
8671pub fn diff_tree_to_worktree_with_git_dir_and_rules_extras(
8672    odb: &Odb,
8673    tree_oid: Option<&ObjectId>,
8674    work_tree: &Path,
8675    repository_git_dir: &Path,
8676    index: &Index,
8677    config: Option<&ConfigSet>,
8678    worktree_rules: Option<&crate::worktree_rules::WorktreeRules>,
8679    extras: DiffTreeToWorktreeExtras,
8680) -> Result<Vec<DiffEntry>> {
8681    use crate::config::ConfigSet;
8682    use crate::crlf;
8683
8684    if worktree_rules.is_none() {
8685        if let Some(tree_oid) = tree_oid {
8686            if index.cache_tree_root.as_ref() == Some(tree_oid)
8687                && crate::write_tree::cache_tree_fully_valid(odb, index.cache_tree.as_ref())
8688            {
8689                let mut idx = index.clone();
8690                let options = DiffIndexToWorktreeOptions {
8691                    repository_git_dir: Some(repository_git_dir.to_path_buf()),
8692                    refresh_index_stat_in_pass: false,
8693                    // Tree↔worktree raw lines use a null worktree OID when content differs; the
8694                    // status fast path (`for_status`) uses the same null OID for size-only drift
8695                    // without hashing (see `status_size_change_proves_modified`).
8696                    for_status: false,
8697                    config: config.cloned().map(std::sync::Arc::new),
8698                    ..DiffIndexToWorktreeOptions::default()
8699                };
8700                let (entries, _, _) = diff_index_to_worktree_inner(
8701                    odb,
8702                    &mut idx,
8703                    work_tree,
8704                    repository_git_dir,
8705                    options,
8706                    false,
8707                    None,
8708                )?;
8709                return Ok(entries);
8710            }
8711        }
8712    }
8713
8714    let config = worktree_rules
8715        .map(|r| r.config().clone())
8716        .unwrap_or_else(|| {
8717            config.cloned().unwrap_or_else(|| {
8718                ConfigSet::load(
8719                    &crate::environment::Environment::empty(),
8720                    Some(repository_git_dir),
8721                    &crate::config::LoadConfigOptions::standard(
8722                        &crate::environment::Environment::empty(),
8723                        Some(repository_git_dir),
8724                    ),
8725                )
8726                .unwrap_or_else(|_| ConfigSet::new())
8727            })
8728        });
8729    let conv = worktree_rules
8730        .map(|r| r.conversion().clone())
8731        .unwrap_or_else(|| crlf::ConversionConfig::from_config(&config));
8732    let legacy_attrs = if worktree_rules.is_none() {
8733        Some(crlf::load_gitattributes_parsed(work_tree))
8734    } else {
8735        None
8736    };
8737    let file_attrs_for = |path: &str| -> crlf::FileAttrs {
8738        if let Some(rules) = worktree_rules {
8739            rules.file_attrs(path, false)
8740        } else if let Some(parsed) = legacy_attrs.as_ref() {
8741            crlf::file_attrs_from_gitattributes(&parsed.rules, &parsed.macros, path, false, &config)
8742        } else {
8743            crlf::FileAttrs::default()
8744        }
8745    };
8746    let filter_fp = worktree_rules.map(|r| r.filter_process());
8747
8748    let sparse_index = index.has_sparse_directory_placeholders();
8749    // Flatten the tree into a BTreeMap keyed by path
8750    let tree_flat = match tree_oid {
8751        Some(oid) if sparse_index => flatten_tree_with_index(odb, oid, "", Some(index))?,
8752        Some(oid) => flatten_tree(odb, oid, "")?,
8753        None => Vec::new(),
8754    };
8755    let tree_map: std::collections::BTreeMap<String, &FlatEntry> =
8756        tree_flat.iter().map(|e| (e.path.clone(), e)).collect();
8757
8758    // Build index lookup: path → &IndexEntry (stage 0 only)
8759    let mut index_entries: std::collections::BTreeMap<&[u8], &IndexEntry> =
8760        std::collections::BTreeMap::new();
8761    let mut index_paths: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
8762    let mut stage0_paths: std::collections::BTreeSet<Vec<u8>> = std::collections::BTreeSet::new();
8763    for ie in &index.entries {
8764        if ie.stage() != 0 {
8765            continue;
8766        }
8767        let path = String::from_utf8_lossy(&ie.path).to_string();
8768        index_entries.insert(&ie.path, ie);
8769        index_paths.insert(path);
8770        stage0_paths.insert(ie.path.clone());
8771    }
8772
8773    // Paths with only unmerged stages (1–3) and no stage 0 — `git diff <rev>` must still list them
8774    // so combined `diff --cc` conflict hunks can be emitted (`t4108-apply-threeway`).
8775    let mut unmerged_only_paths: std::collections::BTreeSet<String> =
8776        std::collections::BTreeSet::new();
8777    for ie in &index.entries {
8778        if !(1..=3).contains(&ie.stage()) {
8779            continue;
8780        }
8781        if stage0_paths.contains(&ie.path) {
8782            continue;
8783        }
8784        unmerged_only_paths.insert(String::from_utf8_lossy(&ie.path).into_owned());
8785    }
8786
8787    // Union of tree paths + index paths
8788    let mut all_paths: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
8789    all_paths.extend(tree_map.keys().cloned());
8790    all_paths.extend(index_paths.iter().cloned());
8791    all_paths.extend(unmerged_only_paths.iter().cloned());
8792
8793    let index_mtime = index_mtime_for_diff(index, None);
8794    let stat_policy = crate::index::StatMatchPolicy::from_config(Some(&config));
8795    let mode_policy = crate::index::WorktreeModePolicy::from_config(&config);
8796    let mut result = Vec::new();
8797
8798    for path in &all_paths {
8799        if extras
8800            .restrict_to_paths
8801            .as_ref()
8802            .is_some_and(|limit| !limit.contains(path))
8803        {
8804            continue;
8805        }
8806        if index_entries
8807            .get(path.as_bytes())
8808            .is_some_and(|ie| ie.skip_worktree())
8809        {
8810            // Sparse checkout: `git diff <rev>` does not report tree↔worktree drift for
8811            // skip-worktree paths (they are outside the sparse cone). Matches t7012 stash flow.
8812            continue;
8813        }
8814        if index_entries
8815            .get(path.as_bytes())
8816            .is_some_and(|ie| ie.assume_unchanged())
8817        {
8818            continue;
8819        }
8820
8821        let tree_entry = tree_map.get(path.as_str());
8822
8823        // Gitlink entries (submodules) — compare HEAD commit, not file content.
8824        let is_gitlink = tree_entry.is_some_and(|te| te.mode == 0o160000)
8825            || index_entries
8826                .get(path.as_bytes())
8827                .is_some_and(|ie| ie.mode == 0o160000);
8828        if is_gitlink {
8829            let sub_dir = work_tree.join(path);
8830            let index_gitlink_oid = index_entries
8831                .get(path.as_bytes())
8832                .filter(|ie| ie.mode == 0o160000)
8833                .map(|ie| ie.oid);
8834            match (tree_entry, index_gitlink_oid) {
8835                (Some(te), _) => {
8836                    let sub_head = read_submodule_head_oid(&sub_dir);
8837                    // A gitlink whose worktree directory no longer exists is a deleted submodule:
8838                    // Git's `diff-lib.c` reports it as a deletion (status `D`, new mode 000000) and
8839                    // `--submodule` renders the `(submodule deleted)` summary (t4041 #46).
8840                    if sub_head.is_none() && !sub_dir.exists() {
8841                        result.push(DiffEntry {
8842                            status: DiffStatus::Deleted,
8843                            old_path: Some(path.clone()),
8844                            new_path: Some(path.clone()),
8845                            old_mode: FileMode::new(te.mode),
8846                            new_mode: FileMode::ABSENT,
8847                            old_oid: te.oid,
8848                            new_oid: zero_oid(),
8849                            score: None,
8850                        });
8851                        continue;
8852                    }
8853                    let ref_matches = if let Some(head) = sub_head {
8854                        head == te.oid
8855                    } else {
8856                        uninitialized_gitlink_worktree_matches_index(&sub_dir)
8857                    };
8858                    let index_matches_tree = index_gitlink_oid.is_some_and(|oid| oid == te.oid);
8859                    // Default tree↔worktree diff matches Git: untracked-only paths inside a
8860                    // checked-out submodule do not make the gitlink differ (see `git diff <tree>`).
8861                    let sub_flags = submodule_porcelain_flags(work_tree, path, te.oid);
8862                    let ignore = crate::submodule_ignore::submodule_ignore_for_path(
8863                        &config, work_tree, path,
8864                    );
8865                    if index_matches_tree
8866                        && crate::submodule_ignore::gitlink_worktree_change_suppressed(
8867                            ignore,
8868                            ref_matches,
8869                            sub_flags,
8870                        )
8871                    {
8872                        continue;
8873                    }
8874                    let dirty_while_aligned =
8875                        index_matches_tree && ref_matches && sub_flags.modified;
8876                    if !ref_matches || dirty_while_aligned {
8877                        // Raw `git diff <tree>` lines use a null OID on the worktree side when the
8878                        // checked-out submodule HEAD differs from the tree's gitlink; patch output
8879                        // still resolves the real commit from the submodule directory.
8880                        let new_oid = if !ref_matches {
8881                            sub_head.unwrap_or_else(zero_oid)
8882                        } else {
8883                            zero_oid()
8884                        };
8885                        result.push(DiffEntry {
8886                            status: DiffStatus::Modified,
8887                            old_path: Some(path.clone()),
8888                            new_path: Some(path.clone()),
8889                            old_mode: FileMode::new(te.mode),
8890                            new_mode: FileMode::new(te.mode),
8891                            old_oid: te.oid,
8892                            new_oid,
8893                            score: None,
8894                        });
8895                    }
8896                }
8897                (None, Some(idx_oid)) => {
8898                    // Gitlink staged in the index but absent from the tree: a new submodule. Git
8899                    // reports it as an addition (status `A`) with the index gitlink on the new side,
8900                    // so `--submodule` renders `(new submodule)` (t4041 #46). The patch renderer
8901                    // resolves the real commit from the index OID / submodule HEAD.
8902                    let new_oid = read_submodule_head_oid(&sub_dir).unwrap_or(idx_oid);
8903                    result.push(DiffEntry {
8904                        status: DiffStatus::Added,
8905                        old_path: Some(path.clone()),
8906                        new_path: Some(path.clone()),
8907                        old_mode: FileMode::ABSENT,
8908                        new_mode: FileMode::new(0o160000),
8909                        old_oid: zero_oid(),
8910                        new_oid,
8911                        score: None,
8912                    });
8913                }
8914                (None, None) => {}
8915            }
8916            continue;
8917        }
8918
8919        let file_path = work_tree.join(path);
8920
8921        let wt_meta = match probe_worktree_symlink_metadata(
8922            &file_path,
8923            path,
8924            extras.diagnostics.as_ref(),
8925            false,
8926            extras.skip_inaccessible_paths,
8927            false,
8928        )? {
8929            WorktreeSymlinkMetadata::Found(m) => Some(m),
8930            WorktreeSymlinkMetadata::Inaccessible => continue,
8931            WorktreeSymlinkMetadata::Absent => None,
8932        };
8933
8934        if unmerged_only_paths.contains(path) {
8935            if let (Some(te), Some(meta)) = (tree_entry, wt_meta.as_ref()) {
8936                let tree_mode = crate::index::canon_mode(te.mode);
8937                let file_attrs = file_attrs_for(path);
8938                let wt_oid = hash_worktree_file(
8939                    odb,
8940                    &file_path,
8941                    meta,
8942                    &conv,
8943                    &file_attrs,
8944                    path,
8945                    None,
8946                    filter_fp,
8947                )?;
8948                let wt_mode =
8949                    mode_policy.index_mode_for_worktree(mode_from_metadata(meta), Some(tree_mode));
8950                if wt_oid != te.oid || wt_mode != tree_mode {
8951                    result.push(DiffEntry {
8952                        status: DiffStatus::for_content_change(tree_mode, wt_mode),
8953                        old_path: Some(path.clone()),
8954                        new_path: Some(path.clone()),
8955                        old_mode: FileMode::new(tree_mode),
8956                        new_mode: FileMode::new(wt_mode),
8957                        old_oid: te.oid,
8958                        new_oid: wt_oid,
8959                        score: None,
8960                    });
8961                }
8962            }
8963            continue;
8964        }
8965
8966        match (tree_entry, wt_meta) {
8967            (Some(te), Some(ref meta)) => {
8968                let tree_mode = crate::index::canon_mode(te.mode);
8969                let Some(ie) = index_entries.get(path.as_bytes()) else {
8970                    continue;
8971                };
8972                let wt_mode =
8973                    mode_policy.index_mode_for_worktree(mode_from_metadata(meta), Some(ie.mode));
8974
8975                let index_matches_tree = ie.oid == te.oid && ie.mode == tree_mode;
8976
8977                // Fully clean: index matches `HEAD`, worktree matches index, stat cache fresh.
8978                let stat_trusted = stat_matches_with_policy(ie, meta, stat_policy)
8979                    && !entry_is_racy_with_policy(ie, index_mtime, stat_policy);
8980                if index_matches_tree && stat_trusted && wt_mode == tree_mode {
8981                    continue;
8982                }
8983
8984                let file_attrs = file_attrs_for(path);
8985                let idx_ent = index_entries.get(path.as_bytes()).copied();
8986
8987                // Staged mode (same blob as `HEAD`, different mode recorded in the index) with
8988                // the worktree still matching the index. `core.filemode=false` does not discard
8989                // an explicit index chmod vs HEAD. Once the worktree drifts from the index, the
8990                // diff must describe the worktree side, so fall through.
8991                if ie.oid == te.oid && ie.mode != tree_mode && stat_trusted && wt_mode == ie.mode {
8992                    result.push(DiffEntry {
8993                        status: DiffStatus::for_content_change(tree_mode, ie.mode),
8994                        old_path: Some(path.clone()),
8995                        new_path: Some(path.clone()),
8996                        old_mode: FileMode::new(tree_mode),
8997                        new_mode: FileMode::new(ie.mode),
8998                        old_oid: te.oid,
8999                        new_oid: te.oid,
9000                        score: None,
9001                    });
9002                    continue;
9003                }
9004
9005                // Index still matches `HEAD`: only unstaged worktree drift (content and/or
9006                // worktree-only exec bit when `update-index` was not run — t4049 harness).
9007                if index_matches_tree {
9008                    let wt_oid = hash_worktree_file(
9009                        odb,
9010                        &file_path,
9011                        meta,
9012                        &conv,
9013                        &file_attrs,
9014                        path,
9015                        None,
9016                        filter_fp,
9017                    )?;
9018                    if wt_oid != te.oid {
9019                        result.push(DiffEntry {
9020                            status: DiffStatus::for_content_change(tree_mode, wt_mode),
9021                            old_path: Some(path.clone()),
9022                            new_path: Some(path.clone()),
9023                            old_mode: FileMode::new(tree_mode),
9024                            new_mode: FileMode::new(wt_mode),
9025                            old_oid: te.oid,
9026                            // Raw `git diff <tree>` uses a null OID on the worktree side (patch
9027                            // output resolves content from the working tree).
9028                            new_oid: zero_oid(),
9029                            score: None,
9030                        });
9031                    } else if wt_mode != tree_mode {
9032                        result.push(DiffEntry {
9033                            status: DiffStatus::for_content_change(tree_mode, wt_mode),
9034                            old_path: Some(path.clone()),
9035                            new_path: Some(path.clone()),
9036                            old_mode: FileMode::new(tree_mode),
9037                            new_mode: FileMode::new(wt_mode),
9038                            old_oid: te.oid,
9039                            new_oid: te.oid,
9040                            score: None,
9041                        });
9042                    }
9043                    continue;
9044                }
9045
9046                // Staged content (and possibly mode): `git diff <rev>` is tree vs working tree.
9047                let wt_oid = hash_worktree_file(
9048                    odb,
9049                    &file_path,
9050                    meta,
9051                    &conv,
9052                    &file_attrs,
9053                    path,
9054                    idx_ent,
9055                    filter_fp,
9056                )?;
9057                if wt_oid != te.oid || wt_mode != tree_mode {
9058                    let new_oid = if wt_oid != te.oid { zero_oid() } else { te.oid };
9059                    result.push(DiffEntry {
9060                        status: DiffStatus::for_content_change(tree_mode, wt_mode),
9061                        old_path: Some(path.clone()),
9062                        new_path: Some(path.clone()),
9063                        old_mode: FileMode::new(tree_mode),
9064                        new_mode: FileMode::new(wt_mode),
9065                        old_oid: te.oid,
9066                        new_oid,
9067                        score: None,
9068                    });
9069                }
9070            }
9071            (Some(te), None) => {
9072                let tree_mode = crate::index::canon_mode(te.mode);
9073                // In tree but missing from worktree
9074                result.push(DiffEntry {
9075                    status: DiffStatus::Deleted,
9076                    old_path: Some(path.clone()),
9077                    new_path: None,
9078                    old_mode: FileMode::new(tree_mode),
9079                    new_mode: FileMode::ABSENT,
9080                    old_oid: te.oid,
9081                    new_oid: zero_oid(),
9082                    score: None,
9083                });
9084            }
9085            (None, Some(ref meta)) => {
9086                // In index but not in tree, and exists in worktree
9087                let file_attrs = file_attrs_for(path);
9088                let idx_ent = index_entries.get(path.as_bytes()).copied();
9089                let wt_oid = materialize_worktree_blob(
9090                    odb,
9091                    &file_path,
9092                    meta,
9093                    &conv,
9094                    &file_attrs,
9095                    path,
9096                    idx_ent,
9097                    filter_fp,
9098                )?;
9099                let wt_mode = mode_from_metadata(meta);
9100                result.push(DiffEntry {
9101                    status: DiffStatus::Added,
9102                    old_path: None,
9103                    new_path: Some(path.clone()),
9104                    old_mode: FileMode::ABSENT,
9105                    new_mode: FileMode::new(wt_mode),
9106                    old_oid: zero_oid(),
9107                    new_oid: wt_oid,
9108                    score: None,
9109                });
9110            }
9111            (None, None) => {
9112                // Tracked in index but neither in tree nor worktree — skip
9113            }
9114        }
9115    }
9116
9117    sort_entries_by_path(&mut result);
9118    Ok(result)
9119}
9120
9121// ── Rename detection ────────────────────────────────────────────────
9122
9123/// Emitted when unmatched rename candidates exceed [`diff.renameLimit`](https://git-scm.com/docs/git-config#Documentation/git-config.txt-diffrenameLimit) after exact and basename pairing.
9124#[derive(Debug, Clone, PartialEq, Eq)]
9125pub struct InexactRenameDetectionSkipped {
9126    /// Unmatched delete-side candidates still eligible for inexact pairing.
9127    pub unmatched_sources: usize,
9128    /// Unmatched add-side candidates still eligible for inexact pairing.
9129    pub unmatched_destinations: usize,
9130    /// Configured `diff.renameLimit`, if any (`None` means unlimited).
9131    pub configured_limit: Option<usize>,
9132    /// Smallest limit that would allow the remaining matrix (`max(sources, destinations)`).
9133    pub suggested_limit: usize,
9134}
9135
9136/// Changed paths from a [`Diff`](super::Diff) metadata diff, after any rename, copy or break
9137/// pairing, with what the diff noticed along the way.
9138#[non_exhaustive]
9139#[derive(Debug, Clone, Default, PartialEq, Eq)]
9140pub struct DiffEntriesResult {
9141    /// Flat diff entries, one per changed path (renames and copies pair two paths).
9142    pub entries: Vec<DiffEntry>,
9143    /// Set when inexact rename detection was skipped due to `diff.renameLimit`.
9144    pub inexact_rename_skipped: Option<InexactRenameDetectionSkipped>,
9145    /// Index-to-work-tree diffs with
9146    /// [`TreeDiffOptions::refresh_index_stat`](super::TreeDiffOptions::refresh_index_stat):
9147    /// whether the in-memory index adopted fresh stat data and is worth writing back.
9148    pub index_stat_refreshed: bool,
9149}
9150
9151impl DiffEntriesResult {
9152    /// Entries with no rename-limit notice and no index refresh.
9153    #[must_use]
9154    pub fn new(entries: Vec<DiffEntry>) -> Self {
9155        Self {
9156            entries,
9157            ..Self::default()
9158        }
9159    }
9160}
9161
9162/// Result of [`detect_renames_with_policy`].
9163#[derive(Debug, Clone, PartialEq, Eq)]
9164pub(crate) struct RenameDetectionResult {
9165    /// Diff entries after rename pairing.
9166    pub entries: Vec<DiffEntry>,
9167    /// Set when inexact rename detection was skipped due to `diff.renameLimit`.
9168    pub inexact_skipped: Option<InexactRenameDetectionSkipped>,
9169}
9170
9171/// Options for [`detect_renames_with_policy`].
9172#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
9173pub(crate) struct DetectRenamesOptions {
9174    /// Skip Git's basename pre-pass (break-rewrite and copy detection paths).
9175    pub skip_basename_heuristic: bool,
9176}
9177
9178fn read_added_entry_bytes(
9179    odb: &Odb,
9180    entry: &DiffEntry,
9181    work_root: Option<&Path>,
9182) -> Option<Vec<u8>> {
9183    if entry.new_oid != zero_oid() {
9184        if let Ok(obj) = odb.read(&entry.new_oid) {
9185            return Some(obj.data);
9186        }
9187    }
9188    let path = entry.new_path.as_deref()?;
9189    let root = work_root?;
9190    fs::read(root.join(path)).ok()
9191}
9192
9193/// Detect renames by pairing Deleted and Added entries with similar content.
9194///
9195/// `threshold` is the minimum similarity percentage (0–100) for a pair to
9196/// be considered a rename (Git's default is 50%).  The function reads blob
9197/// content from the ODB to compute a line-level similarity score.
9198///
9199/// Exact-OID matches are always 100% similar regardless of content.
9200///
9201/// When `work_root` is set, added entries whose `new_oid` is the zero placeholder (as in
9202/// uncached `diff-index` when the work tree diverged from the index) load content from disk
9203/// under that root instead of the object database.
9204#[allow(clippy::expect_used)]
9205pub(crate) fn detect_renames(
9206    odb: &Odb,
9207    work_root: Option<&Path>,
9208    entries: Vec<DiffEntry>,
9209    threshold: u32,
9210) -> Vec<DiffEntry> {
9211    let config = ConfigSet::new();
9212    let policy = ThreadPolicy::default();
9213    detect_renames_with_policy(
9214        odb,
9215        work_root,
9216        entries,
9217        threshold,
9218        &config,
9219        &policy,
9220        None,
9221        DetectRenamesOptions::default(),
9222    )
9223    .expect("rename detection")
9224    .entries
9225}
9226
9227#[inline]
9228fn poll_rename_cancel(cancel: Option<&crate::progress::CancelToken>) -> crate::error::Result<()> {
9229    if let Some(token) = cancel {
9230        token.poll_result()?;
9231    }
9232    Ok(())
9233}
9234
9235/// Like `detect_renames`, but honors `index.threads` / `--threads` via `thread_policy`.
9236///
9237/// A list without both an added and a deleted entry has nothing to pair. When its paths
9238/// are also unique and in [`sort_entries_by_path`] order, as tree diffs return them, the
9239/// list comes back unchanged without reading blobs or allocating.
9240///
9241/// # Errors
9242///
9243/// Returns object database errors from reading candidate blobs, and the cancellation
9244/// error when `cancel` is set.
9245#[allow(clippy::too_many_arguments)]
9246pub(crate) fn detect_renames_with_policy(
9247    odb: &Odb,
9248    work_root: Option<&Path>,
9249    entries: Vec<DiffEntry>,
9250    threshold: u32,
9251    config: &ConfigSet,
9252    thread_policy: &ThreadPolicy,
9253    cancel: Option<&crate::progress::CancelToken>,
9254    detect_opts: DetectRenamesOptions,
9255) -> Result<RenameDetectionResult> {
9256    poll_rename_cancel(cancel)?;
9257    let has_deleted = entries.iter().any(|e| e.status == DiffStatus::Deleted);
9258    let has_added = entries.iter().any(|e| e.status == DiffStatus::Added);
9259    if (!has_deleted || !has_added) && entries_strictly_sorted_by_path(&entries) {
9260        // With unique sorted paths, regrouping and re-sorting would rebuild the same list.
9261        return Ok(RenameDetectionResult {
9262            entries,
9263            inexact_skipped: None,
9264        });
9265    }
9266    // Split entries into deleted, added, and others.
9267    let mut deleted: Vec<DiffEntry> = Vec::new();
9268    let mut added: Vec<DiffEntry> = Vec::new();
9269    let mut others: Vec<DiffEntry> = Vec::new();
9270
9271    for entry in entries {
9272        match entry.status {
9273            DiffStatus::Deleted => deleted.push(entry),
9274            DiffStatus::Added => added.push(entry),
9275            _ => others.push(entry),
9276        }
9277    }
9278
9279    if deleted.is_empty() || added.is_empty() {
9280        // Nothing to pair — return original order.
9281        let mut result = others;
9282        result.extend(deleted);
9283        result.extend(added);
9284        sort_entries_by_path(&mut result);
9285        return Ok(RenameDetectionResult {
9286            entries: result,
9287            inexact_skipped: None,
9288        });
9289    }
9290
9291    fn is_regularish_mode(mode: FileMode) -> bool {
9292        mode == FileMode::REGULAR || mode == FileMode::EXECUTABLE
9293    }
9294
9295    fn same_path_same_blob(del: &DiffEntry, add: &DiffEntry) -> bool {
9296        del.old_path == add.new_path && del.old_oid == add.new_oid && del.old_mode == add.new_mode
9297    }
9298
9299    fn sort_rename_scores(
9300        scores: &mut [(u32, usize, usize)],
9301        deleted: &[DiffEntry],
9302        added: &[DiffEntry],
9303    ) {
9304        scores.sort_by(|a, b| {
9305            let a_noop = same_path_same_blob(&deleted[a.1], &added[a.2]);
9306            let b_noop = same_path_same_blob(&deleted[b.1], &added[b.2]);
9307            let a_same = same_basename(&deleted[a.1], &added[a.2]);
9308            let b_same = same_basename(&deleted[b.1], &added[b.2]);
9309            a_noop
9310                .cmp(&b_noop)
9311                .then_with(|| b_same.cmp(&a_same))
9312                .then_with(|| b.0.cmp(&a.0))
9313        });
9314    }
9315
9316    fn apply_rename_scores(
9317        scores: &[(u32, usize, usize)],
9318        deleted: &[DiffEntry],
9319        added: &[DiffEntry],
9320        used_deleted: &mut [bool],
9321        used_added: &mut [bool],
9322        renames: &mut Vec<DiffEntry>,
9323    ) {
9324        for (score, di, ai) in scores {
9325            if used_deleted[*di] || used_added[*ai] {
9326                continue;
9327            }
9328            used_deleted[*di] = true;
9329            used_added[*ai] = true;
9330
9331            let del = &deleted[*di];
9332            let add = &added[*ai];
9333            if same_path_same_blob(del, add) {
9334                continue;
9335            }
9336
9337            renames.push(DiffEntry {
9338                status: DiffStatus::Renamed,
9339                old_path: del.old_path.clone(),
9340                new_path: add.new_path.clone(),
9341                old_mode: del.old_mode,
9342                new_mode: add.new_mode,
9343                old_oid: del.old_oid,
9344                new_oid: add.new_oid,
9345                score: Some(*score),
9346            });
9347        }
9348    }
9349
9350    let mut used_deleted = vec![false; deleted.len()];
9351    let mut used_added = vec![false; added.len()];
9352    let mut renames: Vec<DiffEntry> = Vec::new();
9353
9354    // Git always runs exact rename detection, regardless of renameLimit.
9355    let mut added_by_oid: std::collections::HashMap<ObjectId, Vec<usize>> =
9356        std::collections::HashMap::new();
9357    for (ai, add) in added.iter().enumerate() {
9358        added_by_oid.entry(add.new_oid).or_default().push(ai);
9359    }
9360    let mut exact_scores: Vec<(u32, usize, usize)> = Vec::new();
9361    for (di, del) in deleted.iter().enumerate() {
9362        poll_rename_cancel(cancel)?;
9363        if let Some(ais) = added_by_oid.get(&del.old_oid) {
9364            for &ai in ais {
9365                exact_scores.push((100, di, ai));
9366            }
9367        }
9368    }
9369    sort_rename_scores(&mut exact_scores, &deleted, &added);
9370    apply_rename_scores(
9371        &exact_scores,
9372        &deleted,
9373        &added,
9374        &mut used_deleted,
9375        &mut used_added,
9376        &mut renames,
9377    );
9378
9379    let mut blob_cache = None;
9380    if !detect_opts.skip_basename_heuristic
9381        && super::basename_rename::basename_needs_similarity_scoring(
9382            &deleted,
9383            &added,
9384            &used_deleted,
9385            &used_added,
9386        )
9387    {
9388        let mut cache = super::rename_blob_cache::RenameBlobCache::new(deleted.len(), added.len());
9389        let par_opts = diff_rename_parallel_options(config, thread_policy);
9390        super::basename_rename::apply_basename_rename_pairs(
9391            odb,
9392            work_root,
9393            &deleted,
9394            &added,
9395            &mut used_deleted,
9396            &mut used_added,
9397            &mut renames,
9398            threshold,
9399            cancel,
9400            &mut cache,
9401            par_opts,
9402        )?;
9403        blob_cache = Some(cache);
9404    }
9405
9406    let remaining_deleted: Vec<usize> = used_deleted
9407        .iter()
9408        .enumerate()
9409        .filter_map(|(i, used)| (!*used).then_some(i))
9410        .collect();
9411    let remaining_added: Vec<usize> = used_added
9412        .iter()
9413        .enumerate()
9414        .filter_map(|(i, used)| (!*used).then_some(i))
9415        .collect();
9416
9417    let rename_limit = parse_rename_limit(config);
9418    let mut inexact_skipped = None;
9419    if !remaining_deleted.is_empty()
9420        && !remaining_added.is_empty()
9421        && inexact_matrix_exceeds_limit(
9422            remaining_added.len(),
9423            remaining_deleted.len(),
9424            rename_limit,
9425        )
9426    {
9427        let suggested = remaining_deleted.len().max(remaining_added.len());
9428        inexact_skipped = Some(InexactRenameDetectionSkipped {
9429            unmatched_sources: remaining_deleted.len(),
9430            unmatched_destinations: remaining_added.len(),
9431            configured_limit: rename_limit.configured_max(),
9432            suggested_limit: suggested,
9433        });
9434    } else if !remaining_deleted.is_empty() && !remaining_added.is_empty() {
9435        let par_opts = diff_rename_parallel_options(config, thread_policy);
9436
9437        let mut prefetch_entries: Vec<DiffEntry> =
9438            Vec::with_capacity(remaining_deleted.len() + remaining_added.len());
9439        for &di in &remaining_deleted {
9440            prefetch_entries.push(deleted[di].clone());
9441        }
9442        for &ai in &remaining_added {
9443            prefetch_entries.push(added[ai].clone());
9444        }
9445        super::tree_content::prefetch_tree_diff_blobs(odb, &prefetch_entries)?;
9446
9447        let deleted_sizes: Vec<Option<usize>> = remaining_deleted
9448            .iter()
9449            .map(|&di| blob_payload_size(odb, &deleted[di].old_oid))
9450            .collect();
9451        let added_sizes: Vec<Option<usize>> = remaining_added
9452            .iter()
9453            .map(|&ai| added_entry_payload_size(odb, &added[ai], work_root))
9454            .collect();
9455
9456        let mut load_deleted = vec![false; remaining_deleted.len()];
9457        let mut load_added = vec![false; remaining_added.len()];
9458        let mut candidate_pairs: Vec<(usize, usize)> = Vec::new();
9459        for (row, &di) in remaining_deleted.iter().enumerate() {
9460            poll_rename_cancel(cancel)?;
9461            let del = &deleted[di];
9462            if !is_regularish_mode(del.old_mode) {
9463                continue;
9464            }
9465            let Some(ds) = deleted_sizes[row] else {
9466                continue;
9467            };
9468            for (col, &ai) in remaining_added.iter().enumerate() {
9469                let add = &added[ai];
9470                if !is_regularish_mode(add.new_mode) {
9471                    continue;
9472                }
9473                let Some(asz) = added_sizes[col] else {
9474                    continue;
9475                };
9476                if rename_pair_size_reachable(ds, asz, threshold) {
9477                    load_deleted[row] = true;
9478                    load_added[col] = true;
9479                    candidate_pairs.push((row, col));
9480                }
9481            }
9482        }
9483
9484        if !candidate_pairs.is_empty() {
9485            let loads_needed = load_deleted.iter().filter(|&&b| b).count()
9486                + load_added.iter().filter(|&&b| b).count();
9487            let (deleted_contents, added_contents) = if let Some(cache) = blob_cache.as_mut() {
9488                let mut deleted_staging: Vec<Option<Vec<u8>>> = vec![None; remaining_deleted.len()];
9489                for (row, &need) in load_deleted.iter().enumerate() {
9490                    if !need {
9491                        continue;
9492                    }
9493                    let di = remaining_deleted[row];
9494                    deleted_staging[row] = cache
9495                        .take_deleted_blob(di)
9496                        .or_else(|| odb.read(&deleted[di].old_oid).ok().map(|obj| obj.data));
9497                }
9498                let mut added_staging: Vec<Option<Vec<u8>>> = vec![None; remaining_added.len()];
9499                for (col, &need) in load_added.iter().enumerate() {
9500                    if !need {
9501                        continue;
9502                    }
9503                    let ai = remaining_added[col];
9504                    added_staging[col] = cache
9505                        .take_added_blob(ai)
9506                        .or_else(|| read_added_entry_bytes(odb, &added[ai], work_root));
9507                }
9508                let load_deleted_blob = |i: usize| -> Option<Vec<u8>> {
9509                    if !load_deleted[i] {
9510                        return None;
9511                    }
9512                    deleted_staging[i].clone()
9513                };
9514                let load_added_blob = |i: usize| -> Option<Vec<u8>> {
9515                    if !load_added[i] {
9516                        return None;
9517                    }
9518                    added_staging[i].clone()
9519                };
9520                if loads_needed <= 32 {
9521                    (
9522                        (0..remaining_deleted.len())
9523                            .map(load_deleted_blob)
9524                            .collect(),
9525                        (0..remaining_added.len()).map(load_added_blob).collect(),
9526                    )
9527                } else {
9528                    (
9529                        parallel_load_optional_blobs(
9530                            odb,
9531                            remaining_deleted.len(),
9532                            par_opts,
9533                            0,
9534                            load_deleted_blob,
9535                        ),
9536                        parallel_load_optional_blobs(
9537                            odb,
9538                            remaining_added.len(),
9539                            par_opts,
9540                            0,
9541                            load_added_blob,
9542                        ),
9543                    )
9544                }
9545            } else {
9546                let mut deleted_staging: Vec<Option<Vec<u8>>> = vec![None; remaining_deleted.len()];
9547                for (row, &need) in load_deleted.iter().enumerate() {
9548                    if !need {
9549                        continue;
9550                    }
9551                    let di = remaining_deleted[row];
9552                    deleted_staging[row] = odb.read(&deleted[di].old_oid).ok().map(|obj| obj.data);
9553                }
9554                let mut added_staging: Vec<Option<Vec<u8>>> = vec![None; remaining_added.len()];
9555                for (col, &need) in load_added.iter().enumerate() {
9556                    if !need {
9557                        continue;
9558                    }
9559                    let ai = remaining_added[col];
9560                    added_staging[col] = read_added_entry_bytes(odb, &added[ai], work_root);
9561                }
9562                let load_deleted_blob = |i: usize| -> Option<Vec<u8>> {
9563                    if !load_deleted[i] {
9564                        return None;
9565                    }
9566                    deleted_staging[i].clone()
9567                };
9568                let load_added_blob = |i: usize| -> Option<Vec<u8>> {
9569                    if !load_added[i] {
9570                        return None;
9571                    }
9572                    added_staging[i].clone()
9573                };
9574                if loads_needed <= 32 {
9575                    (
9576                        (0..remaining_deleted.len())
9577                            .map(load_deleted_blob)
9578                            .collect(),
9579                        (0..remaining_added.len()).map(load_added_blob).collect(),
9580                    )
9581                } else {
9582                    (
9583                        parallel_load_optional_blobs(
9584                            odb,
9585                            remaining_deleted.len(),
9586                            par_opts,
9587                            0,
9588                            load_deleted_blob,
9589                        ),
9590                        parallel_load_optional_blobs(
9591                            odb,
9592                            remaining_added.len(),
9593                            par_opts,
9594                            0,
9595                            load_added_blob,
9596                        ),
9597                    )
9598                }
9599            };
9600
9601            let est_table_bytes: usize = deleted_contents
9602                .iter()
9603                .chain(added_contents.iter())
9604                .filter_map(|opt| opt.as_ref().map(|b| b.len()))
9605                .sum();
9606            let build_deleted_table = |row: usize| -> std::result::Result<
9607                Option<RenameLineTable>,
9608                std::convert::Infallible,
9609            > {
9610                Ok(deleted_contents[row]
9611                    .as_ref()
9612                    .map(|d| RenameLineTable::from_blob(d)))
9613            };
9614            let build_added_table = |col: usize| -> std::result::Result<
9615                Option<RenameLineTable>,
9616                std::convert::Infallible,
9617            > {
9618                Ok(added_contents[col]
9619                    .as_ref()
9620                    .map(|d| RenameLineTable::from_blob(d)))
9621            };
9622            let deleted_tables: Vec<Option<RenameLineTable>> = if loads_needed <= 32 {
9623                (0..remaining_deleted.len())
9624                    .map(|row| build_deleted_table(row).unwrap())
9625                    .collect()
9626            } else {
9627                parallel_map_indices(
9628                    odb,
9629                    remaining_deleted.len(),
9630                    par_opts,
9631                    est_table_bytes,
9632                    build_deleted_table,
9633                )
9634                .expect("deleted rename line tables")
9635            };
9636            let added_tables: Vec<Option<RenameLineTable>> = if loads_needed <= 32 {
9637                (0..remaining_added.len())
9638                    .map(|col| build_added_table(col).unwrap())
9639                    .collect()
9640            } else {
9641                parallel_map_indices(
9642                    odb,
9643                    remaining_added.len(),
9644                    par_opts,
9645                    est_table_bytes,
9646                    build_added_table,
9647                )
9648                .expect("added rename line tables")
9649            };
9650
9651            let mut inexact_scores: Vec<(u32, usize, usize)> = Vec::new();
9652            for (row, col) in candidate_pairs {
9653                poll_rename_cancel(cancel)?;
9654                let di = remaining_deleted[row];
9655                let ai = remaining_added[col];
9656                let (Some(old_table), Some(new_table)) = (&deleted_tables[row], &added_tables[col])
9657                else {
9658                    continue;
9659                };
9660                let score = RenameLineTable::similarity_score(old_table, new_table);
9661                if score >= threshold {
9662                    inexact_scores.push((score, di, ai));
9663                }
9664            }
9665            sort_rename_scores(&mut inexact_scores, &deleted, &added);
9666            apply_rename_scores(
9667                &inexact_scores,
9668                &deleted,
9669                &added,
9670                &mut used_deleted,
9671                &mut used_added,
9672                &mut renames,
9673            );
9674        }
9675    }
9676
9677    // Collect unmatched entries.
9678    let mut result = others;
9679    result.extend(renames);
9680    for (i, entry) in deleted.into_iter().enumerate() {
9681        if !used_deleted[i] {
9682            result.push(entry);
9683        }
9684    }
9685    for (i, entry) in added.into_iter().enumerate() {
9686        if !used_added[i] {
9687            result.push(entry);
9688        }
9689    }
9690
9691    result = prune_break_deletions_obviated_by_renames(result);
9692    sort_entries_by_path(&mut result);
9693    Ok(RenameDetectionResult {
9694        entries: result,
9695        inexact_skipped,
9696    })
9697}
9698
9699/// Detect copies among diff entries.
9700///
9701/// This first runs rename detection (pairing Deleted+Added), then for any
9702/// remaining Added entries, looks for copy sources.
9703///
9704/// - `find_copies_harder` = false: only Modified entries are copy source candidates.
9705/// - `find_copies_harder` = true: also examine unmodified files from `source_tree_entries`.
9706///
9707/// `source_tree_entries` should be a list of (path, mode, oid) from the source tree;
9708/// used when `find_copies_harder` is true to consider unmodified files as copy sources.
9709/// # Errors
9710///
9711/// Returns [`crate::error::ObjectError::HashCollision`] when hashing worktree blob content for copy detection
9712/// would produce a known-bad SHA-1 object id.
9713#[allow(dead_code)]
9714fn detect_copies(
9715    odb: &Odb,
9716    work_root: Option<&Path>,
9717    entries: Vec<DiffEntry>,
9718    threshold: u32,
9719    find_copies_harder: bool,
9720    source_tree_entries: &[(String, FileMode, ObjectId)],
9721) -> Result<Vec<DiffEntry>> {
9722    let config = ConfigSet::new();
9723    let policy = ThreadPolicy::default();
9724    detect_copies_with_policy(
9725        odb,
9726        work_root,
9727        entries,
9728        threshold,
9729        find_copies_harder,
9730        source_tree_entries,
9731        &config,
9732        &policy,
9733    )
9734}
9735
9736/// Like `detect_copies`, but honors parallel thread policy and rename limits.
9737///
9738/// # Errors
9739///
9740/// Same as `detect_copies`.
9741#[expect(clippy::too_many_arguments)]
9742pub(crate) fn detect_copies_with_policy(
9743    odb: &Odb,
9744    work_root: Option<&Path>,
9745    entries: Vec<DiffEntry>,
9746    threshold: u32,
9747    find_copies_harder: bool,
9748    source_tree_entries: &[(String, FileMode, ObjectId)],
9749    config: &ConfigSet,
9750    thread_policy: &ThreadPolicy,
9751) -> Result<Vec<DiffEntry>> {
9752    use std::collections::{HashMap, HashSet};
9753
9754    // Separate entries by status.
9755    let mut deleted: Vec<DiffEntry> = Vec::new();
9756    let mut added: Vec<DiffEntry> = Vec::new();
9757    let mut others: Vec<DiffEntry> = Vec::new();
9758
9759    for entry in entries {
9760        match entry.status {
9761            DiffStatus::Deleted => deleted.push(entry),
9762            DiffStatus::Added => added.push(entry),
9763            _ => others.push(entry),
9764        }
9765    }
9766
9767    // Build source candidates: deleted files, modified files, and optionally tree entries.
9768    // Track which sources are from deleted files (can become renames).
9769    let mut sources: Vec<(String, ObjectId, bool)> = Vec::new(); // (path, oid, is_deleted)
9770    let mut deleted_source_idx: HashMap<String, usize> = HashMap::new();
9771
9772    for entry in &deleted {
9773        if let Some(ref path) = entry.old_path {
9774            deleted_source_idx.insert(path.clone(), sources.len());
9775            sources.push((path.clone(), entry.old_oid, true));
9776        }
9777    }
9778
9779    // Modified and type-changed files are candidates for `-C` (e.g. symlink rewrite leaves the
9780    // old blob available as a copy source for another path; see `t4008-diff-break-rewrite`).
9781    for entry in &others {
9782        if matches!(entry.status, DiffStatus::Modified | DiffStatus::TypeChanged) {
9783            if let Some(ref old_path) = entry.old_path {
9784                if !sources.iter().any(|(p, _, _)| p == old_path) {
9785                    sources.push((old_path.clone(), entry.old_oid, false));
9786                }
9787            }
9788        }
9789    }
9790
9791    // With find_copies_harder, add all source tree entries.
9792    if find_copies_harder {
9793        for (path, _mode, oid) in source_tree_entries {
9794            if !sources.iter().any(|(p, _, _)| p == path) {
9795                sources.push((path.clone(), *oid, false));
9796            }
9797        }
9798    }
9799
9800    // Modes of every source, for the old side of copy and rename entries: the deleted and
9801    // modified entries carry their own, and `source_tree_entries` the rest.
9802    let mut source_modes: Vec<(String, FileMode, ObjectId)> = source_tree_entries.to_vec();
9803    for entry in deleted.iter().chain(&others) {
9804        if let Some(ref path) = entry.old_path {
9805            if !source_modes.iter().any(|(p, _, _)| p == path) {
9806                source_modes.push((path.clone(), entry.old_mode, entry.old_oid));
9807            }
9808        }
9809    }
9810
9811    if sources.is_empty() {
9812        let mut result = others;
9813        result.extend(deleted);
9814        result.extend(added);
9815        sort_entries_by_path(&mut result);
9816        return Ok(result);
9817    }
9818
9819    let par_opts = diff_rename_parallel_options(config, thread_policy);
9820    let rename_limit = parse_rename_limit(config);
9821
9822    fn apply_copy_score_assignments(
9823        scores: &[(u32, usize, usize)],
9824        sources: &[(String, ObjectId, bool)],
9825        added: &[DiffEntry],
9826        source_tree_entries: &[(String, FileMode, ObjectId)],
9827        used_added2: &mut [bool],
9828        renamed_deleted: &mut HashSet<usize>,
9829        result_entries: &mut Vec<DiffEntry>,
9830    ) {
9831        let mut used_added = vec![false; added.len()];
9832        let mut source_to_added: HashMap<usize, Vec<(usize, u32)>> = HashMap::new();
9833        for &(score, si, ai) in scores {
9834            if used_added[ai] {
9835                continue;
9836            }
9837            used_added[ai] = true;
9838            source_to_added.entry(si).or_default().push((ai, score));
9839        }
9840
9841        for (&si, assignments_for_src) in &source_to_added {
9842            let (_, _, is_deleted) = &sources[si];
9843            if *is_deleted && !assignments_for_src.is_empty() {
9844                let rename_ai = assignments_for_src
9845                    .iter()
9846                    .max_by_key(|(ai, _score)| added[*ai].path().to_string())
9847                    .map(|(ai, _)| *ai);
9848
9849                for &(ai, score) in assignments_for_src {
9850                    let (ref src_path, _, _) = sources[si];
9851                    let add = &added[ai];
9852                    let src_mode = source_tree_entries
9853                        .iter()
9854                        .find(|(p, _, _)| p == src_path)
9855                        .map_or(add.old_mode, |(_, m, _)| *m);
9856
9857                    let is_rename = Some(ai) == rename_ai;
9858                    result_entries.push(DiffEntry {
9859                        status: if is_rename {
9860                            DiffStatus::Renamed
9861                        } else {
9862                            DiffStatus::Copied
9863                        },
9864                        old_path: Some(src_path.clone()),
9865                        new_path: add.new_path.clone(),
9866                        old_mode: src_mode,
9867                        new_mode: add.new_mode,
9868                        old_oid: sources[si].1,
9869                        new_oid: add.new_oid,
9870                        score: Some(score),
9871                    });
9872                    used_added2[ai] = true;
9873                }
9874                renamed_deleted.insert(si);
9875            } else {
9876                for &(ai, score) in assignments_for_src {
9877                    let (ref src_path, _, _) = sources[si];
9878                    let add = &added[ai];
9879                    let src_mode = source_tree_entries
9880                        .iter()
9881                        .find(|(p, _, _)| p == src_path)
9882                        .map_or(add.old_mode, |(_, m, _)| *m);
9883
9884                    result_entries.push(DiffEntry {
9885                        status: DiffStatus::Copied,
9886                        old_path: Some(src_path.clone()),
9887                        new_path: add.new_path.clone(),
9888                        old_mode: src_mode,
9889                        new_mode: add.new_mode,
9890                        old_oid: sources[si].1,
9891                        new_oid: add.new_oid,
9892                        score: Some(score),
9893                    });
9894                    used_added2[ai] = true;
9895                }
9896            }
9897        }
9898    }
9899
9900    fn added_entry_oid(
9901        odb: &Odb,
9902        add: &DiffEntry,
9903        added_contents: &[Option<Vec<u8>>],
9904        ai: usize,
9905    ) -> Result<ObjectId> {
9906        if add.new_oid != zero_oid() {
9907            Ok(add.new_oid)
9908        } else if let Some(ref data) = added_contents[ai] {
9909            odb.hash(ObjectKind::Blob, data)
9910        } else {
9911            Ok(zero_oid())
9912        }
9913    }
9914
9915    let mut result_entries: Vec<DiffEntry> = Vec::new();
9916    let mut renamed_deleted: HashSet<usize> = HashSet::new();
9917    let mut used_added2 = vec![false; added.len()];
9918    let mut source_contents = vec![None; sources.len()];
9919
9920    if !added.is_empty() {
9921        let added_contents = parallel_load_optional_blobs(odb, added.len(), par_opts, 0, |ai| {
9922            read_added_entry_bytes(odb, &added[ai], work_root)
9923        });
9924
9925        // Git always scores exact OID copy/rename pairs before applying renameLimit.
9926        let mut exact_scores: Vec<(u32, usize, usize)> = Vec::new();
9927        for (si, (src_path, src_oid, _)) in sources.iter().enumerate() {
9928            for (ai, add) in added.iter().enumerate() {
9929                if add.new_path.as_deref() == Some(src_path.as_str()) {
9930                    continue;
9931                }
9932                let add_oid = added_entry_oid(odb, add, &added_contents, ai)?;
9933                if *src_oid == add_oid && add_oid != zero_oid() {
9934                    exact_scores.push((100, si, ai));
9935                }
9936            }
9937        }
9938        exact_scores.sort_by_key(|a| std::cmp::Reverse(a.0));
9939        apply_copy_score_assignments(
9940            &exact_scores,
9941            &sources,
9942            &added,
9943            &source_modes,
9944            &mut used_added2,
9945            &mut renamed_deleted,
9946            &mut result_entries,
9947        );
9948
9949        let remaining_added: Vec<usize> = used_added2
9950            .iter()
9951            .enumerate()
9952            .filter_map(|(i, used)| (!*used).then_some(i))
9953            .collect();
9954        let remaining_source_count = sources
9955            .iter()
9956            .enumerate()
9957            .filter(|(si, (_, _, is_deleted))| !*is_deleted || !renamed_deleted.contains(si))
9958            .count();
9959
9960        let skip_inexact = remaining_added.is_empty()
9961            || inexact_matrix_exceeds_limit(
9962                remaining_added.len(),
9963                remaining_source_count,
9964                rename_limit,
9965            );
9966
9967        if !skip_inexact {
9968            source_contents = parallel_load_optional_blobs(odb, sources.len(), par_opts, 0, |si| {
9969                odb.read(&sources[si].1).ok().map(|obj| obj.data)
9970            });
9971
9972            let est_bytes: usize = source_contents
9973                .iter()
9974                .chain(added_contents.iter())
9975                .filter_map(|opt| opt.as_ref().map(|b| b.len()))
9976                .sum();
9977
9978            let source_tables: Vec<Option<RenameLineTable>> = parallel_map_indices(
9979                odb,
9980                sources.len(),
9981                par_opts,
9982                est_bytes,
9983                |si| -> std::result::Result<Option<RenameLineTable>, std::convert::Infallible> {
9984                    Ok(source_contents[si]
9985                        .as_ref()
9986                        .map(|d| RenameLineTable::from_blob(d)))
9987                },
9988            )
9989            .expect("copy source line tables");
9990            let added_tables: Vec<Option<RenameLineTable>> = parallel_map_indices(
9991                odb,
9992                added.len(),
9993                par_opts,
9994                est_bytes,
9995                |ai| -> std::result::Result<Option<RenameLineTable>, std::convert::Infallible> {
9996                    Ok(added_contents[ai]
9997                        .as_ref()
9998                        .map(|d| RenameLineTable::from_blob(d)))
9999                },
10000            )
10001            .expect("copy added line tables");
10002
10003            let score_rows: Vec<Vec<(u32, usize, usize)>> = parallel_map_indices(
10004                odb,
10005                sources.len(),
10006                par_opts,
10007                est_bytes,
10008                |si| -> std::result::Result<Vec<(u32, usize, usize)>, std::convert::Infallible> {
10009                    if renamed_deleted.contains(&si) {
10010                        return Ok(Vec::new());
10011                    }
10012                    let (src_path, _, _) = &sources[si];
10013                    let mut row: Vec<(u32, usize, usize)> = Vec::new();
10014                    for &ai in &remaining_added {
10015                        let add = &added[ai];
10016                        if add.new_path.as_deref() == Some(src_path.as_str()) {
10017                            continue;
10018                        }
10019                        let score = match (&source_tables[si], &added_tables[ai]) {
10020                            (Some(old_table), Some(new_table)) => {
10021                                RenameLineTable::similarity_score(old_table, new_table)
10022                            }
10023                            _ => 0,
10024                        };
10025                        if score >= threshold {
10026                            row.push((score, si, ai));
10027                        }
10028                    }
10029                    Ok(row)
10030                },
10031            )
10032            .expect("copy score rows");
10033            let mut inexact_scores: Vec<(u32, usize, usize)> =
10034                score_rows.into_iter().flatten().collect();
10035            inexact_scores.sort_by_key(|a| std::cmp::Reverse(a.0));
10036            apply_copy_score_assignments(
10037                &inexact_scores,
10038                &sources,
10039                &added,
10040                &source_modes,
10041                &mut used_added2,
10042                &mut renamed_deleted,
10043                &mut result_entries,
10044            );
10045        }
10046    }
10047
10048    // Keep deleted entries that weren't consumed by a rename.
10049    for entry in deleted.into_iter() {
10050        if let Some(ref path) = entry.old_path {
10051            if let Some(&si) = deleted_source_idx.get(path) {
10052                if renamed_deleted.contains(&si) {
10053                    // This deletion was consumed by a rename; skip it.
10054                    continue;
10055                }
10056            }
10057        }
10058        result_entries.push(entry);
10059    }
10060
10061    let mut result = others;
10062    result.extend(result_entries);
10063    // Keep unmatched added entries.
10064    for (i, entry) in added.into_iter().enumerate() {
10065        if !used_added2[i] {
10066            result.push(entry);
10067        }
10068    }
10069
10070    // Like `git diff -C`, only created paths become copies: a modification is a copy
10071    // destination only after `-B` splits it into a deletion and an addition, which
10072    // `postprocess_diff_entries` does before calling this.
10073    let mut final_result = result;
10074    sort_entries_by_path(&mut final_result);
10075    Ok(final_result)
10076}
10077
10078/// Options for Git-style rename, copy, and break-rewrite post-processing on tree diffs.
10079#[derive(Debug, Clone, Copy)]
10080pub(crate) struct DiffPostprocessOptions {
10081    /// When set, `--break-rewrites` threshold on Git's internal 0–the internal max score (60_000) scale
10082    /// (`None` = off).
10083    pub break_score: Option<u64>,
10084    /// `-M` similarity threshold (0–100); `100` disables rename detection.
10085    pub rename_threshold: u32,
10086    /// `-C` copy threshold (0–100); `None` skips copy detection.
10087    pub copy_threshold: Option<u32>,
10088    /// `-C --find-copies-harder`: treat unmodified source-tree paths as copy sources.
10089    pub find_copies_harder: bool,
10090    /// When break-rewrite runs on tree↔worktree diffs, fill `new_oid` from worktree bytes for
10091    /// `--raw` output (matches `git diff <tree> -B`; leave false for index↔worktree / diff-files).
10092    pub materialize_break_worktree_new_oid: bool,
10093}
10094
10095impl Default for DiffPostprocessOptions {
10096    fn default() -> Self {
10097        Self {
10098            break_score: None,
10099            rename_threshold: 100,
10100            copy_threshold: None,
10101            find_copies_harder: false,
10102            materialize_break_worktree_new_oid: false,
10103        }
10104    }
10105}
10106
10107/// Index and git directory used to read canonical (clean) worktree bytes during break-rewrite
10108/// post-processing on tree↔worktree diffs.
10109#[derive(Debug, Clone, Copy)]
10110pub(crate) struct DiffPostprocessWorktree<'a> {
10111    /// Current index; stage-0 entries supply autocrlf index-guard blobs when needed.
10112    pub index: &'a Index,
10113}
10114
10115fn blob_bytes(odb: &Odb, oid: &ObjectId) -> Result<Vec<u8>> {
10116    Ok(odb.read(oid)?.data)
10117}
10118
10119/// Bytes on the "new" side of a diff entry (ODB blob, or worktree file when `new_oid` is null).
10120///
10121/// When `worktree` is set, worktree paths are normalized with the same attribute/filter rules as
10122/// [`diff_tree_to_worktree`]; otherwise raw filesystem bytes are read.
10123fn read_new_side_bytes(
10124    odb: &Odb,
10125    entry: &DiffEntry,
10126    work_root: Option<&Path>,
10127    worktree: Option<&DiffPostprocessWorktree<'_>>,
10128    config: &ConfigSet,
10129) -> Result<Vec<u8>> {
10130    if entry.new_oid != zero_oid() {
10131        return blob_bytes(odb, &entry.new_oid);
10132    }
10133    let path = entry.new_path.as_deref().ok_or_else(|| {
10134        Error::Object(ObjectError::NotFound {
10135            name: "modified entry missing new_path for worktree read".into(),
10136        })
10137    })?;
10138    let root = work_root.ok_or_else(|| {
10139        Error::Object(ObjectError::NotFound {
10140            name: format!(
10141                "worktree bytes required for {} but no work_root was provided",
10142                path
10143            ),
10144        })
10145    })?;
10146    if let Some(wctx) = worktree {
10147        let mode = entry.new_mode.bits();
10148        let index_entry = wctx.index.get(path.as_bytes(), 0);
10149        return read_worktree_side_bytes_for_diff(odb, root, path, mode, config, index_entry, None);
10150    }
10151    fs::read(root.join(path)).map_err(Error::Io)
10152}
10153
10154/// True when clean-smudged worktree bytes differ from the raw on-disk file (e.g. `eol=lf` + CRLF).
10155fn worktree_side_differs_after_normalize(
10156    odb: &Odb,
10157    entry: &DiffEntry,
10158    work_root: &Path,
10159    worktree: &DiffPostprocessWorktree<'_>,
10160    config: &ConfigSet,
10161) -> Result<bool> {
10162    let path = entry.new_path.as_deref().ok_or_else(|| {
10163        Error::Object(ObjectError::NotFound {
10164            name: "modified entry missing new_path for worktree read".into(),
10165        })
10166    })?;
10167    let raw = fs::read(work_root.join(path)).map_err(Error::Io)?;
10168    let canonical = read_new_side_bytes(odb, entry, Some(work_root), Some(worktree), config)?;
10169    Ok(raw != canonical)
10170}
10171
10172fn split_broken_modifications(
10173    odb: &Odb,
10174    work_root: Option<&Path>,
10175    entries: Vec<DiffEntry>,
10176    break_score: u64,
10177    worktree: Option<&DiffPostprocessWorktree<'_>>,
10178    config: &ConfigSet,
10179) -> Result<Vec<DiffEntry>> {
10180    let mut out = Vec::with_capacity(entries.len());
10181    for e in entries {
10182        if e.status != DiffStatus::Modified {
10183            out.push(e);
10184            continue;
10185        }
10186        let old = blob_bytes(odb, &e.old_oid)?;
10187        let new = read_new_side_bytes(odb, &e, work_root, worktree, config)?;
10188        if should_break_rewrite_pair(&old, &new, break_score) {
10189            let path = e.path().to_owned();
10190            out.push(DiffEntry {
10191                status: DiffStatus::Deleted,
10192                old_path: Some(path.clone()),
10193                new_path: None,
10194                old_mode: e.old_mode,
10195                new_mode: FileMode::ABSENT,
10196                old_oid: e.old_oid,
10197                new_oid: zero_oid(),
10198                score: None,
10199            });
10200            out.push(DiffEntry {
10201                status: DiffStatus::Added,
10202                old_path: None,
10203                new_path: Some(path),
10204                old_mode: FileMode::ABSENT,
10205                new_mode: e.new_mode,
10206                old_oid: zero_oid(),
10207                new_oid: e.new_oid,
10208                score: None,
10209            });
10210        } else {
10211            out.push(e);
10212        }
10213    }
10214    Ok(out)
10215}
10216
10217fn annotate_broken_modifications(
10218    odb: &Odb,
10219    work_root: Option<&Path>,
10220    entries: Vec<DiffEntry>,
10221    break_score: u64,
10222    worktree: Option<&DiffPostprocessWorktree<'_>>,
10223    config: &ConfigSet,
10224    materialize_worktree_oid: bool,
10225) -> Result<Vec<DiffEntry>> {
10226    let mut out = Vec::with_capacity(entries.len());
10227    for mut e in entries {
10228        if e.status == DiffStatus::Modified {
10229            let old = blob_bytes(odb, &e.old_oid)?;
10230            let new = read_new_side_bytes(odb, &e, work_root, worktree, config)?;
10231            if should_break_rewrite_pair(&old, &new, break_score) {
10232                // `git diff-files -B` keeps plain `M` when the worktree side is still null.
10233                // `git diff <tree> -B` materializes the clean OID; `M###` appears only when
10234                // attributes/filters changed the bytes used for break scoring (e.g. `eol=lf`).
10235                let show_score = if e.new_oid != zero_oid() {
10236                    true
10237                } else if materialize_worktree_oid {
10238                    match (work_root, worktree) {
10239                        (Some(root), Some(wctx)) => {
10240                            worktree_side_differs_after_normalize(odb, &e, root, wctx, config)?
10241                        }
10242                        _ => false,
10243                    }
10244                } else {
10245                    false
10246                };
10247                if show_score {
10248                    e.score = rewrite_dissimilarity_index_percent(&old, &new);
10249                }
10250            }
10251        }
10252        out.push(e);
10253    }
10254    Ok(out)
10255}
10256
10257fn materialize_break_worktree_new_oids(
10258    odb: &Odb,
10259    work_root: Option<&Path>,
10260    entries: &mut [DiffEntry],
10261    worktree: Option<&DiffPostprocessWorktree<'_>>,
10262    config: &ConfigSet,
10263) -> Result<()> {
10264    for e in entries {
10265        if e.new_oid != zero_oid() {
10266            continue;
10267        }
10268        if !matches!(e.status, DiffStatus::Modified | DiffStatus::Added) {
10269            continue;
10270        }
10271        let bytes = read_new_side_bytes(odb, e, work_root, worktree, config)?;
10272        e.new_oid = odb.hash(ObjectKind::Blob, &bytes)?;
10273    }
10274    Ok(())
10275}
10276
10277/// After break-rewrite splits a modified path into delete+add, rename detection may pair the add
10278/// with another deletion (`g` → `f`). Git drops the unused delete at the rename target path.
10279fn prune_break_deletions_obviated_by_renames(entries: Vec<DiffEntry>) -> Vec<DiffEntry> {
10280    use std::collections::HashSet;
10281    let rename_targets: HashSet<String> = entries
10282        .iter()
10283        .filter(|e| e.status == DiffStatus::Renamed)
10284        .filter_map(|e| e.new_path.clone())
10285        .collect();
10286    if rename_targets.is_empty() {
10287        return entries;
10288    }
10289    entries
10290        .into_iter()
10291        .filter(|e| {
10292            if e.status != DiffStatus::Deleted {
10293                return true;
10294            }
10295            e.old_path
10296                .as_ref()
10297                .is_none_or(|p| !rename_targets.contains(p))
10298        })
10299        .collect()
10300}
10301
10302/// Apply break-rewrite, rename, and copy detection to a flat tree or index diff.
10303///
10304/// `source_tree_oid` supplies `--find-copies-harder` candidates; `work_root` is used when added
10305/// entries carry a zero OID (index↔worktree). Pass `worktree` when break-rewrite runs on a
10306/// tree↔worktree diff so worktree-side bytes honor attributes and filters.
10307///
10308/// # Errors
10309///
10310/// Propagates object read failures while evaluating break scores.
10311#[allow(clippy::too_many_arguments)]
10312pub(crate) fn postprocess_diff_entries(
10313    odb: &Odb,
10314    entries: Vec<DiffEntry>,
10315    source_tree_oid: Option<&ObjectId>,
10316    work_root: Option<&Path>,
10317    worktree: Option<&DiffPostprocessWorktree<'_>>,
10318    config: &ConfigSet,
10319    thread_policy: &ThreadPolicy,
10320    opts: &DiffPostprocessOptions,
10321    inexact_notice: Option<&mut Option<InexactRenameDetectionSkipped>>,
10322) -> Result<Vec<DiffEntry>> {
10323    let entries = break_rewrites_for_postprocess(odb, entries, work_root, worktree, config, opts)?;
10324    let source_tree_entries = source_tree_oid
10325        .map(|t| flatten_tree_entries(odb, t))
10326        .transpose()?
10327        .unwrap_or_default();
10328    pair_postprocessed_entries(
10329        odb,
10330        entries,
10331        &source_tree_entries,
10332        work_root,
10333        config,
10334        thread_policy,
10335        opts,
10336        inexact_notice,
10337    )
10338}
10339
10340fn break_rewrites_for_postprocess(
10341    odb: &Odb,
10342    entries: Vec<DiffEntry>,
10343    work_root: Option<&Path>,
10344    worktree: Option<&DiffPostprocessWorktree<'_>>,
10345    config: &ConfigSet,
10346    opts: &DiffPostprocessOptions,
10347) -> Result<Vec<DiffEntry>> {
10348    let mut entries = entries;
10349    if let Some(break_score) = opts.break_score {
10350        let rename_or_copy = opts.rename_threshold < 100 || opts.copy_threshold.is_some();
10351        entries = if rename_or_copy {
10352            split_broken_modifications(odb, work_root, entries, break_score, worktree, config)?
10353        } else {
10354            annotate_broken_modifications(
10355                odb,
10356                work_root,
10357                entries,
10358                break_score,
10359                worktree,
10360                config,
10361                opts.materialize_break_worktree_new_oid,
10362            )?
10363        };
10364        if opts.materialize_break_worktree_new_oid {
10365            materialize_break_worktree_new_oids(odb, work_root, &mut entries, worktree, config)?;
10366        }
10367    }
10368    Ok(entries)
10369}
10370
10371/// Like [`postprocess_diff_entries`] with the copy sources already listed as `(path, mode, oid)`
10372/// (an index's stage-0 entries, say) instead of read from a source tree.
10373#[allow(clippy::too_many_arguments)]
10374pub(crate) fn postprocess_diff_entries_from_sources(
10375    odb: &Odb,
10376    entries: Vec<DiffEntry>,
10377    source_entries: &[(String, FileMode, ObjectId)],
10378    work_root: Option<&Path>,
10379    worktree: Option<&DiffPostprocessWorktree<'_>>,
10380    config: &ConfigSet,
10381    thread_policy: &ThreadPolicy,
10382    opts: &DiffPostprocessOptions,
10383    inexact_notice: Option<&mut Option<InexactRenameDetectionSkipped>>,
10384) -> Result<Vec<DiffEntry>> {
10385    let entries = break_rewrites_for_postprocess(odb, entries, work_root, worktree, config, opts)?;
10386    pair_postprocessed_entries(
10387        odb,
10388        entries,
10389        source_entries,
10390        work_root,
10391        config,
10392        thread_policy,
10393        opts,
10394        inexact_notice,
10395    )
10396}
10397
10398#[allow(clippy::too_many_arguments)]
10399fn pair_postprocessed_entries(
10400    odb: &Odb,
10401    entries: Vec<DiffEntry>,
10402    source_tree_entries: &[(String, FileMode, ObjectId)],
10403    work_root: Option<&Path>,
10404    config: &ConfigSet,
10405    thread_policy: &ThreadPolicy,
10406    opts: &DiffPostprocessOptions,
10407    inexact_notice: Option<&mut Option<InexactRenameDetectionSkipped>>,
10408) -> Result<Vec<DiffEntry>> {
10409    if opts.copy_threshold.is_some() || opts.find_copies_harder {
10410        let threshold = opts.copy_threshold.unwrap_or(opts.rename_threshold.min(99));
10411        detect_copies_with_policy(
10412            odb,
10413            work_root,
10414            entries,
10415            threshold,
10416            opts.find_copies_harder,
10417            source_tree_entries,
10418            config,
10419            thread_policy,
10420        )
10421    } else if opts.rename_threshold < 100 {
10422        let skip_basename = opts.break_score.is_some();
10423        let out = detect_renames_with_policy(
10424            odb,
10425            work_root,
10426            entries,
10427            opts.rename_threshold,
10428            config,
10429            thread_policy,
10430            None,
10431            DetectRenamesOptions {
10432                skip_basename_heuristic: skip_basename,
10433            },
10434        )?;
10435        if let Some(slot) = inexact_notice {
10436            if out.inexact_skipped.is_some() {
10437                *slot = out.inexact_skipped;
10438            }
10439        }
10440        Ok(out.entries)
10441    } else {
10442        Ok(entries)
10443    }
10444}
10445
10446/// Depth-first list of `(path, mode, oid)` blob entries in a tree (for copy detection).
10447pub(crate) fn flatten_tree_entries(
10448    odb: &Odb,
10449    tree_oid: &ObjectId,
10450) -> Result<Vec<(String, FileMode, ObjectId)>> {
10451    fn walk(
10452        odb: &Odb,
10453        tree_oid: &ObjectId,
10454        prefix: &str,
10455        out: &mut Vec<(String, FileMode, ObjectId)>,
10456    ) -> Result<()> {
10457        let obj = odb.read(tree_oid)?;
10458        let oid_len = odb.hash_algo().len();
10459        for e in TreeRefIter::with_oid_len(&obj.data, oid_len) {
10460            let e = e?;
10461            let name = String::from_utf8_lossy(e.name);
10462            let path = if prefix.is_empty() {
10463                name.to_string()
10464            } else {
10465                format!("{prefix}/{name}")
10466            };
10467            if e.mode == 0o040000 {
10468                walk(odb, &e.oid, &path, out)?;
10469            } else {
10470                out.push((path, FileMode::new(e.mode), e.oid));
10471            }
10472        }
10473        Ok(())
10474    }
10475    let mut out = Vec::new();
10476    walk(odb, tree_oid, "", &mut out)?;
10477    Ok(out)
10478}
10479
10480/// Apply Git-style rename and optional copy detection for index↔worktree diffs.
10481///
10482/// When `copies` is true (Git `diff.renames` / `status.renames` set to `copy`/`copies`),
10483/// runs copy detection, which also pairs deleted sources with one rename and any additional
10484/// destinations as copies.
10485///
10486/// # Errors
10487///
10488/// Propagates errors from reading the `head_tree` object from `odb`.
10489#[allow(clippy::too_many_arguments)]
10490pub(crate) fn status_apply_rename_copy_detection(
10491    odb: &Odb,
10492    unstaged_raw: Vec<DiffEntry>,
10493    threshold: u32,
10494    copies: bool,
10495    head_tree: Option<&ObjectId>,
10496    config: &ConfigSet,
10497    thread_policy: &ThreadPolicy,
10498    cancel: Option<&crate::progress::CancelToken>,
10499) -> Result<Vec<DiffEntry>> {
10500    if !copies {
10501        return detect_renames_with_policy(
10502            odb,
10503            None,
10504            unstaged_raw,
10505            threshold,
10506            config,
10507            thread_policy,
10508            cancel,
10509            DetectRenamesOptions::default(),
10510        )
10511        .map(|r| r.entries);
10512    }
10513    let source_tree_entries: Vec<(String, FileMode, ObjectId)> = match head_tree {
10514        Some(oid) => flatten_tree(odb, oid, "")?
10515            .into_iter()
10516            .map(|e| (e.path, FileMode::new(e.mode), e.oid))
10517            .collect(),
10518        None => Vec::new(),
10519    };
10520    detect_copies_with_policy(
10521        odb,
10522        None,
10523        unstaged_raw,
10524        threshold,
10525        false,
10526        &source_tree_entries,
10527        config,
10528        thread_policy,
10529    )
10530}
10531
10532/// Check if two entries share the same filename (basename).
10533fn same_basename(del: &DiffEntry, add: &DiffEntry) -> bool {
10534    let old = del.old_path.as_deref().unwrap_or("");
10535    let new = add.new_path.as_deref().unwrap_or("");
10536    let old_base = old.rsplit('/').next().unwrap_or(old);
10537    let new_base = new.rsplit('/').next().unwrap_or(new);
10538    old_base == new_base && !old_base.is_empty()
10539}
10540
10541const RENAME_SCORE_MAX: u32 = 100;
10542
10543/// Uncompressed blob size without inflating object bodies (for rename pre-filtering).
10544fn blob_payload_size(odb: &Odb, oid: &ObjectId) -> Option<usize> {
10545    if oid.is_zero() {
10546        return None;
10547    }
10548    odb.read_info(oid).ok().map(|info| info.size as usize)
10549}
10550
10551/// Payload size for an added diff side (ODB blob or worktree bytes when not stored yet).
10552fn added_entry_payload_size(odb: &Odb, add: &DiffEntry, work_root: Option<&Path>) -> Option<usize> {
10553    if !add.new_oid.is_zero() {
10554        if let Some(size) = blob_payload_size(odb, &add.new_oid) {
10555            return Some(size);
10556        }
10557    }
10558    read_added_entry_bytes(odb, add, work_root).map(|data| data.len())
10559}
10560
10561/// Git `diffcore-delta` size gate: skip pairs that cannot reach `minimum_score`.
10562fn rename_pair_size_reachable(src_size: usize, dst_size: usize, minimum_score: u32) -> bool {
10563    let max_size = src_size.max(dst_size);
10564    let min_size = src_size.min(dst_size);
10565    if max_size == 0 {
10566        return minimum_score <= RENAME_SCORE_MAX;
10567    }
10568    (max_size - min_size).saturating_mul(RENAME_SCORE_MAX as usize)
10569        <= max_size.saturating_mul((RENAME_SCORE_MAX - minimum_score) as usize)
10570}
10571
10572/// Compute a similarity percentage (0–100) between two byte slices.
10573///
10574/// Matches Git rename detection: span-hash intersection (`hash_chars` in diffcore-delta),
10575/// `score = copied_bytes * 100 / max(src_size, dst_size)` on raw blob sizes.
10576fn compute_similarity(old: &[u8], new: &[u8]) -> u32 {
10577    RenameLineTable::similarity_score(
10578        &RenameLineTable::from_blob(old),
10579        &RenameLineTable::from_blob(new),
10580    )
10581}
10582
10583/// Compute rename/copy similarity percentage (0–100) between two byte slices.
10584///
10585/// Uses Git `diffcore-delta` span hashing (`hash_chars`): 64-byte or newline-bounded
10586/// chunks, CR skipped before LF in text blobs, `score = copied * 100 / max(len)`.
10587#[must_use]
10588pub fn rename_similarity_score(old: &[u8], new: &[u8]) -> u32 {
10589    compute_similarity(old, new)
10590}
10591
10592// ── Output formatting ───────────────────────────────────────────────
10593
10594/// Format a diff entry in Git's raw diff format.
10595///
10596/// Example: `:100644 100644 abc1234... def5678... M\tfile.txt`
10597pub fn format_raw(entry: &DiffEntry) -> String {
10598    let path = match entry.status {
10599        DiffStatus::Renamed | DiffStatus::Copied => {
10600            format!(
10601                "{}\t{}",
10602                entry.old_path.as_deref().unwrap_or(""),
10603                entry.new_path.as_deref().unwrap_or("")
10604            )
10605        }
10606        _ => entry.path().to_owned(),
10607    };
10608
10609    let status_str = match (entry.status, entry.score) {
10610        (DiffStatus::Renamed, Some(s)) => format!("R{:03}", s),
10611        (DiffStatus::Copied, Some(s)) => format!("C{:03}", s),
10612        (DiffStatus::Modified, Some(s)) => format!("M{s:03}"),
10613        (DiffStatus::TypeChanged, Some(s)) => format!("T{s:03}"),
10614        _ => entry.status.letter().to_string(),
10615    };
10616
10617    let (old_hex, new_hex) = raw_oid_hex_pair(&entry.old_oid, &entry.new_oid);
10618    format!(
10619        ":{} {} {} {} {}\t{}",
10620        entry.old_mode, entry.new_mode, old_hex, new_hex, status_str, path
10621    )
10622}
10623
10624/// Render a diff entry's `(old, new)` OIDs as full hex for `--raw` output,
10625/// widening any null OID to the repository's hash width.
10626///
10627/// Diff entries store null OIDs at SHA-1 width regardless of the repository
10628/// algorithm, but `git diff --raw` prints them at the real hash width (64
10629/// zeros in a SHA-256 repo). A diff entry never has both sides null, so the
10630/// width is taken from whichever side carries a real object.
10631fn raw_oid_hex_pair(old: &ObjectId, new: &ObjectId) -> (String, String) {
10632    let width = if !old.is_zero() {
10633        old.algo().hex_len()
10634    } else if !new.is_zero() {
10635        new.algo().hex_len()
10636    } else {
10637        old.algo().hex_len()
10638    };
10639    let render = |oid: &ObjectId| {
10640        if oid.is_zero() {
10641            "0".repeat(width)
10642        } else {
10643            oid.to_hex()
10644        }
10645    };
10646    (render(old), render(new))
10647}
10648
10649/// Format a diff entry with abbreviated OIDs.
10650pub fn format_raw_abbrev(entry: &DiffEntry, abbrev_len: usize) -> String {
10651    let old_hex = format!("{}", entry.old_oid);
10652    let new_hex = format!("{}", entry.new_oid);
10653    let old_abbrev = &old_hex[..abbrev_len.min(old_hex.len())];
10654    let new_abbrev = &new_hex[..abbrev_len.min(new_hex.len())];
10655
10656    // Renames/copies carry a similarity score and a `<old>\t<new>` path pair.
10657    let path = match entry.status {
10658        DiffStatus::Renamed | DiffStatus::Copied => format!(
10659            "{}\t{}",
10660            entry.old_path.as_deref().unwrap_or(""),
10661            entry.new_path.as_deref().unwrap_or("")
10662        ),
10663        _ => entry.path().to_owned(),
10664    };
10665    let status_str = match (entry.status, entry.score) {
10666        (DiffStatus::Renamed, Some(s)) => format!("R{s:03}"),
10667        (DiffStatus::Copied, Some(s)) => format!("C{s:03}"),
10668        (DiffStatus::Modified, Some(s)) => format!("M{s:03}"),
10669        (DiffStatus::TypeChanged, Some(s)) => format!("T{s:03}"),
10670        _ => entry.status.letter().to_string(),
10671    };
10672
10673    format!(
10674        ":{} {} {} {} {}\t{}",
10675        entry.old_mode, entry.new_mode, old_abbrev, new_abbrev, status_str, path
10676    )
10677}
10678
10679pub(crate) fn is_func_line(line: &str, matcher: Option<&FuncnameMatcher>) -> bool {
10680    if let Some(m) = matcher {
10681        return m.match_line(line).is_some();
10682    }
10683    let t = line.trim_end_matches(['\n', '\r']);
10684    if t.is_empty() {
10685        return false;
10686    }
10687    let b = t.as_bytes()[0];
10688    if !(b.is_ascii_alphabetic() || b == b'_' || b == b'$') {
10689        return false;
10690    }
10691    true
10692}
10693
10694/// Extract function context for a hunk header.
10695///
10696/// Given a hunk header like `@@ -8,7 +8,7 @@`, find the last line
10697/// before line 8 in the old content that looks like a function header
10698/// (starts with a non-whitespace character, like Git's default).
10699fn func_context_line_matches(line: &str, funcname_matcher: Option<&FuncnameMatcher>) -> bool {
10700    if let Some(m) = funcname_matcher {
10701        return m.match_line(line).is_some();
10702    }
10703    is_func_line(line, None)
10704}
10705
10706pub(crate) fn extract_function_context(
10707    header: &str,
10708    old_lines: &[&str],
10709    funcname_matcher: Option<&FuncnameMatcher>,
10710    _source_path: Option<&str>,
10711) -> Option<String> {
10712    // Parse the old start line number from "@@ -<start>,<count> ..."
10713    let at_pos = header.find("-")?;
10714    let rest = &header[at_pos + 1..];
10715    let comma_or_space = rest.find([',', ' '])?;
10716    let start_str = &rest[..comma_or_space];
10717    let start_line: usize = start_str.parse().ok()?;
10718
10719    // Parse the old line count; "@@ -<start>,<count> ..." (no comma means count 1).
10720    // Only look for the comma inside the old-range token itself — searching the
10721    // whole remainder would pick up the comma from the new side (e.g. "+0,0").
10722    let old_token_end = rest.find([' ', '\t']).unwrap_or(rest.len());
10723    let old_token = &rest[..old_token_end];
10724    let old_count: usize = if let Some(comma) = old_token.find(',') {
10725        old_token[comma + 1..].parse().unwrap_or(1)
10726    } else {
10727        1
10728    };
10729
10730    if start_line == 0 {
10731        return None;
10732    }
10733
10734    // Look backwards for a line that matches the funcname pattern. start_line is
10735    // 1-indexed. For a normal hunk the first changed pre-image line is
10736    // old_lines[start_line-1], so we search lines strictly before it
10737    // (old_lines[0..start_line-1]). For a pure insertion (old count 0) the
10738    // content is inserted *after* old line start_line, so Git's function search
10739    // begins at that line itself: search old_lines[0..start_line].
10740    if start_line == 0 {
10741        return None;
10742    }
10743    let search_end = if old_count == 0 {
10744        start_line.min(old_lines.len())
10745    } else {
10746        if start_line <= 1 {
10747            return None;
10748        }
10749        (start_line - 1).min(old_lines.len())
10750    };
10751    let truncate = |line: &str| {
10752        let mut end = line.len().min(80);
10753        while end > 0 && !line.is_char_boundary(end) {
10754            end -= 1;
10755        }
10756        let mut out = line[..end].to_owned();
10757        let tail = line[end..].trim_start();
10758        // Git drops trailing whitespace on truncated funcname lines when the
10759        // remainder of the source line continues with normal text (t2021-style
10760        // shell tests), but keeps it when the next token is a string delimiter
10761        // (gettext `.po` msgstr lines).
10762        if tail.is_empty()
10763            || (!tail.starts_with('"')
10764                && !tail.starts_with('\'')
10765                && !line.starts_with("msgstr")
10766                && !line.starts_with("msgid"))
10767        {
10768            out = out.trim_end().to_owned();
10769        }
10770        out
10771    };
10772
10773    for i in (0..search_end).rev() {
10774        let line = old_lines[i];
10775        if line.is_empty() {
10776            continue;
10777        }
10778        if func_context_line_matches(line, funcname_matcher) {
10779            if let Some(matcher) = funcname_matcher {
10780                if let Some(matched) = matcher.match_line(line) {
10781                    return Some(truncate(&matched));
10782                }
10783                continue;
10784            }
10785            return Some(truncate(line));
10786        }
10787    }
10788    None
10789}
10790
10791/// Normalise one line like Git's `-b` / `--ignore-space-change`.
10792#[must_use]
10793pub fn normalize_ignore_space_change_line(line: &str) -> String {
10794    let mut result = String::with_capacity(line.len());
10795    let mut in_space = false;
10796    for c in line.chars() {
10797        if c.is_whitespace() {
10798            if !in_space {
10799                result.push(' ');
10800                in_space = true;
10801            }
10802        } else {
10803            result.push(c);
10804            in_space = false;
10805        }
10806    }
10807    while result.ends_with(' ') {
10808        result.pop();
10809    }
10810    result
10811}
10812
10813/// Normalise text like Git's `-b` / `--ignore-space-change`: on each line, collapse runs of
10814/// whitespace to a single ASCII space and trim trailing spaces.
10815///
10816/// Line breaks are preserved by splitting on [`str::lines`] and rejoining with `\n` (same approach
10817/// as the porcelain `diff` whitespace handling in `grit`).
10818#[must_use]
10819pub fn normalize_ignore_space_change(content: &str) -> String {
10820    content
10821        .lines()
10822        .map(normalize_ignore_space_change_line)
10823        .collect::<Vec<_>>()
10824        .join("\n")
10825}
10826
10827/// Line count for diffstat/`--numstat`, matching Git's `count_lines()` in `diff.c`.
10828///
10829/// Counts newline-terminated lines; a final line without trailing newline still counts as one line.
10830/// An empty buffer yields `0`.
10831#[must_use]
10832pub fn count_git_lines(data: &[u8]) -> usize {
10833    if data.is_empty() {
10834        return 0;
10835    }
10836    let mut count = 0usize;
10837    let mut nl_just_seen = false;
10838    for &ch in data {
10839        if ch == b'\n' {
10840            count += 1;
10841            nl_just_seen = true;
10842        } else {
10843            nl_just_seen = false;
10844        }
10845    }
10846    if !nl_just_seen {
10847        count += 1;
10848    }
10849    count
10850}
10851
10852/// Internal maximum diff score used by Git rename/break heuristics (`MAX_SCORE` in `diffcore.h`).
10853pub(crate) const GIT_DIFF_MAX_SCORE: u64 = 60_000;
10854const DIFF_MAX_SCORE: u64 = GIT_DIFF_MAX_SCORE;
10855const DIFF_MINIMUM_BREAK_SIZE: usize = 400;
10856#[allow(dead_code)]
10857const DIFF_DEFAULT_BREAK_SCORE: u64 = 30_000;
10858/// Default break threshold (`DEFAULT_BREAK_SCORE` in `diffcore.h`), internal 0–the internal max score (60_000) scale.
10859pub(crate) const GIT_DIFF_DEFAULT_BREAK_SCORE: u64 = DIFF_DEFAULT_BREAK_SCORE;
10860const DIFF_HASHBASE: u32 = 107_927;
10861
10862#[derive(Clone, Copy, Default)]
10863struct SpanSlot {
10864    hashval: u32,
10865    cnt: u32,
10866}
10867
10868struct SpanHashTop {
10869    alloc_log2: u8,
10870    free_slots: i32,
10871    data: Vec<SpanSlot>,
10872}
10873
10874impl SpanHashTop {
10875    fn new(initial_log2: u8) -> Self {
10876        let cap = 1usize << initial_log2;
10877        Self {
10878            alloc_log2: initial_log2,
10879            free_slots: initial_free(initial_log2),
10880            data: vec![SpanSlot::default(); cap],
10881        }
10882    }
10883
10884    fn len(&self) -> usize {
10885        1usize << self.alloc_log2
10886    }
10887
10888    fn add_span(&mut self, hashval: u32, cnt: u32) {
10889        loop {
10890            let lim = self.len();
10891            let mut bucket = (hashval as usize) & (lim - 1);
10892            loop {
10893                let h = &mut self.data[bucket];
10894                if h.cnt == 0 {
10895                    h.hashval = hashval;
10896                    h.cnt = cnt;
10897                    self.free_slots -= 1;
10898                    if self.free_slots < 0 {
10899                        self.rehash();
10900                        break;
10901                    }
10902                    return;
10903                }
10904                if h.hashval == hashval {
10905                    h.cnt = h.cnt.saturating_add(cnt);
10906                    return;
10907                }
10908                bucket += 1;
10909                if bucket >= lim {
10910                    bucket = 0;
10911                }
10912            }
10913        }
10914    }
10915
10916    fn rehash(&mut self) {
10917        let old = std::mem::take(&mut self.data);
10918        let old_log = self.alloc_log2;
10919        self.alloc_log2 = old_log.saturating_add(1);
10920        let new_len = 1usize << self.alloc_log2;
10921        self.free_slots = initial_free(self.alloc_log2);
10922        self.data = vec![SpanSlot::default(); new_len];
10923        let old_sz = 1usize << old_log;
10924        for o in old.iter().take(old_sz) {
10925            let o = *o;
10926            if o.cnt == 0 {
10927                continue;
10928            }
10929            self.add_span_after_rehash(o.hashval, o.cnt);
10930        }
10931    }
10932
10933    fn add_span_after_rehash(&mut self, hashval: u32, cnt: u32) {
10934        loop {
10935            let lim = self.len();
10936            let mut bucket = (hashval as usize) & (lim - 1);
10937            loop {
10938                let h = &mut self.data[bucket];
10939                if h.cnt == 0 {
10940                    h.hashval = hashval;
10941                    h.cnt = cnt;
10942                    self.free_slots -= 1;
10943                    if self.free_slots < 0 {
10944                        self.rehash();
10945                        break;
10946                    }
10947                    return;
10948                }
10949                if h.hashval == hashval {
10950                    h.cnt = h.cnt.saturating_add(cnt);
10951                    return;
10952                }
10953                bucket += 1;
10954                if bucket >= lim {
10955                    bucket = 0;
10956                }
10957            }
10958        }
10959    }
10960
10961    fn sort_by_hashval(&mut self) {
10962        let sz = self.len();
10963        self.data[..sz].sort_by(|a, b| {
10964            if a.cnt == 0 {
10965                return std::cmp::Ordering::Greater;
10966            }
10967            if b.cnt == 0 {
10968                return std::cmp::Ordering::Less;
10969            }
10970            a.hashval.cmp(&b.hashval)
10971        });
10972    }
10973}
10974
10975fn initial_free(sz_log2: u8) -> i32 {
10976    let sz = sz_log2 as i32;
10977    ((1i32 << sz_log2) * (sz - 3) / sz).max(0)
10978}
10979
10980fn hash_blob_spans(buf: &[u8], is_text: bool) -> SpanHashTop {
10981    let mut hash = SpanHashTop::new(9);
10982    let mut n = 0u32;
10983    let mut accum1: u32 = 0;
10984    let mut accum2: u32 = 0;
10985    let mut i = 0usize;
10986    while i < buf.len() {
10987        let c = buf[i] as u32;
10988        let old_1 = accum1;
10989        i += 1;
10990
10991        if is_text && c == b'\r' as u32 && i < buf.len() && buf[i] == b'\n' {
10992            continue;
10993        }
10994
10995        accum1 = accum1.wrapping_shl(7) ^ accum2.wrapping_shr(25);
10996        accum2 = accum2.wrapping_shl(7) ^ old_1.wrapping_shr(25);
10997        accum1 = accum1.wrapping_add(c);
10998        n += 1;
10999        if n < 64 && c != b'\n' as u32 {
11000            continue;
11001        }
11002        let hashval = (accum1.wrapping_add(accum2.wrapping_mul(0x61))) % DIFF_HASHBASE;
11003        hash.add_span(hashval, n);
11004        n = 0;
11005        accum1 = 0;
11006        accum2 = 0;
11007    }
11008    if n > 0 {
11009        let hashval = (accum1.wrapping_add(accum2.wrapping_mul(0x61))) % DIFF_HASHBASE;
11010        hash.add_span(hashval, n);
11011    }
11012    hash.sort_by_hashval();
11013    hash
11014}
11015
11016/// Approximate copied vs added material between two blobs (Git `diffcore_count_changes`).
11017///
11018/// Returns `(copied_bytes_from_src, literal_added_bytes_in_dst)` matching Git's
11019/// `diffcore_count_changes` semantics (used for `--dirstat=changes` damage).
11020#[must_use]
11021pub(crate) fn diffcore_count_changes(old: &[u8], new: &[u8]) -> (u64, u64) {
11022    crate::trace::trace_span!("grit.diff.blob");
11023    let src_is_text = !crate::merge_file::is_binary(old);
11024    let dst_is_text = !crate::merge_file::is_binary(new);
11025    let src_count = hash_blob_spans(old, src_is_text);
11026    let dst_count = hash_blob_spans(new, dst_is_text);
11027    let mut sc: u64 = 0;
11028    let mut la: u64 = 0;
11029    let mut si = 0usize;
11030    let mut di = 0usize;
11031    let src_len = src_count.len();
11032    let dst_len = dst_count.len();
11033    loop {
11034        if si >= src_len || src_count.data[si].cnt == 0 {
11035            break;
11036        }
11037        let s_hash = src_count.data[si].hashval;
11038        let s_cnt = u64::from(src_count.data[si].cnt);
11039        while di < dst_len && dst_count.data[di].cnt != 0 && dst_count.data[di].hashval < s_hash {
11040            la += u64::from(dst_count.data[di].cnt);
11041            di += 1;
11042        }
11043        let mut dst_cnt = 0u64;
11044        if di < dst_len && dst_count.data[di].cnt != 0 && dst_count.data[di].hashval == s_hash {
11045            dst_cnt = u64::from(dst_count.data[di].cnt);
11046            di += 1;
11047        }
11048        if s_cnt < dst_cnt {
11049            la += dst_cnt - s_cnt;
11050            sc += s_cnt;
11051        } else {
11052            sc += dst_cnt;
11053        }
11054        si += 1;
11055    }
11056    while di < dst_len && dst_count.data[di].cnt != 0 {
11057        la += u64::from(dst_count.data[di].cnt);
11058        di += 1;
11059    }
11060    (sc, la)
11061}
11062
11063/// Whether an in-place blob edit should be split into delete+create for rename/copy (`should_break`
11064/// in `diffcore-break.c`). `break_score` is on the internal 0–the internal max score (60_000) scale (default
11065/// `DIFF_DEFAULT_BREAK_SCORE`).
11066#[must_use]
11067pub(crate) fn should_break_rewrite_pair(old: &[u8], new: &[u8], break_score: u64) -> bool {
11068    should_break_rewrite_inner(old, new, break_score)
11069}
11070
11071/// Drop entries that do not match `filter` (Git `--diff-filter`, after rename detection).
11072#[must_use]
11073pub(crate) fn apply_diff_filter(
11074    entries: Vec<DiffEntry>,
11075    filter: Option<super::filter::DiffFilter>,
11076) -> Vec<DiffEntry> {
11077    match filter {
11078        Some(f) => f.filter_entries(entries),
11079        None => entries,
11080    }
11081}
11082
11083/// Parse `-M` / `--find-renames` threshold into Git's 0–100 similarity percent.
11084#[must_use]
11085pub fn parse_diff_rename_threshold_percent(arg: &str) -> Option<u8> {
11086    let token = if arg.is_empty() { "50" } else { arg };
11087    let internal = parse_diff_rename_score_token(token)?;
11088    Some((internal.saturating_mul(100) / GIT_DIFF_MAX_SCORE).min(100) as u8)
11089}
11090
11091/// Parse a single Git `parse_rename_score` token (`50`, `50%`, decimal forms) into internal
11092/// 0–the internal max score (60_000) units.
11093pub(crate) fn parse_diff_rename_score_token(arg: &str) -> Option<u64> {
11094    let mut num: u64 = 0;
11095    let mut scale: u64 = 1;
11096    let mut dot = false;
11097    let mut saw_digit = false;
11098    for ch in arg.chars() {
11099        if !dot && ch == '.' {
11100            scale = 1;
11101            dot = true;
11102            continue;
11103        }
11104        if ch == '%' {
11105            scale = if dot { scale.saturating_mul(100) } else { 100 };
11106            break;
11107        }
11108        if ch.is_ascii_digit() {
11109            saw_digit = true;
11110            if scale < 100_000 {
11111                scale = scale.saturating_mul(10);
11112                num = num.saturating_mul(10) + u64::from(ch as u8 - b'0');
11113            }
11114        } else {
11115            break;
11116        }
11117    }
11118    if !saw_digit {
11119        return None;
11120    }
11121    Some(if num >= scale {
11122        GIT_DIFF_MAX_SCORE
11123    } else {
11124        GIT_DIFF_MAX_SCORE * num / scale
11125    })
11126}
11127
11128/// Git `merge_score` from `diffcore-break.c` when a pair is considered broken: how much of the
11129/// source blob was removed (0–`DIFF_MAX_SCORE` scale). Used for `dissimilarity index` metadata.
11130#[must_use]
11131pub(crate) fn rewrite_merge_score(old: &[u8], new: &[u8]) -> Option<u64> {
11132    if old.is_empty() {
11133        return None;
11134    }
11135    let max_size = old.len().max(new.len());
11136    if max_size < DIFF_MINIMUM_BREAK_SIZE {
11137        return None;
11138    }
11139    let (src_copied, _) = diffcore_count_changes(old, new);
11140    let src_copied = src_copied.min(old.len() as u64);
11141    let src_removed = (old.len() as u64).saturating_sub(src_copied);
11142    Some(src_removed * DIFF_MAX_SCORE / old.len() as u64)
11143}
11144
11145/// Percentage shown in `dissimilarity index N%` for a rewrite (`similarity_index` in Git's diff.c).
11146#[must_use]
11147pub(crate) fn rewrite_dissimilarity_index_percent(old: &[u8], new: &[u8]) -> Option<u32> {
11148    let score = rewrite_merge_score(old, new)?;
11149    Some((score * 100 / DIFF_MAX_SCORE).min(100) as u32)
11150}
11151
11152fn should_break_rewrite_inner(src: &[u8], dst: &[u8], break_score: u64) -> bool {
11153    if src.is_empty() {
11154        return false;
11155    }
11156    let max_size = src.len().max(dst.len());
11157    if max_size < DIFF_MINIMUM_BREAK_SIZE {
11158        return false;
11159    }
11160    let (src_copied, literal_added) = diffcore_count_changes(src, dst);
11161    let src_copied = src_copied.min(src.len() as u64);
11162    let mut literal_added = literal_added;
11163    let dst_len = dst.len() as u64;
11164    if src_copied < dst_len && literal_added + src_copied > dst_len {
11165        literal_added = dst_len.saturating_sub(src_copied);
11166    }
11167    let src_removed = (src.len() as u64).saturating_sub(src_copied);
11168    let merge_score = src_removed * DIFF_MAX_SCORE / src.len() as u64;
11169    if merge_score > break_score {
11170        return true;
11171    }
11172    let delta_size = src_removed.saturating_add(literal_added);
11173    if delta_size * DIFF_MAX_SCORE / (max_size as u64) < break_score {
11174        return false;
11175    }
11176    let s = src.len() as u64;
11177    if (s * break_score < src_removed * DIFF_MAX_SCORE)
11178        && (literal_added * 20 < src_removed)
11179        && (literal_added * 20 < src_copied)
11180    {
11181        return false;
11182    }
11183    true
11184}
11185
11186// ── Helpers ─────────────────────────────────────────────────────────
11187
11188/// Flatten a tree object recursively into a sorted list of (path, mode, oid).
11189struct FlatEntry {
11190    path: String,
11191    mode: u32,
11192    oid: ObjectId,
11193}
11194
11195fn flatten_tree(odb: &Odb, tree_oid: &ObjectId, prefix: &str) -> Result<Vec<FlatEntry>> {
11196    flatten_tree_with_index(odb, tree_oid, prefix, None)
11197}
11198
11199fn sparse_index_skips_tree_descent(index: &Index, dir_path: &str, tree_oid: &ObjectId) -> bool {
11200    let mut storage = crate::repo_relpath::storage_bytes(dir_path).into_owned();
11201    if !storage.ends_with(b"/") {
11202        storage.push(b'/');
11203    }
11204    index.find_entry_pos(&storage, 0).is_some_and(|pos| {
11205        let entry = &index.entries[pos];
11206        entry.is_sparse_directory_placeholder() && entry.oid == *tree_oid
11207    })
11208}
11209
11210/// Tree OID at `path` (slash-separated, no trailing slash) under `root`, if every component is a tree.
11211fn subtree_oid_at_path(odb: &Odb, root: &ObjectId, path: &str) -> Result<Option<ObjectId>> {
11212    let mut oid = *root;
11213    for component in path.split('/').filter(|c| !c.is_empty()) {
11214        let entries = read_tree(odb, &oid)?;
11215        let Some(entry) = entries
11216            .iter()
11217            .find(|e| e.name.as_slice() == component.as_bytes())
11218        else {
11219            return Ok(None);
11220        };
11221        if !is_tree_mode(entry.mode) {
11222            return Ok(None);
11223        }
11224        oid = entry.oid;
11225    }
11226    Ok(Some(oid))
11227}
11228
11229/// Report every blob in an index subtree as staged additions (HEAD has no tree at this path).
11230fn emit_flattened_subtree_as_added(
11231    odb: &Odb,
11232    subtree: &ObjectId,
11233    prefix: &str,
11234    ignore_submodules: bool,
11235    result: &mut Vec<DiffEntry>,
11236) -> Result<()> {
11237    for entry in flatten_tree(odb, subtree, prefix)? {
11238        if ignore_submodules && entry.mode == 0o160000 {
11239            continue;
11240        }
11241        result.push(DiffEntry {
11242            status: DiffStatus::Added,
11243            old_path: None,
11244            new_path: Some(entry.path),
11245            old_mode: FileMode::ABSENT,
11246            new_mode: FileMode::new(entry.mode),
11247            old_oid: zero_oid(),
11248            new_oid: entry.oid,
11249            score: None,
11250        });
11251    }
11252    Ok(())
11253}
11254
11255/// Compare two flattened subtrees and append staged diff entries.
11256fn diff_flattened_subtrees(
11257    head: &[FlatEntry],
11258    index: &[FlatEntry],
11259    ignore_submodules: bool,
11260    result: &mut Vec<DiffEntry>,
11261    stage0_paths: &mut std::collections::BTreeSet<String>,
11262    tree_map: &mut std::collections::BTreeMap<&str, &FlatEntry>,
11263) {
11264    let mut head_map: std::collections::BTreeMap<&str, &FlatEntry> = head
11265        .iter()
11266        .map(|entry| (entry.path.as_str(), entry))
11267        .collect();
11268    for entry in index {
11269        stage0_paths.insert(entry.path.clone());
11270        tree_map.remove(entry.path.as_str());
11271        match head_map.remove(entry.path.as_str()) {
11272            Some(te) => {
11273                if te.oid != entry.oid || te.mode != entry.mode {
11274                    result.push(DiffEntry {
11275                        status: DiffStatus::for_content_change(te.mode, entry.mode),
11276                        old_path: Some(entry.path.clone()),
11277                        new_path: Some(entry.path.clone()),
11278                        old_mode: FileMode::new(te.mode),
11279                        new_mode: FileMode::new(entry.mode),
11280                        old_oid: te.oid,
11281                        new_oid: entry.oid,
11282                        score: None,
11283                    });
11284                }
11285            }
11286            None => {
11287                result.push(DiffEntry {
11288                    status: DiffStatus::Added,
11289                    old_path: None,
11290                    new_path: Some(entry.path.clone()),
11291                    old_mode: FileMode::ABSENT,
11292                    new_mode: FileMode::new(entry.mode),
11293                    old_oid: zero_oid(),
11294                    new_oid: entry.oid,
11295                    score: None,
11296                });
11297            }
11298        }
11299    }
11300    for (path, te) in head_map {
11301        if ignore_submodules && te.mode == 0o160000 {
11302            continue;
11303        }
11304        tree_map.remove(path);
11305        result.push(DiffEntry {
11306            status: DiffStatus::Deleted,
11307            old_path: Some(path.to_owned()),
11308            new_path: None,
11309            old_mode: FileMode::new(te.mode),
11310            new_mode: FileMode::ABSENT,
11311            old_oid: te.oid,
11312            new_oid: zero_oid(),
11313            score: None,
11314        });
11315    }
11316}
11317
11318fn flatten_tree_with_index(
11319    odb: &Odb,
11320    tree_oid: &ObjectId,
11321    prefix: &str,
11322    index: Option<&Index>,
11323) -> Result<Vec<FlatEntry>> {
11324    let entries = read_tree(odb, tree_oid)?;
11325    let mut result = Vec::new();
11326
11327    for entry in entries {
11328        let path = crate::repo_relpath::format_path_stored(prefix, &entry.name);
11329        if is_tree_mode(entry.mode) {
11330            if index.is_some_and(|idx| sparse_index_skips_tree_descent(idx, &path, &entry.oid)) {
11331                continue;
11332            }
11333            let nested = flatten_tree_with_index(odb, &entry.oid, &path, index)?;
11334            result.extend(nested);
11335        } else {
11336            result.push(FlatEntry {
11337                path,
11338                mode: entry.mode,
11339                oid: entry.oid,
11340            });
11341        }
11342    }
11343
11344    Ok(result)
11345}
11346
11347/// Paths present in `HEAD`'s tree with mode and blob/commit OID (for status porcelain v2).
11348pub(crate) fn head_path_states(
11349    odb: &Odb,
11350    head_tree: Option<&ObjectId>,
11351) -> Result<std::collections::BTreeMap<String, (u32, ObjectId)>> {
11352    let mut m = std::collections::BTreeMap::new();
11353    let Some(t) = head_tree else {
11354        return Ok(m);
11355    };
11356    for fe in flatten_tree(odb, t, "")? {
11357        m.insert(fe.path, (fe.mode, fe.oid));
11358    }
11359    Ok(m)
11360}
11361
11362/// Whether a mode represents a tree (directory).
11363fn is_tree_mode(mode: u32) -> bool {
11364    crate::index::is_tree_mode(mode)
11365}
11366
11367/// Git orders diff lists by raw path bytes, which differs from the stored form of
11368/// non-UTF-8 paths (see [`crate::repo_relpath::storage_bytes`]).
11369fn path_order(a: &DiffEntry, b: &DiffEntry) -> std::cmp::Ordering {
11370    use crate::repo_relpath::storage_bytes;
11371    storage_bytes(a.path()).cmp(&storage_bytes(b.path()))
11372}
11373
11374fn entries_sorted_by_path(entries: &[DiffEntry]) -> bool {
11375    entries.is_sorted_by(|a, b| path_order(a, b).is_le())
11376}
11377
11378fn entries_strictly_sorted_by_path(entries: &[DiffEntry]) -> bool {
11379    entries.is_sorted_by(|a, b| path_order(a, b).is_lt())
11380}
11381
11382/// Stable-sort diff entries by [`DiffEntry::path`] in raw byte order, Git's output order
11383/// for diff lists (non-UTF-8 paths compare by their decoded bytes).
11384///
11385/// Tree diffs and rename detection already return entries in this order, so the common
11386/// case is a single comparison pass with no allocation (a stable sort would otherwise
11387/// allocate scratch space proportional to the list).
11388///
11389/// # Parameters
11390///
11391/// - `entries`: the list to order in place; entries with equal paths keep their order.
11392pub fn sort_entries_by_path(entries: &mut [DiffEntry]) {
11393    if entries_sorted_by_path(entries) {
11394        return;
11395    }
11396    entries.sort_by(path_order);
11397}
11398
11399/// Format a numeric mode as a zero-padded octal string.
11400pub(crate) fn format_mode(mode: u32) -> String {
11401    format!("{mode:06o}")
11402}
11403
11404/// Read the HEAD commit OID from a submodule checkout directory.
11405///
11406/// Returns `None` if the path is missing, not a submodule checkout, or has no resolvable HEAD.
11407#[must_use]
11408#[allow(dead_code)]
11409fn read_submodule_head_for_checkout(sub_dir: &Path) -> Option<ObjectId> {
11410    read_submodule_head(sub_dir)
11411}
11412
11413/// First line of a commit's message for `git diff --submodule=log` output.
11414///
11415/// Honors `encoding` in the commit object (Latin-1 vs UTF-8) using the same
11416/// rules as Git's submodule summary.
11417#[must_use]
11418#[allow(dead_code)]
11419fn submodule_commit_subject_line(c: &CommitData) -> String {
11420    let enc = c.encoding.as_deref().unwrap_or("UTF-8");
11421    let is_latin1 = enc.eq_ignore_ascii_case("ISO8859-1")
11422        || enc.eq_ignore_ascii_case("ISO-8859-1")
11423        || enc.eq_ignore_ascii_case("LATIN1")
11424        || enc.eq_ignore_ascii_case("ISO-8859-15");
11425    if let Some(raw) = c.raw_message.as_deref() {
11426        let line = raw.split(|b| *b == b'\n').next().unwrap_or(raw);
11427        if is_latin1 {
11428            return line
11429                .iter()
11430                .map(|&b| b as char)
11431                .collect::<String>()
11432                .trim()
11433                .to_owned();
11434        }
11435        return String::from_utf8_lossy(line).trim().to_string();
11436    }
11437    c.message.lines().next().unwrap_or("").trim().to_owned()
11438}
11439
11440/// True when a gitlink directory has no initialized submodule (no embedded `.git`), so the
11441/// recorded index OID is treated as the worktree reference (Git `ce_compare_gitlink` when
11442/// `resolve_gitlink_ref` fails).
11443fn uninitialized_gitlink_worktree_matches_index(sub_dir: &Path) -> bool {
11444    submodule_embedded_git_dir(sub_dir).is_none()
11445}
11446
11447fn read_submodule_head(sub_dir: &Path) -> Option<ObjectId> {
11448    read_submodule_head_oid(sub_dir)
11449}
11450
11451/// Resolve the embedded git directory for a submodule work tree (`sub_dir/.git`).
11452#[must_use]
11453pub(crate) fn submodule_embedded_git_dir(sub_dir: &Path) -> Option<PathBuf> {
11454    let sub_meta = fs::symlink_metadata(sub_dir).ok()?;
11455    if !sub_meta.is_dir() || sub_meta.file_type().is_symlink() {
11456        return None;
11457    }
11458    let gitfile = sub_dir.join(".git");
11459    let meta = fs::metadata(&gitfile).ok()?;
11460    if meta.is_file() {
11461        let content = fs::read_to_string(&gitfile).ok()?;
11462        let gitdir = content
11463            .lines()
11464            .find_map(|l| l.strip_prefix("gitdir: "))?
11465            .trim();
11466        Some(if Path::new(gitdir).is_absolute() {
11467            PathBuf::from(gitdir)
11468        } else {
11469            sub_dir.join(gitdir)
11470        })
11471    } else if meta.is_dir() {
11472        Some(gitfile)
11473    } else {
11474        None
11475    }
11476}
11477
11478/// Whether `abs` is a nested Git checkout (embedded `.git`) not already indexed as a gitlink.
11479///
11480/// Matches Git's `is_nonbare_repository_dir` / `is_git_directory`: the embedded git dir must look
11481/// like a real repository (valid `HEAD` plus `objects/`, or `objects/` and `refs/`), not a bare
11482/// `.git` directory placeholder.
11483#[must_use]
11484pub(crate) fn is_unregistered_embedded_git_repo(index: &Index, rel: &str, abs: &Path) -> bool {
11485    if rel.is_empty() || index.has_gitlink_at(rel.as_bytes()) {
11486        return false;
11487    }
11488    let Some(git_dir) = submodule_embedded_git_dir(abs) else {
11489        return false;
11490    };
11491    crate::submodule_gitdir::is_valid_git_dir(&git_dir)
11492}
11493
11494/// Whether staging should record a gitlink, refuse, or treat the path as a normal directory.
11495pub(crate) enum EmbeddedGitRepoStage {
11496    /// Path is not an unregistered embedded repository.
11497    NotEmbedded,
11498    /// HEAD resolves to a commit that can be stored as mode `160000`.
11499    Gitlink(crate::objects::ObjectId),
11500}
11501
11502/// Classify a worktree path for embedded-repository staging.
11503///
11504/// Unlike [`is_unregistered_embedded_git_repo`], registered gitlinks (including unmerged
11505/// conflict stages) are still checked so an unborn nested HEAD cannot collapse conflicts.
11506///
11507/// Returns [`EmbeddedGitRepoStage::NotEmbedded`] when the path is not a nested checkout.
11508/// Returns [`EmbeddedGitRepoStage::Gitlink`] when HEAD resolves. Otherwise returns
11509/// [`crate::worktree::WorktreeError::EmbeddedRepoWithoutCommit`].
11510pub(crate) fn classify_embedded_git_repo_for_stage(
11511    _index: &Index,
11512    rel: &str,
11513    abs: &Path,
11514) -> crate::error::Result<EmbeddedGitRepoStage> {
11515    if rel.is_empty() {
11516        return Ok(EmbeddedGitRepoStage::NotEmbedded);
11517    }
11518    let Some(git_dir) = submodule_embedded_git_dir(abs) else {
11519        return Ok(EmbeddedGitRepoStage::NotEmbedded);
11520    };
11521    if !crate::submodule_gitdir::is_valid_git_dir(&git_dir) {
11522        return Ok(EmbeddedGitRepoStage::NotEmbedded);
11523    }
11524    let path = rel.trim_end_matches('/').to_owned();
11525    match read_submodule_head_oid_for_stage(abs) {
11526        Some(oid) => Ok(EmbeddedGitRepoStage::Gitlink(oid)),
11527        None => Err(crate::error::Error::Worktree(
11528            WorktreeError::EmbeddedRepoWithoutCommit { path },
11529        )),
11530    }
11531}
11532
11533/// Walk upward from `sub_dir` to find the nearest containing Git work tree.
11534fn find_superproject_git(sub_dir: &Path) -> Option<(PathBuf, PathBuf)> {
11535    let mut cur = sub_dir.parent()?;
11536    loop {
11537        let git_path = cur.join(".git");
11538        if git_path.exists() {
11539            let gd = if git_path.is_file() {
11540                let content = fs::read_to_string(&git_path).ok()?;
11541                let line = content
11542                    .lines()
11543                    .find_map(|l| l.strip_prefix("gitdir: "))?
11544                    .trim();
11545                if Path::new(line).is_absolute() {
11546                    PathBuf::from(line)
11547                } else {
11548                    cur.join(line)
11549                }
11550            } else {
11551                git_path
11552            };
11553            return Some((cur.to_path_buf(), gd));
11554        }
11555        cur = cur.parent()?;
11556    }
11557}
11558
11559fn submodule_git_dir_for_head_read(sub_dir: &Path) -> Option<PathBuf> {
11560    let mut git_dir = submodule_embedded_git_dir(sub_dir)?;
11561    if let Some((super_wt, super_git_dir)) = find_superproject_git(sub_dir) {
11562        let rel = sub_dir.strip_prefix(&super_wt).ok()?;
11563        let rel_str = rel.to_string_lossy().replace('\\', "/");
11564        let local_mod = super_git_dir
11565            .join("modules")
11566            .join(rel_str.trim_start_matches('/'));
11567        if local_mod.join("HEAD").exists() {
11568            let sg = super_git_dir
11569                .canonicalize_simplified()
11570                .unwrap_or(super_git_dir);
11571            let cur = git_dir
11572                .canonicalize_simplified()
11573                .unwrap_or_else(|_| git_dir.clone());
11574            if !cur.starts_with(&sg) {
11575                git_dir = local_mod;
11576            }
11577        }
11578    }
11579    Some(git_dir)
11580}
11581
11582fn read_submodule_head_oid_from_git_dir(
11583    git_dir: &Path,
11584    allow_branch_fallback: bool,
11585) -> Option<ObjectId> {
11586    let head_content = fs::read_to_string(git_dir.join("HEAD")).ok()?;
11587    let head_trimmed = head_content.trim();
11588    if head_trimmed.starts_with("ref: ") {
11589        match crate::refs::resolve_ref(git_dir, "HEAD") {
11590            Ok(oid) => Some(oid),
11591            Err(_) if allow_branch_fallback => {
11592                for branch in ["main", "master"] {
11593                    let p = git_dir.join("refs/heads").join(branch);
11594                    if let Ok(s) = fs::read_to_string(&p) {
11595                        if let Ok(o) = ObjectId::from_hex(s.trim()) {
11596                            return Some(o);
11597                        }
11598                    }
11599                }
11600                None
11601            }
11602            Err(_) => None,
11603        }
11604    } else {
11605        ObjectId::from_hex(head_trimmed).ok()
11606    }
11607}
11608
11609/// Read the HEAD commit OID from a submodule working tree directory.
11610///
11611/// Handles both embedded `.git` directories and `gitdir:` gitfiles pointing at
11612/// `.git/modules/...` (or other locations). Returns `None` if the path is not
11613/// a checkout or has no resolvable HEAD.
11614pub(crate) fn read_submodule_head_oid(sub_dir: &Path) -> Option<ObjectId> {
11615    let git_dir = submodule_git_dir_for_head_read(sub_dir)?;
11616    read_submodule_head_oid_from_git_dir(&git_dir, true)
11617}
11618
11619/// Like [`read_submodule_head_oid`], but requires `HEAD` itself to resolve (no `main`/`master`
11620/// fallback). Used when staging embedded repositories so a broken symref matches Git's refusal.
11621pub(crate) fn read_submodule_head_oid_for_stage(sub_dir: &Path) -> Option<ObjectId> {
11622    let git_dir = submodule_git_dir_for_head_read(sub_dir)?;
11623    read_submodule_head_oid_from_git_dir(&git_dir, false)
11624}
11625
11626/// True when a populated submodule checkout is *broken*: its `HEAD` resolves to a commit OID, but
11627/// that commit object cannot be read from the submodule's own object database.
11628///
11629/// This mirrors Git's `is_submodule_modified`, which shells out to `git status --porcelain=2`
11630/// inside the submodule; when the submodule's object store is corrupt (e.g. `rm -r .git/objects`),
11631/// that inner status fails and Git aborts the surrounding `status`/`diff`/`fetch`. We detect the
11632/// same condition in-process so the superproject operation can return a fatal error rather than
11633/// silently treating the submodule as clean (t5526 "fetching submodule into a broken repository").
11634///
11635/// Returns `false` when the submodule is not checked out, has no embedded git dir, or has an
11636/// unresolvable HEAD (those are handled separately by the unpopulated/placeholder logic).
11637#[must_use]
11638pub(crate) fn submodule_head_object_broken(sub_dir: &Path) -> bool {
11639    let Some(sub_git_dir) = submodule_embedded_git_dir(sub_dir) else {
11640        return false;
11641    };
11642    let Some(head_oid) = read_submodule_head_oid(sub_dir) else {
11643        return false;
11644    };
11645    let odb = Odb::new(&sub_git_dir.join("objects"));
11646    match odb.read(&head_oid) {
11647        // HEAD object present but not a commit would be a different kind of corruption; only the
11648        // missing-object case is the broken-repo scenario Git guards against here.
11649        Ok(obj) => parse_commit(&obj.data).is_err(),
11650        Err(_) => true,
11651    }
11652}
11653
11654/// Submodule dirty bits aligned with Git's `DIRTY_SUBMODULE_*` / porcelain v2 `S???` token.
11655#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
11656pub struct SubmodulePorcelainFlags {
11657    /// Submodule checkout HEAD differs from the gitlink OID recorded in the parent index.
11658    pub new_commits: bool,
11659    /// The submodule has its own staged or unstaged changes (`DIRTY_SUBMODULE_MODIFIED`).
11660    pub modified: bool,
11661    /// The submodule work tree contains paths not in its index (`DIRTY_SUBMODULE_UNTRACKED`).
11662    pub untracked: bool,
11663}
11664
11665/// Inspect a checked-out submodule at `rel_path` (relative to `super_worktree`) and return
11666/// flags used for `git status --porcelain=v2` submodule tokens.
11667///
11668/// `recorded_oid` is the gitlink OID stored in the **parent** index (stage 0). When the
11669/// submodule is not checked out or cannot be opened, returns [`Default::default()`].
11670pub fn submodule_porcelain_flags(
11671    super_worktree: &Path,
11672    rel_path: &str,
11673    recorded_oid: ObjectId,
11674) -> SubmodulePorcelainFlags {
11675    let sub_dir = super_worktree.join(rel_path);
11676    let Some(sub_git_dir) = submodule_embedded_git_dir(&sub_dir) else {
11677        return SubmodulePorcelainFlags::default();
11678    };
11679    let Some(sub_head) = read_submodule_head_oid(&sub_dir) else {
11680        return SubmodulePorcelainFlags::default();
11681    };
11682
11683    let new_commits = sub_head != recorded_oid;
11684
11685    let index_path = sub_git_dir.join("index");
11686    let mut sub_index = match crate::index::Index::load(&index_path) {
11687        Ok(ix) => ix,
11688        Err(_) => {
11689            return SubmodulePorcelainFlags {
11690                new_commits,
11691                ..Default::default()
11692            }
11693        }
11694    };
11695
11696    let tracked: std::collections::BTreeSet<String> = sub_index
11697        .entries
11698        .iter()
11699        .filter(|e| e.stage() == 0)
11700        .map(|e| String::from_utf8_lossy(&e.path).into_owned())
11701        .collect();
11702    let untracked = submodule_dir_has_untracked_inner(&sub_dir, &sub_dir, &tracked, &sub_index);
11703
11704    let objects_dir = sub_git_dir.join("objects");
11705    let odb = Odb::new(&objects_dir);
11706
11707    let sub_head_tree = (|| -> Option<ObjectId> {
11708        let h = fs::read_to_string(sub_git_dir.join("HEAD")).ok()?;
11709        let h_str = h.trim();
11710        let commit_oid = if let Some(r) = h_str.strip_prefix("ref: ") {
11711            let oid_hex = fs::read_to_string(sub_git_dir.join(r)).ok()?;
11712            ObjectId::from_hex(oid_hex.trim()).ok()?
11713        } else {
11714            ObjectId::from_hex(h_str).ok()?
11715        };
11716        let obj = odb.read(&commit_oid).ok()?;
11717        let commit = parse_commit(&obj.data).ok()?;
11718        Some(commit.tree)
11719    })();
11720
11721    let staged_dirty = sub_head_tree
11722        .as_ref()
11723        .map(|t| diff_index_to_tree(&odb, &sub_index, Some(t), false).map(|v| !v.is_empty()))
11724        .unwrap_or(Ok(false));
11725    let staged_dirty = staged_dirty.unwrap_or(false);
11726
11727    let unstaged_dirty = diff_index_to_worktree(&odb, &mut sub_index, &sub_dir, false, true)
11728        .map(|v| !v.is_empty())
11729        .unwrap_or(false);
11730
11731    let mut modified = staged_dirty || unstaged_dirty;
11732
11733    // Nested submodule has its own index: OR `modified` from immediate gitlink children so a
11734    // dirty nested checkout (e.g. staged `file` under `sub1/sub2`) marks the parent gitlink as
11735    // modified in the superproject (t7506). Do **not** OR `untracked` — untracked-only inside a
11736    // nested submodule must stay `S..U` on the parent, not `S.U` / `S.M.`.
11737    for e in &sub_index.entries {
11738        if e.stage() != 0 || e.mode != 0o160000 {
11739            continue;
11740        }
11741        let child = String::from_utf8_lossy(&e.path).into_owned();
11742        let full_rel = if rel_path.is_empty() {
11743            child
11744        } else {
11745            format!("{rel_path}/{child}")
11746        };
11747        let nested = submodule_porcelain_flags(super_worktree, &full_rel, e.oid);
11748        modified |= nested.modified;
11749    }
11750
11751    SubmodulePorcelainFlags {
11752        new_commits,
11753        modified,
11754        untracked,
11755    }
11756}
11757
11758fn submodule_dir_has_untracked_inner(
11759    dir: &Path,
11760    root: &Path,
11761    tracked: &std::collections::BTreeSet<String>,
11762    owning_index: &Index,
11763) -> bool {
11764    let entries = match fs::read_dir(dir) {
11765        Ok(e) => e,
11766        Err(_) => return false,
11767    };
11768    let mut sorted: Vec<_> = entries.filter_map(|e| e.ok()).collect();
11769    sorted.sort_by_key(|e| e.file_name());
11770
11771    for entry in sorted {
11772        let name = entry.file_name().to_string_lossy().to_string();
11773        if name == ".git" {
11774            continue;
11775        }
11776        let path = entry.path();
11777        let rel = path
11778            .strip_prefix(root)
11779            .map(|p| p.to_string_lossy().to_string())
11780            .unwrap_or_else(|_| name.clone());
11781
11782        let is_dir = entry.file_type().map(|ft| ft.is_dir()).unwrap_or(false);
11783        if is_dir {
11784            let is_gitlink = owning_index
11785                .get(rel.as_bytes(), 0)
11786                .is_some_and(|e| e.mode == 0o160000);
11787            if is_gitlink {
11788                let Some(nested_git) = submodule_embedded_git_dir(&path) else {
11789                    continue;
11790                };
11791                let nested_index_path = nested_git.join("index");
11792                let Ok(nested_ix) = crate::index::Index::load(&nested_index_path) else {
11793                    continue;
11794                };
11795                let nested_tracked: std::collections::BTreeSet<String> = nested_ix
11796                    .entries
11797                    .iter()
11798                    .filter(|e| e.stage() == 0)
11799                    .map(|e| String::from_utf8_lossy(&e.path).into_owned())
11800                    .collect();
11801                if submodule_dir_has_untracked_inner(&path, &path, &nested_tracked, &nested_ix) {
11802                    return true;
11803                }
11804            } else if submodule_dir_has_untracked_inner(&path, root, tracked, owning_index) {
11805                return true;
11806            }
11807        } else if !tracked.contains(&rel) {
11808            return true;
11809        }
11810    }
11811    false
11812}
11813
11814#[cfg(test)]
11815mod line_compare_key_tests {
11816    use super::*;
11817    use crate::diff::unified_emit::line_diff_token;
11818    use crate::diff::WhitespaceMode;
11819
11820    #[test]
11821    fn borrowed_keys_match_owned_line_tokens() {
11822        let texts = [
11823            "",
11824            "\n",
11825            "a",
11826            "a\n",
11827            "a\nb",
11828            "a\r\nb\r\n",
11829            "a\r\nb\r",
11830            "\n\n\nlast",
11831            "x\n\r\n\ry\n",
11832        ];
11833        for text in texts {
11834            let side = LineSide::from_text(text);
11835            let mut normalized = Vec::new();
11836            let keys = line_compare_keys(&side, WhitespaceMode::default(), &mut normalized);
11837            let expected: Vec<String> = side.lines().map(|l| line_diff_token(&l)).collect();
11838            assert_eq!(keys, expected, "text {text:?}");
11839            let source = text.as_bytes().as_ptr_range();
11840            assert!(
11841                keys.iter()
11842                    .all(|key| source.contains(&key.as_ptr()) || key.is_empty()),
11843                "text {text:?}"
11844            );
11845        }
11846    }
11847
11848    #[test]
11849    fn whitespace_modes_compare_normalized_bodies() {
11850        let text = "a  b\nab \n";
11851        let side = LineSide::from_text(text);
11852        let ws = WhitespaceMode {
11853            ignore_all_space: true,
11854            ..WhitespaceMode::default()
11855        };
11856        let mut normalized = Vec::new();
11857        let keys = line_compare_keys(&side, ws, &mut normalized);
11858        assert_eq!(keys, vec!["ab", "ab"]);
11859    }
11860
11861    #[test]
11862    fn xdiff_marks_only_edited_lines_in_large_file() {
11863        let old: String = (0..5_000).map(|i| format!("line{i}\n")).collect();
11864        let new: String = (0..5_000)
11865            .map(|i| {
11866                if i % 100 == 0 {
11867                    format!("LINE{i}\n")
11868                } else {
11869                    format!("line{i}\n")
11870                }
11871            })
11872            .collect();
11873        let body =
11874            git_xdiff_unified_body_raw(&old, &new, 3, 0, true, false, WhitespaceMode::default());
11875        assert_eq!(body.matches("\n@@ ").count() + 1, 50);
11876        assert_eq!(body.lines().filter(|l| l.starts_with('-')).count(), 50);
11877        assert_eq!(body.lines().filter(|l| l.starts_with('+')).count(), 50);
11878        assert!(body.starts_with("@@ -1,4 +1,4 @@\n-line0\n+LINE0\n line1\n"));
11879    }
11880}
11881
11882#[cfg(test)]
11883mod entry_order_tests {
11884    use super::*;
11885
11886    fn entry(status: DiffStatus, path: &str, score: Option<u32>) -> DiffEntry {
11887        let mode = |present: bool| {
11888            if present {
11889                FileMode::REGULAR
11890            } else {
11891                FileMode::ABSENT
11892            }
11893        };
11894        DiffEntry {
11895            status,
11896            old_path: (status != DiffStatus::Added).then(|| path.to_owned()),
11897            new_path: (status != DiffStatus::Deleted).then(|| path.to_owned()),
11898            old_mode: mode(status != DiffStatus::Added),
11899            new_mode: mode(status != DiffStatus::Deleted),
11900            old_oid: ObjectId::zero(),
11901            new_oid: ObjectId::zero(),
11902            score,
11903        }
11904    }
11905
11906    #[test]
11907    fn sort_entries_by_path_orders_by_path_and_keeps_equal_paths_in_place() {
11908        let mut entries = vec![
11909            entry(DiffStatus::Modified, "b", None),
11910            entry(DiffStatus::Deleted, "a/x", Some(1)),
11911            entry(DiffStatus::Added, "a/x", Some(2)),
11912            entry(DiffStatus::Added, "a", None),
11913        ];
11914        sort_entries_by_path(&mut entries);
11915        let order: Vec<_> = entries.iter().map(|e| (e.path(), e.score)).collect();
11916        assert_eq!(
11917            order,
11918            [("a", None), ("a/x", Some(1)), ("a/x", Some(2)), ("b", None)]
11919        );
11920        let sorted = entries.clone();
11921        sort_entries_by_path(&mut entries);
11922        assert_eq!(entries, sorted);
11923        sort_entries_by_path(&mut []);
11924    }
11925
11926    #[test]
11927    fn sort_entries_by_path_compares_non_utf8_paths_by_raw_bytes() {
11928        let raw = crate::repo_relpath::format_path_stored("", b"raw-\xff");
11929        assert!(raw.starts_with("grit+pathhex:"));
11930        let mut entries = vec![
11931            entry(DiffStatus::Modified, &raw, None),
11932            entry(DiffStatus::Modified, "node/leaf", None),
11933            entry(DiffStatus::Modified, "zeta", None),
11934        ];
11935        sort_entries_by_path(&mut entries);
11936        let paths: Vec<_> = entries.iter().map(DiffEntry::path).collect();
11937        assert_eq!(paths, ["node/leaf", raw.as_str(), "zeta"]);
11938    }
11939
11940    #[test]
11941    fn rename_pass_returns_unpairable_lists_in_path_order() {
11942        let config = ConfigSet::new();
11943        let policy = ThreadPolicy::default();
11944        let odb = Odb::new(Path::new("/nonexistent-objects"));
11945        let run = |entries: Vec<DiffEntry>| {
11946            detect_renames_with_policy(
11947                &odb,
11948                None,
11949                entries,
11950                50,
11951                &config,
11952                &policy,
11953                None,
11954                DetectRenamesOptions::default(),
11955            )
11956            .expect("no blob is read without both adds and deletes")
11957            .entries
11958        };
11959        let sorted = vec![
11960            entry(DiffStatus::Deleted, "a", None),
11961            entry(DiffStatus::Modified, "b", None),
11962            entry(DiffStatus::Deleted, "c", None),
11963        ];
11964        assert_eq!(run(sorted.clone()), sorted);
11965        let unsorted = vec![
11966            entry(DiffStatus::Added, "z", None),
11967            entry(DiffStatus::Modified, "m", None),
11968            entry(DiffStatus::Added, "a", None),
11969        ];
11970        let paths: Vec<_> = run(unsorted).iter().map(|e| e.path().to_owned()).collect();
11971        assert_eq!(paths, ["a", "m", "z"]);
11972        // A duplicate path takes the regrouping path: modifications before additions.
11973        let duplicate = vec![
11974            entry(DiffStatus::Added, "p", None),
11975            entry(DiffStatus::Modified, "p", None),
11976        ];
11977        let statuses: Vec<_> = run(duplicate).iter().map(|e| e.status).collect();
11978        assert_eq!(statuses, [DiffStatus::Modified, DiffStatus::Added]);
11979    }
11980}
11981
11982#[cfg(test)]
11983mod diff_index_to_tree_cache_tree_tests {
11984    use std::rc::Rc;
11985    use std::sync::atomic::{AtomicUsize, Ordering};
11986
11987    use super::*;
11988    use crate::index::{Index, IndexEntry, MODE_EXECUTABLE, MODE_GITLINK, MODE_REGULAR};
11989    use crate::objects::ObjectKind;
11990    use crate::write_tree::{build_cache_tree_from_index, write_tree_from_index};
11991    use tempfile::TempDir;
11992
11993    struct TreeReadCounter {
11994        counter: Rc<AtomicUsize>,
11995    }
11996
11997    impl TreeReadCounter {
11998        fn start() -> Self {
11999            let counter = Rc::new(AtomicUsize::new(0));
12000            set_diff_tree_read_hook_for_tests(Some(counter.clone()));
12001            Self { counter }
12002        }
12003
12004        fn get(&self) -> usize {
12005            self.counter.load(Ordering::Relaxed)
12006        }
12007    }
12008
12009    impl Drop for TreeReadCounter {
12010        fn drop(&mut self) {
12011            set_diff_tree_read_hook_for_tests(None);
12012        }
12013    }
12014
12015    fn diff_index_to_tree_flatten_reference(
12016        odb: &Odb,
12017        index: &Index,
12018        head_tree: &ObjectId,
12019        ignore_submodules: bool,
12020    ) -> Result<Vec<DiffEntry>> {
12021        let mut flat_index = index.clone();
12022        flat_index.clear_cache_tree();
12023        diff_index_to_tree(odb, &flat_index, Some(head_tree), ignore_submodules)
12024    }
12025
12026    fn index_entry(path: &str, mode: u32, oid: ObjectId) -> IndexEntry {
12027        IndexEntry {
12028            ctime_sec: 0,
12029            ctime_nsec: 0,
12030            mtime_sec: 0,
12031            mtime_nsec: 0,
12032            dev: 0,
12033            ino: 0,
12034            mode,
12035            uid: 0,
12036            gid: 0,
12037            size: 0,
12038            oid,
12039            flags: path.len().min(0xfff) as u16,
12040            flags_extended: None,
12041            path: path.as_bytes().to_vec(),
12042            base_index_pos: 0,
12043        }
12044    }
12045
12046    fn repo_with_tree(depth: usize, width: usize) -> (TempDir, Odb, ObjectId, Index) {
12047        let temp = TempDir::new().expect("tempdir");
12048        let odb = Odb::new(temp.path());
12049        let mut index = Index::new();
12050        for d in 0..depth {
12051            for w in 0..width {
12052                let path = format!("unchanged/d{d}/f{w}.txt");
12053                let oid = odb
12054                    .write(ObjectKind::Blob, format!("body-{d}-{w}").as_bytes())
12055                    .expect("blob");
12056                index.add_or_replace(index_entry(&path, MODE_REGULAR, oid));
12057            }
12058        }
12059        let head_tree = write_tree_from_index(&odb, &index, "").expect("write tree");
12060        let cache = build_cache_tree_from_index(&odb, &index).expect("cache-tree");
12061        index.set_cache_tree(cache);
12062        index.cache_tree_root = Some(head_tree);
12063        (temp, odb, head_tree, index)
12064    }
12065
12066    #[test]
12067    fn diff_index_to_tree_cache_tree_fast_path_matches_slow() {
12068        let (_temp, odb, head_tree, mut index) = repo_with_tree(4, 5);
12069        assert!(index.cache_tree.as_ref().is_some_and(|c| c.is_valid()));
12070
12071        let mut state: u64 = 0xDEAD_BEEF;
12072        for _ in 0..24 {
12073            state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
12074            let d = (state % 4) as usize;
12075            state = state.wrapping_mul(6364136223846793005).wrapping_add(1);
12076            let w = (state % 5) as usize;
12077            let path = format!("unchanged/d{d}/f{w}.txt");
12078            index.invalidate_cache_tree_for_path(path.as_bytes());
12079            assert!(
12080                index.cache_tree_root.is_none(),
12081                "partial invalidation clears cache_tree_root like Index::add"
12082            );
12083            assert!(
12084                index.cache_tree.as_ref().is_some_and(|r| !r.is_valid()),
12085                "root cache node must be invalid after partial invalidation"
12086            );
12087            if let Some(e) = index.get_mut(path.as_bytes(), 0) {
12088                let new_oid = odb
12089                    .write(ObjectKind::Blob, format!("new-{d}-{w}").as_bytes())
12090                    .expect("blob");
12091                e.oid = new_oid;
12092            }
12093            let fast = diff_index_to_tree(&odb, &index, Some(&head_tree), false).expect("fast");
12094            let flat = diff_index_to_tree_flatten_reference(&odb, &index, &head_tree, false)
12095                .expect("flat");
12096            assert_eq!(fast, flat, "mismatch after mutating {path}");
12097        }
12098    }
12099
12100    #[test]
12101    fn diff_index_to_tree_skips_unchanged_subtrees() {
12102        let (_temp, odb, head_tree, index) = repo_with_tree(5, 6);
12103        let reads = TreeReadCounter::start();
12104        diff_index_to_tree(&odb, &index, Some(&head_tree), false).expect("cached diff");
12105        let cached_reads = reads.get();
12106
12107        let mut no_cache = index.clone();
12108        no_cache.clear_cache_tree();
12109        let reads = TreeReadCounter::start();
12110        diff_index_to_tree(&odb, &no_cache, Some(&head_tree), false).expect("flat diff");
12111        let flat_reads = reads.get();
12112
12113        assert!(
12114            cached_reads < flat_reads,
12115            "expected fewer tree reads with cache-tree (cached={cached_reads}, flat={flat_reads})"
12116        );
12117        assert_eq!(
12118            cached_reads, 1,
12119            "unchanged repo should only read the root tree"
12120        );
12121    }
12122
12123    #[test]
12124    fn diff_index_to_tree_partial_invalidation_skips_sibling_subtree() {
12125        let (_temp, odb, head_tree, mut index) = repo_with_tree(5, 6);
12126        let path = "unchanged/d1/f2.txt";
12127        index.invalidate_cache_tree_for_path(path.as_bytes());
12128        let new_oid = odb.write(ObjectKind::Blob, b"changed").expect("blob");
12129        index.get_mut(path.as_bytes(), 0).expect("entry").oid = new_oid;
12130
12131        let flat =
12132            diff_index_to_tree_flatten_reference(&odb, &index, &head_tree, false).expect("flat");
12133        let reads = TreeReadCounter::start();
12134        let fast = diff_index_to_tree(&odb, &index, Some(&head_tree), false).expect("fast");
12135        let cached_reads = reads.get();
12136        assert_eq!(fast, flat);
12137
12138        let reads = TreeReadCounter::start();
12139        diff_index_to_tree_flatten_reference(&odb, &index, &head_tree, false).expect("flat");
12140        let flat_reads = reads.get();
12141
12142        assert!(
12143            cached_reads < flat_reads,
12144            "partial cache-tree should skip sibling dirs (cached={cached_reads}, flat={flat_reads})"
12145        );
12146    }
12147
12148    fn cache_leaf(name: &str) -> CacheTreeNode {
12149        CacheTreeNode::valid(name.as_bytes().to_vec(), 1, ObjectId::zero(), Vec::new())
12150    }
12151
12152    #[test]
12153    fn cache_children_lookup_matches_linear_scan_on_wide_unsorted_nodes() {
12154        let mut children: Vec<CacheTreeNode> = (0..CACHE_CHILD_LINEAR_SCAN_MAX * 3)
12155            .rev()
12156            .map(|i| cache_leaf(&format!("d{i:03}")))
12157            .collect();
12158        let mut first_dup = cache_leaf("d007");
12159        first_dup.entry_count = 7;
12160        children.insert(3, first_dup);
12161        let root = CacheTreeNode::valid(Vec::new(), 0, ObjectId::zero(), children);
12162
12163        let lookup = CacheChildren::new(Some(&root));
12164        assert!(
12165            !lookup.sorted.is_empty(),
12166            "wide nodes must use the sorted lookup"
12167        );
12168        for name in ["d000", "d007", "d020", "d047", "zzz", "d", ""] {
12169            let linear = root.children.iter().find(|c| c.name == name.as_bytes());
12170            let fast = lookup.get(name.as_bytes());
12171            assert_eq!(
12172                fast.map(|c| (&c.name, c.entry_count)),
12173                linear.map(|c| (&c.name, c.entry_count)),
12174                "lookup of {name:?}"
12175            );
12176        }
12177        assert_eq!(lookup.get(b"d007").map(|c| c.entry_count), Some(7));
12178
12179        let narrow = CacheTreeNode::valid(
12180            Vec::new(),
12181            0,
12182            ObjectId::zero(),
12183            vec![cache_leaf("b"), cache_leaf("a")],
12184        );
12185        let lookup = CacheChildren::new(Some(&narrow));
12186        assert!(lookup.sorted.is_empty());
12187        assert_eq!(lookup.get(b"a").map(|c| c.name.as_slice()), Some(&b"a"[..]));
12188        assert!(lookup.get(b"c").is_none());
12189        assert!(CacheChildren::new(None).get(b"a").is_none());
12190    }
12191
12192    #[test]
12193    fn index_relative_in_dir_requires_a_component_boundary() {
12194        assert_eq!(index_relative_in_dir(b"a/b", b""), Some(&b"a/b"[..]));
12195        assert_eq!(index_relative_in_dir(b"a/b", b"a"), Some(&b"b"[..]));
12196        assert_eq!(index_relative_in_dir(b"a", b"a"), Some(&b""[..]));
12197        assert_eq!(index_relative_in_dir(b"ab/c", b"a"), None);
12198        assert_eq!(index_relative_in_dir(b"a.c", b"a"), None);
12199        assert_eq!(index_relative_in_dir(b"b", b"a"), None);
12200        assert_eq!(join_index_path(b"", b"x"), b"x".to_vec());
12201        assert_eq!(join_index_path(b"a/b", b"x"), b"a/b/x".to_vec());
12202    }
12203
12204    /// Wide root (more children than the linear-scan cutoff), cache-tree children stored in
12205    /// reverse order, a few changed paths, plus additions and deletions at both levels.
12206    #[test]
12207    fn diff_index_to_tree_wide_unsorted_cache_tree_matches_flatten() {
12208        let temp = TempDir::new().expect("tempdir");
12209        let odb = Odb::new(temp.path());
12210        let mut index = Index::new();
12211        let width = CACHE_CHILD_LINEAR_SCAN_MAX * 4;
12212        for d in 0..width {
12213            for f in 0..2 {
12214                let path = format!("dir{d:03}/sub/f{f}.txt");
12215                let oid = odb.write(ObjectKind::Blob, path.as_bytes()).expect("blob");
12216                index.add_or_replace(index_entry(&path, MODE_REGULAR, oid));
12217            }
12218        }
12219        let head_tree = write_tree_from_index(&odb, &index, "").expect("write tree");
12220        let mut cache = build_cache_tree_from_index(&odb, &index).expect("cache-tree");
12221        cache.children.reverse();
12222        index.set_cache_tree(cache);
12223        index.cache_tree_root = Some(head_tree);
12224
12225        let changed = [
12226            "dir003/sub/f1.txt",
12227            "dir031/sub/f0.txt",
12228            "dir063/sub/f1.txt",
12229        ];
12230        for path in changed {
12231            index.invalidate_cache_tree_for_path(path.as_bytes());
12232            let oid = odb.write(ObjectKind::Blob, b"changed").expect("blob");
12233            index.get_mut(path.as_bytes(), 0).expect("entry").oid = oid;
12234        }
12235        for path in ["dir010/sub/new.txt", "dir999/new.txt", "top.txt"] {
12236            index.invalidate_cache_tree_for_path(path.as_bytes());
12237            let oid = odb.write(ObjectKind::Blob, path.as_bytes()).expect("blob");
12238            index.add_or_replace(index_entry(path, MODE_REGULAR, oid));
12239        }
12240        for path in [
12241            "dir020/sub/f0.txt",
12242            "dir020/sub/f1.txt",
12243            "dir021/sub/f0.txt",
12244        ] {
12245            index.invalidate_cache_tree_for_path(path.as_bytes());
12246            index.remove(path.as_bytes());
12247        }
12248
12249        let flat =
12250            diff_index_to_tree_flatten_reference(&odb, &index, &head_tree, false).expect("flat");
12251        let reads = TreeReadCounter::start();
12252        let fast = diff_index_to_tree(&odb, &index, Some(&head_tree), false).expect("fast");
12253        let cached_reads = reads.get();
12254        assert_eq!(fast, flat);
12255        assert_eq!(fast.len(), changed.len() + 3 + 3);
12256        assert!(
12257            cached_reads <= 1 + 2 * 6,
12258            "unchanged siblings must be skipped through the cache-tree (reads={cached_reads})"
12259        );
12260    }
12261
12262    #[test]
12263    fn diff_index_to_tree_cache_tree_handles_non_utf8_directory_names() {
12264        let temp = TempDir::new().expect("tempdir");
12265        let odb = Odb::new(temp.path());
12266        let mut index = Index::new();
12267        let raw_dirs: [&[u8]; 3] = [b"caf\xe9", b"caf\xef\xbf\xbd", b"plain"];
12268        for dir in raw_dirs {
12269            for f in [&b"a.txt"[..], b"b.txt"] {
12270                let path = [dir, b"/", f].concat();
12271                let oid = odb.write(ObjectKind::Blob, &path).expect("blob");
12272                let mut entry = index_entry("x", MODE_REGULAR, oid);
12273                entry.flags = path.len().min(0xfff) as u16;
12274                entry.path = path;
12275                index.add_or_replace(entry);
12276            }
12277        }
12278        let head_tree = write_tree_from_index(&odb, &index, "").expect("write tree");
12279        let cache = build_cache_tree_from_index(&odb, &index).expect("cache-tree");
12280        index.set_cache_tree(cache);
12281        index.cache_tree_root = Some(head_tree);
12282
12283        let changed: &[u8] = b"caf\xe9/b.txt";
12284        index.invalidate_cache_tree_for_path(changed);
12285        let oid = odb.write(ObjectKind::Blob, b"changed").expect("blob");
12286        index.get_mut(changed, 0).expect("entry").oid = oid;
12287
12288        // Directory prefixes are compared as raw bytes, so `caf\xe9` and a directory literally
12289        // named `caf\u{fffd}` stay distinct even though they print the same.
12290        let fast = diff_index_to_tree(&odb, &index, Some(&head_tree), false).expect("fast");
12291        assert_eq!(fast.len(), 1, "{fast:?}");
12292        assert_eq!(fast[0].status, DiffStatus::Modified);
12293        assert_eq!(
12294            fast[0].old_oid,
12295            odb.write(ObjectKind::Blob, changed).expect("blob")
12296        );
12297        assert_eq!(fast[0].new_oid, oid);
12298    }
12299
12300    #[test]
12301    fn diff_index_to_tree_reports_mode_gitlink_and_intent_to_add() {
12302        let temp = TempDir::new().expect("tempdir");
12303        let odb = Odb::new(temp.path());
12304        let blob = odb.write(ObjectKind::Blob, b"x").expect("blob");
12305        let mut index = Index::new();
12306        index.add_or_replace(index_entry("tracked.txt", MODE_REGULAR, blob));
12307        let head_tree = write_tree_from_index(&odb, &index, "").expect("tree");
12308        let cache = build_cache_tree_from_index(&odb, &index).expect("cache");
12309        index.set_cache_tree(cache);
12310
12311        let mode_entry = index_entry("tracked.txt", MODE_EXECUTABLE, blob);
12312        index.add_or_replace(mode_entry);
12313        index.invalidate_cache_tree_for_path(b"tracked.txt");
12314
12315        let diffs = diff_index_to_tree(&odb, &index, Some(&head_tree), false).expect("diff");
12316        assert_eq!(diffs.len(), 1);
12317        assert_eq!(diffs[0].status, DiffStatus::Modified);
12318        assert_eq!(diffs[0].old_mode, FileMode::new(MODE_REGULAR));
12319        assert_eq!(diffs[0].new_mode, FileMode::new(MODE_EXECUTABLE));
12320
12321        // Intent-to-add is omitted from staged diff.
12322        let mut ita = index_entry("new.txt", MODE_REGULAR, blob);
12323        ita.set_intent_to_add(true);
12324        index.add_or_replace(ita);
12325        let diffs = diff_index_to_tree(&odb, &index, Some(&head_tree), false).expect("diff");
12326        assert!(
12327            diffs.iter().all(|d| d.path() != "new.txt"),
12328            "intent-to-add must not appear in staged diff"
12329        );
12330
12331        // Gitlink with ignore_submodules skips diff entry but still matches tree.
12332        let gitlink: ObjectId = "855827c583bc30645ba427885caa40c5b81764d2"
12333            .parse()
12334            .expect("oid");
12335        let mut sub_index = Index::new();
12336        sub_index.add_or_replace(index_entry("sub", MODE_GITLINK, gitlink));
12337        let sub_tree = write_tree_from_index(&odb, &sub_index, "").expect("tree");
12338        let sub_cache = build_cache_tree_from_index(&odb, &sub_index).expect("cache");
12339        sub_index.set_cache_tree(sub_cache);
12340        let diffs =
12341            diff_index_to_tree(&odb, &sub_index, Some(&sub_tree), true).expect("gitlink diff");
12342        assert!(
12343            diffs.is_empty(),
12344            "ignored gitlink should not diff: {diffs:?}"
12345        );
12346    }
12347
12348    #[test]
12349    fn ignore_submodules_skips_gitlink_in_added_directory_cache_walk() {
12350        let temp = TempDir::new().expect("tempdir");
12351        let odb = Odb::new(temp.path());
12352        let gitlink: ObjectId = "855827c583bc30645ba427885caa40c5b81764d2"
12353            .parse()
12354            .expect("oid");
12355        let head_tree = write_tree_from_index(&odb, &Index::new(), "").expect("empty tree");
12356        let mut index = Index::new();
12357        index.add_or_replace(index_entry("newdir/sub", MODE_GITLINK, gitlink));
12358        let cache = build_cache_tree_from_index(&odb, &index).expect("cache");
12359        index.set_cache_tree(cache);
12360        index.invalidate_cache_tree_for_path(b"newdir/sub");
12361
12362        let fast = diff_index_to_tree(&odb, &index, Some(&head_tree), true).expect("fast");
12363        let flat =
12364            diff_index_to_tree_flatten_reference(&odb, &index, &head_tree, true).expect("flat");
12365        assert_eq!(fast, flat);
12366        assert!(
12367            fast.is_empty(),
12368            "nested gitlinks in added dirs must be ignored: {fast:?}"
12369        );
12370    }
12371
12372    #[test]
12373    fn ignore_submodules_skips_gitlink_in_deleted_directory_cache_walk() {
12374        let temp = TempDir::new().expect("tempdir");
12375        let odb = Odb::new(temp.path());
12376        let blob = odb.write(ObjectKind::Blob, b"keep").expect("blob");
12377        let gitlink: ObjectId = "855827c583bc30645ba427885caa40c5b81764d2"
12378            .parse()
12379            .expect("oid");
12380        let mut index = Index::new();
12381        index.add_or_replace(index_entry("keep.txt", MODE_REGULAR, blob));
12382        index.add_or_replace(index_entry("olddir/sub", MODE_GITLINK, gitlink));
12383        let head_tree = write_tree_from_index(&odb, &index, "").expect("head");
12384        let cache = build_cache_tree_from_index(&odb, &index).expect("cache");
12385        index.set_cache_tree(cache);
12386        index
12387            .entries
12388            .retain(|e| !String::from_utf8_lossy(&e.path).starts_with("olddir/"));
12389        index.invalidate_cache_tree_for_path(b"olddir/sub");
12390
12391        let fast = diff_index_to_tree(&odb, &index, Some(&head_tree), true).expect("fast");
12392        let flat =
12393            diff_index_to_tree_flatten_reference(&odb, &index, &head_tree, true).expect("flat");
12394        assert_eq!(fast, flat);
12395        assert!(
12396            fast.is_empty(),
12397            "nested gitlinks in deleted dirs must be ignored: {fast:?}"
12398        );
12399    }
12400
12401    #[test]
12402    fn diff_trees_skips_identical_subtree_oids() {
12403        let temp = TempDir::new().expect("tempdir");
12404        let odb = Odb::new(temp.path());
12405        let leaf = odb.write(ObjectKind::Blob, b"leaf").expect("blob");
12406        let mut deep = Index::new();
12407        for i in 0..20 {
12408            deep.add_or_replace(index_entry(&format!("deep/n{i}.txt"), MODE_REGULAR, leaf));
12409        }
12410        let tree = write_tree_from_index(&odb, &deep, "").expect("tree");
12411        let reads = TreeReadCounter::start();
12412        let entries = diff_trees(&odb, Some(&tree), Some(&tree), "").expect("diff");
12413        assert!(entries.is_empty());
12414        assert_eq!(
12415            reads.get(),
12416            0,
12417            "identical tree oids should short-circuit without reading tree objects"
12418        );
12419    }
12420
12421    #[test]
12422    fn diff_tree_path_change_count_matches_diff_trees() {
12423        let temp = TempDir::new().expect("tempdir");
12424        let odb = Odb::new(temp.path());
12425        let a = odb.write(ObjectKind::Blob, b"a").expect("blob");
12426        let b = odb.write(ObjectKind::Blob, b"b").expect("blob");
12427        let mut left = Index::new();
12428        left.add_or_replace(index_entry("f.txt", MODE_REGULAR, a));
12429        let mut right = Index::new();
12430        right.add_or_replace(index_entry("f.txt", MODE_REGULAR, b));
12431        right.add_or_replace(index_entry("g.txt", MODE_REGULAR, a));
12432        let old_tree = write_tree_from_index(&odb, &left, "").expect("old");
12433        let new_tree = write_tree_from_index(&odb, &right, "").expect("new");
12434        let entries = diff_trees(&odb, Some(&old_tree), Some(&new_tree), "").expect("diff");
12435        let count =
12436            diff_tree_path_change_count(&odb, Some(&old_tree), Some(&new_tree), "").expect("count");
12437        assert_eq!(count, entries.len());
12438        assert_eq!(count, 2);
12439    }
12440
12441    #[test]
12442    fn diff_trees_matches_whether_trees_were_cached_parsed_or_raw() {
12443        let temp = TempDir::new().expect("tempdir");
12444        let a = Odb::new(temp.path())
12445            .write(ObjectKind::Blob, b"a")
12446            .expect("blob");
12447        let b = Odb::new(temp.path())
12448            .write(ObjectKind::Blob, b"b")
12449            .expect("blob");
12450        let mut left = Index::new();
12451        let mut right = Index::new();
12452        for i in 0..50 {
12453            left.add_or_replace(index_entry(&format!("same{i:02}.txt"), MODE_REGULAR, a));
12454            right.add_or_replace(index_entry(&format!("same{i:02}.txt"), MODE_REGULAR, a));
12455        }
12456        left.add_or_replace(index_entry("dir/changed.txt", MODE_REGULAR, a));
12457        left.add_or_replace(index_entry("dir/kept.txt", MODE_REGULAR, a));
12458        left.add_or_replace(index_entry("gone/x.txt", MODE_REGULAR, a));
12459        left.add_or_replace(index_entry("mode.sh", MODE_REGULAR, a));
12460        left.add_or_replace(index_entry("swap", MODE_REGULAR, a));
12461        right.add_or_replace(index_entry("dir/changed.txt", MODE_REGULAR, b));
12462        right.add_or_replace(index_entry("dir/kept.txt", MODE_REGULAR, a));
12463        right.add_or_replace(index_entry("mode.sh", MODE_EXECUTABLE, a));
12464        right.add_or_replace(index_entry("new/y.txt", MODE_REGULAR, b));
12465        right.add_or_replace(index_entry("swap/inner.txt", MODE_REGULAR, b));
12466        let setup = Odb::new(temp.path());
12467        let old_tree = write_tree_from_index(&setup, &left, "").expect("old");
12468        let new_tree = write_tree_from_index(&setup, &right, "").expect("new");
12469
12470        let raw = diff_trees(&Odb::new(temp.path()), Some(&old_tree), Some(&new_tree), "")
12471            .expect("raw diff");
12472        let changed: Vec<(DiffStatus, &str)> = raw.iter().map(|e| (e.status, e.path())).collect();
12473        assert_eq!(
12474            changed,
12475            vec![
12476                (DiffStatus::Modified, "dir/changed.txt"),
12477                (DiffStatus::Deleted, "gone/x.txt"),
12478                (DiffStatus::Modified, "mode.sh"),
12479                (DiffStatus::Added, "new/y.txt"),
12480                (DiffStatus::Deleted, "swap"),
12481                (DiffStatus::Added, "swap/inner.txt"),
12482            ]
12483        );
12484
12485        let parsed = Odb::new(temp.path());
12486        parsed.cached_tree_entries(&old_tree).expect("parse old");
12487        parsed.cached_tree_entries(&new_tree).expect("parse new");
12488        assert_eq!(
12489            diff_trees(&parsed, Some(&old_tree), Some(&new_tree), "").expect("parsed diff"),
12490            raw
12491        );
12492
12493        let mixed = Odb::new(temp.path());
12494        mixed.cached_tree_entries(&new_tree).expect("parse new");
12495        assert_eq!(
12496            diff_trees(&mixed, Some(&old_tree), Some(&new_tree), "").expect("mixed diff"),
12497            raw
12498        );
12499        let reparsed = mixed
12500            .cached_tree_entries(&old_tree)
12501            .expect("parse cached raw");
12502        assert_eq!(
12503            reparsed.as_slice(),
12504            crate::objects::parse_tree(&mixed.read(&old_tree).expect("read").data)
12505                .expect("parse")
12506                .as_slice(),
12507            "a tree cached raw by a diff parses to the same entries"
12508        );
12509    }
12510
12511    #[test]
12512    fn diff_trees_reports_a_corrupt_tree() {
12513        let temp = TempDir::new().expect("tempdir");
12514        let odb = Odb::new(temp.path());
12515        let good = write_tree_from_index(&odb, &Index::new(), "").expect("empty tree");
12516        let corrupt = odb
12517            .write(ObjectKind::Tree, b"100644 name-without-oid")
12518            .expect("tree");
12519        let err = diff_trees(&odb, Some(&good), Some(&corrupt), "").expect_err("corrupt");
12520        assert!(
12521            matches!(err, Error::Object(ObjectError::Corrupt { .. })),
12522            "{err:?}"
12523        );
12524    }
12525}
12526
12527#[cfg(test)]
12528mod changed_flags_shared_tests {
12529    use super::git_xdiff;
12530    use crate::line_interner::LineInterner;
12531
12532    /// Cutting identical leading and trailing records out and passing their occurrence counts
12533    /// gives the same changed records as diffing the whole files.
12534    #[test]
12535    fn shared_counts_stand_in_for_records_left_out() {
12536        // `}` is frequent only outside the cut, which makes xdiff discard the middle `}` between
12537        // unique lines instead of matching it.
12538        let mut old: Vec<String> = Vec::new();
12539        let mut new: Vec<String> = Vec::new();
12540        for i in 0..60 {
12541            for side in [&mut old, &mut new] {
12542                side.push(format!("fn f{i}() {{\n"));
12543                side.push("}\n".to_owned());
12544            }
12545        }
12546        old.extend(
12547            [
12548                "u1\n", "u2\n", "u3\n", "u4\n", "}\n", "u5\n", "u6\n", "u7\n",
12549            ]
12550            .map(str::to_owned),
12551        );
12552        new.extend(
12553            [
12554                "v1\n", "v2\n", "v3\n", "v4\n", "}\n", "v5\n", "v6\n", "v7\n",
12555            ]
12556            .map(str::to_owned),
12557        );
12558        for i in 0..40 {
12559            for side in [&mut old, &mut new] {
12560                side.push(format!("fn g{i}() {{\n"));
12561                side.push("}\n".to_owned());
12562            }
12563        }
12564        let old: Vec<&str> = old.iter().map(String::as_str).collect();
12565        let new: Vec<&str> = new.iter().map(String::as_str).collect();
12566        let (head, tail) = (120, 80);
12567
12568        let mut interner = LineInterner::with_capacity(old.len() + new.len());
12569        let ids1 = interner.intern_all(&old);
12570        let ids2 = interner.intern_all(&new);
12571        let (full1, full2) = git_xdiff::changed_flags(&ids1, &ids2, false);
12572
12573        let mut cut = LineInterner::default();
12574        let cut1 = cut.intern_all(&old[head..old.len() - tail]);
12575        let cut2 = cut.intern_all(&new[head..new.len() - tail]);
12576        let mut shared = vec![0u32; cut.len()];
12577        for line in old[..head].iter().chain(&old[old.len() - tail..]) {
12578            if let Some(id) = cut.get(line) {
12579                shared[id as usize] += 1;
12580            }
12581        }
12582        let (part1, part2) =
12583            git_xdiff::changed_flags_with_shared(&cut1, &cut2, &shared, head + tail, false);
12584        assert_eq!(part1, full1[head..old.len() - tail]);
12585        assert_eq!(part2, full2[head..new.len() - tail]);
12586        assert!(full1[..head]
12587            .iter()
12588            .chain(&full1[old.len() - tail..])
12589            .all(|c| !c));
12590        // Like `git diff`, the whole middle is one change: the `}` among unique lines is
12591        // discarded because it is frequent in the whole file.
12592        assert!(full1[head..old.len() - tail].iter().all(|&c| c));
12593        let (alone1, _) = git_xdiff::changed_flags(&cut1, &cut2, false);
12594        assert!(
12595            !alone1[4],
12596            "counted alone, the cut keeps the `}}` as a match"
12597        );
12598    }
12599}
12600
12601#[cfg(test)]
12602mod gitlink_tree_worktree_tests {
12603    use std::fs;
12604
12605    use crate::index::{Index, IndexEntry, MODE_GITLINK};
12606    use crate::objects::{ObjectId, ObjectKind};
12607    use crate::repo::init_repository;
12608
12609    use super::diff_tree_to_worktree;
12610
12611    fn encode_tree_gitlink(name: &str, oid: &ObjectId) -> Vec<u8> {
12612        let mut data = format!("160000 {name}\0").into_bytes();
12613        data.extend_from_slice(oid.as_bytes());
12614        data
12615    }
12616
12617    #[test]
12618    fn unpopulated_gitlink_matching_index_produces_no_diff_entry() {
12619        let dir = tempfile::tempdir().expect("tempdir");
12620        let work_tree = dir.path();
12621        fs::create_dir(work_tree.join("sub")).expect("sub placeholder");
12622
12623        let repo = init_repository(
12624            work_tree,
12625            false,
12626            "main",
12627            None,
12628            crate::RefStorageFormat::Files,
12629        )
12630        .expect("init");
12631        let gitlink: ObjectId = "855827c583bc30645ba427885caa40c5b81764d2"
12632            .parse()
12633            .expect("gitlink oid");
12634        let tree_oid = repo
12635            .odb()
12636            .write(ObjectKind::Tree, &encode_tree_gitlink("sub", &gitlink))
12637            .expect("write tree");
12638
12639        let path = b"sub".to_vec();
12640        let mut index = Index::new();
12641        index.add_or_replace(IndexEntry {
12642            ctime_sec: 0,
12643            ctime_nsec: 0,
12644            mtime_sec: 0,
12645            mtime_nsec: 0,
12646            dev: 0,
12647            ino: 0,
12648            mode: MODE_GITLINK,
12649            uid: 0,
12650            gid: 0,
12651            size: 0,
12652            oid: gitlink,
12653            flags: (path.len().min(0xfff)) as u16,
12654            flags_extended: None,
12655            path,
12656            base_index_pos: 0,
12657        });
12658
12659        let entries = diff_tree_to_worktree(&repo.odb(), Some(&tree_oid), work_tree, &index)
12660            .expect("diff_tree_to_worktree");
12661        assert!(
12662            entries.is_empty(),
12663            "clean uninitialized gitlink should not appear in tree→worktree diff: {entries:?}"
12664        );
12665    }
12666
12667    #[test]
12668    fn unpopulated_gitlink_with_stray_file_produces_no_diff_entry() {
12669        let dir = tempfile::tempdir().expect("tempdir");
12670        let work_tree = dir.path();
12671        let sub = work_tree.join("sub");
12672        fs::create_dir(&sub).expect("sub placeholder");
12673        fs::write(sub.join("x.txt"), b"stray\n").expect("stray file");
12674
12675        let repo = init_repository(
12676            work_tree,
12677            false,
12678            "main",
12679            None,
12680            crate::RefStorageFormat::Files,
12681        )
12682        .expect("init");
12683        let gitlink: ObjectId = "855827c583bc30645ba427885caa40c5b81764d2"
12684            .parse()
12685            .expect("gitlink oid");
12686        let tree_oid = repo
12687            .odb()
12688            .write(ObjectKind::Tree, &encode_tree_gitlink("sub", &gitlink))
12689            .expect("write tree");
12690
12691        let path = b"sub".to_vec();
12692        let mut index = Index::new();
12693        index.add_or_replace(IndexEntry {
12694            ctime_sec: 0,
12695            ctime_nsec: 0,
12696            mtime_sec: 0,
12697            mtime_nsec: 0,
12698            dev: 0,
12699            ino: 0,
12700            mode: MODE_GITLINK,
12701            uid: 0,
12702            gid: 0,
12703            size: 0,
12704            oid: gitlink,
12705            flags: (path.len().min(0xfff)) as u16,
12706            flags_extended: None,
12707            path,
12708            base_index_pos: 0,
12709        });
12710
12711        let tree_entries = diff_tree_to_worktree(&repo.odb(), Some(&tree_oid), work_tree, &index)
12712            .expect("diff_tree_to_worktree");
12713        assert!(
12714            tree_entries.is_empty(),
12715            "uninitialized gitlink with stray files should not diff: {tree_entries:?}"
12716        );
12717
12718        let wt_entries =
12719            super::diff_index_to_worktree(&repo.odb(), &mut index.clone(), work_tree, false, true)
12720                .expect("diff_index_to_worktree");
12721        assert!(
12722            wt_entries.is_empty(),
12723            "uninitialized gitlink with stray files should not diff: {wt_entries:?}"
12724        );
12725
12726        let ie = index.entries()[0].clone();
12727        assert!(
12728            !super::worktree_differs_from_index_entry(
12729                repo.odb(),
12730                repo.git_dir(),
12731                work_tree,
12732                &ie,
12733                false,
12734                None,
12735                None,
12736            )
12737            .expect("differs"),
12738            "stray files in uninitialized submodule should not differ from index"
12739        );
12740    }
12741
12742    #[cfg_attr(
12743        not(feature = "full-tests"),
12744        ignore = "starts system git or is heavy; run with --features full-tests"
12745    )]
12746    #[test]
12747    fn untracked_only_in_checked_out_submodule_produces_no_diff_entry() {
12748        use std::process::Command;
12749
12750        let dir = tempfile::tempdir().expect("tempdir");
12751        let work_tree = dir.path();
12752        fs::create_dir(work_tree.join("sub")).expect("sub dir");
12753
12754        let repo = init_repository(
12755            work_tree,
12756            false,
12757            "main",
12758            None,
12759            crate::RefStorageFormat::Files,
12760        )
12761        .expect("init");
12762        let sub = work_tree.join("sub");
12763        for args in [
12764            &["init", "-q"][..],
12765            &["config", "user.name", "T"][..],
12766            &["config", "user.email", "t@e.com"][..],
12767        ] {
12768            let status = Command::new("git")
12769                .current_dir(&sub)
12770                .args(args)
12771                .status()
12772                .expect("git");
12773            assert!(status.success(), "git {args:?} failed");
12774        }
12775        fs::write(sub.join("tracked.txt"), b"v1\n").expect("write");
12776        for args in [&["add", "tracked.txt"][..], &["commit", "-qm", "sub"][..]] {
12777            let status = Command::new("git")
12778                .current_dir(&sub)
12779                .args(args)
12780                .status()
12781                .expect("git");
12782            assert!(status.success());
12783        }
12784        let head_out = Command::new("git")
12785            .current_dir(&sub)
12786            .args(["rev-parse", "HEAD"])
12787            .output()
12788            .expect("rev-parse");
12789        let gitlink = std::str::from_utf8(&head_out.stdout)
12790            .expect("utf8")
12791            .trim()
12792            .parse::<ObjectId>()
12793            .expect("oid");
12794
12795        let tree_oid = repo
12796            .odb()
12797            .write(ObjectKind::Tree, &encode_tree_gitlink("sub", &gitlink))
12798            .expect("tree");
12799        let path = b"sub".to_vec();
12800        let mut index = Index::new();
12801        index.add_or_replace(IndexEntry {
12802            ctime_sec: 0,
12803            ctime_nsec: 0,
12804            mtime_sec: 0,
12805            mtime_nsec: 0,
12806            dev: 0,
12807            ino: 0,
12808            mode: MODE_GITLINK,
12809            uid: 0,
12810            gid: 0,
12811            size: 0,
12812            oid: gitlink,
12813            flags: (path.len().min(0xfff)) as u16,
12814            flags_extended: None,
12815            path,
12816            base_index_pos: 0,
12817        });
12818
12819        fs::write(sub.join("untracked.txt"), b"extra\n").expect("untracked");
12820
12821        let entries = diff_tree_to_worktree(&repo.odb(), Some(&tree_oid), work_tree, &index)
12822            .expect("diff_tree_to_worktree");
12823        assert!(
12824            entries.is_empty(),
12825            "untracked-only submodule dirt must not appear in default tree→worktree diff: {entries:?}"
12826        );
12827    }
12828}
12829
12830#[cfg(test)]
12831mod racy_index_mtime_diff_tests {
12832    use super::*;
12833    use crate::index::{entry_from_metadata, Index, MODE_REGULAR};
12834    use crate::objects::{HashAlgo, ObjectKind};
12835    use filetime::FileTime;
12836    use std::fs;
12837    use std::path::PathBuf;
12838
12839    const INDEX_MTIME: (u32, u32) = (1_700_000_000, 123_456_789);
12840
12841    fn pin_mtime(path: &Path, sec: u32, nsec: u32) {
12842        filetime::set_file_mtime(path, FileTime::from_unix_time(i64::from(sec), nsec))
12843            .expect("set mtime");
12844    }
12845
12846    fn minimal_sha256_repo(work_tree: &Path) -> (PathBuf, Odb) {
12847        let git_dir = work_tree.join(".git");
12848        fs::create_dir_all(git_dir.join("objects")).expect("objects dir");
12849        fs::write(
12850            git_dir.join("config"),
12851            "[extensions]\n\tobjectformat = sha256\n",
12852        )
12853        .expect("config");
12854        let odb = Odb::new(&git_dir.join("objects")).with_config_git_dir(git_dir.clone());
12855        (git_dir, odb)
12856    }
12857
12858    #[test]
12859    fn detects_same_size_change_when_entry_mtime_matches_index_mtime() {
12860        let td = tempfile::tempdir().expect("tempdir");
12861        let wt = td.path();
12862        let content_a = b"same length!!";
12863        let content_b = b"same length??";
12864        assert_eq!(content_a.len(), content_b.len());
12865
12866        fs::write(wt.join("f.txt"), content_a).expect("write");
12867        pin_mtime(&wt.join("f.txt"), INDEX_MTIME.0, INDEX_MTIME.1);
12868        let meta = fs::symlink_metadata(wt.join("f.txt")).expect("stat");
12869        let git_dir = wt.join(".git");
12870        fs::create_dir_all(git_dir.join("objects")).expect("objects dir");
12871        let odb = Odb::new(&git_dir.join("objects"));
12872        let oid_a = odb.hash(ObjectKind::Blob, content_a).expect("hash");
12873        let entry = entry_from_metadata(&meta, b"f.txt", oid_a, MODE_REGULAR);
12874
12875        let mut index = Index::new();
12876        index.source_mtime = Some(INDEX_MTIME);
12877        index.entries.push(entry);
12878
12879        fs::write(wt.join("f.txt"), content_b).expect("rewrite");
12880        pin_mtime(&wt.join("f.txt"), INDEX_MTIME.0, INDEX_MTIME.1);
12881
12882        let diff = diff_index_to_worktree(&odb, &mut index, wt, false, false).expect("diff");
12883        assert_eq!(
12884            diff.len(),
12885            1,
12886            "racy entry must be re-hashed, not stat-trusted"
12887        );
12888        assert_eq!(diff[0].path(), "f.txt");
12889    }
12890
12891    #[test]
12892    #[cfg(unix)]
12893    fn non_utf8_symlink_clean_racy_equal_mtime_reports_no_diff() {
12894        use crate::index::MODE_SYMLINK;
12895        use std::os::unix::ffi::OsStrExt as _;
12896        use std::os::unix::fs::symlink;
12897
12898        let td = tempfile::tempdir().expect("tempdir");
12899        let wt = td.path();
12900        let target = std::ffi::OsStr::from_bytes(b"bad-\xff-target");
12901        let link_path = wt.join("link");
12902        symlink(target, &link_path).expect("symlink");
12903
12904        let ft = FileTime::from_unix_time(i64::from(INDEX_MTIME.0), INDEX_MTIME.1);
12905        filetime::set_symlink_file_times(&link_path, ft, ft).expect("set symlink mtime");
12906        let meta = fs::symlink_metadata(&link_path).expect("stat");
12907        let git_dir = wt.join(".git");
12908        fs::create_dir_all(git_dir.join("objects")).expect("objects dir");
12909        let odb = Odb::new(&git_dir.join("objects"));
12910        let oid = odb.hash(ObjectKind::Blob, target.as_bytes()).expect("hash");
12911        let entry = entry_from_metadata(&meta, b"link", oid, MODE_SYMLINK);
12912
12913        let mut index = Index::new();
12914        index.source_mtime = Some(INDEX_MTIME);
12915        index.entries.push(entry);
12916
12917        let diff = diff_index_to_worktree(&odb, &mut index, wt, false, false).expect("diff");
12918        assert!(
12919            diff.is_empty(),
12920            "clean non-UTF-8 symlink must not be reported modified: {diff:?}"
12921        );
12922    }
12923
12924    #[test]
12925    fn sha256_clean_racy_equal_mtime_reports_no_diff() {
12926        let td = tempfile::tempdir().expect("tempdir");
12927        let wt = td.path();
12928        let (_git_dir, odb) = minimal_sha256_repo(wt);
12929        assert_eq!(odb.hash_algo(), HashAlgo::Sha256);
12930
12931        let bytes = b"sha256 width!!";
12932        fs::write(wt.join("f.txt"), bytes).expect("write");
12933        pin_mtime(&wt.join("f.txt"), INDEX_MTIME.0, INDEX_MTIME.1);
12934        let meta = fs::symlink_metadata(wt.join("f.txt")).expect("stat");
12935        let oid = odb.hash(ObjectKind::Blob, bytes).expect("hash");
12936        let entry = entry_from_metadata(&meta, b"f.txt", oid, MODE_REGULAR);
12937
12938        let mut index = Index::new();
12939        index.hash_algo = HashAlgo::Sha256;
12940        index.source_mtime = Some(INDEX_MTIME);
12941        index.entries.push(entry);
12942
12943        let diff = diff_index_to_worktree(&odb, &mut index, wt, false, false).expect("diff");
12944        assert!(
12945            diff.is_empty(),
12946            "clean SHA-256 racy entry must not be reported modified: {diff:?}"
12947        );
12948    }
12949
12950    /// Racy entries are hashed with their own path's attributes even when the paths
12951    /// alternate between attribute sets: CRLF files clean to LF, binary files hash as is.
12952    #[test]
12953    fn racy_entries_hash_with_their_own_attributes() {
12954        let td = tempfile::tempdir().expect("tempdir");
12955        let wt = td.path();
12956        let git_dir = wt.join(".git");
12957        fs::create_dir_all(git_dir.join("objects")).expect("objects dir");
12958        fs::write(git_dir.join("config"), "[core]\n").expect("config");
12959        fs::write(wt.join(".gitattributes"), "*.crlf eol=crlf\n*.bin -text\n").expect("attrs");
12960        let odb = Odb::new(&git_dir.join("objects")).with_config_git_dir(git_dir.clone());
12961
12962        let files: [(&str, &[u8], &[u8]); 5] = [
12963            ("a.crlf", b"one\r\ntwo\r\n", b"one\ntwo\n"),
12964            ("b.bin", b"raw\r\nbytes\r\n", b"raw\r\nbytes\r\n"),
12965            ("c.crlf", b"three\r\n", b"three\n"),
12966            ("d.txt", b"plain\n", b"plain\n"),
12967            ("e.bin", b"more\r\n", b"more\r\n"),
12968        ];
12969        let mut index = Index::new();
12970        index.source_mtime = Some(INDEX_MTIME);
12971        for (path, on_disk, blob) in files {
12972            let file = wt.join(path);
12973            fs::write(&file, on_disk).expect("write");
12974            pin_mtime(&file, INDEX_MTIME.0, INDEX_MTIME.1);
12975            let meta = fs::symlink_metadata(&file).expect("stat");
12976            let oid = odb.hash(ObjectKind::Blob, blob).expect("hash");
12977            index.entries.push(entry_from_metadata(
12978                &meta,
12979                path.as_bytes(),
12980                oid,
12981                MODE_REGULAR,
12982            ));
12983        }
12984
12985        let status_diff = |index: &mut Index| {
12986            diff_index_to_worktree_with_options(
12987                &odb,
12988                index,
12989                wt,
12990                DiffIndexToWorktreeOptions {
12991                    index_mtime: Some(INDEX_MTIME),
12992                    repository_git_dir: Some(git_dir.clone()),
12993                    refresh_index_stat_in_pass: true,
12994                    for_status: true,
12995                    ..Default::default()
12996                },
12997            )
12998            .expect("diff")
12999            .0
13000        };
13001        let diff = status_diff(&mut index.clone());
13002        assert!(diff.is_empty(), "clean racy entries reported: {diff:?}");
13003
13004        fs::write(wt.join("c.crlf"), b"thrEe\r\n").expect("rewrite");
13005        pin_mtime(&wt.join("c.crlf"), INDEX_MTIME.0, INDEX_MTIME.1);
13006        let diff = status_diff(&mut index);
13007        let paths: Vec<_> = diff.iter().map(DiffEntry::path).collect();
13008        assert_eq!(paths, ["c.crlf"]);
13009    }
13010}
13011
13012#[cfg(test)]
13013mod smudge_racily_clean_tests {
13014    use super::*;
13015    use crate::config::ConfigSet;
13016    use crate::crlf::{self, FileAttrs};
13017    use crate::index::{entry_from_metadata, Index, MODE_REGULAR};
13018    use crate::objects::{HashAlgo, ObjectKind};
13019    use filetime::FileTime;
13020    use std::fs;
13021    use std::path::PathBuf;
13022
13023    const INDEX_MTIME_PIN: (u32, u32) = (1_700_000_000, 123_456_789);
13024
13025    fn mtime_from_metadata(meta: &fs::Metadata) -> (u32, u32) {
13026        #[cfg(unix)]
13027        {
13028            use std::os::unix::fs::MetadataExt;
13029            (meta.mtime() as u32, meta.mtime_nsec() as u32)
13030        }
13031        #[cfg(not(unix))]
13032        {
13033            use std::time::UNIX_EPOCH;
13034            let mtime = meta
13035                .modified()
13036                .ok()
13037                .and_then(|t| t.duration_since(UNIX_EPOCH).ok())
13038                .unwrap_or_default();
13039            (mtime.as_secs() as u32, mtime.subsec_nanos())
13040        }
13041    }
13042
13043    /// Pin mtime then read back the value the index would store (NTFS quantizes subseconds).
13044    fn pin_mtime_and_sample(path: &Path, sec: u32, nsec: u32) -> (u32, u32) {
13045        filetime::set_file_mtime(path, FileTime::from_unix_time(i64::from(sec), nsec))
13046            .expect("set mtime");
13047        let meta = fs::symlink_metadata(path).expect("stat file");
13048        mtime_from_metadata(&meta)
13049    }
13050
13051    /// Sample the on-disk index mtime used in racy-git tests (matches filesystem quantization).
13052    fn setup_index_mtime(work_tree: &Path) -> (u32, u32) {
13053        let probe = work_tree.join(".grit-index-mtime-probe");
13054        fs::write(&probe, b"").expect("probe file");
13055        pin_mtime_and_sample(&probe, INDEX_MTIME_PIN.0, INDEX_MTIME_PIN.1)
13056    }
13057
13058    fn minimal_repo(work_tree: &Path, config: &str) -> (PathBuf, Odb) {
13059        let git_dir = work_tree.join(".git");
13060        fs::create_dir_all(git_dir.join("objects")).expect("objects dir");
13061        fs::write(git_dir.join("config"), config).expect("config");
13062        let odb = Odb::new(&git_dir.join("objects")).with_config_git_dir(git_dir.clone());
13063        (git_dir, odb)
13064    }
13065
13066    fn staged_entry_for_file(
13067        odb: &Odb,
13068        work_tree: &Path,
13069        rel: &str,
13070        bytes: &[u8],
13071        mtime: (u32, u32),
13072    ) -> IndexEntry {
13073        let path = work_tree.join(rel);
13074        fs::write(&path, bytes).expect("write file");
13075        pin_mtime_and_sample(&path, mtime.0, mtime.1);
13076        let meta = fs::symlink_metadata(&path).expect("stat file");
13077        let oid = odb.hash(ObjectKind::Blob, bytes).expect("hash");
13078        entry_from_metadata(&meta, rel.as_bytes(), oid, MODE_REGULAR)
13079    }
13080
13081    fn staged_entry_with_oid(
13082        work_tree: &Path,
13083        rel: &str,
13084        worktree_bytes: &[u8],
13085        oid: crate::objects::ObjectId,
13086        mtime: (u32, u32),
13087    ) -> IndexEntry {
13088        let path = work_tree.join(rel);
13089        fs::write(&path, worktree_bytes).expect("write file");
13090        pin_mtime_and_sample(&path, mtime.0, mtime.1);
13091        let meta = fs::symlink_metadata(&path).expect("stat file");
13092        entry_from_metadata(&meta, rel.as_bytes(), oid, MODE_REGULAR)
13093    }
13094
13095    #[test]
13096    fn index_stat_cache_trustworthy_rejects_unknown_mtime() {
13097        assert!(!super::index_stat_cache_trustworthy(None));
13098        assert!(!super::index_stat_cache_trustworthy(Some((0, 0))));
13099        assert!(super::index_stat_cache_trustworthy(Some((1, 0))));
13100    }
13101
13102    #[test]
13103    fn index_entry_skips_content_hash_after_stat_match_when_trusted_and_not_racy() {
13104        use crate::index::StatMatchPolicy;
13105        let td = tempfile::tempdir().expect("tempdir");
13106        let wt = td.path();
13107        let (git_dir, odb) = minimal_repo(wt, "[core]\n\tfilemode = true\n");
13108        let rel = "trusted.txt";
13109        let file_mtime = (1_700_000_000, 0);
13110        let mut entry = staged_entry_for_file(&odb, wt, rel, b"payload\n", file_mtime);
13111        let index_mtime = (file_mtime.0 + 1, 0);
13112        let policy = StatMatchPolicy::from_config(None);
13113        assert!(
13114            super::index_entry_skips_worktree_content_hash_after_stat_match(
13115                &entry,
13116                Some(index_mtime),
13117                policy,
13118            )
13119        );
13120        entry.size = 0;
13121        entry.mtime_sec = 0;
13122        entry.mtime_nsec = 0;
13123        assert!(
13124            !super::index_entry_skips_worktree_content_hash_after_stat_match(
13125                &entry,
13126                Some(index_mtime),
13127                policy,
13128            )
13129        );
13130        let _ = git_dir;
13131    }
13132
13133    #[cfg(unix)]
13134    #[test]
13135    fn symlink_dir_cache_detects_symlinked_ancestors_across_threads() {
13136        let td = tempfile::tempdir().expect("tempdir");
13137        let wt = td.path();
13138        fs::create_dir_all(wt.join("real/deep")).expect("mkdir");
13139        fs::create_dir_all(wt.join("target/deep")).expect("mkdir");
13140        std::os::unix::fs::symlink(wt.join("target"), wt.join("linked")).expect("symlink");
13141        std::thread::scope(|scope| {
13142            for _ in 0..4 {
13143                scope.spawn(|| {
13144                    let mut cache = super::SymlinkDirCache::default();
13145                    assert!(!cache.has_symlink_in_path_bytes(wt, b"top.txt", false, false));
13146                    assert!(!cache.has_symlink_in_path_bytes(wt, b"linked", false, false));
13147                    assert!(!cache.has_symlink_in_path_bytes(wt, b"real/deep/f", false, false));
13148                    assert!(cache.has_symlink_in_path_bytes(wt, b"linked/f", false, false));
13149                    assert!(cache.has_symlink_in_path_bytes(wt, b"linked/deep/f", false, false));
13150                    assert!(!cache.has_symlink_in_path_bytes(wt, b"\xff\xfe", false, false));
13151                });
13152            }
13153        });
13154    }
13155
13156    #[cfg(unix)]
13157    #[test]
13158    fn symlink_dir_cache_accepts_a_work_tree_reached_through_a_symlink() {
13159        let td = tempfile::tempdir().expect("tempdir");
13160        let real = td.path().join("real");
13161        fs::create_dir_all(real.join("a/b/c")).expect("mkdir");
13162        fs::create_dir_all(real.join("t/b/c")).expect("mkdir");
13163        std::os::unix::fs::symlink(real.join("t"), real.join("l")).expect("symlink");
13164        let wt = td.path().join("alias");
13165        std::os::unix::fs::symlink(&real, &wt).expect("symlink");
13166        let mut cache = super::SymlinkDirCache::default();
13167        assert!(!cache.has_symlink_in_path_bytes(&wt, b"a/b/c/f", false, false));
13168        assert!(cache.has_symlink_in_path_bytes(&wt, b"l/b/c/f", false, false));
13169        assert!(!cache.has_symlink_in_path_bytes(&real, b"a/b/c/f", false, false));
13170        assert!(cache.has_symlink_in_path_bytes(&real, b"l/b/c/f", false, false));
13171    }
13172
13173    #[cfg(all(target_os = "linux", target_pointer_width = "64"))]
13174    #[test]
13175    fn real_dir_chain_confirms_only_symlink_free_directories() {
13176        let td = tempfile::tempdir().expect("tempdir");
13177        let wt = td.path();
13178        fs::create_dir_all(wt.join("a/b/c")).expect("mkdir");
13179        fs::create_dir_all(wt.join("t/c")).expect("mkdir");
13180        fs::write(wt.join("a/file"), b"f").expect("write");
13181        std::os::unix::fs::symlink(wt.join("t"), wt.join("a/l")).expect("symlink");
13182        let mut chain = crate::worktree_scan::RealDirChain::open(wt);
13183        assert_eq!(chain.work_tree(), wt);
13184        assert!(chain.confirms(b"a"));
13185        assert!(chain.confirms(b"a/b/c"));
13186        assert!(!chain.confirms(b"a/l"));
13187        assert!(!chain.confirms(b"a/l/c"));
13188        assert!(!chain.confirms(b"a/file"));
13189        assert!(!chain.confirms(b"a/missing/c"));
13190        assert!(!chain.confirms(b""));
13191        assert!(!chain.confirms(b"a/\0b"));
13192        assert!(chain.confirms(b"a/b"));
13193    }
13194
13195    #[test]
13196    fn symlink_dir_cache_probes_each_new_directory_once_in_index_order() {
13197        let td = tempfile::tempdir().expect("tempdir");
13198        let wt = td.path();
13199        let depth = 12;
13200        let chains = 20;
13201        let mut paths = Vec::new();
13202        for chain in 0..chains {
13203            let dir: Vec<String> = (0..depth)
13204                .map(|level| format!("c{chain:02}l{level:02}"))
13205                .collect();
13206            let dir = format!("shared/{}", dir.join("/"));
13207            fs::create_dir_all(wt.join(&dir)).expect("mkdir");
13208            paths.extend((0..3).map(|file| format!("{dir}/f{file}")));
13209        }
13210        paths.sort();
13211        let mut cache = super::SymlinkDirCache::default();
13212        for rel in &paths {
13213            assert!(!cache.has_symlink_in_path_bytes(wt, rel.as_bytes(), false, false));
13214        }
13215        // Each chain's directories are new; `shared` and sibling files are already known.
13216        let unique_dirs = 1 + chains * depth;
13217        assert!(
13218            cache.fs_queries <= unique_dirs,
13219            "{} filesystem queries for {unique_dirs} directories",
13220            cache.fs_queries
13221        );
13222        #[cfg(all(target_os = "linux", target_pointer_width = "64"))]
13223        assert!(
13224            cache.fs_queries <= chains + 1,
13225            "one chain walk per new directory: {} queries for {chains} chains",
13226            cache.fs_queries
13227        );
13228    }
13229
13230    #[test]
13231    fn common_dir_prefix_len_stops_at_whole_components() {
13232        use super::{common_dir_prefix_len, path_within_dir};
13233        assert_eq!(common_dir_prefix_len(b"a/b/c", b"a/b/c"), 5);
13234        assert_eq!(common_dir_prefix_len(b"a/b/c", b"a/b"), 3);
13235        assert_eq!(common_dir_prefix_len(b"a/b", b"a/b/c"), 3);
13236        assert_eq!(common_dir_prefix_len(b"a/bc/d", b"a/b"), 1);
13237        assert_eq!(common_dir_prefix_len(b"a/b", b"a/bc/d"), 1);
13238        assert_eq!(common_dir_prefix_len(b"ab", b"a"), 0);
13239        assert_eq!(common_dir_prefix_len(b"a/b", b""), 0);
13240        assert!(path_within_dir(b"a/b", b"a/b"));
13241        assert!(path_within_dir(b"a/b/c", b"a/b"));
13242        assert!(!path_within_dir(b"a/bc", b"a/b"));
13243        assert!(!path_within_dir(b"a", b"a/b"));
13244    }
13245
13246    /// Answer the ancestor-symlink question without any caching, one probe per ancestor.
13247    #[cfg(unix)]
13248    fn naive_has_symlink_ancestor(wt: &Path, rel: &str) -> bool {
13249        let Some((dir, _)) = rel.rsplit_once('/') else {
13250            return false;
13251        };
13252        let mut prefix = wt.to_path_buf();
13253        dir.split('/').any(|component| {
13254            prefix.push(component);
13255            fs::symlink_metadata(&prefix).is_ok_and(|m| m.file_type().is_symlink())
13256        })
13257    }
13258
13259    #[cfg(unix)]
13260    #[test]
13261    fn symlink_dir_cache_matches_uncached_probe_in_any_query_order() {
13262        let td = tempfile::tempdir().expect("tempdir");
13263        let wt = td.path();
13264        fs::create_dir_all(wt.join("a/b/c/d")).expect("mkdir");
13265        fs::create_dir_all(wt.join("a/bb/c")).expect("mkdir");
13266        fs::create_dir_all(wt.join("a/b2")).expect("mkdir");
13267        fs::create_dir_all(wt.join("t/c/d")).expect("mkdir");
13268        fs::create_dir_all(wt.join("x/y")).expect("mkdir");
13269        fs::write(wt.join("a/file"), b"f").expect("write");
13270        fs::write(wt.join("x/y/plain"), b"f").expect("write");
13271        std::os::unix::fs::symlink(wt.join("t"), wt.join("a/b/link")).expect("symlink");
13272        std::os::unix::fs::symlink(wt.join("t"), wt.join("x/l")).expect("symlink");
13273        std::os::unix::fs::symlink(wt.join("t"), wt.join("top")).expect("symlink");
13274
13275        let paths = [
13276            "a/b/c/d/f",
13277            "a/b/c/f",
13278            "a/b/f",
13279            "a/b/link/f",
13280            "a/b/link/c/d/f",
13281            "a/b/linker/f",
13282            "a/b2/f",
13283            "a/bb/c/f",
13284            "a/file/under/f",
13285            "a/missing/deeper/f",
13286            "a/missing/f",
13287            "a/f",
13288            "f",
13289            "top/c/f",
13290            "top/f",
13291            "topper/f",
13292            "x/l/c/d/f",
13293            "x/y/f",
13294            "x/y/plain/f",
13295        ];
13296        let mut orders: Vec<Vec<&str>> = vec![paths.to_vec()];
13297        orders.push(paths.iter().rev().copied().collect());
13298        orders.push(
13299            paths
13300                .iter()
13301                .step_by(2)
13302                .chain(paths.iter().skip(1).step_by(2))
13303                .copied()
13304                .collect(),
13305        );
13306        orders.push(paths.iter().flat_map(|p| [*p, *p]).collect());
13307        for order in orders {
13308            let mut cache = super::SymlinkDirCache::default();
13309            for rel in order {
13310                assert_eq!(
13311                    cache.has_symlink_in_path_bytes(wt, rel.as_bytes(), false, false),
13312                    naive_has_symlink_ancestor(wt, rel),
13313                    "ancestor symlink answer for {rel}"
13314                );
13315            }
13316        }
13317    }
13318
13319    #[test]
13320    fn content_hash_skip_trusts_empty_blob_but_not_smudged_size() {
13321        use crate::index::StatMatchPolicy;
13322        let td = tempfile::tempdir().expect("tempdir");
13323        let wt = td.path();
13324        let (_git_dir, odb) = minimal_repo(wt, "[core]\n\tfilemode = false\n");
13325        let file_mtime = (1_700_000_000, 0);
13326        let index_mtime = Some((file_mtime.0 + 1, 0));
13327        let policy = StatMatchPolicy::from_config(None);
13328
13329        let empty = staged_entry_for_file(&odb, wt, "empty.txt", b"", file_mtime);
13330        assert_eq!(empty.size, 0);
13331        assert!(
13332            super::index_entry_skips_worktree_content_hash_after_stat_match(
13333                &empty,
13334                index_mtime,
13335                policy,
13336            )
13337        );
13338
13339        let smudged_oid = odb.hash(ObjectKind::Blob, b"abc\n").expect("hash");
13340        let smudged = staged_entry_with_oid(wt, "smudged.txt", b"", smudged_oid, file_mtime);
13341        assert_eq!(smudged.size, 0);
13342        assert!(
13343            !super::index_entry_skips_worktree_content_hash_after_stat_match(
13344                &smudged,
13345                index_mtime,
13346                policy,
13347            )
13348        );
13349    }
13350
13351    #[test]
13352    fn status_diff_reports_smudged_entry_whose_file_became_empty() {
13353        let td = tempfile::tempdir().expect("tempdir");
13354        let wt = td.path();
13355        let (git_dir, odb) = minimal_repo(wt, "[core]\n\tfilemode = false\n");
13356        let oid = odb.write(ObjectKind::Blob, b"abc\n").expect("write blob");
13357        let file_mtime = (1_700_000_000, 0);
13358        let mut index = Index::new();
13359        index.entries.push(staged_entry_with_oid(
13360            wt,
13361            "smudged.txt",
13362            b"",
13363            oid,
13364            file_mtime,
13365        ));
13366        let (entries, _) = super::diff_index_to_worktree_with_options(
13367            &odb,
13368            &mut index,
13369            wt,
13370            super::DiffIndexToWorktreeOptions {
13371                repository_git_dir: Some(git_dir),
13372                index_mtime: Some((file_mtime.0 + 1, 0)),
13373                for_status: true,
13374                stat_parallel_threads: Some(1),
13375                ..Default::default()
13376            },
13377        )
13378        .expect("diff");
13379        assert_eq!(entries.len(), 1, "{entries:?}");
13380        assert_eq!(entries[0].status, DiffStatus::Modified);
13381    }
13382
13383    #[test]
13384    fn status_diff_verifies_content_when_index_stat_cache_unset() {
13385        let td = tempfile::tempdir().expect("tempdir");
13386        let wt = td.path();
13387        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13388        let rel = "unset-stat.txt";
13389        let bytes = b"same bytes\n";
13390        let oid = odb.hash(ObjectKind::Blob, bytes).expect("hash");
13391        fs::write(wt.join(rel), bytes).expect("write");
13392        let mut index = Index::new();
13393        let mut entry = staged_entry_for_file(&odb, wt, rel, bytes, (100, 0));
13394        entry.size = 0;
13395        entry.mtime_sec = 0;
13396        entry.mtime_nsec = 0;
13397        entry.oid = oid;
13398        index.entries.push(entry);
13399        let (entries, _) = super::diff_index_to_worktree_with_options(
13400            &odb,
13401            &mut index,
13402            wt,
13403            super::DiffIndexToWorktreeOptions {
13404                repository_git_dir: Some(git_dir),
13405                for_status: true,
13406                ..Default::default()
13407            },
13408        )
13409        .expect("diff");
13410        assert!(
13411            entries.is_empty(),
13412            "unset stat must not size-mismatch a matching worktree: {entries:?}"
13413        );
13414    }
13415
13416    #[cfg_attr(
13417        not(feature = "full-tests"),
13418        ignore = "starts system git or is heavy; run with --features full-tests"
13419    )]
13420    #[test]
13421    fn status_reports_modified_when_ctime_differs_but_mtime_sec_and_size_match_non_racy() {
13422        let td = tempfile::tempdir().expect("tempdir");
13423        let wt = td.path();
13424        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13425        let rel = "tracked.txt";
13426        let original = b"original\n";
13427        let modified = b"modified\n";
13428        assert_eq!(original.len(), modified.len());
13429
13430        let entry_mtime = (1_700_000_000u32, 0u32);
13431        let index_file_mtime = (1_700_000_100u32, 0u32);
13432
13433        std::process::Command::new("git")
13434            .args(["-C", &wt.to_string_lossy(), "init", "-q"])
13435            .status()
13436            .expect("git init");
13437        std::process::Command::new("git")
13438            .args([
13439                "-C",
13440                &wt.to_string_lossy(),
13441                "config",
13442                "user.email",
13443                "t@example.com",
13444            ])
13445            .status()
13446            .expect("git config email");
13447        std::process::Command::new("git")
13448            .args(["-C", &wt.to_string_lossy(), "config", "user.name", "T"])
13449            .status()
13450            .expect("git config name");
13451
13452        fs::write(wt.join(rel), original).expect("write original");
13453        pin_mtime_and_sample(&wt.join(rel), entry_mtime.0, entry_mtime.1);
13454        std::process::Command::new("git")
13455            .args(["-C", &wt.to_string_lossy(), "add", rel])
13456            .status()
13457            .expect("git add");
13458        std::process::Command::new("git")
13459            .args([
13460                "-C",
13461                &wt.to_string_lossy(),
13462                "commit",
13463                "-qm",
13464                "seed",
13465                "--no-gpg-sign",
13466            ])
13467            .status()
13468            .expect("git commit");
13469
13470        let mut index = Index::new();
13471        index.source_mtime = Some(index_file_mtime);
13472        let staged = staged_entry_for_file(&odb, wt, rel, original, entry_mtime);
13473        let oid_original = staged.oid;
13474        index.entries.push(staged);
13475
13476        fs::write(wt.join(rel), modified).expect("modify worktree");
13477        let git_dirty = std::process::Command::new("git")
13478            .args([
13479                "-C",
13480                &wt.to_string_lossy(),
13481                "--no-optional-locks",
13482                "status",
13483                "--short",
13484            ])
13485            .output()
13486            .expect("git status");
13487        assert!(
13488            git_dirty.status.success(),
13489            "git status failed: {}",
13490            String::from_utf8_lossy(&git_dirty.stderr)
13491        );
13492        assert!(
13493            String::from_utf8_lossy(&git_dirty.stdout).contains("tracked.txt"),
13494            "system git must see modification before mtime pin: {}",
13495            String::from_utf8_lossy(&git_dirty.stdout)
13496        );
13497
13498        pin_mtime_and_sample(&wt.join(rel), entry_mtime.0, entry_mtime.1);
13499
13500        let meta = fs::symlink_metadata(wt.join(rel)).expect("stat");
13501        let ie = &mut index.entries[0];
13502        assert_eq!(ie.oid, oid_original);
13503        if stat_matches(ie, &meta) {
13504            ie.ctime_sec = ie.ctime_sec.wrapping_sub(3600);
13505        }
13506        assert!(
13507            !stat_matches(ie, &meta),
13508            "ctime/inode change must fail full stat match"
13509        );
13510        let stat_policy = crate::index::StatMatchPolicy::default();
13511        assert!(
13512            !entry_is_racy_with_policy(ie, Some(index_file_mtime), stat_policy),
13513            "fixture must be non-racy"
13514        );
13515
13516        let (entries, index_changed) = super::diff_index_to_worktree_with_options(
13517            &odb,
13518            &mut index,
13519            wt,
13520            super::DiffIndexToWorktreeOptions {
13521                index_mtime: Some(index_file_mtime),
13522                repository_git_dir: Some(git_dir),
13523                refresh_index_stat_in_pass: true,
13524                for_status: true,
13525                ..Default::default()
13526            },
13527        )
13528        .expect("diff");
13529        assert_eq!(
13530            entries.len(),
13531            1,
13532            "must report modified, not refresh stat clean: {entries:?}, index_changed={index_changed}"
13533        );
13534        assert_eq!(entries[0].path(), rel);
13535    }
13536
13537    #[test]
13538    fn diff_name_status_skips_big_worktree_read_on_size_mismatch() {
13539        let td = tempfile::tempdir().expect("tempdir");
13540        let wt = td.path();
13541        let (git_dir, odb) = minimal_repo(wt, "[core]\n\tbigFileThreshold = 4k\n");
13542        let rel = "big.bin";
13543        let indexed = vec![b'x'; 100];
13544        let mut index = Index::new();
13545        index
13546            .entries
13547            .push(staged_entry_for_file(&odb, wt, rel, &indexed, (100, 0)));
13548        fs::write(wt.join(rel), vec![b'y'; 5000]).expect("rewrite bigger");
13549        let metrics = odb.hot_path_metrics();
13550        metrics.reset_blob_content_reads();
13551        metrics.set_blob_counting(true);
13552        let (entries, _) = super::diff_index_to_worktree_with_options(
13553            &odb,
13554            &mut index,
13555            wt,
13556            super::DiffIndexToWorktreeOptions {
13557                repository_git_dir: Some(git_dir),
13558                for_status: false,
13559                ..Default::default()
13560            },
13561        )
13562        .expect("diff");
13563        metrics.set_blob_counting(false);
13564        assert_eq!(entries.len(), 1);
13565        assert_eq!(entries[0].status, super::DiffStatus::Modified);
13566        assert_eq!(entries[0].new_oid, super::zero_oid());
13567        assert_eq!(
13568            metrics.blob_content_reads(),
13569            0,
13570            "large size-only drift must not hash or read the worktree"
13571        );
13572    }
13573
13574    #[test]
13575    fn status_diff_skips_hash_when_worktree_size_differs() {
13576        let td = tempfile::tempdir().expect("tempdir");
13577        let wt = td.path();
13578        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13579        let rel = "size-change.txt";
13580        let original = b"short\n";
13581        let mut index = Index::new();
13582        index
13583            .entries
13584            .push(staged_entry_for_file(&odb, wt, rel, original, (100, 0)));
13585        fs::write(wt.join(rel), b"longer content\n").expect("rewrite");
13586        let metrics = odb.hot_path_metrics();
13587        metrics.reset_blob_content_reads();
13588        metrics.set_blob_counting(true);
13589        let (entries, _) = super::diff_index_to_worktree_with_options(
13590            &odb,
13591            &mut index,
13592            wt,
13593            super::DiffIndexToWorktreeOptions {
13594                repository_git_dir: Some(git_dir),
13595                for_status: true,
13596                ..Default::default()
13597            },
13598        )
13599        .expect("diff");
13600        metrics.set_blob_counting(false);
13601        assert_eq!(entries.len(), 1);
13602        assert_eq!(entries[0].status, super::DiffStatus::Modified);
13603        assert_eq!(entries[0].path(), rel);
13604        assert_eq!(entries[0].old_oid, index.entries[0].oid);
13605        assert_eq!(entries[0].new_oid, super::zero_oid());
13606        assert_eq!(
13607            metrics.blob_content_reads(),
13608            0,
13609            "identity conversion: a size change alone proves the file modified"
13610        );
13611    }
13612
13613    #[test]
13614    fn status_diff_hashes_size_change_when_eol_conversion_restores_blob() {
13615        for refresh_index_stat_in_pass in [false, true] {
13616            let td = tempfile::tempdir().expect("tempdir");
13617            let wt = td.path();
13618            let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13619            fs::write(wt.join(".gitattributes"), "*.txt text eol=crlf\n").expect("attrs");
13620            let rel = "n.txt";
13621            let blob = odb
13622                .write(ObjectKind::Blob, b"one\ntwo\n")
13623                .expect("write blob");
13624            let mut index = Index::new();
13625            index.entries.push(staged_entry_with_oid(
13626                wt,
13627                rel,
13628                b"one\r\ntwo\r\n",
13629                blob,
13630                (100, 0),
13631            ));
13632            fs::write(wt.join(rel), b"one\ntwo\n").expect("rewrite as LF");
13633            let (entries, index_changed) = super::diff_index_to_worktree_with_options(
13634                &odb,
13635                &mut index,
13636                wt,
13637                super::DiffIndexToWorktreeOptions {
13638                    index_mtime: Some((200, 0)),
13639                    repository_git_dir: Some(git_dir),
13640                    refresh_index_stat_in_pass,
13641                    for_status: true,
13642                    ..Default::default()
13643                },
13644            )
13645            .expect("diff");
13646            assert!(
13647                entries.is_empty(),
13648                "refresh={refresh_index_stat_in_pass}: LF worktree cleans to the indexed blob: \
13649                 {entries:?}"
13650            );
13651            if refresh_index_stat_in_pass {
13652                assert!(index_changed, "verified entry adopts the new stat");
13653                assert_eq!(index.entries[0].size, 8);
13654            }
13655        }
13656    }
13657
13658    #[test]
13659    fn status_diff_reads_size_changed_converted_file_once() {
13660        for refresh_index_stat_in_pass in [false, true] {
13661            let td = tempfile::tempdir().expect("tempdir");
13662            let wt = td.path();
13663            let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13664            fs::write(wt.join(".gitattributes"), "*.txt text eol=crlf\n").expect("attrs");
13665            let rel = "n.txt";
13666            let blob = odb
13667                .write(ObjectKind::Blob, b"one\ntwo\n")
13668                .expect("write blob");
13669            let mut index = Index::new();
13670            index.entries.push(staged_entry_with_oid(
13671                wt,
13672                rel,
13673                b"one\r\ntwo\r\n",
13674                blob,
13675                (100, 0),
13676            ));
13677            fs::write(wt.join(rel), b"one\ntwo\nthree\n").expect("real edit");
13678            let metrics = odb.hot_path_metrics();
13679            metrics.reset_blob_content_reads();
13680            metrics.set_blob_counting(true);
13681            let (entries, _) = super::diff_index_to_worktree_with_options(
13682                &odb,
13683                &mut index,
13684                wt,
13685                super::DiffIndexToWorktreeOptions {
13686                    index_mtime: Some((200, 0)),
13687                    repository_git_dir: Some(git_dir),
13688                    refresh_index_stat_in_pass,
13689                    for_status: true,
13690                    ..Default::default()
13691                },
13692            )
13693            .expect("diff");
13694            metrics.set_blob_counting(false);
13695            assert_eq!(entries.len(), 1, "refresh={refresh_index_stat_in_pass}");
13696            assert_eq!(entries[0].status, super::DiffStatus::Modified);
13697            assert_eq!(entries[0].new_oid, zero_oid());
13698            assert_eq!(
13699                index.entries[0].size, 10,
13700                "a modified file keeps its stale stat"
13701            );
13702            assert_eq!(
13703                metrics.blob_content_reads(),
13704                1,
13705                "refresh={refresh_index_stat_in_pass}: read once, not again after the refresh"
13706            );
13707        }
13708    }
13709
13710    #[test]
13711    fn stat_matches_ignores_subsecond_nsec_by_default() {
13712        let td = tempfile::tempdir().expect("tempdir");
13713        let wt = td.path();
13714        fs::write(wt.join("f.txt"), b"x\n").expect("write");
13715        pin_mtime_and_sample(&wt.join("f.txt"), 100, 0);
13716        let meta = fs::symlink_metadata(wt.join("f.txt")).expect("stat");
13717        let mut entry = entry_from_metadata(&meta, b"f.txt", zero_oid(), MODE_REGULAR);
13718        entry.mtime_nsec = entry.mtime_nsec.wrapping_add(1_000_000);
13719        assert!(stat_matches(&entry, &meta));
13720    }
13721
13722    #[test]
13723    fn classify_add_up_to_date_when_stat_matches() {
13724        let td = tempfile::tempdir().expect("tempdir");
13725        let wt = td.path();
13726        let (_git_dir, odb) = minimal_repo(wt, "[core]\n");
13727        let bytes = b"hello world\n";
13728        let entry = staged_entry_for_file(&odb, wt, "f.txt", bytes, (100, 0));
13729        let meta = fs::symlink_metadata(wt.join("f.txt")).expect("stat");
13730        let conv = crlf::ConversionConfig::from_config(&ConfigSet::new());
13731        let attrs = FileAttrs::default();
13732        let params = WorktreeAddRefreshParams {
13733            odb: &odb,
13734            ie: &entry,
13735            meta: &meta,
13736            abs_path: &wt.join("f.txt"),
13737            rel_path: "f.txt",
13738            conv: &conv,
13739            file_attrs: &attrs,
13740            index_mtime: Some((200, 0)),
13741            staged_mode: MODE_REGULAR,
13742            filter_process: None,
13743        };
13744        let refresh = classify_worktree_entry_for_add(&params).expect("classify");
13745        assert_eq!(refresh, WorktreeAddRefresh::UpToDate);
13746    }
13747
13748    #[test]
13749    fn classify_add_rehashes_when_index_mtime_unknown_and_stat_matches() {
13750        let td = tempfile::tempdir().expect("tempdir");
13751        let wt = td.path();
13752        let (_git_dir, odb) = minimal_repo(wt, "[core]\n");
13753        let entry = staged_entry_for_file(&odb, wt, "f.txt", b"old content\n", (100, 0));
13754        fs::write(wt.join("f.txt"), b"new content\n").expect("rewrite");
13755        pin_mtime_and_sample(&wt.join("f.txt"), 100, 0);
13756        let meta = fs::symlink_metadata(wt.join("f.txt")).expect("stat");
13757        let conv = crlf::ConversionConfig::from_config(&ConfigSet::new());
13758        let attrs = FileAttrs::default();
13759        let params = WorktreeAddRefreshParams {
13760            odb: &odb,
13761            ie: &entry,
13762            meta: &meta,
13763            abs_path: &wt.join("f.txt"),
13764            rel_path: "f.txt",
13765            conv: &conv,
13766            file_attrs: &attrs,
13767            index_mtime: None,
13768            staged_mode: MODE_REGULAR,
13769            filter_process: None,
13770        };
13771        let refresh = classify_worktree_entry_for_add(&params).expect("classify");
13772        assert_eq!(refresh, WorktreeAddRefresh::NeedsRestage);
13773    }
13774
13775    #[test]
13776    fn classify_add_needs_restaging_when_content_differs() {
13777        let td = tempfile::tempdir().expect("tempdir");
13778        let wt = td.path();
13779        let (_git_dir, odb) = minimal_repo(wt, "[core]\n");
13780        let entry = staged_entry_for_file(&odb, wt, "f.txt", b"old content\n", (100, 0));
13781        fs::write(wt.join("f.txt"), b"new content\n").expect("rewrite");
13782        pin_mtime_and_sample(&wt.join("f.txt"), 101, 0);
13783        let meta = fs::symlink_metadata(wt.join("f.txt")).expect("stat");
13784        let conv = crlf::ConversionConfig::from_config(&ConfigSet::new());
13785        let attrs = FileAttrs::default();
13786        let params = WorktreeAddRefreshParams {
13787            odb: &odb,
13788            ie: &entry,
13789            meta: &meta,
13790            abs_path: &wt.join("f.txt"),
13791            rel_path: "f.txt",
13792            conv: &conv,
13793            file_attrs: &attrs,
13794            index_mtime: Some((200, 0)),
13795            staged_mode: MODE_REGULAR,
13796            filter_process: None,
13797        };
13798        let refresh = classify_worktree_entry_for_add(&params).expect("classify");
13799        assert_eq!(refresh, WorktreeAddRefresh::NeedsRestage);
13800    }
13801
13802    #[test]
13803    fn smudges_racily_clean_modified_entry() {
13804        let td = tempfile::tempdir().expect("tempdir");
13805        let wt = td.path();
13806        let index_mtime = setup_index_mtime(wt);
13807        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13808        let content_a = b"same length!!";
13809        let content_b = b"same length??";
13810        assert_eq!(content_a.len(), content_b.len());
13811
13812        let mut index = Index::new();
13813        let entry = staged_entry_for_file(&odb, wt, "f.txt", content_a, index_mtime);
13814        let stale_oid = entry.oid;
13815
13816        fs::write(wt.join("f.txt"), content_b).expect("rewrite");
13817        pin_mtime_and_sample(&wt.join("f.txt"), index_mtime.0, index_mtime.1);
13818        let meta = fs::symlink_metadata(wt.join("f.txt")).expect("stat");
13819        // Stat was refreshed from the worktree but the blob OID is still stale (racy window).
13820        let entry = entry_from_metadata(&meta, b"f.txt", stale_oid, MODE_REGULAR);
13821        index.entries.push(entry.clone());
13822
13823        assert!(stat_matches(&entry, &meta));
13824        assert!(entry_is_racy(&entry, Some(index_mtime)));
13825
13826        let smudged = smudge_racily_clean_entries(
13827            &odb,
13828            &git_dir,
13829            &mut index,
13830            wt,
13831            Some(index_mtime),
13832            None,
13833            None,
13834        );
13835        assert!(smudged);
13836        assert_eq!(index.entries[0].size, 0);
13837
13838        let diff = diff_index_to_worktree(&odb, &mut index, wt, false, false).expect("diff");
13839        assert_eq!(diff.len(), 1);
13840        assert_eq!(diff[0].path(), "f.txt");
13841    }
13842
13843    /// A racy index row plus the git dir and odb it lives in; the worktree no longer matches.
13844    fn racy_stale_entry(wt: &Path) -> (PathBuf, Odb, Index, (u32, u32), ObjectId) {
13845        let index_mtime = setup_index_mtime(wt);
13846        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13847        let entry = staged_entry_for_file(&odb, wt, "f.txt", b"same length!!", index_mtime);
13848        let stale_oid = entry.oid;
13849        fs::write(wt.join("f.txt"), b"same length??").expect("rewrite");
13850        pin_mtime_and_sample(&wt.join("f.txt"), index_mtime.0, index_mtime.1);
13851        let meta = fs::symlink_metadata(wt.join("f.txt")).expect("stat");
13852        let mut index = Index::new();
13853        index.entries.push(entry_from_metadata(
13854            &meta,
13855            b"f.txt",
13856            stale_oid,
13857            MODE_REGULAR,
13858        ));
13859        (git_dir, odb, index, index_mtime, stale_oid)
13860    }
13861
13862    #[test]
13863    fn content_verified_entry_is_not_reread_and_the_mark_is_consumed() {
13864        let td = tempfile::tempdir().expect("tempdir");
13865        let wt = td.path();
13866        let (git_dir, odb, mut index, index_mtime, stale_oid) = racy_stale_entry(wt);
13867        index.mark_content_verified(b"f.txt", stale_oid);
13868
13869        let smudge = |index: &mut Index| {
13870            smudge_racily_clean_entries(&odb, &git_dir, index, wt, Some(index_mtime), None, None)
13871        };
13872        assert!(!smudge(&mut index), "a verified entry is trusted");
13873        assert_ne!(index.entries[0].size, 0);
13874        assert!(index.content_verified.is_empty());
13875        assert!(smudge(&mut index), "without the mark the entry is checked");
13876        assert_eq!(index.entries[0].size, 0);
13877    }
13878
13879    #[test]
13880    fn content_verified_entry_bit_is_trusted_and_consumed() {
13881        let td = tempfile::tempdir().expect("tempdir");
13882        let wt = td.path();
13883        let (git_dir, odb, mut index, index_mtime, _) = racy_stale_entry(wt);
13884        index.entries[0].mark_content_verified();
13885
13886        let smudge = |index: &mut Index| {
13887            smudge_racily_clean_entries(&odb, &git_dir, index, wt, Some(index_mtime), None, None)
13888        };
13889        assert!(!smudge(&mut index), "a marked entry is trusted");
13890        assert_ne!(index.entries[0].size, 0);
13891        assert_eq!(
13892            index.entries[0].flags_extended, None,
13893            "the mark is consumed"
13894        );
13895        assert!(smudge(&mut index), "without the mark the entry is checked");
13896        assert_eq!(index.entries[0].size, 0);
13897    }
13898
13899    #[test]
13900    fn content_verified_mark_for_another_oid_does_not_skip_the_check() {
13901        let td = tempfile::tempdir().expect("tempdir");
13902        let wt = td.path();
13903        let (git_dir, odb, mut index, index_mtime, _) = racy_stale_entry(wt);
13904        let other = odb.hash(ObjectKind::Blob, b"other").expect("hash");
13905        index.mark_content_verified(b"f.txt", other);
13906        assert!(smudge_racily_clean_entries(
13907            &odb,
13908            &git_dir,
13909            &mut index,
13910            wt,
13911            Some(index_mtime),
13912            None,
13913            None,
13914        ));
13915        assert_eq!(index.entries[0].size, 0);
13916    }
13917
13918    #[test]
13919    fn leaves_non_racy_entry_alone() {
13920        let td = tempfile::tempdir().expect("tempdir");
13921        let wt = td.path();
13922        let index_mtime = setup_index_mtime(wt);
13923        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13924        let content_a = b"same length!!";
13925        let content_b = b"same length??";
13926        let entry_mtime = (index_mtime.0 - 10, 0);
13927
13928        let mut index = Index::new();
13929        let entry = staged_entry_for_file(&odb, wt, "f.txt", content_a, entry_mtime);
13930        index.entries.push(entry.clone());
13931
13932        fs::write(wt.join("f.txt"), content_b).expect("rewrite");
13933        pin_mtime_and_sample(&wt.join("f.txt"), entry_mtime.0, entry_mtime.1);
13934
13935        assert!(!entry_is_racy(&entry, Some(index_mtime)));
13936        let smudged = smudge_racily_clean_entries(
13937            &odb,
13938            &git_dir,
13939            &mut index,
13940            wt,
13941            Some(index_mtime),
13942            None,
13943            None,
13944        );
13945        assert!(!smudged);
13946        assert_eq!(index.entries[0].size, entry.size);
13947    }
13948
13949    #[test]
13950    fn leaves_matching_content_alone() {
13951        let td = tempfile::tempdir().expect("tempdir");
13952        let wt = td.path();
13953        let index_mtime = setup_index_mtime(wt);
13954        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13955        let bytes = b"unchanged!!!!";
13956
13957        let mut index = Index::new();
13958        let entry = staged_entry_for_file(&odb, wt, "f.txt", bytes, index_mtime);
13959        index.entries.push(entry.clone());
13960
13961        assert!(entry_is_racy(&entry, Some(index_mtime)));
13962        let smudged = smudge_racily_clean_entries(
13963            &odb,
13964            &git_dir,
13965            &mut index,
13966            wt,
13967            Some(index_mtime),
13968            None,
13969            None,
13970        );
13971        assert!(!smudged);
13972        assert_eq!(index.entries[0].size, entry.size);
13973    }
13974
13975    #[test]
13976    fn status_diff_skips_refresh_rewrite_for_racy_clean_matching_stat() {
13977        let td = tempfile::tempdir().expect("tempdir");
13978        let wt = td.path();
13979        let index_mtime = setup_index_mtime(wt);
13980        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
13981        let bytes = b"racy clean payload\n";
13982
13983        let mut index = Index::new();
13984        index.source_mtime = Some(index_mtime);
13985        let entry = staged_entry_for_file(&odb, wt, "f.txt", bytes, index_mtime);
13986        index.entries.push(entry.clone());
13987        assert!(entry_is_racy(&entry, Some(index_mtime)));
13988
13989        let (entries, index_changed) = super::diff_index_to_worktree_with_options(
13990            &odb,
13991            &mut index,
13992            wt,
13993            super::DiffIndexToWorktreeOptions {
13994                repository_git_dir: Some(git_dir),
13995                index_mtime: Some(index_mtime),
13996                refresh_index_stat_in_pass: true,
13997                for_status: true,
13998                ..Default::default()
13999            },
14000        )
14001        .expect("status diff");
14002        assert!(entries.is_empty(), "unexpected unstaged: {entries:?}");
14003        assert!(
14004            !index_changed,
14005            "matching stat on a racy entry must skip refresh without rewriting the index"
14006        );
14007    }
14008
14009    #[test]
14010    fn leaves_matching_sha256_racy_content_alone() {
14011        let td = tempfile::tempdir().expect("tempdir");
14012        let wt = td.path();
14013        let index_mtime = setup_index_mtime(wt);
14014        let (git_dir, odb) = minimal_repo(wt, "[extensions]\n\tobjectformat = sha256\n");
14015        assert_eq!(odb.hash_algo(), HashAlgo::Sha256);
14016
14017        let bytes = b"sha256 width!!";
14018        let oid = odb.hash(ObjectKind::Blob, bytes).expect("hash");
14019        let mut index = Index::new();
14020        let entry = staged_entry_with_oid(wt, "f.txt", bytes, oid, index_mtime);
14021        index.entries.push(entry.clone());
14022
14023        assert!(entry_is_racy(&entry, Some(index_mtime)));
14024        let smudged = smudge_racily_clean_entries(
14025            &odb,
14026            &git_dir,
14027            &mut index,
14028            wt,
14029            Some(index_mtime),
14030            None,
14031            None,
14032        );
14033        assert!(!smudged, "matching SHA-256 blob must not be smudged");
14034        assert_eq!(index.entries[0].size, entry.size);
14035    }
14036
14037    #[test]
14038    fn materialize_big_binary_under_autocrlf_streams_into_pack() {
14039        use std::io::Write;
14040
14041        let td = tempfile::tempdir().expect("tempdir");
14042        let wt = td.path();
14043        let (git_dir, odb) =
14044            minimal_repo(wt, "[core]\n\tautocrlf = true\n\tbigFileThreshold = 64k\n");
14045        let path = wt.join("big.bin");
14046        {
14047            let mut f = fs::File::create(&path).expect("create");
14048            f.write_all(&[0u8; 9000]).expect("nul prefix");
14049            let chunk = vec![1u8; 256 * 1024];
14050            for _ in 0..20 {
14051                f.write_all(&chunk).expect("chunk");
14052            }
14053        }
14054        let meta = fs::metadata(&path).expect("stat");
14055        let mut conv = crlf::ConversionConfig::from_config(&ConfigSet::new());
14056        conv.autocrlf = crlf::AutoCrlf::True;
14057        let attrs = FileAttrs::default();
14058        let oid =
14059            materialize_worktree_blob(&odb, &path, &meta, &conv, &attrs, "big.bin", None, None)
14060                .expect("materialize");
14061        assert!(
14062            !odb.test_loose_object_path(&oid).exists(),
14063            "big binary under autocrlf should pack, not write loose"
14064        );
14065        let pack_dir = git_dir.join("objects/pack");
14066        let names: Vec<_> = fs::read_dir(&pack_dir)
14067            .expect("pack dir")
14068            .map(|e| e.expect("entry").file_name())
14069            .collect();
14070        assert!(
14071            names.iter().any(|n| n.to_string_lossy().ends_with(".pack")),
14072            "{names:?}"
14073        );
14074        assert!(odb.read(&oid).expect("read blob").data.len() > 9000);
14075    }
14076
14077    #[test]
14078    fn indexed_binary_under_autocrlf_rehash_streams() {
14079        use std::io::Write;
14080
14081        let td = tempfile::tempdir().expect("tempdir");
14082        let wt = td.path();
14083        let (_git_dir, odb) =
14084            minimal_repo(wt, "[core]\n\tautocrlf = true\n\tbigFileThreshold = 1m\n");
14085        let path = wt.join("big.bin");
14086        {
14087            let mut f = fs::File::create(&path).expect("create");
14088            f.write_all(&[0u8; 9000]).expect("nul prefix");
14089            let chunk = vec![1u8; 512 * 1024];
14090            for _ in 0..3 {
14091                f.write_all(&chunk).expect("chunk");
14092            }
14093        }
14094        let meta = fs::symlink_metadata(&path).expect("stat");
14095        let oid = hash_worktree_file(
14096            &odb,
14097            &path,
14098            &meta,
14099            &crlf::ConversionConfig::from_config(&ConfigSet::new()),
14100            &FileAttrs::default(),
14101            "big.bin",
14102            None,
14103            None,
14104        )
14105        .expect("hash");
14106        let entry = entry_from_metadata(&meta, b"big.bin", oid, MODE_REGULAR);
14107        let mut conv = crlf::ConversionConfig::from_config(&ConfigSet::new());
14108        conv.autocrlf = crlf::AutoCrlf::True;
14109        let attrs = FileAttrs::default();
14110        let content = read_regular_worktree_blob(
14111            &odb,
14112            &path,
14113            &conv,
14114            &attrs,
14115            "big.bin",
14116            Some(&entry),
14117            None,
14118            1024 * 1024,
14119        )
14120        .expect("read blob");
14121        assert!(
14122            matches!(content, WorktreeBlobContent::Stream { .. }),
14123            "indexed binary re-hash must stream, not buffer"
14124        );
14125        assert_eq!(content.hash(&odb).expect("hash"), entry.oid);
14126    }
14127
14128    #[test]
14129    fn leaves_matching_crlf_worktree_content_alone() {
14130        let td = tempfile::tempdir().expect("tempdir");
14131        let wt = td.path();
14132        let index_mtime = setup_index_mtime(wt);
14133        let (git_dir, odb) = minimal_repo(wt, "[core]\n\tautocrlf = true\n");
14134        let git_form = b"line one\nline two\n";
14135        let worktree_form = b"line one\r\nline two\r\n";
14136        let oid = odb.hash(ObjectKind::Blob, git_form).expect("hash");
14137
14138        let mut index = Index::new();
14139        let entry = staged_entry_with_oid(wt, "f.txt", worktree_form, oid, index_mtime);
14140        index.entries.push(entry.clone());
14141
14142        assert!(entry_is_racy(&entry, Some(index_mtime)));
14143        let smudged = smudge_racily_clean_entries(
14144            &odb,
14145            &git_dir,
14146            &mut index,
14147            wt,
14148            Some(index_mtime),
14149            None,
14150            None,
14151        );
14152        assert!(
14153            !smudged,
14154            "CRLF worktree matching clean index OID must not be smudged"
14155        );
14156        assert_eq!(index.entries[0].size, entry.size);
14157    }
14158
14159    #[test]
14160    fn skips_skip_worktree_and_intent_to_add() {
14161        let td = tempfile::tempdir().expect("tempdir");
14162        let wt = td.path();
14163        let index_mtime = setup_index_mtime(wt);
14164        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
14165        let content_a = b"same length!!";
14166        let content_b = b"same length??";
14167
14168        let mut index = Index::new();
14169        let mut entry = staged_entry_for_file(&odb, wt, "f.txt", content_a, index_mtime);
14170        entry.set_skip_worktree(true);
14171        index.entries.push(entry);
14172        fs::write(wt.join("f.txt"), content_b).expect("rewrite");
14173        pin_mtime_and_sample(&wt.join("f.txt"), index_mtime.0, index_mtime.1);
14174        assert!(!smudge_racily_clean_entries(
14175            &odb,
14176            &git_dir,
14177            &mut index,
14178            wt,
14179            Some(index_mtime),
14180            None,
14181            None,
14182        ));
14183        assert_ne!(index.entries[0].size, 0);
14184
14185        let mut index = Index::new();
14186        let mut entry = staged_entry_for_file(&odb, wt, "f.txt", content_a, index_mtime);
14187        entry.set_intent_to_add(true);
14188        index.entries.push(entry);
14189        fs::write(wt.join("f.txt"), content_b).expect("rewrite");
14190        pin_mtime_and_sample(&wt.join("f.txt"), index_mtime.0, index_mtime.1);
14191        assert!(!smudge_racily_clean_entries(
14192            &odb,
14193            &git_dir,
14194            &mut index,
14195            wt,
14196            Some(index_mtime),
14197            None,
14198            None,
14199        ));
14200        assert_ne!(index.entries[0].size, 0);
14201    }
14202
14203    #[test]
14204    fn smudges_nothing_without_prior_index_mtime() {
14205        let td = tempfile::tempdir().expect("tempdir");
14206        let wt = td.path();
14207        let index_mtime = setup_index_mtime(wt);
14208        let (git_dir, odb) = minimal_repo(wt, "[core]\n");
14209        let mut index = Index::new();
14210        let entry = staged_entry_for_file(&odb, wt, "f.txt", b"same length!!", index_mtime);
14211        index.entries.push(entry);
14212
14213        fs::write(wt.join("f.txt"), b"same length??").expect("rewrite");
14214        pin_mtime_and_sample(&wt.join("f.txt"), index_mtime.0, index_mtime.1);
14215
14216        let smudged = smudge_racily_clean_entries(&odb, &git_dir, &mut index, wt, None, None, None);
14217        assert!(!smudged);
14218        assert_ne!(index.entries[0].size, 0);
14219    }
14220}
14221
14222#[cfg(all(test, unix))]
14223mod read_worktree_bytes_for_mode_tests {
14224    use super::*;
14225    use crate::index::MODE_SYMLINK;
14226    use std::fs;
14227    use std::os::unix::fs::symlink;
14228
14229    #[test]
14230    fn symlink_mode_reads_link_target_not_followed_file() {
14231        let td = tempfile::tempdir().expect("tempdir");
14232        let wt = td.path();
14233        fs::write(wt.join("payload.txt"), b"secret\n").expect("write");
14234        symlink("payload.txt", wt.join("link")).expect("symlink");
14235        let bytes =
14236            read_worktree_bytes_for_mode(&wt.join("link"), MODE_SYMLINK).expect("read link");
14237        assert_eq!(bytes, b"payload.txt");
14238    }
14239
14240    #[test]
14241    fn symlink_mode_reads_plain_file_holding_the_target() {
14242        let td = tempfile::tempdir().expect("tempdir");
14243        let path = td.path().join("link");
14244        fs::write(&path, b"payload.txt").expect("write");
14245        let bytes = read_worktree_bytes_for_mode(&path, MODE_SYMLINK).expect("read plain link");
14246        assert_eq!(bytes, b"payload.txt");
14247    }
14248
14249    #[test]
14250    fn regular_mode_reads_file_contents() {
14251        let td = tempfile::tempdir().expect("tempdir");
14252        let path = td.path().join("f.txt");
14253        fs::write(&path, b"hello\n").expect("write");
14254        let bytes = read_worktree_bytes_for_mode(&path, crate::index::MODE_REGULAR).expect("read");
14255        assert_eq!(bytes, b"hello\n");
14256    }
14257
14258    #[test]
14259    fn annotate_marks_context_line_when_eof_line_is_unchanged() {
14260        let old = "a\nb\nc";
14261        let new = "a\nx\nc";
14262        use crate::diff::{unified_diff, DiffAlgorithm, UnifiedDiffOptions};
14263        let options = UnifiedDiffOptions::new()
14264            .with_algorithm(DiffAlgorithm::Histogram)
14265            .with_indent_heuristic(crate::diff::IndentHeuristic::Disabled);
14266        let out = unified_diff(old, new, &options);
14267        assert!(
14268            out.contains(" c\n\\ No newline at end of file"),
14269            "histogram hunk should mark trailing context line:\n{out}"
14270        );
14271    }
14272
14273    #[test]
14274    fn annotate_does_not_treat_removed_dash_lines_as_file_headers() {
14275        use crate::diff::{unified_diff, UnifiedDiffOptions};
14276
14277        let old = "--old";
14278        let new = "--new";
14279        let options = UnifiedDiffOptions::new()
14280            .with_paths("x", "x")
14281            .with_indent_heuristic(crate::diff::IndentHeuristic::Disabled);
14282        let raw = unified_diff(old, new, &options);
14283        assert_eq!(
14284            raw.matches("\\ No newline at end of file").count(),
14285            2,
14286            "expected two markers like git, got:\n{raw}"
14287        );
14288    }
14289}
14290
14291#[cfg(test)]
14292mod annotate_no_newline_tests {
14293    use crate::diff::{unified_diff, UnifiedDiffOptions};
14294
14295    #[test]
14296    fn annotate_marks_both_sides_without_trailing_newline() {
14297        let out = unified_diff("foo", "f o o", &UnifiedDiffOptions::new());
14298        assert_eq!(
14299            out,
14300            "@@ -1 +1 @@\n-foo\n\\ No newline at end of file\n+f o o\n\\ No newline at end of file\n"
14301        );
14302    }
14303}
14304
14305#[cfg(test)]
14306mod blob_diff_algorithm_smoke {
14307    use super::unified_hunk_body_for_diff_options;
14308    use crate::diff::{line_change_counts, unified_diff, DiffAlgorithm, UnifiedDiffOptions};
14309
14310    const OLD: &str = "alpha\nbeta\ngamma\n";
14311    const NEW: &str = "alpha\nBETA\ngamma\ndelta\n";
14312
14313    #[test]
14314    fn seed15_histogram_u0_body_matches_git() {
14315        use crate::diff::options::{DiffAlgorithm, DiffOptions};
14316        use std::collections::hash_map::DefaultHasher;
14317        use std::hash::{Hash, Hasher};
14318        let seed = 15u64;
14319        let mut hasher = DefaultHasher::new();
14320        seed.hash(&mut hasher);
14321        let h = hasher.finish();
14322        let line_count = 4 + (h as usize % 12);
14323        let mut old = String::new();
14324        let mut new = String::new();
14325        for i in 0..line_count {
14326            let token = (h.wrapping_add(i as u64 * 131) % 7) as u8 + b'a';
14327            let ch = token as char;
14328            old.push(ch);
14329            if (h >> (i % 8)) & 1 == 1 {
14330                new.push(((token as u8).wrapping_add(1)) as char);
14331            } else {
14332                new.push(ch);
14333            }
14334            old.push('\n');
14335            new.push('\n');
14336        }
14337        let opts = DiffOptions {
14338            context_lines: 0,
14339            algorithm: DiffAlgorithm::Histogram,
14340            indent_heuristic: false,
14341            ..DiffOptions::default()
14342        };
14343        let body = unified_hunk_body_for_diff_options(&old, &new, &opts);
14344        assert_eq!(
14345            body.matches("@@ ").count(),
14346            2,
14347            "git 2.55 splits u0 histogram hunks at seed 15: {body}"
14348        );
14349    }
14350
14351    #[test]
14352    fn t4071_histogram_changed_flags_git_myers_completes() {
14353        let old: Vec<&str> = "x\nx\nx\nx\n".lines().collect();
14354        let new: Vec<&str> = "x\nx\nx\nA\nB\nC\nD\nx\nE\nF\nG\n".lines().collect();
14355        let (c1, c2) = super::git_xdiff::histogram_changed_flags(&old, &new);
14356        assert_eq!(c1.len(), 4);
14357        assert_eq!(c2.len(), 11);
14358        let ops = super::changed_flags_to_ops(&c1, &c2, old.len(), new.len());
14359        assert!(!ops.is_empty());
14360    }
14361
14362    #[test]
14363    fn t4071_histogram_compacted_ops_completes() {
14364        use crate::diff::WhitespaceMode;
14365        let old = "x\nx\nx\nx\n";
14366        let new = "x\nx\nx\nA\nB\nC\nD\nx\nE\nF\nG\n";
14367        let ops = super::histogram_compacted_line_ops(old, new, false, WhitespaceMode::default());
14368        assert!(!ops.is_empty(), "expected diff ops");
14369    }
14370
14371    #[test]
14372    fn t4071_histogram_u0_body_completes() {
14373        use crate::diff::options::{DiffAlgorithm, DiffOptions};
14374        let old = "x\nx\nx\nx\n";
14375        let new = "x\nx\nx\nA\nB\nC\nD\nx\nE\nF\nG\n";
14376        let opts = DiffOptions {
14377            context_lines: 0,
14378            algorithm: DiffAlgorithm::Histogram,
14379            indent_heuristic: false,
14380            ..DiffOptions::default()
14381        };
14382        let body = unified_hunk_body_for_diff_options(old, new, &opts);
14383        assert!(body.contains("@@"), "expected hunk header: {body}");
14384    }
14385
14386    #[test]
14387    fn t4071_minimal_u0_matches_git_hunk_headers() {
14388        use crate::diff::options::{DiffAlgorithm, DiffOptions};
14389        let old = "x\nx\nx\nx\n";
14390        let new = "x\nx\nx\nA\nB\nC\nD\nx\nE\nF\nG\n";
14391        let opts = DiffOptions {
14392            context_lines: 0,
14393            algorithm: DiffAlgorithm::Minimal,
14394            indent_heuristic: false,
14395            ..DiffOptions::default()
14396        };
14397        let body = unified_hunk_body_for_diff_options(old, new, &opts);
14398        assert_eq!(
14399            body.matches("@@ ").count(),
14400            2,
14401            "git 2.55+ splits u0 minimal hunks at repeated anchors: {body}"
14402        );
14403    }
14404
14405    #[test]
14406    fn hunks_do_not_slide_onto_final_line_without_newline() {
14407        use crate::diff::options::{DiffAlgorithm, DiffOptions};
14408
14409        let old = "{\n  \"a\": 1\n}\n";
14410        let new = "{\n  \"a\": 2\n}\n{\n  \"b\": 2\n}";
14411        let expected = "@@ -1,3 +1,6 @@\n {\n-  \"a\": 1\n+  \"a\": 2\n }\n+{\n+  \"b\": 2\n+}\n\\ No newline at end of file\n";
14412        for algorithm in [
14413            DiffAlgorithm::Myers,
14414            DiffAlgorithm::Minimal,
14415            DiffAlgorithm::Patience,
14416            DiffAlgorithm::Histogram,
14417        ] {
14418            for indent_heuristic in [true, false] {
14419                let options = DiffOptions {
14420                    algorithm,
14421                    indent_heuristic,
14422                    ..DiffOptions::default()
14423                };
14424                assert_eq!(
14425                    unified_hunk_body_for_diff_options(old, new, &options),
14426                    expected,
14427                    "{algorithm:?}, indent heuristic {indent_heuristic}"
14428                );
14429            }
14430        }
14431    }
14432
14433    #[test]
14434    fn myers_patience_histogram_minimal_emit_unified_hunks() {
14435        for algo in [
14436            DiffAlgorithm::Myers,
14437            DiffAlgorithm::Minimal,
14438            DiffAlgorithm::Patience,
14439            DiffAlgorithm::Histogram,
14440        ] {
14441            let options = UnifiedDiffOptions::new()
14442                .with_paths("old.txt", "new.txt")
14443                .with_algorithm(algo);
14444            let patch = unified_diff(OLD, NEW, &options);
14445            assert!(patch.starts_with("--- a/old.txt\n+++ b/new.txt\n@@"));
14446            assert!(patch.contains("-beta") || patch.contains("-BETA"));
14447        }
14448    }
14449
14450    #[test]
14451    fn git_histogram_printer_and_anchored_diff_smoke() {
14452        let histogram = UnifiedDiffOptions::new().with_algorithm(DiffAlgorithm::Histogram);
14453        assert!(!unified_diff(OLD, NEW, &histogram).is_empty());
14454        let anchored = UnifiedDiffOptions::new()
14455            .with_paths("old", "new")
14456            .with_anchors(["beta"]);
14457        assert!(unified_diff(OLD, NEW, &anchored).contains("@@"));
14458    }
14459
14460    #[test]
14461    fn count_changes_git_histogram_vs_myers() {
14462        let histogram = UnifiedDiffOptions::new().with_algorithm(DiffAlgorithm::Histogram);
14463        let hist = line_change_counts(OLD, NEW, &histogram);
14464        let myers = line_change_counts(OLD, NEW, &UnifiedDiffOptions::new());
14465        assert!(hist.insertions + hist.deletions > 0);
14466        assert!(myers.insertions + myers.deletions > 0);
14467    }
14468}
14469
14470#[cfg(test)]
14471mod line_coverage_tests {
14472    use super::*;
14473    use crate::config::ConfigSet;
14474    use crate::index::StatMatchPolicy;
14475    use crate::index::{Index, IndexEntry, MODE_REGULAR};
14476    use crate::objects::ObjectKind;
14477    use crate::parallel::ThreadPolicy;
14478    use crate::write_tree::write_tree_from_index;
14479    use similar::Algorithm;
14480    use tempfile::TempDir;
14481
14482    fn index_entry(path: &str, mode: u32, oid: ObjectId) -> IndexEntry {
14483        IndexEntry {
14484            ctime_sec: 0,
14485            ctime_nsec: 0,
14486            mtime_sec: 0,
14487            mtime_nsec: 0,
14488            dev: 0,
14489            ino: 0,
14490            mode,
14491            uid: 0,
14492            gid: 0,
14493            size: 0,
14494            oid,
14495            flags: path.len().min(0xfff) as u16,
14496            flags_extended: None,
14497            path: path.as_bytes().to_vec(),
14498            base_index_pos: 0,
14499        }
14500    }
14501
14502    fn write_tree(odb: &Odb, entries: &[(&str, ObjectId)]) -> ObjectId {
14503        let mut body = Vec::new();
14504        for (name, oid) in entries {
14505            body.extend_from_slice(format!("100644 {name}\0").as_bytes());
14506            body.extend_from_slice(oid.as_bytes());
14507        }
14508        odb.write(ObjectKind::Tree, &body).expect("tree")
14509    }
14510
14511    fn pair_of_trees_with_rename() -> (TempDir, Odb, ObjectId, ObjectId) {
14512        let temp = TempDir::new().expect("tempdir");
14513        let odb = Odb::new(temp.path());
14514        let old_blob = odb
14515            .write(ObjectKind::Blob, b"line one\nline two\n")
14516            .expect("old");
14517        let new_blob = odb
14518            .write(ObjectKind::Blob, b"line one\nline TWO\nline three\n")
14519            .expect("new");
14520        let old_tree = write_tree(&odb, &[("old.txt", old_blob)]);
14521        let new_tree = write_tree(&odb, &[("new.txt", new_blob), ("kept.txt", old_blob)]);
14522        (temp, odb, old_tree, new_tree)
14523    }
14524
14525    #[test]
14526    fn detect_copies_reports_source_modes_without_tree_entries() {
14527        let temp = TempDir::new().expect("tempdir");
14528        let odb = Odb::new(temp.path());
14529        let content = odb
14530            .write(ObjectKind::Blob, b"shared content\n")
14531            .expect("blob");
14532        let changed = odb.write(ObjectKind::Blob, b"changed\n").expect("blob");
14533        let old_tree = write_tree(&odb, &[("gone.txt", content), ("mod.txt", content)]);
14534        let new_tree = write_tree(
14535            &odb,
14536            &[
14537                ("copy.txt", content),
14538                ("mod.txt", changed),
14539                ("moved.txt", content),
14540            ],
14541        );
14542        let entries = diff_trees(&odb, Some(&old_tree), Some(&new_tree), "").expect("diff");
14543        let result = detect_copies(&odb, None, entries, 50, false, &[]).expect("copies");
14544        let pairs: Vec<_> = result
14545            .iter()
14546            .filter(|e| matches!(e.status, DiffStatus::Renamed | DiffStatus::Copied))
14547            .map(|e| (e.old_mode, e.new_mode))
14548            .collect();
14549        assert_eq!(pairs.len(), 2, "{result:?}");
14550        assert!(
14551            pairs
14552                .iter()
14553                .all(|p| *p == (FileMode::REGULAR, FileMode::REGULAR)),
14554            "{pairs:?}"
14555        );
14556    }
14557
14558    #[test]
14559    fn exact_rename_detection_pairs_only_equal_blobs_among_many() -> Result<()> {
14560        let temp = TempDir::new().expect("tempdir");
14561        let odb = Odb::new(temp.path());
14562        const N: usize = 200;
14563        let shared = odb.write(ObjectKind::Blob, b"shared\n").expect("blob");
14564        let mut old_names = Vec::with_capacity(N + 1);
14565        let mut old_oids = Vec::with_capacity(N + 1);
14566        for i in 0..N {
14567            old_names.push(format!("old/{i:03}.txt"));
14568            old_oids.push(
14569                odb.write(ObjectKind::Blob, format!("old {i}\n").as_bytes())
14570                    .expect("blob"),
14571            );
14572        }
14573        old_names.push("shared-old.txt".to_owned());
14574        old_oids.push(shared);
14575        let old_pairs: Vec<(&str, ObjectId)> = old_names
14576            .iter()
14577            .zip(old_oids.iter().copied())
14578            .map(|(name, oid)| (name.as_str(), oid))
14579            .collect();
14580
14581        let mut new_names = Vec::with_capacity(N + 1);
14582        let mut new_oids = Vec::with_capacity(N + 1);
14583        for i in 0..N {
14584            new_names.push(format!("new/{i:03}.txt"));
14585            new_oids.push(
14586                odb.write(ObjectKind::Blob, format!("new {i}\n").as_bytes())
14587                    .expect("blob"),
14588            );
14589        }
14590        new_names.push("shared-new.txt".to_owned());
14591        new_oids.push(shared);
14592        let new_pairs: Vec<(&str, ObjectId)> = new_names
14593            .iter()
14594            .zip(new_oids.iter().copied())
14595            .map(|(name, oid)| (name.as_str(), oid))
14596            .collect();
14597
14598        let old_tree = write_tree(&odb, &old_pairs);
14599        let new_tree = write_tree(&odb, &new_pairs);
14600        let raw = diff_trees(&odb, Some(&old_tree), Some(&new_tree), "")?;
14601        assert_eq!(raw.len(), (N + 1) * 2);
14602        let config = ConfigSet::new();
14603        let policy = ThreadPolicy::default();
14604        let renamed = detect_renames_with_policy(
14605            &odb,
14606            None,
14607            raw,
14608            50,
14609            &config,
14610            &policy,
14611            None,
14612            DetectRenamesOptions::default(),
14613        )?;
14614        assert_eq!(
14615            renamed
14616                .entries
14617                .iter()
14618                .filter(|e| e.status == DiffStatus::Renamed)
14619                .count(),
14620            1
14621        );
14622        Ok(())
14623    }
14624
14625    #[test]
14626    fn pathspec_limited_diff_does_not_pair_outside_pathspec() -> Result<()> {
14627        let temp = TempDir::new().expect("tempdir");
14628        let odb = Odb::new(temp.path());
14629        let shared = odb.write(ObjectKind::Blob, b"payload\n").expect("blob");
14630        let mut old_names = Vec::new();
14631        let mut old_oids = Vec::new();
14632        for i in 0..50 {
14633            old_names.push(format!("bulk/old/{i:02}.txt"));
14634            old_oids.push(
14635                odb.write(ObjectKind::Blob, format!("bulk old {i}\n").as_bytes())
14636                    .expect("blob"),
14637            );
14638        }
14639        old_names.push("target.txt".to_owned());
14640        old_oids.push(shared);
14641        let old_pairs: Vec<(&str, ObjectId)> = old_names
14642            .iter()
14643            .zip(old_oids.iter().copied())
14644            .map(|(name, oid)| (name.as_str(), oid))
14645            .collect();
14646
14647        let mut new_names = Vec::new();
14648        let mut new_oids = Vec::new();
14649        for i in 0..50 {
14650            new_names.push(format!("bulk/new/{i:02}.txt"));
14651            new_oids.push(
14652                odb.write(ObjectKind::Blob, format!("bulk new {i}\n").as_bytes())
14653                    .expect("blob"),
14654            );
14655        }
14656        new_names.push("elsewhere.txt".to_owned());
14657        new_oids.push(shared);
14658        let new_pairs: Vec<(&str, ObjectId)> = new_names
14659            .iter()
14660            .zip(new_oids.iter().copied())
14661            .map(|(name, oid)| (name.as_str(), oid))
14662            .collect();
14663
14664        let old_tree = write_tree(&odb, &old_pairs);
14665        let new_tree = write_tree(&odb, &new_pairs);
14666        let config = ConfigSet::new();
14667        let policy = ThreadPolicy::default();
14668        let pathspecs = vec!["target.txt".to_owned()];
14669        let entries = diff_trees_with_renames_for_pathspecs_and_policy(
14670            &odb,
14671            Some(&old_tree),
14672            Some(&new_tree),
14673            "",
14674            &pathspecs,
14675            None,
14676            50,
14677            &config,
14678            &policy,
14679        )?;
14680        assert_eq!(entries.len(), 1);
14681        assert_eq!(entries[0].status, DiffStatus::Deleted);
14682        assert_eq!(entries[0].path(), "target.txt");
14683        Ok(())
14684    }
14685
14686    #[test]
14687    fn detect_copies_keeps_modifications_as_modifications() {
14688        // `git diff -C` reports `b` as modified even though its new content equals the
14689        // old content of the also-modified `a`: only created paths become copies.
14690        let temp = TempDir::new().expect("tempdir");
14691        let odb = Odb::new(temp.path());
14692        let one = odb.write(ObjectKind::Blob, b"1\n").expect("blob");
14693        let two = odb.write(ObjectKind::Blob, b"2\n").expect("blob");
14694        let old_tree = write_tree(&odb, &[("a", one), ("b", two)]);
14695        let new_tree = write_tree(&odb, &[("a", two), ("b", one)]);
14696        let entries = diff_trees(&odb, Some(&old_tree), Some(&new_tree), "").expect("diff");
14697        let result = detect_copies(&odb, None, entries, 50, false, &[]).expect("copies");
14698        let statuses: Vec<_> = result.iter().map(|e| (e.path(), e.status)).collect();
14699        assert_eq!(
14700            statuses,
14701            [("a", DiffStatus::Modified), ("b", DiffStatus::Modified)]
14702        );
14703    }
14704
14705    #[test]
14706    fn diff_helper_and_format_sweep() {
14707        let cfg = ConfigSet::new();
14708        assert!(resolve_indent_heuristic(&cfg, false, false));
14709        assert!(!resolve_indent_heuristic(&cfg, false, true));
14710
14711        let old = "alpha beta\ngamma\n";
14712        let new = "alpha BETA\ngamma delta\n";
14713        let _ = normalize_ignore_space_change(old);
14714        let _ = normalize_ignore_space_change_line("- alpha  beta ");
14715        let _ = count_git_lines(old.as_bytes());
14716        let _ = diffcore_count_changes(old.as_bytes(), new.as_bytes());
14717        assert!(parse_diff_rename_score_token("50%").is_some());
14718        let _ = rewrite_merge_score(b"a", b"b");
14719        let _ = rewrite_dissimilarity_index_percent(b"abc", b"abd");
14720        assert_eq!(format_mode(0o100644), "100644");
14721
14722        let old_lines = ["a", "b", "c"];
14723        let new_lines = ["a", "b", "d"];
14724        let compact = diff_slice_ops_compacted(&old_lines, &new_lines, Algorithm::Myers, false);
14725        assert!(!compact.is_empty());
14726    }
14727
14728    #[test]
14729    fn tree_diff_rename_copy_postprocess_matrix() -> Result<()> {
14730        let (_temp, odb, old_tree, new_tree) = pair_of_trees_with_rename();
14731        let raw = diff_trees(&odb, Some(&old_tree), Some(&new_tree), "")?;
14732        assert!(!raw.is_empty());
14733        let with_renames = diff_trees_with_renames(&odb, Some(&old_tree), Some(&new_tree), "", 50)?;
14734        assert!(!with_renames.is_empty());
14735        let config = ConfigSet::new();
14736        let policy = ThreadPolicy::default();
14737        let detected = detect_renames_with_policy(
14738            &odb,
14739            None,
14740            raw.clone(),
14741            50,
14742            &config,
14743            &policy,
14744            None,
14745            DetectRenamesOptions::default(),
14746        )?
14747        .entries;
14748        let source_flat = flatten_tree_entries(&odb, &old_tree)?;
14749        let _ = detect_copies_with_policy(
14750            &odb,
14751            None,
14752            detected.clone(),
14753            50,
14754            true,
14755            &source_flat,
14756            &config,
14757            &policy,
14758        )?;
14759        let opts = DiffPostprocessOptions {
14760            break_score: Some(50),
14761            rename_threshold: 50,
14762            copy_threshold: Some(50),
14763            find_copies_harder: true,
14764            materialize_break_worktree_new_oid: false,
14765        };
14766        let processed = postprocess_diff_entries(
14767            &odb,
14768            raw,
14769            Some(&old_tree),
14770            None,
14771            None,
14772            &config,
14773            &policy,
14774            &opts,
14775            None,
14776        )?;
14777        assert!(!processed.is_empty());
14778        let _ = status_apply_rename_copy_detection(
14779            &odb,
14780            with_renames.clone(),
14781            50,
14782            true,
14783            Some(&old_tree),
14784            &config,
14785            &policy,
14786            None,
14787        )?;
14788        let flat = flatten_tree_entries(&odb, &new_tree)?;
14789        assert!(!flat.is_empty());
14790        let _ = diff_trees_show_tree_entries(&odb, Some(&old_tree), Some(&new_tree), "")?;
14791        Ok(())
14792    }
14793
14794    #[test]
14795    fn unified_diff_flag_matrix() {
14796        let old = "fn main() {\n    println!(\"hi\");\n}\n";
14797        let new = "fn main() {\n    println!(\"bye\");\n}\n";
14798        use crate::diff::{
14799            DiffAlgorithm, HunkExtent, IndentHeuristic, PathQuoting, UnifiedDiffOptions,
14800        };
14801        for algo in [
14802            DiffAlgorithm::Myers,
14803            DiffAlgorithm::Patience,
14804            DiffAlgorithm::Histogram,
14805        ] {
14806            for (extent, indent_heuristic, quoting) in [
14807                (
14808                    HunkExtent::Function,
14809                    IndentHeuristic::Disabled,
14810                    PathQuoting::KeepNonAscii,
14811                ),
14812                (
14813                    HunkExtent::Context,
14814                    IndentHeuristic::Enabled,
14815                    PathQuoting::EscapeNonAscii,
14816                ),
14817            ] {
14818                let options = UnifiedDiffOptions::new()
14819                    .with_paths("main.rs", "main.rs")
14820                    .with_algorithm(algo)
14821                    .with_extent(extent)
14822                    .with_indent_heuristic(indent_heuristic)
14823                    .with_path_quoting(quoting);
14824                let patch = crate::diff::unified_diff(old, new, &options);
14825                assert!(patch.contains("@@"), "algo {algo:?}");
14826            }
14827        }
14828        let hist = crate::diff::unified_diff(
14829            old,
14830            new,
14831            &crate::diff::UnifiedDiffOptions::new()
14832                .with_algorithm(crate::diff::DiffAlgorithm::Histogram)
14833                .with_whitespace(crate::diff::WhitespaceMode::ignore_blank_lines()),
14834        );
14835        assert!(!hist.is_empty());
14836        let raw = format_raw_abbrev(
14837            &DiffEntry {
14838                status: DiffStatus::Modified,
14839                old_path: Some("x".into()),
14840                new_path: Some("x".into()),
14841                old_mode: FileMode::REGULAR,
14842                new_mode: FileMode::REGULAR,
14843                old_oid: empty_blob_oid(),
14844                new_oid: empty_blob_oid(),
14845                score: Some(80),
14846            },
14847            7,
14848        );
14849        assert!(raw.contains('M') || raw.contains('x'));
14850    }
14851
14852    #[test]
14853    fn index_and_commit_diff_paths() -> Result<()> {
14854        let temp = TempDir::new().expect("tempdir");
14855        let odb = Odb::new(temp.path());
14856        let b1 = odb.write(ObjectKind::Blob, b"v1\n").expect("b1");
14857        let b2 = odb.write(ObjectKind::Blob, b"v2\n").expect("b2");
14858        let b3 = odb.write(ObjectKind::Blob, b"v3\n").expect("b3");
14859        let mut index = Index::new();
14860        index.add_or_replace(index_entry("file.txt", MODE_REGULAR, b2));
14861        let head = write_tree_from_index(&odb, &index, "").expect("tree");
14862        index.entries_mut()[0].oid = b1;
14863        let idx_diff = diff_index_to_tree(&odb, &index, Some(&head), false)?;
14864        assert!(!idx_diff.is_empty());
14865        let commit_body = format!(
14866            "tree {head}\nauthor T <t@e.com> 1 +0000\ncommitter T <t@e.com> 1 +0000\n\nm\n"
14867        );
14868        let c1 = odb
14869            .write(ObjectKind::Commit, commit_body.as_bytes())
14870            .expect("c1");
14871        let mut index2 = Index::new();
14872        index2.add_or_replace(index_entry("file.txt", MODE_REGULAR, b3));
14873        let head2 = write_tree_from_index(&odb, &index2, "").expect("head2");
14874        let c2_body = format!(
14875            "tree {head2}\nparent {c1}\nauthor T <t@e.com> 2 +0000\ncommitter T <t@e.com> 2 +0000\n\nm2\n"
14876        );
14877        let c2 = odb
14878            .write(ObjectKind::Commit, c2_body.as_bytes())
14879            .expect("c2");
14880        let commit_diff = diff_commits_with_renames(&odb, &c1, &c2, 50)?;
14881        assert!(!commit_diff.is_empty());
14882        let _ = tree_oid_of_commit(&odb, &c2)?;
14883        let mtime = index_mtime_for_diff(&index, Some((1, 0)));
14884        let _ = entry_is_racy(&index.entries()[0], mtime);
14885        let _ = entry_is_racy_with_policy(&index.entries()[0], mtime, StatMatchPolicy::default());
14886        Ok(())
14887    }
14888
14889    #[test]
14890    fn tree_oid_of_commit_reports_invalid_ref_for_non_commits() {
14891        let temp = TempDir::new().expect("tempdir");
14892        let odb = Odb::new(temp.path());
14893        let tree = odb.write(ObjectKind::Tree, &[]).expect("tree");
14894        let body = format!(
14895            "tree {tree}\nauthor T <t@e.com> 1 +0000\ncommitter T <t@e.com> 1 +0000\n\nm\n"
14896        );
14897        let commit = odb
14898            .write(ObjectKind::Commit, body.as_bytes())
14899            .expect("commit");
14900        let blob = odb.write(ObjectKind::Blob, b"x\n").expect("blob");
14901
14902        assert_eq!(tree_oid_of_commit(&odb, &commit).expect("tree"), tree);
14903        assert!(matches!(
14904            tree_oid_of_commit(&odb, &blob),
14905            Err(Error::Ref(RefError::Invalid { .. }))
14906        ));
14907        assert!(matches!(
14908            tree_oid_of_commit(&odb, &tree),
14909            Err(Error::Ref(RefError::Invalid { .. }))
14910        ));
14911    }
14912
14913    #[test]
14914    fn worktree_index_diff_and_rename_score() -> Result<()> {
14915        let temp = TempDir::new().expect("tempdir");
14916        let wt = temp.path();
14917        let git_dir = wt.join(".git");
14918        std::fs::create_dir_all(git_dir.join("objects")).expect("objects");
14919        let objects_dir = git_dir.join("objects");
14920        let odb = Odb::new(&objects_dir);
14921        let content = b"hello world\n";
14922        std::fs::write(wt.join("tracked.txt"), content).expect("write");
14923        let oid = odb.write(ObjectKind::Blob, content).expect("blob");
14924        let mut index = Index::new();
14925        index.add_or_replace(index_entry("tracked.txt", MODE_REGULAR, oid));
14926        let head = write_tree_from_index(&odb, &index, "").expect("tree");
14927        std::fs::write(wt.join("tracked.txt"), b"hello WORLD\n").expect("mutate");
14928        let mut index_mut = index.clone();
14929        let diff = diff_index_to_worktree(&odb, &mut index_mut, wt, false, false)?;
14930        assert!(!diff.is_empty());
14931        let _ = diff_tree_to_worktree(&odb, Some(&head), wt, &index)?;
14932        assert_eq!(rename_similarity_score(b"a", b"a\n"), 0);
14933        assert_eq!(rename_similarity_score(b"a\n", b"a"), 0);
14934        let score = rename_similarity_score(b"line one\nline two\n", b"line one\nline TWO\n");
14935        assert!(score >= 50);
14936        let _ = detect_renames(&odb, Some(wt), diff, 50);
14937        Ok(())
14938    }
14939
14940    #[test]
14941    fn extended_diff_helpers_sweep() -> Result<()> {
14942        use crate::crlf::{ConversionConfig, FileAttrs};
14943        let temp = TempDir::new().expect("tempdir");
14944        let wt = temp.path();
14945        let git_dir = wt.join(".git");
14946        std::fs::create_dir_all(git_dir.join("objects")).expect("objects");
14947        let objects_dir = git_dir.join("objects");
14948        let odb = Odb::new(&objects_dir);
14949        let b_old = odb.write(ObjectKind::Blob, b"alpha\nbeta\n").expect("old");
14950        let b_new = odb
14951            .write(ObjectKind::Blob, b"alpha\nBETA\ngamma\n")
14952            .expect("new");
14953        let old_tree = write_tree(&odb, &[("a.txt", b_old), ("b.txt", b_old)]);
14954        let new_tree = write_tree(
14955            &odb,
14956            &[("a.txt", b_new), ("b.txt", b_new), ("c.txt", b_new)],
14957        );
14958        let entries = diff_trees(&odb, Some(&old_tree), Some(&new_tree), "")?;
14959        assert!(entries.len() >= 2);
14960
14961        let cfg = ConfigSet::new();
14962        assert!(indent_heuristic_from_config(&cfg));
14963        let _ = crate::diff::unified_diff(
14964            "x\n",
14965            "y\n",
14966            &crate::diff::UnifiedDiffOptions::new()
14967                .with_paths("f", "f")
14968                .with_algorithm(crate::diff::DiffAlgorithm::Histogram)
14969                .with_indent_heuristic(crate::diff::IndentHeuristic::Disabled),
14970        );
14971        let _ = should_break_rewrite_pair(b"short", b"totally different body", 50);
14972        let _ = head_path_states(&odb, Some(&new_tree))?;
14973        std::fs::write(wt.join("tracked.txt"), b"hello\n").expect("wt");
14974        let abs = wt.join("tracked.txt");
14975        let meta = std::fs::metadata(&abs).expect("meta");
14976        let mut index = Index::new();
14977        index.add_or_replace(index_entry("tracked.txt", MODE_REGULAR, b_old));
14978        let _ = stat_matches_with_policy(&index.entries()[0], &meta, StatMatchPolicy::default());
14979        let _ = worktree_differs_from_index_entry(
14980            &odb,
14981            &git_dir,
14982            wt,
14983            &index.entries()[0],
14984            false,
14985            None,
14986            None,
14987        )?;
14988        let conv = ConversionConfig::from_config(&cfg);
14989        let attrs = FileAttrs::default();
14990        let params = WorktreeAddRefreshParams {
14991            odb: &odb,
14992            ie: &index.entries()[0],
14993            meta: &meta,
14994            abs_path: &abs,
14995            rel_path: "tracked.txt",
14996            conv: &conv,
14997            file_attrs: &attrs,
14998            index_mtime: None,
14999            staged_mode: MODE_REGULAR,
15000            filter_process: None,
15001        };
15002        let _ = classify_worktree_entry_for_add(&params)?;
15003        let _ = hash_worktree_file(
15004            &odb,
15005            &abs,
15006            &meta,
15007            &conv,
15008            &attrs,
15009            "tracked.txt",
15010            Some(&index.entries()[0]),
15011            None,
15012        )?;
15013        let _ = materialize_worktree_blob(
15014            &odb,
15015            &abs,
15016            &meta,
15017            &conv,
15018            &attrs,
15019            "tracked.txt",
15020            Some(&index.entries()[0]),
15021            None,
15022        )?;
15023        let _ = read_worktree_bytes_for_mode(&abs, MODE_REGULAR);
15024        let _ = mode_from_metadata(&meta);
15025        let _ = smudge_racily_clean_entries(&odb, &git_dir, &mut index, wt, None, Some(&cfg), None);
15026        let _ = refresh_index_stat_content_verified(
15027            &odb,
15028            &git_dir,
15029            &mut index,
15030            wt,
15031            None,
15032            Some(&cfg),
15033            None,
15034        )?;
15035        let _ = refresh_index_stat_content_verified_with_rules(
15036            &odb,
15037            &git_dir,
15038            &mut index,
15039            wt,
15040            None,
15041            Some(&cfg),
15042            None,
15043            None,
15044        )?;
15045        let _ = path_checkout_skip_blob_write_when_up_to_date_with_config(
15046            &odb,
15047            &git_dir,
15048            wt,
15049            &index,
15050            &index.entries()[0],
15051            &b_old,
15052            MODE_REGULAR,
15053            "tracked.txt",
15054            None,
15055            None,
15056        )?;
15057        let patch = crate::diff::unified_diff(
15058            "a\nb\n",
15059            "a\nB\n",
15060            &crate::diff::UnifiedDiffOptions::new()
15061                .with_paths("f", "f")
15062                .with_anchors(["b"]),
15063        );
15064        assert!(patch.contains("@@"));
15065        Ok(())
15066    }
15067
15068    #[test]
15069    fn unmerged_only_tree_worktree_diff_and_func_heuristics() -> Result<()> {
15070        let appended_function = crate::diff::unified_diff(
15071            "a\nb\nc\nd\n",
15072            "a\nb\nc\nd\nvoid added() {\n}\n",
15073            &crate::diff::UnifiedDiffOptions::new().with_extent(crate::diff::HunkExtent::Function),
15074        );
15075        assert_eq!(
15076            appended_function, "@@ -2,3 +2,5 @@ a\n b\n c\n d\n+void added() {\n+}\n",
15077            "an appended whole function keeps ordinary pre-context under -W"
15078        );
15079
15080        let temp = TempDir::new().expect("tempdir");
15081        let wt = temp.path();
15082        let git_dir = wt.join(".git");
15083        let objects_dir = git_dir.join("objects");
15084        std::fs::create_dir_all(&objects_dir).expect("objects");
15085        let odb = Odb::new(&objects_dir);
15086        let tree_blob = odb.write(ObjectKind::Blob, b"base\n").expect("blob");
15087        std::fs::write(wt.join("conflict.txt"), b"worktree\n").expect("wt");
15088        let tree = write_tree(&odb, &[("conflict.txt", tree_blob)]);
15089        let mut index = Index::new();
15090        let mut stage1 = index_entry("conflict.txt", MODE_REGULAR, tree_blob);
15091        stage1.flags = (stage1.flags & 0x0FFF) | (1 << 12);
15092        index.entries_mut().push(stage1);
15093        let cfg = ConfigSet::new();
15094        let diff = diff_tree_to_worktree_with_git_dir(
15095            &odb,
15096            Some(&tree),
15097            wt,
15098            &git_dir,
15099            &index,
15100            Some(&cfg),
15101        )?;
15102        assert!(!diff.is_empty());
15103        Ok(())
15104    }
15105
15106    #[test]
15107    fn high_volume_unified_diff_and_postprocess() -> Result<()> {
15108        let (_temp, odb, old_tree, new_tree) = pair_of_trees_with_rename();
15109        let config = ConfigSet::new();
15110        let policy = ThreadPolicy::default();
15111        for i in 0..600u32 {
15112            let old = format!("seed {i}\nalpha\n");
15113            let new = format!("seed {i}\nalpha\nvoid fn_{i}(int arg) {{\n  return arg;\n}}\n");
15114            let quoting = if i % 7 == 0 {
15115                crate::diff::PathQuoting::EscapeNonAscii
15116            } else {
15117                crate::diff::PathQuoting::KeepNonAscii
15118            };
15119            let algorithm = if i % 3 == 0 {
15120                crate::diff::DiffAlgorithm::Histogram
15121            } else {
15122                crate::diff::DiffAlgorithm::Myers
15123            };
15124            let options = crate::diff::UnifiedDiffOptions::new()
15125                .with_paths("f.c", "f.c")
15126                .with_algorithm(algorithm)
15127                .with_extent(if i % 2 == 0 {
15128                    crate::diff::HunkExtent::Function
15129                } else {
15130                    crate::diff::HunkExtent::Context
15131                })
15132                .with_indent_heuristic(if i % 5 == 0 {
15133                    crate::diff::IndentHeuristic::Enabled
15134                } else {
15135                    crate::diff::IndentHeuristic::Disabled
15136                })
15137                .with_path_quoting(quoting);
15138            let _ = crate::diff::unified_diff(&old, &new, &options);
15139            let threshold = 30 + (i % 40);
15140            let raw = diff_trees(&odb, Some(&old_tree), Some(&new_tree), "")?;
15141            let opts = DiffPostprocessOptions {
15142                rename_threshold: threshold,
15143                copy_threshold: Some(threshold),
15144                find_copies_harder: i % 2 == 0,
15145                break_score: Some(threshold as u64),
15146                materialize_break_worktree_new_oid: i % 4 == 0,
15147            };
15148            let _ = postprocess_diff_entries(
15149                &odb,
15150                raw,
15151                Some(&old_tree),
15152                None,
15153                None,
15154                &config,
15155                &policy,
15156                &opts,
15157                None,
15158            )?;
15159        }
15160        Ok(())
15161    }
15162}
15163
15164#[cfg(test)]
15165mod refresh_checks_tests {
15166    use super::{DistinctFileAttrs, RefreshChecks, RefreshContent};
15167    use crate::crlf::{EolAttr, FileAttrs, TextAttr};
15168    use crate::objects::ObjectId;
15169
15170    fn attrs_with_eol(eol: EolAttr) -> FileAttrs {
15171        FileAttrs {
15172            eol,
15173            ..FileAttrs::default()
15174        }
15175    }
15176
15177    #[test]
15178    fn equal_attributes_share_one_slot() {
15179        let mut attrs = DistinctFileAttrs::default();
15180        assert_eq!(attrs.intern(FileAttrs::default()), 0);
15181        assert_eq!(attrs.intern(attrs_with_eol(EolAttr::Crlf)), 1);
15182        assert_eq!(attrs.intern(FileAttrs::default()), 0);
15183        assert_eq!(attrs.intern(attrs_with_eol(EolAttr::Crlf)), 1);
15184        let binary = FileAttrs {
15185            text: TextAttr::Unset,
15186            ..FileAttrs::default()
15187        };
15188        assert_eq!(attrs.intern(binary.clone()), 2);
15189        assert_eq!(attrs.intern(binary), 2);
15190        assert_eq!(attrs.0.len(), 3);
15191        assert_eq!(attrs.0[1], attrs_with_eol(EolAttr::Crlf));
15192    }
15193
15194    #[test]
15195    fn sets_past_the_search_limit_are_stored_again() {
15196        let mut attrs = DistinctFileAttrs::default();
15197        let first = attrs.intern(FileAttrs::default());
15198        for n in 0..DistinctFileAttrs::SEARCH_LIMIT {
15199            let distinct = FileAttrs {
15200                whitespace: Some(format!("tabwidth={}", n + 1)),
15201                ..FileAttrs::default()
15202            };
15203            assert_eq!(attrs.intern(distinct), n + 1);
15204        }
15205        let again = attrs.intern(FileAttrs::default());
15206        assert_ne!(again, first, "only the most recent sets are searched");
15207        assert_eq!(attrs.0[again], attrs.0[first]);
15208        assert_eq!(attrs.intern(FileAttrs::default()), again);
15209    }
15210
15211    #[test]
15212    fn empty_checks_answer_none_for_every_entry() {
15213        let checks = RefreshChecks::default();
15214        assert!(checks.0.is_empty());
15215        assert!(checks.get(0).is_none());
15216        assert!(checks.get(9_999).is_none());
15217    }
15218
15219    #[test]
15220    fn filled_checks_answer_by_entry_position() {
15221        let oid = ObjectId::from_bytes(&[7u8; 20]).unwrap();
15222        let other = ObjectId::from_bytes(&[8u8; 20]).unwrap();
15223        let checks = RefreshChecks(vec![None, Some(RefreshContent::Hashed(oid)), None]);
15224        assert!(checks.get(0).is_none());
15225        assert!(RefreshContent::matches(checks.get(1), &oid));
15226        assert!(RefreshContent::differs(checks.get(1), &other));
15227        assert!(
15228            checks.get(3).is_none(),
15229            "positions past the end have no check"
15230        );
15231    }
15232}