1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
47
48
49
50 public class HgUpdateCommand extends AbstractUpdateCommand implements Command {
51
52 protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag)
53 throws ScmException {
54 File workingDir = fileSet.getBasedir();
55
56 String[] updateCmd;
57
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
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
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 }