| 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 crate::common::CommandOutput; |
| 16 |
use crate::common::TestEnvironment; |
| 17 |
use crate::common::TestWorkDir; |
| 18 |
use crate::common::create_commit_with_files; |
| 19 |
|
| 20 |
#[test] |
| 21 |
fn test_revert() { |
| 22 |
let test_env = TestEnvironment::default(); |
| 23 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 24 |
let work_dir = test_env.work_dir("repo"); |
| 25 |
|
| 26 |
create_commit_with_files(&work_dir, "a", &[], &[("a", "a\n")]); |
| 27 |
create_commit_with_files(&work_dir, "b", &["a"], &[]); |
| 28 |
create_commit_with_files(&work_dir, "c", &["b"], &[]); |
| 29 |
create_commit_with_files(&work_dir, "d", &["c"], &[]); |
| 30 |
// Test the setup |
| 31 |
insta::assert_snapshot!(get_log_output(&work_dir), @" |
| 32 |
@ 98fb6151f954 d |
| 33 |
○ 96ff42270bbc c |
| 34 |
○ 58aaf278bf58 b |
| 35 |
○ 7d980be7a1d4 a |
| 36 |
◆ 000000000000 |
| 37 |
[EOF] |
| 38 |
"); |
| 39 |
let output = work_dir.run_jj(["diff", "-ra", "-s"]); |
| 40 |
insta::assert_snapshot!(output, @" |
| 41 |
A a |
| 42 |
[EOF] |
| 43 |
"); |
| 44 |
let setup_opid = work_dir.current_operation_id(); |
| 45 |
|
| 46 |
// Reverting without a location is an error |
| 47 |
let output = work_dir.run_jj(["revert", "-ra"]); |
| 48 |
insta::assert_snapshot!(output, @" |
| 49 |
------- stderr ------- |
| 50 |
error: the following required arguments were not provided: |
| 51 |
<--onto <REVSETS>|--insert-after <REVSETS>|--insert-before <REVSETS>> |
| 52 |
|
| 53 |
Usage: jj revert --revision <REVSETS> <--onto <REVSETS>|--insert-after <REVSETS>|--insert-before <REVSETS>> |
| 54 |
|
| 55 |
For more information, try '--help'. |
| 56 |
[EOF] |
| 57 |
[exit status: 2] |
| 58 |
"); |
| 59 |
|
| 60 |
// Revert the commit with `--onto` |
| 61 |
let output = work_dir.run_jj(["revert", "-ra", "-d@"]); |
| 62 |
insta::assert_snapshot!(output, @r#" |
| 63 |
------- stderr ------- |
| 64 |
Reverted 1 commits as follows: |
| 65 |
wqnwkozp 64910788 Revert "a" |
| 66 |
[EOF] |
| 67 |
"#); |
| 68 |
insta::assert_snapshot!(get_log_output(&work_dir), @r#" |
| 69 |
○ 64910788f8a5 Revert "a" |
| 70 |
│ |
| 71 |
│ This reverts commit 7d980be7a1d499e4d316ab4c01242885032f7eaf. |
| 72 |
@ 98fb6151f954 d |
| 73 |
○ 96ff42270bbc c |
| 74 |
○ 58aaf278bf58 b |
| 75 |
○ 7d980be7a1d4 a |
| 76 |
◆ 000000000000 |
| 77 |
[EOF] |
| 78 |
"#); |
| 79 |
let output = work_dir.run_jj(["diff", "-s", "-r@+"]); |
| 80 |
insta::assert_snapshot!(output, @" |
| 81 |
D a |
| 82 |
[EOF] |
| 83 |
"); |
| 84 |
|
| 85 |
// Revert the new reverted commit |
| 86 |
let output = work_dir.run_jj(["revert", "-r@+", "-d@+"]); |
| 87 |
insta::assert_snapshot!(output, @r#" |
| 88 |
------- stderr ------- |
| 89 |
Reverted 1 commits as follows: |
| 90 |
nkmrtpmo 90d12316 Revert "Revert "a"" |
| 91 |
[EOF] |
| 92 |
"#); |
| 93 |
insta::assert_snapshot!(get_log_output(&work_dir), @r#" |
| 94 |
○ 90d123162199 Revert "Revert "a"" |
| 95 |
│ |
| 96 |
│ This reverts commit 64910788f8a5d322739e1e38ef35f7d06ea4b38d. |
| 97 |
○ 64910788f8a5 Revert "a" |
| 98 |
│ |
| 99 |
│ This reverts commit 7d980be7a1d499e4d316ab4c01242885032f7eaf. |
| 100 |
@ 98fb6151f954 d |
| 101 |
○ 96ff42270bbc c |
| 102 |
○ 58aaf278bf58 b |
| 103 |
○ 7d980be7a1d4 a |
| 104 |
◆ 000000000000 |
| 105 |
[EOF] |
| 106 |
"#); |
| 107 |
let output = work_dir.run_jj(["diff", "-s", "-r@++"]); |
| 108 |
insta::assert_snapshot!(output, @" |
| 109 |
A a |
| 110 |
[EOF] |
| 111 |
"); |
| 112 |
work_dir.run_jj(["op", "restore", &setup_opid]).success(); |
| 113 |
|
| 114 |
// Revert the commit with `--insert-after` |
| 115 |
let output = work_dir.run_jj(["revert", "-ra", "-Ab"]); |
| 116 |
insta::assert_snapshot!(output, @r#" |
| 117 |
------- stderr ------- |
| 118 |
Reverted 1 commits as follows: |
| 119 |
nmzmmopx 9e7b8585 Revert "a" |
| 120 |
Rebased 2 descendant commits. |
| 121 |
Working copy (@) now at: vruxwmqv b1885396 d | (empty) d |
| 122 |
Parent commit (@-) : royxmykx efc4bd83 c | (empty) c |
| 123 |
Added 0 files, modified 0 files, removed 1 files |
| 124 |
[EOF] |
| 125 |
"#); |
| 126 |
insta::assert_snapshot!(get_log_output(&work_dir), @r#" |
| 127 |
@ b18853966f79 d |
| 128 |
○ efc4bd83159f c |
| 129 |
○ 9e7b85853718 Revert "a" |
| 130 |
│ |
| 131 |
│ This reverts commit 7d980be7a1d499e4d316ab4c01242885032f7eaf. |
| 132 |
○ 58aaf278bf58 b |
| 133 |
○ 7d980be7a1d4 a |
| 134 |
◆ 000000000000 |
| 135 |
[EOF] |
| 136 |
"#); |
| 137 |
let output = work_dir.run_jj(["diff", "-s", "-rb+"]); |
| 138 |
insta::assert_snapshot!(output, @" |
| 139 |
D a |
| 140 |
[EOF] |
| 141 |
"); |
| 142 |
work_dir.run_jj(["op", "restore", &setup_opid]).success(); |
| 143 |
|
| 144 |
// Revert the commit with `--insert-before` |
| 145 |
let output = work_dir.run_jj(["revert", "-ra", "-Bd"]); |
| 146 |
insta::assert_snapshot!(output, @r#" |
| 147 |
------- stderr ------- |
| 148 |
Reverted 1 commits as follows: |
| 149 |
pzsxstzt d51ea564 Revert "a" |
| 150 |
Rebased 1 descendant commits. |
| 151 |
Working copy (@) now at: vruxwmqv 5c5d60a6 d | (empty) d |
| 152 |
Parent commit (@-) : pzsxstzt d51ea564 Revert "a" |
| 153 |
Added 0 files, modified 0 files, removed 1 files |
| 154 |
[EOF] |
| 155 |
"#); |
| 156 |
insta::assert_snapshot!(get_log_output(&work_dir), @r#" |
| 157 |
@ 5c5d60a69afd d |
| 158 |
○ d51ea56444ce Revert "a" |
| 159 |
│ |
| 160 |
│ This reverts commit 7d980be7a1d499e4d316ab4c01242885032f7eaf. |
| 161 |
○ 96ff42270bbc c |
| 162 |
○ 58aaf278bf58 b |
| 163 |
○ 7d980be7a1d4 a |
| 164 |
◆ 000000000000 |
| 165 |
[EOF] |
| 166 |
"#); |
| 167 |
let output = work_dir.run_jj(["diff", "-s", "-rd-"]); |
| 168 |
insta::assert_snapshot!(output, @" |
| 169 |
D a |
| 170 |
[EOF] |
| 171 |
"); |
| 172 |
work_dir.run_jj(["op", "restore", &setup_opid]).success(); |
| 173 |
|
| 174 |
// Revert the commit with `--insert-after` and `--insert-before` |
| 175 |
let output = work_dir.run_jj(["revert", "-ra", "-Aa", "-Bd"]); |
| 176 |
insta::assert_snapshot!(output, @r#" |
| 177 |
------- stderr ------- |
| 178 |
Reverted 1 commits as follows: |
| 179 |
oupztwtk d311a8f0 Revert "a" |
| 180 |
Rebased 1 descendant commits. |
| 181 |
Working copy (@) now at: vruxwmqv 5b97d572 d | (empty) d |
| 182 |
Parent commit (@-) : royxmykx 96ff4227 c | (empty) c |
| 183 |
Parent commit (@-) : oupztwtk d311a8f0 Revert "a" |
| 184 |
Added 0 files, modified 0 files, removed 1 files |
| 185 |
[EOF] |
| 186 |
"#); |
| 187 |
insta::assert_snapshot!(get_log_output(&work_dir), @r#" |
| 188 |
@ 5b97d572e457 d |
| 189 |
├─╮ |
| 190 |
│ ○ d311a8f0c13f Revert "a" |
| 191 |
│ │ |
| 192 |
│ │ This reverts commit 7d980be7a1d499e4d316ab4c01242885032f7eaf. |
| 193 |
○ │ 96ff42270bbc c |
| 194 |
○ │ 58aaf278bf58 b |
| 195 |
├─╯ |
| 196 |
○ 7d980be7a1d4 a |
| 197 |
◆ 000000000000 |
| 198 |
[EOF] |
| 199 |
"#); |
| 200 |
let output = work_dir.run_jj(["diff", "-s", "-r", "a+ & d-"]); |
| 201 |
insta::assert_snapshot!(output, @" |
| 202 |
D a |
| 203 |
[EOF] |
| 204 |
"); |
| 205 |
work_dir.run_jj(["op", "restore", &setup_opid]).success(); |
| 206 |
|
| 207 |
// Revert nothing |
| 208 |
let output = work_dir.run_jj(["revert", "-r", "none()", "-d", "@"]); |
| 209 |
insta::assert_snapshot!(output, @" |
| 210 |
------- stderr ------- |
| 211 |
No revisions to revert. |
| 212 |
[EOF] |
| 213 |
"); |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn test_revert_multiple() { |
| 218 |
let test_env = TestEnvironment::default(); |
| 219 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 220 |
let work_dir = test_env.work_dir("repo"); |
| 221 |
|
| 222 |
create_commit_with_files(&work_dir, "a", &[], &[("a", "a\n")]); |
| 223 |
create_commit_with_files(&work_dir, "b", &["a"], &[("a", "a\nb\n")]); |
| 224 |
create_commit_with_files(&work_dir, "c", &["b"], &[("a", "a\nb\n"), ("b", "b\n")]); |
| 225 |
create_commit_with_files(&work_dir, "d", &["c"], &[]); |
| 226 |
create_commit_with_files(&work_dir, "e", &["d"], &[("a", "a\nb\nc\n")]); |
| 227 |
|
| 228 |
// Test the setup |
| 229 |
insta::assert_snapshot!(get_log_output(&work_dir), @" |
| 230 |
@ 51a01d6d8cc4 e |
| 231 |
○ 4b9d123d3b33 d |
| 232 |
○ 05e1f540476f c |
| 233 |
○ f93a910dbdf0 b |
| 234 |
○ 7d980be7a1d4 a |
| 235 |
◆ 000000000000 |
| 236 |
[EOF] |
| 237 |
"); |
| 238 |
|
| 239 |
// Revert multiple commits |
| 240 |
let output = work_dir.run_jj(["revert", "-rb", "-rc", "-re", "-d@"]); |
| 241 |
insta::assert_snapshot!(output, @r#" |
| 242 |
------- stderr ------- |
| 243 |
Reverted 3 commits as follows: |
| 244 |
wqnwkozp 4329cf72 Revert "e" |
| 245 |
mouksmqu 092f722e Revert "c" |
| 246 |
tqvpomtp c90eef02 Revert "b" |
| 247 |
[EOF] |
| 248 |
"#); |
| 249 |
insta::assert_snapshot!(get_log_output(&work_dir), @r#" |
| 250 |
○ c90eef022369 Revert "b" |
| 251 |
│ |
| 252 |
│ This reverts commit f93a910dbdf0f841e6cf2bc0ab0ba4c336d6f436. |
| 253 |
○ 092f722e521f Revert "c" |
| 254 |
│ |
| 255 |
│ This reverts commit 05e1f540476f8c4207ff44febbe2ce6e6696dc4b. |
| 256 |
○ 4329cf7230d7 Revert "e" |
| 257 |
│ |
| 258 |
│ This reverts commit 51a01d6d8cc48a296cb87f8383b34ade3c050363. |
| 259 |
@ 51a01d6d8cc4 e |
| 260 |
○ 4b9d123d3b33 d |
| 261 |
○ 05e1f540476f c |
| 262 |
○ f93a910dbdf0 b |
| 263 |
○ 7d980be7a1d4 a |
| 264 |
◆ 000000000000 |
| 265 |
[EOF] |
| 266 |
"#); |
| 267 |
// View the output of each reverted commit |
| 268 |
let output = work_dir.run_jj(["show", "@+"]); |
| 269 |
insta::assert_snapshot!(output, @r#" |
| 270 |
Commit ID: 4329cf7230d7b8229e9c88087cfa2f8aa13a1317 |
| 271 |
Change ID: wqnwkozpkustnxypnnntnykwrqrkrpvv |
| 272 |
Author : Test User <[email protected]> (2001-02-03 08:05:19) |
| 273 |
Committer: Test User <[email protected]> (2001-02-03 08:05:19) |
| 274 |
|
| 275 |
Revert "e" |
| 276 |
|
| 277 |
This reverts commit 51a01d6d8cc48a296cb87f8383b34ade3c050363. |
| 278 |
|
| 279 |
Modified regular file a: |
| 280 |
1 1: a |
| 281 |
2 2: b |
| 282 |
3 : c |
| 283 |
[EOF] |
| 284 |
"#); |
| 285 |
let output = work_dir.run_jj(["show", "@++"]); |
| 286 |
insta::assert_snapshot!(output, @r#" |
| 287 |
Commit ID: 092f722e521fe49fde5a3830568fe1c51b8f2f5f |
| 288 |
Change ID: mouksmquosnpvwqrpsvvxtxpywpnxlss |
| 289 |
Author : Test User <[email protected]> (2001-02-03 08:05:19) |
| 290 |
Committer: Test User <[email protected]> (2001-02-03 08:05:19) |
| 291 |
|
| 292 |
Revert "c" |
| 293 |
|
| 294 |
This reverts commit 05e1f540476f8c4207ff44febbe2ce6e6696dc4b. |
| 295 |
|
| 296 |
Removed regular file b: |
| 297 |
1 : b |
| 298 |
[EOF] |
| 299 |
"#); |
| 300 |
let output = work_dir.run_jj(["show", "@+++"]); |
| 301 |
insta::assert_snapshot!(output, @r#" |
| 302 |
Commit ID: c90eef022369d43d5eae8303101b5889b4b73963 |
| 303 |
Change ID: tqvpomtpwrqsylrpsxknultrymmqxmxv |
| 304 |
Author : Test User <[email protected]> (2001-02-03 08:05:19) |
| 305 |
Committer: Test User <[email protected]> (2001-02-03 08:05:19) |
| 306 |
|
| 307 |
Revert "b" |
| 308 |
|
| 309 |
This reverts commit f93a910dbdf0f841e6cf2bc0ab0ba4c336d6f436. |
| 310 |
|
| 311 |
Modified regular file a: |
| 312 |
1 1: a |
| 313 |
2 : b |
| 314 |
[EOF] |
| 315 |
"#); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
fn test_revert_description_template() { |
| 320 |
let test_env = TestEnvironment::default(); |
| 321 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 322 |
test_env.add_config( |
| 323 |
r#" |
| 324 |
[templates] |
| 325 |
revert_description = ''' |
| 326 |
separate(" ", |
| 327 |
"Revert commit", |
| 328 |
commit_id.short(), |
| 329 |
'"' ++ description.first_line() ++ '"', |
| 330 |
) |
| 331 |
''' |
| 332 |
"#, |
| 333 |
); |
| 334 |
let work_dir = test_env.work_dir("repo"); |
| 335 |
create_commit_with_files(&work_dir, "a", &[], &[("a", "a\n")]); |
| 336 |
|
| 337 |
// Test the setup |
| 338 |
insta::assert_snapshot!(get_log_output(&work_dir), @" |
| 339 |
@ 7d980be7a1d4 a |
| 340 |
◆ 000000000000 |
| 341 |
[EOF] |
| 342 |
"); |
| 343 |
let output = work_dir.run_jj(["diff", "-s"]); |
| 344 |
insta::assert_snapshot!(output, @" |
| 345 |
A a |
| 346 |
[EOF] |
| 347 |
"); |
| 348 |
|
| 349 |
// Verify that message of reverted commit follows the template |
| 350 |
let output = work_dir.run_jj(["revert", "-r@", "-d@"]); |
| 351 |
insta::assert_snapshot!(output, @r#" |
| 352 |
------- stderr ------- |
| 353 |
Reverted 1 commits as follows: |
| 354 |
royxmykx 6bfb98a3 Revert commit 7d980be7a1d4 "a" |
| 355 |
[EOF] |
| 356 |
"#); |
| 357 |
insta::assert_snapshot!(get_log_output(&work_dir), @r#" |
| 358 |
○ 6bfb98a33f58 Revert commit 7d980be7a1d4 "a" |
| 359 |
@ 7d980be7a1d4 a |
| 360 |
◆ 000000000000 |
| 361 |
[EOF] |
| 362 |
"#); |
| 363 |
} |
| 364 |
|
| 365 |
#[test] |
| 366 |
fn test_revert_with_conflict() { |
| 367 |
let test_env = TestEnvironment::default(); |
| 368 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 369 |
let work_dir = test_env.work_dir("repo"); |
| 370 |
|
| 371 |
create_commit_with_files(&work_dir, "a", &[], &[("a", "a\n")]); |
| 372 |
create_commit_with_files(&work_dir, "b", &["a"], &[("a", "a\nb\n")]); |
| 373 |
create_commit_with_files(&work_dir, "c", &["b"], &[("a", "a\nb\nc\n")]); |
| 374 |
|
| 375 |
// Test the setup |
| 376 |
insta::assert_snapshot!(get_log_output(&work_dir), @" |
| 377 |
@ 48b910edc43e c |
| 378 |
○ f93a910dbdf0 b |
| 379 |
○ 7d980be7a1d4 a |
| 380 |
◆ 000000000000 |
| 381 |
[EOF] |
| 382 |
"); |
| 383 |
|
| 384 |
// Create a conflict by reverting B onto C |
| 385 |
let output = work_dir.run_jj(["revert", "-r=b", "--onto=c"]); |
| 386 |
insta::assert_snapshot!(output, @r#" |
| 387 |
------- stderr ------- |
| 388 |
Reverted 1 commits as follows: |
| 389 |
yostqsxw eadfde9a (conflict) Revert "b" |
| 390 |
New conflicts appeared in 1 commits: |
| 391 |
yostqsxw eadfde9a (conflict) Revert "b" |
| 392 |
Hint: To resolve the conflicts, start by creating a commit on top of |
| 393 |
the conflicted commit: |
| 394 |
jj new yostqsxw |
| 395 |
Then use `jj resolve`, or edit the conflict markers in the file directly. |
| 396 |
Once the conflicts are resolved, you can inspect the result with `jj diff`. |
| 397 |
Then run `jj squash` to move the resolution into the conflicted commit. |
| 398 |
[EOF] |
| 399 |
"#); |
| 400 |
insta::assert_snapshot!(get_log_output(&work_dir), @r#" |
| 401 |
× eadfde9a48f3 Revert "b" |
| 402 |
│ |
| 403 |
│ This reverts commit f93a910dbdf0f841e6cf2bc0ab0ba4c336d6f436. |
| 404 |
@ 48b910edc43e c |
| 405 |
○ f93a910dbdf0 b |
| 406 |
○ 7d980be7a1d4 a |
| 407 |
◆ 000000000000 |
| 408 |
[EOF] |
| 409 |
"#); |
| 410 |
// Reverted commit should contain conflict markers |
| 411 |
let output = work_dir.run_jj(["file", "show", "-r=@+", "a"]); |
| 412 |
insta::assert_snapshot!(output, @r#" |
| 413 |
a |
| 414 |
<<<<<<< conflict 1 of 1 |
| 415 |
%%%%%%% diff from: zsuskuln f93a910d "b" (reverted revision) |
| 416 |
\\\\\\\ to: royxmykx 48b910ed "c" (revert destination) |
| 417 |
b |
| 418 |
+c |
| 419 |
+++++++ rlvkpnrz 7d980be7 "a" (parents of reverted revision) |
| 420 |
>>>>>>> conflict 1 of 1 ends |
| 421 |
[EOF] |
| 422 |
"#); |
| 423 |
} |
| 424 |
|
| 425 |
#[must_use] |
| 426 |
fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput { |
| 427 |
let template = r#"commit_id.short() ++ " " ++ description"#; |
| 428 |
work_dir.run_jj(["log", "-T", template]) |
| 429 |
} |