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  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       * {@inheritDoc}
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          // build the command
77          String[] branchCmd = new String[] {HgCommandConstants.BRANCH_CMD, branch};
78  
79          // keep the command about in string form for reporting
80          HgConsumer branchConsumer = new HgConsumer() {
81              public void doConsume(ScmFileStatus status, String trimmedLine) {
82                  // noop
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          // First commit.
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         // now push, if we should.
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         // do an inventory to return the files branched (all of them)
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 }