|
| 1 | +// Copyright 2017 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 | +// Inspired by Clang's clang-format-diff: |
| 12 | +// |
| 13 | +// https://github.com/llvm-mirror/clang/blob/master/tools/clang-format/clang-format-diff.py |
| 14 | + |
| 15 | +#![deny(warnings)] |
| 16 | + |
| 17 | +extern crate env_logger; |
| 18 | +extern crate getopts; |
| 19 | +#[macro_use] |
| 20 | +extern crate log; |
| 21 | +extern crate regex; |
| 22 | +extern crate serde; |
| 23 | +#[macro_use] |
| 24 | +extern crate serde_derive; |
| 25 | +extern crate serde_json as json; |
| 26 | + |
| 27 | +use std::{env, fmt, process}; |
| 28 | +use std::collections::HashSet; |
| 29 | +use std::error::Error; |
| 30 | +use std::io::{self, BufRead}; |
| 31 | + |
| 32 | +use regex::Regex; |
| 33 | + |
| 34 | +/// The default pattern of files to format. |
| 35 | +/// |
| 36 | +/// We only want to format rust files by default. |
| 37 | +const DEFAULT_PATTERN: &'static str = r".*\.rs"; |
| 38 | + |
| 39 | +#[derive(Debug)] |
| 40 | +enum FormatDiffError { |
| 41 | + IncorrectOptions(getopts::Fail), |
| 42 | + IncorrectFilter(regex::Error), |
| 43 | + IoError(io::Error), |
| 44 | +} |
| 45 | + |
| 46 | +impl fmt::Display for FormatDiffError { |
| 47 | + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 48 | + fmt::Display::fmt(self.cause().unwrap(), f) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl Error for FormatDiffError { |
| 53 | + fn description(&self) -> &str { |
| 54 | + self.cause().unwrap().description() |
| 55 | + } |
| 56 | + |
| 57 | + fn cause(&self) -> Option<&Error> { |
| 58 | + Some(match *self { |
| 59 | + FormatDiffError::IoError(ref e) => e, |
| 60 | + FormatDiffError::IncorrectFilter(ref e) => e, |
| 61 | + FormatDiffError::IncorrectOptions(ref e) => e, |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl From<getopts::Fail> for FormatDiffError { |
| 67 | + fn from(fail: getopts::Fail) -> Self { |
| 68 | + FormatDiffError::IncorrectOptions(fail) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl From<regex::Error> for FormatDiffError { |
| 73 | + fn from(err: regex::Error) -> Self { |
| 74 | + FormatDiffError::IncorrectFilter(err) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +impl From<io::Error> for FormatDiffError { |
| 79 | + fn from(fail: io::Error) -> Self { |
| 80 | + FormatDiffError::IoError(fail) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +fn main() { |
| 85 | + let _ = env_logger::init(); |
| 86 | + |
| 87 | + let mut opts = getopts::Options::new(); |
| 88 | + opts.optflag("h", "help", "show this message"); |
| 89 | + opts.optopt( |
| 90 | + "p", |
| 91 | + "skip-prefix", |
| 92 | + "skip the smallest prefix containing NUMBER slashes", |
| 93 | + "NUMBER", |
| 94 | + ); |
| 95 | + opts.optopt( |
| 96 | + "f", |
| 97 | + "filter", |
| 98 | + "custom pattern selecting file paths to reformat", |
| 99 | + "PATTERN", |
| 100 | + ); |
| 101 | + |
| 102 | + if let Err(e) = run(&opts) { |
| 103 | + println!("{}", opts.usage(e.description())); |
| 104 | + process::exit(1); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] |
| 109 | +struct Range { |
| 110 | + file: String, |
| 111 | + range: [u32; 2], |
| 112 | +} |
| 113 | + |
| 114 | +fn run(opts: &getopts::Options) -> Result<(), FormatDiffError> { |
| 115 | + let matches = opts.parse(env::args().skip(1))?; |
| 116 | + |
| 117 | + if matches.opt_present("h") { |
| 118 | + println!("{}", opts.usage("usage: ")); |
| 119 | + return Ok(()); |
| 120 | + } |
| 121 | + |
| 122 | + let filter = matches |
| 123 | + .opt_str("f") |
| 124 | + .unwrap_or_else(|| DEFAULT_PATTERN.into()); |
| 125 | + |
| 126 | + let skip_prefix = matches |
| 127 | + .opt_str("p") |
| 128 | + .and_then(|p| p.parse::<u32>().ok()) |
| 129 | + .unwrap_or(0); |
| 130 | + |
| 131 | + let (files, ranges) = scan_diff(io::stdin(), skip_prefix, &filter)?; |
| 132 | + |
| 133 | + run_rustfmt(&files, &ranges) |
| 134 | +} |
| 135 | + |
| 136 | +fn run_rustfmt(files: &HashSet<String>, ranges: &[Range]) -> Result<(), FormatDiffError> { |
| 137 | + if files.is_empty() || ranges.is_empty() { |
| 138 | + debug!("No files to format found"); |
| 139 | + return Ok(()); |
| 140 | + } |
| 141 | + |
| 142 | + let ranges_as_json = json::to_string(ranges).unwrap(); |
| 143 | + |
| 144 | + debug!("Files: {:?}", files); |
| 145 | + debug!("Ranges: {:?}", ranges); |
| 146 | + |
| 147 | + let exit_status = process::Command::new("rustfmt") |
| 148 | + .args(files) |
| 149 | + .arg("--file-lines") |
| 150 | + .arg(ranges_as_json) |
| 151 | + .status()?; |
| 152 | + |
| 153 | + if !exit_status.success() { |
| 154 | + return Err(FormatDiffError::IoError(io::Error::new( |
| 155 | + io::ErrorKind::Other, |
| 156 | + format!("rustfmt failed with {}", exit_status), |
| 157 | + ))); |
| 158 | + } |
| 159 | + Ok(()) |
| 160 | +} |
| 161 | + |
| 162 | +/// Scans a diff from `from`, and returns the set of files found, and the ranges |
| 163 | +/// in those files. |
| 164 | +fn scan_diff<R>( |
| 165 | + from: R, |
| 166 | + skip_prefix: u32, |
| 167 | + file_filter: &str, |
| 168 | +) -> Result<(HashSet<String>, Vec<Range>), FormatDiffError> |
| 169 | +where |
| 170 | + R: io::Read, |
| 171 | +{ |
| 172 | + let diff_pattern = format!(r"^\+\+\+\s(?:.*?/){{{}}}(\S*)", skip_prefix); |
| 173 | + let diff_pattern = Regex::new(&diff_pattern).unwrap(); |
| 174 | + |
| 175 | + let lines_pattern = Regex::new(r"^@@.*\+(\d+)(,(\d+))?").unwrap(); |
| 176 | + |
| 177 | + let file_filter = Regex::new(&format!("^{}$", file_filter))?; |
| 178 | + |
| 179 | + let mut current_file = None; |
| 180 | + |
| 181 | + let mut files = HashSet::new(); |
| 182 | + let mut ranges = vec![]; |
| 183 | + for line in io::BufReader::new(from).lines() { |
| 184 | + let line = line.unwrap(); |
| 185 | + |
| 186 | + if let Some(captures) = diff_pattern.captures(&line) { |
| 187 | + current_file = Some(captures.get(1).unwrap().as_str().to_owned()); |
| 188 | + } |
| 189 | + |
| 190 | + let file = match current_file { |
| 191 | + Some(ref f) => &**f, |
| 192 | + None => continue, |
| 193 | + }; |
| 194 | + |
| 195 | + // TODO(emilio): We could avoid this most of the time if needed, but |
| 196 | + // it's not clear it's worth it. |
| 197 | + if !file_filter.is_match(file) { |
| 198 | + continue; |
| 199 | + } |
| 200 | + |
| 201 | + let lines_captures = match lines_pattern.captures(&line) { |
| 202 | + Some(captures) => captures, |
| 203 | + None => continue, |
| 204 | + }; |
| 205 | + |
| 206 | + let start_line = lines_captures |
| 207 | + .get(1) |
| 208 | + .unwrap() |
| 209 | + .as_str() |
| 210 | + .parse::<u32>() |
| 211 | + .unwrap(); |
| 212 | + let line_count = match lines_captures.get(3) { |
| 213 | + Some(line_count) => line_count.as_str().parse::<u32>().unwrap(), |
| 214 | + None => 1, |
| 215 | + }; |
| 216 | + |
| 217 | + if line_count == 0 { |
| 218 | + continue; |
| 219 | + } |
| 220 | + |
| 221 | + let end_line = start_line + line_count - 1; |
| 222 | + files.insert(file.to_owned()); |
| 223 | + ranges.push(Range { |
| 224 | + file: file.to_owned(), |
| 225 | + range: [start_line, end_line], |
| 226 | + }); |
| 227 | + } |
| 228 | + |
| 229 | + Ok((files, ranges)) |
| 230 | +} |
| 231 | + |
| 232 | +#[test] |
| 233 | +fn scan_simple_git_diff() { |
| 234 | + const DIFF: &'static str = include_str!("test/bindgen.diff"); |
| 235 | + let (files, ranges) = scan_diff(DIFF.as_bytes(), 1, r".*\.rs").expect("scan_diff failed?"); |
| 236 | + |
| 237 | + assert!( |
| 238 | + files.contains("src/ir/traversal.rs"), |
| 239 | + "Should've matched the filter" |
| 240 | + ); |
| 241 | + |
| 242 | + assert!( |
| 243 | + !files.contains("tests/headers/anon_enum.hpp"), |
| 244 | + "Shouldn't have matched the filter" |
| 245 | + ); |
| 246 | + |
| 247 | + assert_eq!( |
| 248 | + &ranges, |
| 249 | + &[ |
| 250 | + Range { |
| 251 | + file: "src/ir/item.rs".into(), |
| 252 | + range: [148, 158], |
| 253 | + }, |
| 254 | + Range { |
| 255 | + file: "src/ir/item.rs".into(), |
| 256 | + range: [160, 170], |
| 257 | + }, |
| 258 | + Range { |
| 259 | + file: "src/ir/traversal.rs".into(), |
| 260 | + range: [9, 16], |
| 261 | + }, |
| 262 | + Range { |
| 263 | + file: "src/ir/traversal.rs".into(), |
| 264 | + range: [35, 43], |
| 265 | + } |
| 266 | + ] |
| 267 | + ); |
| 268 | +} |
0 commit comments