-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: detect .git symlink as a colocated workspace
Maybe we could load GitBackend without resolving .git symlink, but that would introduce more subtle bugs. Instead, we calculate the expected Git workdir path from the canonical ".git" path. Fixes #2011
- Loading branch information
Showing
3 changed files
with
175 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,18 +12,20 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::path::PathBuf; | ||
use std::path::Path; | ||
|
||
use test_case::test_case; | ||
|
||
use crate::common::TestEnvironment; | ||
|
||
pub mod common; | ||
|
||
fn init_git_repo(git_repo_path: &PathBuf, bare: bool) { | ||
let git_repo = | ||
git2::Repository::init_opts(git_repo_path, git2::RepositoryInitOptions::new().bare(bare)) | ||
.unwrap(); | ||
fn init_git_repo(git_repo_path: &Path, bare: bool) { | ||
init_git_repo_with_opts(git_repo_path, git2::RepositoryInitOptions::new().bare(bare)); | ||
} | ||
|
||
fn init_git_repo_with_opts(git_repo_path: &Path, opts: &git2::RepositoryInitOptions) { | ||
let git_repo = git2::Repository::init_opts(git_repo_path, opts).unwrap(); | ||
let git_blob_oid = git_repo.blob(b"some content").unwrap(); | ||
let mut git_tree_builder = git_repo.treebuilder(None).unwrap(); | ||
git_tree_builder | ||
|
@@ -185,6 +187,152 @@ fn test_init_git_colocated() { | |
│ My commit message | ||
~ | ||
"###); | ||
|
||
// Check that the Git repo's HEAD moves | ||
test_env.jj_cmd_success(&workspace_root, &["new"]); | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ sqpuoqvx [email protected] 2001-02-03 04:05:07.000 +07:00 HEAD@git f61b77cd | ||
│ (no description set) | ||
~ | ||
"###); | ||
} | ||
|
||
#[test] | ||
fn test_init_git_colocated_gitlink() { | ||
let test_env = TestEnvironment::default(); | ||
// <workspace_root>/.git -> <git_repo_path> | ||
let git_repo_path = test_env.env_root().join("git-repo"); | ||
let workspace_root = test_env.env_root().join("repo"); | ||
init_git_repo_with_opts( | ||
&git_repo_path, | ||
git2::RepositoryInitOptions::new().workdir_path(&workspace_root), | ||
); | ||
assert!(workspace_root.join(".git").is_file()); | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["init", "--git-repo", "."]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
Initialized repo in "." | ||
"###); | ||
|
||
// Check that the Git repo's HEAD got checked out | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ mwrttmos [email protected] 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git 8d698d4a | ||
│ My commit message | ||
~ | ||
"###); | ||
|
||
// Check that the Git repo's HEAD moves | ||
test_env.jj_cmd_success(&workspace_root, &["new"]); | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ sqpuoqvx [email protected] 2001-02-03 04:05:07.000 +07:00 HEAD@git f61b77cd | ||
│ (no description set) | ||
~ | ||
"###); | ||
} | ||
|
||
#[cfg(unix)] | ||
#[test] | ||
fn test_init_git_colocated_symlink_directory() { | ||
let test_env = TestEnvironment::default(); | ||
// <workspace_root>/.git -> <git_repo_path> | ||
let git_repo_path = test_env.env_root().join("git-repo"); | ||
let workspace_root = test_env.env_root().join("repo"); | ||
init_git_repo(&git_repo_path, false); | ||
std::fs::create_dir(&workspace_root).unwrap(); | ||
std::os::unix::fs::symlink(git_repo_path.join(".git"), workspace_root.join(".git")).unwrap(); | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["init", "--git-repo", "."]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
Initialized repo in "." | ||
"###); | ||
|
||
// Check that the Git repo's HEAD got checked out | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ mwrttmos [email protected] 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git 8d698d4a | ||
│ My commit message | ||
~ | ||
"###); | ||
|
||
// Check that the Git repo's HEAD moves | ||
test_env.jj_cmd_success(&workspace_root, &["new"]); | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ sqpuoqvx [email protected] 2001-02-03 04:05:07.000 +07:00 HEAD@git f61b77cd | ||
│ (no description set) | ||
~ | ||
"###); | ||
} | ||
|
||
#[cfg(unix)] | ||
#[test] | ||
fn test_init_git_colocated_symlink_gitlink() { | ||
let test_env = TestEnvironment::default(); | ||
// <workspace_root>/.git -> <git_workdir_path>/.git -> <git_repo_path> | ||
let git_repo_path = test_env.env_root().join("git-repo"); | ||
let git_workdir_path = test_env.env_root().join("git-workdir"); | ||
let workspace_root = test_env.env_root().join("repo"); | ||
init_git_repo_with_opts( | ||
&git_repo_path, | ||
git2::RepositoryInitOptions::new().workdir_path(&git_workdir_path), | ||
); | ||
assert!(git_workdir_path.join(".git").is_file()); | ||
std::fs::create_dir(&workspace_root).unwrap(); | ||
std::os::unix::fs::symlink(git_workdir_path.join(".git"), workspace_root.join(".git")).unwrap(); | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["init", "--git-repo", "."]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
Initialized repo in "." | ||
"###); | ||
|
||
// Check that the Git repo's HEAD got checked out | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ mwrttmos [email protected] 1970-01-01 01:02:03.000 +01:00 my-branch HEAD@git 8d698d4a | ||
│ My commit message | ||
~ | ||
"###); | ||
|
||
// Check that the Git repo's HEAD moves | ||
test_env.jj_cmd_success(&workspace_root, &["new"]); | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ sqpuoqvx [email protected] 2001-02-03 04:05:07.000 +07:00 HEAD@git f61b77cd | ||
│ (no description set) | ||
~ | ||
"###); | ||
} | ||
|
||
#[test] | ||
fn test_init_git_external_but_git_dir_exists() { | ||
let test_env = TestEnvironment::default(); | ||
let git_repo_path = test_env.env_root().join("git-repo"); | ||
let workspace_root = test_env.env_root().join("repo"); | ||
git2::Repository::init(&git_repo_path).unwrap(); | ||
init_git_repo(&workspace_root, false); | ||
let stdout = test_env.jj_cmd_success( | ||
&workspace_root, | ||
&["init", "--git-repo", git_repo_path.to_str().unwrap()], | ||
); | ||
insta::assert_snapshot!(stdout, @r###" | ||
Initialized repo in "." | ||
"###); | ||
|
||
// The local ".git" repository is unrelated, so no commits should be imported | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ zzzzzzzz 1970-01-01 00:00:00.000 +00:00 00000000 | ||
(empty) (no description set) | ||
"###); | ||
|
||
// Check that Git HEAD is not set because this isn't a colocated repo | ||
test_env.jj_cmd_success(&workspace_root, &["new"]); | ||
let stdout = test_env.jj_cmd_success(&workspace_root, &["log", "-r", "@-"]); | ||
insta::assert_snapshot!(stdout, @r###" | ||
◉ qpvuntsm [email protected] 2001-02-03 04:05:07.000 +07:00 230dd059 | ||
│ (empty) (no description set) | ||
~ | ||
"###); | ||
} | ||
|
||
#[test] | ||
|