2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2020 The Jujutsu Authors
|
2020-12-12 08:00:42 +00: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-03-28 13:46:21 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-10-29 10:02:41 +00:00
|
|
|
use crate::common::{get_stderr_string, get_stdout_string, TestEnvironment};
|
2022-03-30 17:47:11 +00:00
|
|
|
|
|
|
|
pub mod common;
|
2020-12-12 08:00:42 +00:00
|
|
|
|
2022-03-28 13:46:21 +00:00
|
|
|
fn init_git_repo(git_repo_path: &PathBuf) {
|
2022-10-09 17:08:12 +00:00
|
|
|
let git_repo = git2::Repository::init(git_repo_path).unwrap();
|
2022-03-28 13:46:21 +00:00
|
|
|
let git_blob_oid = git_repo.blob(b"some content").unwrap();
|
|
|
|
let mut git_tree_builder = git_repo.treebuilder(None).unwrap();
|
|
|
|
git_tree_builder
|
|
|
|
.insert("some-file", git_blob_oid, 0o100644)
|
|
|
|
.unwrap();
|
|
|
|
let git_tree_id = git_tree_builder.write().unwrap();
|
|
|
|
let git_tree = git_repo.find_tree(git_tree_id).unwrap();
|
|
|
|
let git_signature = git2::Signature::new(
|
|
|
|
"Git User",
|
|
|
|
"git.user@example.com",
|
|
|
|
&git2::Time::new(123, 60),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
git_repo
|
|
|
|
.commit(
|
|
|
|
Some("refs/heads/my-branch"),
|
|
|
|
&git_signature,
|
|
|
|
&git_signature,
|
|
|
|
"My commit message",
|
|
|
|
&git_tree,
|
|
|
|
&[],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
git_repo.set_head("refs/heads/my-branch").unwrap();
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[test]
|
2020-12-28 08:26:37 +00:00
|
|
|
fn test_init_git_internal() {
|
2022-01-16 06:06:12 +00:00
|
|
|
let test_env = TestEnvironment::default();
|
2022-03-26 21:06:48 +00:00
|
|
|
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
|
2022-04-28 23:32:18 +00:00
|
|
|
insta::assert_snapshot!(stdout, @r###"
|
|
|
|
Initialized repo in "repo"
|
|
|
|
"###);
|
2022-03-05 01:51:02 +00:00
|
|
|
|
2022-03-09 22:12:32 +00:00
|
|
|
let workspace_root = test_env.env_root().join("repo");
|
2022-01-29 04:26:16 +00:00
|
|
|
let jj_path = workspace_root.join(".jj");
|
|
|
|
let repo_path = jj_path.join("repo");
|
|
|
|
let store_path = repo_path.join("store");
|
|
|
|
assert!(workspace_root.is_dir());
|
|
|
|
assert!(jj_path.is_dir());
|
|
|
|
assert!(jj_path.join("working_copy").is_dir());
|
2020-12-28 08:26:37 +00:00
|
|
|
assert!(repo_path.is_dir());
|
2022-01-29 04:26:16 +00:00
|
|
|
assert!(store_path.is_dir());
|
|
|
|
assert!(store_path.join("git").is_dir());
|
|
|
|
assert!(store_path.join("git_target").is_file());
|
|
|
|
let git_target_file_contents = std::fs::read_to_string(store_path.join("git_target")).unwrap();
|
2021-10-14 05:50:50 +00:00
|
|
|
assert_eq!(git_target_file_contents, "git");
|
2020-12-28 08:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_init_git_external() {
|
2022-01-16 06:06:12 +00:00
|
|
|
let test_env = TestEnvironment::default();
|
|
|
|
let git_repo_path = test_env.env_root().join("git-repo");
|
2022-03-28 13:46:21 +00:00
|
|
|
init_git_repo(&git_repo_path);
|
2022-03-05 01:51:02 +00:00
|
|
|
|
2022-03-26 21:06:48 +00:00
|
|
|
let stdout = test_env.jj_cmd_success(
|
|
|
|
test_env.env_root(),
|
|
|
|
&[
|
|
|
|
"init",
|
|
|
|
"repo",
|
|
|
|
"--git-repo",
|
|
|
|
git_repo_path.to_str().unwrap(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
insta::assert_snapshot!(stdout, @r###"
|
2022-05-17 17:22:46 +00:00
|
|
|
Working copy now at: f6950fc115ae (no description set)
|
2022-03-09 21:20:59 +00:00
|
|
|
Added 1 files, modified 0 files, removed 0 files
|
|
|
|
Initialized repo in "repo"
|
|
|
|
"###);
|
2022-03-05 01:51:02 +00:00
|
|
|
|
2022-03-09 22:12:32 +00:00
|
|
|
let workspace_root = test_env.env_root().join("repo");
|
2022-01-29 04:26:16 +00:00
|
|
|
let jj_path = workspace_root.join(".jj");
|
|
|
|
let repo_path = jj_path.join("repo");
|
|
|
|
let store_path = repo_path.join("store");
|
|
|
|
assert!(workspace_root.is_dir());
|
|
|
|
assert!(jj_path.is_dir());
|
|
|
|
assert!(jj_path.join("working_copy").is_dir());
|
2022-03-05 01:51:02 +00:00
|
|
|
assert!(repo_path.is_dir());
|
|
|
|
assert!(store_path.is_dir());
|
|
|
|
let git_target_file_contents = std::fs::read_to_string(store_path.join("git_target")).unwrap();
|
|
|
|
assert!(git_target_file_contents
|
|
|
|
.replace('\\', "/")
|
|
|
|
.ends_with("/git-repo/.git"));
|
2022-03-09 21:20:59 +00:00
|
|
|
|
|
|
|
// Check that the Git repo's HEAD got checked out
|
2022-03-26 21:06:48 +00:00
|
|
|
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "@-"]);
|
|
|
|
insta::assert_snapshot!(stdout, @r###"
|
2022-11-13 18:01:11 +00:00
|
|
|
o d3866db7e30a git.user@example.com 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git 8d698d4a8ee1
|
2022-03-09 21:20:59 +00:00
|
|
|
~ My commit message
|
|
|
|
"###);
|
2022-03-05 01:51:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 18:24:00 +00:00
|
|
|
#[test]
|
|
|
|
fn test_init_git_external_bad_path() {
|
|
|
|
let test_env = TestEnvironment::default();
|
|
|
|
let stderr = test_env.jj_cmd_failure(
|
|
|
|
test_env.env_root(),
|
|
|
|
&["init", "repo", "--git-repo", "non-existent"],
|
|
|
|
);
|
|
|
|
insta::assert_snapshot!(stderr, @r###"
|
|
|
|
Error: $TEST_ENV/non-existent doesn't exist
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
2022-03-05 01:51:02 +00:00
|
|
|
#[test]
|
|
|
|
fn test_init_git_colocated() {
|
|
|
|
let test_env = TestEnvironment::default();
|
|
|
|
let workspace_root = test_env.env_root().join("repo");
|
2022-03-28 13:46:21 +00:00
|
|
|
init_git_repo(&workspace_root);
|
2022-03-26 21:06:48 +00:00
|
|
|
let stdout = test_env.jj_cmd_success(&workspace_root, &["init", "--git-repo", "."]);
|
2022-04-28 23:32:18 +00:00
|
|
|
insta::assert_snapshot!(stdout, @r###"
|
|
|
|
Initialized repo in "."
|
|
|
|
"###);
|
2022-03-05 01:51:02 +00:00
|
|
|
|
|
|
|
let jj_path = workspace_root.join(".jj");
|
|
|
|
let repo_path = jj_path.join("repo");
|
|
|
|
let store_path = repo_path.join("store");
|
|
|
|
assert!(workspace_root.is_dir());
|
|
|
|
assert!(jj_path.is_dir());
|
|
|
|
assert!(jj_path.join("working_copy").is_dir());
|
2020-12-12 08:00:42 +00:00
|
|
|
assert!(repo_path.is_dir());
|
2022-01-29 04:26:16 +00:00
|
|
|
assert!(store_path.is_dir());
|
|
|
|
let git_target_file_contents = std::fs::read_to_string(store_path.join("git_target")).unwrap();
|
2021-10-14 05:50:50 +00:00
|
|
|
assert!(git_target_file_contents
|
2021-03-11 06:35:16 +00:00
|
|
|
.replace('\\', "/")
|
2022-03-05 01:51:02 +00:00
|
|
|
.ends_with("../../../.git"));
|
2022-03-28 13:46:21 +00:00
|
|
|
|
|
|
|
// Check that the Git repo's HEAD got checked out
|
|
|
|
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "@-"]);
|
|
|
|
insta::assert_snapshot!(stdout, @r###"
|
2022-11-13 18:01:11 +00:00
|
|
|
o d3866db7e30a git.user@example.com 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git 8d698d4a8ee1
|
2022-03-28 13:46:21 +00:00
|
|
|
~ My commit message
|
|
|
|
"###);
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 10:02:41 +00:00
|
|
|
#[test]
|
|
|
|
fn test_init_git_internal_but_could_be_colocated() {
|
|
|
|
let test_env = TestEnvironment::default();
|
|
|
|
let workspace_root = test_env.env_root().join("repo");
|
|
|
|
init_git_repo(&workspace_root);
|
|
|
|
|
|
|
|
let assert = test_env
|
|
|
|
.jj_cmd(&workspace_root, &["init", "--git"])
|
|
|
|
.assert()
|
|
|
|
.success();
|
|
|
|
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
|
|
|
|
Initialized repo in "."
|
|
|
|
"###);
|
|
|
|
insta::assert_snapshot!(get_stderr_string(&assert), @r###"
|
2022-11-12 23:41:28 +00:00
|
|
|
Empty repo created.
|
|
|
|
Hint: To create a repo backed by the existing Git repo, run `jj init --git-repo=.` instead.
|
2022-10-29 10:02:41 +00:00
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
2022-11-05 08:40:13 +00:00
|
|
|
#[test]
|
|
|
|
fn test_init_git_bad_wc_path() {
|
|
|
|
let test_env = TestEnvironment::default();
|
|
|
|
std::fs::write(test_env.env_root().join("existing-file"), b"").unwrap();
|
|
|
|
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["init", "--git", "existing-file"]);
|
|
|
|
assert!(stderr.contains("Failed to create workspace"));
|
|
|
|
}
|
|
|
|
|
2022-10-09 05:33:51 +00:00
|
|
|
#[test]
|
|
|
|
fn test_init_local_disallowed() {
|
|
|
|
let test_env = TestEnvironment::default();
|
|
|
|
let stdout = test_env.jj_cmd_failure(test_env.env_root(), &["init", "repo"]);
|
|
|
|
insta::assert_snapshot!(stdout, @r###"
|
2022-11-12 23:28:32 +00:00
|
|
|
Error: The native backend is disallowed by default.
|
|
|
|
Hint: Did you mean to pass `--git`?
|
2022-10-09 05:33:51 +00:00
|
|
|
Set `ui.allow-init-native` to allow initializing a repo with the native backend.
|
|
|
|
"###);
|
|
|
|
}
|
|
|
|
|
2020-12-12 08:00:42 +00:00
|
|
|
#[test]
|
|
|
|
fn test_init_local() {
|
2022-01-16 06:06:12 +00:00
|
|
|
let test_env = TestEnvironment::default();
|
2022-10-09 05:33:51 +00:00
|
|
|
test_env.add_config(
|
|
|
|
br#"[ui]
|
|
|
|
allow-init-native = true
|
|
|
|
"#,
|
|
|
|
);
|
2022-03-26 21:06:48 +00:00
|
|
|
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]);
|
2022-04-28 23:32:18 +00:00
|
|
|
insta::assert_snapshot!(stdout, @r###"
|
|
|
|
Initialized repo in "repo"
|
|
|
|
"###);
|
2022-03-05 01:51:02 +00:00
|
|
|
|
2022-03-09 22:12:32 +00:00
|
|
|
let workspace_root = test_env.env_root().join("repo");
|
2022-01-29 04:26:16 +00:00
|
|
|
let jj_path = workspace_root.join(".jj");
|
|
|
|
let repo_path = jj_path.join("repo");
|
|
|
|
let store_path = repo_path.join("store");
|
|
|
|
assert!(workspace_root.is_dir());
|
|
|
|
assert!(jj_path.is_dir());
|
|
|
|
assert!(jj_path.join("working_copy").is_dir());
|
2020-12-12 08:00:42 +00:00
|
|
|
assert!(repo_path.is_dir());
|
2022-01-29 04:26:16 +00:00
|
|
|
assert!(store_path.is_dir());
|
|
|
|
assert!(store_path.join("commits").is_dir());
|
|
|
|
assert!(store_path.join("trees").is_dir());
|
|
|
|
assert!(store_path.join("files").is_dir());
|
|
|
|
assert!(store_path.join("symlinks").is_dir());
|
|
|
|
assert!(store_path.join("conflicts").is_dir());
|
2020-12-12 08:00:42 +00:00
|
|
|
}
|