@@ -14,6 +14,8 @@ use crate::{AsciiArt, CommitInfo, Configuration, Error, InfoFieldOn};
14
14
type Result < T > = std:: result:: Result < T , crate :: Error > ;
15
15
16
16
pub struct Info {
17
+ git_version : String ,
18
+ git_username : String ,
17
19
project_name : String ,
18
20
current_commit : CommitInfo ,
19
21
version : String ,
@@ -40,7 +42,18 @@ impl std::fmt::Display for Info {
40
42
Some ( & c) => c,
41
43
None => Color :: White ,
42
44
} ;
43
-
45
+ if !self . disable_fields . git_info {
46
+ let git_info;
47
+ if self . git_username != "" {
48
+ git_info = format ! ( "{} : {}" , self . git_username, self . git_version) ;
49
+ write ! ( & mut buf, "{}{}" , & self . get_formatted_info_label( & self . git_username, color) , " : " ) ?;
50
+ } else {
51
+ git_info = self . git_version . clone ( ) ;
52
+ }
53
+ write_buf ( & mut buf, & self . get_formatted_info_label ( & self . git_version , color) , "" ) ?;
54
+ let separator = "-" . repeat ( git_info. len ( ) ) ;
55
+ write_buf ( & mut buf, & self . get_formatted_info_label ( "" , color) , & separator) ?;
56
+ }
44
57
if !self . disable_fields . project {
45
58
write_buf ( & mut buf, & self . get_formatted_info_label ( "Project: " , color) , & self . project_name ) ?;
46
59
}
@@ -190,6 +203,7 @@ impl Info {
190
203
bold_flag : bool ,
191
204
) -> Result < Info > {
192
205
let authors = Info :: get_authors ( & dir, 3 ) ;
206
+ let ( git_v, git_user) = Info :: get_git_info ( & dir) ;
193
207
let current_commit_info = Info :: get_current_commit_info ( & dir) ?;
194
208
let config = Info :: get_configuration ( & dir) ?;
195
209
let version = Info :: get_version ( & dir) ?;
@@ -202,6 +216,8 @@ impl Info {
202
216
let dominant_language = Language :: get_dominant_language ( languages_stats. clone ( ) ) ;
203
217
204
218
Ok ( Info {
219
+ git_version : git_v,
220
+ git_username : git_user,
205
221
project_name : config. repository_name ,
206
222
current_commit : current_commit_info,
207
223
version,
@@ -266,6 +282,25 @@ impl Info {
266
282
authors
267
283
}
268
284
285
+ fn get_git_info ( dir : & str ) -> ( String , String ) {
286
+ let version = Command :: new ( "git" )
287
+ . arg ( "--version" )
288
+ . output ( )
289
+ . expect ( "Failed to execute git." ) ;
290
+ let version = String :: from_utf8_lossy ( & version. stdout ) . replace ( '\n' , "" ) ;
291
+
292
+ let username = Command :: new ( "git" )
293
+ . arg ( "-C" )
294
+ . arg ( dir)
295
+ . arg ( "config" )
296
+ . arg ( "--get" )
297
+ . arg ( "user.name" )
298
+ . output ( )
299
+ . expect ( "Failed to execute git." ) ;
300
+ let username = String :: from_utf8_lossy ( & username. stdout ) . replace ( '\n' , "" ) ;
301
+ ( version, username)
302
+ }
303
+
269
304
fn get_current_commit_info ( dir : & str ) -> Result < CommitInfo > {
270
305
let repo = Repository :: open ( dir) . map_err ( |_| Error :: NotGitRepo ) ?;
271
306
let head = repo. head ( ) . map_err ( |_| Error :: ReferenceInfoError ) ?;
0 commit comments