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  public class HgUpdateCommand extends AbstractUpdateCommand implements Command {
50      /**
51       * {@inheritDoc}
52       */
53      protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag)
54              throws ScmException {
55          File workingDir = fileSet.getBasedir();
56  
57          String[] updateCmd;
58          // Update branch
59          if (repo.isPushChanges()) {
60              updateCmd = new String[] {
61                  HgCommandConstants.PULL_CMD,
62                  HgCommandConstants.REVISION_OPTION,
63                  tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip"
64              };
65          } else {
66              updateCmd = new String[] {
67                  HgCommandConstants.UPDATE_CMD,
68                  tag != null && !StringUtils.isEmpty(tag.getName()) ? tag.getName() : "tip",
69                  HgCommandConstants.CLEAN_OPTION
70              };
71          }
72          ScmResult updateResult = HgUtils.execute(new HgConsumer(), workingDir, updateCmd);
73  
74          if (!updateResult.isSuccess()) {
75              return new UpdateScmResult(null, null, updateResult);
76          }
77  
78          // Find changes from last revision
79          int currentRevision = HgUtils.getCurrentRevisionNumber(workingDir);
80          int previousRevision = currentRevision - 1;
81          String[] diffCmd =
82                  new String[] {HgCommandConstants.DIFF_CMD, HgCommandConstants.REVISION_OPTION, "" + previousRevision};
83          HgDiffConsumer diffConsumer = new HgDiffConsumer(workingDir);
84          ScmResult diffResult = HgUtils.execute(diffConsumer, workingDir, diffCmd);
85  
86          // Now translate between diff and update file status
87          List<ScmFile> updatedFiles = new ArrayList<>();
88          List<CharSequence> changes = new ArrayList<>();
89          List<ScmFile> diffFiles = diffConsumer.getChangedFiles();
90          Map<String, CharSequence> diffChanges = diffConsumer.getDifferences();
91          for (ScmFile file : diffFiles) {
92              changes.add(diffChanges.get(file.getPath()));
93              if (file.getStatus() == ScmFileStatus.MODIFIED) {
94                  updatedFiles.add(new ScmFile(file.getPath(), ScmFileStatus.PATCHED));
95              } else {
96                  updatedFiles.add(file);
97              }
98          }
99  
100         if (repo.isPushChanges()) {
101             String[] hgUpdateCmd = new String[] {HgCommandConstants.UPDATE_CMD};
102             HgUtils.execute(new HgConsumer(), workingDir, hgUpdateCmd);
103         }
104 
105         return new UpdateScmResultWithRevision(
106                 updatedFiles, new ArrayList<>(0), String.valueOf(currentRevision), diffResult);
107     }
108 
109     protected ChangeLogCommand getChangeLogCommand() {
110         return new HgChangeLogCommand();
111     }
112 }