001package org.apache.maven.scm.provider.bazaar.command.update;
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.Iterator;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.maven.scm.ChangeSet;
029import org.apache.maven.scm.ScmException;
030import org.apache.maven.scm.ScmFile;
031import org.apache.maven.scm.ScmFileSet;
032import org.apache.maven.scm.ScmFileStatus;
033import org.apache.maven.scm.ScmResult;
034import org.apache.maven.scm.ScmVersion;
035import org.apache.maven.scm.command.Command;
036import org.apache.maven.scm.command.changelog.ChangeLogCommand;
037import org.apache.maven.scm.command.update.AbstractUpdateCommand;
038import org.apache.maven.scm.command.update.UpdateScmResult;
039import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
040import org.apache.maven.scm.provider.ScmProviderRepository;
041import org.apache.maven.scm.provider.bazaar.BazaarUtils;
042import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
043import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
044import org.apache.maven.scm.provider.bazaar.command.changelog.BazaarChangeLogCommand;
045import org.apache.maven.scm.provider.bazaar.command.diff.BazaarDiffConsumer;
046import org.codehaus.plexus.util.StringUtils;
047
048/**
049 * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
050 *
051 */
052public class BazaarUpdateCommand
053    extends AbstractUpdateCommand
054    implements Command
055{
056
057    /** {@inheritDoc} */
058    protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
059        throws ScmException
060    {
061
062        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
063        {
064            throw new ScmException( "This provider can't handle tags." );
065        }
066
067        File workingDir = fileSet.getBasedir();
068
069        // Update branch
070        String[] updateCmd = new String[] { BazaarConstants.PULL_CMD };
071        ScmResult updateResult = BazaarUtils.execute( new BazaarConsumer( getLogger() ), getLogger(), workingDir,
072                                                      updateCmd );
073
074        if ( !updateResult.isSuccess() )
075        {
076            return new UpdateScmResult( null, null, updateResult );
077        }
078
079        // Find changes from last revision
080        int currentRevision = BazaarUtils.getCurrentRevisionNumber( getLogger(), workingDir );
081        int previousRevision = currentRevision - 1;
082        String[] diffCmd =
083            new String[] { BazaarConstants.DIFF_CMD, BazaarConstants.REVISION_OPTION, "" + previousRevision };
084        BazaarDiffConsumer diffConsumer = new BazaarDiffConsumer( getLogger(), workingDir );
085        ScmResult diffResult = BazaarUtils.execute( diffConsumer, getLogger(), workingDir, diffCmd );
086
087        // Now translate between diff and update file status
088        List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
089        List<CharSequence> changes = new ArrayList<CharSequence>();
090        List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
091        Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
092        for ( Iterator<ScmFile> it = diffFiles.iterator(); it.hasNext(); )
093        {
094            ScmFile file = it.next();
095            changes.add( diffChanges.get( file ) );
096            if ( file.getStatus() == ScmFileStatus.MODIFIED )
097            {
098                updatedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.PATCHED ) );
099            }
100            else
101            {
102                updatedFiles.add( file );
103            }
104        }
105
106        return new UpdateScmResultWithRevision( updatedFiles, new ArrayList<ChangeSet>( 0 ),
107                                                String.valueOf( currentRevision ), diffResult );
108    }
109
110    /** {@inheritDoc} */
111    protected ChangeLogCommand getChangeLogCommand()
112    {
113        BazaarChangeLogCommand command = new BazaarChangeLogCommand();
114        command.setLogger( getLogger() );
115        return command;
116    }
117}