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.VcsKey;
23 import com.intellij.openapi.vcs.changes.ByteBackedContentRevision;
24 import com.intellij.openapi.vcs.changes.MarkerVcsContentRevision;
25 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
26 import com.intellij.openapi.vcs.impl.ContentRevisionCache;
27 import com.intellij.openapi.vcs.impl.CurrentRevisionProvider;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31 import org.jetbrains.idea.svn.status.Status;
32 import org.tmatesoft.svn.core.wc.SVNRevision;
33 import org.tmatesoft.svn.core.wc2.SvnTarget;
36 import java.io.IOException;
38 public class SvnContentRevision extends SvnBaseContentRevision implements ByteBackedContentRevision, MarkerVcsContentRevision {
40 @NotNull private final SVNRevision myRevision;
42 * this flag is necessary since SVN would not do remote request only if constant SVNRevision.BASE
43 * -> usual current revision content class can't be used
45 private final boolean myUseBaseRevision;
47 protected SvnContentRevision(@NotNull SvnVcs vcs, @NotNull FilePath file, @NotNull SVNRevision revision, boolean useBaseRevision) {
49 myRevision = revision;
50 myUseBaseRevision = useBaseRevision;
54 public static SvnContentRevision createBaseRevision(@NotNull SvnVcs vcs, @NotNull FilePath file, @NotNull Status status) {
55 SVNRevision revision = status.getRevision().isValid() ? status.getRevision() : status.getCommittedRevision();
56 return createBaseRevision(vcs, file, revision);
60 public static SvnContentRevision createBaseRevision(@NotNull SvnVcs vcs, @NotNull FilePath file, @NotNull SVNRevision revision) {
61 if (file.getFileType().isBinary()) {
62 return new SvnBinaryContentRevision(vcs, file, revision, true);
64 return new SvnContentRevision(vcs, file, revision, true);
68 public static SvnContentRevision createRemote(@NotNull SvnVcs vcs, @NotNull FilePath file, @NotNull SVNRevision revision) {
69 if (file.getFileType().isBinary()) {
70 return new SvnBinaryContentRevision(vcs, file, revision, false);
72 return new SvnContentRevision(vcs, file, revision, false);
76 public String getContent() throws VcsException {
77 return ContentRevisionCache.getAsString(getContentAsBytes(), myFile, null);
82 public byte[] getContentAsBytes() throws VcsException {
84 if (myUseBaseRevision) {
85 return ContentRevisionCache.getOrLoadCurrentAsBytes(myVcs.getProject(), myFile, myVcs.getKeyInstanceMethod(),
86 new CurrentRevisionProvider() {
88 public VcsRevisionNumber getCurrentRevision() throws VcsException {
89 return getRevisionNumber();
93 public Pair<VcsRevisionNumber, byte[]> get()
94 throws VcsException, IOException {
95 return Pair.create(getRevisionNumber(), getUpToDateBinaryContent());
99 return ContentRevisionCache.getOrLoadAsBytes(myVcs.getProject(), myFile, getRevisionNumber(), myVcs.getKeyInstanceMethod(),
100 ContentRevisionCache.UniqueType.REPOSITORY_CONTENT, () -> getUpToDateBinaryContent());
103 catch (IOException e) {
104 throw new VcsException(e);
108 private byte[] getUpToDateBinaryContent() throws VcsException {
109 File file = myFile.getIOFile();
110 File lock = new File(file.getParentFile(), SvnUtil.PATH_TO_LOCK_FILE);
112 throw new VcsException("Can not access file base revision contents: administrative area is locked");
114 return SvnUtil.getFileContents(myVcs, SvnTarget.fromFile(file), myUseBaseRevision ? SVNRevision.BASE : myRevision,
115 SVNRevision.UNDEFINED);
119 public VcsRevisionNumber getRevisionNumber() {
120 return new SvnRevisionNumber(myRevision);
124 public String toString() {
125 return myFile.getPath();
129 public VcsKey getVcsKey() {
130 return SvnVcs.getKey();