use std::path::Path;
use alint_core::{
Context, Error, Level, PerFileRule, Result, Rule, RuleSpec, Scope, Violation, eval_per_file,
};
#[derive(Debug)]
pub struct NoMergeConflictMarkersRule {
id: String,
level: Level,
policy_url: Option<String>,
message: Option<String>,
scope: Scope,
}
impl Rule for NoMergeConflictMarkersRule {
alint_core::rule_common_impl!();
fn evaluate(&self, ctx: &Context<'_>) -> Result<Vec<Violation>> {
eval_per_file(self, ctx)
}
fn as_per_file(&self) -> Option<&dyn PerFileRule> {
Some(self)
}
}
impl PerFileRule for NoMergeConflictMarkersRule {
fn path_scope(&self) -> &Scope {
&self.scope
}
fn evaluate_file(
&self,
_ctx: &Context<'_>,
path: &Path,
bytes: &[u8],
) -> Result<Vec<Violation>> {
let Ok(text) = std::str::from_utf8(bytes) else {
return Ok(Vec::new());
};
let Some((line_no, marker)) = first_marker(text) else {
return Ok(Vec::new());
};
let msg = self.message.clone().unwrap_or_else(|| {
format!("unresolved merge conflict marker on line {line_no}: {marker:?}")
});
Ok(vec![
Violation::new(msg)
.with_path(std::sync::Arc::<Path>::from(path))
.with_location(line_no, 1),
])
}
}
fn first_marker(text: &str) -> Option<(usize, &'static str)> {
for (idx, line) in text.split('\n').enumerate() {
let trimmed_cr = line.strip_suffix('\r').unwrap_or(line);
if let Some(marker) = classify_marker(trimmed_cr) {
return Some((idx + 1, marker));
}
}
None
}
fn classify_marker(line: &str) -> Option<&'static str> {
if line.len() >= 8 {
let head = line.as_bytes();
if head.starts_with(b"<<<<<<< ") {
return Some("<<<<<<<");
}
if head.starts_with(b">>>>>>> ") {
return Some(">>>>>>>");
}
if head.starts_with(b"||||||| ") {
return Some("|||||||");
}
}
if line == "=======" {
return Some("=======");
}
None
}
pub fn build(spec: &RuleSpec) -> Result<Box<dyn Rule>> {
let _paths = spec.paths.as_ref().ok_or_else(|| {
Error::rule_config(
&spec.id,
"no_merge_conflict_markers requires a `paths` field",
)
})?;
if spec.fix.is_some() {
return Err(Error::rule_config(
&spec.id,
"no_merge_conflict_markers has no fix op — conflict resolution requires human judgment",
));
}
Ok(Box::new(NoMergeConflictMarkersRule {
id: spec.id.clone(),
level: spec.level,
policy_url: spec.policy_url.clone(),
message: spec.message.clone(),
scope: Scope::from_spec(spec)?,
}))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn flags_ours_marker() {
assert_eq!(
first_marker("clean\n<<<<<<< HEAD\nconflict\n"),
Some((2, "<<<<<<<"))
);
}
#[test]
fn flags_separator_marker() {
assert_eq!(
first_marker("<<<<<<< HEAD\nours\n=======\ntheirs\n>>>>>>> branch\n"),
Some((1, "<<<<<<<"))
);
}
#[test]
fn flags_diff3_base_marker() {
assert_eq!(first_marker("||||||| base\nshared\n"), Some((1, "|||||||")));
}
#[test]
fn ignores_marker_not_at_line_start() {
assert_eq!(
first_marker("leading text <<<<<<< HEAD\n"),
None,
"markers must be at column 1"
);
}
#[test]
fn ignores_short_runs() {
assert_eq!(first_marker("<<<<<< HEAD\n"), None);
}
#[test]
fn clean_file_is_silent() {
assert_eq!(first_marker("no markers here\njust code\n"), None);
}
#[test]
fn crlf_line_endings_are_handled() {
assert_eq!(
first_marker("clean\r\n<<<<<<< HEAD\r\nconflict\r\n"),
Some((2, "<<<<<<<"))
);
}
}