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  public class HgCheckOutCommand extends AbstractCheckOutCommand implements Command {
46      /**
47       * {@inheritDoc}
48       */
49      protected CheckOutScmResult executeCheckOutCommand(
50              ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion scmVersion, boolean recursive, boolean shallow)
51              throws ScmException {
52          HgScmProviderRepository repository = (HgScmProviderRepository) repo;
53          String url = repository.getURI();
54  
55          File checkoutDir = fileSet.getBasedir();
56          try {
57              if (logger.isDebugEnabled()) {
58                  logger.debug("Removing " + checkoutDir);
59              }
60              FileUtils.deleteDirectory(checkoutDir);
61          } catch (IOException e) {
62              throw new ScmException("Cannot remove " + checkoutDir);
63          }
64  
65          // Do the actual checkout
66          List<String> cmdList = new ArrayList<>();
67          if (repo.isPushChanges()) {
68              cmdList.add(HgCommandConstants.CLONE_CMD);
69          } else {
70              cmdList.add(HgCommandConstants.UPDATE_CMD);
71          }
72          if (scmVersion != null && !StringUtils.isEmpty(scmVersion.getName())) {
73              cmdList.add(HgCommandConstants.REVISION_OPTION);
74              cmdList.add(scmVersion.getName());
75          }
76          if (!repo.isPushChanges()) {
77              cmdList.add(HgCommandConstants.CLEAN_OPTION);
78          }
79          cmdList.add(url);
80          cmdList.add(checkoutDir.getAbsolutePath());
81          String[] checkoutCmd = cmdList.toArray(new String[0]);
82          HgConsumer checkoutConsumer = new HgConsumer();
83          HgUtils.execute(checkoutConsumer, checkoutDir.getParentFile(), checkoutCmd);
84  
85          // Do inventory to find list of checkedout files
86          String[] inventoryCmd = new String[] {HgCommandConstants.INVENTORY_CMD};
87          HgCheckOutConsumer consumer = new HgCheckOutConsumer(checkoutDir);
88          ScmResult result = HgUtils.execute(consumer, checkoutDir, inventoryCmd);
89  
90          return new CheckOutScmResult(consumer.getCheckedOutFiles(), result);
91      }
92  }