View Javadoc

1   package org.apache.maven.scm.provider.hg.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 org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmResult;
25  import org.apache.maven.scm.ScmVersion;
26  import org.apache.maven.scm.command.Command;
27  import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
28  import org.apache.maven.scm.command.checkout.CheckOutScmResult;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.hg.HgUtils;
31  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
32  import org.apache.maven.scm.provider.hg.command.HgConsumer;
33  import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
34  import org.codehaus.plexus.util.FileUtils;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  import java.io.File;
38  import java.io.IOException;
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
44   * @author Olivier Lamy
45   *
46   */
47  public class HgCheckOutCommand
48      extends AbstractCheckOutCommand
49      implements Command
50  {
51      /**
52       * {@inheritDoc}
53       */
54      protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
55                                                          ScmVersion scmVersion, boolean recursive )
56          throws ScmException
57      {
58          HgScmProviderRepository repository = (HgScmProviderRepository) repo;
59          String url = repository.getURI();
60  
61          File checkoutDir = fileSet.getBasedir();
62          try
63          {
64              if ( getLogger().isInfoEnabled() )
65              {
66                  getLogger().info( "Removing " + checkoutDir );
67              }
68              FileUtils.deleteDirectory( checkoutDir );
69          }
70          catch ( IOException e )
71          {
72              throw new ScmException( "Cannot remove " + checkoutDir );
73          }
74  
75          // Do the actual checkout
76          List<String> cmdList = new ArrayList<String>();
77          if ( repo.isPushChanges() )
78          {
79              cmdList.add( HgCommandConstants.CLONE_CMD );
80          }
81          else
82          {
83              cmdList.add( HgCommandConstants.UPDATE_CMD );
84          }
85          if ( scmVersion != null && !StringUtils.isEmpty( scmVersion.getName() ) )
86          {
87              cmdList.add( HgCommandConstants.REVISION_OPTION );
88              cmdList.add( scmVersion.getName() );
89          }
90          if ( !repo.isPushChanges() )
91          {
92              cmdList.add( HgCommandConstants.CLEAN_OPTION );
93          }
94          cmdList.add( url );
95          cmdList.add( checkoutDir.getAbsolutePath() );
96          String[] checkoutCmd = cmdList.toArray( new String[0] );
97          HgConsumer checkoutConsumer = new HgConsumer( getLogger() );
98          HgUtils.execute( checkoutConsumer, getLogger(), checkoutDir.getParentFile(), checkoutCmd );
99  
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 }