1- /*
1+ /*!
22 * Copyright (c) 2025 Hangzhou Guanwaii Technology Co,.Ltd.
33 *
44 * This source code is licensed under the MIT License,
55 * which is located in the LICENSE file in the source tree's root directory.
66 *
7- * File: git .rs
8- * Author: mingcheng ( [email protected] ) 9- * File Created: 2025-03-01 21:55:54
7+ * File: repository .rs
8+ * Author: mingcheng < [email protected] > 9+ * File Created: 2025-10-16 15:07:05
1010 *
11- * Modified By: mingcheng ( [email protected] ) 12- * Last Modified: 2025-09-26 14:43:17
11+ * Modified By: mingcheng < [email protected] > 12+ * Last Modified: 2025-10-16 15:28:33
1313 */
1414
1515use git2:: { Repository , RepositoryOpenFlags , Signature , StatusOptions } ;
1616use log:: trace;
1717use std:: error:: Error ;
18+ use std:: fmt:: { Display , Formatter } ;
1819use std:: path:: Path ;
1920
21+ use crate :: message:: GitMessage ;
22+
23+ pub struct Author {
24+ pub name : String ,
25+ pub email : String ,
26+ }
2027pub struct Git {
2128 repository : Repository ,
2229}
2330
31+ impl Display for Git {
32+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
33+ write ! ( f, "Git repository at {}" , self . repository. path( ) . display( ) )
34+ }
35+ }
36+
2437impl Git {
2538 pub fn new ( path : & str ) -> Result < Git , Box < dyn Error > > {
2639 trace ! ( "opening repository at {path}" ) ;
@@ -37,10 +50,10 @@ impl Git {
3750 }
3851
3952 /// Commit the changes in the repository
40- pub fn commit ( & self , message : & str , need_signoff : bool ) -> Result < ( ) , Box < dyn Error > > {
53+ pub fn commit ( & self , message : & GitMessage ) -> Result < ( ) , Box < dyn Error > > {
4154 // Get the current index (staged changes)
4255
43- let mut message = message. to_string ( ) ;
56+ let message = message. to_string ( ) ;
4457 let mut index = self . repository . index ( ) ?;
4558
4659 // Write the index to the repository
@@ -51,20 +64,10 @@ impl Git {
5164 let head = self . repository . head ( ) ?. peel_to_commit ( ) ?;
5265
5366 // Create a new commit
54- let author_name = self . get_author_name ( ) ?;
55- let author_email = self . get_author_email ( ) ?;
67+ let author = self . get_author ( ) ?;
5668
5769 // Create a signature for the author and committer
58- let signature = Signature :: now ( & author_name, & author_email) ?;
59-
60- // If the --signoff option is enabled, add signoff to the commit message
61- if need_signoff {
62- trace ! ( "signoff option is enabled, will add signoff to the commit message" ) ;
63-
64- // Add signoff to the commit message
65- let signoff = format ! ( "\n \n Signed-off-by: {author_name} <{author_email}>" ) ;
66- message. push_str ( & signoff) ;
67- }
70+ let signature = Signature :: now ( & author. name , & author. email ) ?;
6871
6972 match self . repository . commit (
7073 Some ( "HEAD" ) ,
@@ -86,40 +89,17 @@ impl Git {
8689 }
8790
8891 /// Get the author email and name from the repository configuration
89- pub fn get_author_email ( & self ) -> Result < String , Box < dyn Error > > {
92+ pub fn get_author ( & self ) -> Result < Author , Box < dyn Error > > {
9093 // Get the configuration of the repository
9194 let config = self . repository . config ( ) ?;
9295
9396 // Get the user email from the configuration
94- match config. get_string ( "user.email" ) {
95- Ok ( email) => {
96- trace ! ( "get author email: {email} from config `user.email`" ) ;
97- Ok ( email)
98- }
99-
100- Err ( e) => {
101- trace ! ( "failed to get author email: {e}" ) ;
102- Err ( Box :: new ( e) )
103- }
104- }
105- }
106-
107- pub fn get_author_name ( & self ) -> Result < String , Box < dyn Error > > {
108- // Get the configuration of the repository
109- let config = self . repository . config ( ) ?;
97+ let email = config. get_string ( "user.email" ) ?;
11098
11199 // Get the user name from the configuration
112- match config. get_string ( "user.name" ) {
113- Ok ( name) => {
114- trace ! ( "get author name: {name} from config `user.name`" ) ;
115- Ok ( name)
116- }
100+ let name = config. get_string ( "user.name" ) ?;
117101
118- Err ( e) => {
119- trace ! ( "failed to get author name: {e}" ) ;
120- Err ( Box :: new ( e) )
121- }
122- }
102+ Ok ( Author { name, email } )
123103 }
124104
125105 /// Get the diff of the current repository
0 commit comments