jj-cli 0.34.0

Jujutsu - an experimental version control system
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
// Copyright 2022 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use bstr::ByteSlice as _;
use indoc::indoc;

use crate::common::TestEnvironment;

#[test]
fn test_diffedit() {
    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    work_dir.write_file("file1", "a\n");
    work_dir.run_jj(["new"]).success();
    work_dir.write_file("file2", "a\n");
    work_dir.write_file("file3", "a\n");
    work_dir.run_jj(["new"]).success();
    work_dir.remove_file("file1");
    work_dir.write_file("file2", "b\n");
    work_dir.run_jj(["debug", "snapshot"]).success();
    let setup_opid = work_dir.current_operation_id();

    // Test the setup; nothing happens if we make no changes
    std::fs::write(
        &edit_script,
        [
            "files-before file1 file2",
            "files-after JJ-INSTRUCTIONS file2",
            "dump JJ-INSTRUCTIONS instrs",
        ]
        .join("\0"),
    )
    .unwrap();
    let output = work_dir.run_jj(["diffedit"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    insta::assert_snapshot!(
        std::fs::read_to_string(test_env.env_root().join("instrs")).unwrap(), @r"
    You are editing changes in: kkmpptxz e4245972 (no description set)

    The diff initially shows the commit's changes.

    Adjust the right side until it shows the contents you want. If you
    don't make any changes, then the operation will be aborted.
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    [EOF]
    ");

    // Try again with ui.diff-instructions=false
    std::fs::write(&edit_script, "files-before file1 file2\0files-after file2").unwrap();
    let output = work_dir.run_jj(["diffedit", "--config=ui.diff-instructions=false"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    [EOF]
    ");

    // Try again with --tool=<name>
    std::fs::write(
        &edit_script,
        "files-before file1 file2\0files-after JJ-INSTRUCTIONS file2",
    )
    .unwrap();
    let output = work_dir.run_jj([
        "diffedit",
        "--config=ui.diff-editor='false'",
        "--tool=fake-diff-editor",
    ]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    [EOF]
    ");

    // Nothing happens if the diff-editor exits with an error
    std::fs::write(&edit_script, "rm file2\0fail").unwrap();
    let output = work_dir.run_jj(["diffedit"]);
    insta::assert_snapshot!(output.normalize_stderr_exit_status(), @r"
    ------- stderr -------
    Error: Failed to edit diff
    Caused by: Tool exited with exit status: 1 (run with --debug to see the exact invocation)
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    [EOF]
    ");

    // Can edit changes to individual files
    std::fs::write(&edit_script, "reset file2").unwrap();
    let output = work_dir.run_jj(["diffedit"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: kkmpptxz 40ad4f80 (no description set)
    Parent commit (@-)      : rlvkpnrz 7e268da3 (no description set)
    Added 0 files, modified 1 files, removed 0 files
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    [EOF]
    ");

    // Changes to a commit are propagated to descendants
    work_dir.run_jj(["op", "restore", &setup_opid]).success();
    std::fs::write(&edit_script, "write file3\nmodified\n").unwrap();
    let output = work_dir.run_jj(["diffedit", "-r", "@-"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Rebased 1 descendant commits
    Working copy  (@) now at: kkmpptxz 9f0ebae1 (no description set)
    Parent commit (@-)      : rlvkpnrz 72bcd8e9 (no description set)
    Added 0 files, modified 1 files, removed 0 files
    [EOF]
    ");
    let contents = work_dir.read_file("file3");
    insta::assert_snapshot!(contents, @"modified");

    // Test diffedit --from @--
    work_dir.run_jj(["op", "restore", &setup_opid]).success();
    std::fs::write(
        &edit_script,
        "files-before file1\0files-after JJ-INSTRUCTIONS file2 file3\0reset file2",
    )
    .unwrap();
    let output = work_dir.run_jj(["diffedit", "--from", "@--"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: kkmpptxz 215fca5f (no description set)
    Parent commit (@-)      : rlvkpnrz 7e268da3 (no description set)
    Added 0 files, modified 0 files, removed 1 files
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    D file2
    [EOF]
    ");

    // Test with path restriction
    work_dir.run_jj(["op", "restore", &setup_opid]).success();
    work_dir.write_file("file3", "a\n");
    work_dir.run_jj(["new"]).success();
    work_dir.write_file("file1", "modified\n");
    work_dir.write_file("file2", "modified\n");
    work_dir.write_file("file3", "modified\n");

    // Edit only file2 with path argument
    std::fs::write(
        &edit_script,
        "files-before file2\0files-after JJ-INSTRUCTIONS file2\0reset file2",
    )
    .unwrap();
    let output = work_dir.run_jj(["diffedit", "file2"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: tlkvzzqu 06bdff15 (no description set)
    Parent commit (@-)      : kkmpptxz e4245972 (no description set)
    Added 0 files, modified 1 files, removed 0 files
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    C {file3 => file1}
    M file3
    [EOF]
    ");
}

#[test]
fn test_diffedit_new_file() {
    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    work_dir.write_file("file1", "a\n");
    work_dir.run_jj(["new"]).success();
    work_dir.remove_file("file1");
    work_dir.write_file("file2", "b\n");
    work_dir.run_jj(["debug", "snapshot"]).success();
    let setup_opid = work_dir.current_operation_id();

    // Test the setup; nothing happens if we make no changes
    std::fs::write(
        &edit_script,
        "files-before file1\0files-after JJ-INSTRUCTIONS file2",
    )
    .unwrap();
    let output = work_dir.run_jj(["diffedit"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    A file2
    [EOF]
    ");

    // Creating `file1` on the right side is noticed by `jj diffedit`
    std::fs::write(&edit_script, "write file1\nmodified\n").unwrap();
    let output = work_dir.run_jj(["diffedit"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: rlvkpnrz c26dcad1 (no description set)
    Parent commit (@-)      : qpvuntsm eb7b8a1f (no description set)
    Added 1 files, modified 0 files, removed 0 files
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    M file1
    A file2
    [EOF]
    ");

    // Creating a file that wasn't on either side is ignored by diffedit.
    // TODO(ilyagr) We should decide whether we like this behavior.
    //
    // On one hand, it is unexpected and potentially a minor BUG. On the other
    // hand, this prevents `jj` from loading any backup files the merge tool
    // generates.
    work_dir.run_jj(["op", "restore", &setup_opid]).success();
    std::fs::write(&edit_script, "write new_file\nnew file\n").unwrap();
    let output = work_dir.run_jj(["diffedit"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    A file2
    [EOF]
    ");
}

#[test]
fn test_diffedit_existing_instructions() {
    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    // A diff containing an existing JJ-INSTRUCTIONS file themselves.
    work_dir.write_file("JJ-INSTRUCTIONS", "instruct");

    std::fs::write(&edit_script, "write JJ-INSTRUCTIONS\nmodified\n").unwrap();
    let output = work_dir.run_jj(["diffedit"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: qpvuntsm e914aaad (no description set)
    Parent commit (@-)      : zzzzzzzz 00000000 (empty) (no description set)
    Added 0 files, modified 1 files, removed 0 files
    [EOF]
    ");
    // Test that we didn't delete or overwrite the "JJ-INSTRUCTIONS" file.
    let content = work_dir.read_file("JJ-INSTRUCTIONS");
    insta::assert_snapshot!(content, @r"modified");
}

#[test]
fn test_diffedit_external_tool_conflict_marker_style() {
    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");
    let file_path = "file";

    // Create a conflict
    work_dir.write_file(
        file_path,
        indoc! {"
            line 1
            line 2
            line 3
            line 4
            line 5
        "},
    );
    work_dir.run_jj(["commit", "-m", "base"]).success();
    work_dir.write_file(
        file_path,
        indoc! {"
            line 1
            line 2.1
            line 2.2
            line 3
            line 4.1
            line 5
        "},
    );
    work_dir.run_jj(["describe", "-m", "side-a"]).success();
    work_dir
        .run_jj(["new", "description(base)", "-m", "side-b"])
        .success();
    work_dir.write_file(
        file_path,
        indoc! {"
            line 1
            line 2.3
            line 3
            line 4.2
            line 4.3
            line 5
        "},
    );

    // Resolve one of the conflicts in the working copy
    work_dir
        .run_jj(["new", "description(side-a)", "description(side-b)"])
        .success();
    work_dir.write_file(
        file_path,
        indoc! {"
            line 1
            line 2.1
            line 2.2
            line 2.3
            line 3
            <<<<<<<
            %%%%%%%
            -line 4
            +line 4.1
            +++++++
            line 4.2
            line 4.3
            >>>>>>>
            line 5
        "},
    );

    // Set up diff editor to use "snapshot" conflict markers
    test_env.add_config(r#"merge-tools.fake-diff-editor.conflict-marker-style = "snapshot""#);

    // We want to see whether the diff editor is using the correct conflict markers,
    // and reset it to make sure that it parses the conflict markers as well
    std::fs::write(
        &edit_script,
        [
            "files-before file",
            "files-after JJ-INSTRUCTIONS file",
            "dump file after-file",
            "reset file",
            "dump file before-file",
        ]
        .join("\0"),
    )
    .unwrap();
    let output = work_dir.run_jj(["diffedit"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: mzvwutvl 268f208f (conflict) (empty) (no description set)
    Parent commit (@-)      : rlvkpnrz 74e448a1 side-a
    Parent commit (@-)      : zsuskuln 6982bce7 side-b
    Added 0 files, modified 1 files, removed 0 files
    Warning: There are unresolved conflicts at these paths:
    file    2-sided conflict
    Existing conflicts were resolved or abandoned from 1 commits.
    [EOF]
    ");
    // Conflicts should render using "snapshot" format in diff editor
    insta::assert_snapshot!(
        std::fs::read_to_string(test_env.env_root().join("before-file")).unwrap(), @r"
    line 1
    <<<<<<< Conflict 1 of 2
    +++++++ Contents of side #1
    line 2.1
    line 2.2
    ------- Contents of base
    line 2
    +++++++ Contents of side #2
    line 2.3
    >>>>>>> Conflict 1 of 2 ends
    line 3
    <<<<<<< Conflict 2 of 2
    +++++++ Contents of side #1
    line 4.1
    ------- Contents of base
    line 4
    +++++++ Contents of side #2
    line 4.2
    line 4.3
    >>>>>>> Conflict 2 of 2 ends
    line 5
    ");
    insta::assert_snapshot!(
        std::fs::read_to_string(test_env.env_root().join("after-file")).unwrap(), @r"
    line 1
    line 2.1
    line 2.2
    line 2.3
    line 3
    <<<<<<< Conflict 1 of 1
    +++++++ Contents of side #1
    line 4.1
    ------- Contents of base
    line 4
    +++++++ Contents of side #2
    line 4.2
    line 4.3
    >>>>>>> Conflict 1 of 1 ends
    line 5
    ");
    // Conflicts should be materialized using "diff" format in working copy
    insta::assert_snapshot!(work_dir.read_file(file_path), @r"
    line 1
    <<<<<<< Conflict 1 of 2
    +++++++ Contents of side #1
    line 2.1
    line 2.2
    %%%%%%% Changes from base to side #2
    -line 2
    +line 2.3
    >>>>>>> Conflict 1 of 2 ends
    line 3
    <<<<<<< Conflict 2 of 2
    %%%%%%% Changes from base to side #1
    -line 4
    +line 4.1
    +++++++ Contents of side #2
    line 4.2
    line 4.3
    >>>>>>> Conflict 2 of 2 ends
    line 5
    ");

    // File should be conflicted with no changes
    let output = work_dir.run_jj(["st"]);
    insta::assert_snapshot!(output, @r"
    The working copy has no changes.
    Working copy  (@) : mzvwutvl 268f208f (conflict) (empty) (no description set)
    Parent commit (@-): rlvkpnrz 74e448a1 side-a
    Parent commit (@-): zsuskuln 6982bce7 side-b
    Warning: There are unresolved conflicts at these paths:
    file    2-sided conflict
    [EOF]
    ");
}

#[test]
fn test_diffedit_3pane() {
    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    work_dir.write_file("file1", "a\n");
    work_dir.run_jj(["new"]).success();
    work_dir.write_file("file2", "a\n");
    work_dir.write_file("file3", "a\n");
    work_dir.run_jj(["new"]).success();
    work_dir.remove_file("file1");
    work_dir.write_file("file2", "b\n");
    work_dir.run_jj(["debug", "snapshot"]).success();
    let setup_opid = work_dir.current_operation_id();

    // 2 configs for a 3-pane setup. In the first, "$right" is passed to what the
    // fake diff editor considers the "after" state.
    let config_with_right_as_after =
        "merge-tools.fake-diff-editor.edit-args=['$left', '$right', '--ignore=$output']";
    let config_with_output_as_after =
        "merge-tools.fake-diff-editor.edit-args=['$left', '$output', '--ignore=$right']";
    std::fs::write(&edit_script, "").unwrap();

    // Nothing happens if we make no changes
    std::fs::write(
        &edit_script,
        "files-before file1 file2\0files-after JJ-INSTRUCTIONS file2",
    )
    .unwrap();
    let output = work_dir.run_jj(["diffedit", "--config", config_with_output_as_after]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    [EOF]
    ");
    // Nothing happens if we make no changes, `config_with_right_as_after` version
    let output = work_dir.run_jj(["diffedit", "--config", config_with_right_as_after]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    [EOF]
    ");

    // Can edit changes to individual files
    std::fs::write(&edit_script, "reset file2").unwrap();
    let output = work_dir.run_jj(["diffedit", "--config", config_with_output_as_after]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: kkmpptxz 239413bd (no description set)
    Parent commit (@-)      : rlvkpnrz 7e268da3 (no description set)
    Added 0 files, modified 1 files, removed 0 files
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    [EOF]
    ");

    // Can write something new to `file1`
    work_dir.run_jj(["op", "restore", &setup_opid]).success();
    std::fs::write(&edit_script, "write file1\nnew content").unwrap();
    let output = work_dir.run_jj(["diffedit", "--config", config_with_output_as_after]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: kkmpptxz 95873a91 (no description set)
    Parent commit (@-)      : rlvkpnrz 7e268da3 (no description set)
    Added 1 files, modified 0 files, removed 0 files
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    M file1
    M file2
    [EOF]
    ");

    // But nothing happens if we modify the right side
    work_dir.run_jj(["op", "restore", &setup_opid]).success();
    std::fs::write(&edit_script, "write file1\nnew content").unwrap();
    let output = work_dir.run_jj(["diffedit", "--config", config_with_right_as_after]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    [EOF]
    ");

    // TODO: test with edit_script of "reset file2". This fails on right side
    // since the file is readonly.
}

#[test]
fn test_diffedit_merge() {
    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    work_dir.write_file("file1", "a\n");
    work_dir.write_file("file2", "a\n");
    work_dir.run_jj(["new"]).success();
    work_dir
        .run_jj(["bookmark", "create", "-r@", "b"])
        .success();
    work_dir.write_file("file1", "b\n");
    work_dir.write_file("file2", "b\n");
    work_dir.run_jj(["new", "@-"]).success();
    work_dir.run_jj(["new"]).success();
    work_dir.write_file("file1", "c\n");
    work_dir.write_file("file2", "c\n");
    work_dir.run_jj(["new", "@", "b", "-m", "merge"]).success();
    // Resolve the conflict in file1, but leave the conflict in file2
    work_dir.write_file("file1", "d\n");
    work_dir.write_file("file3", "d\n");
    work_dir.run_jj(["new"]).success();
    // Test the setup
    let output = work_dir.run_jj(["diff", "-r", "@-", "-s"]);
    insta::assert_snapshot!(output, @r"
    M file1
    A file3
    [EOF]
    ");

    // Remove file1. The conflict remains in the working copy on top of the merge.
    std::fs::write(
        edit_script,
        "files-before file1\0files-after JJ-INSTRUCTIONS file1 file3\0rm file1",
    )
    .unwrap();
    let output = work_dir.run_jj(["diffedit", "-r", "@-"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Rebased 1 descendant commits
    Working copy  (@) now at: yqosqzyt ce686d54 (conflict) (empty) (no description set)
    Parent commit (@-)      : royxmykx 47cae64e (conflict) merge
    Added 0 files, modified 0 files, removed 1 files
    Warning: There are unresolved conflicts at these paths:
    file2    2-sided conflict
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s", "-r", "@-"]);
    insta::assert_snapshot!(output, @r"
    D file1
    A file3
    [EOF]
    ");
    assert!(!work_dir.root().join("file1").exists());
    let output = work_dir.run_jj(["file", "show", "file2"]);
    insta::assert_snapshot!(output, @r"
    <<<<<<< Conflict 1 of 1
    %%%%%%% Changes from base to side #1
    -a
    +c
    +++++++ Contents of side #2
    b
    >>>>>>> Conflict 1 of 1 ends
    [EOF]
    ");
}

#[test]
fn test_diffedit_old_restore_interactive_tests() {
    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    work_dir.write_file("file1", "a\n");
    work_dir.write_file("file2", "a\n");
    work_dir.run_jj(["new"]).success();
    work_dir.remove_file("file1");
    work_dir.write_file("file2", "b\n");
    work_dir.write_file("file3", "b\n");
    work_dir.run_jj(["debug", "snapshot"]).success();
    let setup_opid = work_dir.current_operation_id();

    // Nothing happens if we make no changes
    let output = work_dir.run_jj(["diffedit", "--from", "@-"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Nothing changed.
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    C {file2 => file3}
    [EOF]
    ");

    // Nothing happens if the diff-editor exits with an error
    std::fs::write(&edit_script, "rm file2\0fail").unwrap();
    let output = work_dir.run_jj(["diffedit", "--from", "@-"]);
    insta::assert_snapshot!(output.normalize_stderr_exit_status(), @r"
    ------- stderr -------
    Error: Failed to edit diff
    Caused by: Tool exited with exit status: 1 (run with --debug to see the exact invocation)
    [EOF]
    [exit status: 1]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    M file2
    C {file2 => file3}
    [EOF]
    ");

    // Can restore changes to individual files
    std::fs::write(&edit_script, "reset file2\0reset file3").unwrap();
    let output = work_dir.run_jj(["diffedit", "--from", "@-"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: rlvkpnrz 83b62f75 (no description set)
    Parent commit (@-)      : qpvuntsm fc6f5e82 (no description set)
    Added 0 files, modified 1 files, removed 1 files
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "-s"]);
    insta::assert_snapshot!(output, @r"
    D file1
    [EOF]
    ");

    // Can make unrelated edits
    work_dir.run_jj(["op", "restore", &setup_opid]).success();
    std::fs::write(&edit_script, "write file3\nunrelated\n").unwrap();
    let output = work_dir.run_jj(["diffedit", "--from", "@-"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Working copy  (@) now at: rlvkpnrz 8119c685 (no description set)
    Parent commit (@-)      : qpvuntsm fc6f5e82 (no description set)
    Added 0 files, modified 1 files, removed 0 files
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "--git"]);
    insta::assert_snapshot!(output, @r"
    diff --git a/file1 b/file1
    deleted file mode 100644
    index 7898192261..0000000000
    --- a/file1
    +++ /dev/null
    @@ -1,1 +0,0 @@
    -a
    diff --git a/file2 b/file2
    index 7898192261..6178079822 100644
    --- a/file2
    +++ b/file2
    @@ -1,1 +1,1 @@
    -a
    +b
    diff --git a/file3 b/file3
    new file mode 100644
    index 0000000000..c21c9352f7
    --- /dev/null
    +++ b/file3
    @@ -0,0 +1,1 @@
    +unrelated
    [EOF]
    ");
}

#[test]
fn test_diffedit_restore_descendants() {
    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");

    work_dir.write_file("file", "println!(\"foo\")\n");
    work_dir.run_jj(["new"]).success();
    work_dir.write_file("file", "println!(\"bar\")\n");
    work_dir.run_jj(["new"]).success();
    work_dir.write_file("file", "println!(\"baz\");\n");

    // Add a ";" after the line with "bar". There should be no conflict.
    std::fs::write(edit_script, "write file\nprintln!(\"bar\");\n").unwrap();
    let output = work_dir.run_jj(["diffedit", "-r", "@-", "--restore-descendants"]);
    insta::assert_snapshot!(output, @r"
    ------- stderr -------
    Rebased 1 descendant commits (while preserving their content)
    Working copy  (@) now at: kkmpptxz a35ef1a5 (no description set)
    Parent commit (@-)      : rlvkpnrz 2e949a84 (no description set)
    [EOF]
    ");
    let output = work_dir.run_jj(["diff", "--git"]);
    insta::assert_snapshot!(output, @r#"
    diff --git a/file b/file
    index 1a598a8fc9..7b6a85ab5a 100644
    --- a/file
    +++ b/file
    @@ -1,1 +1,1 @@
    -println!("bar");
    +println!("baz");
    [EOF]
    "#);
}

#[test]
fn test_diffedit_external_tool_eol_conversion() {
    // Create 2 changes: one creates a file with a single LF, another changes the
    // file to contain 2 LFs. The diff editor should see the same EOL in both the
    // before file and the after file. And when the diff editor adds another EOL to
    // update, we should always see 3 LFs in the store.

    let mut test_env = TestEnvironment::default();
    let edit_script = test_env.set_up_fake_diff_editor();
    test_env.run_jj_in(".", ["git", "init", "repo"]).success();
    let work_dir = test_env.work_dir("repo");
    let file_path = "file";

    // Use the none eol-conversion setting to check in as is.
    let eol_conversion_none_config = "working-copy.eol-conversion='none'";
    work_dir.write_file(file_path, "\n");
    work_dir
        .run_jj(["commit", "--config", eol_conversion_none_config, "-m", "1"])
        .success();
    work_dir.write_file(file_path, "\n\n");
    work_dir
        .run_jj(["commit", "--config", eol_conversion_none_config, "-m", "2"])
        .success();

    std::fs::write(
        &edit_script,
        [
            "dump file after-file",
            "reset file",
            "dump file before-file",
        ]
        .join("\0"),
    )
    .unwrap();
    let test_eol_conversion_config = "working-copy.eol-conversion='input-output'";
    work_dir
        .run_jj([
            "diffedit",
            "-r",
            "@-",
            "--config",
            test_eol_conversion_config,
        ])
        .success();
    let before_file_contents = std::fs::read(test_env.env_root().join("before-file")).unwrap();
    let before_file_lines = before_file_contents
        .lines_with_terminator()
        .collect::<Vec<_>>();
    let after_file_contents = std::fs::read(test_env.env_root().join("after-file")).unwrap();
    let after_file_lines = after_file_contents
        .lines_with_terminator()
        .collect::<Vec<_>>();
    assert_eq!(before_file_lines[0], after_file_lines[0]);
    fn get_eol(line: &[u8]) -> &'static str {
        if line.ends_with(b"\r\n") {
            "\r\n"
        } else if line.ends_with(b"\n") {
            "\n"
        } else {
            ""
        }
    }
    let first_eol = get_eol(after_file_lines[0]);
    let second_eol = get_eol(after_file_lines[1]);
    assert_eq!(first_eol, second_eol);
    assert_eq!(
        first_eol, "\n",
        "The EOL the external diff editor receives must be LF to align with the builtin diff \
         editor."
    );
    let eol = first_eol;

    // With the previous diffedit command, file now contains the same content as
    // commit 1, i.e., 1 LF. We create another commit 3 with the 2-LF file, so that
    // the file shows up in the next diffedit command.
    work_dir.write_file(file_path, "\n\n");
    work_dir
        .run_jj(["squash", "--config", eol_conversion_none_config, "-m", "2"])
        .success();

    std::fs::write(&edit_script, format!("write file\n{eol}{eol}{eol}")).unwrap();
    work_dir
        .run_jj([
            "diffedit",
            "-r",
            "@-",
            "--config",
            test_eol_conversion_config,
        ])
        .success();

    work_dir
        .run_jj(["new", "root()", "--config", eol_conversion_none_config])
        .success();
    work_dir
        .run_jj([
            "new",
            "description(2)",
            "--config",
            eol_conversion_none_config,
        ])
        .success();
    let file_content = work_dir.read_file(file_path);
    assert_eq!(file_content, b"\n\n\n");
}