use anyhow::{bail, Result};
#[derive(Debug, Clone)]
pub struct LfsPointer {
pub oid: String,
pub size: u64,
}
const LFS_SIGNATURE: &str = "version https://git-lfs.github.com/spec/";
pub fn is_lfs_pointer(content: &[u8]) -> bool {
if content.len() > 1024 {
return false; }
if let Ok(text) = std::str::from_utf8(content) {
text.starts_with(LFS_SIGNATURE)
} else {
false
}
}
pub fn parse_pointer(content: &[u8]) -> Result<LfsPointer> {
let text = std::str::from_utf8(content)?;
if !text.starts_with(LFS_SIGNATURE) {
bail!("Not an LFS pointer file");
}
let mut oid = None;
let mut size = None;
for line in text.lines() {
if let Some(rest) = line.strip_prefix("oid sha256:") {
oid = Some(rest.trim().to_string());
} else if let Some(rest) = line.strip_prefix("size ") {
size = Some(rest.trim().parse::<u64>()?);
}
}
match (oid, size) {
(Some(oid), Some(size)) => Ok(LfsPointer { oid, size }),
_ => bail!("Invalid LFS pointer: missing oid or size"),
}
}