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_for_pathspecs_and_policy;
12use super::detail::{DiffDetail, DiffTotals, FileDiff, LineStat};
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,
28}
29
30#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct CommitFilesResult {
33 pub output: DiffFilesOutput,
35 pub parent_count: usize,
37}
38
39struct CommitWork {
40 tree: ObjectId,
41 parent_tree: Option<ObjectId>,
42 parent_count: usize,
43}
44
45fn rename_threshold(options: &DiffOptions) -> u32 {
46 options.renames.threshold_percent().unwrap_or(101)
47}
48
49fn filter_entries(
50 repo: &crate::repo::Repository,
51 entries: Vec<DiffEntry>,
52 pathspecs: &[String],
53) -> Result<Vec<DiffEntry>> {
54 if pathspecs.is_empty() {
55 return Ok(entries);
56 }
57 let matcher = repo.pathspec_matcher(pathspecs)?;
58 Ok(entries
59 .into_iter()
60 .filter(|entry| {
61 let path = entry
62 .new_path
63 .as_deref()
64 .or(entry.old_path.as_deref())
65 .unwrap_or("");
66 matcher.matches_change(path, entry.old_path.as_deref())
67 })
68 .collect())
69}
70
71fn parent_tree_for_commit(
72 odb: &Odb,
73 parent: &ObjectId,
74 trees_by_commit: &HashMap<ObjectId, ObjectId>,
75) -> Result<ObjectId> {
76 if let Some(tree) = trees_by_commit.get(parent) {
77 return Ok(*tree);
78 }
79 let raw = odb.read(parent)?;
80 Ok(parse_commit(&raw.data)?.tree)
81}
82
83fn totals_from_patch_files(files: &[FileDiff]) -> DiffTotals {
84 let lines: LineStat = files
85 .iter()
86 .filter_map(|file| {
87 file.structured
88 .as_ref()
89 .map(|structured| LineStat::from_hunks(&structured.hunks))
90 })
91 .sum();
92 DiffTotals {
93 files_changed: files.len(),
94 insertions: lines.insertions,
95 deletions: lines.deletions,
96 }
97}
98
99fn totals_for_detail(files: &[FileDiff], detail: DiffDetail) -> Option<DiffTotals> {
100 match detail {
101 DiffDetail::Stat => Some(DiffTotals::from_files(files)),
102 DiffDetail::Patch => Some(totals_from_patch_files(files)),
103 DiffDetail::NameOnly | DiffDetail::NameStatus => None,
104 }
105}
106
107fn empty_merge_output(detail: DiffDetail) -> DiffFilesOutput {
108 let totals = match detail {
109 DiffDetail::NameOnly | DiffDetail::NameStatus => None,
110 DiffDetail::Stat | DiffDetail::Patch => Some(DiffTotals {
111 files_changed: 0,
112 insertions: 0,
113 deletions: 0,
114 }),
115 };
116 DiffFilesOutput {
117 files: Vec::new(),
118 totals,
119 inexact_rename_skipped: None,
120 }
121}
122
123fn commit_files_many_parallel_options(
129 thread_policy: &ThreadPolicy,
130 config: &ConfigSet,
131) -> ParallelOptions {
132 let batch_policy = if thread_policy.threads_override == Some(1) {
133 *thread_policy
134 } else {
135 ThreadPolicy {
136 threads_override: thread_policy.threads_override.or(Some(0)),
137 min_items_override: thread_policy.min_items_override,
138 min_total_bytes_override: thread_policy.min_total_bytes_override,
139 }
140 };
141 let mut par_opts = batch_policy.resolve(ParallelOp::DiffContent, config);
142 par_opts.min_items = par_opts.min_items.min(2);
143 par_opts.min_total_bytes = None;
144 par_opts
145}
146
147fn diff_one_commit(
148 diff: &Diff,
149 work: &CommitWork,
150 pathspecs: &[String],
151 opts: &CommitFilesManyOptions,
152 config: &ConfigSet,
153 thread_policy: &ThreadPolicy,
154) -> Result<CommitFilesResult> {
155 if opts.skip_merge_patches && work.parent_count > 1 {
156 return Ok(CommitFilesResult {
157 output: empty_merge_output(opts.detail),
158 parent_count: work.parent_count,
159 });
160 }
161
162 let repo = diff.repository();
163 let odb = repo.odb();
164 let pathspec_matcher = if pathspecs.is_empty() {
165 None
166 } else {
167 Some(repo.pathspec_matcher(pathspecs)?)
168 };
169 let mut entries = diff_trees_with_renames_for_pathspecs_and_policy(
170 odb,
171 work.parent_tree.as_ref(),
172 Some(&work.tree),
173 "",
174 pathspecs,
175 pathspec_matcher.as_ref(),
176 rename_threshold(&opts.diff_options),
177 config,
178 thread_policy,
179 )?;
180 if rename_threshold(&opts.diff_options) >= 100 {
181 super::sort_entries_by_path(&mut entries);
182 }
183 entries = filter_entries(diff.repository(), entries, pathspecs)?;
184 let mut output = build_file_diffs(
185 odb,
186 entries,
187 opts.detail,
188 opts.diff_options
189 .clone()
190 .with_renames(crate::diff::RenameDetection::Off),
191 config,
192 thread_policy,
193 )?;
194 if output.totals.is_none() {
195 output.totals = totals_for_detail(&output.files, opts.detail);
196 }
197 Ok(CommitFilesResult {
198 output,
199 parent_count: work.parent_count,
200 })
201}
202
203#[derive(Debug, Clone)]
205pub struct CommitFilesManyInput {
206 pub oid: ObjectId,
208 pub parents: Vec<ObjectId>,
210 pub tree: ObjectId,
212}
213
214impl Diff<'_> {
215 pub fn commit_files_many(
227 &self,
228 oids: &[ObjectId],
229 pathspec: &crate::pathspec::Pathspec,
230 opts: CommitFilesManyOptions,
231 config: &ConfigSet,
232 thread_policy: &ThreadPolicy,
233 ) -> Result<Vec<CommitFilesResult>> {
234 if oids.is_empty() {
235 return Ok(Vec::new());
236 }
237 let repo = self.repository();
238 let odb = repo.odb();
239 let parent_ctx = crate::shallow::EffectiveCommitParents::load(repo);
240 let inputs = oids
241 .iter()
242 .map(|oid| {
243 let commit = odb.read_commit(oid)?;
244 Ok(CommitFilesManyInput {
245 oid: *oid,
246 parents: parent_ctx.for_commit(*oid, &commit.parents),
247 tree: commit.tree,
248 })
249 })
250 .collect::<Result<Vec<_>>>()?;
251 self.commit_files_many_work(&inputs, pathspec, opts, config, thread_policy)
252 }
253
254 pub fn commit_line_totals(
274 &self,
275 oids: &[ObjectId],
276 pathspec: &crate::pathspec::Pathspec,
277 diff_options: DiffOptions,
278 config: &ConfigSet,
279 thread_policy: &ThreadPolicy,
280 ) -> Result<Vec<Option<DiffTotals>>> {
281 let repo = self.repository();
282 let odb = repo.odb();
283 let parent_ctx = crate::shallow::EffectiveCommitParents::load(repo);
284 let mut inputs = Vec::with_capacity(oids.len());
285 let mut slots = Vec::with_capacity(oids.len());
286 for oid in oids {
287 let commit = odb.read_commit(oid)?;
288 let parents = parent_ctx.for_commit(*oid, &commit.parents);
289 if parents.len() > 1 {
290 slots.push(None);
291 continue;
292 }
293 slots.push(Some(inputs.len()));
294 inputs.push(CommitFilesManyInput {
295 oid: *oid,
296 parents,
297 tree: commit.tree,
298 });
299 }
300 let opts = CommitFilesManyOptions {
301 diff_options,
302 detail: DiffDetail::Stat,
303 skip_merge_patches: true,
304 };
305 let results =
306 self.commit_files_many_work(&inputs, pathspec, opts, config, thread_policy)?;
307 Ok(slots
308 .into_iter()
309 .map(|slot| {
310 let output = &results.get(slot?)?.output;
311 Some(
312 output
313 .totals
314 .unwrap_or_else(|| DiffTotals::from_files(&output.files)),
315 )
316 })
317 .collect())
318 }
319
320 pub fn commit_files_many_work(
326 &self,
327 inputs: &[CommitFilesManyInput],
328 pathspec: &crate::pathspec::Pathspec,
329 opts: CommitFilesManyOptions,
330 config: &ConfigSet,
331 thread_policy: &ThreadPolicy,
332 ) -> Result<Vec<CommitFilesResult>> {
333 let pathspecs = pathspec.specs();
334 if inputs.is_empty() {
335 return Ok(Vec::new());
336 }
337 let repo = self.repository();
338 let odb = repo.odb();
339 let mut trees_by_commit: HashMap<ObjectId, ObjectId> = HashMap::with_capacity(inputs.len());
340 for input in inputs {
341 trees_by_commit.insert(input.oid, input.tree);
342 }
343
344 let mut work_items: Vec<CommitWork> = Vec::with_capacity(inputs.len());
345 for (i, input) in inputs.iter().enumerate() {
346 let parent_count = input.parents.len();
347 let parent_tree = if parent_count == 0 {
348 None
349 } else if i + 1 < inputs.len() && input.parents[0] == inputs[i + 1].oid {
350 Some(inputs[i + 1].tree)
351 } else {
352 Some(parent_tree_for_commit(
353 odb,
354 &input.parents[0],
355 &trees_by_commit,
356 )?)
357 };
358 work_items.push(CommitWork {
359 tree: input.tree,
360 parent_tree,
361 parent_count,
362 });
363 }
364
365 odb.with_pack_read_context(|| {
366 let par_opts = commit_files_many_parallel_options(thread_policy, config);
367 try_par_map(&work_items, par_opts, 0, |work| {
368 diff_one_commit(self, work, pathspecs, &opts, config, thread_policy)
369 })
370 .map_err(|err| match err {
371 ParallelHashError::Task(e) => e,
372 })
373 })
374 }
375}
376
377#[cfg(test)]
378mod tests {
379 use super::*;
380 use crate::objects::{serialize_commit, CommitData, ObjectKind, TreeEntry};
381 use crate::repo::{InitOptions, Repository};
382
383 fn tree_with(repo: &Repository, entries: &[(&str, &[u8])]) -> ObjectId {
384 let mut tes = Vec::new();
385 for (name, data) in entries {
386 let oid = repo.odb().write(ObjectKind::Blob, data).expect("blob");
387 tes.push(TreeEntry {
388 mode: 0o100644,
389 name: name.as_bytes().to_vec(),
390 oid,
391 });
392 }
393 let data = crate::objects::serialize_tree(&tes);
394 repo.odb().write(ObjectKind::Tree, &data).expect("tree")
395 }
396
397 fn write_commit(
398 repo: &Repository,
399 tree: ObjectId,
400 parents: Vec<ObjectId>,
401 msg: &str,
402 ) -> ObjectId {
403 let ident = "A <a@example.com> 0 +0000";
404 let commit = CommitData {
405 tree,
406 parents,
407 author: ident.into(),
408 committer: ident.into(),
409 author_raw: Vec::new(),
410 committer_raw: Vec::new(),
411 encoding: None,
412 message: format!("{msg}\n"),
413 raw_message: None,
414 extra_headers: Vec::new(),
415 };
416 repo.odb()
417 .write(ObjectKind::Commit, &serialize_commit(&commit))
418 .expect("commit")
419 }
420
421 #[test]
422 fn commit_files_many_matches_single_commit_calls() -> Result<()> {
423 let dir = tempfile::tempdir().expect("tempdir");
424 let (repo, _) = Repository::init(dir.path(), &InitOptions::default())?;
425 let tree1 = tree_with(&repo, &[("f.txt", b"a\n")]);
426 let root = write_commit(&repo, tree1, vec![], "root");
427 let tree2 = tree_with(&repo, &[("f.txt", b"b\n")]);
428 let child = write_commit(&repo, tree2, vec![root], "child");
429
430 let config = ConfigSet::default();
431 let policy = ThreadPolicy::default();
432 let diff_options = DiffOptions::default();
433 let opts = CommitFilesManyOptions {
434 diff_options,
435 detail: DiffDetail::Stat,
436 skip_merge_patches: true,
437 };
438
439 let many =
440 repo.diff().commit_files_many(
441 &[child],
442 &crate::pathspec::Pathspec::entire_tree(
443 crate::pathspec::PathspecGlobalFlags::default(),
444 ),
445 opts.clone(),
446 &config,
447 &policy,
448 )?;
449 assert_eq!(many.len(), 1);
450
451 let entries = diff_trees_with_renames_for_pathspecs_and_policy(
452 repo.odb(),
453 Some(&tree1),
454 Some(&tree2),
455 "",
456 &[],
457 None,
458 101,
459 &config,
460 &policy,
461 )?;
462 let single = build_file_diffs(
463 repo.odb(),
464 entries,
465 opts.detail,
466 opts.diff_options
467 .clone()
468 .with_renames(crate::diff::RenameDetection::Off),
469 &config,
470 &policy,
471 )?;
472 assert_eq!(many[0].output.files, single.files);
473
474 let policy_serial = ThreadPolicy::with_threads_override(Some(1));
475 let policy_par = ThreadPolicy::with_threads_override(Some(4))
476 .min_items_override(Some(1))
477 .min_total_bytes_override(Some(0));
478 let tree3 = tree_with(&repo, &[("f.txt", b"c\n")]);
479 let mid = write_commit(&repo, tree3, vec![child], "mid");
480 let tree4 = tree_with(&repo, &[("f.txt", b"d\n")]);
481 let tip = write_commit(&repo, tree4, vec![mid], "tip");
482 let batch = [tip, mid, child];
483 let many_par =
484 repo.diff().commit_files_many(
485 &batch,
486 &crate::pathspec::Pathspec::entire_tree(
487 crate::pathspec::PathspecGlobalFlags::default(),
488 ),
489 opts.clone(),
490 &config,
491 &policy_par,
492 )?;
493 let many_serial =
494 repo.diff().commit_files_many(
495 &batch,
496 &crate::pathspec::Pathspec::entire_tree(
497 crate::pathspec::PathspecGlobalFlags::default(),
498 ),
499 opts,
500 &config,
501 &policy_serial,
502 )?;
503 assert_eq!(many_par.len(), 3);
504 assert_eq!(many_par, many_serial);
505 Ok(())
506 }
507
508 #[test]
509 fn commit_line_totals_skip_merges_and_honor_pathspecs() -> Result<()> {
510 let dir = tempfile::tempdir().expect("tempdir");
511 let (repo, _) = Repository::init(dir.path(), &InitOptions::default())?;
512 let root_tree = tree_with(&repo, &[("a.txt", b"1\n2\n"), ("b.txt", b"x\n")]);
513 let root = write_commit(&repo, root_tree, vec![], "root");
514 let edit_tree = tree_with(&repo, &[("a.txt", b"1\nchanged\n3\n"), ("b.txt", b"x\n")]);
515 let edit = write_commit(&repo, edit_tree, vec![root], "edit a");
516 let side_tree = tree_with(&repo, &[("a.txt", b"1\n2\n"), ("b.txt", b"y\n")]);
517 let side = write_commit(&repo, side_tree, vec![root], "edit b");
518 let merge = write_commit(&repo, edit_tree, vec![edit, side], "merge");
519 let binary_tree = tree_with(
520 &repo,
521 &[
522 ("a.txt", b"1\nchanged\n3\n"),
523 ("b.txt", b"x\n"),
524 ("c.bin", b"\0"),
525 ],
526 );
527 let binary = write_commit(&repo, binary_tree, vec![merge], "add binary");
528 let oids = [binary, merge, side, edit, root];
529
530 let config = ConfigSet::default();
531 let totals =
532 repo.diff().commit_line_totals(
533 &oids,
534 &crate::pathspec::Pathspec::entire_tree(
535 crate::pathspec::PathspecGlobalFlags::default(),
536 ),
537 DiffOptions::default(),
538 &config,
539 &ThreadPolicy::default(),
540 )?;
541 let lines = |t: Option<DiffTotals>| t.map(|t| (t.files_changed, t.insertions, t.deletions));
542 assert_eq!(
543 totals.into_iter().map(lines).collect::<Vec<_>>(),
544 vec![
545 Some((1, 0, 0)),
546 None,
547 Some((1, 1, 1)),
548 Some((1, 2, 1)),
549 Some((2, 3, 0)),
550 ]
551 );
552
553 let only_b = repo.diff().commit_line_totals(
554 &oids,
555 &crate::pathspec::Pathspec::scoped(
556 ["b.txt"],
557 crate::pathspec::PathspecGlobalFlags::default(),
558 ),
559 DiffOptions::default(),
560 &config,
561 &ThreadPolicy::with_threads_override(Some(1)),
562 )?;
563 assert_eq!(
564 only_b.into_iter().map(lines).collect::<Vec<_>>(),
565 vec![
566 Some((0, 0, 0)),
567 None,
568 Some((1, 1, 1)),
569 Some((0, 0, 0)),
570 Some((1, 1, 0)),
571 ]
572 );
573
574 assert!(repo
575 .diff()
576 .commit_line_totals(
577 &[root_tree],
578 &crate::pathspec::Pathspec::entire_tree(
579 crate::pathspec::PathspecGlobalFlags::default()
580 ),
581 DiffOptions::default(),
582 &config,
583 &ThreadPolicy::default(),
584 )
585 .is_err());
586 Ok(())
587 }
588
589 #[test]
590 fn patch_totals_match_hunk_line_counts() -> Result<()> {
591 let dir = tempfile::tempdir().expect("tempdir");
592 let (repo, _) = Repository::init(dir.path(), &InitOptions::default())?;
593 let tree1 = tree_with(&repo, &[("a.txt", b"one\n")]);
594 let root = write_commit(&repo, tree1, vec![], "root");
595 let tree2 = tree_with(&repo, &[("a.txt", b"one\ntwo\n")]);
596 let child = write_commit(&repo, tree2, vec![root], "add line");
597
598 let config = ConfigSet::default();
599 let policy = ThreadPolicy::default();
600 let opts = CommitFilesManyOptions {
601 diff_options: DiffOptions::default(),
602 detail: DiffDetail::Patch,
603 skip_merge_patches: true,
604 };
605 let results =
606 repo.diff().commit_files_many(
607 &[child],
608 &crate::pathspec::Pathspec::entire_tree(
609 crate::pathspec::PathspecGlobalFlags::default(),
610 ),
611 opts.clone(),
612 &config,
613 &policy,
614 )?;
615 let totals = results[0].output.totals.expect("patch totals");
616 assert_eq!(
617 (totals.files_changed, totals.insertions, totals.deletions),
618 (1, 1, 0)
619 );
620
621 let name_status_opts = CommitFilesManyOptions {
622 detail: DiffDetail::NameStatus,
623 ..opts.clone()
624 };
625 let name_status =
626 repo.diff().commit_files_many(
627 &[child],
628 &crate::pathspec::Pathspec::entire_tree(
629 crate::pathspec::PathspecGlobalFlags::default(),
630 ),
631 name_status_opts,
632 &config,
633 &policy,
634 )?;
635 assert!(name_status[0].output.totals.is_none());
636 Ok(())
637 }
638
639 #[test]
640 fn skip_merge_patches_empties_stat_and_name_modes() -> Result<()> {
641 let dir = tempfile::tempdir().expect("tempdir");
642 let (repo, _) = Repository::init(dir.path(), &InitOptions::default())?;
643 let base_tree = tree_with(&repo, &[("a.txt", b"one\n")]);
644 let base = write_commit(&repo, base_tree, vec![], "base");
645 let side_tree = tree_with(&repo, &[("a.txt", b"one\n"), ("b.txt", b"side\n")]);
646 let side = write_commit(&repo, side_tree, vec![base], "side");
647 let main_tree = tree_with(&repo, &[("a.txt", b"one\ntwo\n")]);
648 let main = write_commit(&repo, main_tree, vec![base], "main");
649 let merge = write_commit(&repo, side_tree, vec![main, side], "merge");
650
651 let config = ConfigSet::default();
652 let policy = ThreadPolicy::default();
653 let base_opts = CommitFilesManyOptions {
654 diff_options: DiffOptions::default(),
655 detail: DiffDetail::Stat,
656 skip_merge_patches: true,
657 };
658
659 for detail in [
660 DiffDetail::Patch,
661 DiffDetail::Stat,
662 DiffDetail::NameOnly,
663 DiffDetail::NameStatus,
664 ] {
665 let opts = CommitFilesManyOptions {
666 detail,
667 ..base_opts.clone()
668 };
669 let results = repo.diff().commit_files_many(
670 &[merge],
671 &crate::pathspec::Pathspec::entire_tree(
672 crate::pathspec::PathspecGlobalFlags::default(),
673 ),
674 opts,
675 &config,
676 &policy,
677 )?;
678 assert!(
679 results[0].output.files.is_empty(),
680 "merge files should be empty for {detail:?}"
681 );
682 match detail {
683 DiffDetail::NameOnly | DiffDetail::NameStatus => {
684 assert!(results[0].output.totals.is_none());
685 }
686 DiffDetail::Patch | DiffDetail::Stat => {
687 assert_eq!(
688 results[0].output.totals,
689 Some(DiffTotals {
690 files_changed: 0,
691 insertions: 0,
692 deletions: 0,
693 })
694 );
695 }
696 }
697 }
698 Ok(())
699 }
700}