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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use std::{
    convert::TryFrom,
    str::{self, FromStr},
};

use git_ext::ref_format::{component, lit, Component, Qualified, RefStr, RefString};

use crate::refs::refstr_join;

/// A `Branch` represents any git branch. It can be [`Local`] or [`Remote`].
///
/// Note that if a `Branch` is created from a [`git2::Reference`] then
/// any `refs/namespaces` will be stripped.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Branch {
    Local(Local),
    Remote(Remote),
}

impl Branch {
    /// Construct a [`Local`] branch.
    pub fn local<R>(name: R) -> Self
    where
        R: AsRef<RefStr>,
    {
        Self::Local(Local::new(name))
    }

    /// Construct a [`Remote`] branch.
    /// The `remote` is the remote name of the reference name while
    /// the `name` is the suffix, i.e. `refs/remotes/<remote>/<name>`.
    pub fn remote<R>(remote: Component<'_>, name: R) -> Self
    where
        R: AsRef<RefStr>,
    {
        Self::Remote(Remote::new(remote, name))
    }

    /// Return the short `Branch` refname,
    /// e.g. `fix/ref-format`.
    pub fn short_name(&self) -> &RefString {
        match self {
            Branch::Local(local) => local.short_name(),
            Branch::Remote(remote) => remote.short_name(),
        }
    }

    /// Give back the fully qualified `Branch` refname,
    /// e.g. `refs/remotes/origin/fix/ref-format`,
    /// `refs/heads/fix/ref-format`.
    pub fn refname(&self) -> Qualified {
        match self {
            Branch::Local(local) => local.refname(),
            Branch::Remote(remote) => remote.refname(),
        }
    }
}

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

    fn try_from(reference: &git2::Reference<'_>) -> Result<Self, Self::Error> {
        let name = str::from_utf8(reference.name_bytes())?;
        Self::from_str(name)
    }
}

impl TryFrom<&str> for Branch {
    type Error = error::Branch;

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

impl FromStr for Branch {
    type Err = error::Branch;

    fn from_str(name: &str) -> Result<Self, Self::Err> {
        let name = RefStr::try_from_str(name)?;
        let name = match name.to_namespaced() {
            None => name
                .qualified()
                .ok_or_else(|| error::Branch::NotQualified(name.to_string()))?,
            Some(name) => name.strip_namespace_recursive(),
        };

        let (_ref, category, c, cs) = name.non_empty_components();

        if category == component::HEADS {
            Ok(Self::Local(Local::new(refstr_join(c, cs))))
        } else if category == component::REMOTES {
            Ok(Self::Remote(Remote::new(c, cs.collect::<RefString>())))
        } else {
            Err(error::Branch::InvalidName(name.into()))
        }
    }
}

/// A `Local` represents a local branch, i.e. it is a reference under
/// `refs/heads`.
///
/// Note that if a `Local` is created from a [`git2::Reference`] then
/// any `refs/namespaces` will be stripped.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Local {
    name: RefString,
}

impl Local {
    /// Construct a new `Local` with the given `name`.
    ///
    /// If the name is qualified with `refs/heads`, this will be
    /// shortened to the suffix. To get the `Qualified` name again,
    /// use [`Local::refname`].
    pub(crate) fn new<R>(name: R) -> Self
    where
        R: AsRef<RefStr>,
    {
        match name.as_ref().qualified() {
            None => Self {
                name: name.as_ref().to_ref_string(),
            },
            Some(qualified) => {
                let (_refs, heads, c, cs) = qualified.non_empty_components();
                if heads == component::HEADS {
                    Self {
                        name: refstr_join(c, cs),
                    }
                } else {
                    Self {
                        name: name.as_ref().to_ref_string(),
                    }
                }
            }
        }
    }

    /// Return the short `Local` refname,
    /// e.g. `fix/ref-format`.
    pub fn short_name(&self) -> &RefString {
        &self.name
    }

    /// Return the fully qualified `Local` refname,
    /// e.g. `refs/heads/fix/ref-format`.
    pub fn refname(&self) -> Qualified {
        lit::refs_heads(&self.name).into()
    }
}

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

    fn try_from(reference: &git2::Reference) -> Result<Self, Self::Error> {
        let name = str::from_utf8(reference.name_bytes())?;
        Self::from_str(name)
    }
}

impl TryFrom<&str> for Local {
    type Error = error::Local;

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

impl FromStr for Local {
    type Err = error::Local;

    fn from_str(name: &str) -> Result<Self, Self::Err> {
        let name = RefStr::try_from_str(name)?;
        let name = match name.to_namespaced() {
            None => name
                .qualified()
                .ok_or_else(|| error::Local::NotQualified(name.to_string()))?,
            Some(name) => name.strip_namespace_recursive(),
        };

        let (_ref, heads, c, cs) = name.non_empty_components();
        if heads == component::HEADS {
            Ok(Self::new(refstr_join(c, cs)))
        } else {
            Err(error::Local::NotHeads(name.into()))
        }
    }
}

/// A `Remote` represents a remote branch, i.e. it is a reference under
/// `refs/remotes`.
///
/// Note that if a `Remote` is created from a [`git2::Reference`] then
/// any `refs/namespaces` will be stripped.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Remote {
    remote: RefString,
    name: RefString,
}

impl Remote {
    /// Construct a new `Remote` with the given `name` and `remote`.
    ///
    /// ## Note
    /// `name` is expected to be in short form, i.e. not begin with
    /// `refs`.
    ///
    /// If you are creating a `Remote` with a name that begins with
    /// `refs/remotes`, use [`Remote::from_refs_remotes`] instead.
    ///
    /// To get the `Qualified` name, use [`Remote::refname`].
    pub(crate) fn new<R>(remote: Component, name: R) -> Self
    where
        R: AsRef<RefStr>,
    {
        Self {
            name: name.as_ref().to_ref_string(),
            remote: remote.to_ref_string(),
        }
    }

    /// Parse the `name` from the form `refs/remotes/<remote>/<rest>`.
    ///
    /// If the `name` is not of this form, then `None` is returned.
    pub fn from_refs_remotes<R>(name: R) -> Option<Self>
    where
        R: AsRef<RefStr>,
    {
        let qualified = name.as_ref().qualified()?;
        let (_refs, remotes, remote, cs) = qualified.non_empty_components();
        (remotes == component::REMOTES).then_some(Self {
            name: cs.collect(),
            remote: remote.to_ref_string(),
        })
    }

    /// Return the short `Remote` refname,
    /// e.g. `fix/ref-format`.
    pub fn short_name(&self) -> &RefString {
        &self.name
    }

    /// Return the remote of the `Remote`'s refname,
    /// e.g. `origin`.
    pub fn remote(&self) -> &RefString {
        &self.remote
    }

    /// Give back the fully qualified `Remote` refname,
    /// e.g. `refs/remotes/origin/fix/ref-format`.
    pub fn refname(&self) -> Qualified {
        lit::refs_remotes(self.remote.join(&self.name)).into()
    }
}

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

    fn try_from(reference: &git2::Reference) -> Result<Self, Self::Error> {
        let name = str::from_utf8(reference.name_bytes())?;
        Self::from_str(name)
    }
}

impl TryFrom<&str> for Remote {
    type Error = error::Remote;

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

impl FromStr for Remote {
    type Err = error::Remote;

    fn from_str(name: &str) -> Result<Self, Self::Err> {
        let name = RefStr::try_from_str(name)?;
        let name = match name.to_namespaced() {
            None => name
                .qualified()
                .ok_or_else(|| error::Remote::NotQualified(name.to_string()))?,
            Some(name) => name.strip_namespace_recursive(),
        };

        let (_ref, remotes, remote, cs) = name.non_empty_components();
        if remotes == component::REMOTES {
            Ok(Self::new(remote, cs.collect::<RefString>()))
        } else {
            Err(error::Remote::NotRemotes(name.into()))
        }
    }
}

pub mod error {
    use radicle_git_ext::ref_format::{self, RefString};
    use thiserror::Error;

    #[derive(Debug, Error)]
    pub enum Branch {
        #[error("the refname '{0}' did not begin with 'refs/heads' or 'refs/remotes'")]
        InvalidName(RefString),
        #[error("the refname '{0}' did not begin with 'refs/heads' or 'refs/remotes'")]
        NotQualified(String),
        #[error(transparent)]
        RefFormat(#[from] ref_format::Error),
        #[error(transparent)]
        Utf8(#[from] std::str::Utf8Error),
    }

    #[derive(Debug, Error)]
    pub enum Local {
        #[error("the refname '{0}' did not begin with 'refs/heads'")]
        NotHeads(RefString),
        #[error("the refname '{0}' did not begin with 'refs/heads'")]
        NotQualified(String),
        #[error(transparent)]
        RefFormat(#[from] ref_format::Error),
        #[error(transparent)]
        Utf8(#[from] std::str::Utf8Error),
    }

    #[derive(Debug, Error)]
    pub enum Remote {
        #[error("the refname '{0}' did not begin with 'refs/remotes'")]
        NotQualified(String),
        #[error("the refname '{0}' did not begin with 'refs/remotes'")]
        NotRemotes(RefString),
        #[error(transparent)]
        RefFormat(#[from] ref_format::Error),
        #[error(transparent)]
        Utf8(#[from] std::str::Utf8Error),
    }
}