001package 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
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.bazaar.BazaarUtils;
037import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
038import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
039import org.apache.maven.scm.provider.bazaar.command.status.BazaarStatusCommand;
040import org.apache.maven.scm.provider.bazaar.repository.BazaarScmProviderRepository;
041import org.codehaus.plexus.util.StringUtils;
042
043/**
044 * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
045 * @author Olivier Lamy
046 *
047 */
048public 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}