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.branch;
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.ScmBranchParameters;
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.command.Command;
33 import org.apache.maven.scm.command.branch.AbstractBranchCommand;
34 import org.apache.maven.scm.command.branch.BranchScmResult;
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 public class HgBranchCommand extends AbstractBranchCommand implements Command {
49
50 protected ScmResult executeBranchCommand(
51 ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String branch, String message)
52 throws ScmException {
53 return executeBranchCommand(scmProviderRepository, fileSet, branch, new ScmBranchParameters(message));
54 }
55
56
57
58
59 protected ScmResult executeBranchCommand(
60 ScmProviderRepository scmProviderRepository,
61 ScmFileSet fileSet,
62 String branch,
63 ScmBranchParameters scmBranchParameters)
64 throws ScmException {
65
66 if (StringUtils.isBlank(branch)) {
67 throw new ScmException("branch must be specified");
68 }
69
70 if (!fileSet.getFileList().isEmpty()) {
71 throw new ScmException("This provider doesn't support branchging subsets of a directory");
72 }
73
74 File workingDir = fileSet.getBasedir();
75
76
77 String[] branchCmd = new String[] {HgCommandConstants.BRANCH_CMD, branch};
78
79
80 HgConsumer branchConsumer = new HgConsumer() {
81 public void doConsume(ScmFileStatus status, String trimmedLine) {
82
83 }
84 };
85
86 ScmResult result = HgUtils.execute(branchConsumer, workingDir, branchCmd);
87 HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
88
89 if (!result.isSuccess()) {
90 throw new ScmException("Error while executing command " + joinCmd(branchCmd));
91 }
92
93
94 String[] commitCmd = new String[] {
95 HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, scmBranchParameters.getMessage()
96 };
97
98 result = HgUtils.execute(new HgConsumer(), workingDir, commitCmd);
99
100 if (!result.isSuccess()) {
101 throw new ScmException("Error while executing command " + joinCmd(commitCmd));
102 }
103
104
105
106 if (repository.isPushChanges()) {
107 if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
108
109 String[] pushCmd = new String[] {
110 HgCommandConstants.PUSH_CMD, HgCommandConstants.NEW_BRANCH_OPTION, repository.getURI()
111 };
112
113 result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), pushCmd);
114
115 if (!result.isSuccess()) {
116 throw new ScmException("Error while executing command " + joinCmd(pushCmd));
117 }
118 }
119 }
120
121
122 String[] listCmd = new String[] {HgCommandConstants.INVENTORY_CMD};
123 HgListConsumer listconsumer = new HgListConsumer();
124
125 result = HgUtils.execute(listconsumer, fileSet.getBasedir(), listCmd);
126
127 if (!result.isSuccess()) {
128 throw new ScmException("Error while executing command " + joinCmd(listCmd));
129 }
130
131 List<ScmFile> files = listconsumer.getFiles();
132 List<ScmFile> fileList = new ArrayList<>();
133 for (ScmFile f : files) {
134 fileList.add(new ScmFile(f.getPath(), ScmFileStatus.TAGGED));
135 }
136
137 return new BranchScmResult(fileList, result);
138 }
139
140 private String joinCmd(String[] cmd) {
141 StringBuilder result = new StringBuilder();
142 for (int i = 0; i < cmd.length; i++) {
143 String s = cmd[i];
144 result.append(s);
145 if (i < cmd.length - 1) {
146 result.append(" ");
147 }
148 }
149 return result.toString();
150 }
151 }