Search in sources :

Example 1 with MergeResult

use of com.intellij.diff.merge.MergeResult in project intellij-community by JetBrains.

the class ApplyPatchAction method applyOnly.

@NotNull
public static ApplyPatchStatus applyOnly(@Nullable final Project project, @NotNull final ApplyFilePatchBase patch, @Nullable final ApplyPatchContext context, @NotNull final VirtualFile file, @Nullable final CommitContext commitContext, boolean reverse, @Nullable String leftPanelTitle, @Nullable String rightPanelTitle) {
    final ApplyFilePatch.Result result = tryApplyPatch(project, patch, context, file, commitContext);
    final ApplyPatchStatus status = result.getStatus();
    if (ApplyPatchStatus.ALREADY_APPLIED.equals(status) || ApplyPatchStatus.SUCCESS.equals(status)) {
        return status;
    }
    final ApplyPatchForBaseRevisionTexts mergeData = result.getMergeData();
    if (mergeData == null)
        return status;
    final Document document = FileDocumentManager.getInstance().getDocument(file);
    if (document == null)
        return ApplyPatchStatus.FAILURE;
    String baseContent = toString(mergeData.getBase());
    String localContent = toString(mergeData.getLocal());
    String patchedContent = mergeData.getPatched();
    if (localContent == null)
        return ApplyPatchStatus.FAILURE;
    final Ref<ApplyPatchStatus> applyPatchStatusReference = new Ref<>();
    Consumer<MergeResult> callback = new Consumer<MergeResult>() {

        @Override
        public void consume(MergeResult result) {
            FileDocumentManager.getInstance().saveDocument(document);
            applyPatchStatusReference.setIfNull(result != MergeResult.CANCEL ? ApplyPatchStatus.SUCCESS : ApplyPatchStatus.FAILURE);
        }
    };
    try {
        MergeRequest request;
        if (baseContent != null) {
            if (reverse) {
                if (leftPanelTitle == null)
                    leftPanelTitle = VcsBundle.message("patch.apply.conflict.patched.version");
                if (rightPanelTitle == null)
                    rightPanelTitle = VcsBundle.message("patch.apply.conflict.local.version");
                List<String> contents = ContainerUtil.list(patchedContent, baseContent, localContent);
                List<String> titles = ContainerUtil.list(leftPanelTitle, null, rightPanelTitle);
                request = PatchDiffRequestFactory.createMergeRequest(project, document, file, contents, null, titles, callback);
            } else {
                request = PatchDiffRequestFactory.createMergeRequest(project, document, file, baseContent, localContent, patchedContent, callback);
            }
        } else {
            TextFilePatch textPatch = (TextFilePatch) patch.getPatch();
            final GenericPatchApplier applier = new GenericPatchApplier(localContent, textPatch.getHunks());
            applier.execute();
            final AppliedTextPatch appliedTextPatch = AppliedTextPatch.create(applier.getAppliedInfo());
            request = PatchDiffRequestFactory.createBadMergeRequest(project, document, file, localContent, appliedTextPatch, callback);
        }
        request.putUserData(DiffUserDataKeysEx.MERGE_ACTION_CAPTIONS, new Function<MergeResult, String>() {

            @Override
            public String fun(MergeResult result) {
                return result.equals(MergeResult.CANCEL) ? "Abort..." : null;
            }
        });
        request.putUserData(DiffUserDataKeysEx.MERGE_CANCEL_HANDLER, new Condition<MergeTool.MergeViewer>() {

            @Override
            public boolean value(MergeTool.MergeViewer viewer) {
                int result = Messages.showYesNoCancelDialog(viewer.getComponent().getRootPane(), XmlStringUtil.wrapInHtml("Would you like to <u>A</u>bort&Rollback applying patch action or <u>S</u>kip this file?"), "Close Merge", "_Abort", "_Skip", "Cancel", Messages.getQuestionIcon());
                if (result == Messages.YES) {
                    applyPatchStatusReference.set(ApplyPatchStatus.ABORT);
                } else if (result == Messages.NO) {
                    applyPatchStatusReference.set(ApplyPatchStatus.SKIP);
                }
                return result != Messages.CANCEL;
            }
        });
        DiffManager.getInstance().showMerge(project, request);
        return applyPatchStatusReference.get();
    } catch (InvalidDiffRequestException e) {
        LOG.warn(e);
        return ApplyPatchStatus.FAILURE;
    }
}
Also used : InvalidDiffRequestException(com.intellij.diff.InvalidDiffRequestException) MergeResult(com.intellij.diff.merge.MergeResult) Document(com.intellij.openapi.editor.Document) ApplyFilePatch(com.intellij.openapi.diff.impl.patch.apply.ApplyFilePatch) Ref(com.intellij.openapi.util.Ref) MergeRequest(com.intellij.diff.merge.MergeRequest) Consumer(com.intellij.util.Consumer) GenericPatchApplier(com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier) MergeTool(com.intellij.diff.merge.MergeTool) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with MergeResult

use of com.intellij.diff.merge.MergeResult in project intellij-community by JetBrains.

the class MultipleFileMergeDialog method showMergeDialog.

private void showMergeDialog() {
    DiffRequestFactory requestFactory = DiffRequestFactory.getInstance();
    Collection<VirtualFile> files = myTable.getSelection();
    if (!beforeResolve(files)) {
        return;
    }
    for (final VirtualFile file : files) {
        final MergeData mergeData;
        try {
            mergeData = myProvider.loadRevisions(file);
        } catch (VcsException ex) {
            Messages.showErrorDialog(myRootPanel, "Error loading revisions to merge: " + ex.getMessage());
            break;
        }
        if (mergeData.CURRENT == null || mergeData.LAST == null || mergeData.ORIGINAL == null) {
            Messages.showErrorDialog(myRootPanel, "Error loading revisions to merge");
            break;
        }
        String leftTitle = myMergeDialogCustomizer.getLeftPanelTitle(file);
        String baseTitle = myMergeDialogCustomizer.getCenterPanelTitle(file);
        String rightTitle = myMergeDialogCustomizer.getRightPanelTitle(file, mergeData.LAST_REVISION_NUMBER);
        String title = myMergeDialogCustomizer.getMergeWindowTitle(file);
        final List<byte[]> byteContents = ContainerUtil.list(mergeData.CURRENT, mergeData.ORIGINAL, mergeData.LAST);
        List<String> contentTitles = ContainerUtil.list(leftTitle, baseTitle, rightTitle);
        Consumer<MergeResult> callback = result -> {
            Document document = FileDocumentManager.getInstance().getCachedDocument(file);
            if (document != null)
                FileDocumentManager.getInstance().saveDocument(document);
            checkMarkModifiedProject(file);
            if (result != MergeResult.CANCEL) {
                ApplicationManager.getApplication().runWriteAction(() -> {
                    markFileProcessed(file, getSessionResolution(result));
                });
            }
        };
        MergeRequest request;
        try {
            if (myProvider.isBinary(file)) {
                // respect MIME-types in svn
                request = requestFactory.createBinaryMergeRequest(myProject, file, byteContents, title, contentTitles, callback);
            } else {
                request = requestFactory.createMergeRequest(myProject, file, byteContents, title, contentTitles, callback);
            }
            MergeUtil.putRevisionInfos(request, mergeData);
        } catch (InvalidDiffRequestException e) {
            LOG.error(e);
            Messages.showErrorDialog(myRootPanel, "Can't show merge dialog");
            break;
        }
        DiffManager.getInstance().showMerge(myProject, request);
    }
    updateModelFromFiles();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) UIUtil(com.intellij.util.ui.UIUtil) InvalidDiffRequestException(com.intellij.diff.InvalidDiffRequestException) MergeUtil(com.intellij.diff.merge.MergeUtil) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Document(com.intellij.openapi.editor.Document) DiffManager(com.intellij.diff.DiffManager) ColumnInfo(com.intellij.util.ui.ColumnInfo) TableCellRenderer(javax.swing.table.TableCellRenderer) JBLabel(com.intellij.ui.components.JBLabel) SmartList(com.intellij.util.SmartList) DiffUtil(com.intellij.diff.util.DiffUtil) VcsDirtyScopeManager(com.intellij.openapi.vcs.changes.VcsDirtyScopeManager) SpeedSearchUtil(com.intellij.ui.speedSearch.SpeedSearchUtil) Messages(com.intellij.openapi.ui.Messages) FileUtil(com.intellij.openapi.util.io.FileUtil) Logger(com.intellij.openapi.diagnostic.Logger) VcsException(com.intellij.openapi.vcs.VcsException) SimpleTextAttributes(com.intellij.ui.SimpleTextAttributes) ListSelectionEvent(javax.swing.event.ListSelectionEvent) VirtualFilePresentation(com.intellij.ide.presentation.VirtualFilePresentation) TableView(com.intellij.ui.table.TableView) MergeRequest(com.intellij.diff.merge.MergeRequest) DoubleClickListener(com.intellij.ui.DoubleClickListener) MergeVersion(com.intellij.openapi.diff.impl.mergeTool.MergeVersion) Nullable(org.jetbrains.annotations.Nullable) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) DiffRequestFactory(com.intellij.diff.DiffRequestFactory) Ref(com.intellij.openapi.util.Ref) Consumer(com.intellij.util.Consumer) java.util(java.util) ActionListener(java.awt.event.ActionListener) ColoredTableCellRenderer(com.intellij.ui.ColoredTableCellRenderer) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) CommonBundle(com.intellij.CommonBundle) Project(com.intellij.openapi.project.Project) ListTableModel(com.intellij.util.ui.ListTableModel) TableSpeedSearch(com.intellij.ui.TableSpeedSearch) StringUtil(com.intellij.openapi.util.text.StringUtil) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx) Convertor(com.intellij.util.containers.Convertor) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IOException(java.io.IOException) ActionEvent(java.awt.event.ActionEvent) MouseEvent(java.awt.event.MouseEvent) VcsBundle(com.intellij.openapi.vcs.VcsBundle) MergeResult(com.intellij.diff.merge.MergeResult) CommandProcessor(com.intellij.openapi.command.CommandProcessor) ListSelectionListener(javax.swing.event.ListSelectionListener) javax.swing(javax.swing) DiffRequestFactory(com.intellij.diff.DiffRequestFactory) InvalidDiffRequestException(com.intellij.diff.InvalidDiffRequestException) MergeResult(com.intellij.diff.merge.MergeResult) Document(com.intellij.openapi.editor.Document) MergeRequest(com.intellij.diff.merge.MergeRequest) VcsException(com.intellij.openapi.vcs.VcsException)

Aggregations

InvalidDiffRequestException (com.intellij.diff.InvalidDiffRequestException)2 MergeRequest (com.intellij.diff.merge.MergeRequest)2 MergeResult (com.intellij.diff.merge.MergeResult)2 Document (com.intellij.openapi.editor.Document)2 Ref (com.intellij.openapi.util.Ref)2 Consumer (com.intellij.util.Consumer)2 NotNull (org.jetbrains.annotations.NotNull)2 CommonBundle (com.intellij.CommonBundle)1 DiffManager (com.intellij.diff.DiffManager)1 DiffRequestFactory (com.intellij.diff.DiffRequestFactory)1 MergeTool (com.intellij.diff.merge.MergeTool)1 MergeUtil (com.intellij.diff.merge.MergeUtil)1 DiffUtil (com.intellij.diff.util.DiffUtil)1 VirtualFilePresentation (com.intellij.ide.presentation.VirtualFilePresentation)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 CommandProcessor (com.intellij.openapi.command.CommandProcessor)1 Logger (com.intellij.openapi.diagnostic.Logger)1 MergeVersion (com.intellij.openapi.diff.impl.mergeTool.MergeVersion)1 ApplyFilePatch (com.intellij.openapi.diff.impl.patch.apply.ApplyFilePatch)1 GenericPatchApplier (com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier)1