1+ use std:: cmp:: Ordering ;
2+
13use super :: { stash:: is_stash_commit, utils:: repo, CommitId } ;
24use crate :: {
35 error:: Error , error:: Result , StatusItem , StatusItemType ,
@@ -9,12 +11,17 @@ use scopetime::scope_time;
911pub fn get_commit_files (
1012 repo_path : & str ,
1113 id : CommitId ,
14+ other : Option < CommitId > ,
1215) -> Result < Vec < StatusItem > > {
1316 scope_time ! ( "get_commit_files" ) ;
1417
1518 let repo = repo ( repo_path) ?;
1619
17- let diff = get_commit_diff ( & repo, id, None ) ?;
20+ let diff = if let Some ( other) = other {
21+ get_compare_commits_diff ( & repo, ( id, other) , None ) ?
22+ } else {
23+ get_commit_diff ( & repo, id, None ) ?
24+ } ;
1825
1926 let mut res = Vec :: new ( ) ;
2027
@@ -38,6 +45,44 @@ pub fn get_commit_files(
3845 Ok ( res)
3946}
4047
48+ #[ allow( clippy:: needless_pass_by_value) ]
49+ pub fn get_compare_commits_diff (
50+ repo : & Repository ,
51+ ids : ( CommitId , CommitId ) ,
52+ pathspec : Option < String > ,
53+ ) -> Result < Diff < ' _ > > {
54+ // scope_time!("get_compare_commits_diff");
55+
56+ let commits = (
57+ repo. find_commit ( ids. 0 . into ( ) ) ?,
58+ repo. find_commit ( ids. 1 . into ( ) ) ?,
59+ ) ;
60+
61+ let commits = if commits. 0 . time ( ) . cmp ( & commits. 1 . time ( ) )
62+ == Ordering :: Greater
63+ {
64+ ( commits. 1 , commits. 0 )
65+ } else {
66+ commits
67+ } ;
68+
69+ let trees = ( commits. 0 . tree ( ) ?, commits. 1 . tree ( ) ?) ;
70+
71+ let mut opts = DiffOptions :: new ( ) ;
72+ if let Some ( p) = & pathspec {
73+ opts. pathspec ( p. clone ( ) ) ;
74+ }
75+ opts. show_binary ( true ) ;
76+
77+ let diff = repo. diff_tree_to_tree (
78+ Some ( & trees. 0 ) ,
79+ Some ( & trees. 1 ) ,
80+ Some ( & mut opts) ,
81+ ) ?;
82+
83+ Ok ( diff)
84+ }
85+
4186#[ allow( clippy:: redundant_pub_crate) ]
4287pub ( crate ) fn get_commit_diff (
4388 repo : & Repository ,
@@ -48,6 +93,7 @@ pub(crate) fn get_commit_diff(
4893
4994 let commit = repo. find_commit ( id. into ( ) ) ?;
5095 let commit_tree = commit. tree ( ) ?;
96+
5197 let parent = if commit. parent_count ( ) > 0 {
5298 repo. find_commit ( commit. parent_id ( 0 ) ?)
5399 . ok ( )
@@ -116,7 +162,7 @@ mod tests {
116162
117163 let id = commit ( repo_path, "commit msg" ) ?;
118164
119- let diff = get_commit_files ( repo_path, id) ?;
165+ let diff = get_commit_files ( repo_path, id, None ) ?;
120166
121167 assert_eq ! ( diff. len( ) , 1 ) ;
122168 assert_eq ! ( diff[ 0 ] . status, StatusItemType :: New ) ;
@@ -136,7 +182,7 @@ mod tests {
136182
137183 let id = stash_save ( repo_path, None , true , false ) ?;
138184
139- let diff = get_commit_files ( repo_path, id) ?;
185+ let diff = get_commit_files ( repo_path, id, None ) ?;
140186
141187 assert_eq ! ( diff. len( ) , 1 ) ;
142188 assert_eq ! ( diff[ 0 ] . status, StatusItemType :: New ) ;
@@ -164,7 +210,7 @@ mod tests {
164210
165211 let id = stash_save ( repo_path, None , true , false ) ?;
166212
167- let diff = get_commit_files ( repo_path, id) ?;
213+ let diff = get_commit_files ( repo_path, id, None ) ?;
168214
169215 assert_eq ! ( diff. len( ) , 2 ) ;
170216 assert_eq ! ( diff[ 0 ] . status, StatusItemType :: Modified ) ;
0 commit comments