001package org.apache.maven.scm.provider.bazaar.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.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.ScmResult;
030import org.apache.maven.scm.ScmVersion;
031import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
032import org.apache.maven.scm.command.checkout.CheckOutScmResult;
033import org.apache.maven.scm.provider.ScmProviderRepository;
034import org.apache.maven.scm.provider.bazaar.BazaarUtils;
035import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
036import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
037import org.apache.maven.scm.provider.bazaar.repository.BazaarScmProviderRepository;
038import org.codehaus.plexus.util.FileUtils;
039import org.codehaus.plexus.util.StringUtils;
040
041/**
042 * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
043 * @author Olivier Lamy
044 *
045 */
046public class BazaarCheckOutCommand
047    extends AbstractCheckOutCommand
048{
049    /** {@inheritDoc} */
050    protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
051                                                       ScmVersion version, boolean recursive, boolean shallow )
052        throws ScmException
053    {
054        BazaarScmProviderRepository repository = (BazaarScmProviderRepository) repo;
055        String url = repository.getURI();
056
057        File checkoutDir = fileSet.getBasedir();
058        try
059        {
060            if ( getLogger().isInfoEnabled() )
061            {
062                getLogger().info( "Removing " + checkoutDir );
063            }
064            FileUtils.deleteDirectory( checkoutDir );
065        }
066        catch ( IOException e )
067        {
068            throw new ScmException( "Cannot remove " + checkoutDir );
069        }
070
071        // Do the actual checkout
072        List<String> checkoutCmd = new ArrayList<String>();
073        checkoutCmd.add( BazaarConstants.BRANCH_CMD );
074        checkoutCmd.add( url );
075        checkoutCmd.add( checkoutDir.getAbsolutePath() );
076        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
077        {
078             checkoutCmd.add( BazaarConstants.REVISION_OPTION );
079             checkoutCmd.add( "tag:" + version.getName() );
080        }
081        BazaarConsumer checkoutConsumer = new BazaarConsumer( getLogger() );
082        BazaarUtils.execute( checkoutConsumer, getLogger(), checkoutDir.getParentFile(),
083                             (String[]) checkoutCmd.toArray( new String[0] ) );
084
085        // Do inventory to find list of checkedout files
086        String[] inventoryCmd = new String[]{BazaarConstants.INVENTORY_CMD};
087        BazaarCheckOutConsumer consumer = new BazaarCheckOutConsumer( getLogger(), checkoutDir );
088        ScmResult result = BazaarUtils.execute( consumer, getLogger(), checkoutDir, inventoryCmd );
089        if ( !result.isSuccess() )
090        {
091            throw new ScmException( result.getProviderMessage() );
092        }
093        return new CheckOutScmResult( consumer.getCheckedOutFiles(), result );
094    }
095}