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 *
045 */
046public class HgCheckOutCommand extends AbstractCheckOutCommand implements Command {
047    /**
048     * {@inheritDoc}
049     */
050    protected CheckOutScmResult executeCheckOutCommand(
051            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion scmVersion, boolean recursive, boolean shallow)
052            throws ScmException {
053        HgScmProviderRepository repository = (HgScmProviderRepository) repo;
054        String url = repository.getURI();
055
056        File checkoutDir = fileSet.getBasedir();
057        try {
058            if (logger.isInfoEnabled()) {
059                logger.info("Removing " + checkoutDir);
060            }
061            FileUtils.deleteDirectory(checkoutDir);
062        } catch (IOException e) {
063            throw new ScmException("Cannot remove " + checkoutDir);
064        }
065
066        // Do the actual checkout
067        List<String> cmdList = new ArrayList<>();
068        if (repo.isPushChanges()) {
069            cmdList.add(HgCommandConstants.CLONE_CMD);
070        } else {
071            cmdList.add(HgCommandConstants.UPDATE_CMD);
072        }
073        if (scmVersion != null && !StringUtils.isEmpty(scmVersion.getName())) {
074            cmdList.add(HgCommandConstants.REVISION_OPTION);
075            cmdList.add(scmVersion.getName());
076        }
077        if (!repo.isPushChanges()) {
078            cmdList.add(HgCommandConstants.CLEAN_OPTION);
079        }
080        cmdList.add(url);
081        cmdList.add(checkoutDir.getAbsolutePath());
082        String[] checkoutCmd = cmdList.toArray(new String[0]);
083        HgConsumer checkoutConsumer = new HgConsumer();
084        HgUtils.execute(checkoutConsumer, checkoutDir.getParentFile(), checkoutCmd);
085
086        // Do inventory to find list of checkedout files
087        String[] inventoryCmd = new String[] {HgCommandConstants.INVENTORY_CMD};
088        HgCheckOutConsumer consumer = new HgCheckOutConsumer(checkoutDir);
089        ScmResult result = HgUtils.execute(consumer, checkoutDir, inventoryCmd);
090
091        return new CheckOutScmResult(consumer.getCheckedOutFiles(), result);
092    }
093}