Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: in colocated repo, don't restore view.git_head on undo #2347

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cli/src/commands/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,14 @@ fn view_with_desired_portions_restored(
} else {
current_view.clone()
};
new_view.git_refs = if what.contains(&UndoWhatToRestore::GitTracking) {
view_being_restored.git_refs.clone()

let git_source_view = if what.contains(&UndoWhatToRestore::GitTracking) {
view_being_restored
} else {
current_view.git_refs.clone()
current_view
};
new_view.git_refs = git_source_view.git_refs.clone();
new_view.git_head = git_source_view.git_head.clone();

if what.contains(&UndoWhatToRestore::RemoteTracking) == what.contains(&UndoWhatToRestore::Repo)
{
Expand Down
54 changes: 54 additions & 0 deletions cli/tests/test_git_colocated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,60 @@ fn test_git_colocated_squash_undo() {
"###);
}

#[test]
fn test_git_colocated_undo_head_move() {
let test_env = TestEnvironment::default();
let repo_path = test_env.env_root().join("repo");
let git_repo = git2::Repository::init(&repo_path).unwrap();
test_env.jj_cmd_success(&repo_path, &["init", "--git-repo=."]);

// Create new HEAD
test_env.jj_cmd_success(&repo_path, &["new"]);
insta::assert_snapshot!(
git_repo.head().unwrap().target().unwrap().to_string(),
@"230dd059e1b059aefc0da06a2e5a7dbf22362f22");
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ 65b6b74e08973b88d38404430f119c8c79465250
◉ 230dd059e1b059aefc0da06a2e5a7dbf22362f22 HEAD@git
◉ 0000000000000000000000000000000000000000
"###);

// HEAD should be unset
test_env.jj_cmd_success(&repo_path, &["undo"]);
assert!(git_repo.head().is_err());
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
◉ 0000000000000000000000000000000000000000
"###);

// Create commit on non-root commit
test_env.jj_cmd_success(&repo_path, &["new"]);
test_env.jj_cmd_success(&repo_path, &["new"]);
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ 69b19f73cf584f162f078fb0d91c55ca39d10bc7
◉ eb08b363bb5ef8ee549314260488980d7bbe8f63 HEAD@git
◉ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
◉ 0000000000000000000000000000000000000000
"###);
insta::assert_snapshot!(
git_repo.head().unwrap().target().unwrap().to_string(),
@"eb08b363bb5ef8ee549314260488980d7bbe8f63");

// HEAD should be moved back
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["undo"]), @r###"
Working copy now at: royxmykx eb08b363 (empty) (no description set)
Parent commit : qpvuntsm 230dd059 (empty) (no description set)
"###);
insta::assert_snapshot!(
git_repo.head().unwrap().target().unwrap().to_string(),
@"230dd059e1b059aefc0da06a2e5a7dbf22362f22");
insta::assert_snapshot!(get_log_output(&test_env, &repo_path), @r###"
@ eb08b363bb5ef8ee549314260488980d7bbe8f63
◉ 230dd059e1b059aefc0da06a2e5a7dbf22362f22 HEAD@git
◉ 0000000000000000000000000000000000000000
"###);
}

fn get_log_output_divergence(test_env: &TestEnvironment, repo_path: &Path) -> String {
let template = r#"
separate(" ",
Expand Down