View Javadoc
1   package org.apache.maven.scm.provider.bazaar.command.tag;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
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
43      extends AbstractTagCommand
44  {
45      protected ScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
46                                             ScmTagParameters scmTagParameters )
47          throws ScmException
48      {
49          if ( tagName == null || StringUtils.isEmpty( tagName.trim() ) )
50          {
51              throw new ScmException( "tag name must be specified" );
52          }
53  
54          if ( !fileSet.getFileList().isEmpty() )
55          {
56              throw new ScmException( "tagging specific files is not allowed" );
57          }
58  
59          // Perform the tagging operation
60          File bazaarRoot = fileSet.getBasedir();
61          BazaarConsumer consumer = new BazaarConsumer( getLogger() );
62          String[] tagCmd = new String[] { BazaarConstants.TAG_CMD, tagName };
63          ScmResult tagResult = BazaarUtils.execute( consumer, getLogger(), bazaarRoot, tagCmd );
64          if ( !tagResult.isSuccess() )
65          {
66              return new TagScmResult( null, tagResult );
67          }
68  
69          // Do "bzr ls -R -r tag:tagName" to get a list of the tagged files
70          BazaarLsConsumer lsConsumer = new BazaarLsConsumer( getLogger(), bazaarRoot, ScmFileStatus.TAGGED );
71          String[] lsCmd = new String[] {
72                                         BazaarConstants.LS_CMD,
73                                         BazaarConstants.RECURSIVE_OPTION,
74                                         BazaarConstants.REVISION_OPTION,
75                                         "tag:" + tagName
76                                         };
77          ScmResult lsResult = BazaarUtils.execute( lsConsumer, getLogger(), bazaarRoot, lsCmd );
78          if ( !lsResult.isSuccess() )
79          {
80              return new TagScmResult( null, lsResult );
81          }
82  
83          // Push new tags to parent branch if any
84          BazaarScmProviderRepository bazaarRepository = (BazaarScmProviderRepository) repository;
85          if ( !bazaarRepository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) && repository.isPushChanges() )
86          {
87              String[] pushCmd = new String[] { BazaarConstants.PUSH_CMD, bazaarRepository.getURI() };
88              ScmResult pushResult =
89                  BazaarUtils.execute( new BazaarConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
90              if ( !pushResult.isSuccess() )
91              {
92                  return new TagScmResult( null, pushResult );
93              }
94          }
95  
96          return new TagScmResult( lsConsumer.getListedFiles(), tagResult );
97      }
98  }