From ab484fe1170cdd4c0604349e520630073dae4291 Mon Sep 17 00:00:00 2001 From: irengrig Date: Wed, 4 Nov 2009 18:14:38 +0300 Subject: [PATCH] IDEADEV-41182 (ApplyPatch: ShowDiff for file that is not found causes exception) - disable show diff if there'no no "add" changes of changes with base found --- .../patch/ApplyPatchDifferentiatedDialog.java | 110 ++++----------------- 1 file changed, 17 insertions(+), 93 deletions(-) diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java index 259933e1db..8b76ed94c3 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/ApplyPatchDifferentiatedDialog.java @@ -86,6 +86,8 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { private CommitLegendPanel myCommitLegendPanel; private final Consumer myCallback; + private boolean myContainBasedChanges; + public ApplyPatchDifferentiatedDialog(final Project project, final Consumer callback) { super(project, true); myCallback = callback; @@ -332,6 +334,7 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { myPatches.clear(); myChangesTreeList.setChangesToDisplay(Collections.emptyList()); myChangesTreeList.repaint(); + myContainBasedChanges = false; } @Override @@ -395,97 +398,6 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { return myCenterPanel; } - // create change - /*private void showDiff() { - List changes = new ArrayList(); - ApplyPatchContext context = getApplyPatchContext().getPrepareContext(); - Object[] selection = myPatchContentsList.getSelectedValues(); - if (selection.length == 0) { - if (myPatches == null) return; - selection = ArrayUtil.toObjectArray(myPatches); - } - for(Object o: selection) { - final TextFilePatch patch = (TextFilePatch) o; - try { - if (patch.isNewFile()) { - final FilePath newFilePath = FilePathImpl.createNonLocal(patch.getAfterName(), false); - final String content = patch.getNewFileText(); - ContentRevision revision = new SimpleContentRevision(content, newFilePath, patch.getAfterVersionId()); - changes.add(new Change(null, revision)); - } else if ((! patch.isDeletedFile()) && (patch.getBeforeName() != null) && (patch.getAfterName() != null) && - (! patch.getBeforeName().equals(patch.getAfterName()))) { - - final VirtualFile baseDirectory = getBaseDirectory(); - final VirtualFile beforeFile = PatchApplier.getFile(baseDirectory, patch.getBeforeName()); - - if (beforeFile != null) { - final List tail = new ArrayList(); - final VirtualFile partFile = PatchApplier.getFile(baseDirectory, patch.getAfterName(), tail); - final StringBuilder sb = new StringBuilder(partFile.getPath()); - for (String s : tail) { - if (sb.charAt(sb.length() - 1) != '/') { - sb.append('/'); - } - sb.append(s); - } - - final Change change = - changeForPath(beforeFile, patch, FilePathImpl.createNonLocal(FileUtil.toSystemIndependentName(sb.toString()), false)); - if (change != null) { - changes.add(change); - } - } else { - Messages.showErrorDialog(myProject, "Cannot show difference: cannot find file " + patch.getBeforeName(), - VcsBundle.message("patch.apply.dialog.title")); - } - } - else { - final VirtualFile fileToPatch = patch.findFileToPatch(context); - if (fileToPatch != null) { - final FilePathImpl filePath = new FilePathImpl(fileToPatch); - final CurrentContentRevision currentRevision = new CurrentContentRevision(filePath); - if (patch.isDeletedFile()) { - changes.add(new Change(currentRevision, null)); - } - else { - final Change change = changeForPath(fileToPatch, patch, null); - if (change != null) { - changes.add(change); - } - } - } - } - } - catch (Exception e) { - Messages.showErrorDialog(myProject, "Error loading changes for " + patch.getAfterFileName() + ": " + e.getMessage(), - VcsBundle.message("patch.apply.dialog.title")); - return; - } - } - ShowDiffAction.showDiffForChange(changes.toArray(new Change[changes.size()]), 0, myProject, - ShowDiffAction.DiffExtendUIFactory.NONE, false); - } - - @Nullable - private Change changeForPath(final VirtualFile fileToPatch, final TextFilePatch patch, final FilePath newFilePath) { - try { - final FilePathImpl filePath = new FilePathImpl(fileToPatch); - final CurrentContentRevision currentRevision = new CurrentContentRevision(filePath); - final Document doc = FileDocumentManager.getInstance().getDocument(fileToPatch); - String baseContent = doc.getText(); - StringBuilder newText = new StringBuilder(); - patch.applyModifications(baseContent, newText); - ContentRevision revision = new SimpleContentRevision(newText.toString(), (newFilePath == null) ? filePath : newFilePath, patch.getAfterVersionId()); - return new Change(currentRevision, revision); - } catch (ApplyPatchException e) { - ApplyPatchContext context = new ApplyPatchContext(getBaseDirectory(), 0, false, false); - // just show diff here. maybe refactor further.. - ApplyPatchAction.mergeAgainstBaseVersion(myProject, fileToPatch, context, patch, ApplyPatchAction.ApplyPatchMergeRequestFactory.INSTANCE_READ_ONLY); - return null; - } - } */ - - private static class MyChangeTreeList extends ChangesTreeList { private MyChangeTreeList(Project project, Collection initiallyIncluded, @@ -576,6 +488,14 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { myChangesTreeList.setChangesToDisplay(changes); myChangesTreeList.setIncludedChanges(included); myChangesTreeList.repaint(); + + myContainBasedChanges = false; + for (FilePatchInProgress patch : myPatches) { + if (patch.baseExistsOrAdded()) { + myContainBasedChanges = true; + break; + } + } } private List getAllChanges() { @@ -820,23 +740,27 @@ public class ApplyPatchDifferentiatedDialog extends DialogWrapper { } public void update(AnActionEvent e) { - e.getPresentation().setEnabled(! myPatches.isEmpty()); + e.getPresentation().setEnabled((! myPatches.isEmpty()) && myContainBasedChanges); } public void actionPerformed(AnActionEvent e) { - if (myPatches.isEmpty()) return; + if (myPatches.isEmpty() || (! myContainBasedChanges)) return; final List changes = getAllChanges(); final List selectedChanges = myChangesTreeList.getSelectedChanges(); int idx = 0; + boolean goodChange = false; if (! selectedChanges.isEmpty()) { final FilePatchInProgress.PatchChange c = selectedChanges.get(0); for (FilePatchInProgress.PatchChange change : changes) { + if (! change.getPatchInProgress().baseExistsOrAdded()) continue; + goodChange = true; if (change.equals(c)) { break; } ++ idx; } } + if (! goodChange) return; idx = (idx == changes.size()) ? 0 : idx; ShowDiffAction.showDiffForChange(changes.toArray(new Change[changes.size()]), idx, myProject, ShowDiffAction.DiffExtendUIFactory.NONE, false); -- 2.11.4.GIT