001package org.apache.maven.scm.provider.vss.commands.changelog;
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.ScmBranch;
023import org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
026import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
027import org.apache.maven.scm.command.changelog.ChangeLogSet;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
030import org.apache.maven.scm.provider.vss.commands.VssConstants;
031import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.text.SimpleDateFormat;
036import java.util.Date;
037import java.util.Locale;
038
039/**
040 * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
041 * @author Olivier Lamy
042 *
043 */
044public class VssHistoryCommand
045    extends AbstractChangeLogCommand
046{
047    /** {@inheritDoc} */
048    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
049                                                          Date startDate, Date endDate, ScmBranch branch,
050                                                          String datePattern )
051        throws ScmException
052    {
053        VssScmProviderRepository repo = (VssScmProviderRepository) repository;
054
055        Commandline cl = buildCmdLine( repo, fileSet, startDate, endDate );
056
057        if ( getLogger().isInfoEnabled() )
058        {
059            getLogger().info( "Executing: " + cl );
060            getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
061        }
062
063        VssChangeLogConsumer consumer = new VssChangeLogConsumer( repo, datePattern, getLogger() );
064
065        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
066
067        int exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
068
069        if ( exitCode != 0 )
070        {
071            return new ChangeLogScmResult( cl.toString(), "The vss command failed.", stderr.getOutput(), false );
072        }
073
074        return new ChangeLogScmResult( cl.toString(),
075                                       new ChangeLogSet( consumer.getModifications(), startDate, endDate ) );
076    }
077
078    public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, Date startDate, Date endDate )
079        throws ScmException
080    {
081        Commandline command = new Commandline();
082
083        command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
084
085        try
086        {
087            command.addSystemEnvironment();
088        }
089        catch ( Exception e )
090        {
091            throw new ScmException( "Can't add system environment.", e );
092        }
093
094        command.addEnvironment( "SSDIR", repo.getVssdir() );
095
096        String ssDir = VssCommandLineUtils.getSsDir();
097
098        command.setExecutable( ssDir + VssConstants.SS_EXE );
099
100        command.createArg().setValue( VssConstants.COMMAND_HISTORY );
101
102        command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
103
104        //User identification to get access to vss repository
105        if ( repo.getUserPassword() != null )
106        {
107            command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
108        }
109
110        //Display the history of an entire project list
111        command.createArg().setValue( VssConstants.FLAG_RECURSION );
112
113        //Ignore: Do not ask for input under any circumstances.
114        command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
115
116        //Display only versions that fall within specified data range.
117        if ( startDate != null )
118        {
119            if ( endDate == null )
120            {
121                endDate = new Date(); // = now
122            }
123
124            SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy", Locale.ENGLISH );
125            String dateRange = sdf.format( endDate ) + "~" + sdf.format( startDate );
126            command.createArg().setValue( VssConstants.FLAG_VERSION_DATE + dateRange );
127        }
128        return command;
129    }
130}