MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / fold

Function fold

atomic-cli/src/commands/git/parallel.rs:1228–1316  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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).

Callers 4

import_expected_tree_oidFunction · 0.85
newMethod · 0.85
verifyMethod · 0.85

Calls 11

is_excludedFunction · 0.85
git_errorFunction · 0.70
iterMethod · 0.45
cloneMethod · 0.45
is_emptyMethod · 0.45
pushMethod · 0.45
kindMethod · 0.45
as_bytesMethod · 0.45
idMethod · 0.45
cmpMethod · 0.45
insertMethod · 0.45

Tested by 1