/
mod.rs
300 lines (268 loc) · 8.56 KB
/
mod.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use std::io;
use bstr::{BString, ByteSlice};
use git_protocol::fetch::{self, Action, Arguments, LsRefsAction, Ref, Response};
use git_transport::client::Capabilities;
use crate::fixture_bytes;
#[cfg(feature = "blocking-client")]
type Cursor = std::io::Cursor<Vec<u8>>;
#[cfg(feature = "async-client")]
type Cursor = futures_lite::io::Cursor<Vec<u8>>;
fn helper_unused(_action: git_credentials::helper::Action) -> git_credentials::protocol::Result {
panic!("Call to credentials helper is unexpected")
}
#[derive(Default)]
pub struct CloneDelegate {
pack_bytes: usize,
abort_with: Option<std::io::Error>,
}
impl fetch::DelegateBlocking for CloneDelegate {
fn prepare_fetch(
&mut self,
_version: git_transport::Protocol,
_server: &Capabilities,
_features: &mut Vec<(&str, Option<&str>)>,
_refs: &[fetch::Ref],
) -> io::Result<Action> {
match self.abort_with.take() {
Some(err) => Err(err),
None => Ok(Action::Continue),
}
}
fn negotiate(
&mut self,
refs: &[Ref],
arguments: &mut Arguments,
_previous_response: Option<&Response>,
) -> io::Result<Action> {
for r in refs {
if let Some(id) = r.unpack().1 {
arguments.want(id);
}
}
Ok(Action::Cancel)
}
}
/// A delegate which bypasses refs negotiation entirely via `ref-in-want`.
#[derive(Default)]
pub struct CloneRefInWantDelegate {
/// Statically-known refs we want.
want_refs: Vec<BString>,
/// Number of bytes received of the final packfile.
pack_bytes: usize,
/// Refs advertised by `ls-refs` -- should always be empty, as we skip `ls-refs`.
refs: Vec<fetch::Ref>,
/// Refs advertised as `wanted-ref` -- should always match `want_refs`
wanted_refs: Vec<fetch::Ref>,
}
impl fetch::DelegateBlocking for CloneRefInWantDelegate {
fn prepare_ls_refs(
&mut self,
_server: &Capabilities,
_arguments: &mut Vec<BString>,
_features: &mut Vec<(&str, Option<&str>)>,
) -> io::Result<LsRefsAction> {
Ok(LsRefsAction::Skip)
}
fn prepare_fetch(
&mut self,
_version: git_transport::Protocol,
_server: &Capabilities,
_features: &mut Vec<(&str, Option<&str>)>,
refs: &[fetch::Ref],
) -> io::Result<Action> {
self.refs = refs.to_owned();
Ok(Action::Continue)
}
fn negotiate(&mut self, _refs: &[Ref], arguments: &mut Arguments, _prev: Option<&Response>) -> io::Result<Action> {
for wanted_ref in &self.want_refs {
arguments.want_ref(wanted_ref.as_ref())
}
Ok(Action::Cancel)
}
}
#[derive(Default)]
pub struct LsRemoteDelegate {
refs: Vec<fetch::Ref>,
abort_with: Option<std::io::Error>,
}
impl fetch::DelegateBlocking for LsRemoteDelegate {
fn handshake_extra_parameters(&self) -> Vec<(String, Option<String>)> {
vec![("value-only".into(), None), ("key".into(), Some("value".into()))]
}
fn prepare_ls_refs(
&mut self,
_server: &Capabilities,
_arguments: &mut Vec<BString>,
_features: &mut Vec<(&str, Option<&str>)>,
) -> std::io::Result<LsRefsAction> {
match self.abort_with.take() {
Some(err) => Err(err),
None => Ok(LsRefsAction::Continue),
}
}
fn prepare_fetch(
&mut self,
_version: git_transport::Protocol,
_server: &Capabilities,
_features: &mut Vec<(&str, Option<&str>)>,
refs: &[fetch::Ref],
) -> io::Result<fetch::Action> {
self.refs = refs.to_owned();
Ok(fetch::Action::Cancel)
}
fn negotiate(
&mut self,
_refs: &[Ref],
_arguments: &mut Arguments,
_previous_response: Option<&Response>,
) -> io::Result<Action> {
unreachable!("this must not be called after closing the connection in `prepare_fetch(…)`")
}
}
#[cfg(feature = "blocking-client")]
mod blocking_io {
use std::io;
use git_features::progress::Progress;
use git_protocol::{
fetch,
fetch::{Ref, Response},
};
use crate::fetch::{CloneDelegate, CloneRefInWantDelegate, LsRemoteDelegate};
impl fetch::Delegate for CloneDelegate {
fn receive_pack(
&mut self,
mut input: impl io::BufRead,
_progress: impl Progress,
_refs: &[Ref],
_previous_response: &Response,
) -> io::Result<()> {
self.pack_bytes = io::copy(&mut input, &mut io::sink())? as usize;
Ok(())
}
}
impl fetch::Delegate for CloneRefInWantDelegate {
fn receive_pack(
&mut self,
mut input: impl io::BufRead,
_progress: impl Progress,
_refs: &[Ref],
response: &Response,
) -> io::Result<()> {
for wanted in response.wanted_refs() {
self.wanted_refs.push(fetch::Ref::Direct {
full_ref_name: wanted.path.clone(),
object: wanted.id,
});
}
self.pack_bytes = io::copy(&mut input, &mut io::sink())? as usize;
Ok(())
}
}
impl fetch::Delegate for LsRemoteDelegate {
fn receive_pack(
&mut self,
_input: impl io::BufRead,
_progress: impl Progress,
_refs: &[Ref],
_previous_response: &Response,
) -> io::Result<()> {
unreachable!("Should not be called for ls-refs");
}
}
}
#[cfg(feature = "async-client")]
mod async_io {
use std::io;
use async_trait::async_trait;
use futures_io::AsyncBufRead;
use git_features::progress::Progress;
use git_protocol::{
fetch,
fetch::{Ref, Response},
};
use crate::fetch::{CloneDelegate, CloneRefInWantDelegate, LsRemoteDelegate};
#[async_trait(?Send)]
impl fetch::Delegate for CloneDelegate {
async fn receive_pack(
&mut self,
mut input: impl AsyncBufRead + Unpin + 'async_trait,
_progress: impl Progress,
_refs: &[Ref],
_previous_response: &Response,
) -> io::Result<()> {
self.pack_bytes = futures_lite::io::copy(&mut input, &mut futures_lite::io::sink()).await? as usize;
Ok(())
}
}
#[async_trait(?Send)]
impl fetch::Delegate for CloneRefInWantDelegate {
async fn receive_pack(
&mut self,
mut input: impl AsyncBufRead + Unpin + 'async_trait,
_progress: impl Progress,
_refs: &[Ref],
response: &Response,
) -> io::Result<()> {
for wanted in response.wanted_refs() {
self.wanted_refs.push(fetch::Ref::Direct {
full_ref_name: wanted.path.clone(),
object: wanted.id,
});
}
self.pack_bytes = futures_lite::io::copy(&mut input, &mut futures_lite::io::sink()).await? as usize;
Ok(())
}
}
#[async_trait(?Send)]
impl fetch::Delegate for LsRemoteDelegate {
async fn receive_pack(
&mut self,
_input: impl AsyncBufRead + Unpin + 'async_trait,
_progress: impl Progress,
_refs: &[Ref],
_previous_response: &Response,
) -> io::Result<()> {
unreachable!("Should not be called for ls-refs");
}
}
}
pub fn oid(hex_sha: &str) -> git_hash::ObjectId {
git_hash::ObjectId::from_hex(hex_sha.as_bytes()).expect("valid input")
}
#[cfg(feature = "async-client")]
pub fn transport<W: futures_io::AsyncWrite + Unpin>(
out: W,
path: &str,
desired_version: git_transport::Protocol,
mode: git_transport::client::git::ConnectMode,
) -> git_transport::client::git::Connection<Cursor, W> {
let response = fixture_bytes(path);
git_transport::client::git::Connection::new(
Cursor::new(response),
out,
desired_version,
b"does/not/matter".as_bstr().to_owned(),
None::<(&str, _)>,
mode,
)
}
#[cfg(feature = "blocking-client")]
pub fn transport<W: std::io::Write>(
out: W,
path: &str,
version: git_transport::Protocol,
mode: git_transport::client::git::ConnectMode,
) -> git_transport::client::git::Connection<Cursor, W> {
let response = fixture_bytes(path);
git_transport::client::git::Connection::new(
Cursor::new(response),
out,
version,
b"does/not/matter".as_bstr().to_owned(),
None::<(&str, _)>,
mode,
)
}
pub mod response;
mod v1;
mod v2;