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  /**
44   * 
45   */
46  public class AccuRevUpdateCommand
47      extends AbstractAccuRevCommand
48  {
49  
50      public AccuRevUpdateCommand( ScmLogger logger )
51      {
52          super( logger );
53      }
54  
55      @Override
56      protected ScmResult executeAccurevCommand( AccuRevScmProviderRepository repository, ScmFileSet fileSet,
57                                                 CommandParameters parameters )
58          throws ScmException, AccuRevException
59      {
60  
61          AccuRev accuRev = repository.getAccuRev();
62  
63          File basedir = fileSet.getBasedir();
64  
65          AccuRevInfo info = accuRev.info( basedir );
66  
67          if ( !info.isWorkSpace() )
68          {
69              throw new AccuRevException( "No workspace at " + basedir.getAbsolutePath() );
70          }
71  
72          String startRevision = getStartRevision( repository, parameters, info );
73  
74          ScmVersion scmVersion = parameters.getScmVersion( CommandParameter.SCM_VERSION, null );
75  
76          String updateTransactionId = null;
77  
78          if ( scmVersion != null )
79          {
80              AccuRevVersion updateVersion = repository.getAccuRevVersion( scmVersion );
81  
82              // Reparent if necessary
83              String newBasisStream = updateVersion.getBasisStream();
84              if ( newBasisStream != null
85                  && ( !( newBasisStream.equals( info.getWorkSpace() ) || newBasisStream.equals( info.getBasis() ) ) ) )
86              {
87                  getLogger().info( "Reparenting " + info.getWorkSpace() + " to " + newBasisStream );
88                  accuRev.chws( basedir, info.getWorkSpace(), newBasisStream );
89              }
90  
91              if ( !updateVersion.isNow() )
92              {
93                  updateTransactionId = updateVersion.getTimeSpec();
94              }
95          }
96  
97          if ( updateTransactionId == null )
98          {
99              updateTransactionId = repository.getDepotTransactionId( info.getWorkSpace(), "now" );
100         }
101 
102         String endRevision = repository.getRevision( info.getWorkSpace(), updateTransactionId );
103 
104         List<File> updatedFiles = accuRev.update( basedir, updateTransactionId );
105 
106         if ( updatedFiles != null )
107         {
108             return new AccuRevUpdateScmResult( accuRev.getCommandLines(), getScmFiles( updatedFiles,
109                                                                                        ScmFileStatus.UPDATED ),
110                                                startRevision, endRevision );
111         }
112         else
113         {
114             return new AccuRevUpdateScmResult( accuRev.getCommandLines(), "AccuRev error", accuRev.getErrorOutput(),
115                                                null, null, false );
116         }
117     }
118 
119     /*
120      * If we are not capturing info for a changelog then we don't need a start revision. Start date is used if supplied
121      * otherwise get the current high water mark for the workspace as the start version.
122      */
123     private String getStartRevision( AccuRevScmProviderRepository repository, CommandParameters parameters,
124                                      AccuRevInfo info )
125         throws ScmException, AccuRevException
126     {
127 
128         boolean runChangeLog = parameters.getBoolean( CommandParameter.RUN_CHANGELOG_WITH_UPDATE );
129         Date startDate = parameters.getDate( CommandParameter.START_DATE, null );
130         String workspace = info.getWorkSpace();
131 
132         if ( !runChangeLog )
133         {
134             return null;
135         }
136         else
137         {
138             return startDate == null ? repository.getWorkSpaceRevision( workspace )
139                             : repository.getRevision( workspace, startDate );
140         }
141 
142     }
143 
144     public UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
145         throws ScmException
146     {
147         return (UpdateScmResult) execute( repository, fileSet, parameters );
148     }
149 
150 }