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.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmFile;
27  import org.apache.maven.scm.ScmFileSet;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.ScmResult;
30  import org.apache.maven.scm.ScmTagParameters;
31  import org.apache.maven.scm.command.Command;
32  import org.apache.maven.scm.command.tag.AbstractTagCommand;
33  import org.apache.maven.scm.command.tag.TagScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.hg.HgUtils;
36  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
37  import org.apache.maven.scm.provider.hg.command.HgConsumer;
38  import org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer;
39  import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
40  
41  /**
42   * Tag
43   *
44   * @author <a href="mailto:ryan@darksleep.com">ryan daum</a>
45   * @author Olivier Lamy
46   *
47   */
48  public class HgTagCommand extends AbstractTagCommand implements Command {
49  
50      protected ScmResult executeTagCommand(
51              ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag, String message)
52              throws ScmException {
53          return executeTagCommand(scmProviderRepository, fileSet, tag, new ScmTagParameters(message));
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      protected ScmResult executeTagCommand(
60              ScmProviderRepository scmProviderRepository,
61              ScmFileSet fileSet,
62              String tag,
63              ScmTagParameters scmTagParameters)
64              throws ScmException {
65  
66          if (tag == null || tag.trim().isEmpty()) {
67              throw new ScmException("tag must be specified");
68          }
69  
70          if (!fileSet.getFileList().isEmpty()) {
71              throw new ScmException(
72                      "This provider doesn't support tagging subsets of a directory : " + fileSet.getFileList());
73          }
74  
75          File workingDir = fileSet.getBasedir();
76  
77          // build the command
78          String[] tagCmd = new String[] {
79              HgCommandConstants.TAG_CMD, HgCommandConstants.MESSAGE_OPTION, scmTagParameters.getMessage(), tag
80          };
81  
82          // keep the command about in string form for reporting
83          StringBuilder cmd = joinCmd(tagCmd);
84          HgTagConsumer consumer = new HgTagConsumer();
85          ScmResult result = HgUtils.execute(consumer, workingDir, tagCmd);
86          HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
87          if (result.isSuccess()) {
88              // now push
89              // Push to parent branch if any
90  
91              if (repository.isPushChanges()) {
92                  if (!repository.getURI().equals(fileSet.getBasedir().getAbsolutePath())) {
93                      String branchName = HgUtils.getCurrentBranchName(workingDir);
94                      boolean differentOutgoingBranch = HgUtils.differentOutgoingBranchFound(workingDir, branchName);
95  
96                      String[] pushCmd = new String[] {
97                          HgCommandConstants.PUSH_CMD,
98                          differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
99                          repository.getURI()
100                     };
101 
102                     result = HgUtils.execute(new HgConsumer(), fileSet.getBasedir(), pushCmd);
103                 }
104             }
105         } else {
106             throw new ScmException("Error while executing command " + cmd.toString());
107         }
108 
109         // do an inventory to return the files tagged (all of them)
110         String[] listCmd = new String[] {HgCommandConstants.INVENTORY_CMD};
111         HgListConsumer listconsumer = new HgListConsumer();
112         result = HgUtils.execute(listconsumer, fileSet.getBasedir(), listCmd);
113         if (result.isSuccess()) {
114             List<ScmFile> files = listconsumer.getFiles();
115             List<ScmFile> fileList = new ArrayList<>();
116             for (ScmFile f : files) {
117                 if (!f.getPath().endsWith(".hgtags")) {
118                     fileList.add(new ScmFile(f.getPath(), ScmFileStatus.TAGGED));
119                 }
120             }
121 
122             return new TagScmResult(fileList, result);
123         } else {
124             throw new ScmException("Error while executing command " + cmd.toString());
125         }
126     }
127 
128     private StringBuilder joinCmd(String[] cmd) {
129         StringBuilder result = new StringBuilder();
130         for (int i = 0; i < cmd.length; i++) {
131             String s = cmd[i];
132             result.append(s);
133             if (i < cmd.length - 1) {
134                 result.append(" ");
135             }
136         }
137         return result;
138     }
139 }