[patch]: rollback apply patch refactoring: move rollback method to PatchApplier; add javadoc; fix typo

This commit is contained in:
Nadya Zabrodina
2016-01-25 17:05:23 +03:00
parent 5f26d4e6f2
commit c24bd12b3a
5 changed files with 52 additions and 52 deletions
@@ -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);
@@ -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;
@@ -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<BinaryType extends FilePatch> {
return !applier.getBinaryPatches().isEmpty();
}
});
UndoApplyPatchDialog.rollbackApplyPatch(project, ContainerUtil.map(allFailed, new Function<FilePatch, FilePath>() {
@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<FilePatch, FilePath>() {
@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());
@@ -222,8 +222,8 @@ public class PathsVerifier<BinaryType extends FilePatch> {
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<BinaryType extends FilePatch> {
}
private void revert(final String errorMessage) {
//todo show notification balloon
PatchApplier.showError(myProject, errorMessage, true);
// move back
@@ -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<FilePath> myFailedFilePaths;
private final Label myBeforeLabel;
private final Project myProject;
private final boolean myShouldInformAboutBinaries;
private UndoApplyPatchDialog(@NotNull Project project,
@NotNull List<FilePath> filePaths,
@NotNull Label beforeLabel,
boolean shouldInformAboutBinaries) {
UndoApplyPatchDialog(@NotNull Project project,
@NotNull List<FilePath> 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.<br>", 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<FilePath> filePaths,
@NotNull Label historyLabel, boolean shouldInformAboutBinaries) {
new UndoApplyPatchDialog(project, filePaths, historyLabel, shouldInformAboutBinaries).show();
}
}