Skip to content

Commit 9767805

Browse files
committed
Add --version switch
Add --version switch, which prints short hash of the current commit. See rust-lang#612
1 parent bc0fc3a commit 9767805

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/bin/rustfmt.rs

+24
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustfmt::config::Config;
2323
use std::env;
2424
use std::fs::{self, File};
2525
use std::io::{self, Read, Write};
26+
use std::process::Command;
2627
use std::path::{Path, PathBuf};
2728

2829
use getopts::{Matches, Options};
@@ -33,6 +34,8 @@ enum Operation {
3334
Format(Vec<PathBuf>, WriteMode),
3435
/// Print the help message.
3536
Help,
37+
// Print version information
38+
Version,
3639
/// Print detailed configuration help.
3740
ConfigHelp,
3841
/// Invalid program input, including reason.
@@ -82,6 +85,7 @@ fn update_config(config: &mut Config, matches: &Matches) {
8285
fn execute() -> i32 {
8386
let mut opts = Options::new();
8487
opts.optflag("h", "help", "show this message");
88+
opts.optflag("", "version", "show version information");
8589
opts.optflag("v", "verbose", "show progress");
8690
opts.optopt("",
8791
"write-mode",
@@ -111,6 +115,10 @@ fn execute() -> i32 {
111115
print_usage(&opts, "");
112116
0
113117
}
118+
Operation::Version => {
119+
print_version();
120+
0
121+
}
114122
Operation::ConfigHelp => {
115123
Config::print_docs();
116124
0
@@ -168,6 +176,18 @@ fn print_usage(opts: &Options, reason: &str) {
168176
println!("{}", opts.usage(&reason));
169177
}
170178

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+
171191
fn determine_operation(matches: &Matches) -> Operation {
172192
if matches.opt_present("h") {
173193
return Operation::Help;
@@ -177,6 +197,10 @@ fn determine_operation(matches: &Matches) -> Operation {
177197
return Operation::ConfigHelp;
178198
}
179199

200+
if matches.opt_present("version") {
201+
return Operation::Version;
202+
}
203+
180204
// if no file argument is supplied, read from stdin
181205
if matches.free.len() == 0 {
182206

0 commit comments

Comments
 (0)