@@ -84,6 +84,21 @@ impl From<Oid> for CommitId {
84
84
}
85
85
}
86
86
87
+ impl From < gix:: ObjectId > for CommitId {
88
+ fn from ( object_id : gix:: ObjectId ) -> Self {
89
+ #[ allow( clippy:: expect_used) ]
90
+ let oid = Oid :: from_bytes ( object_id. as_bytes ( ) ) . expect ( "`Oid::from_bytes(object_id.as_bytes())` is expected to never fail" ) ;
91
+
92
+ Self :: new ( oid)
93
+ }
94
+ }
95
+
96
+ impl From < CommitId > for gix:: ObjectId {
97
+ fn from ( id : CommitId ) -> Self {
98
+ Self :: from_bytes_or_panic ( id. 0 . as_bytes ( ) )
99
+ }
100
+ }
101
+
87
102
///
88
103
#[ derive( Debug , Clone ) ]
89
104
pub struct CommitInfo {
@@ -142,24 +157,35 @@ pub fn get_commit_info(
142
157
) -> Result < CommitInfo > {
143
158
scope_time ! ( "get_commit_info" ) ;
144
159
145
- let repo = repo ( repo_path) ?;
146
- let mailmap = repo. mailmap ( ) ?;
160
+ let repo: gix:: Repository =
161
+ gix:: ThreadSafeRepository :: discover_with_environment_overrides ( repo_path. gitpath ( ) )
162
+ . map ( Into :: into) ?;
163
+ let mailmap = repo. open_mailmap ( ) ;
164
+
165
+ let commit = repo. find_commit ( * commit_id) ?;
166
+ let commit_ref = commit. decode ( ) ?;
167
+
168
+ let message = gix_get_message ( & commit_ref, None ) ;
147
169
148
- let commit = repo. find_commit ( ( * commit_id) . into ( ) ) ?;
149
- let author = get_author_of_commit ( & commit, & mailmap) ;
170
+ let author = commit_ref. author ( ) ;
171
+
172
+ let author = mailmap. try_resolve ( author) . map_or_else (
173
+ || author. name . into ( ) ,
174
+ |signature| signature. name ,
175
+ ) ;
150
176
151
177
Ok ( CommitInfo {
152
- message : commit . message ( ) . unwrap_or ( "" ) . into ( ) ,
153
- author : author. name ( ) . unwrap_or ( "<unknown>" ) . into ( ) ,
154
- time : commit . time ( ) . seconds ( ) ,
155
- id : CommitId ( commit. id ( ) ) ,
178
+ message,
179
+ author : author. to_string ( ) ,
180
+ time : commit_ref . time ( ) . seconds ,
181
+ id : commit. id ( ) . detach ( ) . into ( ) ,
156
182
} )
157
183
}
158
184
159
185
/// if `message_limit` is set the message will be
160
186
/// limited to the first line and truncated to fit
161
187
pub fn get_message (
162
- c : & Commit ,
188
+ c : & git2 :: Commit ,
163
189
message_limit : Option < usize > ,
164
190
) -> String {
165
191
let msg = String :: from_utf8_lossy ( c. message_bytes ( ) ) ;
@@ -174,6 +200,24 @@ pub fn get_message(
174
200
)
175
201
}
176
202
203
+ /// if `message_limit` is set the message will be
204
+ /// limited to the first line and truncated to fit
205
+ pub fn gix_get_message (
206
+ commit_ref : & gix:: objs:: CommitRef ,
207
+ message_limit : Option < usize > ,
208
+ ) -> String {
209
+ let message = commit_ref. message . to_string ( ) ;
210
+ let message = message. trim ( ) ;
211
+
212
+ message_limit. map_or_else (
213
+ || message. to_string ( ) ,
214
+ |limit| {
215
+ let message = message. lines ( ) . next ( ) . unwrap_or_default ( ) ;
216
+ message. unicode_truncate ( limit) . 0 . to_string ( )
217
+ } ,
218
+ )
219
+ }
220
+
177
221
#[ cfg( test) ]
178
222
mod tests {
179
223
use super :: get_commits_info;
0 commit comments