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  
20  package org.apache.maven.scm.provider.bazaar.command.tag;
21  
22  import java.io.File;
23  
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmFileStatus;
27  import org.apache.maven.scm.ScmResult;
28  import org.apache.maven.scm.ScmTagParameters;
29  import org.apache.maven.scm.command.tag.AbstractTagCommand;
30  import org.apache.maven.scm.command.tag.TagScmResult;
31  import org.apache.maven.scm.provider.ScmProviderRepository;
32  import org.apache.maven.scm.provider.bazaar.BazaarUtils;
33  import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
34  import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
35  import org.apache.maven.scm.provider.bazaar.repository.BazaarScmProviderRepository;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  /**
39   * @author <a href="mailto:johan.walles@gmail.com">Johan Walles</a>
40   *
41   */
42  public class BazaarTagCommand extends AbstractTagCommand {
43      protected ScmResult executeTagCommand( ScmProviderRepository repository,
44              ScmFileSet fileSet, String tagName,
45              ScmTagParameters scmTagParameters ) throws ScmException
46      {
47          if ( tagName == null || StringUtils.isEmpty( tagName.trim() ) ) {
48              throw new ScmException( "tag name must be specified" );
49          }
50          
51          if ( !fileSet.getFileList().isEmpty() ) {
52              throw new ScmException( "tagging specific files is not allowed" );
53          }
54          
55          // Perform the tagging operation
56          File bazaarRoot = fileSet.getBasedir();
57          BazaarConsumer consumer = new BazaarConsumer( getLogger() );
58          String[] tagCmd = new String[] { BazaarConstants.TAG_CMD, tagName };
59          ScmResult tagResult = BazaarUtils.execute( consumer, getLogger(), bazaarRoot, tagCmd );
60          if ( !tagResult.isSuccess() ) {
61              return new TagScmResult( null, tagResult );
62          }
63  
64          // Do "bzr ls -R -r tag:tagName" to get a list of the tagged files
65          BazaarLsConsumer lsConsumer = 
66              new BazaarLsConsumer( getLogger(), bazaarRoot, ScmFileStatus.TAGGED );
67          String[] lsCmd = new String[] {
68                                         BazaarConstants.LS_CMD,
69                                         BazaarConstants.RECURSIVE_OPTION,
70                                         BazaarConstants.REVISION_OPTION,
71                                         "tag:" + tagName
72                                         };
73          ScmResult lsResult = BazaarUtils.execute(lsConsumer, getLogger(), bazaarRoot, lsCmd);
74          if ( !lsResult.isSuccess() ) {
75              return new TagScmResult( null, lsResult );
76          }
77          
78          // Push new tags to parent branch if any
79          BazaarScmProviderRepository bazaarRepository = (BazaarScmProviderRepository) repository;
80          if ( !bazaarRepository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) && repository.isPushChanges() )
81          {
82              String[] pushCmd = new String[] { BazaarConstants.PUSH_CMD, bazaarRepository.getURI() };
83              ScmResult pushResult =
84                  BazaarUtils.execute( new BazaarConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
85              if ( !pushResult.isSuccess() ) {
86                  return new TagScmResult( null, pushResult );
87              }
88          }
89          
90          return new TagScmResult( lsConsumer.getListedFiles(), tagResult );
91      }
92  }