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
use std::borrow::Cow;
use gix_ref::{Category, FullNameRef};
use crate::{
bstr::ByteSlice,
remote,
repository::{branch_remote_ref_name, branch_remote_tracking_ref_name},
Reference,
};
/// Remotes
impl<'repo> Reference<'repo> {
/// Find the name of our remote for `direction` as configured in `branch.<name>.remote|pushRemote` respectively.
/// Return `None` if no remote is configured.
///
/// See also [`Repository::branch_remote_name()`](crate::Repository::branch_remote_name()) for more details.
pub fn remote_name(&self, direction: remote::Direction) -> Option<remote::Name<'_>> {
let (category, shortname) = self.name().category_and_short_name()?;
match category {
Category::RemoteBranch => {
if shortname.find_iter("/").take(2).count() == 1 {
let slash_pos = shortname.find_byte(b'/').expect("it was just found");
shortname[..slash_pos]
.as_bstr()
.to_str()
.ok()
.map(|n| remote::Name::Symbol(n.into()))
} else {
let remotes = self.repo.remote_names();
for slash_pos in shortname.rfind_iter("/") {
let candidate = shortname[..slash_pos].as_bstr();
if remotes.contains(candidate) {
return candidate.to_str().ok().map(|n| remote::Name::Symbol(n.into()));
}
}
None
}
}
Category::LocalBranch => self.repo.branch_remote_name(shortname, direction),
_ => None,
}
}
/// Find the remote along with all configuration associated with it suitable for handling this reference.
///
/// See also [`Repository::branch_remote()`](crate::Repository::branch_remote()) for more details.
pub fn remote(
&self,
direction: remote::Direction,
) -> Option<Result<crate::Remote<'repo>, remote::find::existing::Error>> {
self.repo.branch_remote(self.name().shorten(), direction)
}
/// Return the name of this reference on the remote side.
///
/// See [`Repository::branch_remote_ref_name()`](crate::Repository::branch_remote_ref_name()) for details.
#[doc(alias = "upstream", alias = "git2")]
pub fn remote_ref_name(
&self,
direction: remote::Direction,
) -> Option<Result<Cow<'_, FullNameRef>, branch_remote_ref_name::Error>> {
self.repo.branch_remote_ref_name(self.name(), direction)
}
/// Return the name of the reference that tracks this reference on the remote side.
///
/// See [`Repository::branch_remote_tracking_ref_name()`](crate::Repository::branch_remote_tracking_ref_name()) for details.
#[doc(alias = "upstream", alias = "git2")]
pub fn remote_tracking_ref_name(
&self,
direction: remote::Direction,
) -> Option<Result<Cow<'_, FullNameRef>, branch_remote_tracking_ref_name::Error>> {
self.repo.branch_remote_tracking_ref_name(self.name(), direction)
}
}