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.
|
|
|
|
|
|
|
|
use git2::Oid;
|
2021-01-03 19:45:07 +03:00
|
|
|
use jujube_lib::commit::Commit;
|
|
|
|
use jujube_lib::git;
|
2021-01-11 07:13:52 +03:00
|
|
|
use jujube_lib::git::{GitFetchError, GitPushError};
|
2021-01-31 10:44:31 +03:00
|
|
|
use jujube_lib::repo::ReadonlyRepo;
|
2021-01-03 19:45:07 +03:00
|
|
|
use jujube_lib::settings::UserSettings;
|
|
|
|
use jujube_lib::store::CommitId;
|
|
|
|
use jujube_lib::testutils;
|
2020-12-29 10:31:48 +03:00
|
|
|
use maplit::hashset;
|
|
|
|
use std::collections::HashSet;
|
2021-01-01 09:34:24 +03:00
|
|
|
use std::path::PathBuf;
|
2020-12-28 04:41:20 +03:00
|
|
|
use std::sync::Arc;
|
|
|
|
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,
|
|
|
|
&format!("commit on {}", ref_name),
|
|
|
|
&empty_tree,
|
|
|
|
parents,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
git_repo.find_commit(oid).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn commit_id(commit: &git2::Commit) -> CommitId {
|
|
|
|
CommitId(commit.id().as_bytes().to_vec())
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:31:48 +03:00
|
|
|
#[test]
|
|
|
|
fn test_import_refs() {
|
|
|
|
let settings = testutils::user_settings();
|
2021-01-11 11:26:03 +03:00
|
|
|
let (_temp_dir, repo) = testutils::init_repo(&settings, true);
|
|
|
|
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", &[]);
|
|
|
|
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-01-03 11:26:57 +03:00
|
|
|
let commit5 = empty_git_commit(&git_repo, "refs/remotes/origin/main", &[&commit2]);
|
|
|
|
// Should not be imported
|
|
|
|
empty_git_commit(&git_repo, "refs/notes/x", &[&commit2]);
|
2020-12-29 10:31:48 +03:00
|
|
|
|
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-01-16 23:15:06 +03:00
|
|
|
let heads_before: HashSet<_> = repo.view().heads().clone();
|
2021-01-11 07:13:52 +03:00
|
|
|
jujube_lib::git::import_refs(&mut tx, &git_repo).unwrap_or_default();
|
2021-02-14 00:30:45 +03:00
|
|
|
let view = tx.view();
|
2020-12-29 10:31:48 +03:00
|
|
|
let expected_heads: HashSet<_> = heads_before
|
2021-01-03 11:26:57 +03:00
|
|
|
.union(&hashset!(
|
|
|
|
commit_id(&commit3),
|
|
|
|
commit_id(&commit4),
|
|
|
|
commit_id(&commit5)
|
|
|
|
))
|
2020-12-29 10:31:48 +03:00
|
|
|
.cloned()
|
|
|
|
.collect();
|
2021-01-16 23:15:06 +03:00
|
|
|
assert_eq!(*view.heads(), expected_heads);
|
|
|
|
assert_eq!(*view.public_heads(), hashset!(commit_id(&commit5)));
|
2021-01-03 11:26:57 +03:00
|
|
|
assert_eq!(view.git_refs().len(), 4);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/main"),
|
|
|
|
Some(commit_id(&commit2)).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/feature1"),
|
|
|
|
Some(commit_id(&commit3)).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/feature2"),
|
|
|
|
Some(commit_id(&commit4)).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/remotes/origin/main"),
|
|
|
|
Some(commit_id(&commit5)).as_ref()
|
|
|
|
);
|
2020-12-29 10:31:48 +03:00
|
|
|
tx.discard();
|
|
|
|
}
|
|
|
|
|
2021-01-03 11:26:57 +03:00
|
|
|
#[test]
|
|
|
|
fn test_import_refs_reimport() {
|
|
|
|
let settings = testutils::user_settings();
|
2021-01-11 11:26:03 +03:00
|
|
|
let (_temp_dir, mut repo) = testutils::init_repo(&settings, true);
|
|
|
|
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", &[]);
|
|
|
|
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-01-16 23:15:06 +03:00
|
|
|
let heads_before = repo.view().heads().clone();
|
2021-01-03 11:26:57 +03:00
|
|
|
let mut tx = repo.start_transaction("test");
|
2021-01-11 07:13:52 +03:00
|
|
|
jujube_lib::git::import_refs(&mut tx, &git_repo).unwrap_or_default();
|
2021-01-03 11:26:57 +03:00
|
|
|
tx.commit();
|
|
|
|
|
|
|
|
// 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]);
|
|
|
|
|
|
|
|
Arc::get_mut(&mut repo).unwrap().reload();
|
|
|
|
let mut tx = repo.start_transaction("test");
|
2021-01-11 07:13:52 +03:00
|
|
|
jujube_lib::git::import_refs(&mut tx, &git_repo).unwrap_or_default();
|
2021-01-03 11:26:57 +03:00
|
|
|
|
2021-02-14 00:30:45 +03:00
|
|
|
let view = tx.view();
|
2021-01-03 11:26:57 +03:00
|
|
|
// TODO: commit3 and commit4 should probably be removed
|
|
|
|
let expected_heads: HashSet<_> = heads_before
|
|
|
|
.union(&hashset!(
|
|
|
|
commit_id(&commit3),
|
|
|
|
commit_id(&commit4),
|
|
|
|
commit_id(&commit5)
|
|
|
|
))
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
2021-01-16 23:15:06 +03:00
|
|
|
assert_eq!(*view.heads(), expected_heads);
|
2021-01-03 11:26:57 +03:00
|
|
|
assert_eq!(view.git_refs().len(), 2);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/main"),
|
|
|
|
Some(commit_id(&commit2)).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
view.git_refs().get("refs/heads/feature2"),
|
|
|
|
Some(commit_id(&commit5)).as_ref()
|
|
|
|
);
|
|
|
|
tx.discard();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn git_ref(git_repo: &git2::Repository, name: &str, target: Oid) {
|
|
|
|
git_repo.reference(name, target, true, "").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn delete_git_ref(git_repo: &git2::Repository, name: &str) {
|
|
|
|
git_repo.find_reference(name).unwrap().delete().unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_import_refs_merge() {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let (_temp_dir, mut repo) = testutils::init_repo(&settings, true);
|
|
|
|
let git_repo = repo.store().git_repo().unwrap();
|
|
|
|
|
|
|
|
// Set up the following refs and update them as follows:
|
|
|
|
// sideways-unchanged: one operation rewrites the ref
|
|
|
|
// unchanged-sideways: the other operation rewrites the ref
|
|
|
|
// remove-unchanged: one operation removes the ref
|
|
|
|
// unchanged-remove: the other operation removes the ref
|
|
|
|
// forward-forward: two operations move forward by different amounts
|
|
|
|
// sideways-sideways: two operations rewrite the ref
|
|
|
|
// forward-remove: one operation moves forward, the other operation removes
|
|
|
|
// remove-forward: one operation removes, the other operation moves
|
|
|
|
// add-add: two operations add the ref with different target
|
|
|
|
//
|
|
|
|
// The above cases distinguish between refs moving forward and sideways (and
|
|
|
|
// there are no tests for refs moving backward) because we may want to treat
|
|
|
|
// the cases differently, although that's still unclear.
|
|
|
|
//
|
|
|
|
// TODO: Consider adding more systematic testing to cover
|
|
|
|
// all state transitions. For example, the above does not include a case
|
|
|
|
// where a ref is added on both sides and one is an ancestor of the other
|
|
|
|
// (we should probably resolve that in favor of the descendant).
|
|
|
|
let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
|
|
|
|
let commit2 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit1]);
|
|
|
|
let commit3 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit2]);
|
|
|
|
let commit4 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]);
|
|
|
|
let commit5 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
|
|
|
|
git_ref(&git_repo, "refs/heads/sideways-unchanged", commit3.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/unchanged-sideways", commit3.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/remove-unchanged", commit3.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/unchanged-remove", commit3.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/sideways-sideways", commit3.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/forward-forward", commit1.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/forward-remove", commit1.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/remove-forward", commit1.id());
|
|
|
|
let mut tx = repo.start_transaction("initial import");
|
2021-01-11 07:13:52 +03:00
|
|
|
jujube_lib::git::import_refs(&mut tx, &git_repo).unwrap_or_default();
|
2021-01-03 11:26:57 +03:00
|
|
|
tx.commit();
|
|
|
|
Arc::get_mut(&mut repo).unwrap().reload();
|
|
|
|
|
|
|
|
// One of the concurrent operations:
|
|
|
|
git_ref(&git_repo, "refs/heads/sideways-unchanged", commit4.id());
|
|
|
|
delete_git_ref(&git_repo, "refs/heads/remove-unchanged");
|
|
|
|
git_ref(&git_repo, "refs/heads/sideways-sideways", commit4.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/forward-forward", commit2.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/forward-remove", commit2.id());
|
|
|
|
delete_git_ref(&git_repo, "refs/heads/remove-forward");
|
|
|
|
git_ref(&git_repo, "refs/heads/add-add", commit3.id());
|
|
|
|
let mut tx1 = repo.start_transaction("concurrent import 1");
|
2021-01-11 07:13:52 +03:00
|
|
|
jujube_lib::git::import_refs(&mut tx1, &git_repo).unwrap_or_default();
|
2021-01-03 11:26:57 +03:00
|
|
|
tx1.commit();
|
|
|
|
|
|
|
|
// The other concurrent operation:
|
|
|
|
git_ref(&git_repo, "refs/heads/unchanged-sideways", commit4.id());
|
|
|
|
delete_git_ref(&git_repo, "refs/heads/unchanged-remove");
|
|
|
|
git_ref(&git_repo, "refs/heads/sideways-sideways", commit5.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/forward-forward", commit3.id());
|
|
|
|
delete_git_ref(&git_repo, "refs/heads/forward-remove");
|
|
|
|
git_ref(&git_repo, "refs/heads/remove-forward", commit2.id());
|
|
|
|
git_ref(&git_repo, "refs/heads/add-add", commit4.id());
|
|
|
|
let mut tx2 = repo.start_transaction("concurrent import 2");
|
2021-01-11 07:13:52 +03:00
|
|
|
jujube_lib::git::import_refs(&mut tx2, &git_repo).unwrap_or_default();
|
2021-01-03 11:26:57 +03:00
|
|
|
tx2.commit();
|
|
|
|
|
|
|
|
// Reload the repo, causing the operations to be merged.
|
|
|
|
Arc::get_mut(&mut repo).unwrap().reload();
|
|
|
|
|
|
|
|
let view = repo.view();
|
|
|
|
let git_refs = view.git_refs();
|
|
|
|
assert_eq!(git_refs.len(), 9);
|
|
|
|
assert_eq!(
|
|
|
|
git_refs.get("refs/heads/sideways-unchanged"),
|
|
|
|
Some(commit_id(&commit4)).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
git_refs.get("refs/heads/unchanged-sideways"),
|
|
|
|
Some(commit_id(&commit4)).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(git_refs.get("refs/heads/remove-unchanged"), None);
|
|
|
|
assert_eq!(git_refs.get("refs/heads/unchanged-remove"), None);
|
|
|
|
// TODO: Perhaps we should automatically resolve this to the descendant-most
|
|
|
|
// commit? (We currently do get the descendant-most, but that's only because we
|
|
|
|
// let the later operation overwrite.)
|
|
|
|
assert_eq!(
|
|
|
|
git_refs.get("refs/heads/forward-forward"),
|
|
|
|
Some(commit_id(&commit3)).as_ref()
|
|
|
|
);
|
|
|
|
// TODO: The rest of these should be conflicts (however we decide to represent
|
|
|
|
// that).
|
|
|
|
assert_eq!(
|
|
|
|
git_refs.get("refs/heads/sideways-sideways"),
|
|
|
|
Some(commit_id(&commit5)).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(git_refs.get("refs/heads/forward-remove"), None);
|
|
|
|
assert_eq!(
|
|
|
|
git_refs.get("refs/heads/remove-forward"),
|
|
|
|
Some(commit_id(&commit2)).as_ref()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
git_refs.get("refs/heads/add-add"),
|
|
|
|
Some(commit_id(&commit4)).as_ref()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-12-29 10:31:48 +03:00
|
|
|
#[test]
|
|
|
|
fn test_import_refs_empty_git_repo() {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let git_repo_dir = temp_dir.path().join("source");
|
|
|
|
let jj_repo_dir = temp_dir.path().join("jj");
|
|
|
|
|
2021-01-11 07:13:52 +03:00
|
|
|
let git_repo = git2::Repository::init_bare(&git_repo_dir).unwrap();
|
2020-12-29 10:31:48 +03:00
|
|
|
|
|
|
|
std::fs::create_dir(&jj_repo_dir).unwrap();
|
|
|
|
let repo = ReadonlyRepo::init_external_git(&settings, jj_repo_dir, git_repo_dir);
|
2021-01-16 23:15:06 +03:00
|
|
|
let heads_before = repo.view().heads().clone();
|
2020-12-29 10:31:48 +03:00
|
|
|
let mut tx = repo.start_transaction("test");
|
2021-01-11 07:13:52 +03:00
|
|
|
jujube_lib::git::import_refs(&mut tx, &git_repo).unwrap_or_default();
|
2021-02-14 00:30:45 +03:00
|
|
|
assert_eq!(*tx.view().heads(), heads_before);
|
|
|
|
assert_eq!(tx.view().git_refs().len(), 0);
|
2020-12-29 10:31:48 +03:00
|
|
|
tx.discard();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
let repo = ReadonlyRepo::init_external_git(&settings, jj_repo_dir.clone(), git_repo_dir);
|
|
|
|
// 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]
|
|
|
|
fn test_fetch_success() {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
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_git_repo = git2::Repository::init_bare(&source_repo_dir).unwrap();
|
|
|
|
let initial_git_commit = empty_git_commit(&source_git_repo, "refs/heads/main", &[]);
|
|
|
|
let clone_git_repo =
|
|
|
|
git2::Repository::clone(&source_repo_dir.to_str().unwrap(), &clone_repo_dir).unwrap();
|
2020-12-31 20:56:20 +03:00
|
|
|
std::fs::create_dir(&jj_repo_dir).unwrap();
|
|
|
|
ReadonlyRepo::init_external_git(&settings, jj_repo_dir.clone(), clone_repo_dir.clone());
|
|
|
|
|
2021-01-11 07:13:52 +03:00
|
|
|
let new_git_commit =
|
|
|
|
empty_git_commit(&source_git_repo, "refs/heads/main", &[&initial_git_commit]);
|
2020-12-31 20:56:20 +03:00
|
|
|
|
|
|
|
// The new commit is not visible before git::fetch().
|
2021-01-04 10:11:22 +03:00
|
|
|
let jj_repo = ReadonlyRepo::load(&settings, jj_repo_dir.clone()).unwrap();
|
2021-01-16 23:15:06 +03:00
|
|
|
assert!(!jj_repo.view().heads().contains(&commit_id(&new_git_commit)));
|
2020-12-31 20:56:20 +03:00
|
|
|
|
|
|
|
// The new commit is visible after git::fetch().
|
|
|
|
let mut tx = jj_repo.start_transaction("test");
|
2021-01-11 07:13:52 +03:00
|
|
|
git::fetch(&mut tx, &clone_git_repo, "origin").unwrap();
|
2021-02-14 00:30:45 +03:00
|
|
|
assert!(tx.view().heads().contains(&commit_id(&new_git_commit)));
|
2020-12-31 20:56:20 +03:00
|
|
|
|
|
|
|
tx.discard();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fetch_no_such_remote() {
|
|
|
|
let settings = testutils::user_settings();
|
|
|
|
let temp_dir = tempfile::tempdir().unwrap();
|
|
|
|
let source_repo_dir = temp_dir.path().join("source");
|
|
|
|
let jj_repo_dir = temp_dir.path().join("jj");
|
2021-01-11 07:13:52 +03:00
|
|
|
let git_repo = git2::Repository::init_bare(&source_repo_dir).unwrap();
|
2020-12-31 20:56:20 +03:00
|
|
|
std::fs::create_dir(&jj_repo_dir).unwrap();
|
|
|
|
let jj_repo =
|
|
|
|
ReadonlyRepo::init_external_git(&settings, jj_repo_dir.clone(), source_repo_dir.clone());
|
|
|
|
|
|
|
|
let mut tx = jj_repo.start_transaction("test");
|
2021-01-11 07:13:52 +03:00
|
|
|
let result = git::fetch(&mut tx, &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
|
|
|
|
|
|
|
tx.discard();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
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();
|
|
|
|
let mut jj_repo =
|
2020-12-28 11:26:37 +03:00
|
|
|
ReadonlyRepo::init_external_git(&settings, jj_repo_dir.clone(), clone_repo_dir.clone());
|
2020-12-28 04:41:20 +03:00
|
|
|
let new_commit = testutils::create_random_commit(&settings, &jj_repo)
|
|
|
|
.set_parents(vec![initial_commit_id.clone()])
|
|
|
|
.write_to_new_transaction(&jj_repo, "test");
|
|
|
|
Arc::get_mut(&mut jj_repo).unwrap().reload();
|
|
|
|
PushTestSetup {
|
|
|
|
source_repo_dir,
|
|
|
|
jj_repo,
|
|
|
|
new_commit,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_commit_success() {
|
|
|
|
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();
|
|
|
|
let result = git::push_commit(&clone_repo, &setup.new_commit, "origin", "main");
|
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();
|
|
|
|
let new_oid = Oid::from_bytes(&setup.new_commit.id().0).unwrap();
|
|
|
|
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]
|
|
|
|
fn test_push_commit_not_fast_forward() {
|
|
|
|
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);
|
|
|
|
let new_commit = testutils::create_random_commit(&settings, &setup.jj_repo)
|
|
|
|
.write_to_new_transaction(&setup.jj_repo, "test");
|
|
|
|
Arc::get_mut(&mut setup.jj_repo).unwrap().reload();
|
|
|
|
let result = git::push_commit(
|
|
|
|
&setup.jj_repo.store().git_repo().unwrap(),
|
|
|
|
&new_commit,
|
|
|
|
"origin",
|
|
|
|
"main",
|
|
|
|
);
|
2020-12-28 04:41:20 +03:00
|
|
|
assert_eq!(result, Err(GitPushError::NotFastForward));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_commit_no_such_remote() {
|
|
|
|
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 result = git::push_commit(
|
|
|
|
&setup.jj_repo.store().git_repo().unwrap(),
|
|
|
|
&setup.new_commit,
|
|
|
|
"invalid-remote",
|
|
|
|
"main",
|
|
|
|
);
|
2021-01-01 23:53:07 +03:00
|
|
|
assert!(matches!(result, Err(GitPushError::NoSuchRemote(_))));
|
2020-12-28 04:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_commit_invalid_remote() {
|
|
|
|
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 result = git::push_commit(
|
|
|
|
&setup.jj_repo.store().git_repo().unwrap(),
|
|
|
|
&setup.new_commit,
|
|
|
|
"http://invalid-remote",
|
|
|
|
"main",
|
|
|
|
);
|
2021-01-01 23:53:07 +03:00
|
|
|
assert!(matches!(result, Err(GitPushError::NoSuchRemote(_))));
|
2020-12-28 04:41:20 +03:00
|
|
|
}
|