Skip to main content

gitoxide_core/repository/
credential.rs

1#[derive(Debug, thiserror::Error)]
2enum Error {
3    #[error(transparent)]
4    UrlParse(#[from] gix::url::parse::Error),
5    #[error(transparent)]
6    Configuration(#[from] gix::config::credential_helpers::Error),
7    #[error(transparent)]
8    Protocol(#[from] gix::credentials::protocol::Error),
9    #[error(transparent)]
10    ConfigLoad(#[from] gix::config::file::init::from_paths::Error),
11}
12
13pub fn function(repo: Option<gix::Repository>, action: gix::credentials::program::main::Action) -> anyhow::Result<()> {
14    use gix::credentials::program::main::Action::*;
15    gix::credentials::program::main(
16        Some(action.as_str().into()),
17        std::io::stdin(),
18        std::io::stdout(),
19        gix::credentials::protocol::ContextOptions::default(),
20        |action, context| -> Result<_, Error> {
21            let url = context
22                .url
23                .clone()
24                .or_else(|| context.to_url())
25                .ok_or(Error::Protocol(gix::credentials::protocol::Error::UrlMissing))?;
26
27            let (mut cascade, _action, prompt_options) = match repo {
28                Some(ref repo) => repo.config_snapshot().credential_helpers(gix::url::parse(&url)?)?,
29                None => {
30                    let config = gix::config::File::from_globals()?;
31                    let environment = gix::open::permissions::Environment::all();
32                    gix::config::credential_helpers(
33                        gix::url::parse(&url)?,
34                        &config,
35                        false,    /* lenient config */
36                        |_| true, /* section filter */
37                        environment,
38                        false, /* use http path (override, uses configuration now)*/
39                    )?
40                }
41            };
42            cascade
43                .invoke(
44                    match action {
45                        Get => gix::credentials::helper::Action::Get(context),
46                        Erase => gix::credentials::helper::Action::Erase(context.to_bstring()),
47                        Store => gix::credentials::helper::Action::Store(context.to_bstring()),
48                    },
49                    prompt_options,
50                )
51                .map(|outcome| outcome.and_then(|outcome| (&outcome.next).try_into().ok()))
52                .map_err(Into::into)
53        },
54    )
55    .map_err(Into::into)
56}