@@ -23,6 +23,7 @@ use rustfmt::config::Config;
23
23
use std:: env;
24
24
use std:: fs:: { self , File } ;
25
25
use std:: io:: { self , Read , Write } ;
26
+ use std:: process:: Command ;
26
27
use std:: path:: { Path , PathBuf } ;
27
28
28
29
use getopts:: { Matches , Options } ;
@@ -33,6 +34,8 @@ enum Operation {
33
34
Format ( Vec < PathBuf > , WriteMode ) ,
34
35
/// Print the help message.
35
36
Help ,
37
+ // Print version information
38
+ Version ,
36
39
/// Print detailed configuration help.
37
40
ConfigHelp ,
38
41
/// Invalid program input, including reason.
@@ -82,6 +85,7 @@ fn update_config(config: &mut Config, matches: &Matches) {
82
85
fn execute ( ) -> i32 {
83
86
let mut opts = Options :: new ( ) ;
84
87
opts. optflag ( "h" , "help" , "show this message" ) ;
88
+ opts. optflag ( "" , "version" , "show version information" ) ;
85
89
opts. optflag ( "v" , "verbose" , "show progress" ) ;
86
90
opts. optopt ( "" ,
87
91
"write-mode" ,
@@ -111,6 +115,10 @@ fn execute() -> i32 {
111
115
print_usage ( & opts, "" ) ;
112
116
0
113
117
}
118
+ Operation :: Version => {
119
+ print_version ( ) ;
120
+ 0
121
+ }
114
122
Operation :: ConfigHelp => {
115
123
Config :: print_docs ( ) ;
116
124
0
@@ -168,6 +176,18 @@ fn print_usage(opts: &Options, reason: &str) {
168
176
println ! ( "{}" , opts. usage( & reason) ) ;
169
177
}
170
178
179
+ fn print_version ( ) {
180
+ let cmd = Command :: new ( "git" )
181
+ . arg ( "rev-parse" )
182
+ . arg ( "--short" )
183
+ . arg ( "HEAD" )
184
+ . output ( ) ;
185
+ match cmd {
186
+ Ok ( output) => print ! ( "{}" , String :: from_utf8( output. stdout) . unwrap( ) ) ,
187
+ Err ( e) => panic ! ( "Unable te get version: {}" , e) ,
188
+ }
189
+ }
190
+
171
191
fn determine_operation ( matches : & Matches ) -> Operation {
172
192
if matches. opt_present ( "h" ) {
173
193
return Operation :: Help ;
@@ -177,6 +197,10 @@ fn determine_operation(matches: &Matches) -> Operation {
177
197
return Operation :: ConfigHelp ;
178
198
}
179
199
200
+ if matches. opt_present ( "version" ) {
201
+ return Operation :: Version ;
202
+ }
203
+
180
204
// if no file argument is supplied, read from stdin
181
205
if matches. free . len ( ) == 0 {
182
206
0 commit comments