001package org.apache.maven.scm.provider.accurev.command.checkout;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.scm.CommandParameters;
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.ScmFile;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.ScmResult;
031import org.apache.maven.scm.ScmVersion;
032import org.apache.maven.scm.command.checkout.CheckOutScmResult;
033import org.apache.maven.scm.log.ScmLogger;
034import org.apache.maven.scm.provider.ScmProviderRepository;
035import org.apache.maven.scm.provider.accurev.AccuRev;
036import org.apache.maven.scm.provider.accurev.AccuRevException;
037import org.apache.maven.scm.provider.accurev.AccuRevInfo;
038import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
039import org.apache.maven.scm.provider.accurev.AccuRevVersion;
040import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevExtractSourceCommand;
041
042public class AccuRevCheckOutCommand
043    extends AbstractAccuRevExtractSourceCommand
044{
045
046    public AccuRevCheckOutCommand( ScmLogger logger )
047    {
048        super( logger );
049    }
050
051    public CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet,
052                                       CommandParameters parameters )
053        throws ScmException
054    {
055
056        return (CheckOutScmResult) execute( repository, fileSet, parameters );
057    }
058
059    @Override
060    protected List<File> extractSource( AccuRevScmProviderRepository repository, File basedir, AccuRevVersion version )
061        throws AccuRevException
062    {
063        AccuRev accuRev = repository.getAccuRev();
064
065        AccuRevInfo info = accuRev.info( basedir );
066
067        List<File> extractedFiles = new ArrayList<File>();
068        
069        String basisStream = version.getBasisStream();
070        String transactionId = version.getTimeSpec();
071
072        boolean success = true;
073        if ( info.isWorkSpace() )
074        {
075
076            if ( !repository.isWorkSpaceTop( info ) )
077            {
078                throw new AccuRevException(
079                                            String.format(
080                                                           "Can't checkout to %s, a subdirectory of existing workspace %s",
081                                                           basedir, info.getWorkSpace() ) );
082            }
083            // workspace exists at this basedir already.
084            if ( !basisStream.equals( info.getBasis() ) )
085            {
086                // different basis, reparent.
087                success = accuRev.chws( basedir, info.getWorkSpace(), basisStream );
088            }
089
090            if ( success )
091            {
092                // repopulate everything in the workspace.
093                // note we do NOT want -t here, we just fill in any missing files
094                // to the current transaction watermark...
095                // the update later on will get the extra files
096                List<File> poppedFiles = accuRev.pop( basedir, null );
097                if ( poppedFiles != null )
098                {
099                    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}