001package org.apache.maven.scm.provider.accurev.command.export;
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.Collections;
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.export.ExportScmResult;
033import org.apache.maven.scm.command.export.ExportScmResultWithRevision;
034import org.apache.maven.scm.log.ScmLogger;
035import org.apache.maven.scm.provider.ScmProviderRepository;
036import org.apache.maven.scm.provider.accurev.AccuRev;
037import org.apache.maven.scm.provider.accurev.AccuRevCapability;
038import org.apache.maven.scm.provider.accurev.AccuRevException;
039import org.apache.maven.scm.provider.accurev.AccuRevInfo;
040import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
041import org.apache.maven.scm.provider.accurev.AccuRevVersion;
042import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevExtractSourceCommand;
043
044public class AccuRevExportCommand
045    extends AbstractAccuRevExtractSourceCommand
046{
047
048    public AccuRevExportCommand( ScmLogger logger )
049    {
050        super( logger );
051    }
052
053    public ExportScmResult export( ScmProviderRepository repository, ScmFileSet scmFileSet, CommandParameters params )
054        throws ScmException
055    {
056        return (ExportScmResult) execute( repository, scmFileSet, params );
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        AccuRevInfo info = accuRev.info( basedir );
065        String basisStream = version.getBasisStream();
066        String transactionId = version.getTimeSpec();
067
068        if ( !AccuRevVersion.isNow( transactionId )
069            && !AccuRevCapability.POPULATE_TO_TRANSACTION.isSupported( accuRev.getClientVersion() ) )
070        {
071            getLogger().warn(
072                              String.format( "Ignoring transaction id %s, Export can only extract current sources",
073                                             transactionId ) );
074            transactionId = "now";
075        } else {
076            //We might be heading to a transaction id that is not yet available on a replica
077            accuRev.syncReplica();            
078        }
079
080        boolean removedWorkspace = false;
081
082        // We'll do a pop -V.
083
084        if ( info.isWorkSpace() )
085        {
086
087            String stat = accuRev.stat( basedir );
088
089            if ( stat != null )
090            {
091                throw new AccuRevException(
092                                            String.format(
093                                                           "Cannot populate %s, as it is a non-ignored subdirectory of workspace %s rooted at %s.",
094                                                           basedir.getAbsolutePath(), info.getWorkSpace(),
095                                                           info.getTop() ) );
096            }
097
098            // ok, the subdirectory must be ignored. temporarily remove the workspace.
099            removedWorkspace = accuRev.rmws( info.getWorkSpace() );
100
101        }
102
103        try
104        {
105            return accuRev.popExternal(
106                                        basedir,
107                                        basisStream,
108                                        transactionId,
109                                        Collections.singletonList( new File( repository.getDepotRelativeProjectPath() ) ) );
110
111        }
112        finally
113        {
114            if ( removedWorkspace )
115            {
116                accuRev.reactivate( info.getWorkSpace() );
117            }
118        }
119    }
120
121    @Override
122    protected ScmResult getScmResult( AccuRevScmProviderRepository repository, List<ScmFile> scmFiles,
123                                      ScmVersion scmVersion )
124    {
125        AccuRev accuRev = repository.getAccuRev();
126        if ( scmFiles != null )
127        {
128            if ( scmVersion == null )
129            {
130                return new ExportScmResult( accuRev.getCommandLines(), scmFiles );
131            }
132            else
133            {
134                return new ExportScmResultWithRevision( accuRev.getCommandLines(), scmFiles, scmVersion.toString() );
135            }
136        }
137        else
138        {
139            return new ExportScmResult( accuRev.getCommandLines(), "AccuRev Error", accuRev.getErrorOutput(), false );
140        }
141    }
142
143}