Skip to main content

grit_lib/diff/
commit_files_many.rs

1//! Per-commit file diffs for many commits (used by `grit log -p` / `--stat`).
2
3use std::collections::HashMap;
4
5use crate::config::ConfigSet;
6use crate::error::Result;
7use crate::objects::{parse_commit, ObjectId};
8use crate::odb::Odb;
9use crate::parallel::{try_par_map, ParallelHashError, ParallelOp, ParallelOptions, ThreadPolicy};
10
11use super::core::diff_trees_with_renames_and_policy;
12use super::detail::{DiffDetail, DiffTotals};
13use super::files::{build_file_diffs, DiffFilesOutput};
14use super::options::DiffOptions;
15use super::Diff;
16use super::DiffEntry;
17
18/// Options for [`Diff::commit_files_many`].
19#[derive(Debug, Clone)]
20pub struct CommitFilesManyOptions {
21    /// Line diff and rename settings.
22    pub diff_options: DiffOptions,
23    /// How much to compute per changed file.
24    pub detail: DiffDetail,
25    /// When set with [`DiffDetail::Patch`], merge commits (2+ parents) produce empty file lists.
26    pub skip_merge_patches: bool,
27}
28
29/// One commit's diff output in [`Diff::commit_files_many`] (same order as input oids).
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct CommitFilesResult {
32    /// Per-file diff bodies for this commit.
33    pub output: DiffFilesOutput,
34    /// Number of parents (0 = root).
35    pub parent_count: usize,
36}
37
38struct CommitWork {
39    tree: ObjectId,
40    parent_tree: Option<ObjectId>,
41    parent_count: usize,
42}
43
44fn rename_threshold(options: &DiffOptions) -> u32 {
45    options.renames.threshold_percent().unwrap_or(101)
46}
47
48fn filter_entries(
49    repo: &crate::repo::Repository,
50    entries: Vec<DiffEntry>,
51    pathspecs: &[String],
52) -> Result<Vec<DiffEntry>> {
53    if pathspecs.is_empty() {
54        return Ok(entries);
55    }
56    let matcher = repo.pathspec_matcher(pathspecs)?;
57    Ok(entries
58        .into_iter()
59        .filter(|entry| {
60            let path = entry
61                .new_path
62                .as_deref()
63                .or(entry.old_path.as_deref())
64                .unwrap_or("");
65            matcher.matches_change(path, entry.old_path.as_deref())
66        })
67        .collect())
68}
69
70fn parent_tree_for_commit(
71    odb: &Odb,
72    parent: &ObjectId,
73    trees_by_commit: &HashMap<ObjectId, ObjectId>,
74) -> Result<ObjectId> {
75    if let Some(tree) = trees_by_commit.get(parent) {
76        return Ok(*tree);
77    }
78    let raw = odb.read(parent)?;
79    Ok(parse_commit(&raw.data)?.tree)
80}
81
82fn totals_for_detail(files: &[super::detail::FileDiff], detail: DiffDetail) -> Option<DiffTotals> {
83    match detail {
84        DiffDetail::Stat | DiffDetail::Patch => Some(DiffTotals::from_files(files)),
85        DiffDetail::NameOnly | DiffDetail::NameStatus => Some(DiffTotals {
86            files_changed: files.len(),
87            insertions: 0,
88            deletions: 0,
89        }),
90    }
91}
92
93/// Parallel options for diffing many commits in one batch.
94///
95/// Honors [`ThreadPolicy::threads_override`] `Some(1)` (serial). When the caller did not
96/// cap threads, uses auto worker count so multi-commit batches can run in parallel like
97/// Git's `log -p`. Per-commit work still uses the caller's original policy.
98fn commit_files_many_parallel_options(
99    thread_policy: &ThreadPolicy,
100    config: &ConfigSet,
101) -> ParallelOptions {
102    let batch_policy = if thread_policy.threads_override == Some(1) {
103        *thread_policy
104    } else {
105        ThreadPolicy {
106            threads_override: thread_policy.threads_override.or(Some(0)),
107            min_items_override: thread_policy.min_items_override,
108            min_total_bytes_override: thread_policy.min_total_bytes_override,
109        }
110    };
111    let mut par_opts = batch_policy.resolve(ParallelOp::DiffContent, config);
112    par_opts.min_items = par_opts.min_items.min(2);
113    par_opts.min_total_bytes = None;
114    par_opts
115}
116
117fn diff_one_commit(
118    diff: &Diff,
119    work: &CommitWork,
120    pathspecs: &[String],
121    opts: &CommitFilesManyOptions,
122    config: &ConfigSet,
123    thread_policy: &ThreadPolicy,
124) -> Result<CommitFilesResult> {
125    if opts.skip_merge_patches && opts.detail == DiffDetail::Patch && work.parent_count > 1 {
126        return Ok(CommitFilesResult {
127            output: DiffFilesOutput {
128                files: Vec::new(),
129                totals: Some(DiffTotals {
130                    files_changed: 0,
131                    insertions: 0,
132                    deletions: 0,
133                }),
134            },
135            parent_count: work.parent_count,
136        });
137    }
138
139    let odb = diff.repository().odb();
140    let mut entries = diff_trees_with_renames_and_policy(
141        odb,
142        work.parent_tree.as_ref(),
143        Some(&work.tree),
144        "",
145        rename_threshold(&opts.diff_options),
146        config,
147        thread_policy,
148    )?;
149    if rename_threshold(&opts.diff_options) >= 100 {
150        entries.sort_by(|a, b| a.path().cmp(b.path()));
151    }
152    entries = filter_entries(diff.repository(), entries, pathspecs)?;
153    let mut output = build_file_diffs(
154        odb,
155        entries,
156        opts.detail,
157        opts.diff_options.clone(),
158        config,
159        thread_policy,
160    )?;
161    if output.totals.is_none() {
162        output.totals = totals_for_detail(&output.files, opts.detail);
163    }
164    Ok(CommitFilesResult {
165        output,
166        parent_count: work.parent_count,
167    })
168}
169
170/// One commit's identity for [`Diff::commit_files_many_work`], avoiding a second ODB read.
171#[derive(Debug, Clone)]
172pub struct CommitFilesManyInput {
173    /// Commit object id (order matches the log walk).
174    pub oid: ObjectId,
175    /// First-parent chain and other parents from the commit object.
176    pub parents: Vec<ObjectId>,
177    /// Tree object id for this commit.
178    pub tree: ObjectId,
179}
180
181impl Diff<'_> {
182    /// Build per-commit file diffs for a slice of commit oids, preserving input order.
183    ///
184    /// Parent trees are resolved once and reused across consecutive commits (same as
185    /// [`super::write_commits_log_patch`]). When `oids` are newest-first and history is
186    /// linear, each commit's first parent is the next oid and its tree is reused without
187    /// an extra read. Work runs in parallel when [`ThreadPolicy`] allows and the batch is
188    /// large enough to amortize pool startup.
189    ///
190    /// # Errors
191    ///
192    /// Propagates object read, tree diff, and parallel pool errors.
193    pub fn commit_files_many(
194        &self,
195        oids: &[ObjectId],
196        pathspecs: &[String],
197        opts: CommitFilesManyOptions,
198        config: &ConfigSet,
199        thread_policy: &ThreadPolicy,
200    ) -> Result<Vec<CommitFilesResult>> {
201        if oids.is_empty() {
202            return Ok(Vec::new());
203        }
204        let odb = self.repository().odb();
205        let mut inputs = Vec::with_capacity(oids.len());
206        for oid in oids {
207            let raw = odb.read(oid)?;
208            let commit = parse_commit(&raw.data)?;
209            inputs.push(CommitFilesManyInput {
210                oid: *oid,
211                parents: commit.parents.clone(),
212                tree: commit.tree,
213            });
214        }
215        self.commit_files_many_work(&inputs, pathspecs, opts, config, thread_policy)
216    }
217
218    /// Like [`Self::commit_files_many`] when commit trees and parents are already loaded.
219    ///
220    /// # Errors
221    ///
222    /// Propagates object read, tree diff, and parallel pool errors.
223    pub fn commit_files_many_work(
224        &self,
225        inputs: &[CommitFilesManyInput],
226        pathspecs: &[String],
227        opts: CommitFilesManyOptions,
228        config: &ConfigSet,
229        thread_policy: &ThreadPolicy,
230    ) -> Result<Vec<CommitFilesResult>> {
231        if inputs.is_empty() {
232            return Ok(Vec::new());
233        }
234        let repo = self.repository();
235        let odb = repo.odb();
236        let mut trees_by_commit: HashMap<ObjectId, ObjectId> = HashMap::with_capacity(inputs.len());
237        for input in inputs {
238            trees_by_commit.insert(input.oid, input.tree);
239        }
240
241        let mut work_items: Vec<CommitWork> = Vec::with_capacity(inputs.len());
242        for (i, input) in inputs.iter().enumerate() {
243            let parent_count = input.parents.len();
244            let parent_tree = if parent_count == 0 {
245                None
246            } else if i + 1 < inputs.len() && input.parents[0] == inputs[i + 1].oid {
247                Some(inputs[i + 1].tree)
248            } else {
249                Some(parent_tree_for_commit(
250                    odb,
251                    &input.parents[0],
252                    &trees_by_commit,
253                )?)
254            };
255            work_items.push(CommitWork {
256                tree: input.tree,
257                parent_tree,
258                parent_count,
259            });
260        }
261
262        odb.with_pack_read_context(|| {
263            let par_opts = commit_files_many_parallel_options(thread_policy, config);
264            try_par_map(&work_items, par_opts, 0, |work| {
265                diff_one_commit(self, work, pathspecs, &opts, config, thread_policy)
266            })
267            .map_err(|err| match err {
268                ParallelHashError::Task(e) => e,
269            })
270        })
271    }
272}
273
274#[cfg(test)]
275mod tests {
276    use super::*;
277    use crate::objects::{serialize_commit, CommitData, ObjectKind, TreeEntry};
278    use crate::repo::{InitOptions, Repository};
279
280    fn tree_with(repo: &Repository, entries: &[(&str, &[u8])]) -> ObjectId {
281        let mut tes = Vec::new();
282        for (name, data) in entries {
283            let oid = repo.odb().write(ObjectKind::Blob, data).expect("blob");
284            tes.push(TreeEntry {
285                mode: 0o100644,
286                name: name.as_bytes().to_vec(),
287                oid,
288            });
289        }
290        let data = crate::objects::serialize_tree(&tes);
291        repo.odb().write(ObjectKind::Tree, &data).expect("tree")
292    }
293
294    fn write_commit(
295        repo: &Repository,
296        tree: ObjectId,
297        parents: Vec<ObjectId>,
298        msg: &str,
299    ) -> ObjectId {
300        let ident = "A <a@example.com> 0 +0000";
301        let commit = CommitData {
302            tree,
303            parents,
304            author: ident.into(),
305            committer: ident.into(),
306            author_raw: Vec::new(),
307            committer_raw: Vec::new(),
308            encoding: None,
309            message: format!("{msg}\n"),
310            raw_message: None,
311            extra_headers: Vec::new(),
312        };
313        repo.odb()
314            .write(ObjectKind::Commit, &serialize_commit(&commit))
315            .expect("commit")
316    }
317
318    #[test]
319    fn commit_files_many_matches_single_commit_calls() -> Result<()> {
320        let dir = tempfile::tempdir().expect("tempdir");
321        let repo = Repository::init(dir.path(), &InitOptions::default())?;
322        let tree1 = tree_with(&repo, &[("f.txt", b"a\n")]);
323        let root = write_commit(&repo, tree1, vec![], "root");
324        let tree2 = tree_with(&repo, &[("f.txt", b"b\n")]);
325        let child = write_commit(&repo, tree2, vec![root], "child");
326
327        let config = ConfigSet::default();
328        let policy = ThreadPolicy::default();
329        let diff_options = DiffOptions::default();
330        let opts = CommitFilesManyOptions {
331            diff_options,
332            detail: DiffDetail::Stat,
333            skip_merge_patches: true,
334        };
335
336        let many = repo
337            .diff()
338            .commit_files_many(&[child], &[], opts.clone(), &config, &policy)?;
339        assert_eq!(many.len(), 1);
340
341        let entries = diff_trees_with_renames_and_policy(
342            repo.odb(),
343            Some(&tree1),
344            Some(&tree2),
345            "",
346            101,
347            &config,
348            &policy,
349        )?;
350        let single = build_file_diffs(
351            repo.odb(),
352            entries,
353            opts.detail,
354            opts.diff_options.clone(),
355            &config,
356            &policy,
357        )?;
358        assert_eq!(many[0].output.files, single.files);
359
360        let policy_serial = ThreadPolicy::with_threads_override(Some(1));
361        let policy_par = ThreadPolicy::with_threads_override(Some(4))
362            .min_items_override(Some(1))
363            .min_total_bytes_override(Some(0));
364        let tree3 = tree_with(&repo, &[("f.txt", b"c\n")]);
365        let mid = write_commit(&repo, tree3, vec![child], "mid");
366        let tree4 = tree_with(&repo, &[("f.txt", b"d\n")]);
367        let tip = write_commit(&repo, tree4, vec![mid], "tip");
368        let batch = [tip, mid, child];
369        let many_par =
370            repo.diff()
371                .commit_files_many(&batch, &[], opts.clone(), &config, &policy_par)?;
372        let many_serial =
373            repo.diff()
374                .commit_files_many(&batch, &[], opts, &config, &policy_serial)?;
375        assert_eq!(many_par.len(), 3);
376        assert_eq!(many_par, many_serial);
377        Ok(())
378    }
379}