Debian Codegraph @

cli/tests/test_workspaces.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 test_case::test_case;
16 use testutils::TestResult;
17 use testutils::git;
18
19 use crate::common::CommandOutput;
20 use crate::common::TestEnvironment;
21 use crate::common::TestWorkDir;
22
23 #[test]
24 fn test_workspaces_invalid_name() {
25 let test_env = TestEnvironment::default();
26 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
27 let main_dir = test_env.work_dir("repo");
28
29 // refuse to create, directory not created
30 let output = main_dir.run_jj(["workspace", "add", "--name", "", "../secondary"]);
31 insta::assert_snapshot!(output, @"
32 ------- stderr -------
33 Error: New workspace name cannot be empty
34 [EOF]
35 [exit status: 1]
36 ");
37 assert!(!test_env.env_root().join("secondary").exists());
38
39 // refuse to rename
40 let output = main_dir.run_jj(["workspace", "rename", ""]);
41 insta::assert_snapshot!(output, @"
42 ------- stderr -------
43 Error: New workspace name cannot be empty
44 [EOF]
45 [exit status: 1]
46 ");
47 }
48
49 /// Test adding a second and a third workspace
50 #[test]
51 fn test_workspaces_add_second_and_third_workspace() {
52 let test_env = TestEnvironment::default();
53 test_env.run_jj_in(".", ["git", "init", "main"]).success();
54 let main_dir = test_env.work_dir("main");
55 let secondary_dir = test_env.work_dir("secondary");
56
57 main_dir.write_file("file", "contents");
58 main_dir.run_jj(["commit", "-m", "initial"]).success();
59
60 let output = main_dir.run_jj(["workspace", "list"]);
61 insta::assert_snapshot!(output.normalize_backslash(), @"
62 default: . rlvkpnrz 504e3d8c (empty) (no description set)
63 [EOF]
64 ");
65
66 let output = main_dir.run_jj(["workspace", "add", "--name", "second", "../secondary"]);
67 insta::assert_snapshot!(output.normalize_backslash(), @r#"
68 ------- stderr -------
69 Created workspace in "../secondary"
70 Working copy (@) now at: rzvqmyuk bcc858e1 (empty) (no description set)
71 Parent commit (@-) : qpvuntsm 7b22a8cb initial
72 Added 1 files, modified 0 files, removed 0 files
73 [EOF]
74 "#);
75
76 // Can see the working-copy commit in each workspace in the log output. The "@"
77 // node in the graph indicates the current workspace's working-copy commit.
78 insta::assert_snapshot!(get_log_output(&main_dir), @r#"
79 @ 504e3d8c1bcd default@
80 │ ○ bcc858e1d93f second@
81 ├─╯
82 ○ 7b22a8cbe888 "initial"
83 ◆ 000000000000
84 [EOF]
85 "#);
86 insta::assert_snapshot!(get_log_output(&secondary_dir), @r#"
87 @ bcc858e1d93f second@
88 │ ○ 504e3d8c1bcd default@
89 ├─╯
90 ○ 7b22a8cbe888 "initial"
91 ◆ 000000000000
92 [EOF]
93 "#);
94
95 // Both workspaces show up when we list them
96 let output = main_dir.run_jj(["workspace", "list"]);
97 insta::assert_snapshot!(output.normalize_backslash(), @"
98 default: . rlvkpnrz 504e3d8c (empty) (no description set)
99 second: ../secondary rzvqmyuk bcc858e1 (empty) (no description set)
100 [EOF]
101 ");
102
103 // Check that a workspace can be created in an existing empty directory
104 main_dir.create_dir("../third");
105 let output = main_dir.run_jj(["workspace", "add", "--name", "third", "../third"]);
106 insta::assert_snapshot!(output.normalize_backslash(), @r#"
107 ------- stderr -------
108 Created workspace in "../third"
109 Working copy (@) now at: nuwvvtmy d55e769c (empty) (no description set)
110 Parent commit (@-) : qpvuntsm 7b22a8cb initial
111 Added 1 files, modified 0 files, removed 0 files
112 [EOF]
113 "#);
114
115 // Duplicate names are not allowed, directory not created
116 let output = main_dir.run_jj(["workspace", "add", "--name", "third", "../tertiary"]);
117 insta::assert_snapshot!(output.normalize_backslash(), @"
118 ------- stderr -------
119 Error: Workspace named 'third' already exists
120 [EOF]
121 [exit status: 1]
122 ");
123 assert!(!test_env.env_root().join("tertiary").exists());
124 }
125
126 #[test]
127 fn test_workspaces_add_with_message() {
128 let test_env = TestEnvironment::default();
129 test_env.run_jj_in(".", ["git", "init", "main"]).success();
130 let main_dir = test_env.work_dir("main");
131
132 main_dir.write_file("file", "contents");
133 main_dir.run_jj(["commit", "-m", "initial"]).success();
134
135 let output = main_dir.run_jj([
136 "workspace",
137 "add",
138 "--name",
139 "second",
140 "-m",
141 "add second workspace",
142 "../secondary",
143 ]);
144
145 // Check that the newly created workspace has a description for the commit
146 insta::assert_snapshot!(output.normalize_backslash(), @r#"
147 ------- stderr -------
148 Created workspace in "../secondary"
149 Working copy (@) now at: pmmvwywv 47a2d9a1 (empty) add second workspace
150 Parent commit (@-) : qpvuntsm 7b22a8cb initial
151 Added 1 files, modified 0 files, removed 0 files
152 [EOF]
153 "#);
154
155 main_dir
156 .run_jj([
157 "workspace",
158 "add",
159 "--name",
160 "third",
161 "-m",
162 "first message",
163 "-m",
164 "second message",
165 "../tertiary",
166 ])
167 .success();
168
169 // Check that multiple messages work as expected like with the "new" command
170 let output = main_dir.run_jj(["log", "-r", "third@", "-Tdescription", "--no-graph"]);
171 insta::assert_snapshot!(output.normalize_backslash(), @"
172 first message
173
174 second message
175 [EOF]
176 ");
177
178 // Test that adding workspace with no message has no trailers
179 test_env.add_config(
180 r#"[templates]
181 commit_trailers = '"Signed-off-by: " ++ committer.email()'
182 "#,
183 );
184
185 let output = main_dir.run_jj(["workspace", "add", "--name", "fourth", "../quaternary"]);
186
187 insta::assert_snapshot!(output.normalize_backslash(), @r#"
188 ------- stderr -------
189 Created workspace in "../quaternary"
190 Working copy (@) now at: nppvrztz 0bfa7004 (empty) (no description set)
191 Parent commit (@-) : qpvuntsm 7b22a8cb initial
192 Added 1 files, modified 0 files, removed 0 files
193 [EOF]
194 "#);
195 }
196
197 /// Test how sparse patterns are inherited
198 #[test]
199 fn test_workspaces_sparse_patterns() {
200 let test_env = TestEnvironment::default();
201 test_env.run_jj_in(".", ["git", "init", "ws1"]).success();
202 let ws1_dir = test_env.work_dir("ws1");
203 let ws2_dir = test_env.work_dir("ws2");
204 let ws3_dir = test_env.work_dir("ws3");
205 let ws4_dir = test_env.work_dir("ws4");
206 let ws5_dir = test_env.work_dir("ws5");
207 let ws6_dir = test_env.work_dir("ws6");
208
209 ws1_dir
210 .run_jj(["sparse", "set", "--clear", "--add=foo"])
211 .success();
212 ws1_dir.run_jj(["workspace", "add", "../ws2"]).success();
213 let output = ws2_dir.run_jj(["sparse", "list"]);
214 insta::assert_snapshot!(output, @"
215 foo
216 [EOF]
217 ");
218 ws2_dir.run_jj(["sparse", "set", "--add=bar"]).success();
219 ws2_dir.run_jj(["workspace", "add", "../ws3"]).success();
220 let output = ws3_dir.run_jj(["sparse", "list"]);
221 insta::assert_snapshot!(output, @"
222 bar
223 foo
224 [EOF]
225 ");
226 // --sparse-patterns behavior
227 ws3_dir
228 .run_jj(["workspace", "add", "--sparse-patterns=copy", "../ws4"])
229 .success();
230 let output = ws4_dir.run_jj(["sparse", "list"]);
231 insta::assert_snapshot!(output, @"
232 bar
233 foo
234 [EOF]
235 ");
236 ws3_dir
237 .run_jj(["workspace", "add", "--sparse-patterns=full", "../ws5"])
238 .success();
239 let output = ws5_dir.run_jj(["sparse", "list"]);
240 insta::assert_snapshot!(output, @"
241 .
242 [EOF]
243 ");
244 ws3_dir
245 .run_jj(["workspace", "add", "--sparse-patterns=empty", "../ws6"])
246 .success();
247 let output = ws6_dir.run_jj(["sparse", "list"]);
248 insta::assert_snapshot!(output, @"");
249 }
250
251 /// Test adding a second workspace while the current workspace is editing a
252 /// merge
253 #[test]
254 fn test_workspaces_add_second_workspace_on_merge() {
255 let test_env = TestEnvironment::default();
256 test_env.run_jj_in(".", ["git", "init", "main"]).success();
257 let main_dir = test_env.work_dir("main");
258
259 main_dir.run_jj(["describe", "-m=left"]).success();
260 main_dir.run_jj(["new", "@-", "-m=right"]).success();
261 main_dir.run_jj(["new", "@-+", "-m=merge"]).success();
262
263 let output = main_dir.run_jj(["workspace", "list"]);
264 insta::assert_snapshot!(output, @"
265 default: . zsuskuln 46ed31b6 (empty) merge
266 [EOF]
267 ");
268
269 main_dir
270 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
271 .success();
272
273 // The new workspace's working-copy commit shares all parents with the old one.
274 insta::assert_snapshot!(get_log_output(&main_dir), @r#"
275 @ 46ed31b61ce9 default@ "merge"
276 ├─╮
277 │ │ ○ d23b2d4ff55c second@
278 ╭─┬─╯
279 │ ○ 3c52528f5893 "left"
280 ○ │ a3155ab1bf5a "right"
281 ├─╯
282 ◆ 000000000000
283 [EOF]
284 "#);
285 }
286
287 /// Test that --ignore-working-copy is respected
288 #[test]
289 fn test_workspaces_add_ignore_working_copy() {
290 let test_env = TestEnvironment::default();
291 test_env.run_jj_in(".", ["git", "init", "main"]).success();
292 let main_dir = test_env.work_dir("main");
293
294 // TODO: maybe better to error out early?
295 let output = main_dir.run_jj(["workspace", "add", "--ignore-working-copy", "../secondary"]);
296 insta::assert_snapshot!(output.normalize_backslash(), @r#"
297 ------- stderr -------
298 Created workspace in "../secondary"
299 Error: This command must be able to update the working copy.
300 Hint: Don't use --ignore-working-copy.
301 [EOF]
302 [exit status: 1]
303 "#);
304 }
305
306 /// Test that --no-integrate-operation is respected
307 #[test]
308 fn test_workspaces_add_no_integrate_operation() {
309 let test_env = TestEnvironment::default();
310 test_env.run_jj_in(".", ["git", "init", "main"]).success();
311 let main_dir = test_env.work_dir("main");
312
313 // TODO: maybe better to error out early?
314 let output = main_dir.run_jj([
315 "workspace",
316 "add",
317 "--no-integrate-operation",
318 "../secondary",
319 ]);
320 insta::assert_snapshot!(output.normalize_backslash(), @r#"
321 ------- stderr -------
322 Created workspace in "../secondary"
323 Error: This command must be able to update the working copy.
324 Hint: Don't use --no-integrate-operation.
325 [EOF]
326 [exit status: 1]
327 "#);
328 }
329
330 /// Test that --at-op is respected
331 #[test]
332 fn test_workspaces_add_at_operation() {
333 let test_env = TestEnvironment::default();
334 test_env.run_jj_in(".", ["git", "init", "main"]).success();
335 let main_dir = test_env.work_dir("main");
336
337 main_dir.write_file("file1", "");
338 let output = main_dir.run_jj(["commit", "-m1"]);
339 insta::assert_snapshot!(output, @"
340 ------- stderr -------
341 Working copy (@) now at: rlvkpnrz 59e07459 (empty) (no description set)
342 Parent commit (@-) : qpvuntsm 9e4b0b91 1
343 [EOF]
344 ");
345
346 main_dir.write_file("file2", "");
347 let output = main_dir.run_jj(["commit", "-m2"]);
348 insta::assert_snapshot!(output, @"
349 ------- stderr -------
350 Working copy (@) now at: kkmpptxz 6e9610ac (empty) (no description set)
351 Parent commit (@-) : rlvkpnrz 8b7259b9 2
352 [EOF]
353 ");
354
355 // --at-op should disable snapshot in the main workspace, but the newly
356 // created workspace should still be writable.
357 main_dir.write_file("file3", "");
358 let output = main_dir.run_jj(["workspace", "add", "--at-op=@-", "../secondary"]);
359 insta::assert_snapshot!(output.normalize_backslash(), @r#"
360 ------- stderr -------
361 Created workspace in "../secondary"
362 Working copy (@) now at: rzvqmyuk b8772476 (empty) (no description set)
363 Parent commit (@-) : qpvuntsm 9e4b0b91 1
364 Added 1 files, modified 0 files, removed 0 files
365 [EOF]
366 "#);
367 let secondary_dir = test_env.work_dir("secondary");
368
369 // New snapshot can be taken in the secondary workspace.
370 secondary_dir.write_file("file4", "");
371 let output = secondary_dir.run_jj(["status"]);
372 insta::assert_snapshot!(output, @"
373 Working copy changes:
374 A file4
375 Working copy (@) : rzvqmyuk f2ff8257 (no description set)
376 Parent commit (@-): qpvuntsm 9e4b0b91 1
377 [EOF]
378 ------- stderr -------
379 Concurrent modification detected, resolving automatically.
380 [EOF]
381 ");
382
383 let output = secondary_dir.run_jj(["op", "log", "-Tdescription"]);
384 insta::assert_snapshot!(output, @"
385 @ snapshot working copy
386 ○ reconcile divergent operations
387 ├─╮
388 ○ │ commit 9152e822279787a168ddf4cede6440a21faa00d7
389 │ ○ create initial working-copy commit in workspace secondary
390 │ ○ add workspace 'secondary'
391 ├─╯
392 ○ snapshot working copy
393 ○ commit 093c3c9624b6cfe22b310586f5638792aa80e6d7
394 ○ snapshot working copy
395 ○ add workspace 'default'
396 ○
397 [EOF]
398 ");
399 }
400
401 /// Test adding a workspace, but at a specific revision using '-r'
402 #[test]
403 fn test_workspaces_add_workspace_at_revision() {
404 let test_env = TestEnvironment::default();
405 test_env.run_jj_in(".", ["git", "init", "main"]).success();
406 let main_dir = test_env.work_dir("main");
407 let secondary_dir = test_env.work_dir("secondary");
408
409 main_dir.write_file("file-1", "contents");
410 main_dir.run_jj(["commit", "-m", "first"]).success();
411
412 main_dir.write_file("file-2", "contents");
413 main_dir.run_jj(["commit", "-m", "second"]).success();
414
415 let output = main_dir.run_jj(["workspace", "list"]);
416 insta::assert_snapshot!(output, @"
417 default: . kkmpptxz 5ac9178d (empty) (no description set)
418 [EOF]
419 ");
420
421 let output = main_dir.run_jj([
422 "workspace",
423 "add",
424 "--name",
425 "second",
426 "../secondary",
427 "-r",
428 "@--",
429 ]);
430 insta::assert_snapshot!(output.normalize_backslash(), @r#"
431 ------- stderr -------
432 Created workspace in "../secondary"
433 Working copy (@) now at: zxsnswpr ea5860fb (empty) (no description set)
434 Parent commit (@-) : qpvuntsm 27473635 first
435 Added 1 files, modified 0 files, removed 0 files
436 [EOF]
437 "#);
438
439 // Can see the working-copy commit in each workspace in the log output. The "@"
440 // node in the graph indicates the current workspace's working-copy commit.
441 insta::assert_snapshot!(get_log_output(&main_dir), @r#"
442 @ 5ac9178da8b2 default@
443 ○ a47d8a593529 "second"
444 │ ○ ea5860fbd622 second@
445 ├─╯
446 ○ 27473635a942 "first"
447 ◆ 000000000000
448 [EOF]
449 "#);
450 insta::assert_snapshot!(get_log_output(&secondary_dir), @r#"
451 @ ea5860fbd622 second@
452 │ ○ 5ac9178da8b2 default@
453 │ ○ a47d8a593529 "second"
454 ├─╯
455 ○ 27473635a942 "first"
456 ◆ 000000000000
457 [EOF]
458 "#);
459 }
460
461 /// Test multiple `-r` flags to `workspace add` to create a workspace
462 /// working-copy commit with multiple parents.
463 #[test]
464 fn test_workspaces_add_workspace_multiple_revisions() {
465 let test_env = TestEnvironment::default();
466 test_env.run_jj_in(".", ["git", "init", "main"]).success();
467 let main_dir = test_env.work_dir("main");
468
469 main_dir.write_file("file-1", "contents");
470 main_dir.run_jj(["commit", "-m", "first"]).success();
471 main_dir.run_jj(["new", "-r", "root()"]).success();
472
473 main_dir.write_file("file-2", "contents");
474 main_dir.run_jj(["commit", "-m", "second"]).success();
475 main_dir.run_jj(["new", "-r", "root()"]).success();
476
477 main_dir.write_file("file-3", "contents");
478 main_dir.run_jj(["commit", "-m", "third"]).success();
479 main_dir.run_jj(["new", "-r", "root()"]).success();
480
481 insta::assert_snapshot!(get_log_output(&main_dir), @r#"
482 @ 8d23abddc924
483 │ ○ eba7f49e2358 "third"
484 ├─╯
485 │ ○ 62444a45efcf "second"
486 ├─╯
487 │ ○ 27473635a942 "first"
488 ├─╯
489 ◆ 000000000000
490 [EOF]
491 "#);
492
493 let output = main_dir.run_jj([
494 "workspace",
495 "add",
496 "--name=merge",
497 "../merged",
498 "-r=subject(third)",
499 "-r=subject(second)",
500 "-r=subject(first)",
501 ]);
502 insta::assert_snapshot!(output.normalize_backslash(), @r#"
503 ------- stderr -------
504 Created workspace in "../merged"
505 Working copy (@) now at: wmwvqwsz 2d7c9a2d (empty) (no description set)
506 Parent commit (@-) : mzvwutvl eba7f49e third
507 Parent commit (@-) : kkmpptxz 62444a45 second
508 Parent commit (@-) : qpvuntsm 27473635 first
509 Added 3 files, modified 0 files, removed 0 files
510 [EOF]
511 "#);
512
513 insta::assert_snapshot!(get_log_output(&main_dir), @r#"
514 @ 8d23abddc924 default@
515 │ ○ 2d7c9a2d41dc merge@
516 │ ├─┬─╮
517 │ │ │ ○ 27473635a942 "first"
518 ├─────╯
519 │ │ ○ 62444a45efcf "second"
520 ├───╯
521 │ ○ eba7f49e2358 "third"
522 ├─╯
523 ◆ 000000000000
524 [EOF]
525 "#);
526 }
527
528 #[test]
529 fn test_workspaces_add_workspace_from_subdir() {
530 let test_env = TestEnvironment::default();
531 test_env.run_jj_in(".", ["git", "init", "main"]).success();
532 let main_dir = test_env.work_dir("main");
533 let secondary_dir = test_env.work_dir("secondary");
534
535 let subdir_dir = main_dir.create_dir("subdir");
536 subdir_dir.write_file("file", "contents");
537 main_dir.run_jj(["commit", "-m", "initial"]).success();
538
539 let output = main_dir.run_jj(["workspace", "list"]);
540 insta::assert_snapshot!(output.normalize_backslash(), @"
541 default: . rlvkpnrz 0ba0ff35 (empty) (no description set)
542 [EOF]
543 ");
544
545 // Create workspace while in sub-directory of current workspace
546 let output = subdir_dir.run_jj(["workspace", "add", "../../secondary"]);
547 insta::assert_snapshot!(output.normalize_backslash(), @r#"
548 ------- stderr -------
549 Created workspace in "../../secondary"
550 Working copy (@) now at: rzvqmyuk dea1be10 (empty) (no description set)
551 Parent commit (@-) : qpvuntsm 80b67806 initial
552 Added 1 files, modified 0 files, removed 0 files
553 [EOF]
554 "#);
555
556 // Both workspaces show up when we list them
557 let output = secondary_dir.run_jj(["workspace", "list"]);
558 insta::assert_snapshot!(output.normalize_backslash(), @"
559 default: ../main rlvkpnrz 0ba0ff35 (empty) (no description set)
560 secondary: . rzvqmyuk dea1be10 (empty) (no description set)
561 [EOF]
562 ");
563 }
564
565 #[test]
566 fn test_workspaces_add_workspace_in_current_workspace() {
567 let test_env = TestEnvironment::default();
568 test_env.run_jj_in(".", ["git", "init", "main"]).success();
569 let main_dir = test_env.work_dir("main");
570
571 main_dir.write_file("file", "contents");
572 main_dir.run_jj(["commit", "-m", "initial"]).success();
573
574 // Try to create workspace using name instead of path
575 let output = main_dir.run_jj(["workspace", "add", "secondary"]);
576 insta::assert_snapshot!(output.normalize_backslash(), @r#"
577 ------- stderr -------
578 Created workspace in "secondary"
579 Warning: Workspace created inside current directory. If this was unintentional, delete the "secondary" directory and run `jj workspace forget secondary` to remove it.
580 Working copy (@) now at: pmmvwywv 058f604d (empty) (no description set)
581 Parent commit (@-) : qpvuntsm 7b22a8cb initial
582 Added 1 files, modified 0 files, removed 0 files
583 [EOF]
584 "#);
585
586 // Workspace created despite warning
587 let output = main_dir.run_jj(["workspace", "list"]);
588 insta::assert_snapshot!(output, @"
589 default: . rlvkpnrz 504e3d8c (empty) (no description set)
590 secondary: secondary pmmvwywv 058f604d (empty) (no description set)
591 [EOF]
592 ");
593
594 // Use explicit path instead (no warning)
595 let output = main_dir.run_jj(["workspace", "add", "./third"]);
596 insta::assert_snapshot!(output.normalize_backslash(), @r#"
597 ------- stderr -------
598 Created workspace in "third"
599 Working copy (@) now at: zxsnswpr 1c1effec (empty) (no description set)
600 Parent commit (@-) : qpvuntsm 7b22a8cb initial
601 Added 1 files, modified 0 files, removed 0 files
602 [EOF]
603 "#);
604
605 // Both workspaces created
606 let output = main_dir.run_jj(["workspace", "list"]);
607 insta::assert_snapshot!(output, @"
608 default: . rlvkpnrz 504e3d8c (empty) (no description set)
609 secondary: secondary pmmvwywv 058f604d (empty) (no description set)
610 third: third zxsnswpr 1c1effec (empty) (no description set)
611 [EOF]
612 ");
613
614 let output = main_dir.run_jj(["file", "list"]);
615 insta::assert_snapshot!(output.normalize_backslash(), @"
616 file
617 [EOF]
618 ");
619 }
620
621 #[test]
622 fn test_workspace_add_override_path_in_store() {
623 let test_env = TestEnvironment::default();
624 test_env.run_jj_in(".", ["git", "init", "main"]).success();
625 let main_dir = test_env.work_dir("main");
626
627 main_dir.write_file("file", "contents");
628 main_dir.run_jj(["commit", "-m", "initial"]).success();
629
630 let output = main_dir.run_jj(["workspace", "add", "--name", "second", "../secondary"]);
631 insta::assert_snapshot!(output.normalize_backslash(), @r#"
632 ------- stderr -------
633 Created workspace in "../secondary"
634 Working copy (@) now at: pmmvwywv 058f604d (empty) (no description set)
635 Parent commit (@-) : qpvuntsm 7b22a8cb initial
636 Added 1 files, modified 0 files, removed 0 files
637 [EOF]
638 "#);
639
640 // Both workspaces show up when we list them
641 let output = main_dir.run_jj(["workspace", "list"]);
642 insta::assert_snapshot!(output.normalize_backslash(), @"
643 default: . rlvkpnrz 504e3d8c (empty) (no description set)
644 second: ../secondary pmmvwywv 058f604d (empty) (no description set)
645 [EOF]
646 ");
647
648 // Undoing workspace addition
649 let output = main_dir.run_jj(["operation", "restore", "@--"]);
650 insta::assert_snapshot!(output, @"
651 ------- stderr -------
652 Restored to operation: 7d5981d12f2a (2001-02-03 08:05:08) commit 006bd1130b84e90ab082adeabd7409270d5a86da
653 [EOF]
654 ");
655
656 // Only default workspace show up when we list them
657 let output = main_dir.run_jj(["workspace", "list"]);
658 insta::assert_snapshot!(output.normalize_backslash(), @"
659 default: . rlvkpnrz 504e3d8c (empty) (no description set)
660 [EOF]
661 ");
662
663 // Re-creating the same workspace with different path
664 let output = main_dir.run_jj(["workspace", "add", "--name", "second", "../tertiary"]);
665 insta::assert_snapshot!(output.normalize_backslash(), @r#"
666 ------- stderr -------
667 Created workspace in "../tertiary"
668 Working copy (@) now at: spxsnpux 96ef6c50 (empty) (no description set)
669 Parent commit (@-) : qpvuntsm 7b22a8cb initial
670 Added 1 files, modified 0 files, removed 0 files
671 [EOF]
672 "#);
673
674 // Both workspaces show up when we list them
675 let output = main_dir.run_jj(["workspace", "list"]);
676 insta::assert_snapshot!(output.normalize_backslash(), @"
677 default: . rlvkpnrz 504e3d8c (empty) (no description set)
678 second: ../tertiary spxsnpux 96ef6c50 (empty) (no description set)
679 [EOF]
680 ");
681
682 // The 'second' workspace points to the new path
683 let output = main_dir.run_jj(["workspace", "root", "--name", "second"]);
684 insta::assert_snapshot!(output, @"
685 $TEST_ENV/tertiary
686 [EOF]
687 ");
688 }
689
690 /// Test making changes to the working copy in a workspace as it gets rewritten
691 /// from another workspace
692 #[test]
693 fn test_workspaces_conflicting_edits() {
694 let test_env = TestEnvironment::default();
695 test_env.run_jj_in(".", ["git", "init", "main"]).success();
696 let main_dir = test_env.work_dir("main");
697 let secondary_dir = test_env.work_dir("secondary");
698
699 main_dir.write_file("file", "contents\n");
700 main_dir.run_jj(["new"]).success();
701
702 main_dir
703 .run_jj(["workspace", "add", "../secondary"])
704 .success();
705
706 insta::assert_snapshot!(get_log_output(&main_dir), @"
707 @ 393250c59e39 default@
708 │ ○ 547036666102 secondary@
709 ├─╯
710 ○ 9a462e35578a
711 ◆ 000000000000
712 [EOF]
713 ");
714
715 // Make changes in both working copies
716 main_dir.write_file("file", "changed in main\n");
717 secondary_dir.write_file("file", "changed in second\n");
718 // Squash the changes from the main workspace into the initial commit (before
719 // running any command in the secondary workspace
720 let output = main_dir.run_jj(["squash"]);
721 insta::assert_snapshot!(output, @r"
722 ------- stderr -------
723 Rebased 1 descendant commits.
724 Working copy (@) now at: mzvwutvl 3a9b690d (empty) (no description set)
725 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
726 [EOF]
727 ");
728
729 // The secondary workspace's working-copy commit was updated
730 insta::assert_snapshot!(get_log_output(&main_dir), @"
731 @ 3a9b690d6e67 default@
732 │ ○ 90f3d42e0bff secondary@
733 ├─╯
734 ○ b853f7c8b006
735 ◆ 000000000000
736 [EOF]
737 ");
738 let output = secondary_dir.run_jj(["st"]);
739 insta::assert_snapshot!(output, @"
740 ------- stderr -------
741 Error: The working copy is stale (not updated since operation 149761aea7d1).
742 Hint: Run `jj workspace update-stale` to update it.
743 See https://docs.jj-vcs.dev/latest/working-copy/#stale-working-copy for more information.
744 [EOF]
745 [exit status: 1]
746 ");
747 // Same error on second run, and from another command
748 let output = secondary_dir.run_jj(["log"]);
749 insta::assert_snapshot!(output, @"
750 ------- stderr -------
751 Error: The working copy is stale (not updated since operation 149761aea7d1).
752 Hint: Run `jj workspace update-stale` to update it.
753 See https://docs.jj-vcs.dev/latest/working-copy/#stale-working-copy for more information.
754 [EOF]
755 [exit status: 1]
756 ");
757 // It was detected that the working copy is now stale.
758 // Since there was an uncommitted change in the working copy, it should
759 // have been committed first (causing divergence)
760 let output = secondary_dir.run_jj(["workspace", "update-stale"]);
761 insta::assert_snapshot!(output, @r"
762 ------- stderr -------
763 Concurrent modification detected, resolving automatically.
764 Rebased 1 descendant commits onto commits rewritten by other operation.
765 Working copy (@) now at: pmmvwywv/2 90f3d42e (divergent) (empty) (no description set)
766 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
767 Added 0 files, modified 1 files, removed 0 files
768 Updated working copy to fresh commit 90f3d42e0bff
769 [EOF]
770 ");
771 insta::assert_snapshot!(get_log_output(&secondary_dir),
772 @"
773 @ 90f3d42e0bff secondary@ (divergent)
774 │ × 8823f4273170 (divergent)
775 ├─╯
776 │ ○ 3a9b690d6e67 default@
777 ├─╯
778 ○ b853f7c8b006
779 ◆ 000000000000
780 [EOF]
781 ");
782 // The stale working copy should have been resolved by the previous command
783 insta::assert_snapshot!(get_log_output(&secondary_dir), @"
784 @ 90f3d42e0bff secondary@ (divergent)
785 │ × 8823f4273170 (divergent)
786 ├─╯
787 │ ○ 3a9b690d6e67 default@
788 ├─╯
789 ○ b853f7c8b006
790 ◆ 000000000000
791 [EOF]
792 ");
793 }
794
795 /// Test a clean working copy that gets rewritten from another workspace
796 #[test]
797 fn test_workspaces_updated_by_other() {
798 let test_env = TestEnvironment::default();
799 test_env.run_jj_in(".", ["git", "init", "main"]).success();
800 let main_dir = test_env.work_dir("main");
801 let secondary_dir = test_env.work_dir("secondary");
802
803 main_dir.write_file("file", "contents\n");
804 main_dir.run_jj(["new"]).success();
805
806 main_dir
807 .run_jj(["workspace", "add", "../secondary"])
808 .success();
809
810 insta::assert_snapshot!(get_log_output(&main_dir), @"
811 @ 393250c59e39 default@
812 │ ○ 547036666102 secondary@
813 ├─╯
814 ○ 9a462e35578a
815 ◆ 000000000000
816 [EOF]
817 ");
818
819 // Rewrite the check-out commit in one workspace.
820 main_dir.write_file("file", "changed in main\n");
821 let output = main_dir.run_jj(["squash"]);
822 insta::assert_snapshot!(output, @r"
823 ------- stderr -------
824 Rebased 1 descendant commits.
825 Working copy (@) now at: mzvwutvl 3a9b690d (empty) (no description set)
826 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
827 [EOF]
828 ");
829
830 // The secondary workspace's working-copy commit was updated.
831 insta::assert_snapshot!(get_log_output(&main_dir), @"
832 @ 3a9b690d6e67 default@
833 │ ○ 90f3d42e0bff secondary@
834 ├─╯
835 ○ b853f7c8b006
836 ◆ 000000000000
837 [EOF]
838 ");
839 let output = secondary_dir.run_jj(["st"]);
840 insta::assert_snapshot!(output, @"
841 ------- stderr -------
842 Error: The working copy is stale (not updated since operation 149761aea7d1).
843 Hint: Run `jj workspace update-stale` to update it.
844 See https://docs.jj-vcs.dev/latest/working-copy/#stale-working-copy for more information.
845 [EOF]
846 [exit status: 1]
847 ");
848 // It was detected that the working copy is now stale, but clean. So no
849 // divergent commit should be created.
850 let output = secondary_dir.run_jj(["workspace", "update-stale"]);
851 insta::assert_snapshot!(output, @"
852 ------- stderr -------
853 Working copy (@) now at: pmmvwywv 90f3d42e (empty) (no description set)
854 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
855 Added 0 files, modified 1 files, removed 0 files
856 Updated working copy to fresh commit 90f3d42e0bff
857 [EOF]
858 ");
859 insta::assert_snapshot!(get_log_output(&secondary_dir),
860 @"
861 @ 90f3d42e0bff secondary@
862 │ ○ 3a9b690d6e67 default@
863 ├─╯
864 ○ b853f7c8b006
865 ◆ 000000000000
866 [EOF]
867 ");
868 }
869
870 /// Test a clean working copy that gets rewritten from another workspace
871 #[test]
872 fn test_workspaces_updated_by_other_automatic() {
873 let test_env = TestEnvironment::default();
874 test_env.add_config("snapshot.auto-update-stale = true\n");
875
876 test_env.run_jj_in(".", ["git", "init", "main"]).success();
877 let main_dir = test_env.work_dir("main");
878 let secondary_dir = test_env.work_dir("secondary");
879
880 main_dir.write_file("file", "contents\n");
881 main_dir.run_jj(["new"]).success();
882
883 main_dir
884 .run_jj(["workspace", "add", "../secondary"])
885 .success();
886
887 insta::assert_snapshot!(get_log_output(&main_dir), @"
888 @ 393250c59e39 default@
889 │ ○ 547036666102 secondary@
890 ├─╯
891 ○ 9a462e35578a
892 ◆ 000000000000
893 [EOF]
894 ");
895
896 // Rewrite the check-out commit in one workspace.
897 main_dir.write_file("file", "changed in main\n");
898 let output = main_dir.run_jj(["squash"]);
899 insta::assert_snapshot!(output, @r"
900 ------- stderr -------
901 Rebased 1 descendant commits.
902 Working copy (@) now at: mzvwutvl 3a9b690d (empty) (no description set)
903 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
904 [EOF]
905 ");
906
907 // The secondary workspace's working-copy commit was updated.
908 insta::assert_snapshot!(get_log_output(&main_dir), @"
909 @ 3a9b690d6e67 default@
910 │ ○ 90f3d42e0bff secondary@
911 ├─╯
912 ○ b853f7c8b006
913 ◆ 000000000000
914 [EOF]
915 ");
916
917 // The first working copy gets automatically updated.
918 let output = secondary_dir.run_jj(["st"]);
919 insta::assert_snapshot!(output, @"
920 The working copy has no changes.
921 Working copy (@) : pmmvwywv 90f3d42e (empty) (no description set)
922 Parent commit (@-): qpvuntsm b853f7c8 (no description set)
923 [EOF]
924 ------- stderr -------
925 Working copy (@) now at: pmmvwywv 90f3d42e (empty) (no description set)
926 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
927 Added 0 files, modified 1 files, removed 0 files
928 Updated working copy to fresh commit 90f3d42e0bff
929 [EOF]
930 ");
931
932 insta::assert_snapshot!(get_log_output(&secondary_dir),
933 @"
934 @ 90f3d42e0bff secondary@
935 │ ○ 3a9b690d6e67 default@
936 ├─╯
937 ○ b853f7c8b006
938 ◆ 000000000000
939 [EOF]
940 ");
941 }
942
943 /// Test a dirty working copy that gets rewritten from another workspace
944 #[test]
945 fn test_workspaces_updated_by_other_with_changes_in_working_copy_automatic() {
946 let test_env = TestEnvironment::default();
947 test_env.add_config("snapshot.auto-update-stale = true\n");
948
949 test_env.run_jj_in(".", ["git", "init", "main"]).success();
950 let main_dir = test_env.work_dir("main");
951 let secondary_dir = test_env.work_dir("secondary");
952
953 main_dir.write_file("file", "contents\n");
954 main_dir.run_jj(["new"]).success();
955
956 main_dir
957 .run_jj(["workspace", "add", "../secondary"])
958 .success();
959
960 insta::assert_snapshot!(get_log_output(&main_dir), @"
961 @ 393250c59e39 default@
962 │ ○ 547036666102 secondary@
963 ├─╯
964 ○ 9a462e35578a
965 ◆ 000000000000
966 [EOF]
967 ");
968
969 // Rewrite all commits from one workspace.
970 main_dir.write_file("file", "changed in main\n");
971 let output = main_dir.run_jj(["squash"]);
972 insta::assert_snapshot!(output, @r"
973 ------- stderr -------
974 Rebased 1 descendant commits.
975 Working copy (@) now at: mzvwutvl 3a9b690d (empty) (no description set)
976 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
977 [EOF]
978 ");
979
980 // The secondary workspace's working-copy commit was updated.
981 insta::assert_snapshot!(get_log_output(&main_dir), @"
982 @ 3a9b690d6e67 default@
983 │ ○ 90f3d42e0bff secondary@
984 ├─╯
985 ○ b853f7c8b006
986 ◆ 000000000000
987 [EOF]
988 ");
989
990 // The first working copy gets automatically updated.
991 secondary_dir.write_file("file", "modified contents\n");
992 let output = secondary_dir.run_jj(["describe", "-m", "modified"]);
993 insta::assert_snapshot!(output, @r"
994 ------- stderr -------
995 Concurrent modification detected, resolving automatically.
996 Rebased 1 descendant commits onto commits rewritten by other operation.
997 Working copy (@) now at: pmmvwywv/2 90f3d42e (divergent) (empty) (no description set)
998 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
999 Added 0 files, modified 1 files, removed 0 files
1000 Updated working copy to fresh commit 90f3d42e0bff
1001 Working copy (@) now at: pmmvwywv/0 c38323e3 (divergent) (empty) modified
1002 Parent commit (@-) : qpvuntsm b853f7c8 (no description set)
1003 [EOF]
1004 ");
1005
1006 // The snapshotting of the modified contents happens on top of the old
1007 // operation. The `describe` operation itself happens on top the reconciled
1008 // operation.
1009 let output = main_dir.run_jj(["op", "log", "-Tdescription"]);
1010 insta::assert_snapshot!(output, @"
1011 @ describe commit 90f3d42e0bff073721e2640e32c18fb1c386d7ce
1012 ○ reconcile divergent operations
1013 ├─╮
1014 ○ │ squash commits into 9a462e35578a347e6a3951bf7a58ad7146959a8b
1015 ○ │ snapshot working copy
1016 │ ○ snapshot working copy
1017 ├─╯
1018 ○ create initial working-copy commit in workspace secondary
1019 ○ add workspace 'secondary'
1020 ○ new empty commit
1021 ○ snapshot working copy
1022 ○ add workspace 'default'
1023 ○
1024 [EOF]
1025 ");
1026
1027 // We get divergence between the newly described commit and the commit created
1028 // by snapshotting (the reconciliation happened to point secondary@ to the child
1029 // of the squashed commit rather than the snapshot commit).
1030 insta::assert_snapshot!(get_log_output(&secondary_dir),
1031 @r#"
1032 @ c38323e3e6f3 secondary@ (divergent) "modified"
1033 │ × 48a90f069c8c (divergent)
1034 ├─╯
1035 │ ○ 3a9b690d6e67 default@
1036 ├─╯
1037 ○ b853f7c8b006
1038 ◆ 000000000000
1039 [EOF]
1040 "#);
1041 }
1042
1043 #[test_case(false; "manual")]
1044 #[test_case(true; "automatic")]
1045 fn test_workspaces_current_op_discarded_by_other(automatic: bool) {
1046 let test_env = TestEnvironment::default();
1047 if automatic {
1048 test_env.add_config("snapshot.auto-update-stale = true\n");
1049 }
1050
1051 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1052 let main_dir = test_env.work_dir("main");
1053 let secondary_dir = test_env.work_dir("secondary");
1054
1055 main_dir.write_file("modified", "base\n");
1056 main_dir.write_file("deleted", "base\n");
1057 main_dir.write_file("sparse", "base\n");
1058 main_dir.run_jj(["new"]).success();
1059 main_dir.write_file("modified", "main\n");
1060 main_dir.run_jj(["new"]).success();
1061
1062 main_dir
1063 .run_jj(["workspace", "add", "../secondary"])
1064 .success();
1065 // Make unsnapshotted writes in the secondary working copy
1066 secondary_dir
1067 .run_jj([
1068 "sparse",
1069 "set",
1070 "--clear",
1071 "--add=modified",
1072 "--add=deleted",
1073 "--add=added",
1074 ])
1075 .success();
1076 secondary_dir.write_file("modified", "secondary\n");
1077 secondary_dir.remove_file("deleted");
1078 secondary_dir.write_file("added", "secondary\n");
1079
1080 // Create an op by abandoning the parent commit. Importantly, that commit also
1081 // changes the target tree in the secondary workspace.
1082 main_dir.run_jj(["abandon", "@-"]).success();
1083
1084 let output = main_dir.run_jj([
1085 "operation",
1086 "log",
1087 "--template",
1088 r#"id.short(10) ++ " " ++ description"#,
1089 ]);
1090 insta::allow_duplicates! {
1091 insta::assert_snapshot!(output, @"
1092 @ a9b524b948 abandon commit de90575a14d8b9198dc0930f9de4a69f846ded36
1093 ○ 0392b7d733 create initial working-copy commit in workspace secondary
1094 ○ 766373d1f4 add workspace 'secondary'
1095 ○ eb6701963b new empty commit
1096 ○ 1d937e1f1e snapshot working copy
1097 ○ e22ce69861 new empty commit
1098 ○ 48aa617132 snapshot working copy
1099 ○ f63ee16f95 add workspace 'default'
1100 ○ 0000000000
1101 [EOF]
1102 ");
1103 }
1104
1105 // Abandon ops, including the one the secondary workspace is currently on.
1106 main_dir.run_jj(["operation", "abandon", "..@-"]).success();
1107 main_dir.run_jj(["util", "gc", "--expire=now"]).success();
1108
1109 insta::allow_duplicates! {
1110 insta::assert_snapshot!(get_log_output(&main_dir), @"
1111 @ 320bc89effc9 default@
1112 │ ○ 891f00062e10 secondary@
1113 ├─╯
1114 ○ 367415be5b44
1115 ◆ 000000000000
1116 [EOF]
1117 ");
1118 }
1119
1120 if automatic {
1121 // Run a no-op command to set the randomness seed for commit hashes.
1122 secondary_dir.run_jj(["help"]).success();
1123
1124 let output = secondary_dir.run_jj(["st"]);
1125 insta::assert_snapshot!(output, @"
1126 Working copy changes:
1127 C {modified => added}
1128 D deleted
1129 M modified
1130 Working copy (@) : kmkuslsw 18851b39 RECOVERY COMMIT FROM `jj workspace update-stale`
1131 Parent commit (@-): rzvqmyuk 891f0006 (empty) (no description set)
1132 [EOF]
1133 ------- stderr -------
1134 Failed to read working copy's current operation; attempting recovery. Error message from read attempt: Object 0392b7d73383f694906abd6ffd55416c30d1775a9790553062784f5c4553c09746b388aa86fb7d27b113d1096f50845e28dc24b3de8b1a9d515cbde3b44eb346 of type operation not found
1135 Created and checked out recovery commit 866928d1e0fd
1136 [EOF]
1137 ");
1138 } else {
1139 let output = secondary_dir.run_jj(["st"]);
1140 insta::assert_snapshot!(output, @"
1141 ------- stderr -------
1142 Error: Could not read working copy's operation.
1143 Hint: Run `jj workspace update-stale` to recover.
1144 See https://docs.jj-vcs.dev/latest/working-copy/#stale-working-copy for more information.
1145 [EOF]
1146 [exit status: 1]
1147 ");
1148
1149 let output = secondary_dir.run_jj(["workspace", "update-stale"]);
1150 insta::assert_snapshot!(output, @"
1151 ------- stderr -------
1152 Failed to read working copy's current operation; attempting recovery. Error message from read attempt: Object 0392b7d73383f694906abd6ffd55416c30d1775a9790553062784f5c4553c09746b388aa86fb7d27b113d1096f50845e28dc24b3de8b1a9d515cbde3b44eb346 of type operation not found
1153 Created and checked out recovery commit 866928d1e0fd
1154 [EOF]
1155 ");
1156 }
1157
1158 insta::allow_duplicates! {
1159 insta::assert_snapshot!(get_log_output(&main_dir), @r#"
1160 @ 320bc89effc9 default@
1161 │ ○ 18851b397d09 secondary@ "RECOVERY COMMIT FROM `jj workspace update-stale`"
1162 │ ○ 891f00062e10
1163 ├─╯
1164 ○ 367415be5b44
1165 ◆ 000000000000
1166 [EOF]
1167 "#);
1168 }
1169
1170 // The sparse patterns should remain
1171 let output = secondary_dir.run_jj(["sparse", "list"]);
1172 insta::allow_duplicates! {
1173 insta::assert_snapshot!(output, @"
1174 added
1175 deleted
1176 modified
1177 [EOF]
1178 ");
1179 }
1180 let output = secondary_dir.run_jj(["st"]);
1181 insta::allow_duplicates! {
1182 insta::assert_snapshot!(output, @"
1183 Working copy changes:
1184 C {modified => added}
1185 D deleted
1186 M modified
1187 Working copy (@) : kmkuslsw 18851b39 RECOVERY COMMIT FROM `jj workspace update-stale`
1188 Parent commit (@-): rzvqmyuk 891f0006 (empty) (no description set)
1189 [EOF]
1190 ");
1191 }
1192 insta::allow_duplicates! {
1193 // The modified file should have the same contents it had before (not reset to
1194 // the base contents)
1195 insta::assert_snapshot!(secondary_dir.read_file("modified"), @"secondary");
1196 }
1197
1198 let output = secondary_dir.run_jj(["evolog"]);
1199 if automatic {
1200 insta::assert_snapshot!(output, @"
1201 @ kmkuslsw [email protected] 2001-02-03 08:05:18 secondary@ 18851b39
1202 │ RECOVERY COMMIT FROM `jj workspace update-stale`
1203 │ -- operation 91f539374e6a snapshot working copy
1204 ○ kmkuslsw/1 [email protected] 2001-02-03 08:05:18 866928d1 (hidden)
1205 (empty) RECOVERY COMMIT FROM `jj workspace update-stale`
1206 -- operation 2a845e0b4514 recovery commit
1207 [EOF]
1208 ");
1209 } else {
1210 insta::assert_snapshot!(output, @"
1211 @ kmkuslsw [email protected] 2001-02-03 08:05:18 secondary@ 18851b39
1212 │ RECOVERY COMMIT FROM `jj workspace update-stale`
1213 │ -- operation 2d387a4a6355 snapshot working copy
1214 ○ kmkuslsw/1 [email protected] 2001-02-03 08:05:18 866928d1 (hidden)
1215 (empty) RECOVERY COMMIT FROM `jj workspace update-stale`
1216 -- operation 2a845e0b4514 recovery commit
1217 [EOF]
1218 ");
1219 }
1220 }
1221
1222 #[test]
1223 fn test_workspaces_update_stale_noop() {
1224 let test_env = TestEnvironment::default();
1225 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1226 let main_dir = test_env.work_dir("main");
1227
1228 let output = main_dir.run_jj(["workspace", "update-stale"]);
1229 insta::assert_snapshot!(output, @r"
1230 ------- stderr -------
1231 Attempted recovery, but the working copy is not stale.
1232 [EOF]
1233 ");
1234
1235 let output = main_dir.run_jj(["workspace", "update-stale", "--ignore-working-copy"]);
1236 insta::assert_snapshot!(output, @"
1237 ------- stderr -------
1238 Error: This command must be able to update the working copy.
1239 Hint: Don't use --ignore-working-copy.
1240 [EOF]
1241 [exit status: 1]
1242 ");
1243
1244 let output = main_dir.run_jj(["op", "log", "-Tdescription"]);
1245 insta::assert_snapshot!(output, @"
1246 @ add workspace 'default'
1247 ○
1248 [EOF]
1249 ");
1250 }
1251
1252 /// If the working copy was last updated to an unpublished operation, it should
1253 /// be reported, even if the latest published operation has the same tree.
1254 #[test]
1255 fn test_workspaces_unpublished_operation_same_tree() {
1256 let test_env = TestEnvironment::default();
1257 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1258 let main_dir = test_env.work_dir("main");
1259
1260 main_dir.run_jj(["desc", "-m=A"]).success();
1261 let a_op_id = main_dir.current_operation_id();
1262 main_dir.run_jj(["new", "-m=B"]).success();
1263 let b_op_id = main_dir.current_operation_id();
1264 // Make the repo forget about the B operation
1265 main_dir.remove_file(format!(".jj/repo/op_heads/heads/{b_op_id}"));
1266 main_dir.write_file(format!(".jj/repo/op_heads/heads/{a_op_id}"), "");
1267 main_dir
1268 .run_jj(["new", "-m=C", "--ignore-working-copy"])
1269 .success();
1270 // The working copy should be stale
1271 let output = main_dir.run_jj(["status"]);
1272 insta::assert_snapshot!(output, @"
1273 ------- stderr -------
1274 Internal error: The repo was loaded at operation eb46e46959f1, which seems to be a sibling of the working copy's operation 2a68fa5a5771
1275 Hint: Run `jj op integrate 2a68fa5a5771` to add the working copy's operation to the operation log.
1276 [EOF]
1277 [exit status: 255]
1278 ");
1279 // Test recovery by running `jj workspace update-stale` even though `jj op
1280 // integrate` is a better solution
1281 let output = main_dir.run_jj(["workspace", "update-stale"]);
1282 insta::assert_snapshot!(output, @"
1283 ------- stderr -------
1284 Working copy (@) now at: zsuskuln 36a15ac4 (empty) C
1285 Parent commit (@-) : qpvuntsm 8777db25 (empty) A
1286 Updated working copy to fresh commit 36a15ac414e8
1287 [EOF]
1288 ");
1289 }
1290
1291 /// Test "update-stale" in a dirty, but not stale working copy.
1292 #[test]
1293 fn test_workspaces_update_stale_snapshot() {
1294 let test_env = TestEnvironment::default();
1295 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1296 let main_dir = test_env.work_dir("main");
1297 let secondary_dir = test_env.work_dir("secondary");
1298
1299 main_dir.write_file("file", "changed in main\n");
1300 main_dir.run_jj(["new"]).success();
1301 main_dir
1302 .run_jj(["workspace", "add", "../secondary"])
1303 .success();
1304
1305 // Record new operation in one workspace.
1306 main_dir.run_jj(["new"]).success();
1307
1308 // Snapshot the other working copy, which unfortunately results in concurrent
1309 // operations, but should be resolved cleanly.
1310 secondary_dir.write_file("file", "changed in second\n");
1311 let output = secondary_dir.run_jj(["workspace", "update-stale"]);
1312 insta::assert_snapshot!(output, @r"
1313 ------- stderr -------
1314 Concurrent modification detected, resolving automatically.
1315 Attempted recovery, but the working copy is not stale.
1316 [EOF]
1317 ");
1318
1319 insta::assert_snapshot!(get_log_output(&secondary_dir), @"
1320 @ 35d779b3baea secondary@
1321 │ ○ c9516583d53b default@
1322 │ ○ f6ae7810ef56
1323 ├─╯
1324 ○ 7d5738ba9943
1325 ◆ 000000000000
1326 [EOF]
1327 ");
1328 }
1329
1330 /// Test that "workspace update-stale" works in colocated repos.
1331 ///
1332 /// This is a regression test for a bug introduced in commit 7a296ca1 where
1333 /// the reload-to-HEAD logic (added to fix a race condition) would break
1334 /// "workspace update-stale" by reloading the repo to HEAD before snapshotting,
1335 /// even though recovery intentionally loads at an old operation.
1336 #[test]
1337 fn test_colocated_workspace_update_stale() {
1338 let test_env = TestEnvironment::default();
1339 test_env
1340 .run_jj_in(".", ["git", "init", "--colocate", "main"])
1341 .success();
1342 let main_dir = test_env.work_dir("main");
1343 let secondary_dir = test_env.work_dir("secondary");
1344 let git_repo = git::open(main_dir.root());
1345
1346 main_dir.write_file("file", "contents\n");
1347 main_dir.run_jj(["new"]).success();
1348
1349 // Create new bookmarked revision from the main workspace.
1350 main_dir
1351 .run_jj(["new", "--no-edit", "root()", "-mold book1"])
1352 .success();
1353 main_dir
1354 .run_jj(["bookmark", "set", "-rsubject('old book1')", "book1"])
1355 .success();
1356
1357 main_dir
1358 .run_jj(["workspace", "add", "../secondary"])
1359 .success();
1360
1361 // Rewrite the check-out commit from the secondary workspace.
1362 // This makes the main (colocated) workspace's working copy stale.
1363 secondary_dir.write_file("file", "changed in secondary\n");
1364 secondary_dir.run_jj(["squash"]).success();
1365
1366 // Update and export the bookmark from the secondary workspace.
1367 secondary_dir
1368 .run_jj(["new", "--no-edit", "root()", "-mnew book1"])
1369 .success();
1370 secondary_dir
1371 .run_jj([
1372 "bookmark",
1373 "set",
1374 "-rsubject('new book1')",
1375 "--allow-backwards",
1376 "book1",
1377 ])
1378 .success();
1379 secondary_dir.run_jj(["git", "export"]).success();
1380
1381 // Create new Git ref and commit which will be imported later by "jj
1382 // workspace update-stale".
1383 git::add_commit(&git_repo, "refs/heads/book2", "file", b"", "book2", &[]);
1384
1385 insta::assert_snapshot!(get_log_output(&secondary_dir), @r#"
1386 @ 9cb8253861b5 secondary@
1387 │ ○ f562bf82f2da default@
1388 ├─╯
1389 ○ 30ed2f28b710
1390 │ ○ e97ad7861f78 book1 "new book1"
1391 ├─╯
1392 │ ○ f656b467890b "old book1"
1393 ├─╯
1394 ◆ 000000000000
1395 [EOF]
1396 "#);
1397
1398 // The main workspace's working copy is now stale.
1399 let output = main_dir.run_jj(["st"]);
1400 insta::assert_snapshot!(output, @"
1401 ------- stderr -------
1402 Error: The working copy is stale (not updated since operation 572e45b3fba3).
1403 Hint: Run `jj workspace update-stale` to update it.
1404 See https://docs.jj-vcs.dev/latest/working-copy/#stale-working-copy for more information.
1405 [EOF]
1406 [exit status: 1]
1407 ");
1408
1409 // Before the fix, this would fail with the same "working copy is stale" error
1410 // because the colocated repo reload logic would reload to HEAD before
1411 // snapshotting, breaking the recovery.
1412 let output = main_dir.run_jj(["workspace", "update-stale"]);
1413 insta::assert_snapshot!(output, @"
1414 ------- stderr -------
1415 Working copy (@) now at: rlvkpnrz f562bf82 (empty) (no description set)
1416 Parent commit (@-) : qpvuntsm 30ed2f28 (no description set)
1417 Added 0 files, modified 1 files, removed 0 files
1418 Updated working copy to fresh commit f562bf82f2da
1419 Done importing changes from the underlying Git repo.
1420 [EOF]
1421 ");
1422
1423 // Verify the workspace is now up-to-date. New bookmark "book2" should have
1424 // been imported by the previous command.
1425 let output = main_dir.run_jj(["st"]);
1426 insta::assert_snapshot!(output, @"
1427 The working copy has no changes.
1428 Working copy (@) : rlvkpnrz f562bf82 (empty) (no description set)
1429 Parent commit (@-): qpvuntsm 30ed2f28 (no description set)
1430 [EOF]
1431 ");
1432
1433 // The updated bookmark "book1" shouldn't be re-imported as an external
1434 // change. If it were, the "old book1" revision would be abandoned.
1435 insta::assert_snapshot!(get_log_output(&main_dir), @r#"
1436 @ f562bf82f2da default@
1437 │ ○ 9cb8253861b5 secondary@
1438 ├─╯
1439 ○ 30ed2f28b710
1440 │ ○ 7fe3ff3b9a60 book2 "book2"
1441 ├─╯
1442 │ ○ e97ad7861f78 book1 "new book1"
1443 ├─╯
1444 │ ○ f656b467890b "old book1"
1445 ├─╯
1446 ◆ 000000000000
1447 [EOF]
1448 "#);
1449 }
1450
1451 /// Test forgetting workspaces
1452 #[test]
1453 fn test_workspaces_forget() {
1454 let test_env = TestEnvironment::default();
1455 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1456 let main_dir = test_env.work_dir("main");
1457
1458 main_dir.write_file("file", "contents");
1459 main_dir.run_jj(["new"]).success();
1460
1461 main_dir
1462 .run_jj(["workspace", "add", "../secondary"])
1463 .success();
1464 let output = main_dir.run_jj(["workspace", "forget"]);
1465 insta::assert_snapshot!(output, @"
1466 ------- stderr -------
1467 Warning: The current workspace 'default' no longer exists after this operation. The working copy was left untouched.
1468 Hint: Restore to an operation that contains the workspace (e.g. `jj undo` or `jj redo`).
1469 [EOF]
1470 ");
1471
1472 // When listing workspaces, only the secondary workspace shows up
1473 let output = main_dir.run_jj(["workspace", "list"]);
1474 insta::assert_snapshot!(output.normalize_backslash(), @"
1475 secondary: ../secondary pmmvwywv 31da1455 (empty) (no description set)
1476 [EOF]
1477 ");
1478
1479 // After forgetting the default, secondary root is still recorded, default no
1480 // longer exists
1481 let output = main_dir.run_jj(["workspace", "root", "--name", "secondary"]);
1482 insta::assert_snapshot!(output, @"
1483 $TEST_ENV/secondary
1484 [EOF]
1485 ");
1486 let output = main_dir.run_jj(["workspace", "root", "--name", "default"]);
1487 insta::assert_snapshot!(output, @"
1488 ------- stderr -------
1489 Error: No such workspace: default
1490 [EOF]
1491 [exit status: 1]
1492 ");
1493
1494 // The old working copy doesn't get an "@" in the log output
1495 // TODO: It seems useful to still have the "secondary@" marker here even though
1496 // there's only one workspace. We should show it when the command is not run
1497 // from that workspace.
1498 insta::assert_snapshot!(get_log_output(&main_dir), @"
1499 ○ 31da14559558
1500 ○ 006bd1130b84
1501 ◆ 000000000000
1502 [EOF]
1503 ");
1504
1505 // Revision "@" cannot be used
1506 let output = main_dir.run_jj(["log", "-r", "@"]);
1507 insta::assert_snapshot!(output, @"
1508 ------- stderr -------
1509 Error: Workspace `default` doesn't have a working-copy commit
1510 [EOF]
1511 [exit status: 1]
1512 ");
1513
1514 // Try to add back the workspace
1515 // TODO: We should make this just add it back instead of failing
1516 let output = main_dir.run_jj(["workspace", "add", "."]);
1517 insta::assert_snapshot!(output, @"
1518 ------- stderr -------
1519 Error: Destination path exists and is not an empty directory
1520 [EOF]
1521 [exit status: 1]
1522 ");
1523
1524 // Add a third workspace...
1525 main_dir.run_jj(["workspace", "add", "../third"]).success();
1526 // ... and then forget it, a non-existent one, and the secondary workspace too
1527 let output = main_dir.run_jj(["workspace", "forget", "secondary", "nonexistent", "third"]);
1528 insta::assert_snapshot!(output, @"
1529 ------- stderr -------
1530 Warning: No such workspace: nonexistent
1531 [EOF]
1532 ");
1533 // No workspaces left
1534 let output = main_dir.run_jj(["workspace", "list"]);
1535 insta::assert_snapshot!(output, @"");
1536 }
1537
1538 /// Test forgetting workspace created before workspace store
1539 #[test]
1540 fn test_workspaces_forget_from_before_workspace_store() {
1541 let test_env = TestEnvironment::default();
1542 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1543 let main_dir = test_env.work_dir("main");
1544
1545 main_dir.remove_dir_all(".jj/repo/workspace_store");
1546
1547 let output = main_dir.run_jj(["workspace", "forget"]);
1548 insta::assert_snapshot!(output, @"
1549 ------- stderr -------
1550 Warning: The current workspace 'default' no longer exists after this operation. The working copy was left untouched.
1551 Hint: Restore to an operation that contains the workspace (e.g. `jj undo` or `jj redo`).
1552 [EOF]
1553 ");
1554
1555 // No workspaces left
1556 let output = main_dir.run_jj(["workspace", "list"]);
1557 insta::assert_snapshot!(output, @"");
1558 }
1559
1560 #[test]
1561 fn test_workspaces_forget_nothing_changed() {
1562 let test_env = TestEnvironment::default();
1563 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1564 let main_dir = test_env.work_dir("main");
1565 let output = main_dir.run_jj(["workspace", "forget", "second", "third"]);
1566 insta::assert_snapshot!(output, @"
1567 ------- stderr -------
1568 Warning: No such workspace: second
1569 Warning: No such workspace: third
1570 Nothing changed.
1571 [EOF]
1572 ");
1573 }
1574
1575 #[test]
1576 fn test_workspaces_forget_multi_transaction() {
1577 let test_env = TestEnvironment::default();
1578 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1579 let main_dir = test_env.work_dir("main");
1580
1581 main_dir.write_file("file", "contents");
1582 main_dir.run_jj(["new"]).success();
1583
1584 main_dir.run_jj(["workspace", "add", "../second"]).success();
1585 main_dir.run_jj(["workspace", "add", "../third"]).success();
1586
1587 // there should be three workspaces
1588 let output = main_dir.run_jj(["workspace", "list"]);
1589 insta::assert_snapshot!(output.normalize_backslash(), @"
1590 default: . rlvkpnrz f6bf8819 (empty) (no description set)
1591 second: ../second pmmvwywv 31da1455 (empty) (no description set)
1592 third: ../third rzvqmyuk bf5b5b4d (empty) (no description set)
1593 [EOF]
1594 ");
1595
1596 // delete two at once, in a single tx
1597 main_dir
1598 .run_jj(["workspace", "forget", "second", "third", "fourth"])
1599 .success();
1600 let output = main_dir.run_jj(["workspace", "list"]);
1601 insta::assert_snapshot!(output.normalize_backslash(), @"
1602 default: . rlvkpnrz f6bf8819 (empty) (no description set)
1603 [EOF]
1604 ");
1605
1606 // the op log should have the multiple valid workspaces forgotten in a single tx
1607 let output = main_dir.run_jj(["op", "log", "--limit", "1"]);
1608 insta::assert_snapshot!(output, @"
1609 @ da3075edb24f [email protected] default@ 2001-02-03 04:05:12.000 +07:00 - 2001-02-03 04:05:12.000 +07:00
1610 │ forget workspaces second, third
1611 │ args: jj workspace forget second third fourth
1612 [EOF]
1613 ");
1614
1615 // now, undo, and that should restore both workspaces
1616 main_dir.run_jj(["undo"]).success();
1617
1618 // finally, there should be three workspaces at the end
1619 let output = main_dir.run_jj(["workspace", "list"]);
1620 insta::assert_snapshot!(output.normalize_backslash(), @"
1621 default: . rlvkpnrz f6bf8819 (empty) (no description set)
1622 second: pmmvwywv 31da1455 (empty) (no description set)
1623 third: rzvqmyuk bf5b5b4d (empty) (no description set)
1624 [EOF]
1625 ");
1626 }
1627
1628 #[test]
1629 fn test_workspaces_forget_abandon_commits() {
1630 let test_env = TestEnvironment::default();
1631 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1632 let main_dir = test_env.work_dir("main");
1633
1634 main_dir.write_file("file", "contents");
1635
1636 main_dir.run_jj(["workspace", "add", "../second"]).success();
1637 main_dir.run_jj(["workspace", "add", "../third"]).success();
1638 main_dir.run_jj(["workspace", "add", "../fourth"]).success();
1639 let third_dir = test_env.work_dir("third");
1640 third_dir.run_jj(["edit", "second@"]).success();
1641 let fourth_dir = test_env.work_dir("fourth");
1642 fourth_dir.run_jj(["edit", "second@"]).success();
1643
1644 // there should be four workspaces, three of which are at the same empty commit
1645 let output = main_dir.run_jj(["workspace", "list"]);
1646 insta::assert_snapshot!(output.normalize_backslash(), @"
1647 default: . qpvuntsm 006bd113 (no description set)
1648 fourth: ../fourth uuqppmxq 94f41578 (empty) (no description set)
1649 second: ../second uuqppmxq 94f41578 (empty) (no description set)
1650 third: ../third uuqppmxq 94f41578 (empty) (no description set)
1651 [EOF]
1652 ");
1653 insta::assert_snapshot!(get_log_output(&main_dir), @"
1654 @ 006bd1130b84 default@
1655 │ ○ 94f41578a9e1 fourth@ second@ third@
1656 ├─╯
1657 ◆ 000000000000
1658 [EOF]
1659 ");
1660
1661 // delete the default workspace (should not abandon commit since not empty)
1662 main_dir
1663 .run_jj(["workspace", "forget", "default"])
1664 .success();
1665 insta::assert_snapshot!(get_log_output(&main_dir), @"
1666 ○ 94f41578a9e1 fourth@ second@ third@
1667 │ ○ 006bd1130b84
1668 ├─╯
1669 ◆ 000000000000
1670 [EOF]
1671 ");
1672
1673 // delete the second workspace (should not abandon commit since other workspaces
1674 // still have commit checked out)
1675 main_dir.run_jj(["workspace", "forget", "second"]).success();
1676 insta::assert_snapshot!(get_log_output(&main_dir), @"
1677 ○ 94f41578a9e1 fourth@ third@
1678 │ ○ 006bd1130b84
1679 ├─╯
1680 ◆ 000000000000
1681 [EOF]
1682 ");
1683
1684 // delete the last 2 workspaces (commit should be abandoned now even though
1685 // forgotten in same tx)
1686 main_dir
1687 .run_jj(["workspace", "forget", "third", "fourth"])
1688 .success();
1689 insta::assert_snapshot!(get_log_output(&main_dir), @"
1690 ○ 006bd1130b84
1691 ◆ 000000000000
1692 [EOF]
1693 ");
1694 }
1695
1696 /// Test context of commit summary template
1697 #[test]
1698 fn test_list_workspaces_template() {
1699 let test_env = TestEnvironment::default();
1700 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1701 test_env.add_config(
1702 r#"
1703 templates.workspace_list = """name ++ ": " ++ target.commit_id().short() ++ " " ++
1704 target.description().first_line() ++
1705 if(target.current_working_copy(), " (current)") ++ "\n""""
1706 "#,
1707 );
1708 let main_dir = test_env.work_dir("main");
1709 let secondary_dir = test_env.work_dir("secondary");
1710
1711 main_dir.write_file("file", "contents");
1712 main_dir.run_jj(["commit", "-m", "initial"]).success();
1713 main_dir
1714 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
1715 .success();
1716
1717 // "current_working_copy" should point to the workspace we operate on
1718 let output = main_dir.run_jj(["workspace", "list"]);
1719 insta::assert_snapshot!(output, @"
1720 default: 504e3d8c1bcd (current)
1721 second: 058f604dffcd
1722 [EOF]
1723 ");
1724
1725 let output = secondary_dir.run_jj(["workspace", "list"]);
1726 insta::assert_snapshot!(output, @"
1727 default: 504e3d8c1bcd
1728 second: 058f604dffcd (current)
1729 [EOF]
1730 ");
1731
1732 // Using template option
1733 let template = r#"name ++ ": " ++ target.commit_id().short() ++ "\n""#;
1734 let output = main_dir.run_jj(["workspace", "list", "-T", template]);
1735 insta::assert_snapshot!(output, @"
1736 default: 504e3d8c1bcd
1737 second: 058f604dffcd
1738 [EOF]
1739 ");
1740 }
1741
1742 #[test]
1743 fn test_list_workspaces_template_root() {
1744 let test_env = TestEnvironment::default();
1745 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1746 let main_dir = test_env.work_dir("main");
1747
1748 main_dir
1749 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
1750 .success();
1751
1752 let template = r#"name ++ ": " ++ root ++ "\n""#;
1753 let output = main_dir.run_jj(["workspace", "list", "-T", template]);
1754 insta::assert_snapshot!(output.normalize_backslash(), @"
1755 default: $TEST_ENV/main
1756 second: $TEST_ENV/secondary
1757 [EOF]
1758 ");
1759
1760 let template = r#"name ++ ": " ++ if(root, root ++ " " ++ root.relative()) ++ "\n""#;
1761 let output = main_dir.run_jj(["workspace", "list", "-T", template]);
1762 insta::assert_snapshot!(output.normalize_backslash(), @"
1763 default: $TEST_ENV/main .
1764 second: $TEST_ENV/secondary ../secondary
1765 [EOF]
1766 ");
1767
1768 let template = r#"name ++ ": " ++ if(root, root.relative()) ++ "\n""#;
1769 let output = main_dir.run_jj(["workspace", "list", "-T", template]);
1770 insta::assert_snapshot!(output.normalize_backslash(), @"
1771 default: .
1772 second: ../secondary
1773 [EOF]
1774 ");
1775 }
1776
1777 #[test]
1778 fn test_list_workspaces_template_root_unavailable() {
1779 let test_env = TestEnvironment::default();
1780 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1781 let main_dir = test_env.work_dir("main");
1782
1783 main_dir
1784 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
1785 .success();
1786 std::fs::remove_dir_all(test_env.env_root().join("secondary")).unwrap();
1787
1788 let template = r#"name ++ ": " ++ root ++ "\n""#;
1789 let output = main_dir.run_jj(["workspace", "list", "-T", template]);
1790 insta::assert_snapshot!(output.normalize_backslash(), @"
1791 default: $TEST_ENV/main
1792 second:
1793 [EOF]
1794 ");
1795
1796 let template = r#"name ++ ": " ++ if(root, root.relative()) ++ "\n""#;
1797 let output = main_dir.run_jj(["workspace", "list", "-T", template]);
1798 insta::assert_snapshot!(output, @"
1799 default: .
1800 second:
1801 [EOF]
1802 ");
1803
1804 let output = main_dir.run_jj(["workspace", "list"]);
1805 insta::assert_snapshot!(output, @"
1806 default: . qpvuntsm e8849ae1 (empty) (no description set)
1807 second: uuqppmxq 94f41578 (empty) (no description set)
1808 [EOF]
1809 ");
1810 }
1811
1812 /// Test getting the workspace root from primary and secondary workspaces
1813 #[test]
1814 fn test_workspaces_root() {
1815 let test_env = TestEnvironment::default();
1816 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1817 let main_dir = test_env.work_dir("main");
1818 let secondary_dir = test_env.work_dir("secondary");
1819
1820 let output = main_dir.run_jj(["workspace", "root"]);
1821 insta::assert_snapshot!(output, @"
1822 $TEST_ENV/main
1823 [EOF]
1824 ");
1825 let main_subdir_dir = main_dir.create_dir("subdir");
1826 let output = main_subdir_dir.run_jj(["workspace", "root"]);
1827 insta::assert_snapshot!(output, @"
1828 $TEST_ENV/main
1829 [EOF]
1830 ");
1831
1832 main_dir
1833 .run_jj(["workspace", "add", "--name", "secondary", "../secondary"])
1834 .success();
1835 // Explicitly request root of 'secondary' workspace from main workspace
1836 let output = main_dir.run_jj(["workspace", "root", "--name", "secondary"]);
1837 insta::assert_snapshot!(output, @"
1838 $TEST_ENV/secondary
1839 [EOF]
1840 ");
1841 let output = secondary_dir.run_jj(["workspace", "root"]);
1842 insta::assert_snapshot!(output, @"
1843 $TEST_ENV/secondary
1844 [EOF]
1845 ");
1846 let secondary_subdir_dir = secondary_dir.create_dir("subdir");
1847 let output = secondary_subdir_dir.run_jj(["workspace", "root"]);
1848 insta::assert_snapshot!(output, @"
1849 $TEST_ENV/secondary
1850 [EOF]
1851 ");
1852 }
1853
1854 #[test]
1855 fn test_workspaces_relative_path() -> TestResult {
1856 let test_env = TestEnvironment::default();
1857 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1858 let main_dir = test_env.work_dir("main");
1859
1860 main_dir
1861 .run_jj(["workspace", "add", "../secondary"])
1862 .success();
1863
1864 let repo_file = test_env.env_root().join("secondary/.jj/repo");
1865 let repo_path_bytes = std::fs::read(&repo_file)?;
1866 let stored_path = String::from_utf8(repo_path_bytes)?;
1867 assert_eq!(stored_path, "../../main/.jj/repo");
1868
1869 let secondary_dir = test_env.work_dir("secondary");
1870 let output = secondary_dir.run_jj(["status"]);
1871 insta::assert_snapshot!(output, @"
1872 The working copy has no changes.
1873 Working copy (@) : uuqppmxq 94f41578 (empty) (no description set)
1874 Parent commit (@-): zzzzzzzz 00000000 (empty) (no description set)
1875 [EOF]
1876 ");
1877
1878 let output = main_dir.run_jj(["workspace", "list"]);
1879 insta::assert_snapshot!(output.normalize_backslash(), @"
1880 default: . qpvuntsm e8849ae1 (empty) (no description set)
1881 secondary: ../secondary uuqppmxq 94f41578 (empty) (no description set)
1882 [EOF]
1883 ");
1884
1885 let output = secondary_dir.run_jj(["workspace", "root"]);
1886 insta::assert_snapshot!(output, @"
1887 $TEST_ENV/secondary
1888 [EOF]
1889 ");
1890
1891 let secondary_subdir = secondary_dir.create_dir("subdir");
1892 let output = secondary_subdir.run_jj(["workspace", "root"]);
1893 insta::assert_snapshot!(output, @"
1894 $TEST_ENV/secondary
1895 [EOF]
1896 ");
1897
1898 let output = main_dir.run_jj(["workspace", "root", "--name", "secondary"]);
1899 insta::assert_snapshot!(output, @"
1900 $TEST_ENV/secondary
1901 [EOF]
1902 ");
1903 Ok(())
1904 }
1905
1906 #[test]
1907 fn test_workspaces_root_unavailable() -> TestResult {
1908 let test_env = TestEnvironment::default();
1909 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1910 let main_dir = test_env.work_dir("main");
1911
1912 main_dir
1913 .run_jj(["workspace", "add", "../secondary"])
1914 .success();
1915
1916 std::fs::remove_dir_all(test_env.env_root().join("secondary"))?;
1917
1918 let output = main_dir.run_jj(["workspace", "root", "--name", "secondary"]);
1919 insta::assert_snapshot!(output.normalize_backslash().strip_stderr_last_line(), @"
1920 ------- stderr -------
1921 Error: Cannot resolve absolute workspace path: $TEST_ENV/main/.jj/repo/../../../secondary
1922 [EOF]
1923 [exit status: 1]
1924 ");
1925 Ok(())
1926 }
1927
1928 #[test]
1929 fn test_debug_snapshot() {
1930 let test_env = TestEnvironment::default();
1931 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1932 let work_dir = test_env.work_dir("repo");
1933
1934 work_dir.write_file("file", "contents");
1935 work_dir.run_jj(["debug", "snapshot"]).success();
1936 let output = work_dir.run_jj(["op", "log"]);
1937 insta::assert_snapshot!(output, @"
1938 @ d870c9898b59 [email protected] default@ 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
1939 │ snapshot working copy
1940 │ args: jj debug snapshot
1941 ○ f63ee16f9553 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
1942 │ add workspace 'default'
1943 ○ 000000000000 root()
1944 [EOF]
1945 ");
1946 work_dir.run_jj(["describe", "-m", "initial"]).success();
1947 let output = work_dir.run_jj(["op", "log"]);
1948 insta::assert_snapshot!(output, @"
1949 @ fd7a8fca455a [email protected] default@ 2001-02-03 04:05:10.000 +07:00 - 2001-02-03 04:05:10.000 +07:00
1950 │ describe commit 006bd1130b84e90ab082adeabd7409270d5a86da
1951 │ args: jj describe -m initial
1952 ○ d870c9898b59 [email protected] default@ 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
1953 │ snapshot working copy
1954 │ args: jj debug snapshot
1955 ○ f63ee16f9553 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
1956 │ add workspace 'default'
1957 ○ 000000000000 root()
1958 [EOF]
1959 ");
1960 }
1961
1962 #[test]
1963 fn test_workspaces_rename_nothing_changed() {
1964 let test_env = TestEnvironment::default();
1965 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1966 let main_dir = test_env.work_dir("main");
1967 let output = main_dir.run_jj(["workspace", "rename", "default"]);
1968 insta::assert_snapshot!(output, @"
1969 ------- stderr -------
1970 Nothing changed.
1971 [EOF]
1972 ");
1973 }
1974
1975 #[test]
1976 fn test_workspaces_rename_new_workspace_name_already_used() {
1977 let test_env = TestEnvironment::default();
1978 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1979 let main_dir = test_env.work_dir("main");
1980 main_dir
1981 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
1982 .success();
1983 let output = main_dir.run_jj(["workspace", "rename", "second"]);
1984 insta::assert_snapshot!(output, @"
1985 ------- stderr -------
1986 Error: Failed to rename a workspace
1987 Caused by: Workspace second already exists
1988 [EOF]
1989 [exit status: 1]
1990 ");
1991 }
1992
1993 #[test]
1994 fn test_workspaces_rename_forgotten_workspace() {
1995 let test_env = TestEnvironment::default();
1996 test_env.run_jj_in(".", ["git", "init", "main"]).success();
1997 let main_dir = test_env.work_dir("main");
1998 main_dir
1999 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
2000 .success();
2001 main_dir.run_jj(["workspace", "forget", "second"]).success();
2002 let secondary_dir = test_env.work_dir("secondary");
2003 let output = secondary_dir.run_jj(["workspace", "rename", "third"]);
2004 insta::assert_snapshot!(output, @"
2005 ------- stderr -------
2006 Error: The current workspace 'second' is not tracked in the repo.
2007 [EOF]
2008 [exit status: 1]
2009 ");
2010 }
2011
2012 #[test]
2013 fn test_workspaces_rename_workspace() {
2014 let test_env = TestEnvironment::default();
2015 test_env.run_jj_in(".", ["git", "init", "main"]).success();
2016 let main_dir = test_env.work_dir("main");
2017 main_dir
2018 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
2019 .success();
2020 let secondary_dir = test_env.work_dir("secondary");
2021
2022 // Both workspaces show up when we list them
2023 let output = main_dir.run_jj(["workspace", "list"]);
2024 insta::assert_snapshot!(output.normalize_backslash(), @"
2025 default: . qpvuntsm e8849ae1 (empty) (no description set)
2026 second: ../secondary uuqppmxq 94f41578 (empty) (no description set)
2027 [EOF]
2028 ");
2029
2030 let output = secondary_dir.run_jj(["workspace", "rename", "third"]);
2031 insta::assert_snapshot!(output, @"");
2032
2033 let output = main_dir.run_jj(["workspace", "list"]);
2034 insta::assert_snapshot!(output.normalize_backslash(), @"
2035 default: . qpvuntsm e8849ae1 (empty) (no description set)
2036 third: ../secondary uuqppmxq 94f41578 (empty) (no description set)
2037 [EOF]
2038 ");
2039
2040 // Can see the working-copy commit in each workspace in the log output.
2041 insta::assert_snapshot!(get_log_output(&main_dir), @"
2042 @ e8849ae12c70 default@
2043 │ ○ 94f41578a9e1 third@
2044 ├─╯
2045 ◆ 000000000000
2046 [EOF]
2047 ");
2048 insta::assert_snapshot!(get_log_output(&secondary_dir), @"
2049 @ 94f41578a9e1 third@
2050 │ ○ e8849ae12c70 default@
2051 ├─╯
2052 ◆ 000000000000
2053 [EOF]
2054 ");
2055
2056 // The new workspace root is recorded and accessible
2057 let output = main_dir.run_jj(["workspace", "root", "--name", "secondary"]);
2058 insta::assert_snapshot!(output, @"
2059 ------- stderr -------
2060 Error: No such workspace: secondary
2061 [EOF]
2062 [exit status: 1]
2063 ");
2064 let output = main_dir.run_jj(["workspace", "root", "--name", "third"]);
2065 insta::assert_snapshot!(output, @"
2066 $TEST_ENV/secondary
2067 [EOF]
2068 ");
2069 }
2070
2071 #[test]
2072 fn test_workspaces_rename_workspace_from_before_workspace_store() {
2073 let test_env = TestEnvironment::default();
2074 test_env.run_jj_in(".", ["git", "init", "main"]).success();
2075 let main_dir = test_env.work_dir("main");
2076
2077 main_dir.remove_dir_all(".jj/repo/workspace_store");
2078
2079 let output = main_dir.run_jj(["workspace", "rename", "third"]);
2080 insta::assert_snapshot!(output, @"");
2081
2082 let output = main_dir.run_jj(["workspace", "list"]);
2083 insta::assert_snapshot!(output, @"
2084 third: qpvuntsm e8849ae1 (empty) (no description set)
2085 [EOF]
2086 ");
2087
2088 // The workspace root is not in the store
2089 let output = main_dir.run_jj(["workspace", "root", "--name", "third"]);
2090 insta::assert_snapshot!(output, @"
2091 ------- stderr -------
2092 Error: Workspace has no recorded path: third
2093 [EOF]
2094 [exit status: 1]
2095 ");
2096
2097 let output = main_dir.run_jj(["workspace", "list", "-T", r#"name ++ ": " ++ root ++ "\n""#]);
2098 insta::assert_snapshot!(output, @"
2099 third:
2100 [EOF]
2101 ");
2102 }
2103
2104 #[must_use]
2105 fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput {
2106 let template = r#"
2107 separate(" ",
2108 commit_id.short(),
2109 bookmarks,
2110 working_copies,
2111 if(divergent, "(divergent)"),
2112 surround('"', '"', description.first_line()),
2113 )
2114 "#;
2115 work_dir.run_jj(["log", "-T", template, "-r", "all()"])
2116 }