001package org.apache.maven.scm.provider.hg.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.List;
025import java.util.Map;
026
027import org.apache.maven.scm.ChangeSet;
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileSet;
031import org.apache.maven.scm.ScmFileStatus;
032import org.apache.maven.scm.ScmResult;
033import org.apache.maven.scm.ScmVersion;
034import org.apache.maven.scm.command.Command;
035import org.apache.maven.scm.command.changelog.ChangeLogCommand;
036import org.apache.maven.scm.command.update.AbstractUpdateCommand;
037import org.apache.maven.scm.command.update.UpdateScmResult;
038import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
039import org.apache.maven.scm.provider.ScmProviderRepository;
040import org.apache.maven.scm.provider.hg.HgUtils;
041import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
042import org.apache.maven.scm.provider.hg.command.HgConsumer;
043import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand;
044import org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer;
045import org.codehaus.plexus.util.StringUtils;
046
047/**
048 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
049 * @author Olivier Lamy
050 *
051 */
052public class HgUpdateCommand
053    extends AbstractUpdateCommand
054    implements Command
055{
056    /** {@inheritDoc} */
057    protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag )
058        throws ScmException
059    {
060        File workingDir = fileSet.getBasedir();
061
062        String[] updateCmd;
063        // Update branch
064        if ( repo.isPushChanges() )
065        {
066            updateCmd =
067                new String[] { HgCommandConstants.PULL_CMD, HgCommandConstants.REVISION_OPTION,
068                    tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip" };
069        }
070        else
071        {
072            updateCmd =
073                new String[] { HgCommandConstants.UPDATE_CMD,
074                    tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip",
075                    HgCommandConstants.CLEAN_OPTION };
076        }
077        ScmResult updateResult = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, updateCmd );
078
079        if ( !updateResult.isSuccess() )
080        {
081            return new UpdateScmResult( null, null, updateResult );
082        }
083
084        // Find changes from last revision
085        int currentRevision = HgUtils.getCurrentRevisionNumber( getLogger(), workingDir );
086        int previousRevision = currentRevision - 1;
087        String[] diffCmd = new String[] {
088            HgCommandConstants.DIFF_CMD,
089            HgCommandConstants.REVISION_OPTION,
090            "" + previousRevision };
091        HgDiffConsumer diffConsumer = new HgDiffConsumer( getLogger(), workingDir );
092        ScmResult diffResult = HgUtils.execute( diffConsumer, getLogger(), workingDir, diffCmd );
093
094        // Now translate between diff and update file status
095        List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
096        List<CharSequence> changes = new ArrayList<CharSequence>();
097        List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
098        Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
099        for ( ScmFile file : diffFiles )
100        {
101            changes.add( diffChanges.get( file.getPath() ) );
102            if ( file.getStatus() == ScmFileStatus.MODIFIED )
103            {
104                updatedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.PATCHED ) );
105            }
106            else
107            {
108                updatedFiles.add( file );
109            }
110        }
111        
112        if ( repo.isPushChanges() )
113        {
114            String[] hgUpdateCmd = new String[] { HgCommandConstants.UPDATE_CMD };
115            HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, hgUpdateCmd );
116        }
117
118        return new UpdateScmResultWithRevision( updatedFiles, new ArrayList<ChangeSet>( 0 ),
119                                                String.valueOf( currentRevision ), diffResult );
120    }
121
122    protected ChangeLogCommand getChangeLogCommand()
123    {
124        HgChangeLogCommand command = new HgChangeLogCommand();
125        command.setLogger( getLogger() );
126        return command;
127    }
128}