From c24bd12b3ac498cf32f0025b24b7fbcc04a6a10d Mon Sep 17 00:00:00 2001 From: Nadya Zabrodina Date: Thu, 21 Jan 2016 17:16:51 +0300 Subject: [PATCH] [patch]: rollback apply patch refactoring: move rollback method to PatchApplier; add javadoc; fix typo --- .../src/com/intellij/history/Label.java | 6 +++ .../history/integration/LocalHistoryUtil.java | 2 +- .../diff/impl/patch/formove/PatchApplier.java | 46 +++++++++++++++---- .../impl/patch/formove/PathsVerifier.java | 5 +- .../patch/formove/UndoApplyPatchDialog.java | 45 +++--------------- 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/platform/lvcs-api/src/com/intellij/history/Label.java b/platform/lvcs-api/src/com/intellij/history/Label.java index 8abe4f7fb9c7..20bfcf9f4f72 100644 --- a/platform/lvcs-api/src/com/intellij/history/Label.java +++ b/platform/lvcs-api/src/com/intellij/history/Label.java @@ -32,6 +32,12 @@ public interface Label { } }; + /** + * Revert all changes up to this Label according to the local history + * + * @param file file or directory that should be reverted + * @throws LocalHistoryException + */ void revert(@NotNull Project project, @NotNull VirtualFile file) throws LocalHistoryException; ByteContent getByteContent(String path); diff --git a/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryUtil.java b/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryUtil.java index 6858f3dd2315..ed56a4f1961d 100644 --- a/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryUtil.java +++ b/platform/lvcs-impl/src/com/intellij/history/integration/LocalHistoryUtil.java @@ -33,7 +33,7 @@ public class LocalHistoryUtil { for (int i = 0; i < revs.size(); i++) { final RevisionItem rev = revs.get(i); if (isLabelRevision(rev, label)) return i; - //when lvcs model not constructed yet or empty then putLableChange created but without label, so we need to scan revisions themselves + //when lvcs model is not constructed yet or is empty then PutLabelChange is created but without label, so we need to scan revisions themselves if (isChangeWithId(rev.revision, label.getLabelChangeId())) return i; } return -1; diff --git a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/PatchApplier.java b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/PatchApplier.java index f0749e1c3a75..46cc1866ffd5 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/PatchApplier.java +++ b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/PatchApplier.java @@ -17,6 +17,7 @@ package com.intellij.openapi.diff.impl.patch.formove; import com.intellij.history.Label; import com.intellij.history.LocalHistory; +import com.intellij.history.LocalHistoryException; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.CommandProcessor; @@ -25,6 +26,7 @@ import com.intellij.openapi.diff.impl.patch.ApplyPatchStatus; import com.intellij.openapi.diff.impl.patch.FilePatch; import com.intellij.openapi.diff.impl.patch.apply.ApplyFilePatchBase; import com.intellij.openapi.diff.impl.patch.apply.ApplyTextFilePatch; +import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.MessageType; import com.intellij.openapi.ui.Messages; @@ -313,18 +315,42 @@ public class PatchApplier { return !applier.getBinaryPatches().isEmpty(); } }); - UndoApplyPatchDialog.rollbackApplyPatch(project, ContainerUtil.map(allFailed, new Function() { - @Override - public FilePath fun(FilePatch filePatch) { - String path = - filePatch.getAfterName() == null - ? filePatch.getBeforeName() - : filePatch.getAfterName(); - return VcsUtil.getFilePath(path); - } - }), beforeLabel, shouldInformAboutBinaries); + final UndoApplyPatchDialog undoApplyPatchDialog = + new UndoApplyPatchDialog(project, ContainerUtil.map(allFailed, new Function() { + @Override + public FilePath fun(FilePatch filePatch) { + String path = + filePatch.getAfterName() == null + ? filePatch.getBeforeName() + : filePatch.getAfterName(); + return VcsUtil.getFilePath(path); + } + }), shouldInformAboutBinaries); + undoApplyPatchDialog.show(); + if (undoApplyPatchDialog.isOK()) { + rollbackUnderProgress(project, project.getBaseDir(), beforeLabel); + } } + private static void rollbackUnderProgress(@NotNull final Project project, + @NotNull final VirtualFile virtualFile, + @NotNull final Label labelToRevert) { + ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() { + @Override + public void run() { + try { + labelToRevert.revert(project, virtualFile); + } + catch (LocalHistoryException e) { + VcsNotifier.getInstance(project) + .notifyImportantWarning("Rollback Failed", String.format("Try using local history dialog for %s and perform revert manually.", + virtualFile.getName())); + } + } + }, "Rollback Applied Changes...", true, project); + } + + protected void addSkippedItems(final TriggerAdditionOrDeletion trigger) { trigger.addExisting(myVerifier.getToBeAdded()); trigger.addDeleted(myVerifier.getToBeDeleted()); diff --git a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/PathsVerifier.java b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/PathsVerifier.java index 29f6b322a2b6..4eb782bacb93 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/PathsVerifier.java +++ b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/PathsVerifier.java @@ -222,8 +222,8 @@ public class PathsVerifier { fileType = FileTypeChooser.associateFileType(file.getName()); if (fileType == null) { PatchApplier - .showError(myProject, - "Cannot apply content for " + file.getPresentableName() + " file from patch because its type not defined.", true); + .showError(myProject, "Cannot apply content for " + file.getPresentableName() + " file from patch because its type not defined.", + true); return false; } } @@ -420,7 +420,6 @@ public class PathsVerifier { } private void revert(final String errorMessage) { - //todo show notification balloon PatchApplier.showError(myProject, errorMessage, true); // move back diff --git a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/UndoApplyPatchDialog.java b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/UndoApplyPatchDialog.java index 3d8209264308..b01a231f3622 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/UndoApplyPatchDialog.java +++ b/platform/vcs-impl/src/com/intellij/openapi/diff/impl/patch/formove/UndoApplyPatchDialog.java @@ -15,18 +15,14 @@ */ package com.intellij.openapi.diff.impl.patch.formove; -import com.intellij.history.Label; -import com.intellij.history.LocalHistoryException; -import com.intellij.openapi.progress.ProgressManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vcs.FilePath; -import com.intellij.openapi.vcs.VcsNotifier; import com.intellij.openapi.vcs.changes.ui.FilePathChangesTreeList; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.ui.components.JBLabel; import com.intellij.util.ui.UIUtil; +import com.intellij.xml.util.XmlStringUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -38,20 +34,17 @@ class UndoApplyPatchDialog extends DialogWrapper { private final List myFailedFilePaths; - private final Label myBeforeLabel; private final Project myProject; private final boolean myShouldInformAboutBinaries; - private UndoApplyPatchDialog(@NotNull Project project, - @NotNull List filePaths, - @NotNull Label beforeLabel, - boolean shouldInformAboutBinaries) { + UndoApplyPatchDialog(@NotNull Project project, + @NotNull List filePaths, + boolean shouldInformAboutBinaries) { super(project, true); myProject = project; setTitle("Patch Applying Partly Failed"); setOKButtonText("Rollback"); myFailedFilePaths = filePaths; - myBeforeLabel = beforeLabel; myShouldInformAboutBinaries = shouldInformAboutBinaries; init(); } @@ -62,11 +55,11 @@ class UndoApplyPatchDialog extends DialogWrapper { final JPanel panel = new JPanel(new BorderLayout()); int numFiles = myFailedFilePaths.size(); JPanel labelsPanel = new JPanel(new BorderLayout()); - String detailedText = numFiles == 0 ? "" : String.format("Failed to apply %s below. ", StringUtil.pluralize("file", numFiles)); - final JLabel infoLabel = new JBLabel(detailedText + "Would you like to rollback all applied?"); + String detailedText = numFiles == 0 ? "" : String.format("Failed to apply %s below.
", StringUtil.pluralize("file", numFiles)); + final JLabel infoLabel = new JBLabel(XmlStringUtil.wrapInHtml(detailedText + "Would you like to rollback all applied?")); labelsPanel.add(infoLabel, BorderLayout.NORTH); if (myShouldInformAboutBinaries) { - JLabel warningLabel = new JLabel("Rollback doesn't affect binaries"); + JLabel warningLabel = new JLabel("Rollback will not affect binaries"); warningLabel.setIcon(UIUtil.getBalloonWarningIcon()); labelsPanel.add(warningLabel, BorderLayout.CENTER); } @@ -83,28 +76,4 @@ class UndoApplyPatchDialog extends DialogWrapper { } return panel; } - - @Override - protected void doOKAction() { - super.doOKAction(); - ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() { - @Override - public void run() { - final VirtualFile baseDir = myProject.getBaseDir(); - try { - myBeforeLabel.revert(myProject, baseDir); - } - catch (LocalHistoryException e) { - VcsNotifier.getInstance(myProject) - .notifyImportantWarning("Rollback Failed", String.format("Try to use local history dialog for %s and perform revert manually.", - baseDir.getName())); - } - } - }, "Rollback Applied Changes...", true, myProject); - } - - static void rollbackApplyPatch(@NotNull Project project, @NotNull List filePaths, - @NotNull Label historyLabel, boolean shouldInformAboutBinaries) { - new UndoApplyPatchDialog(project, filePaths, historyLabel, shouldInformAboutBinaries).show(); - } }