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