Browse Source

Document `git-ref`

pull/39/head
Sebastian Thiel 6 months ago
parent
commit
91dce23c8f
No known key found for this signature in database GPG Key ID: 85194C08421980C9
  1. 4
      git-ref/src/lib.rs
  2. 19
      git-ref/src/validated.rs
  3. 2
      tasks.md

4
git-ref/src/lib.rs

@ -1,4 +1,6 @@
//! Various functionality related to git references
#![forbid(unsafe_code)]
#![deny(rust_2018_idioms)]
#![deny(missing_docs, rust_2018_idioms)]
///
pub mod validated;

19
git-ref/src/validated.rs

@ -2,7 +2,9 @@ use bstr::{BStr, BString};
use quick_error::quick_error;
quick_error! {
/// The error returned by [`name()`]
#[derive(Debug)]
#[allow(missing_docs)]
pub enum NameError {
InvalidByte(name: BString) {
display("A ref must not contain invalid bytes or ascii control characters: '{}'", name)
@ -31,16 +33,17 @@ quick_error! {
}
}
pub fn name(name: &BStr) -> Result<&BStr, NameError> {
if name.is_empty() {
/// Assure the given `bytes` resemble a valid git ref name, which are returned unchanged on success.
pub fn name(bytes: &BStr) -> Result<&BStr, NameError> {
if bytes.is_empty() {
return Err(NameError::Empty);
}
let mut last = 0;
for byte in name.iter() {
for byte in bytes.iter() {
match byte {
b'\\' | b'^' | b':' | b'[' | b'?' | b' ' | b'~' | b'\0'..=b'\x1F' | b'\x7F' => {
return Err(NameError::InvalidByte(name.into()))
return Err(NameError::InvalidByte(bytes.into()))
}
b'*' => return Err(NameError::Asterisk),
b'.' if last == b'.' => return Err(NameError::DoubleDot),
@ -49,14 +52,14 @@ pub fn name(name: &BStr) -> Result<&BStr, NameError> {
}
last = *byte;
}
if name[0] == b'.' {
if bytes[0] == b'.' {
return Err(NameError::StartsWithDot);
}
if *name.last().expect("non-empty") == b'/' {
if *bytes.last().expect("non-empty") == b'/' {
return Err(NameError::EndsWithSlash);
}
if name.ends_with(b".lock") {
if bytes.ends_with(b".lock") {
return Err(NameError::LockFileSuffix);
}
Ok(name)
Ok(bytes)
}

2
tasks.md

@ -10,7 +10,7 @@
* [x] git-features
* [x] git-object
* [x] git-url
* [ ] git-ref
* [x] git-ref
* [ ] git-packetline
* [ ] git-protocol
* [ ] git-transport

Loading…
Cancel
Save