gitj 0.1.0

A gitk-style, Windows 3.1-flavored git repository browser and commit helper built on the Saudade toolkit
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! An in-memory [`RepoBackend`] for tests and demos.
//!
//! [`FixtureBackend`] holds a hand-built commit history with deterministic
//! SHAs, timestamps, refs, file lists and diffs, so snapshot tests render
//! identical pixels on every machine without touching a real repository.

use std::cell::RefCell;
use std::collections::{HashMap, HashSet};

use super::{
    ChangeStatus, CommitInfo, Diff, DiffLine, DiffLineKind, FileChange, RefKind, RefLabel,
    RepoBackend, WorkingStatus,
};

/// A file changed by a commit, paired with the diff that produced it.
pub struct FileEntry {
    pub change: FileChange,
    pub diff: Diff,
}

/// One path in the simulated working tree. Real git tracks separate
/// working-vs-index and index-vs-HEAD diffs; the fixture keeps a single diff
/// per file and a `staged` flag, which is enough to drive and snapshot the
/// commit UI deterministically.
struct WorkingEntry {
    change: FileChange,
    diff: Diff,
    staged: bool,
}

pub struct FixtureBackend {
    path: String,
    commits: Vec<CommitInfo>,
    files: HashMap<usize, Vec<FileEntry>>,
    /// The simulated working tree, mutated by stage/unstage/commit.
    working: RefCell<Vec<WorkingEntry>>,
    /// Paths from the HEAD commit the user has pulled out of an in-progress
    /// amend (so they show as unstaged instead of staged while amending).
    amend_removed: RefCell<HashSet<String>>,
    /// The last commit performed, recorded for test assertions: (message, amend).
    last_commit: RefCell<Option<(String, bool)>>,
    /// The simulated commit identity returned by [`RepoBackend::signature`].
    signature: Option<(String, String)>,
}

impl FixtureBackend {
    pub fn new(path: impl Into<String>) -> Self {
        Self {
            path: path.into(),
            commits: Vec::new(),
            files: HashMap::new(),
            working: RefCell::new(Vec::new()),
            amend_removed: RefCell::new(HashSet::new()),
            last_commit: RefCell::new(None),
            signature: Some(("Robert Lillack".to_string(), "rob@example.com".to_string())),
        }
    }

    /// Override the simulated commit identity (or clear it with `None`).
    pub fn with_signature(mut self, signature: Option<(String, String)>) -> Self {
        self.signature = signature;
        self
    }

    /// The files belonging to the `HEAD` commit (index 0, newest first) — the
    /// changes that an amend would re-commit.
    fn head_files(&self) -> &[FileEntry] {
        self.files.get(&0).map(Vec::as_slice).unwrap_or(&[])
    }

    /// Append a commit and the files it touched.
    pub fn add_commit(&mut self, info: CommitInfo, files: Vec<FileEntry>) -> &mut Self {
        let idx = self.commits.len();
        self.commits.push(info);
        self.files.insert(idx, files);
        self
    }

    /// Add a path to the simulated working tree (for commit-mode tests/demos).
    pub fn add_working(
        &mut self,
        path: &str,
        status: ChangeStatus,
        staged: bool,
        diff_lines: &[(DiffLineKind, &str)],
    ) -> &mut Self {
        self.working.borrow_mut().push(WorkingEntry {
            change: FileChange {
                path: path.to_string(),
                old_path: None,
                status,
            },
            diff: diff(diff_lines),
            staged,
        });
        self
    }

    /// The most recent commit recorded via [`RepoBackend::commit`], as
    /// `(message, amend)` — exposed for tests.
    pub fn last_commit(&self) -> Option<(String, bool)> {
        self.last_commit.borrow().clone()
    }

    /// A small, realistic history used by snapshot tests and as a demo when no
    /// real repository is available. Five commits, two refs, varied statuses.
    pub fn sample() -> Self {
        let mut be = FixtureBackend::new("/home/rob/dev/journey");

        be.add_commit(
            commit(
                "a1b2c3d4e5f60718293a4b5c6d7e8f9012345678",
                "Add commit DAG graph view",
                "Robert Lillack",
                "rob@example.com",
                1_716_500_000,
                120,
                &["b2c3d4e5f60718293a4b5c6d7e8f90123456789a"],
                &[("main", RefKind::Head)],
            ),
            vec![file_entry(
                "src/widgets/graph.rs",
                None,
                ChangeStatus::Added,
                &[
                    (
                        DiffLineKind::FileHeader,
                        "diff --git a/src/widgets/graph.rs b/src/widgets/graph.rs",
                    ),
                    (DiffLineKind::FileHeader, "new file mode 100644"),
                    (DiffLineKind::HunkHeader, "@@ -0,0 +1,3 @@"),
                    (DiffLineKind::Addition, "+pub struct Graph {"),
                    (DiffLineKind::Addition, "+    lanes: Vec<Lane>,"),
                    (DiffLineKind::Addition, "+}"),
                ],
            )],
        );

        be.add_commit(
            commit(
                "b2c3d4e5f60718293a4b5c6d7e8f90123456789a",
                "Build basic file list per commit",
                "Robert Lillack",
                "rob@example.com",
                1_716_400_000,
                120,
                &["c3d4e5f60718293a4b5c6d7e8f90123456789ab2"],
                &[("v0.2", RefKind::Tag)],
            ),
            vec![
                file_entry(
                    "src/backend.rs",
                    None,
                    ChangeStatus::Modified,
                    &[
                        (
                            DiffLineKind::FileHeader,
                            "diff --git a/src/backend.rs b/src/backend.rs",
                        ),
                        (
                            DiffLineKind::HunkHeader,
                            "@@ -10,6 +10,10 @@ impl Backend {",
                        ),
                        (
                            DiffLineKind::Context,
                            "     pub fn log(&self) -> Vec<Commit> {",
                        ),
                        (
                            DiffLineKind::Addition,
                            "+        // collect changed files too",
                        ),
                        (DiffLineKind::Addition, "+        self.changed_files();"),
                        (DiffLineKind::Context, "         self.commits.clone()"),
                        (DiffLineKind::Context, "     }"),
                    ],
                ),
                file_entry(
                    "src/main.rs",
                    None,
                    ChangeStatus::Modified,
                    &[
                        (
                            DiffLineKind::FileHeader,
                            "diff --git a/src/main.rs b/src/main.rs",
                        ),
                        (DiffLineKind::HunkHeader, "@@ -42,7 +42,7 @@"),
                        (DiffLineKind::Deletion, "-    let files = vec![];"),
                        (
                            DiffLineKind::Addition,
                            "+    let files = backend.changed_files(idx);",
                        ),
                    ],
                ),
            ],
        );

        be.add_commit(
            commit(
                "c3d4e5f60718293a4b5c6d7e8f90123456789ab2",
                "Show path in title",
                "Robert Lillack",
                "rob@example.com",
                1_716_300_000,
                120,
                &["d4e5f60718293a4b5c6d7e8f90123456789ab2c3"],
                &[],
            ),
            vec![file_entry(
                "src/main.rs",
                None,
                ChangeStatus::Modified,
                &[
                    (
                        DiffLineKind::FileHeader,
                        "diff --git a/src/main.rs b/src/main.rs",
                    ),
                    (DiffLineKind::HunkHeader, "@@ -20,1 +20,1 @@ fn title()"),
                    (DiffLineKind::Deletion, "-        String::from(\"Journey\")"),
                    (
                        DiffLineKind::Addition,
                        "+        format!(\"Journey: {}\", path)",
                    ),
                ],
            )],
        );

        be.add_commit(
            commit(
                "d4e5f60718293a4b5c6d7e8f90123456789ab2c3",
                "Rename boldFont() -> bold_font()",
                "Robert Lillack",
                "rob@example.com",
                1_716_200_000,
                120,
                &["e5f60718293a4b5c6d7e8f90123456789ab2c3d4"],
                &[("origin/main", RefKind::RemoteBranch)],
            ),
            vec![file_entry(
                "src/style.rs",
                None,
                ChangeStatus::Modified,
                &[
                    (
                        DiffLineKind::FileHeader,
                        "diff --git a/src/style.rs b/src/style.rs",
                    ),
                    (DiffLineKind::HunkHeader, "@@ -80,4 +80,4 @@"),
                    (DiffLineKind::Deletion, "-pub fn boldFont() -> Font {"),
                    (DiffLineKind::Addition, "+pub fn bold_font() -> Font {"),
                ],
            )],
        );

        be.add_commit(
            commit(
                "e5f60718293a4b5c6d7e8f90123456789ab2c3d4",
                "Initial import",
                "Robert Lillack",
                "rob@example.com",
                1_716_100_000,
                120,
                &[],
                &[],
            ),
            vec![
                file_entry(
                    "Cargo.toml",
                    None,
                    ChangeStatus::Added,
                    &[
                        (
                            DiffLineKind::FileHeader,
                            "diff --git a/Cargo.toml b/Cargo.toml",
                        ),
                        (DiffLineKind::FileHeader, "new file mode 100644"),
                        (DiffLineKind::HunkHeader, "@@ -0,0 +1,2 @@"),
                        (DiffLineKind::Addition, "+[package]"),
                        (DiffLineKind::Addition, "+name = \"journey\""),
                    ],
                ),
                file_entry(
                    "src/main.rs",
                    None,
                    ChangeStatus::Added,
                    &[
                        (
                            DiffLineKind::FileHeader,
                            "diff --git a/src/main.rs b/src/main.rs",
                        ),
                        (DiffLineKind::FileHeader, "new file mode 100644"),
                        (DiffLineKind::HunkHeader, "@@ -0,0 +1,1 @@"),
                        (DiffLineKind::Addition, "+fn main() {}"),
                    ],
                ),
            ],
        );

        // A working tree with a realistic mix of staged and unstaged changes,
        // so commit mode has something to show.
        be.add_working(
            "src/ui.rs",
            ChangeStatus::Modified,
            false,
            &[
                (
                    DiffLineKind::FileHeader,
                    "diff --git a/src/ui.rs b/src/ui.rs",
                ),
                (
                    DiffLineKind::HunkHeader,
                    "@@ -40,6 +40,9 @@ impl GitClient {",
                ),
                (DiffLineKind::Context, "     fn sync(&mut self) {"),
                (
                    DiffLineKind::Addition,
                    "+        // refresh the working-tree panes",
                ),
                (DiffLineKind::Addition, "+        self.rescan();"),
                (DiffLineKind::Context, "         self.repaint();"),
                (DiffLineKind::Context, "     }"),
            ],
        );
        be.add_working(
            "notes.md",
            ChangeStatus::Untracked,
            false,
            &[
                (DiffLineKind::FileHeader, "diff --git a/notes.md b/notes.md"),
                (DiffLineKind::FileHeader, "new file mode 100644"),
                (DiffLineKind::HunkHeader, "@@ -0,0 +1,2 @@"),
                (DiffLineKind::Addition, "+# Notes"),
                (DiffLineKind::Addition, "+- wire up commit mode"),
            ],
        );
        be.add_working(
            "src/widgets/commit_panel.rs",
            ChangeStatus::Added,
            true,
            &[
                (
                    DiffLineKind::FileHeader,
                    "diff --git a/src/widgets/commit_panel.rs b/src/widgets/commit_panel.rs",
                ),
                (DiffLineKind::FileHeader, "new file mode 100644"),
                (DiffLineKind::HunkHeader, "@@ -0,0 +1,3 @@"),
                (DiffLineKind::Addition, "+pub struct CommitPanel {"),
                (DiffLineKind::Addition, "+    message: String,"),
                (DiffLineKind::Addition, "+}"),
            ],
        );
        be.add_working(
            "Cargo.toml",
            ChangeStatus::Modified,
            true,
            &[
                (
                    DiffLineKind::FileHeader,
                    "diff --git a/Cargo.toml b/Cargo.toml",
                ),
                (
                    DiffLineKind::HunkHeader,
                    "@@ -8,3 +8,4 @@ edition = \"2024\"",
                ),
                (DiffLineKind::Context, " [dependencies]"),
                (
                    DiffLineKind::Addition,
                    "+git2 = { version = \"0.18\", default-features = false }",
                ),
                (
                    DiffLineKind::Context,
                    " saudade = { path = \"../saudade\" }",
                ),
            ],
        );

        be
    }
}

impl RepoBackend for FixtureBackend {
    fn path(&self) -> &str {
        &self.path
    }

    fn commits(&self) -> &[CommitInfo] {
        &self.commits
    }

    fn changed_files(&self, index: usize) -> Vec<FileChange> {
        self.files
            .get(&index)
            .map(|entries| entries.iter().map(|e| e.change.clone()).collect())
            .unwrap_or_default()
    }

    fn commit_diff(&self, index: usize) -> Diff {
        let mut lines = Vec::new();
        if let Some(entries) = self.files.get(&index) {
            for entry in entries {
                lines.extend(entry.diff.lines.iter().cloned());
            }
        }
        Diff { lines }
    }

    fn file_diff(&self, index: usize, path: &str) -> Diff {
        self.files
            .get(&index)
            .and_then(|entries| entries.iter().find(|e| e.change.path == path))
            .map(|e| e.diff.clone())
            .unwrap_or_default()
    }

    fn working_status(&self, amend: bool) -> WorkingStatus {
        let mut status = WorkingStatus::default();
        for entry in self.working.borrow().iter() {
            if entry.staged {
                status.staged.push(entry.change.clone());
            } else {
                status.unstaged.push(entry.change.clone());
            }
        }
        // When amending, the HEAD commit's files join the staged side (they'll
        // be re-committed) unless the user has pulled them back out.
        if amend {
            let removed = self.amend_removed.borrow();
            for fe in self.head_files() {
                if removed.contains(&fe.change.path) {
                    status.unstaged.push(fe.change.clone());
                } else {
                    status.staged.push(fe.change.clone());
                }
            }
        }
        status
    }

    fn working_diff(&self, path: &str, _staged: bool, amend: bool) -> Diff {
        // The simulation keeps a single diff per path, so the staged/unstaged
        // side doesn't change which diff we show.
        if let Some(diff) = self
            .working
            .borrow()
            .iter()
            .find(|e| e.change.path == path)
            .map(|e| e.diff.clone())
        {
            return diff;
        }
        if amend
            && let Some(diff) = self
                .head_files()
                .iter()
                .find(|fe| fe.change.path == path)
                .map(|fe| fe.diff.clone())
        {
            return diff;
        }
        Diff::default()
    }

    fn stage(&self, path: &str) -> Result<(), String> {
        let mut found = false;
        for entry in self.working.borrow_mut().iter_mut() {
            if entry.change.path == path {
                entry.staged = true;
                found = true;
            }
        }
        // Re-staging a HEAD file that had been pulled out of an amend.
        if !found {
            self.amend_removed.borrow_mut().remove(path);
        }
        Ok(())
    }

    fn unstage(&self, path: &str, amend: bool) -> Result<(), String> {
        let mut found = false;
        for entry in self.working.borrow_mut().iter_mut() {
            if entry.change.path == path {
                entry.staged = false;
                found = true;
            }
        }
        // Dropping a HEAD file from the commit being amended.
        if !found && amend {
            self.amend_removed.borrow_mut().insert(path.to_string());
        }
        Ok(())
    }

    fn revert(&self, path: &str) -> Result<(), String> {
        // Drop the unstaged (working-vs-index) entry for this path, the
        // simulation's stand-in for restoring it from the index. Staged entries
        // and untracked files are left alone, matching the live backend.
        self.working.borrow_mut().retain(|e| {
            !(e.change.path == path && !e.staged && e.change.status != ChangeStatus::Untracked)
        });
        Ok(())
    }

    fn delete_untracked(&self, path: &str) -> Result<(), String> {
        // Removing the untracked file takes it out of the simulated working
        // tree entirely.
        self.working.borrow_mut().retain(|e| {
            !(e.change.path == path && !e.staged && e.change.status == ChangeStatus::Untracked)
        });
        Ok(())
    }

    fn commit(&self, message: &str, amend: bool) -> Result<(), String> {
        if message.trim().is_empty() {
            return Err("Please enter a commit message.".into());
        }
        // The staged changes are now part of HEAD; drop them from the
        // working set so the panes clear after committing.
        self.working.borrow_mut().retain(|e| !e.staged);
        self.amend_removed.borrow_mut().clear();
        *self.last_commit.borrow_mut() = Some((message.to_string(), amend));
        Ok(())
    }

    fn head_message(&self) -> Option<String> {
        self.commits.first().map(|c| c.message.clone())
    }

    fn signature(&self) -> Option<(String, String)> {
        self.signature.clone()
    }
}

/// Build a [`CommitInfo`] without the ceremony of naming every field.
#[allow(clippy::too_many_arguments)]
pub fn commit(
    id: &str,
    summary: &str,
    author: &str,
    email: &str,
    time_seconds: i64,
    time_offset_minutes: i32,
    parents: &[&str],
    refs: &[(&str, RefKind)],
) -> CommitInfo {
    CommitInfo {
        id: id.to_string(),
        short_id: id.chars().take(8).collect(),
        summary: summary.to_string(),
        message: format!("{summary}\n"),
        author_name: author.to_string(),
        author_email: email.to_string(),
        committer_name: author.to_string(),
        committer_email: email.to_string(),
        time_seconds,
        time_offset_minutes,
        parents: parents.iter().map(|p| p.to_string()).collect(),
        refs: refs
            .iter()
            .map(|(name, kind)| RefLabel {
                name: name.to_string(),
                kind: *kind,
            })
            .collect(),
    }
}

fn file_entry(
    path: &str,
    old_path: Option<&str>,
    status: ChangeStatus,
    diff_lines: &[(DiffLineKind, &str)],
) -> FileEntry {
    FileEntry {
        change: FileChange {
            path: path.to_string(),
            old_path: old_path.map(str::to_string),
            status,
        },
        diff: diff(diff_lines),
    }
}

/// Build a [`Diff`] from `(kind, text)` pairs.
fn diff(lines: &[(DiffLineKind, &str)]) -> Diff {
    Diff {
        lines: lines
            .iter()
            .map(|(kind, text)| DiffLine::new(*kind, text.to_string()))
            .collect(),
    }
}