| 182 | } |
| 183 | |
| 184 | fn parse_porcelain_v2(bytes: &[u8], limit: usize) -> Result<Vec<Vec<u8>>, ChangeSourceError> { |
| 185 | let records: Vec<&[u8]> = bytes.split(|byte| *byte == 0).collect(); |
| 186 | let mut paths = Vec::new(); |
| 187 | let mut index = 0; |
| 188 | while index < records.len() { |
| 189 | let record = records[index]; |
| 190 | index += 1; |
| 191 | if record.is_empty() { |
| 192 | continue; |
| 193 | } |
| 194 | let (spaces, renamed) = match record[0] { |
| 195 | b'1' => (8, false), |
| 196 | b'2' => (9, true), |
| 197 | b'u' => (10, false), |
| 198 | b'?' => (1, false), |
| 199 | b'!' | b'#' => continue, |
| 200 | _ => { |
| 201 | return Err(ChangeSourceError::MalformedResponse( |
| 202 | "unknown Git porcelain-v2 record".into(), |
| 203 | )) |
| 204 | } |
| 205 | }; |
| 206 | let path = field_after_spaces(record, spaces).ok_or_else(|| { |
| 207 | ChangeSourceError::MalformedResponse("truncated Git porcelain-v2 record".into()) |
| 208 | })?; |
| 209 | if path.is_empty() { |
| 210 | return Err(ChangeSourceError::MalformedResponse( |
| 211 | "empty Git candidate path".into(), |
| 212 | )); |
| 213 | } |
| 214 | paths.push(path.to_vec()); |
| 215 | if paths.len() > limit { |
| 216 | return Err(ChangeSourceError::Overflow { limit }); |
| 217 | } |
| 218 | if renamed { |
| 219 | if index >= records.len() || records[index].is_empty() { |
| 220 | return Err(ChangeSourceError::MalformedResponse( |
| 221 | "rename record has no original path".into(), |
| 222 | )); |
| 223 | } |
| 224 | index += 1; |
| 225 | } |
| 226 | } |
| 227 | Ok(paths) |
| 228 | } |
| 229 | |
| 230 | fn field_after_spaces(record: &[u8], count: usize) -> Option<&[u8]> { |
| 231 | let mut remaining = record; |