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