Skip to main content

hyprstream_p2p/git/
objects.rs

1//! Git object utilities for GitTorrent SHA256 operations
2
3use crate::crypto::hash::sha256_git;
4use crate::{
5    types::{GitHash, GitRefs, ObjectFormat, Sha256Hash},
6    Error, Result,
7};
8use git2::{ObjectType, Oid, Repository};
9use std::collections::HashMap;
10use std::path::Path;
11
12/// Check if a repository is exportable (has git-daemon-export-ok file)
13pub fn is_exportable<P: AsRef<Path>>(repo_path: P) -> bool {
14    let repo_path = repo_path.as_ref();
15
16    // Try both bare and non-bare repository structures
17    let export_ok_paths = [
18        repo_path.join("git-daemon-export-ok"),
19        repo_path.join(".git/git-daemon-export-ok"),
20    ];
21
22    export_ok_paths.iter().any(|path| path.exists())
23}
24
25/// Git object with its SHA256 hash and data
26#[derive(Debug, Clone)]
27pub struct GitObject {
28    pub hash: Sha256Hash,
29    pub object_type: GitObjectType,
30    pub data: Vec<u8>,
31    pub size: usize,
32}
33
34/// Git object types
35#[derive(Debug, Clone, PartialEq, Eq, Hash)]
36pub enum GitObjectType {
37    Commit,
38    Tree,
39    Blob,
40    Tag,
41}
42
43impl From<ObjectType> for GitObjectType {
44    fn from(obj_type: ObjectType) -> Self {
45        match obj_type {
46            ObjectType::Commit => GitObjectType::Commit,
47            ObjectType::Tree => GitObjectType::Tree,
48            ObjectType::Tag => GitObjectType::Tag,
49            // Blob and Any both map to Blob
50            ObjectType::Blob | ObjectType::Any => GitObjectType::Blob,
51        }
52    }
53}
54
55/// Extract all Git objects from a repository
56pub async fn extract_objects(repo_path: &Path) -> Result<Vec<GitObject>> {
57    let repo = Repository::open(repo_path)?;
58    let mut objects = Vec::new();
59
60    // Get the object database
61    let odb = repo.odb()?;
62
63    // Collect all object IDs first
64    let mut object_ids = Vec::new();
65    odb.foreach(|oid| {
66        object_ids.push(*oid);
67        true // Continue iteration
68    })?;
69
70    // Process each object
71    for oid in object_ids {
72        if let Ok(obj) = odb.read(oid) {
73            // Create git object format: "type size\0content"
74            let type_str = match obj.kind() {
75                ObjectType::Commit => "commit",
76                ObjectType::Tree => "tree",
77                ObjectType::Tag => "tag",
78                // Blob and Any both render as "blob"
79                ObjectType::Blob | ObjectType::Any => "blob",
80            };
81
82            let mut git_format = Vec::new();
83            git_format.extend_from_slice(type_str.as_bytes());
84            git_format.push(b' ');
85            git_format.extend_from_slice(obj.len().to_string().as_bytes());
86            git_format.push(0); // null terminator
87            git_format.extend_from_slice(obj.data());
88
89            // Calculate SHA256 hash of the git-formatted object
90            let hash = sha256_git(&git_format)?;
91
92            objects.push(GitObject {
93                hash,
94                object_type: obj.kind().into(),
95                data: git_format,
96                size: obj.len(),
97            });
98        }
99    }
100
101    tracing::info!("Extracted {} objects from repository", objects.len());
102    Ok(objects)
103}
104
105/// Extract objects for a specific commit and its history
106pub async fn extract_commit_objects(repo_path: &Path, commit_hash: &str) -> Result<Vec<GitObject>> {
107    let repo = Repository::open(repo_path)?;
108    let mut objects = Vec::new();
109    let mut processed = std::collections::HashSet::new();
110
111    // Start with the specified commit
112    let commit_oid = Oid::from_str(commit_hash)?;
113    let mut to_process = vec![commit_oid];
114
115    while let Some(oid) = to_process.pop() {
116        if processed.contains(&oid) {
117            continue;
118        }
119        processed.insert(oid);
120
121        // Get the object
122        if let Ok(obj) = repo.find_object(oid, None) {
123            // Add object to our list
124            let git_object = create_git_object(&repo, &obj)?;
125            objects.push(git_object);
126
127            // Add referenced objects based on type
128            match obj.kind() {
129                Some(ObjectType::Commit) => {
130                    if let Some(commit) = obj.as_commit() {
131                        // Add tree
132                        to_process.push(commit.tree_id());
133                        // Add parents
134                        for parent in commit.parent_ids() {
135                            to_process.push(parent);
136                        }
137                    }
138                }
139                Some(ObjectType::Tree) => {
140                    if let Some(tree) = obj.as_tree() {
141                        for entry in tree.iter() {
142                            to_process.push(entry.id());
143                        }
144                    }
145                }
146                _ => {} // Blobs and tags don't reference other objects
147            }
148        }
149    }
150
151    tracing::info!(
152        "Extracted {} objects for commit {}",
153        objects.len(),
154        commit_hash
155    );
156    Ok(objects)
157}
158
159/// Create a GitObject from a git2 Object
160fn create_git_object(repo: &Repository, obj: &git2::Object<'_>) -> Result<GitObject> {
161    let odb = repo.odb()?;
162    let odb_obj = odb.read(obj.id())?;
163
164    // Create git object format
165    let type_str = match obj.kind() {
166        Some(ObjectType::Commit) => "commit",
167        Some(ObjectType::Tree) => "tree",
168        Some(ObjectType::Tag) => "tag",
169        // Blob, Any, or None all render as "blob"
170        Some(ObjectType::Blob | ObjectType::Any) | None => "blob",
171    };
172
173    let mut git_format = Vec::new();
174    git_format.extend_from_slice(type_str.as_bytes());
175    git_format.push(b' ');
176    git_format.extend_from_slice(odb_obj.len().to_string().as_bytes());
177    git_format.push(0);
178    git_format.extend_from_slice(odb_obj.data());
179
180    let hash = sha256_git(&git_format)?;
181
182    Ok(GitObject {
183        hash,
184        object_type: obj.kind().unwrap_or(ObjectType::Blob).into(),
185        data: git_format,
186        size: odb_obj.len(),
187    })
188}
189
190/// Git commit object data
191#[derive(Debug, Clone)]
192pub struct CommitObject {
193    pub tree_hash: GitHash,
194    pub parent_hashes: Vec<GitHash>,
195    pub author: String,
196    pub committer: String,
197    pub message: String,
198}
199
200/// Git tree entry
201#[derive(Debug, Clone)]
202pub struct TreeEntry {
203    pub mode: TreeEntryMode,
204    pub name: String,
205    pub hash: GitHash,
206}
207
208/// Tree entry modes
209#[derive(Debug, Clone, PartialEq)]
210pub enum TreeEntryMode {
211    File,       // 100644
212    Executable, // 100755
213    Directory,  // 040000
214    Symlink,    // 120000
215    Gitlink,    // 160000 (submodule)
216}
217
218/// Git tree object data
219#[derive(Debug, Clone)]
220pub struct TreeObject {
221    pub entries: Vec<TreeEntry>,
222}
223
224/// Parse a commit object from git format
225pub fn parse_commit_object(data: &[u8]) -> Result<CommitObject> {
226    // Parse git object header: "commit <size>\0<content>"
227    let null_pos = data
228        .iter()
229        .position(|&b| b == 0)
230        .ok_or_else(|| Error::other("Invalid git object format"))?;
231
232    let header = std::str::from_utf8(&data[..null_pos])?;
233    let content = &data[null_pos + 1..];
234
235    // Verify it's a commit
236    if !header.starts_with("commit ") {
237        return Err(Error::other("Not a commit object"));
238    }
239
240    let content_str = std::str::from_utf8(content)?;
241    let mut tree_hash = None;
242    let mut parent_hashes = Vec::new();
243    let mut author = String::new();
244    let mut committer = String::new();
245    let mut message_start = None;
246
247    for (i, line) in content_str.lines().enumerate() {
248        if line.is_empty() {
249            // Empty line indicates start of commit message
250            message_start = Some(
251                content_str
252                    .lines()
253                    .skip(i + 1)
254                    .collect::<Vec<_>>()
255                    .join("\n"),
256            );
257            break;
258        }
259
260        if let Some(hash_str) = line.strip_prefix("tree ") {
261            // Parse hash (supports both SHA1 and SHA256)
262            tree_hash = Some(GitHash::from_hex(hash_str.trim())?);
263        } else if let Some(hash_str) = line.strip_prefix("parent ") {
264            parent_hashes.push(GitHash::from_hex(hash_str.trim())?);
265        } else if let Some(author_str) = line.strip_prefix("author ") {
266            author = author_str.to_owned();
267        } else if let Some(committer_str) = line.strip_prefix("committer ") {
268            committer = committer_str.to_owned();
269        }
270    }
271
272    Ok(CommitObject {
273        tree_hash: tree_hash.ok_or_else(|| Error::other("Commit missing tree reference"))?,
274        parent_hashes,
275        author,
276        committer,
277        message: message_start.unwrap_or_default(),
278    })
279}
280
281/// Parse a tree object from git format.
282///
283/// Tree entries store the referenced OID in **binary** form, which is not
284/// self-describing: a SHA-1 repository uses 20-byte OIDs and a SHA-256
285/// repository uses 32-byte OIDs. The caller must supply the repository's
286/// [`ObjectFormat`] — typically derived from the tree object's own hash
287/// (`GitHash::object_format`) — so the entries are sliced at the correct
288/// width instead of a hardcoded 20.
289pub fn parse_tree_object(data: &[u8], format: ObjectFormat) -> Result<TreeObject> {
290    // Parse git object header: "tree <size>\0<content>"
291    let null_pos = data
292        .iter()
293        .position(|&b| b == 0)
294        .ok_or_else(|| Error::other("Invalid git object format"))?;
295
296    let header = std::str::from_utf8(&data[..null_pos])?;
297    let mut content = &data[null_pos + 1..];
298
299    // Verify it's a tree
300    if !header.starts_with("tree ") {
301        return Err(Error::other("Not a tree object"));
302    }
303
304    let mut entries = Vec::new();
305
306    while !content.is_empty() {
307        // Parse mode and name (terminated by null byte)
308        let null_pos = content
309            .iter()
310            .position(|&b| b == 0)
311            .ok_or_else(|| Error::other("Invalid tree entry format"))?;
312
313        let entry_header = std::str::from_utf8(&content[..null_pos])?;
314        let space_pos = entry_header
315            .find(' ')
316            .ok_or_else(|| Error::other("Invalid tree entry header"))?;
317
318        let mode_str = &entry_header[..space_pos];
319        let name = &entry_header[space_pos + 1..];
320
321        // Parse mode
322        let mode = match mode_str {
323            "040000" => TreeEntryMode::Directory,
324            "100644" => TreeEntryMode::File,
325            "100755" => TreeEntryMode::Executable,
326            "120000" => TreeEntryMode::Symlink,
327            "160000" => TreeEntryMode::Gitlink,
328            _ => return Err(Error::other(format!("Unknown tree entry mode: {mode_str}"))),
329        };
330
331        // Hash is 20 bytes (SHA1) or 32 bytes (SHA256) after the null terminator.
332        // The width is not encoded in the tree object itself, so it comes from the
333        // repository's object format (carried in by the caller).
334        let hash_size = format.hash_len();
335        if content.len() < null_pos + 1 + hash_size {
336            return Err(Error::other("Tree entry missing hash"));
337        }
338
339        let hash_bytes = &content[null_pos + 1..null_pos + 1 + hash_size];
340        let hash_hex = hex::encode(hash_bytes);
341        let hash = GitHash::from_hex(&hash_hex)?;
342
343        entries.push(TreeEntry {
344            mode,
345            name: name.to_owned(),
346            hash,
347        });
348
349        // Move to next entry
350        content = &content[null_pos + 1 + hash_size..];
351    }
352
353    Ok(TreeObject { entries })
354}
355
356/// Parse a blob object from git format
357pub fn parse_blob_object(data: &[u8]) -> Result<Vec<u8>> {
358    // Parse git object header: "blob <size>\0<content>"
359    let null_pos = data
360        .iter()
361        .position(|&b| b == 0)
362        .ok_or_else(|| Error::other("Invalid git object format"))?;
363
364    let header = std::str::from_utf8(&data[..null_pos])?;
365
366    // Verify it's a blob
367    if !header.starts_with("blob ") {
368        return Err(Error::other("Not a blob object"));
369    }
370
371    // Return the content after the header
372    Ok(data[null_pos + 1..].to_vec())
373}
374
375/// Parse object references from git object data.
376///
377/// `format` is the repository's object format, needed to slice binary tree
378/// entries at the correct OID width. Callers derive it from the hash of the
379/// object being parsed (`GitHash::object_format`).
380pub fn parse_object_references(object_data: &[u8], format: ObjectFormat) -> Result<Vec<GitHash>> {
381    // Determine object type and parse accordingly
382    let null_pos = object_data
383        .iter()
384        .position(|&b| b == 0)
385        .ok_or_else(|| Error::other("Invalid git object format"))?;
386
387    let header = std::str::from_utf8(&object_data[..null_pos])?;
388
389    if header.starts_with("commit ") {
390        let commit = parse_commit_object(object_data)?;
391        let mut refs = vec![commit.tree_hash];
392        refs.extend(commit.parent_hashes);
393        Ok(refs)
394    } else if header.starts_with("tree ") {
395        let tree = parse_tree_object(object_data, format)?;
396        Ok(tree.entries.into_iter().map(|e| e.hash).collect())
397    } else {
398        // Blobs and tags don't reference other objects
399        Ok(Vec::new())
400    }
401}
402
403/// Write git objects to a repository
404pub async fn write_objects(repo_path: &Path, objects: &[GitObject]) -> Result<()> {
405    let repo = Repository::open(repo_path)?;
406    let odb = repo.odb()?;
407
408    for obj in objects {
409        // Parse the git format to get type and content
410        if let Some(null_pos) = obj.data.iter().position(|&b| b == 0) {
411            let header = &obj.data[..null_pos];
412            let content = &obj.data[null_pos + 1..];
413
414            // Parse "type size" header
415            if let Ok(header_str) = std::str::from_utf8(header) {
416                let parts: Vec<&str> = header_str.split(' ').collect();
417                if parts.len() == 2 {
418                    let obj_type = match parts[0] {
419                        "commit" => ObjectType::Commit,
420                        "tree" => ObjectType::Tree,
421                        "tag" => ObjectType::Tag,
422                        // "blob" or unknown types default to Blob
423                        _ => ObjectType::Blob,
424                    };
425
426                    // Write object to git database
427                    let oid = odb.write(obj_type, content)?;
428                    tracing::debug!("Wrote object {} as git object {}", obj.hash, oid);
429                }
430            }
431        }
432    }
433
434    Ok(())
435}
436
437/// Convert git2::Oid to GitHash (supports both SHA1 and SHA256)
438pub fn oid_to_hash(oid: Oid) -> Result<GitHash> {
439    // git2 Oid.to_string() returns the hex representation
440    // SHA1 = 40 hex chars, SHA256 = 64 hex chars
441    GitHash::from_hex(&oid.to_string())
442}
443
444/// Convert GitHash back to git2::Oid
445/// Note: git2 currently only supports SHA1, so SHA256 hashes will fail
446pub fn hash_to_oid(hash: &GitHash) -> Result<Oid> {
447    // git2 0.20.3 still primarily uses SHA1 Oids
448    // SHA256 support in libgit2 1.9.2 is available but not yet exposed in git2-rs
449    match hash {
450        GitHash::Sha1(_) => Oid::from_str(&hash.to_hex())
451            .map_err(|e| Error::other(format!("Cannot convert hash to Oid: {e}"))),
452        GitHash::Sha256(_) => {
453            // SHA256 git repos are not yet widely supported
454            // When git2 exposes SHA256 Oid support, this will work
455            Err(Error::other("SHA256 Oids not yet supported by git2-rs"))
456        }
457    }
458}
459
460// Keep legacy function for backwards compatibility during migration
461#[deprecated(note = "Use oid_to_hash instead - it supports both SHA1 and SHA256")]
462pub fn oid_to_sha256(oid: Oid) -> Result<Sha256Hash> {
463    let oid_str = oid.to_string();
464    if oid_str.len() == 64 {
465        Sha256Hash::new(oid_str)
466    } else {
467        Err(Error::other(
468            "SHA1 hashes are no longer supported, use SHA256",
469        ))
470    }
471}
472
473// Keep legacy function for backwards compatibility during migration
474#[deprecated(note = "Use hash_to_oid instead - it supports GitHash enum")]
475pub fn sha256_to_oid(hash: &Sha256Hash) -> Result<Oid> {
476    Oid::from_str(hash.as_str()).map_err(|e| {
477        Error::other(format!(
478            "Cannot convert SHA256 to Oid (git2 limitation): {e}"
479        ))
480    })
481}
482
483/// Extract git references from a repository
484pub async fn extract_git_refs(repo_path: &Path) -> Result<GitRefs> {
485    let repo = Repository::open(repo_path)?;
486    let mut refs_map = HashMap::new();
487
488    // Extract all references
489    let refs = repo.references()?;
490    for reference in refs {
491        let reference = reference?;
492        if let Some(name) = reference.name() {
493            if let Some(target) = reference.target() {
494                // Convert git2 Oid to GitHash (supports SHA1 and SHA256)
495                let hash = oid_to_hash(target)?;
496                refs_map.insert(name.to_owned(), hash);
497            }
498        }
499    }
500
501    // Get HEAD reference
502    let head = repo.head()?;
503    let head_name = head.name().unwrap_or("refs/heads/main").to_owned();
504
505    // Current timestamp
506    let created_at = std::time::SystemTime::now()
507        .duration_since(std::time::UNIX_EPOCH)?
508        .as_secs();
509
510    Ok(GitRefs {
511        refs: refs_map,
512        head: head_name,
513        created_at,
514    })
515}
516
517#[cfg(test)]
518#[allow(clippy::unwrap_used, clippy::expect_used)]
519mod tests {
520    use super::*;
521    use git2::Repository;
522    use std::fs;
523    use tempfile::TempDir;
524
525    /// Build the raw bytes of a git tree object with a single entry whose OID
526    /// is `oid` bytes wide, e.g. `"tree <size>\0100644 file\0<oid...>"`.
527    fn make_tree_object(name: &str, oid: &[u8]) -> Vec<u8> {
528        let mut content = Vec::new();
529        content.extend_from_slice(b"100644 ");
530        content.extend_from_slice(name.as_bytes());
531        content.push(0);
532        content.extend_from_slice(oid);
533
534        let mut object = format!("tree {}\0", content.len()).into_bytes();
535        object.extend_from_slice(&content);
536        object
537    }
538
539    #[test]
540    fn test_parse_tree_object_sha256_oid() {
541        // A SHA-256 repository stores 32-byte OIDs in tree entries.
542        let oid = [0xABu8; 32];
543        let object = make_tree_object("modern.txt", &oid);
544
545        let tree = parse_tree_object(&object, ObjectFormat::Sha256).unwrap();
546        assert_eq!(tree.entries.len(), 1);
547        let entry = &tree.entries[0];
548        assert_eq!(entry.name, "modern.txt");
549        assert_eq!(entry.hash, GitHash::Sha256(oid));
550        assert_eq!(entry.hash.to_hex(), hex::encode(oid));
551    }
552
553    #[test]
554    fn test_parse_tree_object_sha1_oid() {
555        // A SHA-1 repository stores 20-byte OIDs; the legacy path must still work.
556        let oid = [0x11u8; 20];
557        let object = make_tree_object("legacy.txt", &oid);
558
559        let tree = parse_tree_object(&object, ObjectFormat::Sha1).unwrap();
560        assert_eq!(tree.entries.len(), 1);
561        let entry = &tree.entries[0];
562        assert_eq!(entry.name, "legacy.txt");
563        assert_eq!(entry.hash, GitHash::Sha1(oid));
564    }
565
566    #[test]
567    fn test_parse_tree_object_sha256_with_sha1_width_is_wrong() {
568        // Regression guard for the hardcoded-20 bug: parsing a 32-byte-OID tree
569        // as SHA-1 must NOT silently yield the correct 32-byte hash.
570        let oid = [0xABu8; 32];
571        let object = make_tree_object("modern.txt", &oid);
572
573        // Parsing at the wrong (20-byte) width must not silently recover the
574        // correct 32-byte hash: it either errors or yields a corrupt entry.
575        // Threading the real object format is what avoids this.
576        if let Ok(tree) = parse_tree_object(&object, ObjectFormat::Sha1) {
577            assert_ne!(tree.entries[0].hash, GitHash::Sha256(oid));
578        }
579    }
580
581    #[test]
582    fn test_object_format_hash_len() {
583        assert_eq!(ObjectFormat::Sha1.hash_len(), 20);
584        assert_eq!(ObjectFormat::Sha256.hash_len(), 32);
585        assert_eq!(GitHash::Sha1([0u8; 20]).object_format(), ObjectFormat::Sha1);
586        assert_eq!(
587            GitHash::Sha256([0u8; 32]).object_format(),
588            ObjectFormat::Sha256
589        );
590    }
591
592    #[tokio::test]
593    async fn test_extract_objects() -> std::result::Result<(), Box<dyn std::error::Error>> {
594        let temp_dir = TempDir::new()?;
595
596        // Initialize repository and create a commit
597        let repo = Repository::init(temp_dir.path())?;
598
599        // Create a test file
600        let file_path = temp_dir.path().join("test.txt");
601        fs::write(&file_path, "Hello World")?;
602
603        // Add and commit
604        let mut index = repo.index()?;
605        index.add_path(Path::new("test.txt"))?;
606        index.write()?;
607
608        let tree_id = index.write_tree()?;
609        let tree = repo.find_tree(tree_id)?;
610        let sig = git2::Signature::now("Test", "test@example.com")?;
611
612        repo.commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])?;
613
614        // Extract objects
615        let objects = extract_objects(temp_dir.path()).await?;
616
617        // Should have at least: blob (file), tree, commit
618        assert!(objects.len() >= 3);
619
620        // Check that we have different object types
621        let types: std::collections::HashSet<_> = objects.iter().map(|o| &o.object_type).collect();
622        assert!(types.contains(&GitObjectType::Blob));
623        assert!(types.contains(&GitObjectType::Tree));
624        assert!(types.contains(&GitObjectType::Commit));
625        Ok(())
626    }
627
628    #[test]
629    fn test_is_exportable() -> std::result::Result<(), Box<dyn std::error::Error>> {
630        let temp_dir = TempDir::new()?;
631
632        // Initialize a new repository
633        let repo = Repository::init(temp_dir.path())?;
634        drop(repo);
635
636        // Should not be exportable initially
637        assert!(!is_exportable(temp_dir.path()));
638
639        // Create the export file
640        let export_file = temp_dir.path().join(".git/git-daemon-export-ok");
641        fs::write(export_file, "")?;
642
643        // Should now be exportable
644        assert!(is_exportable(temp_dir.path()));
645        Ok(())
646    }
647}