repo: Report merge stats against first parent
c60582374f2407de117d0acb8d1bf7b964110eec
Pass --first-parent to git log so merge commit statistics match the diff users review. Assisted-by: Codex:gpt-5.6-sol
1 parent
444c45c9
crates/repo/src/git.rs
+44 -2
| 553 | 553 | } |
|
| 554 | 554 | ||
| 555 | 555 | /// Returns each commit's aggregated diff size, in the given order. |
|
| 556 | 556 | /// |
|
| 557 | 557 | /// All stats come from one `git log --shortstat` invocation so callers can |
|
| 558 | - | /// size a whole review range without a subprocess per commit. Commits with |
|
| 559 | - | /// no shortstat output (empty diffs, unwalked merges) report zero stats. |
|
| 558 | + | /// size a whole review range without a subprocess per commit. Merge commits |
|
| 559 | + | /// are compared to their first parent, matching [`Self::commit_diff`]. |
|
| 560 | 560 | fn commit_stats(&self, commits: &[Oid]) -> Result<Vec<DiffStat>, RepoError> { |
|
| 561 | 561 | if commits.is_empty() { |
|
| 562 | 562 | return Ok(Vec::new()); |
|
| 563 | 563 | } |
|
| 564 | 564 | let mut args = vec![ |
|
| 565 | 565 | "--no-walk=unsorted".to_owned(), |
|
| 566 | + | "--first-parent".to_owned(), |
|
| 566 | 567 | "--format=%H".to_owned(), |
|
| 567 | 568 | "--shortstat".to_owned(), |
|
| 568 | 569 | ]; |
|
| 569 | 570 | args.extend(commits.iter().map(Oid::to_string)); |
|
| 570 | 571 | let args = args.iter().map(String::as_str).collect::<Vec<_>>(); |
| 1050 | 1051 | repository.branch_base(&target).unwrap(), |
|
| 1051 | 1052 | Some(repository.commit_id(&integration).unwrap()) |
|
| 1052 | 1053 | ); |
|
| 1053 | 1054 | } |
|
| 1054 | 1055 | ||
| 1056 | + | #[test] |
|
| 1057 | + | fn commit_stats_uses_merge_first_parent() { |
|
| 1058 | + | let directory = TempIndex::new().unwrap(); |
|
| 1059 | + | let repository_path = directory.path().parent().unwrap(); |
|
| 1060 | + | let status = Command::new("git") |
|
| 1061 | + | .args(["init", "-q", "-b", "main"]) |
|
| 1062 | + | .arg(repository_path) |
|
| 1063 | + | .status() |
|
| 1064 | + | .unwrap(); |
|
| 1065 | + | assert!(status.success()); |
|
| 1066 | + | let repository = Repository::discover(repository_path.to_owned()).unwrap(); |
|
| 1067 | + | fs::write(repository_path.join("base"), "base\n").unwrap(); |
|
| 1068 | + | git(&repository, "add", &["base"]); |
|
| 1069 | + | git(&repository, "commit", &["-q", "-m", "base"]); |
|
| 1070 | + | git(&repository, "switch", &["-q", "-c", "feature"]); |
|
| 1071 | + | fs::write(repository_path.join("feature"), "first\nsecond\n").unwrap(); |
|
| 1072 | + | git(&repository, "add", &["feature"]); |
|
| 1073 | + | git(&repository, "commit", &["-q", "-m", "feature"]); |
|
| 1074 | + | git(&repository, "switch", &["-q", "main"]); |
|
| 1075 | + | fs::write(repository_path.join("main"), "main\n").unwrap(); |
|
| 1076 | + | git(&repository, "add", &["main"]); |
|
| 1077 | + | git(&repository, "commit", &["-q", "-m", "main"]); |
|
| 1078 | + | git( |
|
| 1079 | + | &repository, |
|
| 1080 | + | "merge", |
|
| 1081 | + | &["-q", "--no-ff", "feature", "-m", "merge"], |
|
| 1082 | + | ); |
|
| 1083 | + | let merge = repository |
|
| 1084 | + | .commit_id(&Rev::try_from("HEAD").unwrap()) |
|
| 1085 | + | .unwrap(); |
|
| 1086 | + | ||
| 1087 | + | assert_eq!( |
|
| 1088 | + | repository.commit_stats(&[merge]).unwrap(), |
|
| 1089 | + | vec![DiffStat { |
|
| 1090 | + | files_changed: 1, |
|
| 1091 | + | insertions: 2, |
|
| 1092 | + | deletions: 0, |
|
| 1093 | + | }] |
|
| 1094 | + | ); |
|
| 1095 | + | } |
|
| 1096 | + | ||
| 1055 | 1097 | #[test] |
|
| 1056 | 1098 | fn commit_metadata_parses_four_fields() { |
|
| 1057 | 1099 | let record = [ |
|
| 1058 | 1100 | "0123456789abcdef0123456789abcdef01234567", |
|
| 1059 | 1101 | "Alice", |
a\n--- a/a\n+++ b/a\n@@ -1 +1 @@\n-old\n+new\n".to_owned(),
+0 -0
b\n--- a/b\n+++ b/b\n@@ -0,0 +1 @@\n+line\n".to_owned(),
+0 -0