Debian Codegraph @

cli/tests/test_diff_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 indoc::indoc;
16 use itertools::Itertools as _;
17 use testutils::TestResult;
18
19 use crate::common::CommandOutput;
20 use crate::common::TestEnvironment;
21 use crate::common::TestWorkDir;
22 use crate::common::create_commit;
23 use crate::common::create_commit_with_files;
24 use crate::common::fake_diff_editor_path;
25 use crate::common::to_toml_value;
26
27 fn strip_ansi_escape_codes(output: String) -> String {
28 anstream::adapter::strip_str(&output).to_string()
29 }
30
31 #[test]
32 fn test_diff_basic() {
33 let test_env = TestEnvironment::default();
34 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
35 let work_dir = test_env.work_dir("repo");
36
37 work_dir.write_file("file1", "foo\n");
38 work_dir.write_file("file2", "1\n2\n3\n4\n");
39 work_dir.run_jj(["new"]).success();
40 work_dir.remove_file("file1");
41 work_dir.write_file("file2", "1\n5\n3\n");
42 work_dir.write_file("file3", "foo\n");
43 work_dir.write_file("file4", "1\n2\n3\n4\n");
44
45 let output = work_dir.run_jj(["diff"]);
46 insta::assert_snapshot!(output, @"
47 Modified regular file file2:
48 1 1: 1
49 2 : 2
50 2: 5
51 3 3: 3
52 4 : 4
53 Modified regular file file3 (file1 => file3):
54 Modified regular file file4 (file2 => file4):
55 [EOF]
56 ");
57
58 let output = work_dir.run_jj(["diff", "--context=0"]);
59 insta::assert_snapshot!(output, @"
60 Modified regular file file2:
61 1 1: 1
62 2 : 2
63 2: 5
64 3 3: 3
65 4 : 4
66 Modified regular file file3 (file1 => file3):
67 Modified regular file file4 (file2 => file4):
68 [EOF]
69 ");
70
71 let output = work_dir.run_jj(["diff", "--color=debug"]);
72 insta::assert_snapshot!(output, @"
73 <<diff color_words header::Modified regular file file2:>>
74 <<diff color_words context removed line_number:: 1>><<diff color_words context:: >><<diff color_words context added line_number:: 1>><<diff color_words context::: 1>>
75 <<diff color_words removed line_number:: 2>><<diff color_words:: >><<diff color_words added line_number:: 2>><<diff color_words::: >><<diff color_words removed token::2>><<diff color_words added token::5>><<diff color_words::>>
76 <<diff color_words context removed line_number:: 3>><<diff color_words context:: >><<diff color_words context added line_number:: 3>><<diff color_words context::: 3>>
77 <<diff color_words removed line_number:: 4>><<diff color_words:: : >><<diff color_words removed token::4>>
78 <<diff color_words header::Modified regular file file3 (file1 => file3):>>
79 <<diff color_words header::Modified regular file file4 (file2 => file4):>>
80 [EOF]
81 ");
82
83 let output = work_dir.run_jj(["diff", "-s"]);
84 insta::assert_snapshot!(output, @"
85 M file2
86 R {file1 => file3}
87 C {file2 => file4}
88 [EOF]
89 ");
90
91 let output = work_dir.run_jj(["diff", "--types"]);
92 insta::assert_snapshot!(output, @"
93 FF file2
94 FF {file1 => file3}
95 FF {file2 => file4}
96 [EOF]
97 ");
98
99 let output = work_dir.run_jj(["diff", "--types", "glob:file[12]"]);
100 insta::assert_snapshot!(output, @"
101 F- file1
102 FF file2
103 [EOF]
104 ");
105
106 let template = r#"source.path() ++ ' => ' ++ target.path() ++ ' (' ++ status ++ ")\n""#;
107 let output = work_dir.run_jj(["diff", "-T", template]);
108 insta::assert_snapshot!(output, @"
109 file2 => file2 (modified)
110 file1 => file3 (renamed)
111 file2 => file4 (copied)
112 [EOF]
113 ");
114
115 let output = work_dir.run_jj(["diff", "--git", "file1"]);
116 insta::assert_snapshot!(output, @"
117 diff --git a/file1 b/file1
118 deleted file mode 100644
119 index 257cc5642c..0000000000
120 --- a/file1
121 +++ /dev/null
122 @@ -1,1 +0,0 @@
123 -foo
124 [EOF]
125 ");
126
127 let output = work_dir.run_jj(["diff", "--git"]);
128 insta::assert_snapshot!(output, @"
129 diff --git a/file2 b/file2
130 index 94ebaf9001..1ffc51b472 100644
131 --- a/file2
132 +++ b/file2
133 @@ -1,4 +1,3 @@
134 1
135 -2
136 +5
137 3
138 -4
139 diff --git a/file1 b/file3
140 rename from file1
141 rename to file3
142 diff --git a/file2 b/file4
143 copy from file2
144 copy to file4
145 [EOF]
146 ");
147
148 let output = work_dir.run_jj(["diff", "--git", "--context=0"]);
149 insta::assert_snapshot!(output, @"
150 diff --git a/file2 b/file2
151 index 94ebaf9001..1ffc51b472 100644
152 --- a/file2
153 +++ b/file2
154 @@ -2,1 +2,1 @@
155 -2
156 +5
157 @@ -4,1 +3,0 @@
158 -4
159 diff --git a/file1 b/file3
160 rename from file1
161 rename to file3
162 diff --git a/file2 b/file4
163 copy from file2
164 copy to file4
165 [EOF]
166 ");
167
168 let output = work_dir.run_jj(["diff", "--git", "--color=debug"]);
169 insta::assert_snapshot!(output, @"
170 <<diff git file_header::diff --git a/file2 b/file2>>
171 <<diff git file_header::index 94ebaf9001..1ffc51b472 100644>>
172 <<diff git file_header::--- a/file2>>
173 <<diff git file_header::+++ b/file2>>
174 <<diff git hunk_header::@@ -1,4 +1,3 @@>>
175 <<diff git context:: 1>>
176 <<diff git removed::->><<diff git removed token::2>><<diff git removed::>>
177 <<diff git added::+>><<diff git added token::5>><<diff git added::>>
178 <<diff git context:: 3>>
179 <<diff git removed::->><<diff git removed token::4>>
180 <<diff git file_header::diff --git a/file1 b/file3>>
181 <<diff git file_header::rename from file1>>
182 <<diff git file_header::rename to file3>>
183 <<diff git file_header::diff --git a/file2 b/file4>>
184 <<diff git file_header::copy from file2>>
185 <<diff git file_header::copy to file4>>
186 [EOF]
187 ");
188
189 let output = work_dir.run_jj(["diff", "-s", "--git"]);
190 insta::assert_snapshot!(output, @"
191 M file2
192 R {file1 => file3}
193 C {file2 => file4}
194 diff --git a/file2 b/file2
195 index 94ebaf9001..1ffc51b472 100644
196 --- a/file2
197 +++ b/file2
198 @@ -1,4 +1,3 @@
199 1
200 -2
201 +5
202 3
203 -4
204 diff --git a/file1 b/file3
205 rename from file1
206 rename to file3
207 diff --git a/file2 b/file4
208 copy from file2
209 copy to file4
210 [EOF]
211 ");
212
213 let output = work_dir.run_jj(["diff", "--git", "--config=diff.git.show-path-prefix=false"]);
214 insta::assert_snapshot!(output, @"
215 diff --git file2 file2
216 index 94ebaf9001..1ffc51b472 100644
217 --- file2
218 +++ file2
219 @@ -1,4 +1,3 @@
220 1
221 -2
222 +5
223 3
224 -4
225 diff --git file1 file3
226 rename from file1
227 rename to file3
228 diff --git file2 file4
229 copy from file2
230 copy to file4
231 [EOF]
232 ");
233
234 let output = work_dir.run_jj(["diff", "--stat"]);
235 insta::assert_snapshot!(output, @"
236 file2 | 3 +--
237 {file1 => file3} | 0
238 {file2 => file4} | 0
239 3 files changed, 1 insertion(+), 2 deletions(-)
240 [EOF]
241 ");
242
243 // Reverse-order diff
244 let output = work_dir.run_jj(["diff", "--to", "@-", "--git"]);
245 insta::assert_snapshot!(output, @"
246 diff --git a/file3 b/file1
247 rename from file3
248 rename to file1
249 diff --git a/file2 b/file2
250 index 1ffc51b472..94ebaf9001 100644
251 --- a/file2
252 +++ b/file2
253 @@ -1,3 +1,4 @@
254 1
255 -5
256 +2
257 3
258 +4
259 diff --git a/file4 b/file4
260 deleted file mode 100644
261 index 94ebaf9001..0000000000
262 --- a/file4
263 +++ /dev/null
264 @@ -1,4 +0,0 @@
265 -1
266 -2
267 -3
268 -4
269 [EOF]
270 ");
271
272 // Filter by glob pattern
273 let output = work_dir.run_jj(["diff", "-s", "glob:file[12]"]);
274 insta::assert_snapshot!(output, @"
275 D file1
276 M file2
277 [EOF]
278 ");
279
280 // Unmatched paths should generate warnings
281 let output = test_env.run_jj_in(
282 ".",
283 [
284 "diff",
285 "-Rrepo",
286 "-s",
287 "repo", // matches directory
288 "repo/file1", // deleted in to_tree, but exists in from_tree
289 "repo/x",
290 "repo/y/z",
291 ],
292 );
293 insta::assert_snapshot!(output.normalize_backslash(), @"
294 M repo/file2
295 R repo/{file1 => file3}
296 C repo/{file2 => file4}
297 [EOF]
298 ------- stderr -------
299 Warning: No matching entries for paths: repo/x, repo/y/z
300 [EOF]
301 ");
302
303 // Unmodified paths shouldn't generate warnings
304 let output = work_dir.run_jj(["diff", "-s", "--from=@", "file2"]);
305 insta::assert_snapshot!(output, @"");
306
307 // --tool=:<builtin>
308 let output = work_dir.run_jj(["diff", "--tool=:summary", "--color-words", "file2"]);
309 insta::assert_snapshot!(output, @"
310 M file2
311 Modified regular file file2:
312 1 1: 1
313 2 : 2
314 2: 5
315 3 3: 3
316 4 : 4
317 [EOF]
318 ");
319 let output = work_dir.run_jj(["diff", "--tool=:git", "--stat", "file2"]);
320 insta::assert_snapshot!(output, @"
321 file2 | 3 +--
322 1 file changed, 1 insertion(+), 2 deletions(-)
323 diff --git a/file2 b/file2
324 index 94ebaf9001..1ffc51b472 100644
325 --- a/file2
326 +++ b/file2
327 @@ -1,4 +1,3 @@
328 1
329 -2
330 +5
331 3
332 -4
333 [EOF]
334 ");
335
336 // Bad combination
337 let output = work_dir.run_jj(["diff", "--summary", "--tool=:stat"]);
338 insta::assert_snapshot!(output, @"
339 ------- stderr -------
340 Error: --tool=:stat cannot be used with --summary
341 [EOF]
342 [exit status: 2]
343 ");
344 let output = work_dir.run_jj(["diff", "--git", "--tool=:git"]);
345 insta::assert_snapshot!(output, @"
346 ------- stderr -------
347 Error: --tool=:git cannot be used with --git
348 [EOF]
349 [exit status: 2]
350 ");
351 let output = work_dir.run_jj(["diff", "--git", "--tool=external"]);
352 insta::assert_snapshot!(output, @"
353 ------- stderr -------
354 Error: --tool=external cannot be used with --git
355 [EOF]
356 [exit status: 2]
357 ");
358 let output = work_dir.run_jj(["diff", "-T''", "--summary"]);
359 insta::assert_snapshot!(output, @"
360 ------- stderr -------
361 error: the argument '--template <TEMPLATE>' cannot be used with:
362 --summary
363 --stat
364 --types
365 --name-only
366
367 Usage: jj diff --template <TEMPLATE> [FILESETS]...
368
369 For more information, try '--help'.
370 [EOF]
371 [exit status: 2]
372 ");
373 let output = work_dir.run_jj(["diff", "-T''", "--git"]);
374 insta::assert_snapshot!(output, @"
375 ------- stderr -------
376 error: the argument '--template <TEMPLATE>' cannot be used with:
377 --git
378 --color-words
379
380 Usage: jj diff --template <TEMPLATE> [FILESETS]...
381
382 For more information, try '--help'.
383 [EOF]
384 [exit status: 2]
385 ");
386 let output = work_dir.run_jj(["diff", "-T''", "--tool=:git"]);
387 insta::assert_snapshot!(output, @"
388 ------- stderr -------
389 error: the argument '--template <TEMPLATE>' cannot be used with '--tool <TOOL>'
390
391 Usage: jj diff --template <TEMPLATE> [FILESETS]...
392
393 For more information, try '--help'.
394 [EOF]
395 [exit status: 2]
396 ");
397
398 // Bad builtin format
399 let output = work_dir.run_jj(["diff", "--config=ui.diff-formatter=:unknown"]);
400 insta::assert_snapshot!(output, @"
401 ------- stderr -------
402 Config error: Invalid type or value for ui.diff-formatter
403 Caused by: Invalid builtin diff format: unknown
404 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
405 [EOF]
406 [exit status: 1]
407 ");
408 }
409
410 #[test]
411 fn test_diff_empty() {
412 let test_env = TestEnvironment::default();
413 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
414 let work_dir = test_env.work_dir("repo");
415
416 work_dir.write_file("file1", "");
417 let output = work_dir.run_jj(["diff"]);
418 insta::assert_snapshot!(output, @"
419 Added regular file file1:
420 (empty)
421 [EOF]
422 ");
423
424 work_dir.run_jj(["new"]).success();
425 work_dir.remove_file("file1");
426 let output = work_dir.run_jj(["diff"]);
427 insta::assert_snapshot!(output, @"
428 Removed regular file file1:
429 (empty)
430 [EOF]
431 ");
432
433 let output = work_dir.run_jj(["diff", "--stat"]);
434 insta::assert_snapshot!(output, @"
435 file1 | 0
436 1 file changed, 0 insertions(+), 0 deletions(-)
437 [EOF]
438 ");
439 }
440
441 #[test]
442 fn test_diff_file_mode() {
443 let test_env = TestEnvironment::default();
444 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
445 let work_dir = test_env.work_dir("repo");
446
447 // Test content+mode/mode-only changes of empty/non-empty files:
448 // - file1: ("", x) -> ("2", n) empty, content+mode
449 // - file2: ("1", x) -> ("1", n) non-empty, mode-only
450 // - file3: ("1", n) -> ("2", x) non-empty, content+mode
451 // - file4: ("", n) -> ("", x) empty, mode-only
452
453 work_dir.write_file("file1", "");
454 work_dir.write_file("file2", "1\n");
455 work_dir.write_file("file3", "1\n");
456 work_dir.write_file("file4", "");
457 work_dir
458 .run_jj(["file", "chmod", "x", "file1", "file2"])
459 .success();
460
461 work_dir.run_jj(["new"]).success();
462 work_dir.write_file("file1", "2\n");
463 work_dir.write_file("file3", "2\n");
464 work_dir
465 .run_jj(["file", "chmod", "n", "file1", "file2"])
466 .success();
467 work_dir
468 .run_jj(["file", "chmod", "x", "file3", "file4"])
469 .success();
470
471 work_dir.run_jj(["new"]).success();
472 work_dir.remove_file("file1");
473 work_dir.remove_file("file2");
474 work_dir.remove_file("file3");
475 work_dir.remove_file("file4");
476
477 let output = work_dir.run_jj(["diff", "-r@--"]);
478 insta::assert_snapshot!(output, @"
479 Added executable file file1:
480 (empty)
481 Added executable file file2:
482 1: 1
483 Added regular file file3:
484 1: 1
485 Added regular file file4:
486 (empty)
487 [EOF]
488 ");
489 let output = work_dir.run_jj(["diff", "-r@-"]);
490 insta::assert_snapshot!(output, @"
491 Executable file became non-executable at file1:
492 1: 2
493 Executable file became non-executable at file2:
494 Non-executable file became executable at file3:
495 1 : 1
496 1: 2
497 Non-executable file became executable at file4:
498 [EOF]
499 ");
500 let output = work_dir.run_jj(["diff", "-r@"]);
501 insta::assert_snapshot!(output, @"
502 Removed regular file file1:
503 1 : 2
504 Removed regular file file2:
505 1 : 1
506 Removed executable file file3:
507 1 : 2
508 Removed executable file file4:
509 (empty)
510 [EOF]
511 ");
512
513 let output = work_dir.run_jj(["diff", "-r@--", "--git"]);
514 insta::assert_snapshot!(output, @"
515 diff --git a/file1 b/file1
516 new file mode 100755
517 index 0000000000..e69de29bb2
518 diff --git a/file2 b/file2
519 new file mode 100755
520 index 0000000000..d00491fd7e
521 --- /dev/null
522 +++ b/file2
523 @@ -0,0 +1,1 @@
524 +1
525 diff --git a/file3 b/file3
526 new file mode 100644
527 index 0000000000..d00491fd7e
528 --- /dev/null
529 +++ b/file3
530 @@ -0,0 +1,1 @@
531 +1
532 diff --git a/file4 b/file4
533 new file mode 100644
534 index 0000000000..e69de29bb2
535 [EOF]
536 ");
537 let output = work_dir.run_jj(["diff", "-r@-", "--git"]);
538 insta::assert_snapshot!(output, @"
539 diff --git a/file1 b/file1
540 old mode 100755
541 new mode 100644
542 index e69de29bb2..0cfbf08886
543 --- a/file1
544 +++ b/file1
545 @@ -0,0 +1,1 @@
546 +2
547 diff --git a/file2 b/file2
548 old mode 100755
549 new mode 100644
550 diff --git a/file3 b/file3
551 old mode 100644
552 new mode 100755
553 index d00491fd7e..0cfbf08886
554 --- a/file3
555 +++ b/file3
556 @@ -1,1 +1,1 @@
557 -1
558 +2
559 diff --git a/file4 b/file4
560 old mode 100644
561 new mode 100755
562 [EOF]
563 ");
564 let output = work_dir.run_jj(["diff", "-r@", "--git"]);
565 insta::assert_snapshot!(output, @"
566 diff --git a/file1 b/file1
567 deleted file mode 100644
568 index 0cfbf08886..0000000000
569 --- a/file1
570 +++ /dev/null
571 @@ -1,1 +0,0 @@
572 -2
573 diff --git a/file2 b/file2
574 deleted file mode 100644
575 index d00491fd7e..0000000000
576 --- a/file2
577 +++ /dev/null
578 @@ -1,1 +0,0 @@
579 -1
580 diff --git a/file3 b/file3
581 deleted file mode 100755
582 index 0cfbf08886..0000000000
583 --- a/file3
584 +++ /dev/null
585 @@ -1,1 +0,0 @@
586 -2
587 diff --git a/file4 b/file4
588 deleted file mode 100755
589 index e69de29bb2..0000000000
590 [EOF]
591 ");
592 }
593
594 #[test]
595 fn test_diff_types() -> TestResult {
596 let test_env = TestEnvironment::default();
597 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
598 let work_dir = test_env.work_dir("repo");
599
600 let file_path = "foo";
601
602 // Missing
603 work_dir.run_jj(["new", "root()", "-m=missing"]).success();
604
605 // Normal file
606 work_dir.run_jj(["new", "root()", "-m=file"]).success();
607 work_dir.write_file(file_path, "foo");
608
609 // Conflict (add/add)
610 work_dir.run_jj(["new", "root()", "-m=conflict"]).success();
611 work_dir.write_file(file_path, "foo");
612 work_dir.run_jj(["new", "root()"]).success();
613 work_dir.write_file(file_path, "bar");
614 work_dir
615 .run_jj(["squash", "--into=subject(conflict)"])
616 .success();
617
618 #[cfg(unix)]
619 {
620 use std::os::unix::fs::PermissionsExt as _;
621 use std::path::PathBuf;
622
623 // Executable
624 work_dir
625 .run_jj(["new", "root()", "-m=executable"])
626 .success();
627 work_dir.write_file(file_path, "foo");
628 std::fs::set_permissions(
629 work_dir.root().join(file_path),
630 std::fs::Permissions::from_mode(0o755),
631 )?;
632
633 // Symlink
634 work_dir.run_jj(["new", "root()", "-m=symlink"]).success();
635 std::os::unix::fs::symlink(PathBuf::from("."), work_dir.root().join(file_path))?;
636 }
637
638 let diff = |from: &str, to: &str| {
639 work_dir.run_jj([
640 "diff",
641 "--types",
642 &format!(r#"--from=subject("{from}")"#),
643 &format!(r#"--to=subject("{to}")"#),
644 ])
645 };
646 insta::assert_snapshot!(diff("missing", "file"), @"
647 -F foo
648 [EOF]
649 ");
650 insta::assert_snapshot!(diff("file", "conflict"), @"
651 FC foo
652 [EOF]
653 ");
654 insta::assert_snapshot!(diff("conflict", "missing"), @"
655 C- foo
656 [EOF]
657 ");
658
659 #[cfg(unix)]
660 {
661 insta::assert_snapshot!(diff("symlink", "file"), @"
662 LF foo
663 [EOF]
664 ");
665 insta::assert_snapshot!(diff("missing", "executable"), @"
666 -F foo
667 [EOF]
668 ");
669 }
670 Ok(())
671 }
672
673 #[test]
674 fn test_diff_name_only() {
675 let test_env = TestEnvironment::default();
676 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
677 let work_dir = test_env.work_dir("repo");
678
679 work_dir.run_jj(["new"]).success();
680 work_dir.write_file("deleted", "d");
681 work_dir.write_file("modified", "m");
682 insta::assert_snapshot!(work_dir.run_jj(["diff", "--name-only"]), @"
683 deleted
684 modified
685 [EOF]
686 ");
687 work_dir.run_jj(["commit", "-mfirst"]).success();
688 work_dir.remove_file("deleted");
689 work_dir.write_file("modified", "mod");
690 work_dir.write_file("added", "add");
691 work_dir.create_dir("sub");
692 work_dir.write_file("sub/added", "sub/add");
693 insta::assert_snapshot!(work_dir.run_jj(["diff", "--name-only"]).normalize_backslash(), @"
694 added
695 deleted
696 modified
697 sub/added
698 [EOF]
699 ");
700 }
701
702 #[test]
703 fn test_diff_renamed_file_and_dir() {
704 let test_env = TestEnvironment::default();
705 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
706 let work_dir = test_env.work_dir("repo");
707
708 work_dir.write_file("x", "content1");
709 work_dir.create_dir("y");
710 work_dir.write_file("y/file", "content2");
711 work_dir.run_jj(["new"]).success();
712 work_dir.remove_file("x");
713 work_dir.remove_dir_all("y");
714 work_dir.create_dir("x");
715 work_dir.write_file("x/file", "content2");
716 work_dir.write_file("y", "content1");
717
718 // Renamed directory y->x shouldn't be reported
719 let output = work_dir.run_jj(["diff", "--summary"]);
720 insta::assert_snapshot!(output.normalize_backslash(), @"
721 R {y => x}/file
722 R {x => y}
723 [EOF]
724 ");
725
726 let output = work_dir.run_jj(["diff", "--git"]);
727 insta::assert_snapshot!(output, @"
728 diff --git a/y/file b/x/file
729 rename from y/file
730 rename to x/file
731 diff --git a/x b/y
732 rename from x
733 rename to y
734 [EOF]
735 ");
736 }
737
738 #[test]
739 fn test_diff_bad_args() {
740 let test_env = TestEnvironment::default();
741 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
742 let work_dir = test_env.work_dir("repo");
743
744 let output = work_dir.run_jj(["diff", "-s", "--types"]);
745 insta::assert_snapshot!(output, @"
746 ------- stderr -------
747 error: the argument '--summary' cannot be used with '--types'
748
749 Usage: jj diff --summary [FILESETS]...
750
751 For more information, try '--help'.
752 [EOF]
753 [exit status: 2]
754 ");
755
756 let output = work_dir.run_jj(["diff", "--color-words", "--git"]);
757 insta::assert_snapshot!(output, @"
758 ------- stderr -------
759 error: the argument '--color-words' cannot be used with '--git'
760
761 Usage: jj diff --color-words [FILESETS]...
762
763 For more information, try '--help'.
764 [EOF]
765 [exit status: 2]
766 ");
767 }
768
769 #[test]
770 fn test_diff_relative_paths() {
771 let test_env = TestEnvironment::default();
772 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
773 let work_dir = test_env.work_dir("repo");
774
775 work_dir.create_dir_all("dir1/subdir1");
776 work_dir.create_dir("dir2");
777 work_dir.write_file("file1", "foo1\n");
778 work_dir.write_file("dir1/file2", "foo2\n");
779 work_dir.write_file("dir1/subdir1/file3", "foo3\n");
780 work_dir.write_file("dir2/file4", "foo4\n");
781 work_dir.run_jj(["new"]).success();
782 work_dir.write_file("file1", "bar1\n");
783 work_dir.write_file("dir1/file2", "bar2\n");
784 work_dir.write_file("dir1/subdir1/file3", "bar3\n");
785 work_dir.write_file("dir2/file4", "bar4\n");
786
787 let sub_dir1 = work_dir.dir("dir1");
788 let output = sub_dir1.run_jj(["diff"]);
789 #[cfg(unix)]
790 insta::assert_snapshot!(output, @"
791 Modified regular file file2:
792 1 : foo2
793 1: bar2
794 Modified regular file subdir1/file3:
795 1 : foo3
796 1: bar3
797 Modified regular file ../dir2/file4:
798 1 : foo4
799 1: bar4
800 Modified regular file ../file1:
801 1 : foo1
802 1: bar1
803 [EOF]
804 ");
805 #[cfg(windows)]
806 insta::assert_snapshot!(output, @r"
807 Modified regular file file2:
808 1 : foo2
809 1: bar2
810 Modified regular file subdir1\file3:
811 1 : foo3
812 1: bar3
813 Modified regular file ..\dir2\file4:
814 1 : foo4
815 1: bar4
816 Modified regular file ..\file1:
817 1 : foo1
818 1: bar1
819 [EOF]
820 ");
821
822 let output = sub_dir1.run_jj(["diff", "-s"]);
823 #[cfg(unix)]
824 insta::assert_snapshot!(output, @"
825 M file2
826 M subdir1/file3
827 M ../dir2/file4
828 M ../file1
829 [EOF]
830 ");
831 #[cfg(windows)]
832 insta::assert_snapshot!(output, @r"
833 M file2
834 M subdir1\file3
835 M ..\dir2\file4
836 M ..\file1
837 [EOF]
838 ");
839
840 let output = sub_dir1.run_jj(["diff", "--types"]);
841 #[cfg(unix)]
842 insta::assert_snapshot!(output, @"
843 FF file2
844 FF subdir1/file3
845 FF ../dir2/file4
846 FF ../file1
847 [EOF]
848 ");
849 #[cfg(windows)]
850 insta::assert_snapshot!(output, @r"
851 FF file2
852 FF subdir1\file3
853 FF ..\dir2\file4
854 FF ..\file1
855 [EOF]
856 ");
857
858 let output = sub_dir1.run_jj(["diff", "--git"]);
859 insta::assert_snapshot!(output, @"
860 diff --git a/dir1/file2 b/dir1/file2
861 index 54b060eee9..1fe912cdd8 100644
862 --- a/dir1/file2
863 +++ b/dir1/file2
864 @@ -1,1 +1,1 @@
865 -foo2
866 +bar2
867 diff --git a/dir1/subdir1/file3 b/dir1/subdir1/file3
868 index c1ec6c6f12..f3c8b75ec6 100644
869 --- a/dir1/subdir1/file3
870 +++ b/dir1/subdir1/file3
871 @@ -1,1 +1,1 @@
872 -foo3
873 +bar3
874 diff --git a/dir2/file4 b/dir2/file4
875 index a0016dbc4c..17375f7a12 100644
876 --- a/dir2/file4
877 +++ b/dir2/file4
878 @@ -1,1 +1,1 @@
879 -foo4
880 +bar4
881 diff --git a/file1 b/file1
882 index 1715acd6a5..05c4fe6772 100644
883 --- a/file1
884 +++ b/file1
885 @@ -1,1 +1,1 @@
886 -foo1
887 +bar1
888 [EOF]
889 ");
890
891 let output = sub_dir1.run_jj(["diff", "--stat"]);
892 #[cfg(unix)]
893 insta::assert_snapshot!(output, @"
894 file2 | 2 +-
895 subdir1/file3 | 2 +-
896 ../dir2/file4 | 2 +-
897 ../file1 | 2 +-
898 4 files changed, 4 insertions(+), 4 deletions(-)
899 [EOF]
900 ");
901 #[cfg(windows)]
902 insta::assert_snapshot!(output, @r"
903 file2 | 2 +-
904 subdir1\file3 | 2 +-
905 ..\dir2\file4 | 2 +-
906 ..\file1 | 2 +-
907 4 files changed, 4 insertions(+), 4 deletions(-)
908 [EOF]
909 ");
910 }
911
912 #[test]
913 fn test_diff_hunks() {
914 let test_env = TestEnvironment::default();
915 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
916 let work_dir = test_env.work_dir("repo");
917
918 // Test added, removed, inserted, and modified lines. The modified line
919 // contains unchanged words.
920 work_dir.write_file("file1", "");
921 work_dir.write_file("file2", "foo\n");
922 work_dir.write_file("file3", "foo\nbaz qux blah blah\n");
923 work_dir.run_jj(["new"]).success();
924 work_dir.write_file("file1", "foo\n");
925 work_dir.write_file("file2", "");
926 work_dir.write_file("file3", "foo\nbar\nbaz quux blah blah\n");
927
928 let output = work_dir.run_jj(["diff"]);
929 insta::assert_snapshot!(output, @"
930 Modified regular file file1:
931 1: foo
932 Modified regular file file2:
933 1 : foo
934 Modified regular file file3:
935 1 1: foo
936 2 : baz qux blah blah
937 2: bar
938 3: baz quux blah blah
939 [EOF]
940 ");
941
942 let output = work_dir.run_jj(["diff", "--color=debug"]);
943 insta::assert_snapshot!(output, @"
944 <<diff color_words header::Modified regular file file1:>>
945 <<diff color_words:: >><<diff color_words added line_number:: 1>><<diff color_words::: >><<diff color_words added token::foo>>
946 <<diff color_words header::Modified regular file file2:>>
947 <<diff color_words removed line_number:: 1>><<diff color_words:: : >><<diff color_words removed token::foo>>
948 <<diff color_words header::Modified regular file file3:>>
949 <<diff color_words context removed line_number:: 1>><<diff color_words context:: >><<diff color_words context added line_number:: 1>><<diff color_words context::: foo>>
950 <<diff color_words:: >><<diff color_words added line_number:: 2>><<diff color_words::: >><<diff color_words added token::bar>>
951 <<diff color_words removed line_number:: 2>><<diff color_words:: >><<diff color_words added line_number:: 3>><<diff color_words::: baz >><<diff color_words removed token::qux>><<diff color_words added token::quux>><<diff color_words:: blah blah>>
952 [EOF]
953 ");
954
955 let output = work_dir.run_jj(["diff", "--git"]);
956 insta::assert_snapshot!(output, @"
957 diff --git a/file1 b/file1
958 index e69de29bb2..257cc5642c 100644
959 --- a/file1
960 +++ b/file1
961 @@ -0,0 +1,1 @@
962 +foo
963 diff --git a/file2 b/file2
964 index 257cc5642c..e69de29bb2 100644
965 --- a/file2
966 +++ b/file2
967 @@ -1,1 +0,0 @@
968 -foo
969 diff --git a/file3 b/file3
970 index 221a95a095..a543ef3892 100644
971 --- a/file3
972 +++ b/file3
973 @@ -1,2 +1,3 @@
974 foo
975 -baz qux blah blah
976 +bar
977 +baz quux blah blah
978 [EOF]
979 ");
980
981 let output = work_dir.run_jj(["diff", "--git", "--color=debug"]);
982 insta::assert_snapshot!(output, @"
983 <<diff git file_header::diff --git a/file1 b/file1>>
984 <<diff git file_header::index e69de29bb2..257cc5642c 100644>>
985 <<diff git file_header::--- a/file1>>
986 <<diff git file_header::+++ b/file1>>
987 <<diff git hunk_header::@@ -0,0 +1,1 @@>>
988 <<diff git added::+>><<diff git added token::foo>>
989 <<diff git file_header::diff --git a/file2 b/file2>>
990 <<diff git file_header::index 257cc5642c..e69de29bb2 100644>>
991 <<diff git file_header::--- a/file2>>
992 <<diff git file_header::+++ b/file2>>
993 <<diff git hunk_header::@@ -1,1 +0,0 @@>>
994 <<diff git removed::->><<diff git removed token::foo>>
995 <<diff git file_header::diff --git a/file3 b/file3>>
996 <<diff git file_header::index 221a95a095..a543ef3892 100644>>
997 <<diff git file_header::--- a/file3>>
998 <<diff git file_header::+++ b/file3>>
999 <<diff git hunk_header::@@ -1,2 +1,3 @@>>
1000 <<diff git context:: foo>>
1001 <<diff git removed::-baz >><<diff git removed token::qux>><<diff git removed:: blah blah>>
1002 <<diff git added::+>><<diff git added token::bar>>
1003 <<diff git added::+baz >><<diff git added token::quux>><<diff git added:: blah blah>>
1004 [EOF]
1005 ");
1006 }
1007
1008 #[test]
1009 fn test_diff_color_words_inlining_threshold() {
1010 let test_env = TestEnvironment::default();
1011 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1012 let work_dir = test_env.work_dir("repo");
1013
1014 let render_diff = |max_alternation: i32, args: &[&str]| {
1015 let config = format!("diff.color-words.max-inline-alternation={max_alternation}");
1016 work_dir.run_jj_with(|cmd| cmd.args(["diff", "--config", &config]).args(args))
1017 };
1018 let render_color_diff = |max_alternation| {
1019 render_diff(max_alternation, &["--color=always"])
1020 .normalize_stdout_with(strip_ansi_escape_codes)
1021 };
1022
1023 let file1_path = "file1-single-line";
1024 let file2_path = "file2-multiple-lines-in-single-hunk";
1025 let file3_path = "file3-changes-across-lines";
1026 work_dir.write_file(
1027 file1_path,
1028 indoc! {"
1029 == adds ==
1030 a b c
1031 == removes ==
1032 a b c d e f g
1033 == adds + removes ==
1034 a b c d e
1035 == adds + removes + adds ==
1036 a b c d e
1037 == adds + removes + adds + removes ==
1038 a b c d e f g
1039 "},
1040 );
1041 work_dir.write_file(
1042 file2_path,
1043 indoc! {"
1044 == adds; removes; adds + removes ==
1045 a b c
1046 a b c d e f g
1047 a b c d e
1048 == adds + removes + adds; adds + removes + adds + removes ==
1049 a b c d e
1050 a b c d e f g
1051 "},
1052 );
1053 work_dir.write_file(
1054 file3_path,
1055 indoc! {"
1056 == adds ==
1057 a b c
1058 == removes ==
1059 a b c d
1060 e f g
1061 == adds + removes ==
1062 a b c
1063 d e
1064 == adds + removes + adds ==
1065 a b c
1066 d e
1067 == adds + removes + adds + removes ==
1068 a b
1069 c d e f g
1070 "},
1071 );
1072 work_dir.run_jj(["new"]).success();
1073 work_dir.write_file(
1074 file1_path,
1075 indoc! {"
1076 == adds ==
1077 a X b Y Z c
1078 == removes ==
1079 a c f
1080 == adds + removes ==
1081 a X b d
1082 == adds + removes + adds ==
1083 a X b d Y
1084 == adds + removes + adds + removes ==
1085 X a Y b d Z e
1086 "},
1087 );
1088 work_dir.write_file(
1089 file2_path,
1090 indoc! {"
1091 == adds; removes; adds + removes ==
1092 a X b Y Z c
1093 a c f
1094 a X b d
1095 == adds + removes + adds; adds + removes + adds + removes ==
1096 a X b d Y
1097 X a Y b d Z e
1098 "},
1099 );
1100 work_dir.write_file(
1101 file3_path,
1102 indoc! {"
1103 == adds ==
1104 a X b
1105 Y Z c
1106 == removes ==
1107 a c f
1108 == adds + removes ==
1109 a
1110 X b d
1111 == adds + removes + adds ==
1112 a X b d
1113 Y
1114 == adds + removes + adds + removes ==
1115 X a Y b d
1116 Z e
1117 "},
1118 );
1119
1120 // default
1121 let output = work_dir
1122 .run_jj(["diff", "--color=always"])
1123 .normalize_stdout_with(strip_ansi_escape_codes);
1124 insta::assert_snapshot!(output, @"
1125 Modified regular file file1-single-line:
1126 1 1: == adds ==
1127 2 2: a X b Y Z c
1128 3 3: == removes ==
1129 4 4: a b c d e f g
1130 5 5: == adds + removes ==
1131 6 6: a X b c d e
1132 7 7: == adds + removes + adds ==
1133 8 8: a X b c d eY
1134 9 9: == adds + removes + adds + removes ==
1135 10 : a b c d e f g
1136 10: X a Y b d Z e
1137 Modified regular file file2-multiple-lines-in-single-hunk:
1138 1 1: == adds; removes; adds + removes ==
1139 2 2: a X b Y Z c
1140 3 3: a b c d e f g
1141 4 4: a X b c d e
1142 5 5: == adds + removes + adds; adds + removes + adds + removes ==
1143 6 : a b c d e
1144 7 : a b c d e f g
1145 6: a X b d Y
1146 7: X a Y b d Z e
1147 Modified regular file file3-changes-across-lines:
1148 1 1: == adds ==
1149 2 2: a X b
1150 2 3: Y Z c
1151 3 4: == removes ==
1152 4 5: a b c d
1153 5 5: e f g
1154 6 6: == adds + removes ==
1155 7 7: a
1156 7 8: X b c
1157 8 8: d e
1158 9 9: == adds + removes + adds ==
1159 10 10: a X b c
1160 11 10: d e
1161 11 11: Y
1162 12 12: == adds + removes + adds + removes ==
1163 13 : a b
1164 14 : c d e f g
1165 13: X a Y b d
1166 14: Z e
1167 [EOF]
1168 ");
1169
1170 // -1: inline all
1171 insta::assert_snapshot!(render_color_diff(-1), @"
1172 Modified regular file file1-single-line:
1173 1 1: == adds ==
1174 2 2: a X b Y Z c
1175 3 3: == removes ==
1176 4 4: a b c d e f g
1177 5 5: == adds + removes ==
1178 6 6: a X b c d e
1179 7 7: == adds + removes + adds ==
1180 8 8: a X b c d eY
1181 9 9: == adds + removes + adds + removes ==
1182 10 10: X a Y b c d Z e f g
1183 Modified regular file file2-multiple-lines-in-single-hunk:
1184 1 1: == adds; removes; adds + removes ==
1185 2 2: a X b Y Z c
1186 3 3: a b c d e f g
1187 4 4: a X b c d e
1188 5 5: == adds + removes + adds; adds + removes + adds + removes ==
1189 6 6: a X b c d eY
1190 7 7: X a Y b c d Z e f g
1191 Modified regular file file3-changes-across-lines:
1192 1 1: == adds ==
1193 2 2: a X b
1194 2 3: Y Z c
1195 3 4: == removes ==
1196 4 5: a b c d
1197 5 5: e f g
1198 6 6: == adds + removes ==
1199 7 7: a
1200 7 8: X b c
1201 8 8: d e
1202 9 9: == adds + removes + adds ==
1203 10 10: a X b c
1204 11 10: d e
1205 11 11: Y
1206 12 12: == adds + removes + adds + removes ==
1207 13 13: X a Y b
1208 14 13: c d
1209 14 14: Z e f g
1210 [EOF]
1211 ");
1212
1213 // 0: no inlining
1214 insta::assert_snapshot!(render_color_diff(0), @"
1215 Modified regular file file1-single-line:
1216 1 1: == adds ==
1217 2 : a b c
1218 2: a X b Y Z c
1219 3 3: == removes ==
1220 4 : a b c d e f g
1221 4: a c f
1222 5 5: == adds + removes ==
1223 6 : a b c d e
1224 6: a X b d
1225 7 7: == adds + removes + adds ==
1226 8 : a b c d e
1227 8: a X b d Y
1228 9 9: == adds + removes + adds + removes ==
1229 10 : a b c d e f g
1230 10: X a Y b d Z e
1231 Modified regular file file2-multiple-lines-in-single-hunk:
1232 1 1: == adds; removes; adds + removes ==
1233 2 : a b c
1234 3 : a b c d e f g
1235 4 : a b c d e
1236 2: a X b Y Z c
1237 3: a c f
1238 4: a X b d
1239 5 5: == adds + removes + adds; adds + removes + adds + removes ==
1240 6 : a b c d e
1241 7 : a b c d e f g
1242 6: a X b d Y
1243 7: X a Y b d Z e
1244 Modified regular file file3-changes-across-lines:
1245 1 1: == adds ==
1246 2 : a b c
1247 2: a X b
1248 3: Y Z c
1249 3 4: == removes ==
1250 4 : a b c d
1251 5 : e f g
1252 5: a c f
1253 6 6: == adds + removes ==
1254 7 : a b c
1255 8 : d e
1256 7: a
1257 8: X b d
1258 9 9: == adds + removes + adds ==
1259 10 : a b c
1260 11 : d e
1261 10: a X b d
1262 11: Y
1263 12 12: == adds + removes + adds + removes ==
1264 13 : a b
1265 14 : c d e f g
1266 13: X a Y b d
1267 14: Z e
1268 [EOF]
1269 ");
1270
1271 // 1: inline adds-only or removes-only lines
1272 insta::assert_snapshot!(render_color_diff(1), @"
1273 Modified regular file file1-single-line:
1274 1 1: == adds ==
1275 2 2: a X b Y Z c
1276 3 3: == removes ==
1277 4 4: a b c d e f g
1278 5 5: == adds + removes ==
1279 6 : a b c d e
1280 6: a X b d
1281 7 7: == adds + removes + adds ==
1282 8 : a b c d e
1283 8: a X b d Y
1284 9 9: == adds + removes + adds + removes ==
1285 10 : a b c d e f g
1286 10: X a Y b d Z e
1287 Modified regular file file2-multiple-lines-in-single-hunk:
1288 1 1: == adds; removes; adds + removes ==
1289 2 : a b c
1290 3 : a b c d e f g
1291 4 : a b c d e
1292 2: a X b Y Z c
1293 3: a c f
1294 4: a X b d
1295 5 5: == adds + removes + adds; adds + removes + adds + removes ==
1296 6 : a b c d e
1297 7 : a b c d e f g
1298 6: a X b d Y
1299 7: X a Y b d Z e
1300 Modified regular file file3-changes-across-lines:
1301 1 1: == adds ==
1302 2 2: a X b
1303 2 3: Y Z c
1304 3 4: == removes ==
1305 4 5: a b c d
1306 5 5: e f g
1307 6 6: == adds + removes ==
1308 7 : a b c
1309 8 : d e
1310 7: a
1311 8: X b d
1312 9 9: == adds + removes + adds ==
1313 10 : a b c
1314 11 : d e
1315 10: a X b d
1316 11: Y
1317 12 12: == adds + removes + adds + removes ==
1318 13 : a b
1319 14 : c d e f g
1320 13: X a Y b d
1321 14: Z e
1322 [EOF]
1323 ");
1324
1325 // 2: inline up to adds + removes lines
1326 insta::assert_snapshot!(render_color_diff(2), @"
1327 Modified regular file file1-single-line:
1328 1 1: == adds ==
1329 2 2: a X b Y Z c
1330 3 3: == removes ==
1331 4 4: a b c d e f g
1332 5 5: == adds + removes ==
1333 6 6: a X b c d e
1334 7 7: == adds + removes + adds ==
1335 8 : a b c d e
1336 8: a X b d Y
1337 9 9: == adds + removes + adds + removes ==
1338 10 : a b c d e f g
1339 10: X a Y b d Z e
1340 Modified regular file file2-multiple-lines-in-single-hunk:
1341 1 1: == adds; removes; adds + removes ==
1342 2 2: a X b Y Z c
1343 3 3: a b c d e f g
1344 4 4: a X b c d e
1345 5 5: == adds + removes + adds; adds + removes + adds + removes ==
1346 6 : a b c d e
1347 7 : a b c d e f g
1348 6: a X b d Y
1349 7: X a Y b d Z e
1350 Modified regular file file3-changes-across-lines:
1351 1 1: == adds ==
1352 2 2: a X b
1353 2 3: Y Z c
1354 3 4: == removes ==
1355 4 5: a b c d
1356 5 5: e f g
1357 6 6: == adds + removes ==
1358 7 7: a
1359 7 8: X b c
1360 8 8: d e
1361 9 9: == adds + removes + adds ==
1362 10 : a b c
1363 11 : d e
1364 10: a X b d
1365 11: Y
1366 12 12: == adds + removes + adds + removes ==
1367 13 : a b
1368 14 : c d e f g
1369 13: X a Y b d
1370 14: Z e
1371 [EOF]
1372 ");
1373
1374 // 3: inline up to adds + removes + adds lines
1375 insta::assert_snapshot!(render_color_diff(3), @"
1376 Modified regular file file1-single-line:
1377 1 1: == adds ==
1378 2 2: a X b Y Z c
1379 3 3: == removes ==
1380 4 4: a b c d e f g
1381 5 5: == adds + removes ==
1382 6 6: a X b c d e
1383 7 7: == adds + removes + adds ==
1384 8 8: a X b c d eY
1385 9 9: == adds + removes + adds + removes ==
1386 10 : a b c d e f g
1387 10: X a Y b d Z e
1388 Modified regular file file2-multiple-lines-in-single-hunk:
1389 1 1: == adds; removes; adds + removes ==
1390 2 2: a X b Y Z c
1391 3 3: a b c d e f g
1392 4 4: a X b c d e
1393 5 5: == adds + removes + adds; adds + removes + adds + removes ==
1394 6 : a b c d e
1395 7 : a b c d e f g
1396 6: a X b d Y
1397 7: X a Y b d Z e
1398 Modified regular file file3-changes-across-lines:
1399 1 1: == adds ==
1400 2 2: a X b
1401 2 3: Y Z c
1402 3 4: == removes ==
1403 4 5: a b c d
1404 5 5: e f g
1405 6 6: == adds + removes ==
1406 7 7: a
1407 7 8: X b c
1408 8 8: d e
1409 9 9: == adds + removes + adds ==
1410 10 10: a X b c
1411 11 10: d e
1412 11 11: Y
1413 12 12: == adds + removes + adds + removes ==
1414 13 : a b
1415 14 : c d e f g
1416 13: X a Y b d
1417 14: Z e
1418 [EOF]
1419 ");
1420
1421 // 4: inline up to adds + removes + adds + removes lines
1422 insta::assert_snapshot!(render_color_diff(4), @"
1423 Modified regular file file1-single-line:
1424 1 1: == adds ==
1425 2 2: a X b Y Z c
1426 3 3: == removes ==
1427 4 4: a b c d e f g
1428 5 5: == adds + removes ==
1429 6 6: a X b c d e
1430 7 7: == adds + removes + adds ==
1431 8 8: a X b c d eY
1432 9 9: == adds + removes + adds + removes ==
1433 10 10: X a Y b c d Z e f g
1434 Modified regular file file2-multiple-lines-in-single-hunk:
1435 1 1: == adds; removes; adds + removes ==
1436 2 2: a X b Y Z c
1437 3 3: a b c d e f g
1438 4 4: a X b c d e
1439 5 5: == adds + removes + adds; adds + removes + adds + removes ==
1440 6 6: a X b c d eY
1441 7 7: X a Y b c d Z e f g
1442 Modified regular file file3-changes-across-lines:
1443 1 1: == adds ==
1444 2 2: a X b
1445 2 3: Y Z c
1446 3 4: == removes ==
1447 4 5: a b c d
1448 5 5: e f g
1449 6 6: == adds + removes ==
1450 7 7: a
1451 7 8: X b c
1452 8 8: d e
1453 9 9: == adds + removes + adds ==
1454 10 10: a X b c
1455 11 10: d e
1456 11 11: Y
1457 12 12: == adds + removes + adds + removes ==
1458 13 13: X a Y b
1459 14 13: c d
1460 14 14: Z e f g
1461 [EOF]
1462 ");
1463
1464 // context words in added/removed lines should be labeled as such
1465 insta::assert_snapshot!(render_diff(2, &["--color=always"]), @"
1466 Modified regular file file1-single-line:
1467  1  1: == adds ==
1468  2  2: a X b Y Z c
1469  3  3: == removes ==
1470  4  4: a b c d e f g
1471  5  5: == adds + removes ==
1472  6  6: a X b c d e
1473  7  7: == adds + removes + adds ==
1474  8 : a b c d e
1475  8: a X b d Y
1476  9  9: == adds + removes + adds + removes ==
1477  10 : a b c d e f g
1478  10: X a Y b d Z e
1479 Modified regular file file2-multiple-lines-in-single-hunk:
1480  1  1: == adds; removes; adds + removes ==
1481  2  2: a X b Y Z c
1482  3  3: a b c d e f g
1483  4  4: a X b c d e
1484  5  5: == adds + removes + adds; adds + removes + adds + removes ==
1485  6 : a b c d e
1486  7 : a b c d e f g
1487  6: a X b d Y
1488  7: X a Y b d Z e
1489 Modified regular file file3-changes-across-lines:
1490  1  1: == adds ==
1491  2  2: a X b
1492  2  3: Y Z c
1493  3  4: == removes ==
1494  4  5: a b c d
1495  5  5: e f g
1496  6  6: == adds + removes ==
1497  7  7: a
1498  7  8: X b c
1499  8  8: d e
1500  9  9: == adds + removes + adds ==
1501  10 : a b c
1502  11 : d e
1503  10: a X b d
1504  11: Y
1505  12  12: == adds + removes + adds + removes ==
1506  13 : a b
1507  14 : c d e f g
1508  13: X a Y b d
1509  14: Z e
1510 [EOF]
1511 ");
1512 insta::assert_snapshot!(render_diff(2, &["--color=debug"]), @"
1513 <<diff color_words header::Modified regular file file1-single-line:>>
1514 <<diff color_words context removed line_number:: 1>><<diff color_words context:: >><<diff color_words context added line_number:: 1>><<diff color_words context::: == adds ==>>
1515 <<diff color_words removed line_number:: 2>><<diff color_words:: >><<diff color_words added line_number:: 2>><<diff color_words::: a >><<diff color_words added token::X >><<diff color_words::b >><<diff color_words added token::Y Z >><<diff color_words::c>>
1516 <<diff color_words context removed line_number:: 3>><<diff color_words context:: >><<diff color_words context added line_number:: 3>><<diff color_words context::: == removes ==>>
1517 <<diff color_words removed line_number:: 4>><<diff color_words:: >><<diff color_words added line_number:: 4>><<diff color_words::: a >><<diff color_words removed token::b >><<diff color_words::c >><<diff color_words removed token::d e >><<diff color_words::f>><<diff color_words removed token:: g>><<diff color_words::>>
1518 <<diff color_words context removed line_number:: 5>><<diff color_words context:: >><<diff color_words context added line_number:: 5>><<diff color_words context::: == adds + removes ==>>
1519 <<diff color_words removed line_number:: 6>><<diff color_words:: >><<diff color_words added line_number:: 6>><<diff color_words::: a >><<diff color_words added token::X >><<diff color_words::b >><<diff color_words removed token::c >><<diff color_words::d>><<diff color_words removed token:: e>><<diff color_words::>>
1520 <<diff color_words context removed line_number:: 7>><<diff color_words context:: >><<diff color_words context added line_number:: 7>><<diff color_words context::: == adds + removes + adds ==>>
1521 <<diff color_words removed line_number:: 8>><<diff color_words:: : >><<diff color_words removed::a b >><<diff color_words removed token::c >><<diff color_words removed::d >><<diff color_words removed token::e>><<diff color_words removed::>>
1522 <<diff color_words:: >><<diff color_words added line_number:: 8>><<diff color_words::: >><<diff color_words added::a >><<diff color_words added token::X >><<diff color_words added::b d >><<diff color_words added token::Y>><<diff color_words added::>>
1523 <<diff color_words context removed line_number:: 9>><<diff color_words context:: >><<diff color_words context added line_number:: 9>><<diff color_words context::: == adds + removes + adds + removes ==>>
1524 <<diff color_words removed line_number:: 10>><<diff color_words:: : >><<diff color_words removed::a b >><<diff color_words removed token::c >><<diff color_words removed::d e>><<diff color_words removed token:: f g>><<diff color_words removed::>>
1525 <<diff color_words:: >><<diff color_words added line_number:: 10>><<diff color_words::: >><<diff color_words added token::X >><<diff color_words added::a >><<diff color_words added token::Y >><<diff color_words added::b d >><<diff color_words added token::Z >><<diff color_words added::e>>
1526 <<diff color_words header::Modified regular file file2-multiple-lines-in-single-hunk:>>
1527 <<diff color_words context removed line_number:: 1>><<diff color_words context:: >><<diff color_words context added line_number:: 1>><<diff color_words context::: == adds; removes; adds + removes ==>>
1528 <<diff color_words removed line_number:: 2>><<diff color_words:: >><<diff color_words added line_number:: 2>><<diff color_words::: a >><<diff color_words added token::X >><<diff color_words::b >><<diff color_words added token::Y Z >><<diff color_words::c>>
1529 <<diff color_words removed line_number:: 3>><<diff color_words:: >><<diff color_words added line_number:: 3>><<diff color_words::: a >><<diff color_words removed token::b >><<diff color_words::c >><<diff color_words removed token::d e >><<diff color_words::f>><<diff color_words removed token:: g>><<diff color_words::>>
1530 <<diff color_words removed line_number:: 4>><<diff color_words:: >><<diff color_words added line_number:: 4>><<diff color_words::: a >><<diff color_words added token::X >><<diff color_words::b >><<diff color_words removed token::c >><<diff color_words::d>><<diff color_words removed token:: e>><<diff color_words::>>
1531 <<diff color_words context removed line_number:: 5>><<diff color_words context:: >><<diff color_words context added line_number:: 5>><<diff color_words context::: == adds + removes + adds; adds + removes + adds + removes ==>>
1532 <<diff color_words removed line_number:: 6>><<diff color_words:: : >><<diff color_words removed::a b >><<diff color_words removed token::c >><<diff color_words removed::d >><<diff color_words removed token::e>><<diff color_words removed::>>
1533 <<diff color_words removed line_number:: 7>><<diff color_words:: : >><<diff color_words removed::a b >><<diff color_words removed token::c >><<diff color_words removed::d e>><<diff color_words removed token:: f g>><<diff color_words removed::>>
1534 <<diff color_words:: >><<diff color_words added line_number:: 6>><<diff color_words::: >><<diff color_words added::a >><<diff color_words added token::X >><<diff color_words added::b d >><<diff color_words added token::Y>><<diff color_words added::>>
1535 <<diff color_words:: >><<diff color_words added line_number:: 7>><<diff color_words::: >><<diff color_words added token::X >><<diff color_words added::a >><<diff color_words added token::Y >><<diff color_words added::b d >><<diff color_words added token::Z >><<diff color_words added::e>>
1536 <<diff color_words header::Modified regular file file3-changes-across-lines:>>
1537 <<diff color_words context removed line_number:: 1>><<diff color_words context:: >><<diff color_words context added line_number:: 1>><<diff color_words context::: == adds ==>>
1538 <<diff color_words removed line_number:: 2>><<diff color_words:: >><<diff color_words added line_number:: 2>><<diff color_words::: a >><<diff color_words added token::X >><<diff color_words::b>><<diff color_words added token::>>
1539 <<diff color_words removed line_number:: 2>><<diff color_words:: >><<diff color_words added line_number:: 3>><<diff color_words::: >><<diff color_words added token::Y Z>><<diff color_words:: c>>
1540 <<diff color_words context removed line_number:: 3>><<diff color_words context:: >><<diff color_words context added line_number:: 4>><<diff color_words context::: == removes ==>>
1541 <<diff color_words removed line_number:: 4>><<diff color_words:: >><<diff color_words added line_number:: 5>><<diff color_words::: a >><<diff color_words removed token::b >><<diff color_words::c >><<diff color_words removed token::d>>
1542 <<diff color_words removed line_number:: 5>><<diff color_words:: >><<diff color_words added line_number:: 5>><<diff color_words::: >><<diff color_words removed token::e >><<diff color_words::f>><<diff color_words removed token:: g>><<diff color_words::>>
1543 <<diff color_words context removed line_number:: 6>><<diff color_words context:: >><<diff color_words context added line_number:: 6>><<diff color_words context::: == adds + removes ==>>
1544 <<diff color_words removed line_number:: 7>><<diff color_words:: >><<diff color_words added line_number:: 7>><<diff color_words::: a>><<diff color_words added token::>>
1545 <<diff color_words removed line_number:: 7>><<diff color_words:: >><<diff color_words added line_number:: 8>><<diff color_words::: >><<diff color_words added token::X>><<diff color_words:: b >><<diff color_words removed token::c>>
1546 <<diff color_words removed line_number:: 8>><<diff color_words:: >><<diff color_words added line_number:: 8>><<diff color_words::: d>><<diff color_words removed token:: e>><<diff color_words::>>
1547 <<diff color_words context removed line_number:: 9>><<diff color_words context:: >><<diff color_words context added line_number:: 9>><<diff color_words context::: == adds + removes + adds ==>>
1548 <<diff color_words removed line_number:: 10>><<diff color_words:: : >><<diff color_words removed::a b >><<diff color_words removed token::c>>
1549 <<diff color_words removed line_number:: 11>><<diff color_words:: : >><<diff color_words removed::d>><<diff color_words removed token:: e>><<diff color_words removed::>>
1550 <<diff color_words:: >><<diff color_words added line_number:: 10>><<diff color_words::: >><<diff color_words added::a >><<diff color_words added token::X >><<diff color_words added::b d>><<diff color_words added token::>>
1551 <<diff color_words:: >><<diff color_words added line_number:: 11>><<diff color_words::: >><<diff color_words added token::Y>><<diff color_words added::>>
1552 <<diff color_words context removed line_number:: 12>><<diff color_words context:: >><<diff color_words context added line_number:: 12>><<diff color_words context::: == adds + removes + adds + removes ==>>
1553 <<diff color_words removed line_number:: 13>><<diff color_words:: : >><<diff color_words removed::a b>><<diff color_words removed token::>>
1554 <<diff color_words removed line_number:: 14>><<diff color_words:: : >><<diff color_words removed token::c>><<diff color_words removed:: d e>><<diff color_words removed token:: f g>><<diff color_words removed::>>
1555 <<diff color_words:: >><<diff color_words added line_number:: 13>><<diff color_words::: >><<diff color_words added token::X >><<diff color_words added::a >><<diff color_words added token::Y >><<diff color_words added::b d>><<diff color_words added token::>>
1556 <<diff color_words:: >><<diff color_words added line_number:: 14>><<diff color_words::: >><<diff color_words added token::Z>><<diff color_words added:: e>>
1557 [EOF]
1558 ");
1559 }
1560
1561 #[test]
1562 fn test_diff_color_words_omit_blank_right_line() {
1563 let test_env = TestEnvironment::default();
1564 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1565 let work_dir = test_env.work_dir("repo");
1566
1567 // The middle hunk of file1 and file3 is
1568 // left = " x\n..."
1569 // right = "\n y\nz "
1570 //
1571 // file2 is different because left/right sides have the same number of "\n".
1572 work_dir.write_file(
1573 "file1",
1574 indoc! {"
1575 a x
1576 b
1577 "},
1578 );
1579 work_dir.write_file(
1580 "file2",
1581 indoc! {"
1582 a x
1583
1584 b
1585 "},
1586 );
1587 work_dir.write_file(
1588 "file3",
1589 indoc! {"
1590 a x
1591
1592
1593 b
1594 "},
1595 );
1596 work_dir.run_jj(["new"]).success();
1597 work_dir.write_file(
1598 "file1",
1599 indoc! {"
1600 a
1601 y
1602 z b
1603 "},
1604 );
1605 work_dir.write_file(
1606 "file2",
1607 indoc! {"
1608 a
1609 y
1610 z b
1611 "},
1612 );
1613 work_dir.write_file(
1614 "file3",
1615 indoc! {"
1616 a
1617 y
1618 z b
1619 "},
1620 );
1621
1622 let output = work_dir
1623 .run_jj(["diff", "--color=always"])
1624 .normalize_stdout_with(strip_ansi_escape_codes);
1625 insta::assert_snapshot!(output, @"
1626 Modified regular file file1:
1627 1 1: a x
1628 2: y
1629 2 3: z b
1630 Modified regular file file2:
1631 1 1: a x
1632 2 2: y
1633 3 3: z b
1634 Modified regular file file3:
1635 1 1: a x
1636 2 :
1637 3 :
1638 2: y
1639 4 3: z b
1640 [EOF]
1641 ");
1642 }
1643
1644 #[test]
1645 fn test_diff_missing_newline() {
1646 let test_env = TestEnvironment::default();
1647 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1648 let work_dir = test_env.work_dir("repo");
1649
1650 work_dir.write_file("file1", "foo");
1651 work_dir.write_file("file2", "foo\nbar");
1652 work_dir.run_jj(["new"]).success();
1653 work_dir.write_file("file1", "foo\nbar");
1654 work_dir.write_file("file2", "foo");
1655
1656 let output = work_dir.run_jj(["diff"]);
1657 insta::assert_snapshot!(output, @"
1658 Modified regular file file1:
1659 1 : foo
1660 1: foo
1661 2: bar
1662 Modified regular file file2:
1663 1 : foo
1664 2 : bar
1665 1: foo
1666 [EOF]
1667 ");
1668
1669 let output = work_dir.run_jj(["diff", "--git"]);
1670 insta::assert_snapshot!(output, @r"
1671 diff --git a/file1 b/file1
1672 index 1910281566..a907ec3f43 100644
1673 --- a/file1
1674 +++ b/file1
1675 @@ -1,1 +1,2 @@
1676 -foo
1677 \ No newline at end of file
1678 +foo
1679 +bar
1680 \ No newline at end of file
1681 diff --git a/file2 b/file2
1682 index a907ec3f43..1910281566 100644
1683 --- a/file2
1684 +++ b/file2
1685 @@ -1,2 +1,1 @@
1686 -foo
1687 -bar
1688 \ No newline at end of file
1689 +foo
1690 \ No newline at end of file
1691 [EOF]
1692 ");
1693
1694 let output = work_dir.run_jj(["diff", "--stat"]);
1695 insta::assert_snapshot!(output, @"
1696 file1 | 3 ++-
1697 file2 | 3 +--
1698 2 files changed, 3 insertions(+), 3 deletions(-)
1699 [EOF]
1700 ");
1701 }
1702
1703 #[test]
1704 fn test_color_words_diff_missing_newline() {
1705 let test_env = TestEnvironment::default();
1706 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1707 let work_dir = test_env.work_dir("repo");
1708
1709 work_dir.write_file("file1", "");
1710 work_dir.run_jj(["commit", "-m", "=== Empty"]).success();
1711 work_dir.write_file("file1", "a\nb\nc\nd\ne\nf\ng\nh\ni");
1712 work_dir
1713 .run_jj(["commit", "-m", "=== Add no newline"])
1714 .success();
1715 work_dir.write_file("file1", "A\nb\nc\nd\ne\nf\ng\nh\ni");
1716 work_dir
1717 .run_jj(["commit", "-m", "=== Modify first line"])
1718 .success();
1719 work_dir.write_file("file1", "A\nb\nc\nd\nE\nf\ng\nh\ni");
1720 work_dir
1721 .run_jj(["commit", "-m", "=== Modify middle line"])
1722 .success();
1723 work_dir.write_file("file1", "A\nb\nc\nd\nE\nf\ng\nh\nI");
1724 work_dir
1725 .run_jj(["commit", "-m", "=== Modify last line"])
1726 .success();
1727 work_dir.write_file("file1", "A\nb\nc\nd\nE\nf\ng\nh\nI\n");
1728 work_dir
1729 .run_jj(["commit", "-m", "=== Append newline"])
1730 .success();
1731 work_dir.write_file("file1", "A\nb\nc\nd\nE\nf\ng\nh\nI");
1732 work_dir
1733 .run_jj(["commit", "-m", "=== Remove newline"])
1734 .success();
1735 work_dir.write_file("file1", "");
1736 work_dir.run_jj(["commit", "-m", "=== Empty"]).success();
1737
1738 let output = work_dir
1739 .run_jj([
1740 "log",
1741 "-Tdescription",
1742 "-pr::@-",
1743 "--no-graph",
1744 "--reversed",
1745 "--color=always",
1746 ])
1747 .normalize_stdout_with(strip_ansi_escape_codes);
1748 insta::assert_snapshot!(output, @"
1749 === Empty
1750 Added regular file file1:
1751 (empty)
1752 === Add no newline
1753 Modified regular file file1:
1754 1: a
1755 2: b
1756 3: c
1757 4: d
1758 5: e
1759 6: f
1760 7: g
1761 8: h
1762 9: i
1763 === Modify first line
1764 Modified regular file file1:
1765 1 1: aA
1766 2 2: b
1767 3 3: c
1768 4 4: d
1769 ...
1770 === Modify middle line
1771 Modified regular file file1:
1772 1 1: A
1773 2 2: b
1774 3 3: c
1775 4 4: d
1776 5 5: eE
1777 6 6: f
1778 7 7: g
1779 8 8: h
1780 9 9: i
1781 === Modify last line
1782 Modified regular file file1:
1783 ...
1784 6 6: f
1785 7 7: g
1786 8 8: h
1787 9 9: iI
1788 === Append newline
1789 Modified regular file file1:
1790 ...
1791 6 6: f
1792 7 7: g
1793 8 8: h
1794 9 9: I
1795 === Remove newline
1796 Modified regular file file1:
1797 ...
1798 6 6: f
1799 7 7: g
1800 8 8: h
1801 9 9: I
1802 === Empty
1803 Modified regular file file1:
1804 1 : A
1805 2 : b
1806 3 : c
1807 4 : d
1808 5 : E
1809 6 : f
1810 7 : g
1811 8 : h
1812 9 : I
1813 [EOF]
1814 ");
1815
1816 let output = work_dir
1817 .run_jj([
1818 "log",
1819 "--config=diff.color-words.max-inline-alternation=0",
1820 "-Tdescription",
1821 "-pr::@-",
1822 "--no-graph",
1823 "--reversed",
1824 "--color=always",
1825 ])
1826 .normalize_stdout_with(strip_ansi_escape_codes);
1827 insta::assert_snapshot!(output, @"
1828 === Empty
1829 Added regular file file1:
1830 (empty)
1831 === Add no newline
1832 Modified regular file file1:
1833 1: a
1834 2: b
1835 3: c
1836 4: d
1837 5: e
1838 6: f
1839 7: g
1840 8: h
1841 9: i
1842 === Modify first line
1843 Modified regular file file1:
1844 1 : a
1845 1: A
1846 2 2: b
1847 3 3: c
1848 4 4: d
1849 ...
1850 === Modify middle line
1851 Modified regular file file1:
1852 1 1: A
1853 2 2: b
1854 3 3: c
1855 4 4: d
1856 5 : e
1857 5: E
1858 6 6: f
1859 7 7: g
1860 8 8: h
1861 9 9: i
1862 === Modify last line
1863 Modified regular file file1:
1864 ...
1865 6 6: f
1866 7 7: g
1867 8 8: h
1868 9 : i
1869 9: I
1870 === Append newline
1871 Modified regular file file1:
1872 ...
1873 6 6: f
1874 7 7: g
1875 8 8: h
1876 9 : I
1877 9: I
1878 === Remove newline
1879 Modified regular file file1:
1880 ...
1881 6 6: f
1882 7 7: g
1883 8 8: h
1884 9 : I
1885 9: I
1886 === Empty
1887 Modified regular file file1:
1888 1 : A
1889 2 : b
1890 3 : c
1891 4 : d
1892 5 : E
1893 6 : f
1894 7 : g
1895 8 : h
1896 9 : I
1897 [EOF]
1898 ");
1899 }
1900
1901 #[test]
1902 fn test_diff_ignore_whitespace() {
1903 let test_env = TestEnvironment::default();
1904 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1905 let work_dir = test_env.work_dir("repo");
1906
1907 work_dir.write_file(
1908 "file1",
1909 indoc! {"
1910 foo {
1911 bar;
1912 }
1913 baz {}
1914 "},
1915 );
1916 work_dir.run_jj(["new", "-minsert whitespace"]).success();
1917 work_dir.write_file(
1918 "file1",
1919 indoc! {"
1920 foo {
1921 bar;
1922 }
1923 baz { }
1924 "},
1925 );
1926 work_dir.run_jj(["new", "-mindent"]).success();
1927 work_dir.write_file(
1928 "file1",
1929 indoc! {"
1930 {
1931 foo {
1932 bar;
1933 }
1934 }
1935 baz { }
1936 "},
1937 );
1938
1939 // Git diff as reference output
1940 let output = work_dir.run_jj(["diff", "-r@-", "--git", "--ignore-all-space"]);
1941 insta::assert_snapshot!(output, @"
1942 diff --git a/file1 b/file1
1943 index f532aa68ad..d33445991b 100644
1944 --- a/file1
1945 +++ b/file1
1946 [EOF]
1947 ");
1948 let output = work_dir.run_jj(["diff", "--from=@--", "--git", "--ignore-all-space"]);
1949 insta::assert_snapshot!(output, @"
1950 diff --git a/file1 b/file1
1951 index f532aa68ad..033c4a6168 100644
1952 --- a/file1
1953 +++ b/file1
1954 @@ -1,4 +1,6 @@
1955 +{
1956 foo {
1957 bar;
1958 }
1959 +}
1960 baz { }
1961 [EOF]
1962 ");
1963 let output = work_dir.run_jj(["diff", "--from=@--", "--git", "--ignore-space-change"]);
1964 insta::assert_snapshot!(output, @"
1965 diff --git a/file1 b/file1
1966 index f532aa68ad..033c4a6168 100644
1967 --- a/file1
1968 +++ b/file1
1969 @@ -1,4 +1,6 @@
1970 -foo {
1971 +{
1972 + foo {
1973 bar;
1974 + }
1975 }
1976 -baz {}
1977 +baz { }
1978 [EOF]
1979 ");
1980
1981 // Diff-stat should respects the whitespace options
1982 let output = work_dir.run_jj(["diff", "-r@-", "--stat", "--ignore-all-space"]);
1983 insta::assert_snapshot!(output, @"
1984 file1 | 0
1985 1 file changed, 0 insertions(+), 0 deletions(-)
1986 [EOF]
1987 ");
1988 let output = work_dir.run_jj(["diff", "--from=@--", "--stat", "--ignore-all-space"]);
1989 insta::assert_snapshot!(output, @"
1990 file1 | 2 ++
1991 1 file changed, 2 insertions(+), 0 deletions(-)
1992 [EOF]
1993 ");
1994 let output = work_dir.run_jj(["diff", "--from=@--", "--stat", "--ignore-space-change"]);
1995 insta::assert_snapshot!(output, @"
1996 file1 | 6 ++++--
1997 1 file changed, 4 insertions(+), 2 deletions(-)
1998 [EOF]
1999 ");
2000
2001 // "..." is printed if contents differ only in whitespace
2002 let output = work_dir.run_jj(["diff", "-r@-", "--color=always", "--ignore-all-space"]);
2003 insta::assert_snapshot!(output, @"
2004 Modified regular file file1:
2005 ...
2006 [EOF]
2007 ");
2008
2009 // Word-level changes are still highlighted
2010 let output = work_dir.run_jj(["diff", "--from=@--", "--color=always", "--ignore-all-space"]);
2011 insta::assert_snapshot!(output, @"
2012 Modified regular file file1:
2013  1: {
2014  1  2:  foo {
2015  2  3:  bar;
2016  3  4:  }
2017  5: }
2018  4  6: baz { }
2019 [EOF]
2020 ");
2021 let output = work_dir.run_jj([
2022 "diff",
2023 "--from=@--",
2024 "--color=always",
2025 "--ignore-space-change",
2026 ]);
2027 insta::assert_snapshot!(output, @"
2028 Modified regular file file1:
2029  1: {
2030  1  2:  foo {
2031  2  3:  bar;
2032  4:  }
2033  3  5: }
2034  4  6: baz { }
2035 [EOF]
2036 ");
2037 }
2038
2039 #[test]
2040 fn test_diff_skipped_context() {
2041 let test_env = TestEnvironment::default();
2042 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2043 let work_dir = test_env.work_dir("repo");
2044
2045 work_dir.write_file("file1", "a\nb\nc\nd\ne\nf\ng\nh\ni\nj");
2046 work_dir
2047 .run_jj(["describe", "-m", "=== Left side of diffs"])
2048 .success();
2049
2050 work_dir
2051 .run_jj(["new", "@", "-m", "=== Must skip 2 lines"])
2052 .success();
2053 work_dir.write_file("file1", "A\nb\nc\nd\ne\nf\ng\nh\ni\nJ");
2054 work_dir
2055 .run_jj(["new", "@-", "-m", "=== Don't skip 1 line"])
2056 .success();
2057 work_dir.write_file("file1", "A\nb\nc\nd\ne\nf\ng\nh\nI\nj");
2058 work_dir
2059 .run_jj(["new", "@-", "-m", "=== No gap to skip"])
2060 .success();
2061 work_dir.write_file("file1", "a\nB\nc\nd\ne\nf\ng\nh\nI\nj");
2062 work_dir
2063 .run_jj(["new", "@-", "-m", "=== No gap to skip"])
2064 .success();
2065 work_dir.write_file("file1", "a\nb\nC\nd\ne\nf\ng\nh\nI\nj");
2066 work_dir
2067 .run_jj(["new", "@-", "-m", "=== 1 line at start"])
2068 .success();
2069 work_dir.write_file("file1", "a\nb\nc\nd\nE\nf\ng\nh\ni\nj");
2070 work_dir
2071 .run_jj(["new", "@-", "-m", "=== 1 line at end"])
2072 .success();
2073 work_dir.write_file("file1", "a\nb\nc\nd\ne\nF\ng\nh\ni\nj");
2074
2075 let output = work_dir.run_jj(["log", "-Tdescription", "-p", "--no-graph", "--reversed"]);
2076 insta::assert_snapshot!(output, @"
2077 === Left side of diffs
2078 Added regular file file1:
2079 1: a
2080 2: b
2081 3: c
2082 4: d
2083 5: e
2084 6: f
2085 7: g
2086 8: h
2087 9: i
2088 10: j
2089 === Must skip 2 lines
2090 Modified regular file file1:
2091 1 : a
2092 1: A
2093 2 2: b
2094 3 3: c
2095 4 4: d
2096 ...
2097 7 7: g
2098 8 8: h
2099 9 9: i
2100 10 : j
2101 10: J
2102 === Don't skip 1 line
2103 Modified regular file file1:
2104 1 : a
2105 1: A
2106 2 2: b
2107 3 3: c
2108 4 4: d
2109 5 5: e
2110 6 6: f
2111 7 7: g
2112 8 8: h
2113 9 : i
2114 9: I
2115 10 10: j
2116 === No gap to skip
2117 Modified regular file file1:
2118 1 1: a
2119 2 : b
2120 2: B
2121 3 3: c
2122 4 4: d
2123 5 5: e
2124 6 6: f
2125 7 7: g
2126 8 8: h
2127 9 : i
2128 9: I
2129 10 10: j
2130 === No gap to skip
2131 Modified regular file file1:
2132 1 1: a
2133 2 2: b
2134 3 : c
2135 3: C
2136 4 4: d
2137 5 5: e
2138 6 6: f
2139 7 7: g
2140 8 8: h
2141 9 : i
2142 9: I
2143 10 10: j
2144 === 1 line at start
2145 Modified regular file file1:
2146 1 1: a
2147 2 2: b
2148 3 3: c
2149 4 4: d
2150 5 : e
2151 5: E
2152 6 6: f
2153 7 7: g
2154 8 8: h
2155 ...
2156 === 1 line at end
2157 Modified regular file file1:
2158 ...
2159 3 3: c
2160 4 4: d
2161 5 5: e
2162 6 : f
2163 6: F
2164 7 7: g
2165 8 8: h
2166 9 9: i
2167 10 10: j
2168 [EOF]
2169 ");
2170 }
2171
2172 #[test]
2173 fn test_diff_skipped_context_from_settings_color_words() {
2174 let test_env = TestEnvironment::default();
2175 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2176 let work_dir = test_env.work_dir("repo");
2177
2178 test_env.add_config(
2179 r#"
2180 [diff.color-words]
2181 context = 0
2182 "#,
2183 );
2184
2185 work_dir.write_file("file1", "a\nb\nc\nd\ne");
2186 work_dir
2187 .run_jj(["describe", "-m", "=== First commit"])
2188 .success();
2189
2190 work_dir
2191 .run_jj(["new", "@", "-m", "=== Must show 0 context"])
2192 .success();
2193 work_dir.write_file("file1", "a\nb\nC\nd\ne");
2194
2195 let output = work_dir
2196 .run_jj([
2197 "log",
2198 "-Tdescription",
2199 "-p",
2200 "--no-graph",
2201 "--reversed",
2202 "--color=always",
2203 ])
2204 .normalize_stdout_with(strip_ansi_escape_codes);
2205 insta::assert_snapshot!(output, @"
2206 === First commit
2207 Added regular file file1:
2208 1: a
2209 2: b
2210 3: c
2211 4: d
2212 5: e
2213 === Must show 0 context
2214 Modified regular file file1:
2215 ...
2216 3 3: cC
2217 ...
2218 [EOF]
2219 ");
2220 }
2221
2222 #[test]
2223 fn test_diff_skipped_context_from_settings_git() {
2224 let test_env = TestEnvironment::default();
2225 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2226 let work_dir = test_env.work_dir("repo");
2227
2228 test_env.add_config(
2229 r#"
2230 [diff.git]
2231 context = 0
2232 "#,
2233 );
2234
2235 work_dir.write_file("file1", "a\nb\nc\nd\ne");
2236 work_dir
2237 .run_jj(["describe", "-m", "=== First commit"])
2238 .success();
2239
2240 work_dir
2241 .run_jj(["new", "@", "-m", "=== Must show 0 context"])
2242 .success();
2243 work_dir.write_file("file1", "a\nb\nC\nd\ne");
2244
2245 let output = work_dir.run_jj([
2246 "log",
2247 "-Tdescription",
2248 "-p",
2249 "--git",
2250 "--no-graph",
2251 "--reversed",
2252 ]);
2253 insta::assert_snapshot!(output, @r"
2254 === First commit
2255 diff --git a/file1 b/file1
2256 new file mode 100644
2257 index 0000000000..0fec236860
2258 --- /dev/null
2259 +++ b/file1
2260 @@ -0,0 +1,5 @@
2261 +a
2262 +b
2263 +c
2264 +d
2265 +e
2266 \ No newline at end of file
2267 === Must show 0 context
2268 diff --git a/file1 b/file1
2269 index 0fec236860..b7615dae52 100644
2270 --- a/file1
2271 +++ b/file1
2272 @@ -3,1 +3,1 @@
2273 -c
2274 +C
2275 [EOF]
2276 ");
2277 }
2278
2279 #[test]
2280 fn test_diff_skipped_context_nondefault() {
2281 let test_env = TestEnvironment::default();
2282 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2283 let work_dir = test_env.work_dir("repo");
2284
2285 work_dir.write_file("file1", "a\nb\nc\nd");
2286 work_dir
2287 .run_jj(["describe", "-m", "=== Left side of diffs"])
2288 .success();
2289
2290 work_dir
2291 .run_jj(["new", "@", "-m", "=== Must skip 2 lines"])
2292 .success();
2293 work_dir.write_file("file1", "A\nb\nc\nD");
2294 work_dir
2295 .run_jj(["new", "@-", "-m", "=== Don't skip 1 line"])
2296 .success();
2297 work_dir.write_file("file1", "A\nb\nC\nd");
2298 work_dir
2299 .run_jj(["new", "@-", "-m", "=== No gap to skip"])
2300 .success();
2301 work_dir.write_file("file1", "a\nB\nC\nd");
2302 work_dir
2303 .run_jj(["new", "@-", "-m", "=== 1 line at start"])
2304 .success();
2305 work_dir.write_file("file1", "a\nB\nc\nd");
2306 work_dir
2307 .run_jj(["new", "@-", "-m", "=== 1 line at end"])
2308 .success();
2309 work_dir.write_file("file1", "a\nb\nC\nd");
2310
2311 let output = work_dir.run_jj([
2312 "log",
2313 "-Tdescription",
2314 "-p",
2315 "--no-graph",
2316 "--reversed",
2317 "--context=0",
2318 ]);
2319 insta::assert_snapshot!(output, @"
2320 === Left side of diffs
2321 Added regular file file1:
2322 1: a
2323 2: b
2324 3: c
2325 4: d
2326 === Must skip 2 lines
2327 Modified regular file file1:
2328 1 : a
2329 1: A
2330 ...
2331 4 : d
2332 4: D
2333 === Don't skip 1 line
2334 Modified regular file file1:
2335 1 : a
2336 1: A
2337 2 2: b
2338 3 : c
2339 3: C
2340 4 4: d
2341 === No gap to skip
2342 Modified regular file file1:
2343 1 1: a
2344 2 : b
2345 3 : c
2346 2: B
2347 3: C
2348 4 4: d
2349 === 1 line at start
2350 Modified regular file file1:
2351 1 1: a
2352 2 : b
2353 2: B
2354 ...
2355 === 1 line at end
2356 Modified regular file file1:
2357 ...
2358 3 : c
2359 3: C
2360 4 4: d
2361 [EOF]
2362 ");
2363 }
2364
2365 #[test]
2366 fn test_diff_leading_trailing_context() {
2367 let test_env = TestEnvironment::default();
2368 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2369 let work_dir = test_env.work_dir("repo");
2370
2371 // N=5 context lines at start/end of the file
2372 work_dir.write_file("file1", "1\n2\n3\n4\n5\nL\n6\n7\n8\n9\n10\n11\n");
2373 work_dir.run_jj(["new"]).success();
2374 work_dir.write_file("file1", "1\n2\n3\n4\n5\n6\nR\n7\n8\n9\n10\n11\n");
2375
2376 // N=5 <= num_context_lines + 1: No room to skip.
2377 let output = work_dir.run_jj(["diff", "--context=4"]);
2378 insta::assert_snapshot!(output, @"
2379 Modified regular file file1:
2380 1 1: 1
2381 2 2: 2
2382 3 3: 3
2383 4 4: 4
2384 5 5: 5
2385 6 : L
2386 7 6: 6
2387 7: R
2388 8 8: 7
2389 9 9: 8
2390 10 10: 9
2391 11 11: 10
2392 12 12: 11
2393 [EOF]
2394 ");
2395
2396 // N=5 <= 2 * num_context_lines + 1: The last hunk wouldn't be split if
2397 // trailing diff existed.
2398 let output = work_dir.run_jj(["diff", "--context=3"]);
2399 insta::assert_snapshot!(output, @"
2400 Modified regular file file1:
2401 ...
2402 3 3: 3
2403 4 4: 4
2404 5 5: 5
2405 6 : L
2406 7 6: 6
2407 7: R
2408 8 8: 7
2409 9 9: 8
2410 10 10: 9
2411 ...
2412 [EOF]
2413 ");
2414
2415 // N=5 > 2 * num_context_lines + 1: The last hunk should be split no matter
2416 // if trailing diff existed.
2417 let output = work_dir.run_jj(["diff", "--context=1"]);
2418 insta::assert_snapshot!(output, @"
2419 Modified regular file file1:
2420 ...
2421 5 5: 5
2422 6 : L
2423 7 6: 6
2424 7: R
2425 8 8: 7
2426 ...
2427 [EOF]
2428 ");
2429
2430 // N=5 <= num_context_lines: No room to skip.
2431 let output = work_dir.run_jj(["diff", "--git", "--context=5"]);
2432 insta::assert_snapshot!(output, @"
2433 diff --git a/file1 b/file1
2434 index 1bf57dee4a..69b3e1865c 100644
2435 --- a/file1
2436 +++ b/file1
2437 @@ -1,12 +1,12 @@
2438 1
2439 2
2440 3
2441 4
2442 5
2443 -L
2444 6
2445 +R
2446 7
2447 8
2448 9
2449 10
2450 11
2451 [EOF]
2452 ");
2453
2454 // N=5 <= 2 * num_context_lines: The last hunk wouldn't be split if
2455 // trailing diff existed.
2456 let output = work_dir.run_jj(["diff", "--git", "--context=3"]);
2457 insta::assert_snapshot!(output, @"
2458 diff --git a/file1 b/file1
2459 index 1bf57dee4a..69b3e1865c 100644
2460 --- a/file1
2461 +++ b/file1
2462 @@ -3,8 +3,8 @@
2463 3
2464 4
2465 5
2466 -L
2467 6
2468 +R
2469 7
2470 8
2471 9
2472 [EOF]
2473 ");
2474
2475 // N=5 > 2 * num_context_lines: The last hunk should be split no matter
2476 // if trailing diff existed.
2477 let output = work_dir.run_jj(["diff", "--git", "--context=2"]);
2478 insta::assert_snapshot!(output, @"
2479 diff --git a/file1 b/file1
2480 index 1bf57dee4a..69b3e1865c 100644
2481 --- a/file1
2482 +++ b/file1
2483 @@ -4,6 +4,6 @@
2484 4
2485 5
2486 -L
2487 6
2488 +R
2489 7
2490 8
2491 [EOF]
2492 ");
2493 }
2494
2495 #[test]
2496 fn test_diff_conflict_sides_differ() {
2497 let test_env = TestEnvironment::default();
2498 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2499 let work_dir = test_env.work_dir("repo");
2500
2501 // Based on test_materialize_conflict_basic() in test_conflicts.rs
2502 let base_content = indoc! {"
2503 line 1
2504 line 2
2505 line 3
2506 line 4
2507 line 5
2508 "};
2509 let left1_content = indoc! {"
2510 line 1
2511 line 2
2512 left 3.1
2513 left 3.2
2514 left 3.3
2515 line 4
2516 line 5
2517 "};
2518 let left2_content = indoc! {"
2519 left 1.1
2520 line 2
2521 left 3.1
2522 left 3.2
2523 left 3.3
2524 left 3.4
2525 line 4
2526 line 5
2527 "};
2528 let right1_content = indoc! {"
2529 line 1
2530 line 2
2531 right 3.1
2532 line 4
2533 line 5
2534 "};
2535 let right2_content = indoc! {"
2536 line 1
2537 line 2
2538 right 3.1
2539 line 4
2540 "};
2541 let create_content_commit = |name: &str, parents: &[&str], content: &str| {
2542 create_commit_with_files(&work_dir, name, parents, &[("file", content)]);
2543 };
2544 create_content_commit("base", &[], base_content);
2545 create_content_commit("left1", &["base"], left1_content);
2546 create_content_commit("left2", &["left1"], left2_content);
2547 create_content_commit("right1", &["base"], right1_content);
2548 create_content_commit("right2", &["right1"], right2_content);
2549 create_commit_with_files(&work_dir, "left1+right1", &["left1", "right1"], &[]);
2550 create_commit_with_files(&work_dir, "left2+right2", &["left2", "right2"], &[]);
2551
2552 // Test the setup. left2+right2 can be considered a modified version of
2553 // left1+right1.
2554 work_dir.run_jj(["new", "root()"]).success();
2555 insta::assert_snapshot!(work_dir.run_jj(["log", "-r~@"]), @"
2556 × lylxulpl [email protected] 2001-02-03 08:05:20 left2+right2 9bdc15d2 (conflict)
2557 ├─╮ (empty) left2+right2
2558 │ ○ znkkpsqq [email protected] 2001-02-03 08:05:17 right2 e57450eb
2559 │ │ right2
2560 ○ │ royxmykx [email protected] 2001-02-03 08:05:13 left2 b50b218b
2561 │ │ left2
2562 │ │ × kmkuslsw [email protected] 2001-02-03 08:05:18 left1+right1 b866d751 (conflict)
2563 ╭─┬─╯ (empty) left1+right1
2564 │ ○ vruxwmqv [email protected] 2001-02-03 08:05:15 right1 3fe2e860
2565 │ │ right1
2566 ○ │ zsuskuln [email protected] 2001-02-03 08:05:11 left1 713a980c
2567 ├─╯ left1
2568 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 base aa7e33ed
2569 │ base
2570 ◆ zzzzzzzz root() 00000000
2571 [EOF]
2572 ");
2573
2574 // Diff of empty merge commit
2575 let output = work_dir.run_jj(["diff", "--git", "-rleft1+right1"]);
2576 insta::assert_snapshot!(output, @"");
2577 let output = work_dir.run_jj(["diff", "--color=always", "-rleft1+right1"]);
2578 insta::assert_snapshot!(output, @"");
2579 let output = work_dir.run_jj([
2580 "diff",
2581 "--color=always",
2582 "--config=diff.color-words.conflict=pair",
2583 "-rleft1+right1",
2584 ]);
2585 insta::assert_snapshot!(output, @"");
2586
2587 let diff_git_materialized = |from: &str, to: &str| {
2588 work_dir.run_jj([
2589 "diff",
2590 "--git",
2591 "--context=1",
2592 &format!("--from={from}"),
2593 &format!("--to={to}"),
2594 ])
2595 };
2596 let diff_color_words_materialized = |from: &str, to: &str| {
2597 work_dir.run_jj([
2598 "diff",
2599 "--color=always",
2600 "--context=1",
2601 &format!("--from={from}"),
2602 &format!("--to={to}"),
2603 ])
2604 };
2605 let diff_color_words_conflict_pair = |from: &str, to: &str| {
2606 work_dir.run_jj([
2607 "diff",
2608 "--color=always",
2609 "--context=1",
2610 "--config=diff.color-words.conflict=pair",
2611 &format!("--from={from}"),
2612 &format!("--to={to}"),
2613 ])
2614 };
2615
2616 // Diff from resolved to conflict
2617 insta::assert_snapshot!(diff_git_materialized("base", "left1+right1"), @r#"
2618 diff --git a/file b/file
2619 index 94c99a3280..0000000000 100644
2620 --- a/file
2621 +++ b/file
2622 @@ -2,3 +2,12 @@
2623 line 2
2624 -line 3
2625 +<<<<<<< conflict 1 of 1
2626 ++++++++ zsuskuln 713a980c "left1"
2627 +left 3.1
2628 +left 3.2
2629 +left 3.3
2630 +%%%%%%% diff from: rlvkpnrz aa7e33ed "base"
2631 +\\\\\\\ to: vruxwmqv 3fe2e860 "right1"
2632 +-line 3
2633 ++right 3.1
2634 +>>>>>>> conflict 1 of 1 ends
2635 line 4
2636 [EOF]
2637 "#);
2638 insta::assert_snapshot!(diff_color_words_materialized("base", "left1+right1"), @r#"
2639 Created conflict in file:
2640  1  1: line 1
2641  2  2: line 2
2642  3: <<<<<<< conflict 1 of 1
2643  4: +++++++ zsuskuln 713a980c "left1"
2644  5: left 3.1
2645  6: left 3.2
2646  7: left 3.3
2647  8: %%%%%%% diff from: rlvkpnrz aa7e33ed "base"
2648  9: \\\\\\\ to: vruxwmqv 3fe2e860 "right1"
2649  3  10: -line 3
2650  11: +right 3.1
2651  12: >>>>>>> conflict 1 of 1 ends
2652  4  13: line 4
2653  5  14: line 5
2654 [EOF]
2655 "#);
2656 insta::assert_snapshot!(diff_color_words_conflict_pair("base", "left1+right1"), @"
2657 Created conflict in file:
2658  1  1: line 1
2659  2  2: line 2
2660 <<<<<<< Created conflict
2661 +++++++ left side #1 to right side #1
2662  3  3: lineleft 3.1
2663  4: left 3.2
2664  3  5: left 3.3
2665 ------- left side #1 to right base #1
2666  3  3: line 3
2667 +++++++ left side #1 to right side #2
2668  3  3: lineright 3.1
2669 >>>>>>> Conflict ends
2670  4  6: line 4
2671  5  7: line 5
2672 [EOF]
2673 ");
2674
2675 // Diff from conflict to resolved
2676 insta::assert_snapshot!(diff_git_materialized("left1+right1", "base"), @r#"
2677 diff --git a/file b/file
2678 index 0000000000..94c99a3280 100644
2679 --- a/file
2680 +++ b/file
2681 @@ -2,12 +2,3 @@
2682 line 2
2683 -<<<<<<< conflict 1 of 1
2684 -+++++++ zsuskuln 713a980c "left1"
2685 -left 3.1
2686 -left 3.2
2687 -left 3.3
2688 -%%%%%%% diff from: rlvkpnrz aa7e33ed "base"
2689 -\\\\\\\ to: vruxwmqv 3fe2e860 "right1"
2690 --line 3
2691 -+right 3.1
2692 ->>>>>>> conflict 1 of 1 ends
2693 +line 3
2694 line 4
2695 [EOF]
2696 "#);
2697 insta::assert_snapshot!(diff_color_words_materialized("left1+right1", "base"), @r#"
2698 Resolved conflict in file:
2699  1  1: line 1
2700  2  2: line 2
2701  3 : <<<<<<< conflict 1 of 1
2702  4 : +++++++ zsuskuln 713a980c "left1"
2703  5 : left 3.1
2704  6 : left 3.2
2705  7 : left 3.3
2706  8 : %%%%%%% diff from: rlvkpnrz aa7e33ed "base"
2707  9 : \\\\\\\ to: vruxwmqv 3fe2e860 "right1"
2708  10  3: -line 3
2709  11 : +right 3.1
2710  12 : >>>>>>> conflict 1 of 1 ends
2711  13  4: line 4
2712  14  5: line 5
2713 [EOF]
2714 "#);
2715 insta::assert_snapshot!(diff_color_words_conflict_pair("left1+right1", "base"), @"
2716 Resolved conflict in file:
2717  1  1: line 1
2718  2  2: line 2
2719 <<<<<<< Resolved conflict
2720 +++++++ left side #1 to right side #1
2721  3  3: leftline 3.1
2722  4 : left 3.2
2723  5  3: left 3.3
2724 ------- left base #1 to right side #1
2725  3  3: line 3
2726 +++++++ left side #2 to right side #1
2727  3  3: rightline 3.1
2728 >>>>>>> Conflict ends
2729  6  4: line 4
2730  7  5: line 5
2731 [EOF]
2732 ");
2733
2734 // Diff between conflicts
2735 insta::assert_snapshot!(diff_git_materialized("left1+right1", "left2+right2"), @r#"
2736 diff --git a/file b/file
2737 --- a/file
2738 +++ b/file
2739 @@ -1,5 +1,5 @@
2740 -line 1
2741 +left 1.1
2742 line 2
2743 <<<<<<< conflict 1 of 1
2744 -+++++++ zsuskuln 713a980c "left1"
2745 ++++++++ royxmykx b50b218b "left2"
2746 left 3.1
2747 @@ -7,4 +7,5 @@
2748 left 3.3
2749 +left 3.4
2750 %%%%%%% diff from: rlvkpnrz aa7e33ed "base"
2751 -\\\\\\\ to: vruxwmqv 3fe2e860 "right1"
2752 +\\\\\\\ to: znkkpsqq e57450eb "right2"
2753 -line 3
2754 @@ -13,2 +14,1 @@
2755 line 4
2756 -line 5
2757 [EOF]
2758 "#);
2759 insta::assert_snapshot!(diff_color_words_materialized("left1+right1", "left2+right2"), @r#"
2760 Modified conflict in file:
2761  1  1: lineleft 1.1
2762  2  2: line 2
2763  3  3: <<<<<<< conflict 1 of 1
2764  4 : +++++++ zsuskuln 713a980c "left1"
2765  4: +++++++ royxmykx b50b218b "left2"
2766  5  5: left 3.1
2767  6  6: left 3.2
2768  7  7: left 3.3
2769  8: left 3.4
2770  8  9: %%%%%%% diff from: rlvkpnrz aa7e33ed "base"
2771  9 : \\\\\\\ to: vruxwmqv 3fe2e860 "right1"
2772  10: \\\\\\\ to: znkkpsqq e57450eb "right2"
2773  10  11: -line 3
2774 ...
2775  13  14: line 4
2776  14 : line 5
2777 [EOF]
2778 "#);
2779 insta::assert_snapshot!(diff_color_words_conflict_pair("left1+right1", "left2+right2"), @"
2780 Modified conflict in file:
2781  1  1: lineleft 1.1
2782  2  2: line 2
2783 <<<<<<< Modified conflict
2784 +++++++ left side #1 to right side #1
2785 ...
2786  5  5: left 3.3
2787  6: left 3.4
2788 ------- left base #1 to right base #1
2789  3  3: line 3
2790 +++++++ left side #2 to right side #2
2791  3  3: right 3.1
2792 >>>>>>> Conflict ends
2793  6  7: line 4
2794  7 : line 5
2795 [EOF]
2796 ");
2797 }
2798
2799 #[test]
2800 fn test_diff_conflict_bases_differ() {
2801 let test_env = TestEnvironment::default();
2802 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2803 let work_dir = test_env.work_dir("repo");
2804
2805 // Based on test_materialize_conflict_basic() in test_conflicts.rs
2806 //
2807 // - "line1" differs between L1-B1+R1 and L2-B2+R2
2808 // - "line5" and "line5.1" are deleted cleanly at L1-B1+R1 and L2-B2+R2
2809 // respectively
2810 let base1_content = indoc! {"
2811 line 1
2812 line 2
2813 line 3
2814 line 4
2815 line 5
2816 "};
2817 let base2_content = indoc! {"
2818 line 2
2819 line 3.1
2820 line 3.2
2821 line 4
2822 line 5.1
2823 "};
2824 let left1_content = indoc! {"
2825 line 1
2826 line 2
2827 left 3.1
2828 left 3.2
2829 left 3.3
2830 line 4
2831 line 5
2832 "};
2833 let left2_content = indoc! {"
2834 line 2
2835 left 3.1
2836 left 3.2
2837 left 3.3
2838 line 4
2839 line 5.1
2840 "};
2841 let right1_content = indoc! {"
2842 line 1
2843 line 2
2844 right 3.1
2845 line 4
2846 "};
2847 let right2_content = indoc! {"
2848 line 2
2849 right 3.1
2850 line 4
2851 "};
2852 let create_content_commit = |name: &str, parents: &[&str], content: &str| {
2853 create_commit_with_files(&work_dir, name, parents, &[("file", content)]);
2854 };
2855 create_content_commit("base1", &[], base1_content);
2856 create_content_commit("left1", &["base1"], left1_content);
2857 create_content_commit("right1", &["base1"], right1_content);
2858 create_content_commit("base2", &["base1"], base2_content);
2859 create_content_commit("left2", &["base2"], left2_content);
2860 create_content_commit("right2", &["base2"], right2_content);
2861 create_commit_with_files(&work_dir, "left1+right1", &["left1", "right1"], &[]);
2862 create_commit_with_files(&work_dir, "left2+right2", &["left2", "right2"], &[]);
2863
2864 // Test the setup. left2+right2 can be considered a rebased version of
2865 // left1+right1.
2866 work_dir.run_jj(["new", "root()"]).success();
2867 insta::assert_snapshot!(work_dir.run_jj(["log", "-r~@"]), @"
2868 × nkmrtpmo [email protected] 2001-02-03 08:05:22 left2+right2 18348d27 (conflict)
2869 ├─╮ (empty) left2+right2
2870 │ ○ kmkuslsw [email protected] 2001-02-03 08:05:19 right2 656695c3
2871 │ │ right2
2872 ○ │ znkkpsqq [email protected] 2001-02-03 08:05:17 left2 218094ec
2873 ├─╯ left2
2874 ○ vruxwmqv [email protected] 2001-02-03 08:05:15 base2 3c4d67e6
2875 │ base2
2876 │ × lylxulpl [email protected] 2001-02-03 08:05:20 left1+right1 91e2d46a (conflict)
2877 │ ├─╮ (empty) left1+right1
2878 │ │ ○ royxmykx [email protected] 2001-02-03 08:05:13 right1 3087be1f
2879 ├───╯ right1
2880 │ ○ zsuskuln [email protected] 2001-02-03 08:05:11 left1 9e995075
2881 ├─╯ left1
2882 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 base1 44cfbde6
2883 │ base1
2884 ◆ zzzzzzzz root() 00000000
2885 [EOF]
2886 ");
2887
2888 let diff_git_materialized = |from: &str, to: &str| {
2889 work_dir.run_jj([
2890 "diff",
2891 "--git",
2892 "--context=1",
2893 &format!("--from={from}"),
2894 &format!("--to={to}"),
2895 ])
2896 };
2897 let diff_color_words_materialized = |from: &str, to: &str| {
2898 work_dir.run_jj([
2899 "diff",
2900 "--color=always",
2901 "--context=1",
2902 &format!("--from={from}"),
2903 &format!("--to={to}"),
2904 ])
2905 };
2906 let diff_color_words_conflict_pair = |from: &str, to: &str| {
2907 work_dir.run_jj([
2908 "diff",
2909 "--color=always",
2910 "--context=1",
2911 "--config=diff.color-words.conflict=pair",
2912 &format!("--from={from}"),
2913 &format!("--to={to}"),
2914 ])
2915 };
2916
2917 // Diff between conflicts
2918 insta::assert_snapshot!(diff_git_materialized("left1+right1", "left2+right2"), @r#"
2919 diff --git a/file b/file
2920 --- a/file
2921 +++ b/file
2922 @@ -1,5 +1,4 @@
2923 -line 1
2924 line 2
2925 <<<<<<< conflict 1 of 1
2926 -+++++++ zsuskuln 9e995075 "left1"
2927 ++++++++ znkkpsqq 218094ec "left2"
2928 left 3.1
2929 @@ -7,5 +6,6 @@
2930 left 3.3
2931 -%%%%%%% diff from: rlvkpnrz 44cfbde6 "base1"
2932 -\\\\\\\ to: royxmykx 3087be1f "right1"
2933 --line 3
2934 +%%%%%%% diff from: vruxwmqv 3c4d67e6 "base2"
2935 +\\\\\\\ to: kmkuslsw 656695c3 "right2"
2936 +-line 3.1
2937 +-line 3.2
2938 +right 3.1
2939 [EOF]
2940 "#);
2941 insta::assert_snapshot!(diff_color_words_materialized("left1+right1", "left2+right2"), @r#"
2942 Modified conflict in file:
2943  1 : line 1
2944  2  1: line 2
2945  3  2: <<<<<<< conflict 1 of 1
2946  4 : +++++++ zsuskuln 9e995075 "left1"
2947  3: +++++++ znkkpsqq 218094ec "left2"
2948  5  4: left 3.1
2949  6  5: left 3.2
2950  7  6: left 3.3
2951  8 : %%%%%%% diff from: rlvkpnrz 44cfbde6 "base1"
2952  9 : \\\\\\\ to: royxmykx 3087be1f "right1"
2953  10 : -line 3
2954  7: %%%%%%% diff from: vruxwmqv 3c4d67e6 "base2"
2955  8: \\\\\\\ to: kmkuslsw 656695c3 "right2"
2956  9: -line 3.1
2957  10: -line 3.2
2958  11  11: +right 3.1
2959 ...
2960 [EOF]
2961 "#);
2962 insta::assert_snapshot!(diff_color_words_conflict_pair("left1+right1", "left2+right2"), @"
2963 Modified conflict in file:
2964  1 : line 1
2965  2  1: line 2
2966 <<<<<<< Modified conflict
2967 +++++++ left side #1 to right side #1
2968 ...
2969 ------- left base #1 to right base #1
2970  3  2: line 3.1
2971  3  3: line 3.2
2972 +++++++ left side #2 to right side #2
2973  3  2: right 3.1
2974 >>>>>>> Conflict ends
2975  6  5: line 4
2976 [EOF]
2977 ");
2978 }
2979
2980 #[test]
2981 fn test_diff_conflict_three_sides() {
2982 let test_env = TestEnvironment::default();
2983 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2984 let work_dir = test_env.work_dir("repo");
2985
2986 // Based on test_materialize_conflict_three_sides() in test_conflicts.rs
2987 let base1_content = indoc! {"
2988 line 1
2989 line 2 base
2990 line 5
2991 "};
2992 let base2_content = indoc! {"
2993 line 1
2994 line 2 base
2995 line 3 base
2996 line 4 base
2997 line 5
2998 "};
2999 let side1_content = indoc! {"
3000 line 1
3001 line 2 a.1
3002 line 3 a.2
3003 line 4 base
3004 line 5
3005 "};
3006 let side2_content = indoc! {"
3007 line 1
3008 line 2 b.1
3009 line 3 base
3010 line 4 b.2
3011 line 5
3012 "};
3013 let side3_content = indoc! {"
3014 line 1
3015 line 2 base
3016 line 3 c.2
3017 line 5
3018 "};
3019 let create_content_commit = |name: &str, parents: &[&str], content: &str| {
3020 create_commit_with_files(&work_dir, name, parents, &[("file", content)]);
3021 };
3022 // S1 S2 S3
3023 // B2
3024 // B1
3025 create_content_commit("base1", &[], base1_content);
3026 create_content_commit("base2", &["base1"], base2_content);
3027 create_content_commit("side1", &["base2"], side1_content);
3028 create_content_commit("side2", &["base2"], side2_content);
3029 create_content_commit("side3", &["base1"], side3_content);
3030 create_commit_with_files(&work_dir, "side1+side2", &["side1", "side2"], &[]);
3031 create_commit_with_files(
3032 &work_dir,
3033 "side1+side2+side3",
3034 &["side1+side2", "side3"],
3035 &[],
3036 );
3037
3038 // Test the setup
3039 work_dir.run_jj(["new", "root()"]).success();
3040 insta::assert_snapshot!(work_dir.run_jj(["log", "-r~@"]), @"
3041 × lylxulpl [email protected] 2001-02-03 08:05:20 side1+side2+side3 90cd440f (conflict)
3042 ├─╮ (empty) side1+side2+side3
3043 │ ○ znkkpsqq [email protected] 2001-02-03 08:05:17 side3 f73063c9
3044 │ │ side3
3045 × │ kmkuslsw [email protected] 2001-02-03 08:05:18 side1+side2 02d6c21d (conflict)
3046 ├───╮ (empty) side1+side2
3047 │ │ ○ vruxwmqv [email protected] 2001-02-03 08:05:15 side2 bc176227
3048 │ │ │ side2
3049 ○ │ │ royxmykx [email protected] 2001-02-03 08:05:13 side1 3a079496
3050 ├───╯ side1
3051 ○ │ zsuskuln [email protected] 2001-02-03 08:05:11 base2 75289ea3
3052 ├─╯ base2
3053 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 base1 07965fa1
3054 │ base1
3055 ◆ zzzzzzzz root() 00000000
3056 [EOF]
3057 ");
3058 insta::assert_snapshot!(work_dir.run_jj(["resolve", "-lrside1+side2"]), @"
3059 file 2-sided conflict
3060 [EOF]
3061 ");
3062 insta::assert_snapshot!(work_dir.run_jj(["resolve", "-lrside1+side2+side3"]), @"
3063 file 3-sided conflict
3064 [EOF]
3065 ");
3066
3067 let diff_git_materialized = |from: &str, to: &str| {
3068 work_dir.run_jj([
3069 "diff",
3070 "--git",
3071 "--context=1",
3072 &format!("--from={from}"),
3073 &format!("--to={to}"),
3074 ])
3075 };
3076 let diff_color_words_materialized = |from: &str, to: &str| {
3077 work_dir.run_jj([
3078 "diff",
3079 "--color=always",
3080 "--context=1",
3081 &format!("--from={from}"),
3082 &format!("--to={to}"),
3083 ])
3084 };
3085 let diff_color_words_conflict_pair = |from: &str, to: &str| {
3086 work_dir.run_jj([
3087 "diff",
3088 "--color=always",
3089 "--context=1",
3090 "--config=diff.color-words.conflict=pair",
3091 &format!("--from={from}"),
3092 &format!("--to={to}"),
3093 ])
3094 };
3095
3096 // Diff between conflicts
3097 insta::assert_snapshot!(diff_git_materialized("side1+side2", "side1+side2+side3"), @r#"
3098 diff --git a/file b/file
3099 --- a/file
3100 +++ b/file
3101 @@ -13,2 +13,6 @@
3102 line 4 b.2
3103 +%%%%%%% diff from: rlvkpnrz 07965fa1 "base1"
3104 +\\\\\\\ to: znkkpsqq f73063c9 "side3"
3105 + line 2 base
3106 ++line 3 c.2
3107 >>>>>>> conflict 1 of 1 ends
3108 [EOF]
3109 "#);
3110 insta::assert_snapshot!(diff_color_words_materialized("side1+side2", "side1+side2+side3"), @r#"
3111 Modified conflict in file:
3112 ...
3113  13  13: line 4 b.2
3114  14: %%%%%%% diff from: rlvkpnrz 07965fa1 "base1"
3115  15: \\\\\\\ to: znkkpsqq f73063c9 "side3"
3116  16:  line 2 base
3117  17: +line 3 c.2
3118  14  18: >>>>>>> conflict 1 of 1 ends
3119  15  19: line 5
3120 [EOF]
3121 "#);
3122 insta::assert_snapshot!(diff_color_words_conflict_pair("side1+side2", "side1+side2+side3"), @"
3123 Modified conflict in file:
3124  1  1: line 1
3125 <<<<<<< Modified conflict
3126 +++++++ left side #1 to right side #1
3127 ...
3128 ------- left base #1 to right base #1
3129 ...
3130 +++++++ left side #2 to right side #2
3131 ...
3132 ------- left side #1 to right base #2
3133  2  2: line 2 a.1
3134  3 : line 3 a.2
3135  4  2: line 4 base
3136 +++++++ left side #1 to right side #3
3137  2  2: line 2 a.1
3138  3 : line 3 a.2
3139  4  2: line 4 base
3140  3: line 3 c.2
3141 >>>>>>> Conflict ends
3142  5  5: line 5
3143 [EOF]
3144 ");
3145 }
3146
3147 #[test]
3148 fn test_diff_external_tool() -> TestResult {
3149 let mut test_env = TestEnvironment::default();
3150 let edit_script = test_env.set_up_fake_diff_editor();
3151 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3152 let work_dir = test_env.work_dir("repo");
3153
3154 work_dir.write_file("file1", "foo\n");
3155 work_dir.write_file("file2", "foo\n");
3156 work_dir.run_jj(["new"]).success();
3157 work_dir.remove_file("file1");
3158 work_dir.write_file("file2", "foo\nbar\n");
3159 work_dir.write_file("file3", "foo\n");
3160
3161 // nonzero exit codes should print a warning
3162 std::fs::write(&edit_script, "fail")?;
3163 let output = work_dir.run_jj(["diff", "--config=ui.diff-formatter=fake-diff-editor"]);
3164 let insta_portable_exit_status = {
3165 let mut settings = insta::Settings::clone_current();
3166 settings.add_filter("exit (status|code)", "<exit status>");
3167 settings
3168 };
3169 insta_portable_exit_status.bind(|| {
3170 insta::assert_snapshot!(output, @r"
3171 ------- stderr -------
3172 Warning: Tool exited with <exit status>: 1 (run with --debug to see the exact invocation).
3173 [EOF]
3174 ");
3175 });
3176
3177 // nonzero exit codes should not print a warning if it's an expected exit code
3178 std::fs::write(&edit_script, "fail")?;
3179 let output = work_dir.run_jj([
3180 "diff",
3181 "--tool",
3182 "fake-diff-editor",
3183 "--config=merge-tools.fake-diff-editor.diff-expected-exit-codes=[1]",
3184 ]);
3185 insta::assert_snapshot!(output, @"");
3186
3187 std::fs::write(
3188 &edit_script,
3189 "print-files-before\0print --\0print-files-after",
3190 )?;
3191
3192 // diff without file patterns
3193 insta::assert_snapshot!(work_dir.run_jj(["diff", "--tool=fake-diff-editor"]), @"
3194 file1
3195 file2
3196 --
3197 file2
3198 file3
3199 [EOF]
3200 ");
3201
3202 // diff with unset edit-args
3203 insta::assert_snapshot!(work_dir.run_jj(["diff", "--tool=fake-diff-editor",
3204 "--config=merge-tools.fake-diff-editor.edit-args=[]",
3205 ]), @"
3206 file1
3207 file2
3208 --
3209 file2
3210 file3
3211 [EOF]
3212 ");
3213
3214 // diff with explicitly unset edit-args and diff-args
3215 insta_portable_exit_status.bind(|| {
3216 insta::assert_snapshot!(work_dir.run_jj(["diff", "--tool=fake-diff-editor",
3217 "--config=merge-tools.fake-diff-editor.edit-args=[]",
3218 "--config=merge-tools.fake-diff-editor.diff-args=[]",
3219 ]), @"
3220 ------- stderr -------
3221 Error: The tool `fake-diff-editor` cannot be used for diff formatting
3222 [EOF]
3223 [<exit status>: 2]
3224 ");
3225 });
3226
3227 // diff with file patterns
3228 insta::assert_snapshot!(work_dir.run_jj(["diff", "--tool=fake-diff-editor", "file1"]), @"
3229 file1
3230 --
3231 [EOF]
3232 ");
3233
3234 insta::assert_snapshot!(work_dir.run_jj(["log", "-p", "--tool=fake-diff-editor"]), @"
3235 @ rlvkpnrz [email protected] 2001-02-03 08:05:09 b1e84e17
3236 │ (no description set)
3237 │ file1
3238 │ file2
3239 │ --
3240 │ file2
3241 │ file3
3242 ○ qpvuntsm [email protected] 2001-02-03 08:05:08 74c18ac3
3243 │ (no description set)
3244 │ --
3245 │ file1
3246 │ file2
3247 ◆ zzzzzzzz root() 00000000
3248 --
3249 [EOF]
3250 ");
3251
3252 insta::assert_snapshot!(work_dir.run_jj(["show", "--tool=fake-diff-editor"]), @"
3253 Commit ID: b1e84e171e795eeb9cea971f052a30a21255a0a5
3254 Change ID: rlvkpnrzqnoowoytxnquwvuryrwnrmlp
3255 Author : Test User <[email protected]> (2001-02-03 08:05:09)
3256 Committer: Test User <[email protected]> (2001-02-03 08:05:09)
3257
3258 (no description set)
3259
3260 file1
3261 file2
3262 --
3263 file2
3264 file3
3265 [EOF]
3266 ");
3267
3268 // Enabled by default, looks up the merge-tools table
3269 let config = "--config=ui.diff-formatter=fake-diff-editor";
3270 insta::assert_snapshot!(work_dir.run_jj(["diff", config]), @"
3271 file1
3272 file2
3273 --
3274 file2
3275 file3
3276 [EOF]
3277 ");
3278
3279 // Inlined command arguments
3280 let command_toml = to_toml_value(fake_diff_editor_path());
3281 let config = format!("--config=ui.diff-formatter=[{command_toml}, '$right', '$left']");
3282 insta::assert_snapshot!(work_dir.run_jj(["diff", &config]), @"
3283 file2
3284 file3
3285 --
3286 file1
3287 file2
3288 [EOF]
3289 ");
3290
3291 // Output of external diff tool shouldn't be escaped
3292 std::fs::write(&edit_script, "print \x1b[1;31mred")?;
3293 insta::assert_snapshot!(work_dir.run_jj(["diff", "--color=always", "--tool=fake-diff-editor"]),
3294 @"
3295 red
3296 [EOF]
3297 ");
3298
3299 // Non-zero exit code isn't an error
3300 std::fs::write(&edit_script, "print diff\0fail")?;
3301 let output = work_dir.run_jj(["show", "--tool=fake-diff-editor"]);
3302 insta::assert_snapshot!(output.normalize_stderr_exit_status(), @r"
3303 Commit ID: b1e84e171e795eeb9cea971f052a30a21255a0a5
3304 Change ID: rlvkpnrzqnoowoytxnquwvuryrwnrmlp
3305 Author : Test User <[email protected]> (2001-02-03 08:05:09)
3306 Committer: Test User <[email protected]> (2001-02-03 08:05:09)
3307
3308 (no description set)
3309
3310 diff
3311 [EOF]
3312 ------- stderr -------
3313 Warning: Tool exited with exit status: 1 (run with --debug to see the exact invocation).
3314 [EOF]
3315 ");
3316
3317 // --tool=:builtin shouldn't be ignored
3318 let output = work_dir.run_jj(["diff", "--tool=:builtin"]);
3319 insta::assert_snapshot!(output.strip_stderr_last_line(), @"
3320 ------- stderr -------
3321 Error: Invalid builtin diff format: builtin
3322 [EOF]
3323 [exit status: 2]
3324 ");
3325 Ok(())
3326 }
3327
3328 #[test]
3329 fn test_diff_do_chdir() -> TestResult {
3330 let mut test_env = TestEnvironment::default();
3331 test_env.set_up_fake_echo_merge_tool();
3332 let edit_script = test_env.set_up_fake_diff_editor();
3333 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3334 let work_dir = test_env.work_dir("repo");
3335 work_dir.write_file("file1", "file1\n");
3336
3337 std::fs::write(&edit_script, "print-current-dir")?;
3338 assert_eq!(
3339 work_dir
3340 .run_jj([
3341 "diff",
3342 "--tool=fake-diff-editor",
3343 "--config=merge-tools.fake-diff-editor.diff-do-chdir=false",
3344 ])
3345 .to_string()
3346 .lines()
3347 .next()
3348 .unwrap(),
3349 "$TEST_ENV/repo"
3350 );
3351 assert!(
3352 work_dir
3353 .run_jj(["diff", "--tool=fake-diff-editor"])
3354 .to_string()
3355 .lines()
3356 .next()
3357 .unwrap()
3358 != "$TEST_ENV/repo"
3359 );
3360
3361 insta::assert_snapshot!(work_dir.run_jj(["diff", "--tool=fake-echo"]), @"
3362 left right
3363 [EOF]
3364 ");
3365 insta::assert_snapshot!(work_dir.run_jj(["diff", "--tool=fake-echo",
3366 "--config=merge-tools.fake-echo.diff-invocation-mode=file-by-file"]).normalize_backslash(), @"
3367 left/file1 right/file1
3368 [EOF]
3369 ");
3370 Ok(())
3371 }
3372
3373 #[test]
3374 fn test_diff_external_available_width() {
3375 let test_env = TestEnvironment::default();
3376 test_env.set_up_fake_echo_merge_tool();
3377 test_env.add_config("merge-tools.fake-echo.diff-args = ['$width']");
3378 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3379 let work_dir = test_env.work_dir("repo");
3380 work_dir.write_file("file1", "file1\n");
3381 work_dir.run_jj(["new", "root()"]).success();
3382 work_dir.write_file("file2", "file2\n");
3383
3384 // Directory diff
3385 let output =
3386 work_dir.run_jj_with(|cmd| cmd.args(["diff", "--tool=fake-echo"]).env("COLUMNS", "50"));
3387 insta::assert_snapshot!(output, @"
3388 50
3389 [EOF]
3390 ");
3391
3392 // File-by-file diff
3393 let output = work_dir.run_jj_with(|cmd| {
3394 cmd.args(["diff", "--tool=fake-echo"])
3395 .arg("--config=merge-tools.fake-echo.diff-invocation-mode=file-by-file")
3396 .env("COLUMNS", "50")
3397 });
3398 insta::assert_snapshot!(output, @"
3399 50
3400 [EOF]
3401 ");
3402
3403 // Graph width should be subtracted
3404 let output = work_dir.run_jj_with(|cmd| {
3405 cmd.args(["log", "--tool=fake-echo", "-T''"])
3406 .env("COLUMNS", "50")
3407 });
3408 insta::assert_snapshot!(output, @"
3409 @ 47
3410 │ ○ 45
3411 ├─╯
3412 ◆ 47
3413 [EOF]
3414 ");
3415 }
3416
3417 #[test]
3418 fn test_diff_external_file_by_file_tool() -> TestResult {
3419 let mut test_env = TestEnvironment::default();
3420 let edit_script = test_env.set_up_fake_diff_editor();
3421 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3422 let work_dir = test_env.work_dir("repo");
3423
3424 work_dir.write_file("file1", "file1\n");
3425 work_dir.write_file("file2", "file2\n");
3426 work_dir.run_jj(["new"]).success();
3427 work_dir.remove_file("file1");
3428 work_dir.write_file("file2", "file2\nfile2\n");
3429 work_dir.write_file("file3", "file3\n");
3430 work_dir.write_file("file4", "file1\n");
3431
3432 std::fs::write(
3433 edit_script,
3434 "print ==\0print-files-before\0print --\0print-files-after",
3435 )?;
3436
3437 // Enabled by default, looks up the merge-tools table
3438 let configs: &[_] = &[
3439 "--config=ui.diff-formatter=fake-diff-editor",
3440 "--config=merge-tools.fake-diff-editor.diff-invocation-mode=file-by-file",
3441 ];
3442
3443 // diff without file patterns
3444 insta::assert_snapshot!(work_dir.run_jj_with(|cmd| cmd.arg("diff").args(configs)), @"
3445 ==
3446 file2
3447 --
3448 file2
3449 ==
3450 file3
3451 --
3452 file3
3453 ==
3454 file1
3455 --
3456 file4
3457 [EOF]
3458 ");
3459
3460 // diff with file patterns
3461 insta::assert_snapshot!(
3462 work_dir.run_jj_with(|cmd| cmd.args(["diff", "file1"]).args(configs)), @"
3463 ==
3464 file1
3465 --
3466 file1
3467 [EOF]
3468 ");
3469 insta::assert_snapshot!(
3470 work_dir.run_jj_with(|cmd| cmd.args(["log", "-p"]).args(configs)), @"
3471 @ rlvkpnrz [email protected] 2001-02-03 08:05:09 6e901d9d
3472 │ (no description set)
3473 │ ==
3474 │ file2
3475 │ --
3476 │ file2
3477 │ ==
3478 │ file3
3479 │ --
3480 │ file3
3481 │ ==
3482 │ file1
3483 │ --
3484 │ file4
3485 ○ qpvuntsm [email protected] 2001-02-03 08:05:08 923beb72
3486 │ (no description set)
3487 │ ==
3488 │ file1
3489 │ --
3490 │ file1
3491 │ ==
3492 │ file2
3493 │ --
3494 │ file2
3495 ◆ zzzzzzzz root() 00000000
3496 [EOF]
3497 ");
3498
3499 insta::assert_snapshot!(work_dir.run_jj_with(|cmd| cmd.arg("show").args(configs)), @"
3500 Commit ID: 6e901d9d82f5cdfab386fc2b601186158d12cab7
3501 Change ID: rlvkpnrzqnoowoytxnquwvuryrwnrmlp
3502 Author : Test User <[email protected]> (2001-02-03 08:05:09)
3503 Committer: Test User <[email protected]> (2001-02-03 08:05:09)
3504
3505 (no description set)
3506
3507 ==
3508 file2
3509 --
3510 file2
3511 ==
3512 file3
3513 --
3514 file3
3515 ==
3516 file1
3517 --
3518 file4
3519 [EOF]
3520 ");
3521 Ok(())
3522 }
3523
3524 #[cfg(unix)]
3525 #[test]
3526 fn test_diff_external_tool_symlink() -> TestResult {
3527 let mut test_env = TestEnvironment::default();
3528 let edit_script = test_env.set_up_fake_diff_editor();
3529 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3530 let work_dir = test_env.work_dir("repo");
3531
3532 let external_file_path = test_env.env_root().join("external-file");
3533 std::fs::write(&external_file_path, "")?;
3534 let external_file_permissions = external_file_path.symlink_metadata()?.permissions();
3535
3536 std::os::unix::fs::symlink("non-existent1", work_dir.root().join("dead"))?;
3537 std::os::unix::fs::symlink(&external_file_path, work_dir.root().join("file"))?;
3538 work_dir.run_jj(["new"]).success();
3539 work_dir.remove_file("dead");
3540 std::os::unix::fs::symlink("non-existent2", work_dir.root().join("dead"))?;
3541 work_dir.remove_file("file");
3542 work_dir.write_file("file", "");
3543
3544 std::fs::write(
3545 edit_script,
3546 "print-files-before\0print --\0print-files-after",
3547 )?;
3548
3549 // Shouldn't try to change permission of symlinks
3550 insta::assert_snapshot!(work_dir.run_jj(["diff", "--tool=fake-diff-editor"]), @"
3551 dead
3552 file
3553 --
3554 dead
3555 file
3556 [EOF]
3557 ");
3558
3559 // External file should be intact
3560 assert_eq!(
3561 external_file_path.symlink_metadata()?.permissions(),
3562 external_file_permissions
3563 );
3564 Ok(())
3565 }
3566
3567 #[test]
3568 fn test_diff_external_tool_conflict_marker_style() -> TestResult {
3569 let mut test_env = TestEnvironment::default();
3570 let edit_script = test_env.set_up_fake_diff_editor();
3571 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3572 let work_dir = test_env.work_dir("repo");
3573 let file_path = "file";
3574
3575 // Create a conflict
3576 work_dir.write_file(
3577 file_path,
3578 indoc! {"
3579 line 1
3580 line 2
3581 line 3
3582 line 4
3583 line 5
3584 "},
3585 );
3586 work_dir.run_jj(["commit", "-m", "base"]).success();
3587 work_dir.write_file(
3588 file_path,
3589 indoc! {"
3590 line 1
3591 line 2.1
3592 line 2.2
3593 line 3
3594 line 4.1
3595 line 5
3596 "},
3597 );
3598 work_dir.run_jj(["describe", "-m", "side-a"]).success();
3599 work_dir
3600 .run_jj(["new", "subject(base)", "-m", "side-b"])
3601 .success();
3602 work_dir.write_file(
3603 file_path,
3604 indoc! {"
3605 line 1
3606 line 2.3
3607 line 3
3608 line 4.2
3609 line 4.3
3610 line 5
3611 "},
3612 );
3613
3614 // Resolve one of the conflicts in the working copy
3615 work_dir
3616 .run_jj(["new", "subject(side-a)", "subject(side-b)"])
3617 .success();
3618 work_dir.write_file(
3619 file_path,
3620 indoc! {"
3621 line 1
3622 line 2.1
3623 line 2.2
3624 line 2.3
3625 line 3
3626 <<<<<<<
3627 %%%%%%%
3628 -line 4
3629 +line 4.1
3630 +++++++
3631 line 4.2
3632 line 4.3
3633 >>>>>>>
3634 line 5
3635 "},
3636 );
3637
3638 // Set up diff editor to use "snapshot" conflict markers
3639 test_env.add_config(r#"merge-tools.fake-diff-editor.conflict-marker-style = "snapshot""#);
3640
3641 // We want to see whether the diff is using the correct conflict markers
3642 std::fs::write(
3643 &edit_script,
3644 ["files-before file", "files-after file", "dump file file"].join("\0"),
3645 )?;
3646 let output = work_dir.run_jj(["diff", "--tool", "fake-diff-editor"]);
3647 insta::assert_snapshot!(output, @"");
3648 // Conflicts should render using "snapshot" format
3649 insta::assert_snapshot!(
3650 std::fs::read_to_string(test_env.env_root().join("file"))?, @r#"
3651 line 1
3652 line 2.1
3653 line 2.2
3654 line 2.3
3655 line 3
3656 <<<<<<< conflict 1 of 1
3657 +++++++ rlvkpnrz 74e448a1 "side-a"
3658 line 4.1
3659 ------- qpvuntsm 9bd2e004 "base"
3660 line 4
3661 +++++++ zsuskuln 6982bce7 "side-b"
3662 line 4.2
3663 line 4.3
3664 >>>>>>> conflict 1 of 1 ends
3665 line 5
3666 "#);
3667 Ok(())
3668 }
3669
3670 #[test]
3671 fn test_diff_stat() {
3672 let test_env = TestEnvironment::default();
3673 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3674 let work_dir = test_env.work_dir("repo");
3675 work_dir.write_file("file1", "foo\n");
3676
3677 let output = work_dir.run_jj(["diff", "--stat"]);
3678 insta::assert_snapshot!(output, @"
3679 file1 | 1 +
3680 1 file changed, 1 insertion(+), 0 deletions(-)
3681 [EOF]
3682 ");
3683
3684 work_dir.run_jj(["new"]).success();
3685
3686 let output = work_dir.run_jj(["diff", "--stat"]);
3687 insta::assert_snapshot!(output, @"
3688 0 files changed, 0 insertions(+), 0 deletions(-)
3689 [EOF]
3690 ");
3691
3692 work_dir.write_file("file1", "foo\nbar\n");
3693 work_dir.run_jj(["new"]).success();
3694 work_dir.write_file("file1", "bar\n");
3695
3696 let output = work_dir.run_jj(["diff", "--stat"]);
3697 insta::assert_snapshot!(output, @"
3698 file1 | 1 -
3699 1 file changed, 0 insertions(+), 1 deletion(-)
3700 [EOF]
3701 ");
3702 }
3703
3704 #[test]
3705 fn test_diff_stat_long_name_or_stat() {
3706 let mut test_env = TestEnvironment::default();
3707 test_env.add_env_var("COLUMNS", "30");
3708 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3709 let work_dir = test_env.work_dir("repo");
3710
3711 let get_stat = |work_dir: &TestWorkDir, path_length: usize, stat_size: usize| {
3712 work_dir.run_jj(["new", "root()"]).success();
3713 let ascii_name = "1234567890".chars().cycle().take(path_length).join("");
3714 let han_name = "一二三四五六七八九十"
3715 .chars()
3716 .cycle()
3717 .take(path_length)
3718 .join("");
3719 let content = "content line\n".repeat(stat_size);
3720 work_dir.write_file(ascii_name, &content);
3721 work_dir.write_file(han_name, &content);
3722 work_dir.run_jj(["diff", "--stat"])
3723 };
3724
3725 insta::assert_snapshot!(get_stat(&work_dir, 1, 1), @"
3726 1 | 1 +
3727 一 | 1 +
3728 2 files changed, 2 insertions(+), 0 deletions(-)
3729 [EOF]
3730 ");
3731 insta::assert_snapshot!(get_stat(&work_dir, 1, 10), @"
3732 1 | 10 ++++++++++
3733 一 | 10 ++++++++++
3734 2 files changed, 20 insertions(+), 0 deletions(-)
3735 [EOF]
3736 ");
3737 // 30 column display width means right edge is
3738 // ... here ->|
3739 insta::assert_snapshot!(get_stat(&work_dir, 1, 100), @"
3740 1 | 100 +++++++++++++++++++++
3741 一 | 100 +++++++++++++++++++++
3742 2 files changed, 200 insertions(+), 0 deletions(-)
3743 [EOF]
3744 ");
3745 insta::assert_snapshot!(get_stat(&work_dir, 10, 1), @"
3746 1234567890 | 1 +
3747 ...四五六七八九十 | 1 +
3748 2 files changed, 2 insertions(+), 0 deletions(-)
3749 [EOF]
3750 ");
3751 insta::assert_snapshot!(get_stat(&work_dir, 10, 10), @"
3752 1234567890 | 10 ++++++++
3753 ...五六七八九十 | 10 ++++++++
3754 2 files changed, 20 insertions(+), 0 deletions(-)
3755 [EOF]
3756 ");
3757 insta::assert_snapshot!(get_stat(&work_dir, 10, 100), @"
3758 1234567890 | 100 +++++++
3759 ...五六七八九十 | 100 +++++++
3760 2 files changed, 200 insertions(+), 0 deletions(-)
3761 [EOF]
3762 ");
3763 insta::assert_snapshot!(get_stat(&work_dir, 50, 1), @"
3764 ...78901234567890 | 1 +
3765 ...四五六七八九十 | 1 +
3766 2 files changed, 2 insertions(+), 0 deletions(-)
3767 [EOF]
3768 ");
3769 insta::assert_snapshot!(get_stat(&work_dir, 50, 10), @"
3770 ...8901234567890 | 10 ++++++++
3771 ...五六七八九十 | 10 ++++++++
3772 2 files changed, 20 insertions(+), 0 deletions(-)
3773 [EOF]
3774 ");
3775 insta::assert_snapshot!(get_stat(&work_dir, 50, 100), @"
3776 ...8901234567890 | 100 +++++++
3777 ...五六七八九十 | 100 +++++++
3778 2 files changed, 200 insertions(+), 0 deletions(-)
3779 [EOF]
3780 ");
3781
3782 // Lengths around where we introduce the ellipsis
3783 // 30 column display width means right edge is
3784 // ... here ->|
3785 insta::assert_snapshot!(get_stat(&work_dir, 13, 100), @"
3786 1234567890123 | 100 +++++++
3787 ...八九十一二三 | 100 +++++++
3788 2 files changed, 200 insertions(+), 0 deletions(-)
3789 [EOF]
3790 ");
3791 insta::assert_snapshot!(get_stat(&work_dir, 14, 100), @"
3792 12345678901234 | 100 +++++++
3793 ...九十一二三四 | 100 +++++++
3794 2 files changed, 200 insertions(+), 0 deletions(-)
3795 [EOF]
3796 ");
3797 insta::assert_snapshot!(get_stat(&work_dir, 15, 100), @"
3798 123456789012345 | 100 +++++++
3799 ...十一二三四五 | 100 +++++++
3800 2 files changed, 200 insertions(+), 0 deletions(-)
3801 [EOF]
3802 ");
3803 insta::assert_snapshot!(get_stat(&work_dir, 16, 100), @"
3804 1234567890123456 | 100 +++++++
3805 ...一二三四五六 | 100 +++++++
3806 2 files changed, 200 insertions(+), 0 deletions(-)
3807 [EOF]
3808 ");
3809
3810 // Very narrow terminal (doesn't have to fit, just don't crash)
3811 test_env.add_env_var("COLUMNS", "10");
3812 let work_dir = test_env.work_dir("repo");
3813 insta::assert_snapshot!(get_stat(&work_dir, 10, 10), @"
3814 ... | 10 ++
3815 ... | 10 ++
3816 2 files changed, 20 insertions(+), 0 deletions(-)
3817 [EOF]
3818 ");
3819 test_env.add_env_var("COLUMNS", "3");
3820 let work_dir = test_env.work_dir("repo");
3821 insta::assert_snapshot!(get_stat(&work_dir, 10, 10), @"
3822 ... | 10 ++
3823 ... | 10 ++
3824 2 files changed, 20 insertions(+), 0 deletions(-)
3825 [EOF]
3826 ");
3827 insta::assert_snapshot!(get_stat(&work_dir, 3, 10), @"
3828 123 | 10 ++
3829 ... | 10 ++
3830 2 files changed, 20 insertions(+), 0 deletions(-)
3831 [EOF]
3832 ");
3833 insta::assert_snapshot!(get_stat(&work_dir, 1, 10), @"
3834 1 | 10 +++
3835 一 | 10 +++
3836 2 files changed, 20 insertions(+), 0 deletions(-)
3837 [EOF]
3838 ");
3839 }
3840
3841 /// Verify that diff --stat always shows at least one `+` or `-` even when the
3842 /// file is mostly the other.
3843 #[test]
3844 fn test_diff_stat_rounding() {
3845 let mut test_env = TestEnvironment::default();
3846 test_env.add_env_var("COLUMNS", "40");
3847 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3848 let work_dir = test_env.work_dir("repo");
3849
3850 work_dir.write_file("mostly_adds.txt", b"x\n");
3851 work_dir.write_file("mostly_removes.txt", b"x\n".repeat(300));
3852 work_dir.run_jj(["new"]).success();
3853 work_dir.write_file("mostly_adds.txt", b"y\n".repeat(100));
3854 work_dir.write_file("mostly_removes.txt", b"y\n");
3855 work_dir.write_file("only_adds.txt", b"y\n".repeat(10));
3856
3857 let output = work_dir.run_jj(["diff", "--stat"]);
3858 insta::assert_snapshot!(output, @"
3859 mostly_adds.txt | 101 ++++-
3860 mostly_removes.txt | 301 +--------------
3861 only_adds.txt | 10 +
3862 3 files changed, 111 insertions(+), 301 deletions(-)
3863 [EOF]
3864 ");
3865
3866 // very narrow terminal, with both adds and deletes
3867 test_env.add_env_var("COLUMNS", "3");
3868 let work_dir = test_env.work_dir("repo");
3869 let output = work_dir.run_jj(["diff", "--stat"]);
3870 insta::assert_snapshot!(output, @"
3871 .. | 101 +-
3872 .. | 301 +-
3873 .. | 10 +
3874 3 files changed, 111 insertions(+), 301 deletions(-)
3875 [EOF]
3876 ");
3877 }
3878
3879 #[test]
3880 fn test_diff_binary() {
3881 let mut test_env = TestEnvironment::default();
3882 test_env.add_env_var("COLUMNS", "40");
3883 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3884 let work_dir = test_env.work_dir("repo");
3885
3886 work_dir.write_file("binary_removed.png", b"\x89PNG\r\n\x1a\nabcdefg\0");
3887 work_dir.write_file("binary_modified.png", b"\x89PNG\r\n\x1a\n0123456\0");
3888 work_dir.write_file("binary_modified_to_text.png", b"\x89PNG\r\n\x1a\n0123456\0");
3889 work_dir.run_jj(["new"]).success();
3890 work_dir.remove_file("binary_removed.png");
3891 work_dir.write_file("binary_modified.png", b"\x89PNG\r\n\x1a\n012345x\0");
3892 // this file's contents became a valid text file
3893 work_dir.write_file("binary_modified_to_text.png", "foo\nbar\n");
3894 work_dir.write_file("binary_added.png", b"\x89PNG\r\n\x1a\nxyz\0");
3895 // try a file that's valid UTF-8 but contains control characters
3896 work_dir.write_file("binary_valid_utf8.png", b"\0\0\0");
3897
3898 let output = work_dir.run_jj(["diff"]);
3899 insta::assert_snapshot!(output, @"
3900 Added regular file binary_added.png:
3901 (binary)
3902 Modified regular file binary_modified.png:
3903 (binary)
3904 Modified regular file binary_modified_to_text.png:
3905 (binary)
3906 Removed regular file binary_removed.png:
3907 (binary)
3908 Added regular file binary_valid_utf8.png:
3909 (binary)
3910 [EOF]
3911 ");
3912
3913 let output = work_dir.run_jj(["diff", "--git"]);
3914 insta::assert_snapshot!(output, @"
3915 diff --git a/binary_added.png b/binary_added.png
3916 new file mode 100644
3917 index 0000000000..deacfbc286
3918 Binary files /dev/null and b/binary_added.png differ
3919 diff --git a/binary_modified.png b/binary_modified.png
3920 index 7f036ce788..f666e11aeb 100644
3921 Binary files a/binary_modified.png and b/binary_modified.png differ
3922 diff --git a/binary_modified_to_text.png b/binary_modified_to_text.png
3923 index 7f036ce788..3bd1f0e297 100644
3924 Binary files a/binary_modified_to_text.png and b/binary_modified_to_text.png differ
3925 diff --git a/binary_removed.png b/binary_removed.png
3926 deleted file mode 100644
3927 index 2b65b23c22..0000000000
3928 Binary files a/binary_removed.png and /dev/null differ
3929 diff --git a/binary_valid_utf8.png b/binary_valid_utf8.png
3930 new file mode 100644
3931 index 0000000000..4227ca4e87
3932 Binary files /dev/null and b/binary_valid_utf8.png differ
3933 [EOF]
3934 ");
3935
3936 let output = work_dir.run_jj(["diff", "--stat"]);
3937 // Rightmost display column ->|
3938 insta::assert_snapshot!(output, @"
3939 binary_added.png | (binary) +12 bytes
3940 binary_modified.png | (binary)
3941 ...fied_to_text.png | (binary) -8 bytes
3942 binary_removed.png | (binary) -16 bytes
3943 ...y_valid_utf8.png | (binary) +3 bytes
3944 5 files changed, 0 insertions(+), 0 deletions(-)
3945 [EOF]
3946 ");
3947 }
3948
3949 /// Test diff --stat output width for diffs that have different cases of right
3950 /// side text: solely "(binary)", a mixture of text and binary diffs, and binary
3951 /// size changes.
3952 #[test]
3953 fn test_diff_stat_binary_and_text() {
3954 let mut test_env = TestEnvironment::default();
3955 test_env.add_env_var("COLUMNS", "40");
3956 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
3957 let work_dir = test_env.work_dir("repo");
3958
3959 work_dir.write_file("binary_with_elided_long_file_name.png", b"\x001");
3960 work_dir.run_jj(["new"]).success();
3961 work_dir.write_file("binary_with_elided_long_file_name.png", b"\x002");
3962
3963 // Diff with a modified binary file with no change in size.
3964 let output = work_dir.run_jj(["diff", "--stat"]);
3965 // Rightmost display column ->|
3966 insta::assert_snapshot!(output, @"
3967 ..._elided_long_file_name.png | (binary)
3968 1 file changed, 0 insertions(+), 0 deletions(-)
3969 [EOF]
3970 ");
3971
3972 // With a text file included, more space is used for the +++ part.
3973 work_dir.write_file("text_with_elided_long_file_name.txt", b"a\n".repeat(100));
3974 let output = work_dir.run_jj(["diff", "--stat"]);
3975 // Rightmost display column ->|
3976 insta::assert_snapshot!(output, @"
3977 ...d_long_file_name.png | (binary)
3978 ...d_long_file_name.txt | 100 ++++++++++
3979 2 files changed, 100 insertions(+), 0 deletions(-)
3980 [EOF]
3981 ");
3982
3983 // If the binary file size changed, the right side must be wide enough for that
3984 // text.
3985 work_dir.write_file("binary_with_elided_long_file_name.png", b"\x0033");
3986 let output = work_dir.run_jj(["diff", "--stat"]);
3987 // Rightmost display column ->|
3988 insta::assert_snapshot!(output, @"
3989 ...ong_file_name.png | (binary) +1 bytes
3990 ...ong_file_name.txt | 100 +++++++++++++
3991 2 files changed, 100 insertions(+), 0 deletions(-)
3992 [EOF]
3993 ");
3994 }
3995
3996 #[test]
3997 fn test_diff_revisions() {
3998 let test_env = TestEnvironment::default();
3999 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
4000 let work_dir = test_env.work_dir("repo");
4001
4002 //
4003 //
4004 //
4005 // E
4006 // |\
4007 // C D
4008 // |/
4009 // B
4010 // |
4011 // A
4012 create_commit(&work_dir, "A", &[]);
4013 create_commit(&work_dir, "B", &["A"]);
4014 create_commit(&work_dir, "C", &["B"]);
4015 create_commit(&work_dir, "D", &["B"]);
4016 create_commit(&work_dir, "E", &["C", "D"]);
4017
4018 let diff_revisions = |expression: &str| -> CommandOutput {
4019 work_dir.run_jj(["diff", "--name-only", "-r", expression])
4020 };
4021 // Can diff a single revision
4022 insta::assert_snapshot!(diff_revisions("B"), @"
4023 B
4024 [EOF]
4025 ");
4026
4027 // Can diff a merge
4028 insta::assert_snapshot!(diff_revisions("E"), @"
4029 E
4030 [EOF]
4031 ");
4032
4033 // A gap in the range is not allowed (yet at least)
4034 insta::assert_snapshot!(diff_revisions("A|C"), @"
4035 ------- stderr -------
4036 Error: Cannot diff revsets with gaps in.
4037 Hint: Revision 2378873cd201 would need to be in the set.
4038 [EOF]
4039 [exit status: 1]
4040 ");
4041
4042 // A merge into the chain is not allowed
4043 // We could decide to support this case
4044 insta::assert_snapshot!(diff_revisions("C|E"), @"
4045 ------- stderr -------
4046 Error: Cannot diff revsets with gaps in.
4047 Hint: Revision a90b2fff19e9 would need to be in the set.
4048 [EOF]
4049 [exit status: 1]
4050 ");
4051
4052 // Can diff a linear chain
4053 insta::assert_snapshot!(diff_revisions("A::C"), @"
4054 A
4055 B
4056 C
4057 [EOF]
4058 ");
4059
4060 // Can diff a chain with an internal merge
4061 insta::assert_snapshot!(diff_revisions("B::E"), @"
4062 B
4063 C
4064 D
4065 E
4066 [EOF]
4067 ");
4068
4069 // Can diff a set with multiple roots
4070 insta::assert_snapshot!(diff_revisions("C|D|E"), @"
4071 C
4072 D
4073 E
4074 [EOF]
4075 ");
4076
4077 // Can diff a set with multiple heads
4078 insta::assert_snapshot!(diff_revisions("B|C|D"), @"
4079 B
4080 C
4081 D
4082 [EOF]
4083 ");
4084
4085 // Can diff a set with multiple root and multiple heads
4086 insta::assert_snapshot!(diff_revisions("B|C"), @"
4087 B
4088 C
4089 [EOF]
4090 ");
4091 }
4092
4093 #[test]
4094 fn test_diff_rename_in_merge_commit() {
4095 let test_env = TestEnvironment::default();
4096 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
4097 let work_dir = test_env.work_dir("repo");
4098
4099 // A rename in a merge commit is detected once per parent. When the renamed
4100 // source is present in more than one parent, the same copy record is reported
4101 // for each, and those duplicates must not cancel each other out and drop the
4102 // rename. https://github.com/jj-vcs/jj/issues/9752
4103 work_dir.write_file("a.txt", "aaa\n");
4104 work_dir.run_jj(["describe", "-m", "base"]).success();
4105 work_dir.run_jj(["new", "-m", "first branch"]).success();
4106 work_dir
4107 .run_jj(["new", "@-", "-m", "second branch"])
4108 .success();
4109 // Merge both children of "base" (@-+); each holds a.txt, so the rename is
4110 // detected against both parents.
4111 work_dir.run_jj(["new", "@-+", "-m", "merge"]).success();
4112 work_dir.remove_file("a.txt");
4113 work_dir.write_file("b.txt", "aaa\n");
4114
4115 let output = work_dir.run_jj(["diff", "-s"]);
4116 insta::assert_snapshot!(output, @"
4117 R {a.txt => b.txt}
4118 [EOF]
4119 ");
4120 }
4121
4122 #[test]
4123 fn test_diff_stat_max_bar_width() {
4124 let test_env = TestEnvironment::default();
4125 test_env.add_config(
4126 r#"
4127 [diff.stat]
4128 max-bar-width = 10
4129 "#,
4130 );
4131 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
4132 let work_dir = test_env.work_dir("repo");
4133
4134 // A file adding 50 lines should occupy
4135 // all 10 characters of the max bar width
4136 let mut file_contents: String = "\n".repeat(50);
4137 work_dir.write_file("file", file_contents);
4138 let output = work_dir.run_jj(["diff", "--stat"]);
4139 insta::assert_snapshot!(output, @"
4140 file | 50 ++++++++++
4141 1 file changed, 50 insertions(+), 0 deletions(-)
4142 [EOF]
4143 ");
4144 work_dir.run_jj(["new"]).success();
4145 // Modify the last 25 lines for a total of 50 removed+added lines.
4146 // (Modified lines are interpreted as removing and then adding a line.)
4147 // the diff stat should be half + and half -
4148 file_contents = "\n".repeat(25);
4149 file_contents.push_str(&"A\n".repeat(25));
4150 // There is a second file which takes up 20% of the space by lines changed
4151 let file2_contents: String = "\n".repeat(10);
4152 work_dir.write_file("file", file_contents);
4153 work_dir.write_file("file2", file2_contents);
4154 let output = work_dir.run_jj(["diff", "--stat"]);
4155 insta::assert_snapshot!(output, @"
4156 file | 50 +++++-----
4157 file2 | 10 ++
4158 2 files changed, 35 insertions(+), 25 deletions(-)
4159 [EOF]
4160 ");
4161
4162 // Setting the max-bar-width higher than the column space shouldn't overflow
4163 let output = work_dir.run_jj_with(|cmd| {
4164 cmd.args(["diff", "--stat", "--config=diff.stat.max-bar-width=100"])
4165 .env("COLUMNS", "30")
4166 });
4167 insta::assert_snapshot!(output, @"
4168 file | 50 +++++++++----------
4169 file2 | 10 +++
4170 2 files changed, 35 insertions(+), 25 deletions(-)
4171 [EOF]
4172 ");
4173
4174 // Setting max-bar-width=0 should not crash. The current implementation
4175 // cannot reduce the max width below 2. Currently, setting max-bar-width=0
4176 // with the following test writes '++' instead of '+-'.
4177 let output = work_dir
4178 .run_jj_with(|cmd| cmd.args(["diff", "--stat", "--config=diff.stat.max-bar-width=0"]));
4179 insta::assert_snapshot!(output, @"
4180 file | 50 ++
4181 file2 | 10 +
4182 2 files changed, 35 insertions(+), 25 deletions(-)
4183 [EOF]
4184 ");
4185 }