Improve display of binary values in svn diff

This commit is contained in:
Sebastian Sdorra
2020-11-02 09:16:47 +01:00
parent 7e575036a3
commit 5de4b49392
2 changed files with 47 additions and 10 deletions

View File

@@ -391,8 +391,8 @@ public class SCMSvnDiffGenerator implements ISvnDiffGenerator {
SVNPropertyValue newValue = diff.getSVNPropertyValue(name); SVNPropertyValue newValue = diff.getSVNPropertyValue(name);
try { try {
byte[] originalValueBytes = getPropertyAsBytes(originalValue, getEncoding()); byte[] originalValueBytes = getPropertyAsBytes(originalValue, getEncoding(), true);
byte[] newValueBytes = getPropertyAsBytes(newValue, getEncoding()); byte[] newValueBytes = getPropertyAsBytes(newValue, getEncoding(), true);
if (originalValueBytes == null) { if (originalValueBytes == null) {
originalValueBytes = new byte[0]; originalValueBytes = new byte[0];
@@ -1190,6 +1190,10 @@ public class SCMSvnDiffGenerator implements ISvnDiffGenerator {
} }
private byte[] getPropertyAsBytes(SVNPropertyValue value, String encoding) { private byte[] getPropertyAsBytes(SVNPropertyValue value, String encoding) {
return getPropertyAsBytes(value, encoding, false);
}
private byte[] getPropertyAsBytes(SVNPropertyValue value, String encoding, boolean replaceBinary) {
if (value == null) { if (value == null) {
return null; return null;
} }
@@ -1200,7 +1204,11 @@ public class SCMSvnDiffGenerator implements ISvnDiffGenerator {
return value.getString().getBytes(); return value.getString().getBytes();
} }
} }
return value.getBytes(); if (replaceBinary) {
return String.format("Binary value (%s bytes)", value.getBytes().length).getBytes();
} else {
return value.getBytes();
}
} }
private void displayMergeInfoDiff(OutputStream outputStream, String oldValue, String newValue) throws SVNException, IOException { private void displayMergeInfoDiff(OutputStream outputStream, String oldValue, String newValue) throws SVNException, IOException {

View File

@@ -43,12 +43,16 @@ import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Base64;
import java.util.Map; import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
class SvnDiffCommandTest { class SvnDiffCommandTest {
// the smallest gif of the world
private static final String GIF = "R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";
private final SVNClientManager client = SVNClientManager.newInstance(); private final SVNClientManager client = SVNClientManager.newInstance();
private File repository; private File repository;
@@ -85,8 +89,6 @@ class SvnDiffCommandTest {
String diff = gitDiff("1"); String diff = gitDiff("1");
System.out.println(diff);
assertThat(diff).isEqualToIgnoringNewLines(String.join("\n", assertThat(diff).isEqualToIgnoringNewLines(String.join("\n",
"diff --git a/ b/", "diff --git a/ b/",
"--- a/", "--- a/",
@@ -114,8 +116,6 @@ class SvnDiffCommandTest {
String diff = gitDiff("2"); String diff = gitDiff("2");
System.out.println(diff);
assertThat(diff).isEqualToIgnoringNewLines(String.join("\n", assertThat(diff).isEqualToIgnoringNewLines(String.join("\n",
"diff --git a/ b/", "diff --git a/ b/",
"--- a/", "--- a/",
@@ -128,6 +128,26 @@ class SvnDiffCommandTest {
)); ));
} }
@Test
void shouldCreateGitCompatibleDiffForBinaryProps() throws SVNException, IOException {
createRepository();
byte[] gif = Base64.getDecoder().decode(GIF);
commitProperty("scm:gif", gif);
String diff = gitDiff("1");
assertThat(diff).isEqualToIgnoringNewLines(String.join("\n",
"diff --git a/ b/",
"--- a/",
"+++ b/",
"@@ -0,0 +1 @@",
" # property scm:gif has changed",
"+Binary value (43 bytes)",
"\\ No newline at end of property"
));
}
@Nonnull @Nonnull
private String gitDiff(String revision) throws IOException { private String gitDiff(String revision) throws IOException {
SvnDiffCommand command = createCommand(); SvnDiffCommand command = createCommand();
@@ -145,6 +165,15 @@ class SvnDiffCommandTest {
} }
private void commitProperty(String name, String value) throws SVNException { private void commitProperty(String name, String value) throws SVNException {
setProperty(name, SVNPropertyValue.create(value));
commit("set property " + name + " = " + value);
}
private void commitProperty(String name, byte[] value) throws SVNException {
commitProperty(name, SVNPropertyValue.create(name, value));
}
private void commitProperty(String name, SVNPropertyValue value) throws SVNException {
setProperty(name, value); setProperty(name, value);
commit("set property " + name + " = " + value); commit("set property " + name + " = " + value);
} }
@@ -162,11 +191,11 @@ class SvnDiffCommandTest {
); );
} }
private void setProperty(String name, String value) throws SVNException { private void setProperty(String name, SVNPropertyValue value) throws SVNException {
client.getWCClient().doSetProperty( client.getWCClient().doSetProperty(
workingCopy, workingCopy,
name, name,
SVNPropertyValue.create(value), value,
true, true,
SVNDepth.UNKNOWN, SVNDepth.UNKNOWN,
null, null,
@@ -176,7 +205,7 @@ class SvnDiffCommandTest {
private void commitProperties(Map<String, String> properties) throws SVNException { private void commitProperties(Map<String, String> properties) throws SVNException {
for (Map.Entry<String, String> e : properties.entrySet()) { for (Map.Entry<String, String> e : properties.entrySet()) {
setProperty(e.getKey(), e.getValue()); setProperty(e.getKey(), SVNPropertyValue.create(e.getValue()));
} }
commit("set " + properties.size() + " properties"); commit("set " + properties.size() + " properties");
} }