repo: Diff root commits against empty tree
c26212b940b53e45ce9f80f41433563d84bcd4f3
Resolve the first parent before producing a commit diff. Use Git's empty tree when the commit has no parent so review ranges can include a root commit. Assisted-by: Codex:gpt-5.6-sol
1 parent
8c3092aa
crates/repo/src/git.rs
+38 -3
| 28 | 28 | const HEAD_BYTES: usize = 64; |
|
| 29 | 29 | /// Pathspec separator that ends Git's option list. |
|
| 30 | 30 | const PATHSPEC_SEPARATOR: &str = "--"; |
|
| 31 | 31 | /// Hexadecimal width of a SHA-1 object identifier. |
|
| 32 | 32 | const SHA1_HEX_LEN: usize = 40; |
|
| 33 | + | /// Object identifier of the empty tree in SHA-1 repositories. |
|
| 34 | + | const SHA1_EMPTY_TREE: &str = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; |
|
| 33 | 35 | /// Hexadecimal width of a SHA-256 object identifier. |
|
| 34 | 36 | const SHA256_HEX_LEN: usize = 64; |
|
| 37 | + | /// Object identifier of the empty tree in SHA-256 repositories. |
|
| 38 | + | const SHA256_EMPTY_TREE: &str = "6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321"; |
|
| 35 | 39 | ||
| 36 | 40 | /// Git repository command context. |
|
| 37 | 41 | #[derive(Clone, Debug)] |
|
| 38 | 42 | pub struct Repository { |
|
| 39 | 43 | /// Working directory for worktree-aware invocations. |
| 730 | 734 | commits.push((id, summary.to_owned())); |
|
| 731 | 735 | } |
|
| 732 | 736 | Ok(commits) |
|
| 733 | 737 | } |
|
| 734 | 738 | ||
| 735 | - | /// Returns the unified diff for `commit` against its parent. |
|
| 739 | + | /// Returns the unified diff for `commit` against its first parent. |
|
| 740 | + | /// |
|
| 741 | + | /// Root commits are compared against the empty tree. |
|
| 736 | 742 | fn commit_diff(&self, commit: Oid) -> Result<Vec<u8>, RepoError> { |
|
| 737 | 743 | let commit = commit.to_string(); |
|
| 738 | - | let parent = format!("{commit}^"); |
|
| 744 | + | let parents = self.git("show", &["-s", "--format=%P", &commit])?; |
|
| 745 | + | let parents = utf8(parents)?; |
|
| 746 | + | let empty_tree = match self.object_id_width { |
|
| 747 | + | SHA1_HEX_LEN => SHA1_EMPTY_TREE, |
|
| 748 | + | SHA256_HEX_LEN => SHA256_EMPTY_TREE, |
|
| 749 | + | _ => unreachable!("commit_diff: unsupported object identifier width"), |
|
| 750 | + | }; |
|
| 751 | + | let parent = parents.split_whitespace().next().unwrap_or(empty_tree); |
|
| 752 | + | ||
| 739 | 753 | self.git( |
|
| 740 | 754 | "diff", |
|
| 741 | 755 | &[ |
|
| 742 | 756 | "--no-ext-diff", |
|
| 743 | 757 | "--no-textconv", |
|
| 744 | 758 | "--full-index", |
|
| 745 | - | &parent, |
|
| 759 | + | parent, |
|
| 746 | 760 | &commit, |
|
| 747 | 761 | ], |
|
| 748 | 762 | ) |
|
| 749 | 763 | } |
|
| 750 | 764 | } |
| 1101 | 1115 | deletions: 0, |
|
| 1102 | 1116 | }] |
|
| 1103 | 1117 | ); |
|
| 1104 | 1118 | } |
|
| 1105 | 1119 | ||
| 1120 | + | #[test] |
|
| 1121 | + | fn commit_diff_returns_root_commit_file() { |
|
| 1122 | + | let directory = TempIndex::new().unwrap(); |
|
| 1123 | + | let repository_path = directory.path().parent().unwrap(); |
|
| 1124 | + | let status = Command::new("git") |
|
| 1125 | + | .args(["init", "-q"]) |
|
| 1126 | + | .arg(repository_path) |
|
| 1127 | + | .status() |
|
| 1128 | + | .unwrap(); |
|
| 1129 | + | assert!(status.success()); |
|
| 1130 | + | let repository = Repository::discover(repository_path.to_owned()).unwrap(); |
|
| 1131 | + | fs::write(repository_path.join("root"), "root\n").unwrap(); |
|
| 1132 | + | git(&repository, "add", &["root"]); |
|
| 1133 | + | git(&repository, "commit", &["-q", "-m", "root"]); |
|
| 1134 | + | let root = repository |
|
| 1135 | + | .commit_id(&Rev::try_from("HEAD").unwrap()) |
|
| 1136 | + | .unwrap(); |
|
| 1137 | + | ||
| 1138 | + | let diff = utf8(repository.commit_diff(root).unwrap()).unwrap(); |
|
| 1139 | + | ||
| 1140 | + | assert!(diff.starts_with(" |
root\nnew file mode"));
+0 -0
file\n"));
+0 -0
file\n"),
+0 -0
file\n"),
+0 -0
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