001package org.apache.maven.scm.provider.starteam.command.diff;
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.diff.AbstractDiffCommand;
026import org.apache.maven.scm.command.diff.DiffScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
029import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
030import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
031import org.codehaus.plexus.util.StringUtils;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.util.ArrayList;
036import java.util.List;
037
038/**
039 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
040 *
041 */
042public class StarteamDiffCommand
043    extends AbstractDiffCommand
044    implements StarteamCommand
045{
046    // ----------------------------------------------------------------------
047    // AbstractDiffCommand Implementation
048    // ----------------------------------------------------------------------
049
050    /** {@inheritDoc} */
051    protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion,
052                                                ScmVersion endVersion )
053        throws ScmException
054    {
055        if ( getLogger().isInfoEnabled() )
056        {
057            getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
058        }
059
060        if ( fileSet.getFileList().isEmpty() )
061        {
062            throw new ScmException( "This provider doesn't support diff command on a subsets of a directory" );
063        }
064
065        StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
066
067        StarteamDiffConsumer consumer = new StarteamDiffConsumer( getLogger(), fileSet.getBasedir() );
068
069        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
070
071        Commandline cl = createCommandLine( repository, fileSet, startVersion, endVersion );
072
073        int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
074
075        if ( exitCode != 0 )
076        {
077            return new DiffScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
078        }
079
080        return new DiffScmResult( cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
081                                  consumer.getPatch() );
082    }
083
084    // ----------------------------------------------------------------------
085    //
086    // ----------------------------------------------------------------------
087
088    public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet workingDirectory,
089                                                 ScmVersion startLabel, ScmVersion endLabel )
090        throws ScmException
091    {
092
093        List<String> args = new ArrayList<String>();
094
095        args.add( "-filter" );
096        args.add( "M" );
097
098        if ( startLabel != null && StringUtils.isNotEmpty( startLabel.getName() ) )
099        {
100            args.add( "-vl" );
101
102            args.add( startLabel.getName() );
103        }
104
105        if ( endLabel != null && StringUtils.isNotEmpty( endLabel.getName() ) )
106        {
107            args.add( "-vl" );
108
109            args.add( endLabel.getName() );
110        }
111
112        if ( endLabel != null && ( startLabel == null || StringUtils.isEmpty( startLabel.getName() ) ) )
113        {
114            throw new ScmException( "Missing start label." );
115        }
116
117        StarteamCommandLineUtils.addEOLOption( args );
118
119        return StarteamCommandLineUtils.createStarteamCommandLine( "diff", args, workingDirectory, repo );
120    }
121}