use derive_builder::Builder;
use git_checks_core::impl_prelude::*;
#[derive(Builder, Debug, Clone, Copy)]
#[non_exhaustive]
#[builder(field(private))]
pub struct RejectSeparateRoot {
#[builder(default = "true")]
required: bool,
}
impl Default for RejectSeparateRoot {
fn default() -> Self {
RejectSeparateRoot {
required: true,
}
}
}
impl RejectSeparateRoot {
pub fn builder() -> RejectSeparateRootBuilder {
Default::default()
}
}
impl Check for RejectSeparateRoot {
fn name(&self) -> &str {
"reject-separate-root"
}
fn check(&self, _: &CheckGitContext, commit: &Commit) -> Result<CheckResult, Box<dyn Error>> {
let mut result = CheckResult::new();
if commit.parents.is_empty() {
let message = format!("commit {} not allowed; it is a root commit.", commit.sha1,);
if self.required {
result.add_error(message);
} else {
result.add_warning(message);
}
}
Ok(result)
}
}
#[cfg(feature = "config")]
pub(crate) mod config {
use git_checks_config::{register_checks, CommitCheckConfig, IntoCheck};
use serde::Deserialize;
#[cfg(test)]
use serde_json::json;
use crate::RejectSeparateRoot;
#[derive(Deserialize, Debug)]
pub struct RejectSeparateRootConfig {
#[serde(default = "default_required")]
required: bool,
}
fn default_required() -> bool {
true
}
impl IntoCheck for RejectSeparateRootConfig {
type Check = RejectSeparateRoot;
fn into_check(self) -> Self::Check {
let mut builder = RejectSeparateRoot::builder();
builder.required(self.required);
builder
.build()
.expect("configuration mismatch for `RejectSeparateRoot`")
}
}
register_checks! {
RejectSeparateRootConfig {
"reject_separate_root" => CommitCheckConfig,
},
}
#[test]
fn test_reject_separate_root_config_empty() {
let json = json!({});
let check: RejectSeparateRootConfig = serde_json::from_value(json).unwrap();
assert!(check.required);
let check = check.into_check();
assert!(check.required);
}
#[test]
fn test_reject_separate_root_config_all_fields() {
let json = json!({
"required": false,
});
let check: RejectSeparateRootConfig = serde_json::from_value(json).unwrap();
assert!(!check.required);
let check = check.into_check();
assert!(!check.required);
}
}
#[cfg(test)]
mod tests {
use git_checks_core::Check;
use crate::test::*;
use crate::RejectSeparateRoot;
const NO_ROOT_TOPIC: &str = "ba3dc3cb09a558c88282742413a2dccb17d444fc";
const WITH_ROOT_TOPIC: &str = "ff560e8798ef7a9d10bf43660695f7155b49b398";
#[test]
fn test_reject_separate_root_builder_default() {
assert!(RejectSeparateRoot::builder().build().is_ok());
}
#[test]
fn test_reject_separate_root_name_commit() {
let check = RejectSeparateRoot::default();
assert_eq!(Check::name(&check), "reject-separate-root");
}
#[test]
fn test_reject_separate_root_no_root() {
let check = RejectSeparateRoot::default();
run_check_ok("test_reject_separate_root_no_root", NO_ROOT_TOPIC, check);
}
#[test]
fn test_reject_separate_root_with_root() {
let check = RejectSeparateRoot::default();
let result = run_check(
"test_reject_separate_root_with_root",
WITH_ROOT_TOPIC,
check,
);
test_result_errors(
result,
&["commit ff560e8798ef7a9d10bf43660695f7155b49b398 not allowed; it is a root commit."],
);
}
#[test]
fn test_reject_separate_root_warn() {
let check = RejectSeparateRoot::builder()
.required(false)
.build()
.unwrap();
let result = run_check("test_reject_separate_root_warn", WITH_ROOT_TOPIC, check);
test_result_warnings(
result,
&["commit ff560e8798ef7a9d10bf43660695f7155b49b398 not allowed; it is a root commit."],
);
}
#[test]
fn test_reject_separate_root_warn_no_root() {
let check = RejectSeparateRoot::builder()
.required(false)
.build()
.unwrap();
run_check_ok(
"test_reject_separate_root_warn_no_root",
NO_ROOT_TOPIC,
check,
);
}
}