From 6e063a14307a8bbcaa902b1ee91debeaa4c18b53 Mon Sep 17 00:00:00 2001 From: Jan Likar Date: Mon, 16 Nov 2015 04:37:08 +0100 Subject: [PATCH] Add --version switch Add --version switch, which prints a short hash of the current commit. Fix #612 --- src/bin/rustfmt.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/bin/rustfmt.rs b/src/bin/rustfmt.rs index 789f8773345f..bc9c3445aadf 100644 --- a/src/bin/rustfmt.rs +++ b/src/bin/rustfmt.rs @@ -23,6 +23,7 @@ use rustfmt::config::Config; use std::env; use std::fs::{self, File}; use std::io::{self, Read, Write}; +use std::process::Command; use std::path::{Path, PathBuf}; use getopts::{Matches, Options}; @@ -33,6 +34,8 @@ enum Operation { Format(Vec, WriteMode), /// Print the help message. Help, + // Print version information + Version, /// Print detailed configuration help. ConfigHelp, /// Invalid program input, including reason. @@ -82,6 +85,7 @@ fn update_config(config: &mut Config, matches: &Matches) { fn execute() -> i32 { let mut opts = Options::new(); opts.optflag("h", "help", "show this message"); + opts.optflag("", "version", "show version information"); opts.optflag("v", "verbose", "show progress"); opts.optopt("", "write-mode", @@ -111,6 +115,10 @@ fn execute() -> i32 { print_usage(&opts, ""); 0 } + Operation::Version => { + print_version(); + 0 + } Operation::ConfigHelp => { Config::print_docs(); 0 @@ -168,6 +176,18 @@ fn print_usage(opts: &Options, reason: &str) { println!("{}", opts.usage(&reason)); } +fn print_version() { + let cmd = Command::new("git") + .arg("rev-parse") + .arg("--short") + .arg("HEAD") + .output(); + match cmd { + Ok(output) => print!("{}", String::from_utf8(output.stdout).unwrap()), + Err(e) => panic!("Unable te get version: {}", e), + } +} + fn determine_operation(matches: &Matches) -> Operation { if matches.opt_present("h") { return Operation::Help; @@ -177,6 +197,10 @@ fn determine_operation(matches: &Matches) -> Operation { return Operation::ConfigHelp; } + if matches.opt_present("version") { + return Operation::Version; + } + // if no file argument is supplied, read from stdin if matches.free.len() == 0 {