History

Support for -c CLI config overrides in `gix config`. (#450)

It's special as it creates its own repository.

Sebastian Thiel committed 4 years ago

Commit 19c17461 parent 5d22e293 files · +28 −19Browse files

Unified · Split

3 files
  1. git-repository/src/config/snapshot.rs +4 −4
  2. gitoxide-core/src/repository/config.rs +4 −1
  3. src/plumbing/main.rs +20 −14
git-repository/src/config/snapshot.rs+4−4
@@ -83,7 +83,7 @@ impl<'repo> Snapshot<'repo> { · 82 unmodified lines
83 83
84///84///
85pub mod apply_cli_overrides {85pub mod apply_cli_overrides {
86 use crate::bstr::{BString, ByteSlice};86 use crate::bstr::{BStr, BString, ByteSlice};
87 use crate::config::SnapshotMut;87 use crate::config::SnapshotMut;
88 use std::convert::TryFrom;88 use std::convert::TryFrom;
89 89
@@ -105,13 +105,13 @@ pub mod apply_cli_overrides { · 15 unmodified lines
105 impl SnapshotMut<'_> {105 impl SnapshotMut<'_> {
106 /// Apply configuration values of the form `core.abbrev=5` or `remote.origin.url = foo` or `core.bool-implicit-true`106 /// Apply configuration values of the form `core.abbrev=5` or `remote.origin.url = foo` or `core.bool-implicit-true`
107 /// to the repository configuration, marked with [source CLI][git_config::Source::Cli].107 /// to the repository configuration, marked with [source CLI][git_config::Source::Cli].
108 pub fn apply_cli_overrides(108 pub fn apply_cli_overrides<'a>(
109 &mut self,109 &mut self,
110 values: impl IntoIterator<Item = impl Into<BString>>,110 values: impl IntoIterator<Item = impl AsRef<BStr>>,
111 ) -> Result<(), Error> {111 ) -> Result<(), Error> {
112 let mut file = git_config::File::new(git_config::file::Metadata::from(git_config::Source::Cli));112 let mut file = git_config::File::new(git_config::file::Metadata::from(git_config::Source::Cli));
113 for key_value in values {113 for key_value in values {
114 let key_value = key_value.into();114 let key_value = key_value.as_ref();
115 let mut tokens = key_value.splitn(2, |b| *b == b'=').map(|v| v.trim());115 let mut tokens = key_value.splitn(2, |b| *b == b'=').map(|v| v.trim());
116 let key = tokens.next().expect("always one value").as_bstr();116 let key = tokens.next().expect("always one value").as_bstr();
117 let value = tokens.next();117 let value = tokens.next();
gitoxide-core/src/repository/config.rs+4−1
@@ -1,18 +1,21 @@
1use anyhow::{bail, Result};1use anyhow::{bail, Result};
2use git_repository as git;2use git_repository as git;
3use git_repository::bstr::BString;
3 4
4use crate::OutputFormat;5use crate::OutputFormat;
5 6
6pub fn list(7pub fn list(
7 repo: git::Repository,8 repo: git::Repository,
8 filters: Vec<String>,9 filters: Vec<String>,
10 overrides: Vec<BString>,
9 format: OutputFormat,11 format: OutputFormat,
10 mut out: impl std::io::Write,12 mut out: impl std::io::Write,
11) -> Result<()> {13) -> Result<()> {
12 if format != OutputFormat::Human {14 if format != OutputFormat::Human {
13 bail!("Only human output format is supported at the moment");15 bail!("Only human output format is supported at the moment");
14 }16 }
15 let repo = git::open_opts(repo.git_dir(), repo.open_options().clone().lossy_config(false))?;17 let mut repo = git::open_opts(repo.git_dir(), repo.open_options().clone().lossy_config(false))?;
18 repo.config_snapshot_mut().apply_cli_overrides(overrides.into_iter())?;
16 let config = repo.config_snapshot();19 let config = repo.config_snapshot();
17 let config = config.plumbing();20 let config = config.plumbing();
18 if let Some(frontmatter) = config.frontmatter() {21 if let Some(frontmatter) = config.frontmatter() {
src/plumbing/main.rs+20−14
@@ -62,20 +62,24 @@ pub fn main() -> Result<()> { · 61 unmodified lines
62 Strict,62 Strict,
63 Lenient,63 Lenient,
64 }64 }
65 let repository = move |mode: Mode| -> Result<git::Repository> {65
66 let mut mapping: git::sec::trust::Mapping<git::open::Options> = Default::default();66 let repository = {
67 let toggle = matches!(mode, Mode::Strict);67 let config = config.clone();
68 mapping.full = mapping.full.strict_config(toggle);68 move |mode: Mode| -> Result<git::Repository> {
69 mapping.reduced = mapping.reduced.strict_config(toggle);69 let mut mapping: git::sec::trust::Mapping<git::open::Options> = Default::default();
70 let mut repo = git::ThreadSafeRepository::discover_opts(repository, Default::default(), mapping)70 let toggle = matches!(mode, Mode::Strict);
71 .map(git::Repository::from)71 mapping.full = mapping.full.strict_config(toggle);
72 .map(|r| r.apply_environment())?;72 mapping.reduced = mapping.reduced.strict_config(toggle);
73 if !config.is_empty() {73 let mut repo = git::ThreadSafeRepository::discover_opts(repository, Default::default(), mapping)
74 repo.config_snapshot_mut()74 .map(git::Repository::from)
75 .apply_cli_overrides(config)75 .map(|r| r.apply_environment())?;
76 .context("Unable to parse command-line configuration")?;76 if !config.is_empty() {
77 repo.config_snapshot_mut()
78 .apply_cli_overrides(config.iter())
79 .context("Unable to parse command-line configuration")?;
80 }
81 Ok(repo)
77 }82 }
78 Ok(repo)
79 };83 };
80 84
81 let progress;85 let progress;
@@ -142,7 +146,9 @@ pub fn main() -> Result<()> { · 60 unmodified lines
142 progress,146 progress,
143 progress_keep_open,147 progress_keep_open,
144 None,148 None,
145 move |_progress, out, _err| core::repository::config::list(repository(Mode::Lenient)?, filter, format, out),149 move |_progress, out, _err| {
150 core::repository::config::list(repository(Mode::Lenient)?, filter, config, format, out)
151 },
146 )152 )
147 .map(|_| ()),153 .map(|_| ()),
148 Subcommands::Free(subcommands) => match subcommands {154 Subcommands::Free(subcommands) => match subcommands {

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t