Compare commits
4 Commits
60ffc3b806
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 227dfd41ed | |||
| 5a1c113597 | |||
| 0568ac6e27 | |||
| 57d97306c8 |
9
Cargo.lock
generated
9
Cargo.lock
generated
@@ -92,6 +92,8 @@ dependencies = [
|
|||||||
"clap",
|
"clap",
|
||||||
"crates_io_api",
|
"crates_io_api",
|
||||||
"rust_util",
|
"rust_util",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"toml",
|
"toml",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -942,9 +944,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.6.22"
|
version = "0.6.24"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d86f4838bd1e232610932d5a26fcf58305d1dbc936590612af966f8365afe1e7"
|
checksum = "947d43eb0c81799bdc22bd2c403680ccc60fbee365568662df3e4c7163d3239d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
@@ -1002,6 +1004,9 @@ name = "serde"
|
|||||||
version = "1.0.117"
|
version = "1.0.117"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
|
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
|
||||||
|
dependencies = [
|
||||||
|
"serde_derive",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ repository = "https://git.hatter.ink/hatter/cargotool"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
clap = "2.33.3"
|
clap = "2.33.3"
|
||||||
toml = "0.5.6"
|
toml = "0.5.6"
|
||||||
rust_util = "0.6.22"
|
rust_util = "0.6.24"
|
||||||
crates_io_api = "0.6"
|
crates_io_api = "0.6"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
|||||||
3
justfile
3
justfile
@@ -3,3 +3,6 @@ _:
|
|||||||
|
|
||||||
install:
|
install:
|
||||||
cargo install --path .
|
cargo install --path .
|
||||||
|
|
||||||
|
publish:
|
||||||
|
cargo publish
|
||||||
|
|||||||
@@ -1,7 +1,37 @@
|
|||||||
|
use std::fs;
|
||||||
use clap::{ArgMatches, SubCommand, App, Arg};
|
use clap::{ArgMatches, SubCommand, App, Arg};
|
||||||
use rust_util::util_git;
|
use rust_util::{util_git, util_file};
|
||||||
|
use serde::{Serialize, Deserialize};
|
||||||
use crate::cmd::{Command, CommandResult};
|
use crate::cmd::{Command, CommandResult};
|
||||||
|
|
||||||
|
const GIT_BRANCH_MASTER: &str = "master";
|
||||||
|
const GIT_BRANCH_MAIN: &str = "main";
|
||||||
|
const GIT_CHECK_JSON: &str = "gitcheck.json";
|
||||||
|
|
||||||
|
// filename: gitcheck.json
|
||||||
|
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct GitCheckConfig {
|
||||||
|
is_master_protected: Option<bool>, // default true
|
||||||
|
protected_branches: Option<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GitCheckConfig {
|
||||||
|
fn is_master_protected(&self) -> bool {
|
||||||
|
self.is_master_protected.unwrap_or(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_branch_protected(&self, branch: &str) -> bool {
|
||||||
|
if branch == GIT_BRANCH_MASTER || branch == GIT_BRANCH_MAIN {
|
||||||
|
return self.is_master_protected();
|
||||||
|
}
|
||||||
|
match &self.protected_branches {
|
||||||
|
None => false,
|
||||||
|
Some(bs) => bs.iter().find(|b| *b == branch).is_some(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct CommandImpl;
|
pub struct CommandImpl;
|
||||||
|
|
||||||
impl Command for CommandImpl {
|
impl Command for CommandImpl {
|
||||||
@@ -11,10 +41,20 @@ impl Command for CommandImpl {
|
|||||||
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
||||||
SubCommand::with_name(self.name()).about("Git check subcommand")
|
SubCommand::with_name(self.name()).about("Git check subcommand")
|
||||||
.arg(Arg::with_name("dir").long("dir").required(false).takes_value(true).help("Git check dir"))
|
.arg(Arg::with_name("dir").long("dir").required(false).takes_value(true).help("Git check dir"))
|
||||||
|
.arg(Arg::with_name("allow-master").long("allow-master").required(false).takes_value(false).help("Allow master"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandResult {
|
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandResult {
|
||||||
let dir = sub_arg_matches.value_of("dir");
|
let dir = sub_arg_matches.value_of("dir");
|
||||||
|
let allow_master = sub_arg_matches.is_present("allow-master");
|
||||||
|
let git_check_json = match dir {
|
||||||
|
None => GIT_CHECK_JSON.to_string(),
|
||||||
|
Some(dir) => util_file::join_path(dir, GIT_CHECK_JSON),
|
||||||
|
};
|
||||||
|
let git_check_config = match fs::read_to_string(&git_check_json) {
|
||||||
|
Ok(s) => serde_json::from_str::<GitCheckConfig>(&s).expect(&format!("Parse file failed: {}", GIT_CHECK_JSON)),
|
||||||
|
Err(_) => GitCheckConfig{ ..Default::default() },
|
||||||
|
};
|
||||||
let has_remote_cannotbe_fetched = util_git::git_fetch_dry_run(dir)?;
|
let has_remote_cannotbe_fetched = util_git::git_fetch_dry_run(dir)?;
|
||||||
if !has_remote_cannotbe_fetched {
|
if !has_remote_cannotbe_fetched {
|
||||||
failure!("Git repo has not fetched codes");
|
failure!("Git repo has not fetched codes");
|
||||||
@@ -30,6 +70,14 @@ impl Command for CommandImpl {
|
|||||||
failure!("Git repo has un-push changes");
|
failure!("Git repo has un-push changes");
|
||||||
return Ok(-3);
|
return Ok(-3);
|
||||||
}
|
}
|
||||||
|
if let Some(branch) = util_git::git_branch(dir).expect("Get git branch failed") {
|
||||||
|
if allow_master && (branch == GIT_BRANCH_MASTER || branch == GIT_BRANCH_MAIN) {
|
||||||
|
// just allow
|
||||||
|
} else if git_check_config.is_branch_protected(&branch) {
|
||||||
|
failure!("Git branch check failed, current branch: {} is protected", branch);
|
||||||
|
return Ok(-4);
|
||||||
|
}
|
||||||
|
}
|
||||||
Ok(0)
|
Ok(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user