diff --git a/.gitignore b/.gitignore index 5d2110f..b6f6506 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ ci/Cargo.lock cli/Cargo.lock git/Cargo.lock +logger/Cargo.lock utils/Cargo.lock target/ diff --git a/Cargo.lock b/Cargo.lock index ec087ff..f162d1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "ci" version = "0.1.0" +dependencies = [ + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "clap" @@ -74,6 +77,7 @@ dependencies = [ name = "git" version = "0.1.0" dependencies = [ + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "utils 0.1.0", ] @@ -90,6 +94,22 @@ name = "libc" version = "0.2.65" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "log" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "logger" +version = "0.1.0" +dependencies = [ + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "simple_logger 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ppv-lite86" version = "0.2.6" @@ -158,6 +178,16 @@ dependencies = [ "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "simple_logger" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ssc" version = "0.1.0" @@ -165,6 +195,7 @@ dependencies = [ "ci 0.1.0", "cli 0.1.0", "git 0.1.0", + "logger 0.1.0", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -270,6 +301,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum getrandom 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e7db7ca94ed4cd01190ceee0d8a8052f08a247aa1b469a7f68c6a3b71afcf407" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" +"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" "checksum proc-macro-error 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "aeccfe4d5d8ea175d5f0e4a2ad0637e0f4121d63bd99d356fb1f39ab2e7c6097" "checksum proc-macro2 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" @@ -278,6 +310,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rand_chacha 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03a2a90da8c7523f554344f921aa97283eadf6ac484a6d2a7d0212fa7f8d6853" "checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" "checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +"checksum simple_logger 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fea0c4611f32f4c2bac73754f22dca1f57e6c1945e0590dae4e5f2a077b92367" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum structopt 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c167b61c7d4c126927f5346a4327ce20abf8a186b8041bbeb1ce49e5db49587b" "checksum structopt-derive 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "519621841414165d2ad0d4c92be8f41844203f2b67e245f9345a5a12d40c69d7" diff --git a/Cargo.toml b/Cargo.toml index de365e8..1d67ad7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ edition = "2018" ci = { path = "ci" } cli = { path = "cli" } git = { path = "git" } +logger = { path= "logger" } [dev-dependencies] rand = "0.7" diff --git a/ci/Cargo.toml b/ci/Cargo.toml index cc67104..2f9f2aa 100644 --- a/ci/Cargo.toml +++ b/ci/Cargo.toml @@ -3,3 +3,6 @@ name = "ci" version = "0.1.0" authors = ["KNP Labs"] edition = "2018" + +[dependencies] +log = "0.4.8" diff --git a/ci/src/lib.rs b/ci/src/lib.rs index a26c422..43fbf63 100644 --- a/ci/src/lib.rs +++ b/ci/src/lib.rs @@ -1,16 +1,26 @@ +extern crate log; + use std::process::Command; +use log::debug; pub fn run_stop_cmd(cmd: &String) -> i32 { // As the cmd to run is a single string, we run it though a shell // otherwise rust will look for an executable which *is* the whole // `cmd` string. - let result = Command::new("/bin/sh") + let mut command = Command::new("/bin/sh"); + command .arg("-c") .arg(cmd) + ; + + debug!("Running `{:?}`", command); + + let result = command .output() .expect("Unable to run the stop command.") ; + return result.status.code().unwrap(); } diff --git a/cli/src/lib.rs b/cli/src/lib.rs index db498fe..3311185 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -17,6 +17,11 @@ struct RawCli { #[structopt(long = "cmd", help = "The command to use to skip the build.")] cmd: String, + + // The number of occurrences of the `v/verbose` flag + /// Verbose mode (-v, -vv, -vvv, etc.) + #[structopt(short, long, parse(from_occurrences), help = "Verbosity mode : -v, -vv")] + verbosity: u8, } // The Cli struct represents the resolved CLI args and options. @@ -57,4 +62,8 @@ impl Cli { pub fn cmd(&self) -> &String { return &self.raw_cli.cmd; } + + pub fn verbosity(&self) -> &u8 { + return &self.raw_cli.verbosity; + } } diff --git a/git/Cargo.toml b/git/Cargo.toml index 0dfcaa9..83ae158 100644 --- a/git/Cargo.toml +++ b/git/Cargo.toml @@ -6,3 +6,4 @@ edition = "2018" [dependencies] utils = { path = "../utils" } +log = "0.4.8" diff --git a/git/src/branch.rs b/git/src/branch.rs index e0c2827..60b29d9 100644 --- a/git/src/branch.rs +++ b/git/src/branch.rs @@ -1,16 +1,25 @@ +extern crate log; + use std::path::PathBuf; use std::process::Command; +use log::debug; use utils::assert_or_panic; pub fn get_current_branch( working_directory: &PathBuf, ) -> String { - let result = Command::new("git") + let mut cmd = Command::new("git"); + cmd .arg("rev-parse") .arg("--abbrev-ref") .arg("HEAD") .current_dir(&working_directory) + ; + + debug!("Running `{:?}`", cmd); + + let result = cmd .output() .expect("Failed to determine current branch.") ; @@ -25,10 +34,16 @@ pub fn get_current_branch( pub fn get_current_remote( working_directory: &PathBuf, ) -> String { - let result = Command::new("git") + let mut cmd = Command::new("git"); + cmd .arg("remote") .arg("show") .current_dir(&working_directory) + ; + + debug!("Running `{:?}`", cmd); + + let result = cmd .output() .expect("Failed to determine current remote.") ; @@ -45,7 +60,8 @@ pub fn get_merge_base_commit( remote: &String, base_branch: &String, ) -> String { - let result = Command::new("git") + let mut cmd = Command::new("git"); + cmd .arg("merge-base") .arg(format!( "{}/{}", @@ -54,6 +70,11 @@ pub fn get_merge_base_commit( )) .arg("HEAD") .current_dir(&working_directory) + ; + + debug!("Running `{:?}`", cmd); + + let result = cmd .output() .expect("Failed to determine merge base.") ; diff --git a/git/src/commits_range.rs b/git/src/commits_range.rs index 09648ae..b7c8681 100644 --- a/git/src/commits_range.rs +++ b/git/src/commits_range.rs @@ -9,12 +9,12 @@ pub struct CommitsRange { } impl CommitsRange { - pub fn to_str(&self) -> String { - return format!( - "{}..{}", - &self.from, - &self.to, - ); + pub fn from(&self) -> &String { + return &self.from; + } + + pub fn to(&self) -> &String { + return &self.to; } } diff --git a/git/src/path_inspector.rs b/git/src/path_inspector.rs index b4d005a..ea7c053 100644 --- a/git/src/path_inspector.rs +++ b/git/src/path_inspector.rs @@ -1,5 +1,8 @@ +extern crate log; + use std::path::PathBuf; use std::process::Command; +use log::{debug, info}; use utils::assert_or_panic; use crate::commits_range::CommitsRange; @@ -9,16 +12,31 @@ pub fn has_changes_in_paths( commits_range: &CommitsRange, paths: &Vec ) -> bool { - let result = Command::new("git") - .arg("log") - .arg(commits_range.to_str()) + let mut cmd = Command::new("git"); + cmd + .arg("diff") + .arg("--stat") + .arg(commits_range.from()) + .arg(commits_range.to()) .args(paths) .current_dir(&working_directory) + ; + + debug!("Running `{:?}`", cmd); + + let result = cmd .output() .expect("Failed to run git log command.") ; - assert_or_panic(&result, &String::from("git log")); + assert_or_panic(&result, &String::from("git diff")); + + info!( + "Detected diff from {} to {} :\n{}", + commits_range.from(), + commits_range.to(), + String::from_utf8(result.stdout.to_vec()).unwrap() + ); return !result.stdout.is_empty(); } diff --git a/logger/Cargo.toml b/logger/Cargo.toml new file mode 100644 index 0000000..3dec548 --- /dev/null +++ b/logger/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "logger" +version = "0.1.0" +authors = ["KNP Labs"] +edition = "2018" + +[dependencies] +log = "0.4.8" +simple_logger = { version = "1.6.0", default-features = false } diff --git a/logger/src/lib.rs b/logger/src/lib.rs new file mode 100644 index 0000000..20337ee --- /dev/null +++ b/logger/src/lib.rs @@ -0,0 +1,16 @@ +extern crate log; +extern crate simple_logger; + +use log::Level; + +pub fn configure_logger(verbosity: &u8) { + let level: Level; + + match *verbosity { + 1 => level = Level::Info, + 2 => level = Level::Debug, + _ => level = Level::Warn, + } + + simple_logger::init_with_level(level).unwrap(); +} diff --git a/src/main.rs b/src/main.rs index 30c0d16..26f9525 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,13 @@ use std::process::exit; use cli::Cli; use lib::should_skip_ci; +use logger::configure_logger; fn main() { let cli: Cli = Cli::new(); + configure_logger(cli.verbosity()); + let status_code: i32 = should_skip_ci( ¤t_dir().unwrap(), cli.paths(),