gix_protocol/fetch/response/
mod.rs1use bstr::BString;
2use gix_error::{Error, Result, ResultExt, bail, message, unsupported};
3use gix_transport::Protocol;
4
5use crate::{command::Feature, fetch::Response};
6
7fn unknown_line(line: &str) -> Error {
8 message!("Encountered an unknown line prefix in {line:?}").corrupted_error()
9}
10
11#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub enum Acknowledgement {
15 Common(gix_hash::ObjectId),
17 Ready,
19 Nak,
21}
22
23pub use gix_shallow::Update as ShallowUpdate;
24
25#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub struct WantedRef {
29 pub id: gix_hash::ObjectId,
31 pub path: BString,
33}
34
35pub fn shallow_update_from_line(line: &str) -> Result<ShallowUpdate> {
37 match line.trim_end().split_once(' ') {
38 Some((prefix, id)) => {
39 let id = gix_hash::ObjectId::from_hex(id.as_bytes())
40 .or_raise(|| message!("Encountered an unknown line prefix in {line:?}").corrupted())?;
41 Ok(match prefix {
42 "shallow" => ShallowUpdate::Shallow(id),
43 "unshallow" => ShallowUpdate::Unshallow(id),
44 _ => return Err(unknown_line(line)),
45 })
46 }
47 None => Err(unknown_line(line)),
48 }
49}
50
51impl Acknowledgement {
52 pub fn from_line(line: &str) -> Result<Acknowledgement> {
54 let mut tokens = line.trim_end().splitn(3, ' ');
55 match (tokens.next(), tokens.next(), tokens.next()) {
56 (Some(first), id, description) => Ok(match first {
57 "ready" => Acknowledgement::Ready, "NAK" => Acknowledgement::Nak, "ACK" => {
60 let id = match id {
61 Some(id) => gix_hash::ObjectId::from_hex(id.as_bytes())
62 .or_raise(|| message!("Encountered an unknown line prefix in {line:?}").corrupted())?,
63 None => return Err(unknown_line(line)),
64 };
65 if let Some(description) = description {
66 match description {
67 "common" => {}
68 "ready" => return Ok(Acknowledgement::Ready),
69 _ => return Err(unknown_line(line)),
70 }
71 }
72 Acknowledgement::Common(id)
73 }
74 _ => return Err(unknown_line(line)),
75 }),
76 (None, _, _) => Err(unknown_line(line)),
77 }
78 }
79 pub fn id(&self) -> Option<&gix_hash::ObjectId> {
81 match self {
82 Acknowledgement::Common(id) => Some(id),
83 _ => None,
84 }
85 }
86}
87
88impl WantedRef {
89 pub fn from_line(line: &str) -> Result<WantedRef> {
91 match line.trim_end().split_once(' ') {
92 Some((id, path)) => {
93 let id = gix_hash::ObjectId::from_hex(id.as_bytes())
94 .or_raise(|| message!("Encountered an unknown line prefix in {line:?}").corrupted())?;
95 Ok(WantedRef { id, path: path.into() })
96 }
97 None => Err(unknown_line(line)),
98 }
99 }
100}
101
102impl Response {
103 pub fn has_pack(&self) -> bool {
105 self.has_pack
106 }
107
108 pub fn check_required_features(version: Protocol, features: &[Feature]) -> Result {
114 match version {
115 Protocol::V0 | Protocol::V1 => {
116 let has = |name: &str| features.iter().any(|f| f.0 == name);
117 if !has("multi_ack_detailed") {
119 bail!(unsupported(
120 "Currently we require feature \"multi_ack_detailed\", which is not supported by the server",
121 ));
122 }
123 if !has("side-band") && !has("side-band-64k") {
128 bail!(unsupported(
129 "Currently we require feature \"side-band OR side-band-64k\", which is not supported by the server"
130 ));
131 }
132 }
133 Protocol::V2 => {}
134 }
135 Ok(())
136 }
137
138 pub fn acknowledgements(&self) -> &[Acknowledgement] {
140 &self.acks
141 }
142
143 pub fn shallow_updates(&self) -> &[ShallowUpdate] {
145 &self.shallows
146 }
147
148 pub fn append_v1_shallow_updates(&mut self, updates: Option<Vec<ShallowUpdate>>) {
154 self.shallows.extend(updates.into_iter().flatten());
155 }
156
157 pub fn wanted_refs(&self) -> &[WantedRef] {
159 &self.wanted_refs
160 }
161}
162
163#[cfg(any(feature = "async-client", feature = "blocking-client"))]
164impl Response {
165 fn parse_v1_ack_or_shallow_or_assume_pack(
168 acks: &mut Vec<Acknowledgement>,
169 shallows: &mut Vec<ShallowUpdate>,
170 peeked_line: &str,
171 ) -> bool {
172 match Acknowledgement::from_line(peeked_line) {
173 Ok(ack) => match ack.id() {
174 Some(id) => {
175 if !acks.iter().any(|a| a.id() == Some(id)) {
176 acks.push(ack);
177 }
178 }
179 None => acks.push(ack),
180 },
181 Err(_) => match shallow_update_from_line(peeked_line) {
182 Ok(shallow) => {
183 shallows.push(shallow);
184 }
185 Err(_) => return true,
186 },
187 }
188 false
189 }
190}
191
192#[cfg(any(feature = "async-client", feature = "blocking-client"))]
193mod io;