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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// This file is part of radicle-surf
// <https://github.com/radicle-dev/radicle-surf>
//
// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 or
// later as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::{
    convert::TryFrom,
    fmt,
    str::{self, FromStr},
};

use git_ext::ref_format::{
    self,
    refspec::{NamespacedPattern, PatternString, QualifiedPattern},
    Component, Namespaced, Qualified, RefStr, RefString,
};
use nonempty::NonEmpty;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    /// When parsing a namespace we may come across one that was an empty
    /// string.
    #[error("namespaces must not be empty")]
    EmptyNamespace,
    #[error(transparent)]
    RefFormat(#[from] ref_format::Error),
    #[error(transparent)]
    Utf8(#[from] str::Utf8Error),
}

/// A `Namespace` value allows us to switch the git namespace of
/// a repo.
///
/// A `Namespace` is one or more name components separated by `/`, e.g. `surf`,
/// `surf/git`.
///
/// For each `Namespace`, the reference name will add a single `refs/namespaces`
/// prefix, e.g. `refs/namespaces/surf`,
/// `refs/namespaces/surf/refs/namespaces/git`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Namespace {
    // XXX: we rely on RefString being non-empty here, which
    // git-ref-format ensures that there's no way to construct one.
    pub(super) namespaces: RefString,
}

impl Namespace {
    /// Take a `Qualified` reference name and convert it to a `Namespaced` using
    /// this `Namespace`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// let ns = "surf/git".parse::<Namespace>();
    /// let name = ns.to_namespaced(qualified!("refs/heads/main"));
    /// assert_eq!(
    ///     name.as_str(),
    ///     "refs/namespaces/surf/refs/namespaces/git/refs/heads/main"
    /// );
    /// ```
    pub(crate) fn to_namespaced<'a>(&self, name: &Qualified<'a>) -> Namespaced<'a> {
        let mut components = self.namespaces.components().rev();
        let mut namespaced = name.with_namespace(
            components
                .next()
                .expect("BUG: 'namespaces' cannot be empty"),
        );
        for ns in components {
            let qualified = namespaced.into_qualified();
            namespaced = qualified.with_namespace(ns);
        }
        namespaced
    }

    /// Take a `QualifiedPattern` reference name and convert it to a
    /// `NamespacedPattern` using this `Namespace`.
    ///
    /// # Example
    ///
    /// ```no_run
    /// let ns = "surf/git".parse::<Namespace>();
    /// let name = ns.to_namespaced(pattern!("refs/heads/*").to_qualified().unwrap());
    /// assert_eq!(
    ///     name.as_str(),
    ///     "refs/namespaces/surf/refs/namespaces/git/refs/heads/*"
    /// );
    /// ```
    pub(crate) fn to_namespaced_pattern<'a>(
        &self,
        pat: &QualifiedPattern<'a>,
    ) -> NamespacedPattern<'a> {
        let pattern = PatternString::from(self.namespaces.clone());
        let mut components = pattern.components().rev();
        let mut namespaced = pat
            .with_namespace(
                components
                    .next()
                    .expect("BUG: 'namespaces' cannot be empty"),
            )
            .expect("BUG: 'namespace' cannot have globs");
        for ns in components {
            let qualified = namespaced.into_qualified();
            namespaced = qualified
                .with_namespace(ns)
                .expect("BUG: 'namespaces' cannot have globs");
        }
        namespaced
    }
}

impl fmt::Display for Namespace {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.namespaces)
    }
}

impl<'a> From<NonEmpty<Component<'a>>> for Namespace {
    fn from(cs: NonEmpty<Component<'a>>) -> Self {
        Self {
            namespaces: cs.into_iter().collect::<RefString>(),
        }
    }
}

impl TryFrom<&str> for Namespace {
    type Error = Error;

    fn try_from(name: &str) -> Result<Self, Self::Error> {
        Self::from_str(name)
    }
}

impl TryFrom<&[u8]> for Namespace {
    type Error = Error;

    fn try_from(namespace: &[u8]) -> Result<Self, Self::Error> {
        str::from_utf8(namespace)
            .map_err(Error::from)
            .and_then(Self::from_str)
    }
}

impl FromStr for Namespace {
    type Err = Error;

    fn from_str(name: &str) -> Result<Self, Self::Err> {
        let namespaces = RefStr::try_from_str(name)?.to_ref_string();
        Ok(Self { namespaces })
    }
}

impl From<Namespaced<'_>> for Namespace {
    fn from(namespaced: Namespaced<'_>) -> Self {
        let mut namespaces = namespaced.namespace().to_ref_string();
        let mut qualified = namespaced.strip_namespace();
        while let Some(namespaced) = qualified.to_namespaced() {
            namespaces.push(namespaced.namespace());
            qualified = namespaced.strip_namespace();
        }
        Self { namespaces }
    }
}

impl TryFrom<&git2::Reference<'_>> for Namespace {
    type Error = Error;

    fn try_from(reference: &git2::Reference) -> Result<Self, Self::Error> {
        let name = RefStr::try_from_str(str::from_utf8(reference.name_bytes())?)?;
        name.to_namespaced()
            .ok_or(Error::EmptyNamespace)
            .map(Self::from)
    }
}