Skip to main content

alint_rules/
no_submodules.rs

1//! `no_submodules` — flag the presence of a `.gitmodules` file at
2//! the repo root.
3//!
4//! Submodules introduce a second source of truth (the submodule's
5//! commit pointer) that drifts silently, complicates CI, and
6//! surprises users who `git clone` without `--recurse-submodules`.
7//! Many projects have adopted vendoring or package managers and
8//! want to keep submodules banned forever.
9//!
10//! This rule is intentionally *not* configurable by `paths` —
11//! `.gitmodules` at the repo root is what actually enables git
12//! submodules, and letting users override that would invite
13//! footguns. For more general "file X must not exist" checks,
14//! use `file_absent` instead.
15//!
16//! Fixable via `file_remove`, which deletes `.gitmodules`. Note
17//! this is the surface-level fix — the user still needs to run
18//! `git submodule deinit` and clean up `.git/modules/` themselves.
19
20use alint_core::{
21    Context, Error, FixSpec, Fixer, Level, PathsSpec, Result, Rule, RuleSpec, Scope, Violation,
22};
23
24use crate::fixers::FileRemoveFixer;
25
26#[derive(Debug)]
27pub struct NoSubmodulesRule {
28    id: String,
29    level: Level,
30    policy_url: Option<String>,
31    message: Option<String>,
32    scope: Scope,
33    fixer: Option<FileRemoveFixer>,
34}
35
36impl Rule for NoSubmodulesRule {
37    fn id(&self) -> &str {
38        &self.id
39    }
40    fn level(&self) -> Level {
41        self.level
42    }
43    fn policy_url(&self) -> Option<&str> {
44        self.policy_url.as_deref()
45    }
46
47    fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
48        let mut violations = Vec::new();
49        for entry in ctx.index.files() {
50            if !self.scope.matches(&entry.path) {
51                continue;
52            }
53            let msg = self.message.clone().unwrap_or_else(|| {
54                "`.gitmodules` present — git submodules are forbidden".to_string()
55            });
56            violations.push(Violation::new(msg).with_path(&entry.path));
57        }
58        Ok(violations)
59    }
60
61    fn fixer(&self) -> Option<&dyn Fixer> {
62        self.fixer.as_ref().map(|f| f as &dyn Fixer)
63    }
64}
65
66pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
67    if spec.paths.is_some() {
68        return Err(Error::rule_config(
69            &spec.id,
70            "no_submodules does not accept a `paths` field; it always targets `.gitmodules` at \
71             the repo root. Use `file_absent` for more general patterns.",
72        ));
73    }
74    let fixer = match &spec.fix {
75        Some(FixSpec::FileRemove { .. }) => Some(FileRemoveFixer),
76        Some(other) => {
77            return Err(Error::rule_config(
78                &spec.id,
79                format!(
80                    "fix.{} is not compatible with no_submodules",
81                    other.op_name()
82                ),
83            ));
84        }
85        None => None,
86    };
87    let pinned = PathsSpec::Single(".gitmodules".to_string());
88    Ok(Box::new(NoSubmodulesRule {
89        id: spec.id.clone(),
90        level: spec.level,
91        policy_url: spec.policy_url.clone(),
92        message: spec.message.clone(),
93        scope: Scope::from_paths_spec(&pinned)?,
94        fixer,
95    }))
96}