git_prole/git/refs/
remote_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::Debug;
use std::fmt::Display;
use std::ops::Deref;

use miette::miette;

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

/// A Git reference to a remote branch.
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct RemoteBranchRef(Ref);

impl Debug for RemoteBranchRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(&self.0, f)
    }
}

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

impl RemoteBranchRef {
    pub fn new(remote: &str, name: &str) -> Self {
        Self(Ref::new(
            Ref::REMOTES.to_owned(),
            format!("{remote}/{name}"),
        ))
    }

    /// Get the qualified name of this branch, including the remote name.
    pub fn qualified_branch_name(&self) -> &str {
        self.name()
    }

    /// Get the name of this remote and branch.
    pub fn remote_and_branch(&self) -> (&str, &str) {
        self.0
            .name()
            .split_once('/')
            .expect("A remote branch always has a remote and a branch")
    }

    /// Get the name of this remote.
    pub fn remote(&self) -> &str {
        self.remote_and_branch().0
    }

    /// Get the name of this branch.
    pub fn branch_name(&self) -> &str {
        self.remote_and_branch().1
    }

    /// Get a local branch with the same name.
    pub fn as_local(&self) -> LocalBranchRef {
        LocalBranchRef::new(self.branch_name().to_owned())
    }
}

impl Deref for RemoteBranchRef {
    type Target = Ref;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

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

    fn try_from(value: Ref) -> Result<Self, Self::Error> {
        if value.is_remote_branch() {
            Ok(Self(value))
        } else {
            Err(miette!("Ref is not a remote branch: {value}"))
        }
    }
}

impl Display for RemoteBranchRef {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.0, f)
    }
}

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

    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn remote_branch_ref_try_from() {
        let branch =
            RemoteBranchRef::try_from(Ref::from_str("refs/remotes/puppy/doggy").unwrap()).unwrap();

        assert_eq!(branch.remote(), "puppy");
        assert_eq!(branch.branch_name(), "doggy");
    }

    #[test]
    fn test_remote_branch_new() {
        assert_eq!(
            RemoteBranchRef::new("origin", "puppy"),
            Ref::from_str("refs/remotes/origin/puppy").unwrap(),
        );
    }

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

    #[test]
    fn test_remote_branch_remote_and_branch() {
        assert_eq!(
            RemoteBranchRef::new("origin", "puppy/doggy").remote_and_branch(),
            ("origin", "puppy/doggy"),
        );
    }

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

    #[test]
    fn test_remote_branch_as_local() {
        assert_eq!(
            RemoteBranchRef::new("origin", "puppy").as_local(),
            Ref::from_str("refs/heads/puppy").unwrap(),
        );
    }

    #[test]
    fn test_remote_branch_display() {
        let branch = RemoteBranchRef::new("origin", "puppy");
        assert_eq!(format!("{branch}"), "origin/puppy");
        assert_eq!(format!("{branch:#}"), "refs/remotes/origin/puppy");
    }
}