.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
src/
git/
refs/
check.rs
4.0 KiB
lit.rs
5.7 KiB
name.rs
14.4 KiB
namespaced.rs
3.2 KiB
qualified.rs
6.2 KiB
refspec.rs
18.0 KiB
refs.rs
2.3 KiB
git.rs
54.7 KiB
jj.rs
4.7 KiB
lib.rs
12.2 KiB
oid.rs
3.9 KiB
Cargo.toml
330 B
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/repo/src/git/refs/check.rs
raw
| 1 | // Copyright (c) Kim Altintop <kim@eagain.st> |
| 2 | // Copyright (c) The Radicle Team |
| 3 | |
| 4 | //! Git refname validation. |
| 5 | use std::fmt; |
| 6 | |
| 7 | /// Options controlling Git refname validation. |
| 8 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 9 | pub struct Options { |
| 10 | /// Allows a refname without a slash. |
| 11 | pub allow_onelevel: bool, |
| 12 | /// Allows exactly one `*` pattern character. |
| 13 | pub allow_pattern: bool, |
| 14 | } |
| 15 | |
| 16 | /// A Git refname validation error. |
| 17 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 18 | #[non_exhaustive] |
| 19 | pub enum Error { |
| 20 | /// The refname is empty. |
| 21 | Empty, |
| 22 | /// The refname is the single character `@`. |
| 23 | LoneAt, |
| 24 | /// The refname contains consecutive slashes or a trailing slash. |
| 25 | Slash, |
| 26 | /// A component ends with `.lock`. |
| 27 | DotLock, |
| 28 | /// The refname contains consecutive dots. |
| 29 | DotDot, |
| 30 | /// The refname contains `@{`. |
| 31 | AtOpenBrace, |
| 32 | /// The refname contains an invalid character. |
| 33 | InvalidChar(char), |
| 34 | /// A component starts with dot. |
| 35 | StartsDot, |
| 36 | /// A component ends with dot. |
| 37 | EndsDot, |
| 38 | /// The refname contains an ASCII control character. |
| 39 | Control, |
| 40 | /// The refname contains a space. |
| 41 | Space, |
| 42 | /// The refname contains more than one `*`. |
| 43 | Pattern, |
| 44 | /// The refname must contain at least one slash. |
| 45 | OneLevel, |
| 46 | } |
| 47 | |
| 48 | impl fmt::Display for Error { |
| 49 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 50 | match self { |
| 51 | Self::Empty => f.write_str("empty input"), |
| 52 | Self::LoneAt => f.write_str("lone '@' character"), |
| 53 | Self::Slash => f.write_str("consecutive or trailing slash"), |
| 54 | Self::DotLock => f.write_str("ends with '.lock'"), |
| 55 | Self::DotDot => f.write_str("consecutive dots ('..')"), |
| 56 | Self::AtOpenBrace => f.write_str("at-open-brace ('@{')"), |
| 57 | Self::InvalidChar(c) => write!(f, "invalid character {c:?}"), |
| 58 | Self::StartsDot => f.write_str("component starts with '.'"), |
| 59 | Self::EndsDot => f.write_str("component ends with '.'"), |
| 60 | Self::Control => f.write_str("control character"), |
| 61 | Self::Space => f.write_str("whitespace"), |
| 62 | Self::Pattern => f.write_str("must contain at most one '*'"), |
| 63 | Self::OneLevel => f.write_str("must contain at least one '/'"), |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl std::error::Error for Error {} |
| 69 | |
| 70 | /// Validates that `name` is a Git refname. |
| 71 | pub fn ref_format(opts: Options, name: &str) -> Result<(), Error> { |
| 72 | match name { |
| 73 | "" => return Err(Error::Empty), |
| 74 | "@" => return Err(Error::LoneAt), |
| 75 | "." => return Err(Error::StartsDot), |
| 76 | _ => {} |
| 77 | } |
| 78 | let mut globs = 0usize; |
| 79 | let mut parts = 0usize; |
| 80 | |
| 81 | for component in name.split('/') { |
| 82 | if component.is_empty() { |
| 83 | return Err(Error::Slash); |
| 84 | } |
| 85 | if component.ends_with(".lock") { |
| 86 | return Err(Error::DotLock); |
| 87 | } |
| 88 | parts += 1; |
| 89 | validate_component(component, &mut globs)?; |
| 90 | } |
| 91 | |
| 92 | if parts < 2 && !opts.allow_onelevel { |
| 93 | Err(Error::OneLevel) |
| 94 | } else if globs > 1 && opts.allow_pattern { |
| 95 | Err(Error::Pattern) |
| 96 | } else if globs > 0 && !opts.allow_pattern { |
| 97 | Err(Error::InvalidChar('*')) |
| 98 | } else { |
| 99 | Ok(()) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// Validates one Git refname component. |
| 104 | fn validate_component(component: &str, globs: &mut usize) -> Result<(), Error> { |
| 105 | let next_chars = component.chars().skip(1).map(Some).chain([None]); |
| 106 | for (index, (current, next)) in component.chars().zip(next_chars).enumerate() { |
| 107 | match (current, next) { |
| 108 | ('.', Some('.')) => return Err(Error::DotDot), |
| 109 | ('@', Some('{')) => return Err(Error::AtOpenBrace), |
| 110 | ('\0' | '\\' | '~' | '^' | ':' | '?' | '[', _) => { |
| 111 | return Err(Error::InvalidChar(current)); |
| 112 | } |
| 113 | ('*', _) => *globs += 1, |
| 114 | ('.', _) if index == 0 => return Err(Error::StartsDot), |
| 115 | ('.', None) => return Err(Error::EndsDot), |
| 116 | (' ', _) => return Err(Error::Space), |
| 117 | (c, _) if c.is_ascii_control() => return Err(Error::Control), |
| 118 | _ => {} |
| 119 | } |
| 120 | } |
| 121 | Ok(()) |
| 122 | } |