View Javadoc
1   package org.apache.maven.scm.provider.hg.command.update;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.scm.ChangeSet;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFile;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmFileStatus;
32  import org.apache.maven.scm.ScmResult;
33  import org.apache.maven.scm.ScmVersion;
34  import org.apache.maven.scm.command.Command;
35  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
36  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
37  import org.apache.maven.scm.command.update.UpdateScmResult;
38  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
39  import org.apache.maven.scm.provider.ScmProviderRepository;
40  import org.apache.maven.scm.provider.hg.HgUtils;
41  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
42  import org.apache.maven.scm.provider.hg.command.HgConsumer;
43  import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand;
44  import org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer;
45  import org.codehaus.plexus.util.StringUtils;
46  
47  /**
48   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
49   * @author Olivier Lamy
50   *
51   */
52  public class HgUpdateCommand
53      extends AbstractUpdateCommand
54      implements Command
55  {
56      /** {@inheritDoc} */
57      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag )
58          throws ScmException
59      {
60          File workingDir = fileSet.getBasedir();
61  
62          String[] updateCmd;
63          // Update branch
64          if ( repo.isPushChanges() )
65          {
66              updateCmd =
67                  new String[] { HgCommandConstants.PULL_CMD, HgCommandConstants.REVISION_OPTION,
68                      tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip" };
69          }
70          else
71          {
72              updateCmd =
73                  new String[] { HgCommandConstants.UPDATE_CMD,
74                      tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip",
75                      HgCommandConstants.CLEAN_OPTION };
76          }
77          ScmResult updateResult = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, updateCmd );
78  
79          if ( !updateResult.isSuccess() )
80          {
81              return new UpdateScmResult( null, null, updateResult );
82          }
83  
84          // Find changes from last revision
85          int currentRevision = HgUtils.getCurrentRevisionNumber( getLogger(), workingDir );
86          int previousRevision = currentRevision - 1;
87          String[] diffCmd = new String[] {
88              HgCommandConstants.DIFF_CMD,
89              HgCommandConstants.REVISION_OPTION,
90              "" + previousRevision };
91          HgDiffConsumer diffConsumer = new HgDiffConsumer( getLogger(), workingDir );
92          ScmResult diffResult = HgUtils.execute( diffConsumer, getLogger(), workingDir, diffCmd );
93  
94          // Now translate between diff and update file status
95          List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
96          List<CharSequence> changes = new ArrayList<CharSequence>();
97          List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
98          Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
99          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 }