Skip to content

Commit

Permalink
change: Invert behaviour to open::Options::strict_config(), with le…
Browse files Browse the repository at this point in the history
…nient being the default.

This means API users will get libgit2 behaviour but commands like `gix` can
change options to emulate `git` behaviour.
  • Loading branch information
Byron committed Aug 17, 2022
1 parent 067c334 commit 0235111
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
32 changes: 23 additions & 9 deletions git-repository/src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl ReplacementObjects {
}

/// The options used in [`ThreadSafeRepository::open_opts`]
#[derive(Default, Clone)]
#[derive(Clone)]
pub struct Options {
pub(crate) object_store_slots: git_odb::store::init::Slots,
pub(crate) replacement_objects: ReplacementObjects,
Expand All @@ -72,6 +72,21 @@ pub struct Options {
pub(crate) bail_if_untrusted: bool,
}

impl Default for Options {
fn default() -> Self {
Options {
object_store_slots: Default::default(),
replacement_objects: Default::default(),
permissions: Default::default(),
git_dir_trust: None,
filter_config_section: None,
lossy_config: None,
lenient_config: true,
bail_if_untrusted: false,
}
}
}

#[derive(Default, Clone)]
#[allow(dead_code)]
pub(crate) struct EnvironmentOverrides {
Expand Down Expand Up @@ -182,13 +197,12 @@ impl Options {
self
}

/// If set, default is false, invalid configuration values will be defaulted to acceptable values where when possible,
/// instead of yielding an error during startup.
/// If set, default is false, invalid configuration values will cause an error even if these can safely be defaulted.
///
/// This is recommended for all applications that prefer usability over correctness. `git` itslef by default is not lenient
/// towards malconfigured repositories.
pub fn lenient_config(mut self, toggle: bool) -> Self {
self.lenient_config = toggle;
/// This is recommended for all applications that prefer correctness over usability.
/// `git` itself by defaults to strict configuration mode to let you know if configuration is incorrect.
pub fn strict_config(mut self, toggle: bool) -> Self {
self.lenient_config = !toggle;
self
}

Expand All @@ -209,7 +223,7 @@ impl git_sec::trust::DefaultForLevel for Options {
filter_config_section: Some(config::section::is_trusted),
lossy_config: None,
bail_if_untrusted: false,
lenient_config: false,
lenient_config: true,
},
git_sec::Trust::Reduced => Options {
object_store_slots: git_odb::store::init::Slots::Given(32), // limit resource usage
Expand All @@ -218,7 +232,7 @@ impl git_sec::trust::DefaultForLevel for Options {
git_dir_trust: git_sec::Trust::Reduced.into(),
filter_config_section: Some(config::section::is_trusted),
bail_if_untrusted: false,
lenient_config: false,
lenient_config: true,
lossy_config: None,
},
}
Expand Down
7 changes: 4 additions & 3 deletions git-repository/tests/id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ fn prefix() -> crate::Result {

assert!(
matches!(
git_repository::open(worktree_dir.path()).unwrap_err(),
git_repository::open_opts(worktree_dir.path(), git::open::Options::isolated().strict_config(true))
.unwrap_err(),
git::open::Error::Config(git::config::Error::EmptyValue { .. })
),
"an empty core.abbrev fails the open operation in strict config mode, emulating git behaviour"
);
assert!(
git_repository::open_opts(worktree_dir.path(), git::open::Options::isolated().lenient_config(true)).is_ok(),
"but it can be made to work when we are lenient (good for APIs)"
git_repository::open(worktree_dir.path()).is_ok(),
"By default gitoxide acts like `libgit2` here and we prefer to be lenient when possible"
);
Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ pub fn main() -> Result<()> {
use git_repository as git;
let repository = args.repository;
let repository = move || {
git::ThreadSafeRepository::discover(repository)
let mut mapping: git::sec::trust::Mapping<git::open::Options> = Default::default();
mapping.full = mapping.full.strict_config(true);
mapping.reduced = mapping.reduced.strict_config(true);
git::ThreadSafeRepository::discover_opts(repository, Default::default(), mapping)
.map(git::Repository::from)
.map(|r| r.apply_environment())
};
Expand Down

0 comments on commit 0235111

Please sign in to comment.