2020-12-12 08:00:42 +00:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
use std::fs::OpenOptions;
|
2021-10-21 05:09:09 +00:00
|
|
|
use std::io::{Read, Write};
|
2020-12-12 08:00:42 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-06-09 20:57:48 +00:00
|
|
|
use itertools::Itertools;
|
2020-12-12 08:00:42 +00:00
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
use crate::backend::{FileId, TreeId, TreeValue};
|
2021-05-01 04:41:27 +00:00
|
|
|
use crate::commit::Commit;
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::commit_builder::CommitBuilder;
|
2021-05-01 04:41:27 +00:00
|
|
|
use crate::repo::{MutableRepo, ReadonlyRepo};
|
2021-05-19 16:41:25 +00:00
|
|
|
use crate::repo_path::RepoPath;
|
2021-09-29 18:27:58 +00:00
|
|
|
use crate::rewrite::RebasedDescendant;
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::settings::UserSettings;
|
2021-09-12 06:52:38 +00:00
|
|
|
use crate::store::Store;
|
2020-12-12 08:00:42 +00:00
|
|
|
use crate::tree::Tree;
|
|
|
|
use crate::tree_builder::TreeBuilder;
|
|
|
|
|
2021-03-17 00:09:33 +00:00
|
|
|
pub fn new_user_home() -> TempDir {
|
|
|
|
// Set $HOME to some arbitrary place so libgit2 doesn't use ~/.gitignore
|
|
|
|
// of the person running the tests.
|
|
|
|
let home_dir = tempfile::tempdir().unwrap();
|
|
|
|
std::env::set_var("HOME", home_dir.path());
|
|
|
|
home_dir
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
pub fn user_settings() -> UserSettings {
|
|
|
|
let mut config = config::Config::new();
|
|
|
|
config.set("user.name", "Test User").unwrap();
|
|
|
|
config.set("user.email", "test.user@example.com").unwrap();
|
|
|
|
UserSettings::from_config(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn init_repo(settings: &UserSettings, use_git: bool) -> (TempDir, Arc<ReadonlyRepo>) {
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
|
|
|
|
let wc_path = temp_dir.path().join("repo");
|
|
|
|
fs::create_dir(&wc_path).unwrap();
|
|
|
|
|
|
|
|
let repo = if use_git {
|
|
|
|
let git_path = temp_dir.path().join("git-repo");
|
|
|
|
git2::Repository::init(&git_path).unwrap();
|
2021-06-14 07:18:38 +00:00
|
|
|
ReadonlyRepo::init_external_git(settings, wc_path, git_path).unwrap()
|
2020-12-12 08:00:42 +00:00
|
|
|
} else {
|
2021-06-14 07:18:38 +00:00
|
|
|
ReadonlyRepo::init_local(settings, wc_path).unwrap()
|
2020-12-12 08:00:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
(temp_dir, repo)
|
|
|
|
}
|
|
|
|
|
2021-10-21 05:09:09 +00:00
|
|
|
pub fn read_file(store: &Store, path: &RepoPath, id: &FileId) -> Vec<u8> {
|
|
|
|
let mut reader = store.read_file(path, id).unwrap();
|
|
|
|
let mut content = vec![];
|
|
|
|
reader.read_to_end(&mut content).unwrap();
|
|
|
|
content
|
|
|
|
}
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
pub fn write_file(store: &Store, path: &RepoPath, contents: &str) -> FileId {
|
2020-12-12 08:00:42 +00:00
|
|
|
store.write_file(path, &mut contents.as_bytes()).unwrap()
|
|
|
|
}
|
|
|
|
|
2021-05-17 04:55:51 +00:00
|
|
|
pub fn write_normal_file(tree_builder: &mut TreeBuilder, path: &RepoPath, contents: &str) {
|
2020-12-12 08:00:42 +00:00
|
|
|
let id = write_file(tree_builder.repo(), path, contents);
|
|
|
|
tree_builder.set(
|
2021-05-17 04:55:51 +00:00
|
|
|
path.clone(),
|
2020-12-12 08:00:42 +00:00
|
|
|
TreeValue::Normal {
|
|
|
|
id,
|
|
|
|
executable: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-17 04:55:51 +00:00
|
|
|
pub fn write_executable_file(tree_builder: &mut TreeBuilder, path: &RepoPath, contents: &str) {
|
2020-12-12 08:00:42 +00:00
|
|
|
let id = write_file(tree_builder.repo(), path, contents);
|
|
|
|
tree_builder.set(
|
2021-05-17 04:55:51 +00:00
|
|
|
path.clone(),
|
2020-12-12 08:00:42 +00:00
|
|
|
TreeValue::Normal {
|
|
|
|
id,
|
|
|
|
executable: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-17 04:55:51 +00:00
|
|
|
pub fn write_symlink(tree_builder: &mut TreeBuilder, path: &RepoPath, target: &str) {
|
2020-12-12 08:00:42 +00:00
|
|
|
let id = tree_builder.repo().write_symlink(path, target).unwrap();
|
2021-05-17 04:55:51 +00:00
|
|
|
tree_builder.set(path.clone(), TreeValue::Symlink(id));
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 04:55:51 +00:00
|
|
|
pub fn create_tree(repo: &ReadonlyRepo, path_contents: &[(&RepoPath, &str)]) -> Tree {
|
2020-12-12 08:00:42 +00:00
|
|
|
let store = repo.store();
|
|
|
|
let mut tree_builder = store.tree_builder(store.empty_tree_id().clone());
|
|
|
|
for (path, contents) in path_contents {
|
|
|
|
write_normal_file(&mut tree_builder, path, contents);
|
|
|
|
}
|
|
|
|
let id = tree_builder.write_tree();
|
2021-05-19 16:41:25 +00:00
|
|
|
store.get_tree(&RepoPath::root(), &id).unwrap()
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn create_random_tree(repo: &ReadonlyRepo) -> TreeId {
|
|
|
|
let mut tree_builder = repo
|
|
|
|
.store()
|
|
|
|
.tree_builder(repo.store().empty_tree_id().clone());
|
|
|
|
let number = rand::random::<u32>();
|
2021-05-17 05:47:31 +00:00
|
|
|
let path = RepoPath::from_internal_string(format!("file{}", number).as_str());
|
2020-12-12 08:00:42 +00:00
|
|
|
write_normal_file(&mut tree_builder, &path, "contents");
|
|
|
|
tree_builder.write_tree()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn create_random_commit(settings: &UserSettings, repo: &ReadonlyRepo) -> CommitBuilder {
|
|
|
|
let tree_id = create_random_tree(repo);
|
|
|
|
let number = rand::random::<u32>();
|
|
|
|
CommitBuilder::for_new_commit(settings, repo.store(), tree_id)
|
|
|
|
.set_description(format!("random commit {}", number))
|
|
|
|
}
|
|
|
|
|
2021-05-17 04:55:51 +00:00
|
|
|
pub fn write_working_copy_file(repo: &ReadonlyRepo, path: &RepoPath, contents: &str) {
|
2020-12-12 08:00:42 +00:00
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.truncate(true)
|
2021-05-17 04:14:18 +00:00
|
|
|
.open(path.to_fs_path(repo.working_copy_path()))
|
2020-12-12 08:00:42 +00:00
|
|
|
.unwrap();
|
|
|
|
file.write_all(contents.as_bytes()).unwrap();
|
|
|
|
}
|
2021-05-01 04:41:27 +00:00
|
|
|
|
|
|
|
pub struct CommitGraphBuilder<'settings, 'repo> {
|
|
|
|
settings: &'settings UserSettings,
|
|
|
|
mut_repo: &'repo mut MutableRepo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'settings, 'repo> CommitGraphBuilder<'settings, 'repo> {
|
|
|
|
pub fn new(
|
|
|
|
settings: &'settings UserSettings,
|
|
|
|
mut_repo: &'repo mut MutableRepo,
|
|
|
|
) -> CommitGraphBuilder<'settings, 'repo> {
|
|
|
|
CommitGraphBuilder { settings, mut_repo }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn initial_commit(&mut self) -> Commit {
|
|
|
|
create_random_commit(self.settings, self.mut_repo.base_repo().as_ref())
|
|
|
|
.write_to_repo(self.mut_repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn commit_with_parents(&mut self, parents: &[&Commit]) -> Commit {
|
2021-06-09 20:57:48 +00:00
|
|
|
let parent_ids = parents
|
|
|
|
.iter()
|
|
|
|
.map(|commit| commit.id().clone())
|
|
|
|
.collect_vec();
|
2021-05-01 04:41:27 +00:00
|
|
|
create_random_commit(self.settings, self.mut_repo.base_repo().as_ref())
|
|
|
|
.set_parents(parent_ids)
|
|
|
|
.write_to_repo(self.mut_repo)
|
|
|
|
}
|
|
|
|
}
|
2021-09-29 18:27:58 +00:00
|
|
|
|
|
|
|
pub fn assert_rebased(
|
|
|
|
rebased: Option<RebasedDescendant>,
|
|
|
|
expected_old_commit: &Commit,
|
|
|
|
expected_new_parents: &[&Commit],
|
|
|
|
) -> Commit {
|
|
|
|
if let Some(RebasedDescendant {
|
|
|
|
old_commit,
|
|
|
|
new_commit,
|
|
|
|
}) = rebased
|
|
|
|
{
|
|
|
|
assert_eq!(old_commit, *expected_old_commit);
|
|
|
|
assert_eq!(new_commit.change_id(), expected_old_commit.change_id());
|
|
|
|
assert_eq!(
|
|
|
|
new_commit.parent_ids(),
|
|
|
|
expected_new_parents
|
|
|
|
.iter()
|
|
|
|
.map(|commit| commit.id().clone())
|
|
|
|
.collect_vec()
|
|
|
|
);
|
|
|
|
new_commit
|
|
|
|
} else {
|
|
|
|
panic!("expected rebased commit: {:?}", rebased);
|
|
|
|
}
|
|
|
|
}
|