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, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FilesOnlyOperation {
Gc,
Prune,
Repack,
CommitGraphWrite,
MidxWrite,
PackStore,
PackInstall,
LargestPackIndex,
LooseWrite,
FilesSource,
Offload,
PromisorRewrite,
PartialFetch,
ReceiveQuarantine,
DropLargeBlobs,
}
impl FilesOnlyOperation {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Gc => "gc",
Self::Prune => "prune",
Self::Repack => "repack",
Self::CommitGraphWrite => "commit_graph_write",
Self::MidxWrite => "midx_write",
Self::PackStore => "pack_store",
Self::PackInstall => "pack_install",
Self::LargestPackIndex => "largest_pack_index",
Self::LooseWrite => "loose_write",
Self::FilesSource => "files_source",
Self::Offload => "offload",
Self::PromisorRewrite => "promisor_rewrite",
Self::PartialFetch => "partial_fetch",
Self::ReceiveQuarantine => "receive_quarantine",
Self::DropLargeBlobs => "drop_large_blobs",
}
}
}
impl fmt::Display for FilesOnlyOperation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Gc => "garbage collection",
Self::Prune => "pruning unreachable objects",
Self::Repack => "repacking",
Self::CommitGraphWrite => "writing a commit-graph file",
Self::MidxWrite => "writing a multi-pack-index",
Self::PackStore => "the pack read cache",
Self::PackInstall => "installing a pack file",
Self::LargestPackIndex => "finding the largest pack index",
Self::LooseWrite => "writing loose object files",
Self::FilesSource => "opening the files-backed primary",
Self::Offload => "offloading blobs",
Self::PromisorRewrite => "rewriting promisor packs",
Self::PartialFetch => "a partial fetch",
Self::ReceiveQuarantine => "the receive quarantine directory",
Self::DropLargeBlobs => "dropping promised blobs",
})
}
}
#[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("cannot create {requested} ref storage: the ref store is injected")]
InitRefStorageWithInjectedRefStore {
requested: crate::ref_storage::RefStorageFormat,
},
#[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} needs the files-backed object store, but this repository's primary store is custom"
)]
UnsupportedObjectStore {
operation: FilesOnlyOperation,
},
#[error("{operation} deletes objects, which extensions.preciousObjects forbids here")]
PreciousObjectsRepository {
operation: &'static str,
},
#[error(
"object store hashes with {} but the repository's object format is {}",
store.name(),
repository.name()
)]
ObjectFormatMismatch {
repository: HashAlgo,
store: HashAlgo,
},
#[error("{}", crate::error::maintenance_running_message(holder))]
MaintenanceRunning {
holder: crate::maintenance::GcLockHolder,
},
}
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::InitRefStorageWithInjectedRefStore { .. } => {
"init_ref_storage_with_injected_ref_store"
}
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::ObjectFormatMismatch { .. } => "object_format_mismatch",
Self::MaintenanceRunning { .. } => "maintenance_running",
}
}
#[must_use]
pub fn area(&self) -> &'static str {
match self {
Self::GitDirPathIo(io) => match io.operation {
crate::error::PathIoOperation::GitDir(_) => "repository",
crate::error::PathIoOperation::Worktree(_) => "worktree",
},
Self::RequiresWorkTree
| Self::PromisorFetcherMissing
| Self::UnknownRepositoryExtensions(_)
| Self::V1OnlyExtensionsInFormatZero(_) => "repository",
_ => "repository",
}
}
#[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>;
#[cfg(test)]
#[path = "files_only_operation_tests.rs"]
mod files_only_operation_tests;