Skip to content

Commit

Permalink
Don't try to find 'ERR ' in every packet line we parse…
Browse files Browse the repository at this point in the history
…as error checking really is in the hands of the consumer.
  • Loading branch information
Byron committed Aug 19, 2020
1 parent 25cdbec commit 922fcb6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 10 deletions.
11 changes: 10 additions & 1 deletion git-packetline/src/borrowed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{encode, Channel};
use crate::{encode, Channel, ERR_PREFIX};
use bstr::BStr;
use std::io;

Expand Down Expand Up @@ -33,6 +33,15 @@ impl<'a> Borrowed<'a> {
pub fn to_error(&self) -> Option<Error> {
self.as_slice().map(Error)
}
pub fn check_error(&self) -> Option<Error> {
self.as_slice().and_then(|data| {
if data.len() >= ERR_PREFIX.len() && &data[..ERR_PREFIX.len()] == ERR_PREFIX {
Some(Error(&data[ERR_PREFIX.len()..]))
} else {
None
}
})
}
pub fn to_text(&self) -> Option<Text> {
self.as_slice().map(Into::into)
}
Expand Down
5 changes: 1 addition & 4 deletions git-packetline/src/decode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
PacketLine, {DELIMITER_LINE, ERR_PREFIX, FLUSH_LINE, MAX_DATA_LEN, MAX_LINE_LEN, RESPONSE_END_LINE, U16_HEX_BYTES},
PacketLine, {DELIMITER_LINE, FLUSH_LINE, MAX_DATA_LEN, MAX_LINE_LEN, RESPONSE_END_LINE, U16_HEX_BYTES},
};
use bstr::BString;
use quick_error::quick_error;
Expand Down Expand Up @@ -77,9 +77,6 @@ pub fn to_data_line(data: &[u8]) -> Result<PacketLine, Error> {
return Err(Error::DataLengthLimitExceeded(data.len()));
}

if data.len() >= ERR_PREFIX.len() && &data[..ERR_PREFIX.len()] == ERR_PREFIX {
return Err(Error::Line(data[ERR_PREFIX.len()..].into(), data.len()));
}
Ok(PacketLine::Data(data))
}

Expand Down
7 changes: 7 additions & 0 deletions git-packetline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum Channel {
Error = 3,
}

/// The information usually found in remote progress messages as sent by a git server during
/// fetch, clone and push.
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct RemoteProgress<'a> {
Expand All @@ -32,5 +34,10 @@ pub mod read;
#[doc(inline)]
pub use read::Reader;

pub mod write {
/// An implementor of `Write` which passes all input to an inner `Write` in packet line data encoding.
pub struct Writer {}
}

pub mod decode;
pub mod encode;
19 changes: 15 additions & 4 deletions git-packetline/tests/packet_line/decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod streaming {
use crate::packet_line::assert_err_display;
use bstr::ByteSlice;
use git_packetline::borrowed::Error;
use git_packetline::{
decode::{self, streaming, Stream},
Channel, PacketLine,
Expand Down Expand Up @@ -62,11 +63,18 @@ mod streaming {
}

#[test]
fn error_on_error_line() {
assert_err_display(
fn error_on_error_line() -> crate::Result {
let line = PacketLine::Data(b"ERR the error");
assert_complete(
streaming(b"0011ERR the error-and just ignored because not part of the size"),
"the error",
17,
line,
)?;
assert_eq!(
line.check_error().expect("error to be parsed here"),
Error(b"the error")
);
Ok(())
}

#[test]
Expand Down Expand Up @@ -104,7 +112,10 @@ mod streaming {
.to_error()
.expect("data line")
.to_write(&mut out)?;
assert_err_display(streaming(&out), "the error");
match streaming(&out).expect("decode success") {
Stream::Complete { line, .. } => assert_eq!(line.check_error().expect("err").0, b"the error"),
Stream::Incomplete { .. } => unreachable!(),
};
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* [x] Progress parsing, with 'info(…)' fallback if that fails
* [x] SetName in Progress trait (to allow setting the correct progress information)
* [x] Disable sideband support (e.g. github http V2 doesn't have it)
* [ ] don't coerce line delimiters into empty slices.
* [x] don't coerce line delimiters into empty slices.
* [x] Make 'ERR <error>' handling as error optional, as it may occur only in certain contexts.
* **git-url**
* [ ] parse into components to make them easy to understand
* **connect**
Expand Down

0 comments on commit 922fcb6

Please sign in to comment.