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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
use std::mem::swap;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use git_branchless_undo::testing::{select_past_event, undo_events};
use git_branchless_undo::tui::testing::{
screen_to_string, CursiveTestingBackend, CursiveTestingEvent,
};
use lib::core::dag::Dag;
use lib::core::effects::Effects;
use lib::core::eventlog::{EventCursor, EventLogDb, EventReplayer};
use lib::core::formatting::Glyphs;
use lib::core::repo_ext::RepoExt;
use lib::git::{GitRunInfo, GitVersion, Repo};
use lib::testing::{make_git, trim_lines, Git, GitInitOptions, GitRunOptions};
use cursive_core::event::Key;
use cursive_core::{Cursive, CursiveRunner};
use lib::util::ExitCode;
fn run_select_past_event(
repo: &Repo,
events: Vec<CursiveTestingEvent>,
) -> eyre::Result<Option<EventCursor>> {
let glyphs = Glyphs::text();
let effects = Effects::new_suppress_for_test(glyphs);
let references_snapshot = repo.get_references_snapshot()?;
let conn = repo.get_db_conn()?;
let event_log_db: EventLogDb = EventLogDb::new(&conn)?;
let mut event_replayer = EventReplayer::from_event_log_db(&effects, repo, &event_log_db)?;
let event_cursor = event_replayer.make_default_cursor();
let dag = Dag::open_and_sync(
&effects,
repo,
&event_replayer,
event_cursor,
&references_snapshot,
)?;
let backend = CursiveTestingBackend::init(events);
let siv = Cursive::new();
let siv = CursiveRunner::new(siv, backend);
select_past_event(siv, &effects, repo, &dag, &mut event_replayer)
}
fn run_undo_events(git: &Git, event_cursor: EventCursor) -> eyre::Result<(isize, String)> {
let glyphs = Glyphs::text();
let effects = Effects::new_suppress_for_test(glyphs.clone());
let repo = git.get_repo()?;
let conn = repo.get_db_conn()?;
let mut event_log_db: EventLogDb = EventLogDb::new(&conn)?;
let event_replayer = EventReplayer::from_event_log_db(&effects, &repo, &event_log_db)?;
let input = "y";
let mut in_ = input.as_bytes();
let stdout: Arc<Mutex<Vec<u8>>> = Default::default();
let stderr: Arc<Mutex<Vec<u8>>> = Default::default();
let git_run_info = GitRunInfo {
path_to_git: git.path_to_git.clone(),
working_directory: repo.get_working_copy_path().unwrap().to_path_buf(),
env: git.get_base_env(0).into_iter().collect(),
};
let exit_code = match undo_events(
&mut in_,
&Effects::new_from_buffer_for_test(glyphs, &stdout, &stderr),
&repo,
&git_run_info,
&mut event_log_db,
&event_replayer,
event_cursor,
)? {
Ok(()) => 0,
Err(ExitCode(exit_code)) => exit_code,
};
let stdout = {
let mut buf = stdout.lock().unwrap();
let mut result_buf = Vec::new();
swap(&mut *buf, &mut result_buf);
result_buf
};
let stdout = String::from_utf8(stdout)?;
let stdout = git.preprocess_output(stdout)?;
let stdout = trim_lines(stdout);
Ok((exit_code, stdout))
}
#[test]
fn test_undo_help() -> eyre::Result<()> {
let git = make_git()?;
git.init_repo()?;
{
let screenshot1 = Default::default();
run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::Event('h'.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot1)),
CursiveTestingEvent::Event('q'.into()),
],
)?;
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│O f777ecc (master) create initial.txt │
│ │
│ │
│ │
│ ┌───────────────────────────────────────────┤ How to use ├───────────────────────────────────────────┐ │
│ │ Use `git undo` to view and revert to previous states of the repository. │ │
│ │ │ │
│ │ h/?: Show this help. │ │
│ │ q: Quit. │ │
│ │ p/n or <left>/<right>: View next/previous state. │ │
│ │ g: Go to a provided event ID. │ │
│ │ <enter>: Revert the repository to the given state (requires confirmation). │ │
│ │ │ │
│ │ You can also copy a commit hash from the past and manually run `git unhide` or `git rebase` on it. │ │
│ │ │ │
│ │ <Close> │ │
│ └────────────────────────────────────────────────────────────────────────────────────────────────────┘ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│There are no previous available events. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
}
Ok(())
}
#[test]
fn test_undo_navigate() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? || git.produces_auto_merge_refs()? {
return Ok(());
}
git.init_repo()?;
git.commit_file("test1", 1)?;
git.commit_file("test2", 2)?;
{
let screenshot1 = Default::default();
let screenshot2 = Default::default();
let event_cursor = run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot1)),
CursiveTestingEvent::Event('n'.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot2)),
CursiveTestingEvent::Event(Key::Enter.into()),
],
)?;
insta::assert_debug_snapshot!(event_cursor, @r###"
Some(
EventCursor {
event_id: 6,
},
)
"###);
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│: │
│@ 96d1c37 (master) create test2.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 3 (event 4). Press 'h' for help, 'q' to quit. │
│1. Check out from 62fc20d create test1.txt │
│ to 96d1c37 create test2.txt │
│2. Move branch master from 62fc20d create test1.txt │
│ to 96d1c37 create test2.txt │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
insta::assert_snapshot!(screen_to_string(&screenshot2), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│: │
│@ 96d1c37 (master) create test2.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 4 (event 6). Press 'h' for help, 'q' to quit. │
│1. Commit 96d1c37 create test2.txt │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
};
Ok(())
}
#[test]
fn test_go_to_event() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? || git.produces_auto_merge_refs()? {
return Ok(());
}
git.init_repo()?;
git.commit_file("test1", 1)?;
git.commit_file("test2", 2)?;
let screenshot1 = Default::default();
let screenshot2 = Default::default();
run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot1)),
CursiveTestingEvent::Event('g'.into()),
CursiveTestingEvent::Event('1'.into()),
CursiveTestingEvent::Event(Key::Enter.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot2)),
CursiveTestingEvent::Event('q'.into()),
],
)?;
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│: │
│@ 96d1c37 (master) create test2.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 4 (event 6). Press 'h' for help, 'q' to quit. │
│1. Commit 96d1c37 create test2.txt │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
insta::assert_snapshot!(screen_to_string(&screenshot2), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│: │
│@ 62fc20d create test1.txt │
│| │
│O 96d1c37 (master) create test2.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 1 (event 1). Press 'h' for help, 'q' to quit. │
│1. Check out from f777ecc create initial.txt │
│ to 62fc20d create test1.txt │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
Ok(())
}
#[test]
fn test_undo_hide() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? {
return Ok(());
}
git.init_repo()?;
git.run(&["checkout", "-b", "test1"])?;
git.commit_file("test1", 1)?;
git.run(&["checkout", "HEAD^"])?;
git.commit_file("test2", 2)?;
git.branchless("hide", &["test1"])?;
{
let (stdout, stderr) = git.run(&["smartlog"])?;
insta::assert_snapshot!(stderr, @"");
insta::assert_snapshot!(stdout, @r###"
O f777ecc (master) create initial.txt
|
@ fe65c1f create test2.txt
"###);
}
let event_cursor = run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event(Key::Enter.into()),
CursiveTestingEvent::Event('y'.into()),
],
)?;
insta::assert_debug_snapshot!(event_cursor, @r###"
Some(
EventCursor {
event_id: 9,
},
)
"###);
let event_cursor = event_cursor.unwrap();
{
let (exit_code, stdout) = run_undo_events(&git, event_cursor)?;
insta::assert_snapshot!(stdout, @r###"
Will apply these actions:
1. Create branch test1 at 62fc20d create test1.txt
2. Unhide commit 62fc20d create test1.txt
Confirm? [yN] Applied 2 inverse events.
"###);
assert_eq!(exit_code, 0);
}
{
let stdout = git.smartlog()?;
insta::assert_snapshot!(stdout, @r###"
O f777ecc (master) create initial.txt
|\
| o 62fc20d (test1) create test1.txt
|
@ fe65c1f create test2.txt
"###);
}
Ok(())
}
#[test]
fn test_undo_move_refs() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? {
return Ok(());
}
git.init_repo()?;
git.commit_file("test1", 1)?;
git.commit_file("test2", 2)?;
let event_cursor = run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event(Key::Enter.into()),
CursiveTestingEvent::Event('y'.into()),
],
)?;
insta::assert_debug_snapshot!(event_cursor, @r###"
Some(
EventCursor {
event_id: 3,
},
)
"###);
let event_cursor = event_cursor.unwrap();
{
let (exit_code, stdout) = run_undo_events(&git, event_cursor)?;
insta::assert_snapshot!(stdout, @r###"
Will apply these actions:
1. Hide commit 96d1c37 create test2.txt
2. Move branch master from 96d1c37 create test2.txt
to 62fc20d create test1.txt
3. Check out from 96d1c37 create test2.txt
to 62fc20d create test1.txt
Confirm? [yN] branchless: running command: <git-executable> checkout master --detach
Applied 3 inverse events.
"###);
assert_eq!(exit_code, 0);
}
{
let stdout = git.smartlog()?;
insta::assert_snapshot!(stdout, @r###"
:
@ 62fc20d (master) create test1.txt
"###);
}
Ok(())
}
#[test]
fn test_historical_smartlog_visibility() -> eyre::Result<()> {
let git = make_git()?;
if git.produces_auto_merge_refs()? {
return Ok(());
}
git.init_repo()?;
git.detach_head()?;
git.commit_file("test1", 1)?;
git.branchless("hide", &["HEAD"])?;
let screenshot1 = Default::default();
let screenshot2 = Default::default();
run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot1)),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot2)),
CursiveTestingEvent::Event('q'.into()),
],
)?;
if git.supports_reference_transactions()? {
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│O f777ecc (master) create initial.txt │
│| │
│% 62fc20d (manually hidden) create test1.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 5 (event 5). Press 'h' for help, 'q' to quit. │
│1. Hide commit 62fc20d create test1.txt │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
insta::assert_snapshot!(screen_to_string(&screenshot2), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│O f777ecc (master) create initial.txt │
│| │
│@ 62fc20d create test1.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 4 (event 4). Press 'h' for help, 'q' to quit. │
│1. Commit 62fc20d create test1.txt │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
} else {
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│O f777ecc (master) create initial.txt │
│| │
│% 62fc20d (manually hidden) create test1.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 3 (event 3). Press 'h' for help, 'q' to quit. │
│1. Hide commit 62fc20d create test1.txt │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
insta::assert_snapshot!(screen_to_string(&screenshot2), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│O f777ecc (master) create initial.txt │
│| │
│@ 62fc20d create test1.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 2 (event 2). Press 'h' for help, 'q' to quit. │
│1. Commit 62fc20d create test1.txt │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
}
Ok(())
}
#[test]
fn test_undo_doesnt_make_working_dir_dirty() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? {
return Ok(());
}
git.init_repo()?;
// Modify a reference.
git.run(&["branch", "foo"])?;
// Make a change that causes a checkout.
git.commit_file("test1", 1)?;
// Modify a reference.
git.run(&["branch", "bar"])?;
let screenshot1 = Default::default();
let event_cursor = run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot1)),
CursiveTestingEvent::Event(Key::Enter.into()),
],
)?;
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│: │
│O 62fc20d (master) create test1.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│There are no previous available events. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
// If there are no dirty files in the repository prior to the `undo`,
// then there should still be no dirty files after the `undo`.
let event_cursor = event_cursor.expect("Should have an event cursor to undo");
{
let (stdout, _stderr) = git.run(&["status", "--porcelain"])?;
assert_eq!(stdout, "");
}
{
let (exit_code, stdout) = run_undo_events(&git, event_cursor)?;
insta::assert_snapshot!(stdout, @r###"
Will apply these actions:
1. Delete branch bar at 62fc20d create test1.txt
2. Hide commit 62fc20d create test1.txt
3. Move branch master from 62fc20d create test1.txt
to f777ecc create initial.txt
4. Check out from 62fc20d create test1.txt
to f777ecc create initial.txt
5. Delete branch foo at f777ecc create initial.txt
Confirm? [yN] branchless: running command: <git-executable> checkout master --detach
Applied 5 inverse events.
"###);
assert_eq!(exit_code, 0);
}
{
let (stdout, _stderr) = git.run(&["status", "--porcelain"])?;
assert_eq!(stdout, "");
}
Ok(())
}
/// See https://github.com/arxanas/git-branchless/issues/57
#[cfg(unix)]
#[test]
fn test_git_bisect_produces_empty_event() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? {
return Ok(());
}
if git.get_version()? >= GitVersion(2, 42, 0) {
// Later versions of Git write `BISECT_EXPECTED_REV` to the filesystem
// as well, causing the below test to fail.
return Ok(());
}
git.init_repo()?;
git.commit_file("test1", 1)?;
git.run(&["bisect", "start"])?;
git.run(&["bisect", "good", "HEAD^"])?;
git.run(&["bisect", "bad"])?;
let screenshot1 = Default::default();
run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot1)),
CursiveTestingEvent::Event(Key::Enter.into()),
],
)?;
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│: │
│@ 62fc20d (master) create test1.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 3 (event 4). Press 'h' for help, 'q' to quit. │
│1. Empty event for BISECT_HEAD │
│ This may be an unsupported use-case; see https://github.com/arxanas/git-branchless/issues/57 │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
Ok(())
}
#[test]
fn test_undo_garbage_collected_commit() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? {
return Ok(());
}
let git_version = git.get_version()?;
if git_version >= GitVersion(2, 35, 0) {
// Change in reference-transaction behavior causes this test to fail.
return Ok(());
}
git.init_repo()?;
git.commit_file("test1", 1)?;
git.detach_head()?;
git.commit_file("test2", 2)?;
git.branchless("hide", &["HEAD"])?;
git.run(&["checkout", "HEAD^"])?;
{
let (stdout, _stderr) = git.branchless("gc", &[])?;
insta::assert_snapshot!(stdout, @r###"
branchless: collecting garbage
branchless: 1 dangling reference deleted
"###);
}
git.run(&["gc", "--prune=now"])?;
{
let stdout = git.smartlog()?;
insta::assert_snapshot!(stdout, @r###"
:
@ 62fc20d (master) create test1.txt
"###);
}
let screenshot1 = Default::default();
let event_cursor = run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot1)),
CursiveTestingEvent::Event(Key::Enter.into()),
],
)?;
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│: │
│O 62fc20d (master) create test1.txt │
│| │
│% 96d1c37 (manually hidden) <garbage collected> │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│Repo after transaction 7 (event 8). Press 'h' for help, 'q' to quit. │
│1. Hide commit <commit not available: 96d1c37a3d4363611c49f7e52186e189a04c531f> │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
let event_cursor = event_cursor.unwrap();
{
let (exit_code, stdout) = run_undo_events(&git, event_cursor)?;
insta::assert_snapshot!(stdout, @r###"
Will apply these actions:
1. Move branch master from 62fc20d create test1.txt
to 62fc20d create test1.txt
2. Move branch master from 62fc20d create test1.txt
to 62fc20d create test1.txt
3. Check out from 62fc20d create test1.txt
to <commit not available: 96d1c37a3d4363611c49f7e52186e189a04c531f>
Confirm? [yN] branchless: running command: <git-executable> checkout 96d1c37a3d4363611c49f7e52186e189a04c531f --detach
Failed to check out commit: 96d1c37a3d4363611c49f7e52186e189a04c531f
"###);
assert!(exit_code > 0);
}
Ok(())
}
#[test]
fn test_undo_noninteractive() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? {
return Ok(());
}
git.init_repo()?;
git.commit_file("test1", 1)?;
git.commit_file("test2", 2)?;
git.branchless("wrap", &["--", "commit", "--amend", "-m", "bad message"])?;
{
let (stdout, _stderr) = git.branchless_with_options(
"undo",
&[],
&GitRunOptions {
expected_exit_code: 1,
input: Some("n".to_string()),
..Default::default()
},
)?;
let stdout = trim_lines(stdout);
insta::assert_snapshot!(stdout, @r###"
Will apply these actions:
1. Rewrite commit 9ed8f9a bad message
as 96d1c37 create test2.txt
2. Hide commit 9ed8f9a bad message
3. Move branch master from 9ed8f9a bad message
to 96d1c37 create test2.txt
4. Check out from 9ed8f9a bad message
to 96d1c37 create test2.txt
Confirm? [yN] Aborted.
"###);
}
{
let stdout = git.smartlog()?;
insta::assert_snapshot!(stdout, @r###"
:
@ 9ed8f9a (> master) bad message
"###);
}
{
let (stdout, _stderr) = git.branchless_with_options(
"undo",
&[],
&GitRunOptions {
input: Some("y".to_string()),
..Default::default()
},
)?;
let stdout = trim_lines(stdout);
insta::assert_snapshot!(stdout, @r###"
Will apply these actions:
1. Rewrite commit 9ed8f9a bad message
as 96d1c37 create test2.txt
2. Hide commit 9ed8f9a bad message
3. Move branch master from 9ed8f9a bad message
to 96d1c37 create test2.txt
4. Check out from 9ed8f9a bad message
to 96d1c37 create test2.txt
Confirm? [yN] branchless: running command: <git-executable> checkout master --detach
:
@ 96d1c37 (master) create test2.txt
Applied 4 inverse events.
"###);
}
{
let stdout = git.smartlog()?;
insta::assert_snapshot!(stdout, @r###"
:
@ 96d1c37 (master) create test2.txt
"###);
}
Ok(())
}
#[test]
fn test_undo_no_confirm() -> eyre::Result<()> {
let git = make_git()?;
if !git.supports_reference_transactions()? {
return Ok(());
}
git.init_repo()?;
git.commit_file("test1", 1)?;
{
let (stdout, _stderr) = git.branchless("undo", &["--yes"])?;
let stdout = trim_lines(stdout);
insta::assert_snapshot!(stdout, @r###"
Will apply these actions:
1. Hide commit 62fc20d create test1.txt
Applied 1 inverse event.
"###);
}
Ok(())
}
#[test]
fn test_undo_unseen_commit() -> eyre::Result<()> {
// Disabled since we no longer support `origin/master` as a main branch, but this test might be
// useful in the future.
if false {
let git = make_git()?;
if !git.supports_reference_transactions()? {
return Ok(());
}
git.init_repo_with_options(&GitInitOptions {
run_branchless_init: false,
make_initial_commit: true,
})?;
git.commit_file("test1", 1)?;
git.detach_head()?;
let test2_oid = git.commit_file("test2", 2)?;
git.run(&["checkout", "HEAD^"])?;
let test3_oid = git.commit_file("test3", 3)?;
git.run(&["checkout", "master"])?;
git.branchless("init", &[])?;
// Move the remote-tracking branch to a commit (test3) which doesn't have the previous location
// (test2) as an ancestor, to ensure that the DAG won't have observed it.
git.run(&[
"update-ref",
"refs/remotes/origin/master",
&test2_oid.to_string(),
])?;
git.run(&[
"update-ref",
"refs/remotes/origin/master",
&test3_oid.to_string(),
&test2_oid.to_string(),
])?;
// Set the main branch to `origin/master` to ensure that it appears in historical smartlogs.
git.run(&["remote", "add", "origin", "file:///some-remote"])?;
git.run(&["branch", "-u", "origin/master"])?;
git.branchless("init", &["--main-branch", "origin/master"])?;
{
let screenshot1 = Default::default();
run_select_past_event(
&git.get_repo()?,
vec![
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::Event('p'.into()),
CursiveTestingEvent::TakeScreenshot(Rc::clone(&screenshot1)),
CursiveTestingEvent::Event('q'.into()),
],
)?;
insta::assert_snapshot!(screen_to_string(&screenshot1), @r###"
┌───────────────────────────────────────────────────┤ Commit graph ├───────────────────────────────────────────────────┐
│: │
│O 4838e49 (remote origin/master) create test3.txt │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────┤ Events ├──────────────────────────────────────────────────────┐
│There are no previous available events. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
"###);
}
}
Ok(())
}