| 1 |
// Copyright 2020 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 jj_lib::backend::CommitId; |
| 16 |
use jj_lib::index::Index; |
| 17 |
use jj_lib::merge::Merge; |
| 18 |
use jj_lib::merged_tree::MergedTree; |
| 19 |
use jj_lib::op_store::RefTarget; |
| 20 |
use jj_lib::op_store::RemoteRef; |
| 21 |
use jj_lib::op_store::RemoteRefState; |
| 22 |
use jj_lib::ref_name::RefName; |
| 23 |
use jj_lib::ref_name::RemoteName; |
| 24 |
use jj_lib::ref_name::RemoteRefSymbol; |
| 25 |
use jj_lib::ref_name::WorkspaceName; |
| 26 |
use jj_lib::ref_name::WorkspaceNameBuf; |
| 27 |
use jj_lib::repo::Repo as _; |
| 28 |
use jj_lib::rewrite::RebaseOptions; |
| 29 |
use maplit::hashset; |
| 30 |
use pollster::FutureExt as _; |
| 31 |
use testutils::CommitBuilderExt as _; |
| 32 |
use testutils::TestRepo; |
| 33 |
use testutils::TestResult; |
| 34 |
use testutils::assert_rebased_onto; |
| 35 |
use testutils::create_random_commit; |
| 36 |
use testutils::create_random_tree; |
| 37 |
use testutils::rebase_descendants_with_options_return_map; |
| 38 |
use testutils::write_random_commit; |
| 39 |
use testutils::write_random_commit_with_parents; |
| 40 |
|
| 41 |
fn remote_symbol<'a, N, M>(name: &'a N, remote: &'a M) -> RemoteRefSymbol<'a> |
| 42 |
where |
| 43 |
N: AsRef<RefName> + ?Sized, |
| 44 |
M: AsRef<RemoteName> + ?Sized, |
| 45 |
{ |
| 46 |
RemoteRefSymbol { |
| 47 |
name: name.as_ref(), |
| 48 |
remote: remote.as_ref(), |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
fn index_has_id(index: &dyn Index, commit_id: &CommitId) -> bool { |
| 53 |
index.has_id(commit_id).block_on().unwrap() |
| 54 |
} |
| 55 |
|
| 56 |
#[test] |
| 57 |
fn test_edit() -> TestResult { |
| 58 |
// Test that MutableRepo::edit() uses the requested commit (not a new child) |
| 59 |
let test_repo = TestRepo::init(); |
| 60 |
let repo = &test_repo.repo; |
| 61 |
|
| 62 |
let mut tx = repo.start_transaction(); |
| 63 |
let wc_commit = write_random_commit(tx.repo_mut()); |
| 64 |
let repo = tx.commit("test").block_on()?; |
| 65 |
|
| 66 |
let mut tx = repo.start_transaction(); |
| 67 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 68 |
tx.repo_mut().edit(ws_name.clone(), &wc_commit).block_on()?; |
| 69 |
let repo = tx.commit("test").block_on()?; |
| 70 |
assert_eq!(repo.view().get_wc_commit_id(&ws_name), Some(wc_commit.id())); |
| 71 |
Ok(()) |
| 72 |
} |
| 73 |
|
| 74 |
#[test] |
| 75 |
fn test_checkout() -> TestResult { |
| 76 |
// Test that MutableRepo::check_out() creates a child |
| 77 |
let test_repo = TestRepo::init(); |
| 78 |
let repo = &test_repo.repo; |
| 79 |
|
| 80 |
let mut tx = repo.start_transaction(); |
| 81 |
let wc_commit_parent = write_random_commit(tx.repo_mut()); |
| 82 |
let repo = tx.commit("test").block_on()?; |
| 83 |
|
| 84 |
let mut tx = repo.start_transaction(); |
| 85 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 86 |
let wc_commit = tx |
| 87 |
.repo_mut() |
| 88 |
.check_out(ws_name.clone(), &wc_commit_parent) |
| 89 |
.block_on()?; |
| 90 |
assert_eq!(wc_commit.tree_ids(), wc_commit_parent.tree_ids()); |
| 91 |
assert_eq!(wc_commit.parent_ids().len(), 1); |
| 92 |
assert_eq!(&wc_commit.parent_ids()[0], wc_commit_parent.id()); |
| 93 |
let repo = tx.commit("test").block_on()?; |
| 94 |
assert_eq!(repo.view().get_wc_commit_id(&ws_name), Some(wc_commit.id())); |
| 95 |
Ok(()) |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_edit_previous_not_empty() -> TestResult { |
| 100 |
// Test that MutableRepo::edit() does not usually abandon the previous |
| 101 |
// commit. |
| 102 |
let test_repo = TestRepo::init(); |
| 103 |
let repo = &test_repo.repo; |
| 104 |
|
| 105 |
let mut tx = repo.start_transaction(); |
| 106 |
let mut_repo = tx.repo_mut(); |
| 107 |
let old_wc_commit = write_random_commit(mut_repo); |
| 108 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 109 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 110 |
let repo = tx.commit("test").block_on()?; |
| 111 |
|
| 112 |
let mut tx = repo.start_transaction(); |
| 113 |
let mut_repo = tx.repo_mut(); |
| 114 |
let new_wc_commit = write_random_commit(mut_repo); |
| 115 |
mut_repo.edit(ws_name, &new_wc_commit).block_on()?; |
| 116 |
mut_repo.rebase_descendants().block_on()?; |
| 117 |
assert!(mut_repo.view().heads().contains(old_wc_commit.id())); |
| 118 |
Ok(()) |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_edit_previous_empty() -> TestResult { |
| 123 |
// Test that MutableRepo::edit() abandons the previous commit if it was |
| 124 |
// empty. |
| 125 |
let test_repo = TestRepo::init(); |
| 126 |
let repo = &test_repo.repo; |
| 127 |
|
| 128 |
let mut tx = repo.start_transaction(); |
| 129 |
let mut_repo = tx.repo_mut(); |
| 130 |
let old_wc_commit = mut_repo |
| 131 |
.new_commit( |
| 132 |
vec![repo.store().root_commit_id().clone()], |
| 133 |
repo.store().empty_merged_tree(), |
| 134 |
) |
| 135 |
.write_unwrap(); |
| 136 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 137 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 138 |
let repo = tx.commit("test").block_on()?; |
| 139 |
|
| 140 |
let mut tx = repo.start_transaction(); |
| 141 |
let mut_repo = tx.repo_mut(); |
| 142 |
let new_wc_commit = write_random_commit(mut_repo); |
| 143 |
mut_repo.edit(ws_name, &new_wc_commit).block_on()?; |
| 144 |
mut_repo.rebase_descendants().block_on()?; |
| 145 |
assert!(!mut_repo.view().heads().contains(old_wc_commit.id())); |
| 146 |
Ok(()) |
| 147 |
} |
| 148 |
|
| 149 |
#[test] |
| 150 |
fn test_edit_previous_empty_merge() -> TestResult { |
| 151 |
// Test that MutableRepo::edit() abandons the previous commit if it was |
| 152 |
// an empty merge commit. |
| 153 |
let test_repo = TestRepo::init(); |
| 154 |
let repo = &test_repo.repo; |
| 155 |
|
| 156 |
let mut tx = repo.start_transaction(); |
| 157 |
let mut_repo = tx.repo_mut(); |
| 158 |
let old_parent1 = write_random_commit(mut_repo); |
| 159 |
let old_parent2 = write_random_commit(mut_repo); |
| 160 |
let empty_tree = repo.store().empty_merged_tree(); |
| 161 |
let old_parent_tree = MergedTree::merge(Merge::from_vec(vec![ |
| 162 |
(old_parent1.tree(), "old parent 1".into()), |
| 163 |
(empty_tree, "empty".into()), |
| 164 |
(old_parent2.tree(), "old parent 2".into()), |
| 165 |
])) |
| 166 |
.block_on()?; |
| 167 |
let old_wc_commit = mut_repo |
| 168 |
.new_commit( |
| 169 |
vec![old_parent1.id().clone(), old_parent2.id().clone()], |
| 170 |
repo.store().empty_merged_tree(), |
| 171 |
) |
| 172 |
.set_tree(old_parent_tree) |
| 173 |
.write_unwrap(); |
| 174 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 175 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 176 |
let repo = tx.commit("test").block_on()?; |
| 177 |
|
| 178 |
let mut tx = repo.start_transaction(); |
| 179 |
let mut_repo = tx.repo_mut(); |
| 180 |
let new_wc_commit = write_random_commit(mut_repo); |
| 181 |
mut_repo.edit(ws_name, &new_wc_commit).block_on()?; |
| 182 |
mut_repo.rebase_descendants().block_on()?; |
| 183 |
assert!(!mut_repo.view().heads().contains(old_wc_commit.id())); |
| 184 |
Ok(()) |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
fn test_edit_previous_empty_with_description() -> TestResult { |
| 189 |
// Test that MutableRepo::edit() does not abandon the previous commit if it |
| 190 |
// has a non-empty description. |
| 191 |
let test_repo = TestRepo::init(); |
| 192 |
let repo = &test_repo.repo; |
| 193 |
|
| 194 |
let mut tx = repo.start_transaction(); |
| 195 |
let mut_repo = tx.repo_mut(); |
| 196 |
let old_wc_commit = mut_repo |
| 197 |
.new_commit( |
| 198 |
vec![repo.store().root_commit_id().clone()], |
| 199 |
repo.store().empty_merged_tree(), |
| 200 |
) |
| 201 |
.set_description("not empty") |
| 202 |
.write_unwrap(); |
| 203 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 204 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 205 |
let repo = tx.commit("test").block_on()?; |
| 206 |
|
| 207 |
let mut tx = repo.start_transaction(); |
| 208 |
let mut_repo = tx.repo_mut(); |
| 209 |
let new_wc_commit = write_random_commit(mut_repo); |
| 210 |
mut_repo.edit(ws_name, &new_wc_commit).block_on()?; |
| 211 |
mut_repo.rebase_descendants().block_on()?; |
| 212 |
assert!(mut_repo.view().heads().contains(old_wc_commit.id())); |
| 213 |
Ok(()) |
| 214 |
} |
| 215 |
|
| 216 |
#[test] |
| 217 |
fn test_edit_previous_empty_with_local_bookmark() -> TestResult { |
| 218 |
// Test that MutableRepo::edit() does not abandon the previous commit if it |
| 219 |
// is pointed by local bookmark. |
| 220 |
let test_repo = TestRepo::init(); |
| 221 |
let repo = &test_repo.repo; |
| 222 |
|
| 223 |
let mut tx = repo.start_transaction(); |
| 224 |
let mut_repo = tx.repo_mut(); |
| 225 |
let old_wc_commit = mut_repo |
| 226 |
.new_commit( |
| 227 |
vec![repo.store().root_commit_id().clone()], |
| 228 |
repo.store().empty_merged_tree(), |
| 229 |
) |
| 230 |
.write_unwrap(); |
| 231 |
mut_repo.set_local_bookmark_target("b".as_ref(), RefTarget::normal(old_wc_commit.id().clone())); |
| 232 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 233 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 234 |
let repo = tx.commit("test").block_on()?; |
| 235 |
|
| 236 |
let mut tx = repo.start_transaction(); |
| 237 |
let mut_repo = tx.repo_mut(); |
| 238 |
let new_wc_commit = write_random_commit(mut_repo); |
| 239 |
mut_repo.edit(ws_name, &new_wc_commit).block_on()?; |
| 240 |
mut_repo.rebase_descendants().block_on()?; |
| 241 |
assert!(mut_repo.view().heads().contains(old_wc_commit.id())); |
| 242 |
Ok(()) |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn test_edit_previous_empty_with_local_tag() -> TestResult { |
| 247 |
// Test that MutableRepo::edit() does not abandon the previous commit if it |
| 248 |
// is pointed by local tag. |
| 249 |
let test_repo = TestRepo::init(); |
| 250 |
let repo = &test_repo.repo; |
| 251 |
|
| 252 |
let mut tx = repo.start_transaction(); |
| 253 |
let mut_repo = tx.repo_mut(); |
| 254 |
let old_wc_commit = mut_repo |
| 255 |
.new_commit( |
| 256 |
vec![repo.store().root_commit_id().clone()], |
| 257 |
repo.store().empty_merged_tree(), |
| 258 |
) |
| 259 |
.write_unwrap(); |
| 260 |
mut_repo.set_local_tag_target("t".as_ref(), RefTarget::normal(old_wc_commit.id().clone())); |
| 261 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 262 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 263 |
let repo = tx.commit("test").block_on()?; |
| 264 |
|
| 265 |
let mut tx = repo.start_transaction(); |
| 266 |
let mut_repo = tx.repo_mut(); |
| 267 |
let new_wc_commit = write_random_commit(mut_repo); |
| 268 |
mut_repo.edit(ws_name, &new_wc_commit).block_on()?; |
| 269 |
mut_repo.rebase_descendants().block_on()?; |
| 270 |
assert!(mut_repo.view().heads().contains(old_wc_commit.id())); |
| 271 |
Ok(()) |
| 272 |
} |
| 273 |
|
| 274 |
#[test] |
| 275 |
fn test_edit_previous_empty_with_other_workspace() -> TestResult { |
| 276 |
// Test that MutableRepo::edit() does not abandon the previous commit if it |
| 277 |
// is pointed by another workspace |
| 278 |
let test_repo = TestRepo::init(); |
| 279 |
let repo = &test_repo.repo; |
| 280 |
|
| 281 |
let mut tx = repo.start_transaction(); |
| 282 |
let mut_repo = tx.repo_mut(); |
| 283 |
let old_wc_commit = mut_repo |
| 284 |
.new_commit( |
| 285 |
vec![repo.store().root_commit_id().clone()], |
| 286 |
repo.store().empty_merged_tree(), |
| 287 |
) |
| 288 |
.write_unwrap(); |
| 289 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 290 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 291 |
let other_ws_name = WorkspaceNameBuf::from("other"); |
| 292 |
mut_repo |
| 293 |
.edit(other_ws_name.clone(), &old_wc_commit) |
| 294 |
.block_on()?; |
| 295 |
let repo = tx.commit("test").block_on()?; |
| 296 |
|
| 297 |
let mut tx = repo.start_transaction(); |
| 298 |
let mut_repo = tx.repo_mut(); |
| 299 |
let new_wc_commit = write_random_commit(mut_repo); |
| 300 |
mut_repo.edit(ws_name, &new_wc_commit).block_on()?; |
| 301 |
mut_repo.rebase_descendants().block_on()?; |
| 302 |
assert!(mut_repo.view().heads().contains(old_wc_commit.id())); |
| 303 |
Ok(()) |
| 304 |
} |
| 305 |
|
| 306 |
#[test] |
| 307 |
fn test_edit_previous_empty_non_head() -> TestResult { |
| 308 |
// Test that MutableRepo::edit() does not abandon the previous commit if it |
| 309 |
// was empty and is not a head |
| 310 |
let test_repo = TestRepo::init(); |
| 311 |
let repo = &test_repo.repo; |
| 312 |
|
| 313 |
let mut tx = repo.start_transaction(); |
| 314 |
let mut_repo = tx.repo_mut(); |
| 315 |
let old_wc_commit = mut_repo |
| 316 |
.new_commit( |
| 317 |
vec![repo.store().root_commit_id().clone()], |
| 318 |
repo.store().empty_merged_tree(), |
| 319 |
) |
| 320 |
.write_unwrap(); |
| 321 |
let old_child = mut_repo |
| 322 |
.new_commit(vec![old_wc_commit.id().clone()], old_wc_commit.tree()) |
| 323 |
.write_unwrap(); |
| 324 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 325 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 326 |
let repo = tx.commit("test").block_on()?; |
| 327 |
|
| 328 |
let mut tx = repo.start_transaction(); |
| 329 |
let mut_repo = tx.repo_mut(); |
| 330 |
let new_wc_commit = write_random_commit(mut_repo); |
| 331 |
mut_repo.edit(ws_name, &new_wc_commit).block_on()?; |
| 332 |
mut_repo.rebase_descendants().block_on()?; |
| 333 |
assert_eq!( |
| 334 |
*mut_repo.view().heads(), |
| 335 |
hashset! {old_child.id().clone(), new_wc_commit.id().clone()} |
| 336 |
); |
| 337 |
Ok(()) |
| 338 |
} |
| 339 |
|
| 340 |
#[test] |
| 341 |
fn test_edit_initial() -> TestResult { |
| 342 |
// Test that MutableRepo::edit() can be used on the initial working-copy commit |
| 343 |
// in a workspace |
| 344 |
let test_repo = TestRepo::init(); |
| 345 |
let repo = &test_repo.repo; |
| 346 |
|
| 347 |
let mut tx = repo.start_transaction(); |
| 348 |
let wc_commit = write_random_commit(tx.repo_mut()); |
| 349 |
let repo = tx.commit("test").block_on()?; |
| 350 |
|
| 351 |
let mut tx = repo.start_transaction(); |
| 352 |
let ws_name = WorkspaceNameBuf::from("new-workspace"); |
| 353 |
tx.repo_mut().edit(ws_name.clone(), &wc_commit).block_on()?; |
| 354 |
let repo = tx.commit("test").block_on()?; |
| 355 |
assert_eq!(repo.view().get_wc_commit_id(&ws_name), Some(wc_commit.id())); |
| 356 |
Ok(()) |
| 357 |
} |
| 358 |
|
| 359 |
#[test] |
| 360 |
fn test_edit_hidden_commit() -> TestResult { |
| 361 |
// Test that MutableRepo::edit() edits a hidden commit and updates |
| 362 |
// the view head ids. |
| 363 |
let test_repo = TestRepo::init(); |
| 364 |
let repo = &test_repo.repo; |
| 365 |
|
| 366 |
let mut tx = repo.start_transaction(); |
| 367 |
let wc_commit = write_random_commit(tx.repo_mut()); |
| 368 |
|
| 369 |
// Intentionally not doing tx.commit, so the commit id is not tracked |
| 370 |
// in the view head ids. |
| 371 |
|
| 372 |
let mut tx = repo.start_transaction(); |
| 373 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 374 |
tx.repo_mut().edit(ws_name.clone(), &wc_commit).block_on()?; |
| 375 |
let repo = tx.commit("test").block_on()?; |
| 376 |
assert_eq!(repo.view().get_wc_commit_id(&ws_name), Some(wc_commit.id())); |
| 377 |
assert_eq!(*repo.view().heads(), hashset! {wc_commit.id().clone()}); |
| 378 |
Ok(()) |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn test_add_head_success() -> TestResult { |
| 383 |
// Test that MutableRepo::add_head() adds the head, and that it's still there |
| 384 |
// after commit. It should also be indexed. |
| 385 |
let test_repo = TestRepo::init(); |
| 386 |
let repo = &test_repo.repo; |
| 387 |
|
| 388 |
// Create a commit outside of the repo by using a temporary transaction. Then |
| 389 |
// add that as a head. |
| 390 |
let mut tx = repo.start_transaction(); |
| 391 |
let new_commit = write_random_commit(tx.repo_mut()); |
| 392 |
drop(tx); |
| 393 |
|
| 394 |
let mut tx = repo.start_transaction(); |
| 395 |
let mut_repo = tx.repo_mut(); |
| 396 |
|
| 397 |
assert!(!mut_repo.view().heads().contains(new_commit.id())); |
| 398 |
assert!(!index_has_id(mut_repo.index(), new_commit.id())); |
| 399 |
mut_repo.add_head(&new_commit).block_on()?; |
| 400 |
assert!(mut_repo.view().heads().contains(new_commit.id())); |
| 401 |
assert!(index_has_id(mut_repo.index(), new_commit.id())); |
| 402 |
let repo = tx.commit("test").block_on()?; |
| 403 |
assert!(repo.view().heads().contains(new_commit.id())); |
| 404 |
assert!(index_has_id(repo.index(), new_commit.id())); |
| 405 |
Ok(()) |
| 406 |
} |
| 407 |
|
| 408 |
#[test] |
| 409 |
fn test_add_head_ancestor() -> TestResult { |
| 410 |
// Test that MutableRepo::add_head() does not add a head if it's an ancestor of |
| 411 |
// an existing head. |
| 412 |
let test_repo = TestRepo::init(); |
| 413 |
let repo = &test_repo.repo; |
| 414 |
|
| 415 |
let mut tx = repo.start_transaction(); |
| 416 |
let commit1 = write_random_commit(tx.repo_mut()); |
| 417 |
let commit2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]); |
| 418 |
let commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]); |
| 419 |
let repo = tx.commit("test").block_on()?; |
| 420 |
|
| 421 |
assert_eq!(repo.view().heads(), &hashset! {commit3.id().clone()}); |
| 422 |
let mut tx = repo.start_transaction(); |
| 423 |
let mut_repo = tx.repo_mut(); |
| 424 |
mut_repo.add_head(&commit1).block_on()?; |
| 425 |
assert_eq!(repo.view().heads(), &hashset! {commit3.id().clone()}); |
| 426 |
Ok(()) |
| 427 |
} |
| 428 |
|
| 429 |
#[test] |
| 430 |
fn test_add_head_not_immediate_child() -> TestResult { |
| 431 |
// Test that MutableRepo::add_head() can be used for adding a head that is not |
| 432 |
// an immediate child of a current head. |
| 433 |
let test_repo = TestRepo::init(); |
| 434 |
let repo = &test_repo.repo; |
| 435 |
|
| 436 |
let mut tx = repo.start_transaction(); |
| 437 |
let initial = write_random_commit(tx.repo_mut()); |
| 438 |
let repo = tx.commit("test").block_on()?; |
| 439 |
|
| 440 |
// Create some commits outside of the repo by using a temporary transaction. |
| 441 |
// Then add one of them as a head. |
| 442 |
let mut tx = repo.start_transaction(); |
| 443 |
let rewritten = create_random_commit(tx.repo_mut()) |
| 444 |
.set_change_id(initial.change_id().clone()) |
| 445 |
.set_predecessors(vec![initial.id().clone()]) |
| 446 |
.write_unwrap(); |
| 447 |
let child = write_random_commit_with_parents(tx.repo_mut(), &[&rewritten]); |
| 448 |
drop(tx); |
| 449 |
|
| 450 |
assert_eq!(repo.view().heads(), &hashset! {initial.id().clone()}); |
| 451 |
let mut tx = repo.start_transaction(); |
| 452 |
let mut_repo = tx.repo_mut(); |
| 453 |
mut_repo.add_head(&child).block_on()?; |
| 454 |
assert_eq!( |
| 455 |
mut_repo.view().heads(), |
| 456 |
&hashset! {initial.id().clone(), child.id().clone()} |
| 457 |
); |
| 458 |
assert!(index_has_id(mut_repo.index(), initial.id())); |
| 459 |
assert!(index_has_id(mut_repo.index(), rewritten.id())); |
| 460 |
assert!(index_has_id(mut_repo.index(), child.id())); |
| 461 |
Ok(()) |
| 462 |
} |
| 463 |
|
| 464 |
#[test] |
| 465 |
fn test_remove_head() -> TestResult { |
| 466 |
// Test that MutableRepo::remove_head() removes the head, and that it's still |
| 467 |
// removed after commit. It should remain in the index, since we otherwise would |
| 468 |
// have to reindex everything. |
| 469 |
let test_repo = TestRepo::init(); |
| 470 |
let repo = &test_repo.repo; |
| 471 |
|
| 472 |
let mut tx = repo.start_transaction(); |
| 473 |
let commit1 = write_random_commit(tx.repo_mut()); |
| 474 |
let commit2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]); |
| 475 |
let commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]); |
| 476 |
let repo = tx.commit("test").block_on()?; |
| 477 |
|
| 478 |
let mut tx = repo.start_transaction(); |
| 479 |
let mut_repo = tx.repo_mut(); |
| 480 |
assert!(mut_repo.view().heads().contains(commit3.id())); |
| 481 |
mut_repo.remove_head(commit3.id()); |
| 482 |
let heads = mut_repo.view().heads().clone(); |
| 483 |
assert!(!heads.contains(commit3.id())); |
| 484 |
assert!(!heads.contains(commit2.id())); |
| 485 |
assert!(!heads.contains(commit1.id())); |
| 486 |
assert!(index_has_id(mut_repo.index(), commit1.id())); |
| 487 |
assert!(index_has_id(mut_repo.index(), commit2.id())); |
| 488 |
assert!(index_has_id(mut_repo.index(), commit3.id())); |
| 489 |
let repo = tx.commit("test").block_on()?; |
| 490 |
let heads = repo.view().heads().clone(); |
| 491 |
assert!(!heads.contains(commit3.id())); |
| 492 |
assert!(!heads.contains(commit2.id())); |
| 493 |
assert!(!heads.contains(commit1.id())); |
| 494 |
assert!(index_has_id(repo.index(), commit1.id())); |
| 495 |
assert!(index_has_id(repo.index(), commit2.id())); |
| 496 |
assert!(index_has_id(repo.index(), commit3.id())); |
| 497 |
Ok(()) |
| 498 |
} |
| 499 |
|
| 500 |
#[test] |
| 501 |
fn test_has_changed() -> TestResult { |
| 502 |
// Test that MutableRepo::has_changed() reports changes iff the view has changed |
| 503 |
// (e.g. not after setting a bookmark to point to where it was already |
| 504 |
// pointing). |
| 505 |
let test_repo = TestRepo::init(); |
| 506 |
let repo = &test_repo.repo; |
| 507 |
let normal_remote_ref = |id: &CommitId| RemoteRef { |
| 508 |
target: RefTarget::normal(id.clone()), |
| 509 |
state: RemoteRefState::Tracked, // doesn't matter |
| 510 |
}; |
| 511 |
|
| 512 |
let mut tx = repo.start_transaction(); |
| 513 |
let mut_repo = tx.repo_mut(); |
| 514 |
let commit1 = write_random_commit(mut_repo); |
| 515 |
let commit2 = write_random_commit(mut_repo); |
| 516 |
mut_repo.remove_head(commit2.id()); |
| 517 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 518 |
mut_repo.set_wc_commit(ws_name.clone(), commit1.id().clone())?; |
| 519 |
mut_repo.set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit1.id().clone())); |
| 520 |
mut_repo.set_remote_bookmark( |
| 521 |
remote_symbol("main", "origin"), |
| 522 |
normal_remote_ref(commit1.id()), |
| 523 |
); |
| 524 |
let repo = tx.commit("test").block_on()?; |
| 525 |
// Test the setup |
| 526 |
assert_eq!(repo.view().heads(), &hashset! {commit1.id().clone()}); |
| 527 |
|
| 528 |
let mut tx = repo.start_transaction(); |
| 529 |
let mut_repo = tx.repo_mut(); |
| 530 |
|
| 531 |
mut_repo.add_head(&commit1).block_on()?; |
| 532 |
mut_repo.set_wc_commit(ws_name.clone(), commit1.id().clone())?; |
| 533 |
mut_repo.set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit1.id().clone())); |
| 534 |
mut_repo.set_remote_bookmark( |
| 535 |
remote_symbol("main", "origin"), |
| 536 |
normal_remote_ref(commit1.id()), |
| 537 |
); |
| 538 |
assert!(!mut_repo.has_changes()); |
| 539 |
|
| 540 |
mut_repo.remove_head(commit2.id()); |
| 541 |
mut_repo.set_local_bookmark_target("stable".as_ref(), RefTarget::absent()); |
| 542 |
mut_repo.set_remote_bookmark(remote_symbol("stable", "origin"), RemoteRef::absent()); |
| 543 |
assert!(!mut_repo.has_changes()); |
| 544 |
|
| 545 |
mut_repo.add_head(&commit2).block_on()?; |
| 546 |
assert!(mut_repo.has_changes()); |
| 547 |
mut_repo.remove_head(commit2.id()); |
| 548 |
assert!(!mut_repo.has_changes()); |
| 549 |
|
| 550 |
mut_repo.set_wc_commit(ws_name.clone(), commit2.id().clone())?; |
| 551 |
assert!(mut_repo.has_changes()); |
| 552 |
mut_repo.set_wc_commit(ws_name, commit1.id().clone())?; |
| 553 |
assert!(!mut_repo.has_changes()); |
| 554 |
|
| 555 |
mut_repo.set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit2.id().clone())); |
| 556 |
assert!(mut_repo.has_changes()); |
| 557 |
mut_repo.set_local_bookmark_target("main".as_ref(), RefTarget::normal(commit1.id().clone())); |
| 558 |
mut_repo.remove_head(commit2.id()); |
| 559 |
assert!(!mut_repo.has_changes()); |
| 560 |
|
| 561 |
mut_repo.set_remote_bookmark( |
| 562 |
remote_symbol("main", "origin"), |
| 563 |
normal_remote_ref(commit2.id()), |
| 564 |
); |
| 565 |
assert!(mut_repo.has_changes()); |
| 566 |
mut_repo.set_remote_bookmark( |
| 567 |
remote_symbol("main", "origin"), |
| 568 |
normal_remote_ref(commit1.id()), |
| 569 |
); |
| 570 |
assert!(!mut_repo.has_changes()); |
| 571 |
Ok(()) |
| 572 |
} |
| 573 |
|
| 574 |
#[test] |
| 575 |
fn test_rebase_descendants_simple() -> TestResult { |
| 576 |
// There are many additional tests of this functionality in `test_rewrite.rs`. |
| 577 |
let test_repo = TestRepo::init(); |
| 578 |
let repo = &test_repo.repo; |
| 579 |
|
| 580 |
let mut tx = repo.start_transaction(); |
| 581 |
let commit1 = write_random_commit(tx.repo_mut()); |
| 582 |
let commit2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]); |
| 583 |
let commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]); |
| 584 |
let commit4 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]); |
| 585 |
let commit5 = write_random_commit_with_parents(tx.repo_mut(), &[&commit4]); |
| 586 |
let repo = tx.commit("test").block_on()?; |
| 587 |
|
| 588 |
let mut tx = repo.start_transaction(); |
| 589 |
let mut_repo = tx.repo_mut(); |
| 590 |
let commit6 = write_random_commit_with_parents(mut_repo, &[&commit1]); |
| 591 |
mut_repo.set_rewritten_commit(commit2.id().clone(), commit6.id().clone()); |
| 592 |
mut_repo.record_abandoned_commit(&commit4); |
| 593 |
let rebase_map = |
| 594 |
rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default()); |
| 595 |
// Commit 3 got rebased onto commit 2's replacement, i.e. commit 6 |
| 596 |
assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit3, &[commit6.id()]); |
| 597 |
// Commit 5 got rebased onto commit 4's parent, i.e. commit 1 |
| 598 |
assert_rebased_onto(tx.repo_mut(), &rebase_map, &commit5, &[commit1.id()]); |
| 599 |
assert_eq!(rebase_map.len(), 2); |
| 600 |
|
| 601 |
// No more descendants to rebase if we try again. |
| 602 |
let rebase_map = |
| 603 |
rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default()); |
| 604 |
assert_eq!(rebase_map.len(), 0); |
| 605 |
Ok(()) |
| 606 |
} |
| 607 |
|
| 608 |
#[test] |
| 609 |
fn test_rebase_descendants_divergent_rewrite() -> TestResult { |
| 610 |
// Test rebasing descendants when one commit was rewritten to several other |
| 611 |
// commits. There are many additional tests of this functionality in |
| 612 |
// `test_rewrite.rs`. |
| 613 |
let test_repo = TestRepo::init(); |
| 614 |
let repo = &test_repo.repo; |
| 615 |
|
| 616 |
let mut tx = repo.start_transaction(); |
| 617 |
let commit1 = write_random_commit(tx.repo_mut()); |
| 618 |
let commit2 = write_random_commit_with_parents(tx.repo_mut(), &[&commit1]); |
| 619 |
let _commit3 = write_random_commit_with_parents(tx.repo_mut(), &[&commit2]); |
| 620 |
let repo = tx.commit("test").block_on()?; |
| 621 |
|
| 622 |
let mut tx = repo.start_transaction(); |
| 623 |
let mut_repo = tx.repo_mut(); |
| 624 |
let commit4 = write_random_commit_with_parents(mut_repo, &[&commit1]); |
| 625 |
let commit5 = write_random_commit_with_parents(mut_repo, &[&commit1]); |
| 626 |
mut_repo.set_divergent_rewrite( |
| 627 |
commit2.id().clone(), |
| 628 |
vec![commit4.id().clone(), commit5.id().clone()], |
| 629 |
); |
| 630 |
// Commit 3 does *not* get rebased because it's unclear if it should go onto |
| 631 |
// commit 4 or commit 5 |
| 632 |
let rebase_map = |
| 633 |
rebase_descendants_with_options_return_map(tx.repo_mut(), &RebaseOptions::default()); |
| 634 |
assert!(rebase_map.is_empty()); |
| 635 |
Ok(()) |
| 636 |
} |
| 637 |
|
| 638 |
#[test] |
| 639 |
fn test_rename_remote() { |
| 640 |
let test_repo = TestRepo::init(); |
| 641 |
let repo = &test_repo.repo; |
| 642 |
let mut tx = repo.start_transaction(); |
| 643 |
let mut_repo = tx.repo_mut(); |
| 644 |
let commit = write_random_commit(mut_repo); |
| 645 |
let remote_ref = RemoteRef { |
| 646 |
target: RefTarget::normal(commit.id().clone()), |
| 647 |
state: RemoteRefState::Tracked, // doesn't matter |
| 648 |
}; |
| 649 |
mut_repo.set_remote_bookmark(remote_symbol("main", "origin"), remote_ref.clone()); |
| 650 |
mut_repo.rename_remote("origin".as_ref(), "upstream".as_ref()); |
| 651 |
assert_eq!( |
| 652 |
mut_repo.get_remote_bookmark(remote_symbol("main", "upstream")), |
| 653 |
&remote_ref |
| 654 |
); |
| 655 |
assert_eq!( |
| 656 |
mut_repo.get_remote_bookmark(remote_symbol("main", "origin")), |
| 657 |
RemoteRef::absent_ref() |
| 658 |
); |
| 659 |
} |
| 660 |
|
| 661 |
#[test] |
| 662 |
fn test_remove_wc_commit_previous_not_discardable() -> TestResult { |
| 663 |
// Test that MutableRepo::remove_workspace() does not usually abandon the |
| 664 |
// previous commit. |
| 665 |
let test_repo = TestRepo::init(); |
| 666 |
let repo = &test_repo.repo; |
| 667 |
|
| 668 |
let mut tx = repo.start_transaction(); |
| 669 |
let mut_repo = tx.repo_mut(); |
| 670 |
let old_wc_commit = write_random_commit(mut_repo); |
| 671 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 672 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 673 |
mut_repo.set_git_head_target(&ws_name, RefTarget::normal(old_wc_commit.id().clone())); |
| 674 |
let repo = tx.commit("test").block_on()?; |
| 675 |
|
| 676 |
let mut tx = repo.start_transaction(); |
| 677 |
let mut_repo = tx.repo_mut(); |
| 678 |
mut_repo.remove_workspace(&ws_name).block_on()?; |
| 679 |
mut_repo.rebase_descendants().block_on()?; |
| 680 |
assert!(mut_repo.view().heads().contains(old_wc_commit.id())); |
| 681 |
assert!(mut_repo.view().git_head(&ws_name).is_absent()); |
| 682 |
Ok(()) |
| 683 |
} |
| 684 |
|
| 685 |
#[test] |
| 686 |
fn test_remove_wc_commit_previous_discardable() -> TestResult { |
| 687 |
// Test that MutableRepo::remove_workspace() abandons the previous commit |
| 688 |
// if it was discardable. |
| 689 |
let test_repo = TestRepo::init(); |
| 690 |
let repo = &test_repo.repo; |
| 691 |
|
| 692 |
let mut tx = repo.start_transaction(); |
| 693 |
let mut_repo = tx.repo_mut(); |
| 694 |
let old_wc_commit = mut_repo |
| 695 |
.new_commit( |
| 696 |
vec![repo.store().root_commit_id().clone()], |
| 697 |
repo.store().empty_merged_tree(), |
| 698 |
) |
| 699 |
.write_unwrap(); |
| 700 |
let ws_name = WorkspaceName::DEFAULT.to_owned(); |
| 701 |
mut_repo.edit(ws_name.clone(), &old_wc_commit).block_on()?; |
| 702 |
let repo = tx.commit("test").block_on()?; |
| 703 |
|
| 704 |
let mut tx = repo.start_transaction(); |
| 705 |
let mut_repo = tx.repo_mut(); |
| 706 |
mut_repo.remove_workspace(&ws_name).block_on()?; |
| 707 |
mut_repo.rebase_descendants().block_on()?; |
| 708 |
assert!(!mut_repo.view().heads().contains(old_wc_commit.id())); |
| 709 |
Ok(()) |
| 710 |
} |
| 711 |
|
| 712 |
#[test] |
| 713 |
fn test_reparent_descendants() -> TestResult { |
| 714 |
// Test that MutableRepo::reparent_descendants() reparents descendants of |
| 715 |
// rewritten commits without altering their content. |
| 716 |
let test_repo = TestRepo::init(); |
| 717 |
let repo = &test_repo.repo; |
| 718 |
|
| 719 |
let mut tx = repo.start_transaction(); |
| 720 |
let commit_a = write_random_commit(tx.repo_mut()); |
| 721 |
let commit_b = write_random_commit(tx.repo_mut()); |
| 722 |
let commit_child_a_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a, &commit_b]); |
| 723 |
let commit_grandchild_a_b = |
| 724 |
write_random_commit_with_parents(tx.repo_mut(), &[&commit_child_a_b]); |
| 725 |
let commit_child_a = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a]); |
| 726 |
let commit_child_b = write_random_commit_with_parents(tx.repo_mut(), &[&commit_b]); |
| 727 |
let mut_repo = tx.repo_mut(); |
| 728 |
for (bookmark, commit) in [ |
| 729 |
("b", &commit_b), |
| 730 |
("child_a_b", &commit_child_a_b), |
| 731 |
("grandchild_a_b", &commit_grandchild_a_b), |
| 732 |
("child_a", &commit_child_a), |
| 733 |
("child_b", &commit_child_b), |
| 734 |
] { |
| 735 |
mut_repo |
| 736 |
.set_local_bookmark_target(bookmark.as_ref(), RefTarget::normal(commit.id().clone())); |
| 737 |
} |
| 738 |
let repo = tx.commit("test").block_on()?; |
| 739 |
|
| 740 |
// Rewrite "commit_a". |
| 741 |
let mut tx = repo.start_transaction(); |
| 742 |
let mut_repo = tx.repo_mut(); |
| 743 |
mut_repo |
| 744 |
.rewrite_commit(&commit_a) |
| 745 |
.set_tree(create_random_tree(&repo)) |
| 746 |
.write_unwrap(); |
| 747 |
let reparented = mut_repo.reparent_descendants().block_on()?; |
| 748 |
// "child_a_b", "grandchild_a_b" and "child_a" (3 commits) must have been |
| 749 |
// reparented. |
| 750 |
assert_eq!(reparented, 3); |
| 751 |
let repo = tx.commit("test").block_on()?; |
| 752 |
|
| 753 |
for (bookmark, commit) in [ |
| 754 |
("b", &commit_b), |
| 755 |
("child_a_b", &commit_child_a_b), |
| 756 |
("grandchild_a_b", &commit_grandchild_a_b), |
| 757 |
("child_a", &commit_child_a), |
| 758 |
("child_b", &commit_child_b), |
| 759 |
] { |
| 760 |
let rewritten_id = repo |
| 761 |
.view() |
| 762 |
.get_local_bookmark(bookmark.as_ref()) |
| 763 |
.as_normal() |
| 764 |
.unwrap() |
| 765 |
.clone(); |
| 766 |
if matches!(bookmark, "b" | "child_b") { |
| 767 |
// "b" and "child_b" have been kept untouched. |
| 768 |
assert_eq!(commit.id(), &rewritten_id); |
| 769 |
} else { |
| 770 |
// All commits except "b", and "child_b" have been reparented while keeping |
| 771 |
// their content. |
| 772 |
assert_ne!(commit.id(), &rewritten_id); |
| 773 |
let rewritten_commit = repo.store().get_commit(&rewritten_id)?; |
| 774 |
assert_eq!(commit.tree_ids(), rewritten_commit.tree_ids()); |
| 775 |
let (parent_ids, rewritten_parent_ids) = |
| 776 |
(commit.parent_ids(), rewritten_commit.parent_ids()); |
| 777 |
assert_eq!(parent_ids.len(), rewritten_parent_ids.len()); |
| 778 |
assert_ne!(parent_ids, rewritten_parent_ids); |
| 779 |
} |
| 780 |
} |
| 781 |
Ok(()) |
| 782 |
} |
| 783 |
|
| 784 |
#[test] |
| 785 |
fn test_bookmark_hidden_commit() -> TestResult { |
| 786 |
// Test that MutableRepo::set_local_bookmark_target() on a hidden commit makes |
| 787 |
// it visible. |
| 788 |
let test_repo = TestRepo::init(); |
| 789 |
let repo = &test_repo.repo; |
| 790 |
let root_commit = repo.store().root_commit(); |
| 791 |
|
| 792 |
let mut tx = repo.start_transaction(); |
| 793 |
let commit = write_random_commit(tx.repo_mut()); |
| 794 |
tx.repo_mut().remove_head(commit.id()); |
| 795 |
let repo = tx.commit("test").block_on()?; |
| 796 |
// Test the setup |
| 797 |
assert_eq!(*repo.view().heads(), hashset! {root_commit.id().clone()}); |
| 798 |
|
| 799 |
let mut tx = repo.start_transaction(); |
| 800 |
tx.repo_mut() |
| 801 |
.set_local_bookmark_target("b".as_ref(), RefTarget::normal(commit.id().clone())); |
| 802 |
let repo = tx.commit("test").block_on()?; |
| 803 |
assert_eq!(*repo.view().heads(), hashset! {commit.id().clone()}); |
| 804 |
Ok(()) |
| 805 |
} |