Search in sources :

Example 1 with DiffRequestProducerException

use of com.intellij.diff.chains.DiffRequestProducerException in project intellij-community by JetBrains.

the class ChangeDiffRequestProducer method loadCurrentContents.

@NotNull
protected DiffRequest loadCurrentContents(@NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
    DiffRequestProducerException wrapperException = null;
    DiffRequestProducerException requestException = null;
    DiffViewerWrapper wrapper = null;
    try {
        for (ChangeDiffViewerWrapperProvider provider : ChangeDiffViewerWrapperProvider.EP_NAME.getExtensions()) {
            if (provider.canCreate(myProject, myChange)) {
                wrapper = provider.process(this, context, indicator);
                break;
            }
        }
    } catch (DiffRequestProducerException e) {
        wrapperException = e;
    }
    DiffRequest request = null;
    try {
        for (ChangeDiffRequestProvider provider : ChangeDiffRequestProvider.EP_NAME.getExtensions()) {
            if (provider.canCreate(myProject, myChange)) {
                request = provider.process(this, context, indicator);
                break;
            }
        }
        if (request == null)
            request = createRequest(myProject, myChange, context, indicator);
    } catch (DiffRequestProducerException e) {
        requestException = e;
    }
    if (requestException != null && wrapperException != null) {
        String message = requestException.getMessage() + "\n\n" + wrapperException.getMessage();
        throw new DiffRequestProducerException(message);
    }
    if (requestException != null) {
        request = new ErrorDiffRequest(getRequestTitle(myChange), requestException);
        LOG.info("Request: " + requestException.getMessage());
    }
    if (wrapperException != null) {
        LOG.info("Wrapper: " + wrapperException.getMessage());
    }
    request.putUserData(CHANGE_KEY, myChange);
    request.putUserData(DiffViewerWrapper.KEY, wrapper);
    for (Map.Entry<Key, Object> entry : myChangeContext.entrySet()) {
        request.putUserData(entry.getKey(), entry.getValue());
    }
    DiffUtil.putDataKey(request, VcsDataKeys.CURRENT_CHANGE, myChange);
    return request;
}
Also used : DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) ErrorDiffRequest(com.intellij.diff.requests.ErrorDiffRequest) DiffViewerWrapper(com.intellij.diff.impl.DiffViewerWrapper) ErrorDiffRequest(com.intellij.diff.requests.ErrorDiffRequest) DiffRequest(com.intellij.diff.requests.DiffRequest) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) Map(java.util.Map) Key(com.intellij.openapi.util.Key) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DiffRequestProducerException

use of com.intellij.diff.chains.DiffRequestProducerException in project intellij-community by JetBrains.

the class ChangeDiffRequestProducer method createRequest.

@NotNull
private DiffRequest createRequest(@Nullable Project project, @NotNull Change change, @NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
    if (ChangesUtil.isTextConflictingChange(change)) {
        // three side diff
        // FIXME: This part is ugly as a VCS merge subsystem itself.
        FilePath path = ChangesUtil.getFilePath(change);
        VirtualFile file = path.getVirtualFile();
        if (file == null) {
            file = LocalFileSystem.getInstance().refreshAndFindFileByPath(path.getPath());
        }
        if (file == null)
            throw new DiffRequestProducerException("Can't show merge conflict - file not found");
        if (project == null) {
            throw new DiffRequestProducerException("Can't show merge conflict - project is unknown");
        }
        final AbstractVcs vcs = ChangesUtil.getVcsForChange(change, project);
        if (vcs == null || vcs.getMergeProvider() == null) {
            throw new DiffRequestProducerException("Can't show merge conflict - operation nos supported");
        }
        try {
            // FIXME: loadRevisions() can call runProcessWithProgressSynchronously() inside
            final Ref<Throwable> exceptionRef = new Ref<>();
            final Ref<MergeData> mergeDataRef = new Ref<>();
            final VirtualFile finalFile = file;
            ApplicationManager.getApplication().invokeAndWait(() -> {
                try {
                    mergeDataRef.set(vcs.getMergeProvider().loadRevisions(finalFile));
                } catch (VcsException e) {
                    exceptionRef.set(e);
                }
            });
            if (!exceptionRef.isNull()) {
                Throwable e = exceptionRef.get();
                if (e instanceof VcsException)
                    throw (VcsException) e;
                if (e instanceof Error)
                    throw (Error) e;
                if (e instanceof RuntimeException)
                    throw (RuntimeException) e;
                throw new RuntimeException(e);
            }
            MergeData mergeData = mergeDataRef.get();
            ContentRevision bRev = change.getBeforeRevision();
            ContentRevision aRev = change.getAfterRevision();
            String beforeRevisionTitle = getRevisionTitle(bRev, "Your version");
            String afterRevisionTitle = getRevisionTitle(aRev, "Server version");
            String title = DiffRequestFactory.getInstance().getTitle(file);
            List<String> titles = ContainerUtil.list(beforeRevisionTitle, "Base Version", afterRevisionTitle);
            DiffContentFactory contentFactory = DiffContentFactory.getInstance();
            List<DiffContent> contents = ContainerUtil.list(contentFactory.createFromBytes(project, mergeData.CURRENT, file), contentFactory.createFromBytes(project, mergeData.ORIGINAL, file), contentFactory.createFromBytes(project, mergeData.LAST, file));
            SimpleDiffRequest request = new SimpleDiffRequest(title, contents, titles);
            MergeUtil.putRevisionInfos(request, mergeData);
            return request;
        } catch (VcsException | IOException e) {
            LOG.info(e);
            throw new DiffRequestProducerException(e);
        }
    } else {
        ContentRevision bRev = change.getBeforeRevision();
        ContentRevision aRev = change.getAfterRevision();
        if (bRev == null && aRev == null) {
            LOG.warn("Both revision contents are empty");
            throw new DiffRequestProducerException("Bad revisions contents");
        }
        if (bRev != null)
            checkContentRevision(project, bRev, context, indicator);
        if (aRev != null)
            checkContentRevision(project, aRev, context, indicator);
        String title = getRequestTitle(change);
        indicator.setIndeterminate(true);
        DiffContent content1 = createContent(project, bRev, context, indicator);
        DiffContent content2 = createContent(project, aRev, context, indicator);
        final String userLeftRevisionTitle = (String) myChangeContext.get(DiffUserDataKeysEx.VCS_DIFF_LEFT_CONTENT_TITLE);
        String beforeRevisionTitle = userLeftRevisionTitle != null ? userLeftRevisionTitle : getRevisionTitle(bRev, "Base version");
        final String userRightRevisionTitle = (String) myChangeContext.get(DiffUserDataKeysEx.VCS_DIFF_RIGHT_CONTENT_TITLE);
        String afterRevisionTitle = userRightRevisionTitle != null ? userRightRevisionTitle : getRevisionTitle(aRev, "Your version");
        SimpleDiffRequest request = new SimpleDiffRequest(title, content1, content2, beforeRevisionTitle, afterRevisionTitle);
        boolean bRevCurrent = bRev instanceof CurrentContentRevision;
        boolean aRevCurrent = aRev instanceof CurrentContentRevision;
        if (bRevCurrent && !aRevCurrent)
            request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.LEFT);
        if (!bRevCurrent && aRevCurrent)
            request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.RIGHT);
        return request;
    }
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) DiffContentFactory(com.intellij.diff.DiffContentFactory) MergeData(com.intellij.openapi.vcs.merge.MergeData) IOException(java.io.IOException) AbstractVcs(com.intellij.openapi.vcs.AbstractVcs) Ref(com.intellij.openapi.util.Ref) VcsException(com.intellij.openapi.vcs.VcsException) DiffContent(com.intellij.diff.contents.DiffContent) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with DiffRequestProducerException

use of com.intellij.diff.chains.DiffRequestProducerException 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 4 with DiffRequestProducerException

use of com.intellij.diff.chains.DiffRequestProducerException in project intellij-community by JetBrains.

the class ExternalDiffTool method collectRequests.

@NotNull
private static List<DiffRequest> collectRequests(@Nullable Project project, @NotNull final DiffRequestChain chain, @NotNull ProgressIndicator indicator) {
    List<DiffRequest> requests = new ArrayList<>();
    UserDataHolderBase context = new UserDataHolderBase();
    List<String> errorRequests = new ArrayList<>();
    // TODO: show all changes on explicit selection
    List<? extends DiffRequestProducer> producers = Collections.singletonList(chain.getRequests().get(chain.getIndex()));
    for (DiffRequestProducer producer : producers) {
        try {
            requests.add(producer.process(context, indicator));
        } catch (DiffRequestProducerException e) {
            LOG.warn(e);
            errorRequests.add(producer.getName());
        }
    }
    if (!errorRequests.isEmpty()) {
        new Notification("diff", "Can't load some changes", StringUtil.join(errorRequests, "<br>"), NotificationType.ERROR).notify(project);
    }
    return requests;
}
Also used : DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) UserDataHolderBase(com.intellij.openapi.util.UserDataHolderBase) ArrayList(java.util.ArrayList) ContentDiffRequest(com.intellij.diff.requests.ContentDiffRequest) DiffRequest(com.intellij.diff.requests.DiffRequest) DiffRequestProducer(com.intellij.diff.chains.DiffRequestProducer) Notification(com.intellij.notification.Notification) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with DiffRequestProducerException

use of com.intellij.diff.chains.DiffRequestProducerException in project intellij-community by JetBrains.

the class DiffElement method createDiffContent.

/**
   * Called in background thread without ReadLock OR in EDT
   *
   * @see com.intellij.diff.chains.DiffRequestProducer#process
   */
@NotNull
public DiffContent createDiffContent(@Nullable Project project, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException, ProcessCanceledException {
    try {
        final T src = getValue();
        if (src instanceof VirtualFile) {
            return DiffContentFactory.getInstance().create(project, (VirtualFile) src);
        }
        byte[] content = getContent();
        if (content == null)
            throw new DiffRequestProducerException("Can't get content");
        return DiffContentFactory.getInstance().create(project, new String(content, getCharset()), getFileType());
    } catch (IOException e) {
        throw new DiffRequestProducerException(e);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DiffRequestProducerException (com.intellij.diff.chains.DiffRequestProducerException)10 NotNull (org.jetbrains.annotations.NotNull)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 DiffRequest (com.intellij.diff.requests.DiffRequest)4 FilePath (com.intellij.openapi.vcs.FilePath)4 VcsException (com.intellij.openapi.vcs.VcsException)4 IOException (java.io.IOException)4 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Change (com.intellij.openapi.vcs.changes.Change)3 File (java.io.File)3 DiffRequestProducer (com.intellij.diff.chains.DiffRequestProducer)2 SimpleDiffRequest (com.intellij.diff.requests.SimpleDiffRequest)2 UnknownFileTypeDiffRequest (com.intellij.diff.requests.UnknownFileTypeDiffRequest)2 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 Ref (com.intellij.openapi.util.Ref)2 UserDataHolder (com.intellij.openapi.util.UserDataHolder)2 DiffContentFactory (com.intellij.diff.DiffContentFactory)1 DiffContentFactoryEx (com.intellij.diff.DiffContentFactoryEx)1 DiffContent (com.intellij.diff.contents.DiffContent)1 DiffViewerWrapper (com.intellij.diff.impl.DiffViewerWrapper)1