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