Search in sources :

Example 6 with FilePatch

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

the class TextFilePatchInProgress method getDiffRequestProducers.

@NotNull
@Override
public DiffRequestProducer getDiffRequestProducers(final Project project, final PatchReader patchReader) {
    final PatchChange change = getChange();
    final FilePatch patch = getPatch();
    final String path = patch.getBeforeName() == null ? patch.getAfterName() : patch.getBeforeName();
    final Getter<CharSequence> baseContentGetter = new Getter<CharSequence>() {

        @Override
        public CharSequence get() {
            return patchReader.getBaseRevision(project, path);
        }
    };
    return new DiffRequestProducer() {

        @NotNull
        @Override
        public DiffRequest process(@NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException, ProcessCanceledException {
            if (myCurrentBase != null && myCurrentBase.getFileType() == UnknownFileType.INSTANCE) {
                return new UnknownFileTypeDiffRequest(myCurrentBase, getName());
            }
            if (isConflictingChange()) {
                final VirtualFile file = getCurrentBase();
                Getter<ApplyPatchForBaseRevisionTexts> getter = new Getter<ApplyPatchForBaseRevisionTexts>() {

                    @Override
                    public ApplyPatchForBaseRevisionTexts get() {
                        return ApplyPatchForBaseRevisionTexts.create(project, file, VcsUtil.getFilePath(file), getPatch(), baseContentGetter);
                    }
                };
                String afterTitle = getPatch().getAfterVersionId();
                if (afterTitle == null)
                    afterTitle = "Patched Version";
                return PatchDiffRequestFactory.createConflictDiffRequest(project, file, getPatch(), afterTitle, getter, getName(), context, indicator);
            } else {
                return PatchDiffRequestFactory.createDiffRequest(project, change, getName(), context, indicator);
            }
        }

        @NotNull
        @Override
        public String getName() {
            final File ioCurrentBase = getIoCurrentBase();
            return ioCurrentBase == null ? getCurrentPath() : ioCurrentBase.getPath();
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Getter(com.intellij.openapi.util.Getter) DiffRequestProducer(com.intellij.diff.chains.DiffRequestProducer) UnknownFileTypeDiffRequest(com.intellij.diff.requests.UnknownFileTypeDiffRequest) TextFilePatch(com.intellij.openapi.diff.impl.patch.TextFilePatch) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch) NotNull(org.jetbrains.annotations.NotNull) UserDataHolder(com.intellij.openapi.util.UserDataHolder) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with FilePatch

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

the class BinaryPatchWriter method writeBinaries.

public static void writeBinaries(@Nullable String basePath, @NotNull List<BinaryFilePatch> patches, @NotNull Writer writer) throws IOException {
    //use it for git headers&binary content, otherwise git won't parse&apply it properly                           
    String lineSeparator = "\n";
    for (FilePatch patch : patches) {
        BinaryFilePatch filePatch = (BinaryFilePatch) patch;
        writer.write(String.format(GIT_DIFF_HEADER, filePatch.getBeforeName(), filePatch.getAfterName()));
        writer.write(lineSeparator);
        File afterFile = new File(basePath, filePatch.getAfterName());
        if (filePatch.isDeletedFile()) {
            writer.write(getFileModeHeader(FileStatus.DELETED, REGULAR_FILE_MODE));
            writer.write(lineSeparator);
        } else if (filePatch.isNewFile()) {
            writer.write(getFileModeHeader(FileStatus.ADDED, !SystemInfo.isWindows && afterFile.canExecute() ? EXECUTABLE_FILE_MODE : REGULAR_FILE_MODE));
            writer.write(lineSeparator);
        }
        byte[] afterContent = filePatch.getAfterContent();
        writer.write(getIndexHeader(filePatch.isNewFile() ? NOT_COMMITTED_HASH : getSha1ForContent(filePatch.getBeforeContent()), filePatch.isDeletedFile() ? NOT_COMMITTED_HASH : getSha1ForContent(afterContent)));
        writer.write(lineSeparator);
        writer.write(GIT_BINARY_HEADER);
        writer.write(lineSeparator);
        writer.write(String.format(LITERAL_HEADER, afterContent == null ? 0 : afterContent.length));
        writer.write(lineSeparator);
        try {
            BinaryEncoder.encode(afterFile.exists() ? new FileInputStream(afterFile) : new ByteArrayInputStream(ArrayUtil.EMPTY_BYTE_ARRAY), writer);
        } catch (BinaryEncoder.BinaryPatchException e) {
            LOG.error("Can't write patch for binary file: " + afterFile.getPath(), e);
        }
        writer.write(lineSeparator);
    }
}
Also used : BinaryEncoder(com.intellij.openapi.diff.impl.patch.BinaryEncoder) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch) BinaryFilePatch(com.intellij.openapi.diff.impl.patch.BinaryFilePatch) BinaryFilePatch(com.intellij.openapi.diff.impl.patch.BinaryFilePatch)

Example 8 with FilePatch

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

the class ImportToShelfExecutor method apply.

@Override
public void apply(@NotNull List<FilePatch> remaining, @NotNull final MultiMap<VirtualFile, TextFilePatchInProgress> patchGroupsToApply, @Nullable LocalChangeList localList, @Nullable final String fileName, @Nullable ThrowableComputable<Map<String, Map<String, CharSequence>>, PatchSyntaxException> additionalInfo) {
    if (fileName == null) {
        LOG.error("Patch file name shouldn't be null");
        return;
    }
    final VcsCatchingRunnable vcsCatchingRunnable = new VcsCatchingRunnable() {

        @Override
        public void runImpl() throws VcsException {
            final VirtualFile baseDir = myProject.getBaseDir();
            final File ioBase = new File(baseDir.getPath());
            final List<FilePatch> allPatches = new ArrayList<>();
            for (VirtualFile virtualFile : patchGroupsToApply.keySet()) {
                final File ioCurrentBase = new File(virtualFile.getPath());
                allPatches.addAll(ContainerUtil.map(patchGroupsToApply.get(virtualFile), new Function<TextFilePatchInProgress, TextFilePatch>() {

                    public TextFilePatch fun(TextFilePatchInProgress patchInProgress) {
                        final TextFilePatch was = patchInProgress.getPatch();
                        was.setBeforeName(PathUtil.toSystemIndependentName(FileUtil.getRelativePath(ioBase, new File(ioCurrentBase, was.getBeforeName()))));
                        was.setAfterName(PathUtil.toSystemIndependentName(FileUtil.getRelativePath(ioBase, new File(ioCurrentBase, was.getAfterName()))));
                        return was;
                    }
                }));
            }
            if (!allPatches.isEmpty()) {
                PatchEP[] patchTransitExtensions = null;
                if (additionalInfo != null) {
                    try {
                        final Map<String, PatchEP> extensions = new HashMap<>();
                        for (Map.Entry<String, Map<String, CharSequence>> entry : additionalInfo.compute().entrySet()) {
                            final String filePath = entry.getKey();
                            Map<String, CharSequence> extToValue = entry.getValue();
                            for (Map.Entry<String, CharSequence> innerEntry : extToValue.entrySet()) {
                                TransitExtension patchEP = (TransitExtension) extensions.get(innerEntry.getKey());
                                if (patchEP == null) {
                                    patchEP = new TransitExtension(innerEntry.getKey());
                                    extensions.put(innerEntry.getKey(), patchEP);
                                }
                                patchEP.put(filePath, innerEntry.getValue());
                            }
                        }
                        Collection<PatchEP> values = extensions.values();
                        patchTransitExtensions = values.toArray(new PatchEP[values.size()]);
                    } catch (PatchSyntaxException e) {
                        VcsBalloonProblemNotifier.showOverChangesView(myProject, "Can not import additional patch info: " + e.getMessage(), MessageType.ERROR);
                    }
                }
                try {
                    final ShelvedChangeList shelvedChangeList = ShelveChangesManager.getInstance(myProject).importFilePatches(fileName, allPatches, patchTransitExtensions);
                    ShelvedChangesViewManager.getInstance(myProject).activateView(shelvedChangeList);
                } catch (IOException e) {
                    throw new VcsException(e);
                }
            }
        }
    };
    ProgressManager.getInstance().runProcessWithProgressSynchronously(vcsCatchingRunnable, "Import Patch to Shelf", true, myProject);
    if (!vcsCatchingRunnable.get().isEmpty()) {
        AbstractVcsHelper.getInstance(myProject).showErrors(vcsCatchingRunnable.get(), IMPORT_TO_SHELF);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ShelvedChangeList(com.intellij.openapi.vcs.changes.shelf.ShelvedChangeList) Function(com.intellij.util.Function) PatchEP(com.intellij.openapi.diff.impl.patch.PatchEP) VcsCatchingRunnable(com.intellij.vcsUtil.VcsCatchingRunnable) IOException(java.io.IOException) TextFilePatch(com.intellij.openapi.diff.impl.patch.TextFilePatch) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch) PatchSyntaxException(com.intellij.openapi.diff.impl.patch.PatchSyntaxException) TextFilePatch(com.intellij.openapi.diff.impl.patch.TextFilePatch) VcsException(com.intellij.openapi.vcs.VcsException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) MultiMap(com.intellij.util.containers.MultiMap)

Example 9 with FilePatch

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

the class MatchPatchPaths method workWithNotExisting.

private void workWithNotExisting(@NotNull PatchBaseDirectoryDetector directoryDetector, @NotNull List<FilePatch> newOrWithoutMatches, @NotNull MultiMap<VirtualFile, AbstractFilePatchInProgress> result) {
    for (FilePatch patch : newOrWithoutMatches) {
        String afterName = patch.getAfterName();
        final String[] strings = afterName != null ? afterName.replace('\\', '/').split("/") : ArrayUtil.EMPTY_STRING_ARRAY;
        Pair<VirtualFile, Integer> best = null;
        for (int i = strings.length - 2; i >= 0; --i) {
            final String name = strings[i];
            final Collection<VirtualFile> files = findFilesFromIndex(directoryDetector, name);
            if (!files.isEmpty()) {
                // check all candidates
                for (VirtualFile file : files) {
                    Pair<VirtualFile, Integer> pair = compareNamesImpl(strings, file, i);
                    if (pair != null && pair.getSecond() < i) {
                        if (best == null || pair.getSecond() < best.getSecond() || isGoodAndProjectBased(best, pair)) {
                            best = pair;
                        }
                    }
                }
            }
        }
        if (best != null) {
            final AbstractFilePatchInProgress patchInProgress = createPatchInProgress(patch, best.getFirst());
            if (patchInProgress == null)
                break;
            processStipUp(patchInProgress, best.getSecond());
            result.putValue(best.getFirst(), patchInProgress);
        } else {
            final AbstractFilePatchInProgress patchInProgress = createPatchInProgress(patch, myBaseDir);
            if (patchInProgress == null)
                break;
            result.putValue(myBaseDir, patchInProgress);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ShelvedBinaryFilePatch(com.intellij.openapi.vcs.changes.shelf.ShelvedBinaryFilePatch) BinaryFilePatch(com.intellij.openapi.diff.impl.patch.BinaryFilePatch) TextFilePatch(com.intellij.openapi.diff.impl.patch.TextFilePatch) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch)

Example 10 with FilePatch

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

the class MatchPatchPaths method findCandidates.

private void findCandidates(@NotNull List<? extends FilePatch> list, @NotNull final PatchBaseDirectoryDetector directoryDetector, @NotNull List<PatchAndVariants> candidates, @NotNull List<FilePatch> newOrWithoutMatches) {
    for (final FilePatch patch : list) {
        final String fileName = patch.getBeforeFileName();
        if (patch.isNewFile() || (patch.getBeforeName() == null)) {
            newOrWithoutMatches.add(patch);
            continue;
        }
        final Collection<VirtualFile> files = new ArrayList<>(findFilesFromIndex(directoryDetector, fileName));
        // for directories outside the project scope but under version control
        if (patch.getBeforeName() != null && patch.getBeforeName().startsWith("..")) {
            final VirtualFile relativeFile = VfsUtil.findRelativeFile(myBaseDir, patch.getBeforeName().replace('\\', '/').split("/"));
            if (relativeFile != null) {
                files.add(relativeFile);
            }
        }
        if (files.isEmpty()) {
            newOrWithoutMatches.add(patch);
        } else {
            //files order is not defined, so get the best variant depends on it, too
            final List<AbstractFilePatchInProgress> variants = ObjectsConvertor.convert(files, new Convertor<VirtualFile, AbstractFilePatchInProgress>() {

                @Override
                public AbstractFilePatchInProgress convert(VirtualFile o) {
                    return processMatch(patch, o);
                }
            }, ObjectsConvertor.NOT_NULL);
            if (variants.isEmpty()) {
                // just to be sure
                newOrWithoutMatches.add(patch);
            } else {
                candidates.add(new PatchAndVariants(variants));
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) ShelvedBinaryFilePatch(com.intellij.openapi.vcs.changes.shelf.ShelvedBinaryFilePatch) BinaryFilePatch(com.intellij.openapi.diff.impl.patch.BinaryFilePatch) TextFilePatch(com.intellij.openapi.diff.impl.patch.TextFilePatch) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch)

Aggregations

FilePatch (com.intellij.openapi.diff.impl.patch.FilePatch)13 TextFilePatch (com.intellij.openapi.diff.impl.patch.TextFilePatch)8 BinaryFilePatch (com.intellij.openapi.diff.impl.patch.BinaryFilePatch)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 ShelvedBinaryFilePatch (com.intellij.openapi.vcs.changes.shelf.ShelvedBinaryFilePatch)4 IOException (java.io.IOException)4 ApplyTextFilePatch (com.intellij.openapi.diff.impl.patch.apply.ApplyTextFilePatch)3 ArrayList (java.util.ArrayList)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 VcsException (com.intellij.openapi.vcs.VcsException)2 MultiMap (com.intellij.util.containers.MultiMap)2 File (java.io.File)2 NotNull (org.jetbrains.annotations.NotNull)2 DiffRequestProducer (com.intellij.diff.chains.DiffRequestProducer)1 UnknownFileTypeDiffRequest (com.intellij.diff.requests.UnknownFileTypeDiffRequest)1 ApplyPatchContext (com.intellij.openapi.diff.impl.patch.ApplyPatchContext)1 ApplyPatchStatus (com.intellij.openapi.diff.impl.patch.ApplyPatchStatus)1 BinaryEncoder (com.intellij.openapi.diff.impl.patch.BinaryEncoder)1 PatchEP (com.intellij.openapi.diff.impl.patch.PatchEP)1 PatchReader (com.intellij.openapi.diff.impl.patch.PatchReader)1