2020-12-28 04:41:20 +03: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.
|
|
|
|
|
2021-03-14 20:37:28 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2020-12-28 04:41:20 +03:00
|
|
|
use git2::Oid;
|
2021-09-12 09:52:38 +03:00
|
|
|
use jujutsu_lib::backend::CommitId;
|
2021-05-15 19:16:31 +03:00
|
|
|
use jujutsu_lib::commit::Commit;
|
2021-09-11 08:35:31 +03:00
|
|
|
use jujutsu_lib::git::{GitFetchError, GitPushError, GitRefUpdate};
|
2021-07-15 11:31:48 +03:00
|
|
|
use jujutsu_lib::op_store::{BranchTarget, RefTarget};
|
2021-05-15 19:16:31 +03:00
|
|
|
use jujutsu_lib::repo::ReadonlyRepo;
|
|
|
|
use jujutsu_lib::settings::UserSettings;
|
2021-07-15 11:31:48 +03:00
|
|
|
use jujutsu_lib::testutils::create_random_commit;
|
2021-05-15 19:16:31 +03:00
|
|
|
use jujutsu_lib::{git, testutils};
|
2021-07-15 11:31:48 +03:00
|
|
|
use maplit::{btreemap, hashset};
|
2020-12-28 04:41:20 +03:00
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2021-01-01 09:34:24 +03:00
|
|
|
fn empty_git_commit<'r>(
|
|
|
|
git_repo: &'r git2::Repository,
|
|
|
|
ref_name: &str,
|
|
|
|
parents: &[&git2::Commit],
|
|
|
|
) -> git2::Commit<'r> {
|
|
|
|
let signature = git2::Signature::now("Someone", "someone@example.com").unwrap();
|
|
|
|
let empty_tree_id = Oid::from_str("4b825dc642cb6eb9a060e54bf8d69288fbee4904").unwrap();
|
|
|
|
let empty_tree = git_repo.find_tree(empty_tree_id).unwrap();
|
|
|
|
let oid = git_repo
|
|
|
|
.commit(
|
|
|
|
Some(ref_name),
|
|
|
|
&signature,
|
|
|
|
&signature,
|
2021-08-02 07:17:14 +03:00
|
|
|
&format!("random commit {}", rand::random::<u32>()),
|
2021-01-01 09:34:24 +03:00
|
|
|
&empty_tree,
|
|
|
|
parents,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
git_repo.find_commit(oid).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn commit_id(commit: &git2::Commit) -> CommitId {
|
2021-11-18 01:20:54 +03:00
|
|
|
CommitId::from_bytes(commit.id().as_bytes())
|
2021-01-01 09:34:24 +03:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:31:48 +03:00
|
|
|
#[test]
|
|
|
|
fn test_import_refs() {
|
|
|
|
let settings = testutils::user_settings();
|
2021-11-21 10:46:54 +03:00
|
|
|
let test_workspace = testutils::init_repo(&settings, true);
|
|
|
|
let repo = &test_workspace.repo;
|
2021-01-11 11:26:03 +03:00
|
|
|
let git_repo = repo.store().git_repo().unwrap();
|
2020-12-29 10:31:48 +03:00
|
|
|
|
2021-01-01 09:34:24 +03:00
|
|
|
let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
|
2021-07-15 11:31:48 +03:00
|
|
|
git_ref(&git_repo, "refs/remotes/origin/main", commit1.id());
|
2021-01-01 09:34:24 +03:00
|
|
|
let commit2 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit1]);
|
|
|
|
let commit3 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]);
|
|
|
|
let commit4 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
|
2021-07-15 11:31:48 +03:00
|
|
|
let commit5 = empty_git_commit(&git_repo, "refs/tags/v1.0", &[&commit1]);
|
2021-01-03 11:26:57 +03:00
|
|
|
// Should not be imported
|
|
|
|
empty_git_commit(&git_repo, "refs/notes/x", &[&commit2]);
|
2021-11-29 02:00:46 +03:00
|
|
|
empty_git_commit(&git_repo, "refs/remotes/origin/HEAD", &[&commit2]);
|
2020-12-29 10:31:48 +03:00
|
|
|
|
2021-11-28 23:29:04 +03:00
|
|
|
git_repo.set_head("refs/heads/main").unwrap();
|
|
|
|
|
2021-01-11 07:13:52 +03:00
|
|
|
let git_repo = repo.store().git_repo().unwrap();
|
2020-12-29 10:31:48 +03:00
|
|
|
let mut tx = repo.start_transaction("test");
|
2021-12-09 02:15:21 +03:00
|
|
|
git::import_refs(tx.mut_repo(), &git_repo).unwrap();
|
2021-07-31 00:35:08 +03:00
|
|
|
let repo = tx.commit();
|
|
|
|
let view = repo.view();
|
2021-07-15 11:31:48 +03:00
|
|
|
|
2021-08-02 07:17:14 +03:00
|
|
|
let expected_heads = hashset! {
|
2021-07-15 11:31:48 +03:00
|
|
|
view.checkout().clone(),
|
|
|
|
commit_id(&commit3),
|
|
|
|
commit_id(&commit4),
|
|
|
|
commit_id(&commit5)
|
2021-08-02 07:17:14 +03:00
|
|
|
};
|
2021-01-16 23:15:06 +03:00
|
|
|
assert_eq!(*view.heads(), expected_heads);
|
2021-07-15 11:31:48 +03:00
|
|
|
|
|
|
|
let expected_main_branch = BranchTarget {
|
|
|
|
local_target: Some(RefTarget::Normal(commit_id(&commit2))),
|
|
|
|
remote_targets: btreemap! {
|
|
|
|
"origin".to_string() => RefTarget::Normal(commit_id(&commit1)),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
view.branches().get("main"),
|
|
|
|
Some(expected_main_branch).as_ref()
|
|
|
|
);
|
|
|
|
let expected_feature1_branch = BranchTarget {
|
|
|
|
local_target: Some(RefTarget::Normal(commit_id(&commit3))),
|
|
|
|
remote_targets: btreemap! {},
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
view.branches().get("feature1"),
|
|
|
|
Some(expected_feature1_branch).as_ref()
|
|
|
|
);
|
|
|
|
let expected_feature2_branch = BranchTarget {
|
|
|
|
local_target: Some(RefTarget::Normal(commit_id(&commit4))),
|
|
|
|
remote_targets: btreemap! {},
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
view.branches().get("feature2"),
|
|
|
|
Some(expected_feature2_branch).as_ref()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
view.tags().get("v1.0"),
|
|
|
|
Some(RefTarget::Normal(commit_id(&commit5))).as_ref()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(view.git_refs().len(), 5);
|
2021-01-03 11:26:57 +03:00
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/main"),
|
2021-07-11 20:58:01 +03:00
|
|
|
Some(RefTarget::Normal(commit_id(&commit2))).as_ref()
|
2021-01-03 11:26:57 +03:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/feature1"),
|
2021-07-11 20:58:01 +03:00
|
|
|
Some(RefTarget::Normal(commit_id(&commit3))).as_ref()
|
2021-01-03 11:26:57 +03:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/feature2"),
|
2021-07-11 20:58:01 +03:00
|
|
|
Some(RefTarget::Normal(commit_id(&commit4))).as_ref()
|
2021-01-03 11:26:57 +03:00
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/remotes/origin/main"),
|
2021-07-15 11:31:48 +03:00
|
|
|
Some(RefTarget::Normal(commit_id(&commit1))).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/tags/v1.0"),
|
2021-07-11 20:58:01 +03:00
|
|
|
Some(RefTarget::Normal(commit_id(&commit5))).as_ref()
|
2021-01-03 11:26:57 +03:00
|
|
|
);
|
2021-11-28 23:29:04 +03:00
|
|
|
assert_eq!(view.git_head(), Some(commit_id(&commit2)));
|
2020-12-29 10:31:48 +03:00
|
|
|
}
|
|
|
|
|
2021-01-03 11:26:57 +03:00
|
|
|
#[test]
|
|
|
|
fn test_import_refs_reimport() {
|
|
|
|
let settings = testutils::user_settings();
|
2021-11-21 10:46:54 +03:00
|
|
|
let test_workspace = testutils::init_repo(&settings, true);
|
|
|
|
let repo = &test_workspace.repo;
|
2021-01-11 11:26:03 +03:00
|
|
|
let git_repo = repo.store().git_repo().unwrap();
|
2021-01-03 11:26:57 +03:00
|
|
|
|
|
|
|
let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
|
2021-07-15 11:31:48 +03:00
|
|
|
git_ref(&git_repo, "refs/remotes/origin/main", commit1.id());
|
2021-01-03 11:26:57 +03:00
|
|
|
let commit2 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit1]);
|
|
|
|
let commit3 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]);
|
|
|
|
let commit4 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
|
2021-03-14 23:39:45 +03:00
|
|
|
let pgp_key_oid = git_repo.blob(b"my PGP key").unwrap();
|
2021-03-15 08:27:17 +03:00
|
|
|
git_repo
|
|
|
|
.reference("refs/tags/my-gpg-key", pgp_key_oid, false, "")
|
|
|
|
.unwrap();
|
2021-01-03 11:26:57 +03:00
|
|
|
|
|
|
|
let mut tx = repo.start_transaction("test");
|
2021-12-09 02:15:21 +03:00
|
|
|
git::import_refs(tx.mut_repo(), &git_repo).unwrap();
|
2021-05-19 23:39:03 +03:00
|
|
|
let repo = tx.commit();
|
2021-01-03 11:26:57 +03:00
|
|
|
|
|
|
|
// Delete feature1 and rewrite feature2
|
2021-01-11 11:26:03 +03:00
|
|
|
delete_git_ref(&git_repo, "refs/heads/feature1");
|
|
|
|
delete_git_ref(&git_repo, "refs/heads/feature2");
|
2021-01-03 11:26:57 +03:00
|
|
|
let commit5 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
|
|
|
|
|
2021-07-15 11:31:48 +03:00
|
|
|
// Also modify feature2 on the jj side
|
|
|
|
let mut tx = repo.start_transaction("test");
|
|
|
|
let commit6 = create_random_commit(&settings, &repo)
|
|
|
|
.set_parents(vec![commit_id(&commit2)])
|
|
|
|
.write_to_repo(tx.mut_repo());
|
|
|
|
tx.mut_repo().set_local_branch(
|
|
|
|
"feature2".to_string(),
|
|
|
|
RefTarget::Normal(commit6.id().clone()),
|
|
|
|
);
|
|
|
|
let repo = tx.commit();
|
|
|
|
|
2021-01-03 11:26:57 +03:00
|
|
|
let mut tx = repo.start_transaction("test");
|
2021-12-09 02:15:21 +03:00
|
|
|
git::import_refs(tx.mut_repo(), &git_repo).unwrap();
|
2021-07-31 00:35:08 +03:00
|
|
|
let repo = tx.commit();
|
2021-01-03 11:26:57 +03:00
|
|
|
|
2021-07-31 00:35:08 +03:00
|
|
|
let view = repo.view();
|
2021-01-03 11:26:57 +03:00
|
|
|
// TODO: commit3 and commit4 should probably be removed
|
2021-08-02 07:17:14 +03:00
|
|
|
let expected_heads = hashset! {
|
|
|
|
view.checkout().clone(),
|
2021-01-03 11:26:57 +03:00
|
|
|
commit_id(&commit3),
|
|
|
|
commit_id(&commit4),
|
2021-07-15 11:31:48 +03:00
|
|
|
commit_id(&commit5),
|
|
|
|
commit6.id().clone(),
|
2021-08-02 07:17:14 +03:00
|
|
|
};
|
2021-01-16 23:15:06 +03:00
|
|
|
assert_eq!(*view.heads(), expected_heads);
|
2021-07-15 11:31:48 +03:00
|
|
|
|
|
|
|
assert_eq!(view.branches().len(), 2);
|
|
|
|
let commit1_target = RefTarget::Normal(commit_id(&commit1));
|
|
|
|
let commit2_target = RefTarget::Normal(commit_id(&commit2));
|
|
|
|
let expected_main_branch = BranchTarget {
|
|
|
|
local_target: Some(RefTarget::Normal(commit_id(&commit2))),
|
|
|
|
remote_targets: btreemap! {
|
|
|
|
"origin".to_string() => commit1_target.clone(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
view.branches().get("main"),
|
|
|
|
Some(expected_main_branch).as_ref()
|
|
|
|
);
|
|
|
|
let expected_feature2_branch = BranchTarget {
|
|
|
|
local_target: Some(RefTarget::Conflict {
|
|
|
|
removes: vec![commit_id(&commit4)],
|
|
|
|
adds: vec![commit6.id().clone(), commit_id(&commit5)],
|
|
|
|
}),
|
|
|
|
remote_targets: btreemap! {},
|
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
view.branches().get("feature2"),
|
|
|
|
Some(expected_feature2_branch).as_ref()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(view.tags().is_empty());
|
|
|
|
|
|
|
|
assert_eq!(view.git_refs().len(), 3);
|
2021-01-03 11:26:57 +03:00
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/main"),
|
2021-07-15 11:31:48 +03:00
|
|
|
Some(commit2_target).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/remotes/origin/main"),
|
|
|
|
Some(commit1_target).as_ref()
|
2021-01-03 11:26:57 +03:00
|
|
|
);
|
2021-07-15 11:31:48 +03:00
|
|
|
let commit5_target = RefTarget::Normal(commit_id(&commit5));
|
2021-01-03 11:26:57 +03:00
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/feature2"),
|
2021-07-15 11:31:48 +03:00
|
|
|
Some(commit5_target).as_ref()
|
2021-01-03 11:26:57 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-15 11:31:48 +03:00
|
|
|
fn git_ref(git_repo: &git2::Repository, name: &str, target: Oid) {
|
|
|
|
git_repo.reference(name, target, true, "").unwrap();
|
|
|
|
}
|
|
|
|
|
2021-01-03 11:26:57 +03:00
|
|
|
fn delete_git_ref(git_repo: &git2::Repository, name: &str) {
|
|
|
|
git_repo.find_reference(name).unwrap().delete().unwrap();
|
|
|
|
}
|
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
struct GitRepoData {
|
|
|
|
_temp_dir: TempDir,
|
|
|
|
origin_repo: git2::Repository,
|
|
|
|
git_repo: git2::Repository,
|
|
|
|
repo: Arc<ReadonlyRepo>,
|
|
|
|
}
|
2020-12-29 10:31:48 +03:00
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
impl GitRepoData {
|
|
|
|
fn create() -> Self {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let origin_repo_dir = temp_dir.path().join("source");
|
|
|
|
let origin_repo = git2::Repository::init_bare(&origin_repo_dir).unwrap();
|
|
|
|
let git_repo_dir = temp_dir.path().join("git");
|
|
|
|
let git_repo =
|
|
|
|
git2::Repository::clone(origin_repo_dir.to_str().unwrap(), &git_repo_dir).unwrap();
|
|
|
|
let jj_repo_dir = temp_dir.path().join("jj");
|
|
|
|
std::fs::create_dir(&jj_repo_dir).unwrap();
|
|
|
|
let repo = ReadonlyRepo::init_external_git(&settings, jj_repo_dir, git_repo_dir);
|
|
|
|
Self {
|
|
|
|
_temp_dir: temp_dir,
|
|
|
|
origin_repo,
|
|
|
|
git_repo,
|
|
|
|
repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-29 10:31:48 +03:00
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
#[test]
|
|
|
|
fn test_import_refs_empty_git_repo() {
|
|
|
|
let test_data = GitRepoData::create();
|
|
|
|
let heads_before = test_data.repo.view().heads().clone();
|
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
git::import_refs(tx.mut_repo(), &test_data.git_repo).unwrap();
|
2021-07-31 00:35:08 +03:00
|
|
|
let repo = tx.commit();
|
|
|
|
assert_eq!(*repo.view().heads(), heads_before);
|
2021-07-15 11:31:48 +03:00
|
|
|
assert_eq!(repo.view().branches().len(), 0);
|
|
|
|
assert_eq!(repo.view().tags().len(), 0);
|
2021-07-31 00:35:08 +03:00
|
|
|
assert_eq!(repo.view().git_refs().len(), 0);
|
2021-11-28 23:29:04 +03:00
|
|
|
assert_eq!(repo.view().git_head(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_import_refs_detached_head() {
|
2021-12-09 02:15:21 +03:00
|
|
|
let test_data = GitRepoData::create();
|
|
|
|
let commit1 = empty_git_commit(&test_data.git_repo, "refs/heads/main", &[]);
|
2021-11-28 23:29:04 +03:00
|
|
|
// Delete the reference. Check that the detached HEAD commit still gets added to
|
|
|
|
// the set of heads
|
2021-12-09 02:15:21 +03:00
|
|
|
test_data
|
|
|
|
.git_repo
|
2021-11-28 23:29:04 +03:00
|
|
|
.find_reference("refs/heads/main")
|
|
|
|
.unwrap()
|
|
|
|
.delete()
|
|
|
|
.unwrap();
|
2021-12-09 02:15:21 +03:00
|
|
|
test_data.git_repo.set_head_detached(commit1.id()).unwrap();
|
2021-11-28 23:29:04 +03:00
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
git::import_refs(tx.mut_repo(), &test_data.git_repo).unwrap();
|
2021-11-28 23:29:04 +03:00
|
|
|
let repo = tx.commit();
|
|
|
|
|
|
|
|
let expected_heads = hashset! {
|
|
|
|
repo.view().checkout().clone(),
|
|
|
|
commit_id(&commit1),
|
|
|
|
};
|
|
|
|
assert_eq!(*repo.view().heads(), expected_heads);
|
|
|
|
assert_eq!(repo.view().git_refs().len(), 0);
|
|
|
|
assert_eq!(
|
|
|
|
repo.view().git_head(),
|
|
|
|
Some(CommitId::from_bytes(commit1.id().as_bytes()))
|
|
|
|
);
|
2020-12-29 10:31:48 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 10:19:39 +03:00
|
|
|
#[test]
|
|
|
|
fn test_init() {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let git_repo_dir = temp_dir.path().join("git");
|
|
|
|
let jj_repo_dir = temp_dir.path().join("jj");
|
2021-01-01 09:34:24 +03:00
|
|
|
let git_repo = git2::Repository::init_bare(&git_repo_dir).unwrap();
|
|
|
|
let initial_git_commit = empty_git_commit(&git_repo, "refs/heads/main", &[]);
|
|
|
|
let initial_commit_id = commit_id(&initial_git_commit);
|
2020-12-30 10:19:39 +03:00
|
|
|
std::fs::create_dir(&jj_repo_dir).unwrap();
|
2021-11-24 10:54:30 +03:00
|
|
|
let repo = ReadonlyRepo::init_external_git(&settings, jj_repo_dir, git_repo_dir);
|
2020-12-30 10:19:39 +03:00
|
|
|
// The refs were *not* imported -- it's the caller's responsibility to import
|
|
|
|
// any refs they care about.
|
2021-01-16 23:15:06 +03:00
|
|
|
assert!(!repo.view().heads().contains(&initial_commit_id));
|
2020-12-30 10:19:39 +03:00
|
|
|
}
|
|
|
|
|
2020-12-31 20:56:20 +03:00
|
|
|
#[test]
|
2021-12-09 02:15:21 +03:00
|
|
|
fn test_fetch_empty_repo() {
|
|
|
|
let test_data = GitRepoData::create();
|
2020-12-31 20:56:20 +03:00
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
let default_branch = git::fetch(tx.mut_repo(), &test_data.git_repo, "origin").unwrap();
|
|
|
|
// No default branch and no refs
|
|
|
|
assert_eq!(default_branch, None);
|
|
|
|
assert_eq!(*tx.mut_repo().view().git_refs(), btreemap! {});
|
|
|
|
assert_eq!(*tx.mut_repo().view().branches(), btreemap! {});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch_initial_commit() {
|
|
|
|
let test_data = GitRepoData::create();
|
|
|
|
let initial_git_commit = empty_git_commit(&test_data.origin_repo, "refs/heads/main", &[]);
|
2020-12-31 20:56:20 +03:00
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
let default_branch = git::fetch(tx.mut_repo(), &test_data.git_repo, "origin").unwrap();
|
|
|
|
// No default branch because the origin repo's HEAD wasn't set
|
|
|
|
assert_eq!(default_branch, None);
|
|
|
|
let repo = tx.commit();
|
|
|
|
// The initial commit commit is visible after git::fetch().
|
|
|
|
let view = repo.view();
|
|
|
|
assert!(view.heads().contains(&commit_id(&initial_git_commit)));
|
|
|
|
let initial_commit_target = RefTarget::Normal(commit_id(&initial_git_commit));
|
|
|
|
assert_eq!(
|
|
|
|
*view.git_refs(),
|
|
|
|
btreemap! {
|
|
|
|
"refs/remotes/origin/main".to_string() => initial_commit_target.clone(),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
*view.branches(),
|
|
|
|
btreemap! {
|
|
|
|
"main".to_string() => BranchTarget {
|
|
|
|
local_target: Some(initial_commit_target.clone()),
|
|
|
|
remote_targets: btreemap! {"origin".to_string() => initial_commit_target}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch_success() {
|
|
|
|
let mut test_data = GitRepoData::create();
|
|
|
|
let initial_git_commit = empty_git_commit(&test_data.origin_repo, "refs/heads/main", &[]);
|
|
|
|
|
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
git::fetch(tx.mut_repo(), &test_data.git_repo, "origin").unwrap();
|
|
|
|
test_data.repo = tx.commit();
|
|
|
|
|
|
|
|
test_data.origin_repo.set_head("refs/heads/main").unwrap();
|
|
|
|
let new_git_commit = empty_git_commit(
|
|
|
|
&test_data.origin_repo,
|
|
|
|
"refs/heads/main",
|
|
|
|
&[&initial_git_commit],
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
let default_branch = git::fetch(tx.mut_repo(), &test_data.git_repo, "origin").unwrap();
|
2021-09-22 19:14:56 +03:00
|
|
|
// The default branch is "main"
|
2021-09-23 02:17:01 +03:00
|
|
|
assert_eq!(default_branch, Some("main".to_string()));
|
2021-07-31 00:35:08 +03:00
|
|
|
let repo = tx.commit();
|
2021-12-09 02:15:21 +03:00
|
|
|
// The new commit is visible after we fetch again
|
2021-09-23 07:45:54 +03:00
|
|
|
let view = repo.view();
|
|
|
|
assert!(view.heads().contains(&commit_id(&new_git_commit)));
|
|
|
|
let new_commit_target = RefTarget::Normal(commit_id(&new_git_commit));
|
|
|
|
assert_eq!(
|
|
|
|
*view.git_refs(),
|
|
|
|
btreemap! {
|
|
|
|
"refs/remotes/origin/main".to_string() => new_commit_target.clone(),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
*view.branches(),
|
|
|
|
btreemap! {
|
|
|
|
"main".to_string() => BranchTarget {
|
|
|
|
local_target: Some(new_commit_target.clone()),
|
|
|
|
remote_targets: btreemap! {"origin".to_string() => new_commit_target}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2020-12-31 20:56:20 +03:00
|
|
|
}
|
|
|
|
|
2021-11-08 02:18:24 +03:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_prune_deleted_ref() {
|
2021-12-09 02:15:21 +03:00
|
|
|
let test_data = GitRepoData::create();
|
|
|
|
empty_git_commit(&test_data.git_repo, "refs/heads/main", &[]);
|
2021-11-08 02:18:24 +03:00
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
git::fetch(tx.mut_repo(), &test_data.git_repo, "origin").unwrap();
|
2021-11-08 02:18:24 +03:00
|
|
|
// Test the setup
|
|
|
|
assert!(tx.mut_repo().get_branch("main").is_some());
|
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
test_data
|
|
|
|
.git_repo
|
2021-11-08 02:18:24 +03:00
|
|
|
.find_reference("refs/heads/main")
|
|
|
|
.unwrap()
|
|
|
|
.delete()
|
|
|
|
.unwrap();
|
2021-12-09 02:15:21 +03:00
|
|
|
// After re-fetching, the branch should be deleted
|
|
|
|
git::fetch(tx.mut_repo(), &test_data.git_repo, "origin").unwrap();
|
2021-11-08 02:18:24 +03:00
|
|
|
assert!(tx.mut_repo().get_branch("main").is_none());
|
|
|
|
}
|
|
|
|
|
2021-09-22 19:14:56 +03:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_no_default_branch() {
|
2021-12-09 02:15:21 +03:00
|
|
|
let test_data = GitRepoData::create();
|
|
|
|
let initial_git_commit = empty_git_commit(&test_data.origin_repo, "refs/heads/main", &[]);
|
|
|
|
|
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
git::fetch(tx.mut_repo(), &test_data.git_repo, "origin").unwrap();
|
2021-09-22 19:14:56 +03:00
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
empty_git_commit(
|
|
|
|
&test_data.origin_repo,
|
|
|
|
"refs/heads/main",
|
|
|
|
&[&initial_git_commit],
|
|
|
|
);
|
2021-09-22 19:14:56 +03:00
|
|
|
// It's actually not enough to have a detached HEAD, it also needs to point to a
|
2021-12-09 02:15:21 +03:00
|
|
|
// commit without a branch (that's possibly a bug in Git *and* libgit2), so
|
2021-09-22 19:14:56 +03:00
|
|
|
// we point it to initial_git_commit.
|
2021-12-09 02:15:21 +03:00
|
|
|
test_data
|
|
|
|
.origin_repo
|
2021-09-22 19:14:56 +03:00
|
|
|
.set_head_detached(initial_git_commit.id())
|
|
|
|
.unwrap();
|
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
let default_branch = git::fetch(tx.mut_repo(), &test_data.git_repo, "origin").unwrap();
|
2021-09-22 19:14:56 +03:00
|
|
|
// There is no default branch
|
|
|
|
assert_eq!(default_branch, None);
|
|
|
|
}
|
|
|
|
|
2020-12-31 20:56:20 +03:00
|
|
|
#[test]
|
|
|
|
fn test_fetch_no_such_remote() {
|
2021-12-09 02:15:21 +03:00
|
|
|
let test_data = GitRepoData::create();
|
2020-12-31 20:56:20 +03:00
|
|
|
|
2021-12-09 02:15:21 +03:00
|
|
|
let mut tx = test_data.repo.start_transaction("test");
|
|
|
|
let result = git::fetch(tx.mut_repo(), &test_data.git_repo, "invalid-remote");
|
2021-01-01 23:53:07 +03:00
|
|
|
assert!(matches!(result, Err(GitFetchError::NoSuchRemote(_))));
|
2020-12-31 20:56:20 +03:00
|
|
|
}
|
|
|
|
|
2020-12-28 04:41:20 +03:00
|
|
|
struct PushTestSetup {
|
|
|
|
source_repo_dir: PathBuf,
|
|
|
|
jj_repo: Arc<ReadonlyRepo>,
|
|
|
|
new_commit: Commit,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_up_push_repos(settings: &UserSettings, temp_dir: &TempDir) -> PushTestSetup {
|
|
|
|
let source_repo_dir = temp_dir.path().join("source");
|
|
|
|
let clone_repo_dir = temp_dir.path().join("clone");
|
|
|
|
let jj_repo_dir = temp_dir.path().join("jj");
|
2021-01-11 07:13:52 +03:00
|
|
|
let source_repo = git2::Repository::init_bare(&source_repo_dir).unwrap();
|
|
|
|
let initial_git_commit = empty_git_commit(&source_repo, "refs/heads/main", &[]);
|
2021-01-01 09:34:24 +03:00
|
|
|
let initial_commit_id = commit_id(&initial_git_commit);
|
2021-06-14 10:18:38 +03:00
|
|
|
git2::Repository::clone(source_repo_dir.to_str().unwrap(), &clone_repo_dir).unwrap();
|
2020-12-28 04:41:20 +03:00
|
|
|
std::fs::create_dir(&jj_repo_dir).unwrap();
|
2021-11-24 10:54:30 +03:00
|
|
|
let jj_repo = ReadonlyRepo::init_external_git(settings, jj_repo_dir, clone_repo_dir);
|
2021-09-11 19:45:06 +03:00
|
|
|
let mut tx = jj_repo.start_transaction("test");
|
2021-06-14 10:18:38 +03:00
|
|
|
let new_commit = testutils::create_random_commit(settings, &jj_repo)
|
2021-02-27 10:00:46 +03:00
|
|
|
.set_parents(vec![initial_commit_id])
|
2021-09-11 19:45:06 +03:00
|
|
|
.write_to_repo(tx.mut_repo());
|
|
|
|
let jj_repo = tx.commit();
|
2020-12-28 04:41:20 +03:00
|
|
|
PushTestSetup {
|
|
|
|
source_repo_dir,
|
|
|
|
jj_repo,
|
|
|
|
new_commit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-11 08:35:31 +03:00
|
|
|
fn test_push_updates_success() {
|
2020-12-28 04:41:20 +03:00
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let setup = set_up_push_repos(&settings, &temp_dir);
|
2021-01-11 07:13:52 +03:00
|
|
|
let clone_repo = setup.jj_repo.store().git_repo().unwrap();
|
2021-09-11 08:35:31 +03:00
|
|
|
let result = git::push_updates(
|
|
|
|
&clone_repo,
|
|
|
|
"origin",
|
|
|
|
&[GitRefUpdate {
|
|
|
|
qualified_name: "refs/heads/main".to_string(),
|
|
|
|
force: false,
|
|
|
|
new_target: Some(setup.new_commit.id().clone()),
|
|
|
|
}],
|
|
|
|
);
|
2020-12-28 04:41:20 +03:00
|
|
|
assert_eq!(result, Ok(()));
|
|
|
|
|
|
|
|
// Check that the ref got updated in the source repo
|
|
|
|
let source_repo = git2::Repository::open(&setup.source_repo_dir).unwrap();
|
|
|
|
let new_target = source_repo
|
|
|
|
.find_reference("refs/heads/main")
|
|
|
|
.unwrap()
|
|
|
|
.target();
|
2021-11-18 01:20:54 +03:00
|
|
|
let new_oid = Oid::from_bytes(setup.new_commit.id().as_bytes()).unwrap();
|
2020-12-28 04:41:20 +03:00
|
|
|
assert_eq!(new_target, Some(new_oid));
|
|
|
|
|
|
|
|
// Check that the ref got updated in the cloned repo. This just tests our
|
|
|
|
// assumptions about libgit2 because we want the refs/remotes/origin/main
|
|
|
|
// branch to be updated.
|
|
|
|
let new_target = clone_repo
|
|
|
|
.find_reference("refs/remotes/origin/main")
|
|
|
|
.unwrap()
|
|
|
|
.target();
|
|
|
|
assert_eq!(new_target, Some(new_oid));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-11 08:35:31 +03:00
|
|
|
fn test_push_updates_deletion() {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let setup = set_up_push_repos(&settings, &temp_dir);
|
|
|
|
let clone_repo = setup.jj_repo.store().git_repo().unwrap();
|
|
|
|
|
|
|
|
let source_repo = git2::Repository::open(&setup.source_repo_dir).unwrap();
|
|
|
|
// Test the setup
|
|
|
|
assert!(source_repo.find_reference("refs/heads/main").is_ok());
|
|
|
|
|
|
|
|
let result = git::push_updates(
|
|
|
|
&setup.jj_repo.store().git_repo().unwrap(),
|
|
|
|
"origin",
|
|
|
|
&[GitRefUpdate {
|
|
|
|
qualified_name: "refs/heads/main".to_string(),
|
|
|
|
force: false,
|
|
|
|
new_target: None,
|
|
|
|
}],
|
|
|
|
);
|
|
|
|
assert_eq!(result, Ok(()));
|
|
|
|
|
|
|
|
// Check that the ref got deleted in the source repo
|
|
|
|
assert!(source_repo.find_reference("refs/heads/main").is_err());
|
|
|
|
|
|
|
|
// Check that the ref got deleted in the cloned repo. This just tests our
|
|
|
|
// assumptions about libgit2 because we want the refs/remotes/origin/main
|
|
|
|
// branch to be deleted.
|
|
|
|
assert!(clone_repo
|
|
|
|
.find_reference("refs/remotes/origin/main")
|
|
|
|
.is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_updates_mixed_deletion_and_addition() {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let setup = set_up_push_repos(&settings, &temp_dir);
|
|
|
|
let clone_repo = setup.jj_repo.store().git_repo().unwrap();
|
|
|
|
let result = git::push_updates(
|
|
|
|
&clone_repo,
|
|
|
|
"origin",
|
|
|
|
&[
|
|
|
|
GitRefUpdate {
|
|
|
|
qualified_name: "refs/heads/main".to_string(),
|
|
|
|
force: false,
|
|
|
|
new_target: None,
|
|
|
|
},
|
|
|
|
GitRefUpdate {
|
|
|
|
qualified_name: "refs/heads/topic".to_string(),
|
|
|
|
force: false,
|
|
|
|
new_target: Some(setup.new_commit.id().clone()),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
assert_eq!(result, Ok(()));
|
|
|
|
|
|
|
|
// Check that the topic ref got updated in the source repo
|
|
|
|
let source_repo = git2::Repository::open(&setup.source_repo_dir).unwrap();
|
|
|
|
let new_target = source_repo
|
|
|
|
.find_reference("refs/heads/topic")
|
|
|
|
.unwrap()
|
|
|
|
.target();
|
2021-11-18 01:20:54 +03:00
|
|
|
let new_oid = Oid::from_bytes(setup.new_commit.id().as_bytes()).unwrap();
|
2021-09-11 08:35:31 +03:00
|
|
|
assert_eq!(new_target, Some(new_oid));
|
|
|
|
|
|
|
|
// Check that the main ref got deleted in the source repo
|
|
|
|
assert!(source_repo.find_reference("refs/heads/main").is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_updates_not_fast_forward() {
|
2020-12-28 04:41:20 +03:00
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
2021-01-11 07:13:52 +03:00
|
|
|
let mut setup = set_up_push_repos(&settings, &temp_dir);
|
2021-09-11 19:45:06 +03:00
|
|
|
let mut tx = setup.jj_repo.start_transaction("test");
|
|
|
|
let new_commit =
|
|
|
|
testutils::create_random_commit(&settings, &setup.jj_repo).write_to_repo(tx.mut_repo());
|
|
|
|
setup.jj_repo = tx.commit();
|
2021-09-11 08:35:31 +03:00
|
|
|
let result = git::push_updates(
|
2021-01-11 07:13:52 +03:00
|
|
|
&setup.jj_repo.store().git_repo().unwrap(),
|
|
|
|
"origin",
|
2021-09-11 08:35:31 +03:00
|
|
|
&[GitRefUpdate {
|
|
|
|
qualified_name: "refs/heads/main".to_string(),
|
|
|
|
force: false,
|
|
|
|
new_target: Some(new_commit.id().clone()),
|
|
|
|
}],
|
2021-01-11 07:13:52 +03:00
|
|
|
);
|
2020-12-28 04:41:20 +03:00
|
|
|
assert_eq!(result, Err(GitPushError::NotFastForward));
|
|
|
|
}
|
|
|
|
|
2021-08-05 00:30:06 +03:00
|
|
|
#[test]
|
2021-09-11 08:35:31 +03:00
|
|
|
fn test_push_updates_not_fast_forward_with_force() {
|
2021-08-05 00:30:06 +03:00
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let mut setup = set_up_push_repos(&settings, &temp_dir);
|
2021-09-11 19:45:06 +03:00
|
|
|
let mut tx = setup.jj_repo.start_transaction("test");
|
|
|
|
let new_commit =
|
|
|
|
testutils::create_random_commit(&settings, &setup.jj_repo).write_to_repo(tx.mut_repo());
|
|
|
|
setup.jj_repo = tx.commit();
|
2021-09-11 08:35:31 +03:00
|
|
|
let result = git::push_updates(
|
2021-08-05 00:30:06 +03:00
|
|
|
&setup.jj_repo.store().git_repo().unwrap(),
|
|
|
|
"origin",
|
2021-09-11 08:35:31 +03:00
|
|
|
&[GitRefUpdate {
|
|
|
|
qualified_name: "refs/heads/main".to_string(),
|
|
|
|
force: true,
|
|
|
|
new_target: Some(new_commit.id().clone()),
|
|
|
|
}],
|
2021-08-05 00:30:06 +03:00
|
|
|
);
|
|
|
|
assert_eq!(result, Ok(()));
|
|
|
|
|
|
|
|
// Check that the ref got updated in the source repo
|
|
|
|
let source_repo = git2::Repository::open(&setup.source_repo_dir).unwrap();
|
|
|
|
let new_target = source_repo
|
|
|
|
.find_reference("refs/heads/main")
|
|
|
|
.unwrap()
|
|
|
|
.target();
|
2021-11-18 01:20:54 +03:00
|
|
|
let new_oid = Oid::from_bytes(new_commit.id().as_bytes()).unwrap();
|
2021-08-05 00:30:06 +03:00
|
|
|
assert_eq!(new_target, Some(new_oid));
|
|
|
|
}
|
|
|
|
|
2020-12-28 04:41:20 +03:00
|
|
|
#[test]
|
2021-09-11 08:35:31 +03:00
|
|
|
fn test_push_updates_no_such_remote() {
|
2020-12-28 04:41:20 +03:00
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let setup = set_up_push_repos(&settings, &temp_dir);
|
2021-09-11 08:35:31 +03:00
|
|
|
let result = git::push_updates(
|
2021-01-11 07:13:52 +03:00
|
|
|
&setup.jj_repo.store().git_repo().unwrap(),
|
|
|
|
"invalid-remote",
|
2021-09-11 08:35:31 +03:00
|
|
|
&[GitRefUpdate {
|
|
|
|
qualified_name: "refs/heads/main".to_string(),
|
|
|
|
force: false,
|
|
|
|
new_target: Some(setup.new_commit.id().clone()),
|
|
|
|
}],
|
2021-01-11 07:13:52 +03:00
|
|
|
);
|
2021-01-01 23:53:07 +03:00
|
|
|
assert!(matches!(result, Err(GitPushError::NoSuchRemote(_))));
|
2020-12-28 04:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-11 08:35:31 +03:00
|
|
|
fn test_push_updates_invalid_remote() {
|
2020-12-28 04:41:20 +03:00
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let setup = set_up_push_repos(&settings, &temp_dir);
|
2021-09-11 08:35:31 +03:00
|
|
|
let result = git::push_updates(
|
2021-01-11 07:13:52 +03:00
|
|
|
&setup.jj_repo.store().git_repo().unwrap(),
|
|
|
|
"http://invalid-remote",
|
2021-09-11 08:35:31 +03:00
|
|
|
&[GitRefUpdate {
|
|
|
|
qualified_name: "refs/heads/main".to_string(),
|
|
|
|
force: false,
|
|
|
|
new_target: Some(setup.new_commit.id().clone()),
|
|
|
|
}],
|
2021-01-11 07:13:52 +03:00
|
|
|
);
|
2021-01-01 23:53:07 +03:00
|
|
|
assert!(matches!(result, Err(GitPushError::NoSuchRemote(_))));
|
2020-12-28 04:41:20 +03:00
|
|
|
}
|