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.tag;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.scm.ScmException;
26 import org.apache.maven.scm.ScmFile;
27 import org.apache.maven.scm.ScmFileSet;
28 import org.apache.maven.scm.ScmFileStatus;
29 import org.apache.maven.scm.ScmResult;
30 import org.apache.maven.scm.ScmTagParameters;
31 import org.apache.maven.scm.command.Command;
32 import org.apache.maven.scm.command.tag.AbstractTagCommand;
33 import org.apache.maven.scm.command.tag.TagScmResult;
34 import org.apache.maven.scm.provider.ScmProviderRepository;
35 import org.apache.maven.scm.provider.hg.HgUtils;
36 import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
37 import org.apache.maven.scm.provider.hg.command.HgConsumer;
38 import org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer;
39 import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
40
41
42
43
44
45
46
47 public class HgTagCommand extends AbstractTagCommand implements Command {
48
49 protected ScmResult executeTagCommand(
50 ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag, String message)
51 throws ScmException {
52 return executeTagCommand(scmProviderRepository, fileSet, tag, new ScmTagParameters(message));
53 }
54
55
56
57
58 protected ScmResult executeTagCommand(
59 ScmProviderRepository scmProviderRepository,
60 ScmFileSet fileSet,
61 String tag,
62 ScmTagParameters scmTagParameters)
63 throws ScmException {
64
65 if (tag == null || tag.trim().isEmpty()) {
66 throw new ScmException("tag must be specified");
67 }
68
69 if (!fileSet.getFileList().isEmpty()) {
70 throw new ScmException(
71 "This provider doesn't support tagging subsets of a directory : " + fileSet.getFileList());
72 }
73
74 File workingDir = fileSet.getBasedir();
75
76
77 String[] tagCmd = new String[] {
78 HgCommandConstants.TAG_CMD, HgCommandConstants.MESSAGE_OPTION, scmTagParameters.getMessage(), tag
79 };
80
81
82 StringBuilder cmd = joinCmd(tagCmd);
83 HgTagConsumer consumer = new HgTagConsumer();
84 ScmResult result = HgUtils.execute(consumer, workingDir, tagCmd);
85 HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
86 if (result.isSuccess()) {
87
88
89
90 if (repository.isPushChanges()) {
91 if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
92 String branchName = HgUtils.getCurrentBranchName(workingDir);
93 boolean differentOutgoingBranch = HgUtils.differentOutgoingBranchFound(workingDir, branchName);
94
95 String[] pushCmd = new String[] {
96 HgCommandConstants.PUSH_CMD,
97 differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
98 repository.getURI()
99 };
100
101 result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), pushCmd);
102 }
103 }
104 } else {
105 throw new ScmException("Error while executing command " + cmd.toString());
106 }
107
108
109 String[] listCmd = new String[] {HgCommandConstants.INVENTORY_CMD};
110 HgListConsumer listconsumer = new HgListConsumer();
111 result = HgUtils.execute(listconsumer, fileSet.getBasedir(), listCmd);
112 if (result.isSuccess()) {
113 List<ScmFile> files = listconsumer.getFiles();
114 List<ScmFile> fileList = new ArrayList<>();
115 for (ScmFile f : files) {
116 if (!f.getPath().endsWith(".hgtags")) {
117 fileList.add(new ScmFile(f.getPath(), ScmFileStatus.TAGGED));
118 }
119 }
120
121 return new TagScmResult(fileList, result);
122 } else {
123 throw new ScmException("Error while executing command " + cmd.toString());
124 }
125 }
126
127 private StringBuilder joinCmd(String[] cmd) {
128 StringBuilder result = new StringBuilder();
129 for (int i = 0; i < cmd.length; i++) {
130 String s = cmd[i];
131 result.append(s);
132 if (i < cmd.length - 1) {
133 result.append(" ");
134 }
135 }
136 return result;
137 }
138 }