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.checkin;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.lang3.StringUtils;
026import org.apache.maven.scm.ScmException;
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileSet;
029import org.apache.maven.scm.ScmFileStatus;
030import org.apache.maven.scm.ScmResult;
031import org.apache.maven.scm.ScmVersion;
032import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
033import org.apache.maven.scm.command.checkin.CheckInScmResult;
034import org.apache.maven.scm.command.status.StatusScmResult;
035import org.apache.maven.scm.provider.ScmProviderRepository;
036import org.apache.maven.scm.provider.hg.HgUtils;
037import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
038import org.apache.maven.scm.provider.hg.command.HgConsumer;
039import org.apache.maven.scm.provider.hg.command.status.HgStatusCommand;
040import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
041
042/**
043 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
044 * @author Olivier Lamy
045 *
046 */
047public class HgCheckInCommand extends AbstractCheckInCommand {
048    /**
049     * {@inheritDoc}
050     */
051    protected CheckInScmResult executeCheckInCommand(
052            ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion tag) throws ScmException {
053        if (tag != null && !StringUtils.isEmpty(tag.getName())) {
054            throw new ScmException("This provider can't handle tags for this operation");
055        }
056
057        File workingDir = fileSet.getBasedir();
058        String branchName = HgUtils.getCurrentBranchName(workingDir);
059        boolean differentOutgoingBranch =
060                repo.isPushChanges() ? HgUtils.differentOutgoingBranchFound(workingDir, branchName) : false;
061
062        // Get files that will be committed (if not specified in fileSet)
063        List<ScmFile> commitedFiles = new ArrayList<>();
064        List<File> files = fileSet.getFileList();
065        if (files.isEmpty()) { // Either commit all changes
066            HgStatusCommand statusCmd = new HgStatusCommand();
067            StatusScmResult status = statusCmd.executeStatusCommand(repo, fileSet);
068            List<ScmFile> statusFiles = status.getChangedFiles();
069            for (ScmFile file : statusFiles) {
070                if (file.getStatus() == ScmFileStatus.ADDED
071                        || file.getStatus() == ScmFileStatus.DELETED
072                        || file.getStatus() == ScmFileStatus.MODIFIED) {
073                    commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
074                }
075            }
076
077        } else { // Or commit spesific files
078            for (File file : files) {
079                commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
080            }
081        }
082
083        // Commit to local branch
084        String[] commitCmd = new String[] {HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, message};
085        commitCmd = HgUtils.expandCommandLine(commitCmd, fileSet);
086        ScmResult result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), commitCmd);
087
088        // Push to parent branch if any
089        HgScmProviderRepository repository = (HgScmProviderRepository) repo;
090
091        if (repo.isPushChanges()) {
092
093            if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
094                String[] pushCmd = new String[] {
095                    HgCommandConstants.PUSH_CMD,
096                    differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
097                    repository.getURI()
098                };
099
100                result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), pushCmd);
101            }
102
103            return new CheckInScmResult(commitedFiles, result);
104        }
105
106        return new CheckInScmResult(commitedFiles, result);
107    }
108}