| 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 |
|
| 17 |
use itertools::Itertools as _; |
| 18 |
use jj_lib::default_backend_factories::default_backend_factories; |
| 19 |
use jj_lib::default_backend_factories::default_working_copy_factories; |
| 20 |
use jj_lib::repo::Repo as _; |
| 21 |
use jj_lib::workspace::Workspace; |
| 22 |
use pollster::FutureExt as _; |
| 23 |
use test_case::test_case; |
| 24 |
use testutils::TestRepoBackend; |
| 25 |
use testutils::TestResult; |
| 26 |
use testutils::TestWorkspace; |
| 27 |
use testutils::write_random_commit; |
| 28 |
use testutils::write_random_commit_with_parents; |
| 29 |
|
| 30 |
fn copy_directory(src: &Path, dst: &Path) { |
| 31 |
std::fs::create_dir(dst).ok(); |
| 32 |
for entry in std::fs::read_dir(src).unwrap() { |
| 33 |
let child_src = entry.unwrap().path(); |
| 34 |
let base_name = child_src.file_name().unwrap(); |
| 35 |
let child_dst = dst.join(base_name); |
| 36 |
if child_src.is_dir() { |
| 37 |
copy_directory(&child_src, &child_dst); |
| 38 |
} else { |
| 39 |
std::fs::copy(&child_src, &child_dst).unwrap(); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
fn merge_directories(left: &Path, base: &Path, right: &Path, output: &Path) { |
| 45 |
std::fs::create_dir(output).unwrap(); |
| 46 |
let mut sub_dirs = vec![]; |
| 47 |
// Walk the left side and copy to the output |
| 48 |
if left.exists() { |
| 49 |
for entry in std::fs::read_dir(left).unwrap() { |
| 50 |
let path = entry.unwrap().path(); |
| 51 |
let base_name = path.file_name().unwrap(); |
| 52 |
let child_left = left.join(base_name); |
| 53 |
let child_output = output.join(base_name); |
| 54 |
if child_left.is_dir() { |
| 55 |
sub_dirs.push(base_name.to_os_string()); |
| 56 |
} else { |
| 57 |
std::fs::copy(&child_left, child_output).unwrap(); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
// Walk the base and find files removed in the right side, then remove them in |
| 62 |
// the output |
| 63 |
if base.exists() { |
| 64 |
for entry in std::fs::read_dir(base).unwrap() { |
| 65 |
let path = entry.unwrap().path(); |
| 66 |
let base_name = path.file_name().unwrap(); |
| 67 |
let child_base = base.join(base_name); |
| 68 |
let child_right = right.join(base_name); |
| 69 |
let child_output = output.join(base_name); |
| 70 |
if child_base.is_dir() { |
| 71 |
sub_dirs.push(base_name.to_os_string()); |
| 72 |
} else if !child_right.exists() { |
| 73 |
std::fs::remove_file(child_output).ok(); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
// Walk the right side and find files added in the right side, then add them in |
| 78 |
// the output |
| 79 |
if right.exists() { |
| 80 |
for entry in std::fs::read_dir(right).unwrap() { |
| 81 |
let path = entry.unwrap().path(); |
| 82 |
let base_name = path.file_name().unwrap(); |
| 83 |
let child_base = base.join(base_name); |
| 84 |
let child_right = right.join(base_name); |
| 85 |
let child_output = output.join(base_name); |
| 86 |
if child_right.is_dir() { |
| 87 |
sub_dirs.push(base_name.to_os_string()); |
| 88 |
} else if !child_base.exists() { |
| 89 |
// This overwrites the left side if that's been written. That's fine, since the |
| 90 |
// point of the test is that it should be okay for either side to win. |
| 91 |
std::fs::copy(&child_right, child_output).unwrap(); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
// Do the merge in subdirectories |
| 96 |
for base_name in sub_dirs.iter().sorted().dedup() { |
| 97 |
let child_base = base.join(base_name); |
| 98 |
let child_right = right.join(base_name); |
| 99 |
let child_left = left.join(base_name); |
| 100 |
let child_output = output.join(base_name); |
| 101 |
merge_directories(&child_left, &child_base, &child_right, &child_output); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
#[test_case(TestRepoBackend::Simple; "simple backend")] |
| 106 |
#[test_case(TestRepoBackend::Git; "git backend")] |
| 107 |
fn test_bad_locking_children(backend: TestRepoBackend) -> TestResult { |
| 108 |
// Test that two new commits created on separate machines are both visible (not |
| 109 |
// lost due to lack of locking) |
| 110 |
let settings = testutils::user_settings(); |
| 111 |
let test_workspace = TestWorkspace::init_with_backend_and_settings(backend, &settings); |
| 112 |
let repo = &test_workspace.repo; |
| 113 |
let workspace_root = test_workspace.workspace.workspace_root(); |
| 114 |
|
| 115 |
let mut tx = repo.start_transaction(); |
| 116 |
let initial = write_random_commit(tx.repo_mut()); |
| 117 |
tx.commit("test").block_on()?; |
| 118 |
|
| 119 |
// Simulate a write of a commit that happens on one machine |
| 120 |
let machine1_root = test_workspace.root_dir().join("machine1"); |
| 121 |
copy_directory(workspace_root, &machine1_root); |
| 122 |
let machine1_workspace = Workspace::load( |
| 123 |
&settings, |
| 124 |
&machine1_root, |
| 125 |
&default_backend_factories(), |
| 126 |
&default_working_copy_factories(), |
| 127 |
)?; |
| 128 |
let machine1_repo = machine1_workspace.repo_loader().load_at_head().block_on()?; |
| 129 |
let mut machine1_tx = machine1_repo.start_transaction(); |
| 130 |
let child1 = write_random_commit_with_parents(machine1_tx.repo_mut(), &[&initial]); |
| 131 |
machine1_tx.commit("test").block_on()?; |
| 132 |
|
| 133 |
// Simulate a write of a commit that happens on another machine |
| 134 |
let machine2_root = test_workspace.root_dir().join("machine2"); |
| 135 |
copy_directory(workspace_root, &machine2_root); |
| 136 |
let machine2_workspace = Workspace::load( |
| 137 |
&settings, |
| 138 |
&machine2_root, |
| 139 |
&default_backend_factories(), |
| 140 |
&default_working_copy_factories(), |
| 141 |
)?; |
| 142 |
let machine2_repo = machine2_workspace.repo_loader().load_at_head().block_on()?; |
| 143 |
let mut machine2_tx = machine2_repo.start_transaction(); |
| 144 |
let child2 = write_random_commit_with_parents(machine2_tx.repo_mut(), &[&initial]); |
| 145 |
machine2_tx.commit("test").block_on()?; |
| 146 |
|
| 147 |
// Simulate that the distributed file system now has received the changes from |
| 148 |
// both machines |
| 149 |
let merged_path = test_workspace.root_dir().join("merged"); |
| 150 |
merge_directories(&machine1_root, workspace_root, &machine2_root, &merged_path); |
| 151 |
let merged_workspace = Workspace::load( |
| 152 |
&settings, |
| 153 |
&merged_path, |
| 154 |
&default_backend_factories(), |
| 155 |
&default_working_copy_factories(), |
| 156 |
)?; |
| 157 |
let merged_repo = merged_workspace.repo_loader().load_at_head().block_on()?; |
| 158 |
assert!(merged_repo.view().heads().contains(child1.id())); |
| 159 |
assert!(merged_repo.view().heads().contains(child2.id())); |
| 160 |
let op_id = merged_repo.op_id().clone(); |
| 161 |
let op = merged_repo.op_store().read_operation(&op_id).block_on()?; |
| 162 |
assert_eq!(op.parents.len(), 2); |
| 163 |
Ok(()) |
| 164 |
} |
| 165 |
|
| 166 |
#[test_case(TestRepoBackend::Simple ; "simple backend")] |
| 167 |
#[test_case(TestRepoBackend::Git ; "git backend")] |
| 168 |
fn test_bad_locking_interrupted(backend: TestRepoBackend) -> TestResult { |
| 169 |
// Test that an interrupted update of the op-heads resulting in on op-head |
| 170 |
// that's a descendant of the other is resolved without creating a new |
| 171 |
// operation. |
| 172 |
let settings = testutils::user_settings(); |
| 173 |
let test_workspace = TestWorkspace::init_with_backend_and_settings(backend, &settings); |
| 174 |
let test_env = &test_workspace.env; |
| 175 |
let repo = &test_workspace.repo; |
| 176 |
|
| 177 |
let mut tx = repo.start_transaction(); |
| 178 |
let initial = write_random_commit(tx.repo_mut()); |
| 179 |
let repo = tx.commit("test").block_on()?; |
| 180 |
|
| 181 |
// Simulate a crash that resulted in the old op-head left in place. We simulate |
| 182 |
// it somewhat hackily by copying the .jj/op_heads/ directory before the |
| 183 |
// operation and then copying that back afterwards, leaving the existing |
| 184 |
// op-head(s) in place. |
| 185 |
let op_heads_dir = test_workspace.repo_path().join("op_heads"); |
| 186 |
let backup_path = test_workspace.root_dir().join("backup"); |
| 187 |
copy_directory(&op_heads_dir, &backup_path); |
| 188 |
let mut tx = repo.start_transaction(); |
| 189 |
write_random_commit_with_parents(tx.repo_mut(), &[&initial]); |
| 190 |
let op_id = tx.commit("test").block_on()?.operation().id().clone(); |
| 191 |
|
| 192 |
copy_directory(&backup_path, &op_heads_dir); |
| 193 |
// Reload the repo and check that only the new head is present. |
| 194 |
let reloaded_repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path()); |
| 195 |
assert_eq!(reloaded_repo.op_id(), &op_id); |
| 196 |
// Reload once more to make sure that the .jj/op_heads/ directory was updated |
| 197 |
// correctly. |
| 198 |
let reloaded_repo = test_env.load_repo_at_head(&settings, test_workspace.repo_path()); |
| 199 |
assert_eq!(reloaded_repo.op_id(), &op_id); |
| 200 |
Ok(()) |
| 201 |
} |