@@ -84,6 +84,21 @@ impl From<Oid> for CommitId {
8484 }
8585}
8686
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+
87102///
88103#[ derive( Debug , Clone ) ]
89104pub struct CommitInfo {
@@ -142,24 +157,35 @@ pub fn get_commit_info(
142157) -> Result < CommitInfo > {
143158 scope_time ! ( "get_commit_info" ) ;
144159
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 ) ;
147169
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+ ) ;
150176
151177 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 ( ) ,
156182 } )
157183}
158184
159185/// if `message_limit` is set the message will be
160186/// limited to the first line and truncated to fit
161187pub fn get_message (
162- c : & Commit ,
188+ c : & git2 :: Commit ,
163189 message_limit : Option < usize > ,
164190) -> String {
165191 let msg = String :: from_utf8_lossy ( c. message_bytes ( ) ) ;
@@ -174,6 +200,24 @@ pub fn get_message(
174200 )
175201}
176202
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+
177221#[ cfg( test) ]
178222mod tests {
179223 use super :: get_commits_info;
0 commit comments