Skip to content

Commit 5e9659c

Browse files
committed
feat: introduce the noop negotiator to establish a basic trait for negotiators.
1 parent 92832ca commit 5e9659c

File tree

5 files changed

+73
-15
lines changed

5 files changed

+73
-15
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-negotiate/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@ doctest = false
1313
test = false
1414

1515
[dependencies]
16+
gix-hash = { version = "^0.11.1", path = "../gix-hash" }
17+
gix-object = { version = "^0.29.2", path = "../gix-object" }
18+
gix-commitgraph = { version = "^0.15.0", path = "../gix-commitgraph" }
1619
gix-revision = { version = "^0.14.0", path = "../gix-revision" }

gix-negotiate/src/consecutive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// TODO: make this the actual bitflags
2+
pub(crate) type Flags = u32;

gix-negotiate/src/lib.rs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#![deny(rust_2018_idioms, missing_docs)]
44
#![forbid(unsafe_code)]
55

6+
mod consecutive;
7+
mod noop;
8+
69
/// The way the negotiation is performed.
710
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
811
pub enum Algorithm {
@@ -15,21 +18,6 @@ pub enum Algorithm {
1518
Skipping,
1619
}
1720

18-
// static int next_flush(int stateless_rpc, int count)
19-
// {
20-
// if (stateless_rpc) {
21-
// if (count < LARGE_FLUSH)
22-
// count <<= 1;
23-
// else
24-
// count = count * 11 / 10;
25-
// } else {
26-
// if (count < PIPESAFE_FLUSH)
27-
// count <<= 1;
28-
// else
29-
// count += PIPESAFE_FLUSH;
30-
// }
31-
// return count;
32-
// }
3321
/// Calculate how many `HAVE` lines we may send in one round, with variation depending on whether the `transport_is_stateless` or not.
3422
/// `window_size` is the previous (or initial) value of the window size.
3523
pub fn window_size(transport_is_stateless: bool, window_size: impl Into<Option<usize>>) -> usize {
@@ -54,3 +42,47 @@ pub fn window_size(transport_is_stateless: bool, window_size: impl Into<Option<u
5442
}
5543
}
5644
}
45+
46+
impl Algorithm {
47+
/// Create an instance of a negotiator which implements this algorithm.
48+
pub fn into_negotiator<'find, Find, E>(
49+
self,
50+
find: Find,
51+
cache: impl Into<Option<gix_commitgraph::Graph>>,
52+
) -> Box<dyn Negotiator>
53+
where
54+
Find:
55+
for<'a> FnMut(&gix_hash::oid, &'a mut Vec<u8>) -> Result<Option<gix_object::CommitRefIter<'a>>, E> + 'find,
56+
E: std::error::Error + Send + Sync + 'static,
57+
{
58+
match &self {
59+
Algorithm::Noop => Box::new(noop::Noop) as Box<dyn Negotiator>,
60+
Algorithm::Consecutive => {
61+
let _graph = gix_revision::Graph::<'_, consecutive::Flags>::new(find, cache);
62+
todo!()
63+
}
64+
Algorithm::Skipping => todo!(),
65+
}
66+
}
67+
}
68+
69+
/// A delegate to implement a negotiation algorithm.
70+
pub trait Negotiator {
71+
/// Mark `id` as common between the remote and us.
72+
///
73+
/// These ids are typically the local tips of remote tracking branches.
74+
fn known_common(&mut self, id: &gix_hash::oid);
75+
76+
/// Add `id` as starting point of a traversal across commits that aren't necessarily common between the remote and us.
77+
///
78+
/// These tips are usually the commits of local references whose tips should lead to objects that we have in common with the remote.
79+
fn add_tip(&mut self, id: &gix_hash::oid);
80+
81+
/// 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.
82+
///
83+
/// Returns `None` if we have exhausted all options, which might mean we have traversed the entire commit graph.
84+
fn next_have(&mut self) -> Option<gix_hash::ObjectId>;
85+
86+
/// Mark `id` as being common with the remote (as informed by the remote itself) and return `true` if we knew it was common already.
87+
fn in_common_with_remote(&mut self, id: &gix_hash::oid) -> bool;
88+
}

gix-negotiate/src/noop.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::Negotiator;
2+
use gix_hash::{oid, ObjectId};
3+
4+
pub(crate) struct Noop;
5+
6+
impl Negotiator for Noop {
7+
fn known_common(&mut self, _id: &oid) {}
8+
9+
fn add_tip(&mut self, _id: &oid) {}
10+
11+
fn next_have(&mut self) -> Option<ObjectId> {
12+
None
13+
}
14+
15+
fn in_common_with_remote(&mut self, _id: &oid) -> bool {
16+
false
17+
}
18+
}

0 commit comments

Comments
 (0)