vcs: unify ByteBackedContentRevision usages and implementations

This commit is contained in:
Aleksey Pivovarov
2017-04-19 16:11:45 +03:00
committed by Aleksey Pivovarov
parent 79a4b7aa97
commit be1885f5d5
10 changed files with 54 additions and 47 deletions
@@ -16,13 +16,13 @@
package com.intellij.openapi.vcs.changes;
import org.jetbrains.annotations.Nullable;
import com.intellij.openapi.vcs.VcsException;
import org.jetbrains.annotations.Nullable;
/**
* @author yole
*/
public interface BinaryContentRevision extends ContentRevision {
public interface BinaryContentRevision extends ByteBackedContentRevision {
/**
* Content of the revision. Implementers are encouraged to lazy implement this especially when it requires connection to the
* version control server or something.
@@ -33,4 +33,10 @@ public interface BinaryContentRevision extends ContentRevision {
*/
@Nullable
byte[] getBinaryContent() throws VcsException;
@Nullable
@Override
default byte[] getContentAsBytes() throws VcsException {
return getBinaryContent();
}
}
@@ -18,11 +18,8 @@ package com.intellij.openapi.vcs.changes;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NonNls;
import java.io.IOException;
import org.jetbrains.annotations.Nullable;
/**
* @author yole
@@ -34,14 +31,7 @@ public class CurrentBinaryContentRevision extends CurrentContentRevision impleme
@Nullable
public byte[] getBinaryContent() throws VcsException {
final VirtualFile vFile = getVirtualFile();
if (vFile == null) return null;
try {
return vFile.contentsToByteArray();
}
catch (IOException e) {
throw new VcsException(e);
}
return getContentAsBytes();
}
@NonNls
@@ -21,16 +21,19 @@ import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vcs.FilePath;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.history.VcsRevisionNumber;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
/**
* @author max
*/
public class CurrentContentRevision implements ContentRevision {
public class CurrentContentRevision implements ByteBackedContentRevision {
protected FilePath myFile;
public CurrentContentRevision(final FilePath file) {
@@ -51,6 +54,21 @@ public class CurrentContentRevision implements ContentRevision {
return doc.getText();
}
@Nullable
@Override
public byte[] getContentAsBytes() throws VcsException {
final VirtualFile vFile = getVirtualFile();
if (vFile == null) {
return null;
}
try {
return vFile.contentsToByteArray();
}
catch (IOException e) {
throw new VcsException(e);
}
}
@Nullable
public VirtualFile getVirtualFile() {
final VirtualFile vFile = myFile.getVirtualFile();
@@ -23,7 +23,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author max
* Implementers are encouraged to also implement {@link ByteBackedContentRevision}
*/
public interface ContentRevision {
/**
@@ -36,7 +36,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vcs.*;
import com.intellij.openapi.vcs.changes.BinaryContentRevision;
import com.intellij.openapi.vcs.changes.ByteBackedContentRevision;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vcs.diff.DiffProvider;
@@ -51,7 +50,7 @@ import org.jetbrains.annotations.Nullable;
import java.io.IOException;
// TODO: remove duplication with ChangeDiffRequestPresentable
// TODO: remove duplication with ChangeDiffRequestProducer
public abstract class DiffActionExecutor {
protected final DiffProvider myDiffProvider;
protected final VirtualFile mySelectedFile;
@@ -74,12 +73,7 @@ public abstract class DiffActionExecutor {
DiffContentFactoryEx contentFactory = DiffContentFactoryEx.getInstanceEx();
DiffContent diffContent;
if (fileRevision instanceof BinaryContentRevision) {
final byte[] content = ((BinaryContentRevision)fileRevision).getBinaryContent();
if (content == null) return null;
diffContent = contentFactory.createFromBytes(myProject, content, fileRevision.getFile());
}
else if (fileRevision instanceof ByteBackedContentRevision) {
if (fileRevision instanceof ByteBackedContentRevision) {
byte[] content = ((ByteBackedContentRevision)fileRevision).getContentAsBytes();
if (content == null) throw new VcsException("Failed to load content");
diffContent = contentFactory.createFromBytes(myProject, content, fileRevision.getFile());
@@ -29,7 +29,7 @@ import org.jetbrains.annotations.Nullable;
import java.io.IOException;
public class VcsCurrentRevisionProxy implements ContentRevision {
public class VcsCurrentRevisionProxy implements ByteBackedContentRevision {
@NotNull private final DiffProvider myDiffProvider;
@NotNull private final VirtualFile myFile;
@NotNull private final Project myProject;
@@ -59,7 +59,13 @@ public class VcsCurrentRevisionProxy implements ContentRevision {
@Nullable
public String getContent() throws VcsException {
return ContentRevisionCache.getAsString(getVcsRevision().second, getFile(), null);
return ContentRevisionCache.getAsString(getContentAsBytes(), getFile(), myFile.getCharset());
}
@Nullable
@Override
public byte[] getContentAsBytes() throws VcsException {
return getVcsRevision().second;
}
@NotNull
@@ -118,6 +124,15 @@ public class VcsCurrentRevisionProxy implements ContentRevision {
throw new VcsException("Failed to create content for current revision");
}
return Pair.create(currentRevision, contentRevision.getContent().getBytes(myFile.getCharset()));
byte[] bytes;
if (contentRevision instanceof ByteBackedContentRevision) {
bytes = ((ByteBackedContentRevision)contentRevision).getContentAsBytes();
}
else {
String content = contentRevision.getContent();
if (content == null) throw new VcsException("Can't get revision content");
bytes = content.getBytes(myFile.getCharset());
}
return Pair.create(currentRevision, bytes);
}
}
@@ -372,14 +372,6 @@ public class ChangeDiffRequestProducer implements DiffRequestProducer {
return contentFactory.create(project, vFile);
}
if (revision instanceof BinaryContentRevision) {
byte[] content = ((BinaryContentRevision)revision).getBinaryContent();
if (content == null) {
throw new DiffRequestProducerException("Can't get binary revision content");
}
return contentFactory.createFromBytes(project, content, filePath);
}
if (revision instanceof ByteBackedContentRevision) {
byte[] revisionContent = ((ByteBackedContentRevision)revision).getContentAsBytes();
if (revisionContent == null) throw new DiffRequestProducerException("Can't get revision content");
@@ -21,7 +21,7 @@ import com.google.common.io.Files;
import com.intellij.openapi.util.Couple;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.BinaryContentRevision;
import com.intellij.openapi.vcs.changes.ByteBackedContentRevision;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.util.ObjectUtils;
@@ -67,8 +67,8 @@ public class BlobIndexUtil {
@NotNull
private static byte[] getContentBytes(@NotNull ContentRevision revision, @NotNull Charset charset) throws VcsException {
byte[] binaryContent;
if (revision instanceof BinaryContentRevision) {
binaryContent = ((BinaryContentRevision)revision).getBinaryContent();
if (revision instanceof ByteBackedContentRevision) {
binaryContent = ((ByteBackedContentRevision)revision).getContentAsBytes();
}
else {
String stringContent = revision.getContent();
@@ -33,9 +33,6 @@ public class GitBinaryContentRevision extends GitContentRevision implements Bina
@Override
public byte[] getBinaryContent() throws VcsException {
if (myFile.isDirectory()) {
return null;
}
return getContentAsBytes();
}
}
@@ -26,17 +26,12 @@ import org.jetbrains.idea.svn.SvnVcs;
* @author yole
*/
public class SvnRepositoryBinaryContentRevision extends SvnRepositoryContentRevision implements BinaryContentRevision {
private byte[] myBinaryContent;
public SvnRepositoryBinaryContentRevision(@NotNull SvnVcs vcs, @NotNull FilePath remotePath, @Nullable FilePath localPath, long revision) {
super(vcs, remotePath, localPath, revision);
}
@Nullable
public byte[] getBinaryContent() throws VcsException {
if (myBinaryContent == null) {
myBinaryContent = loadContent().toByteArray();
}
return myBinaryContent;
return getContentAsBytes();
}
}