Debian Codegraph @

cli/tests/test_new_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 crate::common::CommandOutput;
16 use crate::common::TestEnvironment;
17 use crate::common::TestWorkDir;
18 use crate::common::create_commit;
19 use crate::common::create_commit_with_files;
20
21 #[test]
22 fn test_new() {
23 let test_env = TestEnvironment::default();
24 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
25 let work_dir = test_env.work_dir("repo");
26
27 work_dir.run_jj(["describe", "-m", "add a file"]).success();
28 work_dir.run_jj(["new", "-m", "a new commit"]).success();
29
30 insta::assert_snapshot!(get_log_output(&work_dir), @"
31 @ 22aec45f30a36a2d244c70e131e369d79e400962 a new commit
32 ○ 55eabcc47301440da7a71d5610d3db021d1925ca add a file
33 ◆ 0000000000000000000000000000000000000000
34 [EOF]
35 ");
36
37 // Start a new change off of a specific commit (the root commit in this case).
38 work_dir
39 .run_jj(["new", "-m", "off of root", "root()"])
40 .success();
41 insta::assert_snapshot!(get_log_output(&work_dir), @"
42 @ 8818c9ee28d00667cb3072a2114a67619ded7ceb off of root
43 │ ○ 22aec45f30a36a2d244c70e131e369d79e400962 a new commit
44 │ ○ 55eabcc47301440da7a71d5610d3db021d1925ca add a file
45 ├─╯
46 ◆ 0000000000000000000000000000000000000000
47 [EOF]
48 ");
49
50 // --edit is a no-op
51 work_dir
52 .run_jj(["new", "--edit", "-m", "yet another commit"])
53 .success();
54 insta::assert_snapshot!(get_log_output(&work_dir), @"
55 @ 9629f035563a7d9fa86becc783ae71557bd25269 yet another commit
56 ○ 8818c9ee28d00667cb3072a2114a67619ded7ceb off of root
57 │ ○ 22aec45f30a36a2d244c70e131e369d79e400962 a new commit
58 │ ○ 55eabcc47301440da7a71d5610d3db021d1925ca add a file
59 ├─╯
60 ◆ 0000000000000000000000000000000000000000
61 [EOF]
62 ");
63
64 // --edit cannot be used with --no-edit
65 let output = work_dir.run_jj(["new", "--edit", "B", "--no-edit", "D"]);
66 insta::assert_snapshot!(output, @"
67 ------- stderr -------
68 error: the argument '--edit' cannot be used with '--no-edit'
69
70 Usage: jj new <REVSETS>...
71
72 For more information, try '--help'.
73 [EOF]
74 [exit status: 2]
75 ");
76 }
77
78 #[test]
79 fn test_new_merge() {
80 let test_env = TestEnvironment::default();
81 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
82 let work_dir = test_env.work_dir("repo");
83
84 work_dir
85 .run_jj(["bookmark", "create", "-r@", "main"])
86 .success();
87 work_dir.run_jj(["describe", "-m", "add file1"]).success();
88 work_dir.write_file("file1", "a");
89 work_dir
90 .run_jj(["new", "root()", "-m", "add file2"])
91 .success();
92 work_dir.write_file("file2", "b");
93 work_dir.run_jj(["debug", "snapshot"]).success();
94 let setup_opid = work_dir.current_operation_id();
95
96 // Create a merge commit
97 work_dir.run_jj(["new", "main", "@"]).success();
98 insta::assert_snapshot!(get_log_output(&work_dir), @"
99 @ 94ce38ef81dc7912c1574cc5aa2f434b9057d58a
100 ├─╮
101 │ ○ 5bf404a038660799fae348cc31b9891349c128c1 add file2
102 ○ │ 96ab002e5b86c39a661adc0524df211a3dac3f1b add file1
103 ├─╯
104 ◆ 0000000000000000000000000000000000000000
105 [EOF]
106 ");
107 let output = work_dir.run_jj(["file", "show", "file1"]);
108 insta::assert_snapshot!(output, @"a[EOF]");
109 let output = work_dir.run_jj(["file", "show", "file2"]);
110 insta::assert_snapshot!(output, @"b[EOF]");
111
112 // Same test with `--no-edit`
113 work_dir.run_jj(["op", "restore", &setup_opid]).success();
114 let output = work_dir.run_jj(["new", "main", "@", "--no-edit"]);
115 insta::assert_snapshot!(output, @"
116 ------- stderr -------
117 Created new commit kpqxywon 061f4210 (empty) (no description set)
118 [EOF]
119 ");
120 insta::assert_snapshot!(get_log_output(&work_dir), @"
121 ○ 061f42107e030034242b424264f22985429552c1
122 ├─╮
123 │ @ 5bf404a038660799fae348cc31b9891349c128c1 add file2
124 ○ │ 96ab002e5b86c39a661adc0524df211a3dac3f1b add file1
125 ├─╯
126 ◆ 0000000000000000000000000000000000000000
127 [EOF]
128 ");
129
130 // Same test with `jj new`
131 work_dir.run_jj(["op", "restore", &setup_opid]).success();
132 work_dir.run_jj(["new", "main", "@"]).success();
133 insta::assert_snapshot!(get_log_output(&work_dir), @"
134 @ cee60a55c085ff349af7fa1e7d6b7d4b7bdd4c3a
135 ├─╮
136 │ ○ 5bf404a038660799fae348cc31b9891349c128c1 add file2
137 ○ │ 96ab002e5b86c39a661adc0524df211a3dac3f1b add file1
138 ├─╯
139 ◆ 0000000000000000000000000000000000000000
140 [EOF]
141 ");
142
143 // merge with non-unique revisions
144 let output = work_dir.run_jj(["new", "@", "3a44e"]);
145 insta::assert_snapshot!(output, @"
146 ------- stderr -------
147 Error: Revision `3a44e` doesn't exist
148 [EOF]
149 [exit status: 1]
150 ");
151 // duplicates are allowed
152 let output = work_dir.run_jj(["new", "@", "visible_heads()"]);
153 insta::assert_snapshot!(output, @"
154 ------- stderr -------
155 Working copy (@) now at: uyznsvlq 68a7f50c (empty) (no description set)
156 Parent commit (@-) : lylxulpl cee60a55 (empty) (no description set)
157 [EOF]
158 ");
159
160 // merge with root
161 let output = work_dir.run_jj(["new", "@", "root()"]);
162 insta::assert_snapshot!(output, @"
163 ------- stderr -------
164 Error: The Git backend does not support creating merge commits with the root commit as one of the parents.
165 [EOF]
166 [exit status: 1]
167 ");
168 }
169
170 #[test]
171 fn test_new_merge_parents_order() {
172 let test_env = TestEnvironment::default();
173 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
174 let work_dir = test_env.work_dir("repo");
175
176 create_commit(&work_dir, "1", &[]);
177 create_commit(&work_dir, "2", &[]);
178 create_commit(&work_dir, "3", &[]);
179 create_commit(&work_dir, "4", &[]);
180 create_commit(&work_dir, "5", &[]);
181
182 // The order of positional and -r/-o args should be preserved
183 work_dir
184 .run_jj([
185 "new",
186 "-osubject(2)",
187 "subject(3)",
188 "-rsubject(1)",
189 "-dsubject(5)",
190 "subject(4)",
191 ])
192 .success();
193 insta::assert_snapshot!(get_log_output(&work_dir), @"
194 @ 2034dc93c6ddad404d1aa677ab99b0621d8e109d
195 ├─┬─┬─┬─╮
196 │ │ │ │ ○ de5bab19f679c52a021c343b8942ca875ec6aae7 4
197 │ │ │ ○ │ de3c6b2c8e065351203063817ef0794df1adb2f9 5
198 │ │ │ ├─╯
199 │ │ ○ │ 1783ad0eb5c21d958b2e1bf8caab9b019f3d16e9 1
200 │ │ ├─╯
201 │ ○ │ fb047eea4c2bec04e2fd3cd21eca0193d5720341 3
202 │ ├─╯
203 ○ │ 36b1b4840ac943a08e7f953df4eadc53e825f2a6 2
204 ├─╯
205 ◆ 0000000000000000000000000000000000000000
206 [EOF]
207 ");
208 }
209
210 #[test]
211 fn test_new_merge_conflicts() {
212 let test_env = TestEnvironment::default();
213 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
214 let work_dir = test_env.work_dir("repo");
215
216 create_commit_with_files(&work_dir, "1", &[], &[("file", "1a\n1b\n")]);
217 create_commit_with_files(&work_dir, "2", &["1"], &[("file", "1a 2a\n1b\n2c\n")]);
218 create_commit_with_files(&work_dir, "3", &["1"], &[("file", "3a 1a\n1b\n")]);
219
220 // merge line by line by default
221 let output = work_dir.run_jj(["new", "2|3"]);
222 insta::assert_snapshot!(output, @"
223 ------- stderr -------
224 Working copy (@) now at: vruxwmqv 4665f78e (conflict) (empty) (no description set)
225 Parent commit (@-) : royxmykx 1b282e07 3 | 3
226 Parent commit (@-) : zsuskuln 7ac709e5 2 | 2
227 Added 0 files, modified 1 files, removed 0 files
228 Warning: There are unresolved conflicts at these paths:
229 file 2-sided conflict
230 [EOF]
231 ");
232 insta::assert_snapshot!(work_dir.read_file("file"), @r#"
233 <<<<<<< conflict 1 of 1
234 %%%%%%% diff from: rlvkpnrz a93ed0a5 "1"
235 \\\\\\\ to: royxmykx 1b282e07 "3"
236 -1a
237 +3a 1a
238 +++++++ zsuskuln 7ac709e5 "2"
239 1a 2a
240 >>>>>>> conflict 1 of 1 ends
241 1b
242 2c
243 "#);
244
245 // reset working copy
246 work_dir.run_jj(["new", "root()"]).success();
247
248 // merge word by word
249 let output = work_dir.run_jj(["new", "2|3", "--config=merge.hunk-level=word"]);
250 insta::assert_snapshot!(output, @"
251 ------- stderr -------
252 Working copy (@) now at: znkkpsqq 892ac90f (empty) (no description set)
253 Parent commit (@-) : royxmykx 1b282e07 3 | 3
254 Parent commit (@-) : zsuskuln 7ac709e5 2 | 2
255 Added 1 files, modified 0 files, removed 0 files
256 [EOF]
257 ");
258 insta::assert_snapshot!(work_dir.read_file("file"), @"
259 3a 1a 2a
260 1b
261 2c
262 ");
263 }
264
265 #[test]
266 fn test_new_merge_same_change() {
267 let test_env = TestEnvironment::default();
268 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
269 let work_dir = test_env.work_dir("repo");
270
271 create_commit_with_files(&work_dir, "1", &[], &[("file", "a\n")]);
272 create_commit_with_files(&work_dir, "2", &["1"], &[("file", "a\nb\n")]);
273 create_commit_with_files(&work_dir, "3", &["1"], &[("file", "a\nb\n")]);
274
275 // same-change conflict is resolved by default
276 let output = work_dir.run_jj(["new", "2|3"]);
277 insta::assert_snapshot!(output, @"
278 ------- stderr -------
279 Working copy (@) now at: vruxwmqv 7bebf0fe (empty) (no description set)
280 Parent commit (@-) : royxmykx 1b9fe696 3 | 3
281 Parent commit (@-) : zsuskuln 829e1e90 2 | 2
282 [EOF]
283 ");
284 insta::assert_snapshot!(work_dir.read_file("file"), @"
285 a
286 b
287 ");
288
289 // reset working copy
290 work_dir.run_jj(["new", "root()"]).success();
291
292 // keep same-change conflict
293 let output = work_dir.run_jj(["new", "2|3", "--config=merge.same-change=keep"]);
294 insta::assert_snapshot!(output, @"
295 ------- stderr -------
296 Working copy (@) now at: znkkpsqq 7be86433 (conflict) (empty) (no description set)
297 Parent commit (@-) : royxmykx 1b9fe696 3 | 3
298 Parent commit (@-) : zsuskuln 829e1e90 2 | 2
299 Added 1 files, modified 0 files, removed 0 files
300 Warning: There are unresolved conflicts at these paths:
301 file 2-sided conflict
302 [EOF]
303 ");
304 insta::assert_snapshot!(work_dir.read_file("file"), @r#"
305 a
306 <<<<<<< conflict 1 of 1
307 %%%%%%% diff from: rlvkpnrz 2adf972b "1"
308 \\\\\\\ to: royxmykx 1b9fe696 "3"
309 +b
310 +++++++ zsuskuln 829e1e90 "2"
311 b
312 >>>>>>> conflict 1 of 1 ends
313 "#);
314 }
315
316 #[test]
317 fn test_new_description_template() {
318 let test_env = TestEnvironment::default();
319 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
320 let work_dir = test_env.work_dir("repo");
321
322 test_env.add_config(r#"templates.new_description = '"custom default\n"'"#);
323
324 let output = work_dir.run_jj(["new"]);
325 insta::assert_snapshot!(output, @"
326 ------- stderr -------
327 Working copy (@) now at: rlvkpnrz 0cdaf03d (empty) custom default
328 Parent commit (@-) : qpvuntsm e8849ae1 (empty) (no description set)
329 [EOF]
330 ");
331
332 let output = work_dir.run_jj(["new", "-m", "explicit message"]);
333 insta::assert_snapshot!(output, @"
334 ------- stderr -------
335 Working copy (@) now at: kkmpptxz ccc7aea5 (empty) explicit message
336 Parent commit (@-) : rlvkpnrz 0cdaf03d (empty) custom default
337 [EOF]
338 ");
339
340 test_env.add_config(r#"templates.new_description = '""'"#);
341 let output = work_dir.run_jj(["new"]);
342 insta::assert_snapshot!(output, @"
343 ------- stderr -------
344 Working copy (@) now at: zsuskuln 49261437 (empty) (no description set)
345 Parent commit (@-) : kkmpptxz ccc7aea5 (empty) explicit message
346 [EOF]
347 ");
348
349 // Test that template can access commit properties
350 test_env.add_config(r#"templates.new_description = '"parents: " ++ parents.len() ++ "\n"'"#);
351 let output = work_dir.run_jj(["new"]);
352 insta::assert_snapshot!(output, @r"
353 ------- stderr -------
354 Working copy (@) now at: mzvwutvl 10155342 (empty) parents: 1
355 Parent commit (@-) : zsuskuln 49261437 (empty) (no description set)
356 [EOF]
357 ");
358 }
359
360 #[test]
361 fn test_new_insert_after() {
362 let test_env = TestEnvironment::default();
363 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
364 let work_dir = test_env.work_dir("repo");
365 setup_before_insertion(&work_dir);
366 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
367 @ F
368 ├─╮
369 │ ○ E
370 ○ │ D
371 ├─╯
372 │ ○ C
373 │ ○ B
374 │ ○ A
375 ├─╯
376 ◆ root
377 [EOF]
378 ");
379
380 // --insert-after can be repeated; --after is an alias
381 let output = work_dir.run_jj(["new", "-m", "G", "--insert-after", "B", "--after", "D"]);
382 insta::assert_snapshot!(output, @r"
383 ------- stderr -------
384 Rebased 2 descendant commits.
385 Working copy (@) now at: kxryzmor 57acfedf (empty) G
386 Parent commit (@-) : kkmpptxz bb98b010 B | (empty) B
387 Parent commit (@-) : vruxwmqv 521674f5 D | (empty) D
388 [EOF]
389 ");
390 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
391 ○ C
392 │ ○ F
393 ╭─┤
394 │ ○ E
395 @ │ G
396 ├───╮
397 │ │ ○ D
398 │ ├─╯
399 ○ │ B
400 ○ │ A
401 ├─╯
402 ◆ root
403 [EOF]
404 ");
405
406 // Inserting a new commit should not change the order of its child commits'
407 // parents (i.e. G should have the parents H and D).
408 let output = work_dir.run_jj(["new", "-m", "H", "--insert-after", "B"]);
409 insta::assert_snapshot!(output, @r"
410 ------- stderr -------
411 Rebased 3 descendant commits.
412 Working copy (@) now at: uyznsvlq ec395e2e (empty) H
413 Parent commit (@-) : kkmpptxz bb98b010 B | (empty) B
414 [EOF]
415 ");
416 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
417 ○ C
418 │ ○ F
419 ╭─┤
420 │ ○ E
421 ○ │ G
422 ├───╮
423 │ │ ○ D
424 │ ├─╯
425 @ │ H
426 ○ │ B
427 ○ │ A
428 ├─╯
429 ◆ root
430 [EOF]
431 ");
432
433 // --after cannot be used with revisions
434 let output = work_dir.run_jj(["new", "--after", "B", "D"]);
435 insta::assert_snapshot!(output, @"
436 ------- stderr -------
437 error: the argument '--insert-after <REVSETS>' cannot be used with:
438 [REVSETS]...
439 -o <REVSETS>
440
441 Usage: jj new --insert-after <REVSETS> [REVSETS]...
442
443 For more information, try '--help'.
444 [EOF]
445 [exit status: 2]
446 ");
447 }
448
449 #[test]
450 fn test_new_insert_after_children() {
451 let test_env = TestEnvironment::default();
452 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
453 let work_dir = test_env.work_dir("repo");
454 setup_before_insertion(&work_dir);
455 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
456 @ F
457 ├─╮
458 │ ○ E
459 ○ │ D
460 ├─╯
461 │ ○ C
462 │ ○ B
463 │ ○ A
464 ├─╯
465 ◆ root
466 [EOF]
467 ");
468
469 // Attempting to insert G after A and C errors out due to the cycle created
470 // as A is an ancestor of C.
471 let output = work_dir.run_jj([
472 "new",
473 "-m",
474 "G",
475 "--insert-after",
476 "A",
477 "--insert-after",
478 "C",
479 ]);
480 insta::assert_snapshot!(output, @"
481 ------- stderr -------
482 Error: Refusing to create a loop: commit d32ebe56a293 would be both an ancestor and a descendant of the new commit
483 [EOF]
484 [exit status: 1]
485 ");
486 }
487
488 #[test]
489 fn test_new_insert_before() {
490 let test_env = TestEnvironment::default();
491 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
492 let work_dir = test_env.work_dir("repo");
493 setup_before_insertion(&work_dir);
494 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
495 @ F
496 ├─╮
497 │ ○ E
498 ○ │ D
499 ├─╯
500 │ ○ C
501 │ ○ B
502 │ ○ A
503 ├─╯
504 ◆ root
505 [EOF]
506 ");
507
508 let output = work_dir.run_jj([
509 "new",
510 "-m",
511 "G",
512 "--insert-before",
513 "C",
514 "--insert-before",
515 "F",
516 ]);
517 insta::assert_snapshot!(output, @r"
518 ------- stderr -------
519 Rebased 2 descendant commits.
520 Working copy (@) now at: kxryzmor 2f16c40d (empty) G
521 Parent commit (@-) : kkmpptxz bb98b010 B | (empty) B
522 Parent commit (@-) : vruxwmqv 521674f5 D | (empty) D
523 Parent commit (@-) : znkkpsqq 56a33cd0 E | (empty) E
524 [EOF]
525 ");
526 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
527 ○ F
528 │ ○ C
529 ├─╯
530 @ G
531 ├─┬─╮
532 │ │ ○ E
533 │ ○ │ D
534 │ ├─╯
535 ○ │ B
536 ○ │ A
537 ├─╯
538 ◆ root
539 [EOF]
540 ");
541
542 // --before cannot be used with revisions
543 let output = work_dir.run_jj(["new", "--before", "B", "D"]);
544 insta::assert_snapshot!(output, @"
545 ------- stderr -------
546 error: the argument '--insert-before <REVSETS>' cannot be used with:
547 [REVSETS]...
548 -o <REVSETS>
549
550 Usage: jj new --insert-before <REVSETS> [REVSETS]...
551
552 For more information, try '--help'.
553 [EOF]
554 [exit status: 2]
555 ");
556 }
557
558 #[test]
559 fn test_new_insert_before_root_successors() {
560 let test_env = TestEnvironment::default();
561 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
562 let work_dir = test_env.work_dir("repo");
563 setup_before_insertion(&work_dir);
564 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
565 @ F
566 ├─╮
567 │ ○ E
568 ○ │ D
569 ├─╯
570 │ ○ C
571 │ ○ B
572 │ ○ A
573 ├─╯
574 ◆ root
575 [EOF]
576 ");
577
578 let output = work_dir.run_jj([
579 "new",
580 "-m",
581 "G",
582 "--insert-before",
583 "A",
584 "--insert-before",
585 "D",
586 ]);
587 insta::assert_snapshot!(output, @r"
588 ------- stderr -------
589 Rebased 5 descendant commits.
590 Working copy (@) now at: kxryzmor 8c026b06 (empty) G
591 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
592 [EOF]
593 ");
594 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
595 ○ F
596 ├─╮
597 │ ○ E
598 ○ │ D
599 │ │ ○ C
600 │ │ ○ B
601 │ │ ○ A
602 ├───╯
603 @ │ G
604 ├─╯
605 ◆ root
606 [EOF]
607 ");
608 }
609
610 #[test]
611 fn test_new_insert_before_no_loop() {
612 let test_env = TestEnvironment::default();
613 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
614 let work_dir = test_env.work_dir("repo");
615 setup_before_insertion(&work_dir);
616 let template = r#"commit_id.short() ++ " " ++ if(description, description, "root")"#;
617 let output = work_dir.run_jj(["log", "-T", template]);
618 insta::assert_snapshot!(output, @"
619 @ a8176a8a5348 F
620 ├─╮
621 │ ○ 56a33cd09d90 E
622 ○ │ 521674f591a6 D
623 ├─╯
624 │ ○ d32ebe56a293 C
625 │ ○ bb98b0102ef5 B
626 │ ○ 515354d01f1b A
627 ├─╯
628 ◆ 000000000000 root
629 [EOF]
630 ");
631
632 let output = work_dir.run_jj([
633 "new",
634 "-m",
635 "G",
636 "--insert-before",
637 "A",
638 "--insert-before",
639 "C",
640 ]);
641 insta::assert_snapshot!(output, @"
642 ------- stderr -------
643 Error: Refusing to create a loop: commit bb98b0102ef5 would be both an ancestor and a descendant of the new commit
644 [EOF]
645 [exit status: 1]
646 ");
647 }
648
649 #[test]
650 fn test_new_insert_before_no_root_merge() {
651 let test_env = TestEnvironment::default();
652 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
653 let work_dir = test_env.work_dir("repo");
654 setup_before_insertion(&work_dir);
655 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
656 @ F
657 ├─╮
658 │ ○ E
659 ○ │ D
660 ├─╯
661 │ ○ C
662 │ ○ B
663 │ ○ A
664 ├─╯
665 ◆ root
666 [EOF]
667 ");
668
669 let output = work_dir.run_jj([
670 "new",
671 "-m",
672 "G",
673 "--insert-before",
674 "B",
675 "--insert-before",
676 "D",
677 ]);
678 insta::assert_snapshot!(output, @"
679 ------- stderr -------
680 Error: The Git backend does not support creating merge commits with the root commit as one of the parents.
681 [EOF]
682 [exit status: 1]
683 ");
684 }
685
686 #[test]
687 fn test_new_insert_before_root() {
688 let test_env = TestEnvironment::default();
689 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
690 let work_dir = test_env.work_dir("repo");
691 setup_before_insertion(&work_dir);
692 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
693 @ F
694 ├─╮
695 │ ○ E
696 ○ │ D
697 ├─╯
698 │ ○ C
699 │ ○ B
700 │ ○ A
701 ├─╯
702 ◆ root
703 [EOF]
704 ");
705
706 let output = work_dir.run_jj(["new", "-m", "G", "--insert-before", "root()"]);
707 insta::assert_snapshot!(output, @"
708 ------- stderr -------
709 Error: The root commit 000000000000 is immutable
710 [EOF]
711 [exit status: 1]
712 ");
713 }
714
715 #[test]
716 fn test_new_insert_after_before() {
717 let test_env = TestEnvironment::default();
718 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
719 let work_dir = test_env.work_dir("repo");
720 setup_before_insertion(&work_dir);
721 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
722 @ F
723 ├─╮
724 │ ○ E
725 ○ │ D
726 ├─╯
727 │ ○ C
728 │ ○ B
729 │ ○ A
730 ├─╯
731 ◆ root
732 [EOF]
733 ");
734
735 let output = work_dir.run_jj(["new", "-m", "G", "--after", "C", "--before", "F"]);
736 insta::assert_snapshot!(output, @r"
737 ------- stderr -------
738 Rebased 1 descendant commits.
739 Working copy (@) now at: kxryzmor 55a63f47 (empty) G
740 Parent commit (@-) : mzvwutvl d32ebe56 C | (empty) C
741 [EOF]
742 ");
743 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
744 ○ F
745 ├─┬─╮
746 │ │ @ G
747 │ │ ○ C
748 │ │ ○ B
749 │ │ ○ A
750 │ ○ │ E
751 │ ├─╯
752 ○ │ D
753 ├─╯
754 ◆ root
755 [EOF]
756 ");
757
758 let output = work_dir.run_jj(["new", "-m", "H", "--after", "D", "--before", "B"]);
759 insta::assert_snapshot!(output, @r"
760 ------- stderr -------
761 Rebased 4 descendant commits.
762 Working copy (@) now at: uyznsvlq fd3f1413 (empty) H
763 Parent commit (@-) : vruxwmqv 521674f5 D | (empty) D
764 [EOF]
765 ");
766 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
767 ○ F
768 ├─┬─╮
769 │ │ ○ G
770 │ │ ○ C
771 │ │ ○ B
772 │ │ ├─╮
773 │ │ │ @ H
774 ├─────╯
775 ○ │ │ D
776 │ │ ○ A
777 ├───╯
778 │ ○ E
779 ├─╯
780 ◆ root
781 [EOF]
782 ");
783 }
784
785 #[test]
786 fn test_new_insert_after_before_no_loop() {
787 let test_env = TestEnvironment::default();
788 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
789 let work_dir = test_env.work_dir("repo");
790 setup_before_insertion(&work_dir);
791 let template = r#"commit_id.short() ++ " " ++ if(description, description, "root")"#;
792 let output = work_dir.run_jj(["log", "-T", template]);
793 insta::assert_snapshot!(output, @"
794 @ a8176a8a5348 F
795 ├─╮
796 │ ○ 56a33cd09d90 E
797 ○ │ 521674f591a6 D
798 ├─╯
799 │ ○ d32ebe56a293 C
800 │ ○ bb98b0102ef5 B
801 │ ○ 515354d01f1b A
802 ├─╯
803 ◆ 000000000000 root
804 [EOF]
805 ");
806
807 let output = work_dir.run_jj([
808 "new",
809 "-m",
810 "G",
811 "--insert-before",
812 "A",
813 "--insert-after",
814 "C",
815 ]);
816 insta::assert_snapshot!(output, @"
817 ------- stderr -------
818 Error: Refusing to create a loop: commit d32ebe56a293 would be both an ancestor and a descendant of the new commit
819 [EOF]
820 [exit status: 1]
821 ");
822 }
823
824 #[test]
825 fn test_new_insert_after_empty_before() {
826 let test_env = TestEnvironment::default();
827 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
828 let work_dir = test_env.work_dir("repo");
829 setup_before_insertion(&work_dir);
830 let template = r#"commit_id.short() ++ " " ++ if(description, description, "root")"#;
831 let output = work_dir.run_jj(["log", "-T", template]);
832 insta::assert_snapshot!(output, @"
833 @ a8176a8a5348 F
834 ├─╮
835 │ ○ 56a33cd09d90 E
836 ○ │ 521674f591a6 D
837 ├─╯
838 │ ○ d32ebe56a293 C
839 │ ○ bb98b0102ef5 B
840 │ ○ 515354d01f1b A
841 ├─╯
842 ◆ 000000000000 root
843 [EOF]
844 ");
845
846 let output = work_dir.run_jj(["new", "-mG", "--insert-before=none()"]);
847 insta::assert_snapshot!(output, @"
848 ------- stderr -------
849 Error: No revisions found to use as parent
850 [EOF]
851 [exit status: 1]
852 ");
853
854 let output = work_dir.run_jj(["new", "-mG", "--insert-before=none()", "--insert-after=B"]);
855 insta::assert_snapshot!(output, @"
856 ------- stderr -------
857 Working copy (@) now at: nkmrtpmo d7088f92 (empty) G
858 Parent commit (@-) : kkmpptxz bb98b010 B | (empty) B
859 [EOF]
860 ");
861 insta::assert_snapshot!(get_short_log_output(&work_dir), @"
862 @ G
863 │ ○ C
864 ├─╯
865 ○ B
866 ○ A
867 │ ○ F
868 │ ├─╮
869 │ │ ○ E
870 ├───╯
871 │ ○ D
872 ├─╯
873 ◆ root
874 [EOF]
875 ");
876 }
877
878 #[test]
879 fn test_new_conflicting_bookmarks() {
880 let test_env = TestEnvironment::default();
881 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
882 let work_dir = test_env.work_dir("repo");
883
884 work_dir.run_jj(["describe", "-m", "one"]).success();
885 work_dir.run_jj(["new", "-m", "two", "@-"]).success();
886 work_dir
887 .run_jj(["bookmark", "create", "-r@", "foo"])
888 .success();
889 work_dir
890 .run_jj(["--at-op=@-", "bookmark", "create", "foo", "-rsubject(one)"])
891 .success();
892
893 // Trigger resolution of divergent operations
894 work_dir.run_jj(["st"]).success();
895
896 let output = work_dir.run_jj(["new", "foo"]);
897 insta::assert_snapshot!(output, @"
898 ------- stderr -------
899 Error: Name `foo` is conflicted
900 Hint: Use commit ID to select single revision from: 96948328bc42, 401ea16fc3fe
901 Hint: Use `bookmarks(foo)` to select all revisions
902 Hint: To set which revision the bookmark points to, run `jj bookmark set foo -r <REVISION>`
903 [EOF]
904 [exit status: 1]
905 ");
906 }
907
908 #[test]
909 fn test_new_conflicting_change_ids() {
910 let test_env = TestEnvironment::default();
911 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
912 let work_dir = test_env.work_dir("repo");
913
914 work_dir.run_jj(["describe", "-m", "one"]).success();
915 work_dir
916 .run_jj(["--at-op=@-", "describe", "-m", "two"])
917 .success();
918
919 // Trigger resolution of divergent operations
920 work_dir.run_jj(["st"]).success();
921
922 let output = work_dir.run_jj(["new", "qpvuntsm"]);
923 insta::assert_snapshot!(output, @"
924 ------- stderr -------
925 Error: Change ID `qpvuntsm` is divergent
926 Hint: Use change offset to select single revision: qpvuntsm/0, qpvuntsm/1
927 Hint: Use `change_id(qpvuntsm)` to select all revisions
928 Hint: To abandon unneeded revisions, run `jj abandon <commit_id>`
929 [EOF]
930 [exit status: 1]
931 ");
932 }
933
934 #[test]
935 fn test_new_error_revision_does_not_exist() {
936 let test_env = TestEnvironment::default();
937 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
938 let work_dir = test_env.work_dir("repo");
939
940 work_dir.run_jj(["describe", "-m", "one"]).success();
941 work_dir.run_jj(["new", "-m", "two"]).success();
942
943 let output = work_dir.run_jj(["new", "this"]);
944 insta::assert_snapshot!(output, @"
945 ------- stderr -------
946 Error: Revision `this` doesn't exist
947 [EOF]
948 [exit status: 1]
949 ");
950 }
951
952 #[test]
953 fn test_new_with_trailers() {
954 let test_env = TestEnvironment::default();
955 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
956 let work_dir = test_env.work_dir("repo");
957
958 work_dir.run_jj(["describe", "-m", "one"]).success();
959
960 test_env.add_config(
961 r#"[templates]
962 commit_trailers = '"Signed-off-by: " ++ committer.email()'
963 "#,
964 );
965 work_dir.run_jj(["new", "-m", "two"]).success();
966
967 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
968 insta::assert_snapshot!(output, @"
969 two
970
971 Signed-off-by: [email protected]
972 [EOF]
973 ");
974
975 // new without message has no trailer
976 work_dir.run_jj(["new"]).success();
977
978 let output = work_dir.run_jj(["log", "--no-graph", "-r@", "-Tdescription"]);
979 insta::assert_snapshot!(output, @"");
980 }
981
982 fn setup_before_insertion(work_dir: &TestWorkDir) {
983 work_dir
984 .run_jj(["bookmark", "create", "-r@", "A"])
985 .success();
986 work_dir.run_jj(["commit", "-m", "A"]).success();
987 work_dir
988 .run_jj(["bookmark", "create", "-r@", "B"])
989 .success();
990 work_dir.run_jj(["commit", "-m", "B"]).success();
991 work_dir
992 .run_jj(["bookmark", "create", "-r@", "C"])
993 .success();
994 work_dir.run_jj(["describe", "-m", "C"]).success();
995 work_dir.run_jj(["new", "-m", "D", "root()"]).success();
996 work_dir
997 .run_jj(["bookmark", "create", "-r@", "D"])
998 .success();
999 work_dir.run_jj(["new", "-m", "E", "root()"]).success();
1000 work_dir
1001 .run_jj(["bookmark", "create", "-r@", "E"])
1002 .success();
1003 work_dir.run_jj(["new", "-m", "F", "D", "E"]).success();
1004 work_dir
1005 .run_jj(["bookmark", "create", "-r@", "F"])
1006 .success();
1007 }
1008
1009 #[must_use]
1010 fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
1011 let template = r#"commit_id ++ " " ++ description"#;
1012 work_dir.run_jj(["log", "-T", template])
1013 }
1014
1015 #[must_use]
1016 fn get_short_log_output(work_dir: &TestWorkDir) -> CommandOutput {
1017 let template = r#"if(description, description, "root")"#;
1018 work_dir.run_jj(["log", "-T", template])
1019 }