2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.jetbrains.idea.svn;
19 import com.intellij.openapi.util.Pair;
20 import com.intellij.openapi.vcs.FilePath;
21 import com.intellij.openapi.vcs.VcsException;
22 import com.intellij.openapi.vcs.changes.ByteBackedContentRevision;
23 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
24 import com.intellij.openapi.vcs.impl.ContentRevisionCache;
25 import com.intellij.openapi.vcs.impl.CurrentRevisionProvider;
26 import org.jetbrains.annotations.NonNls;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 import org.jetbrains.idea.svn.status.Status;
30 import org.tmatesoft.svn.core.wc.SVNRevision;
31 import org.tmatesoft.svn.core.wc2.SvnTarget;
34 import java.io.IOException;
36 public class SvnContentRevision extends SvnBaseContentRevision implements ByteBackedContentRevision {
38 @NotNull private final SVNRevision myRevision;
40 * this flag is necessary since SVN would not do remote request only if constant SVNRevision.BASE
41 * -> usual current revision content class can't be used
43 private final boolean myUseBaseRevision;
45 protected SvnContentRevision(@NotNull SvnVcs vcs, @NotNull FilePath file, @NotNull SVNRevision revision, boolean useBaseRevision) {
47 myRevision = revision;
48 myUseBaseRevision = useBaseRevision;
52 public static SvnContentRevision createBaseRevision(@NotNull SvnVcs vcs, @NotNull FilePath file, @NotNull Status status) {
53 SVNRevision revision = status.getRevision().isValid() ? status.getRevision() : status.getCommittedRevision();
54 return createBaseRevision(vcs, file, revision);
58 public static SvnContentRevision createBaseRevision(@NotNull SvnVcs vcs, @NotNull FilePath file, @NotNull SVNRevision revision) {
59 if (file.getFileType().isBinary()) {
60 return new SvnBinaryContentRevision(vcs, file, revision, true);
62 return new SvnContentRevision(vcs, file, revision, true);
66 public static SvnContentRevision createRemote(@NotNull SvnVcs vcs, @NotNull FilePath file, @NotNull SVNRevision revision) {
67 if (file.getFileType().isBinary()) {
68 return new SvnBinaryContentRevision(vcs, file, revision, false);
70 return new SvnContentRevision(vcs, file, revision, false);
74 public String getContent() throws VcsException {
75 return ContentRevisionCache.getAsString(getContentAsBytes(), myFile, null);
80 public byte[] getContentAsBytes() throws VcsException {
82 if (myUseBaseRevision) {
83 return ContentRevisionCache.getOrLoadCurrentAsBytes(myVcs.getProject(), myFile, myVcs.getKeyInstanceMethod(),
84 new CurrentRevisionProvider() {
86 public VcsRevisionNumber getCurrentRevision() throws VcsException {
87 return getRevisionNumber();
91 public Pair<VcsRevisionNumber, byte[]> get()
92 throws VcsException, IOException {
93 return Pair.create(getRevisionNumber(), getUpToDateBinaryContent());
97 return ContentRevisionCache.getOrLoadAsBytes(myVcs.getProject(), myFile, getRevisionNumber(), myVcs.getKeyInstanceMethod(),
98 ContentRevisionCache.UniqueType.REPOSITORY_CONTENT, () -> getUpToDateBinaryContent());
101 catch (IOException e) {
102 throw new VcsException(e);
106 private byte[] getUpToDateBinaryContent() throws VcsException {
107 File file = myFile.getIOFile();
108 File lock = new File(file.getParentFile(), SvnUtil.PATH_TO_LOCK_FILE);
110 throw new VcsException("Can not access file base revision contents: administrative area is locked");
112 return SvnUtil.getFileContents(myVcs, SvnTarget.fromFile(file), myUseBaseRevision ? SVNRevision.BASE : myRevision,
113 SVNRevision.UNDEFINED);
117 public VcsRevisionNumber getRevisionNumber() {
118 return new SvnRevisionNumber(myRevision);
122 public String toString() {
123 return myFile.getPath();