Search in sources :

Example 1 with TextFilePatch

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

the class AutoMatchIterator method execute.

public List<TextFilePatchInProgress> execute(final List<TextFilePatch> list) {
    final List<TextFilePatch> creations = new LinkedList<>();
    final PatchBaseDirectoryDetector directoryDetector = PatchBaseDirectoryDetector.getInstance(myProject);
    for (TextFilePatch patch : list) {
        if (patch.isNewFile() || (patch.getBeforeName() == null)) {
            creations.add(patch);
            continue;
        }
        final String fileName = patch.getBeforeFileName();
        final Collection<VirtualFile> files = ApplicationManager.getApplication().runReadAction(new Computable<Collection<VirtualFile>>() {

            public Collection<VirtualFile> compute() {
                return directoryDetector.findFiles(fileName);
            }
        });
        for (AutoMatchStrategy strategy : myStrategies) {
            strategy.acceptPatch(patch, files);
        }
    }
    for (AutoMatchStrategy strategy : myStrategies) {
        strategy.beforeCreations();
    }
    // then try to match creations
    for (TextFilePatch creation : creations) {
        for (AutoMatchStrategy strategy : myStrategies) {
            strategy.processCreation(creation);
        }
    }
    for (AutoMatchStrategy strategy : myStrategies) {
        if (strategy.succeeded()) {
            return strategy.getResult();
        }
    }
    return myStrategies.get(myStrategies.size() - 1).getResult();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Collection(java.util.Collection) LinkedList(java.util.LinkedList) TextFilePatch(com.intellij.openapi.diff.impl.patch.TextFilePatch)

Example 2 with TextFilePatch

use of com.intellij.openapi.diff.impl.patch.TextFilePatch 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 3 with TextFilePatch

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

the class ApplyPatchSaveToFileExecutor method toOnePatchGroup.

@NotNull
public static List<FilePatch> toOnePatchGroup(@NotNull MultiMap<VirtualFile, TextFilePatchInProgress> patchGroups, @NotNull VirtualFile newPatchBase) throws IOException {
    List<FilePatch> result = newArrayList();
    for (Map.Entry<VirtualFile, Collection<TextFilePatchInProgress>> entry : patchGroups.entrySet()) {
        VirtualFile oldPatchBase = entry.getKey();
        String relativePath = VfsUtilCore.getRelativePath(oldPatchBase, newPatchBase, '/');
        boolean toConvert = !isEmptyOrSpaces(relativePath) && !".".equals(relativePath);
        for (TextFilePatchInProgress patchInProgress : entry.getValue()) {
            TextFilePatch patch = patchInProgress.getPatch();
            if (toConvert) {
                patch.setBeforeName(getNewBaseRelativePath(newPatchBase, oldPatchBase, patch.getBeforeName()));
                patch.setAfterName(getNewBaseRelativePath(newPatchBase, oldPatchBase, patch.getAfterName()));
            }
            result.add(patch);
        }
    }
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) TextFilePatchInProgress(com.intellij.openapi.vcs.changes.patch.TextFilePatchInProgress) Collection(java.util.Collection) TextFilePatch(com.intellij.openapi.diff.impl.patch.TextFilePatch) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap) TextFilePatch(com.intellij.openapi.diff.impl.patch.TextFilePatch) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TextFilePatch (com.intellij.openapi.diff.impl.patch.TextFilePatch)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 FilePatch (com.intellij.openapi.diff.impl.patch.FilePatch)2 MultiMap (com.intellij.util.containers.MultiMap)2 Collection (java.util.Collection)2 PatchEP (com.intellij.openapi.diff.impl.patch.PatchEP)1 PatchSyntaxException (com.intellij.openapi.diff.impl.patch.PatchSyntaxException)1 VcsException (com.intellij.openapi.vcs.VcsException)1 TextFilePatchInProgress (com.intellij.openapi.vcs.changes.patch.TextFilePatchInProgress)1 ShelvedChangeList (com.intellij.openapi.vcs.changes.shelf.ShelvedChangeList)1 Function (com.intellij.util.Function)1 VcsCatchingRunnable (com.intellij.vcsUtil.VcsCatchingRunnable)1 File (java.io.File)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1