Skip to content

Commit

Permalink
feat: add the gitoxide.credentials.terminalPrompt key to represent …
Browse files Browse the repository at this point in the history
…the GIT_TERMINAL_PROMPT (GitoxideLabs#1090)

That way, it's easy to control the usage of terminals without using and environment.
  • Loading branch information
Byron committed Nov 7, 2023
1 parent e47c46d commit e95bb9f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
9 changes: 9 additions & 0 deletions gix/src/config/cache/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,15 @@ fn apply_environment_overrides(
},
],
),
(
"gitoxide",
Some(Cow::Borrowed("credentials".into())),
git_prefix,
&[{
let key = &gitoxide::Credentials::TERMINAL_PROMPT;
(env(key), key.name)
}],
),
(
"gitoxide",
Some(Cow::Borrowed("committer".into())),
Expand Down
8 changes: 6 additions & 2 deletions gix/src/config/snapshot/credential_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{borrow::Cow, convert::TryFrom};
pub use error::Error;

use crate::config::cache::util::IgnoreEmptyPath;
use crate::config::tree::gitoxide::Credentials;
use crate::{
bstr::{ByteSlice, ByteVec},
config::{
Expand Down Expand Up @@ -144,9 +145,12 @@ impl Snapshot<'_> {
.transpose()
.ignore_empty()?
.map(|c| Cow::Owned(c.into_owned())),
..Default::default()
mode: self
.boolean(Credentials::TERMINAL_PROMPT.logical_name().as_str())
.and_then(|val| (!val).then_some(gix_prompt::Mode::Disable))
.unwrap_or_default(),
}
.apply_environment(allow_git_env, allow_ssh_env, allow_git_env);
.apply_environment(allow_git_env, allow_ssh_env, false /* terminal prompt */);
Ok((
gix_credentials::helper::Cascade {
programs,
Expand Down
28 changes: 27 additions & 1 deletion gix/src/config/tree/sections/gitoxide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ impl Gitoxide {
pub const COMMIT: Commit = Commit;
/// The `gitoxide.committer` section.
pub const COMMITTER: Committer = Committer;
/// The `gitoxide.credentials` section.
pub const CREDENTIALS: Credentials = Credentials;
/// The `gitoxide.http` section.
pub const HTTP: Http = Http;
/// The `gitoxide.https` section.
Expand Down Expand Up @@ -427,6 +429,30 @@ mod subsections {
}
}

/// The `credentials` sub-section.
#[derive(Copy, Clone, Default)]
pub struct Credentials;
impl Credentials {
/// The `gitoxide.credentials.terminalPrompt` key.
pub const TERMINAL_PROMPT: keys::Boolean = keys::Boolean::new_boolean("terminalPrompt", &Gitoxide::CREDENTIALS)
.with_note("This is a custom addition to provide an alternative to the respective environment variable.")
.with_environment_override("GIT_TERMINAL_PROMPT");
}

impl Section for Credentials {
fn name(&self) -> &str {
"credentials"
}

fn keys(&self) -> &[&dyn Key] {
&[&Self::TERMINAL_PROMPT]
}

fn parent(&self) -> Option<&dyn Section> {
Some(&Tree::GITOXIDE)
}
}

/// The `commit` sub-section.
#[derive(Copy, Clone, Default)]
pub struct Commit;
Expand Down Expand Up @@ -454,7 +480,7 @@ mod subsections {
}
}
}
pub use subsections::{Allow, Author, Commit, Committer, Core, Http, Https, Objects, Pathspec, Ssh, User};
pub use subsections::{Allow, Author, Commit, Committer, Core, Credentials, Http, Https, Objects, Pathspec, Ssh, User};

pub mod validate {
use std::error::Error;
Expand Down
2 changes: 2 additions & 0 deletions gix/tests/gix-init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ mod with_overrides {
.set("GIT_GLOB_PATHSPECS", "pathspecs-glob")
.set("GIT_NOGLOB_PATHSPECS", "pathspecs-noglob")
.set("GIT_ICASE_PATHSPECS", "pathspecs-icase")
.set("GIT_TERMINAL_PROMPT", "42")
.set("GIT_SHALLOW_FILE", "shallow-file-env");
let mut opts = gix::open::Options::isolated()
.cli_overrides([
Expand Down Expand Up @@ -234,6 +235,7 @@ mod with_overrides {
("gitoxide.pathspec.glob", "pathspecs-glob"),
("gitoxide.pathspec.noglob", "pathspecs-noglob"),
("gitoxide.pathspec.literal", "pathspecs-literal"),
("gitoxide.credentials.terminalPrompt", "42"),
] {
assert_eq!(
config
Expand Down

0 comments on commit e95bb9f

Please sign in to comment.