Debian Codegraph @

cli/tests/test_diffedit_command.rs

raw
1 // Copyright 2022 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use bstr::ByteSlice as _;
16 use indoc::indoc;
17 use testutils::TestResult;
18
19 use crate::common::TestEnvironment;
20
21 #[test]
22 fn test_diffedit() -> TestResult {
23 let mut test_env = TestEnvironment::default();
24 let edit_script = test_env.set_up_fake_diff_editor();
25 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
26 let work_dir = test_env.work_dir("repo");
27
28 work_dir.write_file("file1", "a\n");
29 work_dir.run_jj(["new"]).success();
30 work_dir.write_file("file2", "a\n");
31 work_dir.write_file("file3", "a\n");
32 work_dir.run_jj(["new"]).success();
33 work_dir.remove_file("file1");
34 work_dir.write_file("file2", "b\n");
35 work_dir.run_jj(["debug", "snapshot"]).success();
36 let setup_opid = work_dir.current_operation_id();
37
38 // Test the setup; nothing happens if we make no changes
39 std::fs::write(
40 &edit_script,
41 [
42 "files-before file1 file2",
43 "files-after JJ-INSTRUCTIONS file2",
44 "dump JJ-INSTRUCTIONS instrs",
45 ]
46 .join("\0"),
47 )?;
48 let output = work_dir.run_jj(["diffedit"]);
49 insta::assert_snapshot!(output, @"
50 ------- stderr -------
51 Nothing changed.
52 [EOF]
53 ");
54 insta::assert_snapshot!(
55 std::fs::read_to_string(test_env.env_root().join("instrs"))?, @"
56 You are editing changes in: kkmpptxz e4245972 (no description set)
57
58 The diff initially shows the commit's changes.
59
60 Adjust the right side until it shows the contents you want. If you
61 don't make any changes, then the operation will be aborted.
62 ");
63 let output = work_dir.run_jj(["diff", "-s"]);
64 insta::assert_snapshot!(output, @"
65 D file1
66 M file2
67 [EOF]
68 ");
69
70 // Try again with ui.diff-instructions=false
71 std::fs::write(&edit_script, "files-before file1 file2\0files-after file2")?;
72 let output = work_dir.run_jj(["diffedit", "--config=ui.diff-instructions=false"]);
73 insta::assert_snapshot!(output, @"
74 ------- stderr -------
75 Nothing changed.
76 [EOF]
77 ");
78 let output = work_dir.run_jj(["diff", "-s"]);
79 insta::assert_snapshot!(output, @"
80 D file1
81 M file2
82 [EOF]
83 ");
84
85 // Try again with --tool=<name>
86 std::fs::write(
87 &edit_script,
88 "files-before file1 file2\0files-after JJ-INSTRUCTIONS file2",
89 )?;
90 let output = work_dir.run_jj([
91 "diffedit",
92 "--config=ui.diff-editor='false'",
93 "--tool=fake-diff-editor",
94 ]);
95 insta::assert_snapshot!(output, @"
96 ------- stderr -------
97 Nothing changed.
98 [EOF]
99 ");
100 let output = work_dir.run_jj(["diff", "-s"]);
101 insta::assert_snapshot!(output, @"
102 D file1
103 M file2
104 [EOF]
105 ");
106
107 // Nothing happens if the diff-editor exits with an error
108 std::fs::write(&edit_script, "rm file2\0fail")?;
109 let output = work_dir.run_jj(["diffedit"]);
110 insta::assert_snapshot!(output.normalize_stderr_exit_status(), @"
111 ------- stderr -------
112 Error: Failed to edit diff
113 Caused by: Tool exited with exit status: 1 (run with --debug to see the exact invocation)
114 [EOF]
115 [exit status: 1]
116 ");
117 let output = work_dir.run_jj(["diff", "-s"]);
118 insta::assert_snapshot!(output, @"
119 D file1
120 M file2
121 [EOF]
122 ");
123
124 // Can edit changes to individual files
125 std::fs::write(&edit_script, "reset file2")?;
126 let output = work_dir.run_jj(["diffedit"]);
127 insta::assert_snapshot!(output, @"
128 ------- stderr -------
129 Working copy (@) now at: kkmpptxz 40ad4f80 (no description set)
130 Parent commit (@-) : rlvkpnrz 7e268da3 (no description set)
131 Added 0 files, modified 1 files, removed 0 files
132 [EOF]
133 ");
134 let output = work_dir.run_jj(["diff", "-s"]);
135 insta::assert_snapshot!(output, @"
136 D file1
137 [EOF]
138 ");
139
140 // Changes to a commit are propagated to descendants
141 work_dir.run_jj(["op", "restore", &setup_opid]).success();
142 std::fs::write(&edit_script, "write file3\nmodified\n")?;
143 let output = work_dir.run_jj(["diffedit", "-r", "@-"]);
144 insta::assert_snapshot!(output, @r"
145 ------- stderr -------
146 Rebased 1 descendant commits.
147 Working copy (@) now at: kkmpptxz 9f0ebae1 (no description set)
148 Parent commit (@-) : rlvkpnrz 72bcd8e9 (no description set)
149 Added 0 files, modified 1 files, removed 0 files
150 [EOF]
151 ");
152 let contents = work_dir.read_file("file3");
153 insta::assert_snapshot!(contents, @"modified");
154
155 // Test diffedit --from @--
156 work_dir.run_jj(["op", "restore", &setup_opid]).success();
157 std::fs::write(
158 &edit_script,
159 "files-before file1\0files-after JJ-INSTRUCTIONS file2 file3\0reset file2",
160 )?;
161 let output = work_dir.run_jj(["diffedit", "--from", "@--"]);
162 insta::assert_snapshot!(output, @"
163 ------- stderr -------
164 Working copy (@) now at: kkmpptxz 215fca5f (no description set)
165 Parent commit (@-) : rlvkpnrz 7e268da3 (no description set)
166 Added 0 files, modified 0 files, removed 1 files
167 [EOF]
168 ");
169 let output = work_dir.run_jj(["diff", "-s"]);
170 insta::assert_snapshot!(output, @"
171 D file1
172 D file2
173 [EOF]
174 ");
175
176 // Test with path restriction
177 work_dir.run_jj(["op", "restore", &setup_opid]).success();
178 work_dir.write_file("file3", "a\n");
179 work_dir.run_jj(["new"]).success();
180 work_dir.write_file("file1", "modified\n");
181 work_dir.write_file("file2", "modified\n");
182 work_dir.write_file("file3", "modified\n");
183
184 // Edit only file2 with path argument
185 std::fs::write(
186 &edit_script,
187 "files-before file2\0files-after JJ-INSTRUCTIONS file2\0reset file2",
188 )?;
189 let output = work_dir.run_jj(["diffedit", "file2"]);
190 insta::assert_snapshot!(output, @"
191 ------- stderr -------
192 Working copy (@) now at: tlkvzzqu 06bdff15 (no description set)
193 Parent commit (@-) : kkmpptxz e4245972 (no description set)
194 Added 0 files, modified 1 files, removed 0 files
195 [EOF]
196 ");
197 let output = work_dir.run_jj(["diff", "-s"]);
198 insta::assert_snapshot!(output, @"
199 C {file3 => file1}
200 M file3
201 [EOF]
202 ");
203
204 // Test reverse-order diffedit --to @-
205 work_dir.run_jj(["op", "restore", &setup_opid]).success();
206 std::fs::write(
207 &edit_script,
208 [
209 "files-before file2",
210 "files-after JJ-INSTRUCTIONS file1 file2",
211 "reset file2",
212 "dump JJ-INSTRUCTIONS instrs",
213 ]
214 .join("\0"),
215 )?;
216 let output = work_dir.run_jj(["diffedit", "--to", "@-"]);
217 insta::assert_snapshot!(output, @r"
218 ------- stderr -------
219 Rebased 1 descendant commits.
220 Working copy (@) now at: kkmpptxz 9a4e9bcc (no description set)
221 Parent commit (@-) : rlvkpnrz fb5c77f4 (no description set)
222 [EOF]
223 ");
224 insta::assert_snapshot!(
225 std::fs::read_to_string(test_env.env_root().join("instrs"))?, @"
226 You are editing changes in: rlvkpnrz 7e268da3 (no description set)
227
228 The diff initially shows the commit's changes relative to:
229 kkmpptxz e4245972 (no description set)
230
231 Adjust the right side until it shows the contents you want. If you
232 don't make any changes, then the operation will be aborted.
233 ");
234 let output = work_dir.run_jj(["diff", "-s"]);
235 insta::assert_snapshot!(output, @"
236 D file1
237 [EOF]
238 ");
239 Ok(())
240 }
241
242 #[test]
243 fn test_diffedit_file_by_file() -> TestResult {
244 let mut test_env = TestEnvironment::default();
245 let edit_script = test_env.set_up_fake_diff_editor();
246 test_env.add_config(r#"merge-tools.fake-diff-editor.edit-invocation-mode = "file-by-file""#);
247 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
248 let work_dir = test_env.work_dir("repo");
249
250 work_dir.write_file("file1", "a\n");
251 work_dir.write_file("file2", "a\n");
252 work_dir.run_jj(["new"]).success();
253 work_dir.remove_file("file1");
254 work_dir.write_file("file2", "b\n");
255 work_dir.write_file("file3", "c\n");
256 work_dir.run_jj(["debug", "snapshot"]).success();
257
258 // The editor should be invoked once per changed file, with file paths
259 // rather than directory paths. The fake editor prints the file basenames
260 // on each invocation, so we expect one stanza per changed file. Note that
261 // JJ-INSTRUCTIONS is *not* expected to appear: file-by-file mode iterates
262 // only the changed files, not the working-copy directory.
263 std::fs::write(
264 &edit_script,
265 [
266 "print ==",
267 "print-files-before",
268 "print --",
269 "print-files-after",
270 ]
271 .join("\0"),
272 )?;
273 let output = work_dir.run_jj(["diffedit"]);
274 insta::assert_snapshot!(output, @r"
275 ==
276 file1
277 --
278 file1
279 ==
280 file2
281 --
282 file2
283 ==
284 file3
285 --
286 file3
287 [EOF]
288 ------- stderr -------
289 Nothing changed.
290 [EOF]
291 ");
292
293 Ok(())
294 }
295
296 #[test]
297 fn test_diffedit_new_file() -> TestResult {
298 let mut test_env = TestEnvironment::default();
299 let edit_script = test_env.set_up_fake_diff_editor();
300 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
301 let work_dir = test_env.work_dir("repo");
302
303 work_dir.write_file("file1", "a\n");
304 work_dir.run_jj(["new"]).success();
305 work_dir.remove_file("file1");
306 work_dir.write_file("file2", "b\n");
307 work_dir.run_jj(["debug", "snapshot"]).success();
308 let setup_opid = work_dir.current_operation_id();
309
310 // Test the setup; nothing happens if we make no changes
311 std::fs::write(
312 &edit_script,
313 "files-before file1\0files-after JJ-INSTRUCTIONS file2",
314 )?;
315 let output = work_dir.run_jj(["diffedit"]);
316 insta::assert_snapshot!(output, @"
317 ------- stderr -------
318 Nothing changed.
319 [EOF]
320 ");
321 let output = work_dir.run_jj(["diff", "-s"]);
322 insta::assert_snapshot!(output, @"
323 D file1
324 A file2
325 [EOF]
326 ");
327
328 // Creating `file1` on the right side is noticed by `jj diffedit`
329 std::fs::write(&edit_script, "write file1\nmodified\n")?;
330 let output = work_dir.run_jj(["diffedit"]);
331 insta::assert_snapshot!(output, @"
332 ------- stderr -------
333 Working copy (@) now at: rlvkpnrz c26dcad1 (no description set)
334 Parent commit (@-) : qpvuntsm eb7b8a1f (no description set)
335 Added 1 files, modified 0 files, removed 0 files
336 [EOF]
337 ");
338 let output = work_dir.run_jj(["diff", "-s"]);
339 insta::assert_snapshot!(output, @"
340 M file1
341 A file2
342 [EOF]
343 ");
344
345 // Creating a file that wasn't on either side is ignored by diffedit.
346 // TODO(ilyagr) We should decide whether we like this behavior.
347 //
348 // On one hand, it is unexpected and potentially a minor BUG. On the other
349 // hand, this prevents `jj` from loading any backup files the merge tool
350 // generates.
351 work_dir.run_jj(["op", "restore", &setup_opid]).success();
352 std::fs::write(&edit_script, "write new_file\nnew file\n")?;
353 let output = work_dir.run_jj(["diffedit"]);
354 insta::assert_snapshot!(output, @"
355 ------- stderr -------
356 Nothing changed.
357 [EOF]
358 ");
359 let output = work_dir.run_jj(["diff", "-s"]);
360 insta::assert_snapshot!(output, @"
361 D file1
362 A file2
363 [EOF]
364 ");
365 Ok(())
366 }
367
368 #[test]
369 fn test_diffedit_existing_instructions() -> TestResult {
370 let mut test_env = TestEnvironment::default();
371 let edit_script = test_env.set_up_fake_diff_editor();
372 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
373 let work_dir = test_env.work_dir("repo");
374
375 // A diff containing an existing JJ-INSTRUCTIONS file themselves.
376 work_dir.write_file("JJ-INSTRUCTIONS", "instruct");
377
378 std::fs::write(&edit_script, "write JJ-INSTRUCTIONS\nmodified\n")?;
379 let output = work_dir.run_jj(["diffedit"]);
380 insta::assert_snapshot!(output, @"
381 ------- stderr -------
382 Working copy (@) now at: qpvuntsm e914aaad (no description set)
383 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
384 Added 0 files, modified 1 files, removed 0 files
385 [EOF]
386 ");
387 // Test that we didn't delete or overwrite the "JJ-INSTRUCTIONS" file.
388 let content = work_dir.read_file("JJ-INSTRUCTIONS");
389 insta::assert_snapshot!(content, @"modified");
390 Ok(())
391 }
392
393 #[test]
394 fn test_diffedit_external_tool_conflict_marker_style() -> TestResult {
395 let mut test_env = TestEnvironment::default();
396 let edit_script = test_env.set_up_fake_diff_editor();
397 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
398 let work_dir = test_env.work_dir("repo");
399 let file_path = "file";
400
401 // Create a conflict
402 work_dir.write_file(
403 file_path,
404 indoc! {"
405 line 1
406 line 2
407 line 3
408 line 4
409 line 5
410 "},
411 );
412 work_dir.run_jj(["commit", "-m", "base"]).success();
413 work_dir.write_file(
414 file_path,
415 indoc! {"
416 line 1
417 line 2.1
418 line 2.2
419 line 3
420 line 4.1
421 line 5
422 "},
423 );
424 work_dir.run_jj(["describe", "-m", "side-a"]).success();
425 work_dir
426 .run_jj(["new", "subject(base)", "-m", "side-b"])
427 .success();
428 work_dir.write_file(
429 file_path,
430 indoc! {"
431 line 1
432 line 2.3
433 line 3
434 line 4.2
435 line 4.3
436 line 5
437 "},
438 );
439
440 // Resolve one of the conflicts in the working copy
441 work_dir
442 .run_jj(["new", "subject(side-a)", "subject(side-b)"])
443 .success();
444 work_dir.write_file(
445 file_path,
446 indoc! {"
447 line 1
448 line 2.1
449 line 2.2
450 line 2.3
451 line 3
452 <<<<<<<
453 %%%%%%%
454 -line 4
455 +line 4.1
456 +++++++
457 line 4.2
458 line 4.3
459 >>>>>>>
460 line 5
461 "},
462 );
463
464 // Set up diff editor to use "snapshot" conflict markers
465 test_env.add_config(r#"merge-tools.fake-diff-editor.conflict-marker-style = "snapshot""#);
466
467 // We want to see whether the diff editor is using the correct conflict markers,
468 // and reset it to make sure that it parses the conflict markers as well
469 std::fs::write(
470 &edit_script,
471 [
472 "files-before file",
473 "files-after JJ-INSTRUCTIONS file",
474 "dump file after-file",
475 "reset file",
476 "dump file before-file",
477 ]
478 .join("\0"),
479 )?;
480 let output = work_dir.run_jj(["diffedit"]);
481 insta::assert_snapshot!(output, @r"
482 ------- stderr -------
483 Working copy (@) now at: mzvwutvl a2e4617e (conflict) (empty) (no description set)
484 Parent commit (@-) : rlvkpnrz 74e448a1 side-a
485 Parent commit (@-) : zsuskuln 6982bce7 side-b
486 Added 0 files, modified 1 files, removed 0 files
487 Warning: There are unresolved conflicts at these paths:
488 file 2-sided conflict
489 Existing conflicts were resolved or abandoned from 1 commits.
490 [EOF]
491 ");
492 // Conflicts should render using "snapshot" format in diff editor
493 insta::assert_snapshot!(
494 std::fs::read_to_string(test_env.env_root().join("before-file"))?, @r#"
495 line 1
496 <<<<<<< conflict 1 of 2
497 +++++++ rlvkpnrz 74e448a1 "side-a"
498 line 2.1
499 line 2.2
500 ------- qpvuntsm 9bd2e004 "base"
501 line 2
502 +++++++ zsuskuln 6982bce7 "side-b"
503 line 2.3
504 >>>>>>> conflict 1 of 2 ends
505 line 3
506 <<<<<<< conflict 2 of 2
507 +++++++ rlvkpnrz 74e448a1 "side-a"
508 line 4.1
509 ------- qpvuntsm 9bd2e004 "base"
510 line 4
511 +++++++ zsuskuln 6982bce7 "side-b"
512 line 4.2
513 line 4.3
514 >>>>>>> conflict 2 of 2 ends
515 line 5
516 "#);
517 insta::assert_snapshot!(
518 std::fs::read_to_string(test_env.env_root().join("after-file"))?, @r#"
519 line 1
520 line 2.1
521 line 2.2
522 line 2.3
523 line 3
524 <<<<<<< conflict 1 of 1
525 +++++++ rlvkpnrz 74e448a1 "side-a"
526 line 4.1
527 ------- qpvuntsm 9bd2e004 "base"
528 line 4
529 +++++++ zsuskuln 6982bce7 "side-b"
530 line 4.2
531 line 4.3
532 >>>>>>> conflict 1 of 1 ends
533 line 5
534 "#);
535 // Conflicts should be materialized using "diff" format in working copy
536 insta::assert_snapshot!(work_dir.read_file(file_path), @r#"
537 line 1
538 <<<<<<< conflict 1 of 2
539 +++++++ rlvkpnrz 74e448a1 "side-a"
540 line 2.1
541 line 2.2
542 %%%%%%% diff from: qpvuntsm 9bd2e004 "base"
543 \\\\\\\ to: zsuskuln 6982bce7 "side-b"
544 -line 2
545 +line 2.3
546 >>>>>>> conflict 1 of 2 ends
547 line 3
548 <<<<<<< conflict 2 of 2
549 %%%%%%% diff from: qpvuntsm 9bd2e004 "base"
550 \\\\\\\ to: rlvkpnrz 74e448a1 "side-a"
551 -line 4
552 +line 4.1
553 +++++++ zsuskuln 6982bce7 "side-b"
554 line 4.2
555 line 4.3
556 >>>>>>> conflict 2 of 2 ends
557 line 5
558 "#);
559
560 // File should be conflicted with no changes
561 let output = work_dir.run_jj(["st"]);
562 insta::assert_snapshot!(output, @"
563 The working copy has no changes.
564 Working copy (@) : mzvwutvl a2e4617e (conflict) (empty) (no description set)
565 Parent commit (@-): rlvkpnrz 74e448a1 side-a
566 Parent commit (@-): zsuskuln 6982bce7 side-b
567 Warning: There are unresolved conflicts at these paths:
568 file 2-sided conflict
569 [EOF]
570 ");
571 Ok(())
572 }
573
574 #[test]
575 fn test_diffedit_3pane() -> TestResult {
576 let mut test_env = TestEnvironment::default();
577 let edit_script = test_env.set_up_fake_diff_editor();
578 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
579 let work_dir = test_env.work_dir("repo");
580
581 work_dir.write_file("file1", "a\n");
582 work_dir.run_jj(["new"]).success();
583 work_dir.write_file("file2", "a\n");
584 work_dir.write_file("file3", "a\n");
585 work_dir.run_jj(["new"]).success();
586 work_dir.remove_file("file1");
587 work_dir.write_file("file2", "b\n");
588 work_dir.run_jj(["debug", "snapshot"]).success();
589 let setup_opid = work_dir.current_operation_id();
590
591 // 2 configs for a 3-pane setup. In the first, "$right" is passed to what the
592 // fake diff editor considers the "after" state.
593 let config_with_right_as_after =
594 "merge-tools.fake-diff-editor.edit-args=['$left', '$right', '--ignore=$output']";
595 let config_with_output_as_after =
596 "merge-tools.fake-diff-editor.edit-args=['$left', '$output', '--ignore=$right']";
597 std::fs::write(&edit_script, "")?;
598
599 // Nothing happens if we make no changes
600 std::fs::write(
601 &edit_script,
602 "files-before file1 file2\0files-after JJ-INSTRUCTIONS file2",
603 )?;
604 let output = work_dir.run_jj(["diffedit", "--config", config_with_output_as_after]);
605 insta::assert_snapshot!(output, @"
606 ------- stderr -------
607 Nothing changed.
608 [EOF]
609 ");
610 let output = work_dir.run_jj(["diff", "-s"]);
611 insta::assert_snapshot!(output, @"
612 D file1
613 M file2
614 [EOF]
615 ");
616 // Nothing happens if we make no changes, `config_with_right_as_after` version
617 let output = work_dir.run_jj(["diffedit", "--config", config_with_right_as_after]);
618 insta::assert_snapshot!(output, @"
619 ------- stderr -------
620 Nothing changed.
621 [EOF]
622 ");
623 let output = work_dir.run_jj(["diff", "-s"]);
624 insta::assert_snapshot!(output, @"
625 D file1
626 M file2
627 [EOF]
628 ");
629
630 // Can edit changes to individual files
631 std::fs::write(&edit_script, "reset file2")?;
632 let output = work_dir.run_jj(["diffedit", "--config", config_with_output_as_after]);
633 insta::assert_snapshot!(output, @"
634 ------- stderr -------
635 Working copy (@) now at: kkmpptxz 239413bd (no description set)
636 Parent commit (@-) : rlvkpnrz 7e268da3 (no description set)
637 Added 0 files, modified 1 files, removed 0 files
638 [EOF]
639 ");
640 let output = work_dir.run_jj(["diff", "-s"]);
641 insta::assert_snapshot!(output, @"
642 D file1
643 [EOF]
644 ");
645
646 // Can write something new to `file1`
647 work_dir.run_jj(["op", "restore", &setup_opid]).success();
648 std::fs::write(&edit_script, "write file1\nnew content")?;
649 let output = work_dir.run_jj(["diffedit", "--config", config_with_output_as_after]);
650 insta::assert_snapshot!(output, @"
651 ------- stderr -------
652 Working copy (@) now at: kkmpptxz 95873a91 (no description set)
653 Parent commit (@-) : rlvkpnrz 7e268da3 (no description set)
654 Added 1 files, modified 0 files, removed 0 files
655 [EOF]
656 ");
657 let output = work_dir.run_jj(["diff", "-s"]);
658 insta::assert_snapshot!(output, @"
659 M file1
660 M file2
661 [EOF]
662 ");
663
664 // But nothing happens if we modify the right side
665 work_dir.run_jj(["op", "restore", &setup_opid]).success();
666 std::fs::write(&edit_script, "write file1\nnew content")?;
667 let output = work_dir.run_jj(["diffedit", "--config", config_with_right_as_after]);
668 insta::assert_snapshot!(output, @"
669 ------- stderr -------
670 Nothing changed.
671 [EOF]
672 ");
673 let output = work_dir.run_jj(["diff", "-s"]);
674 insta::assert_snapshot!(output, @"
675 D file1
676 M file2
677 [EOF]
678 ");
679
680 // TODO: test with edit_script of "reset file2". This fails on right side
681 // since the file is readonly.
682 Ok(())
683 }
684
685 #[test]
686 fn test_diffedit_merge() -> TestResult {
687 let mut test_env = TestEnvironment::default();
688 let edit_script = test_env.set_up_fake_diff_editor();
689 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
690 let work_dir = test_env.work_dir("repo");
691
692 work_dir.write_file("file1", "a\n");
693 work_dir.write_file("file2", "a\n");
694 work_dir.run_jj(["new"]).success();
695 work_dir
696 .run_jj(["bookmark", "create", "-r@", "b"])
697 .success();
698 work_dir.write_file("file1", "b\n");
699 work_dir.write_file("file2", "b\n");
700 work_dir.run_jj(["new", "@-"]).success();
701 work_dir.run_jj(["new"]).success();
702 work_dir.write_file("file1", "c\n");
703 work_dir.write_file("file2", "c\n");
704 work_dir.run_jj(["new", "@", "b", "-m", "merge"]).success();
705 // Resolve the conflict in file1, but leave the conflict in file2
706 work_dir.write_file("file1", "d\n");
707 work_dir.write_file("file3", "d\n");
708 work_dir.run_jj(["new"]).success();
709 // Test the setup
710 let output = work_dir.run_jj(["diff", "-r", "@-", "-s"]);
711 insta::assert_snapshot!(output, @"
712 M file1
713 C {file1 => file3}
714 [EOF]
715 ");
716
717 // Remove file1. The conflict remains in the working copy on top of the merge.
718 std::fs::write(
719 edit_script,
720 "files-before file1\0files-after JJ-INSTRUCTIONS file1 file3\0rm file1",
721 )?;
722 let output = work_dir.run_jj(["diffedit", "-r", "@-"]);
723 insta::assert_snapshot!(output, @r"
724 ------- stderr -------
725 Rebased 1 descendant commits.
726 Working copy (@) now at: yqosqzyt 9fc53f3e (conflict) (empty) (no description set)
727 Parent commit (@-) : royxmykx 70ca98fe (conflict) merge
728 Added 0 files, modified 0 files, removed 1 files
729 Warning: There are unresolved conflicts at these paths:
730 file2 2-sided conflict
731 [EOF]
732 ");
733 let output = work_dir.run_jj(["diff", "-s", "-r", "@-"]);
734 insta::assert_snapshot!(output, @"
735 D file1
736 A file3
737 [EOF]
738 ");
739 assert!(!work_dir.root().join("file1").exists());
740 let output = work_dir.run_jj(["file", "show", "file2"]);
741 insta::assert_snapshot!(output, @r"
742 <<<<<<< conflict 1 of 1
743 %%%%%%% diff from: qpvuntsm fc6f5e82
744 \\\\\\\ to: mzvwutvl e3b18dc7
745 -a
746 +c
747 +++++++ rlvkpnrz 7027fb26
748 b
749 >>>>>>> conflict 1 of 1 ends
750 [EOF]
751 ");
752 Ok(())
753 }
754
755 #[test]
756 fn test_diffedit_old_restore_interactive_tests() -> TestResult {
757 let mut test_env = TestEnvironment::default();
758 let edit_script = test_env.set_up_fake_diff_editor();
759 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
760 let work_dir = test_env.work_dir("repo");
761
762 work_dir.write_file("file1", "a\n");
763 work_dir.write_file("file2", "a\n");
764 work_dir.run_jj(["new"]).success();
765 work_dir.remove_file("file1");
766 work_dir.write_file("file2", "b\n");
767 work_dir.write_file("file3", "b\n");
768 work_dir.run_jj(["debug", "snapshot"]).success();
769 let setup_opid = work_dir.current_operation_id();
770
771 // Nothing happens if we make no changes
772 let output = work_dir.run_jj(["diffedit", "--from", "@-"]);
773 insta::assert_snapshot!(output, @"
774 ------- stderr -------
775 Nothing changed.
776 [EOF]
777 ");
778 let output = work_dir.run_jj(["diff", "-s"]);
779 insta::assert_snapshot!(output, @"
780 D file1
781 M file2
782 C {file2 => file3}
783 [EOF]
784 ");
785
786 // Nothing happens if the diff-editor exits with an error
787 std::fs::write(&edit_script, "rm file2\0fail")?;
788 let output = work_dir.run_jj(["diffedit", "--from", "@-"]);
789 insta::assert_snapshot!(output.normalize_stderr_exit_status(), @"
790 ------- stderr -------
791 Error: Failed to edit diff
792 Caused by: Tool exited with exit status: 1 (run with --debug to see the exact invocation)
793 [EOF]
794 [exit status: 1]
795 ");
796 let output = work_dir.run_jj(["diff", "-s"]);
797 insta::assert_snapshot!(output, @"
798 D file1
799 M file2
800 C {file2 => file3}
801 [EOF]
802 ");
803
804 // Can restore changes to individual files
805 std::fs::write(&edit_script, "reset file2\0reset file3")?;
806 let output = work_dir.run_jj(["diffedit", "--from", "@-"]);
807 insta::assert_snapshot!(output, @"
808 ------- stderr -------
809 Working copy (@) now at: rlvkpnrz 83b62f75 (no description set)
810 Parent commit (@-) : qpvuntsm fc6f5e82 (no description set)
811 Added 0 files, modified 1 files, removed 1 files
812 [EOF]
813 ");
814 let output = work_dir.run_jj(["diff", "-s"]);
815 insta::assert_snapshot!(output, @"
816 D file1
817 [EOF]
818 ");
819
820 // Can make unrelated edits
821 work_dir.run_jj(["op", "restore", &setup_opid]).success();
822 std::fs::write(&edit_script, "write file3\nunrelated\n")?;
823 let output = work_dir.run_jj(["diffedit", "--from", "@-"]);
824 insta::assert_snapshot!(output, @"
825 ------- stderr -------
826 Working copy (@) now at: rlvkpnrz 8119c685 (no description set)
827 Parent commit (@-) : qpvuntsm fc6f5e82 (no description set)
828 Added 0 files, modified 1 files, removed 0 files
829 [EOF]
830 ");
831 let output = work_dir.run_jj(["diff", "--git"]);
832 insta::assert_snapshot!(output, @"
833 diff --git a/file1 b/file1
834 deleted file mode 100644
835 index 7898192261..0000000000
836 --- a/file1
837 +++ /dev/null
838 @@ -1,1 +0,0 @@
839 -a
840 diff --git a/file2 b/file2
841 index 7898192261..6178079822 100644
842 --- a/file2
843 +++ b/file2
844 @@ -1,1 +1,1 @@
845 -a
846 +b
847 diff --git a/file3 b/file3
848 new file mode 100644
849 index 0000000000..c21c9352f7
850 --- /dev/null
851 +++ b/file3
852 @@ -0,0 +1,1 @@
853 +unrelated
854 [EOF]
855 ");
856 Ok(())
857 }
858
859 #[test]
860 fn test_diffedit_restore_descendants() -> TestResult {
861 let mut test_env = TestEnvironment::default();
862 let edit_script = test_env.set_up_fake_diff_editor();
863 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
864 let work_dir = test_env.work_dir("repo");
865
866 work_dir.write_file("file", "println!(\"foo\")\n");
867 work_dir.run_jj(["new"]).success();
868 work_dir.write_file("file", "println!(\"bar\")\n");
869 work_dir.run_jj(["new"]).success();
870 work_dir.write_file("file", "println!(\"baz\");\n");
871
872 // Add a ";" after the line with "bar". There should be no conflict.
873 std::fs::write(edit_script, "write file\nprintln!(\"bar\");\n")?;
874 let output = work_dir.run_jj(["diffedit", "-r", "@-", "--restore-descendants"]);
875 insta::assert_snapshot!(output, @r"
876 ------- stderr -------
877 Rebased 1 descendant commits (while preserving their content).
878 Working copy (@) now at: kkmpptxz a35ef1a5 (no description set)
879 Parent commit (@-) : rlvkpnrz 2e949a84 (no description set)
880 [EOF]
881 ");
882 let output = work_dir.run_jj(["diff", "--git"]);
883 insta::assert_snapshot!(output, @r#"
884 diff --git a/file b/file
885 index 1a598a8fc9..7b6a85ab5a 100644
886 --- a/file
887 +++ b/file
888 @@ -1,1 +1,1 @@
889 -println!("bar");
890 +println!("baz");
891 [EOF]
892 "#);
893 Ok(())
894 }
895
896 #[test]
897 fn test_diffedit_external_tool_eol_conversion() -> TestResult {
898 // Create 2 changes: one creates a file with a single LF, another changes the
899 // file to contain 2 LFs. The diff editor should see the same EOL in both the
900 // before file and the after file. And when the diff editor adds another EOL to
901 // update, we should always see 3 LFs in the store.
902
903 let mut test_env = TestEnvironment::default();
904 let edit_script = test_env.set_up_fake_diff_editor();
905 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
906 let work_dir = test_env.work_dir("repo");
907 let file_path = "file";
908
909 // Use the none eol-conversion setting to check in as is.
910 let eol_conversion_none_config = "working-copy.eol-conversion='none'";
911 work_dir.write_file(file_path, "\n");
912 work_dir
913 .run_jj(["commit", "--config", eol_conversion_none_config, "-m", "1"])
914 .success();
915 work_dir.write_file(file_path, "\n\n");
916 work_dir
917 .run_jj(["commit", "--config", eol_conversion_none_config, "-m", "2"])
918 .success();
919
920 std::fs::write(
921 &edit_script,
922 [
923 "dump file after-file",
924 "reset file",
925 "dump file before-file",
926 ]
927 .join("\0"),
928 )?;
929 let test_eol_conversion_config = "working-copy.eol-conversion='input-output'";
930 work_dir
931 .run_jj([
932 "diffedit",
933 "-r",
934 "@-",
935 "--config",
936 test_eol_conversion_config,
937 ])
938 .success();
939 let before_file_contents = std::fs::read(test_env.env_root().join("before-file"))?;
940 let before_file_lines = before_file_contents
941 .lines_with_terminator()
942 .collect::<Vec<_>>();
943 let after_file_contents = std::fs::read(test_env.env_root().join("after-file"))?;
944 let after_file_lines = after_file_contents
945 .lines_with_terminator()
946 .collect::<Vec<_>>();
947 assert_eq!(before_file_lines[0], after_file_lines[0]);
948 fn get_eol(line: &[u8]) -> &'static str {
949 if line.ends_with(b"\r\n") {
950 "\r\n"
951 } else if line.ends_with(b"\n") {
952 "\n"
953 } else {
954 ""
955 }
956 }
957 let first_eol = get_eol(after_file_lines[0]);
958 let second_eol = get_eol(after_file_lines[1]);
959 assert_eq!(first_eol, second_eol);
960 assert_eq!(
961 first_eol, "\n",
962 "The EOL the external diff editor receives must be LF to align with the builtin diff \
963 editor."
964 );
965 let eol = first_eol;
966
967 // With the previous diffedit command, file now contains the same content as
968 // commit 1, i.e., 1 LF. We create another commit 3 with the 2-LF file, so that
969 // the file shows up in the next diffedit command.
970 work_dir.write_file(file_path, "\n\n");
971 work_dir
972 .run_jj(["squash", "--config", eol_conversion_none_config, "-m", "2"])
973 .success();
974
975 std::fs::write(&edit_script, format!("write file\n{eol}{eol}{eol}"))?;
976 work_dir
977 .run_jj([
978 "diffedit",
979 "-r",
980 "@-",
981 "--config",
982 test_eol_conversion_config,
983 ])
984 .success();
985
986 work_dir
987 .run_jj(["new", "root()", "--config", eol_conversion_none_config])
988 .success();
989 work_dir
990 .run_jj(["new", "subject(2)", "--config", eol_conversion_none_config])
991 .success();
992 let file_content = work_dir.read_file(file_path);
993 assert_eq!(file_content, b"\n\n\n");
994 Ok(())
995 }