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
044/**
045 * 
046 */
047public class AccuRevExportCommand
048    extends AbstractAccuRevExtractSourceCommand
049{
050
051    public AccuRevExportCommand( ScmLogger logger )
052    {
053        super( logger );
054    }
055
056    public ExportScmResult export( ScmProviderRepository repository, ScmFileSet scmFileSet, CommandParameters params )
057        throws ScmException
058    {
059        return (ExportScmResult) execute( repository, scmFileSet, params );
060    }
061
062    @Override
063    protected List<File> extractSource( AccuRevScmProviderRepository repository, File basedir, AccuRevVersion version )
064        throws AccuRevException
065    {
066        AccuRev accuRev = repository.getAccuRev();
067        AccuRevInfo info = accuRev.info( basedir );
068        String basisStream = version.getBasisStream();
069        String transactionId = version.getTimeSpec();
070
071        if ( !AccuRevVersion.isNow( transactionId )
072            && !AccuRevCapability.POPULATE_TO_TRANSACTION.isSupported( accuRev.getClientVersion() ) )
073        {
074            getLogger().warn(
075                              String.format( "Ignoring transaction id %s, Export can only extract current sources",
076                                             transactionId ) );
077            transactionId = "now";
078        }
079        else
080        {
081            //We might be heading to a transaction id that is not yet available on a replica
082            accuRev.syncReplica();            
083        }
084
085        boolean removedWorkspace = false;
086
087        // We'll do a pop -V.
088
089        if ( info.isWorkSpace() )
090        {
091
092            String stat = accuRev.stat( basedir );
093
094            if ( stat != null )
095            {
096                throw new AccuRevException( String.format( "Cannot populate %s, as it is a non-ignored "
097                                                               + "subdirectory of workspace %s rooted at %s.",
098                                                           basedir.getAbsolutePath(), info.getWorkSpace(),
099                                                           info.getTop() ) );
100            }
101
102            // ok, the subdirectory must be ignored. temporarily remove the workspace.
103            removedWorkspace = accuRev.rmws( info.getWorkSpace() );
104
105        }
106
107        try
108        {
109            File path = new File( repository.getDepotRelativeProjectPath() );
110            return accuRev.popExternal( basedir, basisStream, transactionId, Collections.singletonList( path ) );
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}