001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.hg.command.checkout;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.lang3.StringUtils;
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.Command;
032import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
033import org.apache.maven.scm.command.checkout.CheckOutScmResult;
034import org.apache.maven.scm.provider.ScmProviderRepository;
035import org.apache.maven.scm.provider.hg.HgUtils;
036import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
037import org.apache.maven.scm.provider.hg.command.HgConsumer;
038import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
039import org.codehaus.plexus.util.FileUtils;
040
041/**
042 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
043 * @author Olivier Lamy
044 */
045public class HgCheckOutCommand extends AbstractCheckOutCommand implements Command {
046    /**
047     * {@inheritDoc}
048     */
049    protected CheckOutScmResult executeCheckOutCommand(
050            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion scmVersion, boolean recursive, boolean shallow)
051            throws ScmException {
052        HgScmProviderRepository repository = (HgScmProviderRepository) repo;
053        String url = repository.getURI();
054
055        File checkoutDir = fileSet.getBasedir();
056        try {
057            if (logger.isDebugEnabled()) {
058                logger.debug("Removing " + checkoutDir);
059            }
060            FileUtils.deleteDirectory(checkoutDir);
061        } catch (IOException e) {
062            throw new ScmException("Cannot remove " + checkoutDir);
063        }
064
065        // Do the actual checkout
066        List<String> cmdList = new ArrayList<>();
067        if (repo.isPushChanges()) {
068            cmdList.add(HgCommandConstants.CLONE_CMD);
069        } else {
070            cmdList.add(HgCommandConstants.UPDATE_CMD);
071        }
072        if (scmVersion != null && !StringUtils.isEmpty(scmVersion.getName())) {
073            cmdList.add(HgCommandConstants.REVISION_OPTION);
074            cmdList.add(scmVersion.getName());
075        }
076        if (!repo.isPushChanges()) {
077            cmdList.add(HgCommandConstants.CLEAN_OPTION);
078        }
079        cmdList.add(url);
080        cmdList.add(checkoutDir.getAbsolutePath());
081        String[] checkoutCmd = cmdList.toArray(new String[0]);
082        HgConsumer checkoutConsumer = new HgConsumer();
083        HgUtils.execute(checkoutConsumer, checkoutDir.getParentFile(), checkoutCmd);
084
085        // Do inventory to find list of checkedout files
086        String[] inventoryCmd = new String[] {HgCommandConstants.INVENTORY_CMD};
087        HgCheckOutConsumer consumer = new HgCheckOutConsumer(checkoutDir);
088        ScmResult result = HgUtils.execute(consumer, checkoutDir, inventoryCmd);
089
090        return new CheckOutScmResult(consumer.getCheckedOutFiles(), result);
091    }
092}