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 = repo.isPushChanges() ? HgUtils.differentOutgoingBranchFound( getLogger(), workingDir, branchName ) : false;
067
068        // Get files that will be committed (if not specified in fileSet)
069        List<ScmFile> commitedFiles = new ArrayList<ScmFile>();
070        List<File> files = fileSet.getFileList();
071        if ( files.isEmpty() )
072        { //Either commit all changes
073            HgStatusCommand statusCmd = new HgStatusCommand();
074            statusCmd.setLogger( getLogger() );
075            StatusScmResult status = statusCmd.executeStatusCommand( repo, fileSet );
076            List<ScmFile> statusFiles = status.getChangedFiles();
077            for ( ScmFile file : statusFiles )
078            {
079                if ( file.getStatus() == ScmFileStatus.ADDED || file.getStatus() == ScmFileStatus.DELETED ||
080                    file.getStatus() == ScmFileStatus.MODIFIED )
081                {
082                    commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
083                }
084            }
085
086        }
087        else
088        { //Or commit spesific files
089            for ( File file : files )
090            {
091                commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
092            }
093        }
094
095        // Commit to local branch
096        String[] commitCmd = new String[]{ HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, message };
097        commitCmd = HgUtils.expandCommandLine( commitCmd, fileSet );
098        ScmResult result =
099            HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), commitCmd );
100
101        // Push to parent branch if any
102        HgScmProviderRepository repository = (HgScmProviderRepository) repo;
103
104        if ( repo.isPushChanges() )
105        {
106
107            if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
108            {
109                String[] pushCmd = new String[]{ HgCommandConstants.PUSH_CMD,
110                    differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
111                    repository.getURI() };
112
113                result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
114            }
115
116            return new CheckInScmResult( commitedFiles, result );
117        }
118
119        return new CheckInScmResult( commitedFiles, result );
120    }
121
122
123}