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
use git_refspec::RefSpec;
use crate::{bstr::BStr, remote, Remote};
impl<'repo> Remote<'repo> {
pub fn name(&self) -> Option<&remote::Name<'static>> {
self.name.as_ref()
}
pub fn repo(&self) -> &'repo crate::Repository {
self.repo
}
pub fn refspecs(&self, direction: remote::Direction) -> &[RefSpec] {
match direction {
remote::Direction::Fetch => &self.fetch_specs,
remote::Direction::Push => &self.push_specs,
}
}
pub fn fetch_tags(&self) -> remote::fetch::Tags {
self.fetch_tags
}
pub fn url(&self, direction: remote::Direction) -> Option<&git_url::Url> {
match direction {
remote::Direction::Fetch => self.url_alias.as_ref().or(self.url.as_ref()),
remote::Direction::Push => self
.push_url_alias
.as_ref()
.or(self.push_url.as_ref())
.or_else(|| self.url(remote::Direction::Fetch)),
}
}
}
impl Remote<'_> {
pub fn rewrite_urls(&mut self) -> Result<&mut Self, remote::init::Error> {
let url_err = match remote::init::rewrite_url(&self.repo.config, self.url.as_ref(), remote::Direction::Fetch) {
Ok(url) => {
self.url_alias = url;
None
}
Err(err) => err.into(),
};
let push_url_err =
match remote::init::rewrite_url(&self.repo.config, self.push_url.as_ref(), remote::Direction::Push) {
Ok(url) => {
self.push_url_alias = url;
None
}
Err(err) => err.into(),
};
url_err.or(push_url_err).map(Err::<&mut Self, _>).transpose()?;
Ok(self)
}
pub fn replace_refspecs<Spec>(
&mut self,
specs: impl IntoIterator<Item = Spec>,
direction: remote::Direction,
) -> Result<(), git_refspec::parse::Error>
where
Spec: AsRef<BStr>,
{
use remote::Direction::*;
let specs: Vec<_> = specs
.into_iter()
.map(|spec| {
git_refspec::parse(
spec.as_ref(),
match direction {
Push => git_refspec::parse::Operation::Push,
Fetch => git_refspec::parse::Operation::Fetch,
},
)
.map(|url| url.to_owned())
})
.collect::<Result<_, _>>()?;
let dst = match direction {
Push => &mut self.push_specs,
Fetch => &mut self.fetch_specs,
};
*dst = specs;
Ok(())
}
}