Skip to content

Commit

Permalink
Allow fastworwards that break the lease
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyagr committed Apr 19, 2024
1 parent 81b11aa commit 068f97a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
24 changes: 10 additions & 14 deletions cli/tests/test_git_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ fn test_git_push_other_remote_has_branch() {
"###);
// The branch was moved on the "other" remote as well (since it's actually the
// same remote), but `jj` is not aware of that since it thinks this is a
// different remote. So, the push fails.
let stderr = test_env.jj_cmd_failure(&workspace_root, &["git", "push", "--remote=other"]);
// different remote. So, the push should fail.
//
// But it succeeds! That's because the branch is created at the same location as
// it is on the remote. This would also work for a descendant.
let (stdout, stderr) = test_env.jj_cmd_ok(&workspace_root, &["git", "push", "--remote=other"]);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r###"
Branch changes to push to other:
Add branch branch1 to d73041288714
Error: Refusing to push a branch that unexpectedly moved on the remote. Affected refs: ["refs/heads/branch1"]
Hint: Try fetching from the remote, then make the branch point to where you want it to be, and push again.
"###);
}

Expand Down Expand Up @@ -498,17 +500,14 @@ fn test_git_push_fastforward_but_unexpectedly_moved_sideways_on_remote() {
insta::assert_snapshot!(get_stderr_string(&assert), @"");
test_env.jj_cmd_ok(&origin_path, &["git", "export"]);

// BUG?
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.jj_cmd(&workspace_root, &["git", "push", "-bbranch1"])
.assert()
.failure();
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"");
insta::assert_snapshot!(get_stderr_string(&assert), @r###"
Branch changes to push to origin:
Force branch branch1 from a0c93395c0a3 to 7a722cf0a4d8
Error: Refusing to push a branch that unexpectedly moved on the remote. Affected refs: ["refs/heads/branch1"]
Hint: Try fetching from the remote, then make the branch point to where you want it to be, and push again.
"###);
}

Expand Down Expand Up @@ -626,17 +625,14 @@ fn test_git_push_fastforward_but_unexpectedly_moved_up_on_remote() {
insta::assert_snapshot!(get_stderr_string(&assert), @"");
test_env.jj_cmd_ok(&origin_path, &["git", "export"]);

// BUG?
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.jj_cmd(&workspace_root, &["git", "push", "-bbranch1"])
.assert()
.failure();
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"");
insta::assert_snapshot!(get_stderr_string(&assert), @r###"
Branch changes to push to origin:
Move branch branch1 from 8144f454db0f to 9a0388ceac79
Error: Refusing to push a branch that unexpectedly moved on the remote. Affected refs: ["refs/heads/branch1"]
Hint: Try fetching from the remote, then make the branch point to where you want it to be, and push again.
"###);
}

Expand Down
12 changes: 11 additions & 1 deletion lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,7 @@ fn push_refs(
update.src_refname(), // Position to update the branch to (!)
update.dst_refname(), // Branch to update
update.dst(), // Commit id of `src_refname` (!)
update.src(), // Expected current position
update.src(), // Current position
&expected_location
); */
// See also https://github.com/git/git/blob/master/templates/hooks--pre-push.sample
Expand All @@ -1371,6 +1371,16 @@ fn push_refs(
/* TODO: Check if src and src_refname point to different things, if not this is a
* no-op */
{
if update.src() == update.dst()
|| git_repo
.graph_descendant_of(update.dst(), update.src())
.unwrap_or(false)
{
// The Git remote would consider this a fast-forward regardless of where our
// remote-tracking branch points. See tests with names that start with
// `test_git_push_fastforward_but_unexpectedly_moved`
continue;
}
failed_push_negotiations_ref.push(update.dst_refname().map(|s| s.to_string()));
}
}
Expand Down

0 comments on commit 068f97a

Please sign in to comment.