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.update;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.commons.lang3.StringUtils;
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.ScmFile;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.ScmFileStatus;
031import org.apache.maven.scm.ScmResult;
032import org.apache.maven.scm.ScmVersion;
033import org.apache.maven.scm.command.Command;
034import org.apache.maven.scm.command.changelog.ChangeLogCommand;
035import org.apache.maven.scm.command.update.AbstractUpdateCommand;
036import org.apache.maven.scm.command.update.UpdateScmResult;
037import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
038import org.apache.maven.scm.provider.ScmProviderRepository;
039import org.apache.maven.scm.provider.hg.HgUtils;
040import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
041import org.apache.maven.scm.provider.hg.command.HgConsumer;
042import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand;
043import org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer;
044
045/**
046 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
047 * @author Olivier Lamy
048 *
049 */
050public class HgUpdateCommand extends AbstractUpdateCommand implements Command {
051    /** {@inheritDoc} */
052    protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag)
053            throws ScmException {
054        File workingDir = fileSet.getBasedir();
055
056        String[] updateCmd;
057        // Update branch
058        if (repo.isPushChanges()) {
059            updateCmd = new String[] {
060                HgCommandConstants.PULL_CMD,
061                HgCommandConstants.REVISION_OPTION,
062                tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip"
063            };
064        } else {
065            updateCmd = new String[] {
066                HgCommandConstants.UPDATE_CMD,
067                tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip",
068                HgCommandConstants.CLEAN_OPTION
069            };
070        }
071        ScmResult updateResult = HgUtils.execute(new HgConsumer(), workingDir, updateCmd);
072
073        if (!updateResult.isSuccess()) {
074            return new UpdateScmResult(null, null, updateResult);
075        }
076
077        // Find changes from last revision
078        int currentRevision = HgUtils.getCurrentRevisionNumber(workingDir);
079        int previousRevision = currentRevision - 1;
080        String[] diffCmd =
081                new String[] {HgCommandConstants.DIFF_CMD, HgCommandConstants.REVISION_OPTION, "" + previousRevision};
082        HgDiffConsumer diffConsumer = new HgDiffConsumer(workingDir);
083        ScmResult diffResult = HgUtils.execute(diffConsumer, workingDir, diffCmd);
084
085        // Now translate between diff and update file status
086        List<ScmFile> updatedFiles = new ArrayList<>();
087        List<CharSequence> changes = new ArrayList<>();
088        List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
089        Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
090        for (ScmFile file : diffFiles) {
091            changes.add(diffChanges.get(file.getPath()));
092            if (file.getStatus() == ScmFileStatus.MODIFIED) {
093                updatedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.PATCHED));
094            } else {
095                updatedFiles.add(file);
096            }
097        }
098
099        if (repo.isPushChanges()) {
100            String[] hgUpdateCmd = new String[] {HgCommandConstants.UPDATE_CMD};
101            HgUtils.execute(new HgConsumer(), workingDir, hgUpdateCmd);
102        }
103
104        return new UpdateScmResultWithRevision(
105                updatedFiles, new ArrayList<>(0), String.valueOf(currentRevision), diffResult);
106    }
107
108    protected ChangeLogCommand getChangeLogCommand() {
109        return new HgChangeLogCommand();
110    }
111}