001package org.apache.maven.scm.provider.hg.command.checkin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.List;
025
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;
041import org.codehaus.plexus.util.StringUtils;
042
043/**
044 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
045 * @author Olivier Lamy
046 *
047 */
048public class HgCheckInCommand
049    extends AbstractCheckInCommand
050{
051    /**
052     * {@inheritDoc}
053     */
054    protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
055                                                      ScmVersion tag )
056        throws ScmException
057    {
058        if ( tag != null && !StringUtils.isEmpty( tag.getName() ) )
059        {
060            throw new ScmException( "This provider can't handle tags for this operation" );
061        }
062
063
064        File workingDir = fileSet.getBasedir();
065        String branchName = HgUtils.getCurrentBranchName( getLogger(), workingDir );
066        boolean differentOutgoingBranch =
067            repo.isPushChanges() ? HgUtils.differentOutgoingBranchFound( getLogger(), workingDir, branchName ) : false;
068
069        // Get files that will be committed (if not specified in fileSet)
070        List<ScmFile> commitedFiles = new ArrayList<ScmFile>();
071        List<File> files = fileSet.getFileList();
072        if ( files.isEmpty() )
073        { //Either commit all changes
074            HgStatusCommand statusCmd = new HgStatusCommand();
075            statusCmd.setLogger( getLogger() );
076            StatusScmResult status = statusCmd.executeStatusCommand( repo, fileSet );
077            List<ScmFile> statusFiles = status.getChangedFiles();
078            for ( ScmFile file : statusFiles )
079            {
080                if ( file.getStatus() == ScmFileStatus.ADDED || file.getStatus() == ScmFileStatus.DELETED
081                    || file.getStatus() == ScmFileStatus.MODIFIED )
082                {
083                    commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
084                }
085            }
086
087        }
088        else
089        { //Or commit spesific files
090            for ( File file : files )
091            {
092                commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
093            }
094        }
095
096        // Commit to local branch
097        String[] commitCmd = new String[]{ HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, message };
098        commitCmd = HgUtils.expandCommandLine( commitCmd, fileSet );
099        ScmResult result =
100            HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), commitCmd );
101
102        // Push to parent branch if any
103        HgScmProviderRepository repository = (HgScmProviderRepository) repo;
104
105        if ( repo.isPushChanges() )
106        {
107
108            if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
109            {
110                String[] pushCmd = new String[]{ HgCommandConstants.PUSH_CMD,
111                    differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
112                    repository.getURI() };
113
114                result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
115            }
116
117            return new CheckInScmResult( commitedFiles, result );
118        }
119
120        return new CheckInScmResult( commitedFiles, result );
121    }
122
123
124}