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 public class HgUpdateCommand extends AbstractUpdateCommand implements Command {
50
51
52
53 protected UpdateScmResult executeUpdateCommand(ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion tag)
54 throws ScmException {
55 File workingDir = fileSet.getBasedir();
56
57 String[] updateCmd;
58
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
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
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 }