diff options
Diffstat (limited to 'vendor/gix-discover/src/parse.rs')
-rw-r--r-- | vendor/gix-discover/src/parse.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/vendor/gix-discover/src/parse.rs b/vendor/gix-discover/src/parse.rs new file mode 100644 index 000000000..589718327 --- /dev/null +++ b/vendor/gix-discover/src/parse.rs @@ -0,0 +1,33 @@ +use std::path::PathBuf; + +use bstr::ByteSlice; + +/// +pub mod gitdir { + use bstr::BString; + + /// The error returned by [`parse::gitdir()`][super::gitdir()]. + #[derive(Debug, thiserror::Error)] + #[allow(missing_docs)] + pub enum Error { + #[error("Format should be 'gitdir: <path>', but got: {:?}", .input)] + InvalidFormat { input: BString }, + #[error("Couldn't decode {:?} as UTF8", .input)] + IllformedUtf8 { input: BString }, + } +} + +/// Parse typical `gitdir` files as seen in worktrees and submodules. +pub fn gitdir(input: &[u8]) -> Result<PathBuf, gitdir::Error> { + let path = input + .strip_prefix(b"gitdir: ") + .ok_or_else(|| gitdir::Error::InvalidFormat { input: input.into() })? + .as_bstr(); + let path = path.trim_end().as_bstr(); + if path.is_empty() { + return Err(gitdir::Error::InvalidFormat { input: input.into() }); + } + Ok(gix_path::try_from_bstr(path) + .map_err(|_| gitdir::Error::IllformedUtf8 { input: input.into() })? + .into_owned()) +} |