View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Branch. Mercurial has weird branches. After a branch is created, it must be committed to the server, otherwise
44   * the branch does not exist (yet) in the repository.
45   *
46   * @author Henning Schmiedehausen
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       * {@inheritDoc}
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          // build the command
78          String[] branchCmd = new String[] {HgCommandConstants.BRANCH_CMD, branch};
79  
80          // keep the command about in string form for reporting
81          HgConsumer branchConsumer = new HgConsumer() {
82              public void doConsume(ScmFileStatus status, String trimmedLine) {
83                  // noop
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          // First commit.
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         // now push, if we should.
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         // do an inventory to return the files branched (all of them)
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<ScmFile>();
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 }