From c2c32ba0dc5397d2f1b56b2fc108de9c2ec451da Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Mon, 4 Jul 2022 23:14:39 -0700 Subject: [PATCH] cli: don't use stale index to look up snapshotted working copy --- src/commands.rs | 20 ++++++++++++++------ tests/test_git_push.rs | 10 ++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index e8db37b7b..0a3b43f48 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -5082,14 +5082,15 @@ fn cmd_git_push( args: &GitPushArgs, ) -> Result<(), CommandError> { let mut workspace_command = command.workspace_helper(ui)?; - let repo = workspace_command.repo().clone(); let mut tx; let mut branch_updates = vec![]; if let Some(branch_name) = &args.branch { - if let Some(update) = - branch_updates_for_push(repo.as_repo_ref(), &args.remote, branch_name)? - { + if let Some(update) = branch_updates_for_push( + workspace_command.repo().as_repo_ref(), + &args.remote, + branch_name, + )? { branch_updates.push((branch_name.clone(), update)); } else { writeln!( @@ -5109,7 +5110,12 @@ fn cmd_git_push( ui.settings().push_branch_prefix(), commit.change_id().hex() ); - if repo.view().get_local_branch(&branch_name).is_none() { + if workspace_command + .repo() + .view() + .get_local_branch(&branch_name) + .is_none() + { writeln!( ui, "Creating branch {} for revision {}", @@ -5136,7 +5142,7 @@ fn cmd_git_push( } } else { // TODO: Is it useful to warn about conflicted branches? - for (branch_name, branch_target) in repo.view().branches() { + for (branch_name, branch_target) in workspace_command.repo().view().branches() { let push_action = classify_branch_push_action(branch_target, &args.remote); match push_action { BranchPushAction::AlreadyMatches => {} @@ -5156,6 +5162,8 @@ fn cmd_git_push( return Ok(()); } + let repo = workspace_command.repo(); + let mut ref_updates = vec![]; let mut new_heads = vec![]; for (branch_name, update) in &branch_updates { diff --git a/tests/test_git_push.rs b/tests/test_git_push.rs index c17fa4d8f..c5aae65ea 100644 --- a/tests/test_git_push.rs +++ b/tests/test_git_push.rs @@ -112,6 +112,16 @@ fn test_git_push_success() { "###); } +#[test] +fn test_git_push_unsnapshotted_change() { + let (test_env, workspace_root) = set_up(); + test_env.jj_cmd_success(&workspace_root, &["describe", "-m", "foo"]); + std::fs::write(workspace_root.join("file"), "contents").unwrap(); + test_env.jj_cmd_success(&workspace_root, &["git", "push", "--change", "@"]); + std::fs::write(workspace_root.join("file"), "modified").unwrap(); + test_env.jj_cmd_success(&workspace_root, &["git", "push", "--change", "@"]); +} + #[test] fn test_git_push_conflict() { let (test_env, workspace_root) = set_up();