View Javadoc
1   package org.apache.maven.scm.provider.accurev.command.checkout;
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.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.scm.CommandParameters;
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmResult;
31  import org.apache.maven.scm.ScmVersion;
32  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
33  import org.apache.maven.scm.log.ScmLogger;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.accurev.AccuRev;
36  import org.apache.maven.scm.provider.accurev.AccuRevException;
37  import org.apache.maven.scm.provider.accurev.AccuRevInfo;
38  import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
39  import org.apache.maven.scm.provider.accurev.AccuRevVersion;
40  import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevExtractSourceCommand;
41  
42  public class AccuRevCheckOutCommand
43      extends AbstractAccuRevExtractSourceCommand
44  {
45  
46      public AccuRevCheckOutCommand( ScmLogger logger )
47      {
48          super( logger );
49      }
50  
51      public CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet,
52                                         CommandParameters parameters )
53          throws ScmException
54      {
55  
56          return (CheckOutScmResult) execute( repository, fileSet, parameters );
57      }
58  
59      @Override
60      protected List<File> extractSource( AccuRevScmProviderRepository repository, File basedir, AccuRevVersion version )
61          throws AccuRevException
62      {
63          AccuRev accuRev = repository.getAccuRev();
64  
65          AccuRevInfo info = accuRev.info( basedir );
66  
67          List<File> extractedFiles = new ArrayList<File>();
68          
69          String basisStream = version.getBasisStream();
70          String transactionId = version.getTimeSpec();
71  
72          boolean success = true;
73          if ( info.isWorkSpace() )
74          {
75  
76              if ( !repository.isWorkSpaceTop( info ) )
77              {
78                  throw new AccuRevException(
79                                              String.format(
80                                                             "Can't checkout to %s, a subdirectory of existing workspace %s",
81                                                             basedir, info.getWorkSpace() ) );
82              }
83              // workspace exists at this basedir already.
84              if ( !basisStream.equals( info.getBasis() ) )
85              {
86                  // different basis, reparent.
87                  success = accuRev.chws( basedir, info.getWorkSpace(), basisStream );
88              }
89  
90              if ( success )
91              {
92                  // repopulate everything in the workspace.
93                  // note we do NOT want -t here, we just fill in any missing files
94                  // to the current transaction watermark...
95                  // the update later on will get the extra files
96                  List<File> poppedFiles = accuRev.pop( basedir, null );
97                  if ( poppedFiles != null )
98                  {
99                      extractedFiles.addAll( poppedFiles );
100                 }
101                 else
102                 {
103                     success = false;
104                 }
105             }
106 
107         }
108         else
109         {
110             // not a workspace, make one...
111             // TODO set incl rules to only include the projectPath
112             // TODO somehow set provider message (via throw exception?
113             // if basisStream is null
114             String workSpaceName = getWorkSpaceName( basedir, basisStream );
115 
116             success = accuRev.mkws( basisStream, workSpaceName, basedir );
117             
118             //Even though a new workspace starts with "0" as the high water mark
119             //it can't be updated to anything less than its own mkstream transaction
120             //now is close enough since even if something does sneak inbetween we
121             //were just lucky that it didn't happen before...
122             transactionId = "now";
123 
124             if ( success )
125             {
126                 getLogger().info( "Created workspace " + workSpaceName );
127             }
128         }
129 
130         if ( success )
131         {
132             List<File> updatedFiles = accuRev.update( basedir, transactionId );
133             if ( updatedFiles != null )
134             {
135                 extractedFiles.addAll( updatedFiles );
136             }
137             else
138             {
139                 success = false;
140             }
141         }
142         return success ? extractedFiles : null;
143     }
144 
145     @Override
146     protected ScmResult getScmResult( AccuRevScmProviderRepository repository, List<ScmFile> scmFiles , ScmVersion version)
147     {
148 
149         AccuRev accuRev = repository.getAccuRev();
150         if ( scmFiles != null )
151         {
152             return new CheckOutScmResult( accuRev.getCommandLines(), scmFiles, repository.getProjectPath() );
153         }
154         else
155         {
156             return new CheckOutScmResult( accuRev.getCommandLines(), "AccuRev Error", accuRev.getErrorOutput(), false );
157         }
158     }
159 
160     public static String getWorkSpaceName( File basedir, String basisStream )
161     {
162         String workSpaceName;
163         String baseName = basedir.getName();
164         if ( baseName.contains( basisStream ) )
165         {
166             workSpaceName = baseName;
167         }
168         else if ( basisStream.contains( baseName ) )
169         {
170             workSpaceName = basisStream;
171         }
172         else
173         {
174             workSpaceName = basisStream + "_" + baseName;
175         }
176         return workSpaceName;
177     }
178 
179 }