2 * Copyright 2000-2010 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
.cvsExecution
.ModalityContext
;
19 import com
.intellij
.cvsSupport2
.cvsExecution
.ModalityContextImpl
;
20 import com
.intellij
.cvsSupport2
.cvsoperations
.common
.CvsExecutionEnvironment
;
21 import com
.intellij
.cvsSupport2
.cvsoperations
.common
.PostCvsActivity
;
22 import com
.intellij
.cvsSupport2
.cvsoperations
.cvsErrors
.ErrorProcessor
;
23 import com
.intellij
.cvsSupport2
.cvsoperations
.cvsLog
.LocalPathIndifferentLogOperation
;
24 import com
.intellij
.cvsSupport2
.cvsoperations
.cvsMessages
.CvsMessagesAdapter
;
25 import com
.intellij
.cvsSupport2
.history
.CvsRevisionNumber
;
26 import com
.intellij
.openapi
.util
.Comparing
;
27 import com
.intellij
.openapi
.util
.Ref
;
28 import com
.intellij
.openapi
.util
.text
.StringUtil
;
29 import com
.intellij
.openapi
.vcs
.VcsException
;
30 import com
.intellij
.openapi
.vfs
.VirtualFile
;
31 import com
.intellij
.util
.containers
.Convertor
;
32 import org
.jetbrains
.annotations
.Nullable
;
33 import org
.netbeans
.lib
.cvsclient
.command
.CommandAbortedException
;
34 import org
.netbeans
.lib
.cvsclient
.command
.log
.LogInformation
;
35 import org
.netbeans
.lib
.cvsclient
.command
.log
.Revision
;
38 import java
.util
.Collection
;
39 import java
.util
.Date
;
40 import java
.util
.List
;
42 public abstract class StickyHeadGetter
{
43 protected final String myStickyData
;
45 protected StickyHeadGetter(final String stickyData
) {
46 myStickyData
= stickyData
;
49 public static class MyStickyBranchHeadGetter
extends StickyHeadGetter
{
50 public MyStickyBranchHeadGetter(final String headRevision
) {
55 public String
getHead(final VirtualFile parent
, final String name
) {
56 final String branchRoot
= getTagStart(myStickyData
);
57 if (branchRoot
== null) return myStickyData
;
59 return getBranchHeadRevision(parent
, name
, new Convertor
<CvsRevisionNumber
, Boolean
>() {
60 public Boolean
convert(CvsRevisionNumber o
) {
61 return o
.asString().startsWith(branchRoot
);
68 private static String
getTagStart(final String currentRevision
) {
69 final int[] subRevisions
= new CvsRevisionNumber(currentRevision
).getSubRevisions();
70 if (subRevisions
== null || subRevisions
.length
< 2) return null;
72 final int[] top
= new int[subRevisions
.length
- 1];
73 System
.arraycopy(subRevisions
, 0, top
, 0, subRevisions
.length
- 1);
74 return StringUtil
.join(top
, ".");
77 public static class MyStickyTagGetter
extends StickyHeadGetter
{
78 public MyStickyTagGetter(final String stickyData
) {
83 public String
getHead(final VirtualFile parent
, final String name
) {
88 public static class MyStickyDateGetter
extends StickyHeadGetter
{
89 private final Date myStickyDate
;
90 private String myTagStart
;
92 public MyStickyDateGetter(String stickyData
, final Date stickyDate
, final String currentRevision
) {
94 myStickyDate
= stickyDate
;
95 myTagStart
= getTagStart(currentRevision
);
99 public String
getHead(final VirtualFile parent
, final String name
) {
100 if (myTagStart
== null) return myStickyData
;
101 return getBranchHeadRevision(parent
, name
, null);
105 protected String
extractRevision(LocalPathIndifferentLogOperation operation
, Convertor
<CvsRevisionNumber
, Boolean
> chooser
) {
106 final List
<LogInformation
> informations
= operation
.getLogInformationList();
107 for (LogInformation information
: informations
) {
108 final List
<Revision
> revisionList
= information
.getRevisionList();
109 for (Revision revision
: revisionList
) {
110 if (revision
.getNumber().startsWith(myTagStart
)) {
111 if (Comparing
.compare(revision
.getDate(), myStickyDate
) <= 0) {
112 return revision
.getNumber();
121 public abstract String
getHead(final VirtualFile parent
, final String name
);
124 protected String
getBranchHeadRevision(final VirtualFile parent
, final String name
, final Convertor
<CvsRevisionNumber
, Boolean
> chooser
) {
125 final LocalPathIndifferentLogOperation operation
= new LocalPathIndifferentLogOperation(new File(parent
.getPath(), name
));
126 final Ref
<Boolean
> logSuccess
= new Ref
<Boolean
>(Boolean
.TRUE
);
127 final ModalityContext context
= ModalityContextImpl
.NON_MODAL
;
128 final CvsExecutionEnvironment cvsExecutionEnvironment
= new CvsExecutionEnvironment(new CvsMessagesAdapter(),
129 CvsExecutionEnvironment
.DUMMY_STOPPER
, new ErrorProcessor() {
130 public void addError(VcsException ex
) {
131 logSuccess
.set(Boolean
.FALSE
);
133 public void addWarning(VcsException ex
) {
135 public List
getErrors() {
138 }, context
, PostCvsActivity
.DEAF
);
140 // should already be logged in
141 //operation.login(context);
142 operation
.execute(cvsExecutionEnvironment
);
144 catch (VcsException e
) {
147 catch (CommandAbortedException e
) {
150 if (Boolean
.TRUE
.equals(logSuccess
.get())) {
151 return extractRevision(operation
, chooser
);
157 protected String
extractRevision(final LocalPathIndifferentLogOperation operation
, final Convertor
<CvsRevisionNumber
, Boolean
> chooser
) {
158 final Collection
<CvsRevisionNumber
> numberCollection
= operation
.getAllRevisions();
159 if (numberCollection
== null) return null;
161 for (CvsRevisionNumber revisionNumber
: numberCollection
) {
162 final String stringPresentation
= revisionNumber
.asString();
163 if (chooser
.convert(revisionNumber
)) {
164 return stringPresentation
;