use std::fmt;
use std::fs;
use std::io::{ErrorKind, Read};
use std::path::{Path, PathBuf};
use shep_core::protocol::Vcs;
const READ_CAP: u64 = 4096;
const HEX_LENGTHS: [usize; 2] = [40, 64];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Absent {
NotACheckout,
Unreadable,
Unrecognised,
}
impl fmt::Display for Absent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::NotACheckout => "not a checkout",
Self::Unreadable => "unreadable",
Self::Unrecognised => "unrecognised",
})
}
}
#[must_use]
pub fn read_revision(dir: &Path) -> Option<Vcs> {
revision_of(dir).ok()
}
pub(crate) fn revision_of(dir: &Path) -> Result<Vcs, Absent> {
let git_dir = find_git_dir(dir)?;
let common_dir = common_dir_of(&git_dir)?;
if git_dir.join("reftable").is_dir() || common_dir.join("reftable").is_dir() {
return Err(Absent::Unrecognised);
}
let head = read_capped(&git_dir.join("HEAD"))?.ok_or(Absent::Unreadable)?;
let head = line(&head);
if let Some(name) = head.strip_prefix("ref: ") {
let revision = resolve_ref(name, &[&git_dir, &common_dir])?;
let vcs = Vcs::new(revision);
return Ok(match name.strip_prefix("refs/heads/") {
Some(branch) => vcs.with_branch(branch),
None => vcs,
});
}
if is_revision(head) {
return Ok(Vcs::new(head));
}
Err(Absent::Unrecognised)
}
fn find_git_dir(dir: &Path) -> Result<PathBuf, Absent> {
for candidate in dir.ancestors() {
let dot_git = candidate.join(".git");
let Ok(found) = fs::symlink_metadata(&dot_git) else {
continue;
};
let meta = if found.is_symlink() {
fs::metadata(&dot_git).map_err(|_| Absent::Unreadable)?
} else {
found
};
if meta.is_dir() {
return Ok(dot_git);
}
let pointer = read_capped(&dot_git)?.ok_or(Absent::Unreadable)?;
let target = line(&pointer)
.strip_prefix("gitdir: ")
.ok_or(Absent::Unrecognised)?;
return Ok(candidate.join(target));
}
Err(Absent::NotACheckout)
}
fn common_dir_of(git_dir: &Path) -> Result<PathBuf, Absent> {
Ok(match read_capped(&git_dir.join("commondir"))? {
Some(pointer) => git_dir.join(line(&pointer)),
None => git_dir.to_path_buf(),
})
}
fn resolve_ref(name: &str, dirs: &[&Path]) -> Result<String, Absent> {
if !is_ref_name(name) {
return Err(Absent::Unrecognised);
}
for dir in dirs {
if let Some(loose) = read_capped(&dir.join(name))? {
return revision_text(line(&loose));
}
}
for dir in dirs {
let Some(packed) = read_capped(&dir.join("packed-refs"))? else {
continue;
};
let found = packed
.lines()
.filter_map(|entry| entry.split_once(' '))
.find(|(_, packed_name)| *packed_name == name);
if let Some((revision, _)) = found {
return revision_text(revision);
}
}
Err(Absent::Unrecognised)
}
fn revision_text(text: &str) -> Result<String, Absent> {
if is_revision(text) {
Ok(text.to_string())
} else {
Err(Absent::Unrecognised)
}
}
fn line(text: &str) -> &str {
text.strip_suffix('\n').unwrap_or(text)
}
fn is_ref_name(name: &str) -> bool {
let Some(rest) = name.strip_prefix("refs/") else {
return false;
};
!rest.is_empty()
&& rest.split('/').all(|part| {
!part.is_empty()
&& !part.starts_with('.')
&& !part.contains("..")
&& !part.ends_with(".lock")
&& part
.chars()
.all(|c| !c.is_control() && !matches!(c, '\\' | ':' | ' '))
})
}
fn is_revision(text: &str) -> bool {
HEX_LENGTHS.contains(&text.len())
&& text.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
}
fn read_capped(path: &Path) -> Result<Option<String>, Absent> {
let meta = match fs::metadata(path) {
Ok(meta) => meta,
Err(err) if err.kind() == ErrorKind::NotFound => return Ok(None),
Err(_) => return Err(Absent::Unreadable),
};
if !meta.is_file() {
return Err(Absent::Unrecognised);
}
let mut text = String::new();
let read = fs::File::open(path)
.map_err(|_| Absent::Unreadable)?
.take(READ_CAP + 1)
.read_to_string(&mut text);
match read {
Ok(_) if text.len() as u64 > READ_CAP => Err(Absent::Unrecognised),
Ok(_) => Ok(Some(text)),
Err(err) if err.kind() == ErrorKind::InvalidData => Err(Absent::Unrecognised),
Err(_) => Err(Absent::Unreadable),
}
}
#[cfg(test)]
mod tests;