jj/tests/test_git_push.rs
Martin von Zweigbergk cdc7a0c242 cli: when pushing all branches (the default), skip open commit
Open commits are work-in-progress and `jj git push --branch <name>`
therefore errors out if the branch points to an open commit. However,
we don't do the same check if you run `jj git push` to push all
branches. This patch introduces such a check. Rather than error out,
we skip such branches instead.

I didn't make it check for open commits in ancestors. It's quite
unusual (at least in my workflow) to have a closed commit on top of an
open one. We can revisit if users get surprised by it.
2022-03-12 10:46:58 -08:00

93 lines
3 KiB
Rust

// Copyright 2022 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 jujutsu::testutils::{get_stdout_string, TestEnvironment};
#[test]
fn test_git_push() {
let test_env = TestEnvironment::default();
let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init(&git_repo_path).unwrap();
test_env
.jj_cmd(
test_env.env_root(),
&["git", "clone", "git-repo", "jj-repo"],
)
.assert()
.success();
let workspace_root = test_env.env_root().join("jj-repo");
// No branches to push yet
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @"Nothing changed.
");
// When pushing everything, won't push an open commit even if there's a branch
// on it
test_env
.jj_cmd(&workspace_root, &["branch", "my-branch"])
.assert()
.success();
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.assert()
.success();
insta::assert_snapshot!(get_stdout_string(&assert), @r###"
Skipping branch 'my-branch' since it points to an open commit.
Nothing changed.
"###);
// When pushing a specific branch, won't push it if it points to an open commit
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push", "--branch", "my-branch"])
.assert()
.failure();
insta::assert_snapshot!(get_stdout_string(&assert), @"Error: Won't push open commit
");
// Try pushing a conflict
std::fs::write(workspace_root.join("file"), "first").unwrap();
test_env
.jj_cmd(&workspace_root, &["close", "-m", "first"])
.assert()
.success();
std::fs::write(workspace_root.join("file"), "second").unwrap();
test_env
.jj_cmd(&workspace_root, &["close", "-m", "second"])
.assert()
.success();
std::fs::write(workspace_root.join("file"), "third").unwrap();
test_env
.jj_cmd(&workspace_root, &["rebase", "-d", "@--"])
.assert()
.success();
test_env
.jj_cmd(&workspace_root, &["branch", "my-branch"])
.assert()
.success();
test_env
.jj_cmd(&workspace_root, &["close", "-m", "third"])
.assert()
.success();
let assert = test_env
.jj_cmd(&workspace_root, &["git", "push"])
.assert()
.failure();
insta::assert_snapshot!(get_stdout_string(&assert), @"Error: Won't push commit 28b5642cb786 since it has conflicts
");
}