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
use crate::git::repository::{Repository, RepositoryError};
use std::path::{Path, PathBuf};

/// Main application struct that handles shared logic
pub struct GitPlumber {
    repo_path: PathBuf,
    repository: Option<Repository>,
}

impl GitPlumber {
    /// Create a new `GitPlumber` instance
    pub fn new(repo_path: impl AsRef<Path>) -> Self {
        let repo_path = repo_path.as_ref().to_path_buf();
        let repository = Repository::new(&repo_path).ok();

        Self {
            repo_path,
            repository,
        }
    }

    /// Get the repository path
    #[must_use]
    pub fn get_repo_path(&self) -> &Path {
        &self.repo_path
    }

    /// Get access to the repository if it exists
    #[must_use]
    pub const fn get_repository(&self) -> Option<&Repository> {
        self.repository.as_ref()
    }

    /// List all pack files in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when reading the objects/pack directory
    pub fn list_pack_files(&self) -> Result<Vec<PathBuf>, RepositoryError> {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            Repository::list_pack_files,
        )
    }

    /// List all pack-related files grouped by their base name
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when reading the objects/pack directory
    pub fn list_pack_groups(
        &self,
    ) -> Result<std::collections::HashMap<String, crate::git::repository::PackGroup>, RepositoryError>
    {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            Repository::list_pack_groups,
        )
    }

    /// List all head refs (local branches) in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when reading the refs/heads directory
    pub fn list_head_refs(&self) -> Result<Vec<PathBuf>, RepositoryError> {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            Repository::list_head_refs,
        )
    }

    /// List all remote refs in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when reading the refs/remotes directory
    pub fn list_remote_refs(&self) -> Result<Vec<(String, Vec<PathBuf>)>, RepositoryError> {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            Repository::list_remote_refs,
        )
    }

    /// List all tag refs in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when reading the refs/tags directory
    pub fn list_tag_refs(&self) -> Result<Vec<PathBuf>, RepositoryError> {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            Repository::list_tag_refs,
        )
    }

    /// Check if the repository has stash refs
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when checking for stash refs
    pub fn has_stash_ref(&self) -> Result<bool, RepositoryError> {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            Repository::has_stash_ref,
        )
    }

    /// List loose objects in the repository with a limit
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when reading loose object directories
    pub fn list_loose_objects(&self, limit: usize) -> Result<Vec<PathBuf>, RepositoryError> {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            |repo| repo.list_loose_objects(limit),
        )
    }

    /// List parsed loose objects in the repository with a limit
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when reading loose object directories
    /// - Loose objects cannot be parsed or decompressed
    pub fn list_parsed_loose_objects(
        &self,
        limit: usize,
    ) -> Result<Vec<crate::git::loose_object::LooseObject>, RepositoryError> {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            |repo| repo.list_parsed_loose_objects(limit),
        )
    }

    /// Get statistics about loose objects in the repository
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The path is not a valid git repository
    /// - File system operations fail when reading loose object directories
    /// - Loose objects cannot be parsed or analyzed
    pub fn get_loose_object_stats(
        &self,
    ) -> Result<crate::git::repository::LooseObjectStats, RepositoryError> {
        self.repository.as_ref().map_or_else(
            || {
                Err(RepositoryError::NotGitRepository(format!(
                    "{} is not a git repository",
                    self.repo_path.display()
                )))
            },
            Repository::get_loose_object_stats,
        )
    }

    /// Parse a pack file (basic analysis)
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The pack file cannot be read
    /// - The pack file format is invalid
    /// - Parsing operations fail
    pub fn parse_pack_file(&self, path: &Path) -> Result<(), String> {
        // Read the pack file
        let pack_data = std::fs::read(path).map_err(|e| format!("Error reading file: {e}"))?;

        // Parse the pack file
        match crate::git::pack::Header::parse(&pack_data) {
            Ok((objects_data, header)) => {
                crate::cli::safe_println(&format!("Pack version: {}", header.version))?;
                crate::cli::safe_println(&format!("Number of objects: {}", header.object_count))?;
                let mut remaining_data = objects_data;
                for i in 0..header.object_count {
                    match crate::git::pack::Object::parse(remaining_data) {
                        Ok((new_remaining_data, object)) => {
                            crate::cli::safe_println(&format!("{object}"))?;
                            remaining_data = new_remaining_data;
                        }
                        Err(e) => {
                            return Err(format!("Error parsing object: {e}"));
                        }
                    }
                    if i < header.object_count - 1 {
                        crate::cli::safe_println("--------------------------------")?;
                    }
                }
                Ok(())
            }
            Err(e) => Err(format!("Error parsing pack file: {e}")),
        }
    }

    /// Parse a pack file with detailed formatting for display
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The pack file cannot be read
    /// - The pack file format is invalid
    /// - Parsing or display formatting operations fail
    pub fn parse_pack_file_rich(&self, path: &Path) -> Result<(), String> {
        use crate::cli::formatters::CliPackFormatter;

        // Read the pack file
        let pack_data = std::fs::read(path).map_err(|e| format!("Error reading file: {e}"))?;

        // Parse the pack file header
        match crate::git::pack::Header::parse(&pack_data) {
            Ok((mut remaining_data, header)) => {
                let mut objects = Vec::new();

                // Parse all objects
                for _i in 0..header.object_count {
                    match crate::git::pack::Object::parse(remaining_data) {
                        Ok((new_remaining_data, object)) => {
                            objects.push(object);
                            remaining_data = new_remaining_data;
                        }
                        Err(e) => {
                            return Err(format!("Error parsing object: {e}"));
                        }
                    }
                }

                // Format and display the rich output
                let formatted_output = CliPackFormatter::format_pack_file(&header, &objects);
                crate::cli::safe_print(&formatted_output)?;

                Ok(())
            }
            Err(e) => Err(format!("Error parsing pack file: {e}")),
        }
    }

    /// View a file as a loose object with rich formatting
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The file cannot be read or parsed as a loose object
    /// - The formatting operations fail
    pub fn view_file_as_object(&self, path: &Path) -> Result<(), String> {
        use crate::cli::formatters::CliLooseFormatter;

        self.repository.as_ref().map_or_else(
            || {
                Err(format!(
                    "Not a git repository: {}",
                    self.repo_path.display()
                ))
            },
            |repo| match repo.read_loose_object(path) {
                Ok(loose_obj) => {
                    let formatted_output = CliLooseFormatter::format_loose_object(&loose_obj);
                    crate::cli::safe_print(&formatted_output)?;
                    Ok(())
                }
                Err(e) => Err(format!("Error reading loose object: {e}")),
            },
        )
    }

    /// View an object by hash with rich formatting
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - The object cannot be found by hash
    /// - Multiple objects match a partial hash (disambiguation needed)
    /// - The formatting operations fail
    pub fn view_object_by_hash(&self, hash: &str) -> Result<(), String> {
        use crate::cli::formatters::{CliLooseFormatter, CliPackFormatter};
        use std::fmt::Write;

        match self.repository.as_ref() {
            Some(_repo) => {
                // First try as loose object
                if let Ok(loose_obj) = self.find_loose_object_by_partial_hash(hash) {
                    let formatted_output = CliLooseFormatter::format_loose_object(&loose_obj);
                    crate::cli::safe_print(&formatted_output)?;
                    return Ok(());
                }

                // If not found in loose objects, search pack files
                if let Ok(pack_obj) = self.find_pack_object_by_partial_hash(hash) {
                    // Format pack object using existing formatter - create single object "pack file"
                    if let Some(ref object_data) = pack_obj.object_data {
                        let mut output = String::new();
                        writeln!(&mut output, "\x1b[1mPACK OBJECT (found by hash)\x1b[0m").unwrap();
                        writeln!(&mut output, "{}", "─".repeat(50)).unwrap();
                        writeln!(&mut output).unwrap();

                        // Create a PackObject from the found object and format it
                        let formatted_pack_obj = crate::tui::model::PackObject {
                            index: pack_obj.index,
                            obj_type: pack_obj.obj_type.clone(),
                            size: pack_obj.size,
                            sha1: pack_obj.sha1.clone(),
                            base_info: pack_obj.base_info.clone(),
                            object_data: Some(object_data.clone()),
                        };

                        let mut widget =
                            crate::tui::widget::pack_obj_details::PackObjectWidget::new(
                                formatted_pack_obj,
                            );
                        let formatted_text = widget.text();

                        // Convert ratatui Text to ANSI colored string (reuse formatter logic)
                        let colored_text = CliPackFormatter::text_to_ansi_string(&formatted_text);
                        output.push_str(&colored_text);

                        crate::cli::safe_print(&output)?;
                    } else {
                        // Fallback to basic info if no object data
                        crate::cli::safe_println("Pack Object (found by hash):")?;
                        crate::cli::safe_println(&format!(
                            "SHA1: {}",
                            pack_obj.sha1.as_deref().unwrap_or("unknown")
                        ))?;
                        crate::cli::safe_println(&format!("Type: {}", pack_obj.obj_type))?;
                        crate::cli::safe_println(&format!("Size: {} bytes", pack_obj.size))?;
                    }
                    return Ok(());
                }

                Err(format!("Object not found: {hash}"))
            }
            None => Err(format!(
                "Not a git repository: {}",
                self.repo_path.display()
            )),
        }
    }

    /// Find a loose object by partial hash (4-40 characters)
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - No matching objects are found
    /// - Multiple objects match the partial hash
    fn find_loose_object_by_partial_hash(
        &self,
        partial_hash: &str,
    ) -> Result<crate::git::loose_object::LooseObject, String> {
        // For full hash (40 chars), use direct lookup
        if partial_hash.len() == 40 {
            return self
                .repository
                .as_ref()
                .expect("Repository should be available for hash lookup")
                .read_loose_object_by_hash(partial_hash)
                .map_err(|e| format!("Object not found: {e}"));
        }

        // For partial hash, we need to search all loose objects
        match self.list_parsed_loose_objects(10000) {
            // Large limit for comprehensive search
            Ok(objects) => {
                let matches: Vec<_> = objects
                    .into_iter()
                    .filter(|obj| obj.object_id.starts_with(partial_hash))
                    .collect();

                match matches.len() {
                    0 => Err(format!("No loose objects found matching: {partial_hash}")),
                    1 => Ok(matches
                        .into_iter()
                        .next()
                        .expect("Should have exactly one match")),
                    _ => {
                        let mut error_msg = format!("Multiple objects match '{partial_hash}':\n");
                        for obj in matches {
                            use std::fmt::Write;
                            writeln!(&mut error_msg, "  {} ({})", obj.object_id, obj.object_type)
                                .expect("Writing to string should not fail");
                        }
                        Err(error_msg)
                    }
                }
            }
            Err(e) => Err(format!("Error searching loose objects: {e}")),
        }
    }

    /// Find a pack object by partial hash (4-40 characters)
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - No matching objects are found
    /// - Multiple objects match the partial hash
    /// - Pack files cannot be read or parsed
    fn find_pack_object_by_partial_hash(
        &self,
        partial_hash: &str,
    ) -> Result<crate::tui::model::PackObject, String> {
        use crate::git::pack::{Header, Object};
        use sha1::Digest;

        // Get all pack files
        let pack_files = self
            .list_pack_files()
            .map_err(|e| format!("Error listing pack files: {e}"))?;

        let mut matches = Vec::new();

        // Search through each pack file
        for pack_path in pack_files {
            let pack_data =
                std::fs::read(&pack_path).map_err(|e| format!("Error reading pack file: {e}"))?;

            if let Ok((mut remaining_data, header)) = Header::parse(&pack_data) {
                // Parse all objects in this pack file
                for index in 0..header.object_count {
                    if let Ok((new_remaining_data, object)) = Object::parse(remaining_data) {
                        // Calculate SHA-1 hash for this object
                        let obj_type = object.header.obj_type();
                        let size = object.header.uncompressed_data_size();
                        let mut hasher = sha1::Sha1::new();
                        let header_str = format!("{obj_type} {size}\0");
                        hasher.update(header_str.as_bytes());
                        hasher.update(&object.uncompressed_data);
                        let sha1 = format!("{:x}", hasher.finalize());

                        // Check if this hash matches our partial hash
                        if sha1.starts_with(partial_hash) {
                            let pack_obj = crate::tui::model::PackObject {
                                index: index as usize + 1,
                                obj_type: obj_type.to_string(),
                                size: u32::try_from(size).unwrap_or(u32::MAX),
                                sha1: Some(sha1),
                                base_info: None, // TODO: Add delta info if needed
                                object_data: Some(object),
                            };
                            matches.push(pack_obj);
                        }

                        remaining_data = new_remaining_data;
                    }
                }
            }
        }

        match matches.len() {
            0 => Err(format!("No pack objects found matching: {partial_hash}")),
            1 => Ok(matches
                .into_iter()
                .next()
                .expect("Should have exactly one match")),
            _ => {
                let mut error_msg = format!("Multiple pack objects match '{partial_hash}':\n");
                for obj in matches {
                    use std::fmt::Write;
                    writeln!(
                        &mut error_msg,
                        "  {} ({})",
                        obj.sha1.as_deref().unwrap_or("unknown"),
                        obj.obj_type
                    )
                    .expect("Writing to string should not fail");
                }
                Err(error_msg)
            }
        }
    }
}