arks-server 1.3.3

Git server: upload-pack / receive-pack + smart/dumb HTTP + daemon (M11/M25)
Documentation
//! Walk commit/tree/blob closure for pack generation.

use crate::error::ServerError;
use arks_object::{Commit, ObjectKind, RawObject, Tree, TreeMode};
use arks_oid::Oid;
use std::collections::HashSet;

pub fn collect_closure<F>(tips: &[Oid], mut get: F) -> Result<Vec<RawObject>, ServerError>
where
    F: FnMut(&Oid) -> Result<Option<RawObject>, ServerError>,
{
    let mut seen = HashSet::new();
    let mut stack: Vec<Oid> = tips.to_vec();
    let mut out = Vec::new();
    while let Some(oid) = stack.pop() {
        if !seen.insert(oid) {
            continue;
        }
        let Some(obj) = get(&oid)? else {
            return Err(ServerError::Protocol(format!(
                "missing object {}",
                oid.to_hex()
            )));
        };
        match obj.kind {
            ObjectKind::Commit => {
                let commit = Commit::decode(&obj.payload)
                    .map_err(|e| ServerError::Protocol(e.to_string()))?;
                stack.push(commit.tree);
                stack.extend(commit.parents);
            }
            ObjectKind::Tree => {
                let tree = Tree::decode(&obj.payload, oid.kind())
                    .map_err(|e| ServerError::Protocol(e.to_string()))?;
                for entry in tree.entries {
                    if entry.mode == TreeMode::Gitlink {
                        continue;
                    }
                    stack.push(entry.oid);
                }
            }
            ObjectKind::Tag => {
                if let Ok(text) = std::str::from_utf8(&obj.payload) {
                    for line in text.lines() {
                        if line.is_empty() {
                            break;
                        }
                        if let Some(hex) = line.strip_prefix("object ") {
                            if let Ok(id) = Oid::from_hex(hex.trim()) {
                                stack.push(id);
                            }
                        }
                    }
                }
            }
            ObjectKind::Blob => {}
        }
        out.push(obj);
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use arks_object::{Signature, TreeEntry};
    use arks_store::{MemoryObjectStore, ObjectFormat, ObjectStore};

    #[test]
    fn collect_closure_supports_sha256_tree_and_commit() {
        let mut store = MemoryObjectStore::new();
        let blob = RawObject::new(ObjectKind::Blob, b"sha256 payload\n".to_vec());
        let blob_oid = store.put(ObjectFormat::Sha256, &blob).unwrap();
        assert_eq!(blob_oid.to_hex().len(), 64);

        let tree = Tree::new(vec![TreeEntry {
            mode: TreeMode::File,
            name: "hello.txt".into(),
            oid: blob_oid,
        }])
        .unwrap();
        let tree_obj = RawObject::new(ObjectKind::Tree, tree.encode().unwrap());
        let tree_oid = store.put(ObjectFormat::Sha256, &tree_obj).unwrap();

        let sig = Signature {
            name: "arks".into(),
            email: "arks@example.invalid".into(),
            timestamp: 1_700_000_000,
            timezone: "+0000".into(),
        };
        let commit = Commit {
            tree: tree_oid,
            parents: Vec::new(),
            author: sig.clone(),
            committer: sig,
            gpgsig: None,
            message: "sha256 init\n".into(),
        };
        let commit_obj = RawObject::new(ObjectKind::Commit, commit.encode().unwrap());
        let commit_oid = store.put(ObjectFormat::Sha256, &commit_obj).unwrap();

        let objs = collect_closure(&[commit_oid], |oid| Ok(store.get(oid).ok())).unwrap();
        assert_eq!(objs.len(), 3);
    }
}