use std::path::PathBuf;
use thiserror::Error;
use std::fmt;
use crate::error::{NotARepositoryReason, PathIoError};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnknownRepositoryExtensions {
pub extensions: Vec<String>,
}
impl fmt::Display for UnknownRepositoryExtensions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let lead = if self.extensions.len() == 1 {
"unknown repository extension found:"
} else {
"unknown repository extensions found:"
};
write!(f, "{lead}")?;
for ext in &self.extensions {
write!(f, "\n\t{ext}")?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct V1OnlyExtensionsInFormatZero {
pub extensions: Vec<String>,
}
impl fmt::Display for V1OnlyExtensionsInFormatZero {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let lead = if self.extensions.len() == 1 {
"repo version is 0, but v1-only extension found:"
} else {
"repo version is 0, but v1-only extensions found:"
};
write!(f, "{lead}")?;
for ext in &self.extensions {
write!(f, "\n\t{ext}")?;
}
Ok(())
}
}
use crate::git_dir::GitDirPathOperation;
use crate::objects::HashAlgo;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum RepositoryError {
#[error("not a git repository (or any of the parent directories): {0}")]
NotARepository(NotARepositoryReason),
#[error("cannot use bare repository '{path}' (safe.bareRepository is 'explicit')")]
ForbiddenBareRepository {
path: String,
},
#[error("detected dubious ownership in repository at '{path}'")]
DubiousOwnership {
path: String,
owner_uid: Option<u32>,
effective_uid: Option<u32>,
},
#[error("unsupported repository format version '{version}'")]
UnsupportedRepositoryFormatVersion {
version: u32,
},
#[error("invalid value for 'extensions.refstorage': '{value}'")]
InvalidRefStorageFormat {
value: String,
},
#[error("cannot change object format in an existing repository")]
InitObjectFormatChange,
#[error("cannot combine bare repository with separate git directory")]
InitBareWithSeparateGitDir,
#[error("this operation needs a working tree")]
RequiresWorkTree,
#[error("promisor prefetch requires a PromisorFetcher on RepositoryOptions")]
PromisorFetcherMissing,
#[error("{0}")]
UnknownRepositoryExtensions(UnknownRepositoryExtensions),
#[error("{0}")]
V1OnlyExtensionsInFormatZero(V1OnlyExtensionsInFormatZero),
#[error("{0}")]
GitDirPathIo(PathIoError),
#[error("{operation} requires the files-backed object store; it is not supported with a custom primary")]
UnsupportedObjectStore {
operation: &'static str,
},
#[error("{operation} deletes objects, which extensions.preciousObjects forbids here")]
PreciousObjectsRepository {
operation: &'static str,
},
#[error(
"object database {} is not the repository's objects directory {}",
builder.display(),
repository.display()
)]
OdbObjectsDirMismatch {
builder: PathBuf,
repository: PathBuf,
},
#[error(
"object store hashes with {} but the repository's object format is {}",
store.name(),
repository.name()
)]
ObjectFormatMismatch {
repository: HashAlgo,
store: HashAlgo,
},
}
impl RepositoryError {
#[must_use]
pub fn kind_name(&self) -> &'static str {
match self {
Self::NotARepository(..) => "not_a_repository",
Self::ForbiddenBareRepository { .. } => "forbidden_bare_repository",
Self::DubiousOwnership { .. } => "dubious_ownership",
Self::UnsupportedRepositoryFormatVersion { .. } => {
"unsupported_repository_format_version"
}
Self::InvalidRefStorageFormat { .. } => "invalid_ref_storage_format",
Self::InitObjectFormatChange => "init_object_format_change",
Self::InitBareWithSeparateGitDir => "init_bare_with_separate_git_dir",
Self::RequiresWorkTree => "requires_work_tree",
Self::PromisorFetcherMissing => "message",
Self::UnknownRepositoryExtensions(_) | Self::V1OnlyExtensionsInFormatZero(_) => {
"message"
}
Self::GitDirPathIo(io) => match io.operation {
crate::error::PathIoOperation::GitDir(_) => "git_dir_path_io",
crate::error::PathIoOperation::Worktree(_) => "worktree_path_io",
},
Self::UnsupportedObjectStore { .. } => "unsupported_object_store",
Self::PreciousObjectsRepository { .. } => "precious_objects_repository",
Self::OdbObjectsDirMismatch { .. } => "odb_objects_dir_mismatch",
Self::ObjectFormatMismatch { .. } => "object_format_mismatch",
}
}
#[must_use]
pub fn git_dir_path_io(
path: impl Into<String>,
operation: GitDirPathOperation,
source: std::io::Error,
) -> Self {
Self::GitDirPathIo(PathIoError::git_dir(path, operation, source))
}
}
impl From<PathIoError> for RepositoryError {
fn from(value: PathIoError) -> Self {
Self::GitDirPathIo(value)
}
}
pub type RepositoryResult<T> = std::result::Result<T, RepositoryError>;