From 0e278b449c13588706a84f3a8d830b7cbac9d742 Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Thu, 14 May 2020 14:53:31 +0200 Subject: [PATCH 1/4] add logger package --- .gitignore | 1 + Cargo.lock | 29 +++++++++++++++++++++++++++++ Cargo.toml | 1 + cli/src/lib.rs | 9 +++++++++ logger/Cargo.toml | 9 +++++++++ logger/src/lib.rs | 14 ++++++++++++++ src/main.rs | 3 +++ 7 files changed, 66 insertions(+) create mode 100644 logger/Cargo.toml create mode 100644 logger/src/lib.rs 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..2c2bee1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,6 +90,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 +174,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 +191,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 +297,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 +306,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/cli/src/lib.rs b/cli/src/lib.rs index db498fe..5e5fc11 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")] + 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/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..17c195a --- /dev/null +++ b/logger/src/lib.rs @@ -0,0 +1,14 @@ +extern crate log; +extern crate simple_logger; + +use log::Level; + +pub fn configure_logger(verbosity: &u8) { + let mut level = Level::Warn; + + if 1 == *verbosity { + level = Level::Info; + } + + 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(), From 379d503d4890c4542b9683ead9deb54d1ce76b86 Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Thu, 14 May 2020 15:24:32 +0200 Subject: [PATCH 2/4] log git log output when verbose mode is on --- Cargo.lock | 1 + git/Cargo.toml | 1 + git/src/path_inspector.rs | 6 ++++++ 3 files changed, 8 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 2c2bee1..5abe38b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,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", ] 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/path_inspector.rs b/git/src/path_inspector.rs index b4d005a..876065d 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::info; use utils::assert_or_panic; use crate::commits_range::CommitsRange; @@ -12,6 +15,7 @@ pub fn has_changes_in_paths( let result = Command::new("git") .arg("log") .arg(commits_range.to_str()) + .arg("--stat") .args(paths) .current_dir(&working_directory) .output() @@ -20,5 +24,7 @@ pub fn has_changes_in_paths( assert_or_panic(&result, &String::from("git log")); + info!("{}", String::from_utf8(result.stdout.to_vec()).unwrap()); + return !result.stdout.is_empty(); } From 8bf07fde6177eed405c077ba90a5e4c08d55e5f5 Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Thu, 14 May 2020 16:06:18 +0200 Subject: [PATCH 3/4] add debug level of verbosity to print ran git commands --- Cargo.lock | 3 +++ ci/Cargo.toml | 3 +++ ci/src/lib.rs | 12 +++++++++++- cli/src/lib.rs | 2 +- git/src/branch.rs | 27 ++++++++++++++++++++++++--- git/src/path_inspector.rs | 10 ++++++++-- logger/src/lib.rs | 8 +++++--- 7 files changed, 55 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5abe38b..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" 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 5e5fc11..3311185 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -20,7 +20,7 @@ struct RawCli { // 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")] + #[structopt(short, long, parse(from_occurrences), help = "Verbosity mode : -v, -vv")] verbosity: u8, } 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/path_inspector.rs b/git/src/path_inspector.rs index 876065d..6c67c63 100644 --- a/git/src/path_inspector.rs +++ b/git/src/path_inspector.rs @@ -2,7 +2,7 @@ extern crate log; use std::path::PathBuf; use std::process::Command; -use log::info; +use log::{debug, info}; use utils::assert_or_panic; use crate::commits_range::CommitsRange; @@ -12,12 +12,18 @@ pub fn has_changes_in_paths( commits_range: &CommitsRange, paths: &Vec ) -> bool { - let result = Command::new("git") + let mut cmd = Command::new("git"); + cmd .arg("log") .arg(commits_range.to_str()) .arg("--stat") .args(paths) .current_dir(&working_directory) + ; + + debug!("Running `{:?}`", cmd); + + let result = cmd .output() .expect("Failed to run git log command.") ; diff --git a/logger/src/lib.rs b/logger/src/lib.rs index 17c195a..20337ee 100644 --- a/logger/src/lib.rs +++ b/logger/src/lib.rs @@ -4,10 +4,12 @@ extern crate simple_logger; use log::Level; pub fn configure_logger(verbosity: &u8) { - let mut level = Level::Warn; + let level: Level; - if 1 == *verbosity { - level = Level::Info; + match *verbosity { + 1 => level = Level::Info, + 2 => level = Level::Debug, + _ => level = Level::Warn, } simple_logger::init_with_level(level).unwrap(); From 4c5728ab43632c2c6aa6576de1c7f83cce63aa40 Mon Sep 17 00:00:00 2001 From: Nicolas MURE Date: Thu, 14 May 2020 17:05:15 +0200 Subject: [PATCH 4/4] print squashed detected diff Do not print the changes commit per commits for the commits within the range, but directly print the whole diff of the range start to the range end. This behavior is possible by changing the `git log` command usage to `git diff` instead. --- git/src/commits_range.rs | 12 ++++++------ git/src/path_inspector.rs | 14 ++++++++++---- 2 files changed, 16 insertions(+), 10 deletions(-) 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 6c67c63..ea7c053 100644 --- a/git/src/path_inspector.rs +++ b/git/src/path_inspector.rs @@ -14,9 +14,10 @@ pub fn has_changes_in_paths( ) -> bool { let mut cmd = Command::new("git"); cmd - .arg("log") - .arg(commits_range.to_str()) + .arg("diff") .arg("--stat") + .arg(commits_range.from()) + .arg(commits_range.to()) .args(paths) .current_dir(&working_directory) ; @@ -28,9 +29,14 @@ pub fn has_changes_in_paths( .expect("Failed to run git log command.") ; - assert_or_panic(&result, &String::from("git log")); + assert_or_panic(&result, &String::from("git diff")); - info!("{}", String::from_utf8(result.stdout.to_vec()).unwrap()); + 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(); }