diff --git a/plugins/git4idea/src/git4idea/branch/GitCheckoutOperation.java b/plugins/git4idea/src/git4idea/branch/GitCheckoutOperation.java index 07cd7fd94f62..a38ce8ae9b47 100644 --- a/plugins/git4idea/src/git4idea/branch/GitCheckoutOperation.java +++ b/plugins/git4idea/src/git4idea/branch/GitCheckoutOperation.java @@ -21,6 +21,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Pair; import com.intellij.openapi.vcs.changes.Change; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.ArrayUtil; import git4idea.GitPlatformFacade; import git4idea.GitVcs; import git4idea.commands.*; @@ -129,7 +130,12 @@ class GitCheckoutOperation extends GitBranchOperation { } } else if (smartCheckoutDecision == GitSmartOperationDialog.FORCE_EXIT_CODE) { - return checkoutOrNotify(allConflictingRepositories, myStartPointReference, myNewBranch, true); + boolean forceCheckoutSucceeded = checkoutOrNotify(allConflictingRepositories, myStartPointReference, myNewBranch, true); + if (forceCheckoutSucceeded) { + markSuccessful(ArrayUtil.toObjectArray(allConflictingRepositories, GitRepository.class)); + refresh(ArrayUtil.toObjectArray(allConflictingRepositories, GitRepository.class)); + } + return forceCheckoutSucceeded; } else { fatalLocalChangesError(myStartPointReference); diff --git a/plugins/git4idea/tests/git4idea/branch/GitBranchWorkerTest.groovy b/plugins/git4idea/tests/git4idea/branch/GitBranchWorkerTest.groovy index ca53c8066718..e115beeaf710 100644 --- a/plugins/git4idea/tests/git4idea/branch/GitBranchWorkerTest.groovy +++ b/plugins/git4idea/tests/git4idea/branch/GitBranchWorkerTest.groovy @@ -412,6 +412,29 @@ class GitBranchWorkerTest extends GitLightTest { assertNotNull "Rollback proposal was not shown", rollbackMsg } + @Test + void "Force checkout in case of local changes that would be overwritten by checkout"() { + // IDEA-99849 + prepareLocalChangesOverwrittenBy(myUltimate) + + String errorMessage = null; + String successMessage = null; + def uiHandler = [ + showSmartOperationDialog: { Project p, List cs, String op, boolean force -> + GitSmartOperationDialog.FORCE_EXIT_CODE; + }, + notifySuccess: { String message -> successMessage = message }, + notifyError: { String title, String message -> errorMessage = message } + ] as GitBranchUiHandler + GitBranchWorker brancher = new GitBranchWorker(myProject, myPlatformFacade, myGit, uiHandler) + brancher.checkoutNewBranchStartingFrom("new_branch", "feature", myRepositories) + + assertNull("Error notification is unexpected, but was: $errorMessage", errorMessage) + assertEquals("Notification about successful branch creation is incorrect", + "Checked out new branch new_branch from feature", successMessage) + assertCurrentBranch("new_branch") + } + @Test public void "rollback of 'checkout branch as new branch' should delete branches"() { branchWithCommit(myRepositories, "feature")