gix_protocol/fetch/negotiate.rs
1//! A modules with primitives to perform negotiation as part of a fetch operation.
2//!
3//! The functions provided are called in a certain order:
4//!
5//! 1. [`mark_complete_and_common_ref()`] - initialize the [`negotiator`](gix_negotiate::Negotiator) with all state known on the remote.
6//! 2. [`add_wants()`] is called if the call at 1) returned [`Action::MustNegotiate`].
7//! 3. [`one_round()`] is called for each negotiation round, providing information if the negotiation is done.
8use gix_date::SecondsSinceUnixEpoch;
9use gix_negotiate::Flags;
10use gix_ref::file::ReferenceExt;
11use std::borrow::Cow;
12
13use crate::fetch::{refmap, RefMap, Shallow, Tags};
14
15type Queue = gix_revwalk::PriorityQueue<SecondsSinceUnixEpoch, gix_hash::ObjectId>;
16
17/// The error returned during [`one_round()`] or [`mark_complete_and_common_ref()`].
18#[derive(Debug, thiserror::Error)]
19#[allow(missing_docs)]
20pub enum Error {
21 #[error("We were unable to figure out what objects the server should send after {rounds} round(s)")]
22 NegotiationFailed { rounds: usize },
23 #[error(transparent)]
24 LookupCommitInGraph(#[from] gix_revwalk::graph::get_or_insert_default::Error),
25 #[error(transparent)]
26 OpenPackedRefsBuffer(#[from] gix_ref::packed::buffer::open::Error),
27 #[error(transparent)]
28 IO(#[from] std::io::Error),
29 #[error(transparent)]
30 InitRefIter(#[from] gix_ref::file::iter::loose_then_packed::Error),
31 #[error(transparent)]
32 PeelToId(#[from] gix_ref::peel::to_id::Error),
33 #[error(transparent)]
34 AlternateRefsAndObjects(Box<dyn std::error::Error + Send + Sync + 'static>),
35}
36
37/// Determines what should be done after [preparing the commit-graph for negotiation](mark_complete_and_common_ref).
38#[must_use]
39#[derive(Debug, Clone)]
40pub enum Action {
41 /// None of the remote refs moved compared to our last recorded state (via tracking refs), so there is nothing to do at all,
42 /// not even a ref update.
43 NoChange,
44 /// Don't negotiate, don't fetch the pack, skip right to updating the references.
45 ///
46 /// This happens if we already have all local objects even though the server seems to have changed.
47 SkipToRefUpdate,
48 /// We can't know for sure if fetching *is not* needed, so we go ahead and negotiate.
49 MustNegotiate {
50 /// Each `ref_map.mapping` has a slot here which is `true` if we have the object the remote ref points to, locally.
51 remote_ref_target_known: Vec<bool>,
52 },
53}
54
55/// Key information about each round in the pack-negotiation, as produced by [`one_round()`].
56#[derive(Debug, Clone, Copy)]
57pub struct Round {
58 /// The amount of `HAVE` lines sent this round.
59 ///
60 /// Each `HAVE` is an object that we tell the server about which would acknowledge each one it has as well.
61 pub haves_sent: usize,
62 /// A total counter, over all previous rounds, indicating how many `HAVE`s we sent without seeing a single acknowledgement,
63 /// i.e. the indication of a common object.
64 ///
65 /// This number maybe zero or be lower compared to the previous round if we have received at least one acknowledgement.
66 pub in_vain: usize,
67 /// The amount of haves we should send in this round.
68 ///
69 /// If the value is lower than `haves_sent` (the `HAVE` lines actually sent), the negotiation algorithm has run out of options
70 /// which typically indicates the end of the negotiation phase.
71 pub haves_to_send: usize,
72 /// If `true`, the server reported, as response to our previous `HAVE`s, that at least one of them is in common by acknowledging it.
73 ///
74 /// This may also lead to the server responding with a pack.
75 pub previous_response_had_at_least_one_in_common: bool,
76}
77
78/// This function is modeled after the similarly named one in the git codebase to mark known refs in a commit-graph.
79///
80/// It to do the following:
81///
82/// * figure out all advertised refs on the remote *that we already have* and keep track of the oldest one as cutoff date.
83/// * mark all of our own refs as tips for a traversal.
84/// * mark all their parents, recursively, up to (and including) the cutoff date up to which we have seen the servers commit that we have.
85/// * pass all known-to-be-common-with-remote commits to the negotiator as common commits.
86///
87/// This is done so that we already find the most recent common commits, even if we are ahead, which is *potentially* better than
88/// what we would get if we would rely on tracking refs alone, particularly if one wouldn't trust the tracking refs for some reason.
89///
90/// Note that git doesn't trust its own tracking refs as the server *might* have changed completely, for instance by force-pushing, so
91/// marking our local tracking refs as known is something that's actually not proven to be correct so it's not done.
92///
93/// Additionally, it does what's done in `transport.c` and we check if a fetch is actually needed as at least one advertised ref changed.
94///
95/// Finally, we also mark tips in the `negotiator` in one go to avoid traversing all refs twice, since we naturally encounter all tips during
96/// our own walk.
97///
98/// Return whether we should negotiate, along with a queue for later use.
99///
100/// # Parameters
101///
102/// * `objects`
103/// - Access to the object database. *Note* that the `exists()` calls must not trigger a refresh of the ODB packs as plenty of them might fail, i.e. find on object.
104/// * `refs`
105/// - Access to the git references database.
106/// * `alternates`
107/// - A function that returns an iterator over `(refs, objects)` for each alternate repository, to assure all known objects are added also according to their tips.
108/// * `negotiator`
109/// - The implementation that performs the negotiation later, i.e. prepare wants and haves.
110/// * `graph`
111/// - The commit-graph for use by the `negotiator` - we populate it with tips to initialize the graph traversal.
112/// * `ref_map`
113/// - The references known on the remote, as previously obtained with [`RefMap::new()`].
114/// * `shallow`
115/// - How to deal with shallow repositories. It does affect how negotiations are performed.
116/// * `mapping_is_ignored`
117/// - `f(mapping) -> bool` returns `true` if the given mapping should not participate in change tracking.
118/// - [`make_refmapping_ignore_predicate()`] is a typical implementation for this.
119#[allow(clippy::too_many_arguments)]
120pub fn mark_complete_and_common_ref<Out, F, E>(
121 objects: &(impl gix_object::Find + gix_object::FindHeader + gix_object::Exists),
122 refs: &gix_ref::file::Store,
123 alternates: impl FnOnce() -> Result<Out, E>,
124 negotiator: &mut dyn gix_negotiate::Negotiator,
125 graph: &mut gix_negotiate::Graph<'_, '_>,
126 ref_map: &RefMap,
127 shallow: &Shallow,
128 mapping_is_ignored: impl Fn(&refmap::Mapping) -> bool,
129) -> Result<Action, Error>
130where
131 E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
132 Out: Iterator<Item = (gix_ref::file::Store, F)>,
133 F: gix_object::Find,
134{
135 let _span = gix_trace::detail!("mark_complete_and_common_ref", mappings = ref_map.mappings.len());
136 if ref_map.mappings.is_empty() {
137 return Ok(Action::NoChange);
138 }
139 if let Shallow::Deepen(0) = shallow {
140 // Avoid deepening (relative) with zero as it seems to upset the server. Git also doesn't actually
141 // perform the negotiation for some reason (couldn't find it in code).
142 return Ok(Action::NoChange);
143 }
144 if let Some(refmap::Mapping {
145 remote: refmap::Source::Ref(crate::handshake::Ref::Unborn { .. }),
146 ..
147 }) = ref_map.mappings.last().filter(|_| ref_map.mappings.len() == 1)
148 {
149 // There is only an unborn branch, as the remote has an empty repository. This means there is nothing to do except for
150 // possibly reproducing the unborn branch locally.
151 return Ok(Action::SkipToRefUpdate);
152 }
153
154 // Compute the cut-off date by checking which of the refs advertised (and matched in refspecs) by the remote we have,
155 // and keep the oldest one.
156 let mut cutoff_date = None::<SecondsSinceUnixEpoch>;
157 let mut num_mappings_with_change = 0;
158 let mut remote_ref_target_known: Vec<bool> = std::iter::repeat(false).take(ref_map.mappings.len()).collect();
159 let mut remote_ref_included: Vec<bool> = std::iter::repeat(false).take(ref_map.mappings.len()).collect();
160
161 for (mapping_idx, mapping) in ref_map.mappings.iter().enumerate() {
162 let want_id = mapping.remote.as_id();
163 let have_id = mapping.local.as_ref().and_then(|name| {
164 // this is the only time git uses the peer-id.
165 let r = refs.find(name).ok()?;
166 r.target.try_id().map(ToOwned::to_owned)
167 });
168
169 // Even for ignored mappings we want to know if the `want` is already present locally, so skip nothing else.
170 if !mapping_is_ignored(mapping) {
171 remote_ref_included[mapping_idx] = true;
172 // Like git, we don't let known unchanged mappings participate in the tree traversal
173 if want_id.zip(have_id).map_or(true, |(want, have)| want != have) {
174 num_mappings_with_change += 1;
175 }
176 }
177
178 if let Some(commit) = want_id
179 .and_then(|id| graph.get_or_insert_commit(id.into(), |_| {}).transpose())
180 .transpose()?
181 {
182 remote_ref_target_known[mapping_idx] = true;
183 cutoff_date = cutoff_date.unwrap_or_default().max(commit.commit_time).into();
184 } else if want_id.is_some_and(|maybe_annotated_tag| objects.exists(maybe_annotated_tag)) {
185 remote_ref_target_known[mapping_idx] = true;
186 }
187 }
188
189 if matches!(shallow, Shallow::NoChange) {
190 if num_mappings_with_change == 0 {
191 return Ok(Action::NoChange);
192 } else if remote_ref_target_known
193 .iter()
194 .zip(remote_ref_included)
195 .filter_map(|(known, included)| included.then_some(known))
196 .all(|known| *known)
197 {
198 return Ok(Action::SkipToRefUpdate);
199 }
200 }
201
202 // color our commits as complete as identified by references, unconditionally
203 // (`git` is conditional here based on `deepen`, but it doesn't make sense and it's hard to extract from history when that happened).
204 let mut queue = Queue::new();
205 mark_all_refs_in_repo(refs, objects, graph, &mut queue, Flags::COMPLETE)?;
206 for (alt_refs, alt_objs) in alternates().map_err(|err| Error::AlternateRefsAndObjects(err.into()))? {
207 mark_all_refs_in_repo(&alt_refs, &alt_objs, graph, &mut queue, Flags::COMPLETE)?;
208 }
209 // Keep track of the tips, which happen to be on our queue right, before we traverse the graph with cutoff.
210 let tips = if let Some(cutoff) = cutoff_date {
211 let tips = Cow::Owned(queue.clone());
212 // color all their parents up to the cutoff date, the oldest commit we know the server has.
213 mark_recent_complete_commits(&mut queue, graph, cutoff)?;
214 tips
215 } else {
216 Cow::Borrowed(&queue)
217 };
218
219 gix_trace::detail!("mark known_common").into_scope(|| -> Result<_, Error> {
220 // mark all complete advertised refs as common refs.
221 for mapping in ref_map
222 .mappings
223 .iter()
224 .zip(remote_ref_target_known.iter().copied())
225 // We need this filter as the graph wouldn't contain annotated tags.
226 .filter_map(|(mapping, known)| (!known).then_some(mapping))
227 {
228 let want_id = mapping.remote.as_id();
229 if let Some(common_id) = want_id
230 .and_then(|id| graph.get(id).map(|c| (c, id)))
231 .filter(|(c, _)| c.data.flags.contains(Flags::COMPLETE))
232 .map(|(_, id)| id)
233 {
234 negotiator.known_common(common_id.into(), graph)?;
235 }
236 }
237 Ok(())
238 })?;
239
240 // As negotiators currently may rely on getting `known_common` calls first and tips after, we adhere to that which is the only
241 // reason we cached the set of tips.
242 gix_trace::detail!("mark tips", num_tips = tips.len()).into_scope(|| -> Result<_, Error> {
243 for tip in tips.iter_unordered() {
244 negotiator.add_tip(*tip, graph)?;
245 }
246 Ok(())
247 })?;
248
249 Ok(Action::MustNegotiate {
250 remote_ref_target_known,
251 })
252}
253
254/// Create a predicate that checks if a refspec mapping should be ignored.
255///
256/// We want to ignore mappings during negotiation if they would be handled implicitly by the server, which is the case
257/// when tags would be sent implicitly due to `Tags::Included`.
258pub fn make_refmapping_ignore_predicate(fetch_tags: Tags, ref_map: &RefMap) -> impl Fn(&refmap::Mapping) -> bool + '_ {
259 // With included tags, we have to keep mappings of tags to handle them later when updating refs, but we don't want to
260 // explicitly `want` them as the server will determine by itself which tags are pointing to a commit it wants to send.
261 // If we would not exclude implicit tag mappings like this, we would get too much of the graph.
262 let tag_refspec_to_ignore = matches!(fetch_tags, Tags::Included)
263 .then(|| fetch_tags.to_refspec())
264 .flatten();
265 move |mapping| {
266 tag_refspec_to_ignore.is_some_and(|tag_spec| {
267 mapping
268 .spec_index
269 .implicit_index()
270 .and_then(|idx| ref_map.extra_refspecs.get(idx))
271 .is_some_and(|spec| spec.to_ref() == tag_spec)
272 })
273 }
274}
275
276/// Add all 'wants' to `arguments` once it's known negotiation is necessary.
277///
278/// This is a call to be made when [`mark_complete_and_common_ref()`] returned [`Action::MustNegotiate`].
279/// That variant also contains the `remote_ref_target_known` field which is supposed to be passed here.
280///
281/// `objects` are used to see if remote ids are known here and are tags, in which case they are also added as 'haves' as
282/// [negotiators](gix_negotiate::Negotiator) don't see tags at all.
283///
284/// * `ref_map` is the state of refs as known on the remote.
285/// * `shallow` defines if the history should be shallow.
286/// * `mapping_is_ignored` is typically initialized with [`make_refmapping_ignore_predicate`].
287///
288/// Returns `true` if at least one [want](crate::fetch::Arguments::want()) was added, or `false` otherwise.
289/// Note that not adding a single want can make the remote hang, so it's avoided on the client side by ending the fetch operation.
290pub fn add_wants(
291 objects: &impl gix_object::FindHeader,
292 arguments: &mut crate::fetch::Arguments,
293 ref_map: &RefMap,
294 remote_ref_target_known: &[bool],
295 shallow: &Shallow,
296 mapping_is_ignored: impl Fn(&refmap::Mapping) -> bool,
297) -> bool {
298 // When using shallow, we can't exclude `wants` as the remote won't send anything then. Thus, we have to resend everything
299 // we have as want instead to get exactly the same graph, but possibly deepened.
300 let is_shallow = !matches!(shallow, Shallow::NoChange);
301 let mut has_want = false;
302 let wants = ref_map
303 .mappings
304 .iter()
305 .zip(remote_ref_target_known)
306 .filter_map(|(m, known)| (is_shallow || !*known).then_some(m))
307 .filter(|m| !mapping_is_ignored(m));
308 for want in wants {
309 let id_on_remote = want.remote.as_id();
310 if !arguments.can_use_ref_in_want() || matches!(want.remote, refmap::Source::ObjectId(_)) {
311 if let Some(id) = id_on_remote {
312 arguments.want(id);
313 has_want = true;
314 }
315 } else {
316 arguments.want_ref(
317 want.remote
318 .as_name()
319 .expect("name available if this isn't an object id"),
320 );
321 has_want = true;
322 }
323 let id_is_annotated_tag_we_have = id_on_remote
324 .and_then(|id| objects.try_header(id).ok().flatten().map(|h| (id, h)))
325 .filter(|(_, h)| h.kind == gix_object::Kind::Tag)
326 .map(|(id, _)| id);
327 if let Some(tag_on_remote) = id_is_annotated_tag_we_have {
328 // Annotated tags are not handled at all by negotiators in the commit-graph - they only see commits and thus won't
329 // ever add `have`s for tags. To correct for that, we add these haves here to avoid getting them sent again.
330 arguments.have(tag_on_remote);
331 }
332 }
333 has_want
334}
335
336/// Remove all commits that are more recent than the cut-off, which is the commit time of the oldest common commit we have with the server.
337fn mark_recent_complete_commits(
338 queue: &mut Queue,
339 graph: &mut gix_negotiate::Graph<'_, '_>,
340 cutoff: SecondsSinceUnixEpoch,
341) -> Result<(), Error> {
342 let _span = gix_trace::detail!("mark_recent_complete", queue_len = queue.len());
343 while let Some(id) = queue
344 .peek()
345 .and_then(|(commit_time, id)| (commit_time >= &cutoff).then_some(*id))
346 {
347 queue.pop_value();
348 let commit = graph.get(&id).expect("definitely set when adding tips or parents");
349 for parent_id in commit.parents.clone() {
350 let mut was_complete = false;
351 if let Some(parent) = graph
352 .get_or_insert_commit(parent_id, |md| {
353 was_complete = md.flags.contains(Flags::COMPLETE);
354 md.flags |= Flags::COMPLETE;
355 })?
356 .filter(|_| !was_complete)
357 {
358 queue.insert(parent.commit_time, parent_id);
359 }
360 }
361 }
362 Ok(())
363}
364
365fn mark_all_refs_in_repo(
366 store: &gix_ref::file::Store,
367 objects: &impl gix_object::Find,
368 graph: &mut gix_negotiate::Graph<'_, '_>,
369 queue: &mut Queue,
370 mark: Flags,
371) -> Result<(), Error> {
372 let _span = gix_trace::detail!("mark_all_refs");
373 for local_ref in store.iter()?.all()? {
374 let mut local_ref = local_ref?;
375 let id = local_ref.peel_to_id_in_place_packed(
376 store,
377 objects,
378 store.cached_packed_buffer()?.as_ref().map(|b| &***b),
379 )?;
380 let mut is_complete = false;
381 if let Some(commit) = graph
382 .get_or_insert_commit(id, |md| {
383 is_complete = md.flags.contains(Flags::COMPLETE);
384 md.flags |= mark;
385 })?
386 .filter(|_| !is_complete)
387 {
388 queue.insert(commit.commit_time, id);
389 };
390 }
391 Ok(())
392}
393
394///
395pub mod one_round {
396 /// State to keep between individual [rounds](super::one_round()).
397 #[derive(Clone, Debug)]
398 pub struct State {
399 /// The amount of haves to send the next round.
400 /// It's initialized with the standard window size for negotations.
401 pub haves_to_send: usize,
402 /// Is turned `true` if the remote as confirmed any common commit so far.
403 pub(super) seen_ack: bool,
404 /// The amount of haves we have sent that didn't have a match on the remote.
405 ///
406 /// The higher this number, the more time was wasted.
407 pub(super) in_vain: usize,
408 /// Commits we have in common.
409 ///
410 /// Only set when we are stateless as we have to resend known common commits each round.
411 pub(super) common_commits: Option<Vec<gix_hash::ObjectId>>,
412 }
413
414 impl State {
415 /// Create a new instance.
416 ///
417 /// setting `connection_is_stateless` accordingly which affects the amount of haves to send.
418 pub fn new(connection_is_stateless: bool) -> Self {
419 State {
420 haves_to_send: gix_negotiate::window_size(connection_is_stateless, None),
421 seen_ack: false,
422 in_vain: 0,
423 common_commits: connection_is_stateless.then(Vec::new),
424 }
425 }
426 }
427
428 impl State {
429 /// Return `true` if the transports connection is stateless.
430 fn connection_is_stateless(&self) -> bool {
431 self.common_commits.is_some()
432 }
433 pub(super) fn adjust_window_size(&mut self) {
434 self.haves_to_send = gix_negotiate::window_size(self.connection_is_stateless(), Some(self.haves_to_send));
435 }
436 }
437}
438
439/// Prepare to negotiate a single round in the process of letting the remote know what we have, and have in common.
440///
441/// Note that this function only configures `arguments`, no IO is performed.
442///
443/// The operation is performed with `negotiator` and `graph`, sending the amount of `haves_to_send` after possibly
444/// making the common commits (as sent by the remote) known to `negotiator` using `previous_response`, if this isn't the first round.
445/// All [commits we have](crate::fetch::Arguments::have()) are added to `arguments` accordingly.
446///
447/// Returns information about this round, and `true` if we are done and should stop negotiating *after* the `arguments` have
448/// been sent to the remote one last time.
449pub fn one_round(
450 negotiator: &mut dyn gix_negotiate::Negotiator,
451 graph: &mut gix_negotiate::Graph<'_, '_>,
452 state: &mut one_round::State,
453 arguments: &mut crate::fetch::Arguments,
454 previous_response: Option<&crate::fetch::Response>,
455) -> Result<(Round, bool), Error> {
456 let mut seen_ack = false;
457 if let Some(response) = previous_response {
458 use crate::fetch::response::Acknowledgement;
459 for ack in response.acknowledgements() {
460 match ack {
461 Acknowledgement::Common(id) => {
462 seen_ack = true;
463 negotiator.in_common_with_remote(*id, graph)?;
464 if let Some(common) = &mut state.common_commits {
465 common.push(*id);
466 }
467 }
468 Acknowledgement::Ready => {
469 // NOTE: In git, there is some logic dealing with whether to expect a DELIM or FLUSH package,
470 // but we handle this with peeking.
471 }
472 Acknowledgement::Nak => {}
473 }
474 }
475 }
476
477 // `common` is set only if this is a stateless transport, and we repeat previously confirmed common commits as HAVE, because
478 // we are not going to repeat them otherwise.
479 if let Some(common) = &mut state.common_commits {
480 for have_id in common {
481 arguments.have(have_id);
482 }
483 }
484
485 let mut haves_added = 0;
486 for have_id in (0..state.haves_to_send).map_while(|_| negotiator.next_have(graph)) {
487 arguments.have(have_id?);
488 haves_added += 1;
489 }
490 // Note that we are differing from the git implementation, which does an extra-round of with no new haves sent at all.
491 // For us, it seems better to just say we are done when we know we are done, as potentially additional acks won't affect the
492 // queue of our implementation at all (so the negotiator won't come up with more haves next time either).
493 if seen_ack {
494 state.in_vain = 0;
495 }
496 state.seen_ack |= seen_ack;
497 state.in_vain += haves_added;
498 let round = Round {
499 haves_sent: haves_added,
500 in_vain: state.in_vain,
501 haves_to_send: state.haves_to_send,
502 previous_response_had_at_least_one_in_common: seen_ack,
503 };
504 let is_done = haves_added != state.haves_to_send || (state.seen_ack && state.in_vain >= 256);
505 state.adjust_window_size();
506
507 Ok((round, is_done))
508}