delta-gitu 0.1.18

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
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
//! The screen gitu shows as git's pager: a patch that arrived on stdin rather than
//! one gitu asked git for.
//!
//! Set as git's pager, gitu is handed the very bytes `git diff` / `git show`
//! produce, so the patch text is the source of truth exactly as it is
//! everywhere else — the difference is only where it came from. git colours
//! what it writes to a pager, so the text is stripped of ANSI before it is
//! parsed; the colours in the view are the renderer's, applied afterwards.

use super::{Items, Screen, streamed_items};
use crate::style::{Modifier, Style};
use crate::{
    Res,
    calling_process::{GitCommand, Stat},
    config::{Config, FileHeader},
    error::Error,
    git::diff::{Diff, DiffType},
    gitu_diff,
    item_data::ItemData,
    items::{self, Item, RenderParams},
    stream::{Input, Snapshot, Stream, Streams},
};
use git2::{Oid, Repository};
use regex::Regex;
use std::{cell::RefCell, rc::Rc, sync::Arc};

/// What git handed gitu as its pager: its output, and the argv of the command
/// that produced it where gitu could find it.
pub struct Paged {
    pub text: String,
    pub git_argv: Option<Vec<String>>,
    /// Where git ran the command, relative to the repository's root (see
    /// [`GitCommand::prefix`]).
    pub prefix: String,
    /// Whether reading the input stopped at the byte limit rather than at its
    /// end (see [`crate::capped_input::Capped`]).
    pub truncated: bool,
}

pub(crate) fn create(
    config: Arc<Config>,
    repo: Rc<Repository>,
    params: RenderParams,
    paged: Paged,
) -> Res<Screen> {
    let source = Source::of(paged, &config.general.hide);
    let git_command = source.git_command();

    let mut screen = Screen::streamed(
        Arc::clone(&config),
        params,
        Box::new(move |params: RenderParams| {
            if params.streaming {
                source.first_items(&config, &repo, &params)
            } else {
                source.items(&config, &repo, &params).map(Items::from)
            }
        }),
    )?;
    screen.git_command = git_command;
    // With its files folded there is no hunk to start on; the first file is
    // where the one wanted is chosen from.
    if !matches!(screen.get_selected_item().data, ItemData::Hunk { .. }) {
        screen.select_matching(|data| matches!(data, ItemData::Delta { .. }));
    }

    Ok(screen)
}

/// The view `git show <reference>` gives with gitu as git's pager.
pub(crate) fn show(
    config: Arc<Config>,
    repo: Rc<Repository>,
    params: RenderParams,
    reference: &str,
) -> Res<Screen> {
    let git_argv = ["git", "show"]
        .into_iter()
        .chain(config.general.show_args.iter().map(String::as_str))
        .chain([reference])
        .map(String::from)
        .collect();
    live(config, repo, params, git_argv)
}

/// The view `git log` gives with gitu as git's pager: of `rev`, or of `HEAD`,
/// at most `limit` commits, and only those whose message matches `msg_regex`.
pub(crate) fn log(
    config: Arc<Config>,
    repo: Rc<Repository>,
    params: RenderParams,
    limit: usize,
    rev: Option<Oid>,
    msg_regex: Option<&Regex>,
) -> Res<Screen> {
    let mut git_argv: Vec<String> = ["git", "log"]
        .into_iter()
        .map(String::from)
        .chain(config.general.log_args.iter().cloned())
        .collect();
    // "No limit" is `u32::MAX`, which git rejects as not an integer; leaving the
    // option off is what an unrepresentable limit means anyway.
    if let Ok(limit) = i32::try_from(limit) {
        git_argv.push(format!("-n{limit}"));
    }
    if let Some(regex) = msg_regex {
        git_argv.push("--extended-regexp".into());
        git_argv.push(format!("--grep={regex}"));
    }
    git_argv.extend(rev.map(|oid| oid.to_string()));
    live(config, repo, params, git_argv)
}

/// The view of a git command gitu runs itself, as though git had been run with
/// gitu as its pager.
pub(crate) fn live(
    config: Arc<Config>,
    repo: Rc<Repository>,
    params: RenderParams,
    git_argv: Vec<String>,
) -> Res<Screen> {
    live_in(config, repo, params, git_argv, String::new())
}

/// [`live`], for a command run in `prefix` (see [`GitCommand::prefix`]).
pub(crate) fn live_in(
    config: Arc<Config>,
    repo: Rc<Repository>,
    params: RenderParams,
    git_argv: Vec<String>,
    prefix: String,
) -> Res<Screen> {
    create(
        config,
        repo,
        params,
        Paged {
            text: String::new(),
            git_argv: Some(git_argv),
            prefix,
            truncated: false,
        },
    )
}

/// Where the rows come from each time the screen is rebuilt.
enum Source {
    /// A question git can be put again, gitu having found the command that
    /// produced the patch. Asking again is what makes it a live view — staging
    /// a hunk changes what it shows — and the question itself can be edited,
    /// so what comes back is classified afresh on every ask.
    Live { git: Rc<RefCell<GitCommand>> },
    /// A patch gitu found no command for: a saved patch file, another repo's,
    /// one piped in from a process already gone. It stays as it came, and gitu
    /// structures it.
    Patch { text: String, truncated: bool },
    /// Output with no diff in it: a log, a blame, a grep, a man page. gitu has
    /// no structure of its own to impose, so the renderer draws it and what it
    /// says about the rows it drew decides what they are.
    Unstructured { text: String, truncated: bool },
}

impl Source {
    /// The command gitu found is what the patch is: it says what the patch is a
    /// diff of, and so which ops it admits (see [`DiffType`]), and it can be
    /// put again. Without one, the patch stays as it came and gitu structures
    /// whatever of it is a diff.
    ///
    /// The files the user has said to `hide` are dropped from the question
    /// before it is first asked, so the command holds them like any other
    /// pathspec: `:` shows them, and `&` takes them back.
    fn of(paged: Paged, hide: &[String]) -> Self {
        if let Some(git) = paged.git_argv.and_then(GitCommand::of) {
            let git = git.run_in(paged.prefix);
            let mut patterns = git.file_patterns();
            patterns.extend(hide.iter().map(|glob| format!("!{glob}")));

            return Source::Live {
                git: Rc::new(RefCell::new(git.asking_for(&patterns))),
            };
        }

        let file_diffs = gitu_diff::Parser::new(&crate::diff_renderer::strip_ansi(&paged.text))
            .parse_diff()
            .unwrap_or_default();

        if file_diffs.is_empty() {
            return Source::Unstructured {
                text: paged.text,
                truncated: paged.truncated,
            };
        }

        Source::Patch {
            text: paged.text,
            truncated: paged.truncated,
        }
    }

    /// The command being asked, where there is one to edit.
    fn git_command(&self) -> Option<Rc<RefCell<GitCommand>>> {
        match self {
            Source::Live { git, .. } => Some(Rc::clone(git)),
            Source::Patch { .. } | Source::Unstructured { .. } => None,
        }
    }

    /// All of the rows, once the renderer has drawn all of them.
    fn items(&self, config: &Config, repo: &Repository, params: &RenderParams) -> Res<Vec<Item>> {
        let (stream, shape) = self.start(config, repo, params)?;
        stream.wait_for(usize::MAX);
        built(config, repo, params, &shape, stream.snapshot())
    }

    /// The rows as soon as the renderer has drawn two screenfuls of them, as a
    /// pager shows the first screen of a pipeline, and all of them once it has
    /// drawn the rest.
    fn first_items(
        &self,
        config: &Arc<Config>,
        repo: &Rc<Repository>,
        params: &RenderParams,
    ) -> Res<Items> {
        let (stream, shape) = self.start(config, repo, params)?;
        let (config, repo, built_params) = (Arc::clone(config), Rc::clone(repo), params.clone());
        streamed_items(Streams(vec![stream]), params, move |mut snapshots| {
            built(&config, &repo, &built_params, &shape, snapshots.remove(0))
        })
    }

    /// Start the output through the renderer: the command put again, or the
    /// text that arrived.
    fn start(
        &self,
        config: &Config,
        repo: &Repository,
        params: &RenderParams,
    ) -> Res<(Stream, Shape)> {
        let (input, shape) = match self {
            Source::Live { git } => {
                let git = git.borrow();
                let stat = git.stat();
                // A diffstat gitu counts is shown as the files of the patch
                // headed by their stat rows, so the patch alone is what is
                // asked for.
                let mut command = match stat {
                    Stat::No => git.command(params.context.as_deref()),
                    Stat::Git => git.command(None),
                    Stat::WithPatch | Stat::Folded => {
                        git.as_patch().command(params.context.as_deref())
                    }
                };
                let workdir = repo.workdir().ok_or(Error::NoRepoWorkdir)?;
                command.current_dir(workdir.join(git.prefix()));
                let shape = Shape::Diff {
                    diff_type: git.diff_type(),
                    named_commit: true,
                    stat,
                    truncated: false,
                };
                (Input::Command(command), shape)
            }
            Source::Patch { text, truncated } => {
                let shape = Shape::Diff {
                    diff_type: DiffType::TreeToTree,
                    named_commit: false,
                    stat: Stat::No,
                    truncated: *truncated,
                };
                (Input::Text(text.clone()), shape)
            }
            Source::Unstructured { text, truncated } => (
                Input::Text(text.clone()),
                Shape::Rows {
                    truncated: *truncated,
                },
            ),
        };

        let renderer = (!config.general.diff_renderer.command.is_empty())
            .then(|| {
                crate::diff_renderer::renderer_command(
                    &config.general.diff_renderer.command,
                    params,
                    None,
                )
            })
            .flatten();
        let stream = Stream::start(input, renderer, config.general.max_input_bytes)
            .map_err(Error::GitDiff)?;
        Ok((stream, shape))
    }
}

/// What the output is taken to be, and what is said about it besides.
enum Shape {
    /// A patch, or what may be one: what the command put again says is
    /// classified on every ask.
    Diff {
        diff_type: DiffType,
        /// Whether its first line names the commit it is of, as `git show`'s
        /// does.
        named_commit: bool,
        /// The diffstat the command asks for.
        stat: Stat,
        /// Whether the input it came from was already cut short.
        truncated: bool,
    },
    /// Output with no diff in it, which the renderer draws.
    Rows { truncated: bool },
}

/// The rows for what the output and the renderer have produced so far.
fn built(
    config: &Config,
    repo: &Repository,
    params: &RenderParams,
    shape: &Shape,
    snapshot: Snapshot,
) -> Res<Vec<Item>> {
    if let Some(refused) = snapshot.refused {
        return Err(Error::GitRefusedTheCommand(refused));
    }
    let limit = config.general.max_input_bytes;
    // Without a renderer, or with one that failed, the text is drawn as it
    // arrived.
    let drawn = snapshot.rendered.as_deref().unwrap_or(&snapshot.text);

    // A renderer that failed has drawn nothing; what it failed to draw is
    // shown as it arrived, under what went wrong.
    let failed = snapshot.renderer_failed.clone();
    let (mut items, truncated) = match shape {
        Shape::Rows { truncated } => (rows(repo, drawn), *truncated),
        Shape::Diff {
            diff_type,
            named_commit,
            stat,
            truncated,
        } => {
            let mut items = Vec::new();
            let headed_by_stat =
                stat.is_counted() || config.general.file_header == FileHeader::Stat;
            // Files folded to stat rows are shown before the renderer reaches
            // them; other files are not, and only what the renderer has taken
            // need be parsed.
            let stat_folded =
                *stat == Stat::Folded || (config.general.start_folded && headed_by_stat);
            let input = if snapshot.done || stat_folded {
                &snapshot.text
            } else {
                snapshot.taken()
            };
            // The output arrives coloured; the parser reads none of it through
            // the escapes.
            let text = crate::diff_renderer::strip_ansi(input);
            let file_diffs = gitu_diff::Parser::new(&text)
                .parse_patches()
                .unwrap_or_default();
            // What came back has no diff in it: a log, a blame, a grep, a man
            // page. gitu has no structure of its own to impose, so the renderer
            // draws it.
            // `git diff --stat` is the patch with its files folded, as every
            // patch is with `start_folded`.
            let fold = *stat == Stat::Folded || config.general.start_folded;
            let patches = items::patches_of(&text, &file_diffs);
            if file_diffs.is_empty() {
                items.extend(rows(repo, drawn));
            } else if patches.len() > 1 {
                // A log of patches. A renderer that does not say which of its
                // rows are which leaves gitu nothing to place them by.
                items.extend(
                    snapshot
                        .rendered
                        .as_deref()
                        .and_then(|rendered| {
                            items::log_of_patches_items(
                                config,
                                params,
                                repo,
                                patches,
                                rendered,
                                snapshot.done,
                                diff_type,
                                stat.is_counted(),
                                fold,
                            )
                        })
                        .unwrap_or_else(|| rows(repo, drawn)),
                );
            } else {
                let diff = Rc::new(Diff {
                    file_diffs,
                    diff_type: diff_type.clone(),
                    commit: named_commit.then(|| commit_named_by(&text)).flatten(),
                    text,
                });
                let folded_to_stat_rows = fold && headed_by_stat;
                let drawn = items::drawn_diff_items(
                    config,
                    &diff,
                    snapshot.rendered.as_deref(),
                    !snapshot.done && !folded_to_stat_rows,
                    0,
                    fold,
                    None,
                );
                if let Some(preamble) = preamble(&snapshot.text, &diff.text) {
                    items.extend(items::plain_rows(&render(config, params, &preamble)));
                }
                items.extend(if headed_by_stat {
                    items::headed_by_counted_stat(drawn, params.width(), true)
                } else {
                    drawn
                });
            }
            (items, *truncated)
        }
    };

    items.extend(truncated_rows(truncated || snapshot.truncated, limit));
    if let Some(failed) = failed {
        items.insert(0, items::error_row(config, failed));
    }
    Ok(items)
}

/// One line saying that what is shown is not all of the output, for output that
/// stopped at the byte limit rather than at its end. It goes last, where the
/// reading stops: nobody who does not get that far needs to be told. What is
/// past the limit was not read, so there is nothing to say about how much of it
/// there was.
fn truncated_rows(truncated: bool, limit: u64) -> Vec<Item> {
    if !truncated {
        return Vec::new();
    }

    vec![dim_row(format!(
        "input truncated at {limit} bytes (general.max_input_bytes)"
    ))]
}

/// A row of gitu's own, saying something about the view rather than being part
/// of it.
fn dim_row(text: String) -> Item {
    Item {
        unselectable: true,
        rendered: Some(Rc::new(vec![(
            text,
            Style {
                add_modifier: Modifier::DIM,
                ..Style::new()
            },
        )])),
        ..Default::default()
    }
}

/// Text the renderer drew, which gitu does not structure. A renderer that says
/// which commit a row belongs to has turned it into a log; one that says
/// nothing has just drawn it.
fn rows(repo: &Repository, drawn: &str) -> Vec<Item> {
    items::rendered_log_items(repo, drawn).unwrap_or_else(|| items::plain_rows(drawn))
}

/// The commit a patch from `git show` is of, named in its first line.
fn commit_named_by(text: &str) -> Option<String> {
    let id = text
        .lines()
        .next()?
        .strip_prefix("commit ")?
        .split_whitespace()
        .next()?;

    id.chars()
        .all(|c| c.is_ascii_hexdigit())
        .then(|| id.to_owned())
}

/// What a patch opens with before its first file's diff, in the colours it
/// arrived with: the commit it is of, or a diffstat. It belongs to no file, so
/// gitu has no structure to give it. A patch whose first line already begins a
/// file's diff opens with none. `plain` is `coloured` without its colours,
/// line for line.
fn preamble(coloured: &str, plain: &str) -> Option<String> {
    let end = match plain.find("\ndiff --git ") {
        Some(i) if !plain.starts_with("diff --git ") => i + 1,
        _ => return None,
    };
    let lines = plain[..end].lines().count();
    Some(coloured.split_inclusive('\n').take(lines).collect())
}

/// `text` as the renderer draws it, or as it arrived when there is no renderer
/// to draw it — which is also how output that already came through one keeps
/// the records it came with.
fn render(config: &Config, params: &RenderParams, text: &str) -> String {
    if config.general.diff_renderer.command.is_empty() {
        return text.to_owned();
    }

    // A renderer that fails here fails on the rest of the view too, which says
    // so.
    crate::diff_renderer::run(
        &config.general.diff_renderer.command,
        Some(text),
        params,
        None,
    )
    .ok()
    .flatten()
    .unwrap_or_else(|| text.to_owned())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config;
    use crate::item_data::ItemData;
    use crate::repo_setup_clone;
    use crate::tests::helpers::{RepoTestContext, run};
    use stdext::function_name;

    const PATCH: &str = "diff --git a/f.rs b/f.rs\n\
                         index 1..2 100644\n\
                         --- a/f.rs\n\
                         +++ b/f.rs\n\
                         @@ -1,3 +1,3 @@\n\
                         \x20keep\n\
                         -gone\n\
                         +added\n";

    /// Input as it arrives when the limit did not stop it.
    fn paged(text: &str, git_argv: Option<Vec<String>>) -> Paged {
        Paged {
            text: text.to_owned(),
            git_argv,
            prefix: String::new(),
            truncated: false,
        }
    }

    fn items_of(repo: &Repository, patch: &str) -> Vec<ItemData> {
        let config = config::init_test_config().unwrap();
        Source::of(paged(patch, None), &[])
            .items(&config, repo, &Default::default())
            .unwrap()
            .into_iter()
            .map(|item| item.data)
            .collect()
    }

    fn screen_of(ctx: &RepoTestContext, patch: &str) -> Screen {
        create(
            Arc::new(config::init_test_config().unwrap()),
            Rc::new(Repository::open(&ctx.dir).unwrap()),
            RenderParams {
                size: (80, 20),
                features: Rc::from([]),
                context: None,
                streaming: false,
            },
            paged(patch, None),
        )
        .unwrap()
    }

    #[test]
    fn a_piped_patch_becomes_a_file_a_hunk_and_its_lines() {
        let ctx = repo_setup_clone!();
        let items = items_of(&ctx.local_repo, PATCH);

        assert!(
            matches!(items.first(), Some(ItemData::Delta { .. })),
            "got {items:?}"
        );
        assert!(items.iter().any(|d| matches!(d, ItemData::Hunk { .. })));
        assert_eq!(
            items
                .iter()
                .filter(|d| matches!(d, ItemData::HunkLine { .. }))
                .count(),
            3
        );
    }

    /// git colours what it writes to its pager, and the parser reads none of it
    /// through the escapes, so they have to come off first.
    #[test]
    fn a_coloured_patch_parses_the_same_as_a_plain_one() {
        let ctx = repo_setup_clone!();
        let coloured = "\x1b[1mdiff --git a/f.rs b/f.rs\x1b[m\n\
             \x1b[1mindex 1..2 100644\x1b[m\n\
             \x1b[1m--- a/f.rs\x1b[m\n\
             \x1b[1m+++ b/f.rs\x1b[m\n\
             \x1b[36m@@ -1,3 +1,3 @@\x1b[m\n\
             \x20keep\n\
             \x1b[31m-gone\x1b[m\n\
             \x1b[32m+added\x1b[m\n";

        assert_eq!(
            items_of(&ctx.local_repo, coloured).len(),
            items_of(&ctx.local_repo, PATCH).len()
        );
    }

    /// A patch from `git show` opens with the commit it is the diff of. That is
    /// not part of any file's diff, and dropping it loses who wrote the change
    /// and why.
    #[test]
    fn a_commit_patch_keeps_its_message_above_the_diff() {
        let ctx = repo_setup_clone!();
        let patch = format!(
            "commit 0123456789abcdef\n\
                             Author: Author Name <author@email.com>\n\
                             \n\
                             \x20   add initial-file\n\
                             \n\
                             {PATCH}"
        );

        let screen = screen_of(&ctx, &patch);

        let rows = screen.row_texts();
        assert!(
            rows.starts_with(&[
                "commit 0123456789abcdef".to_string(),
                "Author: Author Name <author@email.com>".to_string(),
                String::new(),
                "    add initial-file".to_string(),
                String::new(),
            ]),
            "got {rows:?}"
        );
        assert!(
            items_of(&ctx.local_repo, &patch)
                .iter()
                .any(|data| matches!(data, ItemData::HunkLine { .. })),
            "the diff below it is still a diff"
        );
    }

    /// A patch that opens with a file's diff has nothing above it, and every
    /// file it touches is shown once.
    #[test]
    fn a_patch_of_several_files_shows_each_of_them_once() {
        let ctx = repo_setup_clone!();
        let patch = format!("{PATCH}{}", PATCH.replace("f.rs", "g.rs"));

        let items = items_of(&ctx.local_repo, &patch);

        assert!(
            matches!(items.first(), Some(ItemData::Delta { .. })),
            "got {items:?}"
        );
        assert_eq!(
            items
                .iter()
                .filter(|d| matches!(d, ItemData::Delta { .. }))
                .count(),
            2
        );
        assert_eq!(
            items
                .iter()
                .filter(|d| matches!(d, ItemData::HunkLine { .. }))
                .count(),
            6
        );
    }

    /// Unstructured rows are rows like any other: each one takes the cursor.
    #[test]
    fn the_cursor_moves_between_unstructured_rows() {
        let ctx = repo_setup_clone!();
        let mut screen = screen_of(&ctx, "one\ntwo\nthree\n");
        assert_eq!(screen.row_texts().len(), 3, "{:?}", screen.row_texts());

        screen.select_next(crate::screen::NavMode::IncludeSubLines);

        assert_eq!(selected_row(&screen), "two");
    }

    fn selected_row(screen: &Screen) -> String {
        screen
            .get_selected_item()
            .rendered
            .as_ref()
            .map(|row| row.iter().map(|(text, _)| text.as_str()).collect())
            .unwrap_or_default()
    }

    /// Input that is not a git patch offers no structure rather than failing.
    #[test]
    fn output_that_is_not_a_patch_yields_no_files() {
        let ctx = repo_setup_clone!();
        assert!(
            !items_of(&ctx.local_repo, "this is not a diff\nnor is this\n")
                .iter()
                .any(|data| matches!(
                    data,
                    ItemData::Delta { .. } | ItemData::Hunk { .. } | ItemData::HunkLine { .. }
                ))
        );
        assert!(items_of(&ctx.local_repo, "").is_empty());
    }

    /// ... and is shown as it arrived, rather than as an empty screen.
    #[test]
    fn output_that_is_not_a_patch_is_still_readable() {
        let ctx = repo_setup_clone!();
        let grep = "m.rs:2:    let alpha = 1;\nm.rs:3:    let gamma = 42;\n";
        let screen = screen_of(&ctx, grep);

        assert_eq!(
            screen.row_texts(),
            ["m.rs:2:    let alpha = 1;", "m.rs:3:    let gamma = 42;"]
        );
    }

    /// A diff against another rev is as live as any other: git names the
    /// command it ran, so gitu can run it again and ask for more of the file
    /// around each change.
    #[test]
    fn the_context_of_a_diff_against_a_rev_can_be_widened() {
        let ctx = repo_setup_clone!();
        let lines = (1..=21).map(|n| format!("line {n}\n")).collect::<String>();
        write(&ctx, &lines);
        run(&ctx.dir, &["git", "add", "f.txt"]);
        run(&ctx.dir, &["git", "commit", "-m", "add f.txt"]);
        run(&ctx.dir, &["git", "checkout", "-b", "topic"]);
        write(&ctx, &lines.replace("line 11\n", "LINE 11\n"));
        run(&ctx.dir, &["git", "commit", "-am", "change f.txt"]);

        let patch = run(&ctx.dir, &["git", "diff", "main"]);
        let argv = ["git", "diff", "main"].map(String::from).to_vec();

        assert!(
            hunk_lines(&ctx, &patch, &argv, Some("-U5")) > hunk_lines(&ctx, &patch, &argv, None)
        );
    }

    /// Output with no diff in it is still a question gitu holds, so editing it
    /// into one that has a diff turns rows into files and hunks.
    #[test]
    fn an_edit_that_asks_for_a_diff_gets_one() {
        let ctx = repo_setup_clone!();
        let config = config::init_test_config().unwrap();
        let repo = Repository::open(&ctx.dir).unwrap();
        let source = Source::of(
            paged(
                &run(&ctx.dir, &["git", "log"]),
                Some(["git", "log"].map(String::from).to_vec()),
            ),
            &[],
        );
        let items = |source: &Source| {
            source
                .items(&config, &repo, &Default::default())
                .unwrap()
                .into_iter()
                .map(|item| item.data)
                .collect::<Vec<_>>()
        };

        assert!(
            !items(&source)
                .iter()
                .any(|data| matches!(data, ItemData::HunkLine { .. })),
            "a log has no diff in it"
        );

        let git = source.git_command().expect("the command was found");
        let edited = git.borrow().edited("git log -p").unwrap();
        *git.borrow_mut() = edited;

        assert!(
            items(&source)
                .iter()
                .any(|data| matches!(data, ItemData::HunkLine { .. })),
            "a log with patches does"
        );
    }

    /// Generated files and fixtures are noise in every patch, so saying so once
    /// in config drops them before the question is first asked.
    #[test]
    fn the_files_config_hides_are_gone_from_the_first_ask() {
        let ctx = repo_setup_clone!();
        let config = config::init_test_config().unwrap();
        let repo = Repository::open(&ctx.dir).unwrap();
        for file in ["a.rs", "a.pb.rs"] {
            std::fs::write(ctx.dir.join(file), "generated\n").unwrap();
        }
        run(&ctx.dir, &["git", "add", "."]);

        let source = Source::of(
            paged(
                &run(&ctx.dir, &["git", "diff", "--cached"]),
                Some(["git", "diff", "--cached"].map(String::from).to_vec()),
            ),
            &["*.pb.rs".to_owned()],
        );
        let files = source
            .items(&config, &repo, &Default::default())
            .unwrap()
            .iter()
            .filter_map(|item| match &item.data {
                ItemData::Delta { diff, file_i, .. } => Some(
                    diff.file_diffs[*file_i]
                        .header
                        .new_file
                        .fmt(&diff.text)
                        .into_owned(),
                ),
                _ => None,
            })
            .collect::<Vec<_>>();

        assert_eq!(files, ["a.rs"]);
    }

    /// `git diff --stat` paged by gitu reads as git prints it, a row per file
    /// and the summary, and each file's row is that file.
    #[test]
    fn a_stat_shows_a_row_per_file_as_git_prints_it() {
        let ctx = repo_setup_clone!();
        let screen = stat_screen(&ctx);

        assert_eq!(
            screen.shown_row_texts(),
            run(&ctx.dir, &["git", "diff", "--stat"])
                .lines()
                .collect::<Vec<_>>()
        );
        assert_eq!(
            screen
                .visible_items()
                .filter(|&i| matches!(screen.items()[i].data, ItemData::Delta { .. }))
                .count(),
            2
        );
    }

    /// ... and each of those rows is a file, which unfolds to its diff.
    #[test]
    fn a_file_in_a_stat_unfolds_to_its_diff() {
        let ctx = repo_setup_clone!();
        let mut screen = stat_screen(&ctx);
        let shows_a_line = |screen: &Screen| {
            screen
                .visible_items()
                .any(|i| matches!(screen.items()[i].data, ItemData::HunkLine { .. }))
        };
        assert!(!shows_a_line(&screen));

        screen.toggle_section().unwrap();

        assert!(shows_a_line(&screen));
    }

    /// ... and an unfolded file is headed by its stat row, not the renderer's
    /// file header.
    #[test]
    fn an_unfolded_stat_file_keeps_its_stat_row() {
        let ctx = repo_setup_clone!();
        let mut screen = stat_screen(&ctx);
        let stat_row = screen.shown_row_texts()[0].clone();

        screen.toggle_section().unwrap();

        assert_eq!(screen.shown_row_texts()[0], stat_row);
        assert_eq!(
            screen.get_selected_item().header.as_deref().map(|row| row
                .iter()
                .map(|(text, _)| text.as_str())
                .collect::<String>()),
            Some(stat_row)
        );
    }

    /// Moving down a stat goes from file to file.
    #[test]
    fn the_cursor_moves_down_a_stat_file_by_file() {
        let ctx = repo_setup_clone!();
        let mut screen = stat_screen(&ctx);
        let first = screen.get_selected_item().data.clone();

        screen.select_next(crate::screen::NavMode::Normal);

        assert!(
            matches!(first, ItemData::Delta { file_i: 0, .. }),
            "{first:?}"
        );
        assert!(
            matches!(
                screen.get_selected_item().data,
                ItemData::Delta { file_i: 1, .. }
            ),
            "{:?}",
            screen.get_selected_item().data
        );
    }

    /// Folding everything from within a file's diff leaves the cursor on that
    /// file.
    #[test]
    fn folding_all_from_within_a_stat_file_keeps_the_cursor_on_it() {
        let ctx = repo_setup_clone!();
        for file_i in 0..2 {
            let mut screen = stat_screen(&ctx);
            for _ in 0..file_i {
                screen.select_next(crate::screen::NavMode::Normal);
            }
            screen.toggle_section().unwrap();
            for _ in 0..3 {
                screen.select_next(crate::screen::NavMode::IncludeSubLines);
            }
            assert!(
                matches!(screen.get_selected_item().data, ItemData::HunkLine { .. }),
                "{:?}",
                screen.get_selected_item().data
            );

            screen.toggle_all_sections();

            assert!(
                matches!(
                    screen.get_selected_item().data,
                    ItemData::Delta { file_i: i, .. } if i == file_i
                ),
                "file {file_i}: {:?}",
                screen.get_selected_item().data
            );
        }
    }

    /// The screen for `git diff --stat` of changes to two files.
    fn stat_screen(ctx: &RepoTestContext) -> Screen {
        std::fs::write(ctx.dir.join("initial-file"), "hello\nworld\n").unwrap();
        std::fs::write(ctx.dir.join("second-file"), "one\n").unwrap();
        run(&ctx.dir, &["git", "add", "-N", "second-file"]);
        let argv = ["git", "diff", "--stat"].map(String::from).to_vec();

        create(
            Arc::new(config::init_test_config().unwrap()),
            Rc::new(Repository::open(&ctx.dir).unwrap()),
            RenderParams {
                size: (80, 20),
                features: Rc::from([]),
                context: None,
                streaming: false,
            },
            paged(&run(&ctx.dir, &["git", "diff", "--stat"]), Some(argv)),
        )
        .unwrap()
    }

    fn write(ctx: &RepoTestContext, content: &str) {
        std::fs::write(ctx.dir.join("f.txt"), content).unwrap();
    }

    /// How many lines of the diff the screen shows, for the patch git piped and
    /// the command it ran to make it.
    fn hunk_lines(
        ctx: &RepoTestContext,
        patch: &str,
        argv: &[String],
        context: Option<&str>,
    ) -> usize {
        let config = config::init_test_config().unwrap();
        let repo = Repository::open(&ctx.dir).unwrap();
        Source::of(paged(patch, Some(argv.to_vec())), &[])
            .items(
                &config,
                &repo,
                &RenderParams {
                    context: context.map(Rc::from),
                    ..Default::default()
                },
            )
            .unwrap()
            .iter()
            .filter(|item| matches!(item.data, ItemData::HunkLine { .. }))
            .count()
    }
}