(
bytes: &[u8],
algorithm: GitHashAlgorithm,
)
| 1128 | } |
| 1129 | |
| 1130 | fn parse_tree_entries( |
| 1131 | bytes: &[u8], |
| 1132 | algorithm: GitHashAlgorithm, |
| 1133 | ) -> Result<Vec<GitTreeEntry>, ProjectTreeError> { |
| 1134 | let oid_len = match algorithm { |
| 1135 | GitHashAlgorithm::Sha1 => 20, |
| 1136 | GitHashAlgorithm::Sha256 => 32, |
| 1137 | }; |
| 1138 | let mut cursor = 0usize; |
| 1139 | let mut entries = Vec::new(); |
| 1140 | while cursor < bytes.len() { |
| 1141 | let space = bytes[cursor..] |
| 1142 | .iter() |
| 1143 | .position(|byte| *byte == b' ') |
| 1144 | .ok_or_else(|| ProjectTreeError::MalformedGitTree("missing mode separator".into()))? |
| 1145 | + cursor; |
| 1146 | let mode_text = std::str::from_utf8(&bytes[cursor..space]) |
| 1147 | .map_err(|_| ProjectTreeError::MalformedGitTree("non-ASCII mode".into()))?; |
| 1148 | if mode_text.starts_with('0') { |
| 1149 | return Err(ProjectTreeError::MalformedGitTree( |
| 1150 | "non-canonical mode".into(), |
| 1151 | )); |
| 1152 | } |
| 1153 | let mode = u32::from_str_radix(mode_text, 8) |
| 1154 | .map_err(|_| ProjectTreeError::MalformedGitTree("invalid mode".into()))?; |
| 1155 | cursor = space + 1; |
| 1156 | let nul = bytes[cursor..] |
| 1157 | .iter() |
| 1158 | .position(|byte| *byte == 0) |
| 1159 | .ok_or_else(|| ProjectTreeError::MalformedGitTree("missing name terminator".into()))? |
| 1160 | + cursor; |
| 1161 | let name = bytes[cursor..nul].to_vec(); |
| 1162 | if name.is_empty() || name.contains(&b'/') { |
| 1163 | return Err(ProjectTreeError::MalformedGitTree( |
| 1164 | "invalid tree entry name".into(), |
| 1165 | )); |
| 1166 | } |
| 1167 | cursor = nul + 1; |
| 1168 | let end = cursor |
| 1169 | .checked_add(oid_len) |
| 1170 | .filter(|end| *end <= bytes.len()) |
| 1171 | .ok_or_else(|| ProjectTreeError::MalformedGitTree("truncated object ID".into()))?; |
| 1172 | let oid = GitObjectId::new(algorithm, bytes[cursor..end].to_vec()) |
| 1173 | .map_err(|error| ProjectTreeError::GitObjectId(error.to_string()))?; |
| 1174 | cursor = end; |
| 1175 | entries.push(GitTreeEntry { mode, name, oid }); |
| 1176 | } |
| 1177 | Ok(entries) |
| 1178 | } |
| 1179 | |
| 1180 | pub(super) fn git_object_id( |
| 1181 | algorithm: GitHashAlgorithm, |
no test coverage detected