git_prole/git/refs/
branch.rs

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
use std::fmt::Display;
use std::ops::Deref;
use std::str::FromStr;

use miette::miette;

use super::LocalBranchRef;
use super::Ref;
use super::RemoteBranchRef;

/// A Git reference to a remote branch.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum BranchRef {
    /// A local branch.
    Local(LocalBranchRef),
    /// A remote-tracking branch.
    Remote(RemoteBranchRef),
}

impl BranchRef {
    /// Get the qualified name of this branch.
    pub fn qualified_branch_name(&self) -> &str {
        match &self {
            BranchRef::Local(ref_name) => ref_name.branch_name(),
            BranchRef::Remote(ref_name) => ref_name.name(),
        }
    }

    /// Get the name of this branch.
    pub fn branch_name(&self) -> &str {
        match &self {
            BranchRef::Local(ref_name) => ref_name.branch_name(),
            BranchRef::Remote(ref_name) => ref_name.branch_name(),
        }
    }

    pub fn as_local(&self) -> LocalBranchRef {
        match self {
            BranchRef::Local(local) => local.clone(),
            BranchRef::Remote(remote) => remote.as_local(),
        }
    }
}

impl PartialEq<Ref> for BranchRef {
    fn eq(&self, other: &Ref) -> bool {
        self.deref().eq(other)
    }
}

impl Display for BranchRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BranchRef::Local(ref_name) => Display::fmt(ref_name, f),
            BranchRef::Remote(ref_name) => Display::fmt(ref_name, f),
        }
    }
}

impl Deref for BranchRef {
    type Target = Ref;

    fn deref(&self) -> &Self::Target {
        match self {
            BranchRef::Local(ref_name) => ref_name.deref(),
            BranchRef::Remote(ref_name) => ref_name.deref(),
        }
    }
}

impl TryFrom<Ref> for BranchRef {
    type Error = miette::Report;

    fn try_from(value: Ref) -> Result<Self, Self::Error> {
        match value.kind() {
            Ref::HEADS => Ok(Self::Local(LocalBranchRef::try_from(value)?)),
            Ref::REMOTES => Ok(Self::Remote(RemoteBranchRef::try_from(value)?)),
            _ => Err(miette!("Ref is not a local or remote branch: {value}")),
        }
    }
}

impl FromStr for BranchRef {
    type Err = miette::Report;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ref::from_str(s)?.try_into()
    }
}

impl From<LocalBranchRef> for BranchRef {
    fn from(value: LocalBranchRef) -> Self {
        Self::Local(value)
    }
}

impl From<RemoteBranchRef> for BranchRef {
    fn from(value: RemoteBranchRef) -> Self {
        Self::Remote(value)
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn test_branch_ref_try_from() {
        let branch = BranchRef::try_from(Ref::from_str("refs/heads/puppy/doggy").unwrap()).unwrap();

        assert_eq!(branch.branch_name(), "puppy/doggy",);
        assert_eq!(branch.qualified_branch_name(), "puppy/doggy",);

        let branch =
            BranchRef::try_from(Ref::from_str("refs/remotes/puppy/doggy").unwrap()).unwrap();

        assert_eq!(branch.branch_name(), "doggy",);
        assert_eq!(branch.qualified_branch_name(), "puppy/doggy",);

        assert!(BranchRef::try_from(Ref::from_str("refs/tags/v1.0.0").unwrap()).is_err());
    }

    #[test]
    fn test_branch_qualified_branch_name() {
        assert_eq!(
            BranchRef::Remote(RemoteBranchRef::new("origin", "puppy")).qualified_branch_name(),
            "origin/puppy",
        );

        assert_eq!(
            BranchRef::Local(LocalBranchRef::new("puppy".into())).qualified_branch_name(),
            "puppy",
        );
    }

    #[test]
    fn test_branch_branch_name() {
        assert_eq!(
            BranchRef::Remote(RemoteBranchRef::new("origin", "puppy")).branch_name(),
            "puppy",
        );

        assert_eq!(
            BranchRef::Local(LocalBranchRef::new("puppy".into())).branch_name(),
            "puppy",
        );
    }
}