]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/cargo_lock.rs
165dd52758ecc525b21ad7f66b982da99a37ae49
[rust.git] / src / tools / tidy / src / cargo_lock.rs
1 // Copyright 2016 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 use std::path::Path;
12 use std::ffi::OsStr;
13
14 const CARGO_LOCK: &'static str = "Cargo.lock";
15
16 pub fn check(path: &Path, bad: &mut bool) {
17     use std::process::Command;
18
19     super::walk(path,
20                 &mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
21                 &mut |file| {
22         if let Some(CARGO_LOCK) = file.file_name().and_then(OsStr::to_str) {
23             let rel_path = file.strip_prefix(path).unwrap();
24             let git_friendly_path = rel_path.to_str().unwrap().replace("\\", "/");
25             let ret_code = Command::new("git")
26                                         .arg("diff")
27                                         .arg("--exit-code")
28                                         .arg("--patch")
29                                         .arg("HEAD")
30                                         .arg(&git_friendly_path)
31                                         .current_dir(path)
32                                         .status()
33                                         .unwrap_or_else(|e| {
34                                             panic!("could not run git diff-index: {}", e);
35                                         });
36             if !ret_code.success() {
37                 let parent_path = file.parent().unwrap().join("Cargo.toml");
38                 print!("dirty lock file found at {} ", rel_path.display());
39                 println!("please commit your changes or update the lock file by running:");
40                 println!("\n\tcargo update --manifest-path {}", parent_path.display());
41                 *bad = true;
42             }
43         }
44     });
45 }