IDEA-26360 (Performance and inconsistency issues with svn:externals and "Detect neste...
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / SvnDiffProvider.java
blob00f780860c0ac02d68a080f66cd429644f419c8c
1 /*
2 * Copyright 2000-2009 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.
16 package org.jetbrains.idea.svn;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.vcs.FilePath;
20 import com.intellij.openapi.vcs.actions.VcsContextFactory;
21 import com.intellij.openapi.vcs.changes.ContentRevision;
22 import com.intellij.openapi.vcs.diff.DiffProvider;
23 import com.intellij.openapi.vcs.diff.ItemLatestState;
24 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import org.jetbrains.idea.svn.history.LatestExistentSearcher;
27 import org.tmatesoft.svn.core.SVNException;
28 import org.tmatesoft.svn.core.wc.SVNRevision;
29 import org.tmatesoft.svn.core.wc.SVNStatus;
30 import org.tmatesoft.svn.core.wc.SVNStatusClient;
31 import org.tmatesoft.svn.core.wc.SVNStatusType;
33 import java.io.File;
35 public class SvnDiffProvider implements DiffProvider {
36 private static final Logger LOG = Logger.getInstance("#org.jetbrains.idea.svn.SvnDiffProvider");
37 private final SvnVcs myVcs;
39 public SvnDiffProvider(final SvnVcs vcs) {
40 myVcs = vcs;
43 public VcsRevisionNumber getCurrentRevision(VirtualFile file) {
44 final SVNStatusClient client = myVcs.createStatusClient();
45 try {
46 final SVNStatus svnStatus = client.doStatus(new File(file.getPresentableUrl()), false, false);
47 if (svnStatus.getCommittedRevision().equals(SVNRevision.UNDEFINED) && svnStatus.isCopied()) {
48 return new SvnRevisionNumber(svnStatus.getCopyFromRevision());
50 return new SvnRevisionNumber(svnStatus.getCommittedRevision());
52 catch (SVNException e) {
53 LOG.debug(e); // most likely the file is unversioned
54 return null;
58 private static ItemLatestState defaultResult() {
59 return createResult(SVNRevision.HEAD, true, true);
62 private static ItemLatestState createResult(final SVNRevision revision, final boolean exists, boolean defaultHead) {
63 return new ItemLatestState(new SvnRevisionNumber(revision), exists, defaultHead);
66 public ItemLatestState getLastRevision(VirtualFile file) {
67 return getLastRevision(new File(file.getPath()));
70 public ContentRevision createFileContent(final VcsRevisionNumber revisionNumber, final VirtualFile selectedFile) {
71 final SVNRevision svnRevision = ((SvnRevisionNumber)revisionNumber).getRevision();
72 FilePath filePath = VcsContextFactory.SERVICE.getInstance().createFilePathOn(selectedFile);
73 final SVNStatusClient client = myVcs.createStatusClient();
74 try {
75 final SVNStatus svnStatus = client.doStatus(new File(selectedFile.getPresentableUrl()), false, false);
76 if (svnRevision.equals(svnStatus.getCommittedRevision())) {
77 return SvnContentRevision.create(myVcs, filePath, svnRevision);
80 catch (SVNException e) {
81 LOG.debug(e); // most likely the file is unversioned
83 return SvnContentRevision.createRemote(myVcs, filePath, svnRevision);
86 public ItemLatestState getLastRevision(FilePath filePath) {
87 return getLastRevision(filePath.getIOFile());
90 public VcsRevisionNumber getLatestCommittedRevision(VirtualFile vcsRoot) {
91 // todo
92 return null;
95 private ItemLatestState getLastRevision(final File file) {
96 final SVNStatusClient client = myVcs.createStatusClient();
97 try {
98 final SVNStatus svnStatus = client.doStatus(file, true, false);
99 if (svnStatus == null) {
100 // IDEADEV-21785 (no idea why this can happen)
101 LOG.info("No SVN status returned for " + file.getPath());
102 return defaultResult();
104 final boolean exists = ! SVNStatusType.STATUS_DELETED.equals(svnStatus.getRemoteContentsStatus());
105 if (! exists) {
106 // get really latest revision
107 final LatestExistentSearcher searcher = new LatestExistentSearcher(myVcs, svnStatus.getURL());
108 final long revision = searcher.getDeletionRevision();
110 return createResult(SVNRevision.create(revision), exists, false);
112 final SVNRevision remoteRevision = svnStatus.getRemoteRevision();
113 if (remoteRevision != null) {
114 return createResult(remoteRevision, exists, false);
116 return createResult(svnStatus.getCommittedRevision(), exists, false);
118 catch (SVNException e) {
119 LOG.debug(e); // most likely the file is unversioned
120 return defaultResult();