001package org.apache.maven.scm.provider.vss.commands.checkout;
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 org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmVersion;
025import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
026import org.apache.maven.scm.command.checkout.CheckOutScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
029import org.apache.maven.scm.provider.vss.commands.VssConstants;
030import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
031import org.codehaus.plexus.util.cli.CommandLineUtils;
032import org.codehaus.plexus.util.cli.Commandline;
033
034/**
035 * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
036 */
037public class VssCheckOutCommand
038    extends AbstractCheckOutCommand
039{
040
041    /** {@inheritDoc} */
042    protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repository, ScmFileSet fileSet,
043                                                       ScmVersion version, boolean recursive, boolean shallow )
044        throws ScmException
045    {
046        if ( getLogger().isDebugEnabled() )
047        {
048            getLogger().debug( "executing checkout command..." );
049        }
050
051        VssScmProviderRepository repo = (VssScmProviderRepository) repository;
052
053        Commandline cl = buildCmdLine( repo, fileSet, version );
054
055        VssCheckOutConsumer consumer = new VssCheckOutConsumer( repo, getLogger() );
056
057        //      TODO handle deleted files from VSS
058        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
059
060        int exitCode;
061
062        if ( getLogger().isDebugEnabled() )
063        {
064            getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
065        }
066
067        exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
068
069        if ( exitCode != 0 )
070        {
071            String error = stderr.getOutput();
072
073            if ( getLogger().isDebugEnabled() )
074            {
075                getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
076            }
077            if ( error.indexOf( "A writable copy of" ) < 0 )
078            {
079                return new CheckOutScmResult( cl.toString(), "The vss command failed.", error, false );
080            }
081            // print out the writable copy for manual handling
082            if ( getLogger().isWarnEnabled() )
083            {
084                getLogger().warn( error );
085            }
086        }
087
088        return new CheckOutScmResult( cl.toString(), consumer.getUpdatedFiles() );
089    }
090
091    public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
092        throws ScmException
093    {
094
095        Commandline command = new Commandline();
096
097        command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
098
099        try
100        {
101            command.addSystemEnvironment();
102        }
103        catch ( Exception e )
104        {
105            throw new ScmException( "Can't add system environment.", e );
106        }
107
108        command.addEnvironment( "SSDIR", repo.getVssdir() );
109
110        String ssDir = VssCommandLineUtils.getSsDir();
111
112        command.setExecutable( ssDir + VssConstants.SS_EXE );
113
114        command.createArg().setValue( VssConstants.COMMAND_GET );
115
116        command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
117
118        //User identification to get access to vss repository
119        if ( repo.getUserPassword() != null )
120        {
121            command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
122        }
123
124        //Display the history of an entire project list
125        command.createArg().setValue( VssConstants.FLAG_RECURSION );
126
127        //Ignore: Do not ask for input under any circumstances.
128        command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
129
130        //Ignore: Do not touch local writable files.
131        command.createArg().setValue( VssConstants.FLAG_REPLACE_WRITABLE );
132
133        if ( version != null )
134        {
135            command.createArg().setValue( VssConstants.FLAG_VERSION_LABEL + '"' + version + '"' );
136        }
137
138        return command;
139    }
140}