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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use crate::git::loose_object::{LooseObject, LooseObjectError};
use crate::git::pack::{PackError, PackIndex};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Represents a group of pack-related files with the same base name
#[derive(Debug, Clone)]
pub struct PackGroup {
    pub base_name: String,
    pub pack_file: Option<PathBuf>,
    pub idx_file: Option<PathBuf>,
    pub rev_file: Option<PathBuf>,
    pub mtimes_file: Option<PathBuf>,
}

impl PackGroup {
    /// Creates a new PackGroup with the given base name
    pub fn new(base_name: &str) -> Self {
        Self {
            base_name: base_name.to_string(),
            pack_file: None,
            idx_file: None,
            rev_file: None,
            mtimes_file: None,
        }
    }

    /// Returns true if this group has at least a .pack file
    pub fn is_valid(&self) -> bool {
        self.pack_file.is_some()
    }

    /// Returns true if this group has both .pack and .idx files
    pub fn has_index(&self) -> bool {
        self.pack_file.is_some() && self.idx_file.is_some()
    }

    /// Returns all available file paths in this group
    pub fn get_all_files(&self) -> Vec<(&str, &PathBuf)> {
        let mut files = Vec::new();

        if let Some(ref path) = self.pack_file {
            files.push(("packfile", path));
        }
        if let Some(ref path) = self.idx_file {
            files.push(("index", path));
        }
        if let Some(ref path) = self.rev_file {
            files.push(("xedni", path)); // reversed index
        }
        if let Some(ref path) = self.mtimes_file {
            files.push(("mtime", path)); // mtimes
        }

        files
    }

    /// Load and parse the index file if available
    pub fn load_index(&self) -> Result<Option<PackIndex>, PackError> {
        if let Some(ref idx_path) = self.idx_file {
            match std::fs::read(idx_path) {
                Ok(data) => match PackIndex::parse(&data) {
                    Ok((_, index)) => Ok(Some(index)),
                    Err(e) => Err(PackError::ParseError(format!(
                        "Failed to parse index: {:?}",
                        e
                    ))),
                },
                Err(e) => Err(PackError::DecompressionError(e)),
            }
        } else {
            Ok(None)
        }
    }

    /// Look up an object by SHA-1 hash using the index file
    /// Returns the byte offset in the pack file if found
    pub fn lookup_object_offset(&self, sha1: &[u8; 20]) -> Result<Option<u64>, PackError> {
        match self.load_index()? {
            Some(index) => Ok(index.lookup_object(sha1)),
            None => Ok(None),
        }
    }

    /// Get basic statistics about the pack group
    pub fn get_stats(&self) -> Result<PackGroupStats, PackError> {
        let mut stats = PackGroupStats {
            base_name: self.base_name.clone(),
            has_pack: self.pack_file.is_some(),
            has_index: self.idx_file.is_some(),
            has_rev: self.rev_file.is_some(),
            has_mtimes: self.mtimes_file.is_some(),
            object_count: None,
            pack_size: None,
            index_size: None,
        };

        // Get pack file size
        if let Some(ref pack_path) = self.pack_file
            && let Ok(metadata) = std::fs::metadata(pack_path)
        {
            stats.pack_size = Some(metadata.len());
        }

        // Get index file size and object count
        if let Some(ref idx_path) = self.idx_file {
            if let Ok(metadata) = std::fs::metadata(idx_path) {
                stats.index_size = Some(metadata.len());
            }

            // Load index to get object count
            if let Ok(Some(index)) = self.load_index() {
                stats.object_count = Some(index.object_count());
            }
        }

        Ok(stats)
    }
}

/// Statistics about a pack group
#[derive(Debug, Clone)]
pub struct PackGroupStats {
    pub base_name: String,
    pub has_pack: bool,
    pub has_index: bool,
    pub has_rev: bool,
    pub has_mtimes: bool,
    pub object_count: Option<usize>,
    pub pack_size: Option<u64>,
    pub index_size: Option<u64>,
}

impl std::fmt::Display for PackGroupStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Pack Group: {}", self.base_name)?;

        if let Some(count) = self.object_count {
            writeln!(f, "Objects: {}", count)?;
        }

        if let Some(size) = self.pack_size {
            writeln!(f, "Pack size: {} bytes", size)?;
        }

        if let Some(size) = self.index_size {
            writeln!(f, "Index size: {} bytes", size)?;
        }

        writeln!(f, "Files present:")?;
        writeln!(f, "  Pack: {}", if self.has_pack { "✓" } else { "✗" })?;
        writeln!(f, "  Index: {}", if self.has_index { "✓" } else { "✗" })?;
        writeln!(f, "  Rev: {}", if self.has_rev { "✓" } else { "✗" })?;
        writeln!(f, "  Mtimes: {}", if self.has_mtimes { "✓" } else { "✗" })?;

        Ok(())
    }
}

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

    #[error("Not a git repository: {0}")]
    NotGitRepository(String),

    #[error("Loose object error: {0}")]
    LooseObjectError(#[from] LooseObjectError),

    #[error("Pack error: {0}")]
    PackError(#[from] PackError),
}

/// Statistics about loose objects in the repository
#[derive(Debug, Clone, Default)]
pub struct LooseObjectStats {
    pub total_count: usize,
    pub total_size: usize,
    pub commit_count: usize,
    pub tree_count: usize,
    pub blob_count: usize,
    pub tag_count: usize,
}

impl LooseObjectStats {
    /// Get a formatted summary of the statistics
    #[must_use]
    pub fn summary(&self) -> String {
        format!(
            "Total: {} objects ({} bytes)\nCommits: {}, Trees: {}, Blobs: {}, Annotated Tags: {}",
            self.total_count,
            self.total_size,
            self.commit_count,
            self.tree_count,
            self.blob_count,
            self.tag_count
        )
    }
}

/// Represents a Git repository
pub struct Repository {
    path: PathBuf,
}

impl Repository {
    /// Creates a new Repository instance from a path
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The provided path does not contain a .git directory
    pub fn new(path: impl AsRef<Path>) -> Result<Self, RepositoryError> {
        let path = path.as_ref().to_path_buf();
        let git_dir = path.join(".git");

        if !git_dir.exists() {
            return Err(RepositoryError::NotGitRepository(
                "No .git directory found".to_string(),
            ));
        }

        Ok(Self { path })
    }

    /// Returns the path to the repository
    #[must_use]
    pub fn get_path(&self) -> &Path {
        &self.path
    }

    /// Lists all pack files in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when reading the objects/pack directory
    pub fn list_pack_files(&self) -> Result<Vec<PathBuf>, RepositoryError> {
        let pack_dir = self.path.join(".git/objects/pack");

        if !pack_dir.exists() {
            return Ok(Vec::new()); // Return empty list if pack directory doesn't exist
        }

        let mut pack_files = Vec::new();
        for entry in fs::read_dir(pack_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_some_and(|ext| ext == "pack") {
                pack_files.push(path);
            }
        }

        Ok(pack_files)
    }

    /// Lists all pack-related files grouped by their base name (without extension)
    ///
    /// Returns a map where keys are base names (e.g., "pack-abc123") and values are
    /// structs containing paths to all related files (.pack, .idx, .rev, .mtimes)
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when reading the objects/pack directory
    pub fn list_pack_groups(&self) -> Result<HashMap<String, PackGroup>, RepositoryError> {
        let pack_dir = self.path.join(".git/objects/pack");

        if !pack_dir.exists() {
            return Ok(HashMap::new());
        }

        let mut pack_groups: HashMap<String, PackGroup> = HashMap::new();

        for entry in fs::read_dir(pack_dir)? {
            let entry = entry?;
            let path = entry.path();

            if let Some(extension) = path.extension().and_then(|ext| ext.to_str())
                && let Some(file_stem) = path.file_stem().and_then(|stem| stem.to_str())
            {
                let group = pack_groups
                    .entry(file_stem.to_string())
                    .or_insert_with(|| PackGroup::new(file_stem));

                match extension {
                    "pack" => group.pack_file = Some(path),
                    "idx" => group.idx_file = Some(path),
                    "rev" => group.rev_file = Some(path),
                    "mtimes" => group.mtimes_file = Some(path),
                    _ => {} // Ignore other extensions
                }
            }
        }

        Ok(pack_groups)
    }

    /// Lists all head refs (local branches) in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when reading the refs/heads directory
    pub fn list_head_refs(&self) -> Result<Vec<PathBuf>, RepositoryError> {
        Self::list_refs_in_dir(self.path.join(".git/refs/heads"))
    }

    /// Lists all remote refs grouped by remote name
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when reading the refs/remotes directory
    pub fn list_remote_refs(&self) -> Result<Vec<(String, Vec<PathBuf>)>, RepositoryError> {
        let remotes_dir = self.path.join(".git/refs/remotes");
        if !remotes_dir.exists() {
            return Ok(Vec::new());
        }

        let mut remotes = Vec::new();
        for entry in fs::read_dir(remotes_dir)? {
            let entry = entry?;
            if entry.path().is_dir() {
                let remote_name = entry.file_name().to_string_lossy().to_string();

                let remote_refs = Self::list_refs_in_dir(entry.path())?;
                remotes.push((remote_name, remote_refs));
            }
        }

        Ok(remotes)
    }

    /// Lists all tag refs in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when reading the refs/tags directory
    pub fn list_tag_refs(&self) -> Result<Vec<PathBuf>, RepositoryError> {
        Self::list_refs_in_dir(self.path.join(".git/refs/tags"))
    }

    /// Checks if stash ref exists
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when checking for stash refs
    pub fn has_stash_ref(&self) -> Result<bool, RepositoryError> {
        let stash_path = self.path.join(".git/refs/stash");
        Ok(stash_path.exists())
    }

    /// Helper method to list refs in a directory
    fn list_refs_in_dir(dir_path: PathBuf) -> Result<Vec<PathBuf>, RepositoryError> {
        if !dir_path.exists() {
            return Ok(Vec::new());
        }

        let mut refs = Vec::new();
        for entry in fs::read_dir(dir_path)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_file() {
                refs.push(path);
            }
        }

        Ok(refs)
    }

    /// Lists a sample of loose objects in the repository
    /// Limit parameter controls the maximum number of objects to return
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when reading loose object directories
    pub fn list_loose_objects(&self, limit: usize) -> Result<Vec<PathBuf>, RepositoryError> {
        let objects_dir = self.path.join(".git/objects");
        if !objects_dir.exists() {
            return Ok(Vec::new());
        }

        let mut loose_objects = Vec::new();
        let mut count = 0;

        for entry in fs::read_dir(&objects_dir)? {
            let entry = entry?;
            let dir_name = entry.file_name().to_string_lossy().to_string();

            // Skip info and pack directories
            if dir_name == "info" || dir_name == "pack" || !entry.path().is_dir() {
                continue;
            }

            if let Ok(subentries) = fs::read_dir(entry.path()) {
                for subentry in subentries.flatten() {
                    if count < limit {
                        loose_objects.push(subentry.path());
                        count += 1;
                    } else {
                        return Ok(loose_objects);
                    }
                }
            }
        }

        Ok(loose_objects)
    }

    /// Reads and parses a loose object from the given path
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The file cannot be read
    /// - The object cannot be decompressed or parsed
    pub fn read_loose_object(&self, path: &Path) -> Result<LooseObject, RepositoryError> {
        Ok(LooseObject::read_from_path(path)?)
    }

    /// Reads and parses a loose object by its hash
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The object file cannot be found or read
    /// - The object cannot be decompressed or parsed
    pub fn read_loose_object_by_hash(&self, hash: &str) -> Result<LooseObject, RepositoryError> {
        if hash.len() != 40 {
            return Err(RepositoryError::LooseObjectError(
                LooseObjectError::InvalidFormat("Hash must be 40 characters".to_string()),
            ));
        }

        let (dir, file) = hash.split_at(2);
        let path = self.path.join(".git/objects").join(dir).join(file);

        self.read_loose_object(&path)
    }

    /// List parsed loose objects with a limit
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when reading loose object directories  
    /// - Objects cannot be parsed or decompressed
    pub fn list_parsed_loose_objects(
        &self,
        limit: usize,
    ) -> Result<Vec<LooseObject>, RepositoryError> {
        let loose_object_paths = self.list_loose_objects(limit)?;
        let mut parsed_objects = Vec::new();

        for path in loose_object_paths {
            match self.read_loose_object(&path) {
                Ok(object) => parsed_objects.push(object),
                Err(e) => {
                    // Log the error but continue processing other objects
                    eprintln!(
                        "Warning: Failed to parse loose object {}: {e}",
                        path.display()
                    );
                }
            }
        }

        Ok(parsed_objects)
    }

    /// Check if a loose object exists by its hash
    #[must_use]
    pub fn loose_object_exists(&self, hash: &str) -> bool {
        if hash.len() != 40 {
            return false;
        }

        let (dir, file) = hash.split_at(2);
        let path = self.path.join(".git/objects").join(dir).join(file);

        path.exists() && path.is_file()
    }

    /// Get statistics about all loose objects in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - File system operations fail when reading loose object directories
    /// - Objects cannot be parsed or analyzed
    pub fn get_loose_object_stats(&self) -> Result<LooseObjectStats, RepositoryError> {
        let objects_dir = self.path.join(".git/objects");
        if !objects_dir.exists() {
            return Ok(LooseObjectStats::default());
        }

        let mut stats = LooseObjectStats::default();

        for entry in fs::read_dir(&objects_dir)? {
            let entry = entry?;
            let dir_name = entry.file_name().to_string_lossy().to_string();

            // Skip info and pack directories
            if dir_name == "info" || dir_name == "pack" || !entry.path().is_dir() {
                continue;
            }

            if let Ok(subentries) = fs::read_dir(entry.path()) {
                for subentry in subentries.flatten() {
                    if let Ok(object) = self.read_loose_object(&subentry.path()) {
                        stats.total_count += 1;
                        stats.total_size += object.size;

                        match object.object_type {
                            crate::git::loose_object::LooseObjectType::Commit => {
                                stats.commit_count += 1;
                            }
                            crate::git::loose_object::LooseObjectType::Tree => {
                                stats.tree_count += 1;
                            }
                            crate::git::loose_object::LooseObjectType::Blob => {
                                stats.blob_count += 1;
                            }
                            crate::git::loose_object::LooseObjectType::Tag => stats.tag_count += 1,
                        }
                    }
                }
            }
        }

        Ok(stats)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_list_pack_files() {
        // Create a temporary directory structure
        let temp_dir = tempfile::tempdir().unwrap();
        let git_dir = temp_dir.path().join(".git/objects/pack");
        fs::create_dir_all(&git_dir).unwrap();

        // Create some test pack files
        fs::write(git_dir.join("pack-1.pack"), b"").unwrap();
        fs::write(git_dir.join("pack-2.pack"), b"").unwrap();
        fs::write(git_dir.join("pack-1.idx"), b"").unwrap(); // Should be ignored

        let repo = Repository::new(temp_dir.path()).unwrap();
        let pack_files = repo.list_pack_files().unwrap();

        assert_eq!(pack_files.len(), 2);
        assert!(pack_files.iter().all(|p| p.extension().unwrap() == "pack"));
    }
}