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
use crate::vcs::git::{self, error::Error, ext, reference::Ref};
use std::{cmp::Ordering, convert::TryFrom, fmt, str};
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub enum BranchType {
Local,
Remote {
name: Option<String>,
},
}
impl From<BranchType> for git2::BranchType {
fn from(other: BranchType) -> Self {
match other {
BranchType::Local => git2::BranchType::Local,
BranchType::Remote { .. } => git2::BranchType::Remote,
}
}
}
impl From<git2::BranchType> for BranchType {
fn from(other: git2::BranchType) -> Self {
match other {
git2::BranchType::Local => BranchType::Local,
git2::BranchType::Remote => BranchType::Remote { name: None },
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct BranchName(pub(crate) String);
impl fmt::Display for BranchName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl TryFrom<&[u8]> for BranchName {
type Error = str::Utf8Error;
fn try_from(name: &[u8]) -> Result<Self, Self::Error> {
let name = str::from_utf8(name)?;
let short_name = match git::ext::try_extract_refname(name) {
Ok(stripped) => stripped,
Err(original) => original,
};
Ok(Self(short_name))
}
}
impl BranchName {
pub fn new(name: &str) -> Self {
Self(name.into())
}
pub fn name(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Branch {
pub name: BranchName,
pub locality: BranchType,
}
impl PartialOrd for Branch {
fn partial_cmp(&self, other: &Branch) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Branch {
fn cmp(&self, other: &Branch) -> Ordering {
self.name.cmp(&other.name)
}
}
impl From<Branch> for Ref {
fn from(other: Branch) -> Self {
match other.locality {
BranchType::Local => Self::LocalBranch { name: other.name },
BranchType::Remote { name } => Self::RemoteBranch {
name: other.name,
remote: name.unwrap_or_else(|| "**".to_string()),
},
}
}
}
impl Branch {
pub fn remote(name: &str, remote: &str) -> Self {
Self {
name: BranchName(name.to_string()),
locality: BranchType::Remote {
name: Some(remote.to_string()),
},
}
}
pub fn local(name: &str) -> Self {
Self {
name: BranchName(name.to_string()),
locality: BranchType::Local,
}
}
pub fn name(&self) -> String {
let branch_name = self.name.0.clone();
match self.locality {
BranchType::Local => branch_name,
BranchType::Remote { ref name } => match name {
None => branch_name,
Some(remote_name) => format!("{}/{}", remote_name, branch_name),
},
}
}
}
impl<'repo> TryFrom<git2::Reference<'repo>> for Branch {
type Error = Error;
fn try_from(reference: git2::Reference) -> Result<Self, Self::Error> {
let is_remote = ext::is_remote(&reference);
let is_tag = reference.is_tag();
let is_note = reference.is_note();
let name = BranchName::try_from(reference.name_bytes())?;
if is_tag || is_note {
return Err(Error::NotBranch(name));
}
if is_remote {
let mut split = name.0.splitn(2, '/');
let remote_name = split
.next()
.ok_or_else(|| Error::ParseRemoteBranch(name.clone()))?;
let name = split
.next()
.ok_or_else(|| Error::ParseRemoteBranch(name.clone()))?;
Ok(Self {
name: BranchName(name.to_string()),
locality: BranchType::Remote {
name: Some(remote_name.to_string()),
},
})
} else {
Ok(Self {
name,
locality: BranchType::Local,
})
}
}
}