gitpane 0.8.1

Multi-repo Git workspace dashboard TUI
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
use color_eyre::Result;
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::{layout::Rect, widgets::ListState};
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::mpsc::UnboundedSender;

use crate::action::Action;
use crate::git::graph::{BranchSegment, GraphBuilder, GraphOptions, GraphRow};
use crate::theme::Theme;

mod component;
#[cfg(test)]
mod tests;

/// RAII guard that guarantees the graph's `load_in_flight` latch is released
/// even when the background build panics. The build runs in a fire-and-forget
/// `spawn_blocking` whose `JoinHandle` is never awaited, so a panic in
/// `GraphBuilder::build` would otherwise swallow both the success and error
/// paths and strand `load_in_flight = true` — freezing the graph for that repo
/// until the user switches away and back. On drop without `complete()`, it
/// emits `GraphLoadAborted` so the latch is cleared for its generation.
struct GraphLoadGuard {
    generation: u64,
    tx: UnboundedSender<Action>,
    completed: bool,
}

impl GraphLoadGuard {
    fn new(generation: u64, tx: UnboundedSender<Action>) -> Self {
        Self {
            generation,
            tx,
            completed: false,
        }
    }

    /// Mark the build as having reported a terminal result so `Drop` is a no-op.
    fn complete(mut self) {
        self.completed = true;
    }
}

impl Drop for GraphLoadGuard {
    fn drop(&mut self) {
        if !self.completed {
            let _ = self.tx.send(Action::GraphLoadAborted {
                generation: self.generation,
            });
        }
    }
}

struct CommitDetail {
    oid: String,
    message: String,
    files: Vec<(String, String)>,
    file_state: ListState,
    diff_content: Option<String>,
    diff_scroll: u16,
    msg_scroll: u16,
    /// Rendered rect for the commit message block (set during draw).
    msg_area: Rect,
    /// Rendered rect for the file list block (set during draw).
    file_list_area: Rect,
}

struct SearchState {
    visible: bool,
    input: String,
    matches: Vec<usize>,
    current_match: Option<usize>,
}

impl SearchState {
    fn new() -> Self {
        Self {
            visible: false,
            input: String::new(),
            matches: Vec::new(),
            current_match: None,
        }
    }

    fn clear(&mut self) {
        self.visible = false;
        self.input.clear();
        self.matches.clear();
        self.current_match = None;
    }
}

/// How many consecutive aborted (panicked) builds to auto-retry before giving
/// up and surfacing an error. Keeps a deterministically-failing build from
/// replaying indefinitely while still self-healing from a one-off panic.
const MAX_CONSECUTIVE_ABORTS: u8 = 2;

pub(crate) struct GitGraph {
    /// Display rows (may contain collapsed placeholders).
    rows: Vec<GraphRow>,
    /// Full rows from the graph builder (never filtered).
    all_rows: Vec<GraphRow>,
    /// Branches currently collapsed in the view.
    collapsed_branches: std::collections::HashSet<String>,
    /// DAG-computed branch segments (non-trunk groups of commits).
    segments: Vec<BranchSegment>,
    /// Maps all_rows index → segment index (None = main trunk).
    row_to_segment: Vec<Option<usize>>,
    state: ListState,
    repo_name: String,
    repo_path: Option<PathBuf>,
    loading: bool,
    error: Option<String>,
    pub focused: bool,
    action_tx: Option<UnboundedSender<Action>>,
    render_area: Rect,
    graph_list_area: Rect,
    files_area: Rect,
    diff_area: Rect,
    commit_detail: Option<CommitDetail>,
    pub(crate) graph_options: GraphOptions,
    search: SearchState,
    /// Horizontal scroll offset (characters) for the graph list
    h_scroll: usize,
    pub horizontal_layout: bool,
    /// Deferred reload: set when graph data arrives while detail is open.
    needs_reload: bool,
    /// True while a graph rebuild is running for the current repo.
    load_in_flight: bool,
    /// Consecutive aborted (panicked) builds since the last successful load.
    /// Bounds the auto-retry so a deterministically-panicking build can't
    /// replay forever under a watcher event storm.
    consecutive_aborts: u8,
    /// Monotonic counter to discard stale GraphLoaded/DiffStatsLoaded results.
    load_generation: u64,
    /// Monotonic counter to discard stale CommitFilesLoaded/CommitDiffLoaded results.
    detail_generation: u64,
    theme: Arc<Theme>,
}

impl GitGraph {
    pub fn new(theme: Arc<Theme>) -> Self {
        Self {
            rows: Vec::new(),
            all_rows: Vec::new(),
            collapsed_branches: std::collections::HashSet::new(),
            segments: Vec::new(),
            row_to_segment: Vec::new(),
            state: ListState::default(),
            repo_name: String::new(),
            repo_path: None,
            loading: false,
            error: None,
            focused: false,
            action_tx: None,
            render_area: Rect::default(),
            graph_list_area: Rect::default(),
            files_area: Rect::default(),
            diff_area: Rect::default(),
            commit_detail: None,
            graph_options: GraphOptions::default(),
            search: SearchState::new(),
            h_scroll: 0,
            horizontal_layout: false,
            needs_reload: false,
            load_in_flight: false,
            consecutive_aborts: 0,
            load_generation: 0,
            detail_generation: 0,
            theme,
        }
    }

    pub fn set_theme(&mut self, theme: Arc<Theme>) {
        self.theme = theme;
    }

    pub fn load_repo(&mut self, path: PathBuf, repo_name: &str) {
        let is_same_repo = self.repo_path.as_deref() == Some(path.as_path());

        if is_same_repo && self.load_in_flight {
            self.needs_reload = true;
            return;
        }

        self.repo_name = repo_name.to_string();
        self.repo_path = Some(path.clone());
        self.error = None;

        // Keep old rows visible during reload (prevents blinking).
        // Only clear on repo switch.
        if !is_same_repo {
            self.loading = true;
            self.rows.clear();
            self.all_rows.clear();
            self.state.select(None);
            self.commit_detail = None;
            self.needs_reload = false;
            self.consecutive_aborts = 0;
            self.search.clear();
            self.collapsed_branches.clear();
            self.segments.clear();
            self.row_to_segment.clear();
        }

        let Some(tx) = &self.action_tx else { return };
        let tx = tx.clone();
        let options = self.graph_options.clone();
        self.load_in_flight = true;
        self.load_generation += 1;
        let load_gen = self.load_generation;

        tokio::task::spawn_blocking(move || {
            // Releases `load_in_flight` via `GraphLoadAborted` if `build` panics
            // before either terminal action below is sent.
            let guard = GraphLoadGuard::new(load_gen, tx.clone());
            let builder = GraphBuilder::new();
            match builder.build(&path, &options) {
                Ok(rows) => {
                    let oids: Vec<git2::Oid> = rows.iter().map(|r| r.oid).collect();
                    let _ = tx.send(Action::GraphLoaded {
                        generation: load_gen,
                        rows,
                    });
                    // `GraphLoaded` is the terminal action for the latch, so
                    // mark complete before the optional (and separately
                    // fallible) stats pass — a panic in `batch_diff_stats` must
                    // not emit a false `GraphLoadAborted` for an already-loaded
                    // graph.
                    guard.complete();
                    // Compute stats after graph is sent — graph appears instantly
                    if options.show_stats
                        && let Ok(stats) = crate::git::commit_files::batch_diff_stats(&path, &oids)
                    {
                        let _ = tx.send(Action::DiffStatsLoaded {
                            generation: load_gen,
                            stats,
                        });
                    }
                }
                Err(e) => {
                    let _ = tx.send(Action::GraphError {
                        generation: load_gen,
                        message: format!("Failed to load graph: {}", e),
                    });
                    guard.complete();
                }
            }
        });
    }

    pub fn set_error(&mut self, msg: String) {
        self.error = Some(msg);
        self.loading = false;
        self.load_in_flight = false;
    }

    pub fn set_rows(&mut self, mut rows: Vec<GraphRow>) {
        // Preserve selection position on refresh if possible
        let prev_selected = self.state.selected();
        // Carry forward diff_stats from previous all_rows to avoid blink on refresh
        if !self.all_rows.is_empty() {
            let old_stats: std::collections::HashMap<git2::Oid, crate::git::graph::DiffStat> = self
                .all_rows
                .iter()
                .filter_map(|r| r.diff_stat.clone().map(|s| (r.oid, s)))
                .collect();
            for row in &mut rows {
                if row.diff_stat.is_none() {
                    row.diff_stat = old_stats.get(&row.oid).cloned();
                }
            }
        }
        self.all_rows = rows;
        self.loading = false;
        self.load_in_flight = false;
        self.consecutive_aborts = 0;
        self.recompute_segments();
        self.recompute_collapsed_rows();
        if !self.display_rows().is_empty() {
            let idx = prev_selected
                .map(|i| i.min(self.display_rows().len() - 1))
                .unwrap_or(0);
            self.state.select(Some(idx));
        }

        if std::mem::take(&mut self.needs_reload) {
            self.reload_graph();
        }
    }

    pub fn set_diff_stats(&mut self, stats: Vec<(git2::Oid, crate::git::graph::DiffStat)>) {
        let stat_map: std::collections::HashMap<_, _> = stats.into_iter().collect();
        for row in &mut self.all_rows {
            if let Some(stat) = stat_map.get(&row.oid) {
                row.diff_stat = Some(stat.clone());
            }
        }
        self.recompute_collapsed_rows();
    }

    pub fn set_commit_files(&mut self, oid: String, message: String, files: Vec<(String, String)>) {
        let mut file_state = ListState::default();
        if !files.is_empty() {
            file_state.select(Some(0));
        }
        self.commit_detail = Some(CommitDetail {
            oid,
            message,
            files,
            file_state,
            diff_content: None,
            diff_scroll: 0,
            msg_scroll: 0,
            msg_area: Rect::default(),
            file_list_area: Rect::default(),
        });
    }

    pub fn set_commit_diff(&mut self, content: String) {
        if let Some(ref mut detail) = self.commit_detail {
            detail.diff_content = Some(content);
            detail.diff_scroll = 0;
        }
    }

    pub fn has_detail(&self) -> bool {
        self.commit_detail.is_some()
    }

    pub fn set_needs_reload(&mut self) {
        self.needs_reload = true;
    }

    /// Release the in-flight latch when a background build aborts without
    /// reporting (it panicked). Keeps the currently-displayed rows so the view
    /// doesn't blink, and honors a pending `needs_reload` so a refresh that was
    /// coalesced during the dead build still runs — but only up to
    /// `MAX_CONSECUTIVE_ABORTS`, after which it surfaces an error and stops
    /// auto-retrying so a deterministically-panicking build can't replay forever.
    pub fn abort_load(&mut self) {
        // Ignore a stale abort: a build that already reported `GraphLoaded`
        // (latch cleared) may still drop its guard if a later stats pass
        // panics. Without this, the abort would consume a fresh `needs_reload`
        // and fire a spurious reload for an already-loaded graph.
        if !self.load_in_flight {
            return;
        }
        self.load_in_flight = false;
        self.loading = false;
        self.consecutive_aborts = self.consecutive_aborts.saturating_add(1);

        if self.consecutive_aborts > MAX_CONSECUTIVE_ABORTS {
            // Drop the coalesced reload; replaying it would just panic again.
            // A fresh repo (re)selection resets the counter and lets it retry.
            self.needs_reload = false;
            self.error = Some("Graph build failed repeatedly".to_string());
            return;
        }

        if std::mem::take(&mut self.needs_reload) {
            self.reload_graph();
        }
    }

    pub fn current_generation(&self) -> u64 {
        self.load_generation
    }

    pub fn current_detail_generation(&self) -> u64 {
        self.detail_generation
    }

    /// Toggle collapse on the selected row's branch (or expand a collapsed group).
    fn toggle_collapse_selected(&mut self) {
        let Some(idx) = self.state.selected() else {
            return;
        };
        let Some(row) = self.display_rows().get(idx) else {
            return;
        };

        // Extract data before dropping the borrow on self
        let collapsed_key = row.collapsed.as_ref().map(|(k, _)| k.clone());
        let row_oid = row.oid;

        // If this is a collapsed placeholder, expand it
        if let Some(key) = collapsed_key {
            self.collapsed_branches.remove(key.as_str());
            self.recompute_collapsed_rows();
            return;
        }

        // Find this row in all_rows and look up its segment
        let Some(all_idx) = self.all_rows.iter().position(|r| r.oid == row_oid) else {
            return;
        };
        let Some(Some(seg_idx)) = self.row_to_segment.get(all_idx) else {
            return; // Main trunk — not collapsible
        };
        let seg = &self.segments[*seg_idx];
        self.collapsed_branches.insert(seg.id.clone());
        self.recompute_collapsed_rows();
    }

    /// Expand all collapsed branches.
    fn expand_all_branches(&mut self) {
        if self.collapsed_branches.is_empty() {
            return;
        }
        self.collapsed_branches.clear();
        self.recompute_collapsed_rows();
    }

    fn reload_graph(&mut self) {
        if let Some(path) = self.repo_path.clone() {
            let name = self.repo_name.clone();
            self.load_repo(path, &name);
        }
    }

    /// Recompute segments and row_to_segment mapping from all_rows.
    fn recompute_segments(&mut self) {
        self.segments = crate::git::graph::compute_branch_segments(&self.all_rows);
        self.row_to_segment = vec![None; self.all_rows.len()];
        for (seg_idx, seg) in self.segments.iter().enumerate() {
            for &row_idx in &seg.row_indices {
                self.row_to_segment[row_idx] = Some(seg_idx);
            }
        }
    }

    /// Returns the appropriate row slice for read-only access.
    /// When no branches are collapsed, reads directly from `all_rows`
    /// to avoid an unnecessary clone.
    fn display_rows(&self) -> &[GraphRow] {
        if self.collapsed_branches.is_empty() {
            &self.all_rows
        } else {
            &self.rows
        }
    }

    /// Recompute `self.rows` from `self.all_rows`, collapsing groups.
    fn recompute_collapsed_rows(&mut self) {
        if self.collapsed_branches.is_empty() {
            self.rows.clear();
            return;
        }

        // Collect all hidden row indices and prepare placeholders
        let mut hidden: std::collections::HashSet<usize> = std::collections::HashSet::new();
        // (tip_row_idx, segment_id, display_name, count)
        let mut placeholders: Vec<(usize, String, String, usize)> = Vec::new();

        for seg in &self.segments {
            if !self.collapsed_branches.contains(&seg.id) {
                continue;
            }
            for &row_idx in &seg.row_indices {
                hidden.insert(row_idx);
            }
            let tip_idx = seg.row_indices[0];
            placeholders.push((
                tip_idx,
                seg.id.clone(),
                seg.display_name.clone(),
                seg.row_indices.len(),
            ));
        }

        let mut rows = Vec::new();
        for (i, row) in self.all_rows.iter().enumerate() {
            if hidden.contains(&i) {
                if let Some((_, seg_id, name, count)) =
                    placeholders.iter().find(|(tip, _, _, _)| *tip == i)
                {
                    let mut placeholder = row.clone();
                    placeholder.message = format!("\u{25b6} {name} ({count} commits)");
                    placeholder.short_id = String::new();
                    placeholder.author = String::new();
                    placeholder.labels = Vec::new();
                    placeholder.diff_stat = None;
                    placeholder.collapsed = Some((seg_id.clone(), *count));
                    rows.push(placeholder);
                }
                continue;
            }
            rows.push(row.clone());
        }

        self.rows = rows;
    }

    pub fn selected_text(&self) -> Option<String> {
        // If viewing commit files, copy the selected file path
        if let Some(ref detail) = self.commit_detail
            && let Some(idx) = detail.file_state.selected()
            && let Some((_, path)) = detail.files.get(idx)
        {
            return Some(path.clone());
        }
        // Otherwise copy the selected commit's short id + message
        let idx = self.state.selected()?;
        let row = self.display_rows().get(idx)?;
        Some(format!("{} {}", row.short_id, row.message))
    }

    pub fn search_visible(&self) -> bool {
        self.search.visible
    }

    pub fn handle_search_key(&mut self, key: KeyEvent) -> Result<Option<Action>> {
        match key.code {
            KeyCode::Esc => {
                self.search.visible = false;
            }
            KeyCode::Enter => {
                self.search.visible = false;
                // Jump to first match if any
                if let Some(&idx) = self.search.matches.first() {
                    self.search.current_match = Some(0);
                    self.state.select(Some(idx));
                }
            }
            KeyCode::Backspace => {
                self.search.input.pop();
                self.update_search_matches();
            }
            KeyCode::Char(c) => {
                self.search.input.push(c);
                self.update_search_matches();
            }
            _ => {}
        }
        Ok(None)
    }

    fn update_search_matches(&mut self) {
        self.search.current_match = None;
        if self.search.input.is_empty() {
            self.search.matches.clear();
            return;
        }
        let query = self.search.input.to_lowercase();
        let matches: Vec<usize> = self
            .display_rows()
            .iter()
            .enumerate()
            .filter(|(_, row)| {
                row.message.to_lowercase().contains(&query)
                    || row.author.to_lowercase().contains(&query)
                    || row.short_id.to_lowercase().contains(&query)
            })
            .map(|(i, _)| i)
            .collect();
        if !matches.is_empty() {
            self.search.current_match = Some(0);
        }
        self.search.matches = matches;
    }

    fn search_next(&mut self) {
        if self.search.matches.is_empty() {
            return;
        }
        let next = match self.search.current_match {
            Some(i) => (i + 1) % self.search.matches.len(),
            None => 0,
        };
        self.search.current_match = Some(next);
        self.state.select(Some(self.search.matches[next]));
    }

    fn search_prev(&mut self) {
        if self.search.matches.is_empty() {
            return;
        }
        let prev = match self.search.current_match {
            Some(0) | None => self.search.matches.len() - 1,
            Some(i) => i - 1,
        };
        self.search.current_match = Some(prev);
        self.state.select(Some(self.search.matches[prev]));
    }

    fn select_next(&mut self) {
        if self.display_rows().is_empty() {
            return;
        }
        let i = match self.state.selected() {
            Some(i) => (i + 1).min(self.display_rows().len() - 1),
            None => 0,
        };
        self.state.select(Some(i));
    }

    fn select_prev(&mut self) {
        if self.display_rows().is_empty() {
            return;
        }
        let i = match self.state.selected() {
            Some(i) => i.saturating_sub(1),
            None => 0,
        };
        self.state.select(Some(i));
    }

    fn try_show_commit_files(&mut self) -> Option<Action> {
        let idx = self.state.selected()?;
        let oid = self.display_rows().get(idx)?.oid.to_string();
        let repo_path = self.repo_path.clone()?;
        self.detail_generation += 1;
        Some(Action::ShowCommitFiles { repo_path, oid })
    }

    fn try_show_commit_diff(&mut self) -> Option<Action> {
        let detail = self.commit_detail.as_ref()?;
        let file_idx = detail.file_state.selected()?;
        let (_, file_path) = detail.files.get(file_idx)?;
        let repo_path = self.repo_path.clone()?;
        self.detail_generation += 1;
        Some(Action::ShowCommitDiff {
            repo_path,
            oid: detail.oid.clone(),
            file_path: file_path.clone(),
        })
    }
}