1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::{borrow::Cow, convert::TryFrom, path::Path};
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Terminal prompts are disabled")]
Disabled,
#[error("The current platform has no implementation for prompting in the terminal")]
UnsupportedPlatform,
#[error(
"Failed to open terminal at {:?} for writing prompt, or to write it",
crate::unix::TTY_PATH
)]
TtyIo(#[from] std::io::Error),
#[cfg(unix)]
#[error("Failed to obtain or set terminal configuration")]
TerminalConfiguration(#[from] nix::errno::Errno),
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Mode {
Visible,
Hidden,
Disable,
}
impl Default for Mode {
fn default() -> Self {
Mode::Hidden
}
}
#[derive(Default, Clone)]
pub struct Options<'a> {
pub askpass: Option<Cow<'a, Path>>,
pub mode: Mode,
}
impl Options<'_> {
pub fn apply_environment(
mut self,
use_git_askpass: bool,
use_ssh_askpass: bool,
use_git_terminal_prompt: bool,
) -> Self {
if let Some(askpass) = use_git_askpass.then(|| std::env::var_os("GIT_ASKPASS")).flatten() {
self.askpass = Some(Cow::Owned(askpass.into()))
}
if self.askpass.is_none() {
if let Some(askpass) = use_ssh_askpass.then(|| std::env::var_os("SSH_ASKPASS")).flatten() {
self.askpass = Some(Cow::Owned(askpass.into()))
}
}
self.mode = use_git_terminal_prompt
.then(|| {
std::env::var_os("GIT_TERMINAL_PROMPT")
.and_then(|val| git_config_value::Boolean::try_from(val).ok())
.and_then(|allow| (!allow.0).then(|| Mode::Disable))
})
.flatten()
.unwrap_or(self.mode);
self
}
}