use std::path::Path;
use crate::common::TestEnvironment;
#[test]
fn test_undo_rewrite_with_child() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "initial"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "modified"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["op", "log"]);
let op_id_hex = stdout[3..15].to_string();
test_env.jj_cmd_ok(&repo_path, &["new", "-m", "child"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description"]);
insta::assert_snapshot!(stdout, @r###"
@ child
â—‰ modified
â—‰
"###);
test_env.jj_cmd_ok(&repo_path, &["undo", &op_id_hex]);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description"]);
insta::assert_snapshot!(stdout, @r###"
@ child
â—‰ initial
â—‰
"###);
}
#[test]
fn test_git_push_undo() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init_bare(git_repo_path).unwrap();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "clone", "git-repo", "repo"]);
let repo_path = test_env.env_root().join("repo");
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "AA"]);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "BB"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 0cffb614 (empty) AA
"###);
let pre_push_opid = test_env.current_operation_id(&repo_path);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin: qpvuntsm 8c05de15 (empty) BB
"###);
test_env.jj_cmd_ok(&repo_path, &["op", "restore", &pre_push_opid]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 0cffb614 (empty) AA
"###);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "CC"]);
test_env.jj_cmd_ok(&repo_path, &["git", "fetch"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main (conflicted):
- qpvuntsm hidden 0cffb614 (empty) AA
+ qpvuntsm?? 0a3e99f0 (empty) CC
+ qpvuntsm?? 8c05de15 (empty) BB
@origin (behind by 1 commits): qpvuntsm?? 8c05de15 (empty) BB
"###);
}
#[test]
fn test_git_push_undo_with_import() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init_bare(git_repo_path).unwrap();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "clone", "git-repo", "repo"]);
let repo_path = test_env.env_root().join("repo");
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "AA"]);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "BB"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 0cffb614 (empty) AA
"###);
let pre_push_opid = test_env.current_operation_id(&repo_path);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin: qpvuntsm 8c05de15 (empty) BB
"###);
test_env.jj_cmd_ok(&repo_path, &["op", "restore", &pre_push_opid]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 0cffb614 (empty) AA
"###);
test_env.jj_cmd_ok(&repo_path, &["git", "import"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin: qpvuntsm 8c05de15 (empty) BB
"###);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "CC"]);
test_env.jj_cmd_ok(&repo_path, &["git", "fetch"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 0a3e99f0 (empty) CC
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 8c05de15 (empty) BB
"###);
}
#[test]
fn test_git_push_undo_colocated() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init_bare(git_repo_path.clone()).unwrap();
let repo_path = test_env.env_root().join("clone");
git2::Repository::clone(git_repo_path.to_str().unwrap(), &repo_path).unwrap();
test_env.jj_cmd_ok(&repo_path, &["init", "--git-repo=."]);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "AA"]);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "BB"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@git: qpvuntsm 8c05de15 (empty) BB
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 0cffb614 (empty) AA
"###);
let pre_push_opid = test_env.current_operation_id(&repo_path);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@git: qpvuntsm 8c05de15 (empty) BB
@origin: qpvuntsm 8c05de15 (empty) BB
"###);
test_env.jj_cmd_ok(&repo_path, &["op", "restore", &pre_push_opid]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@git: qpvuntsm 8c05de15 (empty) BB
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 0cffb614 (empty) AA
"###);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "CC"]);
test_env.jj_cmd_ok(&repo_path, &["git", "fetch"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main (conflicted):
- qpvuntsm hidden 0cffb614 (empty) AA
+ qpvuntsm?? 0a3e99f0 (empty) CC
+ qpvuntsm?? 8c05de15 (empty) BB
@git (behind by 1 commits): qpvuntsm?? 0a3e99f0 (empty) CC
@origin (behind by 1 commits): qpvuntsm?? 8c05de15 (empty) BB
"###);
}
#[test]
fn test_git_push_undo_repo_only() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init_bare(git_repo_path).unwrap();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "clone", "git-repo", "repo"]);
let repo_path = test_env.env_root().join("repo");
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "main"]);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "AA"]);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 0cffb614 (empty) AA
@origin: qpvuntsm 0cffb614 (empty) AA
"###);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "BB"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 0cffb614 (empty) AA
"###);
let pre_push_opid = test_env.current_operation_id(&repo_path);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
test_env.jj_cmd_ok(
&repo_path,
&["op", "restore", "--what=repo", &pre_push_opid],
);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 8c05de15 (empty) BB
@origin: qpvuntsm 8c05de15 (empty) BB
"###);
test_env.advance_test_rng_seed_to_multiple_of(100_000);
test_env.jj_cmd_ok(&repo_path, &["describe", "-m", "CC"]);
test_env.jj_cmd_ok(&repo_path, &["git", "fetch"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
main: qpvuntsm 0a3e99f0 (empty) CC
@origin (ahead by 1 commits, behind by 1 commits): qpvuntsm hidden 8c05de15 (empty) BB
"###);
}
#[test]
fn test_branch_track_untrack_undo() {
let test_env = TestEnvironment::default();
test_env.add_config(r#"revset-aliases."immutable_heads()" = "none()""#);
let git_repo_path = test_env.env_root().join("git-repo");
git2::Repository::init_bare(git_repo_path).unwrap();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "clone", "git-repo", "repo"]);
let repo_path = test_env.env_root().join("repo");
test_env.jj_cmd_ok(&repo_path, &["describe", "-mcommit"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "create", "feature1", "feature2"]);
test_env.jj_cmd_ok(&repo_path, &["git", "push"]);
test_env.jj_cmd_ok(&repo_path, &["branch", "delete", "feature2"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
feature1: qpvuntsm 270721f5 (empty) commit
@origin: qpvuntsm 270721f5 (empty) commit
feature2 (deleted)
@origin: qpvuntsm 270721f5 (empty) commit
(this branch will be *deleted permanently* on the remote on the next `jj git push`. Use `jj branch forget` to prevent this)
"###);
test_env.jj_cmd_ok(
&repo_path,
&["branch", "untrack", "feature1@origin", "feature2@origin"],
);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
feature1: qpvuntsm 270721f5 (empty) commit
feature1@origin: qpvuntsm 270721f5 (empty) commit
feature2@origin: qpvuntsm 270721f5 (empty) commit
"###);
test_env.jj_cmd_ok(&repo_path, &["undo"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
feature1: qpvuntsm 270721f5 (empty) commit
@origin: qpvuntsm 270721f5 (empty) commit
feature2 (deleted)
@origin: qpvuntsm 270721f5 (empty) commit
(this branch will be *deleted permanently* on the remote on the next `jj git push`. Use `jj branch forget` to prevent this)
"###);
test_env.jj_cmd_ok(&repo_path, &["undo"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
feature1: qpvuntsm 270721f5 (empty) commit
feature1@origin: qpvuntsm 270721f5 (empty) commit
feature2@origin: qpvuntsm 270721f5 (empty) commit
"###);
test_env.jj_cmd_ok(&repo_path, &["branch", "track", "feature1@origin"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
feature1: qpvuntsm 270721f5 (empty) commit
@origin: qpvuntsm 270721f5 (empty) commit
feature2@origin: qpvuntsm 270721f5 (empty) commit
"###);
test_env.jj_cmd_ok(&repo_path, &["undo"]);
insta::assert_snapshot!(get_branch_output(&test_env, &repo_path), @r###"
feature1: qpvuntsm 270721f5 (empty) commit
feature1@origin: qpvuntsm 270721f5 (empty) commit
feature2@origin: qpvuntsm 270721f5 (empty) commit
"###);
}
fn get_branch_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(repo_path, &["branch", "list", "--all"])
}