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