Skip to main content

ralph/config/validation/
git_refs.rs

1//! Git-ref validation rules.
2//!
3//! Responsibilities:
4//! - Validate git branch/ref names used by configuration or runtime helpers.
5//! - Return human-readable invalidity reasons instead of boolean results.
6//!
7//! Not handled here:
8//! - Git command execution.
9//! - Queue, trust, or agent validation.
10//!
11//! Invariants/assumptions:
12//! - Rules track Ralph's supported subset of `git check-ref-format`.
13
14pub fn git_ref_invalid_reason(branch: &str) -> Option<String> {
15    if branch.is_empty() {
16        return Some("branch name cannot be empty".to_string());
17    }
18    if branch.chars().any(|c| c.is_ascii_control() || c == ' ') {
19        return Some("branch name cannot contain spaces or control characters".to_string());
20    }
21    if branch.contains("..") {
22        return Some("branch name cannot contain '..'".to_string());
23    }
24    if branch.contains("@{") {
25        return Some("branch name cannot contain '@{{'".to_string());
26    }
27    if branch.starts_with('.') {
28        return Some("branch name cannot start with '.'".to_string());
29    }
30    if branch.ends_with(".lock") {
31        return Some("branch name cannot end with '.lock'".to_string());
32    }
33    if branch.contains("//") || branch.contains("/.") || branch.ends_with('/') {
34        return Some("branch name contains invalid slash/dot pattern".to_string());
35    }
36    if branch == "@" || branch.starts_with("@/") || branch.contains("/@/") || branch.ends_with("/@")
37    {
38        return Some("branch name cannot be '@' or contain '@' as a path component".to_string());
39    }
40    if branch.contains('~') {
41        return Some("branch name cannot contain '~'".to_string());
42    }
43    if branch.contains('^') {
44        return Some("branch name cannot contain '^'".to_string());
45    }
46    if branch.contains(':') {
47        return Some("branch name cannot contain ':'".to_string());
48    }
49    if branch.contains('\\') {
50        return Some("branch name cannot contain '\\'".to_string());
51    }
52    None
53}