delta-gitu 0.1.14

An experimental fork of gitu (a git client inspired by Magit), published as a library for delta to embed
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
use super::*;
use crate::item_data::ItemData;
use std::time::{Duration, Instant};

fn setup(ctx: TestContext) -> TestContext {
    commit(&ctx.dir, "third commit", "");
    commit(&ctx.dir, "second commit", "");
    commit(&ctx.dir, "first commit", "");
    ctx
}

#[test]
fn limit_prompt() {
    snapshot!(setup(setup_clone!()), "l-n-n");
}

#[test]
fn limit_set_10() {
    snapshot!(setup(setup_clone!()), "l-n-n10<enter>");
}

#[test]
fn limit_invalid() {
    snapshot!(setup(setup_clone!()), "l-n-nfff<enter>");
}

#[test]
fn limit_2_commits() {
    snapshot!(setup(setup_clone!()), "l-n-n2<enter>l");
}

#[test]
fn limit_2_commits_other() {
    snapshot!(setup(setup_clone!()), "l-n-n2<enter>l");
}

#[test]
fn grep_prompt() {
    snapshot!(setup(setup_clone!()), "l-F");
}

#[test]
fn grep_set_example() {
    snapshot!(setup(setup_clone!()), "l-Fexample<enter>");
}

#[test]
fn grep_second() {
    snapshot!(setup(setup_clone!()), "l-Fsecond<enter>l");
}

#[test]
fn grep_no_match() {
    snapshot!(setup(setup_clone!()), "l-Fdoesntexist<enter>l");
}

#[test]
fn grep_second_other() {
    snapshot!(setup(setup_clone!()), "l-Fsecond<enter>omain<enter>");
}

#[test]
fn log_other_prompt() {
    snapshot!(setup(setup_clone!()), "lljlo");
}

#[test]
fn log_other() {
    snapshot!(setup(setup_clone!()), "lljlo<enter>");
}

#[test]
fn log_other_input() {
    snapshot!(setup(setup_clone!()), "lomain~1<enter>");
}

#[test]
fn log_other_invalid() {
    snapshot!(setup(setup_clone!()), "lo <enter>");
}

#[test]
fn log_empty_branch() {
    // Regression for #262: showing the log of a branch with no commits used to
    // panic ("index out of bounds") because the log screen had no items but the
    // cursor still indexed into it.
    let mut ctx = setup_clone!();
    run(&ctx.dir, &["rm", "-rf", ".git"]);
    run(&ctx.dir, &["rm", "initial-file"]);
    run(&ctx.dir, &["git", "init", "--initial-branch=main"]);

    let mut app = ctx.init_app();
    ctx.update(&mut app, keys("ll"));
    insta::assert_snapshot!(ctx.redact_buffer());
}

/// Give `git log` the user's arguments, as `general.log_args` does.
pub(super) fn with_log_args(mut ctx: TestContext, args: &[&str]) -> TestContext {
    ctx.config().general.log_args = args.iter().map(|arg| arg.to_string()).collect();
    ctx
}

/// A format printing each commit over several rows, as the renderer marks them.
/// Dates are absolute so the output doesn't drift with the wall clock.
pub(super) const MULTI_ROW_LOG_ARGS: &[&str] = &[
    "--date=short",
    "--format=%n\u{25b8} %h %an %ad%C(auto)%d%C(reset)%n    %s",
];

/// The command a view is the output of, as `:` offers it for editing.
fn git_command_of(app: &crate::app::App) -> Option<String> {
    app.screen()
        .git_command
        .as_ref()
        .map(|git| git.borrow().line(None))
}

/// The log opened from gitu is the view `git log` gives on the command line,
/// with the log menu's arguments.
#[test]
fn the_log_is_the_output_of_git_log() {
    let mut ctx = setup(setup_clone!());
    let mut app = ctx.init_app();

    ctx.update(&mut app, keys("l-Fsecond<enter>l"));

    assert_eq!(
        git_command_of(&app),
        Some("git log -n256 --extended-regexp --grep=second".into())
    );
}

#[test]
fn log_args_are_given_to_git_log() {
    let mut ctx = with_log_args(setup(setup_clone!()), &["--format=logged %s"]);
    let mut app = ctx.init_app();

    ctx.update(&mut app, keys("ll"));

    let buffer = ctx.redact_buffer();
    assert!(buffer.contains("logged add second commit"), "{buffer}");
}

/// The log is drawn once the renderer has drawn its first screen, as the
/// command-line `git log` is.
#[test]
fn the_log_is_drawn_before_the_renderer_finishes() {
    let mut ctx = setup_clone!();
    for i in 1..=40 {
        commit(&ctx.dir, &format!("file-{i:02}"), "");
    }
    ctx.config().general.diff_renderer.command = super::pager::pausing_renderer();

    let started = Instant::now();
    let mut app = ctx.init_app_streaming_with_args(Default::default());
    ctx.update(&mut app, keys("ll"));

    assert!(
        started.elapsed() < Duration::from_secs(2),
        "{:?}",
        started.elapsed()
    );
    assert!(ctx.redact_buffer().contains("add file-40"));
}

#[test]
fn log_move_down_steps_a_whole_commit() {
    snapshot!(
        with_log_args(setup(setup_clone!()), MULTI_ROW_LOG_ARGS),
        "lljj"
    );
}

#[test]
fn log_move_next_section_steps_a_whole_commit() {
    snapshot!(
        with_log_args(setup(setup_clone!()), MULTI_ROW_LOG_ARGS),
        "ll<alt+j><alt+j>"
    );
}

#[test]
fn log_move_prev_section_steps_a_whole_commit() {
    snapshot!(
        with_log_args(setup(setup_clone!()), MULTI_ROW_LOG_ARGS),
        "lljj<alt+k>"
    );
}

#[test]
fn log_fold_hides_the_commit_rows() {
    snapshot!(
        with_log_args(setup(setup_clone!()), MULTI_ROW_LOG_ARGS),
        "ll<tab>"
    );
}

/// The summary is a row of the commit under its first, so a search matching it
/// selects the commit, and enter shows that commit.
#[test]
fn log_search_selects_the_commit_whose_row_matches() {
    let mut ctx = with_log_args(setup(setup_clone!()), MULTI_ROW_LOG_ARGS);
    let mut app = ctx.init_app();
    ctx.update(&mut app, keys("ll/second<enter>"));
    assert_eq!(selected_summary(&app), "add second commit");
}

/// The selected commit's own match is behind the cursor, so `n` moves on.
#[test]
fn log_search_next_steps_a_whole_commit() {
    let mut ctx = with_log_args(setup(setup_clone!()), MULTI_ROW_LOG_ARGS);
    let mut app = ctx.init_app();
    ctx.update(&mut app, keys("ll"));
    let selected: Vec<String> = ["/commit<enter>", "n", "n"]
        .into_iter()
        .map(|input| {
            ctx.update(&mut app, keys(input));
            selected_summary(&app)
        })
        .collect();
    assert_eq!(
        selected,
        ["add second commit", "add third commit", "add first commit"]
    );
}

#[test]
fn log_show_acts_on_the_selected_commit() {
    snapshot!(setup(setup_clone!()), "llj<enter>");
}

#[test]
fn log_copy_hash_of_the_selected_commit() {
    snapshot!(setup(setup_clone!()), "lljy");
}

#[test]
fn log_with_stat() {
    // Extra rows a commit prints after its message stay part of that commit.
    snapshot!(
        with_log_args(
            setup(setup_clone!()),
            &["--stat", "--format=\u{25b8} %h %s"]
        ),
        "llj"
    );
}

/// A renderer that draws a rule above each commit, as delta does with
/// `commit-decoration-style = ol`.
#[test]
fn log_divider_is_not_the_cursor_line() {
    let mut ctx = with_log_args(setup(setup_clone!()), &["--format=\u{25b8} %h %s"]);
    ctx.config().general.diff_renderer.command = [
        "awk",
        // The string terminator's backslash is %c of 92: on Windows, awk reads
        // `\\` in its argument as a single backslash.
        r#"/^▸ /{printf "\033]1717;1;C;;;%s\033%c\n─────\n", $2, 92} {print}"#,
    ]
    .map(String::from)
    .to_vec();
    snapshot!(ctx, "llj");
}

#[test]
fn log_keeps_the_first_commit_when_the_renderer_greets_on_its_line() {
    // A renderer emits its OSC-1717 handshake as its first output, which shares
    // a line with the first commit's record (delta does this). The newest commit
    // must still be a commit: selectable, cursor on it.
    let mut ctx = with_log_args(setup(setup_clone!()), &["--format=\u{25b8} %h %s"]);
    ctx.config().general.diff_renderer.command = [
        "awk",
        r#"BEGIN{printf "\033]1717;1\033\\"} /^â–¸ /{printf "\033]1717;1;C;;;%s\033\\", $2} {print}"#,
    ]
    .map(String::from)
    .to_vec();

    let mut app = ctx.init_app();
    ctx.update(&mut app, keys("ll"));
    insta::assert_snapshot!(ctx.redact_buffer());
}

/// Turning the page takes the cursor with it: the commit selected afterwards is
/// one of those now on screen, so the next op acts on what is being looked at.
#[test]
fn paging_lands_on_a_commit_of_the_new_page() {
    let mut ctx = setup_clone!();
    for i in 1..=40 {
        commit(&ctx.dir, &format!("file-{i:02}"), "");
    }

    let mut app = ctx.init_app();
    ctx.update(&mut app, keys("ll"));
    let top = selected_summary(&app);

    ctx.update(&mut app, keys("<pagedown>"));
    let paged = selected_summary(&app);
    assert_ne!(top, paged, "the cursor stayed on the first commit");
    assert!(
        ctx.redact_buffer().contains(&paged),
        "selected {paged} is off screen:\n{}",
        ctx.redact_buffer()
    );

    ctx.update(&mut app, keys("<pageup>"));
    assert_eq!(top, selected_summary(&app), "paging back missed the commit");
}

/// Turning a renderer feature on and off in a commit opened from the log
/// rebuilds the log under it too; leaving the commit returns to the commit it
/// was opened from.
#[test]
fn leaving_a_commit_after_toggling_a_feature_returns_to_it_in_the_log() {
    let mut ctx = setup_clone!();
    for i in 1..=40 {
        commit(&ctx.dir, &format!("file-{i:02}"), "");
    }
    // The log is rebuilt a screenful at a time, and the commit opened is past
    // the first screenfuls.
    ctx.config().general.diff_renderer.command = super::pager::pausing_renderer();
    let mut app = ctx.init_app_streaming_with_args(Default::default());
    ctx.update(&mut app, keys("ll"));
    while app.screen().is_drawing() {
        app.screen_mut().finish().unwrap();
        std::thread::sleep(Duration::from_millis(10));
    }
    ctx.update(&mut app, keys("G<ctrl+k><ctrl+k>"));
    let opened = selected_summary(&app);

    ctx.update(&mut app, keys("<enter>SSSq"));
    assert_eq!(selected_summary(&app), opened, "{}", ctx.redact_buffer());
}

fn selected_summary(app: &crate::app::App) -> String {
    match &app.state.screens.last().unwrap().get_selected_item().data {
        ItemData::Commit { summary, .. } => summary.clone(),
        data => panic!("not a commit: {data:?}"),
    }
}

/// `file_header = "stat"` heads each file of `git log -p` with its stat row, as
/// it heads each file of `git diff`.
#[test]
fn a_log_of_patches_heads_each_file_with_its_stat_row() {
    let mut ctx = setup_clone!();
    ctx.config().general.file_header = crate::config::FileHeader::Stat;
    log_of_patches(&mut ctx);
    let buffer = ctx.redact_buffer();
    assert!(buffer.contains(" a.rs | 2 +-"), "{buffer}");
}

/// `git log --stat` has no patch, so no context to ask for: asking for some
/// leaves git's stat as it was, one row per file.
#[test]
fn a_log_stat_with_the_context_asked_for_has_one_stat_row_per_file() {
    let mut ctx = setup_clone!();
    ctx.config().general.file_header = crate::config::FileHeader::Stat;
    commit(&ctx.dir, "a.rs", "alpha\n");
    commit(&ctx.dir, "b.rs", "beta\n");
    commit(&ctx.dir, "a.rs", "alpha changed\n");
    let mut app = ctx.init_app_as_pager_of(&["git", "log", "--stat", "-3"]);

    ctx.update(&mut app, keys("U8<enter>"));

    let buffer = ctx.redact_buffer();
    let newest = buffer.split("Author:").nth(1).unwrap();
    assert_eq!(newest.matches(" a.rs | ").count(), 1, "{buffer}");
    assert!(!newest.contains("---"), "{buffer}");
}

/// A log of patches with their stats has one stat row per file, heading it.
#[test]
fn a_log_of_patches_with_their_stats_has_one_stat_row_per_file() {
    let mut ctx = setup_clone!();
    ctx.config().general.file_header = crate::config::FileHeader::Stat;
    commit(&ctx.dir, "a.rs", "alpha\n");
    commit(&ctx.dir, "b.rs", "beta\n");
    commit(&ctx.dir, "a.rs", "alpha changed\n");
    ctx.init_app_as_pager_of(&["git", "log", "-p", "--stat", "-3"]);

    let buffer = ctx.redact_buffer();
    let newest = buffer.split("Author:").nth(1).unwrap();
    assert_eq!(newest.matches(" a.rs | ").count(), 1, "{buffer}");
    assert!(!newest.contains("---"), "{buffer}");
    assert!(newest.contains("alpha changed"), "{buffer}");
}

/// Three commits, the newest changing a file the oldest added, paged through
/// a renderer that marks commits and annotates diff lines, as delta does.
fn log_of_patches(ctx: &mut TestContext) -> crate::app::App {
    commit(&ctx.dir, "a.rs", "alpha\n");
    commit(&ctx.dir, "b.rs", "beta\n");
    commit(&ctx.dir, "a.rs", "alpha changed\n");
    ctx.init_app_as_pager_of(&["git", "log", "-p", "-3"])
}

/// Each item of the view, as the summary of the commit it is under and,
/// within a diff, the file and line it is.
fn under_commits(app: &crate::app::App) -> Vec<(String, String)> {
    let mut summary = String::new();
    app.screen()
        .items()
        .iter()
        .filter_map(|item| match &item.data {
            ItemData::Commit { summary: of, .. } => {
                summary = of.clone();
                None
            }
            ItemData::HunkLine {
                diff,
                file_i,
                hunk_i,
                line_range,
                ..
            } => Some((
                summary.clone(),
                format!(
                    "{} {}",
                    diff.file_diffs[*file_i].header.new_file.fmt(&diff.text),
                    diff.hunk_content(*file_i, *hunk_i)[line_range.clone()].trim_end()
                ),
            )),
            _ => None,
        })
        .collect()
}

#[test]
fn a_log_of_patches_has_each_diff_under_its_commit() {
    let mut ctx = setup_clone!();
    let app = log_of_patches(&mut ctx);

    let lines = under_commits(&app);
    let lines: Vec<(&str, &str)> = lines
        .iter()
        .map(|(commit, line)| (commit.as_str(), line.as_str()))
        .collect();
    assert_eq!(
        lines,
        [
            ("modify a.rs", "a.rs -alpha"),
            ("modify a.rs", "a.rs +alpha changed"),
            ("add b.rs", "b.rs +beta"),
            ("add a.rs", "a.rs +alpha"),
        ]
    );
}

#[test]
fn a_log_of_patches_nests_each_diff_under_its_commit() {
    let mut ctx = setup_clone!();
    let app = log_of_patches(&mut ctx);

    let items = app.screen().items();
    let commit = items
        .iter()
        .position(
            |item| matches!(&item.data, ItemData::Commit { summary, .. } if summary == "add b.rs"),
        )
        .unwrap();
    let file = commit
        + items[commit..]
            .iter()
            .position(|item| matches!(&item.data, ItemData::Delta { .. }))
            .unwrap();
    assert!(items[file].depth > items[commit].depth);
}

#[test]
fn folding_a_commit_in_a_log_of_patches_hides_its_diff() {
    let mut ctx = setup_clone!();
    let mut app = log_of_patches(&mut ctx);

    ctx.update(&mut app, keys("gg<tab>"));

    let buffer = ctx.redact_buffer();
    assert!(!buffer.contains("alpha changed"), "{buffer}");
    assert!(buffer.contains("+beta"), "{buffer}");
}

/// A hunk of a commit in the log is the commit's: reversing it undoes that
/// commit's change in the working tree.
#[test]
fn a_hunk_in_a_log_of_patches_reverses_its_commits_change() {
    let mut ctx = setup_clone!();
    let mut app = log_of_patches(&mut ctx);

    ctx.update(&mut app, keys("/alpha changed<enter><ctrl+k>v"));

    assert_eq!(fs::read_to_string(ctx.dir.join("a.rs")).unwrap(), "alpha\n");
}

/// A renderer that marks commits but says nothing of the diff's lines gives
/// gitu no way to place them, so the log is its commits and their rows.
#[test]
fn a_log_of_patches_drawn_without_line_records_is_its_commits() {
    let mut ctx = setup_clone!();
    commit(&ctx.dir, "a.rs", "alpha\n");
    commit(&ctx.dir, "b.rs", "beta\n");
    let app = ctx.init_app_as_pager_of(&["git", "log", "-p", "-2"]);

    let commits = app
        .screen()
        .items()
        .iter()
        .filter(|item| matches!(item.data, ItemData::Commit { .. }))
        .count();
    assert_eq!(commits, 2);
    assert!(ctx.redact_buffer().contains("+beta"));
}

/// shift+tab folds a log of patches one level further out each time: its
/// files, then its commits, and then opens it all again.
#[test]
fn folding_everything_in_a_log_of_patches_cycles_through_its_levels() {
    let mut ctx = setup_clone!();
    let mut app = log_of_patches(&mut ctx);

    ctx.update(&mut app, keys("<backtab>"));
    let buffer = ctx.redact_buffer();
    assert!(buffer.contains("add b.rs"), "{buffer}");
    assert!(buffer.contains("b.rs"), "{buffer}");
    assert!(!buffer.contains("+beta"), "{buffer}");

    ctx.update(&mut app, keys("<backtab>"));
    let buffer = ctx.redact_buffer();
    assert!(buffer.contains("commit "), "{buffer}");
    assert!(!buffer.contains("b.rs"), "{buffer}");

    ctx.update(&mut app, keys("<backtab>"));
    let buffer = ctx.redact_buffer();
    assert!(buffer.contains("+alpha changed"), "{buffer}");
}