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
use std::collections::HashSet;
use git_features::progress::Progress;
use git_protocol::transport::client::Transport;
use crate::{
bstr::{BString, ByteVec},
remote::{connection::HandshakeWithRefs, fetch, Connection, Direction},
};
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error(transparent)]
Handshake(#[from] git_protocol::fetch::handshake::Error),
#[error(transparent)]
ListRefs(#[from] git_protocol::fetch::refs::Error),
#[error(transparent)]
Transport(#[from] git_protocol::transport::client::Error),
#[error(transparent)]
ConfigureCredentials(#[from] crate::config::credential_helpers::Error),
#[error(transparent)]
MappingValidation(#[from] git_refspec::match_group::validate::Error),
}
#[derive(Debug, Clone)]
pub struct Options {
pub prefix_from_spec_as_filter_on_remote: bool,
pub handshake_parameters: Vec<(String, Option<String>)>,
}
impl Default for Options {
fn default() -> Self {
Options {
prefix_from_spec_as_filter_on_remote: true,
handshake_parameters: Default::default(),
}
}
}
impl<'remote, 'repo, T, P> Connection<'remote, 'repo, T, P>
where
T: Transport,
P: Progress,
{
#[git_protocol::maybe_async::maybe_async]
pub async fn ref_map(mut self, options: Options) -> Result<fetch::RefMap, Error> {
let res = self.ref_map_inner(options).await;
git_protocol::fetch::indicate_end_of_interaction(&mut self.transport)
.await
.ok();
res
}
#[git_protocol::maybe_async::maybe_async]
pub(crate) async fn ref_map_inner(
&mut self,
Options {
prefix_from_spec_as_filter_on_remote,
handshake_parameters,
}: Options,
) -> Result<fetch::RefMap, Error> {
let remote = self
.fetch_refs(prefix_from_spec_as_filter_on_remote, handshake_parameters)
.await?;
let group = git_refspec::MatchGroup::from_fetch_specs(self.remote.fetch_specs.iter().map(|s| s.to_ref()));
let (res, fixes) = group
.match_remotes(remote.refs.iter().map(|r| {
let (full_ref_name, target, object) = r.unpack();
git_refspec::match_group::Item {
full_ref_name,
target,
object,
}
}))
.validated()?;
let mappings = res.mappings;
let mappings = mappings
.into_iter()
.map(|m| fetch::Mapping {
remote: m
.item_index
.map(|idx| fetch::Source::Ref(remote.refs[idx].clone()))
.unwrap_or_else(|| {
fetch::Source::ObjectId(match m.lhs {
git_refspec::match_group::SourceRef::ObjectId(id) => id,
_ => unreachable!("no item index implies having an object id"),
})
}),
local: m.rhs.map(|c| c.into_owned()),
spec_index: m.spec_index,
})
.collect();
Ok(fetch::RefMap {
mappings,
fixes,
remote_refs: remote.refs,
handshake: remote.outcome,
})
}
#[git_protocol::maybe_async::maybe_async]
async fn fetch_refs(
&mut self,
filter_by_prefix: bool,
extra_parameters: Vec<(String, Option<String>)>,
) -> Result<HandshakeWithRefs, Error> {
let mut credentials_storage;
let authenticate = match self.authenticate.as_mut() {
Some(f) => f,
None => {
let url = self
.remote
.url(Direction::Fetch)
.map(ToOwned::to_owned)
.unwrap_or_else(|| {
git_url::parse(self.transport.to_url().as_bytes().into())
.expect("valid URL to be provided by transport")
});
credentials_storage = self.configured_credentials(url)?;
&mut credentials_storage
}
};
let mut outcome =
git_protocol::fetch::handshake(&mut self.transport, authenticate, extra_parameters, &mut self.progress)
.await?;
let refs = match outcome.refs.take() {
Some(refs) => refs,
None => {
let specs = &self.remote.fetch_specs;
git_protocol::fetch::refs(
&mut self.transport,
outcome.server_protocol_version,
&outcome.capabilities,
|_capabilities, arguments, _features| {
if filter_by_prefix {
let mut seen = HashSet::new();
for spec in specs {
let spec = spec.to_ref();
if seen.insert(spec.instruction()) {
if let Some(prefix) = spec.prefix() {
let mut arg: BString = "ref-prefix ".into();
arg.push_str(prefix);
arguments.push(arg)
}
}
}
}
Ok(git_protocol::fetch::delegate::LsRefsAction::Continue)
},
&mut self.progress,
)
.await?
}
};
Ok(HandshakeWithRefs { outcome, refs })
}
}