Canonical Git tree-object encoding for one directory's entries: `" \0 "` per entry, in git tree-name order (byte compare with `/` appended to tree names), hashed through the workspace's own per-algorithm object hashing.
(
git: &GitRepository,
tree: &git2::Tree<'_>,
prefix: Vec<u8>,
policy: &ConversionPolicy,
objects: &mut atomic_repository::repository::GitObjectDatabase,
)
| 1226 | /// (byte compare with `/` appended to tree names), hashed through the |
| 1227 | /// workspace's own per-algorithm object hashing. |
| 1228 | fn fold( |
| 1229 | git: &GitRepository, |
| 1230 | tree: &git2::Tree<'_>, |
| 1231 | prefix: Vec<u8>, |
| 1232 | policy: &ConversionPolicy, |
| 1233 | objects: &mut atomic_repository::repository::GitObjectDatabase, |
| 1234 | ) -> CliResult<Option<GitObjectId>> { |
| 1235 | let mut entries: Vec<(Vec<u8>, u32, GitObjectId, bool)> = Vec::new(); |
| 1236 | for entry in tree.iter() { |
| 1237 | let name = entry.name_bytes().to_vec(); |
| 1238 | let mut path = prefix.clone(); |
| 1239 | if !path.is_empty() { |
| 1240 | path.push(b'/'); |
| 1241 | } |
| 1242 | path.extend_from_slice(&name); |
| 1243 | match entry.kind() { |
| 1244 | Some(git2::ObjectType::Tree) => { |
| 1245 | // An excluded directory never reaches the projection; |
| 1246 | // skip the whole subtree. |
| 1247 | if is_excluded(policy, &path) { |
| 1248 | continue; |
| 1249 | } |
| 1250 | let sub = entry |
| 1251 | .to_object(git) |
| 1252 | .and_then(|object| object.peel_to_tree()) |
| 1253 | .map_err(|error| { |
| 1254 | git_error(format!( |
| 1255 | "cannot read Git subtree '{}': {error}", |
| 1256 | atomic_repository::escape_repo_path(&path) |
| 1257 | )) |
| 1258 | })?; |
| 1259 | let Some(sub_oid) = fold(git, &sub, path, policy, objects)? else { |
| 1260 | // The whole subtree is excluded; Git trees cannot be |
| 1261 | // empty, so the expected tree omits it entirely. |
| 1262 | continue; |
| 1263 | }; |
| 1264 | entries.push((name, 0o040000, sub_oid, true)); |
| 1265 | } |
| 1266 | _ => { |
| 1267 | if is_excluded(policy, &path) { |
| 1268 | continue; |
| 1269 | } |
| 1270 | entries.push(( |
| 1271 | name, |
| 1272 | entry.filemode() as u32, |
| 1273 | GitObjectId::new(policy.object_format, entry.id().as_bytes().to_vec()) |
| 1274 | .map_err(|error| { |
| 1275 | git_error(format!("Git tree entry OID is invalid: {error}")) |
| 1276 | })?, |
| 1277 | false, |
| 1278 | )); |
| 1279 | } |
| 1280 | } |
| 1281 | } |
| 1282 | if entries.is_empty() { |
| 1283 | // Git trees cannot be empty: report absence so the caller omits |
| 1284 | // this subtree from its parent (the ROOT caller hashes the |
| 1285 | // canonical empty tree below). |