Debian Codegraph @

cli/tests/test_op_revert_command.rs

raw
1 // Copyright 2025 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use testutils::git;
16
17 use crate::common::CommandOutput;
18 use crate::common::TestEnvironment;
19 use crate::common::TestWorkDir;
20
21 #[test]
22 fn test_revert_root_operation() {
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 // TODO: `jj op revert 'root()'` is not a valid command, so use the
28 // hardcoded root op id here.
29 let output = work_dir.run_jj(["op", "revert", "000000000000"]);
30 insta::assert_snapshot!(output, @"
31 ------- stderr -------
32 Error: Cannot revert root operation
33 [EOF]
34 [exit status: 1]
35 ");
36 }
37
38 #[test]
39 fn test_revert_merge_operation() {
40 let test_env = TestEnvironment::default();
41 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
42 let work_dir = test_env.work_dir("repo");
43
44 work_dir.run_jj(["new"]).success();
45 work_dir.run_jj(["new", "--at-op=@-"]).success();
46 let output = work_dir.run_jj(["op", "revert"]);
47 insta::assert_snapshot!(output, @"
48 ------- stderr -------
49 Concurrent modification detected, resolving automatically.
50 Error: Cannot revert a merge operation
51 [EOF]
52 [exit status: 1]
53 ");
54 }
55
56 #[test]
57 fn test_revert_rewrite_with_child() {
58 // Test that if we revert an operation that rewrote some commit, any descendants
59 // after that will be rebased on top of the un-rewritten commit.
60 let test_env = TestEnvironment::default();
61 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
62 let work_dir = test_env.work_dir("repo");
63
64 work_dir.run_jj(["describe", "-m", "initial"]).success();
65 work_dir.run_jj(["describe", "-m", "modified"]).success();
66 work_dir.run_jj(["new", "-m", "child"]).success();
67 let output = work_dir.run_jj(["log", "-T", "description"]);
68 insta::assert_snapshot!(output, @"
69 @ child
70 ○ modified
71 ◆
72 [EOF]
73 ");
74 work_dir.run_jj(["op", "revert", "@-"]).success();
75
76 // Since we undid the description-change, the child commit should now be on top
77 // of the initial commit
78 let output = work_dir.run_jj(["log", "-T", "description"]);
79 insta::assert_snapshot!(output, @"
80 @ child
81 ○ initial
82 ◆
83 [EOF]
84 ");
85 }
86
87 #[test]
88 fn test_git_push_revert() {
89 let test_env = TestEnvironment::default();
90 test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
91 let git_repo_path = test_env.env_root().join("git-repo");
92 git::init_bare(git_repo_path);
93 test_env
94 .run_jj_in(".", ["git", "clone", "git-repo", "repo"])
95 .success();
96 let work_dir = test_env.work_dir("repo");
97
98 test_env.advance_test_rng_seed_to_multiple_of(100_000);
99 work_dir
100 .run_jj(["bookmark", "create", "-r@", "main"])
101 .success();
102 work_dir.run_jj(["describe", "-m", "AA"]).success();
103 work_dir.run_jj(["git", "push", "--all"]).success();
104 test_env.advance_test_rng_seed_to_multiple_of(100_000);
105 work_dir.run_jj(["describe", "-m", "BB"]).success();
106 // Refs at this point look as follows (-- means no ref)
107 // | jj refs | jj's | git
108 // | | git | repo
109 // | |tracking|
110 // ------------------------------------------
111 // local `main` | BB | -- | --
112 // remote-tracking | AA | AA | AA
113 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
114 main: qpvuntsm d9a9f6a0 (empty) BB
115 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 3a44d6c5 (hidden) (empty) AA
116 [EOF]
117 ");
118 let pre_push_opid = work_dir.current_operation_id();
119 work_dir.run_jj(["git", "push"]).success();
120 // | jj refs | jj's | git
121 // | | git | repo
122 // | |tracking|
123 // ------------------------------------------
124 // local `main` | BB | -- | --
125 // remote-tracking | BB | BB | BB
126 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
127 main: qpvuntsm d9a9f6a0 (empty) BB
128 @origin: qpvuntsm d9a9f6a0 (empty) BB
129 [EOF]
130 ");
131
132 // Revert the push
133 work_dir.run_jj(["op", "restore", &pre_push_opid]).success();
134 // | jj refs | jj's | git
135 // | | git | repo
136 // | |tracking|
137 // ------------------------------------------
138 // local `main` | BB | -- | --
139 // remote-tracking | AA | AA | BB
140 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
141 main: qpvuntsm d9a9f6a0 (empty) BB
142 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 3a44d6c5 (hidden) (empty) AA
143 [EOF]
144 ");
145 test_env.advance_test_rng_seed_to_multiple_of(100_000);
146 work_dir.run_jj(["describe", "-m", "CC"]).success();
147 work_dir.run_jj(["git", "fetch"]).success();
148 // TODO: The user would probably not expect a conflict here. It currently is
149 // because the revert made us forget that the remote was at v2, so the fetch
150 // made us think it updated from v1 to v2 (instead of the no-op it could
151 // have been).
152 //
153 // One option to solve this would be to have `op revert` not restore
154 // remote-tracking bookmarks, but that also has undersired consequences: the
155 // second fetch in `jj git fetch && jj op revert && jj git fetch` would
156 // become a no-op.
157 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
158 main (conflicted):
159 - qpvuntsm/2 3a44d6c5 (hidden) (empty) AA
160 + qpvuntsm/0 1e742089 (divergent) (empty) CC
161 + qpvuntsm/1 d9a9f6a0 (divergent) (empty) BB
162 @origin (behind by 1 commits): qpvuntsm/1 d9a9f6a0 (divergent) (empty) BB
163 [EOF]
164 ");
165 }
166
167 /// This test is identical to the previous one, except for one additional
168 /// import. It demonstrates that this changes the outcome.
169 #[test]
170 fn test_git_push_revert_with_import() {
171 let test_env = TestEnvironment::default();
172 test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
173 let git_repo_path = test_env.env_root().join("git-repo");
174 git::init_bare(git_repo_path);
175 test_env
176 .run_jj_in(".", ["git", "clone", "git-repo", "repo"])
177 .success();
178 let work_dir = test_env.work_dir("repo");
179
180 test_env.advance_test_rng_seed_to_multiple_of(100_000);
181 work_dir
182 .run_jj(["bookmark", "create", "-r@", "main"])
183 .success();
184 work_dir.run_jj(["describe", "-m", "AA"]).success();
185 work_dir.run_jj(["git", "push", "--all"]).success();
186 test_env.advance_test_rng_seed_to_multiple_of(100_000);
187 work_dir.run_jj(["describe", "-m", "BB"]).success();
188 // Refs at this point look as follows (-- means no ref)
189 // | jj refs | jj's | git
190 // | | git | repo
191 // | |tracking|
192 // ------------------------------------------
193 // local `main` | BB | -- | --
194 // remote-tracking | AA | AA | AA
195 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
196 main: qpvuntsm d9a9f6a0 (empty) BB
197 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 3a44d6c5 (hidden) (empty) AA
198 [EOF]
199 ");
200 let pre_push_opid = work_dir.current_operation_id();
201 work_dir.run_jj(["git", "push"]).success();
202 // | jj refs | jj's | git
203 // | | git | repo
204 // | |tracking|
205 // ------------------------------------------
206 // local `main` | BB | -- | --
207 // remote-tracking | BB | BB | BB
208 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
209 main: qpvuntsm d9a9f6a0 (empty) BB
210 @origin: qpvuntsm d9a9f6a0 (empty) BB
211 [EOF]
212 ");
213
214 // Revert the push
215 work_dir.run_jj(["op", "restore", &pre_push_opid]).success();
216 // | jj refs | jj's | git
217 // | | git | repo
218 // | |tracking|
219 // ------------------------------------------
220 // local `main` | BB | -- | --
221 // remote-tracking | AA | AA | BB
222 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
223 main: qpvuntsm d9a9f6a0 (empty) BB
224 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 3a44d6c5 (hidden) (empty) AA
225 [EOF]
226 ");
227
228 // PROBLEM: inserting this import changes the outcome compared to previous test
229 // TODO: decide if this is the better behavior, and whether import of
230 // remote-tracking bookmarks should happen on every operation.
231 work_dir.run_jj(["git", "import"]).success();
232 // | jj refs | jj's | git
233 // | | git | repo
234 // | |tracking|
235 // ------------------------------------------
236 // local `main` | BB | -- | --
237 // remote-tracking | BB | BB | BB
238 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
239 main: qpvuntsm d9a9f6a0 (empty) BB
240 @origin: qpvuntsm d9a9f6a0 (empty) BB
241 [EOF]
242 ");
243 test_env.advance_test_rng_seed_to_multiple_of(100_000);
244 work_dir.run_jj(["describe", "-m", "CC"]).success();
245 work_dir.run_jj(["git", "fetch"]).success();
246 // There is not a conflict. This seems like a good outcome; reverting `git push`
247 // was essentially a no-op.
248 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
249 main: qpvuntsm 1e742089 (empty) CC
250 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 d9a9f6a0 (hidden) (empty) BB
251 [EOF]
252 ");
253 }
254
255 // This test is currently *identical* to `test_git_push_revert` except the repo
256 // it's operating on is colocated.
257 #[test]
258 fn test_git_push_revert_colocated() {
259 let test_env = TestEnvironment::default();
260 test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
261 let git_repo_path = test_env.env_root().join("git-repo");
262 git::init_bare(git_repo_path.clone());
263 let work_dir = test_env.work_dir("clone");
264 git::clone(work_dir.root(), git_repo_path.to_str().unwrap(), None);
265
266 work_dir.run_jj(["git", "init", "--git-repo=."]).success();
267
268 test_env.advance_test_rng_seed_to_multiple_of(100_000);
269 work_dir
270 .run_jj(["bookmark", "create", "-r@", "main"])
271 .success();
272 work_dir.run_jj(["describe", "-m", "AA"]).success();
273 work_dir.run_jj(["git", "push", "--all"]).success();
274 test_env.advance_test_rng_seed_to_multiple_of(100_000);
275 work_dir.run_jj(["describe", "-m", "BB"]).success();
276 // Refs at this point look as follows (-- means no ref)
277 // | jj refs | jj's | git
278 // | | git | repo
279 // | |tracking|
280 // ------------------------------------------
281 // local `main` | BB | BB | BB
282 // remote-tracking | AA | AA | AA
283 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
284 main: qpvuntsm d9a9f6a0 (empty) BB
285 @git: qpvuntsm d9a9f6a0 (empty) BB
286 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 3a44d6c5 (hidden) (empty) AA
287 [EOF]
288 ");
289 let pre_push_opid = work_dir.current_operation_id();
290 work_dir.run_jj(["git", "push"]).success();
291 // | jj refs | jj's | git
292 // | | git | repo
293 // | |tracking|
294 // ------------------------------------------
295 // local `main` | BB | BB | BB
296 // remote-tracking | BB | BB | BB
297 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
298 main: qpvuntsm d9a9f6a0 (empty) BB
299 @git: qpvuntsm d9a9f6a0 (empty) BB
300 @origin: qpvuntsm d9a9f6a0 (empty) BB
301 [EOF]
302 ");
303
304 // Revert the push
305 work_dir.run_jj(["op", "restore", &pre_push_opid]).success();
306 // === Before auto-export ====
307 // | jj refs | jj's | git
308 // | | git | repo
309 // | |tracking|
310 // ------------------------------------------
311 // local `main` | BB | BB | BB
312 // remote-tracking | AA | BB | BB
313 // === After automatic `jj git export` ====
314 // | jj refs | jj's | git
315 // | | git | repo
316 // | |tracking|
317 // ------------------------------------------
318 // local `main` | BB | BB | BB
319 // remote-tracking | AA | AA | AA
320 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
321 main: qpvuntsm d9a9f6a0 (empty) BB
322 @git: qpvuntsm d9a9f6a0 (empty) BB
323 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 3a44d6c5 (hidden) (empty) AA
324 [EOF]
325 ");
326 test_env.advance_test_rng_seed_to_multiple_of(100_000);
327 work_dir.run_jj(["describe", "-m", "CC"]).success();
328 work_dir.run_jj(["git", "fetch"]).success();
329 // We have the same conflict as `test_git_push_revert`. TODO: why did we get the
330 // same result in a seemingly different way?
331 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
332 main (conflicted):
333 - qpvuntsm/2 3a44d6c5 (hidden) (empty) AA
334 + qpvuntsm/0 1e742089 (divergent) (empty) CC
335 + qpvuntsm/1 d9a9f6a0 (divergent) (empty) BB
336 @git (behind by 1 commits): qpvuntsm/0 1e742089 (divergent) (empty) CC
337 @origin (behind by 1 commits): qpvuntsm/1 d9a9f6a0 (divergent) (empty) BB
338 [EOF]
339 ");
340 }
341
342 // This test is currently *identical* to `test_git_push_revert` except
343 // both the git_refs and the remote-tracking bookmarks are preserved by revert.
344 // TODO: Investigate the different outcome
345 #[test]
346 fn test_git_push_revert_repo_only() {
347 let test_env = TestEnvironment::default();
348 test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
349 let git_repo_path = test_env.env_root().join("git-repo");
350 git::init_bare(git_repo_path);
351 test_env
352 .run_jj_in(".", ["git", "clone", "git-repo", "repo"])
353 .success();
354 let work_dir = test_env.work_dir("repo");
355
356 test_env.advance_test_rng_seed_to_multiple_of(100_000);
357 work_dir
358 .run_jj(["bookmark", "create", "-r@", "main"])
359 .success();
360 work_dir.run_jj(["describe", "-m", "AA"]).success();
361 work_dir.run_jj(["git", "push", "--all"]).success();
362 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
363 main: qpvuntsm 3a44d6c5 (empty) AA
364 @origin: qpvuntsm 3a44d6c5 (empty) AA
365 [EOF]
366 ");
367 test_env.advance_test_rng_seed_to_multiple_of(100_000);
368 work_dir.run_jj(["describe", "-m", "BB"]).success();
369 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
370 main: qpvuntsm d9a9f6a0 (empty) BB
371 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 3a44d6c5 (hidden) (empty) AA
372 [EOF]
373 ");
374 let pre_push_opid = work_dir.current_operation_id();
375 work_dir.run_jj(["git", "push"]).success();
376
377 // Revert the push, but keep both the git_refs and the remote-tracking bookmarks
378 work_dir
379 .run_jj(["op", "restore", "--what=repo", &pre_push_opid])
380 .success();
381 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
382 main: qpvuntsm d9a9f6a0 (empty) BB
383 @origin: qpvuntsm d9a9f6a0 (empty) BB
384 [EOF]
385 ");
386 test_env.advance_test_rng_seed_to_multiple_of(100_000);
387 work_dir.run_jj(["describe", "-m", "CC"]).success();
388 work_dir.run_jj(["git", "fetch"]).success();
389 // This currently gives an identical result to `test_git_push_revert_import`.
390 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
391 main: qpvuntsm 1e742089 (empty) CC
392 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/1 d9a9f6a0 (hidden) (empty) BB
393 [EOF]
394 ");
395
396 // Revert the remote-tracking, without modifying the repo
397 work_dir
398 .run_jj(["op", "restore", "--what=remote-tracking", &pre_push_opid])
399 .success();
400 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
401 main: qpvuntsm 1e742089 (empty) CC
402 @origin (ahead by 1 commits, behind by 1 commits): qpvuntsm/2 3a44d6c5 (hidden) (empty) AA
403 [EOF]
404 ");
405 }
406
407 #[test]
408 fn test_bookmark_track_untrack_revert() {
409 let test_env = TestEnvironment::default();
410 test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
411 let git_repo_path = test_env.env_root().join("git-repo");
412 git::init_bare(git_repo_path);
413 test_env
414 .run_jj_in(".", ["git", "clone", "git-repo", "repo"])
415 .success();
416 let work_dir = test_env.work_dir("repo");
417
418 work_dir.run_jj(["describe", "-mcommit"]).success();
419 work_dir
420 .run_jj(["bookmark", "create", "-r@", "feature1", "feature2"])
421 .success();
422 work_dir.run_jj(["git", "push", "--all"]).success();
423 work_dir
424 .run_jj(["bookmark", "delete", "feature2"])
425 .success();
426 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
427 feature1: qpvuntsm bab5b5ef (empty) commit
428 @origin: qpvuntsm bab5b5ef (empty) commit
429 feature2 (deleted)
430 @origin: qpvuntsm bab5b5ef (empty) commit
431 [EOF]
432 ");
433
434 // Track/untrack can be reverted so long as states can be trivially merged.
435 work_dir
436 .run_jj(["bookmark", "untrack", "feature1 | feature2"])
437 .success();
438 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
439 feature1: qpvuntsm bab5b5ef (empty) commit
440 feature1@origin: qpvuntsm bab5b5ef (empty) commit
441 feature2@origin: qpvuntsm bab5b5ef (empty) commit
442 [EOF]
443 ");
444
445 work_dir.run_jj(["op", "revert"]).success();
446 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
447 feature1: qpvuntsm bab5b5ef (empty) commit
448 @origin: qpvuntsm bab5b5ef (empty) commit
449 feature2 (deleted)
450 @origin: qpvuntsm bab5b5ef (empty) commit
451 [EOF]
452 ");
453
454 work_dir.run_jj(["op", "revert"]).success();
455 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
456 feature1: qpvuntsm bab5b5ef (empty) commit
457 feature1@origin: qpvuntsm bab5b5ef (empty) commit
458 feature2@origin: qpvuntsm bab5b5ef (empty) commit
459 [EOF]
460 ");
461
462 work_dir.run_jj(["bookmark", "track", "feature1"]).success();
463 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
464 feature1: qpvuntsm bab5b5ef (empty) commit
465 @origin: qpvuntsm bab5b5ef (empty) commit
466 feature2@origin: qpvuntsm bab5b5ef (empty) commit
467 [EOF]
468 ");
469
470 work_dir.run_jj(["op", "revert"]).success();
471 insta::assert_snapshot!(get_bookmark_output(&work_dir), @"
472 feature1: qpvuntsm bab5b5ef (empty) commit
473 feature1@origin: qpvuntsm bab5b5ef (empty) commit
474 feature2@origin: qpvuntsm bab5b5ef (empty) commit
475 [EOF]
476 ");
477 }
478
479 #[must_use]
480 fn get_bookmark_output(work_dir: &TestWorkDir) -> CommandOutput {
481 // --quiet to suppress deleted bookmarks hint
482 work_dir.run_jj(["bookmark", "list", "--all-remotes", "--quiet"])
483 }