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);
}
}
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();
}
});
}
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();
}
Aggregations