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