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 */
046public class HgCheckInCommand extends AbstractCheckInCommand {
047    /**
048     * {@inheritDoc}
049     */
050    protected CheckInScmResult executeCheckInCommand(
051            ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion tag) throws ScmException {
052        if (tag != null && !StringUtils.isEmpty(tag.getName())) {
053            throw new ScmException("This provider can't handle tags for this operation");
054        }
055
056        File workingDir = fileSet.getBasedir();
057        String branchName = HgUtils.getCurrentBranchName(workingDir);
058        boolean differentOutgoingBranch =
059                repo.isPushChanges() ? HgUtils.differentOutgoingBranchFound(workingDir, branchName) : false;
060
061        // Get files that will be committed (if not specified in fileSet)
062        List<ScmFile> commitedFiles = new ArrayList<>();
063        List<File> files = fileSet.getFileList();
064        if (files.isEmpty()) { // Either commit all changes
065            HgStatusCommand statusCmd = new HgStatusCommand();
066            StatusScmResult status = statusCmd.executeStatusCommand(repo, fileSet);
067            List<ScmFile> statusFiles = status.getChangedFiles();
068            for (ScmFile file : statusFiles) {
069                if (file.getStatus() == ScmFileStatus.ADDED
070                        || file.getStatus() == ScmFileStatus.DELETED
071                        || file.getStatus() == ScmFileStatus.MODIFIED) {
072                    commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
073                }
074            }
075
076        } else { // Or commit spesific files
077            for (File file : files) {
078                commitedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.CHECKED_IN));
079            }
080        }
081
082        // Commit to local branch
083        String[] commitCmd = new String[] {HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, message};
084        commitCmd = HgUtils.expandCommandLine(commitCmd, fileSet);
085        ScmResult result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), commitCmd);
086
087        // Push to parent branch if any
088        HgScmProviderRepository repository = (HgScmProviderRepository) repo;
089
090        if (repo.isPushChanges()) {
091
092            if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
093                String[] pushCmd = new String[] {
094                    HgCommandConstants.PUSH_CMD,
095                    differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
096                    repository.getURI()
097                };
098
099                result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), pushCmd);
100            }
101
102            return new CheckInScmResult(commitedFiles, result);
103        }
104
105        return new CheckInScmResult(commitedFiles, result);
106    }
107}