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
use crate::git::pack::PackIndex;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::Line;

pub struct ObjectsFormatter<'a> {
    pack_index: &'a PackIndex,
}

impl<'a> ObjectsFormatter<'a> {
    pub fn new(pack_index: &'a PackIndex) -> Self {
        Self { pack_index }
    }

    /// Calculate the 1-based byte position in the .idx file for a given object entry
    ///
    /// Pack index file structure:
    /// - Header: 8 bytes (magic + version)
    /// - Fan-out table: 256 * 4 = 1024 bytes
    /// - Object names: N * 20 bytes (where N = object count)
    /// - CRC32 checksums: N * 4 bytes
    /// - Offsets: N * 4 bytes
    /// - Large offsets: variable (if any)
    /// - Pack checksum: 20 bytes
    /// - Index checksum: 20 bytes
    fn calculate_object_byte_position(&self, object_index: usize) -> u64 {
        let header_size = 8u64; // magic (4) + version (4)
        let fanout_size = 256 * 4u64; // 256 entries * 4 bytes each
        let object_names_start = header_size + fanout_size;

        // Each object name is 20 bytes
        // Add 1 to convert from 0-based to 1-based indexing
        object_names_start + (object_index as u64 * 20) + 1
    }

    /// Calculate the 1-based byte position in the .idx file for a CRC32 entry
    fn calculate_crc32_byte_position(&self, object_index: usize) -> u64 {
        let header_size = 8u64; // magic (4) + version (4)
        let fanout_size = 256 * 4u64; // 256 entries * 4 bytes each
        let object_names_size = self.pack_index.object_count() as u64 * 20; // N * 20 bytes
        let crc32_start = header_size + fanout_size + object_names_size;

        // Each CRC32 is 4 bytes
        // Add 1 to convert from 0-based to 1-based indexing
        crc32_start + (object_index as u64 * 4) + 1
    }

    /// Calculate the 1-based byte position in the .idx file for an offset entry
    fn calculate_offset_byte_position(&self, object_index: usize) -> u64 {
        let header_size = 8u64; // magic (4) + version (4)
        let fanout_size = 256 * 4u64; // 256 entries * 4 bytes each
        let object_names_size = self.pack_index.object_count() as u64 * 20; // N * 20 bytes
        let crc32_size = self.pack_index.object_count() as u64 * 4; // N * 4 bytes
        let offsets_start = header_size + fanout_size + object_names_size + crc32_size;

        // Each offset is 4 bytes
        // Add 1 to convert from 0-based to 1-based indexing
        offsets_start + (object_index as u64 * 4) + 1
    }

    pub fn format_objects_overview(&self, lines: &mut Vec<Line<'static>>) {
        self.format_object_names_table(lines);
        self.format_crc32_table(lines);
        self.format_offsets_table(lines);
    }

    fn format_object_names_table(&self, lines: &mut Vec<Line<'static>>) {
        lines.push(Line::styled(
            "OBJECT NAMES TABLE",
            Style::default().add_modifier(Modifier::BOLD),
        ));
        lines.push(Line::from("".repeat(25)));
        lines.push(Line::from(""));

        lines.push(Line::from(
            "SHA-1 hashes sorted lexicographically (20 bytes each)",
        ));
        lines.push(Line::from(format!(
            "Total objects: {}",
            self.pack_index.object_count()
        )));
        lines.push(Line::from(""));

        if self.pack_index.object_names.is_empty() {
            lines.push(Line::from("No objects in this index."));
            lines.push(Line::from(""));
            return;
        }

        // Table header
        lines.push(Line::from(
            "┌──────┬──────────────────────────────────────────┐",
        ));
        lines.push(Line::styled(
            "│ Byte │ SHA-1 Hash                               │",
            Style::default().add_modifier(Modifier::BOLD),
        ));
        lines.push(Line::from(
            "├──────┼──────────────────────────────────────────┤",
        ));

        let entries_to_show = self.determine_sample_indices();

        for (i, show_entry) in entries_to_show.iter().enumerate() {
            if *show_entry {
                let hash = hex::encode(self.pack_index.object_names[i]);
                let byte_pos = self.calculate_object_byte_position(i);

                let line_parts = vec![
                    ("".to_string(), Style::default()),
                    (format!("{:4}", byte_pos), Style::default().fg(Color::Cyan)),
                    ("".to_string(), Style::default()),
                    (hash, Style::default().fg(Color::Yellow)),
                    ("".to_string(), Style::default()),
                ];

                let mut line = Line::default();
                for (text, style) in line_parts {
                    line.spans.push(ratatui::text::Span::styled(text, style));
                }
                lines.push(line);
            } else if i > 0 && entries_to_show[i - 1] {
                // Show ellipsis after last shown entry
                let line_parts = vec![
                    ("".to_string(), Style::default()),
                    ("...".to_string(), Style::default().fg(Color::Gray)),
                    ("".to_string(), Style::default()),
                    (
                        "...                                     ".to_string(),
                        Style::default().fg(Color::Gray),
                    ),
                    ("".to_string(), Style::default()),
                ];

                let mut line = Line::default();
                for (text, style) in line_parts {
                    line.spans.push(ratatui::text::Span::styled(text, style));
                }
                lines.push(line);
            }
        }

        lines.push(Line::from(
            "└──────┴──────────────────────────────────────────┘",
        ));
        lines.push(Line::from(""));
    }

    fn format_crc32_table(&self, lines: &mut Vec<Line<'static>>) {
        lines.push(Line::styled(
            "CRC32 CHECKSUMS TABLE",
            Style::default().add_modifier(Modifier::BOLD),
        ));
        lines.push(Line::from("".repeat(30)));
        lines.push(Line::from(""));

        lines.push(Line::from(
            "CRC32 checksums for integrity verification (4 bytes each)",
        ));
        lines.push(Line::from(format!(
            "Total checksums: {}",
            self.pack_index.crc32_checksums.len()
        )));
        lines.push(Line::from(""));

        if self.pack_index.crc32_checksums.is_empty() {
            lines.push(Line::from("No CRC32 checksums available."));
            lines.push(Line::from(""));
            return;
        }

        // Table header
        lines.push(Line::from("┌──────┬─────────────┐"));
        lines.push(Line::styled(
            "│ Byte │ Hex Bytes   │",
            Style::default().add_modifier(Modifier::BOLD),
        ));
        lines.push(Line::from("├──────┼─────────────┤"));

        let entries_to_show = self.determine_sample_indices();

        for (i, show_entry) in entries_to_show.iter().enumerate() {
            if *show_entry {
                let crc32 = self.pack_index.crc32_checksums[i];
                let byte_pos = self.calculate_crc32_byte_position(i);
                let hex_bytes = format!(
                    "{:02x} {:02x} {:02x} {:02x}",
                    (crc32 >> 24) & 0xff,
                    (crc32 >> 16) & 0xff,
                    (crc32 >> 8) & 0xff,
                    crc32 & 0xff
                );

                let line_parts = vec![
                    ("".to_string(), Style::default()),
                    (format!("{:4}", byte_pos), Style::default().fg(Color::Cyan)),
                    ("".to_string(), Style::default()),
                    (
                        format!("{:11}", hex_bytes),
                        Style::default().fg(Color::Blue),
                    ),
                    ("".to_string(), Style::default()),
                ];

                let mut line = Line::default();
                for (text, style) in line_parts {
                    line.spans.push(ratatui::text::Span::styled(text, style));
                }
                lines.push(line);
            } else if i > 0 && entries_to_show[i - 1] {
                // Show ellipsis after last shown entry
                let line_parts = vec![
                    ("".to_string(), Style::default()),
                    ("...".to_string(), Style::default().fg(Color::Gray)),
                    ("".to_string(), Style::default()),
                    ("...".to_string(), Style::default().fg(Color::Gray)),
                    ("".to_string(), Style::default()),
                ];

                let mut line = Line::default();
                for (text, style) in line_parts {
                    line.spans.push(ratatui::text::Span::styled(text, style));
                }
                lines.push(line);
            }
        }

        lines.push(Line::from("└──────┴─────────────┘"));
        lines.push(Line::from(""));
    }

    fn format_offsets_table(&self, lines: &mut Vec<Line<'static>>) {
        lines.push(Line::styled(
            "PACK FILE OFFSETS TABLE",
            Style::default().add_modifier(Modifier::BOLD),
        ));
        lines.push(Line::from("".repeat(30)));
        lines.push(Line::from(""));

        lines.push(Line::from("Pack file byte positions (4 bytes each)"));
        lines.push(Line::from(format!(
            "Total offsets: {}",
            self.pack_index.offsets.len()
        )));

        // Show large offset info if present
        let large_offset_count = self
            .pack_index
            .offsets
            .iter()
            .filter(|&&offset| offset & 0x80000000 != 0)
            .count();

        if large_offset_count > 0 {
            lines.push(Line::styled(
                format!(
                    "Large offsets: {} (MSB set, using 8-byte table)",
                    large_offset_count
                ),
                Style::default().fg(Color::Yellow),
            ));
        }
        lines.push(Line::from(""));

        if self.pack_index.offsets.is_empty() {
            lines.push(Line::from("No offsets available."));
            lines.push(Line::from(""));
            return;
        }

        // Table header
        lines.push(Line::from("┌──────┬─────────────┬───────────┐"));
        lines.push(Line::styled(
            "│ Byte │ Hex Bytes   │ Offset    │",
            Style::default().add_modifier(Modifier::BOLD),
        ));
        lines.push(Line::from("├──────┼─────────────┼───────────┤"));

        let entries_to_show = self.determine_sample_indices();

        for (i, show_entry) in entries_to_show.iter().enumerate() {
            if *show_entry {
                let raw_offset = self.pack_index.offsets[i];
                let actual_offset = self.pack_index.get_object_offset(i);
                let byte_pos = self.calculate_offset_byte_position(i);
                let hex_bytes = format!(
                    "{:02x} {:02x} {:02x} {:02x}",
                    (raw_offset >> 24) & 0xff,
                    (raw_offset >> 16) & 0xff,
                    (raw_offset >> 8) & 0xff,
                    raw_offset & 0xff
                );

                let offset_display = if raw_offset & 0x80000000 != 0 {
                    format!("{:>8} L", actual_offset) // L for Large offset
                } else {
                    format!("{:>9}", actual_offset)
                };

                let line_parts = vec![
                    ("".to_string(), Style::default()),
                    (format!("{:4}", byte_pos), Style::default().fg(Color::Cyan)),
                    ("".to_string(), Style::default()),
                    (
                        format!("{:10}", hex_bytes),
                        Style::default().fg(Color::Blue),
                    ),
                    ("".to_string(), Style::default()),
                    (
                        offset_display,
                        if raw_offset & 0x80000000 != 0 {
                            Style::default().fg(Color::Yellow)
                        } else {
                            Style::default().fg(Color::Magenta)
                        },
                    ),
                    ("".to_string(), Style::default()),
                ];

                let mut line = Line::default();
                for (text, style) in line_parts {
                    line.spans.push(ratatui::text::Span::styled(text, style));
                }
                lines.push(line);
            } else if i > 0 && entries_to_show[i - 1] {
                // Show ellipsis after last shown entry
                let line_parts = vec![
                    ("".to_string(), Style::default()),
                    ("...".to_string(), Style::default().fg(Color::Gray)),
                    ("".to_string(), Style::default()),
                    ("...".to_string(), Style::default().fg(Color::Gray)),
                    ("".to_string(), Style::default()),
                    ("...".to_string(), Style::default().fg(Color::Gray)),
                    ("".to_string(), Style::default()),
                ];

                let mut line = Line::default();
                for (text, style) in line_parts {
                    line.spans.push(ratatui::text::Span::styled(text, style));
                }
                lines.push(line);
            }
        }

        lines.push(Line::from("└──────┴─────────────┴───────────┘"));
        lines.push(Line::from(""));

        // Add large offset table info if present
        if let Some(ref large_offsets) = self.pack_index.large_offsets {
            self.add_large_offsets_info(lines, large_offsets);
        }
    }

    fn add_large_offsets_info(&self, lines: &mut Vec<Line<'static>>, large_offsets: &[u64]) {
        lines.push(Line::styled(
            "Large Offset Table (8-byte offsets for pack files > 4GB):",
            Style::default().add_modifier(Modifier::BOLD),
        ));
        lines.push(Line::from(format!("Entries: {}", large_offsets.len())));

        if !large_offsets.is_empty() {
            let min_large = large_offsets.iter().min().unwrap_or(&0);
            let max_large = large_offsets.iter().max().unwrap_or(&0);
            lines.push(Line::from(format!(
                "Range: {} - {} bytes",
                min_large, max_large
            )));
        }
        lines.push(Line::from(""));
    }

    /// Determines which object indices should be shown in tables
    /// Shows first few, last few, and some from the middle if there are many objects
    fn determine_sample_indices(&self) -> Vec<bool> {
        let object_count = self.pack_index.object_count();
        let mut show_entry = vec![false; object_count];

        if object_count == 0 {
            return show_entry;
        }

        // For small collections, show all entries
        if object_count <= 10 {
            show_entry.fill(true);
            return show_entry;
        }

        // For larger collections, show strategic samples
        let sample_size = 3; // Show first 3, last 3, and some from middle

        // Always show first few entries
        for item in show_entry.iter_mut().take(sample_size.min(object_count)) {
            *item = true;
        }

        // Always show last few entries
        let last_start = object_count.saturating_sub(sample_size);
        for item in show_entry
            .iter_mut()
            .skip(last_start)
            .take(object_count - last_start)
        {
            *item = true;
        }

        // Show some from the middle if we have enough objects
        if object_count > 20 {
            let mid_start = (object_count / 2).saturating_sub(1);
            let mid_end = (mid_start + 2).min(object_count);

            for item in show_entry
                .iter_mut()
                .skip(mid_start)
                .take(mid_end - mid_start)
            {
                *item = true;
            }
        }

        show_entry
    }
}