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