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
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span, Text};
/// Educational content for different Git object types and categories
use std::collections::HashMap;

pub struct EducationalContent {
    content_map: HashMap<String, Text<'static>>,
}

impl EducationalContent {
    pub fn new() -> Self {
        let mut content_map = HashMap::new();

        // Pack files educational content
        content_map.insert(
            "Packs".to_string(),
            Text::from(
                "PACK FILES\n\nPack files are Git's way of efficiently storing objects.\n\
             Instead of storing each object separately, Git combines them\n\
             into a single file with delta compression for better efficiency.\n\
             This significantly reduces repository size and improves performance. \n\
             \n\
             \n\
             Pack file header is 12 bytes:\n\
             \n\
             │1 2 3 4│5 6 7 8│9 0 1 2│\n\
             ├───────┼───────┼───────┤\n\
             │  Sign │  Ver  │ Count │\n\
             ╰───────┴───────┴───────╯\n\
             \n\
             Sign. This is a file type signature, a.k.a. a magic number.\n\
             It helps to identify pack files even whithout `.pack` extension.\n\
             For pack files it is always 4 bytes - \"PACK\".\n\
             \n\
             Ver is the version number of the pack file format.\n\
             Currently versions 2 and 3 are supported. But only version 2 is set.\n\
             \n\
             Count is the number of objects in the pack file. Stored in network byte order.\n\
             \n\
             Theoretical maximum for both version and number of objects is 4G.",
            ),
        );

        // Pack Index files educational content
        content_map.insert(
            "Pack Index".to_string(),
            Text::from(
                "PACK INDEX FILES (.idx)\n\nPack index files provide efficient lookup into pack files.\n\
             Instead of scanning the entire pack file to find an object,\n\
             the index maps object SHA-1 hashes to their byte offsets.\n\
             This enables instant object access and verification.\n\
             \n\
             Index file structure (version 2):\n\
             \n\
             ┌─────────────────┐\n\
             │ Magic + Version │ 8 bytes (\\377tOc + version 2)\n\
             ├─────────────────┤\n\
             │   Fan-out Table │ 256 × 4 bytes (object counts by first byte)\n\
             ├─────────────────┤\n\
             │ Object Names    │ N × 20 bytes (sorted SHA-1 hashes)\n\
             ├─────────────────┤\n\
             │ CRC32 Table     │ N × 4 bytes (data integrity checksums)\n\
             ├─────────────────┤\n\
             │ Offset Table    │ N × 4 bytes (pack file byte offsets)\n\
             ├─────────────────┤\n\
             │ Large Offsets   │ Optional: M × 8 bytes (for big pack files)\n\
             ├─────────────────┤\n\
             │ Pack Checksum   │ 20 bytes (SHA-1 of corresponding pack)\n\
             ├─────────────────┤\n\
             │ Index Checksum  │ 20 bytes (SHA-1 of all index data)\n\
             └─────────────────┘\n\
             \n\
             The fan-out table enables binary search optimization:\n\
             - Entry N contains count of objects with first byte ≤ N\n\
             - Allows quick range determination for binary search\n\
             - Reduces average lookup from O(N) to O(log N)\n\
             \n\
             CRC32 checksums enable data integrity verification\n\
             without unpacking the entire object data.",
            ),
        );

        // References educational content
        content_map.insert(
            "Refs".to_string(),
            Text::from(
                "REFERENCES\n\nGit references are pointers to specific commits.\n\
             They help track branches, tags, and other important positions.\n\
             References make it easy to find commits without using hashes.\n\
             Common ref types: heads (branches), tags, remotes, and stash.",
            ),
        );

        // Heads (branches) educational content
        content_map.insert(
            "Heads".to_string(),
            Text::from(
                "BRANCHES (HEADS)\n\nBranches in Git are just references to specific commits.\n\
             Each branch is stored as a file in .git/refs/heads/.\n\
             The file contains the SHA-1 hash of the commit it points to.\n\
             When you commit to a branch, this reference is updated.",
            ),
        );

        // Remotes educational content
        content_map.insert(
            "Remotes".to_string(),
            Text::from(
                "REMOTE REFERENCES\n\nRemote references track branches from remote repositories.\n\
             They're stored in .git/refs/remotes/<remote-name>/.\n\
             These are updated when you fetch or pull from a remote.\n\
             Unlike local branches, you can't commit directly to them.",
            ),
        );

        // Tags educational content
        content_map.insert(
            "Tags".to_string(),
            Text::from(
                "TAGS\n\nTags are references that point to specific points in Git history.\n\
             Unlike branches, tags don't move as you make new commits.\n\
             They're stored in .git/refs/tags/ directory.\n\
             Lightweight tags are just refs, annotated tags are Git objects.",
            ),
        );

        // Loose Objects educational content
        content_map.insert(
            "Loose Objects".to_string(),
            Text::from(
                "LOOSE OBJECTS\n\nLoose objects are individual Git objects not yet packed.\n\
             They're stored in .git/objects/ with the first 2 hash chars as directory.\n\
             These include blobs (file contents), trees (directories),\n\
             commits (snapshots), and tags (references to commits).",
            ),
        );

        Self { content_map }
    }

    /// Get educational content for a specific category
    pub fn get_category_content(&self, category_name: &str) -> Text<'static> {
        self.content_map
            .get(category_name)
            .cloned()
            .unwrap_or_else(|| {
                Text::from(format!(
                    "Category: {category_name}\n\nSupport is coming soon"
                ))
            })
    }

    /// Get preview content for a reference file
    pub fn get_ref_preview(&self, content: &str) -> Text<'static> {
        Text::from(format!(
            "Reference Content\n\n{}\n\nThis reference points to the commit hash shown above.",
            content.trim()
        ))
    }

    /// Get preview content for a loose object
    pub fn get_loose_object_preview(&self, object_id: &str) -> Text<'static> {
        Text::from(format!(
            "Loose Object Preview\n\nObject ID: {object_id}\n\nThis is a raw Git object stored as a single file.\nUse 'cat-file' command to examine its contents."
        ))
    }

    /// Get pack file preview with detailed header breakdown
    pub fn get_pack_preview(&self, header: &crate::git::pack::Header) -> Text<'static> {
        let mut lines: Vec<Line> = Vec::new();
        let border_style = Style::default().fg(Color::Gray);
        let left_bit_style = Style::default().fg(Color::LightBlue);
        let right_bit_style = Style::default().fg(Color::LightGreen);

        // Ensure we have the expected 12 bytes of raw data
        if header.raw_data.len() < 12 {
            lines.push(Line::from("Error: Invalid header data"));
            return Text::from(lines);
        }

        lines.push(Line::from("Signature (magic number)").centered());
        lines.push(Line::styled(
            "byte│1        2        3        4",
            border_style,
        ));
        lines.push(Line::styled(
            "bit │76543210 76543210 76543210 76543210",
            border_style,
        ));
        lines.push(Line::from(vec![Span::styled(
            "    ├────────┼────────┼────────┼────────┼",
            border_style,
        )]));
        lines.push(Line::from(vec![
            Span::from("bin "),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[0] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[0] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[1] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[1] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[2] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[2] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[3] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[3] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
        ]));
        lines.push(Line::from(vec![
            Span::from("hex "),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[0] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[0] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[1] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[1] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[2] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[2] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[3] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[3] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
        ]));
        lines.push(Line::from(vec![
            Span::from("utf8"),
            Span::styled("", border_style),
            Span::styled("  ╰─", left_bit_style),
            Span::from(format!("{}", header.raw_data[0] as char)),
            Span::styled("─╯ ", right_bit_style),
            Span::styled("", border_style),
            Span::styled("  ╰─", left_bit_style),
            Span::from(format!("{}", header.raw_data[1] as char)),
            Span::styled("─╯ ", right_bit_style),
            Span::styled("", border_style),
            Span::styled("  ╰─", left_bit_style),
            Span::from(format!("{}", header.raw_data[2] as char)),
            Span::styled("─╯ ", right_bit_style),
            Span::styled("", border_style),
            Span::styled("  ╰─", left_bit_style),
            Span::from(format!("{}", header.raw_data[3] as char)),
            Span::styled("─╯ ", right_bit_style),
            Span::styled("", border_style),
        ]));
        lines.push(Line::from(vec![Span::styled(
            "    ╰────────┴────────┴────────┴────────┴",
            border_style,
        )]));
        lines.push(Line::from(""));

        lines.push(Line::from(format!("Version: {}", header.version)).centered());
        lines.push(Line::styled(
            "byte 5        6        7        8",
            border_style,
        ));
        lines.push(Line::styled(
            "bit  76543210 76543210 76543210 76543210",
            border_style,
        ));
        lines.push(Line::from(vec![Span::styled(
            "    ┼────────┼────────┼────────┼────────┼",
            border_style,
        )]));
        lines.push(Line::from(vec![
            Span::from("bin "),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[4] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[4] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[5] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[5] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[6] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[6] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[7] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[7] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
        ]));
        lines.push(Line::from(vec![
            Span::from("hex "),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[4] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[4] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[5] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01x}", header.raw_data[5] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[6] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[6] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[7] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[7] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
        ]));
        lines.push(Line::from(vec![Span::styled(
            "    ┴────────┴────────┴────────┴────────┴",
            border_style,
        )]));
        lines.push(Line::from(""));

        lines.push(Line::from(format!("Number of objects: {}", header.object_count)).centered());
        lines.push(Line::styled(
            "byte 9        10       11       12      │",
            border_style,
        ));
        lines.push(Line::styled(
            "bit  76543210 76543210 76543210 76543210│",
            border_style,
        ));
        lines.push(Line::from(vec![Span::styled(
            "    ┼────────┼────────┼────────┼────────┤",
            border_style,
        )]));
        lines.push(Line::from(vec![
            Span::from("bin "),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[8] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[8] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[9] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[9] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[10] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[10] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(format!("{:04b}", header.raw_data[11] >> 4), left_bit_style),
            Span::styled(
                format!("{:04b}", header.raw_data[11] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
        ]));
        lines.push(Line::from(vec![
            Span::from("hex "),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[8] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[8] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[9] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[9] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[10] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[10] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[11] >> 4),
                left_bit_style,
            ),
            Span::styled(
                format!("╰─{:01X}", header.raw_data[11] & 0x0F),
                right_bit_style,
            ),
            Span::styled("", border_style),
        ]));
        lines.push(Line::from(vec![Span::styled(
            "    ┴────────┴────────┴────────┴────────╯",
            border_style,
        )]));

        Text::from(lines)
    }

    /// Get default content when no object is selected
    pub fn get_default_content(&self) -> Text<'static> {
        Text::from("Select an object to view details")
    }
}

impl Default for EducationalContent {
    fn default() -> Self {
        Self::new()
    }
}