001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.maven.scm.provider.bazaar.command.tag;
021    
022    import java.io.File;
023    
024    import org.apache.maven.scm.ScmException;
025    import org.apache.maven.scm.ScmFileSet;
026    import org.apache.maven.scm.ScmFileStatus;
027    import org.apache.maven.scm.ScmResult;
028    import org.apache.maven.scm.ScmTagParameters;
029    import org.apache.maven.scm.command.tag.AbstractTagCommand;
030    import org.apache.maven.scm.command.tag.TagScmResult;
031    import org.apache.maven.scm.provider.ScmProviderRepository;
032    import org.apache.maven.scm.provider.bazaar.BazaarUtils;
033    import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
034    import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
035    import org.apache.maven.scm.provider.bazaar.repository.BazaarScmProviderRepository;
036    import org.codehaus.plexus.util.StringUtils;
037    
038    /**
039     * @author <a href="mailto:johan.walles@gmail.com">Johan Walles</a>
040     * @version $Id: BazaarTagCommand.java 952830 2010-06-08 21:39:49Z olamy $
041     */
042    public class BazaarTagCommand extends AbstractTagCommand {
043        protected ScmResult executeTagCommand( ScmProviderRepository repository,
044                ScmFileSet fileSet, String tagName,
045                ScmTagParameters scmTagParameters ) throws ScmException
046        {
047            if ( tagName == null || StringUtils.isEmpty( tagName.trim() ) ) {
048                throw new ScmException( "tag name must be specified" );
049            }
050            
051            if ( !fileSet.getFileList().isEmpty() ) {
052                throw new ScmException( "tagging specific files is not allowed" );
053            }
054            
055            // Perform the tagging operation
056            File bazaarRoot = fileSet.getBasedir();
057            BazaarConsumer consumer = new BazaarConsumer( getLogger() );
058            String[] tagCmd = new String[] { BazaarConstants.TAG_CMD, tagName };
059            ScmResult tagResult = BazaarUtils.execute( consumer, getLogger(), bazaarRoot, tagCmd );
060            if ( !tagResult.isSuccess() ) {
061                return new TagScmResult( null, tagResult );
062            }
063    
064            // Do "bzr ls -R -r tag:tagName" to get a list of the tagged files
065            BazaarLsConsumer lsConsumer = 
066                new BazaarLsConsumer( getLogger(), bazaarRoot, ScmFileStatus.TAGGED );
067            String[] lsCmd = new String[] {
068                                           BazaarConstants.LS_CMD,
069                                           BazaarConstants.RECURSIVE_OPTION,
070                                           BazaarConstants.REVISION_OPTION,
071                                           "tag:" + tagName
072                                           };
073            ScmResult lsResult = BazaarUtils.execute(lsConsumer, getLogger(), bazaarRoot, lsCmd);
074            if ( !lsResult.isSuccess() ) {
075                return new TagScmResult( null, lsResult );
076            }
077            
078            // Push new tags to parent branch if any
079            BazaarScmProviderRepository bazaarRepository = (BazaarScmProviderRepository) repository;
080            if ( !bazaarRepository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) && repository.isPushChanges() )
081            {
082                String[] pushCmd = new String[] { BazaarConstants.PUSH_CMD, bazaarRepository.getURI() };
083                ScmResult pushResult =
084                    BazaarUtils.execute( new BazaarConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
085                if ( !pushResult.isSuccess() ) {
086                    return new TagScmResult( null, pushResult );
087                }
088            }
089            
090            return new TagScmResult( lsConsumer.getListedFiles(), tagResult );
091        }
092    }