1
+ use std:: cmp:: Ordering ;
2
+
1
3
use super :: { stash:: is_stash_commit, utils:: repo, CommitId } ;
2
4
use crate :: {
3
5
error:: Error , error:: Result , StatusItem , StatusItemType ,
@@ -9,12 +11,17 @@ use scopetime::scope_time;
9
11
pub fn get_commit_files (
10
12
repo_path : & str ,
11
13
id : CommitId ,
14
+ other : Option < CommitId > ,
12
15
) -> Result < Vec < StatusItem > > {
13
16
scope_time ! ( "get_commit_files" ) ;
14
17
15
18
let repo = repo ( repo_path) ?;
16
19
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
+ } ;
18
25
19
26
let mut res = Vec :: new ( ) ;
20
27
@@ -38,6 +45,44 @@ pub fn get_commit_files(
38
45
Ok ( res)
39
46
}
40
47
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
+
41
86
#[ allow( clippy:: redundant_pub_crate) ]
42
87
pub ( crate ) fn get_commit_diff (
43
88
repo : & Repository ,
@@ -48,6 +93,7 @@ pub(crate) fn get_commit_diff(
48
93
49
94
let commit = repo. find_commit ( id. into ( ) ) ?;
50
95
let commit_tree = commit. tree ( ) ?;
96
+
51
97
let parent = if commit. parent_count ( ) > 0 {
52
98
repo. find_commit ( commit. parent_id ( 0 ) ?)
53
99
. ok ( )
@@ -116,7 +162,7 @@ mod tests {
116
162
117
163
let id = commit ( repo_path, "commit msg" ) ?;
118
164
119
- let diff = get_commit_files ( repo_path, id) ?;
165
+ let diff = get_commit_files ( repo_path, id, None ) ?;
120
166
121
167
assert_eq ! ( diff. len( ) , 1 ) ;
122
168
assert_eq ! ( diff[ 0 ] . status, StatusItemType :: New ) ;
@@ -136,7 +182,7 @@ mod tests {
136
182
137
183
let id = stash_save ( repo_path, None , true , false ) ?;
138
184
139
- let diff = get_commit_files ( repo_path, id) ?;
185
+ let diff = get_commit_files ( repo_path, id, None ) ?;
140
186
141
187
assert_eq ! ( diff. len( ) , 1 ) ;
142
188
assert_eq ! ( diff[ 0 ] . status, StatusItemType :: New ) ;
@@ -164,7 +210,7 @@ mod tests {
164
210
165
211
let id = stash_save ( repo_path, None , true , false ) ?;
166
212
167
- let diff = get_commit_files ( repo_path, id) ?;
213
+ let diff = get_commit_files ( repo_path, id, None ) ?;
168
214
169
215
assert_eq ! ( diff. len( ) , 2 ) ;
170
216
assert_eq ! ( diff[ 0 ] . status, StatusItemType :: Modified ) ;
0 commit comments