001package org.apache.maven.scm.provider.accurev.command.update;
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.Date;
024import java.util.List;
025
026import org.apache.maven.scm.CommandParameter;
027import org.apache.maven.scm.CommandParameters;
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.ScmFileStatus;
031import org.apache.maven.scm.ScmResult;
032import org.apache.maven.scm.ScmVersion;
033import org.apache.maven.scm.command.update.UpdateScmResult;
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.AccuRevException;
038import org.apache.maven.scm.provider.accurev.AccuRevInfo;
039import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
040import org.apache.maven.scm.provider.accurev.AccuRevVersion;
041import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommand;
042
043/**
044 * 
045 */
046public class AccuRevUpdateCommand
047    extends AbstractAccuRevCommand
048{
049
050    public AccuRevUpdateCommand( ScmLogger logger )
051    {
052        super( logger );
053    }
054
055    @Override
056    protected ScmResult executeAccurevCommand( AccuRevScmProviderRepository repository, ScmFileSet fileSet,
057                                               CommandParameters parameters )
058        throws ScmException, AccuRevException
059    {
060
061        AccuRev accuRev = repository.getAccuRev();
062
063        File basedir = fileSet.getBasedir();
064
065        AccuRevInfo info = accuRev.info( basedir );
066
067        if ( !info.isWorkSpace() )
068        {
069            throw new AccuRevException( "No workspace at " + basedir.getAbsolutePath() );
070        }
071
072        String startRevision = getStartRevision( repository, parameters, info );
073
074        ScmVersion scmVersion = parameters.getScmVersion( CommandParameter.SCM_VERSION, null );
075
076        String updateTransactionId = null;
077
078        if ( scmVersion != null )
079        {
080            AccuRevVersion updateVersion = repository.getAccuRevVersion( scmVersion );
081
082            // Reparent if necessary
083            String newBasisStream = updateVersion.getBasisStream();
084            if ( newBasisStream != null
085                && ( !( newBasisStream.equals( info.getWorkSpace() ) || newBasisStream.equals( info.getBasis() ) ) ) )
086            {
087                getLogger().info( "Reparenting " + info.getWorkSpace() + " to " + newBasisStream );
088                accuRev.chws( basedir, info.getWorkSpace(), newBasisStream );
089            }
090
091            if ( !updateVersion.isNow() )
092            {
093                updateTransactionId = updateVersion.getTimeSpec();
094            }
095        }
096
097        if ( updateTransactionId == null )
098        {
099            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}