001    package org.apache.maven.scm.provider.bazaar.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    
022    import java.io.File;
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.apache.maven.scm.ScmException;
027    import org.apache.maven.scm.ScmFile;
028    import org.apache.maven.scm.ScmFileSet;
029    import org.apache.maven.scm.ScmFileStatus;
030    import org.apache.maven.scm.ScmResult;
031    import org.apache.maven.scm.ScmVersion;
032    import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
033    import org.apache.maven.scm.command.checkin.CheckInScmResult;
034    import org.apache.maven.scm.command.status.StatusScmResult;
035    import org.apache.maven.scm.provider.ScmProviderRepository;
036    import org.apache.maven.scm.provider.bazaar.BazaarUtils;
037    import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
038    import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
039    import org.apache.maven.scm.provider.bazaar.command.status.BazaarStatusCommand;
040    import org.apache.maven.scm.provider.bazaar.repository.BazaarScmProviderRepository;
041    import org.codehaus.plexus.util.StringUtils;
042    
043    /**
044     * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
045     * @author Olivier Lamy
046     * @version $Id: BazaarCheckInCommand.java 1054455 2011-01-02 18:40:29Z olamy $
047     */
048    public class BazaarCheckInCommand
049        extends AbstractCheckInCommand
050    {
051        /** {@inheritDoc} */
052        protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
053                                                          ScmVersion version )
054            throws ScmException
055        {
056    
057            if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
058            {
059                throw new ScmException( "This provider can't handle tags." );
060            }
061    
062            // Get files that will be committed (if not specified in fileSet)
063            List<ScmFile> commitedFiles = new ArrayList<ScmFile>();
064            List<File> files = fileSet.getFileList();
065            if ( files.isEmpty() )
066            { //Either commit all changes
067                BazaarStatusCommand statusCmd = new BazaarStatusCommand();
068                statusCmd.setLogger( getLogger() );
069                StatusScmResult status = statusCmd.executeStatusCommand( repo, fileSet );
070                List<ScmFile> statusFiles = status.getChangedFiles();
071                for ( ScmFile file : statusFiles )
072                {
073                    if ( file.getStatus() == ScmFileStatus.ADDED || file.getStatus() == ScmFileStatus.DELETED
074                        || file.getStatus() == ScmFileStatus.MODIFIED )
075                    {
076                        commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
077                    }
078                }
079    
080            }
081            else
082            { //Or commit spesific files
083                for ( File file : files )
084                {
085                    commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
086                }
087            }
088    
089            // Commit to local branch
090            String[] commitCmd = new String[]{BazaarConstants.COMMIT_CMD, BazaarConstants.MESSAGE_OPTION, message};
091            commitCmd = BazaarUtils.expandCommandLine( commitCmd, fileSet );
092            ScmResult result =
093                BazaarUtils.execute( new BazaarConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), commitCmd );
094    
095            // Push to parent branch if any
096            BazaarScmProviderRepository repository = (BazaarScmProviderRepository) repo;
097            if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) && repo.isPushChanges() )
098            {
099                String[] pushCmd = new String[] { BazaarConstants.PUSH_CMD, BazaarConstants.NO_STRICT_OPTION, repository.getURI() };
100                result =
101                    BazaarUtils.execute( new BazaarConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
102            }
103    
104            return new CheckInScmResult( commitedFiles, result );
105        }
106    }