Debian Codegraph @

cli/tests/test_restore_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 testutils::TestResult;
16
17 use crate::common::CommandOutput;
18 use crate::common::TestEnvironment;
19 use crate::common::TestWorkDir;
20 use crate::common::create_commit_with_files;
21
22 #[test]
23 fn test_restore() {
24 let test_env = TestEnvironment::default();
25 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
26 let work_dir = test_env.work_dir("repo");
27
28 work_dir.write_file("file1", "a\n");
29 work_dir.run_jj(["new"]).success();
30 work_dir.write_file("file2", "b\n");
31 work_dir.run_jj(["new"]).success();
32 work_dir.remove_file("file1");
33 work_dir.write_file("file2", "c\n");
34 work_dir.write_file("file3", "c\n");
35 work_dir.run_jj(["debug", "snapshot"]).success();
36 let setup_opid = work_dir.current_operation_id();
37
38 // There is no `-r` argument
39 let output = work_dir.run_jj(["restore", "-r=@-"]);
40 insta::assert_snapshot!(output, @"
41 ------- stderr -------
42 Error: `jj restore` does not have a `--revision`/`-r` option.
43 Hint: To modify the current revision, use `--from`.
44 Hint: To undo changes in a revision compared to its parents, use `--changes-in`.
45 [EOF]
46 [exit status: 1]
47 ");
48
49 // Restores from parent by default
50 let output = work_dir.run_jj(["restore"]);
51 insta::assert_snapshot!(output, @"
52 ------- stderr -------
53 Working copy (@) now at: kkmpptxz ff7ef1df (empty) (no description set)
54 Parent commit (@-) : rlvkpnrz 1d3e40a3 (no description set)
55 Added 1 files, modified 1 files, removed 1 files
56 [EOF]
57 ");
58 let output = work_dir.run_jj(["diff", "-s"]);
59 insta::assert_snapshot!(output, @"");
60
61 // Can restore another revision from its parents
62 work_dir.run_jj(["op", "restore", &setup_opid]).success();
63 let output = work_dir.run_jj(["diff", "-s", "-r=@-"]);
64 insta::assert_snapshot!(output, @"
65 A file2
66 [EOF]
67 ");
68 let output = work_dir.run_jj(["restore", "-c=@-"]);
69 insta::assert_snapshot!(output, @r"
70 ------- stderr -------
71 Rebased 1 descendant commits.
72 Working copy (@) now at: kkmpptxz faa5911c (conflict) (no description set)
73 Parent commit (@-) : rlvkpnrz 67841e01 (empty) (no description set)
74 Added 0 files, modified 1 files, removed 0 files
75 Warning: There are unresolved conflicts at these paths:
76 file2 2-sided conflict including 1 deletion
77 New conflicts appeared in 1 commits:
78 kkmpptxz faa5911c (conflict) (no description set)
79 Hint: To resolve the conflicts, start by creating a commit on top of
80 the conflicted commit:
81 jj new kkmpptxz
82 Then use `jj resolve`, or edit the conflict markers in the file directly.
83 Once the conflicts are resolved, you can inspect the result with `jj diff`.
84 Then run `jj squash` to move the resolution into the conflicted commit.
85 [EOF]
86 ");
87 let output = work_dir.run_jj(["diff", "-s", "-r=@-"]);
88 insta::assert_snapshot!(output, @"");
89
90 // Can restore this revision from another revision
91 work_dir.run_jj(["op", "restore", &setup_opid]).success();
92 let output = work_dir.run_jj(["restore", "--from", "@--"]);
93 insta::assert_snapshot!(output, @"
94 ------- stderr -------
95 Working copy (@) now at: kkmpptxz 3beda426 (no description set)
96 Parent commit (@-) : rlvkpnrz 1d3e40a3 (no description set)
97 Added 1 files, modified 0 files, removed 2 files
98 [EOF]
99 ");
100 let output = work_dir.run_jj(["diff", "-s"]);
101 insta::assert_snapshot!(output, @"
102 D file2
103 [EOF]
104 ");
105
106 // Can restore into other revision
107 work_dir.run_jj(["op", "restore", &setup_opid]).success();
108 let output = work_dir.run_jj(["restore", "--into", "@-"]);
109 insta::assert_snapshot!(output, @r"
110 ------- stderr -------
111 Rebased 1 descendant commits.
112 Working copy (@) now at: kkmpptxz 5edd8125 (empty) (no description set)
113 Parent commit (@-) : rlvkpnrz e01fe0b9 (no description set)
114 [EOF]
115 ");
116 let output = work_dir.run_jj(["diff", "-s"]);
117 insta::assert_snapshot!(output, @"");
118 let output = work_dir.run_jj(["diff", "-s", "-r", "@-"]);
119 insta::assert_snapshot!(output, @"
120 D file1
121 A file2
122 A file3
123 [EOF]
124 ");
125
126 // Can combine `--from` and `--into`
127 work_dir.run_jj(["op", "restore", &setup_opid]).success();
128 let output = work_dir.run_jj(["restore", "--from", "@", "--into", "@-"]);
129 insta::assert_snapshot!(output, @r"
130 ------- stderr -------
131 Rebased 1 descendant commits.
132 Working copy (@) now at: kkmpptxz 9807d79b (empty) (no description set)
133 Parent commit (@-) : rlvkpnrz f3774db8 (no description set)
134 [EOF]
135 ");
136 let output = work_dir.run_jj(["diff", "-s"]);
137 insta::assert_snapshot!(output, @"");
138 let output = work_dir.run_jj(["diff", "-s", "-r", "@-"]);
139 insta::assert_snapshot!(output, @"
140 D file1
141 A file2
142 A file3
143 [EOF]
144 ");
145
146 // Can restore only specified paths
147 work_dir.run_jj(["op", "restore", &setup_opid]).success();
148 let output = work_dir.run_jj(["restore", "file2", "file3"]);
149 insta::assert_snapshot!(output, @"
150 ------- stderr -------
151 Working copy (@) now at: kkmpptxz 08b04134 (no description set)
152 Parent commit (@-) : rlvkpnrz 1d3e40a3 (no description set)
153 Added 0 files, modified 1 files, removed 1 files
154 [EOF]
155 ");
156 let output = work_dir.run_jj(["diff", "-s"]);
157 insta::assert_snapshot!(output, @"
158 D file1
159 [EOF]
160 ");
161
162 // The output filtered to a non-existent file should display a warning.
163 let output = work_dir.run_jj(["restore", "nonexistent"]);
164 insta::assert_snapshot!(output, @"
165 ------- stderr -------
166 Warning: No matching entries for paths: nonexistent
167 Nothing changed.
168 [EOF]
169 ");
170 }
171
172 // Much of this test is copied from test_resolve_command
173 #[test]
174 fn test_restore_conflicted_merge() {
175 let test_env = TestEnvironment::default();
176 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
177 let work_dir = test_env.work_dir("repo");
178
179 create_commit_with_files(&work_dir, "base", &[], &[("file", "base\n")]);
180 create_commit_with_files(&work_dir, "a", &["base"], &[("file", "a\n")]);
181 create_commit_with_files(&work_dir, "b", &["base"], &[("file", "b\n")]);
182 create_commit_with_files(&work_dir, "conflict", &["a", "b"], &[]);
183 // Test the setup
184 insta::assert_snapshot!(get_log_output(&work_dir), @"
185 @ conflict
186 ├─╮
187 │ ○ b
188 ○ │ a
189 ├─╯
190 ○ base
191 ◆
192 [EOF]
193 ");
194 insta::assert_snapshot!(work_dir.read_file("file"), @r#"
195 <<<<<<< conflict 1 of 1
196 %%%%%%% diff from: rlvkpnrz 1792382a "base"
197 \\\\\\\ to: zsuskuln 45537d53 "a"
198 -base
199 +a
200 +++++++ royxmykx 89d1b299 "b"
201 b
202 >>>>>>> conflict 1 of 1 ends
203 "#);
204
205 // Overwrite the file...
206 work_dir.write_file("file", "resolution");
207 insta::assert_snapshot!(work_dir.run_jj(["diff"]), @r#"
208 Resolved conflict in file:
209 1 : <<<<<<< conflict 1 of 1
210 2 : %%%%%%% diff from: rlvkpnrz 1792382a "base"
211 3 : \\\\\\\ to: zsuskuln 45537d53 "a"
212 4 : -base
213 5 : +a
214 6 : +++++++ royxmykx 89d1b299 "b"
215 7 : b
216 8 : >>>>>>> conflict 1 of 1 ends
217 1: resolution
218 [EOF]
219 "#);
220
221 // ...and restore it back again.
222 let output = work_dir.run_jj(["restore", "file"]);
223 insta::assert_snapshot!(output, @"
224 ------- stderr -------
225 Working copy (@) now at: vruxwmqv 7efcd5f8 conflict | (conflict) (empty) conflict
226 Parent commit (@-) : zsuskuln 45537d53 a | a
227 Parent commit (@-) : royxmykx 89d1b299 b | b
228 Added 0 files, modified 1 files, removed 0 files
229 Warning: There are unresolved conflicts at these paths:
230 file 2-sided conflict
231 [EOF]
232 ");
233 insta::assert_snapshot!(work_dir.read_file("file"), @r#"
234 <<<<<<< conflict 1 of 1
235 %%%%%%% diff from: rlvkpnrz 1792382a "base"
236 \\\\\\\ to: zsuskuln 45537d53 "a"
237 -base
238 +a
239 +++++++ royxmykx 89d1b299 "b"
240 b
241 >>>>>>> conflict 1 of 1 ends
242 "#);
243 let output = work_dir.run_jj(["diff"]);
244 insta::assert_snapshot!(output, @"");
245
246 // The same, but without the `file` argument. Overwrite the file...
247 work_dir.write_file("file", "resolution");
248 insta::assert_snapshot!(work_dir.run_jj(["diff"]), @r#"
249 Resolved conflict in file:
250 1 : <<<<<<< conflict 1 of 1
251 2 : %%%%%%% diff from: rlvkpnrz 1792382a "base"
252 3 : \\\\\\\ to: zsuskuln 45537d53 "a"
253 4 : -base
254 5 : +a
255 6 : +++++++ royxmykx 89d1b299 "b"
256 7 : b
257 8 : >>>>>>> conflict 1 of 1 ends
258 1: resolution
259 [EOF]
260 "#);
261
262 // ... and restore it back again.
263 let output = work_dir.run_jj(["restore"]);
264 insta::assert_snapshot!(output, @"
265 ------- stderr -------
266 Working copy (@) now at: vruxwmqv e0c3b307 conflict | (conflict) (empty) conflict
267 Parent commit (@-) : zsuskuln 45537d53 a | a
268 Parent commit (@-) : royxmykx 89d1b299 b | b
269 Added 0 files, modified 1 files, removed 0 files
270 Warning: There are unresolved conflicts at these paths:
271 file 2-sided conflict
272 [EOF]
273 ");
274 insta::assert_snapshot!(work_dir.read_file("file"), @r#"
275 <<<<<<< conflict 1 of 1
276 %%%%%%% diff from: rlvkpnrz 1792382a "base"
277 \\\\\\\ to: zsuskuln 45537d53 "a"
278 -base
279 +a
280 +++++++ royxmykx 89d1b299 "b"
281 b
282 >>>>>>> conflict 1 of 1 ends
283 "#);
284 }
285
286 #[test]
287 fn test_restore_restore_descendants() {
288 let test_env = TestEnvironment::default();
289 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
290 let work_dir = test_env.work_dir("repo");
291
292 create_commit_with_files(&work_dir, "base", &[], &[("file", "base\n")]);
293 create_commit_with_files(&work_dir, "a", &["base"], &[("file", "a\n")]);
294 create_commit_with_files(
295 &work_dir,
296 "b",
297 &["base"],
298 &[("file", "b\n"), ("file2", "b\n")],
299 );
300 create_commit_with_files(&work_dir, "ab", &["a", "b"], &[("file", "ab\n")]);
301 // Test the setup
302 insta::assert_snapshot!(get_log_output(&work_dir), @"
303 @ ab
304 ├─╮
305 │ ○ b
306 ○ │ a
307 ├─╯
308 ○ base
309 ◆
310 [EOF]
311 ");
312 insta::assert_snapshot!(work_dir.read_file("file"), @"ab");
313
314 // Commit "b" was not supposed to modify "file", restore it from its parent
315 // while preserving its child commit content.
316 let output = work_dir.run_jj(["restore", "-c", "b", "file", "--restore-descendants"]);
317 insta::assert_snapshot!(output, @r"
318 ------- stderr -------
319 Rebased 1 descendant commits (while preserving their content).
320 Working copy (@) now at: vruxwmqv 14c0c336 ab | ab
321 Parent commit (@-) : zsuskuln 45537d53 a | a
322 Parent commit (@-) : royxmykx 5fd3f8c5 b | b
323 [EOF]
324 ");
325
326 // Check that "a", "b", and "ab" have their expected content by diffing them.
327 // "ab" must have kept its content.
328 insta::assert_snapshot!(work_dir.run_jj(["diff", "--from=a", "--to=ab", "--git"]), @"
329 diff --git a/file b/file
330 index 7898192261..81bf396956 100644
331 --- a/file
332 +++ b/file
333 @@ -1,1 +1,1 @@
334 -a
335 +ab
336 diff --git a/file2 b/file2
337 new file mode 100644
338 index 0000000000..6178079822
339 --- /dev/null
340 +++ b/file2
341 @@ -0,0 +1,1 @@
342 +b
343 [EOF]
344 ");
345 insta::assert_snapshot!(work_dir.run_jj(["diff", "--from=b", "--to=ab", "--git"]), @"
346 diff --git a/file b/file
347 index df967b96a5..81bf396956 100644
348 --- a/file
349 +++ b/file
350 @@ -1,1 +1,1 @@
351 -base
352 +ab
353 [EOF]
354 ");
355 }
356
357 #[test]
358 fn test_restore_interactive() -> TestResult {
359 let mut test_env = TestEnvironment::default();
360 let diff_editor = test_env.set_up_fake_diff_editor();
361 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
362 let work_dir = test_env.work_dir("repo");
363
364 create_commit_with_files(&work_dir, "a", &[], &[("file1", "a1\n"), ("file2", "a2\n")]);
365 create_commit_with_files(
366 &work_dir,
367 "b",
368 &["a"],
369 &[("file1", "b1\n"), ("file2", "b2\n"), ("file3", "b3\n")],
370 );
371 let output = work_dir.run_jj(["log", "--summary"]);
372 insta::assert_snapshot!(output, @"
373 @ zsuskuln [email protected] 2001-02-03 08:05:11 b 38153274
374 │ b
375 │ M file1
376 │ M file2
377 │ A file3
378 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 a 6c8d5b87
379 │ a
380 │ A file1
381 │ A file2
382 ◆ zzzzzzzz root() 00000000
383 [EOF]
384 ");
385 let setup_opid = work_dir.current_operation_id();
386
387 let diff_script = [
388 "files-before file1 file2 file3",
389 "files-after JJ-INSTRUCTIONS file1 file2",
390 "reset file2",
391 "dump JJ-INSTRUCTIONS instrs",
392 ]
393 .join("\0");
394 std::fs::write(diff_editor, diff_script)?;
395
396 // Restore file1 and file3
397 let output = work_dir.run_jj(["restore", "-i", "--from=@-"]);
398 insta::assert_snapshot!(output, @"
399 ------- stderr -------
400 Working copy (@) now at: zsuskuln 7cd0a341 b | b
401 Parent commit (@-) : rlvkpnrz 6c8d5b87 a | a
402 Added 0 files, modified 1 files, removed 1 files
403 [EOF]
404 ");
405
406 insta::assert_snapshot!(
407 std::fs::read_to_string(test_env.env_root().join("instrs"))?, @"
408 You are restoring changes from: rlvkpnrz 6c8d5b87 a | a
409 to commit: zsuskuln 38153274 b | b
410
411 The diff initially shows all changes restored. Adjust the right side until it
412 shows the contents you want for the destination commit.
413 ");
414
415 let output = work_dir.run_jj(["log", "--summary"]);
416 insta::assert_snapshot!(output, @"
417 @ zsuskuln [email protected] 2001-02-03 08:05:13 b 7cd0a341
418 │ b
419 │ M file2
420 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 a 6c8d5b87
421 │ a
422 │ A file1
423 │ A file2
424 ◆ zzzzzzzz root() 00000000
425 [EOF]
426 ");
427
428 // Try again with --tool, which should imply --interactive
429 work_dir.run_jj(["op", "restore", &setup_opid]).success();
430 let output = work_dir.run_jj(["restore", "--tool=fake-diff-editor"]);
431 insta::assert_snapshot!(output, @"
432 ------- stderr -------
433 Working copy (@) now at: zsuskuln 0f1263f5 b | b
434 Parent commit (@-) : rlvkpnrz 6c8d5b87 a | a
435 Added 0 files, modified 1 files, removed 1 files
436 [EOF]
437 ");
438
439 let output = work_dir.run_jj(["log", "--summary"]);
440 insta::assert_snapshot!(output, @"
441 @ zsuskuln [email protected] 2001-02-03 08:05:16 b 0f1263f5
442 │ b
443 │ M file2
444 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 a 6c8d5b87
445 │ a
446 │ A file1
447 │ A file2
448 ◆ zzzzzzzz root() 00000000
449 [EOF]
450 ");
451 Ok(())
452 }
453
454 #[test]
455 fn test_restore_interactive_merge() -> TestResult {
456 let mut test_env = TestEnvironment::default();
457 let diff_editor = test_env.set_up_fake_diff_editor();
458 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
459 let work_dir = test_env.work_dir("repo");
460
461 create_commit_with_files(&work_dir, "a", &[], &[("file1", "a1\n")]);
462 create_commit_with_files(&work_dir, "b", &[], &[("file2", "b1\n")]);
463 create_commit_with_files(
464 &work_dir,
465 "c",
466 &["a", "b"],
467 &[("file1", "c1\n"), ("file2", "c2\n"), ("file3", "c3\n")],
468 );
469 let output = work_dir.run_jj(["log", "--summary"]);
470 insta::assert_snapshot!(output, @"
471 @ royxmykx [email protected] 2001-02-03 08:05:13 c e37470c3
472 ├─╮ c
473 │ │ M file1
474 │ │ M file2
475 │ │ A file3
476 │ ○ zsuskuln [email protected] 2001-02-03 08:05:11 b ca7e57cd
477 │ │ b
478 │ │ A file2
479 ○ │ rlvkpnrz [email protected] 2001-02-03 08:05:09 a 78059355
480 ├─╯ a
481 │ A file1
482 ◆ zzzzzzzz root() 00000000
483 [EOF]
484 ");
485
486 let diff_script = [
487 "files-before file1 file2 file3",
488 "files-after JJ-INSTRUCTIONS file1 file2",
489 "reset file2",
490 "dump JJ-INSTRUCTIONS instrs",
491 ]
492 .join("\0");
493 std::fs::write(diff_editor, diff_script)?;
494
495 // Restore file1 and file3
496 let output = work_dir.run_jj(["restore", "-i"]);
497 insta::assert_snapshot!(output, @"
498 ------- stderr -------
499 Working copy (@) now at: royxmykx 196af27b c | c
500 Parent commit (@-) : rlvkpnrz 78059355 a | a
501 Parent commit (@-) : zsuskuln ca7e57cd b | b
502 Added 0 files, modified 1 files, removed 1 files
503 [EOF]
504 ");
505
506 insta::assert_snapshot!(
507 std::fs::read_to_string(test_env.env_root().join("instrs"))?, @"
508 You are restoring changes from: rlvkpnrz 78059355 a | a
509 zsuskuln ca7e57cd b | b
510 to commit: royxmykx e37470c3 c | c
511
512 The diff initially shows all changes restored. Adjust the right side until it
513 shows the contents you want for the destination commit.
514 ");
515
516 let output = work_dir.run_jj(["log", "--summary"]);
517 insta::assert_snapshot!(output, @"
518 @ royxmykx [email protected] 2001-02-03 08:05:15 c 196af27b
519 ├─╮ c
520 │ │ M file2
521 │ ○ zsuskuln [email protected] 2001-02-03 08:05:11 b ca7e57cd
522 │ │ b
523 │ │ A file2
524 ○ │ rlvkpnrz [email protected] 2001-02-03 08:05:09 a 78059355
525 ├─╯ a
526 │ A file1
527 ◆ zzzzzzzz root() 00000000
528 [EOF]
529 ");
530 Ok(())
531 }
532
533 #[test]
534 fn test_restore_interactive_with_paths() -> TestResult {
535 let mut test_env = TestEnvironment::default();
536 let diff_editor = test_env.set_up_fake_diff_editor();
537 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
538 let work_dir = test_env.work_dir("repo");
539
540 create_commit_with_files(&work_dir, "a", &[], &[("file1", "a1\n"), ("file2", "a2\n")]);
541 create_commit_with_files(
542 &work_dir,
543 "b",
544 &["a"],
545 &[("file1", "b1\n"), ("file2", "b2\n"), ("file3", "b3\n")],
546 );
547 let output = work_dir.run_jj(["log", "--summary"]);
548 insta::assert_snapshot!(output, @"
549 @ zsuskuln [email protected] 2001-02-03 08:05:11 b 38153274
550 │ b
551 │ M file1
552 │ M file2
553 │ A file3
554 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 a 6c8d5b87
555 │ a
556 │ A file1
557 │ A file2
558 ◆ zzzzzzzz root() 00000000
559 [EOF]
560 ");
561
562 let diff_script = [
563 "files-before file1 file2",
564 "files-after JJ-INSTRUCTIONS file1 file2",
565 "reset file2",
566 ]
567 .join("\0");
568 std::fs::write(diff_editor, diff_script)?;
569
570 // Restore file1 (file2 is reset by interactive editor)
571 let output = work_dir.run_jj(["restore", "-i", "file1", "file2"]);
572 insta::assert_snapshot!(output, @"
573 ------- stderr -------
574 Working copy (@) now at: zsuskuln 8b2f997d b | b
575 Parent commit (@-) : rlvkpnrz 6c8d5b87 a | a
576 Added 0 files, modified 1 files, removed 0 files
577 [EOF]
578 ");
579
580 let output = work_dir.run_jj(["log", "--summary"]);
581 insta::assert_snapshot!(output, @"
582 @ zsuskuln [email protected] 2001-02-03 08:05:13 b 8b2f997d
583 │ b
584 │ M file2
585 │ A file3
586 ○ rlvkpnrz [email protected] 2001-02-03 08:05:09 a 6c8d5b87
587 │ a
588 │ A file1
589 │ A file2
590 ◆ zzzzzzzz root() 00000000
591 [EOF]
592 ");
593 Ok(())
594 }
595
596 #[must_use]
597 fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
598 work_dir.run_jj(["log", "-T", "bookmarks"])
599 }