gitstack 5.3.0

Git history viewer with insights - Author stats, file heatmap, code ownership
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
//! Application type definitions (InputMode, QuickAction, SidebarPanel, CommitType, etc.)

use std::collections::HashSet;

use crate::compare::{BranchCompare, CompareTab};
use crate::git::{BlameLine, FileHistoryEntry, FilePatch, StashEntry};
use crate::i18n::Language;
use crate::insights::{ActionRecommendation, HandoffContext, ReviewPack};
use crate::navigation::ListNavigation;
use crate::pr::PrCreateState;
use crate::related_files::RelatedFiles;
use crate::review_queue::ReviewQueue;
use crate::stats::{
    ChangeCouplingAnalysis, CodeOwnership, CommitImpactAnalysis, CommitQualityAnalysis,
    FileHeatmap, ProjectHealth, RepoStats,
};

// ===== View state structs =====

/// Stats view state (cache + navigation)
#[derive(Default)]
pub struct StatsViewState {
    pub cache: Option<RepoStats>,
    pub nav: ListNavigation,
}

/// Heatmap view state
#[derive(Default)]
pub struct HeatmapViewState {
    pub cache: Option<FileHeatmap>,
    pub nav: ListNavigation,
}

/// File history view state
#[derive(Default)]
pub struct FileHistoryViewState {
    pub cache: Option<Vec<FileHistoryEntry>>,
    pub path: Option<String>,
    pub nav: ListNavigation,
}

/// Blame view state
#[derive(Default)]
pub struct BlameViewState {
    pub cache: Option<Vec<BlameLine>>,
    pub path: Option<String>,
    pub nav: ListNavigation,
}

/// Code ownership view state
#[derive(Default)]
pub struct OwnershipViewState {
    pub cache: Option<CodeOwnership>,
    pub nav: ListNavigation,
}

/// Stash view state
#[derive(Default)]
pub struct StashViewState {
    pub cache: Option<Vec<StashEntry>>,
    pub nav: ListNavigation,
}

/// Patch view state
#[derive(Default)]
pub struct PatchViewState {
    pub cache: Option<FilePatch>,
    pub scroll_offset: usize,
}

/// Branch compare view state
#[derive(Default)]
pub struct BranchCompareViewState {
    pub cache: Option<BranchCompare>,
    pub tab: CompareTab,
    pub nav: ListNavigation,
}

/// Related files view state
#[derive(Default)]
pub struct RelatedFilesViewState {
    pub cache: Option<RelatedFiles>,
    pub nav: ListNavigation,
}

/// Impact score view state
#[derive(Default)]
pub struct ImpactScoreViewState {
    pub cache: Option<CommitImpactAnalysis>,
    pub nav: ListNavigation,
}

/// Change coupling view state
#[derive(Default)]
pub struct ChangeCouplingViewState {
    pub cache: Option<ChangeCouplingAnalysis>,
    pub nav: ListNavigation,
}

/// Quality score view state
#[derive(Default)]
pub struct QualityScoreViewState {
    pub cache: Option<CommitQualityAnalysis>,
    pub nav: ListNavigation,
}

/// Health dashboard view state
#[derive(Default)]
pub struct HealthViewState {
    pub cache: Option<ProjectHealth>,
}

/// Review queue view state
#[derive(Default)]
pub struct ReviewQueueViewState {
    pub cache: Option<ReviewQueue>,
    pub nav: ListNavigation,
}

/// PR create view state (wraps PrCreateState)
#[derive(Default)]
pub struct PrCreateViewState(pub PrCreateState);

/// Review pack view state (RiskSummary, ReviewPack, Verify)
#[derive(Default)]
pub struct ReviewPackViewState {
    pub cache: Option<ReviewPack>,
    pub verdict: Option<serde_json::Value>,
    pub nav: ListNavigation,
}

/// Next actions view state
#[derive(Default)]
pub struct NextActionsViewState {
    pub cache: Option<Vec<ActionRecommendation>>,
    pub nav: ListNavigation,
}

/// Handoff view state (Claude, Codex, Copilot)
#[derive(Default)]
pub struct HandoffViewState {
    pub cache: Option<HandoffContext>,
    pub nav: ListNavigation,
}

/// Commit detail panel state
#[derive(Default)]
pub struct CommitDetailState {
    pub scroll: usize,
    pub h_scroll: usize,
    pub selected_file: usize,
    pub expanded_files: HashSet<usize>,
}

/// File diff cache state
#[derive(Default)]
pub struct FileDiffState {
    pub cache: Option<FilePatch>,
    pub(crate) cache_path: Option<String>,
    pub scroll: usize,
}

/// Status message level
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StatusMessageLevel {
    Info,
    Success,
    Error,
}

/// Input mode
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum InputMode {
    #[default]
    Normal,
    Filter,
    BranchSelect,
    BranchCreate,
    StatusView,
    CommitInput,
    TopologyView,
    StatsView,
    HeatmapView,
    FileHistoryView,
    TimelineView,
    BlameView,
    OwnershipView,
    StashView,
    PatchView,
    PresetSave,
    BranchCompareView,
    RelatedFilesView,
    ImpactScoreView,
    ChangeCouplingView,
    QualityScoreView,
    QuickActionView,
    ReviewQueueView,
    PrCreate,
    ReviewPackView,
    NextActionsView,
    HandoffView,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuickAction {
    RiskSummary,
    ReviewPack,
    NextActions,
    Verify,
    HandoffClaude,
    HandoffCodex,
    HandoffCopilot,
    Timeline,
    Ownership,
    ImpactScore,
    ChangeCoupling,
    QualityScore,
    AuthorStats,
    Heatmap,
}

impl QuickAction {
    pub fn id(&self) -> &'static str {
        match self {
            Self::RiskSummary => "risk-summary",
            Self::ReviewPack => "review-pack",
            Self::NextActions => "next-actions",
            Self::Verify => "verify",
            Self::HandoffClaude => "handoff-claude",
            Self::HandoffCodex => "handoff-codex",
            Self::HandoffCopilot => "handoff-copilot",
            Self::Timeline => "timeline",
            Self::Ownership => "ownership",
            Self::ImpactScore => "impact-score",
            Self::ChangeCoupling => "change-coupling",
            Self::QualityScore => "quality-score",
            Self::AuthorStats => "author-stats",
            Self::Heatmap => "heatmap",
        }
    }

    pub fn title(&self, lang: Language) -> &'static str {
        match self {
            Self::RiskSummary => lang.quick_risk_summary(),
            Self::ReviewPack => lang.quick_review_pack(),
            Self::NextActions => lang.quick_next_actions(),
            Self::Verify => lang.quick_verify(),
            Self::HandoffClaude => lang.quick_handoff_claude(),
            Self::HandoffCodex => lang.quick_handoff_codex(),
            Self::HandoffCopilot => lang.quick_handoff_copilot(),
            Self::Timeline => lang.quick_timeline(),
            Self::Ownership => lang.quick_ownership(),
            Self::ImpactScore => lang.quick_impact_score(),
            Self::ChangeCoupling => lang.quick_change_coupling(),
            Self::QualityScore => lang.quick_quality_score(),
            Self::AuthorStats => lang.quick_author_stats(),
            Self::Heatmap => lang.quick_heatmap(),
        }
    }

    pub fn all() -> &'static [QuickAction] {
        &[
            Self::RiskSummary,
            Self::ReviewPack,
            Self::NextActions,
            Self::Verify,
            Self::HandoffClaude,
            Self::HandoffCodex,
            Self::HandoffCopilot,
            Self::Timeline,
            Self::Ownership,
            Self::ImpactScore,
            Self::ChangeCoupling,
            Self::QualityScore,
            Self::AuthorStats,
            Self::Heatmap,
        ]
    }
}

/// Dashboard-style sidebar panel
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SidebarPanel {
    #[default]
    Commits, // 2: Commit history (default)
    Status,   // 1: Repository info
    Branches, // 3: Branch list
    Files,    // 4: File change list
    Stash,    // 5: Stash list
}

impl SidebarPanel {
    /// Get panel from number key
    pub fn from_number(n: u8) -> Option<Self> {
        match n {
            1 => Some(Self::Status),
            2 => Some(Self::Commits),
            3 => Some(Self::Branches),
            4 => Some(Self::Files),
            5 => Some(Self::Stash),
            _ => None,
        }
    }

    /// Get panel number
    pub fn number(&self) -> u8 {
        match self {
            Self::Status => 1,
            Self::Commits => 2,
            Self::Branches => 3,
            Self::Files => 4,
            Self::Stash => 5,
        }
    }

    /// Get panel label
    pub fn label(&self, lang: Language) -> &'static str {
        match self {
            Self::Status => lang.status(),
            Self::Commits => lang.commits(),
            Self::Branches => lang.branches(),
            Self::Files => lang.files(),
            Self::Stash => lang.stash(),
        }
    }

    /// Go to next panel
    pub fn next(self) -> Self {
        match self {
            Self::Status => Self::Commits,
            Self::Commits => Self::Branches,
            Self::Branches => Self::Files,
            Self::Files => Self::Stash,
            Self::Stash => Self::Status,
        }
    }

    /// Go to previous panel
    pub fn prev(self) -> Self {
        match self {
            Self::Status => Self::Stash,
            Self::Commits => Self::Status,
            Self::Branches => Self::Commits,
            Self::Files => Self::Branches,
            Self::Stash => Self::Files,
        }
    }

    /// Return all panels in order
    pub fn all() -> &'static [SidebarPanel] {
        &[
            Self::Status,
            Self::Commits,
            Self::Branches,
            Self::Files,
            Self::Stash,
        ]
    }
}

/// Commit type (Conventional Commits)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CommitType {
    Feat,
    Fix,
    Docs,
    Style,
    Refactor,
    Test,
    Chore,
    Perf,
}

impl CommitType {
    /// Get commit message prefix
    pub fn prefix(&self) -> &'static str {
        match self {
            Self::Feat => "feat: ",
            Self::Fix => "fix: ",
            Self::Docs => "docs: ",
            Self::Style => "style: ",
            Self::Refactor => "refactor: ",
            Self::Test => "test: ",
            Self::Chore => "chore: ",
            Self::Perf => "perf: ",
        }
    }

    /// Get corresponding key
    pub fn key(&self) -> char {
        match self {
            Self::Feat => 'f',
            Self::Fix => 'x',
            Self::Docs => 'd',
            Self::Style => 's',
            Self::Refactor => 'r',
            Self::Test => 't',
            Self::Chore => 'c',
            Self::Perf => 'p',
        }
    }

    /// Get all commit types
    pub fn all() -> &'static [CommitType] {
        &[
            Self::Feat,
            Self::Fix,
            Self::Docs,
            Self::Style,
            Self::Refactor,
            Self::Test,
            Self::Chore,
            Self::Perf,
        ]
    }

    /// Get display name
    pub fn name(&self) -> &'static str {
        match self {
            Self::Feat => "feat",
            Self::Fix => "fix",
            Self::Docs => "docs",
            Self::Style => "style",
            Self::Refactor => "refactor",
            Self::Test => "test",
            Self::Chore => "chore",
            Self::Perf => "perf",
        }
    }
}

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

    // ===== View state defaults =====

    #[test]
    fn test_stats_view_state_default() {
        let s = StatsViewState::default();
        assert!(s.cache.is_none());
        assert_eq!(s.nav.selected_index, 0);
    }

    #[test]
    fn test_heatmap_view_state_default() {
        let s = HeatmapViewState::default();
        assert!(s.cache.is_none());
    }

    #[test]
    fn test_file_history_view_state_default() {
        let s = FileHistoryViewState::default();
        assert!(s.cache.is_none());
        assert!(s.path.is_none());
    }

    #[test]
    fn test_blame_view_state_default() {
        let s = BlameViewState::default();
        assert!(s.cache.is_none());
        assert!(s.path.is_none());
    }

    #[test]
    fn test_ownership_view_state_default() {
        let s = OwnershipViewState::default();
        assert!(s.cache.is_none());
    }

    #[test]
    fn test_stash_view_state_default() {
        let s = StashViewState::default();
        assert!(s.cache.is_none());
    }

    #[test]
    fn test_patch_view_state_default() {
        let s = PatchViewState::default();
        assert!(s.cache.is_none());
        assert_eq!(s.scroll_offset, 0);
    }

    #[test]
    fn test_branch_compare_view_state_default() {
        let s = BranchCompareViewState::default();
        assert!(s.cache.is_none());
        assert_eq!(s.tab, CompareTab::default());
    }

    #[test]
    fn test_related_files_view_state_default() {
        let s = RelatedFilesViewState::default();
        assert!(s.cache.is_none());
    }

    #[test]
    fn test_impact_score_view_state_default() {
        let s = ImpactScoreViewState::default();
        assert!(s.cache.is_none());
    }

    #[test]
    fn test_change_coupling_view_state_default() {
        let s = ChangeCouplingViewState::default();
        assert!(s.cache.is_none());
    }

    #[test]
    fn test_quality_score_view_state_default() {
        let s = QualityScoreViewState::default();
        assert!(s.cache.is_none());
    }

    #[test]
    fn test_health_view_state_default() {
        let s = HealthViewState::default();
        assert!(s.cache.is_none());
    }

    #[test]
    fn test_commit_detail_state_default() {
        let s = CommitDetailState::default();
        assert_eq!(s.scroll, 0);
        assert_eq!(s.h_scroll, 0);
        assert_eq!(s.selected_file, 0);
        assert!(s.expanded_files.is_empty());
    }

    #[test]
    fn test_file_diff_state_default() {
        let s = FileDiffState::default();
        assert!(s.cache.is_none());
        assert!(s.cache_path.is_none());
        assert_eq!(s.scroll, 0);
    }

    // ===== QuickAction =====

    #[test]
    fn test_quick_action_all_returns_14_items() {
        assert_eq!(QuickAction::all().len(), 14);
    }

    #[test]
    fn test_quick_action_id_mapping() {
        assert_eq!(QuickAction::RiskSummary.id(), "risk-summary");
        assert_eq!(QuickAction::ReviewPack.id(), "review-pack");
        assert_eq!(QuickAction::NextActions.id(), "next-actions");
        assert_eq!(QuickAction::Verify.id(), "verify");
        assert_eq!(QuickAction::HandoffClaude.id(), "handoff-claude");
        assert_eq!(QuickAction::HandoffCodex.id(), "handoff-codex");
        assert_eq!(QuickAction::HandoffCopilot.id(), "handoff-copilot");
        assert_eq!(QuickAction::Timeline.id(), "timeline");
        assert_eq!(QuickAction::Ownership.id(), "ownership");
        assert_eq!(QuickAction::ImpactScore.id(), "impact-score");
        assert_eq!(QuickAction::ChangeCoupling.id(), "change-coupling");
        assert_eq!(QuickAction::QualityScore.id(), "quality-score");
        assert_eq!(QuickAction::AuthorStats.id(), "author-stats");
        assert_eq!(QuickAction::Heatmap.id(), "heatmap");
    }

    #[test]
    fn test_quick_action_title_en() {
        let lang = Language::En;
        for action in QuickAction::all() {
            let title = action.title(lang);
            assert!(!title.is_empty());
        }
    }

    #[test]
    fn test_quick_action_title_ja() {
        let lang = Language::Ja;
        for action in QuickAction::all() {
            let title = action.title(lang);
            assert!(!title.is_empty());
        }
    }

    #[test]
    fn test_quick_action_all_ids_are_unique() {
        let ids: Vec<&str> = QuickAction::all().iter().map(|a| a.id()).collect();
        let mut deduped = ids.clone();
        deduped.sort();
        deduped.dedup();
        assert_eq!(ids.len(), deduped.len());
    }

    // ===== SidebarPanel =====

    #[test]
    fn test_sidebar_panel_default_is_commits() {
        assert_eq!(SidebarPanel::default(), SidebarPanel::Commits);
    }

    #[test]
    fn test_sidebar_panel_from_number() {
        assert_eq!(SidebarPanel::from_number(1), Some(SidebarPanel::Status));
        assert_eq!(SidebarPanel::from_number(2), Some(SidebarPanel::Commits));
        assert_eq!(SidebarPanel::from_number(3), Some(SidebarPanel::Branches));
        assert_eq!(SidebarPanel::from_number(4), Some(SidebarPanel::Files));
        assert_eq!(SidebarPanel::from_number(5), Some(SidebarPanel::Stash));
        assert_eq!(SidebarPanel::from_number(0), None);
        assert_eq!(SidebarPanel::from_number(6), None);
    }

    #[test]
    fn test_sidebar_panel_number_roundtrip() {
        for panel in SidebarPanel::all() {
            assert_eq!(SidebarPanel::from_number(panel.number()), Some(*panel));
        }
    }

    #[test]
    fn test_sidebar_panel_next_cycles() {
        let start = SidebarPanel::Status;
        let mut current = start;
        let mut visited = vec![];
        for _ in 0..5 {
            visited.push(current);
            current = current.next();
        }
        assert_eq!(visited.len(), 5);
        assert_eq!(current, start); // full cycle
    }

    #[test]
    fn test_sidebar_panel_prev_cycles() {
        let start = SidebarPanel::Status;
        let mut current = start;
        let mut visited = vec![];
        for _ in 0..5 {
            visited.push(current);
            current = current.prev();
        }
        assert_eq!(visited.len(), 5);
        assert_eq!(current, start); // full cycle
    }

    #[test]
    fn test_sidebar_panel_next_prev_inverse() {
        for panel in SidebarPanel::all() {
            assert_eq!(panel.next().prev(), *panel);
            assert_eq!(panel.prev().next(), *panel);
        }
    }

    #[test]
    fn test_sidebar_panel_label_not_empty() {
        for panel in SidebarPanel::all() {
            assert!(!panel.label(Language::En).is_empty());
            assert!(!panel.label(Language::Ja).is_empty());
        }
    }

    #[test]
    fn test_sidebar_panel_all_returns_5() {
        assert_eq!(SidebarPanel::all().len(), 5);
    }

    // ===== CommitType =====

    #[test]
    fn test_commit_type_all_returns_8() {
        assert_eq!(CommitType::all().len(), 8);
    }

    #[test]
    fn test_commit_type_prefix() {
        assert_eq!(CommitType::Feat.prefix(), "feat: ");
        assert_eq!(CommitType::Fix.prefix(), "fix: ");
        assert_eq!(CommitType::Docs.prefix(), "docs: ");
        assert_eq!(CommitType::Style.prefix(), "style: ");
        assert_eq!(CommitType::Refactor.prefix(), "refactor: ");
        assert_eq!(CommitType::Test.prefix(), "test: ");
        assert_eq!(CommitType::Chore.prefix(), "chore: ");
        assert_eq!(CommitType::Perf.prefix(), "perf: ");
    }

    #[test]
    fn test_commit_type_key() {
        assert_eq!(CommitType::Feat.key(), 'f');
        assert_eq!(CommitType::Fix.key(), 'x');
        assert_eq!(CommitType::Docs.key(), 'd');
        assert_eq!(CommitType::Style.key(), 's');
        assert_eq!(CommitType::Refactor.key(), 'r');
        assert_eq!(CommitType::Test.key(), 't');
        assert_eq!(CommitType::Chore.key(), 'c');
        assert_eq!(CommitType::Perf.key(), 'p');
    }

    #[test]
    fn test_commit_type_name() {
        for ct in CommitType::all() {
            let name = ct.name();
            assert!(!name.is_empty());
            // prefix should start with name
            assert!(ct.prefix().starts_with(name));
        }
    }

    #[test]
    fn test_commit_type_keys_are_unique() {
        let keys: Vec<char> = CommitType::all().iter().map(|c| c.key()).collect();
        let mut deduped = keys.clone();
        deduped.sort();
        deduped.dedup();
        assert_eq!(keys.len(), deduped.len());
    }

    // ===== QuickAction selection =====

    #[test]
    fn test_quick_action_selects_author_stats() {
        let actions = QuickAction::all();
        assert_eq!(actions[12], QuickAction::AuthorStats);
    }

    #[test]
    fn test_quick_action_selects_heatmap() {
        let actions = QuickAction::all();
        assert_eq!(actions[13], QuickAction::Heatmap);
    }

    #[test]
    fn test_quick_action_navigate_to_last_item() {
        use crate::app::App;
        let mut app = App::new();
        app.start_quick_action_view();
        for _ in 0..13 {
            app.quick_action_move_down();
        }
        assert_eq!(app.selected_quick_action(), Some(QuickAction::Heatmap));
    }

    // ===== InputMode =====

    #[test]
    fn test_input_mode_default_is_normal() {
        assert_eq!(InputMode::default(), InputMode::Normal);
    }
}