Skip to content

Commit

Permalink
Support for -c CLI config overrides in gix config. (#450)
Browse files Browse the repository at this point in the history
It's special as it creates its own repository.
  • Loading branch information
Byron committed Aug 22, 2022
1 parent 5d22e29 commit 19c1746
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
8 changes: 4 additions & 4 deletions git-repository/src/config/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'repo> Snapshot<'repo> {

///
pub mod apply_cli_overrides {
use crate::bstr::{BString, ByteSlice};
use crate::bstr::{BStr, BString, ByteSlice};
use crate::config::SnapshotMut;
use std::convert::TryFrom;

Expand All @@ -105,13 +105,13 @@ pub mod apply_cli_overrides {
impl SnapshotMut<'_> {
/// Apply configuration values of the form `core.abbrev=5` or `remote.origin.url = foo` or `core.bool-implicit-true`
/// to the repository configuration, marked with [source CLI][git_config::Source::Cli].
pub fn apply_cli_overrides(
pub fn apply_cli_overrides<'a>(
&mut self,
values: impl IntoIterator<Item = impl Into<BString>>,
values: impl IntoIterator<Item = impl AsRef<BStr>>,
) -> Result<(), Error> {
let mut file = git_config::File::new(git_config::file::Metadata::from(git_config::Source::Cli));
for key_value in values {
let key_value = key_value.into();
let key_value = key_value.as_ref();
let mut tokens = key_value.splitn(2, |b| *b == b'=').map(|v| v.trim());
let key = tokens.next().expect("always one value").as_bstr();
let value = tokens.next();
Expand Down
5 changes: 4 additions & 1 deletion gitoxide-core/src/repository/config.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use anyhow::{bail, Result};
use git_repository as git;
use git_repository::bstr::BString;

use crate::OutputFormat;

pub fn list(
repo: git::Repository,
filters: Vec<String>,
overrides: Vec<BString>,
format: OutputFormat,
mut out: impl std::io::Write,
) -> Result<()> {
if format != OutputFormat::Human {
bail!("Only human output format is supported at the moment");
}
let repo = git::open_opts(repo.git_dir(), repo.open_options().clone().lossy_config(false))?;
let mut repo = git::open_opts(repo.git_dir(), repo.open_options().clone().lossy_config(false))?;
repo.config_snapshot_mut().apply_cli_overrides(overrides.into_iter())?;
let config = repo.config_snapshot();
let config = config.plumbing();
if let Some(frontmatter) = config.frontmatter() {
Expand Down
34 changes: 20 additions & 14 deletions src/plumbing/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,24 @@ pub fn main() -> Result<()> {
Strict,
Lenient,
}
let repository = move |mode: Mode| -> Result<git::Repository> {
let mut mapping: git::sec::trust::Mapping<git::open::Options> = Default::default();
let toggle = matches!(mode, Mode::Strict);
mapping.full = mapping.full.strict_config(toggle);
mapping.reduced = mapping.reduced.strict_config(toggle);
let mut repo = git::ThreadSafeRepository::discover_opts(repository, Default::default(), mapping)
.map(git::Repository::from)
.map(|r| r.apply_environment())?;
if !config.is_empty() {
repo.config_snapshot_mut()
.apply_cli_overrides(config)
.context("Unable to parse command-line configuration")?;

let repository = {
let config = config.clone();
move |mode: Mode| -> Result<git::Repository> {
let mut mapping: git::sec::trust::Mapping<git::open::Options> = Default::default();
let toggle = matches!(mode, Mode::Strict);
mapping.full = mapping.full.strict_config(toggle);
mapping.reduced = mapping.reduced.strict_config(toggle);
let mut repo = git::ThreadSafeRepository::discover_opts(repository, Default::default(), mapping)
.map(git::Repository::from)
.map(|r| r.apply_environment())?;
if !config.is_empty() {
repo.config_snapshot_mut()
.apply_cli_overrides(config.iter())
.context("Unable to parse command-line configuration")?;
}
Ok(repo)
}
Ok(repo)
};

let progress;
Expand Down Expand Up @@ -142,7 +146,9 @@ pub fn main() -> Result<()> {
progress,
progress_keep_open,
None,
move |_progress, out, _err| core::repository::config::list(repository(Mode::Lenient)?, filter, format, out),
move |_progress, out, _err| {
core::repository::config::list(repository(Mode::Lenient)?, filter, config, format, out)
},
)
.map(|_| ()),
Subcommands::Free(subcommands) => match subcommands {
Expand Down

0 comments on commit 19c1746

Please sign in to comment.