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