summaryrefslogtreecommitdiffstats
path: root/vendor/gix/src/remote/init.rs
blob: 13b747eda7cacaf9859be5495c3ee7ea27bc87e6 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use std::convert::TryInto;

use gix_refspec::RefSpec;

use crate::{config, remote, Remote, Repository};

mod error {
    use crate::bstr::BString;

    /// The error returned by [`Repository::remote_at(…)`][crate::Repository::remote_at()].
    #[derive(Debug, thiserror::Error)]
    #[allow(missing_docs)]
    pub enum Error {
        #[error(transparent)]
        Url(#[from] gix_url::parse::Error),
        #[error("The rewritten {kind} url {rewritten_url:?} failed to parse")]
        RewrittenUrlInvalid {
            kind: &'static str,
            rewritten_url: BString,
            source: gix_url::parse::Error,
        },
    }
}
pub use error::Error;

use crate::bstr::BString;

/// Initialization
impl<'repo> Remote<'repo> {
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn from_preparsed_config(
        name_or_url: Option<BString>,
        url: Option<gix_url::Url>,
        push_url: Option<gix_url::Url>,
        fetch_specs: Vec<RefSpec>,
        push_specs: Vec<RefSpec>,
        should_rewrite_urls: bool,
        fetch_tags: remote::fetch::Tags,
        repo: &'repo Repository,
    ) -> Result<Self, Error> {
        debug_assert!(
            url.is_some() || push_url.is_some(),
            "BUG: fetch or push url must be set at least"
        );
        let (url_alias, push_url_alias) = should_rewrite_urls
            .then(|| rewrite_urls(&repo.config, url.as_ref(), push_url.as_ref()))
            .unwrap_or(Ok((None, None)))?;
        Ok(Remote {
            name: name_or_url.map(Into::into),
            url,
            url_alias,
            push_url,
            push_url_alias,
            fetch_specs,
            push_specs,
            fetch_tags,
            repo,
        })
    }

    pub(crate) fn from_fetch_url<Url, E>(
        url: Url,
        should_rewrite_urls: bool,
        repo: &'repo Repository,
    ) -> Result<Self, Error>
    where
        Url: TryInto<gix_url::Url, Error = E>,
        gix_url::parse::Error: From<E>,
    {
        Self::from_fetch_url_inner(
            url.try_into().map_err(|err| Error::Url(err.into()))?,
            should_rewrite_urls,
            repo,
        )
    }

    fn from_fetch_url_inner(
        url: gix_url::Url,
        should_rewrite_urls: bool,
        repo: &'repo Repository,
    ) -> Result<Self, Error> {
        let (url_alias, _) = should_rewrite_urls
            .then(|| rewrite_urls(&repo.config, Some(&url), None))
            .unwrap_or(Ok((None, None)))?;
        Ok(Remote {
            name: None,
            url: Some(url),
            url_alias,
            push_url: None,
            push_url_alias: None,
            fetch_specs: Vec::new(),
            push_specs: Vec::new(),
            fetch_tags: Default::default(),
            repo,
        })
    }
}

pub(crate) fn rewrite_url(
    config: &config::Cache,
    url: Option<&gix_url::Url>,
    direction: remote::Direction,
) -> Result<Option<gix_url::Url>, Error> {
    url.and_then(|url| config.url_rewrite().longest(url, direction))
        .map(|url| {
            gix_url::parse(url.as_ref()).map_err(|err| Error::RewrittenUrlInvalid {
                kind: match direction {
                    remote::Direction::Fetch => "fetch",
                    remote::Direction::Push => "push",
                },
                source: err,
                rewritten_url: url,
            })
        })
        .transpose()
}

pub(crate) fn rewrite_urls(
    config: &config::Cache,
    url: Option<&gix_url::Url>,
    push_url: Option<&gix_url::Url>,
) -> Result<(Option<gix_url::Url>, Option<gix_url::Url>), Error> {
    let url_alias = rewrite_url(config, url, remote::Direction::Fetch)?;
    let push_url_alias = rewrite_url(config, push_url, remote::Direction::Push)?;

    Ok((url_alias, push_url_alias))
}