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
use std::sync::atomic::AtomicBool;
use git_odb::FindExt;
use git_protocol::transport::client::Transport;
use crate::{
remote,
remote::{
fetch,
fetch::{DryRun, RefMap},
ref_map, Connection,
},
Progress,
};
mod error {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("{message}{}", desired.map(|n| format!(" (got {})", n)).unwrap_or_default())]
Configuration {
message: &'static str,
desired: Option<i64>,
source: Option<git_config::value::Error>,
},
#[error("Could not decode server reply")]
FetchResponse(#[from] git_protocol::fetch::response::Error),
#[error(transparent)]
Negotiate(#[from] super::negotiate::Error),
#[error(transparent)]
Client(#[from] git_protocol::transport::client::Error),
#[error(transparent)]
WritePack(#[from] git_pack::bundle::write::Error),
#[error(transparent)]
UpdateRefs(#[from] super::refs::update::Error),
}
}
pub use error::Error;
#[derive(Debug, Clone)]
pub enum Status {
NoChange,
Change {
write_pack_bundle: git_pack::bundle::write::Outcome,
update_refs: refs::update::Outcome,
},
DryRun {
update_refs: refs::update::Outcome,
},
}
#[derive(Debug, Clone)]
pub struct Outcome {
pub ref_map: RefMap,
pub status: Status,
}
pub mod negotiate;
pub mod prepare {
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Cannot perform a meaningful fetch operation without any configured ref-specs")]
MissingRefSpecs,
#[error(transparent)]
RefMap(#[from] crate::remote::ref_map::Error),
}
}
impl<'remote, 'repo, T, P> Connection<'remote, 'repo, T, P>
where
T: Transport,
P: Progress,
{
pub fn prepare_fetch(mut self, options: ref_map::Options) -> Result<Prepare<'remote, 'repo, T, P>, prepare::Error> {
if self.remote.refspecs(remote::Direction::Fetch).is_empty() {
return Err(prepare::Error::MissingRefSpecs);
}
let ref_map = self.ref_map_inner(options)?;
Ok(Prepare {
con: Some(self),
ref_map,
dry_run: DryRun::No,
})
}
}
impl<'remote, 'repo, T, P> Prepare<'remote, 'repo, T, P>
where
T: Transport,
P: Progress,
{
pub fn receive(mut self, should_interrupt: &AtomicBool) -> Result<Outcome, Error> {
let mut con = self.con.take().expect("receive() can only be called once");
let handshake = &self.ref_map.handshake;
let protocol_version = handshake.server_protocol_version;
let fetch = git_protocol::fetch::Command::Fetch;
let fetch_features = fetch.default_features(protocol_version, &handshake.capabilities);
git_protocol::fetch::Response::check_required_features(protocol_version, &fetch_features)?;
let sideband_all = fetch_features.iter().any(|(n, _)| *n == "sideband-all");
let mut arguments = git_protocol::fetch::Arguments::new(protocol_version, fetch_features);
let mut previous_response = None::<git_protocol::fetch::Response>;
let mut round = 1;
let progress = &mut con.progress;
let repo = con.remote.repo;
let reader = 'negotiation: loop {
progress.step();
progress.set_name(format!("negotiate (round {})", round));
let is_done = match negotiate::one_round(
negotiate::Algorithm::Naive,
round,
repo,
&self.ref_map,
&mut arguments,
previous_response.as_ref(),
) {
Ok(_) if arguments.is_empty() => {
git_protocol::fetch::indicate_end_of_interaction(&mut con.transport).ok();
return Ok(Outcome {
ref_map: std::mem::take(&mut self.ref_map),
status: Status::NoChange,
});
}
Ok(is_done) => is_done,
Err(err) => {
git_protocol::fetch::indicate_end_of_interaction(&mut con.transport).ok();
return Err(err.into());
}
};
round += 1;
let mut reader = arguments.send(&mut con.transport, is_done)?;
if sideband_all {
setup_remote_progress(progress, &mut reader);
}
let response = git_protocol::fetch::Response::from_line_reader(protocol_version, &mut reader)?;
if response.has_pack() {
progress.step();
progress.set_name("receiving pack");
if !sideband_all {
setup_remote_progress(progress, &mut reader);
}
break 'negotiation reader;
} else {
previous_response = Some(response);
}
};
let options = git_pack::bundle::write::Options {
thread_limit: config::index_threads(repo)?,
index_version: config::pack_index_version(repo)?,
iteration_mode: git_pack::data::input::Mode::Verify,
object_hash: con.remote.repo.object_hash(),
};
let write_pack_bundle = if matches!(self.dry_run, fetch::DryRun::No) {
Some(git_pack::Bundle::write_to_directory(
reader,
Some(repo.objects.store_ref().path().join("pack")),
con.progress,
should_interrupt,
Some(Box::new({
let repo = repo.clone();
move |oid, buf| repo.objects.find(oid, buf).ok()
})),
options,
)?)
} else {
drop(reader);
None
};
if matches!(protocol_version, git_protocol::transport::Protocol::V2) {
git_protocol::fetch::indicate_end_of_interaction(&mut con.transport).ok();
}
let update_refs = refs::update(
repo,
"fetch",
&self.ref_map.mappings,
con.remote.refspecs(remote::Direction::Fetch),
self.dry_run,
)?;
Ok(Outcome {
ref_map: std::mem::take(&mut self.ref_map),
status: match write_pack_bundle {
Some(write_pack_bundle) => Status::Change {
write_pack_bundle,
update_refs,
},
None => Status::DryRun { update_refs },
},
})
}
}
fn setup_remote_progress(
progress: &mut impl Progress,
reader: &mut Box<dyn git_protocol::transport::client::ExtendedBufRead + Unpin + '_>,
) {
use git_protocol::transport::client::ExtendedBufRead;
reader.set_progress_handler(Some(Box::new({
let mut remote_progress = progress.add_child("remote");
move |is_err: bool, data: &[u8]| {
git_protocol::RemoteProgress::translate_to_progress(is_err, data, &mut remote_progress)
}
}) as git_protocol::transport::client::HandleProgress));
}
mod config;
#[path = "update_refs/mod.rs"]
pub mod refs;
pub struct Prepare<'remote, 'repo, T, P>
where
T: Transport,
{
con: Option<Connection<'remote, 'repo, T, P>>,
ref_map: RefMap,
dry_run: DryRun,
}
impl<'remote, 'repo, T, P> Prepare<'remote, 'repo, T, P>
where
T: Transport,
{
pub fn with_dry_run(mut self, enabled: bool) -> Self {
self.dry_run = enabled.then(|| DryRun::Yes).unwrap_or(DryRun::No);
self
}
}
impl<'remote, 'repo, T, P> Drop for Prepare<'remote, 'repo, T, P>
where
T: Transport,
{
fn drop(&mut self) {
if let Some(mut con) = self.con.take() {
git_protocol::fetch::indicate_end_of_interaction(&mut con.transport).ok();
}
}
}