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        |action, context| -> Result<_, Error> {
20            let url = context
21                .url
22                .clone()
23                .or_else(|| context.to_url())
24                .ok_or(Error::Protocol(gix::credentials::protocol::Error::UrlMissing))?;
25
26            let (mut cascade, _action, prompt_options) = match repo {
27                Some(ref repo) => repo
28                    .config_snapshot()
29                    .credential_helpers(gix::url::parse(url.as_ref())?)?,
30                None => {
31                    let config = gix::config::File::from_globals()?;
32                    let environment = gix::open::permissions::Environment::all();
33                    gix::config::credential_helpers(
34                        gix::url::parse(url.as_ref())?,
35                        &config,
36                        false,    /* lenient config */
37                        |_| true, /* section filter */
38                        environment,
39                        false, /* use http path (override, uses configuration now)*/
40                    )?
41                }
42            };
43            cascade
44                .invoke(
45                    match action {
46                        Get => gix::credentials::helper::Action::Get(context),
47                        Erase => gix::credentials::helper::Action::Erase(context.to_bstring()),
48                        Store => gix::credentials::helper::Action::Store(context.to_bstring()),
49                    },
50                    prompt_options,
51                )
52                .map(|outcome| outcome.and_then(|outcome| (&outcome.next).try_into().ok()))
53                .map_err(Into::into)
54        },
55    )
56    .map_err(Into::into)
57}