gitsnitch 0.3.2

Lints your Git commit history against a declarative ruleset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Config loading, parsing, and semantic validation.
//!
//! Supported formats: TOML, JSON, JSON5, YAML.
//! Format is detected by file extension; extensionless files (e.g. `.gitsnitchrc`) are parsed as TOML.

use std::collections::HashMap;
use std::path::Path;

use regex::Regex;
use serde::Deserialize;
use thiserror::Error;

const MAX_SEVERITY: u8 = 250;

// ── Error ────────────────────────────────────────────────────────────────────

#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("failed to parse config as TOML: {0}")]
    Toml(#[from] toml::de::Error),

    #[error("failed to parse config as JSON: {0}")]
    Json(#[from] serde_json::Error),

    #[error("failed to parse config as JSON5: {0}")]
    Json5(#[from] json5::Error),

    #[error("failed to parse config as YAML: {0}")]
    Yaml(#[from] serde_yaml::Error),

    #[error("semantic error: {0}")]
    Semantic(String),
}

// ── Format ───────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigFormat {
    Toml,
    Json,
    Json5,
    Yaml,
}

impl ConfigFormat {
    /// Detect format from file extension. Extensionless → TOML.
    pub fn from_path(path: &Path) -> Self {
        match path.extension().and_then(|e| e.to_str()) {
            Some("json") => Self::Json,
            Some("json5") => Self::Json5,
            Some("yaml" | "yml") => Self::Yaml,
            _ => Self::Toml, // .toml and extensionless (e.g. .gitsnitchrc) both use TOML
        }
    }
}

// ── Config types ─────────────────────────────────────────────────────────────

pub type CustomMeta = HashMap<String, String>;

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
    pub api_version: ApiVersion,

    #[serde(default)]
    pub history: Option<History>,

    #[serde(default)]
    pub custom_meta: CustomMeta,

    #[serde(default)]
    pub violation_severity_as_exit_code: bool,

    #[serde(default = "defaults::severity_bands")]
    pub severity_bands: SeverityBands,

    #[serde(default)]
    pub assertions: Vec<Assertion>,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
pub enum ApiVersion {
    #[serde(rename = "pre")]
    Pre,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
#[allow(clippy::struct_field_names)]
pub struct History {
    #[serde(default = "defaults::autoheal_shallow")]
    pub autoheal_shallow: AutohealShallow,

    #[serde(default = "defaults::autoheal_shallow_shift")]
    pub autoheal_shallow_shift: u32,

    #[serde(default = "defaults::autoheal_shallow_tries")]
    pub autoheal_shallow_tries: u32,
}

impl Default for History {
    fn default() -> Self {
        Self {
            autoheal_shallow: defaults::autoheal_shallow(),
            autoheal_shallow_shift: defaults::autoheal_shallow_shift(),
            autoheal_shallow_tries: defaults::autoheal_shallow_tries(),
        }
    }
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AutohealShallow {
    Never,
    Incremental,
    Full,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SeverityBands {
    #[serde(rename = "Fatal")]
    pub fatal: u8,
    #[serde(rename = "Error")]
    pub error: u8,
    #[serde(rename = "Warning")]
    pub warning: u8,
    #[serde(rename = "Information")]
    pub information: u8,
}

impl Default for SeverityBands {
    fn default() -> Self {
        defaults::severity_bands()
    }
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Assertion {
    pub alias: String,

    #[serde(default)]
    pub skip: bool,

    #[serde(default)]
    pub description: String,

    #[serde(default)]
    pub banner: String,

    #[serde(default)]
    pub hint: String,

    #[serde(default = "defaults::severity")]
    pub severity: u8,

    pub must_satisfy: ConditionContainer,

    #[serde(default)]
    pub skip_if: Option<ConditionContainer>,

    #[serde(default)]
    pub custom_meta: CustomMeta,
}

#[derive(Debug, Deserialize)]
pub struct ConditionContainer {
    pub condition: Condition,
}

#[derive(Debug, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Condition {
    MsgMatchAny(MsgMatchCondition),
    MsgMatchNone(MsgMatchCondition),
    DiffMatchAny(DiffMatchCondition),
    DiffMatchNone(DiffMatchCondition),
    BranchMatch(BranchMatchCondition),
    ThresholdCompare(ThresholdCondition),
}

#[derive(Debug, Deserialize)]
pub struct MsgMatchCondition {
    #[serde(default)]
    pub name: String,
    pub mode: MsgMode,
    pub patterns: Vec<String>,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum MsgMode {
    Raw,
    Title,
    Body,
}

#[derive(Debug, Deserialize)]
pub struct DiffMatchCondition {
    #[serde(default)]
    pub name: String,
    pub mode: DiffMode,
    pub patterns: Vec<String>,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum DiffMode {
    Raw,
    File,
    Line,
}

#[derive(Debug, Deserialize)]
pub struct BranchMatchCondition {
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub patterns: Vec<String>,
}

#[derive(Debug, Deserialize)]
pub struct ThresholdCondition {
    #[serde(default)]
    pub name: String,
    pub metric: ThresholdMetric,
    pub operator: ThresholdOperator,
    pub value: u32,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ThresholdMetric {
    LineCount,
    FileCount,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ThresholdOperator {
    Lte,
    Gte,
}

// ── Defaults ─────────────────────────────────────────────────────────────────

mod defaults {
    use super::{AutohealShallow, SeverityBands};

    pub const fn severity() -> u8 {
        10
    }
    pub const fn autoheal_shallow_shift() -> u32 {
        10
    }
    pub const fn autoheal_shallow_tries() -> u32 {
        6
    }
    pub const fn autoheal_shallow() -> AutohealShallow {
        AutohealShallow::Incremental
    }
    pub const fn severity_bands() -> SeverityBands {
        SeverityBands {
            fatal: 250,
            error: 10,
            warning: 1,
            information: 0,
        }
    }
}

// ── Parse ─────────────────────────────────────────────────────────────────────

/// Parse config content using the format detected from `source_path`.
/// If `source_path` is `None` (e.g. stdin), defaults to TOML.
pub fn parse(content: &str, source_path: Option<&Path>) -> Result<Config, ConfigError> {
    let format = source_path.map_or(ConfigFormat::Toml, ConfigFormat::from_path);
    let config = match format {
        ConfigFormat::Toml => toml::from_str(content)?,
        ConfigFormat::Json => serde_json::from_str(content)?,
        ConfigFormat::Json5 => json5::from_str(content)?,
        ConfigFormat::Yaml => serde_yaml::from_str(content)?,
    };
    validate(config)
}

// ── Semantic validation ───────────────────────────────────────────────────────

fn validate(config: Config) -> Result<Config, ConfigError> {
    match &config.api_version {
        ApiVersion::Pre => {}
    }
    validate_assertions(&config.assertions)?;
    validate_severity_bands(&config.severity_bands)?;
    Ok(config)
}

pub fn validate_assertions(assertions: &[Assertion]) -> Result<(), ConfigError> {
    validate_assertion_aliases(assertions)?;
    validate_assertion_severities(assertions)?;
    validate_assertion_patterns(assertions)?;
    Ok(())
}

fn validate_assertion_aliases(assertions: &[Assertion]) -> Result<(), ConfigError> {
    let mut seen = std::collections::HashSet::new();
    for assertion in assertions {
        if !seen.insert(assertion.alias.as_str()) {
            return Err(ConfigError::Semantic(format!(
                "duplicate assertion alias: '{}'",
                assertion.alias
            )));
        }
    }
    Ok(())
}

fn validate_assertion_severities(assertions: &[Assertion]) -> Result<(), ConfigError> {
    for assertion in assertions {
        if assertion.severity > MAX_SEVERITY {
            return Err(ConfigError::Semantic(format!(
                "assertion '{}' has severity {} which must be <= {MAX_SEVERITY}",
                assertion.alias, assertion.severity
            )));
        }
    }

    Ok(())
}

fn validate_assertion_patterns(assertions: &[Assertion]) -> Result<(), ConfigError> {
    for assertion in assertions {
        validate_condition_patterns(
            &assertion.must_satisfy.condition,
            &assertion.alias,
            "must_satisfy",
        )?;

        if let Some(skip_if) = &assertion.skip_if {
            validate_condition_patterns(&skip_if.condition, &assertion.alias, "skip_if")?;
        }
    }

    Ok(())
}

fn validate_condition_patterns(
    condition: &Condition,
    assertion_alias: &str,
    condition_field: &str,
) -> Result<(), ConfigError> {
    let patterns = match condition {
        Condition::MsgMatchAny(cond) | Condition::MsgMatchNone(cond) => Some(&cond.patterns),
        Condition::DiffMatchAny(cond) | Condition::DiffMatchNone(cond) => Some(&cond.patterns),
        Condition::BranchMatch(cond) => Some(&cond.patterns),
        Condition::ThresholdCompare(_) => None,
    };

    if let Some(patterns) = patterns {
        for (index, pattern) in patterns.iter().enumerate() {
            Regex::new(pattern).map_err(|error| {
                ConfigError::Semantic(format!(
                    "assertion '{assertion_alias}' has invalid regex in {condition_field}.patterns[{index}]: '{pattern}' ({error})"
                ))
            })?;
        }
    }

    Ok(())
}

fn validate_severity_bands(bands: &SeverityBands) -> Result<(), ConfigError> {
    for (name, value) in [
        ("Fatal", bands.fatal),
        ("Error", bands.error),
        ("Warning", bands.warning),
        ("Information", bands.information),
    ] {
        if value > MAX_SEVERITY {
            return Err(ConfigError::Semantic(format!(
                "severity_bands: {name} ({value}) must be <= {MAX_SEVERITY}"
            )));
        }
    }

    // Bands must be strictly monotonic: Fatal > Error > Warning >= Information.
    // Fatal must be strictly greater than Error, Error strictly greater than Warning.
    if bands.fatal <= bands.error {
        return Err(ConfigError::Semantic(format!(
            "severity_bands: Fatal ({}) must be greater than Error ({})",
            bands.fatal, bands.error
        )));
    }
    if bands.error <= bands.warning {
        return Err(ConfigError::Semantic(format!(
            "severity_bands: Error ({}) must be greater than Warning ({})",
            bands.error, bands.warning
        )));
    }
    // Information is allowed to equal Warning (both can be 0).
    if bands.warning < bands.information {
        return Err(ConfigError::Semantic(format!(
            "severity_bands: Warning ({}) must be >= Information ({})",
            bands.warning, bands.information
        )));
    }
    Ok(())
}

#[cfg(test)]
mod tests;