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