Search in sources :

Example 1 with BinaryFilePatch

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

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

the class RevertCommittedStuffAbstractAction method actionPerformed.

public void actionPerformed(final AnActionEvent e) {
    final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
    final VirtualFile baseDir = project.getBaseDir();
    assert baseDir != null;
    final Change[] changes = myForPerformConvertor.convert(e);
    if (changes == null || changes.length == 0)
        return;
    final List<Change> changesList = new ArrayList<>();
    Collections.addAll(changesList, changes);
    FileDocumentManager.getInstance().saveAllDocuments();
    String defaultName = null;
    final ChangeList[] changeLists = e.getData(VcsDataKeys.CHANGE_LISTS);
    if (changeLists != null && changeLists.length > 0) {
        defaultName = VcsBundle.message("revert.changes.default.name", changeLists[0].getName());
    }
    final ChangeListChooser chooser = new ChangeListChooser(project, ChangeListManager.getInstance(project).getChangeListsCopy(), null, "Select Target Changelist", defaultName);
    if (!chooser.showAndGet()) {
        return;
    }
    final List<FilePatch> patches = new ArrayList<>();
    ProgressManager.getInstance().run(new Task.Backgroundable(project, VcsBundle.message("revert.changes.title"), true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            try {
                final List<Change> preprocessed = ChangesPreprocess.preprocessChangesRemoveDeletedForDuplicateMoved(changesList);
                patches.addAll(IdeaTextPatchBuilder.buildPatch(project, preprocessed, baseDir.getPresentableUrl(), true));
            } catch (final VcsException ex) {
                WaitForProgressToShow.runOrInvokeLaterAboveProgress(new Runnable() {

                    @Override
                    public void run() {
                        Messages.showErrorDialog(project, "Failed to revert changes: " + ex.getMessage(), VcsBundle.message("revert.changes.title"));
                    }
                }, null, myProject);
                indicator.cancel();
            }
        }

        @Override
        public void onSuccess() {
            new PatchApplier<BinaryFilePatch>(project, baseDir, patches, chooser.getSelectedList(), null, null).execute();
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) ChangeListChooser(com.intellij.openapi.vcs.changes.ui.ChangeListChooser) ArrayList(java.util.ArrayList) Change(com.intellij.openapi.vcs.changes.Change) BinaryFilePatch(com.intellij.openapi.diff.impl.patch.BinaryFilePatch) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch) Project(com.intellij.openapi.project.Project) ChangeList(com.intellij.openapi.vcs.changes.ChangeList) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsException(com.intellij.openapi.vcs.VcsException) PatchApplier(com.intellij.openapi.diff.impl.patch.formove.PatchApplier) ArrayList(java.util.ArrayList) List(java.util.List) ChangeList(com.intellij.openapi.vcs.changes.ChangeList)

Example 3 with BinaryFilePatch

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

the class PatchingTestCase method applyPatch.

protected void applyPatch() throws Exception {
    PatchReader reader = PatchVirtualFileReader.create(LocalFileSystem.getInstance().refreshAndFindFileByPath(patchFilePath));
    List<FilePatch> patches = new ArrayList<>(reader.readTextPatches());
    new PatchApplier<BinaryFilePatch>(myProject, myRoot, patches, null, null, null).execute();
}
Also used : ArrayList(java.util.ArrayList) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch) BinaryFilePatch(com.intellij.openapi.diff.impl.patch.BinaryFilePatch) BinaryFilePatch(com.intellij.openapi.diff.impl.patch.BinaryFilePatch) PatchReader(com.intellij.openapi.diff.impl.patch.PatchReader)

Aggregations

BinaryFilePatch (com.intellij.openapi.diff.impl.patch.BinaryFilePatch)3 FilePatch (com.intellij.openapi.diff.impl.patch.FilePatch)3 ArrayList (java.util.ArrayList)2 BinaryEncoder (com.intellij.openapi.diff.impl.patch.BinaryEncoder)1 PatchReader (com.intellij.openapi.diff.impl.patch.PatchReader)1 PatchApplier (com.intellij.openapi.diff.impl.patch.formove.PatchApplier)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 VcsException (com.intellij.openapi.vcs.VcsException)1 Change (com.intellij.openapi.vcs.changes.Change)1 ChangeList (com.intellij.openapi.vcs.changes.ChangeList)1 ChangeListChooser (com.intellij.openapi.vcs.changes.ui.ChangeListChooser)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 List (java.util.List)1