| 1 | //! An implementation of negotiation algorithms to help the server figure out what we have in common so it can optimize |
| 2 | //! the pack it sends to only contain what we don't have. |
| 3 | #![deny(rust_2018_idioms, missing_docs)] |
| 4 | #![forbid(unsafe_code)] |
| 5 | mod consecutiverust-analyzer cargo gix-negotiate 0.31.0 consecutive/; |
| 6 | mod nooprust-analyzer cargo gix-negotiate 0.31.0 noop/; |
| 7 | mod skippingrust-analyzer cargo gix-negotiate 0.31.0 skipping/; |
| 8 | |
| 9 | bitflagsrust-analyzer cargo bitflags 2.11.1 crate/::bitflagsrust-analyzer cargo bitflags 2.11.1 bitflags!! { |
| 10 | /// Multi purpose, shared flags that are used by negotiation algorithms and by the caller as well |
| 11 | /// |
| 12 | /// However, in this crate we can't implement the calling side, so we marry this type to whatever is needed in downstream crates. |
| 13 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 14 | pub struct Flagsrust-analyzer cargo gix-negotiate 0.31.0 Flags#: u8 { |
| 15 | /// The object is already available locally and doesn't need to be fetched by the remote. |
| 16 | const COMPLETErust-analyzer cargo gix-negotiate 0.31.0 impl#[Flags]COMPLETE. = 1 << 0; |
| 17 | /// A commit from an alternate object database. |
| 18 | const ALTERNATErust-analyzer cargo gix-negotiate 0.31.0 impl#[Flags]ALTERNATE. = 1 << 1; |
| 19 | |
| 20 | /// The revision is known to be in common with the remote. |
| 21 | /// |
| 22 | /// Used by `consecutive` and `skipping`. |
| 23 | const COMMONrust-analyzer cargo gix-negotiate 0.31.0 impl#[Flags]COMMON. = 1 << 2; |
| 24 | /// The revision has entered the priority queue. |
| 25 | /// |
| 26 | /// Used by `consecutive` and `skipping`. |
| 27 | const SEENrust-analyzer cargo gix-negotiate 0.31.0 impl#[Flags]SEEN. = 1 << 3; |
| 28 | /// The revision was popped off our primary priority queue, used to avoid double-counting of `non_common_revs` |
| 29 | /// |
| 30 | /// Used by `consecutive` and `skipping`. |
| 31 | const POPPEDrust-analyzer cargo gix-negotiate 0.31.0 impl#[Flags]POPPED. = 1 << 4; |
| 32 | |
| 33 | /// The revision is common and was set by merit of a remote tracking ref (e.g. `refs/heads/origin/main`). |
| 34 | /// |
| 35 | /// Used by `consecutive`. |
| 36 | const COMMON_REFrust-analyzer cargo gix-negotiate 0.31.0 impl#[Flags]COMMON_REF. = 1 << 5; |
| 37 | |
| 38 | /// The remote let us know it has the object. We still have to tell the server we have this object or one of its descendants. |
| 39 | /// We won't tell the server about its ancestors. |
| 40 | /// |
| 41 | /// Used by `skipping`. |
| 42 | const ADVERTISEDrust-analyzer cargo gix-negotiate 0.31.0 impl#[Flags]ADVERTISED. = 1 << 6; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Additional data to store with each commit when used by any of our algorithms. |
| 47 | /// |
| 48 | /// It's shared among those who use the [`Negotiator`] trait, and all implementations of it. |
| 49 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 50 | pub struct Metadatarust-analyzer cargo gix-negotiate 0.31.0 Metadata# { |
| 51 | /// Used by `skipping`. |
| 52 | /// Only used if commit is not COMMON |
| 53 | pub original_ttlrust-analyzer cargo gix-negotiate 0.31.0 Metadata#original_ttl.: u16, |
| 54 | /// Used by `skipping`. |
| 55 | pub ttlrust-analyzer cargo gix-negotiate 0.31.0 Metadata#ttl.: u16, |
| 56 | /// Additional information about each commit |
| 57 | pub flagsrust-analyzer cargo gix-negotiate 0.31.0 Metadata#flags.: Flagsrust-analyzer cargo gix-negotiate 0.31.0 Flags#, |
| 58 | } |
| 59 | |
| 60 | /// The graph our callers use to store traversal information, for (re-)use in the negotiation implementation. |
| 61 | pub type Graphrust-analyzer cargo gix-negotiate 0.31.0 Graph#<'find, 'cache> = gix_revwalkrust-analyzer cargo gix-revwalk 0.31.0 crate/::Graphrust-analyzer cargo gix-revwalk 0.31.0 Graph#<'find, 'cache, gix_revwalkrust-analyzer cargo gix-revwalk 0.31.0 crate/::graphrust-analyzer cargo gix-revwalk 0.31.0 graph/::Commitrust-analyzer cargo gix-revwalk 0.31.0 graph/Commit#<Metadatarust-analyzer cargo gix-negotiate 0.31.0 Metadata#>>; |
| 62 | |
| 63 | /// A map associating an object id with its commit-metadata. |
| 64 | pub type IdMaprust-analyzer cargo gix-negotiate 0.31.0 IdMap# = gix_revwalkrust-analyzer cargo gix-revwalk 0.31.0 crate/::graphrust-analyzer cargo gix-revwalk 0.31.0 graph/::IdMaprust-analyzer cargo gix-revwalk 0.31.0 graph/IdMap#<gix_revwalkrust-analyzer cargo gix-revwalk 0.31.0 crate/::graphrust-analyzer cargo gix-revwalk 0.31.0 graph/::Commitrust-analyzer cargo gix-revwalk 0.31.0 graph/Commit#<Metadatarust-analyzer cargo gix-negotiate 0.31.0 Metadata#>>; |
| 65 | |
| 66 | /// The way the negotiation is performed. |
| 67 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 68 | pub enum Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm# { |
| 69 | /// Do not send any information at all, which typically leads to complete packs to be sent. |
| 70 | Nooprust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Noop#, |
| 71 | /// Walk over consecutive commits and check each one. This can be costly be assures packs are exactly the size they need to be. |
| 72 | #[defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/default:] |
| 73 | Consecutiverust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Consecutive#, |
| 74 | /// Like `Consecutive`, but skips commits to converge faster, at the cost of receiving packs that are larger than they have to be. |
| 75 | Skippingrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Skipping#, |
| 76 | } |
| 77 | |
| 78 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm# { |
| 79 | fn fmtrust-analyzer cargo gix-negotiate 0.31.0 impl#[Algorithm][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 80 | match self { |
| 81 | Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#::Nooprust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Noop# => "noop", |
| 82 | Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#::Consecutiverust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Consecutive# => "consecutive", |
| 83 | Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#::Skippingrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Skipping# => "skipping", |
| 84 | } |
| 85 | .fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[str][Display]fmt().(f) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Calculate how many `HAVE` lines we may send in one round, with variation depending on whether the `transport_is_stateless` or not. |
| 90 | /// `window_size` is the previous (or initial) value of the window size. |
| 91 | pub fn window_sizerust-analyzer cargo gix-negotiate 0.31.0 window_size().(transport_is_stateless: bool, window_size: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) -> usize { |
| 92 | let current_size = match window_size { |
| 93 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => return 16, |
| 94 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(cs) => cs, |
| 95 | }; |
| 96 | const PIPESAFE_FLUSHrust-analyzer cargo gix-negotiate 0.31.0 PIPESAFE_FLUSH.: usize = 32; |
| 97 | const LARGE_FLUSHrust-analyzer cargo gix-negotiate 0.31.0 LARGE_FLUSH.: usize = 16384; |
| 98 | |
| 99 | if transport_is_stateless { |
| 100 | if current_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().LARGE_FLUSHrust-analyzer cargo gix-negotiate 0.31.0 LARGE_FLUSH. { |
| 101 | current_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2 |
| 102 | } else { |
| 103 | current_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().11 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().10 |
| 104 | } |
| 105 | } else if current_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().PIPESAFE_FLUSHrust-analyzer cargo gix-negotiate 0.31.0 PIPESAFE_FLUSH. { |
| 106 | current_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().2 |
| 107 | } else { |
| 108 | current_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().PIPESAFE_FLUSHrust-analyzer cargo gix-negotiate 0.31.0 PIPESAFE_FLUSH. |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | impl Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm# { |
| 113 | /// Create an instance of a negotiator which implements this algorithm. |
| 114 | pub fn into_negotiatorrust-analyzer cargo gix-negotiate 0.31.0 impl#[Algorithm]into_negotiator().(self) -> Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Negotiatorrust-analyzer cargo gix-negotiate 0.31.0 Negotiator#> { |
| 115 | match &self { |
| 116 | Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#::Nooprust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Noop# => Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(nooprust-analyzer cargo gix-negotiate 0.31.0 noop/::Nooprust-analyzer cargo gix-negotiate 0.31.0 noop/Noop#) as Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Negotiatorrust-analyzer cargo gix-negotiate 0.31.0 Negotiator#>, |
| 117 | Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#::Consecutiverust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Consecutive# => Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::<consecutiverust-analyzer cargo gix-negotiate 0.31.0 consecutive/::Algorithmrust-analyzer cargo gix-negotiate 0.31.0 consecutive/Algorithm#>::defaultrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`][Default]default().(), |
| 118 | Algorithmrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#::Skippingrust-analyzer cargo gix-negotiate 0.31.0 Algorithm#Skipping# => Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::<skippingrust-analyzer cargo gix-negotiate 0.31.0 skipping/::Algorithmrust-analyzer cargo gix-negotiate 0.31.0 skipping/Algorithm#>::defaultrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`][Default]default().(), |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /// A delegate to implement a negotiation algorithm. |
| 124 | pub trait Negotiatorrust-analyzer cargo gix-negotiate 0.31.0 Negotiator# { |
| 125 | /// Mark `id` as common between the remote and us. |
| 126 | /// |
| 127 | /// These ids are typically the local tips of remote tracking branches. |
| 128 | fn known_commonrust-analyzer cargo gix-negotiate 0.31.0 Negotiator#known_common().(&mut self, id: gix_hashrust-analyzer cargo gix-hash 0.25.0 crate/::ObjectIdrust-analyzer cargo gix-hash 0.25.0 object_id/ObjectId#, graph: &mut Graphrust-analyzer cargo gix-negotiate 0.31.0 Graph#<'_, '_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo gix-negotiate 0.31.0 Error#>; |
| 129 | |
| 130 | /// Add `id` as starting point of a traversal across commits that aren't necessarily common between the remote and us. |
| 131 | /// |
| 132 | /// These tips are usually the commits of local references whose tips should lead to objects that we have in common with the remote. |
| 133 | fn add_tiprust-analyzer cargo gix-negotiate 0.31.0 Negotiator#add_tip().(&mut self, id: gix_hashrust-analyzer cargo gix-hash 0.25.0 crate/::ObjectIdrust-analyzer cargo gix-hash 0.25.0 object_id/ObjectId#, graph: &mut Graphrust-analyzer cargo gix-negotiate 0.31.0 Graph#<'_, '_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo gix-negotiate 0.31.0 Error#>; |
| 134 | |
| 135 | /// Produce the next id of an object that we want the server to know we have. It's an object we don't know we have in common or not. |
| 136 | /// |
| 137 | /// Returns `None` if we have exhausted all options, which might mean we have traversed the entire commit graph. |
| 138 | fn next_haverust-analyzer cargo gix-negotiate 0.31.0 Negotiator#next_have().(&mut self, graph: &mut Graphrust-analyzer cargo gix-negotiate 0.31.0 Graph#<'_, '_>) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<gix_hashrust-analyzer cargo gix-hash 0.25.0 crate/::ObjectIdrust-analyzer cargo gix-hash 0.25.0 object_id/ObjectId#, Errorrust-analyzer cargo gix-negotiate 0.31.0 Error#>>; |
| 139 | |
| 140 | /// Mark `id` as being common with the remote (as informed by the remote itself) and return `true` if we knew it was common already. |
| 141 | /// |
| 142 | /// We can assume to have already seen `id` as we were the one to inform the remote in a prior `have`. |
| 143 | fn in_common_with_remoterust-analyzer cargo gix-negotiate 0.31.0 Negotiator#in_common_with_remote().(&mut self, id: gix_hashrust-analyzer cargo gix-hash 0.25.0 crate/::ObjectIdrust-analyzer cargo gix-hash 0.25.0 object_id/ObjectId#, graph: &mut Graphrust-analyzer cargo gix-negotiate 0.31.0 Graph#<'_, '_>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, Errorrust-analyzer cargo gix-negotiate 0.31.0 Error#>; |
| 144 | } |
| 145 | |
| 146 | /// An error that happened during any of the methods on a [`Negotiator`]. |
| 147 | pub type Errorrust-analyzer cargo gix-negotiate 0.31.0 Error# = gix_revwalkrust-analyzer cargo gix-revwalk 0.31.0 crate/::graphrust-analyzer cargo gix-revwalk 0.31.0 graph/::get_or_insert_defaultrust-analyzer cargo gix-revwalk 0.31.0 graph/errors/get_or_insert_default/::Errorrust-analyzer cargo gix-revwalk 0.31.0 graph/errors/get_or_insert_default/Error#; |