001    package 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    
022    import java.io.File;
023    import java.util.ArrayList;
024    import java.util.Iterator;
025    import java.util.List;
026    import java.util.Map;
027    
028    import org.apache.maven.scm.ChangeSet;
029    import org.apache.maven.scm.ScmException;
030    import org.apache.maven.scm.ScmFile;
031    import org.apache.maven.scm.ScmFileSet;
032    import org.apache.maven.scm.ScmFileStatus;
033    import org.apache.maven.scm.ScmResult;
034    import org.apache.maven.scm.ScmVersion;
035    import org.apache.maven.scm.command.Command;
036    import org.apache.maven.scm.command.changelog.ChangeLogCommand;
037    import org.apache.maven.scm.command.update.AbstractUpdateCommand;
038    import org.apache.maven.scm.command.update.UpdateScmResult;
039    import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
040    import org.apache.maven.scm.provider.ScmProviderRepository;
041    import org.apache.maven.scm.provider.bazaar.BazaarUtils;
042    import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
043    import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
044    import org.apache.maven.scm.provider.bazaar.command.changelog.BazaarChangeLogCommand;
045    import org.apache.maven.scm.provider.bazaar.command.diff.BazaarDiffConsumer;
046    import org.codehaus.plexus.util.StringUtils;
047    
048    /**
049     * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
050     * @version $Id: BazaarUpdateCommand.java 1056980 2011-01-09 17:23:55Z olamy $
051     */
052    public 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), String.valueOf( currentRevision ), diffResult );
107        }
108    
109        /** {@inheritDoc} */
110        protected ChangeLogCommand getChangeLogCommand()
111        {
112            BazaarChangeLogCommand command = new BazaarChangeLogCommand();
113            command.setLogger( getLogger() );
114            return command;
115        }
116    }