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
;
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
) {
43 public VcsRevisionNumber
getCurrentRevision(VirtualFile file
) {
44 final SVNStatusClient client
= myVcs
.createStatusClient();
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
58 private static ItemLatestState
defaultResult() {
59 return createResult(SVNRevision
.HEAD
, true);
62 private static ItemLatestState
createResult(final SVNRevision revision
, final boolean exists
) {
63 return new ItemLatestState(new SvnRevisionNumber(revision
), exists
);
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();
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
) {
95 private ItemLatestState
getLastRevision(final File file
) {
96 final SVNStatusClient client
= myVcs
.createStatusClient();
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());
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
);
112 final SVNRevision remoteRevision
= svnStatus
.getRemoteRevision();
113 if (remoteRevision
!= null) {
114 return createResult(remoteRevision
, exists
);
116 return createResult(svnStatus
.getCommittedRevision(), exists
);
118 catch (SVNException e
) {
119 LOG
.debug(e
); // most likely the file is unversioned
120 return defaultResult();