git-plumber 0.1.3

Explore git internals, the plumbing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use flate2::read::ZlibDecoder;
use std::fs;
use std::io::Read;
use std::path::Path;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum LooseObjectError {
    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    #[error("Invalid object format: {0}")]
    InvalidFormat(String),

    #[error("Unknown object type: {0}")]
    UnknownType(String),

    #[error("Decompression error: {0}")]
    DecompressionError(String),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LooseObjectType {
    Commit,
    Tree,
    Blob,
    Tag,
}

impl std::fmt::Display for LooseObjectType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Commit => write!(f, "commit"),
            Self::Tree => write!(f, "tree"),
            Self::Blob => write!(f, "blob"),
            Self::Tag => write!(f, "tag"),
        }
    }
}

impl std::str::FromStr for LooseObjectType {
    type Err = LooseObjectError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "commit" => Ok(Self::Commit),
            "tree" => Ok(Self::Tree),
            "blob" => Ok(Self::Blob),
            "tag" => Ok(Self::Tag),
            _ => Err(LooseObjectError::UnknownType(s.to_string())),
        }
    }
}

// Parsed commit object structure
#[derive(Debug, Clone)]
pub struct CommitObject {
    pub tree: String,
    pub parents: Vec<String>,
    pub author: String,
    pub author_date: String,
    pub committer: String,
    pub committer_date: String,
    pub message: String,
}

// Parsed tree entry structure
#[derive(Debug, Clone)]
pub struct TreeEntry {
    pub mode: String,
    pub name: String,
    pub sha1: String,
    pub object_type: TreeEntryType,
}

#[derive(Debug, Clone)]
pub enum TreeEntryType {
    Blob,
    Tree,
    Executable,
    Symlink,
    Submodule,
}

// Parsed tree object structure
#[derive(Debug, Clone)]
pub struct TreeObject {
    pub entries: Vec<TreeEntry>,
}

// Parsed tag object structure
#[derive(Debug, Clone)]
pub struct TagObject {
    pub object: String,
    pub object_type: String,
    pub tag: String,
    pub tagger: Option<String>,
    pub tagger_date: Option<String>,
    pub message: String,
}

// Parsed object content
#[derive(Debug, Clone)]
pub enum ParsedContent {
    Commit(CommitObject),
    Tree(TreeObject),
    Blob(Vec<u8>),
    Tag(TagObject),
}

#[derive(Debug, Clone)]
pub struct LooseObject {
    pub object_type: LooseObjectType,
    pub size: usize,
    pub content: Vec<u8>,
    pub object_id: String,
    pub parsed_content: Option<ParsedContent>,
}

impl LooseObject {
    /// Read and parse a loose object from the given path
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The file path cannot be read
    /// - The object ID cannot be extracted from the path
    /// - The file cannot be decompressed
    /// - The object format is invalid
    /// - The object type is unknown
    pub fn read_from_path(path: &Path) -> Result<Self, LooseObjectError> {
        // Extract object ID from path
        let object_id = Self::extract_object_id(path)?;

        // Read the compressed file
        let compressed_data = fs::read(path)?;

        // Decompress the data
        let mut decoder = ZlibDecoder::new(&compressed_data[..]);
        let mut decompressed = Vec::new();
        decoder
            .read_to_end(&mut decompressed)
            .map_err(|e| LooseObjectError::DecompressionError(e.to_string()))?;

        // Parse the object header and content
        Self::parse_object_data(&decompressed, object_id)
    }

    /// Extract object ID from the file path
    /// Path format: .git/objects/ab/cdef123456...
    fn extract_object_id(path: &Path) -> Result<String, LooseObjectError> {
        let filename = path
            .file_name()
            .ok_or_else(|| LooseObjectError::InvalidFormat("No filename".to_string()))?
            .to_string_lossy();

        let parent_dir = path
            .parent()
            .ok_or_else(|| LooseObjectError::InvalidFormat("No parent directory".to_string()))?
            .file_name()
            .ok_or_else(|| LooseObjectError::InvalidFormat("No parent directory name".to_string()))?
            .to_string_lossy();

        if parent_dir.len() != 2 {
            return Err(LooseObjectError::InvalidFormat(
                "Parent directory should be 2 characters".to_string(),
            ));
        }

        if filename.len() != 38 {
            return Err(LooseObjectError::InvalidFormat(
                "Filename should be 38 characters".to_string(),
            ));
        }

        Ok(format!("{parent_dir}{filename}"))
    }

    /// Parse the decompressed object data
    /// Format: "<type> <size>\0<content>"
    fn parse_object_data(data: &[u8], object_id: String) -> Result<Self, LooseObjectError> {
        // Find the null terminator that separates header from content
        let null_pos = data.iter().position(|&b| b == 0).ok_or_else(|| {
            LooseObjectError::InvalidFormat("No null terminator found".to_string())
        })?;

        // Split header and content
        let header = &data[..null_pos];
        let content = &data[null_pos + 1..];

        // Parse header: "<type> <size>"
        let header_str = String::from_utf8_lossy(header);
        let parts: Vec<&str> = header_str.split(' ').collect();

        if parts.len() != 2 {
            return Err(LooseObjectError::InvalidFormat(
                "Header should contain type and size".to_string(),
            ));
        }

        let object_type = parts[0].parse::<LooseObjectType>()?;
        let size = parts[1]
            .parse::<usize>()
            .map_err(|_| LooseObjectError::InvalidFormat("Invalid size".to_string()))?;

        // Verify size matches content length
        if size != content.len() {
            return Err(LooseObjectError::InvalidFormat(format!(
                "Size mismatch: header says {}, content is {}",
                size,
                content.len()
            )));
        }

        // Parse type-specific content
        let parsed_content = match object_type {
            LooseObjectType::Commit => {
                Some(ParsedContent::Commit(Self::parse_commit_content(content)))
            }
            LooseObjectType::Tree => Some(ParsedContent::Tree(Self::parse_tree_content(content))),
            LooseObjectType::Blob => Some(ParsedContent::Blob(content.to_vec())),
            LooseObjectType::Tag => Some(ParsedContent::Tag(Self::parse_tag_content(content))),
        };

        Ok(Self {
            object_type,
            size,
            content: content.to_vec(),
            object_id,
            parsed_content,
        })
    }

    /// Parse commit object content
    fn parse_commit_content(content: &[u8]) -> CommitObject {
        let content_str = String::from_utf8_lossy(content);
        let lines = content_str.lines();

        let mut tree = String::new();
        let mut parents = Vec::new();
        let mut author = String::new();
        let mut author_date = String::new();
        let mut committer = String::new();
        let mut committer_date = String::new();
        let mut message = String::new();

        // Parse header lines
        let mut in_message = false;
        for line in lines {
            if in_message {
                if !message.is_empty() {
                    message.push('\n');
                }
                message.push_str(line);
            } else if line.is_empty() {
                in_message = true;
            } else if let Some(stripped) = line.strip_prefix("tree ") {
                tree = stripped.to_string();
            } else if let Some(stripped) = line.strip_prefix("parent ") {
                parents.push(stripped.to_string());
            } else if let Some(author_line) = line.strip_prefix("author ") {
                if let Some(date_start) = author_line.rfind(' ')
                    && let Some(name_end) = author_line[..date_start].rfind(' ')
                {
                    author = author_line[..name_end].to_string();
                    author_date = author_line[name_end + 1..].to_string();
                }
            } else if let Some(committer_line) = line.strip_prefix("committer ")
                && let Some(date_start) = committer_line.rfind(' ')
                && let Some(name_end) = committer_line[..date_start].rfind(' ')
            {
                committer = committer_line[..name_end].to_string();
                committer_date = committer_line[name_end + 1..].to_string();
            }
        }

        CommitObject {
            tree,
            parents,
            author,
            author_date,
            committer,
            committer_date,
            message,
        }
    }

    /// Parse tree object content
    fn parse_tree_content(content: &[u8]) -> TreeObject {
        let mut entries = Vec::new();
        let mut i = 0;

        while i < content.len() {
            // Read mode (until space)
            let mode_start = i;
            while i < content.len() && content[i] != b' ' {
                i += 1;
            }
            if i >= content.len() {
                break;
            }
            let mode = String::from_utf8_lossy(&content[mode_start..i]).to_string();
            i += 1; // Skip space

            // Read filename (until null)
            let name_start = i;
            while i < content.len() && content[i] != 0 {
                i += 1;
            }
            if i >= content.len() {
                break;
            }
            let name = String::from_utf8_lossy(&content[name_start..i]).to_string();
            i += 1; // Skip null

            // Read SHA-1 (20 bytes)
            if i + 20 > content.len() {
                break;
            }
            let sha1 = hex::encode(&content[i..i + 20]);
            i += 20;

            // Determine object type from mode
            let object_type = match mode.as_str() {
                "100755" => TreeEntryType::Executable,
                "120000" => TreeEntryType::Symlink,
                "160000" => TreeEntryType::Submodule,
                "040000" => TreeEntryType::Tree,
                _ => TreeEntryType::Blob, // Default fallback for "100644" and others
            };

            entries.push(TreeEntry {
                mode,
                name,
                sha1,
                object_type,
            });
        }

        TreeObject { entries }
    }

    /// Parse tag object content
    fn parse_tag_content(content: &[u8]) -> TagObject {
        let content_str = String::from_utf8_lossy(content);
        let lines = content_str.lines();

        let mut object = String::new();
        let mut object_type = String::new();
        let mut tag = String::new();
        let mut tagger = None;
        let mut tagger_date = None;
        let mut message = String::new();

        // Parse header lines
        let mut in_message = false;
        for line in lines {
            if in_message {
                if !message.is_empty() {
                    message.push('\n');
                }
                message.push_str(line);
            } else if line.is_empty() {
                in_message = true;
            } else if let Some(stripped) = line.strip_prefix("object ") {
                object = stripped.to_string();
            } else if let Some(stripped) = line.strip_prefix("type ") {
                object_type = stripped.to_string();
            } else if let Some(stripped) = line.strip_prefix("tag ") {
                tag = stripped.to_string();
            } else if let Some(tagger_line) = line.strip_prefix("tagger ")
                && let Some(date_start) = tagger_line.rfind(' ')
                && let Some(name_end) = tagger_line[..date_start].rfind(' ')
            {
                tagger = Some(tagger_line[..name_end].to_string());
                tagger_date = Some(tagger_line[name_end + 1..].to_string());
            }
        }

        TagObject {
            object,
            object_type,
            tag,
            tagger,
            tagger_date,
            message,
        }
    }

    /// Get the content as a UTF-8 string (for text objects like commits)
    #[must_use]
    pub fn content_as_string(&self) -> String {
        String::from_utf8_lossy(&self.content).to_string()
    }

    /// Check if this object is binary (likely a blob)
    #[must_use]
    pub fn is_binary(&self) -> bool {
        // Simple heuristic: if content contains null bytes, it's likely binary
        self.content.contains(&0) || matches!(self.object_type, LooseObjectType::Blob)
    }

    /// Get parsed content if available
    #[must_use]
    pub const fn get_parsed_content(&self) -> Option<&ParsedContent> {
        self.parsed_content.as_ref()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use flate2::Compression;
    use flate2::write::ZlibEncoder;
    use std::io::Write;

    #[test]
    fn test_extract_object_id() {
        let path = Path::new(".git/objects/ab/cdef1234567890123456789012345678901234");
        let object_id = LooseObject::extract_object_id(path).unwrap();
        assert_eq!(object_id, "abcdef1234567890123456789012345678901234");
    }

    #[test]
    fn test_parse_object_data() {
        let content = b"Hello, World!";
        let header = b"blob 13\0";
        let mut data = Vec::new();
        data.extend_from_slice(header);
        data.extend_from_slice(content);

        let object = LooseObject::parse_object_data(&data, "test123".to_string()).unwrap();
        assert_eq!(object.object_type, LooseObjectType::Blob);
        assert_eq!(object.size, 13);
        assert_eq!(object.content, content);
        assert_eq!(object.object_id, "test123");
    }

    #[test]
    fn test_create_and_read_loose_object() {
        let temp_dir = tempfile::tempdir().unwrap();
        let objects_dir = temp_dir.path().join("objects").join("ab");
        std::fs::create_dir_all(&objects_dir).unwrap();

        // Create test object content
        let content = b"Hello, World!";
        let header = b"blob 13\0";
        let mut data = Vec::new();
        data.extend_from_slice(header);
        data.extend_from_slice(content);

        // Compress the data
        let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
        encoder.write_all(&data).unwrap();
        let compressed = encoder.finish().unwrap();

        // Write to file
        let file_path = objects_dir.join("cdef1234567890123456789012345678901234");
        std::fs::write(&file_path, compressed).unwrap();

        // Read and parse
        let object = LooseObject::read_from_path(&file_path).unwrap();
        assert_eq!(object.object_type, LooseObjectType::Blob);
        assert_eq!(object.size, 13);
        assert_eq!(object.content, content);
        assert_eq!(object.object_id, "abcdef1234567890123456789012345678901234");
    }

    #[test]
    fn test_parse_commit_content() {
        let content = b"tree 1234567890123456789012345678901234567890\nparent abcdef1234567890123456789012345678901234\nauthor John Doe <john@example.com> 1234567890 +0000\ncommitter John Doe <john@example.com> 1234567890 +0000\n\nInitial commit\n";

        let commit = LooseObject::parse_commit_content(content);
        assert_eq!(commit.tree, "1234567890123456789012345678901234567890");
        assert_eq!(commit.parents.len(), 1);
        assert_eq!(
            commit.parents[0],
            "abcdef1234567890123456789012345678901234"
        );
        assert!(commit.author.contains("John Doe"));
        assert_eq!(commit.message, "Initial commit");
    }
}