1use 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#[derive(Debug, Clone)]
20pub struct CommitFilesManyOptions {
21 pub diff_options: DiffOptions,
23 pub detail: DiffDetail,
25 pub skip_merge_patches: bool,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
31pub struct CommitFilesResult {
32 pub output: DiffFilesOutput,
34 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
93fn 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 super::sort_entries_by_path(&mut entries);
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
158 .clone()
159 .with_renames(crate::diff::RenameDetection::Off),
160 config,
161 thread_policy,
162 )?;
163 if output.totals.is_none() {
164 output.totals = totals_for_detail(&output.files, opts.detail);
165 }
166 Ok(CommitFilesResult {
167 output,
168 parent_count: work.parent_count,
169 })
170}
171
172#[derive(Debug, Clone)]
174pub struct CommitFilesManyInput {
175 pub oid: ObjectId,
177 pub parents: Vec<ObjectId>,
179 pub tree: ObjectId,
181}
182
183impl Diff<'_> {
184 pub fn commit_files_many(
196 &self,
197 oids: &[ObjectId],
198 pathspecs: &[String],
199 opts: CommitFilesManyOptions,
200 config: &ConfigSet,
201 thread_policy: &ThreadPolicy,
202 ) -> Result<Vec<CommitFilesResult>> {
203 if oids.is_empty() {
204 return Ok(Vec::new());
205 }
206 let odb = self.repository().odb();
207 let mut inputs = Vec::with_capacity(oids.len());
208 for oid in oids {
209 let raw = odb.read(oid)?;
210 let commit = parse_commit(&raw.data)?;
211 inputs.push(CommitFilesManyInput {
212 oid: *oid,
213 parents: commit.parents.clone(),
214 tree: commit.tree,
215 });
216 }
217 self.commit_files_many_work(&inputs, pathspecs, opts, config, thread_policy)
218 }
219
220 pub fn commit_files_many_work(
226 &self,
227 inputs: &[CommitFilesManyInput],
228 pathspecs: &[String],
229 opts: CommitFilesManyOptions,
230 config: &ConfigSet,
231 thread_policy: &ThreadPolicy,
232 ) -> Result<Vec<CommitFilesResult>> {
233 if inputs.is_empty() {
234 return Ok(Vec::new());
235 }
236 let repo = self.repository();
237 let odb = repo.odb();
238 let mut trees_by_commit: HashMap<ObjectId, ObjectId> = HashMap::with_capacity(inputs.len());
239 for input in inputs {
240 trees_by_commit.insert(input.oid, input.tree);
241 }
242
243 let mut work_items: Vec<CommitWork> = Vec::with_capacity(inputs.len());
244 for (i, input) in inputs.iter().enumerate() {
245 let parent_count = input.parents.len();
246 let parent_tree = if parent_count == 0 {
247 None
248 } else if i + 1 < inputs.len() && input.parents[0] == inputs[i + 1].oid {
249 Some(inputs[i + 1].tree)
250 } else {
251 Some(parent_tree_for_commit(
252 odb,
253 &input.parents[0],
254 &trees_by_commit,
255 )?)
256 };
257 work_items.push(CommitWork {
258 tree: input.tree,
259 parent_tree,
260 parent_count,
261 });
262 }
263
264 odb.with_pack_read_context(|| {
265 let par_opts = commit_files_many_parallel_options(thread_policy, config);
266 try_par_map(&work_items, par_opts, 0, |work| {
267 diff_one_commit(self, work, pathspecs, &opts, config, thread_policy)
268 })
269 .map_err(|err| match err {
270 ParallelHashError::Task(e) => e,
271 })
272 })
273 }
274}
275
276#[cfg(test)]
277mod tests {
278 use super::*;
279 use crate::objects::{serialize_commit, CommitData, ObjectKind, TreeEntry};
280 use crate::repo::{InitOptions, Repository};
281
282 fn tree_with(repo: &Repository, entries: &[(&str, &[u8])]) -> ObjectId {
283 let mut tes = Vec::new();
284 for (name, data) in entries {
285 let oid = repo.odb().write(ObjectKind::Blob, data).expect("blob");
286 tes.push(TreeEntry {
287 mode: 0o100644,
288 name: name.as_bytes().to_vec(),
289 oid,
290 });
291 }
292 let data = crate::objects::serialize_tree(&tes);
293 repo.odb().write(ObjectKind::Tree, &data).expect("tree")
294 }
295
296 fn write_commit(
297 repo: &Repository,
298 tree: ObjectId,
299 parents: Vec<ObjectId>,
300 msg: &str,
301 ) -> ObjectId {
302 let ident = "A <a@example.com> 0 +0000";
303 let commit = CommitData {
304 tree,
305 parents,
306 author: ident.into(),
307 committer: ident.into(),
308 author_raw: Vec::new(),
309 committer_raw: Vec::new(),
310 encoding: None,
311 message: format!("{msg}\n"),
312 raw_message: None,
313 extra_headers: Vec::new(),
314 };
315 repo.odb()
316 .write(ObjectKind::Commit, &serialize_commit(&commit))
317 .expect("commit")
318 }
319
320 #[test]
321 fn commit_files_many_matches_single_commit_calls() -> Result<()> {
322 let dir = tempfile::tempdir().expect("tempdir");
323 let repo = Repository::init(dir.path(), &InitOptions::default())?;
324 let tree1 = tree_with(&repo, &[("f.txt", b"a\n")]);
325 let root = write_commit(&repo, tree1, vec![], "root");
326 let tree2 = tree_with(&repo, &[("f.txt", b"b\n")]);
327 let child = write_commit(&repo, tree2, vec![root], "child");
328
329 let config = ConfigSet::default();
330 let policy = ThreadPolicy::default();
331 let diff_options = DiffOptions::default();
332 let opts = CommitFilesManyOptions {
333 diff_options,
334 detail: DiffDetail::Stat,
335 skip_merge_patches: true,
336 };
337
338 let many = repo
339 .diff()
340 .commit_files_many(&[child], &[], opts.clone(), &config, &policy)?;
341 assert_eq!(many.len(), 1);
342
343 let entries = diff_trees_with_renames_and_policy(
344 repo.odb(),
345 Some(&tree1),
346 Some(&tree2),
347 "",
348 101,
349 &config,
350 &policy,
351 )?;
352 let single = build_file_diffs(
353 repo.odb(),
354 entries,
355 opts.detail,
356 opts.diff_options
357 .clone()
358 .with_renames(crate::diff::RenameDetection::Off),
359 &config,
360 &policy,
361 )?;
362 assert_eq!(many[0].output.files, single.files);
363
364 let policy_serial = ThreadPolicy::with_threads_override(Some(1));
365 let policy_par = ThreadPolicy::with_threads_override(Some(4))
366 .min_items_override(Some(1))
367 .min_total_bytes_override(Some(0));
368 let tree3 = tree_with(&repo, &[("f.txt", b"c\n")]);
369 let mid = write_commit(&repo, tree3, vec![child], "mid");
370 let tree4 = tree_with(&repo, &[("f.txt", b"d\n")]);
371 let tip = write_commit(&repo, tree4, vec![mid], "tip");
372 let batch = [tip, mid, child];
373 let many_par =
374 repo.diff()
375 .commit_files_many(&batch, &[], opts.clone(), &config, &policy_par)?;
376 let many_serial =
377 repo.diff()
378 .commit_files_many(&batch, &[], opts, &config, &policy_serial)?;
379 assert_eq!(many_par.len(), 3);
380 assert_eq!(many_par, many_serial);
381 Ok(())
382 }
383}