001    package org.apache.maven.scm.provider.hg.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    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmResult;
025    import org.apache.maven.scm.ScmVersion;
026    import org.apache.maven.scm.command.Command;
027    import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
028    import org.apache.maven.scm.command.checkout.CheckOutScmResult;
029    import org.apache.maven.scm.provider.ScmProviderRepository;
030    import org.apache.maven.scm.provider.hg.HgUtils;
031    import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
032    import org.apache.maven.scm.provider.hg.command.HgConsumer;
033    import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
034    import org.codehaus.plexus.util.FileUtils;
035    import org.codehaus.plexus.util.StringUtils;
036    
037    import java.io.File;
038    import java.io.IOException;
039    import java.util.ArrayList;
040    import java.util.List;
041    
042    /**
043     * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
044     * @author Olivier Lamy
045     * @version $Id: HgCheckOutCommand.java 1207290 2011-11-28 15:20:44Z olamy $
046     */
047    public class HgCheckOutCommand
048        extends AbstractCheckOutCommand
049        implements Command
050    {
051        /**
052         * {@inheritDoc}
053         */
054        protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
055                                                            ScmVersion scmVersion, boolean recursive )
056            throws ScmException
057        {
058            HgScmProviderRepository repository = (HgScmProviderRepository) repo;
059            String url = repository.getURI();
060    
061            File checkoutDir = fileSet.getBasedir();
062            try
063            {
064                if ( getLogger().isInfoEnabled() )
065                {
066                    getLogger().info( "Removing " + checkoutDir );
067                }
068                FileUtils.deleteDirectory( checkoutDir );
069            }
070            catch ( IOException e )
071            {
072                throw new ScmException( "Cannot remove " + checkoutDir );
073            }
074    
075            // Do the actual checkout
076            List<String> cmdList = new ArrayList<String>();
077            if ( repo.isPushChanges() )
078            {
079                cmdList.add( HgCommandConstants.CLONE_CMD );
080            }
081            else
082            {
083                cmdList.add( HgCommandConstants.UPDATE_CMD );
084            }
085            if ( scmVersion != null && !StringUtils.isEmpty( scmVersion.getName() ) )
086            {
087                cmdList.add( HgCommandConstants.REVISION_OPTION );
088                cmdList.add( scmVersion.getName() );
089            }
090            if ( !repo.isPushChanges() )
091            {
092                cmdList.add( HgCommandConstants.CLEAN_OPTION );
093            }
094            cmdList.add( url );
095            cmdList.add( checkoutDir.getAbsolutePath() );
096            String[] checkoutCmd = cmdList.toArray( new String[0] );
097            HgConsumer checkoutConsumer = new HgConsumer( getLogger() );
098            HgUtils.execute( checkoutConsumer, getLogger(), checkoutDir.getParentFile(), checkoutCmd );
099    
100            // Do inventory to find list of checkedout files
101            String[] inventoryCmd = new String[]{ HgCommandConstants.INVENTORY_CMD };
102            HgCheckOutConsumer consumer = new HgCheckOutConsumer( getLogger(), checkoutDir );
103            ScmResult result = HgUtils.execute( consumer, getLogger(), checkoutDir, inventoryCmd );
104    
105            return new CheckOutScmResult( consumer.getCheckedOutFiles(), result );
106        }
107    }