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.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   * Tag
44   *
45   * @author <a href="mailto:ryan@darksleep.com">ryan daum</a>
46   * @author Olivier Lamy
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       * {@inheritDoc}
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          // build the command
79          String[] tagCmd = new String[] {
80              HgCommandConstants.TAG_CMD, HgCommandConstants.MESSAGE_OPTION, scmTagParameters.getMessage(), tag
81          };
82  
83          // keep the command about in string form for reporting
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              // now push
90              // Push to parent branch if any
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         // do an inventory to return the files tagged (all of them)
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 }