Search in sources :

Example 1 with GenericPatchApplier

use of com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier 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 GenericPatchApplier

use of com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier in project intellij-community by JetBrains.

the class PatchDiffRequestFactory method createConflictDiffRequest.

@NotNull
public static DiffRequest createConflictDiffRequest(@Nullable Project project, @Nullable VirtualFile file, @NotNull TextFilePatch patch, @NotNull String afterTitle, @NotNull final Getter<ApplyPatchForBaseRevisionTexts> textsGetter, @NotNull String name, @NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
    if (file == null)
        throw new DiffRequestProducerException("Can't show diff for '" + name + "'");
    if (file.getFileType().isBinary())
        throw new DiffRequestProducerException("Can't show diff for binary file '" + name + "'");
    final Ref<ApplyPatchForBaseRevisionTexts> textsRef = new Ref<>();
    ApplicationManager.getApplication().invokeAndWait(new Runnable() {

        @Override
        public void run() {
            textsRef.set(textsGetter.get());
        }
    }, indicator.getModalityState());
    ApplyPatchForBaseRevisionTexts texts = textsRef.get();
    if (texts.getLocal() == null)
        throw new DiffRequestProducerException("Can't show diff for '" + file.getPresentableUrl() + "'");
    if (texts.getBase() == null) {
        String localContent = texts.getLocal().toString();
        final GenericPatchApplier applier = new GenericPatchApplier(localContent, patch.getHunks());
        applier.execute();
        final AppliedTextPatch appliedTextPatch = AppliedTextPatch.create(applier.getAppliedInfo());
        return createBadDiffRequest(project, file, localContent, appliedTextPatch, null, null, null, null);
    } else {
        String localContent = texts.getLocal().toString();
        String baseContent = texts.getBase().toString();
        String patchedContent = texts.getPatched();
        return createDiffRequest(project, file, ContainerUtil.list(localContent, baseContent, patchedContent), null, ContainerUtil.list("Current Version", "Base Version", afterTitle));
    }
}
Also used : Ref(com.intellij.openapi.util.Ref) DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) GenericPatchApplier(com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GenericPatchApplier

use of com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier in project intellij-community by JetBrains.

the class LazyPatchContentRevision method getContent.

public String getContent() {
    if (myContent == null) {
        final String localContext = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

            @Override
            public String compute() {
                final Document doc = FileDocumentManager.getInstance().getDocument(myVf);
                return doc == null ? null : doc.getText();
            }
        });
        if (localContext == null) {
            myPatchApplyFailed = true;
            return null;
        }
        final GenericPatchApplier applier = new GenericPatchApplier(localContext, myPatch.getHunks());
        if (applier.execute()) {
            myContent = applier.getAfter();
        } else {
            myPatchApplyFailed = true;
        }
    }
    return myContent;
}
Also used : Document(com.intellij.openapi.editor.Document) GenericPatchApplier(com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier)

Example 4 with GenericPatchApplier

use of com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier in project intellij-community by JetBrains.

the class MatchPatchPaths method getMatchingLines.

private static int getMatchingLines(final AbstractFilePatchInProgress<TextFilePatch> patch) {
    final VirtualFile base = patch.getCurrentBase();
    if (base == null)
        return -1;
    String text;
    try {
        if (base.getLength() > BIG_FILE_BOUND) {
            // partially
            text = VfsUtilCore.loadText(base, BIG_FILE_BOUND);
        } else {
            text = VfsUtilCore.loadText(base);
        }
    } catch (IOException e) {
        return 0;
    }
    return new GenericPatchApplier(text, patch.getPatch().getHunks()).weightContextMatch(100, 5);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IOException(java.io.IOException) GenericPatchApplier(com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier)

Aggregations

GenericPatchApplier (com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier)4 Document (com.intellij.openapi.editor.Document)2 Ref (com.intellij.openapi.util.Ref)2 NotNull (org.jetbrains.annotations.NotNull)2 InvalidDiffRequestException (com.intellij.diff.InvalidDiffRequestException)1 DiffRequestProducerException (com.intellij.diff.chains.DiffRequestProducerException)1 MergeRequest (com.intellij.diff.merge.MergeRequest)1 MergeResult (com.intellij.diff.merge.MergeResult)1 MergeTool (com.intellij.diff.merge.MergeTool)1 ApplyFilePatch (com.intellij.openapi.diff.impl.patch.apply.ApplyFilePatch)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Consumer (com.intellij.util.Consumer)1 IOException (java.io.IOException)1