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              updateCmd = new String[] {
66                      HgCommandConstants.PULL_CMD,
67                      HgCommandConstants.REVISION_OPTION,
68                      tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip" };
69          } else {
70              updateCmd = new String[] {
71                      HgCommandConstants.UPDATE_CMD,
72                      tag != null && !StringUtils.isEmpty( tag.getName() ) ? tag.getName() : "tip",
73                      HgCommandConstants.CLEAN_OPTION };
74          }
75          ScmResult updateResult = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, updateCmd );
76  
77          if ( !updateResult.isSuccess() )
78          {
79              return new UpdateScmResult( null, null, updateResult );
80          }
81  
82          // Find changes from last revision
83          int currentRevision = HgUtils.getCurrentRevisionNumber( getLogger(), workingDir );
84          int previousRevision = currentRevision - 1;
85          String[] diffCmd = new String[] {
86              HgCommandConstants.DIFF_CMD,
87              HgCommandConstants.REVISION_OPTION,
88              "" + previousRevision };
89          HgDiffConsumer diffConsumer = new HgDiffConsumer( getLogger(), workingDir );
90          ScmResult diffResult = HgUtils.execute( diffConsumer, getLogger(), workingDir, diffCmd );
91  
92          // Now translate between diff and update file status
93          List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
94          List<CharSequence> changes = new ArrayList<CharSequence>();
95          List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
96          Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
97          for ( ScmFile file : diffFiles )
98          {
99              changes.add( diffChanges.get( file.getPath() ) );
100             if ( file.getStatus() == ScmFileStatus.MODIFIED )
101             {
102                 updatedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.PATCHED ) );
103             }
104             else
105             {
106                 updatedFiles.add( file );
107             }
108         }
109         
110         if ( repo.isPushChanges() ) {
111 	        String[] hgUpdateCmd = new String[] { HgCommandConstants.UPDATE_CMD };
112 	        HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, hgUpdateCmd );
113         }
114 
115         return new UpdateScmResultWithRevision( updatedFiles, new ArrayList<ChangeSet>(0), String.valueOf( currentRevision ), diffResult );
116     }
117 
118     protected ChangeLogCommand getChangeLogCommand()
119     {
120         HgChangeLogCommand command = new HgChangeLogCommand();
121         command.setLogger( getLogger() );
122         return command;
123     }
124 }