View Javadoc
1   package org.apache.maven.scm.provider.bazaar.command.checkout;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ScmException;
28  import org.apache.maven.scm.ScmFileSet;
29  import org.apache.maven.scm.ScmResult;
30  import org.apache.maven.scm.ScmVersion;
31  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
32  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.bazaar.BazaarUtils;
35  import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
36  import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
37  import org.apache.maven.scm.provider.bazaar.repository.BazaarScmProviderRepository;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  /**
42   * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
43   * @author Olivier Lamy
44   *
45   */
46  public class BazaarCheckOutCommand
47      extends AbstractCheckOutCommand
48  {
49      /** {@inheritDoc} */
50      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
51                                                          ScmVersion version, boolean recursive )
52          throws ScmException
53      {
54          BazaarScmProviderRepository repository = (BazaarScmProviderRepository) repo;
55          String url = repository.getURI();
56  
57          File checkoutDir = fileSet.getBasedir();
58          try
59          {
60              if ( getLogger().isInfoEnabled() )
61              {
62                  getLogger().info( "Removing " + checkoutDir );
63              }
64              FileUtils.deleteDirectory( checkoutDir );
65          }
66          catch ( IOException e )
67          {
68              throw new ScmException( "Cannot remove " + checkoutDir );
69          }
70  
71          // Do the actual checkout
72          List<String> checkoutCmd = new ArrayList<String>();
73          checkoutCmd.add( BazaarConstants.BRANCH_CMD );
74          checkoutCmd.add( url );
75          checkoutCmd.add( checkoutDir.getAbsolutePath() );
76          if ( version != null && StringUtils.isNotEmpty( version.getName() ) ) {
77               checkoutCmd.add( BazaarConstants.REVISION_OPTION );
78               checkoutCmd.add( "tag:" + version.getName() );
79          }
80          BazaarConsumer checkoutConsumer = new BazaarConsumer( getLogger() );
81          BazaarUtils.execute( checkoutConsumer, getLogger(), checkoutDir.getParentFile(),
82                               (String[]) checkoutCmd.toArray( new String[0] ) );
83  
84          // Do inventory to find list of checkedout files
85          String[] inventoryCmd = new String[]{BazaarConstants.INVENTORY_CMD};
86          BazaarCheckOutConsumer consumer = new BazaarCheckOutConsumer( getLogger(), checkoutDir );
87          ScmResult result = BazaarUtils.execute( consumer, getLogger(), checkoutDir, inventoryCmd );
88          if ( !result.isSuccess() )
89          {
90              throw new ScmException( result.getProviderMessage() );
91          }
92          return new CheckOutScmResult( consumer.getCheckedOutFiles(), result );
93      }
94  }