| 1 |
// Copyright 2022 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use crate::common::CommandOutput; |
| 16 |
use crate::common::TestEnvironment; |
| 17 |
use crate::common::TestWorkDir; |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn test_edit() { |
| 21 |
let test_env = TestEnvironment::default(); |
| 22 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 23 |
let work_dir = test_env.work_dir("repo"); |
| 24 |
work_dir.write_file("file1", "0"); |
| 25 |
work_dir.run_jj(["commit", "-m", "first"]).success(); |
| 26 |
work_dir.run_jj(["describe", "-m", "second"]).success(); |
| 27 |
work_dir.write_file("file1", "1"); |
| 28 |
|
| 29 |
// Errors out without argument |
| 30 |
let output = work_dir.run_jj(["edit"]); |
| 31 |
insta::assert_snapshot!(output, @" |
| 32 |
------- stderr ------- |
| 33 |
error: the following required arguments were not provided: |
| 34 |
<REVSET|-r <REVSET>> |
| 35 |
|
| 36 |
Usage: jj edit <REVSET|-r <REVSET>> |
| 37 |
|
| 38 |
For more information, try '--help'. |
| 39 |
[EOF] |
| 40 |
[exit status: 2] |
| 41 |
"); |
| 42 |
|
| 43 |
// Errors out with duplicated arguments |
| 44 |
let output = work_dir.run_jj(["edit", "@", "-r@"]); |
| 45 |
insta::assert_snapshot!(output, @" |
| 46 |
------- stderr ------- |
| 47 |
error: the argument '[REVSET]' cannot be used with '-r <REVSET>' |
| 48 |
|
| 49 |
Usage: jj edit <REVSET|-r <REVSET>> |
| 50 |
|
| 51 |
For more information, try '--help'. |
| 52 |
[EOF] |
| 53 |
[exit status: 2] |
| 54 |
"); |
| 55 |
|
| 56 |
// Makes the specified commit the working-copy commit |
| 57 |
let output = work_dir.run_jj(["edit", "@-"]); |
| 58 |
insta::assert_snapshot!(output, @" |
| 59 |
------- stderr ------- |
| 60 |
Working copy (@) now at: qpvuntsm 1f6994f8 first |
| 61 |
Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set) |
| 62 |
Added 0 files, modified 1 files, removed 0 files |
| 63 |
[EOF] |
| 64 |
"); |
| 65 |
let output = get_log_output(&work_dir); |
| 66 |
insta::assert_snapshot!(output, @" |
| 67 |
○ adc8b2014db6 second |
| 68 |
@ 1f6994f8b95b first |
| 69 |
◆ 000000000000 |
| 70 |
[EOF] |
| 71 |
"); |
| 72 |
insta::assert_snapshot!(work_dir.read_file("file1"), @"0"); |
| 73 |
|
| 74 |
// Changes in the working copy are amended into the commit |
| 75 |
work_dir.write_file("file2", "0"); |
| 76 |
let output = get_log_output(&work_dir); |
| 77 |
insta::assert_snapshot!(output, @r" |
| 78 |
○ 29c4ac30d18f second |
| 79 |
@ b10da1f49611 first |
| 80 |
◆ 000000000000 |
| 81 |
[EOF] |
| 82 |
------- stderr ------- |
| 83 |
Rebased 1 descendant commits onto updated working copy. |
| 84 |
[EOF] |
| 85 |
"); |
| 86 |
|
| 87 |
// Cannot edit multiple revisions |
| 88 |
let output = work_dir.run_jj(["edit", "-r::"]); |
| 89 |
insta::assert_snapshot!(output, @" |
| 90 |
------- stderr ------- |
| 91 |
Error: Revset `::` resolved to more than one revision |
| 92 |
Hint: The revset `::` resolved to these revisions: |
| 93 |
rlvkpnrz 29c4ac30 second |
| 94 |
qpvuntsm b10da1f4 first |
| 95 |
zzzzzzzz 00000000 (empty) (no description set) |
| 96 |
[EOF] |
| 97 |
[exit status: 1] |
| 98 |
"); |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn test_edit_current() { |
| 103 |
let test_env = TestEnvironment::default(); |
| 104 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 105 |
let work_dir = test_env.work_dir("repo"); |
| 106 |
|
| 107 |
// Editing the current revision is a no-op |
| 108 |
let output = work_dir.run_jj(["edit", "@"]); |
| 109 |
insta::assert_snapshot!(output, @r" |
| 110 |
------- stderr ------- |
| 111 |
Already editing that commit. |
| 112 |
[EOF] |
| 113 |
"); |
| 114 |
|
| 115 |
// No operation created |
| 116 |
let output = work_dir.run_jj(["op", "log", "--limit=1"]); |
| 117 |
insta::assert_snapshot!(output, @" |
| 118 |
@ f63ee16f9553 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00 |
| 119 |
│ add workspace 'default' |
| 120 |
[EOF] |
| 121 |
"); |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
// Windows says "Access is denied" when trying to delete the object file. |
| 126 |
#[cfg(unix)] |
| 127 |
fn test_edit_current_wc_commit_missing() { |
| 128 |
use std::path::PathBuf; |
| 129 |
|
| 130 |
// Test that we get a reasonable error message when the current working-copy |
| 131 |
// commit is missing |
| 132 |
|
| 133 |
let test_env = TestEnvironment::default(); |
| 134 |
test_env.run_jj_in(".", ["git", "init", "repo"]).success(); |
| 135 |
let work_dir = test_env.work_dir("repo"); |
| 136 |
work_dir.run_jj(["commit", "-m", "first"]).success(); |
| 137 |
work_dir.run_jj(["describe", "-m", "second"]).success(); |
| 138 |
work_dir.run_jj(["edit", "@-"]).success(); |
| 139 |
|
| 140 |
let wc_id = work_dir |
| 141 |
.run_jj(["log", "--no-graph", "-T=commit_id", "-r=@"]) |
| 142 |
.success() |
| 143 |
.stdout |
| 144 |
.into_raw(); |
| 145 |
let wc_child_id = work_dir |
| 146 |
.run_jj(["log", "--no-graph", "-T=commit_id", "-r=@+"]) |
| 147 |
.success() |
| 148 |
.stdout |
| 149 |
.into_raw(); |
| 150 |
// Make the Git backend fail to read the current working copy commit |
| 151 |
let commit_object_path = PathBuf::from_iter([ |
| 152 |
".jj", |
| 153 |
"repo", |
| 154 |
"store", |
| 155 |
"git", |
| 156 |
"objects", |
| 157 |
&wc_id[..2], |
| 158 |
&wc_id[2..], |
| 159 |
]); |
| 160 |
work_dir.remove_file(commit_object_path); |
| 161 |
|
| 162 |
// Pass --ignore-working-copy to avoid triggering the error at snapshot time |
| 163 |
let output = work_dir.run_jj(["edit", "--ignore-working-copy", &wc_child_id]); |
| 164 |
insta::assert_snapshot!(output, @" |
| 165 |
------- stderr ------- |
| 166 |
Internal error: Failed to edit a commit |
| 167 |
Caused by: |
| 168 |
1: Current working-copy commit not found |
| 169 |
2: Object 68a505386f936fff6d718f55005e77ea72589bc1 of type commit not found |
| 170 |
3: An object with id 68a505386f936fff6d718f55005e77ea72589bc1 could not be found |
| 171 |
[EOF] |
| 172 |
[exit status: 255] |
| 173 |
"); |
| 174 |
} |
| 175 |
|
| 176 |
#[must_use] |
| 177 |
fn get_log_output(work_dir: &TestWorkDir) -> CommandOutput { |
| 178 |
let template = r#"commit_id.short() ++ " " ++ description"#; |
| 179 |
work_dir.run_jj(["log", "-T", template]) |
| 180 |
} |