| 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 std::path::Path; |
| 16 |
use std::slice; |
| 17 |
use std::sync::Arc; |
| 18 |
use std::time::SystemTime; |
| 19 |
|
| 20 |
use assert_matches::assert_matches; |
| 21 |
use futures::TryStreamExt as _; |
| 22 |
use itertools::Itertools as _; |
| 23 |
use jj_lib::backend::CommitId; |
| 24 |
use jj_lib::config::ConfigLayer; |
| 25 |
use jj_lib::config::ConfigSource; |
| 26 |
use jj_lib::evolution::walk_predecessors; |
| 27 |
use jj_lib::index::Index; |
| 28 |
use jj_lib::object_id::ObjectId as _; |
| 29 |
use jj_lib::op_store::OperationId; |
| 30 |
use jj_lib::op_walk; |
| 31 |
use jj_lib::op_walk::OpsetEvaluationError; |
| 32 |
use jj_lib::op_walk::OpsetResolutionError; |
| 33 |
use jj_lib::operation::Operation; |
| 34 |
use jj_lib::repo::ReadonlyRepo; |
| 35 |
use jj_lib::repo::Repo; |
| 36 |
use jj_lib::settings::UserSettings; |
| 37 |
use pollster::FutureExt as _; |
| 38 |
use testutils::CommitBuilderExt as _; |
| 39 |
use testutils::TestRepo; |
| 40 |
use testutils::TestResult; |
| 41 |
use testutils::write_random_commit; |
| 42 |
use testutils::write_random_commit_with_parents; |
| 43 |
|
| 44 |
fn get_predecessors(repo: &ReadonlyRepo, id: &CommitId) -> Vec<CommitId> { |
| 45 |
let entries: Vec<_> = walk_predecessors(repo, slice::from_ref(id)) |
| 46 |
.try_collect() |
| 47 |
.block_on() |
| 48 |
.expect("unreachable predecessors shouldn't be visited"); |
| 49 |
let first = entries |
| 50 |
.first() |
| 51 |
.expect("specified commit should be reachable"); |
| 52 |
first.predecessor_ids().to_vec() |
| 53 |
} |
| 54 |
|
| 55 |
fn list_dir(dir: &Path) -> Vec<String> { |
| 56 |
std::fs::read_dir(dir) |
| 57 |
.unwrap() |
| 58 |
.map(|entry| entry.unwrap().file_name().to_str().unwrap().to_owned()) |
| 59 |
.sorted() |
| 60 |
.collect() |
| 61 |
} |
| 62 |
|
| 63 |
fn index_has_id(index: &dyn Index, commit_id: &CommitId) -> bool { |
| 64 |
index.has_id(commit_id).block_on().unwrap() |
| 65 |
} |
| 66 |
|
| 67 |
#[test] |
| 68 |
fn test_unpublished_operation() -> TestResult { |
| 69 |
// Test that the operation doesn't get published until that's requested. |
| 70 |
let test_repo = TestRepo::init(); |
| 71 |
let repo = &test_repo.repo; |
| 72 |
|
| 73 |
let op_heads_dir = test_repo.repo_path().join("op_heads").join("heads"); |
| 74 |
let op_id0 = repo.op_id().clone(); |
| 75 |
assert_eq!(list_dir(&op_heads_dir), vec![repo.op_id().hex()]); |
| 76 |
|
| 77 |
let mut tx1 = repo.start_transaction(); |
| 78 |
write_random_commit(tx1.repo_mut()); |
| 79 |
let unpublished_op = tx1.write("transaction 1").block_on()?; |
| 80 |
let op_id1 = unpublished_op.operation().id().clone(); |
| 81 |
assert_ne!(op_id1, op_id0); |
| 82 |
assert_eq!(list_dir(&op_heads_dir), vec![op_id0.hex()]); |
| 83 |
unpublished_op.publish().block_on()?; |
| 84 |
assert_eq!(list_dir(&op_heads_dir), vec![op_id1.hex()]); |
| 85 |
Ok(()) |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_consecutive_operations() -> TestResult { |
| 90 |
// Test that consecutive operations result in a single op-head on disk after |
| 91 |
// each operation |
| 92 |
let test_repo = TestRepo::init(); |
| 93 |
let repo = &test_repo.repo; |
| 94 |
|
| 95 |
let op_heads_dir = test_repo.repo_path().join("op_heads").join("heads"); |
| 96 |
let op_id0 = repo.op_id().clone(); |
| 97 |
assert_eq!(list_dir(&op_heads_dir), vec![repo.op_id().hex()]); |
| 98 |
|
| 99 |
let mut tx1 = repo.start_transaction(); |
| 100 |
write_random_commit(tx1.repo_mut()); |
| 101 |
let op_id1 = tx1 |
| 102 |
.commit("transaction 1") |
| 103 |
.block_on()? |
| 104 |
.operation() |
| 105 |
.id() |
| 106 |
.clone(); |
| 107 |
assert_ne!(op_id1, op_id0); |
| 108 |
assert_eq!(list_dir(&op_heads_dir), vec![op_id1.hex()]); |
| 109 |
|
| 110 |
let repo = repo.reload_at_head().block_on()?; |
| 111 |
let mut tx2 = repo.start_transaction(); |
| 112 |
write_random_commit(tx2.repo_mut()); |
| 113 |
let op_id2 = tx2 |
| 114 |
.commit("transaction 2") |
| 115 |
.block_on()? |
| 116 |
.operation() |
| 117 |
.id() |
| 118 |
.clone(); |
| 119 |
assert_ne!(op_id2, op_id0); |
| 120 |
assert_ne!(op_id2, op_id1); |
| 121 |
assert_eq!(list_dir(&op_heads_dir), vec![op_id2.hex()]); |
| 122 |
|
| 123 |
// Reloading the repo makes no difference (there are no conflicting operations |
| 124 |
// to resolve). |
| 125 |
let _repo = repo.reload_at_head().block_on()?; |
| 126 |
assert_eq!(list_dir(&op_heads_dir), vec![op_id2.hex()]); |
| 127 |
Ok(()) |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn test_concurrent_operations() -> TestResult { |
| 132 |
// Test that consecutive operations result in multiple op-heads on disk until |
| 133 |
// the repo has been reloaded (which currently happens right away). |
| 134 |
let test_repo = TestRepo::init(); |
| 135 |
let repo = &test_repo.repo; |
| 136 |
|
| 137 |
let op_heads_dir = test_repo.repo_path().join("op_heads").join("heads"); |
| 138 |
let op_id0 = repo.op_id().clone(); |
| 139 |
assert_eq!(list_dir(&op_heads_dir), vec![repo.op_id().hex()]); |
| 140 |
|
| 141 |
let mut tx1 = repo.start_transaction(); |
| 142 |
write_random_commit(tx1.repo_mut()); |
| 143 |
let op_id1 = tx1 |
| 144 |
.commit("transaction 1") |
| 145 |
.block_on()? |
| 146 |
.operation() |
| 147 |
.id() |
| 148 |
.clone(); |
| 149 |
assert_ne!(op_id1, op_id0); |
| 150 |
assert_eq!(list_dir(&op_heads_dir), vec![op_id1.hex()]); |
| 151 |
|
| 152 |
// After both transactions have committed, we should have two op-heads on disk, |
| 153 |
// since they were run in parallel. |
| 154 |
let mut tx2 = repo.start_transaction(); |
| 155 |
write_random_commit(tx2.repo_mut()); |
| 156 |
let op_id2 = tx2 |
| 157 |
.commit("transaction 2") |
| 158 |
.block_on()? |
| 159 |
.operation() |
| 160 |
.id() |
| 161 |
.clone(); |
| 162 |
assert_ne!(op_id2, op_id0); |
| 163 |
assert_ne!(op_id2, op_id1); |
| 164 |
let mut actual_heads_on_disk = list_dir(&op_heads_dir); |
| 165 |
actual_heads_on_disk.sort(); |
| 166 |
let mut expected_heads_on_disk = vec![op_id1.hex(), op_id2.hex()]; |
| 167 |
expected_heads_on_disk.sort(); |
| 168 |
assert_eq!(actual_heads_on_disk, expected_heads_on_disk); |
| 169 |
|
| 170 |
// Reloading the repo causes the operations to be merged |
| 171 |
let repo = repo.reload_at_head().block_on()?; |
| 172 |
let merged_op_id = repo.op_id().clone(); |
| 173 |
assert_ne!(merged_op_id, op_id0); |
| 174 |
assert_ne!(merged_op_id, op_id1); |
| 175 |
assert_ne!(merged_op_id, op_id2); |
| 176 |
assert_eq!(list_dir(&op_heads_dir), vec![merged_op_id.hex()]); |
| 177 |
Ok(()) |
| 178 |
} |
| 179 |
|
| 180 |
fn assert_heads(repo: &dyn Repo, expected: Vec<&CommitId>) { |
| 181 |
let expected = expected.iter().copied().cloned().collect(); |
| 182 |
assert_eq!(*repo.view().heads(), expected); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn test_isolation() -> TestResult { |
| 187 |
// Test that two concurrent transactions don't see each other's changes. |
| 188 |
let test_repo = TestRepo::init(); |
| 189 |
let repo = &test_repo.repo; |
| 190 |
|
| 191 |
let mut tx = repo.start_transaction(); |
| 192 |
let initial = write_random_commit_with_parents(tx.repo_mut(), &[]); |
| 193 |
let repo = tx.commit("test").block_on()?; |
| 194 |
|
| 195 |
let mut tx1 = repo.start_transaction(); |
| 196 |
let mut_repo1 = tx1.repo_mut(); |
| 197 |
let mut tx2 = repo.start_transaction(); |
| 198 |
let mut_repo2 = tx2.repo_mut(); |
| 199 |
|
| 200 |
assert_heads(repo.as_ref(), vec![initial.id()]); |
| 201 |
assert_heads(mut_repo1, vec![initial.id()]); |
| 202 |
assert_heads(mut_repo2, vec![initial.id()]); |
| 203 |
|
| 204 |
let rewrite1 = mut_repo1 |
| 205 |
.rewrite_commit(&initial) |
| 206 |
.set_description("rewrite1") |
| 207 |
.write_unwrap(); |
| 208 |
mut_repo1.rebase_descendants().block_on()?; |
| 209 |
let rewrite2 = mut_repo2 |
| 210 |
.rewrite_commit(&initial) |
| 211 |
.set_description("rewrite2") |
| 212 |
.write_unwrap(); |
| 213 |
mut_repo2.rebase_descendants().block_on()?; |
| 214 |
|
| 215 |
// Neither transaction has committed yet, so each transaction sees its own |
| 216 |
// commit. |
| 217 |
assert_heads(repo.as_ref(), vec![initial.id()]); |
| 218 |
assert_heads(mut_repo1, vec![rewrite1.id()]); |
| 219 |
assert_heads(mut_repo2, vec![rewrite2.id()]); |
| 220 |
|
| 221 |
// The base repo and tx2 don't see the commits from tx1. |
| 222 |
tx1.commit("transaction 1").block_on()?; |
| 223 |
assert_heads(repo.as_ref(), vec![initial.id()]); |
| 224 |
assert_heads(mut_repo2, vec![rewrite2.id()]); |
| 225 |
|
| 226 |
// The base repo still doesn't see the commits after both transactions commit. |
| 227 |
tx2.commit("transaction 2").block_on()?; |
| 228 |
assert_heads(repo.as_ref(), vec![initial.id()]); |
| 229 |
// After reload, the base repo sees both rewrites. |
| 230 |
let repo = repo.reload_at_head().block_on()?; |
| 231 |
assert_heads(repo.as_ref(), vec![rewrite1.id(), rewrite2.id()]); |
| 232 |
Ok(()) |
| 233 |
} |
| 234 |
|
| 235 |
#[test] |
| 236 |
fn test_stored_commit_predecessors() -> TestResult { |
| 237 |
let test_repo = TestRepo::init(); |
| 238 |
let repo = &test_repo.repo; |
| 239 |
let loader = repo.loader(); |
| 240 |
|
| 241 |
let mut tx = repo.start_transaction(); |
| 242 |
let commit1 = write_random_commit(tx.repo_mut()); |
| 243 |
let commit2 = tx |
| 244 |
.repo_mut() |
| 245 |
.rewrite_commit(&commit1) |
| 246 |
.set_description("rewritten") |
| 247 |
.write_unwrap(); |
| 248 |
tx.repo_mut().rebase_descendants().block_on()?; |
| 249 |
let repo = tx.commit("test").block_on()?; |
| 250 |
|
| 251 |
// Reload operation from disk. |
| 252 |
let op = loader.load_operation(repo.op_id()).block_on()?; |
| 253 |
assert!(op.stores_commit_predecessors()); |
| 254 |
assert_matches!(op.predecessors_for_commit(commit1.id()), Some([])); |
| 255 |
assert_matches!(op.predecessors_for_commit(commit2.id()), Some([id]) if id == commit1.id()); |
| 256 |
|
| 257 |
// Save operation without the predecessors as old jj would do. |
| 258 |
let mut data = op.store_operation().clone(); |
| 259 |
data.commit_predecessors = None; |
| 260 |
let op_id = loader.op_store().write_operation(&data).block_on()?; |
| 261 |
assert_ne!(&op_id, op.id()); |
| 262 |
let op = loader.load_operation(&op_id).block_on()?; |
| 263 |
assert!(!op.stores_commit_predecessors()); |
| 264 |
Ok(()) |
| 265 |
} |
| 266 |
|
| 267 |
#[test] |
| 268 |
fn test_reparent_range_linear() -> TestResult { |
| 269 |
let test_repo = TestRepo::init(); |
| 270 |
let repo_0 = test_repo.repo; |
| 271 |
let loader = repo_0.loader(); |
| 272 |
let op_store = repo_0.op_store(); |
| 273 |
|
| 274 |
let read_op = |id| loader.load_operation(id).block_on().unwrap(); |
| 275 |
|
| 276 |
fn op_parents<const N: usize>(op: &Operation) -> [Operation; N] { |
| 277 |
let parents = op.parents().block_on().unwrap(); |
| 278 |
parents.try_into().unwrap() |
| 279 |
} |
| 280 |
|
| 281 |
// Set up linear operation graph: |
| 282 |
// D |
| 283 |
// C |
| 284 |
// B |
| 285 |
// A |
| 286 |
// 0 (initial) |
| 287 |
let random_tx = |repo: &Arc<ReadonlyRepo>| { |
| 288 |
let mut tx = repo.start_transaction(); |
| 289 |
write_random_commit(tx.repo_mut()); |
| 290 |
tx |
| 291 |
}; |
| 292 |
let repo_a = random_tx(&repo_0).commit("op A").block_on()?; |
| 293 |
let repo_b = random_tx(&repo_a).commit("op B").block_on()?; |
| 294 |
let repo_c = random_tx(&repo_b).commit("op C").block_on()?; |
| 295 |
let repo_d = random_tx(&repo_c).commit("op D").block_on()?; |
| 296 |
|
| 297 |
// Reparent B..D (=C|D) onto A: |
| 298 |
// D' |
| 299 |
// C' |
| 300 |
// A |
| 301 |
// 0 (initial) |
| 302 |
let stats = op_walk::reparent_range( |
| 303 |
op_store.as_ref(), |
| 304 |
slice::from_ref(repo_b.operation()), |
| 305 |
slice::from_ref(repo_d.operation()), |
| 306 |
repo_a.operation(), |
| 307 |
) |
| 308 |
.block_on()?; |
| 309 |
assert_eq!(stats.new_head_ids.len(), 1); |
| 310 |
assert_eq!(stats.rewritten_count, 2); |
| 311 |
assert_eq!(stats.unreachable_count, 1); |
| 312 |
let new_op_d = read_op(&stats.new_head_ids[0]); |
| 313 |
assert_eq!(new_op_d.metadata(), repo_d.operation().metadata()); |
| 314 |
assert_eq!(new_op_d.view_id(), repo_d.operation().view_id()); |
| 315 |
let [new_op_c] = op_parents(&new_op_d); |
| 316 |
assert_eq!(new_op_c.metadata(), repo_c.operation().metadata()); |
| 317 |
assert_eq!(new_op_c.view_id(), repo_c.operation().view_id()); |
| 318 |
assert_eq!(new_op_c.parent_ids(), slice::from_ref(repo_a.op_id())); |
| 319 |
|
| 320 |
// Reparent empty range onto A |
| 321 |
let stats = op_walk::reparent_range( |
| 322 |
op_store.as_ref(), |
| 323 |
slice::from_ref(repo_d.operation()), |
| 324 |
slice::from_ref(repo_d.operation()), |
| 325 |
repo_a.operation(), |
| 326 |
) |
| 327 |
.block_on()?; |
| 328 |
assert_eq!(stats.new_head_ids, vec![repo_a.op_id().clone()]); |
| 329 |
assert_eq!(stats.rewritten_count, 0); |
| 330 |
assert_eq!(stats.unreachable_count, 3); |
| 331 |
Ok(()) |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn test_reparent_range_branchy() -> TestResult { |
| 336 |
let test_repo = TestRepo::init(); |
| 337 |
let repo_0 = test_repo.repo; |
| 338 |
let loader = repo_0.loader(); |
| 339 |
let op_store = repo_0.op_store(); |
| 340 |
|
| 341 |
let read_op = |id| loader.load_operation(id).block_on().unwrap(); |
| 342 |
|
| 343 |
fn op_parents<const N: usize>(op: &Operation) -> [Operation; N] { |
| 344 |
let parents = op.parents().block_on().unwrap(); |
| 345 |
parents.try_into().unwrap() |
| 346 |
} |
| 347 |
|
| 348 |
// Set up branchy operation graph: |
| 349 |
// G |
| 350 |
// |\ |
| 351 |
// | F |
| 352 |
// E | |
| 353 |
// D | |
| 354 |
// |/ |
| 355 |
// C |
| 356 |
// B |
| 357 |
// A |
| 358 |
// 0 (initial) |
| 359 |
let random_tx = |repo: &Arc<ReadonlyRepo>| { |
| 360 |
let mut tx = repo.start_transaction(); |
| 361 |
write_random_commit(tx.repo_mut()); |
| 362 |
tx |
| 363 |
}; |
| 364 |
let repo_a = random_tx(&repo_0).commit("op A").block_on()?; |
| 365 |
let repo_b = random_tx(&repo_a).commit("op B").block_on()?; |
| 366 |
let repo_c = random_tx(&repo_b).commit("op C").block_on()?; |
| 367 |
let repo_d = random_tx(&repo_c).commit("op D").block_on()?; |
| 368 |
let tx_e = random_tx(&repo_d); |
| 369 |
let tx_f = random_tx(&repo_c); |
| 370 |
let repo_g = testutils::commit_transactions(vec![tx_e, tx_f]); |
| 371 |
let [op_e, op_f] = op_parents(repo_g.operation()); |
| 372 |
|
| 373 |
// Reparent D..G (= E|F|G) onto B: |
| 374 |
// G' |
| 375 |
// |\ |
| 376 |
// | F' |
| 377 |
// E'| |
| 378 |
// |/ |
| 379 |
// B |
| 380 |
// A |
| 381 |
// 0 (initial) |
| 382 |
let stats = op_walk::reparent_range( |
| 383 |
op_store.as_ref(), |
| 384 |
slice::from_ref(repo_d.operation()), |
| 385 |
slice::from_ref(repo_g.operation()), |
| 386 |
repo_b.operation(), |
| 387 |
) |
| 388 |
.block_on()?; |
| 389 |
assert_eq!(stats.new_head_ids.len(), 1); |
| 390 |
assert_eq!(stats.rewritten_count, 3); |
| 391 |
assert_eq!(stats.unreachable_count, 2); |
| 392 |
let new_op_g = read_op(&stats.new_head_ids[0]); |
| 393 |
assert_eq!(new_op_g.metadata(), repo_g.operation().metadata()); |
| 394 |
assert_eq!(new_op_g.view_id(), repo_g.operation().view_id()); |
| 395 |
let [new_op_e, new_op_f] = op_parents(&new_op_g); |
| 396 |
assert_eq!(new_op_e.parent_ids(), slice::from_ref(repo_b.op_id())); |
| 397 |
assert_eq!(new_op_f.parent_ids(), slice::from_ref(repo_b.op_id())); |
| 398 |
|
| 399 |
// Reparent B..G (=C|D|E|F|G) onto A: |
| 400 |
// G' |
| 401 |
// |\ |
| 402 |
// | F' |
| 403 |
// E'| |
| 404 |
// D'| |
| 405 |
// |/ |
| 406 |
// C' |
| 407 |
// A |
| 408 |
// 0 (initial) |
| 409 |
let stats = op_walk::reparent_range( |
| 410 |
op_store.as_ref(), |
| 411 |
slice::from_ref(repo_b.operation()), |
| 412 |
slice::from_ref(repo_g.operation()), |
| 413 |
repo_a.operation(), |
| 414 |
) |
| 415 |
.block_on()?; |
| 416 |
assert_eq!(stats.new_head_ids.len(), 1); |
| 417 |
assert_eq!(stats.rewritten_count, 5); |
| 418 |
assert_eq!(stats.unreachable_count, 1); |
| 419 |
let new_op_g = read_op(&stats.new_head_ids[0]); |
| 420 |
assert_eq!(new_op_g.metadata(), repo_g.operation().metadata()); |
| 421 |
assert_eq!(new_op_g.view_id(), repo_g.operation().view_id()); |
| 422 |
let [new_op_e, new_op_f] = op_parents(&new_op_g); |
| 423 |
let [new_op_d] = op_parents(&new_op_e); |
| 424 |
assert_eq!(new_op_d.parent_ids(), new_op_f.parent_ids()); |
| 425 |
let [new_op_c] = op_parents(&new_op_d); |
| 426 |
assert_eq!(new_op_c.parent_ids(), slice::from_ref(repo_a.op_id())); |
| 427 |
|
| 428 |
// Reparent (E|F)..G (=G) onto D: |
| 429 |
// G' |
| 430 |
// D |
| 431 |
// C |
| 432 |
// B |
| 433 |
// A |
| 434 |
// 0 (initial) |
| 435 |
let stats = op_walk::reparent_range( |
| 436 |
op_store.as_ref(), |
| 437 |
&[op_e.clone(), op_f.clone()], |
| 438 |
slice::from_ref(repo_g.operation()), |
| 439 |
repo_d.operation(), |
| 440 |
) |
| 441 |
.block_on()?; |
| 442 |
assert_eq!(stats.new_head_ids.len(), 1); |
| 443 |
assert_eq!(stats.rewritten_count, 1); |
| 444 |
assert_eq!(stats.unreachable_count, 2); |
| 445 |
let new_op_g = read_op(&stats.new_head_ids[0]); |
| 446 |
assert_eq!(new_op_g.metadata(), repo_g.operation().metadata()); |
| 447 |
assert_eq!(new_op_g.view_id(), repo_g.operation().view_id()); |
| 448 |
assert_eq!(new_op_g.parent_ids(), slice::from_ref(repo_d.op_id())); |
| 449 |
|
| 450 |
// Reparent C..F (=F) onto D (ignoring G): |
| 451 |
// F' |
| 452 |
// D |
| 453 |
// C |
| 454 |
// B |
| 455 |
// A |
| 456 |
// 0 (initial) |
| 457 |
let stats = op_walk::reparent_range( |
| 458 |
op_store.as_ref(), |
| 459 |
slice::from_ref(repo_c.operation()), |
| 460 |
slice::from_ref(&op_f), |
| 461 |
repo_d.operation(), |
| 462 |
) |
| 463 |
.block_on()?; |
| 464 |
assert_eq!(stats.new_head_ids.len(), 1); |
| 465 |
assert_eq!(stats.rewritten_count, 1); |
| 466 |
assert_eq!(stats.unreachable_count, 0); |
| 467 |
let new_op_f = read_op(&stats.new_head_ids[0]); |
| 468 |
assert_eq!(new_op_f.metadata(), op_f.metadata()); |
| 469 |
assert_eq!(new_op_f.view_id(), op_f.view_id()); |
| 470 |
assert_eq!(new_op_f.parent_ids(), slice::from_ref(repo_d.op_id())); |
| 471 |
Ok(()) |
| 472 |
} |
| 473 |
|
| 474 |
#[test] |
| 475 |
fn test_reparent_discarding_predecessors() -> TestResult { |
| 476 |
let test_repo = TestRepo::init(); |
| 477 |
let repo_0 = test_repo.repo; |
| 478 |
let loader = repo_0.loader(); |
| 479 |
let op_store = repo_0.op_store(); |
| 480 |
|
| 481 |
let repo_at = |id: &OperationId| { |
| 482 |
let op = loader.load_operation(id).block_on().unwrap(); |
| 483 |
loader.load_at(&op).block_on().unwrap() |
| 484 |
}; |
| 485 |
let head_commits = |repo: &dyn Repo| { |
| 486 |
repo.view() |
| 487 |
.heads() |
| 488 |
.iter() |
| 489 |
.map(|id| repo.store().get_commit(id).unwrap()) |
| 490 |
.collect_vec() |
| 491 |
}; |
| 492 |
|
| 493 |
// Set up rewriting as follows: |
| 494 |
// |
| 495 |
// op1 op2 op3 op4 |
| 496 |
// B0 B0 B1 B1 |
| 497 |
// | | | | |
| 498 |
// A0 A0 A1 A0 A1 A2 |
| 499 |
let mut tx = repo_0.start_transaction(); |
| 500 |
let commit_a0 = write_random_commit(tx.repo_mut()); |
| 501 |
let commit_b0 = write_random_commit_with_parents(tx.repo_mut(), &[&commit_a0]); |
| 502 |
let repo_1 = tx.commit("op1").block_on()?; |
| 503 |
|
| 504 |
let mut tx = repo_1.start_transaction(); |
| 505 |
let commit_a1 = tx |
| 506 |
.repo_mut() |
| 507 |
.rewrite_commit(&commit_a0) |
| 508 |
.set_description("a1") |
| 509 |
.write_unwrap(); |
| 510 |
tx.repo_mut().rebase_descendants().block_on()?; |
| 511 |
let [commit_b1] = head_commits(tx.repo()).try_into().unwrap(); |
| 512 |
tx.repo_mut().add_head(&commit_b0).block_on()?; // resurrect rewritten commits |
| 513 |
let repo_2 = tx.commit("op2").block_on()?; |
| 514 |
|
| 515 |
let mut tx = repo_2.start_transaction(); |
| 516 |
tx.repo_mut().record_abandoned_commit(&commit_b0); |
| 517 |
tx.repo_mut().rebase_descendants().block_on()?; |
| 518 |
let repo_3 = tx.commit("op3").block_on()?; |
| 519 |
|
| 520 |
let mut tx = repo_3.start_transaction(); |
| 521 |
tx.repo_mut().record_abandoned_commit(&commit_a0); |
| 522 |
tx.repo_mut().record_abandoned_commit(&commit_b1); |
| 523 |
let commit_a2 = tx |
| 524 |
.repo_mut() |
| 525 |
.rewrite_commit(&commit_a1) |
| 526 |
.set_description("a2") |
| 527 |
.write_unwrap(); |
| 528 |
tx.repo_mut().rebase_descendants().block_on()?; |
| 529 |
let repo_4 = tx.commit("op4").block_on()?; |
| 530 |
|
| 531 |
// Sanity check for the setup |
| 532 |
assert_eq!(repo_1.view().heads().len(), 1); |
| 533 |
assert_eq!(repo_2.view().heads().len(), 2); |
| 534 |
assert_eq!(repo_3.view().heads().len(), 2); |
| 535 |
assert_eq!(repo_4.view().heads().len(), 1); |
| 536 |
assert_eq!(repo_4.index().all_heads_for_gc()?.count(), 3); |
| 537 |
assert!(repo_4.operation().stores_commit_predecessors(),); |
| 538 |
assert_eq!( |
| 539 |
get_predecessors(&repo_4, commit_a1.id()), |
| 540 |
[commit_a0.id().clone()] |
| 541 |
); |
| 542 |
assert_eq!( |
| 543 |
get_predecessors(&repo_4, commit_a2.id()), |
| 544 |
[commit_a1.id().clone()] |
| 545 |
); |
| 546 |
assert_eq!( |
| 547 |
get_predecessors(&repo_4, commit_b1.id()), |
| 548 |
[commit_b0.id().clone()] |
| 549 |
); |
| 550 |
|
| 551 |
// Abandon op1 |
| 552 |
let stats = op_walk::reparent_range( |
| 553 |
op_store.as_ref(), |
| 554 |
slice::from_ref(repo_1.operation()), |
| 555 |
slice::from_ref(repo_4.operation()), |
| 556 |
repo_0.operation(), |
| 557 |
) |
| 558 |
.block_on()?; |
| 559 |
assert_eq!(stats.new_head_ids.len(), 1); |
| 560 |
assert_eq!(stats.rewritten_count, 3); |
| 561 |
assert_eq!(stats.unreachable_count, 1); |
| 562 |
let repo = repo_at(&stats.new_head_ids[0]); |
| 563 |
// A0 - B0 are still reachable |
| 564 |
assert!(index_has_id(repo.index(), commit_a0.id())); |
| 565 |
assert!(index_has_id(repo.index(), commit_b0.id())); |
| 566 |
assert_eq!( |
| 567 |
get_predecessors(&repo, commit_a1.id()), |
| 568 |
[commit_a0.id().clone()] |
| 569 |
); |
| 570 |
assert_eq!( |
| 571 |
get_predecessors(&repo, commit_b1.id()), |
| 572 |
[commit_b0.id().clone()] |
| 573 |
); |
| 574 |
assert_eq!(get_predecessors(&repo, commit_a0.id()), []); |
| 575 |
assert_eq!(get_predecessors(&repo, commit_b0.id()), []); |
| 576 |
|
| 577 |
// Abandon op1 and op2 |
| 578 |
let stats = op_walk::reparent_range( |
| 579 |
op_store.as_ref(), |
| 580 |
slice::from_ref(repo_2.operation()), |
| 581 |
slice::from_ref(repo_4.operation()), |
| 582 |
repo_0.operation(), |
| 583 |
) |
| 584 |
.block_on()?; |
| 585 |
assert_eq!(stats.new_head_ids.len(), 1); |
| 586 |
assert_eq!(stats.rewritten_count, 2); |
| 587 |
assert_eq!(stats.unreachable_count, 2); |
| 588 |
let repo = repo_at(&stats.new_head_ids[0]); |
| 589 |
// A0 is still reachable |
| 590 |
assert!(index_has_id(repo.index(), commit_a0.id())); |
| 591 |
// B0 is no longer reachable |
| 592 |
assert!(!index_has_id(repo.index(), commit_b0.id())); |
| 593 |
// the predecessor record `A1: A0` no longer exists |
| 594 |
assert_eq!(get_predecessors(&repo, commit_a1.id()), []); |
| 595 |
// Unreachable predecessors should be excluded |
| 596 |
assert_eq!(get_predecessors(&repo, commit_b1.id()), []); |
| 597 |
|
| 598 |
// Abandon op1, op2, and op3 |
| 599 |
let stats = op_walk::reparent_range( |
| 600 |
op_store.as_ref(), |
| 601 |
slice::from_ref(repo_3.operation()), |
| 602 |
slice::from_ref(repo_4.operation()), |
| 603 |
repo_0.operation(), |
| 604 |
) |
| 605 |
.block_on()?; |
| 606 |
assert_eq!(stats.new_head_ids.len(), 1); |
| 607 |
assert_eq!(stats.rewritten_count, 1); |
| 608 |
assert_eq!(stats.unreachable_count, 3); |
| 609 |
let repo = repo_at(&stats.new_head_ids[0]); |
| 610 |
// A0 is no longer reachable |
| 611 |
assert!(!index_has_id(repo.index(), commit_a0.id())); |
| 612 |
// A1 is still reachable through A2 |
| 613 |
assert!(index_has_id(repo.index(), commit_a1.id())); |
| 614 |
assert_eq!( |
| 615 |
get_predecessors(&repo, commit_a2.id()), |
| 616 |
[commit_a1.id().clone()] |
| 617 |
); |
| 618 |
assert_eq!(get_predecessors(&repo, commit_a1.id()), []); |
| 619 |
Ok(()) |
| 620 |
} |
| 621 |
|
| 622 |
fn stable_op_id_settings() -> UserSettings { |
| 623 |
let mut config = testutils::base_user_config(); |
| 624 |
config.add_layer( |
| 625 |
ConfigLayer::parse( |
| 626 |
ConfigSource::User, |
| 627 |
"debug.operation-timestamp = 2001-02-03T04:05:06+07:00", |
| 628 |
) |
| 629 |
.unwrap(), |
| 630 |
); |
| 631 |
UserSettings::from_config(config).unwrap() |
| 632 |
} |
| 633 |
|
| 634 |
#[test] |
| 635 |
fn test_resolve_op_id() -> TestResult { |
| 636 |
let settings = stable_op_id_settings(); |
| 637 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 638 |
let repo = test_repo.repo; |
| 639 |
let loader = repo.loader(); |
| 640 |
|
| 641 |
let mut operations = Vec::new(); |
| 642 |
// The actual value of `i` doesn't matter, we just need to make sure we end |
| 643 |
// up with hashes with ambiguous prefixes. |
| 644 |
for i in (1..5).chain([10, 24, 9]) { |
| 645 |
let tx = repo.start_transaction(); |
| 646 |
let repo = tx.commit(format!("transaction {i}")).block_on()?; |
| 647 |
operations.push(repo.operation().clone()); |
| 648 |
} |
| 649 |
// "b" is ambiguous (operations[2] and operations[5]) |
| 650 |
// "0" is ambiguous with the root operation (operations[6]) |
| 651 |
insta::assert_debug_snapshot!(operations.iter().map(|op| op.id().hex()).collect_vec(), @r#" |
| 652 |
[ |
| 653 |
"33d54aefeb762fec7d849fba6cdb19e4f571f9b0a14445c9ab764d800d52c7abbe9d6bb8a152a6e2b02514de2ca769038c8e3ec0093d254369d1c138eaf5b02a", |
| 654 |
"13f64f9b2c7a8a882f3e5e03885cdc0cb64c8bc40e6f2b6f188c84a4ac49f9e6f1e49e5b3a1c627ef8c077184ea870e532a06554c9e9783bf78338a5e4c0b781", |
| 655 |
"b1e7aa2b8e7d6015d7f281baadb1f34bdbb8841a762a7e164f7849973ace8d0b8f87309416ccc81c0459373846022c24a93486f8a4ed2ff0ec4221b587ca28f2", |
| 656 |
"c9cc85d1bf80aa756583a1ba708961f94643277ae716d913b17152e80b42f1886afd1127ec8e34fc15507040ec5a643e32d4b727ea70417ba2fbb29e2e225f88", |
| 657 |
"94cfe4b05e25e4329dcd2494e0e4a4e4a6608c65ccc9d0a186c81d3bf8f5606ec7b31cd9333dcacbc658ebe0df5e51f91d2d52b58a5806e1273d31c083e4acd6", |
| 658 |
"b527c15f0261c2520ffe84056f8eb3a216a9994b7117fe17b996f7203bb3cebeace369461f481c412f9969a91490dc5af629efe109c3433afa0c6d0b26fdbc17", |
| 659 |
"007480c57328c8532149d66fb2a44b6e4ff79e44362e0c9578f82c4147c8cbebf84ad08d9a1f661814902cef89bc76d90628cb606f2431e45f74493d5e1e0cbd", |
| 660 |
] |
| 661 |
"#); |
| 662 |
|
| 663 |
let repo_loader = repo.loader(); |
| 664 |
let resolve = |op_str: &str| op_walk::resolve_op_for_load(repo_loader, op_str).block_on(); |
| 665 |
|
| 666 |
// Full id |
| 667 |
assert_eq!(resolve(&operations[0].id().hex())?, operations[0]); |
| 668 |
// Short id, odd length |
| 669 |
assert_eq!(resolve(&operations[0].id().hex()[..3])?, operations[0]); |
| 670 |
// Short id, even length |
| 671 |
assert_eq!(resolve(&operations[1].id().hex()[..2])?, operations[1]); |
| 672 |
// Ambiguous id |
| 673 |
assert_matches!( |
| 674 |
resolve("b"), |
| 675 |
Err(OpsetEvaluationError::OpsetResolution( |
| 676 |
OpsetResolutionError::AmbiguousIdPrefix(_) |
| 677 |
)) |
| 678 |
); |
| 679 |
// Empty id |
| 680 |
assert_matches!( |
| 681 |
resolve(""), |
| 682 |
Err(OpsetEvaluationError::OpsetResolution( |
| 683 |
OpsetResolutionError::InvalidIdPrefix(_) |
| 684 |
)) |
| 685 |
); |
| 686 |
// Unknown id |
| 687 |
assert_matches!( |
| 688 |
resolve("deadbee"), |
| 689 |
Err(OpsetEvaluationError::OpsetResolution( |
| 690 |
OpsetResolutionError::NoSuchOperation(_) |
| 691 |
)) |
| 692 |
); |
| 693 |
// Virtual root id |
| 694 |
let root_operation = loader.root_operation().block_on(); |
| 695 |
assert_eq!(resolve(&root_operation.id().hex())?, root_operation); |
| 696 |
assert_eq!(resolve("b5")?, operations[5]); |
| 697 |
// "0" is ambiguous with the root operation and operations[6] |
| 698 |
assert_matches!( |
| 699 |
resolve("0"), |
| 700 |
Err(OpsetEvaluationError::OpsetResolution( |
| 701 |
OpsetResolutionError::AmbiguousIdPrefix(_) |
| 702 |
)) |
| 703 |
); |
| 704 |
assert_matches!( |
| 705 |
resolve("00"), |
| 706 |
Err(OpsetEvaluationError::OpsetResolution( |
| 707 |
OpsetResolutionError::AmbiguousIdPrefix(_) |
| 708 |
)) |
| 709 |
); |
| 710 |
assert_eq!(resolve("000")?, root_operation); |
| 711 |
assert_eq!(resolve("007")?, operations[6]); |
| 712 |
Ok(()) |
| 713 |
} |
| 714 |
|
| 715 |
#[test] |
| 716 |
fn test_resolve_current_op() -> TestResult { |
| 717 |
let settings = stable_op_id_settings(); |
| 718 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 719 |
let repo = test_repo.repo; |
| 720 |
|
| 721 |
assert_eq!( |
| 722 |
op_walk::resolve_op_with_repo(&repo, "@").block_on()?, |
| 723 |
*repo.operation() |
| 724 |
); |
| 725 |
Ok(()) |
| 726 |
} |
| 727 |
|
| 728 |
#[test] |
| 729 |
fn test_resolve_op_parents_children() -> TestResult { |
| 730 |
// Use monotonic timestamp to stabilize merge order of transactions |
| 731 |
let settings = testutils::user_settings(); |
| 732 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 733 |
let mut repo = &test_repo.repo; |
| 734 |
|
| 735 |
let mut repos = Vec::new(); |
| 736 |
for _ in 0..3 { |
| 737 |
let tx = repo.start_transaction(); |
| 738 |
repos.push(tx.commit("test").block_on()?); |
| 739 |
repo = repos.last().unwrap(); |
| 740 |
} |
| 741 |
let operations = repos.iter().map(|repo| repo.operation()).collect_vec(); |
| 742 |
|
| 743 |
// Parent |
| 744 |
let op2_id_hex = operations[2].id().hex(); |
| 745 |
assert_eq!( |
| 746 |
op_walk::resolve_op_with_repo(repo, &format!("{op2_id_hex}-")).block_on()?, |
| 747 |
*operations[1] |
| 748 |
); |
| 749 |
assert_eq!( |
| 750 |
op_walk::resolve_op_with_repo(repo, &format!("{op2_id_hex}--")).block_on()?, |
| 751 |
*operations[0] |
| 752 |
); |
| 753 |
// "{op2_id_hex}----" is the root operation |
| 754 |
assert_matches!( |
| 755 |
op_walk::resolve_op_with_repo(repo, &format!("{op2_id_hex}-----")).block_on(), |
| 756 |
Err(OpsetEvaluationError::OpsetResolution( |
| 757 |
OpsetResolutionError::EmptyOperations(_) |
| 758 |
)) |
| 759 |
); |
| 760 |
|
| 761 |
// Child |
| 762 |
let op0_id_hex = operations[0].id().hex(); |
| 763 |
assert_eq!( |
| 764 |
op_walk::resolve_op_with_repo(repo, &format!("{op0_id_hex}+")).block_on()?, |
| 765 |
*operations[1] |
| 766 |
); |
| 767 |
assert_eq!( |
| 768 |
op_walk::resolve_op_with_repo(repo, &format!("{op0_id_hex}++")).block_on()?, |
| 769 |
*operations[2] |
| 770 |
); |
| 771 |
assert_matches!( |
| 772 |
op_walk::resolve_op_with_repo(repo, &format!("{op0_id_hex}+++")).block_on(), |
| 773 |
Err(OpsetEvaluationError::OpsetResolution( |
| 774 |
OpsetResolutionError::EmptyOperations(_) |
| 775 |
)) |
| 776 |
); |
| 777 |
|
| 778 |
// Child of parent |
| 779 |
assert_eq!( |
| 780 |
op_walk::resolve_op_with_repo(repo, &format!("{op2_id_hex}--+")).block_on()?, |
| 781 |
*operations[1] |
| 782 |
); |
| 783 |
|
| 784 |
// Child at old repo: new operations shouldn't be visible |
| 785 |
assert_eq!( |
| 786 |
op_walk::resolve_op_with_repo(&repos[1], &format!("{op0_id_hex}+")).block_on()?, |
| 787 |
*operations[1] |
| 788 |
); |
| 789 |
assert_matches!( |
| 790 |
op_walk::resolve_op_with_repo(&repos[0], &format!("{op0_id_hex}+")).block_on(), |
| 791 |
Err(OpsetEvaluationError::OpsetResolution( |
| 792 |
OpsetResolutionError::EmptyOperations(_) |
| 793 |
)) |
| 794 |
); |
| 795 |
|
| 796 |
// Merge and fork |
| 797 |
let tx1 = repo.start_transaction(); |
| 798 |
let tx2 = repo.start_transaction(); |
| 799 |
let repo = testutils::commit_transactions(vec![tx1, tx2]); |
| 800 |
let parent_op_ids = repo.operation().parent_ids(); |
| 801 |
|
| 802 |
// The subexpression that resolves to multiple operations (i.e. the accompanying |
| 803 |
// op ids) should be reported, not the full expression provided by the user. |
| 804 |
let op5_id_hex = repo.operation().id().hex(); |
| 805 |
let parents_op_str = format!("{op5_id_hex}-"); |
| 806 |
let error = op_walk::resolve_op_with_repo(&repo, &parents_op_str) |
| 807 |
.block_on() |
| 808 |
.unwrap_err(); |
| 809 |
assert_eq!( |
| 810 |
extract_multiple_operations_error(&error).unwrap(), |
| 811 |
(&parents_op_str, parent_op_ids) |
| 812 |
); |
| 813 |
let grandparents_op_str = format!("{op5_id_hex}--"); |
| 814 |
let error = op_walk::resolve_op_with_repo(&repo, &grandparents_op_str) |
| 815 |
.block_on() |
| 816 |
.unwrap_err(); |
| 817 |
assert_eq!( |
| 818 |
extract_multiple_operations_error(&error).unwrap(), |
| 819 |
(&parents_op_str, parent_op_ids) |
| 820 |
); |
| 821 |
let children_of_parents_op_str = format!("{op5_id_hex}-+"); |
| 822 |
let error = op_walk::resolve_op_with_repo(&repo, &children_of_parents_op_str) |
| 823 |
.block_on() |
| 824 |
.unwrap_err(); |
| 825 |
assert_eq!( |
| 826 |
extract_multiple_operations_error(&error).unwrap(), |
| 827 |
(&parents_op_str, parent_op_ids) |
| 828 |
); |
| 829 |
|
| 830 |
let op2_id_hex = operations[2].id().hex(); |
| 831 |
let op_str = format!("{op2_id_hex}+"); |
| 832 |
let error = op_walk::resolve_op_with_repo(&repo, &op_str) |
| 833 |
.block_on() |
| 834 |
.unwrap_err(); |
| 835 |
assert_eq!( |
| 836 |
extract_multiple_operations_error(&error).unwrap(), |
| 837 |
(&op_str, parent_op_ids) |
| 838 |
); |
| 839 |
Ok(()) |
| 840 |
} |
| 841 |
|
| 842 |
#[test] |
| 843 |
fn test_walk_ancestors() -> TestResult { |
| 844 |
let test_repo = TestRepo::init(); |
| 845 |
let repo_0 = test_repo.repo; |
| 846 |
let loader = repo_0.loader(); |
| 847 |
|
| 848 |
fn op_parents<const N: usize>(op: &Operation) -> [Operation; N] { |
| 849 |
let parents = op.parents().block_on().unwrap(); |
| 850 |
parents.try_into().unwrap() |
| 851 |
} |
| 852 |
|
| 853 |
fn collect_ancestors(head_ops: &[Operation]) -> Vec<Operation> { |
| 854 |
op_walk::walk_ancestors(head_ops) |
| 855 |
.try_collect() |
| 856 |
.block_on() |
| 857 |
.unwrap() |
| 858 |
} |
| 859 |
|
| 860 |
fn collect_ancestors_range(head_ops: &[Operation], root_ops: &[Operation]) -> Vec<Operation> { |
| 861 |
op_walk::walk_ancestors_range(head_ops, root_ops) |
| 862 |
.try_collect() |
| 863 |
.block_on() |
| 864 |
.unwrap() |
| 865 |
} |
| 866 |
|
| 867 |
// Set up operation graph: |
| 868 |
// H |
| 869 |
// G |
| 870 |
// |\ |
| 871 |
// | F |
| 872 |
// E | |
| 873 |
// D | |
| 874 |
// |/ |
| 875 |
// C |
| 876 |
// | B |
| 877 |
// A | |
| 878 |
// |/ |
| 879 |
// 0 (initial) |
| 880 |
let repo_a = repo_0.start_transaction().commit("op A").block_on()?; |
| 881 |
let repo_b = repo_0 |
| 882 |
.start_transaction() |
| 883 |
.write("op B") |
| 884 |
.block_on()? |
| 885 |
.leave_unpublished(); |
| 886 |
let repo_c = repo_a.start_transaction().commit("op C").block_on()?; |
| 887 |
let repo_d = repo_c.start_transaction().commit("op D").block_on()?; |
| 888 |
let tx_e = repo_d.start_transaction(); |
| 889 |
let tx_f = repo_c.start_transaction(); |
| 890 |
let repo_g = testutils::commit_transactions(vec![tx_e, tx_f]); |
| 891 |
let [op_e, op_f] = op_parents(repo_g.operation()); |
| 892 |
let repo_h = repo_g.start_transaction().commit("op H").block_on()?; |
| 893 |
|
| 894 |
// At merge, parents are visited in forward order, which isn't important. |
| 895 |
assert_eq!( |
| 896 |
collect_ancestors(slice::from_ref(repo_h.operation())), |
| 897 |
[ |
| 898 |
repo_h.operation().clone(), |
| 899 |
repo_g.operation().clone(), |
| 900 |
op_e.clone(), |
| 901 |
repo_d.operation().clone(), |
| 902 |
op_f.clone(), |
| 903 |
repo_c.operation().clone(), |
| 904 |
repo_a.operation().clone(), |
| 905 |
loader.root_operation().block_on(), |
| 906 |
] |
| 907 |
); |
| 908 |
|
| 909 |
// Ancestors of multiple heads |
| 910 |
assert_eq!( |
| 911 |
collect_ancestors(&[op_f.clone(), repo_b.operation().clone()]), |
| 912 |
[ |
| 913 |
op_f.clone(), |
| 914 |
repo_c.operation().clone(), |
| 915 |
repo_a.operation().clone(), |
| 916 |
repo_b.operation().clone(), |
| 917 |
loader.root_operation().block_on(), |
| 918 |
] |
| 919 |
); |
| 920 |
|
| 921 |
// Exclude direct ancestor |
| 922 |
assert_eq!( |
| 923 |
collect_ancestors_range( |
| 924 |
slice::from_ref(repo_h.operation()), |
| 925 |
slice::from_ref(repo_d.operation()), |
| 926 |
), |
| 927 |
[ |
| 928 |
repo_h.operation().clone(), |
| 929 |
repo_g.operation().clone(), |
| 930 |
op_e.clone(), |
| 931 |
op_f.clone(), |
| 932 |
] |
| 933 |
); |
| 934 |
|
| 935 |
// Exclude indirect ancestor |
| 936 |
assert_eq!( |
| 937 |
collect_ancestors_range(slice::from_ref(&op_e), slice::from_ref(&op_f)), |
| 938 |
[op_e.clone(), repo_d.operation().clone()] |
| 939 |
); |
| 940 |
|
| 941 |
// Exclude far ancestor |
| 942 |
assert_eq!( |
| 943 |
collect_ancestors_range( |
| 944 |
slice::from_ref(repo_h.operation()), |
| 945 |
slice::from_ref(repo_a.operation()), |
| 946 |
), |
| 947 |
[ |
| 948 |
repo_h.operation().clone(), |
| 949 |
repo_g.operation().clone(), |
| 950 |
op_e.clone(), |
| 951 |
repo_d.operation().clone(), |
| 952 |
op_f.clone(), |
| 953 |
repo_c.operation().clone(), |
| 954 |
] |
| 955 |
); |
| 956 |
|
| 957 |
// Exclude ancestors of descendant |
| 958 |
assert_eq!( |
| 959 |
collect_ancestors_range( |
| 960 |
slice::from_ref(repo_g.operation()), |
| 961 |
slice::from_ref(repo_h.operation()), |
| 962 |
), |
| 963 |
[] |
| 964 |
); |
| 965 |
|
| 966 |
// Exclude multiple roots |
| 967 |
assert_eq!( |
| 968 |
collect_ancestors_range( |
| 969 |
slice::from_ref(repo_g.operation()), |
| 970 |
&[repo_d.operation().clone(), op_f.clone()], |
| 971 |
), |
| 972 |
[repo_g.operation().clone(), op_e.clone()] |
| 973 |
); |
| 974 |
Ok(()) |
| 975 |
} |
| 976 |
|
| 977 |
#[test] |
| 978 |
fn test_gc() -> TestResult { |
| 979 |
let settings = stable_op_id_settings(); |
| 980 |
let test_repo = TestRepo::init_with_settings(&settings); |
| 981 |
let op_dir = test_repo.repo_path().join("op_store").join("operations"); |
| 982 |
let view_dir = test_repo.repo_path().join("op_store").join("views"); |
| 983 |
let repo_0 = test_repo.repo; |
| 984 |
let op_store = repo_0.op_store(); |
| 985 |
|
| 986 |
// Set up operation graph: |
| 987 |
// |
| 988 |
// F |
| 989 |
// E (empty) |
| 990 |
// D | |
| 991 |
// C | |
| 992 |
// |/ |
| 993 |
// B |
| 994 |
// A |
| 995 |
// 0 (root) |
| 996 |
let empty_tx = |repo: &Arc<ReadonlyRepo>| repo.start_transaction(); |
| 997 |
let random_tx = |repo: &Arc<ReadonlyRepo>| { |
| 998 |
let mut tx = repo.start_transaction(); |
| 999 |
write_random_commit(tx.repo_mut()); |
| 1000 |
tx |
| 1001 |
}; |
| 1002 |
let repo_a = random_tx(&repo_0).commit("op A").block_on()?; |
| 1003 |
let repo_b = random_tx(&repo_a).commit("op B").block_on()?; |
| 1004 |
let repo_c = random_tx(&repo_b).commit("op C").block_on()?; |
| 1005 |
let repo_d = random_tx(&repo_c).commit("op D").block_on()?; |
| 1006 |
let repo_e = empty_tx(&repo_b).commit("op E").block_on()?; |
| 1007 |
let repo_f = random_tx(&repo_e).commit("op F").block_on()?; |
| 1008 |
|
| 1009 |
// Sanity check for the original state |
| 1010 |
let mut expected_op_entries = list_dir(&op_dir); |
| 1011 |
let mut expected_view_entries = list_dir(&view_dir); |
| 1012 |
assert_eq!(expected_op_entries.len(), 6); |
| 1013 |
assert_eq!(expected_view_entries.len(), 5); |
| 1014 |
|
| 1015 |
// No heads, but all kept by file modification time |
| 1016 |
op_store.gc(&[], SystemTime::UNIX_EPOCH).block_on()?; |
| 1017 |
assert_eq!(list_dir(&op_dir), expected_op_entries); |
| 1018 |
assert_eq!(list_dir(&view_dir), expected_view_entries); |
| 1019 |
|
| 1020 |
// All reachable from heads |
| 1021 |
let now = SystemTime::now(); |
| 1022 |
let head_ids = [repo_d.op_id().clone(), repo_f.op_id().clone()]; |
| 1023 |
op_store.gc(&head_ids, now).block_on()?; |
| 1024 |
assert_eq!(list_dir(&op_dir), expected_op_entries); |
| 1025 |
assert_eq!(list_dir(&view_dir), expected_view_entries); |
| 1026 |
|
| 1027 |
// E|F are no longer reachable, but E's view is still reachable |
| 1028 |
op_store |
| 1029 |
.gc(slice::from_ref(repo_d.op_id()), now) |
| 1030 |
.block_on()?; |
| 1031 |
expected_op_entries |
| 1032 |
.retain(|name| *name != repo_e.op_id().hex() && *name != repo_f.op_id().hex()); |
| 1033 |
expected_view_entries.retain(|name| *name != repo_f.operation().view_id().hex()); |
| 1034 |
assert_eq!(list_dir(&op_dir), expected_op_entries); |
| 1035 |
assert_eq!(list_dir(&view_dir), expected_view_entries); |
| 1036 |
|
| 1037 |
// B|C|D are no longer reachable |
| 1038 |
op_store |
| 1039 |
.gc(slice::from_ref(repo_a.op_id()), now) |
| 1040 |
.block_on()?; |
| 1041 |
expected_op_entries.retain(|name| { |
| 1042 |
*name != repo_b.op_id().hex() |
| 1043 |
&& *name != repo_c.op_id().hex() |
| 1044 |
&& *name != repo_d.op_id().hex() |
| 1045 |
}); |
| 1046 |
expected_view_entries.retain(|name| { |
| 1047 |
*name != repo_b.operation().view_id().hex() |
| 1048 |
&& *name != repo_c.operation().view_id().hex() |
| 1049 |
&& *name != repo_d.operation().view_id().hex() |
| 1050 |
}); |
| 1051 |
assert_eq!(list_dir(&op_dir), expected_op_entries); |
| 1052 |
assert_eq!(list_dir(&view_dir), expected_view_entries); |
| 1053 |
|
| 1054 |
// Sanity check for the last state |
| 1055 |
assert_eq!(expected_op_entries.len(), 1); |
| 1056 |
assert_eq!(expected_view_entries.len(), 1); |
| 1057 |
Ok(()) |
| 1058 |
} |
| 1059 |
|
| 1060 |
#[track_caller] |
| 1061 |
fn extract_multiple_operations_error( |
| 1062 |
error: &OpsetEvaluationError, |
| 1063 |
) -> Option<(&String, &[OperationId])> { |
| 1064 |
if let OpsetEvaluationError::OpsetResolution(OpsetResolutionError::MultipleOperations { |
| 1065 |
expr, |
| 1066 |
candidates, |
| 1067 |
}) = error |
| 1068 |
{ |
| 1069 |
Some((expr, candidates)) |
| 1070 |
} else { |
| 1071 |
None |
| 1072 |
} |
| 1073 |
} |