(
bytes: &[u8],
algorithm: GitHashAlgorithm,
)
| 1106 | } |
| 1107 | |
| 1108 | fn parse_tree_entries( |
| 1109 | bytes: &[u8], |
| 1110 | algorithm: GitHashAlgorithm, |
| 1111 | ) -> Result<Vec<GitTreeEntry>, ProjectTreeError> { |
| 1112 | let oid_len = match algorithm { |
| 1113 | GitHashAlgorithm::Sha1 => 20, |
| 1114 | GitHashAlgorithm::Sha256 => 32, |
| 1115 | }; |
| 1116 | let mut cursor = 0usize; |
| 1117 | let mut entries = Vec::new(); |
| 1118 | while cursor < bytes.len() { |
| 1119 | let space = bytes[cursor..] |
| 1120 | .iter() |
| 1121 | .position(|byte| *byte == b' ') |
| 1122 | .ok_or_else(|| ProjectTreeError::MalformedGitTree("missing mode separator".into()))? |
| 1123 | + cursor; |
| 1124 | let mode_text = std::str::from_utf8(&bytes[cursor..space]) |
| 1125 | .map_err(|_| ProjectTreeError::MalformedGitTree("non-ASCII mode".into()))?; |
| 1126 | if mode_text.starts_with('0') { |
| 1127 | return Err(ProjectTreeError::MalformedGitTree( |
| 1128 | "non-canonical mode".into(), |
| 1129 | )); |
| 1130 | } |
| 1131 | let mode = u32::from_str_radix(mode_text, 8) |
| 1132 | .map_err(|_| ProjectTreeError::MalformedGitTree("invalid mode".into()))?; |
| 1133 | cursor = space + 1; |
| 1134 | let nul = bytes[cursor..] |
| 1135 | .iter() |
| 1136 | .position(|byte| *byte == 0) |
| 1137 | .ok_or_else(|| ProjectTreeError::MalformedGitTree("missing name terminator".into()))? |
| 1138 | + cursor; |
| 1139 | let name = bytes[cursor..nul].to_vec(); |
| 1140 | if name.is_empty() || name.contains(&b'/') { |
| 1141 | return Err(ProjectTreeError::MalformedGitTree( |
| 1142 | "invalid tree entry name".into(), |
| 1143 | )); |
| 1144 | } |
| 1145 | cursor = nul + 1; |
| 1146 | let end = cursor |
| 1147 | .checked_add(oid_len) |
| 1148 | .filter(|end| *end <= bytes.len()) |
| 1149 | .ok_or_else(|| ProjectTreeError::MalformedGitTree("truncated object ID".into()))?; |
| 1150 | let oid = GitObjectId::new(algorithm, bytes[cursor..end].to_vec()) |
| 1151 | .map_err(|error| ProjectTreeError::GitObjectId(error.to_string()))?; |
| 1152 | cursor = end; |
| 1153 | entries.push(GitTreeEntry { mode, name, oid }); |
| 1154 | } |
| 1155 | Ok(entries) |
| 1156 | } |
| 1157 | |
| 1158 | pub(super) fn git_object_id( |
| 1159 | algorithm: GitHashAlgorithm, |
no test coverage detected