diff --git a/lib/tests/test_git.rs b/lib/tests/test_git.rs index d72203024..dda2228ed 100644 --- a/lib/tests/test_git.rs +++ b/lib/tests/test_git.rs @@ -71,6 +71,7 @@ fn test_import_refs() { let commit3 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]); let commit4 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]); let commit5 = empty_git_commit(&git_repo, "refs/tags/v1.0", &[&commit1]); + let commit6 = empty_git_commit(&git_repo, "refs/remotes/origin/feature3", &[&commit1]); // Should not be imported empty_git_commit(&git_repo, "refs/notes/x", &[&commit2]); empty_git_commit(&git_repo, "refs/remotes/origin/HEAD", &[&commit2]); @@ -87,7 +88,8 @@ fn test_import_refs() { let expected_heads = hashset! { jj_id(&commit3), jj_id(&commit4), - jj_id(&commit5) + jj_id(&commit5), + jj_id(&commit6) }; assert_eq!(*view.heads(), expected_heads); @@ -117,13 +119,23 @@ fn test_import_refs() { view.branches().get("feature2"), Some(expected_feature2_branch).as_ref() ); + let expected_feature3_branch = BranchTarget { + local_target: Some(RefTarget::Normal(jj_id(&commit6))), + remote_targets: btreemap! { + "origin".to_string() => RefTarget::Normal(jj_id(&commit6)), + }, + }; + assert_eq!( + view.branches().get("feature3"), + Some(expected_feature3_branch).as_ref() + ); assert_eq!( view.tags().get("v1.0"), Some(RefTarget::Normal(jj_id(&commit5))).as_ref() ); - assert_eq!(view.git_refs().len(), 5); + assert_eq!(view.git_refs().len(), 6); assert_eq!( view.git_refs().get("refs/heads/main"), Some(RefTarget::Normal(jj_id(&commit2))).as_ref() @@ -140,6 +152,10 @@ fn test_import_refs() { view.git_refs().get("refs/remotes/origin/main"), Some(RefTarget::Normal(jj_id(&commit1))).as_ref() ); + assert_eq!( + view.git_refs().get("refs/remotes/origin/feature3"), + Some(RefTarget::Normal(jj_id(&commit6))).as_ref() + ); assert_eq!( view.git_refs().get("refs/tags/v1.0"), Some(RefTarget::Normal(jj_id(&commit5))).as_ref() diff --git a/tests/test_git_import_export.rs b/tests/test_git_import_export.rs index 93d9b5e3b..927ab1c1d 100644 --- a/tests/test_git_import_export.rs +++ b/tests/test_git_import_export.rs @@ -11,6 +11,7 @@ // 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::path::Path; use crate::common::{get_stderr_string, TestEnvironment}; @@ -37,3 +38,46 @@ fn test_git_export_conflicting_git_refs() { export or their "parent" branches. "###); } + +#[test] +fn test_git_import_remote_only_branch() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]); + let repo_path = test_env.env_root().join("repo"); + + // Create non-empty git repo to add as a remote + let git_repo_path = test_env.env_root().join("git-repo"); + let git_repo = git2::Repository::init(git_repo_path).unwrap(); + let signature = + git2::Signature::new("Some One", "some.one@example.com", &git2::Time::new(0, 0)).unwrap(); + let mut tree_builder = git_repo.treebuilder(None).unwrap(); + let file_oid = git_repo.blob(b"content").unwrap(); + tree_builder + .insert("file", file_oid, git2::FileMode::Blob.into()) + .unwrap(); + let tree_oid = tree_builder.write().unwrap(); + let tree = git_repo.find_tree(tree_oid).unwrap(); + test_env.jj_cmd_success( + &repo_path, + &["git", "remote", "add", "origin", "../git-repo"], + ); + + git_repo + .commit( + Some("refs/heads/feature1"), + &signature, + &signature, + "message", + &tree, + &[], + ) + .unwrap(); + test_env.jj_cmd_success(&repo_path, &["git", "fetch", "--remote=origin"]); + insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###" + feature1: 9f01a0e04879 message + "###); +} + +fn get_branch_output(test_env: &TestEnvironment, repo_path: &Path) -> String { + test_env.jj_cmd_success(repo_path, &["branch", "list"]) +}