use std::{
ops::ControlFlow,
path::{Path, PathBuf},
sync::Arc,
};
use gix_error::ErrorExt;
use gix_sec::Trust;
use crate::{Repository, bstr::BString};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RelatedRepositoryKind {
Submodule,
LinkedWorktree,
MainRepository,
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct RelatedRepository<'a> {
pub kind: RelatedRepositoryKind,
pub git_dir: &'a Path,
pub inferred_worktree: Option<&'a Path>,
pub initial_trust: Trust,
pub parent: &'a Repository,
}
type ConfigureRelatedRepository =
dyn Fn(RelatedRepository<'_>, Options) -> crate::Result<ControlFlow<(), Options>> + Send + Sync;
pub(crate) fn related_repository_skipped(git_dir: &Path) -> crate::Error {
gix_error::message!(
"Opening the repository at \"{}\" was skipped by the callback",
git_dir.display()
)
.raise()
}
#[derive(Copy, Clone)]
pub(crate) enum RepositoryTrust {
Precomputed(Trust),
Enforced(Trust),
}
impl RepositoryTrust {
pub(crate) fn level(self) -> Trust {
match self {
Self::Precomputed(level) | Self::Enforced(level) => level,
}
}
}
#[derive(Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Hash)]
pub struct Permissions {
pub env: permissions::Environment,
pub config: permissions::Config,
pub attributes: permissions::Attributes,
}
pub struct Options {
pub(crate) object_store_slots: gix_odb::store::init::Slots,
pub permissions: Permissions,
pub(crate) git_dir_trust: Option<RepositoryTrust>,
pub(crate) filter_config_section: Option<fn(&gix_config::file::Metadata) -> bool>,
pub(crate) lossy_config: bool,
pub(crate) lenient_config: bool,
pub(crate) bail_if_untrusted: bool,
pub(crate) api_config_overrides: Vec<BString>,
pub(crate) cli_config_overrides: Vec<BString>,
pub(crate) git_installation_config_path: Option<Box<std::path::Path>>,
pub(crate) system_config_path: Option<Box<std::path::Path>>,
pub(crate) use_repository_local_environment: bool,
pub(crate) open_path_as_is: bool,
pub(crate) retain_inferred_worktree_when_bare: bool,
pub(crate) current_dir: Option<PathBuf>,
configure_related_repository: Option<Arc<ConfigureRelatedRepository>>,
}
mod options;
pub mod permissions;
mod repository;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn copying_for_another_repository_clears_repository_specific_state() -> gix_testtools::TestResult {
let mut options = Options::isolated()
.with_enforced_trust(gix_sec::Trust::Reduced)
.open_path_as_is(true);
options.current_dir = Some("captured-cwd".into());
options.retain_inferred_worktree_when_bare = true;
let copied = options.clone_without_trust();
assert!(
copied.git_dir_trust.is_none(),
"ownership must determine the new repository's trust"
);
assert!(
copied.current_dir.is_none(),
"the opening call must capture its own CWD"
);
assert!(
!copied.open_path_as_is,
"path interpretation is specific to the original opening call"
);
assert!(
!copied.retain_inferred_worktree_when_bare,
"checkout retention is a one-shot policy"
);
assert!(
!copied.use_repository_local_environment,
"another repository must not inherit environment redirection"
);
Ok(())
}
#[cfg(feature = "sha1")]
#[test]
fn retained_bare_submodule_checkout_is_used_for_safe_directory_checks() -> gix_testtools::TestResult {
let root = gix_testtools::scripted_fixture_read_only("make_unlisted_gitlinks.sh")?.join("listed-gitfile-bare");
let git_dir = root.join(".git/embedded");
let worktree = root.join("d");
let cwd = gix_fs::current_dir(false)?;
for trust in [gix_sec::Trust::Full, gix_sec::Trust::Reduced] {
let mut options = Options::isolated().with_trust(trust).bail_if_untrusted(true);
options.current_dir = Some(cwd.clone());
options.retain_inferred_worktree_when_bare = true;
let opened =
crate::ThreadSafeRepository::open_from_paths(git_dir.clone(), Some(worktree.clone()), options, None);
if trust == gix_sec::Trust::Full {
let repo = opened?;
assert_eq!(
repo.work_tree.as_deref(),
Some(worktree.as_path()),
"the checkout is retained"
);
assert!(
!repo.linked_worktree_options.retain_inferred_worktree_when_bare,
"the submodule-only policy must not leak into later opens"
);
} else {
let err = opened.expect_err("reduced trust requires safe.directory approval");
assert!(
err.to_string()
.contains(worktree.canonicalize()?.to_string_lossy().as_ref()),
"safe.directory checks must use the retained checkout, not the Git directory: {err}"
);
}
}
Ok(())
}
}