Skip to content

Commit 13bf25e

Browse files
committed
[clone] decouple packet line from git-features and progress
Now all progress handling can truly be done where it is known: in the protocol layer
1 parent fb7dd26 commit 13bf25e

File tree

6 files changed

+32
-80
lines changed

6 files changed

+32
-80
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git-packetline/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ serde1 = ["serde", "bstr/serde1"]
1717
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1818

1919
[dependencies]
20-
git-features = { version = "^0.4.0", path = "../git-features" }
2120
serde = { version = "1.0.114", optional = true, default-features = false, features = ["std", "derive"]}
2221
quick-error = "2.0.0"
2322
hex = "0.4.2"

git-packetline/src/lib.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,6 @@ pub enum Channel {
1515
Error = 3,
1616
}
1717

18-
/// The information usually found in remote progress messages as sent by a git server during
19-
/// fetch, clone and push.
20-
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
21-
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
22-
pub struct RemoteProgress<'a> {
23-
#[cfg_attr(feature = "serde1", serde(borrow))]
24-
pub action: &'a bstr::BStr,
25-
pub percent: Option<u32>,
26-
pub step: Option<usize>,
27-
pub max: Option<usize>,
28-
}
29-
3018
pub mod borrowed;
3119
pub use borrowed::Borrowed as PacketLine;
3220

git-packetline/src/reader/mod.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
use crate::PacketLine;
12
use crate::{decode, MAX_LINE_LEN, U16_HEX_BYTES};
2-
use crate::{PacketLine, RemoteProgress};
33
use bstr::ByteSlice;
4-
use git_features::{progress, progress::Progress};
54
use std::io;
65

76
mod read;
8-
pub use read::ReadWithProgress;
7+
pub use read::ReadWithSidebands;
98

109
/// Read pack lines one after another, without consuming more than needed from the underlying
1110
/// `Read`. `Flush` lines cause the reader to stop producing lines forever, leaving `Read` at the
@@ -141,15 +140,11 @@ where
141140
})
142141
}
143142

144-
pub fn as_read_with_sidebands<P: Progress>(
145-
&mut self,
146-
progress: P,
147-
parse_progress: fn(&[u8]) -> Option<RemoteProgress>,
148-
) -> ReadWithProgress<T, P> {
149-
ReadWithProgress::with_progress(self, progress, parse_progress)
143+
pub fn as_read_with_sidebands<F: FnMut(bool, &[u8])>(&mut self, handle_progress: F) -> ReadWithSidebands<T, F> {
144+
ReadWithSidebands::with_progress_handler(self, handle_progress)
150145
}
151146

152-
pub fn as_read(&mut self) -> ReadWithProgress<T, progress::Discard> {
153-
ReadWithProgress::new(self)
147+
pub fn as_read(&mut self) -> ReadWithSidebands<T, fn(bool, &[u8])> {
148+
ReadWithSidebands::new(self)
154149
}
155150
}

git-packetline/src/reader/read.rs

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
use crate::{
22
borrowed::{Band, Text},
3-
Reader, RemoteProgress, MAX_DATA_LEN,
3+
Reader, MAX_DATA_LEN,
44
};
5-
use bstr::ByteSlice;
6-
use git_features::{progress, progress::Progress};
75
use std::io;
86

9-
type ProgressAndParser<P> = (P, fn(&[u8]) -> Option<RemoteProgress>);
10-
11-
pub struct ReadWithProgress<'a, T, P>
7+
pub struct ReadWithSidebands<'a, T, F>
128
where
139
T: io::Read,
1410
{
1511
parent: &'a mut Reader<T>,
16-
progress_and_parse: Option<ProgressAndParser<P>>,
12+
handle_progress: Option<F>,
1713
buf: Vec<u8>,
1814
pos: usize,
1915
cap: usize,
2016
}
2117

22-
impl<'a, T, P> Drop for ReadWithProgress<'a, T, P>
18+
impl<'a, T, F> Drop for ReadWithSidebands<'a, T, F>
2319
where
2420
T: io::Read,
2521
{
@@ -28,45 +24,41 @@ where
2824
}
2925
}
3026

31-
impl<'a, T> ReadWithProgress<'a, T, progress::Discard>
27+
impl<'a, T> ReadWithSidebands<'a, T, fn(bool, &[u8])>
3228
where
3329
T: io::Read,
3430
{
3531
pub fn new(parent: &'a mut Reader<T>) -> Self {
36-
ReadWithProgress {
32+
ReadWithSidebands {
3733
parent,
38-
progress_and_parse: None,
34+
handle_progress: None,
3935
buf: vec![0; MAX_DATA_LEN],
4036
pos: 0,
4137
cap: 0,
4238
}
4339
}
4440
}
4541

46-
impl<'a, T, P> ReadWithProgress<'a, T, P>
42+
impl<'a, T, F> ReadWithSidebands<'a, T, F>
4743
where
4844
T: io::Read,
49-
P: Progress,
45+
F: FnMut(bool, &[u8]),
5046
{
51-
pub fn with_progress(
52-
parent: &'a mut Reader<T>,
53-
progress: P,
54-
parse_progress: fn(&[u8]) -> Option<RemoteProgress>,
55-
) -> Self {
56-
ReadWithProgress {
47+
pub fn with_progress_handler(parent: &'a mut Reader<T>, handle_progress: F) -> Self {
48+
ReadWithSidebands {
5749
parent,
58-
progress_and_parse: Some((progress, parse_progress)),
50+
handle_progress: Some(handle_progress),
5951
buf: vec![0; MAX_DATA_LEN],
6052
pos: 0,
6153
cap: 0,
6254
}
6355
}
6456
}
6557

66-
impl<'a, T, P> io::BufRead for ReadWithProgress<'a, T, P>
58+
impl<'a, T, F> io::BufRead for ReadWithSidebands<'a, T, F>
6759
where
6860
T: io::Read,
69-
P: Progress,
61+
F: FnMut(bool, &[u8]),
7062
{
7163
fn fill_buf(&mut self) -> io::Result<&[u8]> {
7264
use io::Read;
@@ -77,38 +69,21 @@ where
7769
Some(line) => line?.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?,
7870
None => break 0,
7971
};
80-
match self.progress_and_parse.as_mut() {
81-
Some((progress, parse_progress)) => {
72+
match self.handle_progress.as_mut() {
73+
Some(handle_progress) => {
8274
let mut band = line
8375
.decode_band()
8476
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
85-
fn progress_name(current: Option<String>, action: &[u8]) -> String {
86-
match current {
87-
Some(current) => format!("{}: {}", current, action.as_bstr()),
88-
None => action.as_bstr().to_string(),
89-
}
90-
}
9177
match band {
9278
Band::Data(ref mut d) => break d.read(&mut self.buf)?,
9379
Band::Progress(d) => {
9480
let text = Text::from(d).0;
95-
match (parse_progress)(text) {
96-
Some(RemoteProgress {
97-
action,
98-
percent: _,
99-
step,
100-
max,
101-
}) => {
102-
progress.set_name(progress_name(progress.name(), action));
103-
progress.init(max, git_features::progress::count("objects"));
104-
if let Some(step) = step {
105-
progress.set(step);
106-
}
107-
}
108-
None => progress.set_name(progress_name(progress.name(), text)),
109-
};
81+
(handle_progress)(false, text);
82+
}
83+
Band::Error(d) => {
84+
let text = Text::from(d).0;
85+
(handle_progress)(true, text);
11086
}
111-
Band::Error(d) => progress.fail(progress_name(None, Text::from(d).as_slice())),
11287
};
11388
}
11489
None => {
@@ -134,10 +109,10 @@ where
134109
}
135110
}
136111

137-
impl<'a, T, P> io::Read for ReadWithProgress<'a, T, P>
112+
impl<'a, T, F> io::Read for ReadWithSidebands<'a, T, F>
138113
where
139114
T: io::Read,
140-
P: Progress,
115+
F: FnMut(bool, &[u8]),
141116
{
142117
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
143118
use std::io::BufRead;

git-packetline/tests/packet_line/read.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use bstr::ByteSlice;
22
use git_packetline::PacketLine;
3-
use std::io;
4-
use std::path::PathBuf;
3+
use std::{io, path::PathBuf};
54

65
fn fixture_path(path: &str) -> PathBuf {
76
PathBuf::from("tests/fixtures").join(path)
@@ -15,7 +14,6 @@ mod to_read {
1514
use crate::packet_line::read::fixture_bytes;
1615
use bstr::ByteSlice;
1716
use git_odb::pack;
18-
use git_packetline::RemoteProgress;
1917
use std::io::Read;
2018

2119
#[test]
@@ -37,10 +35,8 @@ mod to_read {
3735
.as_bstr(),
3836
b"NAK".as_bstr()
3937
);
40-
fn no_parsing(_: &[u8]) -> Option<RemoteProgress> {
41-
None
42-
}
43-
let pack_read = rd.as_read_with_sidebands(git_features::progress::Discard, no_parsing);
38+
fn do_nothing(_is_err: bool, _data: &[u8]) {}
39+
let pack_read = rd.as_read_with_sidebands(do_nothing);
4440
let pack_entries = pack::data::Iter::new_from_header(
4541
pack_read,
4642
pack::data::iter::Mode::Verify,

0 commit comments

Comments
 (0)