Skip to content
Permalink
Browse files

Handle binary patches in webrev

Reviewed-by: rwestberg
  • Loading branch information
Erik Helin
Erik Helin committed Sep 20, 2019
1 parent 35ba541 commit 1721a19a88b0ddf9915ed40c08158ed1e1426df2
@@ -79,7 +79,10 @@ public void render(Writer w) throws IOException {
var rawView = new RawView(out, patch.target().path().get(), newContent);
rawView.render(w);
} else {
w.write("------ ------ ------ ------ --- --- ----- ");
w.write("------ ------ ------ ------ --- --- ");

var addedPatchView = new AddedPatchView(out, patch.target().path().get(), patch.asBinaryPatch());
addedPatchView.render(w);

var rawView = new RawView(out, patch.target().path().get(), binaryContent);
rawView.render(w);
@@ -23,40 +23,77 @@
package org.openjdk.skara.webrev;

import org.openjdk.skara.vcs.TextualPatch;
import org.openjdk.skara.vcs.BinaryPatch;

import java.io.*;
import java.nio.file.*;

class AddedPatchView implements View {
private final Path out;
private final Path file;
private final TextualPatch patch;
private final TextualPatch textualPatch;
private final BinaryPatch binaryPatch;

public AddedPatchView(Path out, Path file, TextualPatch patch) {
this.out = out;
this.file = file;
this.patch = patch;
this.textualPatch = patch;
this.binaryPatch = null;
}

public AddedPatchView(Path out, Path file, BinaryPatch patch) {
this.out = out;
this.file = file;
this.textualPatch = null;
this.binaryPatch = patch;
}

@Override
public void render(Writer w) throws IOException {
var patchFile = out.resolve(file.toString() + ".patch");
Files.createDirectories(patchFile.getParent());

if (binaryPatch != null) {
renderBinary(patchFile);
} else {
renderTextual(patchFile);
}

w.write("<a href=\"");
w.write(Webrev.relativeToIndex(out, patchFile));
w.write("\">Patch</a>\n");
}

private void renderBinary(Path patchFile) throws IOException {
try (var fw = Files.newBufferedWriter(patchFile)) {
var targetPath = ViewUtils.pathWithUnixSeps(binaryPatch.target().path().get());
fw.write("diff a/");
fw.write(ViewUtils.pathWithUnixSeps(patch.target().path().get()));
fw.write(targetPath);
fw.write(" b/");
fw.write(ViewUtils.pathWithUnixSeps(patch.target().path().get()));
fw.write(targetPath);
fw.write("\n");
fw.write("Binary files /dev/null and ");
fw.write(targetPath);
fw.write(" differ\n");
}
}

private void renderTextual(Path patchFile) throws IOException {
try (var fw = Files.newBufferedWriter(patchFile)) {
fw.write("diff a/");
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.target().path().get()));
fw.write(" b/");
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.target().path().get()));
fw.write("\n");
fw.write("--- /dev/null");
fw.write("\n");
fw.write("+++ b/");
fw.write(ViewUtils.pathWithUnixSeps(patch.target().path().get()));
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.target().path().get()));
fw.write("\n");

assert patch.hunks().size() == 1;
assert textualPatch.hunks().size() == 1;

var hunk = patch.hunks().get(0);
var hunk = textualPatch.hunks().get(0);

assert hunk.source().range().start() == 0;
assert hunk.source().range().count() == 0;
@@ -78,10 +115,6 @@ public void render(Writer w) throws IOException {
fw.write("\n");
}
}

w.write("<a href=\"");
w.write(Webrev.relativeToIndex(out, patchFile));
w.write("\">Patch</a>\n");
}
}

@@ -120,7 +120,10 @@ private void renderTextual(Writer w) throws IOException {
}

private void renderBinary(Writer w) throws IOException {
w.write("------ ------ ------ ------ --- --- ----- ");
w.write("------ ------ ------ ------ --- --- ");

var patchView = new PatchView(out, patch.target().path().get(), patch.asBinaryPatch());
patchView.render(w);

var rawView = new RawView(out, patch.target().path().get(), binaryContent);
rawView.render(w);
@@ -23,6 +23,7 @@
package org.openjdk.skara.webrev;

import org.openjdk.skara.vcs.TextualPatch;
import org.openjdk.skara.vcs.BinaryPatch;

import java.io.*;
import java.nio.file.*;
@@ -31,44 +32,86 @@
class PatchView implements View {
private final Path out;
private final Path file;
private final TextualPatch patch;
private final TextualPatch textualPatch;
private final BinaryPatch binaryPatch;
private final List<String> sourceContent;
private final List<String> destContent;
private static final int NUM_CONTEXT_LINES = 5;

public PatchView(Path out, Path file, TextualPatch patch, List<String> sourceContent, List<String> destContent) {
this.out = out;
this.file = file;
this.patch = patch;
this.textualPatch = patch;
this.binaryPatch = null;
this.sourceContent = sourceContent;
this.destContent = destContent;
}

public PatchView(Path out, Path file, BinaryPatch patch) {
this.out = out;
this.file = file;
this.textualPatch = null;
this.binaryPatch = patch;
this.sourceContent = null;
this.destContent = null;
}

private void writeLine(Writer w, String prepend, Line line) throws IOException {
w.write(prepend);
w.write(line.text());
w.write("\n");
}

@Override
public void render(Writer w) throws IOException {
var patchFile = out.resolve(file.toString() + ".patch");
Files.createDirectories(patchFile.getParent());

if (binaryPatch != null) {
renderBinary(patchFile);
} else {
renderTextual(patchFile);
}

w.write("<a href=\"");
w.write(Webrev.relativeToIndex(out, patchFile));
w.write("\">Patch</a>\n");
}

private void renderBinary(Path patchFile) throws IOException {
try (var fw = Files.newBufferedWriter(patchFile)) {
var sourcePath = ViewUtils.pathWithUnixSeps(binaryPatch.source().path().get());
var targetPath = ViewUtils.pathWithUnixSeps(binaryPatch.target().path().get());
fw.write("diff a/");
fw.write(ViewUtils.pathWithUnixSeps(patch.source().path().get()));
fw.write(sourcePath);
fw.write(" b/");
fw.write(ViewUtils.pathWithUnixSeps(patch.target().path().get()));
fw.write(targetPath);
fw.write("\n");
fw.write("Binary files ");
fw.write(sourcePath);
fw.write(" and ");
fw.write(targetPath);
fw.write(" differ\n");
}

}

private void renderTextual(Path patchFile) throws IOException {
try (var fw = Files.newBufferedWriter(patchFile)) {
fw.write("diff a/");
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.source().path().get()));
fw.write(" b/");
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.target().path().get()));
fw.write("\n");
fw.write("--- a/");
fw.write(ViewUtils.pathWithUnixSeps(patch.source().path().get()));
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.source().path().get()));
fw.write("\n");
fw.write("+++ b/");
fw.write(ViewUtils.pathWithUnixSeps(patch.target().path().get()));
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.target().path().get()));
fw.write("\n");

var coalescer = new HunkCoalescer(NUM_CONTEXT_LINES, sourceContent, destContent);
for (var group : coalescer.coalesce(patch.hunks())) {
for (var group : coalescer.coalesce(textualPatch.hunks())) {
var sourceRange = group.header().source();
var destRange = group.header().target();

@@ -99,9 +142,5 @@ public void render(Writer w) throws IOException {
}
}
}

w.write("<a href=\"");
w.write(Webrev.relativeToIndex(out, patchFile));
w.write("\">Patch</a>\n");
}
}
@@ -72,7 +72,10 @@ public void render(Writer w) throws IOException {
var rawView = new RawView(out, patch.source().path().get(), oldContent);
rawView.render(w);
} else {
w.write("------ ------ ------ ------ --- --- ----- ");
w.write("------ ------ ------ ------ --- --- ");

var patchView = new RemovedPatchView(out, patch.source().path().get(), patch.asBinaryPatch());
patchView.render(w);

var rawView = new RawView(out, patch.source().path().get(), binaryContent);
rawView.render(w);
@@ -23,39 +23,77 @@
package org.openjdk.skara.webrev;

import org.openjdk.skara.vcs.TextualPatch;
import org.openjdk.skara.vcs.BinaryPatch;

import java.io.*;
import java.nio.file.*;

class RemovedPatchView implements View {
private final Path out;
private final Path file;
private final TextualPatch patch;
private final TextualPatch textualPatch;
private final BinaryPatch binaryPatch;

public RemovedPatchView(Path out, Path file, TextualPatch patch) {
this.out = out;
this.file = file;
this.patch = patch;
this.textualPatch = patch;
this.binaryPatch = null;
}

public RemovedPatchView(Path out, Path file, BinaryPatch patch) {
this.out = out;
this.file = file;
this.textualPatch = null;
this.binaryPatch = patch;
}

@Override
public void render(Writer w) throws IOException {
var patchFile = out.resolve(file.toString() + ".patch");
Files.createDirectories(patchFile.getParent());

if (binaryPatch != null) {
renderBinary(patchFile);
} else {
renderTextual(patchFile);
}

w.write("<a href=\"");
w.write(Webrev.relativeToIndex(out, patchFile));
w.write("\">Patch</a>\n");
}

private void renderBinary(Path patchFile) throws IOException {
try (var fw = Files.newBufferedWriter(patchFile)) {
var sourcePath = ViewUtils.pathWithUnixSeps(binaryPatch.source().path().get());
fw.write("diff a/");
fw.write(ViewUtils.pathWithUnixSeps(patch.source().path().get()));
fw.write(sourcePath);
fw.write(" b/");
fw.write(ViewUtils.pathWithUnixSeps(patch.source().path().get()));
fw.write(sourcePath);
fw.write("\n");
fw.write("Binary files ");
fw.write(sourcePath);
fw.write(" and /dev/null differ\n");
}
}

private void renderTextual(Path patchFile) throws IOException {
try (var fw = Files.newBufferedWriter(patchFile)) {
fw.write("diff a/");
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.source().path().get()));
fw.write(" b/");
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.source().path().get()));
fw.write("\n");
fw.write("--- a/");
fw.write(ViewUtils.pathWithUnixSeps(patch.source().path().get()));
fw.write(ViewUtils.pathWithUnixSeps(textualPatch.source().path().get()));
fw.write("\n");
fw.write("+++ /dev/null");
fw.write("\n");

assert patch.hunks().size() == 1;
assert textualPatch.hunks().size() == 1;

var hunk = patch.hunks().get(0);
var hunk = textualPatch.hunks().get(0);

assert hunk.target().range().start() == 0;
assert hunk.target().range().count() == 0;
@@ -77,10 +115,6 @@ public void render(Writer w) throws IOException {
fw.write("\n");
}
}

w.write("<a href=\"");
w.write(Webrev.relativeToIndex(out, patchFile));
w.write("\">Patch</a>\n");
}
}

0 comments on commit 1721a19

Please sign in to comment.
You can’t perform that action at this time.