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.ScmException;
28  import org.apache.maven.scm.ScmFile;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmFileStatus;
31  import org.apache.maven.scm.ScmResult;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.Command;
34  import org.apache.maven.scm.command.changelog.ChangeLogCommand;
35  import org.apache.maven.scm.command.update.AbstractUpdateCommand;
36  import org.apache.maven.scm.command.update.UpdateScmResult;
37  import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
38  import org.apache.maven.scm.provider.ScmProviderRepository;
39  import org.apache.maven.scm.provider.hg.HgUtils;
40  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
41  import org.apache.maven.scm.provider.hg.command.HgConsumer;
42  import org.apache.maven.scm.provider.hg.command.changelog.HgChangeLogCommand;
43  import org.apache.maven.scm.provider.hg.command.diff.HgDiffConsumer;
44  import org.codehaus.plexus.util.StringUtils;
45  
46  /**
47   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
48   * @author Olivier Lamy
49   *
50   */
51  public class HgUpdateCommand
52      extends AbstractUpdateCommand
53      implements Command
54  {
55      /** {@inheritDoc} */
56      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag )
57          throws ScmException
58      {
59          File workingDir = fileSet.getBasedir();
60  
61          String[] updateCmd;
62          // Update branch
63          if ( repo.isPushChanges() )
64          {
65              updateCmd =
66                  new String[] { HgCommandConstants.PULL_CMD, HgCommandConstants.REVISION_OPTION,
67                      tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip" };
68          }
69          else
70          {
71              updateCmd =
72                  new String[] { HgCommandConstants.UPDATE_CMD,
73                      tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip",
74                      HgCommandConstants.CLEAN_OPTION };
75          }
76          ScmResult updateResult = HgUtils.execute( new HgConsumer(), workingDir, updateCmd );
77  
78          if ( !updateResult.isSuccess() )
79          {
80              return new UpdateScmResult( null, null, updateResult );
81          }
82  
83          // Find changes from last revision
84          int currentRevision = HgUtils.getCurrentRevisionNumber( workingDir );
85          int previousRevision = currentRevision - 1;
86          String[] diffCmd = new String[] {
87              HgCommandConstants.DIFF_CMD,
88              HgCommandConstants.REVISION_OPTION,
89              "" + previousRevision };
90          HgDiffConsumer diffConsumer = new HgDiffConsumer( workingDir );
91          ScmResult diffResult = HgUtils.execute( diffConsumer, workingDir, diffCmd );
92  
93          // Now translate between diff and update file status
94          List<ScmFile> updatedFiles = new ArrayList<>();
95          List<CharSequence> changes = new ArrayList<>();
96          List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
97          Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
98          for ( ScmFile file : diffFiles )
99          {
100             changes.add( diffChanges.get( file.getPath() ) );
101             if ( file.getStatus() == ScmFileStatus.MODIFIED )
102             {
103                 updatedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.PATCHED ) );
104             }
105             else
106             {
107                 updatedFiles.add( file );
108             }
109         }
110         
111         if ( repo.isPushChanges() )
112         {
113             String[] hgUpdateCmd = new String[] { HgCommandConstants.UPDATE_CMD };
114             HgUtils.execute( new HgConsumer(), workingDir, hgUpdateCmd );
115         }
116 
117         return new UpdateScmResultWithRevision( updatedFiles, new ArrayList<>( 0 ),
118                                                 String.valueOf( currentRevision ), diffResult );
119     }
120 
121     protected ChangeLogCommand getChangeLogCommand()
122     {
123         return new HgChangeLogCommand();
124     }
125 }