Skip to content

Commit

Permalink
Merge pull request #22 from KnpLabs/v0.2.0
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
nm2107 authored May 14, 2020
2 parents 213de18 + c7852de commit 8b92cb6
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 28 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
ci/Cargo.lock
cli/Cargo.lock
git/Cargo.lock
logger/Cargo.lock
utils/Cargo.lock
target/
51 changes: 42 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ssc"
version = "0.1.0"
version = "0.2.0"
description = "A CLI tool to skip a CI build that is not concerned by the latest changes."
homepage = "https://github.com/KnpLabs/should-skip-ci"
repository = "git@github.com:KnpLabs/should-skip-ci.git"
Expand All @@ -15,6 +15,7 @@ edition = "2018"
ci = { path = "ci" }
cli = { path = "cli" }
git = { path = "git" }
logger = { path= "logger" }

[dev-dependencies]
rand = "0.7"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ $ sudo curl -sSL -o /usr/local/bin/ssc https://github.com/KnpLabs/should-skip-ci
$ sudo chmod +x /usr/local/bin/ssc
```

See the latest version in the [releases panel](https://github.com/KnpLabs/should-skip-ci/releases).

### Usage

```
Expand All @@ -71,6 +73,7 @@ USAGE:
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
-v,-vv Increases verbosity (-v for info, -vv for debug).
OPTIONS:
--base-branch <base-branch> The branch to use as a base to know from where the commit range starts (i.e. to
Expand Down
5 changes: 4 additions & 1 deletion ci/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[package]
name = "ci"
version = "0.1.0"
version = "0.2.0"
authors = ["KNP Labs"]
edition = "2018"

[dependencies]
log = "0.4.8"
12 changes: 11 additions & 1 deletion ci/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cli"
version = "0.1.0"
version = "0.2.0"
authors = ["KNP Labs"]
edition = "2018"

Expand Down
9 changes: 9 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion git/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "git"
version = "0.1.0"
version = "0.2.0"
authors = ["KNP Labs"]
edition = "2018"

[dependencies]
utils = { path = "../utils" }
log = "0.4.8"
27 changes: 24 additions & 3 deletions git/src/branch.rs
Original file line number Diff line number Diff line change
@@ -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.")
;
Expand All @@ -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.")
;
Expand All @@ -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!(
"{}/{}",
Expand All @@ -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.")
;
Expand Down
12 changes: 6 additions & 6 deletions git/src/commits_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
26 changes: 22 additions & 4 deletions git/src/path_inspector.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,16 +12,31 @@ pub fn has_changes_in_paths(
commits_range: &CommitsRange,
paths: &Vec<PathBuf>
) -> 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();
}
9 changes: 9 additions & 0 deletions logger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "logger"
version = "0.2.0"
authors = ["KNP Labs"]
edition = "2018"

[dependencies]
log = "0.4.8"
simple_logger = { version = "1.6.0", default-features = false }
16 changes: 16 additions & 0 deletions logger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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();
}
Loading

0 comments on commit 8b92cb6

Please sign in to comment.