gix_validate/
tag.rs

1use bstr::{BStr, BString, ByteSlice};
2
3///
4pub mod name {
5    use bstr::BString;
6
7    /// The error returned by [`name()`][super::name()].
8    #[derive(Debug)]
9    #[allow(missing_docs)]
10    #[non_exhaustive]
11    pub enum Error {
12        InvalidByte { byte: BString },
13        StartsWithSlash,
14        RepeatedSlash,
15        RepeatedDot,
16        LockFileSuffix,
17        ReflogPortion,
18        Asterisk,
19        StartsWithDot,
20        EndsWithDot,
21        EndsWithSlash,
22        Empty,
23    }
24
25    impl std::fmt::Display for Error {
26        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27            match self {
28                Error::InvalidByte { byte } => write!(
29                    f,
30                    "A ref must not contain invalid bytes or ascii control characters: {byte:?}"
31                ),
32                Error::StartsWithSlash => write!(f, "A reference name must not start with a slash '/'"),
33                Error::RepeatedSlash => write!(
34                    f,
35                    "Multiple slashes in a row are not allowed as they may change the reference's meaning"
36                ),
37                Error::RepeatedDot => write!(f, "A ref must not contain '..' as it may be mistaken for a range"),
38                Error::LockFileSuffix => write!(f, "A ref must not end with '.lock'"),
39                Error::ReflogPortion => write!(f, "A ref must not contain '@{{' which is a part of a ref-log"),
40                Error::Asterisk => write!(f, "A ref must not contain '*' character"),
41                Error::StartsWithDot => write!(f, "A ref must not start with a '.'"),
42                Error::EndsWithDot => write!(f, "A ref must not end with a '.'"),
43                Error::EndsWithSlash => write!(f, "A ref must not end with a '/'"),
44                Error::Empty => write!(f, "A ref must not be empty"),
45            }
46        }
47    }
48
49    impl std::error::Error for Error {}
50}
51
52/// Assure the given `input` resemble a valid git tag name, which is returned unchanged on success.
53/// Tag names are provided as names, like `v1.0` or `alpha-1`, without paths.
54pub fn name(input: &BStr) -> Result<&BStr, name::Error> {
55    match name_inner(input, Mode::Validate)? {
56        None => Ok(input),
57        Some(_) => {
58            unreachable!("When validating, the input isn't changed")
59        }
60    }
61}
62
63#[derive(Eq, PartialEq)]
64pub(crate) enum Mode {
65    Sanitize,
66    Validate,
67}
68
69pub(crate) fn name_inner(input: &BStr, mode: Mode) -> Result<Option<BString>, name::Error> {
70    let mut out: Option<BString> =
71        matches!(mode, Mode::Sanitize).then(|| BString::from(Vec::with_capacity(input.len())));
72    if input.is_empty() {
73        return if let Some(mut out) = out {
74            out.push(b'-');
75            Ok(Some(out))
76        } else {
77            Err(name::Error::Empty)
78        };
79    }
80    if *input.last().expect("non-empty") == b'/' && out.is_none() {
81        return Err(name::Error::EndsWithSlash);
82    }
83    if input.first() == Some(&b'/') && out.is_none() {
84        return Err(name::Error::StartsWithSlash);
85    }
86
87    let mut previous = 0;
88    let mut component_start;
89    let mut component_end = 0;
90    let last = input.len() - 1;
91    for (byte_pos, byte) in input.iter().enumerate() {
92        match byte {
93            b'\\' | b'^' | b':' | b'[' | b'?' | b' ' | b'~' | b'\0'..=b'\x1F' | b'\x7F' => {
94                if let Some(out) = out.as_mut() {
95                    out.push(b'-');
96                } else {
97                    return Err(name::Error::InvalidByte {
98                        byte: (&[*byte][..]).into(),
99                    });
100                }
101            }
102            b'*' => {
103                if let Some(out) = out.as_mut() {
104                    out.push(b'-');
105                } else {
106                    return Err(name::Error::Asterisk);
107                }
108            }
109
110            b'.' if previous == b'.' => {
111                if out.is_none() {
112                    return Err(name::Error::RepeatedDot);
113                }
114            }
115            b'.' if previous == b'/' => {
116                if let Some(out) = out.as_mut() {
117                    out.push(b'-');
118                } else {
119                    return Err(name::Error::StartsWithDot);
120                }
121            }
122            b'{' if previous == b'@' => {
123                if let Some(out) = out.as_mut() {
124                    out.push(b'-');
125                } else {
126                    return Err(name::Error::ReflogPortion);
127                }
128            }
129            b'/' if previous == b'/' => {
130                if out.is_none() {
131                    return Err(name::Error::RepeatedSlash);
132                }
133            }
134            c => {
135                if *c == b'/' {
136                    component_start = component_end;
137                    component_end = byte_pos;
138
139                    if input[component_start..component_end].ends_with_str(".lock") {
140                        if let Some(out) = out.as_mut() {
141                            while out.ends_with(b".lock") {
142                                let len_without_suffix = out.len() - b".lock".len();
143                                out.truncate(len_without_suffix);
144                            }
145                        } else {
146                            return Err(name::Error::LockFileSuffix);
147                        }
148                    }
149                }
150
151                if let Some(out) = out.as_mut() {
152                    out.push(*c);
153                }
154
155                if byte_pos == last && input[component_end + 1..].ends_with_str(".lock") {
156                    if let Some(out) = out.as_mut() {
157                        while out.ends_with(b".lock") {
158                            let len_without_suffix = out.len() - b".lock".len();
159                            out.truncate(len_without_suffix);
160                        }
161                    } else {
162                        return Err(name::Error::LockFileSuffix);
163                    }
164                }
165            }
166        }
167        previous = *byte;
168    }
169
170    if let Some(out) = out.as_mut() {
171        while out.last() == Some(&b'/') {
172            out.pop();
173        }
174        while out.first() == Some(&b'/') {
175            out.remove(0);
176        }
177    }
178    if out.as_ref().map_or(input, |b| b.as_bstr())[0] == b'.' {
179        if let Some(out) = out.as_mut() {
180            out[0] = b'-';
181        } else {
182            return Err(name::Error::StartsWithDot);
183        }
184    }
185    let last = out.as_ref().map_or(input, |b| b.as_bstr()).len() - 1;
186    if out.as_ref().map_or(input, |b| b.as_bstr())[last] == b'.' {
187        if let Some(out) = out.as_mut() {
188            let last = out.len() - 1;
189            out[last] = b'-';
190        } else {
191            return Err(name::Error::EndsWithDot);
192        }
193    }
194    Ok(out)
195}