Skip to content

Commit

Permalink
insert refs check (need to streamline)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyagr committed May 11, 2024
1 parent ef10a8b commit 8b7e930
Showing 1 changed file with 130 additions and 36 deletions.
166 changes: 130 additions & 36 deletions lib/tests/test_git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use jj_lib::git::{
use jj_lib::git_backend::GitBackend;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::{BranchTarget, RefTarget, RemoteRef, RemoteRefState};
use jj_lib::refs::BranchPushUpdate;
use jj_lib::refs::{merge_ref_targets, BranchPushUpdate};
use jj_lib::repo::{MutableRepo, ReadonlyRepo, Repo};
use jj_lib::settings::{GitSettings, UserSettings};
use jj_lib::signing::Signer;
Expand Down Expand Up @@ -2789,31 +2789,73 @@ fn test_push_branches_unexpectedly_moved_sideways_on_remote() {
let sideways_commit = write_random_commit(tx.mut_repo(), &settings);
setup.jj_repo = tx.commit("test");
let mut tx = setup.jj_repo.start_transaction(&settings);
let index = setup.jj_repo.index();

// The main branch is actually at `initial_commit` on the remote. If we expected
// The main branch is actually at `main_commit` on the remote. If we expected
// it to be at `sideways_commit`, we cannot delete it or move it anywhere else.
// However, "moving" it to the same place it already is is OK, following the
// behavior in `test_merge_ref_targets`.

let targets = GitBranchPushTargets {
branch_updates: vec![(
"main".to_owned(),
BranchPushUpdate {
old_target: Some(sideways_commit.id().clone()),
new_target: None,
let merge_with_main_commit = |r: &RefTarget| {
merge_ref_targets(
index,
r,
&RefTarget::normal(sideways_commit.id().clone()),
&RefTarget::normal(setup.main_commit.id().clone()),
)
};
let assert_merge_resolves_to_ref = |r: &RefTarget| assert_eq!(r, &merge_with_main_commit(r));
let assert_merge_fails_to_resolve_to_ref = |r: &RefTarget| {
let result = merge_with_main_commit(r);
assert_ne!(r, &result);
result
};

let mut attempt_push = |target: Option<CommitId>| {
let targets = GitBranchPushTargets {
branch_updates: vec![(
"main".to_owned(),
BranchPushUpdate {
old_target: Some(sideways_commit.id().clone()),
new_target: target,
},
)],
force_pushed_branches: hashset! {
"main".to_owned(),
},
)],
force_pushed_branches: hashset! {
"main".to_owned(),
},
};
git::push_branches(
tx.mut_repo(),
&get_git_repo(&setup.jj_repo),
"origin",
&targets,
git::RemoteCallbacks::default(),
)
};
let result = git::push_branches(
tx.mut_repo(),
&get_git_repo(&setup.jj_repo),
"origin",
&targets,
git::RemoteCallbacks::default(),

assert_debug_snapshot!(
assert_merge_fails_to_resolve_to_ref(RefTarget::absent_ref()),
@r###"
RefTarget {
merge: Conflicted(
[
None,
Some(
CommitId(
"4a13f358841efe180c2572e1b6a34366d352569c",
),
),
Some(
CommitId(
"9e8d54991333f00d0ed060519d596c59c1b56cba",
),
),
],
),
}
"###
);
let result = attempt_push(None);
assert_debug_snapshot!(result, @r###"
Err(
RefInUnexpectedLocation(
Expand All @@ -2826,25 +2868,69 @@ fn test_push_branches_unexpectedly_moved_sideways_on_remote() {
)
"###);

let targets = GitBranchPushTargets {
branch_updates: vec![(
"main".to_owned(),
BranchPushUpdate {
old_target: Some(sideways_commit.id().clone()),
new_target: Some(setup.child_of_main_commit.id().clone()),
},
)],
force_pushed_branches: hashset! {
"main".to_owned(),
},
};
let result = git::push_branches(
tx.mut_repo(),
&get_git_repo(&setup.jj_repo),
"origin",
&targets,
git::RemoteCallbacks::default(),
assert_debug_snapshot!(
assert_merge_fails_to_resolve_to_ref(&RefTarget::normal(
setup.child_of_main_commit.id().clone(),
)),
@r###"
RefTarget {
merge: Conflicted(
[
Some(
CommitId(
"6085ef63edbd863e7ae6df25cecab9efcf06c4ed",
),
),
Some(
CommitId(
"4a13f358841efe180c2572e1b6a34366d352569c",
),
),
Some(
CommitId(
"9e8d54991333f00d0ed060519d596c59c1b56cba",
),
),
],
),
}
"###
);
let result = attempt_push(Some(setup.child_of_main_commit.id().clone()));
assert_debug_snapshot!(result, @r###"
Err(
RefInUnexpectedLocation(
[
Some(
"refs/heads/main",
),
],
),
)
"###);

// TODO: Too confusing
// Here, we attempt to move the branch from `sideways_commit` to
// `sideways_commit`. It didn't move locally, but it moved to `main` on the
// remote. So, the conflict resolves to `main` and we should not succeed in the
// push.
assert_debug_snapshot!(
assert_merge_fails_to_resolve_to_ref(&RefTarget::normal(
sideways_commit.id().clone()
)),
@r###"
RefTarget {
merge: Resolved(
Some(
CommitId(
"9e8d54991333f00d0ed060519d596c59c1b56cba",
),
),
),
}
"###
);
let result = attempt_push(Some(sideways_commit.id().clone()));
assert_debug_snapshot!(result, @r###"
Err(
RefInUnexpectedLocation(
Expand Down Expand Up @@ -2920,6 +3006,14 @@ fn test_push_branches_unexpectedly_moved_sideways_on_remote() {
"###);

// Moving the branch to the same place it already is is OK.
let result = merge_ref_targets(
index,
&RefTarget::normal(setup.main_commit.id().clone()),
&RefTarget::normal(sideways_commit.id().clone()),
&RefTarget::normal(setup.main_commit.id().clone()),
);
assert_eq!(result, RefTarget::normal(setup.main_commit.id().clone()));
dbg!(result);
let targets = GitBranchPushTargets {
branch_updates: vec![(
"main".to_owned(),
Expand Down

0 comments on commit 8b7e930

Please sign in to comment.