001    package org.apache.maven.scm.provider.perforce.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    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmVersion;
025    import org.apache.maven.scm.command.diff.AbstractDiffCommand;
026    import org.apache.maven.scm.command.diff.DiffScmResult;
027    import org.apache.maven.scm.provider.ScmProviderRepository;
028    import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
029    import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
030    import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
031    import org.codehaus.plexus.util.StringUtils;
032    import org.codehaus.plexus.util.cli.CommandLineException;
033    import org.codehaus.plexus.util.cli.CommandLineUtils;
034    import org.codehaus.plexus.util.cli.Commandline;
035    
036    import java.io.File;
037    
038    /**
039     * @author Mike Perham
040     * @version $Id: PerforceDiffCommand.java 1306867 2012-03-29 13:45:10Z olamy $
041     */
042    public class PerforceDiffCommand
043        extends AbstractDiffCommand
044        implements PerforceCommand
045    {
046        /** {@inheritDoc} */
047        protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet files, ScmVersion startRev,
048                                                    ScmVersion endRev )
049            throws ScmException
050        {
051            Commandline cl =
052                createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), startRev, endRev );
053            PerforceDiffConsumer consumer = new PerforceDiffConsumer();
054            if ( getLogger().isInfoEnabled() )
055            {
056                getLogger().info( "Executing: " + PerforceScmProvider.clean( cl.toString() ) );
057            }
058            boolean success = false;
059            try
060            {
061                CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
062                int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
063    
064                if ( exitCode != 0 )
065                {
066                    String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
067    
068                    StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
069                    msg.append( '\n' );
070                    msg.append( "Command line was:" + cmdLine );
071    
072                    throw new CommandLineException( msg.toString() );
073                }
074            }
075            catch ( CommandLineException e )
076            {
077                if ( getLogger().isErrorEnabled() )
078                {
079                    getLogger().error( "CommandLineException " + e.getMessage(), e );
080                }
081            }
082    
083            return new DiffScmResult( cl.toString(), success ? "Diff successful" : "Unable to diff", consumer
084                .getOutput(), success );
085        }
086    
087        public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
088                                                     ScmVersion startRev, ScmVersion endRev )
089        {
090            String start = startRev != null && StringUtils.isNotEmpty( startRev.getName() ) ? "@" + startRev.getName() : "";
091            String end = endRev != null && StringUtils.isNotEmpty( endRev.getName() ) ? endRev.getName() : "now";
092    
093            Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
094    
095            command.createArg().setValue( "diff2" );
096            command.createArg().setValue( "-u" );
097            // I'm assuming the "revs" are actually labels
098            command.createArg().setValue( "..." + start );
099            command.createArg().setValue( "...@" + end );
100            return command;
101        }
102    
103    }