History

refactor

Sebastian Thiel committed 6 years ago

Commit fab9f991 parent fcb4cc11 file · +17 −45Browse files

Unified · Split

git-protocol/src/fetch/response.rs+17−45
@@ -103,57 +103,29 @@ impl<'a> Response<'a> { · 102 unmodified lines
103 ) -> Result<Response<'a>, Error> {103 ) -> Result<Response<'a>, Error> {
104 match version {104 match version {
105 Protocol::V1 => {105 Protocol::V1 => {
106 enum State {
107 AtFirstCheckClone,
108 ParseAcks(Vec<Acknowledgement>),
109 }
110 let mut state = State::AtFirstCheckClone;
111 let mut line = String::new();106 let mut line = String::new();
107 let mut acks = Vec::<Acknowledgement>::new();
112 let (acks, pack) = loop {108 let (acks, pack) = loop {
113 line.clear();109 line.clear();
114 match state {110 let peeked_line = match reader.peek_data_line() {
115 State::AtFirstCheckClone => {111 Some(line) => String::from_utf8_lossy(line??),
116 if reader.read_line(&mut line)? == 0 {112 None => break (acks, None), // EOF
117 return Err(Error::Io(io::Error::new(113 };
118 io::ErrorKind::UnexpectedEof,
119 "Could not read a single line",
120 )));
121 };
122 let acks = vec![Acknowledgement::from_line(&line)?];
123 match acks.last().expect("one ack present") {
124 Acknowledgement::NAK => {
125 // we are a clone (or so we think) as the first line is a NAK
126 // We expect the following to be the pack
127 break (acks, Some(reader));
128 }
129 _ => {
130 state = State::ParseAcks(acks);
131 }
132 }
133 }
134 State::ParseAcks(mut acks) => {
135 let peeked_line = match reader.peek_data_line() {
136 Some(line) => String::from_utf8_lossy(line??),
137 None => break (acks, None), // EOF
138 };
139 114
140 // with a friendly server, we just assume that a non-ack line is a pack line115 // with a friendly server, we just assume that a non-ack line is a pack line
141 // which is our hint to stop here.116 // which is our hint to stop here.
142 let ack = match Acknowledgement::from_line(&peeked_line) {117 let ack = match Acknowledgement::from_line(&peeked_line) {
143 Ok(ack) => ack,118 Ok(ack) => ack,
144 Err(_) => break (acks, Some(reader)),119 Err(_) => break (acks, Some(reader)),
145 };120 };
146 assert_ne!(reader.read_line(&mut line)?, 0, "consuming a peeked line works");121 assert_ne!(reader.read_line(&mut line)?, 0, "consuming a peeked line works");
147 match ack.id() {122 match ack.id() {
148 Some(id) => {123 Some(id) => {
149 if !acks.iter().any(|a| a.id() == Some(id)) {124 if !acks.iter().any(|a| a.id() == Some(id)) {
150 acks.push(ack);125 acks.push(ack);
151 }
152 }
153 None => acks.push(ack),
154 }126 }
155 state = State::ParseAcks(acks)
156 }127 }
128 None => acks.push(ack),
157 }129 }
158 };130 };
159 Ok(Response { acks, pack })131 Ok(Response { acks, pack })