2022-11-27 02:57:50 +03:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 11:00:42 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-08-01 07:57:03 +03:00
|
|
|
use std::fs::OpenOptions;
|
|
|
|
use std::io::Write;
|
2021-03-12 04:36:55 +03:00
|
|
|
#[cfg(unix)]
|
2020-12-12 11:00:42 +03:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2022-06-17 23:24:55 +03:00
|
|
|
#[cfg(unix)]
|
|
|
|
use std::os::unix::net::UnixListener;
|
2021-04-11 19:13:00 +03:00
|
|
|
use std::sync::Arc;
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2021-06-09 23:57:48 +03:00
|
|
|
use itertools::Itertools;
|
2023-06-28 17:12:40 +03:00
|
|
|
use jj_lib::backend::{TreeId, TreeValue};
|
|
|
|
use jj_lib::conflicts::Conflict;
|
|
|
|
use jj_lib::fsmonitor::FsmonitorKind;
|
2022-06-29 02:02:14 +03:00
|
|
|
#[cfg(unix)]
|
2023-06-28 17:12:40 +03:00
|
|
|
use jj_lib::op_store::OperationId;
|
|
|
|
use jj_lib::op_store::WorkspaceId;
|
|
|
|
use jj_lib::repo::{ReadonlyRepo, Repo};
|
|
|
|
use jj_lib::repo_path::{RepoPath, RepoPathComponent, RepoPathJoin};
|
|
|
|
use jj_lib::settings::UserSettings;
|
|
|
|
use jj_lib::tree_builder::TreeBuilder;
|
|
|
|
use jj_lib::working_copy::{LockedWorkingCopy, SnapshotOptions, WorkingCopy};
|
2020-12-12 11:00:42 +03:00
|
|
|
use test_case::test_case;
|
2022-12-24 18:38:20 +03:00
|
|
|
use testutils::{write_random_commit, TestWorkspace};
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2021-09-12 09:52:38 +03:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2020-12-12 11:00:42 +03:00
|
|
|
fn test_root(use_git: bool) {
|
2021-08-11 21:18:37 +03:00
|
|
|
// Test that the working copy is clean and empty after init.
|
2020-12-12 11:00:42 +03:00
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, use_git);
|
2021-11-21 10:46:54 +03:00
|
|
|
let repo = &test_workspace.repo;
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2021-11-22 01:39:34 +03:00
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2023-07-14 23:33:59 +03:00
|
|
|
assert_eq!(wc.sparse_patterns().unwrap(), vec![RepoPath::root()]);
|
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2021-08-11 21:18:37 +03:00
|
|
|
locked_wc.discard();
|
2022-09-30 08:43:02 +03:00
|
|
|
let wc_commit_id = repo
|
2022-09-19 00:46:12 +03:00
|
|
|
.view()
|
|
|
|
.get_wc_commit_id(&WorkspaceId::default())
|
|
|
|
.unwrap();
|
2022-09-30 08:43:02 +03:00
|
|
|
let wc_commit = repo.store().get_commit(wc_commit_id).unwrap();
|
|
|
|
assert_eq!(&new_tree_id, wc_commit.tree_id());
|
2021-08-11 21:18:37 +03:00
|
|
|
assert_eq!(&new_tree_id, repo.store().empty_tree_id());
|
2020-12-12 11:00:42 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 09:52:38 +03:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2020-12-12 11:00:42 +03:00
|
|
|
fn test_checkout_file_transitions(use_git: bool) {
|
|
|
|
// Tests switching between commits where a certain path is of one type in one
|
|
|
|
// commit and another type in the other. Includes a "missing" type, so we cover
|
|
|
|
// additions and removals as well.
|
|
|
|
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, use_git);
|
2021-11-21 10:46:54 +03:00
|
|
|
let repo = &test_workspace.repo;
|
2020-12-12 11:00:42 +03:00
|
|
|
let store = repo.store().clone();
|
2021-11-24 09:45:41 +03:00
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2022-11-23 22:34:38 +03:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
2020-12-12 11:00:42 +03:00
|
|
|
enum Kind {
|
|
|
|
Missing,
|
|
|
|
Normal,
|
|
|
|
Executable,
|
2022-04-15 09:03:08 +03:00
|
|
|
// Executable, but same content as Normal, to test transition where only the bit changed
|
|
|
|
ExecutableNormalContent,
|
2021-10-29 06:55:36 +03:00
|
|
|
Conflict,
|
2021-06-14 07:38:34 +03:00
|
|
|
#[cfg_attr(windows, allow(dead_code))]
|
2020-12-12 11:00:42 +03:00
|
|
|
Symlink,
|
|
|
|
Tree,
|
|
|
|
GitSubmodule,
|
2021-02-27 10:00:46 +03:00
|
|
|
}
|
2020-12-12 11:00:42 +03:00
|
|
|
|
|
|
|
fn write_path(
|
|
|
|
settings: &UserSettings,
|
2021-04-11 19:13:00 +03:00
|
|
|
repo: &Arc<ReadonlyRepo>,
|
2020-12-12 11:00:42 +03:00
|
|
|
tree_builder: &mut TreeBuilder,
|
|
|
|
kind: Kind,
|
2022-03-31 19:32:03 +03:00
|
|
|
path: &RepoPath,
|
2020-12-12 11:00:42 +03:00
|
|
|
) {
|
|
|
|
let store = repo.store();
|
|
|
|
let value = match kind {
|
|
|
|
Kind::Missing => {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Kind::Normal => {
|
2022-03-31 19:32:03 +03:00
|
|
|
let id = testutils::write_file(store, path, "normal file contents");
|
2022-11-15 00:27:18 +03:00
|
|
|
TreeValue::File {
|
2020-12-12 11:00:42 +03:00
|
|
|
id,
|
|
|
|
executable: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Kind::Executable => {
|
2022-03-31 19:32:03 +03:00
|
|
|
let id = testutils::write_file(store, path, "executable file contents");
|
2022-11-15 00:27:18 +03:00
|
|
|
TreeValue::File {
|
2020-12-12 11:00:42 +03:00
|
|
|
id,
|
|
|
|
executable: true,
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 09:03:08 +03:00
|
|
|
Kind::ExecutableNormalContent => {
|
|
|
|
let id = testutils::write_file(store, path, "normal file contents");
|
2022-11-15 00:27:18 +03:00
|
|
|
TreeValue::File {
|
2022-04-15 09:03:08 +03:00
|
|
|
id,
|
|
|
|
executable: true,
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 06:55:36 +03:00
|
|
|
Kind::Conflict => {
|
2022-03-31 19:32:03 +03:00
|
|
|
let base_file_id = testutils::write_file(store, path, "base file contents");
|
|
|
|
let left_file_id = testutils::write_file(store, path, "left file contents");
|
|
|
|
let right_file_id = testutils::write_file(store, path, "right file contents");
|
2023-06-01 02:49:07 +03:00
|
|
|
let conflict = Conflict::new(
|
|
|
|
vec![Some(TreeValue::File {
|
|
|
|
id: base_file_id,
|
|
|
|
executable: false,
|
|
|
|
})],
|
|
|
|
vec![
|
|
|
|
Some(TreeValue::File {
|
|
|
|
id: left_file_id,
|
2021-10-29 06:55:36 +03:00
|
|
|
executable: false,
|
2023-06-01 02:49:07 +03:00
|
|
|
}),
|
|
|
|
Some(TreeValue::File {
|
|
|
|
id: right_file_id,
|
|
|
|
executable: false,
|
|
|
|
}),
|
2021-10-29 06:55:36 +03:00
|
|
|
],
|
2023-06-01 02:49:07 +03:00
|
|
|
);
|
2022-03-31 19:21:50 +03:00
|
|
|
let conflict_id = store.write_conflict(path, &conflict).unwrap();
|
2021-10-29 06:55:36 +03:00
|
|
|
TreeValue::Conflict(conflict_id)
|
|
|
|
}
|
2020-12-12 11:00:42 +03:00
|
|
|
Kind::Symlink => {
|
2022-03-31 19:32:03 +03:00
|
|
|
let id = store.write_symlink(path, "target").unwrap();
|
2020-12-12 11:00:42 +03:00
|
|
|
TreeValue::Symlink(id)
|
|
|
|
}
|
|
|
|
Kind::Tree => {
|
|
|
|
let mut sub_tree_builder = store.tree_builder(store.empty_tree_id().clone());
|
2022-03-31 19:32:03 +03:00
|
|
|
let file_path = path.join(&RepoPathComponent::from("file"));
|
2020-12-12 11:00:42 +03:00
|
|
|
write_path(
|
|
|
|
settings,
|
|
|
|
repo,
|
|
|
|
&mut sub_tree_builder,
|
|
|
|
Kind::Normal,
|
|
|
|
&file_path,
|
|
|
|
);
|
|
|
|
let id = sub_tree_builder.write_tree();
|
|
|
|
TreeValue::Tree(id)
|
|
|
|
}
|
|
|
|
Kind::GitSubmodule => {
|
2022-11-13 09:29:06 +03:00
|
|
|
let mut tx = repo.start_transaction(settings, "test");
|
2022-12-24 18:38:20 +03:00
|
|
|
let id = write_random_commit(tx.mut_repo(), settings).id().clone();
|
2021-09-11 19:45:06 +03:00
|
|
|
tx.commit();
|
2020-12-12 11:00:42 +03:00
|
|
|
TreeValue::GitSubmodule(id)
|
|
|
|
}
|
|
|
|
};
|
2022-03-31 19:32:03 +03:00
|
|
|
tree_builder.set(path.clone(), value);
|
2021-02-27 10:00:46 +03:00
|
|
|
}
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2021-10-29 06:55:36 +03:00
|
|
|
let mut kinds = vec![
|
|
|
|
Kind::Missing,
|
|
|
|
Kind::Normal,
|
|
|
|
Kind::Executable,
|
2022-04-15 09:03:08 +03:00
|
|
|
Kind::ExecutableNormalContent,
|
2021-10-29 06:55:36 +03:00
|
|
|
Kind::Conflict,
|
|
|
|
Kind::Tree,
|
|
|
|
];
|
2021-06-14 07:38:34 +03:00
|
|
|
#[cfg(unix)]
|
|
|
|
kinds.push(Kind::Symlink);
|
2020-12-12 11:00:42 +03:00
|
|
|
if use_git {
|
|
|
|
kinds.push(Kind::GitSubmodule);
|
|
|
|
}
|
|
|
|
let mut left_tree_builder = store.tree_builder(store.empty_tree_id().clone());
|
|
|
|
let mut right_tree_builder = store.tree_builder(store.empty_tree_id().clone());
|
|
|
|
let mut files = vec![];
|
|
|
|
for left_kind in &kinds {
|
|
|
|
for right_kind in &kinds {
|
2022-12-15 05:30:06 +03:00
|
|
|
let path = RepoPath::from_internal_string(&format!("{left_kind:?}_{right_kind:?}"));
|
2021-11-21 10:46:54 +03:00
|
|
|
write_path(&settings, repo, &mut left_tree_builder, *left_kind, &path);
|
|
|
|
write_path(&settings, repo, &mut right_tree_builder, *right_kind, &path);
|
2020-12-12 11:00:42 +03:00
|
|
|
files.push((*left_kind, *right_kind, path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let left_tree_id = left_tree_builder.write_tree();
|
|
|
|
let right_tree_id = right_tree_builder.write_tree();
|
2022-02-13 10:46:38 +03:00
|
|
|
let left_tree = store.get_tree(&RepoPath::root(), &left_tree_id).unwrap();
|
|
|
|
let right_tree = store.get_tree(&RepoPath::root(), &right_tree_id).unwrap();
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2021-11-22 01:39:34 +03:00
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2022-02-13 10:46:38 +03:00
|
|
|
wc.check_out(repo.op_id().clone(), None, &left_tree)
|
2021-11-27 09:33:36 +03:00
|
|
|
.unwrap();
|
2022-02-13 10:46:38 +03:00
|
|
|
wc.check_out(repo.op_id().clone(), None, &right_tree)
|
2021-11-27 09:33:36 +03:00
|
|
|
.unwrap();
|
2020-12-12 11:00:42 +03:00
|
|
|
|
|
|
|
// Check that the working copy is clean.
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2021-08-11 21:18:37 +03:00
|
|
|
locked_wc.discard();
|
2022-02-13 10:46:38 +03:00
|
|
|
assert_eq!(new_tree_id, right_tree_id);
|
2020-12-12 11:00:42 +03:00
|
|
|
|
|
|
|
for (_left_kind, right_kind, path) in &files {
|
2022-03-31 19:32:03 +03:00
|
|
|
let wc_path = workspace_root.join(path.to_internal_file_string());
|
2020-12-12 11:00:42 +03:00
|
|
|
let maybe_metadata = wc_path.symlink_metadata();
|
|
|
|
match right_kind {
|
|
|
|
Kind::Missing => {
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(maybe_metadata.is_err(), "{path:?} should not exist");
|
2020-12-12 11:00:42 +03:00
|
|
|
}
|
|
|
|
Kind::Normal => {
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(maybe_metadata.is_ok(), "{path:?} should exist");
|
2020-12-12 11:00:42 +03:00
|
|
|
let metadata = maybe_metadata.unwrap();
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(metadata.is_file(), "{path:?} should be a file");
|
2021-03-11 09:13:52 +03:00
|
|
|
#[cfg(unix)]
|
2020-12-12 11:00:42 +03:00
|
|
|
assert_eq!(
|
|
|
|
metadata.permissions().mode() & 0o111,
|
|
|
|
0,
|
2022-12-15 05:30:06 +03:00
|
|
|
"{path:?} should not be executable"
|
2020-12-12 11:00:42 +03:00
|
|
|
);
|
|
|
|
}
|
2022-04-15 09:03:08 +03:00
|
|
|
Kind::Executable | Kind::ExecutableNormalContent => {
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(maybe_metadata.is_ok(), "{path:?} should exist");
|
2020-12-12 11:00:42 +03:00
|
|
|
let metadata = maybe_metadata.unwrap();
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(metadata.is_file(), "{path:?} should be a file");
|
2021-03-11 09:13:52 +03:00
|
|
|
#[cfg(unix)]
|
2020-12-12 11:00:42 +03:00
|
|
|
assert_ne!(
|
|
|
|
metadata.permissions().mode() & 0o111,
|
|
|
|
0,
|
2022-12-15 05:30:06 +03:00
|
|
|
"{path:?} should be executable"
|
2020-12-12 11:00:42 +03:00
|
|
|
);
|
|
|
|
}
|
2021-10-29 06:55:36 +03:00
|
|
|
Kind::Conflict => {
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(maybe_metadata.is_ok(), "{path:?} should exist");
|
2021-10-29 06:55:36 +03:00
|
|
|
let metadata = maybe_metadata.unwrap();
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(metadata.is_file(), "{path:?} should be a file");
|
2021-10-29 06:55:36 +03:00
|
|
|
#[cfg(unix)]
|
|
|
|
assert_eq!(
|
|
|
|
metadata.permissions().mode() & 0o111,
|
|
|
|
0,
|
2022-12-15 05:30:06 +03:00
|
|
|
"{path:?} should not be executable"
|
2021-10-29 06:55:36 +03:00
|
|
|
);
|
|
|
|
}
|
2020-12-12 11:00:42 +03:00
|
|
|
Kind::Symlink => {
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(maybe_metadata.is_ok(), "{path:?} should exist");
|
2020-12-12 11:00:42 +03:00
|
|
|
let metadata = maybe_metadata.unwrap();
|
2021-04-28 18:57:30 +03:00
|
|
|
assert!(
|
2020-12-12 11:00:42 +03:00
|
|
|
metadata.file_type().is_symlink(),
|
2022-12-15 05:30:06 +03:00
|
|
|
"{path:?} should be a symlink"
|
2020-12-12 11:00:42 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Kind::Tree => {
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(maybe_metadata.is_ok(), "{path:?} should exist");
|
2020-12-12 11:00:42 +03:00
|
|
|
let metadata = maybe_metadata.unwrap();
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(metadata.is_dir(), "{path:?} should be a directory");
|
2020-12-12 11:00:42 +03:00
|
|
|
}
|
|
|
|
Kind::GitSubmodule => {
|
|
|
|
// Not supported for now
|
2022-12-15 05:30:06 +03:00
|
|
|
assert!(maybe_metadata.is_err(), "{path:?} should not exist");
|
2020-12-12 11:00:42 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 15:00:24 +03:00
|
|
|
|
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
|
|
|
fn test_tree_builder_file_directory_transition(use_git: bool) {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let test_workspace = TestWorkspace::init(&settings, use_git);
|
|
|
|
let repo = &test_workspace.repo;
|
|
|
|
let store = repo.store();
|
|
|
|
let mut workspace = test_workspace.workspace;
|
|
|
|
let workspace_root = workspace.workspace_root().clone();
|
|
|
|
let mut check_out_tree = |tree_id: &TreeId| {
|
|
|
|
let tree = repo.store().get_tree(&RepoPath::root(), tree_id).unwrap();
|
|
|
|
let wc = workspace.working_copy_mut();
|
|
|
|
wc.check_out(repo.op_id().clone(), None, &tree).unwrap();
|
|
|
|
};
|
|
|
|
|
|
|
|
let parent_path = RepoPath::from_internal_string("foo/bar");
|
|
|
|
let child_path = RepoPath::from_internal_string("foo/bar/baz");
|
|
|
|
|
|
|
|
// Add file at parent_path
|
|
|
|
let mut tree_builder = store.tree_builder(store.empty_tree_id().clone());
|
|
|
|
testutils::write_normal_file(&mut tree_builder, &parent_path, "");
|
|
|
|
let tree_id = tree_builder.write_tree();
|
|
|
|
check_out_tree(&tree_id);
|
|
|
|
assert!(parent_path.to_fs_path(&workspace_root).is_file());
|
|
|
|
assert!(!child_path.to_fs_path(&workspace_root).exists());
|
|
|
|
|
|
|
|
// Turn parent_path into directory, add file at child_path
|
|
|
|
let mut tree_builder = store.tree_builder(tree_id);
|
|
|
|
tree_builder.remove(parent_path.clone());
|
|
|
|
testutils::write_normal_file(&mut tree_builder, &child_path, "");
|
|
|
|
let tree_id = tree_builder.write_tree();
|
|
|
|
check_out_tree(&tree_id);
|
|
|
|
assert!(parent_path.to_fs_path(&workspace_root).is_dir());
|
|
|
|
assert!(child_path.to_fs_path(&workspace_root).is_file());
|
|
|
|
|
|
|
|
// Turn parent_path back to file
|
|
|
|
let mut tree_builder = store.tree_builder(tree_id);
|
|
|
|
tree_builder.remove(child_path.clone());
|
|
|
|
testutils::write_normal_file(&mut tree_builder, &parent_path, "");
|
|
|
|
let tree_id = tree_builder.write_tree();
|
|
|
|
check_out_tree(&tree_id);
|
2023-05-31 16:30:20 +03:00
|
|
|
assert!(parent_path.to_fs_path(&workspace_root).is_file());
|
2023-05-31 15:00:24 +03:00
|
|
|
assert!(!child_path.to_fs_path(&workspace_root).exists());
|
|
|
|
}
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2022-01-18 02:20:59 +03:00
|
|
|
#[test]
|
|
|
|
fn test_reset() {
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, false);
|
2022-01-18 02:20:59 +03:00
|
|
|
let repo = &test_workspace.repo;
|
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
|
|
|
|
|
|
|
let ignored_path = RepoPath::from_internal_string("ignored");
|
|
|
|
let gitignore_path = RepoPath::from_internal_string(".gitignore");
|
|
|
|
|
|
|
|
let tree_without_file = testutils::create_tree(repo, &[(&gitignore_path, "ignored\n")]);
|
|
|
|
let tree_with_file = testutils::create_tree(
|
|
|
|
repo,
|
|
|
|
&[(&gitignore_path, "ignored\n"), (&ignored_path, "code")],
|
|
|
|
);
|
|
|
|
|
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2022-02-13 10:46:38 +03:00
|
|
|
wc.check_out(repo.op_id().clone(), None, &tree_with_file)
|
2021-11-27 09:33:36 +03:00
|
|
|
.unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
|
|
|
|
// Test the setup: the file should exist on disk and in the tree state.
|
|
|
|
assert!(ignored_path.to_fs_path(&workspace_root).is_file());
|
2023-07-14 23:33:59 +03:00
|
|
|
assert!(wc.file_states().unwrap().contains_key(&ignored_path));
|
2022-01-18 02:20:59 +03:00
|
|
|
|
|
|
|
// After we reset to the commit without the file, it should still exist on disk,
|
|
|
|
// but it should not be in the tree state, and it should not get added when we
|
|
|
|
// commit the working copy (because it's ignored).
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
locked_wc.reset(&tree_without_file).unwrap();
|
2023-07-14 22:45:00 +03:00
|
|
|
locked_wc.finish(repo.op_id().clone()).unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
assert!(ignored_path.to_fs_path(&workspace_root).is_file());
|
2023-07-14 23:33:59 +03:00
|
|
|
assert!(!wc.file_states().unwrap().contains_key(&ignored_path));
|
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
assert_eq!(new_tree_id, *tree_without_file.id());
|
|
|
|
locked_wc.discard();
|
|
|
|
|
|
|
|
// After we reset to the commit without the file, it should still exist on disk,
|
|
|
|
// but it should not be in the tree state, and it should not get added when we
|
|
|
|
// commit the working copy (because it's ignored).
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
locked_wc.reset(&tree_without_file).unwrap();
|
2023-07-14 22:45:00 +03:00
|
|
|
locked_wc.finish(repo.op_id().clone()).unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
assert!(ignored_path.to_fs_path(&workspace_root).is_file());
|
2023-07-14 23:33:59 +03:00
|
|
|
assert!(!wc.file_states().unwrap().contains_key(&ignored_path));
|
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
assert_eq!(new_tree_id, *tree_without_file.id());
|
|
|
|
locked_wc.discard();
|
|
|
|
|
|
|
|
// Now test the opposite direction: resetting to a commit where the file is
|
|
|
|
// tracked. The file should become tracked (even though it's ignored).
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
locked_wc.reset(&tree_with_file).unwrap();
|
2023-07-14 22:45:00 +03:00
|
|
|
locked_wc.finish(repo.op_id().clone()).unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
assert!(ignored_path.to_fs_path(&workspace_root).is_file());
|
2023-07-14 23:33:59 +03:00
|
|
|
assert!(wc.file_states().unwrap().contains_key(&ignored_path));
|
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2022-01-18 02:20:59 +03:00
|
|
|
assert_eq!(new_tree_id, *tree_with_file.id());
|
|
|
|
locked_wc.discard();
|
|
|
|
}
|
|
|
|
|
2022-02-07 09:18:17 +03:00
|
|
|
#[test]
|
|
|
|
fn test_checkout_discard() {
|
|
|
|
// Start a mutation, do a checkout, and then discard the mutation. The working
|
|
|
|
// copy files should remain changed, but the state files should not be
|
|
|
|
// written.
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, false);
|
2022-02-07 09:18:17 +03:00
|
|
|
let repo = test_workspace.repo.clone();
|
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
|
|
|
|
|
|
|
let file1_path = RepoPath::from_internal_string("file1");
|
|
|
|
let file2_path = RepoPath::from_internal_string("file2");
|
|
|
|
|
|
|
|
let store = repo.store();
|
|
|
|
let tree1 = testutils::create_tree(&repo, &[(&file1_path, "contents")]);
|
|
|
|
let tree2 = testutils::create_tree(&repo, &[(&file2_path, "contents")]);
|
|
|
|
|
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
|
|
|
let state_path = wc.state_path().to_path_buf();
|
2022-02-13 10:46:38 +03:00
|
|
|
wc.check_out(repo.op_id().clone(), None, &tree1).unwrap();
|
2022-02-07 09:18:17 +03:00
|
|
|
|
|
|
|
// Test the setup: the file should exist on disk and in the tree state.
|
|
|
|
assert!(file1_path.to_fs_path(&workspace_root).is_file());
|
2023-07-14 23:33:59 +03:00
|
|
|
assert!(wc.file_states().unwrap().contains_key(&file1_path));
|
2022-02-07 09:18:17 +03:00
|
|
|
|
|
|
|
// Start a checkout
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2022-02-13 22:54:48 +03:00
|
|
|
locked_wc.check_out(&tree2).unwrap();
|
2022-02-07 09:18:17 +03:00
|
|
|
// The change should be reflected in the working copy but not saved
|
|
|
|
assert!(!file1_path.to_fs_path(&workspace_root).is_file());
|
|
|
|
assert!(file2_path.to_fs_path(&workspace_root).is_file());
|
|
|
|
let reloaded_wc = WorkingCopy::load(store.clone(), workspace_root.clone(), state_path.clone());
|
2023-07-14 23:33:59 +03:00
|
|
|
assert!(reloaded_wc.file_states().unwrap().contains_key(&file1_path));
|
|
|
|
assert!(!reloaded_wc.file_states().unwrap().contains_key(&file2_path));
|
2022-02-07 09:18:17 +03:00
|
|
|
locked_wc.discard();
|
|
|
|
|
|
|
|
// The change should remain in the working copy, but not in memory and not saved
|
2023-07-14 23:33:59 +03:00
|
|
|
assert!(wc.file_states().unwrap().contains_key(&file1_path));
|
|
|
|
assert!(!wc.file_states().unwrap().contains_key(&file2_path));
|
2022-02-07 09:18:17 +03:00
|
|
|
assert!(!file1_path.to_fs_path(&workspace_root).is_file());
|
|
|
|
assert!(file2_path.to_fs_path(&workspace_root).is_file());
|
|
|
|
let reloaded_wc = WorkingCopy::load(store.clone(), workspace_root, state_path);
|
2023-07-14 23:33:59 +03:00
|
|
|
assert!(reloaded_wc.file_states().unwrap().contains_key(&file1_path));
|
|
|
|
assert!(!reloaded_wc.file_states().unwrap().contains_key(&file2_path));
|
2022-02-07 09:18:17 +03:00
|
|
|
}
|
|
|
|
|
2021-09-12 09:52:38 +03:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2022-06-17 23:24:55 +03:00
|
|
|
fn test_snapshot_racy_timestamps(use_git: bool) {
|
2020-12-12 11:00:42 +03:00
|
|
|
// Tests that file modifications are detected even if they happen the same
|
|
|
|
// millisecond as the updated working copy state.
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, use_git);
|
2021-11-21 10:46:54 +03:00
|
|
|
let repo = &test_workspace.repo;
|
2021-11-24 09:45:41 +03:00
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
2020-12-12 11:00:42 +03:00
|
|
|
|
2021-11-24 09:45:41 +03:00
|
|
|
let file_path = workspace_root.join("file");
|
2020-12-12 11:00:42 +03:00
|
|
|
let mut previous_tree_id = repo.store().empty_tree_id().clone();
|
2021-11-22 01:39:34 +03:00
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2020-12-12 11:00:42 +03:00
|
|
|
for i in 0..100 {
|
|
|
|
{
|
2022-12-15 22:59:10 +03:00
|
|
|
// https://github.com/rust-lang/rust-clippy/issues/9778
|
|
|
|
#[allow(clippy::needless_borrow)]
|
2020-12-12 11:00:42 +03:00
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.write(true)
|
|
|
|
.open(&file_path)
|
|
|
|
.unwrap();
|
2022-12-15 05:30:06 +03:00
|
|
|
file.write_all(format!("contents {i}").as_bytes()).unwrap();
|
2020-12-12 11:00:42 +03:00
|
|
|
}
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2021-08-11 21:18:37 +03:00
|
|
|
locked_wc.discard();
|
2020-12-12 11:00:42 +03:00
|
|
|
assert_ne!(new_tree_id, previous_tree_id);
|
|
|
|
previous_tree_id = new_tree_id;
|
|
|
|
}
|
|
|
|
}
|
2020-12-19 09:50:01 +03:00
|
|
|
|
2022-06-17 23:24:55 +03:00
|
|
|
#[cfg(unix)]
|
|
|
|
#[test]
|
|
|
|
fn test_snapshot_special_file() {
|
|
|
|
// Tests that we ignore when special files (such as sockets and pipes) exist on
|
|
|
|
// disk.
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, false);
|
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
|
|
|
let store = test_workspace.repo.store();
|
|
|
|
|
|
|
|
let file1_path = RepoPath::from_internal_string("file1");
|
|
|
|
let file1_disk_path = file1_path.to_fs_path(&workspace_root);
|
|
|
|
std::fs::write(&file1_disk_path, "contents".as_bytes()).unwrap();
|
|
|
|
let file2_path = RepoPath::from_internal_string("file2");
|
|
|
|
let file2_disk_path = file2_path.to_fs_path(&workspace_root);
|
2022-11-24 21:39:22 +03:00
|
|
|
std::fs::write(file2_disk_path, "contents".as_bytes()).unwrap();
|
2022-06-17 23:24:55 +03:00
|
|
|
let socket_disk_path = workspace_root.join("socket");
|
|
|
|
UnixListener::bind(&socket_disk_path).unwrap();
|
|
|
|
// Test the setup
|
|
|
|
assert!(socket_disk_path.exists());
|
|
|
|
assert!(!socket_disk_path.is_file());
|
|
|
|
|
|
|
|
// Snapshot the working copy with the socket file
|
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2023-07-14 22:45:00 +03:00
|
|
|
locked_wc.finish(OperationId::from_hex("abc123")).unwrap();
|
2022-06-17 23:24:55 +03:00
|
|
|
let tree = store.get_tree(&RepoPath::root(), &tree_id).unwrap();
|
|
|
|
// Only the regular files should be in the tree
|
|
|
|
assert_eq!(
|
|
|
|
tree.entries().map(|(path, _value)| path).collect_vec(),
|
|
|
|
vec![file1_path.clone(), file2_path.clone()]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-07-14 23:33:59 +03:00
|
|
|
wc.file_states().unwrap().keys().cloned().collect_vec(),
|
2022-06-17 23:24:55 +03:00
|
|
|
vec![file1_path, file2_path.clone()]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Replace a regular file by a socket and snapshot the working copy again
|
|
|
|
std::fs::remove_file(&file1_disk_path).unwrap();
|
|
|
|
UnixListener::bind(&file1_disk_path).unwrap();
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2023-07-14 22:45:00 +03:00
|
|
|
locked_wc.finish(OperationId::from_hex("abc123")).unwrap();
|
2022-06-17 23:24:55 +03:00
|
|
|
let tree = store.get_tree(&RepoPath::root(), &tree_id).unwrap();
|
|
|
|
// Only the regular file should be in the tree
|
|
|
|
assert_eq!(
|
|
|
|
tree.entries().map(|(path, _value)| path).collect_vec(),
|
|
|
|
vec![file2_path.clone()]
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2023-07-14 23:33:59 +03:00
|
|
|
wc.file_states().unwrap().keys().cloned().collect_vec(),
|
2022-06-17 23:24:55 +03:00
|
|
|
vec![file2_path]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-12 09:52:38 +03:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2020-12-19 09:50:01 +03:00
|
|
|
fn test_gitignores(use_git: bool) {
|
|
|
|
// Tests that .gitignore files are respected.
|
|
|
|
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, use_git);
|
2021-11-21 10:46:54 +03:00
|
|
|
let repo = &test_workspace.repo;
|
2021-11-22 01:39:34 +03:00
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
2020-12-19 09:50:01 +03:00
|
|
|
|
2021-05-17 08:47:31 +03:00
|
|
|
let gitignore_path = RepoPath::from_internal_string(".gitignore");
|
|
|
|
let added_path = RepoPath::from_internal_string("added");
|
|
|
|
let modified_path = RepoPath::from_internal_string("modified");
|
|
|
|
let removed_path = RepoPath::from_internal_string("removed");
|
|
|
|
let ignored_path = RepoPath::from_internal_string("ignored");
|
|
|
|
let subdir_modified_path = RepoPath::from_internal_string("dir/modified");
|
|
|
|
let subdir_ignored_path = RepoPath::from_internal_string("dir/ignored");
|
2020-12-19 09:50:01 +03:00
|
|
|
|
2021-11-22 01:39:34 +03:00
|
|
|
testutils::write_working_copy_file(&workspace_root, &gitignore_path, "ignored\n");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &modified_path, "1");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &removed_path, "1");
|
2021-11-22 01:31:44 +03:00
|
|
|
std::fs::create_dir(workspace_root.join("dir")).unwrap();
|
2021-11-22 01:39:34 +03:00
|
|
|
testutils::write_working_copy_file(&workspace_root, &subdir_modified_path, "1");
|
2020-12-19 09:50:01 +03:00
|
|
|
|
2021-11-22 01:39:34 +03:00
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id1 = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2023-07-14 22:45:00 +03:00
|
|
|
locked_wc.finish(repo.op_id().clone()).unwrap();
|
2021-08-11 21:18:37 +03:00
|
|
|
let tree1 = repo
|
|
|
|
.store()
|
|
|
|
.get_tree(&RepoPath::root(), &new_tree_id1)
|
|
|
|
.unwrap();
|
|
|
|
let files1 = tree1.entries().map(|(name, _value)| name).collect_vec();
|
2020-12-19 09:50:01 +03:00
|
|
|
assert_eq!(
|
|
|
|
files1,
|
|
|
|
vec![
|
2021-05-17 07:55:51 +03:00
|
|
|
gitignore_path.clone(),
|
|
|
|
subdir_modified_path.clone(),
|
|
|
|
modified_path.clone(),
|
|
|
|
removed_path.clone(),
|
2020-12-19 09:50:01 +03:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2021-11-22 01:31:44 +03:00
|
|
|
testutils::write_working_copy_file(
|
2021-11-22 01:39:34 +03:00
|
|
|
&workspace_root,
|
2021-11-22 01:31:44 +03:00
|
|
|
&gitignore_path,
|
|
|
|
"ignored\nmodified\nremoved\n",
|
|
|
|
);
|
2021-11-22 01:39:34 +03:00
|
|
|
testutils::write_working_copy_file(&workspace_root, &added_path, "2");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &modified_path, "2");
|
|
|
|
std::fs::remove_file(removed_path.to_fs_path(&workspace_root)).unwrap();
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &ignored_path, "2");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &subdir_modified_path, "2");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &subdir_ignored_path, "2");
|
2020-12-19 09:50:01 +03:00
|
|
|
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id2 = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2021-08-11 21:18:37 +03:00
|
|
|
locked_wc.discard();
|
|
|
|
let tree2 = repo
|
|
|
|
.store()
|
|
|
|
.get_tree(&RepoPath::root(), &new_tree_id2)
|
|
|
|
.unwrap();
|
|
|
|
let files2 = tree2.entries().map(|(name, _value)| name).collect_vec();
|
2020-12-19 09:50:01 +03:00
|
|
|
assert_eq!(
|
|
|
|
files2,
|
|
|
|
vec![
|
2021-06-14 10:18:38 +03:00
|
|
|
gitignore_path,
|
|
|
|
added_path,
|
|
|
|
subdir_modified_path,
|
|
|
|
modified_path,
|
2020-12-19 09:50:01 +03:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2021-05-15 09:22:42 +03:00
|
|
|
|
2021-09-12 09:52:38 +03:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2022-08-01 08:56:08 +03:00
|
|
|
fn test_gitignores_checkout_never_overwrites_ignored(use_git: bool) {
|
|
|
|
// Tests that a .gitignore'd file doesn't get overwritten if check out a commit
|
|
|
|
// where the file is tracked.
|
2021-05-15 09:22:42 +03:00
|
|
|
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, use_git);
|
2021-11-21 10:46:54 +03:00
|
|
|
let repo = &test_workspace.repo;
|
2021-11-24 09:45:41 +03:00
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
2021-05-15 09:22:42 +03:00
|
|
|
|
|
|
|
// Write an ignored file called "modified" to disk
|
2021-05-17 08:47:31 +03:00
|
|
|
let gitignore_path = RepoPath::from_internal_string(".gitignore");
|
2021-11-24 09:45:41 +03:00
|
|
|
testutils::write_working_copy_file(&workspace_root, &gitignore_path, "modified\n");
|
2021-05-17 08:47:31 +03:00
|
|
|
let modified_path = RepoPath::from_internal_string("modified");
|
2021-11-24 09:45:41 +03:00
|
|
|
testutils::write_working_copy_file(&workspace_root, &modified_path, "garbage");
|
2021-05-15 09:22:42 +03:00
|
|
|
|
2022-02-13 10:46:38 +03:00
|
|
|
// Create a tree that adds the same file but with different contents
|
2021-05-15 09:22:42 +03:00
|
|
|
let mut tree_builder = repo
|
|
|
|
.store()
|
|
|
|
.tree_builder(repo.store().empty_tree_id().clone());
|
|
|
|
testutils::write_normal_file(&mut tree_builder, &modified_path, "contents");
|
|
|
|
let tree_id = tree_builder.write_tree();
|
2022-02-13 10:46:38 +03:00
|
|
|
let tree = repo.store().get_tree(&RepoPath::root(), &tree_id).unwrap();
|
2021-05-15 09:22:42 +03:00
|
|
|
|
2022-02-13 10:46:38 +03:00
|
|
|
// Now check out the tree that adds the file "modified" with contents
|
2022-08-01 08:56:08 +03:00
|
|
|
// "contents". The exiting contents ("garbage") shouldn't be replaced in the
|
2021-05-15 09:22:42 +03:00
|
|
|
// working copy.
|
2021-11-22 01:39:34 +03:00
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2022-08-01 08:56:08 +03:00
|
|
|
assert!(wc.check_out(repo.op_id().clone(), None, &tree).is_err());
|
2021-05-15 09:22:42 +03:00
|
|
|
|
2022-08-01 08:56:08 +03:00
|
|
|
// Check that the old contents are in the working copy
|
2021-11-24 09:45:41 +03:00
|
|
|
let path = workspace_root.join("modified");
|
2021-05-15 09:22:42 +03:00
|
|
|
assert!(path.is_file());
|
2022-08-01 08:56:08 +03:00
|
|
|
assert_eq!(std::fs::read(&path).unwrap(), b"garbage");
|
2021-05-15 09:22:42 +03:00
|
|
|
}
|
2021-05-15 18:27:28 +03:00
|
|
|
|
2021-09-12 09:52:38 +03:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2021-05-15 18:27:28 +03:00
|
|
|
fn test_gitignores_ignored_directory_already_tracked(use_git: bool) {
|
|
|
|
// Tests that a .gitignore'd directory that already has a tracked file in it
|
2022-06-17 23:24:55 +03:00
|
|
|
// does not get removed when snapshotting the working directory.
|
2021-05-15 18:27:28 +03:00
|
|
|
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, use_git);
|
2021-11-21 10:46:54 +03:00
|
|
|
let repo = &test_workspace.repo;
|
2021-05-15 18:27:28 +03:00
|
|
|
|
|
|
|
// Add a .gitignore file saying to ignore the directory "ignored/"
|
2021-05-17 08:47:31 +03:00
|
|
|
let gitignore_path = RepoPath::from_internal_string(".gitignore");
|
2021-11-22 01:31:44 +03:00
|
|
|
testutils::write_working_copy_file(
|
|
|
|
test_workspace.workspace.workspace_root(),
|
|
|
|
&gitignore_path,
|
|
|
|
"/ignored/\n",
|
|
|
|
);
|
2021-05-17 08:47:31 +03:00
|
|
|
let file_path = RepoPath::from_internal_string("ignored/file");
|
2021-05-15 18:27:28 +03:00
|
|
|
|
2022-02-13 10:46:38 +03:00
|
|
|
// Create a tree that adds a file in the ignored directory
|
2021-05-15 18:27:28 +03:00
|
|
|
let mut tree_builder = repo
|
|
|
|
.store()
|
|
|
|
.tree_builder(repo.store().empty_tree_id().clone());
|
|
|
|
testutils::write_normal_file(&mut tree_builder, &file_path, "contents");
|
|
|
|
let tree_id = tree_builder.write_tree();
|
2022-02-13 10:46:38 +03:00
|
|
|
let tree = repo.store().get_tree(&RepoPath::root(), &tree_id).unwrap();
|
2021-05-15 18:27:28 +03:00
|
|
|
|
2022-02-13 10:46:38 +03:00
|
|
|
// Check out the tree with the file in ignored/
|
2021-11-22 01:39:34 +03:00
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2022-02-13 10:46:38 +03:00
|
|
|
wc.check_out(repo.op_id().clone(), None, &tree).unwrap();
|
2021-05-15 18:27:28 +03:00
|
|
|
|
2022-06-17 23:24:55 +03:00
|
|
|
// Check that the file is still in the tree created by snapshotting the working
|
2021-05-15 18:27:28 +03:00
|
|
|
// copy (that it didn't get removed because the directory is ignored)
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2021-08-11 21:18:37 +03:00
|
|
|
locked_wc.discard();
|
|
|
|
let new_tree = repo
|
|
|
|
.store()
|
|
|
|
.get_tree(&RepoPath::root(), &new_tree_id)
|
|
|
|
.unwrap();
|
|
|
|
assert!(new_tree.path_value(&file_path).is_some());
|
2021-05-15 18:27:28 +03:00
|
|
|
}
|
2021-08-31 19:38:28 +03:00
|
|
|
|
2021-09-12 09:52:38 +03:00
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
2021-08-31 19:38:28 +03:00
|
|
|
fn test_dotgit_ignored(use_git: bool) {
|
|
|
|
// Tests that .git directories and files are always ignored (we could accept
|
|
|
|
// them if the backend is not git).
|
|
|
|
|
|
|
|
let settings = testutils::user_settings();
|
2022-05-21 20:55:51 +03:00
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, use_git);
|
2021-11-21 10:46:54 +03:00
|
|
|
let repo = &test_workspace.repo;
|
2021-11-22 01:39:34 +03:00
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
2021-08-31 19:38:28 +03:00
|
|
|
|
|
|
|
// Test with a .git/ directory (with a file in, since we don't write empty
|
|
|
|
// trees)
|
2021-11-24 09:45:41 +03:00
|
|
|
let dotgit_path = workspace_root.join(".git");
|
2021-08-31 19:38:28 +03:00
|
|
|
std::fs::create_dir(&dotgit_path).unwrap();
|
|
|
|
testutils::write_working_copy_file(
|
2021-11-22 01:39:34 +03:00
|
|
|
&workspace_root,
|
2021-08-31 19:38:28 +03:00
|
|
|
&RepoPath::from_internal_string(".git/file"),
|
|
|
|
"contents",
|
|
|
|
);
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = test_workspace
|
|
|
|
.workspace
|
|
|
|
.working_copy_mut()
|
|
|
|
.start_mutation()
|
|
|
|
.unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2021-08-31 19:38:28 +03:00
|
|
|
assert_eq!(new_tree_id, *repo.store().empty_tree_id());
|
2022-01-16 20:48:30 +03:00
|
|
|
locked_wc.discard();
|
2021-08-31 19:38:28 +03:00
|
|
|
std::fs::remove_dir_all(&dotgit_path).unwrap();
|
|
|
|
|
|
|
|
// Test with a .git file
|
2021-11-22 01:31:44 +03:00
|
|
|
testutils::write_working_copy_file(
|
2021-11-22 01:39:34 +03:00
|
|
|
&workspace_root,
|
2021-11-22 01:31:44 +03:00
|
|
|
&RepoPath::from_internal_string(".git"),
|
|
|
|
"contents",
|
|
|
|
);
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = test_workspace
|
|
|
|
.workspace
|
|
|
|
.working_copy_mut()
|
|
|
|
.start_mutation()
|
|
|
|
.unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2021-08-31 19:38:28 +03:00
|
|
|
assert_eq!(new_tree_id, *repo.store().empty_tree_id());
|
2022-01-16 20:48:30 +03:00
|
|
|
locked_wc.discard();
|
2021-08-31 19:38:28 +03:00
|
|
|
}
|
2022-08-16 08:47:02 +03:00
|
|
|
|
2022-11-23 22:34:38 +03:00
|
|
|
#[test]
|
|
|
|
fn test_gitsubmodule() {
|
|
|
|
// Tests that git submodules are ignored.
|
|
|
|
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, true);
|
|
|
|
let repo = &test_workspace.repo;
|
|
|
|
let store = repo.store().clone();
|
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
|
|
|
|
|
|
|
let mut tree_builder = store.tree_builder(store.empty_tree_id().clone());
|
|
|
|
|
|
|
|
let added_path = RepoPath::from_internal_string("added");
|
|
|
|
let submodule_path = RepoPath::from_internal_string("submodule");
|
|
|
|
let added_submodule_path = RepoPath::from_internal_string("submodule/added");
|
|
|
|
|
|
|
|
tree_builder.set(
|
|
|
|
added_path.clone(),
|
|
|
|
TreeValue::File {
|
|
|
|
id: testutils::write_file(repo.store(), &added_path, "added\n"),
|
|
|
|
executable: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut tx = repo.start_transaction(&settings, "create submodule commit");
|
2022-12-24 18:38:20 +03:00
|
|
|
let submodule_id = write_random_commit(tx.mut_repo(), &settings).id().clone();
|
2022-11-23 22:34:38 +03:00
|
|
|
tx.commit();
|
|
|
|
|
|
|
|
tree_builder.set(
|
|
|
|
submodule_path.clone(),
|
|
|
|
TreeValue::GitSubmodule(submodule_id),
|
|
|
|
);
|
|
|
|
|
|
|
|
let tree_id = tree_builder.write_tree();
|
|
|
|
let tree = store.get_tree(&RepoPath::root(), &tree_id).unwrap();
|
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
|
|
|
wc.check_out(repo.op_id().clone(), None, &tree).unwrap();
|
|
|
|
|
|
|
|
std::fs::create_dir(submodule_path.to_fs_path(&workspace_root)).unwrap();
|
|
|
|
|
|
|
|
testutils::write_working_copy_file(
|
|
|
|
&workspace_root,
|
|
|
|
&added_submodule_path,
|
|
|
|
"i am a file in a submodule\n",
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check that the files present in the submodule are not tracked
|
|
|
|
// when we snapshot
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-06-28 02:10:35 +03:00
|
|
|
let new_tree_id = locked_wc
|
|
|
|
.snapshot(SnapshotOptions::empty_for_test())
|
|
|
|
.unwrap();
|
2022-11-23 22:34:38 +03:00
|
|
|
locked_wc.discard();
|
|
|
|
assert_eq!(new_tree_id, tree_id);
|
|
|
|
|
|
|
|
// Check that the files in the submodule are not deleted
|
|
|
|
let file_in_submodule_path = added_submodule_path.to_fs_path(&workspace_root);
|
|
|
|
assert!(
|
|
|
|
file_in_submodule_path.metadata().is_ok(),
|
2022-12-15 05:30:06 +03:00
|
|
|
"{file_in_submodule_path:?} should exist"
|
2022-11-23 22:34:38 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-16 08:47:02 +03:00
|
|
|
#[cfg(unix)]
|
|
|
|
#[test_case(false ; "local backend")]
|
|
|
|
#[test_case(true ; "git backend")]
|
|
|
|
fn test_existing_directory_symlink(use_git: bool) {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, use_git);
|
|
|
|
let repo = &test_workspace.repo;
|
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
|
|
|
|
|
|
|
// Creates a symlink in working directory, and a tree that will add a file under
|
|
|
|
// the symlinked directory.
|
|
|
|
std::os::unix::fs::symlink("..", workspace_root.join("parent")).unwrap();
|
|
|
|
let mut tree_builder = repo
|
|
|
|
.store()
|
|
|
|
.tree_builder(repo.store().empty_tree_id().clone());
|
|
|
|
testutils::write_normal_file(
|
|
|
|
&mut tree_builder,
|
|
|
|
&RepoPath::from_internal_string("parent/escaped"),
|
|
|
|
"contents",
|
|
|
|
);
|
|
|
|
let tree_id = tree_builder.write_tree();
|
|
|
|
let tree = repo.store().get_tree(&RepoPath::root(), &tree_id).unwrap();
|
|
|
|
|
|
|
|
// Checkout should fail because "parent" already exists and is a symlink.
|
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
|
|
|
assert!(wc.check_out(repo.op_id().clone(), None, &tree).is_err());
|
|
|
|
|
|
|
|
// Therefore, "../escaped" shouldn't be created.
|
|
|
|
assert!(!workspace_root.parent().unwrap().join("escaped").exists());
|
|
|
|
}
|
2023-07-08 12:23:32 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fsmonitor() {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let mut test_workspace = TestWorkspace::init(&settings, true);
|
|
|
|
let repo = &test_workspace.repo;
|
|
|
|
let workspace_root = test_workspace.workspace.workspace_root().clone();
|
|
|
|
|
|
|
|
let wc = test_workspace.workspace.working_copy_mut();
|
2023-07-14 23:33:59 +03:00
|
|
|
assert_eq!(wc.sparse_patterns().unwrap(), vec![RepoPath::root()]);
|
2023-07-08 12:23:32 +03:00
|
|
|
|
|
|
|
let foo_path = RepoPath::from_internal_string("foo");
|
|
|
|
let bar_path = RepoPath::from_internal_string("bar");
|
|
|
|
let nested_path = RepoPath::from_internal_string("path/to/nested");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &foo_path, "foo\n");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &bar_path, "bar\n");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &nested_path, "nested\n");
|
|
|
|
|
|
|
|
let ignored_path = RepoPath::from_internal_string("path/to/ignored");
|
|
|
|
let gitignore_path = RepoPath::from_internal_string("path/.gitignore");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &ignored_path, "ignored\n");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &gitignore_path, "to/ignored\n");
|
|
|
|
|
|
|
|
let snapshot = |locked_wc: &mut LockedWorkingCopy, paths: &[&RepoPath]| {
|
|
|
|
let fs_paths = paths
|
|
|
|
.iter()
|
|
|
|
.map(|p| p.to_fs_path(&workspace_root))
|
|
|
|
.collect();
|
|
|
|
locked_wc
|
|
|
|
.snapshot(SnapshotOptions {
|
|
|
|
fsmonitor_kind: Some(FsmonitorKind::Test {
|
|
|
|
changed_files: fs_paths,
|
|
|
|
}),
|
|
|
|
..SnapshotOptions::empty_for_test()
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-07-08 12:23:32 +03:00
|
|
|
let tree_id = snapshot(&mut locked_wc, &[]);
|
|
|
|
assert_eq!(tree_id, *repo.store().empty_tree_id());
|
|
|
|
locked_wc.discard();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-07-08 12:23:32 +03:00
|
|
|
let tree_id = snapshot(&mut locked_wc, &[&foo_path]);
|
|
|
|
insta::assert_snapshot!(testutils::dump_tree(repo.store(), &tree_id), @r###"
|
|
|
|
tree 205f6b799e7d5c2524468ca006a0131aa57ecce7
|
|
|
|
file "foo" (257cc5642cb1a054f08cc83f2d943e56fd3ebe99): "foo\n"
|
|
|
|
"###);
|
|
|
|
locked_wc.discard();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-07-08 12:23:32 +03:00
|
|
|
let tree_id = snapshot(
|
|
|
|
&mut locked_wc,
|
|
|
|
&[&foo_path, &bar_path, &nested_path, &ignored_path],
|
|
|
|
);
|
|
|
|
insta::assert_snapshot!(testutils::dump_tree(repo.store(), &tree_id), @r###"
|
|
|
|
tree ab5a0465cc71725a723f28b685844a5bc0f5b599
|
|
|
|
file "bar" (5716ca5987cbf97d6bb54920bea6adde242d87e6): "bar\n"
|
|
|
|
file "foo" (257cc5642cb1a054f08cc83f2d943e56fd3ebe99): "foo\n"
|
|
|
|
file "path/to/nested" (79c53955ef856f16f2107446bc721c8879a1bd2e): "nested\n"
|
|
|
|
"###);
|
2023-07-14 22:45:00 +03:00
|
|
|
locked_wc.finish(repo.op_id().clone()).unwrap();
|
2023-07-08 12:23:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &foo_path, "updated foo\n");
|
|
|
|
testutils::write_working_copy_file(&workspace_root, &bar_path, "updated bar\n");
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-07-08 12:23:32 +03:00
|
|
|
let tree_id = snapshot(&mut locked_wc, &[&foo_path]);
|
|
|
|
insta::assert_snapshot!(testutils::dump_tree(repo.store(), &tree_id), @r###"
|
|
|
|
tree 2f57ab8f48ae62e3137079f2add9878dfa1d1bcc
|
|
|
|
file "bar" (5716ca5987cbf97d6bb54920bea6adde242d87e6): "bar\n"
|
|
|
|
file "foo" (9d053d7c8a18a286dce9b99a59bb058be173b463): "updated foo\n"
|
|
|
|
file "path/to/nested" (79c53955ef856f16f2107446bc721c8879a1bd2e): "nested\n"
|
|
|
|
"###);
|
|
|
|
locked_wc.discard();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::fs::remove_file(foo_path.to_fs_path(&workspace_root)).unwrap();
|
2023-07-14 23:33:59 +03:00
|
|
|
let mut locked_wc = wc.start_mutation().unwrap();
|
2023-07-08 12:23:32 +03:00
|
|
|
let tree_id = snapshot(&mut locked_wc, &[&foo_path]);
|
|
|
|
insta::assert_snapshot!(testutils::dump_tree(repo.store(), &tree_id), @r###"
|
|
|
|
tree 34b83765131477e1a7d72160079daec12c6144e3
|
|
|
|
file "bar" (5716ca5987cbf97d6bb54920bea6adde242d87e6): "bar\n"
|
|
|
|
file "path/to/nested" (79c53955ef856f16f2107446bc721c8879a1bd2e): "nested\n"
|
|
|
|
"###);
|
2023-07-14 22:45:00 +03:00
|
|
|
locked_wc.finish(repo.op_id().clone()).unwrap();
|
2023-07-08 12:23:32 +03:00
|
|
|
}
|
|
|
|
}
|