Skip to content

Commit 279ba7c

Browse files
committed
feat!: remove SnapshotMut::apply_cli_overrides() in favor of open::Options::cli_overrides().
1 parent 5b9bffe commit 279ba7c

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

git-repository/src/clone/fetch/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn replace_changed_local_config_file(repo: &mut Repository, mut config: git_
3838
for id in ids_to_remove {
3939
repo_config.remove_section_by_id(id);
4040
}
41-
crate::config::overrides::apply(&mut config, &repo.options.config_overrides, git_config::Source::Api)
41+
crate::config::overrides::append(&mut config, &repo.options.api_config_overrides, git_config::Source::Api)
4242
.expect("applied once and can be applied again");
4343
repo_config.append(config);
4444
repo.reread_values_and_clear_caches()

git-repository/src/config/cache/init.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ impl Cache {
3737
includes: use_includes,
3838
}: repository::permissions::Config,
3939
lenient_config: bool,
40-
config_overrides: &[BString],
40+
api_config_overrides: &[BString],
41+
cli_config_overrides: &[BString],
4142
) -> Result<Self, Error> {
4243
let options = git_config::file::init::Options {
4344
includes: if use_includes {
@@ -113,8 +114,11 @@ impl Cache {
113114
if use_env {
114115
globals.append(git_config::File::from_env(options)?.unwrap_or_default());
115116
}
116-
if !config_overrides.is_empty() {
117-
crate::config::overrides::apply(&mut globals, config_overrides, git_config::Source::Api)?;
117+
if !cli_config_overrides.is_empty() {
118+
crate::config::overrides::append(&mut globals, cli_config_overrides, git_config::Source::Cli)?;
119+
}
120+
if !api_config_overrides.is_empty() {
121+
crate::config::overrides::append(&mut globals, api_config_overrides, git_config::Source::Api)?;
118122
}
119123
globals
120124
};

git-repository/src/config/overrides.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub enum Error {
1717
SectionHeader(#[from] git_config::parse::section::header::Error),
1818
}
1919

20-
pub(crate) fn apply(
20+
pub(crate) fn append(
2121
config: &mut git_config::File<'static>,
2222
values: impl IntoIterator<Item = impl AsRef<BStr>>,
2323
source: git_config::Source,

git-repository/src/config/snapshot/access.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,17 @@ impl<'repo> Snapshot<'repo> {
8888

8989
/// Utilities
9090
impl<'repo> SnapshotMut<'repo> {
91-
/// Apply configuration values of the form `core.abbrev=5` or `remote.origin.url = foo` or `core.bool-implicit-true`
92-
/// to the repository configuration, marked with [source CLI][git_config::Source::Cli].
93-
pub fn apply_cli_overrides(
91+
/// Append configuration values of the form `core.abbrev=5` or `remote.origin.url = foo` or `core.bool-implicit-true`
92+
/// to the end of the repository configuration, with each section marked with the given `source`.
93+
///
94+
/// Note that doing so applies the configuration at the very end, so it will always override what came before it
95+
/// even though the `source` is of lower priority as what's there.
96+
pub fn append_config(
9497
&mut self,
9598
values: impl IntoIterator<Item = impl AsRef<BStr>>,
99+
source: git_config::Source,
96100
) -> Result<&mut Self, crate::config::overrides::Error> {
97-
crate::config::overrides::apply(&mut self.config, values, git_config::Source::Cli)?;
101+
crate::config::overrides::append(&mut self.config, values, source)?;
98102
Ok(self)
99103
}
100104
/// Apply all changes made to this instance.

git-repository/src/open.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ pub struct Options {
7575
pub(crate) lossy_config: Option<bool>,
7676
pub(crate) lenient_config: bool,
7777
pub(crate) bail_if_untrusted: bool,
78-
pub(crate) config_overrides: Vec<BString>,
78+
pub(crate) api_config_overrides: Vec<BString>,
79+
pub(crate) cli_config_overrides: Vec<BString>,
7980
/// Internal to pass an already obtained CWD on to where it may also be used. This avoids the CWD being queried more than once per repo.
8081
pub(crate) current_dir: Option<PathBuf>,
8182
}
@@ -91,7 +92,8 @@ impl Default for Options {
9192
lossy_config: None,
9293
lenient_config: true,
9394
bail_if_untrusted: false,
94-
config_overrides: Vec::new(),
95+
api_config_overrides: Vec::new(),
96+
cli_config_overrides: Vec::new(),
9597
current_dir: None,
9698
}
9799
}
@@ -138,7 +140,15 @@ impl Options {
138140
/// as the configuration is initialized to allow affecting the repository instantiation phase, both on disk or when opening.
139141
/// The configuration is marked with [source API][git_config::Source::Api].
140142
pub fn config_overrides(mut self, values: impl IntoIterator<Item = impl Into<BString>>) -> Self {
141-
self.config_overrides = values.into_iter().map(Into::into).collect();
143+
self.api_config_overrides = values.into_iter().map(Into::into).collect();
144+
self
145+
}
146+
147+
/// Set configuration values of the form `core.abbrev=5` or `remote.origin.url = foo` or `core.bool-implicit-true` for application
148+
/// as CLI overrides to the repository configuration, marked with [source CLI][git_config::Source::Cli].
149+
/// These are equivalent to CLI overrides passed with `-c` in `git`, for example.
150+
pub fn cli_overrides(mut self, values: impl IntoIterator<Item = impl Into<BString>>) -> Self {
151+
self.cli_config_overrides = values.into_iter().map(Into::into).collect();
142152
self
143153
}
144154

@@ -241,7 +251,8 @@ impl git_sec::trust::DefaultForLevel for Options {
241251
lossy_config: None,
242252
bail_if_untrusted: false,
243253
lenient_config: true,
244-
config_overrides: Vec::new(),
254+
api_config_overrides: Vec::new(),
255+
cli_config_overrides: Vec::new(),
245256
current_dir: None,
246257
},
247258
git_sec::Trust::Reduced => Options {
@@ -253,7 +264,8 @@ impl git_sec::trust::DefaultForLevel for Options {
253264
bail_if_untrusted: false,
254265
lenient_config: true,
255266
lossy_config: None,
256-
config_overrides: Vec::new(),
267+
api_config_overrides: Vec::new(),
268+
cli_config_overrides: Vec::new(),
257269
current_dir: None,
258270
},
259271
}
@@ -361,7 +373,8 @@ impl ThreadSafeRepository {
361373
lenient_config,
362374
bail_if_untrusted,
363375
permissions: Permissions { ref env, config },
364-
ref config_overrides,
376+
ref api_config_overrides,
377+
ref cli_config_overrides,
365378
ref current_dir,
366379
} = options;
367380
let current_dir = current_dir.as_deref().expect("BUG: current_dir must be set by caller");
@@ -402,7 +415,8 @@ impl ThreadSafeRepository {
402415
env.clone(),
403416
config,
404417
lenient_config,
405-
config_overrides,
418+
api_config_overrides,
419+
cli_config_overrides,
406420
)?;
407421

408422
if bail_if_untrusted && git_dir_trust != git_sec::Trust::Full {
@@ -571,7 +585,7 @@ mod tests {
571585
#[test]
572586
fn size_of_options() {
573587
let actual = std::mem::size_of::<Options>();
574-
let limit = 140;
588+
let limit = 160;
575589
assert!(
576590
actual <= limit,
577591
"{} <= {}: size shouldn't change without us knowing (on windows, it's bigger)",

git-repository/tests/repository/config/config_snapshot/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,15 @@ fn values_are_set_in_memory_only() {
8484
#[test]
8585
fn apply_cli_overrides() -> crate::Result {
8686
let mut repo = named_repo("make_config_repo.sh").unwrap();
87-
repo.config_snapshot_mut().apply_cli_overrides([
88-
"a.b=c",
89-
"remote.origin.url = url",
90-
"implicit.bool-true",
91-
"implicit.bool-false = ",
92-
])?;
87+
repo.config_snapshot_mut().append_config(
88+
[
89+
"a.b=c",
90+
"remote.origin.url = url",
91+
"implicit.bool-true",
92+
"implicit.bool-false = ",
93+
],
94+
git_config::Source::Cli,
95+
)?;
9396

9497
let config = repo.config_snapshot();
9598
assert_eq!(config.string("a.b").expect("present").as_ref(), "c");

0 commit comments

Comments
 (0)