Skip to content

Commit 4e46719

Browse files
committed
[protocol] suppot for V2 special lines
1 parent 3f4fd90 commit 4e46719

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

git-protocol/src/packet_line/decode.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::packet_line::{self, ERR_PREFIX, FLUSH_LINE, MAX_DATA_LEN, MAX_LINE_LEN, U16_HEX_BYTES};
1+
use crate::packet_line::{
2+
self, DELIMITER_LINE, ERR_PREFIX, FLUSH_LINE, MAX_DATA_LEN, MAX_LINE_LEN, RESPONSE_END_LINE, U16_HEX_BYTES,
3+
};
24
use bstr::BString;
35
use quick_error::quick_error;
46

@@ -48,6 +50,18 @@ pub fn streaming(data: &[u8]) -> Result<Stream, Error> {
4850
bytes_consumed: 4,
4951
});
5052
}
53+
if hex_bytes == DELIMITER_LINE {
54+
return Ok(Stream::Complete {
55+
line: packet_line::Borrowed::Delimiter,
56+
bytes_consumed: 4,
57+
});
58+
}
59+
if hex_bytes == RESPONSE_END_LINE {
60+
return Ok(Stream::Complete {
61+
line: packet_line::Borrowed::ResponseEnd,
62+
bytes_consumed: 4,
63+
});
64+
}
5165

5266
let mut buf = [0u8; U16_HEX_BYTES / 2];
5367
hex::decode_to_slice(hex_bytes, &mut buf)?;

git-protocol/src/packet_line/encode.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::packet_line::{ERR_PREFIX, FLUSH_LINE, MAX_DATA_LEN};
1+
use crate::packet_line::{DELIMITER_LINE, ERR_PREFIX, FLUSH_LINE, MAX_DATA_LEN, RESPONSE_END_LINE};
22
use quick_error::quick_error;
33
use std::io;
44

@@ -19,6 +19,14 @@ quick_error! {
1919
}
2020
}
2121

22+
pub fn response_end_to_write(mut out: impl io::Write) -> io::Result<usize> {
23+
out.write_all(RESPONSE_END_LINE).map(|_| 4)
24+
}
25+
26+
pub fn delim_to_write(mut out: impl io::Write) -> io::Result<usize> {
27+
out.write_all(DELIMITER_LINE).map(|_| 4)
28+
}
29+
2230
pub fn flush_to_write(mut out: impl io::Write) -> io::Result<usize> {
2331
out.write_all(FLUSH_LINE).map(|_| 4)
2432
}

git-protocol/src/packet_line/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,33 @@ 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 DELIMITER_LINE: &[u8] = b"0001";
9+
pub(crate) const RESPONSE_END_LINE: &[u8] = b"0002";
810
pub(crate) const ERR_PREFIX: &[u8] = b"ERR ";
911

1012
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
1113
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
1214
pub enum Borrowed<'a> {
1315
Data(&'a [u8]),
1416
Flush,
17+
Delimiter,
18+
ResponseEnd,
1519
}
1620

1721
impl<'a> Borrowed<'a> {
1822
pub fn to_write(&self, out: impl io::Write) -> Result<usize, encode::Error> {
1923
match self {
20-
Borrowed::Flush => encode::flush_to_write(out).map_err(Into::into),
2124
Borrowed::Data(d) => encode::data_to_write(d, out),
25+
Borrowed::Flush => encode::flush_to_write(out).map_err(Into::into),
26+
Borrowed::Delimiter => encode::delim_to_write(out).map_err(Into::into),
27+
Borrowed::ResponseEnd => encode::response_end_to_write(out).map_err(Into::into),
2228
}
2329
}
2430

2531
pub fn as_slice(&self) -> &[u8] {
2632
match self {
2733
Borrowed::Data(d) => d,
28-
Borrowed::Flush => &[],
34+
Borrowed::Flush | Borrowed::Delimiter | Borrowed::ResponseEnd => &[],
2935
}
3036
}
3137
pub fn as_bstr(&self) -> &BStr {

git-protocol/tests/packet_line/decode.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ mod streaming {
6666

6767
#[test]
6868
fn round_trips() -> crate::Result {
69-
for (line, bytes) in &[(PacketLine::Flush, 4), (PacketLine::Data(b"hello there"), 15)] {
69+
for (line, bytes) in &[
70+
(PacketLine::ResponseEnd, 4),
71+
(PacketLine::Delimiter, 4),
72+
(PacketLine::Flush, 4),
73+
(PacketLine::Data(b"hello there"), 15),
74+
] {
7075
let mut out = Vec::new();
7176
line.to_write(&mut out)?;
7277
assert_complete(streaming(&out), *bytes, *line)?;

git-protocol/tests/packet_line/encode.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
mod data_to_write {
22
use crate::packet_line::assert_err_display;
33
use bstr::ByteSlice;
4-
use git_protocol::packet_line::encode::{data_to_write, error_to_write, flush_to_write};
4+
use git_protocol::packet_line::encode::{
5+
data_to_write, delim_to_write, error_to_write, flush_to_write, response_end_to_write,
6+
};
57
use std::io;
68

79
fn vec_sized(size: usize) -> Vec<u8> {
@@ -23,10 +25,18 @@ mod data_to_write {
2325
}
2426

2527
#[test]
26-
fn success_flush() -> crate::Result {
28+
fn success_flush_delim_response_end() -> crate::Result {
2729
let mut out = Vec::new();
2830
assert_eq!(flush_to_write(&mut out)?, 4);
2931
assert_eq!(out.as_bstr(), b"0000".as_bstr());
32+
33+
out.clear();
34+
assert_eq!(delim_to_write(&mut out)?, 4);
35+
assert_eq!(out.as_bstr(), b"0001".as_bstr());
36+
37+
out.clear();
38+
assert_eq!(response_end_to_write(&mut out)?, 4);
39+
assert_eq!(out.as_bstr(), b"0002".as_bstr());
3040
Ok(())
3141
}
3242

0 commit comments

Comments
 (0)