From 367311a66cc05275b83bbf55ae5da272020f0e44 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Mon, 1 Oct 2018 19:27:58 +0300 Subject: [PATCH] git: re-check protected branches before rewording commit follow-up: 24e4b6da70c2d24914bdae095818d85408f84082 --- .../git4idea/rebase/GitCommitEditingAction.kt | 2 +- .../src/git4idea/rebase/GitRewordAction.kt | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/plugins/git4idea/src/git4idea/rebase/GitCommitEditingAction.kt b/plugins/git4idea/src/git4idea/rebase/GitCommitEditingAction.kt index 877fbf38eb36..109b07719836 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitCommitEditingAction.kt +++ b/plugins/git4idea/src/git4idea/rebase/GitCommitEditingAction.kt @@ -135,7 +135,7 @@ abstract class GitCommitEditingAction : DumbAwareAction() { }, "Searching for branches containing the selected commit", true, data.project) } - private fun commitPushedToProtectedBranchError(protectedBranch: String) + protected fun commitPushedToProtectedBranchError(protectedBranch: String) = "The commit is already pushed to protected branch '$protectedBranch'" protected fun prohibitRebaseDuringRebase(e: AnActionEvent, operation: String, allowRebaseIfHeadCommit: Boolean = false) { diff --git a/plugins/git4idea/src/git4idea/rebase/GitRewordAction.kt b/plugins/git4idea/src/git4idea/rebase/GitRewordAction.kt index 418d2ab78b25..3d564734df94 100644 --- a/plugins/git4idea/src/git4idea/rebase/GitRewordAction.kt +++ b/plugins/git4idea/src/git4idea/rebase/GitRewordAction.kt @@ -27,6 +27,7 @@ import com.intellij.openapi.progress.Task import com.intellij.openapi.project.Project import com.intellij.openapi.ui.DialogWrapper import com.intellij.openapi.ui.ValidationInfo +import com.intellij.openapi.util.Disposer import com.intellij.openapi.vcs.VcsException import com.intellij.openapi.vcs.VcsNotifier import com.intellij.openapi.vcs.ui.CommitMessage @@ -40,6 +41,7 @@ import com.intellij.vcs.log.data.VcsLogData import com.intellij.vcs.log.impl.VcsCommitMetadataImpl import com.intellij.vcs.log.util.VcsLogUtil import com.intellij.vcs.log.util.VcsUserUtil.getShortPresentation +import git4idea.findProtectedRemoteBranch import git4idea.repo.GitRepository private val LOG: Logger = logger() @@ -58,7 +60,7 @@ class GitRewordAction : GitCommitEditingAction() { val repository = getRepository(e) val details = getOrLoadDetails(project, getLogData(e), commit) - RewordDialog(project, details, repository).show() + RewordDialog(project, getLogData(e), details, repository).show() } private fun getOrLoadDetails(project: Project, data: VcsLogData, commit: VcsShortCommitDetails): VcsCommitMetadata { @@ -107,7 +109,7 @@ class GitRewordAction : GitCommitEditingAction() { }.queue() } - private inner class RewordDialog(val project: Project, val commit: VcsCommitMetadata, val repository: GitRepository) + private inner class RewordDialog(val project: Project, val data: VcsLogData, val commit: VcsCommitMetadata, val repository: GitRepository) : DialogWrapper(project, true) { val originalHEAD = repository.info.currentRevision @@ -143,9 +145,17 @@ class GitRewordAction : GitCommitEditingAction() { } override fun doValidate(): ValidationInfo? { - if (repository.info.currentRevision != originalHEAD) { + if (repository.info.currentRevision != originalHEAD || + Disposer.isDisposed(data)) { return ValidationInfo("Can't reword commit: repository state was changed") } + + val branches = findContainingBranches(data, commit.root, commit.id) + val protectedBranch = findProtectedRemoteBranch(repository, branches) + if (protectedBranch != null) { + return ValidationInfo("Can't reword commit: " + commitPushedToProtectedBranchError(protectedBranch)) + } + return null }