Skip to content

Commit 0235111

Browse files
committed
change: Invert behaviour to open::Options::strict_config(), with lenient being the default.
This means API users will get libgit2 behaviour but commands like `gix` can change options to emulate `git` behaviour.
1 parent 067c334 commit 0235111

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

Diff for: git-repository/src/open.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl ReplacementObjects {
6060
}
6161

6262
/// The options used in [`ThreadSafeRepository::open_opts`]
63-
#[derive(Default, Clone)]
63+
#[derive(Clone)]
6464
pub struct Options {
6565
pub(crate) object_store_slots: git_odb::store::init::Slots,
6666
pub(crate) replacement_objects: ReplacementObjects,
@@ -72,6 +72,21 @@ pub struct Options {
7272
pub(crate) bail_if_untrusted: bool,
7373
}
7474

75+
impl Default for Options {
76+
fn default() -> Self {
77+
Options {
78+
object_store_slots: Default::default(),
79+
replacement_objects: Default::default(),
80+
permissions: Default::default(),
81+
git_dir_trust: None,
82+
filter_config_section: None,
83+
lossy_config: None,
84+
lenient_config: true,
85+
bail_if_untrusted: false,
86+
}
87+
}
88+
}
89+
7590
#[derive(Default, Clone)]
7691
#[allow(dead_code)]
7792
pub(crate) struct EnvironmentOverrides {
@@ -182,13 +197,12 @@ impl Options {
182197
self
183198
}
184199

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

@@ -209,7 +223,7 @@ impl git_sec::trust::DefaultForLevel for Options {
209223
filter_config_section: Some(config::section::is_trusted),
210224
lossy_config: None,
211225
bail_if_untrusted: false,
212-
lenient_config: false,
226+
lenient_config: true,
213227
},
214228
git_sec::Trust::Reduced => Options {
215229
object_store_slots: git_odb::store::init::Slots::Given(32), // limit resource usage
@@ -218,7 +232,7 @@ impl git_sec::trust::DefaultForLevel for Options {
218232
git_dir_trust: git_sec::Trust::Reduced.into(),
219233
filter_config_section: Some(config::section::is_trusted),
220234
bail_if_untrusted: false,
221-
lenient_config: false,
235+
lenient_config: true,
222236
lossy_config: None,
223237
},
224238
}

Diff for: git-repository/tests/id/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ fn prefix() -> crate::Result {
3131

3232
assert!(
3333
matches!(
34-
git_repository::open(worktree_dir.path()).unwrap_err(),
34+
git_repository::open_opts(worktree_dir.path(), git::open::Options::isolated().strict_config(true))
35+
.unwrap_err(),
3536
git::open::Error::Config(git::config::Error::EmptyValue { .. })
3637
),
3738
"an empty core.abbrev fails the open operation in strict config mode, emulating git behaviour"
3839
);
3940
assert!(
40-
git_repository::open_opts(worktree_dir.path(), git::open::Options::isolated().lenient_config(true)).is_ok(),
41-
"but it can be made to work when we are lenient (good for APIs)"
41+
git_repository::open(worktree_dir.path()).is_ok(),
42+
"By default gitoxide acts like `libgit2` here and we prefer to be lenient when possible"
4243
);
4344
Ok(())
4445
}

Diff for: src/plumbing/main.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ pub fn main() -> Result<()> {
5454
use git_repository as git;
5555
let repository = args.repository;
5656
let repository = move || {
57-
git::ThreadSafeRepository::discover(repository)
57+
let mut mapping: git::sec::trust::Mapping<git::open::Options> = Default::default();
58+
mapping.full = mapping.full.strict_config(true);
59+
mapping.reduced = mapping.reduced.strict_config(true);
60+
git::ThreadSafeRepository::discover_opts(repository, Default::default(), mapping)
5861
.map(git::Repository::from)
5962
.map(|r| r.apply_environment())
6063
};

0 commit comments

Comments
 (0)