use std::collections::BTreeMap;
use std::fmt;
use serde::{Deserialize, Serialize};
use super::dates::{window_problem, KeepClock, WhichDate, WorkWindowRule, MAX_OFFSET_MINUTES};
use super::identity::{
anonymise_problem, set_problem, AnonymiseRule, CompiledMatch, IdentityRule, Who,
};
use super::message::{CompiledPattern, TextPattern, TextReplace};
use super::paths::{validate_glob, ExtractMode, PathMove};
use super::script::ScriptPolicy;
use super::textconv::{LineEnding, TextEncoding};
pub use super::trailers::TrailerIfExists;
use crate::domain::Oid;
pub const RECIPE_VERSION: u32 = 1;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RewriteRefusal {
OperationInProgress,
AutoStashPending,
Shallow,
PartialClone,
UnsupportedObjectFormat,
EmptyScope,
NoRule,
InvalidRule,
DirtyPathsTouched,
CheckedOutElsewhere,
ProtectedRef,
Stale,
ToolMissing,
ScriptFailed,
Purged,
ScriptsDisabled,
ScriptNotTrusted,
SignatureChoiceNeeded,
ExtractModeNeeded,
SigningFailed,
LfsObjectsMissing,
PathCollision,
}
impl RewriteRefusal {
pub fn code(self) -> &'static str {
match self {
Self::OperationInProgress => "operation_in_progress",
Self::AutoStashPending => "auto_stash_pending",
Self::Shallow => "shallow",
Self::PartialClone => "partial_clone",
Self::UnsupportedObjectFormat => "unsupported_object_format",
Self::EmptyScope => "empty_scope",
Self::NoRule => "no_rule",
Self::InvalidRule => "invalid_rule",
Self::DirtyPathsTouched => "dirty_paths_touched",
Self::CheckedOutElsewhere => "checked_out_elsewhere",
Self::ProtectedRef => "protected_ref",
Self::Stale => "stale",
Self::ToolMissing => "tool_missing",
Self::ScriptFailed => "script_failed",
Self::Purged => "purged",
Self::ScriptsDisabled => "scripts_disabled",
Self::ScriptNotTrusted => "script_not_trusted",
Self::SignatureChoiceNeeded => "signature_choice_needed",
Self::ExtractModeNeeded => "extract_mode_needed",
Self::SigningFailed => "signing_failed",
Self::LfsObjectsMissing => "lfs_objects_missing",
Self::PathCollision => "path_collision",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "scope", rename_all = "snake_case")]
pub enum Scope {
CurrentBranch {
#[serde(default, skip_serializing_if = "Option::is_none")]
since: Option<Oid>,
},
Selection { commits: Vec<Oid> },
Range { base: Oid, tip: Oid },
Refs { refs: Vec<String> },
AllLocal,
}
impl Default for Scope {
fn default() -> Self {
Scope::CurrentBranch { since: None }
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScopeOptions {
#[serde(default = "yes")]
pub include_tags: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub include_protected: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub extra_refs: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub protected_branches: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub host_protection: Option<super::HostProtection>,
}
fn yes() -> bool {
true
}
impl Default for ScopeOptions {
fn default() -> Self {
Self {
include_tags: true,
include_protected: Vec::new(),
extra_refs: Vec::new(),
protected_branches: Vec::new(),
host_protection: None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CommitterPolicy {
#[default]
Keep,
MeNow,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SignaturePolicy {
Strip,
ResignSigned,
ResignAll,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PruneEmpty {
#[default]
Auto,
Never,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RunOptions {
#[serde(default)]
pub committer: CommitterPolicy,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signatures: Option<SignaturePolicy>,
#[serde(default)]
pub prune_empty: PruneEmpty,
#[serde(default)]
pub update_message_hashes: bool,
#[serde(default)]
pub keep_backup_refs: bool,
}
impl Default for RunOptions {
fn default() -> Self {
Self {
committer: CommitterPolicy::Keep,
signatures: None,
prune_empty: PruneEmpty::Auto,
update_message_hashes: false,
keep_backup_refs: false,
}
}
}
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct SecretText(pub String);
impl SecretText {
pub fn finding_id(&self) -> Option<&str> {
super::secrets::is_finding_id(&self.0).then_some(self.0.as_str())
}
}
impl fmt::Debug for SecretText {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("SecretText(<redacted>)")
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "find", rename_all = "snake_case")]
pub enum ContentFind {
Pattern {
#[serde(flatten)]
pattern: TextPattern,
},
Secrets {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
literals: Vec<SecretText>,
#[serde(default)]
scan: bool,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ContentReplace {
#[serde(default)]
pub paths: Vec<String>,
#[serde(flatten)]
pub find: ContentFind,
#[serde(default)]
pub with: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_blob_bytes: Option<u64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TrailerAction {
Add,
Remove,
Map,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TrailerRule {
pub action: TrailerAction,
pub key: String,
#[serde(default)]
pub value: String,
#[serde(default)]
pub if_exists: TrailerIfExists,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub find: Option<TextPattern>,
#[serde(default)]
pub with: String,
}
impl TrailerRule {
pub fn add(key: &str, value: &str) -> Self {
Self {
action: TrailerAction::Add,
key: key.to_string(),
value: value.to_string(),
if_exists: TrailerIfExists::Add,
find: None,
with: String::new(),
}
}
pub fn remove(key: &str) -> Self {
Self {
action: TrailerAction::Remove,
..Self::add(key, "")
}
}
pub fn map(key: &str, old: &str, new: &str) -> Self {
Self {
action: TrailerAction::Map,
find: Some(TextPattern::literal(old)),
with: new.to_string(),
..Self::add(key, "")
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScriptLanguage {
Lua,
Shell,
Python,
LuaExternal,
}
impl ScriptLanguage {
pub const ALL: [ScriptLanguage; 4] = [
ScriptLanguage::Lua,
ScriptLanguage::Shell,
ScriptLanguage::Python,
ScriptLanguage::LuaExternal,
];
pub fn is_external(self) -> bool {
!matches!(self, ScriptLanguage::Lua)
}
pub fn name(self) -> &'static str {
match self {
Self::Lua => "lua",
Self::Shell => "shell",
Self::Python => "python",
Self::LuaExternal => "lua-external",
}
}
pub fn from_name(name: &str) -> Option<Self> {
Self::ALL.into_iter().find(|l| l.name() == name)
}
pub fn setting_name(self) -> Option<&'static str> {
match self {
Self::Lua => None,
Self::Shell => Some("shell"),
Self::Python => Some("python"),
Self::LuaExternal => Some("lua"),
}
}
pub fn from_extension(path: &str) -> Option<Self> {
let ext = path.rsplit_once('.')?.1.to_ascii_lowercase();
match ext.as_str() {
"lua" => Some(Self::Lua),
"sh" | "bash" | "ps1" => Some(Self::Shell),
"py" => Some(Self::Python),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "from", rename_all = "snake_case")]
pub enum ScriptSource {
Inline {
text: String,
},
File {
path: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScriptRef {
pub language: ScriptLanguage,
pub source: ScriptSource,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub params: BTreeMap<String, String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LfsImportRule {
pub patterns: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub min_size: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LfsExportRule {
pub patterns: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EolRule {
pub target: LineEnding,
#[serde(default)]
pub paths: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub exclude: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub follow_attributes: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_blob_bytes: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StripExecRule {
pub paths: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub exclude: Vec<String>,
}
fn is_false(b: &bool) -> bool {
!*b
}
fn default_fallback() -> TextEncoding {
TextEncoding::Windows1252
}
fn is_default_fallback(e: &TextEncoding) -> bool {
*e == TextEncoding::Windows1252
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReencodeRule {
pub target: TextEncoding,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from: Option<TextEncoding>,
#[serde(
default = "default_fallback",
skip_serializing_if = "is_default_fallback"
)]
pub fallback: TextEncoding,
#[serde(default)]
pub paths: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub exclude: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub max_blob_bytes: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RuleKind {
Identity(IdentityRule),
Mailmap {
#[serde(default)]
apply_to: Who,
#[serde(default, skip_serializing_if = "Option::is_none")]
file: Option<String>,
},
Anonymise(AnonymiseRule),
DateShift {
by_seconds: i64,
#[serde(default)]
apply_to: WhichDate,
},
DateTimezone {
offset_minutes: i16,
keep: KeepClock,
#[serde(default)]
apply_to: WhichDate,
},
DateWindow(WorkWindowRule),
CommitterFromAuthor,
MessageReplace(TextReplace),
MessageDeleteLines {
pattern: TextPattern,
},
Trailer(TrailerRule),
ContentReplace(ContentReplace),
RemovePaths {
globs: Vec<String>,
},
RemoveBlobsOver {
bytes: u64,
},
MovePaths(PathMove),
LfsImport(LfsImportRule),
LfsExport(LfsExportRule),
LineEndings(EolRule),
StripExec(StripExecRule),
Reencode(ReencodeRule),
Script(ScriptRef),
RemoveSignatures,
MessageEncoding {
#[serde(default)]
names: bool,
},
}
impl RuleKind {
pub fn name(&self) -> &'static str {
match self {
Self::Identity(_) => "identity",
Self::Mailmap { .. } => "mailmap",
Self::Anonymise(_) => "anonymise",
Self::DateShift { .. } => "date_shift",
Self::DateTimezone { .. } => "date_timezone",
Self::DateWindow(_) => "date_window",
Self::CommitterFromAuthor => "committer_from_author",
Self::MessageReplace(_) => "message_replace",
Self::MessageDeleteLines { .. } => "message_delete_lines",
Self::Trailer(_) => "trailer",
Self::ContentReplace(_) => "content_replace",
Self::RemovePaths { .. } => "remove_paths",
Self::RemoveBlobsOver { .. } => "remove_blobs_over",
Self::MovePaths(_) => "move_paths",
Self::LfsImport(_) => "lfs_import",
Self::LfsExport(_) => "lfs_export",
Self::LineEndings(_) => "line_endings",
Self::StripExec(_) => "strip_exec",
Self::Reencode(_) => "reencode",
Self::Script(_) => "script",
Self::RemoveSignatures => "remove_signatures",
Self::MessageEncoding { .. } => "message_encoding",
}
}
pub fn changes_content(&self) -> bool {
matches!(
self,
Self::ContentReplace(_)
| Self::RemovePaths { .. }
| Self::RemoveBlobsOver { .. }
| Self::MovePaths(_)
| Self::LfsImport(_)
| Self::LfsExport(_)
| Self::LineEndings(_)
| Self::StripExec(_)
| Self::Reencode(_)
| Self::Script(_)
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Rule {
#[serde(default = "yes")]
pub enabled: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
pub kind: RuleKind,
}
impl Rule {
pub fn new(kind: RuleKind) -> Self {
Self {
enabled: true,
label: None,
kind,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Recipe {
#[serde(default = "version")]
pub version: u32,
#[serde(default)]
pub name: String,
#[serde(default)]
pub scope: Scope,
#[serde(default)]
pub scope_options: ScopeOptions,
#[serde(default)]
pub rules: Vec<Rule>,
#[serde(default)]
pub options: RunOptions,
#[serde(default, skip_serializing_if = "ScriptPolicy::is_default")]
pub script_policy: ScriptPolicy,
}
fn version() -> u32 {
RECIPE_VERSION
}
impl Recipe {
pub fn new(name: &str, scope: Scope, rules: Vec<Rule>) -> Self {
Self {
version: RECIPE_VERSION,
name: name.to_string(),
scope,
scope_options: ScopeOptions::default(),
rules,
options: RunOptions::default(),
script_policy: ScriptPolicy::default(),
}
}
pub fn enabled_rules(&self) -> impl Iterator<Item = (usize, &Rule)> {
self.rules.iter().enumerate().filter(|(_, r)| r.enabled)
}
pub fn extraction(&self) -> Option<(&str, Option<ExtractMode>)> {
self.enabled_rules().find_map(|(_, r)| match &r.kind {
RuleKind::MovePaths(PathMove::ExtractSubdir { dir, mode }) => {
Some((dir.as_str(), *mode))
}
_ => None,
})
}
pub fn saved(&self) -> Recipe {
let mut copy = self.clone();
copy.script_policy = ScriptPolicy::default();
for rule in &mut copy.rules {
if let RuleKind::ContentReplace(c) = &mut rule.kind {
if let ContentFind::Secrets { literals, scan } = &mut c.find {
literals.retain(|l| *scan && l.finding_id().is_some());
}
}
}
copy
}
pub fn holds_secrets(&self) -> bool {
self.rules.iter().any(|r| {
matches!(
&r.kind,
RuleKind::ContentReplace(ContentReplace {
find: ContentFind::Secrets { literals, scan },
..
}) if literals.iter().any(|l| !(*scan && l.finding_id().is_some()))
)
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecipeIssue {
pub rule: Option<usize>,
pub refusal: RewriteRefusal,
pub detail: String,
}
impl fmt::Display for RecipeIssue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.rule {
Some(i) => write!(f, "rule {}: {}", i + 1, self.detail),
None => f.write_str(&self.detail),
}
}
}
fn issue(rule: Option<usize>, refusal: RewriteRefusal, detail: impl Into<String>) -> RecipeIssue {
RecipeIssue {
rule,
refusal,
detail: detail.into(),
}
}
pub fn validate_recipe(recipe: &Recipe) -> Result<(), RecipeIssue> {
use RewriteRefusal as R;
if recipe.version != RECIPE_VERSION {
return Err(issue(
None,
R::InvalidRule,
format!("recipe version {} is not supported", recipe.version),
));
}
match &recipe.scope {
Scope::Selection { commits } if commits.is_empty() => {
return Err(issue(None, R::EmptyScope, "no commit is selected"));
}
Scope::Refs { refs } if refs.iter().all(|r| r.trim().is_empty()) => {
return Err(issue(None, R::EmptyScope, "no branch or tag is named"));
}
Scope::Range { base, tip } if base == tip => {
return Err(issue(None, R::EmptyScope, "the range is empty"));
}
_ => {}
}
if recipe.enabled_rules().next().is_none() {
return Err(issue(None, R::NoRule, "no rule is enabled"));
}
for (i, rule) in recipe.enabled_rules() {
if let Some(detail) = rule_problem(&rule.kind) {
let refusal = match &rule.kind {
RuleKind::MovePaths(PathMove::ExtractSubdir { mode: None, .. }) => {
R::ExtractModeNeeded
}
_ => R::InvalidRule,
};
return Err(issue(Some(i), refusal, detail));
}
}
let mut moves = recipe.enabled_rules().filter_map(|(i, r)| match &r.kind {
RuleKind::MovePaths(m) => Some((i, m)),
_ => None,
});
let mut seen_move = false;
let mut extracted = false;
for (i, m) in &mut moves {
if let PathMove::ExtractSubdir { mode, .. } = m {
if extracted {
return Err(issue(Some(i), R::InvalidRule, "only one rule can extract"));
}
extracted = true;
if seen_move && matches!(mode, Some(ExtractMode::Clone | ExtractMode::Submodule)) {
return Err(issue(
Some(i),
R::InvalidRule,
"extracting into a clone or a submodule comes before the other path rules",
));
}
}
seen_move = true;
}
Ok(())
}
fn globs_problem(globs: &[String]) -> Option<String> {
globs.iter().find_map(|g| validate_glob(g).err())
}
fn filter_problem(paths: &[String], exclude: &[String]) -> Option<String> {
globs_problem(paths).or_else(|| globs_problem(exclude))
}
fn pattern_problem(p: &TextPattern) -> Option<String> {
CompiledPattern::new(p).err()
}
fn trailer_problem(t: &TrailerRule) -> Option<String> {
let ok_key = !t.key.is_empty() && t.key.chars().all(|c| c.is_ascii_alphanumeric() || c == '-');
if !ok_key {
return Some("a trailer key is letters, digits and `-`".to_string());
}
match t.action {
TrailerAction::Add => (t.value.trim().is_empty() || t.value.contains(['\n', '\r']))
.then(|| "a trailer to add needs a one-line value".to_string()),
TrailerAction::Remove => t.find.as_ref().and_then(pattern_problem),
TrailerAction::Map => match &t.find {
None => Some("a trailer to map needs the text to replace".to_string()),
Some(p) => pattern_problem(p).or_else(|| {
t.with
.contains(['\n', '\r'])
.then(|| "a trailer value is one line".to_string())
}),
},
}
}
fn rule_problem(kind: &RuleKind) -> Option<String> {
match kind {
RuleKind::Identity(r) => CompiledMatch::new(&r.when)
.err()
.or_else(|| set_problem(&r.set).map(str::to_string)),
RuleKind::Mailmap { .. }
| RuleKind::CommitterFromAuthor
| RuleKind::RemoveSignatures
| RuleKind::MessageEncoding { .. } => None,
RuleKind::Anonymise(r) => anonymise_problem(r),
RuleKind::DateShift { by_seconds, .. } => (*by_seconds == 0)
.then(|| "a shift of zero changes nothing".to_string())
.or_else(|| {
(by_seconds.unsigned_abs() > 200 * 365 * 86_400)
.then(|| "the shift is larger than 200 years".to_string())
}),
RuleKind::DateTimezone { offset_minutes, .. } => (offset_minutes.abs()
> MAX_OFFSET_MINUTES)
.then(|| "the offset is out of range".to_string()),
RuleKind::DateWindow(w) => window_problem(w),
RuleKind::MessageReplace(r) => pattern_problem(&r.find),
RuleKind::MessageDeleteLines { pattern } => pattern_problem(pattern),
RuleKind::Trailer(t) => trailer_problem(t),
RuleKind::ContentReplace(c) => globs_problem(&c.paths).or_else(|| match &c.find {
ContentFind::Pattern { pattern } => pattern_problem(pattern),
ContentFind::Secrets { literals, scan } => (literals.is_empty() && !*scan)
.then(|| "give the secret values or turn the scanner on".to_string())
.or_else(|| {
literals
.iter()
.any(|l| l.0.is_empty())
.then(|| "a secret value is empty".to_string())
}),
}),
RuleKind::RemovePaths { globs } => (globs.is_empty())
.then(|| "name at least one path".to_string())
.or_else(|| globs_problem(globs)),
RuleKind::RemoveBlobsOver { bytes } => {
(*bytes == 0).then(|| "the size limit must be above zero".to_string())
}
RuleKind::MovePaths(m) => m.problem(),
RuleKind::LfsImport(l) => (l.patterns.is_empty())
.then(|| "name at least one pattern".to_string())
.or_else(|| globs_problem(&l.patterns)),
RuleKind::LfsExport(l) => (l.patterns.is_empty())
.then(|| "name at least one pattern".to_string())
.or_else(|| globs_problem(&l.patterns)),
RuleKind::LineEndings(e) => filter_problem(&e.paths, &e.exclude),
RuleKind::StripExec(e) => (e.paths.is_empty())
.then(|| "name at least one path".to_string())
.or_else(|| filter_problem(&e.paths, &e.exclude)),
RuleKind::Reencode(e) => filter_problem(&e.paths, &e.exclude).or_else(|| {
(e.from == Some(e.target))
.then(|| "the source and the target encoding are the same".to_string())
}),
RuleKind::Script(s) => match &s.source {
ScriptSource::Inline { text } if text.trim().is_empty() => {
Some("the script is empty".to_string())
}
ScriptSource::File { path } if path.trim().is_empty() => {
Some("the script path is empty".to_string())
}
_ => None,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::rewrite::dates::{WeekWindow, Weekday};
use crate::domain::rewrite::identity::{IdentityMatch, IdentitySet};
use crate::domain::rewrite::paths::ExtractMode;
fn oid(byte: u8) -> Oid {
Oid([byte; 20])
}
fn identity_rule() -> Rule {
Rule::new(RuleKind::Identity(IdentityRule {
when: IdentityMatch::email("old@example.com"),
set: IdentitySet {
name: Some("Alice Example".into()),
email: Some("alice@example.com".into()),
},
apply_to: Who::Both,
include_taggers: true,
map_trailers: false,
}))
}
fn recipe(rules: Vec<Rule>) -> Recipe {
Recipe::new("test", Scope::default(), rules)
}
fn refused(r: &Recipe) -> (Option<usize>, RewriteRefusal) {
let e = validate_recipe(r).unwrap_err();
(e.rule, e.refusal)
}
#[test]
fn a_sound_recipe_validates() {
assert!(validate_recipe(&recipe(vec![identity_rule()])).is_ok());
}
#[test]
fn scope_refusals() {
let rules = vec![identity_rule()];
let mut r = recipe(rules.clone());
r.scope = Scope::Selection { commits: vec![] };
assert_eq!(refused(&r), (None, RewriteRefusal::EmptyScope));
r.scope = Scope::Refs {
refs: vec!["".into()],
};
assert_eq!(refused(&r), (None, RewriteRefusal::EmptyScope));
r.scope = Scope::Range {
base: oid(1),
tip: oid(1),
};
assert_eq!(refused(&r), (None, RewriteRefusal::EmptyScope));
r.scope = Scope::Range {
base: oid(1),
tip: oid(2),
};
assert!(validate_recipe(&r).is_ok());
r.scope = Scope::AllLocal;
assert!(validate_recipe(&r).is_ok());
}
#[test]
fn no_rule_and_only_disabled_rules_are_refused() {
assert_eq!(refused(&recipe(vec![])), (None, RewriteRefusal::NoRule));
let mut off = identity_rule();
off.enabled = false;
assert_eq!(refused(&recipe(vec![off])), (None, RewriteRefusal::NoRule));
}
#[test]
fn a_disabled_broken_rule_does_not_block_and_an_enabled_one_names_its_index() {
let mut broken = Rule::new(RuleKind::RemovePaths { globs: vec![] });
broken.enabled = false;
assert!(validate_recipe(&recipe(vec![identity_rule(), broken.clone()])).is_ok());
broken.enabled = true;
assert_eq!(
refused(&recipe(vec![identity_rule(), broken])),
(Some(1), RewriteRefusal::InvalidRule)
);
}
#[test]
fn a_wrong_version_is_refused() {
let mut r = recipe(vec![identity_rule()]);
r.version = 99;
assert_eq!(refused(&r), (None, RewriteRefusal::InvalidRule));
}
#[test]
fn every_rule_kind_has_a_refusal() {
let bad: Vec<RuleKind> = vec![
RuleKind::Identity(IdentityRule {
when: IdentityMatch::everyone(),
set: IdentitySet::default(),
apply_to: Who::Both,
include_taggers: false,
map_trailers: false,
}),
RuleKind::Identity(IdentityRule {
when: IdentityMatch::everyone(),
set: IdentitySet {
name: None,
email: Some("a<b@example.com".into()),
},
apply_to: Who::Both,
include_taggers: false,
map_trailers: false,
}),
RuleKind::Identity(IdentityRule {
when: IdentityMatch {
name: Some("(".into()),
regex: true,
..IdentityMatch::default()
},
set: IdentitySet {
name: Some("X".into()),
email: None,
},
apply_to: Who::Both,
include_taggers: false,
map_trailers: false,
}),
RuleKind::Anonymise(AnonymiseRule {
email_pattern: "fixed@anonymous.invalid".into(),
..AnonymiseRule::default()
}),
RuleKind::DateShift {
by_seconds: 0,
apply_to: WhichDate::Both,
},
RuleKind::DateShift {
by_seconds: i64::MAX,
apply_to: WhichDate::Both,
},
RuleKind::DateTimezone {
offset_minutes: 2000,
keep: KeepClock::Instant,
apply_to: WhichDate::Both,
},
RuleKind::DateWindow(WorkWindowRule {
windows: vec![WeekWindow::new(&[Weekday::Mon], 600, 600)],
..WorkWindowRule::evenings_and_weekends()
}),
RuleKind::MessageReplace(TextReplace {
find: TextPattern::regex("("),
with: String::new(),
}),
RuleKind::MessageReplace(TextReplace {
find: TextPattern::literal(""),
with: String::new(),
}),
RuleKind::MessageDeleteLines {
pattern: TextPattern::regex("["),
},
RuleKind::Trailer(TrailerRule::add("Co authored", "x")),
RuleKind::Trailer(TrailerRule::add("Signed-off-by", "")),
RuleKind::Trailer(TrailerRule::add("Signed-off-by", "two\nlines")),
RuleKind::Trailer(TrailerRule {
find: None,
..TrailerRule::map("Signed-off-by", "a", "b")
}),
RuleKind::Trailer(TrailerRule {
find: Some(TextPattern::regex("(")),
..TrailerRule::map("Signed-off-by", "a", "b")
}),
RuleKind::Trailer(TrailerRule::map("Signed-off-by", "a", "b\nc")),
RuleKind::Trailer(TrailerRule {
find: Some(TextPattern::regex("[")),
..TrailerRule::remove("Signed-off-by")
}),
RuleKind::ContentReplace(ContentReplace {
paths: vec!["../x".into()],
find: ContentFind::Pattern {
pattern: TextPattern::literal("a"),
},
with: String::new(),
max_blob_bytes: None,
}),
RuleKind::ContentReplace(ContentReplace {
paths: vec![],
find: ContentFind::Secrets {
literals: vec![],
scan: false,
},
with: String::new(),
max_blob_bytes: None,
}),
RuleKind::RemovePaths { globs: vec![] },
RuleKind::RemovePaths {
globs: vec!["[".into()],
},
RuleKind::RemoveBlobsOver { bytes: 0 },
RuleKind::MovePaths(PathMove::IntoSubdir { dir: "".into() }),
RuleKind::LfsImport(LfsImportRule {
patterns: vec![],
min_size: None,
}),
RuleKind::LfsExport(LfsExportRule { patterns: vec![] }),
RuleKind::Script(ScriptRef {
language: ScriptLanguage::Lua,
source: ScriptSource::Inline { text: " ".into() },
params: BTreeMap::new(),
}),
RuleKind::Script(ScriptRef {
language: ScriptLanguage::Shell,
source: ScriptSource::File { path: "".into() },
params: BTreeMap::new(),
}),
];
for (n, kind) in bad.into_iter().enumerate() {
let name = kind.name();
let r = recipe(vec![Rule::new(kind)]);
assert_eq!(
refused(&r),
(Some(0), RewriteRefusal::InvalidRule),
"case {n} ({name})"
);
}
}
#[test]
fn extracting_a_subdirectory_asks_for_a_mode() {
let ask = |mode| {
recipe(vec![Rule::new(RuleKind::MovePaths(
PathMove::ExtractSubdir {
dir: "app".into(),
mode,
},
))])
};
assert_eq!(
refused(&ask(None)),
(Some(0), RewriteRefusal::ExtractModeNeeded)
);
for mode in [
ExtractMode::Clone,
ExtractMode::InPlace,
ExtractMode::Submodule,
] {
assert!(validate_recipe(&ask(Some(mode))).is_ok());
}
}
#[test]
fn defaults_keep_the_committer_and_ask_about_signatures() {
let o = RunOptions::default();
assert_eq!(o.committer, CommitterPolicy::Keep);
assert_eq!(o.signatures, None);
assert!(!o.keep_backup_refs);
assert!(ScopeOptions::default().include_protected.is_empty());
}
fn everything() -> Recipe {
let mut r = Recipe::new(
"everything",
Scope::Range {
base: oid(1),
tip: oid(2),
},
vec![
identity_rule(),
Rule {
enabled: false,
label: Some("anon".into()),
kind: RuleKind::Anonymise(AnonymiseRule::default()),
},
Rule::new(RuleKind::DateShift {
by_seconds: -3600,
apply_to: WhichDate::AuthorDate,
}),
Rule::new(RuleKind::DateTimezone {
offset_minutes: -300,
keep: KeepClock::WallClock,
apply_to: WhichDate::Both,
}),
Rule::new(RuleKind::DateWindow(WorkWindowRule::evenings_and_weekends())),
Rule::new(RuleKind::CommitterFromAuthor),
Rule::new(RuleKind::Mailmap {
apply_to: Who::Author,
file: None,
}),
Rule::new(RuleKind::MessageReplace(TextReplace {
find: TextPattern::regex("PROJ-(\\d+)"),
with: "#$1".into(),
})),
Rule::new(RuleKind::MessageDeleteLines {
pattern: TextPattern::literal("WIP"),
}),
Rule::new(RuleKind::Trailer(TrailerRule::add(
"Signed-off-by",
"Alice Example <alice@example.com>",
))),
Rule::new(RuleKind::Trailer(TrailerRule::remove("Change-Id"))),
Rule::new(RuleKind::Trailer(TrailerRule::map(
"Co-authored-by",
"@old.org",
"@new.org",
))),
Rule::new(RuleKind::ContentReplace(ContentReplace {
paths: vec!["*.env".into()],
find: ContentFind::Pattern {
pattern: TextPattern::literal("token"),
},
with: "x".into(),
max_blob_bytes: Some(1 << 20),
})),
Rule::new(RuleKind::RemovePaths {
globs: vec!["secrets/".into()],
}),
Rule::new(RuleKind::RemoveBlobsOver { bytes: 10_000_000 }),
Rule::new(RuleKind::MovePaths(PathMove::ExtractSubdir {
dir: "app".into(),
mode: Some(ExtractMode::Submodule),
})),
Rule::new(RuleKind::LfsImport(LfsImportRule {
patterns: vec!["*.psd".into()],
min_size: Some(1 << 20),
})),
Rule::new(RuleKind::LfsExport(LfsExportRule {
patterns: vec!["*.zip".into()],
})),
Rule::new(RuleKind::Script(ScriptRef {
language: ScriptLanguage::Lua,
source: ScriptSource::Inline {
text: "return commit".into(),
},
params: BTreeMap::from([("who".to_string(), "Alice".to_string())]),
})),
],
);
r.scope_options.include_protected = vec!["main".into()];
r.options.committer = CommitterPolicy::MeNow;
r.options.signatures = Some(SignaturePolicy::ResignSigned);
r
}
#[test]
fn a_recipe_round_trips_through_toml() {
let r = everything();
let text = toml::to_string(&r).unwrap();
let back: Recipe = toml::from_str(&text).unwrap();
assert_eq!(back, r, "{text}");
}
#[test]
fn a_recipe_round_trips_through_json() {
let r = everything();
let text = serde_json::to_string(&r).unwrap();
let back: Recipe = serde_json::from_str(&text).unwrap();
assert_eq!(back, r);
}
#[test]
fn a_minimal_file_fills_in_defaults() {
let r: Recipe = toml::from_str(
r#"
name = "tidy"
[[rules]]
kind = { type = "message_delete_lines", pattern = { pattern = "WIP" } }
"#,
)
.unwrap();
assert_eq!(r.version, RECIPE_VERSION);
assert_eq!(r.scope, Scope::default());
assert!(r.rules[0].enabled);
assert_eq!(r.options.signatures, None);
assert!(validate_recipe(&r).is_ok());
}
#[test]
fn an_undecided_signature_choice_is_not_written() {
let r = recipe(vec![identity_rule()]);
let text = toml::to_string(&r).unwrap();
assert!(!text.contains("signatures"), "{text}");
}
fn secret_recipe() -> Recipe {
recipe(vec![Rule::new(RuleKind::ContentReplace(ContentReplace {
paths: vec![],
find: ContentFind::Secrets {
literals: vec![SecretText("hunter2-not-a-real-secret".into())],
scan: true,
},
with: "***REMOVED***".into(),
max_blob_bytes: None,
}))])
}
#[test]
fn saved_recipes_carry_no_secret_literal() {
let r = secret_recipe();
assert!(r.holds_secrets());
let saved = r.saved();
assert!(!saved.holds_secrets());
let text = toml::to_string(&saved).unwrap();
assert!(!text.contains("hunter2"), "{text}");
assert!(!serde_json::to_string(&saved).unwrap().contains("hunter2"));
let back: Recipe = toml::from_str(&text).unwrap();
assert!(validate_recipe(&back).is_ok());
}
#[test]
fn finding_ids_are_kept_when_saving_but_values_next_to_them_are_not() {
let mut r = secret_recipe();
if let RuleKind::ContentReplace(c) = &mut r.rules[0].kind {
if let ContentFind::Secrets { literals, .. } = &mut c.find {
literals.push(SecretText("f-0123456789ab".into()));
}
}
assert!(r.holds_secrets());
let saved = r.saved();
assert!(!saved.holds_secrets());
let text = serde_json::to_string(&saved).unwrap();
assert!(
text.contains("f-0123456789ab") && !text.contains("hunter2"),
"{text}"
);
assert!(!saved.holds_secrets());
let mut plain = saved.clone();
if let RuleKind::ContentReplace(c) = &mut plain.rules[0].kind {
c.find = ContentFind::Secrets {
literals: vec![SecretText("f-0123456789ab".into())],
scan: false,
};
}
assert!(plain.holds_secrets());
assert!(!plain.saved().holds_secrets());
}
#[test]
fn a_secret_never_shows_up_in_debug_output() {
let r = secret_recipe();
assert!(!format!("{r:?}").contains("hunter2"));
}
#[test]
fn refusal_codes_are_stable_and_distinct() {
use std::collections::HashSet;
let all = [
RewriteRefusal::OperationInProgress,
RewriteRefusal::AutoStashPending,
RewriteRefusal::Shallow,
RewriteRefusal::PartialClone,
RewriteRefusal::UnsupportedObjectFormat,
RewriteRefusal::EmptyScope,
RewriteRefusal::NoRule,
RewriteRefusal::InvalidRule,
RewriteRefusal::DirtyPathsTouched,
RewriteRefusal::CheckedOutElsewhere,
RewriteRefusal::ProtectedRef,
RewriteRefusal::Stale,
RewriteRefusal::ToolMissing,
RewriteRefusal::ScriptFailed,
RewriteRefusal::Purged,
RewriteRefusal::SignatureChoiceNeeded,
RewriteRefusal::ExtractModeNeeded,
];
let codes: HashSet<_> = all.iter().map(|r| r.code()).collect();
assert_eq!(codes.len(), all.len());
for r in all {
assert_eq!(
serde_json::to_string(&r).unwrap(),
format!("\"{}\"", r.code())
);
}
}
}