1 extern crate env_logger;
5 extern crate rustfmt_nightly as rustfmt;
8 use std::path::{Path, PathBuf};
9 use std::process::Command;
10 use std::str::FromStr;
12 use getopts::{Matches, Options};
14 use rustfmt::{run, Input};
18 fn prune_files(files: Vec<&str>) -> Vec<&str> {
19 let prefixes: Vec<_> = files
21 .filter(|f| f.ends_with("mod.rs") || f.ends_with("lib.rs"))
22 .map(|f| &f[..f.len() - 6])
25 let mut pruned_prefixes = vec![];
27 let mut include = true;
28 if !p1.starts_with("src/bin/") {
29 for p2 in &pruned_prefixes {
30 if p1.starts_with(p2) {
37 pruned_prefixes.push(p1);
40 debug!("prefixes: {:?}", pruned_prefixes);
45 let mut include = true;
46 if f.ends_with("mod.rs") || f.ends_with("lib.rs") || f.starts_with("src/bin/") {
49 for pp in &pruned_prefixes {
50 if f.starts_with(pp) {
60 fn git_diff(commits: &str) -> String {
61 let mut cmd = Command::new("git");
64 cmd.arg(format!("HEAD~{}", commits));
66 let output = cmd.output().expect("Couldn't execute `git diff`");
67 String::from_utf8_lossy(&output.stdout).into_owned()
70 fn get_files(input: &str) -> Vec<&str> {
73 .filter(|line| line.starts_with("+++ b/") && line.ends_with(".rs"))
74 .map(|line| &line[6..])
78 fn fmt_files(files: &[&str]) -> i32 {
79 let (config, _) = config::Config::from_resolved_toml_path(Path::new("."))
80 .unwrap_or_else(|_| (config::Config::default(), None));
82 let mut exit_code = 0;
84 let summary = run(Input::File(PathBuf::from(file)), &config);
85 if !summary.has_no_errors() {
92 fn uncommitted_files() -> Vec<String> {
93 let mut cmd = Command::new("git");
96 cmd.arg("--modified");
97 cmd.arg("--exclude-standard");
98 let output = cmd.output().expect("Couldn't execute Git");
99 let stdout = String::from_utf8_lossy(&output.stdout);
102 .filter(|s| s.ends_with(".rs"))
103 .map(|s| s.to_owned())
107 fn check_uncommitted() {
108 let uncommitted = uncommitted_files();
109 debug!("uncommitted files: {:?}", uncommitted);
110 if !uncommitted.is_empty() {
111 println!("Found untracked changes:");
112 for f in &uncommitted {
115 println!("Commit your work, or run with `-u`.");
116 println!("Exiting.");
117 std::process::exit(1);
121 fn make_opts() -> Options {
122 let mut opts = Options::new();
123 opts.optflag("h", "help", "show this message");
124 opts.optflag("c", "check", "check only, don't format (unimplemented)");
125 opts.optflag("u", "uncommitted", "format uncommitted files");
136 fn from_args(matches: &Matches, opts: &Options) -> Config {
137 // `--help` display help message and quit
138 if matches.opt_present("h") {
139 let message = format!(
140 "\nusage: {} <commits> [options]\n\n\
141 commits: number of commits to format, default: 1",
142 env::args_os().next().unwrap().to_string_lossy()
144 println!("{}", opts.usage(&message));
145 std::process::exit(0);
148 let mut config = Config {
149 commits: "1".to_owned(),
154 if matches.opt_present("c") {
159 if matches.opt_present("u") {
160 config.uncommitted = true;
163 if matches.free.len() > 1 {
164 panic!("unknown arguments, use `-h` for usage");
166 if matches.free.len() == 1 {
167 let commits = matches.free[0].trim();
168 if u32::from_str(commits).is_err() {
169 panic!("Couldn't parse number of commits");
171 config.commits = commits.to_owned();
179 let _ = env_logger::init();
181 let opts = make_opts();
182 let matches = opts.parse(env::args().skip(1))
183 .expect("Couldn't parse command line");
184 let config = Config::from_args(&matches, &opts);
186 if !config.uncommitted {
190 let stdout = git_diff(&config.commits);
191 let files = get_files(&stdout);
192 debug!("files: {:?}", files);
193 let files = prune_files(files);
194 debug!("pruned files: {:?}", files);
195 let exit_code = fmt_files(&files);
196 std::process::exit(exit_code);