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