]> git.lizzy.rs Git - rust.git/blob - src/git-rustfmt/main.rs
Merge pull request #2838 from nrc/chains
[rust.git] / src / git-rustfmt / main.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 extern crate env_logger;
12 extern crate getopts;
13 #[macro_use]
14 extern crate log;
15 extern crate rustfmt_nightly as rustfmt;
16
17 use std::env;
18 use std::io::stdout;
19 use std::path::{Path, PathBuf};
20 use std::process::Command;
21 use std::str::FromStr;
22
23 use getopts::{Matches, Options};
24
25 use rustfmt::{load_config, CliOptions, Input, Session};
26
27 fn prune_files(files: Vec<&str>) -> Vec<&str> {
28     let prefixes: Vec<_> = files
29         .iter()
30         .filter(|f| f.ends_with("mod.rs") || f.ends_with("lib.rs"))
31         .map(|f| &f[..f.len() - 6])
32         .collect();
33
34     let mut pruned_prefixes = vec![];
35     for p1 in prefixes {
36         if p1.starts_with("src/bin/") || pruned_prefixes.iter().all(|p2| !p1.starts_with(p2)) {
37             pruned_prefixes.push(p1);
38         }
39     }
40     debug!("prefixes: {:?}", pruned_prefixes);
41
42     files
43         .into_iter()
44         .filter(|f| {
45             if f.ends_with("mod.rs") || f.ends_with("lib.rs") || f.starts_with("src/bin/") {
46                 return true;
47             }
48             pruned_prefixes.iter().all(|pp| !f.starts_with(pp))
49         }).collect()
50 }
51
52 fn git_diff(commits: &str) -> String {
53     let mut cmd = Command::new("git");
54     cmd.arg("diff");
55     if commits != "0" {
56         cmd.arg(format!("HEAD~{}", commits));
57     }
58     let output = cmd.output().expect("Couldn't execute `git diff`");
59     String::from_utf8_lossy(&output.stdout).into_owned()
60 }
61
62 fn get_files(input: &str) -> Vec<&str> {
63     input
64         .lines()
65         .filter(|line| line.starts_with("+++ b/") && line.ends_with(".rs"))
66         .map(|line| &line[6..])
67         .collect()
68 }
69
70 fn fmt_files(files: &[&str]) -> i32 {
71     let (config, _) =
72         load_config::<NullOptions>(Some(Path::new(".")), None).expect("couldn't load config");
73
74     let mut exit_code = 0;
75     let mut out = stdout();
76     let mut session = Session::new(config, Some(&mut out));
77     for file in files {
78         let report = session.format(Input::File(PathBuf::from(file))).unwrap();
79         if report.has_warnings() {
80             eprintln!("{}", report);
81         }
82         if !session.summary.has_no_errors() {
83             exit_code = 1;
84         }
85     }
86     exit_code
87 }
88
89 struct NullOptions;
90
91 impl CliOptions for NullOptions {
92     fn apply_to(self, _: &mut rustfmt::Config) {
93         unreachable!();
94     }
95     fn config_path(&self) -> Option<&Path> {
96         unreachable!();
97     }
98 }
99
100 fn uncommitted_files() -> Vec<String> {
101     let mut cmd = Command::new("git");
102     cmd.arg("ls-files");
103     cmd.arg("--others");
104     cmd.arg("--modified");
105     cmd.arg("--exclude-standard");
106     let output = cmd.output().expect("Couldn't execute Git");
107     let stdout = String::from_utf8_lossy(&output.stdout);
108     stdout
109         .lines()
110         .filter(|s| s.ends_with(".rs"))
111         .map(|s| s.to_owned())
112         .collect()
113 }
114
115 fn check_uncommitted() {
116     let uncommitted = uncommitted_files();
117     debug!("uncommitted files: {:?}", uncommitted);
118     if !uncommitted.is_empty() {
119         println!("Found untracked changes:");
120         for f in &uncommitted {
121             println!("  {}", f);
122         }
123         println!("Commit your work, or run with `-u`.");
124         println!("Exiting.");
125         std::process::exit(1);
126     }
127 }
128
129 fn make_opts() -> Options {
130     let mut opts = Options::new();
131     opts.optflag("h", "help", "show this message");
132     opts.optflag("c", "check", "check only, don't format (unimplemented)");
133     opts.optflag("u", "uncommitted", "format uncommitted files");
134     opts
135 }
136
137 struct Config {
138     commits: String,
139     uncommitted: bool,
140     check: bool,
141 }
142
143 impl Config {
144     fn from_args(matches: &Matches, opts: &Options) -> Config {
145         // `--help` display help message and quit
146         if matches.opt_present("h") {
147             let message = format!(
148                 "\nusage: {} <commits> [options]\n\n\
149                  commits: number of commits to format, default: 1",
150                 env::args_os().next().unwrap().to_string_lossy()
151             );
152             println!("{}", opts.usage(&message));
153             std::process::exit(0);
154         }
155
156         let mut config = Config {
157             commits: "1".to_owned(),
158             uncommitted: false,
159             check: false,
160         };
161
162         if matches.opt_present("c") {
163             config.check = true;
164             unimplemented!();
165         }
166
167         if matches.opt_present("u") {
168             config.uncommitted = true;
169         }
170
171         if matches.free.len() > 1 {
172             panic!("unknown arguments, use `-h` for usage");
173         }
174         if matches.free.len() == 1 {
175             let commits = matches.free[0].trim();
176             if u32::from_str(commits).is_err() {
177                 panic!("Couldn't parse number of commits");
178             }
179             config.commits = commits.to_owned();
180         }
181
182         config
183     }
184 }
185
186 fn main() {
187     env_logger::init();
188
189     let opts = make_opts();
190     let matches = opts
191         .parse(env::args().skip(1))
192         .expect("Couldn't parse command line");
193     let config = Config::from_args(&matches, &opts);
194
195     if !config.uncommitted {
196         check_uncommitted();
197     }
198
199     let stdout = git_diff(&config.commits);
200     let files = get_files(&stdout);
201     debug!("files: {:?}", files);
202     let files = prune_files(files);
203     debug!("pruned files: {:?}", files);
204     let exit_code = fmt_files(&files);
205     std::process::exit(exit_code);
206 }