IDEA-51710 (CVS Compare compares with wrong version)
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / CvsDiffProvider.java
blob2745699777ef91b59c19488fea1bbd9b77d2c4b7
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 com.intellij.cvsSupport2;
18 import com.intellij.cvsSupport2.application.CvsEntriesManager;
19 import com.intellij.cvsSupport2.changeBrowser.CvsBinaryContentRevision;
20 import com.intellij.cvsSupport2.changeBrowser.CvsContentRevision;
21 import com.intellij.cvsSupport2.connections.CvsConnectionSettings;
22 import com.intellij.cvsSupport2.cvsExecution.ModalityContext;
23 import com.intellij.cvsSupport2.cvsExecution.ModalityContextImpl;
24 import com.intellij.cvsSupport2.cvsoperations.common.CvsExecutionEnvironment;
25 import com.intellij.cvsSupport2.cvsoperations.common.PostCvsActivity;
26 import com.intellij.cvsSupport2.cvsoperations.cvsErrors.ErrorProcessor;
27 import com.intellij.cvsSupport2.cvsoperations.cvsLog.LocalPathIndifferentLogOperation;
28 import com.intellij.cvsSupport2.cvsoperations.cvsMessages.CvsMessagesAdapter;
29 import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDate;
30 import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.RevisionOrDateImpl;
31 import com.intellij.cvsSupport2.cvsoperations.dateOrRevision.SimpleRevision;
32 import com.intellij.cvsSupport2.history.CvsRevisionNumber;
33 import com.intellij.openapi.project.Project;
34 import com.intellij.openapi.util.Ref;
35 import com.intellij.openapi.util.text.StringUtil;
36 import com.intellij.openapi.vcs.FilePath;
37 import com.intellij.openapi.vcs.VcsException;
38 import com.intellij.openapi.vcs.changes.ContentRevision;
39 import com.intellij.openapi.vcs.diff.DiffProvider;
40 import com.intellij.openapi.vcs.diff.ItemLatestState;
41 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
42 import com.intellij.openapi.vfs.LocalFileSystem;
43 import com.intellij.openapi.vfs.VirtualFile;
44 import org.jetbrains.annotations.Nullable;
45 import org.netbeans.lib.cvsclient.admin.Entry;
46 import org.netbeans.lib.cvsclient.command.CommandAbortedException;
48 import java.io.File;
49 import java.util.Collection;
50 import java.util.List;
52 public class CvsDiffProvider implements DiffProvider{
53 private final Project myProject;
55 public CvsDiffProvider(final Project project) {
56 myProject = project;
59 public VcsRevisionNumber getCurrentRevision(VirtualFile file) {
60 final Entry entry = CvsEntriesManager.getInstance().getEntryFor(file);
61 if (entry == null) return null;
62 return new CvsRevisionNumber(entry.getRevision());
65 public ItemLatestState getLastRevision(VirtualFile virtualFile) {
66 //return getLastRevision(CvsVfsUtil.getFileFor(virtualFile));
67 if (virtualFile.getParent() == null) {
68 return new ItemLatestState(new CvsRevisionNumber("HEAD"), true, true);
70 return getLastState(virtualFile.getParent(), virtualFile.getName());
73 public ContentRevision createFileContent(final VcsRevisionNumber revisionNumber, VirtualFile selectedFile) {
74 if ((revisionNumber instanceof CvsRevisionNumber)) {
75 final CvsConnectionSettings settings = CvsEntriesManager.getInstance().getCvsConnectionSettingsFor(selectedFile.getParent());
76 final File file = new File(CvsUtil.getModuleName(selectedFile));
77 final CvsRevisionNumber cvsRevisionNumber = ((CvsRevisionNumber)revisionNumber);
78 final RevisionOrDate versionInfo;
79 if (cvsRevisionNumber.getDateOrRevision() != null) {
80 versionInfo = RevisionOrDateImpl.createOn(cvsRevisionNumber.getDateOrRevision());
82 else {
83 versionInfo = new SimpleRevision(cvsRevisionNumber.asString());
86 if (selectedFile.getFileType().isBinary()) {
87 return new CvsBinaryContentRevision(file, file, versionInfo, settings, myProject);
89 else {
90 return new CvsContentRevision(file, file, versionInfo, settings, myProject);
93 } else {
94 return null;
98 public ItemLatestState getLastRevision(FilePath filePath) {
99 //return getLastRevision(filePath.getIOFile());
100 VirtualFile parent = filePath.getVirtualFileParent();
101 if (parent == null) {
102 parent = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(filePath.getParentPath().getIOFile());
104 if (parent != null) {
105 return getLastState(parent, filePath.getName());
107 return new ItemLatestState(new CvsRevisionNumber("HEAD"), true, true);
110 public VcsRevisionNumber getLatestCommittedRevision(VirtualFile vcsRoot) {
111 // todo
112 return null;
115 private ItemLatestState getLastState(final VirtualFile parent, final String name) {
116 final Entry entry = CvsEntriesManager.getInstance().getEntryFor(parent, name);
117 if (entry == null) return new ItemLatestState(new CvsRevisionNumber("HEAD"), true, true);
119 String headRevision = null;
120 if (entry.getStickyDate() != null) {
121 headRevision = new StickyHeadGetter.MyStickyDateGetter(entry.getStickyDateString(), entry.getStickyDate(), entry.getRevision()).getHead(parent, name);
122 } else if (entry.getStickyRevision() != null) {
123 headRevision = entry.getStickyRevision();
124 } else if (entry.getStickyTag() != null) {
125 headRevision = new StickyHeadGetter.MyStickyBranchHeadGetter(entry.getRevision()).getHead(parent, name);
126 } else {
127 headRevision = new StickyHeadGetter.MyStickyBranchHeadGetter(entry.getRevision()).getHead(parent, name);
129 if (headRevision != null) {
130 return new ItemLatestState(new CvsRevisionNumber(headRevision), (! entry.isRemoved()), false);
133 return new ItemLatestState(new CvsRevisionNumber("HEAD"), (! entry.isRemoved()), true);
136 @Nullable
137 private String getBranchHeadRevision(final VirtualFile parent, final String name, final String currentRevNumber) {
138 final int[] subRevisions = new CvsRevisionNumber(currentRevNumber).getSubRevisions();
139 if (subRevisions == null || subRevisions.length < 2) return currentRevNumber;
141 final int[] top = new int[subRevisions.length - 1];
142 System.arraycopy(subRevisions, 1, top, 0, subRevisions.length - 1);
143 final String branchRoot = StringUtil.join(top, ".");
145 final LocalPathIndifferentLogOperation operation = new LocalPathIndifferentLogOperation(new File(parent.getPath(), name));
146 final Ref<Boolean> logSuccess = new Ref<Boolean>(Boolean.TRUE);
147 final ModalityContext context = ModalityContextImpl.NON_MODAL;
148 final CvsExecutionEnvironment cvsExecutionEnvironment = new CvsExecutionEnvironment(new CvsMessagesAdapter(),
149 CvsExecutionEnvironment.DUMMY_STOPPER, new ErrorProcessor() {
150 public void addError(VcsException ex) {
151 logSuccess.set(Boolean.FALSE);
153 public void addWarning(VcsException ex) {
155 public List getErrors() {
156 return null;
158 }, context, PostCvsActivity.DEAF);
159 try {
160 // should already be logged in
161 //operation.login(context);
162 operation.execute(cvsExecutionEnvironment);
164 catch (VcsException e) {
167 catch (CommandAbortedException e) {
170 if (Boolean.TRUE.equals(logSuccess.get())) {
171 final Collection<CvsRevisionNumber> numberCollection = operation.getAllRevisions();
172 if (numberCollection == null) return null;
174 for (CvsRevisionNumber revisionNumber : numberCollection) {
175 final String stringPresentation = revisionNumber.asString();
176 if (stringPresentation.startsWith(branchRoot)) {
177 return stringPresentation;
181 return null;