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
use bstr::BString;
use gix_error::{Error, Result, ResultExt, bail, message, unsupported};
use gix_transport::Protocol;
use crate::{command::Feature, fetch::Response};
fn unknown_line(line: &str) -> Error {
message!("Encountered an unknown line prefix in {line:?}").corrupted_error()
}
/// An 'ACK' line received from the server.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Acknowledgement {
/// The contained `id` is in common.
Common(gix_hash::ObjectId),
/// The server is ready to receive more lines.
Ready,
/// The server isn't ready yet.
Nak,
}
pub use gix_shallow::Update as ShallowUpdate;
/// A wanted-ref line received from the server.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct WantedRef {
/// The object id of the wanted ref, as seen by the server.
pub id: gix_hash::ObjectId,
/// The name of the ref, as requested by the client as a `want-ref` argument.
pub path: BString,
}
/// Parse a `ShallowUpdate` from a `line` as received to the server.
pub fn shallow_update_from_line(line: &str) -> Result<ShallowUpdate> {
match line.trim_end().split_once(' ') {
Some((prefix, id)) => {
let id = gix_hash::ObjectId::from_hex(id.as_bytes())
.or_raise(|| message!("Encountered an unknown line prefix in {line:?}").corrupted())?;
Ok(match prefix {
"shallow" => ShallowUpdate::Shallow(id),
"unshallow" => ShallowUpdate::Unshallow(id),
_ => return Err(unknown_line(line)),
})
}
None => Err(unknown_line(line)),
}
}
impl Acknowledgement {
/// Parse an `Acknowledgement` from a `line` as received to the server.
pub fn from_line(line: &str) -> Result<Acknowledgement> {
let mut tokens = line.trim_end().splitn(3, ' ');
match (tokens.next(), tokens.next(), tokens.next()) {
(Some(first), id, description) => Ok(match first {
"ready" => Acknowledgement::Ready, // V2
"NAK" => Acknowledgement::Nak, // V1
"ACK" => {
let id = match id {
Some(id) => gix_hash::ObjectId::from_hex(id.as_bytes())
.or_raise(|| message!("Encountered an unknown line prefix in {line:?}").corrupted())?,
None => return Err(unknown_line(line)),
};
if let Some(description) = description {
match description {
"common" => {}
"ready" => return Ok(Acknowledgement::Ready),
_ => return Err(unknown_line(line)),
}
}
Acknowledgement::Common(id)
}
_ => return Err(unknown_line(line)),
}),
(None, _, _) => Err(unknown_line(line)),
}
}
/// Returns the hash of the acknowledged object if this instance acknowledges a common one.
pub fn id(&self) -> Option<&gix_hash::ObjectId> {
match self {
Acknowledgement::Common(id) => Some(id),
_ => None,
}
}
}
impl WantedRef {
/// Parse a `WantedRef` from a `line` as received from the server.
pub fn from_line(line: &str) -> Result<WantedRef> {
match line.trim_end().split_once(' ') {
Some((id, path)) => {
let id = gix_hash::ObjectId::from_hex(id.as_bytes())
.or_raise(|| message!("Encountered an unknown line prefix in {line:?}").corrupted())?;
Ok(WantedRef { id, path: path.into() })
}
None => Err(unknown_line(line)),
}
}
}
impl Response {
/// Return true if the response has a pack which can be read next.
pub fn has_pack(&self) -> bool {
self.has_pack
}
/// Return an error if the given `features` don't contain the required ones (the ones this implementation needs)
/// for the given `version` of the protocol.
///
/// Even though technically any set of features supported by the server could work, we only implement the ones that
/// make it easy to maintain all versions with a single code base that aims to be and remain maintainable.
pub fn check_required_features(version: Protocol, features: &[Feature]) -> Result {
match version {
Protocol::V0 | Protocol::V1 => {
let has = |name: &str| features.iter().any(|f| f.0 == name);
// Let's focus on V2 standards, and simply not support old servers to keep our code simpler
if !has("multi_ack_detailed") {
bail!(unsupported(
"Currently we require feature \"multi_ack_detailed\", which is not supported by the server",
));
}
// It's easy to NOT do sideband for us, but then again, everyone supports it.
// CORRECTION: If sideband is off, it would send the packfile without packet line encoding,
// which is nothing we ever want to deal with (despite it being more efficient). In V2, this
// is not even an option anymore, sidebands are always present.
if !has("side-band") && !has("side-band-64k") {
bail!(unsupported(
"Currently we require feature \"side-band OR side-band-64k\", which is not supported by the server"
));
}
}
Protocol::V2 => {}
}
Ok(())
}
/// Return all acknowledgements [parsed previously][Response::from_line_reader()].
pub fn acknowledgements(&self) -> &[Acknowledgement] {
&self.acks
}
/// Return all shallow update lines [parsed previously][Response::from_line_reader()].
pub fn shallow_updates(&self) -> &[ShallowUpdate] {
&self.shallows
}
/// Append the given `updates` which may have been obtained from a
/// (handshake::Outcome)[crate::Handshake::v1_shallow_updates].
///
/// In V2, these are received as part of the pack, but V1 sends them early, so we
/// offer to re-integrate them here.
pub fn append_v1_shallow_updates(&mut self, updates: Option<Vec<ShallowUpdate>>) {
self.shallows.extend(updates.into_iter().flatten());
}
/// Return all wanted-refs [parsed previously][Response::from_line_reader()].
pub fn wanted_refs(&self) -> &[WantedRef] {
&self.wanted_refs
}
}
#[cfg(any(feature = "async-client", feature = "blocking-client"))]
impl Response {
/// with a friendly server, we just assume that a non-ack line is a pack line
/// which is our hint to stop here.
fn parse_v1_ack_or_shallow_or_assume_pack(
acks: &mut Vec<Acknowledgement>,
shallows: &mut Vec<ShallowUpdate>,
peeked_line: &str,
) -> bool {
match Acknowledgement::from_line(peeked_line) {
Ok(ack) => match ack.id() {
Some(id) => {
if !acks.iter().any(|a| a.id() == Some(id)) {
acks.push(ack);
}
}
None => acks.push(ack),
},
Err(_) => match shallow_update_from_line(peeked_line) {
Ok(shallow) => {
shallows.push(shallow);
}
Err(_) => return true,
},
}
false
}
}
#[cfg(any(feature = "async-client", feature = "blocking-client"))]
mod io;