git: test import of remote-only branch

This test coverage will become more important when we make changes to
remote branch importing.
This commit is contained in:
Glen Choo 2022-12-12 17:04:57 -08:00
parent 469d315003
commit 4716c1e9e2
2 changed files with 62 additions and 2 deletions

View file

@ -71,6 +71,7 @@ fn test_import_refs() {
let commit3 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]); let commit3 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]);
let commit4 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&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 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 // Should not be imported
empty_git_commit(&git_repo, "refs/notes/x", &[&commit2]); empty_git_commit(&git_repo, "refs/notes/x", &[&commit2]);
empty_git_commit(&git_repo, "refs/remotes/origin/HEAD", &[&commit2]); empty_git_commit(&git_repo, "refs/remotes/origin/HEAD", &[&commit2]);
@ -87,7 +88,8 @@ fn test_import_refs() {
let expected_heads = hashset! { let expected_heads = hashset! {
jj_id(&commit3), jj_id(&commit3),
jj_id(&commit4), jj_id(&commit4),
jj_id(&commit5) jj_id(&commit5),
jj_id(&commit6)
}; };
assert_eq!(*view.heads(), expected_heads); assert_eq!(*view.heads(), expected_heads);
@ -117,13 +119,23 @@ fn test_import_refs() {
view.branches().get("feature2"), view.branches().get("feature2"),
Some(expected_feature2_branch).as_ref() 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!( assert_eq!(
view.tags().get("v1.0"), view.tags().get("v1.0"),
Some(RefTarget::Normal(jj_id(&commit5))).as_ref() Some(RefTarget::Normal(jj_id(&commit5))).as_ref()
); );
assert_eq!(view.git_refs().len(), 5); assert_eq!(view.git_refs().len(), 6);
assert_eq!( assert_eq!(
view.git_refs().get("refs/heads/main"), view.git_refs().get("refs/heads/main"),
Some(RefTarget::Normal(jj_id(&commit2))).as_ref() Some(RefTarget::Normal(jj_id(&commit2))).as_ref()
@ -140,6 +152,10 @@ fn test_import_refs() {
view.git_refs().get("refs/remotes/origin/main"), view.git_refs().get("refs/remotes/origin/main"),
Some(RefTarget::Normal(jj_id(&commit1))).as_ref() 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!( assert_eq!(
view.git_refs().get("refs/tags/v1.0"), view.git_refs().get("refs/tags/v1.0"),
Some(RefTarget::Normal(jj_id(&commit5))).as_ref() Some(RefTarget::Normal(jj_id(&commit5))).as_ref()

View file

@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::path::Path;
use crate::common::{get_stderr_string, TestEnvironment}; use crate::common::{get_stderr_string, TestEnvironment};
@ -37,3 +38,46 @@ fn test_git_export_conflicting_git_refs() {
export or their "parent" branches. 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"])
}