001package org.apache.maven.scm.provider.integrity.command.branch;
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 com.mks.api.response.APIException;
023import com.mks.api.response.Response;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFile;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.command.branch.AbstractBranchCommand;
029import org.apache.maven.scm.command.branch.BranchScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.integrity.ExceptionHandler;
032import org.apache.maven.scm.provider.integrity.Project;
033import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
034
035import java.util.ArrayList;
036
037/**
038 * MKS Integrity implementation for Maven's AbstractBranchCommand
039 * <br>For a normal and variant configuration, a fresh checkpoint is executed
040 * prior to creating a Development Path (branch).  In the case of a build
041 * configuration, the specified checkpoint revision is used to create
042 * the Development Path
043 *
044 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
045 * @version $Id: IntegrityBranchCommand.java 1.3 2011/08/22 13:06:17EDT Cletus D'Souza (dsouza) Exp  $
046 * @since 1.6
047 */
048public class IntegrityBranchCommand
049    extends AbstractBranchCommand
050{
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    public BranchScmResult executeBranchCommand( ScmProviderRepository repository, ScmFileSet fileSet,
056                                                 String branchName, String message )
057        throws ScmException
058    {
059        BranchScmResult result;
060        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
061        Project siProject = iRepo.getProject();
062        getLogger().info(
063            "Attempting to branch project " + siProject.getProjectName() + " using branch name '" + branchName + "'" );
064        try
065        {
066            Project.validateTag( branchName );
067            Response res = siProject.createDevPath( branchName );
068            int exitCode = res.getExitCode();
069            boolean success = ( exitCode == 0 ? true : false );
070            ScmResult scmResult = new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode, success );
071            result = new BranchScmResult( new ArrayList<ScmFile>(), scmResult );
072        }
073        catch ( APIException aex )
074        {
075            ExceptionHandler eh = new ExceptionHandler( aex );
076            getLogger().error( "MKS API Exception: " + eh.getMessage() );
077            getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
078            result = new BranchScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
079        }
080        catch ( Exception e )
081        {
082            getLogger().error( "Failed to checkpoint project! " + e.getMessage() );
083            result = new BranchScmResult( "si createdevpath", e.getMessage(), "", false );
084        }
085
086        return result;
087    }
088
089}