Skip to content

Commit

Permalink
[ref-ls] Allow multiple delimiters at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Sep 8, 2020
1 parent 549f404 commit cfae63a
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 18 deletions.
15 changes: 8 additions & 7 deletions git-packetline/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ pub struct Provider<T> {
peek_buf: Vec<u8>,
fail_on_err_lines: bool,
buf: Vec<u8>,
delimiter: PacketLine<'static>,
delimiters: Vec<PacketLine<'static>>,
is_done: bool,
}

impl<T> Provider<T>
where
T: io::Read,
{
pub fn new(inner: T, delimiter: PacketLine<'static>) -> Self {
pub fn new(inner: T, delimiters: impl IntoIterator<Item = PacketLine<'static>>) -> Self {
Provider {
inner,
buf: vec![0; MAX_LINE_LEN],
peek_buf: Vec::new(),
delimiter,
delimiters: delimiters.into_iter().collect(),
fail_on_err_lines: false,
is_done: false,
}
Expand All @@ -40,11 +40,12 @@ where
}

pub fn reset(&mut self) {
self.reset_with(self.delimiter);
let delimiters = std::mem::take(&mut self.delimiters);
self.reset_with(delimiters);
}

pub fn reset_with(&mut self, delimiter: PacketLine<'static>) {
self.delimiter = delimiter;
pub fn reset_with(&mut self, delimiters: impl IntoIterator<Item = PacketLine<'static>>) {
self.delimiters = delimiters.into_iter().collect();
self.fail_on_err_lines = false;
self.is_done = false;
}
Expand Down Expand Up @@ -83,7 +84,7 @@ where
}
match Self::read_line_inner(&mut self.inner, &mut self.buf) {
Ok(Ok(line)) => {
if line == self.delimiter {
if self.delimiters.contains(&line) {
self.is_done = true;
None
} else if self.fail_on_err_lines {
Expand Down
12 changes: 6 additions & 6 deletions git-packetline/tests/packet_line/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ fn first_line() -> PacketLine<'static> {
}
#[test]
fn peek_non_data() -> crate::Result {
let mut rd = git_packetline::Provider::new(&b"000000010002"[..], PacketLine::ResponseEnd);
let mut rd = git_packetline::Provider::new(&b"000000010002"[..], Some(PacketLine::ResponseEnd));
assert_eq!(rd.read_line().expect("line")??, PacketLine::Flush);
assert_eq!(rd.read_line().expect("line")??, PacketLine::Delimiter);
rd.reset_with(PacketLine::Flush);
rd.reset_with(Some(PacketLine::Flush));
assert_eq!(rd.read_line().expect("line")??, PacketLine::ResponseEnd);
for _ in 0..2 {
assert_eq!(
Expand All @@ -35,15 +35,15 @@ fn peek_non_data() -> crate::Result {
#[test]
fn fail_on_err_lines() -> crate::Result {
let input = b"00010009ERR e0002";
let mut rd = git_packetline::Provider::new(&input[..], PacketLine::Flush);
let mut rd = git_packetline::Provider::new(&input[..], Some(PacketLine::Flush));
assert_eq!(rd.read_line().expect("line")??, PacketLine::Delimiter);
assert_eq!(
rd.read_line().expect("line")??.as_bstr(),
Some(b"ERR e".as_bstr()),
"by default no special handling"
);

let mut rd = git_packetline::Provider::new(&input[..], PacketLine::Flush);
let mut rd = git_packetline::Provider::new(&input[..], Some(PacketLine::Flush));
rd.fail_on_err_lines(true);
assert_eq!(rd.read_line().expect("line")??, PacketLine::Delimiter);
assert_eq!(
Expand All @@ -67,7 +67,7 @@ fn fail_on_err_lines() -> crate::Result {
#[test]
fn peek() -> crate::Result {
let bytes = fixture_bytes("v1/fetch/01-many-refs.response");
let mut rd = git_packetline::Provider::new(&bytes[..], PacketLine::Flush);
let mut rd = git_packetline::Provider::new(&bytes[..], Some(PacketLine::Flush));
assert_eq!(rd.peek_line().expect("line")??, first_line(), "peek returns first line");
assert_eq!(
rd.peek_line().expect("line")??,
Expand Down Expand Up @@ -97,7 +97,7 @@ fn peek() -> crate::Result {
fn read_from_file_and_reader_advancement() -> crate::Result {
let mut bytes = fixture_bytes("v1/fetch/01-many-refs.response");
bytes.extend(fixture_bytes("v1/fetch/01-many-refs.response").into_iter());
let mut rd = git_packetline::Provider::new(&bytes[..], PacketLine::Flush);
let mut rd = git_packetline::Provider::new(&bytes[..], Some(PacketLine::Flush));
assert_eq!(rd.read_line().expect("line")??, first_line());
assert_eq!(exhaust(&mut rd) + 1, 1561, "it stops after seeing the flush byte");
rd.reset();
Expand Down
4 changes: 2 additions & 2 deletions git-packetline/tests/packet_line/reader/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::{BufRead, Read};
#[test]
fn read_line_trait_method_reads_one_packet_line_at_a_time() -> crate::Result {
let buf = fixture_bytes("v1/01-clone.combined-output-no-binary");
let mut rd = git_packetline::Provider::new(&buf[..], PacketLine::Flush);
let mut rd = git_packetline::Provider::new(&buf[..], Some(PacketLine::Flush));

let mut out = String::new();
let mut r = rd.as_read();
Expand Down Expand Up @@ -46,7 +46,7 @@ fn read_line_trait_method_reads_one_packet_line_at_a_time() -> crate::Result {
#[test]
fn read_pack_with_progress_extraction() -> crate::Result {
let buf = fixture_bytes("v1/01-clone.combined-output");
let mut rd = git_packetline::Provider::new(&buf[..], PacketLine::Flush);
let mut rd = git_packetline::Provider::new(&buf[..], Some(PacketLine::Flush));

// Read without sideband decoding
let mut out = Vec::new();
Expand Down
1 change: 1 addition & 0 deletions git-protocol/src/fetch/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ impl Response {
"acknowledgments" => {
let acks = acks.get_or_insert_with(Vec::new);
line.clear();
// reader.reset_with(Some(client::MessageKind::Delimiter));
while reader.read_line(&mut line)? != 0 {
acks.push(Acknowledgement::from_line(&line)?);
line.clear();
Expand Down
2 changes: 1 addition & 1 deletion git-protocol/tests/fetch/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io;
fn mock_reader(path: &str) -> git_packetline::Provider<std::io::Cursor<Vec<u8>>> {
use crate::fixture_bytes;
let buf = fixture_bytes(path);
git_packetline::Provider::new(io::Cursor::new(buf), git_packetline::PacketLine::Flush)
git_packetline::Provider::new(io::Cursor::new(buf), Some(git_packetline::PacketLine::Flush))
}

fn id(hex: &str) -> git_object::owned::Id {
Expand Down
2 changes: 1 addition & 1 deletion git-transport/src/client/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ where
) -> Self {
Connection {
writer: write,
line_provider: git_packetline::Provider::new(read, PacketLine::Flush),
line_provider: git_packetline::Provider::new(read, Some(PacketLine::Flush)),
path: repository_path.into(),
virtual_host: virtual_host.map(|(h, p)| (h.into(), p)),
desired_version,
Expand Down
2 changes: 1 addition & 1 deletion git-transport/src/client/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<H: Http> client::Transport for Transport<H> {

let line_reader = self
.line_provider
.get_or_insert_with(|| git_packetline::Provider::new(body, PacketLine::Flush));
.get_or_insert_with(|| git_packetline::Provider::new(body, Some(PacketLine::Flush)));

let mut announced_service = String::new();
line_reader.as_read().read_to_string(&mut announced_service)?;
Expand Down

0 comments on commit cfae63a

Please sign in to comment.