gitpane 0.13.0

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
use super::*;
use crate::git::status::StashEntry;
use std::path::Path;

#[test]
fn refresh_decision_runs_without_previous_refresh() {
    let now = Instant::now();
    assert_eq!(
        refresh_decision(None, now, Duration::from_millis(1000)),
        RefreshDecision::Now
    );
}

#[test]
fn refresh_decision_delays_inside_cooldown() {
    let last = Instant::now();
    let now = last + Duration::from_millis(250);
    assert_eq!(
        refresh_decision(Some(last), now, Duration::from_millis(1000)),
        RefreshDecision::Later(Duration::from_millis(750))
    );
}

#[test]
fn refresh_decision_runs_after_cooldown() {
    let last = Instant::now();
    let now = last + Duration::from_millis(1000);
    assert_eq!(
        refresh_decision(Some(last), now, Duration::from_millis(1000)),
        RefreshDecision::Now
    );
}

#[test]
fn graph_status_changed_ignores_file_only_changes() {
    let previous = test_status("main", Some("aaa"));
    let next = test_status("main", Some("aaa"));

    // Same HEAD and same rendered refs: a working-tree edit must not reload
    // the graph (the no-churn guarantee that keeps CPU down).
    assert!(!graph_status_changed(
        Some(&previous),
        &next,
        BranchFilter::All
    ));
}

#[test]
fn graph_status_changed_detects_head_changes() {
    let previous = test_status("main", Some("aaa"));
    let next = test_status("main", Some("bbb"));

    assert!(graph_status_changed(
        Some(&previous),
        &next,
        BranchFilter::All
    ));
}

#[test]
fn graph_status_changed_detects_stash_changes() {
    let previous = test_status("main", Some("aaa"));
    let mut next = test_status("main", Some("aaa"));
    next.stashes.push(StashEntry {
        index: 0,
        message: "WIP on main".to_string(),
        oid: "stash-oid".to_string(),
    });

    assert!(graph_status_changed(
        Some(&previous),
        &next,
        BranchFilter::All
    ));
}

#[test]
fn graph_status_changed_detects_commit_on_other_branch() {
    // The worktree case: the root's checked-out HEAD is unchanged, but a
    // commit landed on another local branch (its shared `refs/heads/*` tip
    // moved). The graph renders that branch, so it must reload.
    let previous = test_status("main", Some("aaa"));
    let mut next = test_status("main", Some("aaa"));
    next.refs.local = previous.refs.local ^ 0x9e37_79b9;

    assert!(graph_status_changed(
        Some(&previous),
        &next,
        BranchFilter::All
    ));
    // Local-only graphs render local branches too, so they also reload.
    assert!(graph_status_changed(
        Some(&previous),
        &next,
        BranchFilter::Local
    ));
}

#[test]
fn graph_status_changed_local_filter_ignores_remote_only_moves() {
    // A background fetch advances a remote-tracking ref. A local-only graph
    // does not draw it, so it must not reload; an all-branches graph does.
    let previous = test_status("main", Some("aaa"));
    let mut next = test_status("main", Some("aaa"));
    next.refs.remote = previous.refs.remote ^ 0x1234_5678;

    assert!(!graph_status_changed(
        Some(&previous),
        &next,
        BranchFilter::Local
    ));
    assert!(graph_status_changed(
        Some(&previous),
        &next,
        BranchFilter::All
    ));
}

fn test_status(branch: &str, head_oid: Option<&str>) -> RepoStatus {
    RepoStatus {
        branch: branch.to_string(),
        head_oid: head_oid.map(str::to_string),
        files: Vec::new(),
        ahead: 0,
        behind: 0,
        is_dirty: false,
        worktree_info: Vec::new(),
        has_submodules: false,
        submodules: Vec::new(),
        has_dirty_submodules: false,
        has_unpushed_submodules: false,
        fetch_failed: false,
        stashes: Vec::new(),
        refs: crate::git::status::RefsFingerprint::default(),
    }
}

/// Create a minimal repo (a `.git` directory with a `HEAD`) at `<root>/<rel>`
/// and return its path. Matches what `scanner::is_real_git_dir` accepts.
fn make_repo(root: &std::path::Path, rel: &str) -> std::path::PathBuf {
    let repo = root.join(rel);
    let dot_git = repo.join(".git");
    std::fs::create_dir_all(&dot_git).unwrap();
    std::fs::write(dot_git.join("HEAD"), "ref: refs/heads/main\n").unwrap();
    repo
}

fn displays(app: &App) -> Vec<String> {
    app.repo_list
        .repos
        .iter()
        .map(|r| r.display.clone())
        .collect()
}

/// The list must open in the order a later rescan reproduces. Discovery
/// orders by basename while rows are labelled and re-sorted by breadcrumb
/// path, so with the two out of step the list silently reshuffled the first
/// time anything triggered a rescan.
#[test]
fn app_opens_with_the_repo_list_already_sorted() {
    let tmp = tempfile::TempDir::new().unwrap();
    // Basename order is (bravo, zulu); breadcrumb order is the reverse.
    make_repo(tmp.path(), "alpha/zulu");
    make_repo(tmp.path(), "zeta/bravo");

    let config = Config {
        root_dirs: vec![tmp.path().to_path_buf()],
        scan_depth: 3,
        ..Config::default()
    };
    let mut app = App::new(config);

    // Breadcrumb labels carry the platform's native separator.
    let zulu = Path::new("alpha").join("zulu").display().to_string();
    let bravo = Path::new("zeta").join("bravo").display().to_string();
    let at_startup = displays(&app);
    assert_eq!(at_startup, [zulu, bravo]);

    app.sort_repos();
    assert_eq!(
        displays(&app),
        at_startup,
        "first rescan reshuffled the list"
    );
}

/// Sorting is flat: a pinned repo takes its alphabetical place like any other
/// row. Pinning controls persistence (the repo survives rescans), not
/// position — a pinned `zzz` grouped above `aaa` read as "sorting is broken
/// for manually added repos".
#[test]
fn sort_repos_intermixes_pinned_repos() {
    let tmp = tempfile::TempDir::new().unwrap();
    make_repo(tmp.path(), "aaa");
    let pinned = make_repo(tmp.path(), "zzz");

    let config = Config {
        root_dirs: vec![tmp.path().to_path_buf()],
        pinned_repos: vec![pinned],
        scan_depth: 2,
        ..Config::default()
    };
    let mut app = App::new(config);
    assert_eq!(
        displays(&app),
        ["aaa", "zzz"],
        "pin was grouped, not sorted"
    );

    app.sort_order = SortOrder::ReverseAlphabetical;
    app.sort_repos();
    assert_eq!(displays(&app), ["zzz", "aaa"]);
}

/// The sort key lowercases, so `Presentations` files between `aaa` and `zzz`
/// instead of ASCII-sorting all uppercase names to the top; Z-A is the exact
/// reverse.
#[test]
fn sort_repos_is_case_insensitive_in_both_directions() {
    let tmp = tempfile::TempDir::new().unwrap();
    make_repo(tmp.path(), "zzz");
    make_repo(tmp.path(), "Presentations");
    make_repo(tmp.path(), "aaa");

    let config = Config {
        root_dirs: vec![tmp.path().to_path_buf()],
        scan_depth: 2,
        ..Config::default()
    };
    let mut app = App::new(config);
    assert_eq!(displays(&app), ["aaa", "Presentations", "zzz"]);

    app.sort_order = SortOrder::ReverseAlphabetical;
    app.sort_repos();
    assert_eq!(displays(&app), ["zzz", "Presentations", "aaa"]);
}

/// `s` must round-trip through every mode and come back to where it started.
#[test]
fn sort_order_cycles_through_all_modes() {
    let mut order = SortOrder::Alphabetical;
    let mut labels = Vec::new();
    for _ in 0..3 {
        labels.push(order.label());
        order = order.next();
    }
    assert_eq!(labels, ["A-Z", "Z-A", "Dirty"]);
    assert_eq!(order, SortOrder::Alphabetical);
}

/// Regression: `s` dumped the selection onto the first row. A sort changes
/// where rows live, never which row the user is on.
#[test]
fn sort_keeps_the_selected_repo() {
    let tmp = tempfile::TempDir::new().unwrap();
    make_repo(tmp.path(), "aaa");
    make_repo(tmp.path(), "zzz");

    let config = Config {
        root_dirs: vec![tmp.path().to_path_buf()],
        scan_depth: 2,
        ..Config::default()
    };
    let mut app = App::new(config);
    app.repo_list.select_repo_row(1); // zzz

    app.sort_order = SortOrder::ReverseAlphabetical;
    app.sort_repos();

    let selected = app.repo_list.selected_repo().unwrap();
    assert_eq!(selected.display, "zzz");
    assert_eq!(
        app.repo_list.selected_index(),
        Some(0),
        "zzz leads the Z-A order"
    );
}

/// Drain every action queued so far. Events translate to actions
/// synchronously, so this is the full observable outcome of `handle_event`.
fn drain_actions(app: &mut App) -> Vec<Action> {
    let mut actions = Vec::new();
    while let Ok(a) = app.action_rx.try_recv() {
        actions.push(a);
    }
    actions
}

fn power_test_app() -> App {
    let tmp = tempfile::TempDir::new().unwrap();
    let config = Config {
        root_dirs: vec![tmp.path().to_path_buf()],
        scan_depth: 2,
        ..Config::default()
    };
    App::new(config)
}

#[test]
fn deep_sleep_drops_watcher_refreshes_and_wake_repaints() {
    let mut app = power_test_app();
    drain_actions(&mut app);

    app.handle_event(Event::Power(PowerState::DeepSleep))
        .unwrap();
    app.handle_event(Event::RepoChanged("/some/repo".into()))
        .unwrap();
    assert!(
        drain_actions(&mut app).is_empty(),
        "a hidden pane must not refresh on watcher events"
    );

    app.handle_event(Event::Power(PowerState::Awake)).unwrap();
    assert!(
        drain_actions(&mut app)
            .iter()
            .any(|a| matches!(a, Action::Render)),
        "waking must repaint the stale frame"
    );
}

#[test]
fn doze_still_refreshes_on_real_repo_changes() {
    let mut app = power_test_app();
    drain_actions(&mut app);

    app.handle_event(Event::Power(PowerState::Doze)).unwrap();
    app.handle_event(Event::RepoChanged("/some/repo".into()))
        .unwrap();
    assert!(
        drain_actions(&mut app)
            .iter()
            .any(|a| matches!(a, Action::RefreshRepo(_))),
        "a visible (dozing) pane must keep tracking real changes"
    );
}

#[test]
fn root_change_while_deep_asleep_is_replayed_once_on_wake() {
    let mut app = power_test_app();
    drain_actions(&mut app);

    app.handle_event(Event::Power(PowerState::DeepSleep))
        .unwrap();
    app.handle_event(Event::ReposRootChanged).unwrap();
    app.handle_event(Event::ReposRootChanged).unwrap();
    assert!(
        drain_actions(&mut app).is_empty(),
        "no discovery walk while nobody can see the pane"
    );

    app.handle_event(Event::Power(PowerState::Awake)).unwrap();
    let discoveries = drain_actions(&mut app)
        .iter()
        .filter(|a| matches!(a, Action::DiscoverNewRepos))
        .count();
    assert_eq!(discoveries, 1, "deferred root changes coalesce into one");
}

#[test]
fn doze_to_awake_does_not_replay_or_repaint() {
    let mut app = power_test_app();
    drain_actions(&mut app);

    // Doze never deferred anything, so resuming from it is a no-op.
    app.handle_event(Event::Power(PowerState::Doze)).unwrap();
    app.handle_event(Event::Power(PowerState::Awake)).unwrap();
    assert!(drain_actions(&mut app).is_empty());
}

#[tokio::test]
async fn quit_waits_for_mutating_git_ops_and_force_quits_on_second_request() {
    let mut app = power_test_app();
    assert!(!app.ready_to_exit());

    // Quit with nothing in flight exits immediately.
    app.should_quit = true;
    assert!(app.ready_to_exit());

    // An in-flight mutating op (live GitOpGuard) defers the exit...
    let (tx, _rx) = mpsc::unbounded_channel();
    let guard = GitOpGuard::new(RepoId(std::path::PathBuf::from("/tmp/repo")), tx);
    assert!(!app.ready_to_exit());

    // ...unless the user insists with a second quit request.
    app.force_quit = true;
    assert!(app.ready_to_exit());

    // Completion of the op re-enables the normal exit path.
    app.force_quit = false;
    guard.complete();
    assert!(app.ready_to_exit());
}

/// The live-worktree refresh must drop the worktree's cached graph even
/// when the reload is deferred (commit detail open): a cache hit there would
/// resurrect stale rows for up to a poll interval. Guards the
/// `invalidate_repo(&aw.path)` line in `refresh_active_worktree`.
#[test]
fn worktree_refresh_invalidates_the_cached_graph_for_the_worktree_path() {
    let tmp = tempfile::tempdir().unwrap();
    let config = Config {
        root_dirs: vec![tmp.path().to_path_buf()],
        ..Config::default()
    };
    let mut app = App::new(config);
    // `spawn_blocking` in the miss path needs a runtime context.
    let runtime = tokio::runtime::Runtime::new().unwrap();
    let _guard = runtime.enter();

    let wt = tmp.path().join("live-wt");
    app.active_worktree = Some(ActiveWorktree {
        path: wt.clone(),
        repo_id: RepoId(wt.clone()),
        display_name: "live-wt".to_string(),
    });

    // Seed the cache the way a completed build would.
    app.git_graph.load_repo(wt.clone(), "live-wt");
    app.git_graph.set_rows(vec![mock_graph_row()]);
    assert!(
        app.git_graph.has_cached_graph_for(&wt),
        "precondition: the worktree graph is cached",
    );

    app.refresh_active_worktree();

    assert!(
        !app.git_graph.has_cached_graph_for(&wt),
        "refresh_active_worktree must invalidate the worktree's cached graph",
    );
}

fn mock_graph_row() -> crate::git::graph::GraphRow {
    crate::git::graph::GraphRow {
        commit_col: 0,
        lanes: vec![crate::git::graph::LaneSegment::Commit],
        horizontal_spans: Vec::new(),
        oid: git2::Oid::from_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap(),
        short_id: "abc1234".to_string(),
        message: "m".to_string(),
        author: "a".to_string(),
        time: 0,
        labels: Vec::new(),
        is_merge: false,
        parent_oids: Vec::new(),
        diff_stat: None,
        collapsed: None,
    }
}