From be1885f5d533b766352e1502e1aeea7327dd0560 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Tue, 18 Apr 2017 13:17:34 +0300 Subject: [PATCH] vcs: unify ByteBackedContentRevision usages and implementations --- .../vcs/changes/BinaryContentRevision.java | 10 +++++++-- .../changes/CurrentBinaryContentRevision.java | 14 ++----------- .../vcs/changes/CurrentContentRevision.java | 20 +++++++++++++++++- .../openapi/vcs/changes/ContentRevision.java | 2 +- .../vcs/actions/DiffActionExecutor.java | 10 ++------- .../vcs/changes/VcsCurrentRevisionProxy.java | 21 ++++++++++++++++--- .../diff/ChangeDiffRequestProducer.java | 8 ------- .../vcs/changes/patch/BlobIndexUtil.java | 6 +++--- .../git4idea/GitBinaryContentRevision.java | 3 --- .../SvnRepositoryBinaryContentRevision.java | 7 +------ 10 files changed, 54 insertions(+), 47 deletions(-) diff --git a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/BinaryContentRevision.java b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/BinaryContentRevision.java index 94b9e3aea565..2bc9ddb64994 100644 --- a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/BinaryContentRevision.java +++ b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/BinaryContentRevision.java @@ -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(); + } } \ No newline at end of file diff --git a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/CurrentBinaryContentRevision.java b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/CurrentBinaryContentRevision.java index 83c693408672..3b582eed5cb8 100644 --- a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/CurrentBinaryContentRevision.java +++ b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/CurrentBinaryContentRevision.java @@ -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 diff --git a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/CurrentContentRevision.java b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/CurrentContentRevision.java index b7f01e0815f3..c51a19fb85b6 100644 --- a/platform/vcs-api/src/com/intellij/openapi/vcs/changes/CurrentContentRevision.java +++ b/platform/vcs-api/src/com/intellij/openapi/vcs/changes/CurrentContentRevision.java @@ -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(); diff --git a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/vcs/changes/ContentRevision.java b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/vcs/changes/ContentRevision.java index 716cb39da651..7b181025daa3 100644 --- a/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/vcs/changes/ContentRevision.java +++ b/platform/vcs-api/vcs-api-core/src/com/intellij/openapi/vcs/changes/ContentRevision.java @@ -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 { /** diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/DiffActionExecutor.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/DiffActionExecutor.java index 80e5f7060369..21b2769f7bd7 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/DiffActionExecutor.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/actions/DiffActionExecutor.java @@ -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()); diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsCurrentRevisionProxy.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsCurrentRevisionProxy.java index 55b157a61de2..629e9c2f5665 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsCurrentRevisionProxy.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/VcsCurrentRevisionProxy.java @@ -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); } } diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/ChangeDiffRequestProducer.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/ChangeDiffRequestProducer.java index 2c0c909ba7bf..976c521783d3 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/ChangeDiffRequestProducer.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/actions/diff/ChangeDiffRequestProducer.java @@ -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"); diff --git a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/BlobIndexUtil.java b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/BlobIndexUtil.java index b7f36edb28e5..2a65b4139609 100644 --- a/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/BlobIndexUtil.java +++ b/platform/vcs-impl/src/com/intellij/openapi/vcs/changes/patch/BlobIndexUtil.java @@ -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(); diff --git a/plugins/git4idea/src/git4idea/GitBinaryContentRevision.java b/plugins/git4idea/src/git4idea/GitBinaryContentRevision.java index 163930f0ef13..0b01ea0a2e9a 100644 --- a/plugins/git4idea/src/git4idea/GitBinaryContentRevision.java +++ b/plugins/git4idea/src/git4idea/GitBinaryContentRevision.java @@ -33,9 +33,6 @@ public class GitBinaryContentRevision extends GitContentRevision implements Bina @Override public byte[] getBinaryContent() throws VcsException { - if (myFile.isDirectory()) { - return null; - } return getContentAsBytes(); } } diff --git a/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnRepositoryBinaryContentRevision.java b/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnRepositoryBinaryContentRevision.java index 01633653aab2..6c46e4e86119 100644 --- a/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnRepositoryBinaryContentRevision.java +++ b/plugins/svn4idea/src/org/jetbrains/idea/svn/history/SvnRepositoryBinaryContentRevision.java @@ -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(); } }