Skip to content

Commit 1f58568

Browse files
committed
decode ERR lines as actual errors
1 parent c34d88b commit 1f58568

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

git-protocol/src/packet_line/decode.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::packet_line::{self, FLUSH_LINE, MAX_DATA_LEN, MAX_LINE_LEN, U16_HEX_BYTES};
1+
use crate::packet_line::{self, ERR_PREFIX, FLUSH_LINE, MAX_DATA_LEN, MAX_LINE_LEN, U16_HEX_BYTES};
22
use bstr::BString;
33
use quick_error::quick_error;
44

@@ -16,7 +16,7 @@ quick_error! {
1616
DataIsEmpty {
1717
display("Received an invalid empty line")
1818
}
19-
Line(data: BString) {
19+
Line(data: BString, bytes_consumed: usize) {
2020
display("{}", data)
2121
}
2222
}
@@ -65,11 +65,15 @@ pub fn streaming(data: &[u8]) -> Result<Stream, Error> {
6565
return Err(Error::DataIsEmpty);
6666
}
6767

68-
// todo: error line
6968
let mut data = &data[U16_HEX_BYTES..wanted_bytes];
7069
if data[data.len() - 1] == b'\n' {
7170
data = &data[..data.len() - 1];
7271
}
72+
73+
if data.len() >= ERR_PREFIX.len() && &data[..ERR_PREFIX.len()] == ERR_PREFIX {
74+
return Err(Error::Line(data[ERR_PREFIX.len()..].into(), wanted_bytes));
75+
}
76+
7377
Ok(Stream::Complete {
7478
line: packet_line::Borrowed::Data(data),
7579
bytes_consumed: wanted_bytes,

git-protocol/src/packet_line/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub(crate) const U16_HEX_BYTES: usize = 4;
55
pub(crate) const MAX_DATA_LEN: usize = 65516;
66
pub(crate) const MAX_LINE_LEN: usize = MAX_DATA_LEN + U16_HEX_BYTES;
77
pub(crate) const FLUSH_LINE: &[u8] = b"0000";
8+
pub(crate) const ERR_PREFIX: &[u8] = b"ERR ";
89

910
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
1011
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]

git-protocol/tests/packet_line/decode.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ mod streaming {
3030
assert_complete(streaming(b"0006a\n"), 6, PacketLine::Data(b"a"))
3131
}
3232

33+
#[test]
34+
fn ignore_extra_bytes() -> crate::Result {
35+
assert_complete(streaming(b"0006a\nhello"), 6, PacketLine::Data(b"a"))
36+
}
37+
3338
#[test]
3439
fn error_on_oversized_line() {
3540
assert_err_display(
@@ -38,6 +43,14 @@ mod streaming {
3843
);
3944
}
4045

46+
#[test]
47+
fn error_on_error_line() {
48+
assert_err_display(
49+
streaming(b"0011ERR the error-and just ignored because not part of the size"),
50+
"the error",
51+
);
52+
}
53+
4154
#[test]
4255
fn error_on_invalid_hex() {
4356
assert_err_display(

0 commit comments

Comments
 (0)