View Javadoc
1   package org.apache.maven.scm.provider.accurev.command.update;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *    http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Date;
24  import java.util.List;
25  
26  import org.apache.maven.scm.CommandParameter;
27  import org.apache.maven.scm.CommandParameters;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmFileStatus;
31  import org.apache.maven.scm.ScmResult;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.update.UpdateScmResult;
34  import org.apache.maven.scm.log.ScmLogger;
35  import org.apache.maven.scm.provider.ScmProviderRepository;
36  import org.apache.maven.scm.provider.accurev.AccuRev;
37  import org.apache.maven.scm.provider.accurev.AccuRevException;
38  import org.apache.maven.scm.provider.accurev.AccuRevInfo;
39  import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
40  import org.apache.maven.scm.provider.accurev.AccuRevVersion;
41  import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommand;
42  
43  public class AccuRevUpdateCommand
44      extends AbstractAccuRevCommand
45  {
46  
47      public AccuRevUpdateCommand( ScmLogger logger )
48      {
49          super( logger );
50      }
51  
52      @Override
53      protected ScmResult executeAccurevCommand( AccuRevScmProviderRepository repository, ScmFileSet fileSet,
54                                                 CommandParameters parameters )
55          throws ScmException, AccuRevException
56      {
57  
58          AccuRev accuRev = repository.getAccuRev();
59  
60          File basedir = fileSet.getBasedir();
61  
62          AccuRevInfo info = accuRev.info( basedir );
63  
64          if ( !info.isWorkSpace() )
65          {
66              throw new AccuRevException( "No workspace at " + basedir.getAbsolutePath() );
67          }
68  
69          String startRevision = getStartRevision( repository, parameters, info );
70  
71          ScmVersion scmVersion = parameters.getScmVersion( CommandParameter.SCM_VERSION, null );
72  
73          String updateTransactionId = null;
74  
75          if ( scmVersion != null )
76          {
77              AccuRevVersion updateVersion = repository.getAccuRevVersion( scmVersion );
78  
79              // Reparent if necessary
80              String newBasisStream = updateVersion.getBasisStream();
81              if ( newBasisStream != null
82                  && ( !( newBasisStream.equals( info.getWorkSpace() ) || newBasisStream.equals( info.getBasis() ) ) ) )
83              {
84                  getLogger().info( "Reparenting " + info.getWorkSpace() + " to " + newBasisStream );
85                  accuRev.chws( basedir, info.getWorkSpace(), newBasisStream );
86              }
87  
88              if ( !updateVersion.isNow() )
89              {
90                  updateTransactionId = updateVersion.getTimeSpec();
91              }
92          }
93  
94          if ( updateTransactionId == null )
95          {
96              updateTransactionId = repository.getDepotTransactionId( info.getWorkSpace(), "now" );
97          }
98  
99          String endRevision = repository.getRevision( info.getWorkSpace(), updateTransactionId );
100 
101         List<File> updatedFiles = accuRev.update( basedir, updateTransactionId );
102 
103         if ( updatedFiles != null )
104         {
105             return new AccuRevUpdateScmResult( accuRev.getCommandLines(), getScmFiles( updatedFiles,
106                                                                                        ScmFileStatus.UPDATED ),
107                                                startRevision, endRevision );
108         }
109         else
110         {
111             return new AccuRevUpdateScmResult( accuRev.getCommandLines(), "AccuRev error", accuRev.getErrorOutput(),
112                                                null, null, false );
113         }
114     }
115 
116     /*
117      * If we are not capturing info for a changelog then we don't need a start revision. Start date is used if supplied
118      * otherwise get the current high water mark for the workspace as the start version.
119      */
120     private String getStartRevision( AccuRevScmProviderRepository repository, CommandParameters parameters,
121                                      AccuRevInfo info )
122         throws ScmException, AccuRevException
123     {
124 
125         boolean runChangeLog = parameters.getBoolean( CommandParameter.RUN_CHANGELOG_WITH_UPDATE );
126         Date startDate = parameters.getDate( CommandParameter.START_DATE, null );
127         String workspace = info.getWorkSpace();
128 
129         if ( !runChangeLog )
130         {
131             return null;
132         }
133         else
134         {
135             return startDate == null ? repository.getWorkSpaceRevision( workspace )
136                             : repository.getRevision( workspace, startDate );
137         }
138 
139     }
140 
141     public UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
142         throws ScmException
143     {
144         return (UpdateScmResult) execute( repository, fileSet, parameters );
145     }
146 
147 }